process_unicode.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include "process_unicode.h"
  2. static uint8_t input_mode;
  3. __attribute__((weak))
  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. uint8_t get_unicode_input_mode(void) {
  19. return input_mode;
  20. }
  21. __attribute__((weak))
  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(KC_U);
  31. unregister_code(KC_U);
  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. __attribute__((weak))
  44. void unicode_input_finish (void) {
  45. switch(input_mode) {
  46. case UC_OSX:
  47. case UC_WIN:
  48. unregister_code(KC_LALT);
  49. break;
  50. case UC_LNX:
  51. register_code(KC_SPC);
  52. unregister_code(KC_SPC);
  53. break;
  54. }
  55. }
  56. void register_hex(uint16_t hex) {
  57. for(int i = 3; i >= 0; i--) {
  58. uint8_t digit = ((hex >> (i*4)) & 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 UNICODEMAP_ENABLE
  73. __attribute__((weak))
  74. const uint32_t PROGMEM unicode_map[] = {
  75. };
  76. // 5 digit max because of linux limitation
  77. void register_hex32(uint32_t hex) {
  78. for(int i = 4; i >= 0; i--) {
  79. uint8_t digit = ((hex >> (i*4)) & 0xF);
  80. register_code(hex_to_keycode(digit));
  81. unregister_code(hex_to_keycode(digit));
  82. }
  83. }
  84. bool process_unicode_map(uint16_t keycode, keyrecord_t *record) {
  85. if ((keycode & QK_UNICODE_MAP) == QK_UNICODE_MAP && record->event.pressed) {
  86. const uint32_t* map = unicode_map;
  87. uint16_t index = keycode & 0x7FF;
  88. unicode_input_start();
  89. register_hex32(pgm_read_dword_far(&map[index]));
  90. unicode_input_finish();
  91. }
  92. return true;
  93. }
  94. #endif
  95. #ifdef UCIS_ENABLE
  96. qk_ucis_state_t qk_ucis_state;
  97. void qk_ucis_start(void) {
  98. qk_ucis_state.count = 0;
  99. qk_ucis_state.in_progress = true;
  100. qk_ucis_start_user();
  101. }
  102. __attribute__((weak))
  103. void qk_ucis_start_user(void) {
  104. unicode_input_start();
  105. register_hex(0x2328);
  106. unicode_input_finish();
  107. }
  108. static bool is_uni_seq(char *seq) {
  109. uint8_t i;
  110. for (i = 0; seq[i]; i++) {
  111. uint16_t code;
  112. if (('1' <= seq[i]) && (seq[i] <= '0'))
  113. code = seq[i] - '1' + KC_1;
  114. else
  115. code = seq[i] - 'a' + KC_A;
  116. if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
  117. return false;
  118. }
  119. return (qk_ucis_state.codes[i] == KC_ENT ||
  120. qk_ucis_state.codes[i] == KC_SPC);
  121. }
  122. __attribute__((weak))
  123. void qk_ucis_symbol_fallback (void) {
  124. for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
  125. uint8_t code = qk_ucis_state.codes[i];
  126. register_code(code);
  127. unregister_code(code);
  128. wait_ms(UNICODE_TYPE_DELAY);
  129. }
  130. }
  131. void register_ucis(const char *hex) {
  132. for(int i = 0; hex[i]; i++) {
  133. uint8_t kc = 0;
  134. char c = hex[i];
  135. switch (c) {
  136. case '0':
  137. kc = KC_0;
  138. break;
  139. case '1' ... '9':
  140. kc = c - '1' + KC_1;
  141. break;
  142. case 'a' ... 'f':
  143. kc = c - 'a' + KC_A;
  144. break;
  145. case 'A' ... 'F':
  146. kc = c - 'A' + KC_A;
  147. break;
  148. }
  149. if (kc) {
  150. register_code (kc);
  151. unregister_code (kc);
  152. wait_ms (UNICODE_TYPE_DELAY);
  153. }
  154. }
  155. }
  156. bool process_ucis (uint16_t keycode, keyrecord_t *record) {
  157. uint8_t i;
  158. if (!qk_ucis_state.in_progress)
  159. return true;
  160. if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
  161. !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
  162. return false;
  163. }
  164. if (!record->event.pressed)
  165. return true;
  166. qk_ucis_state.codes[qk_ucis_state.count] = keycode;
  167. qk_ucis_state.count++;
  168. if (keycode == KC_BSPC) {
  169. if (qk_ucis_state.count >= 2) {
  170. qk_ucis_state.count -= 2;
  171. return true;
  172. } else {
  173. qk_ucis_state.count--;
  174. return false;
  175. }
  176. }
  177. if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
  178. bool symbol_found = false;
  179. for (i = qk_ucis_state.count; i > 0; i--) {
  180. register_code (KC_BSPC);
  181. unregister_code (KC_BSPC);
  182. wait_ms(UNICODE_TYPE_DELAY);
  183. }
  184. if (keycode == KC_ESC) {
  185. qk_ucis_state.in_progress = false;
  186. return false;
  187. }
  188. unicode_input_start();
  189. for (i = 0; ucis_symbol_table[i].symbol; i++) {
  190. if (is_uni_seq (ucis_symbol_table[i].symbol)) {
  191. symbol_found = true;
  192. register_ucis(ucis_symbol_table[i].code + 2);
  193. break;
  194. }
  195. }
  196. if (!symbol_found) {
  197. qk_ucis_symbol_fallback();
  198. }
  199. unicode_input_finish();
  200. qk_ucis_state.in_progress = false;
  201. return false;
  202. }
  203. return true;
  204. }
  205. #endif