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