process_unicode.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "process_unicode.h"
  2. static uint8_t input_mode;
  3. static uint8_t first_flag = 0;
  4. __attribute__((weak))
  5. uint16_t hex_to_keycode(uint8_t hex)
  6. {
  7. if (hex == 0x0) {
  8. return KC_0;
  9. } else if (hex < 0xA) {
  10. return KC_1 + (hex - 0x1);
  11. } else {
  12. return KC_A + (hex - 0xA);
  13. }
  14. }
  15. void set_unicode_input_mode(uint8_t os_target)
  16. {
  17. input_mode = os_target;
  18. eeprom_update_byte(EECONFIG_UNICODEMODE, os_target);
  19. }
  20. uint8_t get_unicode_input_mode(void) {
  21. return input_mode;
  22. }
  23. __attribute__((weak))
  24. void unicode_input_start (void) {
  25. switch(input_mode) {
  26. case UC_OSX:
  27. register_code(KC_LALT);
  28. break;
  29. case UC_LNX:
  30. register_code(KC_LCTL);
  31. register_code(KC_LSFT);
  32. register_code(KC_U);
  33. unregister_code(KC_U);
  34. unregister_code(KC_LSFT);
  35. unregister_code(KC_LCTL);
  36. break;
  37. case UC_WIN:
  38. register_code(KC_LALT);
  39. register_code(KC_PPLS);
  40. unregister_code(KC_PPLS);
  41. break;
  42. case UC_WINC:
  43. register_code(KC_RALT);
  44. unregister_code(KC_RALT);
  45. register_code(KC_U);
  46. unregister_code(KC_U);
  47. }
  48. wait_ms(UNICODE_TYPE_DELAY);
  49. }
  50. __attribute__((weak))
  51. void unicode_input_finish (void) {
  52. switch(input_mode) {
  53. case UC_OSX:
  54. case UC_WIN:
  55. unregister_code(KC_LALT);
  56. break;
  57. case UC_LNX:
  58. register_code(KC_SPC);
  59. unregister_code(KC_SPC);
  60. break;
  61. }
  62. }
  63. void register_hex(uint16_t hex) {
  64. for(int i = 3; i >= 0; i--) {
  65. uint8_t digit = ((hex >> (i*4)) & 0xF);
  66. register_code(hex_to_keycode(digit));
  67. unregister_code(hex_to_keycode(digit));
  68. }
  69. }
  70. bool process_unicode(uint16_t keycode, keyrecord_t *record) {
  71. if (keycode > QK_UNICODE && record->event.pressed) {
  72. if (first_flag == 0) {
  73. set_unicode_input_mode(eeprom_read_byte(EECONFIG_UNICODEMODE));
  74. first_flag = 1;
  75. }
  76. uint16_t unicode = keycode & 0x7FFF;
  77. unicode_input_start();
  78. register_hex(unicode);
  79. unicode_input_finish();
  80. }
  81. return true;
  82. }
  83. #ifdef UNICODEMAP_ENABLE
  84. __attribute__((weak))
  85. const uint32_t PROGMEM unicode_map[] = {
  86. };
  87. void register_hex32(uint32_t hex) {
  88. uint8_t onzerostart = 1;
  89. for(int i = 7; i >= 0; i--) {
  90. if (i <= 3) {
  91. onzerostart = 0;
  92. }
  93. uint8_t digit = ((hex >> (i*4)) & 0xF);
  94. if (digit == 0) {
  95. if (onzerostart == 0) {
  96. register_code(hex_to_keycode(digit));
  97. unregister_code(hex_to_keycode(digit));
  98. }
  99. } else {
  100. register_code(hex_to_keycode(digit));
  101. unregister_code(hex_to_keycode(digit));
  102. onzerostart = 0;
  103. }
  104. }
  105. }
  106. __attribute__((weak))
  107. void unicode_map_input_error() {}
  108. bool process_unicode_map(uint16_t keycode, keyrecord_t *record) {
  109. if ((keycode & QK_UNICODE_MAP) == QK_UNICODE_MAP && record->event.pressed) {
  110. const uint32_t* map = unicode_map;
  111. uint16_t index = keycode & 0x7FF;
  112. uint32_t code = pgm_read_dword_far(&map[index]);
  113. if ((code > 0xFFFF && input_mode == UC_OSX) || (code > 0xFFFFF && input_mode == UC_LNX)) {
  114. // when character is out of range supported by the OS
  115. unicode_map_input_error();
  116. } else {
  117. unicode_input_start();
  118. register_hex32(code);
  119. unicode_input_finish();
  120. }
  121. }
  122. return true;
  123. }
  124. #endif
  125. #ifdef UCIS_ENABLE
  126. qk_ucis_state_t qk_ucis_state;
  127. void qk_ucis_start(void) {
  128. qk_ucis_state.count = 0;
  129. qk_ucis_state.in_progress = true;
  130. qk_ucis_start_user();
  131. }
  132. __attribute__((weak))
  133. void qk_ucis_start_user(void) {
  134. unicode_input_start();
  135. register_hex(0x2328);
  136. unicode_input_finish();
  137. }
  138. static bool is_uni_seq(char *seq) {
  139. uint8_t i;
  140. for (i = 0; seq[i]; i++) {
  141. uint16_t code;
  142. if (('1' <= seq[i]) && (seq[i] <= '0'))
  143. code = seq[i] - '1' + KC_1;
  144. else
  145. code = seq[i] - 'a' + KC_A;
  146. if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
  147. return false;
  148. }
  149. return (qk_ucis_state.codes[i] == KC_ENT ||
  150. qk_ucis_state.codes[i] == KC_SPC);
  151. }
  152. __attribute__((weak))
  153. void qk_ucis_symbol_fallback (void) {
  154. for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
  155. uint8_t code = qk_ucis_state.codes[i];
  156. register_code(code);
  157. unregister_code(code);
  158. wait_ms(UNICODE_TYPE_DELAY);
  159. }
  160. }
  161. void register_ucis(const char *hex) {
  162. for(int i = 0; hex[i]; i++) {
  163. uint8_t kc = 0;
  164. char c = hex[i];
  165. switch (c) {
  166. case '0':
  167. kc = KC_0;
  168. break;
  169. case '1' ... '9':
  170. kc = c - '1' + KC_1;
  171. break;
  172. case 'a' ... 'f':
  173. kc = c - 'a' + KC_A;
  174. break;
  175. case 'A' ... 'F':
  176. kc = c - 'A' + KC_A;
  177. break;
  178. }
  179. if (kc) {
  180. register_code (kc);
  181. unregister_code (kc);
  182. wait_ms (UNICODE_TYPE_DELAY);
  183. }
  184. }
  185. }
  186. bool process_ucis (uint16_t keycode, keyrecord_t *record) {
  187. uint8_t i;
  188. if (!qk_ucis_state.in_progress)
  189. return true;
  190. if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
  191. !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
  192. return false;
  193. }
  194. if (!record->event.pressed)
  195. return true;
  196. qk_ucis_state.codes[qk_ucis_state.count] = keycode;
  197. qk_ucis_state.count++;
  198. if (keycode == KC_BSPC) {
  199. if (qk_ucis_state.count >= 2) {
  200. qk_ucis_state.count -= 2;
  201. return true;
  202. } else {
  203. qk_ucis_state.count--;
  204. return false;
  205. }
  206. }
  207. if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
  208. bool symbol_found = false;
  209. for (i = qk_ucis_state.count; i > 0; i--) {
  210. register_code (KC_BSPC);
  211. unregister_code (KC_BSPC);
  212. wait_ms(UNICODE_TYPE_DELAY);
  213. }
  214. if (keycode == KC_ESC) {
  215. qk_ucis_state.in_progress = false;
  216. return false;
  217. }
  218. unicode_input_start();
  219. for (i = 0; ucis_symbol_table[i].symbol; i++) {
  220. if (is_uni_seq (ucis_symbol_table[i].symbol)) {
  221. symbol_found = true;
  222. register_ucis(ucis_symbol_table[i].code + 2);
  223. break;
  224. }
  225. }
  226. if (!symbol_found) {
  227. qk_ucis_symbol_fallback();
  228. }
  229. unicode_input_finish();
  230. qk_ucis_state.in_progress = false;
  231. return false;
  232. }
  233. return true;
  234. }
  235. #endif