send_string.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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, 0);
  135. }
  136. void send_string_with_delay(const char *string, uint8_t interval) {
  137. while (1) {
  138. char ascii_code = *string;
  139. if (!ascii_code) break;
  140. if (ascii_code == SS_QMK_PREFIX) {
  141. ascii_code = *(++string);
  142. if (ascii_code == SS_TAP_CODE) {
  143. // tap
  144. uint8_t keycode = *(++string);
  145. tap_code(keycode);
  146. } else if (ascii_code == SS_DOWN_CODE) {
  147. // down
  148. uint8_t keycode = *(++string);
  149. register_code(keycode);
  150. } else if (ascii_code == SS_UP_CODE) {
  151. // up
  152. uint8_t keycode = *(++string);
  153. unregister_code(keycode);
  154. } else if (ascii_code == SS_DELAY_CODE) {
  155. // delay
  156. int ms = 0;
  157. uint8_t keycode = *(++string);
  158. while (isdigit(keycode)) {
  159. ms *= 10;
  160. ms += keycode - '0';
  161. keycode = *(++string);
  162. }
  163. while (ms--)
  164. wait_ms(1);
  165. }
  166. } else {
  167. send_char(ascii_code);
  168. }
  169. ++string;
  170. // interval
  171. {
  172. uint8_t ms = interval;
  173. while (ms--)
  174. wait_ms(1);
  175. }
  176. }
  177. }
  178. void send_char(char ascii_code) {
  179. #if defined(AUDIO_ENABLE) && defined(SENDSTRING_BELL)
  180. if (ascii_code == '\a') { // BEL
  181. PLAY_SONG(bell_song);
  182. return;
  183. }
  184. #endif
  185. uint8_t keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
  186. bool is_shifted = PGM_LOADBIT(ascii_to_shift_lut, (uint8_t)ascii_code);
  187. bool is_altgred = PGM_LOADBIT(ascii_to_altgr_lut, (uint8_t)ascii_code);
  188. bool is_dead = PGM_LOADBIT(ascii_to_dead_lut, (uint8_t)ascii_code);
  189. if (is_shifted) {
  190. register_code(KC_LEFT_SHIFT);
  191. }
  192. if (is_altgred) {
  193. register_code(KC_RIGHT_ALT);
  194. }
  195. tap_code(keycode);
  196. if (is_altgred) {
  197. unregister_code(KC_RIGHT_ALT);
  198. }
  199. if (is_shifted) {
  200. unregister_code(KC_LEFT_SHIFT);
  201. }
  202. if (is_dead) {
  203. tap_code(KC_SPACE);
  204. }
  205. }
  206. void send_dword(uint32_t number) {
  207. send_word(number >> 16);
  208. send_word(number & 0xFFFFUL);
  209. }
  210. void send_word(uint16_t number) {
  211. send_byte(number >> 8);
  212. send_byte(number & 0xFF);
  213. }
  214. void send_byte(uint8_t number) {
  215. send_nibble(number >> 4);
  216. send_nibble(number & 0xF);
  217. }
  218. void send_nibble(uint8_t number) {
  219. switch (number & 0xF) {
  220. case 0 ... 9:
  221. send_char(number + '0');
  222. break;
  223. case 10 ... 15:
  224. send_char(number - 10 + 'a');
  225. break;
  226. }
  227. }
  228. void tap_random_base64(void) {
  229. #if defined(__AVR_ATmega32U4__)
  230. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  231. #else
  232. uint8_t key = rand() % 64;
  233. #endif
  234. switch (key) {
  235. case 0 ... 25:
  236. send_char(key + 'A');
  237. break;
  238. case 26 ... 51:
  239. send_char(key - 26 + 'a');
  240. break;
  241. case 52:
  242. send_char('0');
  243. break;
  244. case 53 ... 61:
  245. send_char(key - 53 + '1');
  246. break;
  247. case 62:
  248. send_char('+');
  249. break;
  250. case 63:
  251. send_char('/');
  252. break;
  253. }
  254. }
  255. #if defined(__AVR__)
  256. void send_string_P(const char *string) {
  257. send_string_with_delay_P(string, 0);
  258. }
  259. void send_string_with_delay_P(const char *string, uint8_t interval) {
  260. while (1) {
  261. char ascii_code = pgm_read_byte(string);
  262. if (!ascii_code) break;
  263. if (ascii_code == SS_QMK_PREFIX) {
  264. ascii_code = pgm_read_byte(++string);
  265. if (ascii_code == SS_TAP_CODE) {
  266. // tap
  267. uint8_t keycode = pgm_read_byte(++string);
  268. tap_code(keycode);
  269. } else if (ascii_code == SS_DOWN_CODE) {
  270. // down
  271. uint8_t keycode = pgm_read_byte(++string);
  272. register_code(keycode);
  273. } else if (ascii_code == SS_UP_CODE) {
  274. // up
  275. uint8_t keycode = pgm_read_byte(++string);
  276. unregister_code(keycode);
  277. } else if (ascii_code == SS_DELAY_CODE) {
  278. // delay
  279. int ms = 0;
  280. uint8_t keycode = pgm_read_byte(++string);
  281. while (isdigit(keycode)) {
  282. ms *= 10;
  283. ms += keycode - '0';
  284. keycode = pgm_read_byte(++string);
  285. }
  286. while (ms--)
  287. wait_ms(1);
  288. }
  289. } else {
  290. send_char(ascii_code);
  291. }
  292. ++string;
  293. // interval
  294. {
  295. uint8_t ms = interval;
  296. while (ms--)
  297. wait_ms(1);
  298. }
  299. }
  300. }
  301. #endif