process_unicode.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "process_unicode.h"
  2. static uint8_t input_mode;
  3. uint16_t hex_to_keycode(uint8_t hex)
  4. {
  5. if (hex == 0x0) {
  6. return KC_0;
  7. } else if (hex < 0xA) {
  8. return KC_1 + (hex - 0x1);
  9. } else {
  10. return KC_A + (hex - 0xA);
  11. }
  12. }
  13. void set_unicode_input_mode(uint8_t os_target)
  14. {
  15. input_mode = os_target;
  16. }
  17. uint8_t get_unicode_input_mode(void) {
  18. return input_mode;
  19. }
  20. __attribute__((weak))
  21. void unicode_input_start (void) {
  22. switch(input_mode) {
  23. case UC_OSX:
  24. register_code(KC_LALT);
  25. break;
  26. case UC_LNX:
  27. register_code(KC_LCTL);
  28. register_code(KC_LSFT);
  29. register_code(KC_U);
  30. unregister_code(KC_U);
  31. unregister_code(KC_LSFT);
  32. unregister_code(KC_LCTL);
  33. break;
  34. case UC_WIN:
  35. register_code(KC_LALT);
  36. register_code(KC_PPLS);
  37. unregister_code(KC_PPLS);
  38. break;
  39. }
  40. wait_ms(UNICODE_TYPE_DELAY);
  41. }
  42. __attribute__((weak))
  43. void unicode_input_finish (void) {
  44. switch(input_mode) {
  45. case UC_OSX:
  46. case UC_WIN:
  47. unregister_code(KC_LALT);
  48. break;
  49. case UC_LNX:
  50. register_code(KC_SPC);
  51. unregister_code(KC_SPC);
  52. break;
  53. }
  54. }
  55. void register_hex(uint16_t hex) {
  56. for(int i = 3; i >= 0; i--) {
  57. uint8_t digit = ((hex >> (i*4)) & 0xF);
  58. register_code(hex_to_keycode(digit));
  59. unregister_code(hex_to_keycode(digit));
  60. }
  61. }
  62. bool process_unicode(uint16_t keycode, keyrecord_t *record) {
  63. if (keycode > QK_UNICODE && record->event.pressed) {
  64. uint16_t unicode = keycode & 0x7FFF;
  65. unicode_input_start();
  66. register_hex(unicode);
  67. unicode_input_finish();
  68. }
  69. return true;
  70. }
  71. #ifdef UCIS_ENABLE
  72. qk_ucis_state_t qk_ucis_state;
  73. void qk_ucis_start(void) {
  74. qk_ucis_state.count = 0;
  75. qk_ucis_state.in_progress = true;
  76. qk_ucis_start_user();
  77. }
  78. __attribute__((weak))
  79. void qk_ucis_start_user(void) {
  80. unicode_input_start();
  81. register_hex(0x2328);
  82. unicode_input_finish();
  83. }
  84. static bool is_uni_seq(char *seq) {
  85. uint8_t i;
  86. for (i = 0; seq[i]; i++) {
  87. uint16_t code;
  88. if (('1' <= seq[i]) && (seq[i] <= '0'))
  89. code = seq[i] - '1' + KC_1;
  90. else
  91. code = seq[i] - 'a' + KC_A;
  92. if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
  93. return false;
  94. }
  95. return (qk_ucis_state.codes[i] == KC_ENT ||
  96. qk_ucis_state.codes[i] == KC_SPC);
  97. }
  98. __attribute__((weak))
  99. void qk_ucis_symbol_fallback (void) {
  100. for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
  101. uint8_t code = qk_ucis_state.codes[i];
  102. register_code(code);
  103. unregister_code(code);
  104. wait_ms(UNICODE_TYPE_DELAY);
  105. }
  106. }
  107. void register_ucis(const char *hex) {
  108. for(int i = 0; hex[i]; i++) {
  109. uint8_t kc = 0;
  110. char c = hex[i];
  111. switch (c) {
  112. case '0':
  113. kc = KC_0;
  114. break;
  115. case '1' ... '9':
  116. kc = c - '1' + KC_1;
  117. break;
  118. case 'a' ... 'f':
  119. kc = c - 'a' + KC_A;
  120. break;
  121. case 'A' ... 'F':
  122. kc = c - 'A' + KC_A;
  123. break;
  124. }
  125. if (kc) {
  126. register_code (kc);
  127. unregister_code (kc);
  128. wait_ms (UNICODE_TYPE_DELAY);
  129. }
  130. }
  131. }
  132. bool process_ucis (uint16_t keycode, keyrecord_t *record) {
  133. uint8_t i;
  134. if (!qk_ucis_state.in_progress)
  135. return true;
  136. if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
  137. !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
  138. return false;
  139. }
  140. if (!record->event.pressed)
  141. return true;
  142. qk_ucis_state.codes[qk_ucis_state.count] = keycode;
  143. qk_ucis_state.count++;
  144. if (keycode == KC_BSPC) {
  145. if (qk_ucis_state.count >= 2) {
  146. qk_ucis_state.count -= 2;
  147. return true;
  148. } else {
  149. qk_ucis_state.count--;
  150. return false;
  151. }
  152. }
  153. if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
  154. bool symbol_found = false;
  155. for (i = qk_ucis_state.count; i > 0; i--) {
  156. register_code (KC_BSPC);
  157. unregister_code (KC_BSPC);
  158. wait_ms(UNICODE_TYPE_DELAY);
  159. }
  160. if (keycode == KC_ESC) {
  161. qk_ucis_state.in_progress = false;
  162. return false;
  163. }
  164. unicode_input_start();
  165. for (i = 0; ucis_symbol_table[i].symbol; i++) {
  166. if (is_uni_seq (ucis_symbol_table[i].symbol)) {
  167. symbol_found = true;
  168. register_ucis(ucis_symbol_table[i].code + 2);
  169. break;
  170. }
  171. }
  172. if (!symbol_found) {
  173. qk_ucis_symbol_fallback();
  174. }
  175. unicode_input_finish();
  176. qk_ucis_state.in_progress = false;
  177. return false;
  178. }
  179. return true;
  180. }
  181. #endif