send_string.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 <ctype.h>
  17. #include "quantum.h"
  18. #include "send_string.h"
  19. // clang-format off
  20. /* Bit-Packed look-up table to convert an ASCII character to whether
  21. * [Shift] needs to be sent with the keycode.
  22. */
  23. __attribute__((weak)) const uint8_t ascii_to_shift_lut[16] PROGMEM = {
  24. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  25. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  26. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  27. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  28. KCLUT_ENTRY(0, 1, 1, 1, 1, 1, 1, 0),
  29. KCLUT_ENTRY(1, 1, 1, 1, 0, 0, 0, 0),
  30. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  31. KCLUT_ENTRY(0, 0, 1, 0, 1, 0, 1, 1),
  32. KCLUT_ENTRY(1, 1, 1, 1, 1, 1, 1, 1),
  33. KCLUT_ENTRY(1, 1, 1, 1, 1, 1, 1, 1),
  34. KCLUT_ENTRY(1, 1, 1, 1, 1, 1, 1, 1),
  35. KCLUT_ENTRY(1, 1, 1, 0, 0, 0, 1, 1),
  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, 0, 0, 1, 1, 1, 1, 0)
  40. };
  41. /* Bit-Packed look-up table to convert an ASCII character to whether
  42. * [AltGr] needs to be sent with the keycode.
  43. */
  44. __attribute__((weak)) const uint8_t ascii_to_altgr_lut[16] PROGMEM = {
  45. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  46. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  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, 0, 0, 0, 0, 0),
  51. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  52. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  53. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  54. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  55. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  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. };
  62. /* Bit-Packed look-up table to convert an ASCII character to whether
  63. * [Space] needs to be sent after the keycode
  64. */
  65. __attribute__((weak)) const uint8_t ascii_to_dead_lut[16] PROGMEM = {
  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. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  73. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  74. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  75. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  76. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  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. };
  83. /* Look-up table to convert an ASCII character to a keycode.
  84. */
  85. __attribute__((weak)) const uint8_t ascii_to_keycode_lut[128] PROGMEM = {
  86. // NUL SOH STX ETX EOT ENQ ACK BEL
  87. XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  88. // BS TAB LF VT FF CR SO SI
  89. KC_BSPC, KC_TAB, KC_ENT, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  90. // DLE DC1 DC2 DC3 DC4 NAK SYN ETB
  91. XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  92. // CAN EM SUB ESC FS GS RS US
  93. XXXXXXX, XXXXXXX, XXXXXXX, KC_ESC, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  94. // ! " # $ % & '
  95. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  96. // ( ) * + , - . /
  97. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  98. // 0 1 2 3 4 5 6 7
  99. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  100. // 8 9 : ; < = > ?
  101. KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  102. // @ A B C D E F G
  103. KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  104. // H I J K L M N O
  105. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  106. // P Q R S T U V W
  107. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  108. // X Y Z [ \ ] ^ _
  109. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  110. // ` a b c d e f g
  111. KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  112. // h i j k l m n o
  113. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  114. // p q r s t u v w
  115. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  116. // x y z { | } ~ DEL
  117. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  118. };
  119. // clang-format on
  120. // Note: we bit-pack in "reverse" order to optimize loading
  121. #define PGM_LOADBIT(mem, pos) ((pgm_read_byte(&((mem)[(pos) / 8])) >> ((pos) % 8)) & 0x01)
  122. void send_string(const char *str) { send_string_with_delay(str, 0); }
  123. void send_string_P(const char *str) { send_string_with_delay_P(str, 0); }
  124. void send_string_with_delay(const char *str, uint8_t interval) {
  125. while (1) {
  126. char ascii_code = *str;
  127. if (!ascii_code) break;
  128. if (ascii_code == SS_QMK_PREFIX) {
  129. ascii_code = *(++str);
  130. if (ascii_code == SS_TAP_CODE) {
  131. // tap
  132. uint8_t keycode = *(++str);
  133. tap_code(keycode);
  134. } else if (ascii_code == SS_DOWN_CODE) {
  135. // down
  136. uint8_t keycode = *(++str);
  137. register_code(keycode);
  138. } else if (ascii_code == SS_UP_CODE) {
  139. // up
  140. uint8_t keycode = *(++str);
  141. unregister_code(keycode);
  142. } else if (ascii_code == SS_DELAY_CODE) {
  143. // delay
  144. int ms = 0;
  145. uint8_t keycode = *(++str);
  146. while (isdigit(keycode)) {
  147. ms *= 10;
  148. ms += keycode - '0';
  149. keycode = *(++str);
  150. }
  151. while (ms--) wait_ms(1);
  152. }
  153. } else {
  154. send_char(ascii_code);
  155. }
  156. ++str;
  157. // interval
  158. {
  159. uint8_t ms = interval;
  160. while (ms--) wait_ms(1);
  161. }
  162. }
  163. }
  164. void send_string_with_delay_P(const char *str, uint8_t interval) {
  165. while (1) {
  166. char ascii_code = pgm_read_byte(str);
  167. if (!ascii_code) break;
  168. if (ascii_code == SS_QMK_PREFIX) {
  169. ascii_code = pgm_read_byte(++str);
  170. if (ascii_code == SS_TAP_CODE) {
  171. // tap
  172. uint8_t keycode = pgm_read_byte(++str);
  173. tap_code(keycode);
  174. } else if (ascii_code == SS_DOWN_CODE) {
  175. // down
  176. uint8_t keycode = pgm_read_byte(++str);
  177. register_code(keycode);
  178. } else if (ascii_code == SS_UP_CODE) {
  179. // up
  180. uint8_t keycode = pgm_read_byte(++str);
  181. unregister_code(keycode);
  182. } else if (ascii_code == SS_DELAY_CODE) {
  183. // delay
  184. int ms = 0;
  185. uint8_t keycode = pgm_read_byte(++str);
  186. while (isdigit(keycode)) {
  187. ms *= 10;
  188. ms += keycode - '0';
  189. keycode = pgm_read_byte(++str);
  190. }
  191. while (ms--) wait_ms(1);
  192. }
  193. } else {
  194. send_char(ascii_code);
  195. }
  196. ++str;
  197. // interval
  198. {
  199. uint8_t ms = interval;
  200. while (ms--) wait_ms(1);
  201. }
  202. }
  203. }
  204. void send_char(char ascii_code) {
  205. #if defined(AUDIO_ENABLE) && defined(SENDSTRING_BELL)
  206. if (ascii_code == '\a') { // BEL
  207. PLAY_SONG(bell_song);
  208. return;
  209. }
  210. #endif
  211. uint8_t keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
  212. bool is_shifted = PGM_LOADBIT(ascii_to_shift_lut, (uint8_t)ascii_code);
  213. bool is_altgred = PGM_LOADBIT(ascii_to_altgr_lut, (uint8_t)ascii_code);
  214. bool is_dead = PGM_LOADBIT(ascii_to_dead_lut, (uint8_t)ascii_code);
  215. if (is_shifted) {
  216. register_code(KC_LSFT);
  217. }
  218. if (is_altgred) {
  219. register_code(KC_RALT);
  220. }
  221. tap_code(keycode);
  222. if (is_altgred) {
  223. unregister_code(KC_RALT);
  224. }
  225. if (is_shifted) {
  226. unregister_code(KC_LSFT);
  227. }
  228. if (is_dead) {
  229. tap_code(KC_SPACE);
  230. }
  231. }