From f1a6a6a5cf7d61c2df185206cb0a5b0e7eceb3c1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Tue, 26 Nov 2019 22:53:32 +0100 Subject: [PATCH] Add command control via named pipe --- src/kaka/cakelight/CakeLight.java | 3 +- src/kaka/cakelight/Console.java | 50 ++++++++++++++++++---------------- src/kaka/cakelight/PipeController.java | 42 ++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 24 deletions(-) create mode 100644 src/kaka/cakelight/PipeController.java diff --git a/src/kaka/cakelight/CakeLight.java b/src/kaka/cakelight/CakeLight.java index 4b37bd7..65d325c 100644 --- a/src/kaka/cakelight/CakeLight.java +++ b/src/kaka/cakelight/CakeLight.java @@ -34,7 +34,8 @@ public class CakeLight { } public void startLoop() { - Console.start(this, config); + Console console = Console.start(this, config); + PipeController.start(console); initNativeHook(); // TODO // FrameGrabber grabber = FrameGrabber.from(config); diff --git a/src/kaka/cakelight/Console.java b/src/kaka/cakelight/Console.java index cf72cce..f32ba98 100644 --- a/src/kaka/cakelight/Console.java +++ b/src/kaka/cakelight/Console.java @@ -11,18 +11,18 @@ import java.util.Map; public class Console extends Thread { private CakeLight cakelight; private Configuration config; - private BufferedReader reader; private Map commands = new HashMap<>(); private List commandList = new ArrayList<>(); - public static void start(CakeLight cakelight, Configuration config) { - new Console(cakelight, config).start(); + public static Console start(CakeLight cakelight, Configuration config) { + Console console = new Console(cakelight, config); + console.start(); + return console; } private Console(CakeLight cakelight, Configuration config) { this.cakelight = cakelight; this.config = config; - reader = new BufferedReader(new InputStreamReader(System.in)); register(new HelpCommand()); register(Commands.quit()); register(Commands.video()); @@ -70,28 +70,32 @@ public class Console extends Thread { @Override public void run() { while (true) { - System.out.print("> "); - try { - String input = reader.readLine(); - String[] splitInput = input.split("\\s+", 2); - String name = splitInput[0]; - String[] args = splitInput.length == 2 - ? splitInput[1].split("\\s+") - : new String[]{}; - - Command cmd = commands.get(name); - if (cmd != null) { - cmd.activate(this, args); - } else { - out("no command named '" + name + "'"); - } - } catch (IOException e) { - System.out.println("Error reading from command line"); - break; - } + System.out.print("> "); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { + String input = reader.readLine(); + handleInput(input); + } catch (IOException e) { + System.out.println("Error reading from command line"); + break; + } } } + void handleInput(String input) { + String[] splitInput = input.split("\\s+", 2); + String name = splitInput[0]; + String[] args = splitInput.length == 2 + ? splitInput[1].split("\\s+") + : new String[]{}; + + Command cmd = commands.get(name); + if (cmd != null) { + cmd.activate(this, args); + } else { + out("no command named '" + name + "'"); + } + } + void out(String text) { System.out.println("(" + text + ")"); } diff --git a/src/kaka/cakelight/PipeController.java b/src/kaka/cakelight/PipeController.java new file mode 100644 index 0000000..10ed6be --- /dev/null +++ b/src/kaka/cakelight/PipeController.java @@ -0,0 +1,42 @@ +package kaka.cakelight; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +public class PipeController extends Thread { + private static final String PIPE = "cakectrl"; + private Console console; + + public static void start(Console console) { + new PipeController(console).start(); + } + + private PipeController(Console console) { + this.console = console; + } + + @Override + public void run() { + while (true) { + createPipe(); + try (BufferedReader reader = new BufferedReader(new FileReader(new File(PIPE)))) { + String input; + while ((input = reader.readLine()) != null) { + console.handleInput(input); + } + } catch (IOException e) { + System.out.println("Error reading from pipe '" + PIPE + "'"); + } + } + } + + private void createPipe() { + try { + Runtime.getRuntime().exec("mkfifo " + PIPE); + } catch (IOException e) { + e.printStackTrace(); + } + } +} -- 2.11.0