process_unicode.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include "process_unicode.h"
  2. #include "action_util.h"
  3. static uint8_t input_mode;
  4. uint8_t mods;
  5. __attribute__((weak))
  6. uint16_t hex_to_keycode(uint8_t hex)
  7. {
  8. if (hex == 0x0) {
  9. return KC_0;
  10. } else if (hex < 0xA) {
  11. return KC_1 + (hex - 0x1);
  12. } else {
  13. return KC_A + (hex - 0xA);
  14. }
  15. }
  16. void set_unicode_input_mode(uint8_t os_target)
  17. {
  18. input_mode = os_target;
  19. }
  20. uint8_t get_unicode_input_mode(void) {
  21. return input_mode;
  22. }
  23. __attribute__((weak))
  24. void unicode_input_start (void) {
  25. // save current mods
  26. mods = keyboard_report->mods;
  27. // unregister all mods to start from clean state
  28. if (mods & MOD_BIT(KC_LSFT)) unregister_code(KC_LSFT);
  29. if (mods & MOD_BIT(KC_RSFT)) unregister_code(KC_RSFT);
  30. if (mods & MOD_BIT(KC_LCTL)) unregister_code(KC_LCTL);
  31. if (mods & MOD_BIT(KC_RCTL)) unregister_code(KC_RCTL);
  32. if (mods & MOD_BIT(KC_LALT)) unregister_code(KC_LALT);
  33. if (mods & MOD_BIT(KC_RALT)) unregister_code(KC_RALT);
  34. if (mods & MOD_BIT(KC_LGUI)) unregister_code(KC_LGUI);
  35. if (mods & MOD_BIT(KC_RGUI)) unregister_code(KC_RGUI);
  36. switch(input_mode) {
  37. case UC_OSX:
  38. register_code(KC_LALT);
  39. break;
  40. case UC_LNX:
  41. register_code(KC_LCTL);
  42. register_code(KC_LSFT);
  43. register_code(KC_U);
  44. unregister_code(KC_U);
  45. unregister_code(KC_LSFT);
  46. unregister_code(KC_LCTL);
  47. break;
  48. case UC_WIN:
  49. register_code(KC_LALT);
  50. register_code(KC_PPLS);
  51. unregister_code(KC_PPLS);
  52. break;
  53. case UC_WINC:
  54. register_code(KC_RALT);
  55. unregister_code(KC_RALT);
  56. register_code(KC_U);
  57. unregister_code(KC_U);
  58. }
  59. wait_ms(UNICODE_TYPE_DELAY);
  60. }
  61. __attribute__((weak))
  62. void unicode_input_finish (void) {
  63. switch(input_mode) {
  64. case UC_OSX:
  65. case UC_WIN:
  66. unregister_code(KC_LALT);
  67. break;
  68. case UC_LNX:
  69. register_code(KC_SPC);
  70. unregister_code(KC_SPC);
  71. break;
  72. }
  73. // reregister previously set mods
  74. if (mods & MOD_BIT(KC_LSFT)) register_code(KC_LSFT);
  75. if (mods & MOD_BIT(KC_RSFT)) register_code(KC_RSFT);
  76. if (mods & MOD_BIT(KC_LCTL)) register_code(KC_LCTL);
  77. if (mods & MOD_BIT(KC_RCTL)) register_code(KC_RCTL);
  78. if (mods & MOD_BIT(KC_LALT)) register_code(KC_LALT);
  79. if (mods & MOD_BIT(KC_RALT)) register_code(KC_RALT);
  80. if (mods & MOD_BIT(KC_LGUI)) register_code(KC_LGUI);
  81. if (mods & MOD_BIT(KC_RGUI)) register_code(KC_RGUI);
  82. }
  83. void register_hex(uint16_t hex) {
  84. for(int i = 3; i >= 0; i--) {
  85. uint8_t digit = ((hex >> (i*4)) & 0xF);
  86. register_code(hex_to_keycode(digit));
  87. unregister_code(hex_to_keycode(digit));
  88. }
  89. }
  90. bool process_unicode(uint16_t keycode, keyrecord_t *record) {
  91. if (keycode > QK_UNICODE && record->event.pressed) {
  92. uint16_t unicode = keycode & 0x7FFF;
  93. unicode_input_start();
  94. register_hex(unicode);
  95. unicode_input_finish();
  96. }
  97. return true;
  98. }
  99. #ifdef UNICODEMAP_ENABLE
  100. __attribute__((weak))
  101. const uint32_t PROGMEM unicode_map[] = {
  102. };
  103. void register_hex32(uint32_t hex) {
  104. uint8_t onzerostart = 1;
  105. for(int i = 7; i >= 0; i--) {
  106. if (i <= 3) {
  107. onzerostart = 0;
  108. }
  109. uint8_t digit = ((hex >> (i*4)) & 0xF);
  110. if (digit == 0) {
  111. if (onzerostart == 0) {
  112. register_code(hex_to_keycode(digit));
  113. unregister_code(hex_to_keycode(digit));
  114. }
  115. } else {
  116. register_code(hex_to_keycode(digit));
  117. unregister_code(hex_to_keycode(digit));
  118. onzerostart = 0;
  119. }
  120. }
  121. }
  122. __attribute__((weak))
  123. void unicode_map_input_error() {}
  124. bool process_unicode_map(uint16_t keycode, keyrecord_t *record) {
  125. if ((keycode & QK_UNICODE_MAP) == QK_UNICODE_MAP && record->event.pressed) {
  126. const uint32_t* map = unicode_map;
  127. uint16_t index = keycode & 0x7FF;
  128. uint32_t code = pgm_read_dword_far(&map[index]);
  129. if ((code > 0xFFFF && input_mode == UC_OSX) || (code > 0xFFFFF && input_mode == UC_LNX)) {
  130. // when character is out of range supported by the OS
  131. unicode_map_input_error();
  132. } else {
  133. unicode_input_start();
  134. register_hex32(code);
  135. unicode_input_finish();
  136. }
  137. }
  138. return true;
  139. }
  140. #endif
  141. #ifdef UCIS_ENABLE
  142. qk_ucis_state_t qk_ucis_state;
  143. void qk_ucis_start(void) {
  144. qk_ucis_state.count = 0;
  145. qk_ucis_state.in_progress = true;
  146. qk_ucis_start_user();
  147. }
  148. __attribute__((weak))
  149. void qk_ucis_start_user(void) {
  150. unicode_input_start();
  151. register_hex(0x2328);
  152. unicode_input_finish();
  153. }
  154. static bool is_uni_seq(char *seq) {
  155. uint8_t i;
  156. for (i = 0; seq[i]; i++) {
  157. uint16_t code;
  158. if (('1' <= seq[i]) && (seq[i] <= '0'))
  159. code = seq[i] - '1' + KC_1;
  160. else
  161. code = seq[i] - 'a' + KC_A;
  162. if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
  163. return false;
  164. }
  165. return (qk_ucis_state.codes[i] == KC_ENT ||
  166. qk_ucis_state.codes[i] == KC_SPC);
  167. }
  168. __attribute__((weak))
  169. void qk_ucis_symbol_fallback (void) {
  170. for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
  171. uint8_t code = qk_ucis_state.codes[i];
  172. register_code(code);
  173. unregister_code(code);
  174. wait_ms(UNICODE_TYPE_DELAY);
  175. }
  176. }
  177. void register_ucis(const char *hex) {
  178. for(int i = 0; hex[i]; i++) {
  179. uint8_t kc = 0;
  180. char c = hex[i];
  181. switch (c) {
  182. case '0':
  183. kc = KC_0;
  184. break;
  185. case '1' ... '9':
  186. kc = c - '1' + KC_1;
  187. break;
  188. case 'a' ... 'f':
  189. kc = c - 'a' + KC_A;
  190. break;
  191. case 'A' ... 'F':
  192. kc = c - 'A' + KC_A;
  193. break;
  194. }
  195. if (kc) {
  196. register_code (kc);
  197. unregister_code (kc);
  198. wait_ms (UNICODE_TYPE_DELAY);
  199. }
  200. }
  201. }
  202. bool process_ucis (uint16_t keycode, keyrecord_t *record) {
  203. uint8_t i;
  204. if (!qk_ucis_state.in_progress)
  205. return true;
  206. if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
  207. !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
  208. return false;
  209. }
  210. if (!record->event.pressed)
  211. return true;
  212. qk_ucis_state.codes[qk_ucis_state.count] = keycode;
  213. qk_ucis_state.count++;
  214. if (keycode == KC_BSPC) {
  215. if (qk_ucis_state.count >= 2) {
  216. qk_ucis_state.count -= 2;
  217. return true;
  218. } else {
  219. qk_ucis_state.count--;
  220. return false;
  221. }
  222. }
  223. if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
  224. bool symbol_found = false;
  225. for (i = qk_ucis_state.count; i > 0; i--) {
  226. register_code (KC_BSPC);
  227. unregister_code (KC_BSPC);
  228. wait_ms(UNICODE_TYPE_DELAY);
  229. }
  230. if (keycode == KC_ESC) {
  231. qk_ucis_state.in_progress = false;
  232. return false;
  233. }
  234. unicode_input_start();
  235. for (i = 0; ucis_symbol_table[i].symbol; i++) {
  236. if (is_uni_seq (ucis_symbol_table[i].symbol)) {
  237. symbol_found = true;
  238. register_ucis(ucis_symbol_table[i].code + 2);
  239. break;
  240. }
  241. }
  242. if (!symbol_found) {
  243. qk_ucis_symbol_fallback();
  244. }
  245. unicode_input_finish();
  246. qk_ucis_state.in_progress = false;
  247. return false;
  248. }
  249. return true;
  250. }
  251. #endif