process_unicode.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. void register_hex32(uint32_t hex) {
  57. for(int i = 7; i >= 0; i--) {
  58. uint8_t digit = ((hex >> (i*8)) & 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. void qk_ucis_start(void) {
  74. qk_ucis_state.count = 0;
  75. qk_ucis_state.in_progress = true;
  76. qk_ucis_start_user();
  77. }
  78. __attribute__((weak))
  79. void qk_ucis_start_user(void) {
  80. unicode_input_start();
  81. register_hex(0x2328);
  82. unicode_input_finish();
  83. }
  84. static bool is_uni_seq(char *seq) {
  85. uint8_t i;
  86. for (i = 0; seq[i]; i++) {
  87. uint16_t code;
  88. if (('1' <= seq[i]) && (seq[i] <= '0'))
  89. code = seq[i] - '1' + KC_1;
  90. else
  91. code = seq[i] - 'a' + KC_A;
  92. if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
  93. return false;
  94. }
  95. return (qk_ucis_state.codes[i] == KC_ENT ||
  96. qk_ucis_state.codes[i] == KC_SPC);
  97. }
  98. __attribute__((weak))
  99. void qk_ucis_symbol_fallback (void) {
  100. for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
  101. uint8_t code = qk_ucis_state.codes[i];
  102. register_code(code);
  103. unregister_code(code);
  104. }
  105. }
  106. bool process_ucis (uint16_t keycode, keyrecord_t *record) {
  107. uint8_t i;
  108. if (!qk_ucis_state.in_progress)
  109. return true;
  110. if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
  111. !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
  112. return false;
  113. }
  114. if (!record->event.pressed)
  115. return true;
  116. qk_ucis_state.codes[qk_ucis_state.count] = keycode;
  117. qk_ucis_state.count++;
  118. if (keycode == KC_BSPC) {
  119. if (qk_ucis_state.count >= 2) {
  120. qk_ucis_state.count -= 2;
  121. return true;
  122. } else {
  123. qk_ucis_state.count--;
  124. return false;
  125. }
  126. }
  127. if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
  128. bool symbol_found = false;
  129. for (i = qk_ucis_state.count; i > 0; i--) {
  130. register_code (KC_BSPC);
  131. unregister_code (KC_BSPC);
  132. }
  133. if (keycode == KC_ESC) {
  134. qk_ucis_state.in_progress = false;
  135. return false;
  136. }
  137. unicode_input_start();
  138. for (i = 0; ucis_symbol_table[i].symbol; i++) {
  139. if (is_uni_seq (ucis_symbol_table[i].symbol)) {
  140. symbol_found = true;
  141. register_hex32(ucis_symbol_table[i].code);
  142. break;
  143. }
  144. }
  145. if (!symbol_found) {
  146. qk_ucis_symbol_fallback();
  147. }
  148. unicode_input_finish();
  149. qk_ucis_state.in_progress = false;
  150. return false;
  151. }
  152. return true;
  153. }
  154. #endif