1 /* Copyright 2017 Jack Humbert
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.
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.
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/>.
17 #include "process_terminal.h"
24 #define CMD_BUFF_SIZE 5
28 bool terminal_enabled = false;
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;
36 short int current_cmd_buffer_pos = 0; //used for up/down arrows - keeps track of where you are in the command buffer
38 __attribute__ ((weak))
39 const char terminal_prompt[8] = "> ";
43 #define TERMINAL_SONG SONG(TERMINAL_SOUND)
45 float terminal_song[][2] = TERMINAL_SONG;
46 #define TERMINAL_BELL() PLAY_SONG(terminal_song)
48 #define TERMINAL_BELL()
51 __attribute__ ((weak))
52 const char keycode_to_ascii_lut[58] = {
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, ';', '\'', '`', ',', '.', '/'
60 __attribute__ ((weak))
61 const char shifted_keycode_to_ascii_lut[58] = {
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, ':', '\'', '~', '<', '>', '?'
74 void enable_terminal(void) {
75 terminal_enabled = true;
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);
85 void disable_terminal(void) {
86 terminal_enabled = false;
90 void push_to_cmd_buffer(void) {
91 if (cmd_buffer_enabled) {
92 if (cmd_buffer == NULL) {
97 strcpy(cmd_buffer[0],buffer);
101 for (int i= CMD_BUFF_SIZE - 1;i > 0 ;--i) {
102 strncpy(cmd_buffer[i],cmd_buffer[i-1],80);
105 strcpy(cmd_buffer[0],buffer);
112 void terminal_about(void) {
113 SEND_STRING("QMK Firmware\n");
115 SEND_STRING(QMK_VERSION);
116 SEND_STRING("\n"SS_TAP(X_HOME)" Built: ");
117 SEND_STRING(QMK_BUILDDATE);
118 send_string(newline);
120 if (strlen(arguments[1]) != 0) {
121 SEND_STRING("You entered: ");
122 send_string(arguments[1]);
123 send_string(newline);
128 void terminal_help(void);
130 extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
132 void terminal_keycode(void) {
133 if (strlen(arguments[1]) != 0 && strlen(arguments[2]) != 0 && strlen(arguments[3]) != 0) {
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);
143 send_string(keycode_hex);
145 send_string(keycode_dec);
149 SEND_STRING("usage: keycode <layer> <row> <col>\n");
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]);
161 sprintf(keycode_s, "0x%04x,", keycode);
162 send_string(keycode_s);
164 send_string(newline);
168 SEND_STRING("usage: keymap <layer>\n");
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 */
177 for(int i=0;i<CMD_BUFF_SIZE;i++){
179 itoa(i ,&tmpChar,10);
180 const char * tmpCnstCharStr = &tmpChar; //because sned_string wont take a normal char *
181 send_string(tmpCnstCharStr);
183 send_string(cmd_buffer[i]);
189 void flush_cmd_buffer(void) {
190 memset(cmd_buffer,0,CMD_BUFF_SIZE * 80);
191 SEND_STRING("Buffer Cleared!\n");
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 }
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);
210 send_string(newline);
213 void command_not_found(void) {
214 wait_ms(50); //sometimes buffer isnt grabbed quick enough
215 SEND_STRING("command \"");
217 SEND_STRING("\" not found\n");
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);
226 pch = strtok(buffer, " ");
227 while (pch != NULL) {
228 strcpy(arguments[i], pch);
229 pch = strtok(NULL, " ");
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;
245 if (terminal_enabled) {
247 for (int i = 0; i < 6; i++)
248 strcpy(arguments[i], "");
249 SEND_STRING(SS_TAP(X_HOME));
250 send_string(terminal_prompt);
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;
264 bool process_terminal(uint16_t keycode, keyrecord_t *record) {
266 if (keycode == TERM_ON && record->event.pressed) {
271 if (terminal_enabled && record->event.pressed) {
272 if (keycode == TERM_OFF && record->event.pressed) {
281 push_to_cmd_buffer();
282 current_cmd_buffer_pos = 0;
283 process_terminal_command();
290 str_len = strlen(buffer);
292 buffer[str_len-1] = 0;
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);
310 strncpy(buffer,cmd_buffer[current_cmd_buffer_pos],80);
313 ++current_cmd_buffer_pos; //get ready to access the above cmd if up/down is pressed again
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);
324 strncpy(buffer,cmd_buffer[current_cmd_buffer_pos],79);
327 --current_cmd_buffer_pos; //get ready to access the above cmd if down/up is pressed again
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];
338 if (char_to_add != 0) {
339 strncat(buffer, &char_to_add, 1);