Add command control via named pipe
[kaka/cakelight.git] / src / kaka / cakelight / Console.java
CommitLineData
cd28f68c
TW
1package kaka.cakelight;
2
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.InputStreamReader;
2b49e4e2
TW
6import java.util.ArrayList;
7import java.util.HashMap;
8import java.util.List;
9import java.util.Map;
cd28f68c
TW
10
11public class Console extends Thread {
12 private CakeLight cakelight;
13 private Configuration config;
35990bbd
TW
14 private Map<String, Command> commands = new HashMap<>();
15 private List<Command> commandList = new ArrayList<>();
cd28f68c 16
f1a6a6a5
TW
17 public static Console start(CakeLight cakelight, Configuration config) {
18 Console console = new Console(cakelight, config);
19 console.start();
20 return console;
cd28f68c
TW
21 }
22
23 private Console(CakeLight cakelight, Configuration config) {
24 this.cakelight = cakelight;
25 this.config = config;
be3f2496 26 register(new HelpCommand());
2b49e4e2
TW
27 register(Commands.quit());
28 register(Commands.video());
29 register(Commands.color());
30 register(Commands.brightness());
31 register(Commands.gamma());
32 register(Commands.saturation());
33 register(Commands.ambientMode());
34 register(Commands.twoColorNoiseMode());
be3f2496 35 }
fa013e4b
TW
36
37 public CakeLight getCakelight() {
38 return cakelight;
39 }
40
41 public Configuration getConfig() {
42 return config;
43 }
35990bbd 44
be3f2496
TW
45 private class HelpCommand implements Command {
46 @Override
47 public String[] getNames() {
48 return new String[] {"?", "h", "help"};
49 }
50
51 @Override
52 public void activate(Console console, String[] args) {
53 for (Command c : commandList) {
54 System.out.println(String.join("|", c.getNames()));
55 }
56 }
57 }
58
35990bbd
TW
59 private void register(Command cmd) {
60 for (String name : cmd.getNames()){
61 if (commands.containsKey(name)) {
62 System.out.println("Command name '" + name + "' is already registered by '" + commands.get(name).getClass() + "'!");
63 System.exit(1);
64 }
65 commands.put(name, cmd);
66 }
67 commandList.add(cmd);
cd28f68c
TW
68 }
69
70 @Override
71 public void run() {
72 while (true) {
f1a6a6a5
TW
73 System.out.print("> ");
74 try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
75 String input = reader.readLine();
76 handleInput(input);
77 } catch (IOException e) {
78 System.out.println("Error reading from command line");
79 break;
80 }
cd28f68c
TW
81 }
82 }
eca6fd31 83
f1a6a6a5
TW
84 void handleInput(String input) {
85 String[] splitInput = input.split("\\s+", 2);
86 String name = splitInput[0];
87 String[] args = splitInput.length == 2
88 ? splitInput[1].split("\\s+")
89 : new String[]{};
90
91 Command cmd = commands.get(name);
92 if (cmd != null) {
93 cmd.activate(this, args);
94 } else {
95 out("no command named '" + name + "'");
96 }
97 }
98
2b49e4e2
TW
99 void out(String text) {
100 System.out.println("(" + text + ")");
101 }
baaaa10b 102
2b49e4e2
TW
103 Color parseColor(String s) {
104 switch (s.toLowerCase()) {
105 case "r": return Color.rgb(255, 0, 0);
106 case "g": return Color.rgb(0, 255, 0);
107 case "b": return Color.rgb(0, 0, 255);
108 default: // assume hexadecimal
109 if (s.startsWith("#")) {
110 s = s.substring(1);
111 }
112 if (s.length() == 3) {
113 return Color.rgb(
114 Integer.parseInt(s.substring(0, 1), 16) * 16 + Integer.parseInt(s.substring(0, 1), 16),
115 Integer.parseInt(s.substring(1, 2), 16) * 16 + Integer.parseInt(s.substring(1, 2), 16),
116 Integer.parseInt(s.substring(2, 3), 16) * 16 + Integer.parseInt(s.substring(2, 3), 16)
117 );
118 } else if (s.length() == 6) {
119 return Color.rgb(
120 Integer.parseInt(s.substring(0, 2), 16),
121 Integer.parseInt(s.substring(2, 4), 16),
122 Integer.parseInt(s.substring(4, 6), 16)
123 );
124 }
baaaa10b 125 }
2b49e4e2
TW
126 System.out.println("Failed to parse color '" + s + "'. Using black instead.");
127 return Color.BLACK;
128 }
35990bbd 129
2b49e4e2
TW
130 public interface Command {
131 String[] getNames();
132 void activate(Console console, String[] args);
eca6fd31 133 }
cd28f68c 134}