process_unicode.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "process_unicode.h"
  2. static uint8_t input_mode;
  3. static uint16_t linux_key = UNICODE_LNX_KEY;
  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. void set_unicode_input_key_lnx(uint16_t key)
  19. {
  20. linux_key = key;
  21. }
  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(linux_key);
  31. unregister_code(linux_key);
  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. 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. void qk_ucis_start(void) {
  73. qk_ucis_state.count = 0;
  74. qk_ucis_state.in_progress = true;
  75. qk_ucis_start_user();
  76. }
  77. __attribute__((weak))
  78. void qk_ucis_start_user(void) {
  79. unicode_input_start();
  80. register_hex(0x2328);
  81. unicode_input_finish();
  82. }
  83. static bool is_uni_seq(char *seq) {
  84. uint8_t i;
  85. for (i = 0; seq[i]; i++) {
  86. uint16_t code;
  87. if (('1' <= seq[i]) && (seq[i] <= '0'))
  88. code = seq[i] - '1' + KC_1;
  89. else
  90. code = seq[i] - 'a' + KC_A;
  91. if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
  92. return false;
  93. }
  94. return (qk_ucis_state.codes[i] == KC_ENT ||
  95. qk_ucis_state.codes[i] == KC_SPC);
  96. }
  97. __attribute__((weak))
  98. void qk_ucis_symbol_fallback (void) {
  99. for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
  100. uint8_t code = qk_ucis_state.codes[i];
  101. register_code(code);
  102. unregister_code(code);
  103. wait_ms(UNICODE_TYPE_DELAY);
  104. }
  105. }
  106. void register_ucis(const char *hex) {
  107. for(int i = 0; hex[i]; i++) {
  108. uint8_t kc = 0;
  109. char c = hex[i];
  110. switch (c) {
  111. case '0':
  112. kc = KC_0;
  113. break;
  114. case '1' ... '9':
  115. kc = c - '1' + KC_1;
  116. break;
  117. case 'a' ... 'f':
  118. kc = c - 'a' + KC_A;
  119. break;
  120. case 'A' ... 'F':
  121. kc = c - 'A' + KC_A;
  122. break;
  123. }
  124. if (kc) {
  125. register_code (kc);
  126. unregister_code (kc);
  127. wait_ms (UNICODE_TYPE_DELAY);
  128. }
  129. }
  130. }
  131. bool process_ucis (uint16_t keycode, keyrecord_t *record) {
  132. uint8_t i;
  133. if (!qk_ucis_state.in_progress)
  134. return true;
  135. if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
  136. !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
  137. return false;
  138. }
  139. if (!record->event.pressed)
  140. return true;
  141. qk_ucis_state.codes[qk_ucis_state.count] = keycode;
  142. qk_ucis_state.count++;
  143. if (keycode == KC_BSPC) {
  144. if (qk_ucis_state.count >= 2) {
  145. qk_ucis_state.count -= 2;
  146. return true;
  147. } else {
  148. qk_ucis_state.count--;
  149. return false;
  150. }
  151. }
  152. if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
  153. bool symbol_found = false;
  154. for (i = qk_ucis_state.count; i > 0; i--) {
  155. register_code (KC_BSPC);
  156. unregister_code (KC_BSPC);
  157. wait_ms(UNICODE_TYPE_DELAY);
  158. }
  159. if (keycode == KC_ESC) {
  160. qk_ucis_state.in_progress = false;
  161. return false;
  162. }
  163. unicode_input_start();
  164. for (i = 0; ucis_symbol_table[i].symbol; i++) {
  165. if (is_uni_seq (ucis_symbol_table[i].symbol)) {
  166. symbol_found = true;
  167. register_ucis(ucis_symbol_table[i].code + 2);
  168. break;
  169. }
  170. }
  171. if (!symbol_found) {
  172. qk_ucis_symbol_fallback();
  173. }
  174. unicode_input_finish();
  175. qk_ucis_state.in_progress = false;
  176. return false;
  177. }
  178. return true;
  179. }
  180. #endif