1 package kaka.cakelight;
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;
11 public class CakeLight {
12 private Configuration config;
14 private LedController ledController;
16 public CakeLight(Configuration config, LedController ledController) {
18 this.ledController = ledController;
19 Color.calculateGammaCorrection(config.gamma);
22 public void setMode(Mode mode) {
25 mode.setFrameListener(ledController::onFrame);
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
36 public void startLoop() {
37 Console console = Console.start(this, config);
38 PipeController.start(console);
41 // FrameGrabber grabber = FrameGrabber.from(config);
43 // VideoFrame frame = grabber.grabFrame();
45 // for (int i = 0; i < 100; i++) {
46 // time += timeIt("frame", () -> grabber.grabFrame());
48 // System.out.println("time = " + time);
50 // byte[] data = frame.getData();
51 // saveFile(data, "/home/kaka/test.img");
54 private void initNativeHook() {
56 GlobalScreen.registerNativeHook();
57 GlobalScreen.addNativeKeyListener(new NativeKeyAdapter() {
59 public void nativeKeyPressed(NativeKeyEvent e) {
60 System.out.println("key code = " + e.getKeyCode() + ", key text = '" + NativeKeyEvent.getKeyText(e.getKeyCode()) + "'");
63 GlobalScreen.addNativeMouseMotionListener(new NativeMouseMotionAdapter() {
65 public void nativeMouseMoved(NativeMouseEvent e) {
66 System.out.println("mouse point = " + e.getPoint());
69 } catch (NativeHookException e) {
74 public void turnOff() {
76 ledController.onFrame(LedFrame.from(config).fillColor(0, 0, 0));