Move all modes to package kaka.cakelight.mode
[kaka/cakelight.git] / src / kaka / cakelight / CakeLight.java
CommitLineData
4a2d6056
TW
1package kaka.cakelight;
2
67b0a758 3import kaka.cakelight.mode.Mode;
8ff7ee5a
TW
4import org.jnativehook.GlobalScreen;
5import org.jnativehook.NativeHookException;
6import org.jnativehook.keyboard.NativeKeyAdapter;
7import org.jnativehook.keyboard.NativeKeyEvent;
8import org.jnativehook.mouse.NativeMouseEvent;
9import org.jnativehook.mouse.NativeMouseMotionAdapter;
10
4a2d6056
TW
11public class CakeLight {
12 private Configuration config;
13 private Mode mode;
03b67a73 14 private LedController ledController;
4a2d6056 15
6b569670 16 public CakeLight(Configuration config, LedController ledController) {
4a2d6056 17 this.config = config;
6b569670 18 this.ledController = ledController;
38c759f8 19 Color.calculateGammaCorrection(config.gamma);
4a2d6056
TW
20 }
21
22 public void setMode(Mode mode) {
23 cleanup();
24 this.mode = mode;
d182b8cc 25 mode.setFrameListener(ledController::onFrame);
4a2d6056
TW
26 mode.enter(config);
27 }
28
29 public void cleanup() {
30 if (this.mode != null) {
6e568391 31 this.mode.setFrameListener(ledFrame -> {}); // To avoid any frame being sent to the controller while the thread is exiting
4a2d6056
TW
32 this.mode.exit();
33 }
34 }
35
36 public void startLoop() {
cd28f68c 37 Console.start(this, config);
8ff7ee5a 38 initNativeHook();
4a2d6056
TW
39 // TODO
40// FrameGrabber grabber = FrameGrabber.from(config);
41// grabber.prepare();
adc29b9a 42// VideoFrame frame = grabber.grabFrame();
4a2d6056
TW
43// double time = 0;
44// for (int i = 0; i < 100; i++) {
45// time += timeIt("frame", () -> grabber.grabFrame());
46// }
47// System.out.println("time = " + time);
48// grabber.close();
49// byte[] data = frame.getData();
50// saveFile(data, "/home/kaka/test.img");
51 }
a276d5ab 52
8ff7ee5a
TW
53 private void initNativeHook() {
54 try {
55 GlobalScreen.registerNativeHook();
56 GlobalScreen.addNativeKeyListener(new NativeKeyAdapter() {
57 @Override
58 public void nativeKeyPressed(NativeKeyEvent e) {
59 System.out.println("key code = " + e.getKeyCode() + ", key text = '" + NativeKeyEvent.getKeyText(e.getKeyCode()) + "'");
60 }
61 });
62 GlobalScreen.addNativeMouseMotionListener(new NativeMouseMotionAdapter() {
63 @Override
64 public void nativeMouseMoved(NativeMouseEvent e) {
65 System.out.println("mouse point = " + e.getPoint());
66 }
67 });
68 } catch (NativeHookException e) {
69 e.printStackTrace();
70 }
71 }
72
a276d5ab
TW
73 public void turnOff() {
74 cleanup();
6e568391 75 ledController.onFrame(LedFrame.from(config).fillColor(0, 0, 0));
a276d5ab 76 }
4a2d6056 77}