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