]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_terminal.c
Remove more commented out MCUs
[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
277         if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
278             keycode = keycode & 0xFF;
279         }
280
281         if (keycode < 256) {
282             uint8_t str_len;
283             char char_to_add;
284             switch (keycode) {
285                 case KC_ENTER:
286                 case KC_KP_ENTER:
287                     push_to_cmd_buffer();
288                     current_cmd_buffer_pos = 0;
289                     process_terminal_command();
290                     return false; break;
291                 case KC_ESC:
292                     SEND_STRING("\n");
293                     enable_terminal();
294                     return false; break;
295                 case KC_BSPC:
296                     str_len = strlen(buffer);
297                     if (str_len > 0) {
298                         buffer[str_len-1] = 0;
299                         return true;
300                     } else {
301                         TERMINAL_BELL();
302                         return false;
303                     } break;
304                 case KC_LEFT:
305                     return false; break;
306                 case KC_RIGHT:
307                     return false; break;
308                 case KC_UP: // 0 = recent
309                   check_pos(); //check our current buffer position is valid
310                   if (current_cmd_buffer_pos <= CMD_BUFF_SIZE - 1) { //once we get to the top, dont do anything
311                     str_len = strlen(buffer);
312                     for(int  i= 0;i < str_len ;++i) {
313                         send_string(SS_TAP(X_BSPACE)); //clear w/e is on the line already
314                         //process_terminal(KC_BSPC,record);
315                     }
316                     strncpy(buffer,cmd_buffer[current_cmd_buffer_pos],80);
317
318                     send_string(buffer);
319                     ++current_cmd_buffer_pos; //get ready to access the above cmd if up/down is pressed again
320                   }
321                     return false; break;
322                 case KC_DOWN:
323                   check_pos();
324                   if (current_cmd_buffer_pos >= 0) { //once we get to the bottom, dont do anything
325                       str_len = strlen(buffer);
326                       for(int  i= 0;i < str_len ;++i) {
327                           send_string(SS_TAP(X_BSPACE)); //clear w/e is on the line already
328                           //process_terminal(KC_BSPC,record);
329                       }
330                       strncpy(buffer,cmd_buffer[current_cmd_buffer_pos],79);
331
332                       send_string(buffer);
333                       --current_cmd_buffer_pos; //get ready to access the above cmd if down/up is pressed again
334                     }
335                     return false; break;
336                 default:
337                     if (keycode <= 58) {
338                         char_to_add = 0;
339                         if (get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))) {
340                             char_to_add = shifted_keycode_to_ascii_lut[keycode];
341                         } else if (get_mods() == 0) {
342                             char_to_add = keycode_to_ascii_lut[keycode];
343                         }
344                         if (char_to_add != 0) {
345                             strncat(buffer, &char_to_add, 1);
346                         }
347                     } break;
348             }
349
350
351
352         }
353     }
354     return true;
355 }