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 import java.util.Objects;
12 import java.util.Stack;
14 public class CakeLight {
15 private Configuration config;
16 private Stack<Mode> modes = new Stack<>();
17 private LedController ledController;
19 public CakeLight(Configuration config, LedController ledController) {
21 this.ledController = ledController;
22 Color.calculateGammaCorrection(config.gamma);
25 public void setMode(Mode mode) {
30 public void cleanup() {
31 while (popMode() != null);
34 public void pushMode(Mode mode) {
35 Objects.requireNonNull(mode);
36 if (!modes.isEmpty()) {
37 pauseMode(modes.peek());
41 // TODO: create a composite fading mode of top of stack and new mode
44 public Mode popMode() {
45 if (modes.size() > 1) {
46 Mode mode = modes.pop();
48 resumeMode(modes.peek());
52 // TODO: create a composite fading mode of popped mode and top of stack, unless doing cleanup
55 private void startMode(Mode mode) {
56 mode.setFrameListener(ledController::onFrame);
60 private void pauseMode(Mode mode) {
64 private void resumeMode(Mode mode) {
68 private void stopMode(Mode mode) {
69 mode.setFrameListener(ledFrame -> {}); // To avoid any frame being sent to the controller while the thread is exiting
73 public void startLoop() {
74 Console console = Console.start(this, config);
75 PipeController.start(console);
78 // FrameGrabber grabber = FrameGrabber.from(config);
80 // VideoFrame frame = grabber.grabFrame();
82 // for (int i = 0; i < 100; i++) {
83 // time += timeIt("frame", () -> grabber.grabFrame());
85 // System.out.println("time = " + time);
87 // byte[] data = frame.getData();
88 // saveFile(data, "/home/kaka/test.img");
91 private void initNativeHook() {
93 GlobalScreen.registerNativeHook();
94 GlobalScreen.addNativeKeyListener(new NativeKeyAdapter() {
96 public void nativeKeyPressed(NativeKeyEvent e) {
97 System.out.println("key code = " + e.getKeyCode() + ", key text = '" + NativeKeyEvent.getKeyText(e.getKeyCode()) + "'");
100 GlobalScreen.addNativeMouseMotionListener(new NativeMouseMotionAdapter() {
102 public void nativeMouseMoved(NativeMouseEvent e) {
103 System.out.println("mouse point = " + e.getPoint());
106 } catch (NativeHookException e) {
111 public void turnOff() {
113 ledController.onFrame(LedFrame.from(config).fillColor(0, 0, 0));