Move all modes to package kaka.cakelight.mode
[kaka/cakelight.git] / src / kaka / cakelight / CakeLight.java
1 package kaka.cakelight;
2
3 import kaka.cakelight.mode.Mode;
4 import org.jnativehook.GlobalScreen;
5 import org.jnativehook.NativeHookException;
6 import org.jnativehook.keyboard.NativeKeyAdapter;
7 import org.jnativehook.keyboard.NativeKeyEvent;
8 import org.jnativehook.mouse.NativeMouseEvent;
9 import org.jnativehook.mouse.NativeMouseMotionAdapter;
10
11 public class CakeLight {
12     private Configuration config;
13     private Mode mode;
14     private LedController ledController;
15
16     public CakeLight(Configuration config, LedController ledController) {
17         this.config = config;
18         this.ledController = ledController;
19         Color.calculateGammaCorrection(config.gamma);
20     }
21
22     public void setMode(Mode mode) {
23         cleanup();
24         this.mode = mode;
25         mode.setFrameListener(ledController::onFrame);
26         mode.enter(config);
27     }
28
29     public void cleanup() {
30         if (this.mode != null) {
31             this.mode.setFrameListener(ledFrame -> {}); // To avoid any frame being sent to the controller while the thread is exiting
32             this.mode.exit();
33         }
34     }
35
36     public void startLoop() {
37         Console.start(this, config);
38         initNativeHook();
39         // TODO
40 //        FrameGrabber grabber = FrameGrabber.from(config);
41 //        grabber.prepare();
42 //        VideoFrame frame = grabber.grabFrame();
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     }
52
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
73     public void turnOff() {
74         cleanup();
75         ledController.onFrame(LedFrame.from(config).fillColor(0, 0, 0));
76     }
77 }