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"
23 bool terminal_enabled = false;
25 char newline[2] = "\n";
26 char arguments[6][20];
28 __attribute__ ((weak))
29 const char terminal_prompt[8] = "> ";
33 #define TERMINAL_SONG SONG(TERMINAL_SOUND)
35 float terminal_song[][2] = TERMINAL_SONG;
36 #define TERMINAL_BELL() PLAY_SONG(terminal_song)
38 #define TERMINAL_BELL()
41 __attribute__ ((weak))
42 const char keycode_to_ascii_lut[58] = {
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, ';', '\'', '`', ',', '.', '/'
50 __attribute__ ((weak))
51 const char shifted_keycode_to_ascii_lut[58] = {
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, ':', '\'', '~', '<', '>', '?'
64 void enable_terminal(void) {
65 terminal_enabled = true;
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);
74 void disable_terminal(void) {
75 terminal_enabled = false;
78 void terminal_about(void) {
79 SEND_STRING("QMK Firmware\n");
81 SEND_STRING(QMK_VERSION);
82 SEND_STRING("\n"SS_TAP(X_HOME)" Built: ");
83 SEND_STRING(QMK_BUILDDATE);
86 if (strlen(arguments[1]) != 0) {
87 SEND_STRING("You entered: ");
88 send_string(arguments[1]);
94 void terminal_help(void);
96 extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
98 void terminal_keycode(void) {
99 if (strlen(arguments[1]) != 0 && strlen(arguments[2]) != 0 && strlen(arguments[3]) != 0) {
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);
109 send_string(keycode_hex);
111 send_string(keycode_dec);
115 SEND_STRING("usage: keycode <layer> <row> <col>\n");
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]);
127 sprintf(keycode_s, "0x%04x, ", keycode);
128 send_string(keycode_s);
130 send_string(newline);
134 SEND_STRING("usage: keymap <layer>\n");
139 stringcase terminal_cases[] = {
140 { "about", terminal_about },
141 { "help", terminal_help },
142 { "keycode", terminal_keycode },
143 { "keymap", terminal_keymap },
144 { "exit", disable_terminal }
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);
153 send_string(newline);
156 void command_not_found(void) {
157 SEND_STRING("command \"");
159 SEND_STRING("\" not found\n");
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);
168 pch = strtok(buffer, " ");
169 while (pch != NULL) {
170 strcpy(arguments[i], pch);
171 pch = strtok(NULL, " ");
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;
187 if (terminal_enabled) {
189 for (int i = 0; i < 6; i++)
190 strcpy(arguments[i], "");
191 SEND_STRING(SS_TAP(X_HOME));
192 send_string(terminal_prompt);
196 bool process_terminal(uint16_t keycode, keyrecord_t *record) {
198 if (keycode == TERM_ON && record->event.pressed) {
203 if (terminal_enabled && record->event.pressed) {
204 if (keycode == TERM_OFF && record->event.pressed) {
213 process_terminal_command();
220 str_len = strlen(buffer);
222 buffer[str_len-1] = 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];
241 if (char_to_add != 0) {
242 strncat(buffer, &char_to_add, 1);