1 package kaka.cakelight;
3 import org.jnativehook.GlobalScreen;
4 import org.jnativehook.NativeHookException;
5 import org.jnativehook.keyboard.NativeKeyAdapter;
6 import org.jnativehook.keyboard.NativeKeyEvent;
7 import org.jnativehook.mouse.NativeMouseEvent;
8 import org.jnativehook.mouse.NativeMouseMotionAdapter;
10 public class CakeLight {
11 private Configuration config;
13 private LedController ledController;
15 public CakeLight(Configuration config, LedController ledController) {
17 this.ledController = ledController;
18 Color.calculateGammaCorrection(config.gamma);
21 public void setMode(Mode mode) {
24 mode.setFrameListener(ledController::onFrame);
28 public void cleanup() {
29 if (this.mode != null) {
30 this.mode.setFrameListener(ledFrame -> {}); // To avoid any frame being sent to the controller while the thread is exiting
35 public void startLoop() {
36 Console.start(this, config);
39 // FrameGrabber grabber = FrameGrabber.from(config);
41 // VideoFrame frame = grabber.grabFrame();
43 // for (int i = 0; i < 100; i++) {
44 // time += timeIt("frame", () -> grabber.grabFrame());
46 // System.out.println("time = " + time);
48 // byte[] data = frame.getData();
49 // saveFile(data, "/home/kaka/test.img");
52 private void initNativeHook() {
54 GlobalScreen.registerNativeHook();
55 GlobalScreen.addNativeKeyListener(new NativeKeyAdapter() {
57 public void nativeKeyPressed(NativeKeyEvent e) {
58 System.out.println("key code = " + e.getKeyCode() + ", key text = '" + NativeKeyEvent.getKeyText(e.getKeyCode()) + "'");
61 GlobalScreen.addNativeMouseMotionListener(new NativeMouseMotionAdapter() {
63 public void nativeMouseMoved(NativeMouseEvent e) {
64 System.out.println("mouse point = " + e.getPoint());
67 } catch (NativeHookException e) {
72 public void turnOff() {
74 ledController.onFrame(LedFrame.from(config).fillColor(0, 0, 0));