Add command control via named pipe
[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() {
f1a6a6a5
TW
37 Console console = Console.start(this, config);
38 PipeController.start(console);
8ff7ee5a 39 initNativeHook();
4a2d6056
TW
40 // TODO
41// FrameGrabber grabber = FrameGrabber.from(config);
42// grabber.prepare();
adc29b9a 43// VideoFrame frame = grabber.grabFrame();
4a2d6056
TW
44// double time = 0;
45// for (int i = 0; i < 100; i++) {
46// time += timeIt("frame", () -> grabber.grabFrame());
47// }
48// System.out.println("time = " + time);
49// grabber.close();
50// byte[] data = frame.getData();
51// saveFile(data, "/home/kaka/test.img");
52 }
a276d5ab 53
8ff7ee5a
TW
54 private void initNativeHook() {
55 try {
56 GlobalScreen.registerNativeHook();
57 GlobalScreen.addNativeKeyListener(new NativeKeyAdapter() {
58 @Override
59 public void nativeKeyPressed(NativeKeyEvent e) {
60 System.out.println("key code = " + e.getKeyCode() + ", key text = '" + NativeKeyEvent.getKeyText(e.getKeyCode()) + "'");
61 }
62 });
63 GlobalScreen.addNativeMouseMotionListener(new NativeMouseMotionAdapter() {
64 @Override
65 public void nativeMouseMoved(NativeMouseEvent e) {
66 System.out.println("mouse point = " + e.getPoint());
67 }
68 });
69 } catch (NativeHookException e) {
70 e.printStackTrace();
71 }
72 }
73
a276d5ab
TW
74 public void turnOff() {
75 cleanup();
6e568391 76 ledController.onFrame(LedFrame.from(config).fillColor(0, 0, 0));
a276d5ab 77 }
4a2d6056 78}