process_unicode.c 6.0 KB

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