logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

send_string.c (11039B)


  1. /* Copyright 2021
  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 "send_string.h"
  17. #include <ctype.h>
  18. #include <stdlib.h>
  19. #include "quantum_keycodes.h"
  20. #include "keycode.h"
  21. #include "action.h"
  22. #include "wait.h"
  23. #if defined(AUDIO_ENABLE) && defined(SENDSTRING_BELL)
  24. # include "audio.h"
  25. # ifndef BELL_SOUND
  26. # define BELL_SOUND TERMINAL_SOUND
  27. # endif
  28. float bell_song[][2] = SONG(BELL_SOUND);
  29. #endif
  30. // clang-format off
  31. /* Bit-Packed look-up table to convert an ASCII character to whether
  32. * [Shift] needs to be sent with the keycode.
  33. */
  34. __attribute__((weak)) const uint8_t ascii_to_shift_lut[16] PROGMEM = {
  35. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  36. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  37. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  38. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  39. KCLUT_ENTRY(0, 1, 1, 1, 1, 1, 1, 0),
  40. KCLUT_ENTRY(1, 1, 1, 1, 0, 0, 0, 0),
  41. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  42. KCLUT_ENTRY(0, 0, 1, 0, 1, 0, 1, 1),
  43. KCLUT_ENTRY(1, 1, 1, 1, 1, 1, 1, 1),
  44. KCLUT_ENTRY(1, 1, 1, 1, 1, 1, 1, 1),
  45. KCLUT_ENTRY(1, 1, 1, 1, 1, 1, 1, 1),
  46. KCLUT_ENTRY(1, 1, 1, 0, 0, 0, 1, 1),
  47. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  48. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  49. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  50. KCLUT_ENTRY(0, 0, 0, 1, 1, 1, 1, 0)
  51. };
  52. /* Bit-Packed look-up table to convert an ASCII character to whether
  53. * [AltGr] needs to be sent with the keycode.
  54. */
  55. __attribute__((weak)) const uint8_t ascii_to_altgr_lut[16] PROGMEM = {
  56. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  57. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  58. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  59. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  60. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  61. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  62. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  63. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  64. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  65. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  66. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  67. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  68. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  69. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  70. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  71. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0)
  72. };
  73. /* Bit-Packed look-up table to convert an ASCII character to whether
  74. * [Space] needs to be sent after the keycode
  75. */
  76. __attribute__((weak)) const uint8_t ascii_to_dead_lut[16] PROGMEM = {
  77. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  78. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  79. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  80. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  81. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  82. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  83. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  84. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  85. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  86. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  87. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  88. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  89. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  90. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  91. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  92. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0)
  93. };
  94. /* Look-up table to convert an ASCII character to a keycode.
  95. */
  96. __attribute__((weak)) const uint8_t ascii_to_keycode_lut[128] PROGMEM = {
  97. // NUL SOH STX ETX EOT ENQ ACK BEL
  98. XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  99. // BS TAB LF VT FF CR SO SI
  100. KC_BSPC, KC_TAB, KC_ENT, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  101. // DLE DC1 DC2 DC3 DC4 NAK SYN ETB
  102. XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  103. // CAN EM SUB ESC FS GS RS US
  104. XXXXXXX, XXXXXXX, XXXXXXX, KC_ESC, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  105. // ! " # $ % & '
  106. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  107. // ( ) * + , - . /
  108. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  109. // 0 1 2 3 4 5 6 7
  110. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  111. // 8 9 : ; < = > ?
  112. KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  113. // @ A B C D E F G
  114. KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  115. // H I J K L M N O
  116. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  117. // P Q R S T U V W
  118. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  119. // X Y Z [ \ ] ^ _
  120. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  121. // ` a b c d e f g
  122. KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  123. // h i j k l m n o
  124. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  125. // p q r s t u v w
  126. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  127. // x y z { | } ~ DEL
  128. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  129. };
  130. // clang-format on
  131. // Note: we bit-pack in "reverse" order to optimize loading
  132. #define PGM_LOADBIT(mem, pos) ((pgm_read_byte(&((mem)[(pos) / 8])) >> ((pos) % 8)) & 0x01)
  133. void send_string(const char *string) {
  134. send_string_with_delay(string, TAP_CODE_DELAY);
  135. }
  136. void send_string_with_delay_impl(char (*getter)(void *), void *arg, uint8_t interval) {
  137. while (1) {
  138. char ascii_code = getter(arg);
  139. if (!ascii_code) break;
  140. if (ascii_code == SS_QMK_PREFIX) {
  141. ascii_code = getter(arg);
  142. if (ascii_code == SS_TAP_CODE) {
  143. // tap
  144. uint8_t keycode = getter(arg);
  145. tap_code(keycode);
  146. } else if (ascii_code == SS_DOWN_CODE) {
  147. // down
  148. uint8_t keycode = getter(arg);
  149. register_code(keycode);
  150. } else if (ascii_code == SS_UP_CODE) {
  151. // up
  152. uint8_t keycode = getter(arg);
  153. unregister_code(keycode);
  154. } else if (ascii_code == SS_DELAY_CODE) {
  155. // delay
  156. int ms = 0;
  157. ascii_code = getter(arg);
  158. while (isdigit(ascii_code)) {
  159. ms *= 10;
  160. ms += ascii_code - '0';
  161. ascii_code = getter(arg);
  162. }
  163. wait_ms(ms);
  164. }
  165. wait_ms(interval);
  166. // if we had a delay that terminated with a null, we're done
  167. if (ascii_code == 0) break;
  168. } else {
  169. send_char_with_delay(ascii_code, interval);
  170. }
  171. }
  172. }
  173. typedef struct send_string_memory_state_t {
  174. const char *string;
  175. } send_string_memory_state_t;
  176. char send_string_get_next_ram(void *arg) {
  177. send_string_memory_state_t *state = (send_string_memory_state_t *)arg;
  178. char ret = *state->string;
  179. state->string++;
  180. return ret;
  181. }
  182. void send_string_with_delay(const char *string, uint8_t interval) {
  183. send_string_memory_state_t state = {string};
  184. send_string_with_delay_impl(send_string_get_next_ram, &state, interval);
  185. }
  186. void send_char(char ascii_code) {
  187. send_char_with_delay(ascii_code, TAP_CODE_DELAY);
  188. }
  189. void send_char_with_delay(char ascii_code, uint8_t interval) {
  190. #if defined(AUDIO_ENABLE) && defined(SENDSTRING_BELL)
  191. if (ascii_code == '\a') { // BEL
  192. PLAY_SONG(bell_song);
  193. return;
  194. }
  195. #endif
  196. uint8_t keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
  197. bool is_shifted = PGM_LOADBIT(ascii_to_shift_lut, (uint8_t)ascii_code);
  198. bool is_altgred = PGM_LOADBIT(ascii_to_altgr_lut, (uint8_t)ascii_code);
  199. bool is_dead = PGM_LOADBIT(ascii_to_dead_lut, (uint8_t)ascii_code);
  200. if (is_shifted) {
  201. register_code(KC_LEFT_SHIFT);
  202. wait_ms(interval);
  203. }
  204. if (is_altgred) {
  205. register_code(KC_RIGHT_ALT);
  206. wait_ms(interval);
  207. }
  208. tap_code_delay(keycode, interval);
  209. wait_ms(interval);
  210. if (is_altgred) {
  211. unregister_code(KC_RIGHT_ALT);
  212. wait_ms(interval);
  213. }
  214. if (is_shifted) {
  215. unregister_code(KC_LEFT_SHIFT);
  216. wait_ms(interval);
  217. }
  218. if (is_dead) {
  219. tap_code(KC_SPACE);
  220. wait_ms(interval);
  221. }
  222. }
  223. void send_dword(uint32_t number) {
  224. send_word(number >> 16);
  225. send_word(number & 0xFFFFUL);
  226. }
  227. void send_word(uint16_t number) {
  228. send_byte(number >> 8);
  229. send_byte(number & 0xFF);
  230. }
  231. void send_byte(uint8_t number) {
  232. send_nibble(number >> 4);
  233. send_nibble(number & 0xF);
  234. }
  235. void send_nibble(uint8_t number) {
  236. switch (number & 0xF) {
  237. case 0 ... 9:
  238. send_char(number + '0');
  239. break;
  240. case 10 ... 15:
  241. send_char(number - 10 + 'a');
  242. break;
  243. }
  244. }
  245. void tap_random_base64(void) {
  246. #if defined(__AVR_ATmega32U4__)
  247. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  248. #else
  249. uint8_t key = rand() % 64;
  250. #endif
  251. switch (key) {
  252. case 0 ... 25:
  253. send_char(key + 'A');
  254. break;
  255. case 26 ... 51:
  256. send_char(key - 26 + 'a');
  257. break;
  258. case 52:
  259. send_char('0');
  260. break;
  261. case 53 ... 61:
  262. send_char(key - 53 + '1');
  263. break;
  264. case 62:
  265. send_char('+');
  266. break;
  267. case 63:
  268. send_char('/');
  269. break;
  270. }
  271. }
  272. #if defined(__AVR__)
  273. void send_string_P(const char *string) {
  274. send_string_with_delay_P(string, TAP_CODE_DELAY);
  275. }
  276. char send_string_get_next_progmem(void *arg) {
  277. send_string_memory_state_t *state = (send_string_memory_state_t *)arg;
  278. char ret = pgm_read_byte(state->string);
  279. state->string++;
  280. return ret;
  281. }
  282. void send_string_with_delay_P(const char *string, uint8_t interval) {
  283. send_string_memory_state_t state = {string};
  284. send_string_with_delay_impl(send_string_get_next_progmem, &state, interval);
  285. }
  286. #endif