summaryrefslogtreecommitdiff
path: root/quantum/process_keycode
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/process_keycode')
-rw-r--r--quantum/process_keycode/process_terminal.c330
-rw-r--r--quantum/process_keycode/process_terminal.h24
-rw-r--r--quantum/process_keycode/process_terminal_nop.h22
3 files changed, 0 insertions, 376 deletions
diff --git a/quantum/process_keycode/process_terminal.c b/quantum/process_keycode/process_terminal.c
deleted file mode 100644
index da1c4d506f..0000000000
--- a/quantum/process_keycode/process_terminal.c
+++ /dev/null
@@ -1,330 +0,0 @@
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#ifndef CMD_BUFF_SIZE
24# define CMD_BUFF_SIZE 5
25#endif
26
27bool terminal_enabled = false;
28char buffer[80] = "";
29char cmd_buffer[CMD_BUFF_SIZE][80];
30bool cmd_buffer_enabled = true; // replace with ifdef?
31char newline[2] = "\n";
32char arguments[6][20];
33bool firstTime = true;
34
35short int current_cmd_buffer_pos = 0; // used for up/down arrows - keeps track of where you are in the command buffer
36
37__attribute__((weak)) const char terminal_prompt[8] = "> ";
38
39#ifdef AUDIO_ENABLE
40# ifndef TERMINAL_SONG
41# define TERMINAL_SONG SONG(TERMINAL_SOUND)
42# endif
43float terminal_song[][2] = TERMINAL_SONG;
44# define TERMINAL_BELL() PLAY_SONG(terminal_song)
45#else
46# define TERMINAL_BELL()
47#endif
48
49__attribute__((weak)) const char keycode_to_ascii_lut[58] = {0, 0, 0, 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 0, 0, 0, '\t', ' ', '-', '=', '[', ']', '\\', 0, ';', '\'', '`', ',', '.', '/'};
50
51__attribute__((weak)) const char shifted_keycode_to_ascii_lut[58] = {0, 0, 0, 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', 0, 0, 0, '\t', ' ', '_', '+', '{', '}', '|', 0, ':', '\'', '~', '<', '>', '?'};
52
53struct stringcase {
54 char *string;
55 void (*func)(void);
56} typedef stringcase;
57
58void enable_terminal(void) {
59 terminal_enabled = true;
60 strcpy(buffer, "");
61 memset(cmd_buffer, 0, CMD_BUFF_SIZE * 80);
62 for (int i = 0; i < 6; i++)
63 strcpy(arguments[i], "");
64 // select all text to start over
65 // SEND_STRING(SS_LCTL("a"));
66 send_string(terminal_prompt);
67}
68
69void disable_terminal(void) {
70 terminal_enabled = false;
71 SEND_STRING("\n");
72}
73
74void push_to_cmd_buffer(void) {
75 if (cmd_buffer_enabled) {
76 if (cmd_buffer == NULL) {
77 return;
78 } else {
79 if (firstTime) {
80 firstTime = false;
81 strcpy(cmd_buffer[0], buffer);
82 return;
83 }
84
85 for (int i = CMD_BUFF_SIZE - 1; i > 0; --i) {
86 strncpy(cmd_buffer[i], cmd_buffer[i - 1], 80);
87 }
88
89 strcpy(cmd_buffer[0], buffer);
90
91 return;
92 }
93 }
94}
95
96void terminal_about(void) {
97 SEND_STRING("QMK Firmware\n");
98 SEND_STRING(" v");
99 SEND_STRING(QMK_VERSION);
100 SEND_STRING("\n" SS_TAP(X_HOME) " Built: ");
101 SEND_STRING(QMK_BUILDDATE);
102 send_string(newline);
103#ifdef TERMINAL_HELP
104 if (strlen(arguments[1]) != 0) {
105 SEND_STRING("You entered: ");
106 send_string(arguments[1]);
107 send_string(newline);
108 }
109#endif
110}
111
112void terminal_help(void);
113
114extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
115
116void terminal_keycode(void) {
117 if (strlen(arguments[1]) != 0 && strlen(arguments[2]) != 0 && strlen(arguments[3]) != 0) {
118 char keycode_dec[5];
119 char keycode_hex[5];
120 uint16_t layer = strtol(arguments[1], (char **)NULL, 10);
121 uint16_t row = strtol(arguments[2], (char **)NULL, 10);
122 uint16_t col = strtol(arguments[3], (char **)NULL, 10);
123 uint16_t keycode = pgm_read_word(&keymaps[layer][row][col]);
124 itoa(keycode, keycode_dec, 10);
125 itoa(keycode, keycode_hex, 16);
126 SEND_STRING("0x");
127 send_string(keycode_hex);
128 SEND_STRING(" (");
129 send_string(keycode_dec);
130 SEND_STRING(")\n");
131 } else {
132#ifdef TERMINAL_HELP
133 SEND_STRING("usage: keycode <layer> <row> <col>\n");
134#endif
135 }
136}
137
138void terminal_keymap(void) {
139 if (strlen(arguments[1]) != 0) {
140 uint16_t layer = strtol(arguments[1], (char **)NULL, 10);
141 for (int r = 0; r < MATRIX_ROWS; r++) {
142 for (int c = 0; c < MATRIX_COLS; c++) {
143 uint16_t keycode = pgm_read_word(&keymaps[layer][r][c]);
144 char keycode_s[8];
145 sprintf(keycode_s, "0x%04x,", keycode);
146 send_string(keycode_s);
147 }
148 send_string(newline);
149 }
150 } else {
151#ifdef TERMINAL_HELP
152 SEND_STRING("usage: keymap <layer>\n");
153#endif
154 }
155}
156
157void print_cmd_buff(void) {
158 /* without the below wait, a race condition can occur wherein the
159 buffer can be printed before it has been fully moved */
160 wait_ms(250);
161 for (int i = 0; i < CMD_BUFF_SIZE; i++) {
162 char tmpChar = ' ';
163 itoa(i, &tmpChar, 10);
164 const char *tmpCnstCharStr = &tmpChar; // because sned_string wont take a normal char *
165 send_string(tmpCnstCharStr);
166 SEND_STRING(". ");
167 send_string(cmd_buffer[i]);
168 SEND_STRING("\n");
169 }
170}
171
172void flush_cmd_buffer(void) {
173 memset(cmd_buffer, 0, CMD_BUFF_SIZE * 80);
174 SEND_STRING("Buffer Cleared!\n");
175}
176
177stringcase terminal_cases[] = {{"about", terminal_about}, {"help", terminal_help}, {"keycode", terminal_keycode}, {"keymap", terminal_keymap}, {"flush-buffer", flush_cmd_buffer}, {"print-buffer", print_cmd_buff}, {"exit", disable_terminal}};
178
179void terminal_help(void) {
180 SEND_STRING("commands available:\n ");
181 for (stringcase *case_p = terminal_cases; case_p != terminal_cases + sizeof(terminal_cases) / sizeof(terminal_cases[0]); case_p++) {
182 send_string(case_p->string);
183 SEND_STRING(" ");
184 }
185 send_string(newline);
186}
187
188void command_not_found(void) {
189 wait_ms(50); // sometimes buffer isnt grabbed quick enough
190 SEND_STRING("command \"");
191 send_string(buffer);
192 SEND_STRING("\" not found\n");
193}
194
195void process_terminal_command(void) {
196 // we capture return bc of the order of events, so we need to manually send a newline
197 send_string(newline);
198
199 char * pch;
200 uint8_t i = 0;
201 pch = strtok(buffer, " ");
202 while (pch != NULL) {
203 strcpy(arguments[i], pch);
204 pch = strtok(NULL, " ");
205 i++;
206 }
207
208 bool command_found = false;
209 for (stringcase *case_p = terminal_cases; case_p != terminal_cases + sizeof(terminal_cases) / sizeof(terminal_cases[0]); case_p++) {
210 if (0 == strcmp(case_p->string, buffer)) {
211 command_found = true;
212 (*case_p->func)();
213 break;
214 }
215 }
216
217 if (!command_found) command_not_found();
218
219 if (terminal_enabled) {
220 strcpy(buffer, "");
221 for (int i = 0; i < 6; i++)
222 strcpy(arguments[i], "");
223 SEND_STRING(SS_TAP(X_HOME));
224 send_string(terminal_prompt);
225 }
226}
227void check_pos(void) {
228 if (current_cmd_buffer_pos >= CMD_BUFF_SIZE) { // if over the top, move it back down to the top of the buffer so you can climb back down...
229 current_cmd_buffer_pos = CMD_BUFF_SIZE - 1;
230 } else if (current_cmd_buffer_pos < 0) { //...and if you fall under the bottom of the buffer, reset back to 0 so you can climb back up
231 current_cmd_buffer_pos = 0;
232 }
233}
234
235bool process_terminal(uint16_t keycode, keyrecord_t *record) {
236 if (keycode == TERM_ON && record->event.pressed) {
237 enable_terminal();
238 return false;
239 }
240
241 if (terminal_enabled && record->event.pressed) {
242 if (keycode == TERM_OFF && record->event.pressed) {
243 disable_terminal();
244 return false;
245 }
246
247 if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
248 keycode = keycode & 0xFF;
249 }
250
251 if (keycode < 256) {
252 uint8_t str_len;
253 char char_to_add;
254 switch (keycode) {
255 case KC_ENTER:
256 case KC_KP_ENTER:
257 push_to_cmd_buffer();
258 current_cmd_buffer_pos = 0;
259 process_terminal_command();
260 return false;
261 break;
262 case KC_ESCAPE:
263 SEND_STRING("\n");
264 enable_terminal();
265 return false;
266 break;
267 case KC_BACKSPACE:
268 str_len = strlen(buffer);
269 if (str_len > 0) {
270 buffer[str_len - 1] = 0;
271 return true;
272 } else {
273 TERMINAL_BELL();
274 return false;
275 }
276 break;
277 case KC_LEFT:
278 return false;
279 break;
280 case KC_RIGHT:
281 return false;
282 break;
283 case KC_UP: // 0 = recent
284 check_pos(); // check our current buffer position is valid
285 if (current_cmd_buffer_pos <= CMD_BUFF_SIZE - 1) { // once we get to the top, dont do anything
286 str_len = strlen(buffer);
287 for (int i = 0; i < str_len; ++i) {
288 send_string(SS_TAP(X_BSPACE)); // clear w/e is on the line already
289 // process_terminal(KC_BACKSPACE,record);
290 }
291 strncpy(buffer, cmd_buffer[current_cmd_buffer_pos], 80);
292
293 send_string(buffer);
294 ++current_cmd_buffer_pos; // get ready to access the above cmd if up/down is pressed again
295 }
296 return false;
297 break;
298 case KC_DOWN:
299 check_pos();
300 if (current_cmd_buffer_pos >= 0) { // once we get to the bottom, dont do anything
301 str_len = strlen(buffer);
302 for (int i = 0; i < str_len; ++i) {
303 send_string(SS_TAP(X_BSPACE)); // clear w/e is on the line already
304 // process_terminal(KC_BACKSPACE,record);
305 }
306 strncpy(buffer, cmd_buffer[current_cmd_buffer_pos], 79);
307
308 send_string(buffer);
309 --current_cmd_buffer_pos; // get ready to access the above cmd if down/up is pressed again
310 }
311 return false;
312 break;
313 default:
314 if (keycode <= 58) {
315 char_to_add = 0;
316 if (get_mods() & (MOD_BIT(KC_LEFT_SHIFT) | MOD_BIT(KC_RIGHT_SHIFT))) {
317 char_to_add = shifted_keycode_to_ascii_lut[keycode];
318 } else if (get_mods() == 0) {
319 char_to_add = keycode_to_ascii_lut[keycode];
320 }
321 if (char_to_add != 0) {
322 strncat(buffer, &char_to_add, 1);
323 }
324 }
325 break;
326 }
327 }
328 }
329 return true;
330}
diff --git a/quantum/process_keycode/process_terminal.h b/quantum/process_keycode/process_terminal.h
deleted file mode 100644
index 0159131e5b..0000000000
--- a/quantum/process_keycode/process_terminal.h
+++ /dev/null
@@ -1,24 +0,0 @@
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#pragma once
18
19#include "quantum.h"
20
21extern const char keycode_to_ascii_lut[58];
22extern const char shifted_keycode_to_ascii_lut[58];
23extern const char terminal_prompt[8];
24bool process_terminal(uint16_t keycode, keyrecord_t *record);
diff --git a/quantum/process_keycode/process_terminal_nop.h b/quantum/process_keycode/process_terminal_nop.h
deleted file mode 100644
index 36e25320c5..0000000000
--- a/quantum/process_keycode/process_terminal_nop.h
+++ /dev/null
@@ -1,22 +0,0 @@
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#pragma once
18
19#include "quantum.h"
20
21#define TERM_ON KC_NO
22#define TERM_OFF KC_NO