Commit | Line | Data |
---|---|---|
4a2d6056 TW |
1 | package kaka.cakelight; |
2 | ||
67b0a758 | 3 | import kaka.cakelight.mode.Mode; |
8ff7ee5a TW |
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 | ||
fc040bcb TW |
11 | import java.util.Objects; |
12 | import java.util.Stack; | |
13 | ||
4a2d6056 TW |
14 | public 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() { |
5552e9bd | 45 | if (modes.size() > 1) { |
fc040bcb TW |
46 | Mode mode = modes.pop(); |
47 | stopMode(mode); | |
5552e9bd | 48 | resumeMode(modes.peek()); |
c9edf58d | 49 | return mode; |
4a2d6056 | 50 | } |
c9edf58d | 51 | return null; |
fc040bcb TW |
52 | // TODO: create a composite fading mode of popped mode and top of stack, unless doing cleanup |
53 | } | |
54 | ||
55 | private void startMode(Mode mode) { | |
56 | mode.setFrameListener(ledController::onFrame); | |
57 | mode.enter(config); | |
58 | } | |
59 | ||
d0afa6fb TW |
60 | private void pauseMode(Mode mode) { |
61 | mode.pause(); | |
62 | } | |
63 | ||
64 | private void resumeMode(Mode mode) { | |
65 | mode.resume(); | |
66 | } | |
67 | ||
fc040bcb TW |
68 | private void stopMode(Mode mode) { |
69 | mode.setFrameListener(ledFrame -> {}); // To avoid any frame being sent to the controller while the thread is exiting | |
70 | mode.exit(); | |
4a2d6056 TW |
71 | } |
72 | ||
73 | public void startLoop() { | |
f1a6a6a5 TW |
74 | Console console = Console.start(this, config); |
75 | PipeController.start(console); | |
82687249 | 76 | // initNativeHook(); |
4a2d6056 TW |
77 | // TODO |
78 | // FrameGrabber grabber = FrameGrabber.from(config); | |
79 | // grabber.prepare(); | |
adc29b9a | 80 | // VideoFrame frame = grabber.grabFrame(); |
4a2d6056 TW |
81 | // double time = 0; |
82 | // for (int i = 0; i < 100; i++) { | |
83 | // time += timeIt("frame", () -> grabber.grabFrame()); | |
84 | // } | |
85 | // System.out.println("time = " + time); | |
86 | // grabber.close(); | |
87 | // byte[] data = frame.getData(); | |
88 | // saveFile(data, "/home/kaka/test.img"); | |
89 | } | |
a276d5ab | 90 | |
8ff7ee5a TW |
91 | private void initNativeHook() { |
92 | try { | |
93 | GlobalScreen.registerNativeHook(); | |
94 | GlobalScreen.addNativeKeyListener(new NativeKeyAdapter() { | |
95 | @Override | |
96 | public void nativeKeyPressed(NativeKeyEvent e) { | |
97 | System.out.println("key code = " + e.getKeyCode() + ", key text = '" + NativeKeyEvent.getKeyText(e.getKeyCode()) + "'"); | |
98 | } | |
99 | }); | |
100 | GlobalScreen.addNativeMouseMotionListener(new NativeMouseMotionAdapter() { | |
101 | @Override | |
102 | public void nativeMouseMoved(NativeMouseEvent e) { | |
103 | System.out.println("mouse point = " + e.getPoint()); | |
104 | } | |
105 | }); | |
106 | } catch (NativeHookException e) { | |
107 | e.printStackTrace(); | |
108 | } | |
109 | } | |
110 | ||
a276d5ab TW |
111 | public void turnOff() { |
112 | cleanup(); | |
6e568391 | 113 | ledController.onFrame(LedFrame.from(config).fillColor(0, 0, 0)); |
a276d5ab | 114 | } |
4a2d6056 | 115 | } |