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.

251 lines
7.6 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. bool terminal_enabled = false;
  22. char buffer[80] = "";
  23. char newline[2] = "\n";
  24. char arguments[6][20];
  25. __attribute__ ((weak))
  26. const char terminal_prompt[8] = "> ";
  27. #ifdef AUDIO_ENABLE
  28. #ifndef TERMINAL_SONG
  29. #define TERMINAL_SONG SONG(TERMINAL_SOUND)
  30. #endif
  31. float terminal_song[][2] = TERMINAL_SONG;
  32. #define TERMINAL_BELL() PLAY_SONG(terminal_song)
  33. #else
  34. #define TERMINAL_BELL()
  35. #endif
  36. __attribute__ ((weak))
  37. const char keycode_to_ascii_lut[58] = {
  38. 0, 0, 0, 0,
  39. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  40. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  41. '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 0, 0, 0, '\t',
  42. ' ', '-', '=', '[', ']', '\\', 0, ';', '\'', '`', ',', '.', '/'
  43. };
  44. __attribute__ ((weak))
  45. const char shifted_keycode_to_ascii_lut[58] = {
  46. 0, 0, 0, 0,
  47. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  48. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  49. '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', 0, 0, 0, '\t',
  50. ' ', '_', '+', '{', '}', '|', 0, ':', '\'', '~', '<', '>', '?'
  51. };
  52. struct stringcase {
  53. char* string;
  54. void (*func)(void);
  55. } typedef stringcase;
  56. void enable_terminal(void) {
  57. terminal_enabled = true;
  58. strcpy(buffer, "");
  59. for (int i = 0; i < 6; i++)
  60. strcpy(arguments[i], "");
  61. // select all text to start over
  62. // SEND_STRING(SS_LCTRL("a"));
  63. send_string(terminal_prompt);
  64. }
  65. void disable_terminal(void) {
  66. terminal_enabled = false;
  67. }
  68. void terminal_about(void) {
  69. SEND_STRING("QMK Firmware\n");
  70. SEND_STRING(" v");
  71. SEND_STRING(QMK_VERSION);
  72. SEND_STRING("\n"SS_TAP(X_HOME)" Built: ");
  73. SEND_STRING(QMK_BUILDDATE);
  74. send_string(newline);
  75. #ifdef TERMINAL_HELP
  76. if (strlen(arguments[1]) != 0) {
  77. SEND_STRING("You entered: ");
  78. send_string(arguments[1]);
  79. send_string(newline);
  80. }
  81. #endif
  82. }
  83. void terminal_help(void);
  84. extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
  85. void terminal_keycode(void) {
  86. if (strlen(arguments[1]) != 0 && strlen(arguments[2]) != 0 && strlen(arguments[3]) != 0) {
  87. char keycode_dec[5];
  88. char keycode_hex[5];
  89. uint16_t layer = strtol(arguments[1], (char **)NULL, 10);
  90. uint16_t row = strtol(arguments[2], (char **)NULL, 10);
  91. uint16_t col = strtol(arguments[3], (char **)NULL, 10);
  92. uint16_t keycode = pgm_read_word(&keymaps[layer][row][col]);
  93. itoa(keycode, keycode_dec, 10);
  94. itoa(keycode, keycode_hex, 16);
  95. SEND_STRING("0x");
  96. send_string(keycode_hex);
  97. SEND_STRING(" (");
  98. send_string(keycode_dec);
  99. SEND_STRING(")\n");
  100. } else {
  101. #ifdef TERMINAL_HELP
  102. SEND_STRING("usage: keycode <layer> <row> <col>\n");
  103. #endif
  104. }
  105. }
  106. void terminal_keymap(void) {
  107. if (strlen(arguments[1]) != 0) {
  108. uint16_t layer = strtol(arguments[1], (char **)NULL, 10);
  109. for (int r = 0; r < MATRIX_ROWS; r++) {
  110. for (int c = 0; c < MATRIX_COLS; c++) {
  111. uint16_t keycode = pgm_read_word(&keymaps[layer][r][c]);
  112. char keycode_s[8];
  113. sprintf(keycode_s, "0x%04x, ", keycode);
  114. send_string(keycode_s);
  115. }
  116. send_string(newline);
  117. }
  118. } else {
  119. #ifdef TERMINAL_HELP
  120. SEND_STRING("usage: keymap <layer>\n");
  121. #endif
  122. }
  123. }
  124. stringcase terminal_cases[] = {
  125. { "about", terminal_about },
  126. { "help", terminal_help },
  127. { "keycode", terminal_keycode },
  128. { "keymap", terminal_keymap },
  129. { "exit", disable_terminal }
  130. };
  131. void terminal_help(void) {
  132. SEND_STRING("commands available:\n ");
  133. for( stringcase* case_p = terminal_cases; case_p != terminal_cases + sizeof( terminal_cases ) / sizeof( terminal_cases[0] ); case_p++ ) {
  134. send_string(case_p->string);
  135. SEND_STRING(" ");
  136. }
  137. send_string(newline);
  138. }
  139. void command_not_found(void) {
  140. SEND_STRING("command \"");
  141. send_string(buffer);
  142. SEND_STRING("\" not found\n");
  143. }
  144. void process_terminal_command(void) {
  145. // we capture return bc of the order of events, so we need to manually send a newline
  146. send_string(newline);
  147. char * pch;
  148. uint8_t i = 0;
  149. pch = strtok(buffer, " ");
  150. while (pch != NULL) {
  151. strcpy(arguments[i], pch);
  152. pch = strtok(NULL, " ");
  153. i++;
  154. }
  155. bool command_found = false;
  156. for( stringcase* case_p = terminal_cases; case_p != terminal_cases + sizeof( terminal_cases ) / sizeof( terminal_cases[0] ); case_p++ ) {
  157. if( 0 == strcmp( case_p->string, buffer ) ) {
  158. command_found = true;
  159. (*case_p->func)();
  160. break;
  161. }
  162. }
  163. if (!command_found)
  164. command_not_found();
  165. if (terminal_enabled) {
  166. strcpy(buffer, "");
  167. for (int i = 0; i < 6; i++)
  168. strcpy(arguments[i], "");
  169. SEND_STRING(SS_TAP(X_HOME));
  170. send_string(terminal_prompt);
  171. }
  172. }
  173. bool process_terminal(uint16_t keycode, keyrecord_t *record) {
  174. if (keycode == TERM_ON && record->event.pressed) {
  175. enable_terminal();
  176. return false;
  177. }
  178. if (terminal_enabled && record->event.pressed) {
  179. if (keycode == TERM_OFF && record->event.pressed) {
  180. disable_terminal();
  181. return false;
  182. }
  183. if (keycode < 256) {
  184. uint8_t str_len;
  185. char char_to_add;
  186. switch (keycode) {
  187. case KC_ENTER:
  188. process_terminal_command();
  189. return false; break;
  190. case KC_ESC:
  191. SEND_STRING("\n");
  192. enable_terminal();
  193. return false; break;
  194. case KC_BSPC:
  195. str_len = strlen(buffer);
  196. if (str_len > 0) {
  197. buffer[str_len-1] = 0;
  198. return true;
  199. } else {
  200. TERMINAL_BELL();
  201. return false;
  202. } break;
  203. case KC_LEFT:
  204. case KC_RIGHT:
  205. case KC_UP:
  206. case KC_DOWN:
  207. return false; break;
  208. default:
  209. if (keycode <= 58) {
  210. char_to_add = 0;
  211. if (get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))) {
  212. char_to_add = shifted_keycode_to_ascii_lut[keycode];
  213. } else if (get_mods() == 0) {
  214. char_to_add = keycode_to_ascii_lut[keycode];
  215. }
  216. if (char_to_add != 0) {
  217. strncat(buffer, &char_to_add, 1);
  218. }
  219. } break;
  220. }
  221. }
  222. }
  223. return true;
  224. }