process_unicode.c 4.2 KB

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