process_terminal.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. #include "process_terminal.h"
  17. #include <string.h>
  18. #include "version.h"
  19. #include <stdio.h>
  20. #include <math.h>
  21. #ifndef CMD_BUFF_SIZE
  22. # define CMD_BUFF_SIZE 5
  23. #endif
  24. bool terminal_enabled = false;
  25. char buffer[80] = "";
  26. char cmd_buffer[CMD_BUFF_SIZE][80];
  27. bool cmd_buffer_enabled = true; // replace with ifdef?
  28. char newline[2] = "\n";
  29. char arguments[6][20];
  30. bool firstTime = true;
  31. short int current_cmd_buffer_pos = 0; // used for up/down arrows - keeps track of where you are in the command buffer
  32. __attribute__((weak)) const char terminal_prompt[8] = "> ";
  33. #ifdef AUDIO_ENABLE
  34. # ifndef TERMINAL_SONG
  35. # define TERMINAL_SONG SONG(TERMINAL_SOUND)
  36. # endif
  37. float terminal_song[][2] = TERMINAL_SONG;
  38. # define TERMINAL_BELL() PLAY_SONG(terminal_song)
  39. #else
  40. # define TERMINAL_BELL()
  41. #endif
  42. __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, ';', '\'', '`', ',', '.', '/'};
  43. __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, ':', '\'', '~', '<', '>', '?'};
  44. struct stringcase {
  45. char *string;
  46. void (*func)(void);
  47. } typedef stringcase;
  48. void enable_terminal(void) {
  49. terminal_enabled = true;
  50. strcpy(buffer, "");
  51. memset(cmd_buffer, 0, CMD_BUFF_SIZE * 80);
  52. for (int i = 0; i < 6; i++)
  53. strcpy(arguments[i], "");
  54. // select all text to start over
  55. // SEND_STRING(SS_LCTL("a"));
  56. send_string(terminal_prompt);
  57. }
  58. void disable_terminal(void) {
  59. terminal_enabled = false;
  60. SEND_STRING("\n");
  61. }
  62. void push_to_cmd_buffer(void) {
  63. if (cmd_buffer_enabled) {
  64. if (cmd_buffer == NULL) {
  65. return;
  66. } else {
  67. if (firstTime) {
  68. firstTime = false;
  69. strcpy(cmd_buffer[0], buffer);
  70. return;
  71. }
  72. for (int i = CMD_BUFF_SIZE - 1; i > 0; --i) {
  73. strncpy(cmd_buffer[i], cmd_buffer[i - 1], 80);
  74. }
  75. strcpy(cmd_buffer[0], buffer);
  76. return;
  77. }
  78. }
  79. }
  80. void terminal_about(void) {
  81. SEND_STRING("QMK Firmware\n");
  82. SEND_STRING(" v");
  83. SEND_STRING(QMK_VERSION);
  84. SEND_STRING("\n" SS_TAP(X_HOME) " Built: ");
  85. SEND_STRING(QMK_BUILDDATE);
  86. send_string(newline);
  87. #ifdef TERMINAL_HELP
  88. if (strlen(arguments[1]) != 0) {
  89. SEND_STRING("You entered: ");
  90. send_string(arguments[1]);
  91. send_string(newline);
  92. }
  93. #endif
  94. }
  95. void terminal_help(void);
  96. extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
  97. void terminal_keycode(void) {
  98. if (strlen(arguments[1]) != 0 && strlen(arguments[2]) != 0 && strlen(arguments[3]) != 0) {
  99. char keycode_dec[5];
  100. char keycode_hex[5];
  101. uint16_t layer = strtol(arguments[1], (char **)NULL, 10);
  102. uint16_t row = strtol(arguments[2], (char **)NULL, 10);
  103. uint16_t col = strtol(arguments[3], (char **)NULL, 10);
  104. uint16_t keycode = pgm_read_word(&keymaps[layer][row][col]);
  105. itoa(keycode, keycode_dec, 10);
  106. itoa(keycode, keycode_hex, 16);
  107. SEND_STRING("0x");
  108. send_string(keycode_hex);
  109. SEND_STRING(" (");
  110. send_string(keycode_dec);
  111. SEND_STRING(")\n");
  112. } else {
  113. #ifdef TERMINAL_HELP
  114. SEND_STRING("usage: keycode <layer> <row> <col>\n");
  115. #endif
  116. }
  117. }
  118. void terminal_keymap(void) {
  119. if (strlen(arguments[1]) != 0) {
  120. uint16_t layer = strtol(arguments[1], (char **)NULL, 10);
  121. for (int r = 0; r < MATRIX_ROWS; r++) {
  122. for (int c = 0; c < MATRIX_COLS; c++) {
  123. uint16_t keycode = pgm_read_word(&keymaps[layer][r][c]);
  124. char keycode_s[8];
  125. sprintf(keycode_s, "0x%04x,", keycode);
  126. send_string(keycode_s);
  127. }
  128. send_string(newline);
  129. }
  130. } else {
  131. #ifdef TERMINAL_HELP
  132. SEND_STRING("usage: keymap <layer>\n");
  133. #endif
  134. }
  135. }
  136. void print_cmd_buff(void) {
  137. /* without the below wait, a race condition can occur wherein the
  138. buffer can be printed before it has been fully moved */
  139. wait_ms(250);
  140. for (int i = 0; i < CMD_BUFF_SIZE; i++) {
  141. char tmpChar = ' ';
  142. itoa(i, &tmpChar, 10);
  143. const char *tmpCnstCharStr = &tmpChar; // because sned_string wont take a normal char *
  144. send_string(tmpCnstCharStr);
  145. SEND_STRING(". ");
  146. send_string(cmd_buffer[i]);
  147. SEND_STRING("\n");
  148. }
  149. }
  150. void flush_cmd_buffer(void) {
  151. memset(cmd_buffer, 0, CMD_BUFF_SIZE * 80);
  152. SEND_STRING("Buffer Cleared!\n");
  153. }
  154. stringcase 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}};
  155. void terminal_help(void) {
  156. SEND_STRING("commands available:\n ");
  157. for (stringcase *case_p = terminal_cases; case_p != terminal_cases + sizeof(terminal_cases) / sizeof(terminal_cases[0]); case_p++) {
  158. send_string(case_p->string);
  159. SEND_STRING(" ");
  160. }
  161. send_string(newline);
  162. }
  163. void command_not_found(void) {
  164. wait_ms(50); // sometimes buffer isnt grabbed quick enough
  165. SEND_STRING("command \"");
  166. send_string(buffer);
  167. SEND_STRING("\" not found\n");
  168. }
  169. void process_terminal_command(void) {
  170. // we capture return bc of the order of events, so we need to manually send a newline
  171. send_string(newline);
  172. char * pch;
  173. uint8_t i = 0;
  174. pch = strtok(buffer, " ");
  175. while (pch != NULL) {
  176. strcpy(arguments[i], pch);
  177. pch = strtok(NULL, " ");
  178. i++;
  179. }
  180. bool command_found = false;
  181. for (stringcase *case_p = terminal_cases; case_p != terminal_cases + sizeof(terminal_cases) / sizeof(terminal_cases[0]); case_p++) {
  182. if (0 == strcmp(case_p->string, buffer)) {
  183. command_found = true;
  184. (*case_p->func)();
  185. break;
  186. }
  187. }
  188. if (!command_found) command_not_found();
  189. if (terminal_enabled) {
  190. strcpy(buffer, "");
  191. for (int i = 0; i < 6; i++)
  192. strcpy(arguments[i], "");
  193. SEND_STRING(SS_TAP(X_HOME));
  194. send_string(terminal_prompt);
  195. }
  196. }
  197. void check_pos(void) {
  198. 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...
  199. current_cmd_buffer_pos = CMD_BUFF_SIZE - 1;
  200. } 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
  201. current_cmd_buffer_pos = 0;
  202. }
  203. }
  204. bool process_terminal(uint16_t keycode, keyrecord_t *record) {
  205. if (keycode == TERM_ON && record->event.pressed) {
  206. enable_terminal();
  207. return false;
  208. }
  209. if (terminal_enabled && record->event.pressed) {
  210. if (keycode == TERM_OFF && record->event.pressed) {
  211. disable_terminal();
  212. return false;
  213. }
  214. if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
  215. keycode = keycode & 0xFF;
  216. }
  217. if (keycode < 256) {
  218. uint8_t str_len;
  219. char char_to_add;
  220. switch (keycode) {
  221. case KC_ENTER:
  222. case KC_KP_ENTER:
  223. push_to_cmd_buffer();
  224. current_cmd_buffer_pos = 0;
  225. process_terminal_command();
  226. return false;
  227. break;
  228. case KC_ESCAPE:
  229. SEND_STRING("\n");
  230. enable_terminal();
  231. return false;
  232. break;
  233. case KC_BACKSPACE:
  234. str_len = strlen(buffer);
  235. if (str_len > 0) {
  236. buffer[str_len - 1] = 0;
  237. return true;
  238. } else {
  239. TERMINAL_BELL();
  240. return false;
  241. }
  242. break;
  243. case KC_LEFT:
  244. return false;
  245. break;
  246. case KC_RIGHT:
  247. return false;
  248. break;
  249. case KC_UP: // 0 = recent
  250. check_pos(); // check our current buffer position is valid
  251. if (current_cmd_buffer_pos <= CMD_BUFF_SIZE - 1) { // once we get to the top, dont do anything
  252. str_len = strlen(buffer);
  253. for (int i = 0; i < str_len; ++i) {
  254. send_string(SS_TAP(X_BSPACE)); // clear w/e is on the line already
  255. // process_terminal(KC_BACKSPACE,record);
  256. }
  257. strncpy(buffer, cmd_buffer[current_cmd_buffer_pos], 80);
  258. send_string(buffer);
  259. ++current_cmd_buffer_pos; // get ready to access the above cmd if up/down is pressed again
  260. }
  261. return false;
  262. break;
  263. case KC_DOWN:
  264. check_pos();
  265. if (current_cmd_buffer_pos >= 0) { // once we get to the bottom, dont do anything
  266. str_len = strlen(buffer);
  267. for (int i = 0; i < str_len; ++i) {
  268. send_string(SS_TAP(X_BSPACE)); // clear w/e is on the line already
  269. // process_terminal(KC_BACKSPACE,record);
  270. }
  271. strncpy(buffer, cmd_buffer[current_cmd_buffer_pos], 79);
  272. send_string(buffer);
  273. --current_cmd_buffer_pos; // get ready to access the above cmd if down/up is pressed again
  274. }
  275. return false;
  276. break;
  277. default:
  278. if (keycode <= 58) {
  279. char_to_add = 0;
  280. if (get_mods() & (MOD_BIT(KC_LEFT_SHIFT) | MOD_BIT(KC_RIGHT_SHIFT))) {
  281. char_to_add = shifted_keycode_to_ascii_lut[keycode];
  282. } else if (get_mods() == 0) {
  283. char_to_add = keycode_to_ascii_lut[keycode];
  284. }
  285. if (char_to_add != 0) {
  286. strncat(buffer, &char_to_add, 1);
  287. }
  288. }
  289. break;
  290. }
  291. }
  292. }
  293. return true;
  294. }