diff options
Diffstat (limited to 'quantum/process_keycode')
-rw-r--r-- | quantum/process_keycode/process_terminal.c | 252 | ||||
-rw-r--r-- | quantum/process_keycode/process_terminal.h | 27 | ||||
-rw-r--r-- | quantum/process_keycode/process_terminal_nop.h | 25 |
3 files changed, 304 insertions, 0 deletions
diff --git a/quantum/process_keycode/process_terminal.c b/quantum/process_keycode/process_terminal.c new file mode 100644 index 000000000..deb1543e3 --- /dev/null +++ b/quantum/process_keycode/process_terminal.c | |||
@@ -0,0 +1,252 @@ | |||
1 | /* Copyright 2017 Jack Humbert | ||
2 | * | ||
3 | * This program is free software: you can redistribute it and/or modify | ||
4 | * it under the terms of the GNU General Public License as published by | ||
5 | * the Free Software Foundation, either version 2 of the License, or | ||
6 | * (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | #include "process_terminal.h" | ||
18 | #include <string.h> | ||
19 | #include "version.h" | ||
20 | #include <stdio.h> | ||
21 | #include <math.h> | ||
22 | |||
23 | bool terminal_enabled = false; | ||
24 | char buffer[80] = ""; | ||
25 | char newline[2] = "\n"; | ||
26 | char arguments[6][20]; | ||
27 | |||
28 | __attribute__ ((weak)) | ||
29 | const char terminal_prompt[8] = "> "; | ||
30 | |||
31 | #ifdef AUDIO_ENABLE | ||
32 | #ifndef TERMINAL_SONG | ||
33 | #define TERMINAL_SONG SONG(TERMINAL_SOUND) | ||
34 | #endif | ||
35 | float terminal_song[][2] = TERMINAL_SONG; | ||
36 | #define TERMINAL_BELL() PLAY_SONG(terminal_song) | ||
37 | #else | ||
38 | #define TERMINAL_BELL() | ||
39 | #endif | ||
40 | |||
41 | __attribute__ ((weak)) | ||
42 | const char keycode_to_ascii_lut[58] = { | ||
43 | 0, 0, 0, 0, | ||
44 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', | ||
45 | 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', | ||
46 | '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 0, 0, 0, '\t', | ||
47 | ' ', '-', '=', '[', ']', '\\', 0, ';', '\'', '`', ',', '.', '/' | ||
48 | }; | ||
49 | |||
50 | __attribute__ ((weak)) | ||
51 | const char shifted_keycode_to_ascii_lut[58] = { | ||
52 | 0, 0, 0, 0, | ||
53 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', | ||
54 | 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', | ||
55 | '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', 0, 0, 0, '\t', | ||
56 | ' ', '_', '+', '{', '}', '|', 0, ':', '\'', '~', '<', '>', '?' | ||
57 | }; | ||
58 | |||
59 | struct stringcase { | ||
60 | char* string; | ||
61 | void (*func)(void); | ||
62 | } typedef stringcase; | ||
63 | |||
64 | void enable_terminal(void) { | ||
65 | terminal_enabled = true; | ||
66 | strcpy(buffer, ""); | ||
67 | for (int i = 0; i < 6; i++) | ||
68 | strcpy(arguments[i], ""); | ||
69 | // select all text to start over | ||
70 | // SEND_STRING(SS_LCTRL("a")); | ||
71 | send_string(terminal_prompt); | ||
72 | } | ||
73 | |||
74 | void disable_terminal(void) { | ||
75 | terminal_enabled = false; | ||
76 | } | ||
77 | |||
78 | void terminal_about(void) { | ||
79 | SEND_STRING("QMK Firmware\n"); | ||
80 | SEND_STRING(" v"); | ||
81 | SEND_STRING(QMK_VERSION); | ||
82 | SEND_STRING("\n"SS_TAP(X_HOME)" Built: "); | ||
83 | SEND_STRING(QMK_BUILDDATE); | ||
84 | send_string(newline); | ||
85 | #ifdef TERMINAL_HELP | ||
86 | if (strlen(arguments[1]) != 0) { | ||
87 | SEND_STRING("You entered: "); | ||
88 | send_string(arguments[1]); | ||
89 | send_string(newline); | ||
90 | } | ||
91 | #endif | ||
92 | } | ||
93 | |||
94 | void terminal_help(void); | ||
95 | |||
96 | extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS]; | ||
97 | |||
98 | void terminal_keycode(void) { | ||
99 | if (strlen(arguments[1]) != 0 && strlen(arguments[2]) != 0 && strlen(arguments[3]) != 0) { | ||
100 | char keycode_dec[5]; | ||
101 | char keycode_hex[5]; | ||
102 | uint16_t layer = strtol(arguments[1], (char **)NULL, 10); | ||
103 | uint16_t row = strtol(arguments[2], (char **)NULL, 10); | ||
104 | uint16_t col = strtol(arguments[3], (char **)NULL, 10); | ||
105 | uint16_t keycode = pgm_read_word(&keymaps[layer][row][col]); | ||
106 | itoa(keycode, keycode_dec, 10); | ||
107 | itoa(keycode, keycode_hex, 16); | ||
108 | SEND_STRING("0x"); | ||
109 | send_string(keycode_hex); | ||
110 | SEND_STRING(" ("); | ||
111 | send_string(keycode_dec); | ||
112 | SEND_STRING(")\n"); | ||
113 | } else { | ||
114 | #ifdef TERMINAL_HELP | ||
115 | SEND_STRING("usage: keycode <layer> <row> <col>\n"); | ||
116 | #endif | ||
117 | } | ||
118 | } | ||
119 | |||
120 | void terminal_keymap(void) { | ||
121 | if (strlen(arguments[1]) != 0) { | ||
122 | uint16_t layer = strtol(arguments[1], (char **)NULL, 10); | ||
123 | for (int r = 0; r < MATRIX_ROWS; r++) { | ||
124 | for (int c = 0; c < MATRIX_COLS; c++) { | ||
125 | uint16_t keycode = pgm_read_word(&keymaps[layer][r][c]); | ||
126 | char keycode_s[8]; | ||
127 | sprintf(keycode_s, "0x%04x, ", keycode); | ||
128 | send_string(keycode_s); | ||
129 | } | ||
130 | send_string(newline); | ||
131 | } | ||
132 | } else { | ||
133 | #ifdef TERMINAL_HELP | ||
134 | SEND_STRING("usage: keymap <layer>\n"); | ||
135 | #endif | ||
136 | } | ||
137 | } | ||
138 | |||
139 | stringcase terminal_cases[] = { | ||
140 | { "about", terminal_about }, | ||
141 | { "help", terminal_help }, | ||
142 | { "keycode", terminal_keycode }, | ||
143 | { "keymap", terminal_keymap }, | ||
144 | { "exit", disable_terminal } | ||
145 | }; | ||
146 | |||
147 | void terminal_help(void) { | ||
148 | SEND_STRING("commands available:\n "); | ||
149 | for( stringcase* case_p = terminal_cases; case_p != terminal_cases + sizeof( terminal_cases ) / sizeof( terminal_cases[0] ); case_p++ ) { | ||
150 | send_string(case_p->string); | ||
151 | SEND_STRING(" "); | ||
152 | } | ||
153 | send_string(newline); | ||
154 | } | ||
155 | |||
156 | void command_not_found(void) { | ||
157 | SEND_STRING("command \""); | ||
158 | send_string(buffer); | ||
159 | SEND_STRING("\" not found\n"); | ||
160 | } | ||
161 | |||
162 | void process_terminal_command(void) { | ||
163 | // we capture return bc of the order of events, so we need to manually send a newline | ||
164 | send_string(newline); | ||
165 | |||
166 | char * pch; | ||
167 | uint8_t i = 0; | ||
168 | pch = strtok(buffer, " "); | ||
169 | while (pch != NULL) { | ||
170 | strcpy(arguments[i], pch); | ||
171 | pch = strtok(NULL, " "); | ||
172 | i++; | ||
173 | } | ||
174 | |||
175 | bool command_found = false; | ||
176 | for( stringcase* case_p = terminal_cases; case_p != terminal_cases + sizeof( terminal_cases ) / sizeof( terminal_cases[0] ); case_p++ ) { | ||
177 | if( 0 == strcmp( case_p->string, buffer ) ) { | ||
178 | command_found = true; | ||
179 | (*case_p->func)(); | ||
180 | break; | ||
181 | } | ||
182 | } | ||
183 | |||
184 | if (!command_found) | ||
185 | command_not_found(); | ||
186 | |||
187 | if (terminal_enabled) { | ||
188 | strcpy(buffer, ""); | ||
189 | for (int i = 0; i < 6; i++) | ||
190 | strcpy(arguments[i], ""); | ||
191 | SEND_STRING(SS_TAP(X_HOME)); | ||
192 | send_string(terminal_prompt); | ||
193 | } | ||
194 | } | ||
195 | |||
196 | bool process_terminal(uint16_t keycode, keyrecord_t *record) { | ||
197 | |||
198 | if (keycode == TERM_ON && record->event.pressed) { | ||
199 | enable_terminal(); | ||
200 | return false; | ||
201 | } | ||
202 | |||
203 | if (terminal_enabled && record->event.pressed) { | ||
204 | if (keycode == TERM_OFF && record->event.pressed) { | ||
205 | disable_terminal(); | ||
206 | return false; | ||
207 | } | ||
208 | if (keycode < 256) { | ||
209 | uint8_t str_len; | ||
210 | char char_to_add; | ||
211 | switch (keycode) { | ||
212 | case KC_ENTER: | ||
213 | process_terminal_command(); | ||
214 | return false; break; | ||
215 | case KC_ESC: | ||
216 | SEND_STRING("\n"); | ||
217 | enable_terminal(); | ||
218 | return false; break; | ||
219 | case KC_BSPC: | ||
220 | str_len = strlen(buffer); | ||
221 | if (str_len > 0) { | ||
222 | buffer[str_len-1] = 0; | ||
223 | return true; | ||
224 | } else { | ||
225 | TERMINAL_BELL(); | ||
226 | return false; | ||
227 | } break; | ||
228 | case KC_LEFT: | ||
229 | case KC_RIGHT: | ||
230 | case KC_UP: | ||
231 | case KC_DOWN: | ||
232 | return false; break; | ||
233 | default: | ||
234 | if (keycode <= 58) { | ||
235 | char_to_add = 0; | ||
236 | if (get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))) { | ||
237 | char_to_add = shifted_keycode_to_ascii_lut[keycode]; | ||
238 | } else if (get_mods() == 0) { | ||
239 | char_to_add = keycode_to_ascii_lut[keycode]; | ||
240 | } | ||
241 | if (char_to_add != 0) { | ||
242 | strncat(buffer, &char_to_add, 1); | ||
243 | } | ||
244 | } break; | ||
245 | } | ||
246 | |||
247 | |||
248 | |||
249 | } | ||
250 | } | ||
251 | return true; | ||
252 | } \ No newline at end of file | ||
diff --git a/quantum/process_keycode/process_terminal.h b/quantum/process_keycode/process_terminal.h new file mode 100644 index 000000000..d945949a4 --- /dev/null +++ b/quantum/process_keycode/process_terminal.h | |||
@@ -0,0 +1,27 @@ | |||
1 | /* Copyright 2017 Jack Humbert | ||
2 | * | ||
3 | * This program is free software: you can redistribute it and/or modify | ||
4 | * it under the terms of the GNU General Public License as published by | ||
5 | * the Free Software Foundation, either version 2 of the License, or | ||
6 | * (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | #ifndef PROCESS_TERMINAL_H | ||
18 | #define PROCESS_TERMINAL_H | ||
19 | |||
20 | #include "quantum.h" | ||
21 | |||
22 | extern const char keycode_to_ascii_lut[58]; | ||
23 | extern const char shifted_keycode_to_ascii_lut[58]; | ||
24 | extern const char terminal_prompt[8]; | ||
25 | bool process_terminal(uint16_t keycode, keyrecord_t *record); | ||
26 | |||
27 | #endif \ No newline at end of file | ||
diff --git a/quantum/process_keycode/process_terminal_nop.h b/quantum/process_keycode/process_terminal_nop.h new file mode 100644 index 000000000..56895b33c --- /dev/null +++ b/quantum/process_keycode/process_terminal_nop.h | |||
@@ -0,0 +1,25 @@ | |||
1 | /* Copyright 2017 Jack Humbert | ||
2 | * | ||
3 | * This program is free software: you can redistribute it and/or modify | ||
4 | * it under the terms of the GNU General Public License as published by | ||
5 | * the Free Software Foundation, either version 2 of the License, or | ||
6 | * (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | #ifndef PROCESS_TERMINAL_H | ||
18 | #define PROCESS_TERMINAL_H | ||
19 | |||
20 | #include "quantum.h" | ||
21 | |||
22 | #define TERM_ON KC_NO | ||
23 | #define TERM_OFF KC_NO | ||
24 | |||
25 | #endif \ No newline at end of file | ||