process_unicode.c 7.0 KB

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