process_unicode.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 && input_mode == UC_OSX) || (code > 0xFFFFF && input_mode == UC_LNX)) {
  108. // when character is out of range supported by the OS
  109. unicode_map_input_error();
  110. } else {
  111. unicode_input_start();
  112. register_hex32(code);
  113. unicode_input_finish();
  114. }
  115. }
  116. return true;
  117. }
  118. #endif
  119. #ifdef UCIS_ENABLE
  120. qk_ucis_state_t qk_ucis_state;
  121. void qk_ucis_start(void) {
  122. qk_ucis_state.count = 0;
  123. qk_ucis_state.in_progress = true;
  124. qk_ucis_start_user();
  125. }
  126. __attribute__((weak))
  127. void qk_ucis_start_user(void) {
  128. unicode_input_start();
  129. register_hex(0x2328);
  130. unicode_input_finish();
  131. }
  132. static bool is_uni_seq(char *seq) {
  133. uint8_t i;
  134. for (i = 0; seq[i]; i++) {
  135. uint16_t code;
  136. if (('1' <= seq[i]) && (seq[i] <= '0'))
  137. code = seq[i] - '1' + KC_1;
  138. else
  139. code = seq[i] - 'a' + KC_A;
  140. if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
  141. return false;
  142. }
  143. return (qk_ucis_state.codes[i] == KC_ENT ||
  144. qk_ucis_state.codes[i] == KC_SPC);
  145. }
  146. __attribute__((weak))
  147. void qk_ucis_symbol_fallback (void) {
  148. for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
  149. uint8_t code = qk_ucis_state.codes[i];
  150. register_code(code);
  151. unregister_code(code);
  152. wait_ms(UNICODE_TYPE_DELAY);
  153. }
  154. }
  155. void register_ucis(const char *hex) {
  156. for(int i = 0; hex[i]; i++) {
  157. uint8_t kc = 0;
  158. char c = hex[i];
  159. switch (c) {
  160. case '0':
  161. kc = KC_0;
  162. break;
  163. case '1' ... '9':
  164. kc = c - '1' + KC_1;
  165. break;
  166. case 'a' ... 'f':
  167. kc = c - 'a' + KC_A;
  168. break;
  169. case 'A' ... 'F':
  170. kc = c - 'A' + KC_A;
  171. break;
  172. }
  173. if (kc) {
  174. register_code (kc);
  175. unregister_code (kc);
  176. wait_ms (UNICODE_TYPE_DELAY);
  177. }
  178. }
  179. }
  180. bool process_ucis (uint16_t keycode, keyrecord_t *record) {
  181. uint8_t i;
  182. if (!qk_ucis_state.in_progress)
  183. return true;
  184. if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
  185. !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
  186. return false;
  187. }
  188. if (!record->event.pressed)
  189. return true;
  190. qk_ucis_state.codes[qk_ucis_state.count] = keycode;
  191. qk_ucis_state.count++;
  192. if (keycode == KC_BSPC) {
  193. if (qk_ucis_state.count >= 2) {
  194. qk_ucis_state.count -= 2;
  195. return true;
  196. } else {
  197. qk_ucis_state.count--;
  198. return false;
  199. }
  200. }
  201. if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
  202. bool symbol_found = false;
  203. for (i = qk_ucis_state.count; i > 0; i--) {
  204. register_code (KC_BSPC);
  205. unregister_code (KC_BSPC);
  206. wait_ms(UNICODE_TYPE_DELAY);
  207. }
  208. if (keycode == KC_ESC) {
  209. qk_ucis_state.in_progress = false;
  210. return false;
  211. }
  212. unicode_input_start();
  213. for (i = 0; ucis_symbol_table[i].symbol; i++) {
  214. if (is_uni_seq (ucis_symbol_table[i].symbol)) {
  215. symbol_found = true;
  216. register_ucis(ucis_symbol_table[i].code + 2);
  217. break;
  218. }
  219. }
  220. if (!symbol_found) {
  221. qk_ucis_symbol_fallback();
  222. }
  223. unicode_input_finish();
  224. qk_ucis_state.in_progress = false;
  225. return false;
  226. }
  227. return true;
  228. }
  229. #endif