process_unicode.c 4.0 KB

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