process_unicode.c 4.3 KB

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