process_unicode.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. // 5 digit max because of linux limitation
  82. void register_hex32(uint32_t hex) {
  83. for(int i = 4; i >= 0; i--) {
  84. uint8_t digit = ((hex >> (i*4)) & 0xF);
  85. register_code(hex_to_keycode(digit));
  86. unregister_code(hex_to_keycode(digit));
  87. }
  88. }
  89. bool process_unicode_map(uint16_t keycode, keyrecord_t *record) {
  90. if ((keycode & QK_UNICODE_MAP) == QK_UNICODE_MAP && record->event.pressed) {
  91. const uint32_t* map = unicode_map;
  92. uint16_t index = keycode & 0x7FF;
  93. unicode_input_start();
  94. register_hex32(pgm_read_dword_far(&map[index]));
  95. unicode_input_finish();
  96. }
  97. return true;
  98. }
  99. #endif
  100. #ifdef UCIS_ENABLE
  101. qk_ucis_state_t qk_ucis_state;
  102. void qk_ucis_start(void) {
  103. qk_ucis_state.count = 0;
  104. qk_ucis_state.in_progress = true;
  105. qk_ucis_start_user();
  106. }
  107. __attribute__((weak))
  108. void qk_ucis_start_user(void) {
  109. unicode_input_start();
  110. register_hex(0x2328);
  111. unicode_input_finish();
  112. }
  113. static bool is_uni_seq(char *seq) {
  114. uint8_t i;
  115. for (i = 0; seq[i]; i++) {
  116. uint16_t code;
  117. if (('1' <= seq[i]) && (seq[i] <= '0'))
  118. code = seq[i] - '1' + KC_1;
  119. else
  120. code = seq[i] - 'a' + KC_A;
  121. if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
  122. return false;
  123. }
  124. return (qk_ucis_state.codes[i] == KC_ENT ||
  125. qk_ucis_state.codes[i] == KC_SPC);
  126. }
  127. __attribute__((weak))
  128. void qk_ucis_symbol_fallback (void) {
  129. for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
  130. uint8_t code = qk_ucis_state.codes[i];
  131. register_code(code);
  132. unregister_code(code);
  133. wait_ms(UNICODE_TYPE_DELAY);
  134. }
  135. }
  136. void register_ucis(const char *hex) {
  137. for(int i = 0; hex[i]; i++) {
  138. uint8_t kc = 0;
  139. char c = hex[i];
  140. switch (c) {
  141. case '0':
  142. kc = KC_0;
  143. break;
  144. case '1' ... '9':
  145. kc = c - '1' + KC_1;
  146. break;
  147. case 'a' ... 'f':
  148. kc = c - 'a' + KC_A;
  149. break;
  150. case 'A' ... 'F':
  151. kc = c - 'A' + KC_A;
  152. break;
  153. }
  154. if (kc) {
  155. register_code (kc);
  156. unregister_code (kc);
  157. wait_ms (UNICODE_TYPE_DELAY);
  158. }
  159. }
  160. }
  161. bool process_ucis (uint16_t keycode, keyrecord_t *record) {
  162. uint8_t i;
  163. if (!qk_ucis_state.in_progress)
  164. return true;
  165. if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
  166. !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
  167. return false;
  168. }
  169. if (!record->event.pressed)
  170. return true;
  171. qk_ucis_state.codes[qk_ucis_state.count] = keycode;
  172. qk_ucis_state.count++;
  173. if (keycode == KC_BSPC) {
  174. if (qk_ucis_state.count >= 2) {
  175. qk_ucis_state.count -= 2;
  176. return true;
  177. } else {
  178. qk_ucis_state.count--;
  179. return false;
  180. }
  181. }
  182. if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
  183. bool symbol_found = false;
  184. for (i = qk_ucis_state.count; i > 0; i--) {
  185. register_code (KC_BSPC);
  186. unregister_code (KC_BSPC);
  187. wait_ms(UNICODE_TYPE_DELAY);
  188. }
  189. if (keycode == KC_ESC) {
  190. qk_ucis_state.in_progress = false;
  191. return false;
  192. }
  193. unicode_input_start();
  194. for (i = 0; ucis_symbol_table[i].symbol; i++) {
  195. if (is_uni_seq (ucis_symbol_table[i].symbol)) {
  196. symbol_found = true;
  197. register_ucis(ucis_symbol_table[i].code + 2);
  198. break;
  199. }
  200. }
  201. if (!symbol_found) {
  202. qk_ucis_symbol_fallback();
  203. }
  204. unicode_input_finish();
  205. qk_ucis_state.in_progress = false;
  206. return false;
  207. }
  208. return true;
  209. }
  210. #endif