You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
12 KiB

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