Use stack of modes instead of just one
[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 import java.util.Objects;
12 import java.util.Stack;
13
14 public class CakeLight {
15     private Configuration config;
16     private Stack<Mode> modes = new Stack<>();
17     private LedController ledController;
18
19     public CakeLight(Configuration config, LedController ledController) {
20         this.config = config;
21         this.ledController = ledController;
22         Color.calculateGammaCorrection(config.gamma);
23     }
24
25     public void setMode(Mode mode) {
26         cleanup();
27         pushMode(mode);
28     }
29
30     public void cleanup() {
31         while (popMode());
32     }
33
34     public void pushMode(Mode mode) {
35         Objects.requireNonNull(mode);
36         if (!modes.isEmpty()) {
37             stopMode(modes.peek());
38         }
39         modes.push(mode);
40         startMode(mode);
41         // TODO: create a composite fading mode of top of stack and new mode
42     }
43
44     public boolean popMode() {
45         if (!modes.isEmpty()) {
46             Mode mode = modes.pop();
47             stopMode(mode);
48             if (!modes.isEmpty()) {
49                 startMode(modes.peek());
50             }
51             return true;
52         }
53         return false;
54         // TODO: create a composite fading mode of popped mode and top of stack, unless doing cleanup
55     }
56
57     private void startMode(Mode mode) {
58         mode.setFrameListener(ledController::onFrame);
59         mode.enter(config);
60     }
61
62     private void stopMode(Mode mode) {
63         mode.setFrameListener(ledFrame -> {}); // To avoid any frame being sent to the controller while the thread is exiting
64         mode.exit();
65     }
66
67     public void startLoop() {
68         Console console = Console.start(this, config);
69         PipeController.start(console);
70 //        initNativeHook();
71         // TODO
72 //        FrameGrabber grabber = FrameGrabber.from(config);
73 //        grabber.prepare();
74 //        VideoFrame frame = grabber.grabFrame();
75 //        double time = 0;
76 //        for (int i = 0; i < 100; i++) {
77 //            time += timeIt("frame", () -> grabber.grabFrame());
78 //        }
79 //        System.out.println("time = " + time);
80 //        grabber.close();
81 //      byte[] data = frame.getData();
82 //      saveFile(data, "/home/kaka/test.img");
83     }
84
85     private void initNativeHook() {
86         try {
87             GlobalScreen.registerNativeHook();
88             GlobalScreen.addNativeKeyListener(new NativeKeyAdapter() {
89                 @Override
90                 public void nativeKeyPressed(NativeKeyEvent e) {
91                     System.out.println("key code = " + e.getKeyCode() + ", key text = '" + NativeKeyEvent.getKeyText(e.getKeyCode()) + "'");
92                 }
93             });
94             GlobalScreen.addNativeMouseMotionListener(new NativeMouseMotionAdapter() {
95                 @Override
96                 public void nativeMouseMoved(NativeMouseEvent e) {
97                     System.out.println("mouse point = " + e.getPoint());
98                 }
99             });
100         } catch (NativeHookException e) {
101             e.printStackTrace();
102         }
103     }
104
105     public void turnOff() {
106         cleanup();
107         ledController.onFrame(LedFrame.from(config).fillColor(0, 0, 0));
108     }
109 }