process_unicode.c 4.2 KB

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