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