1 #include "process_unicode.h"
3 static uint8_t input_mode;
5 uint16_t hex_to_keycode(uint8_t hex)
9 } else if (hex < 0xA) {
10 return KC_1 + (hex - 0x1);
12 return KC_A + (hex - 0xA);
16 void set_unicode_input_mode(uint8_t os_target)
18 input_mode = os_target;
22 void unicode_input_start (void) {
25 register_code(KC_LALT);
28 register_code(KC_LCTL);
29 register_code(KC_LSFT);
31 unregister_code(KC_U);
32 unregister_code(KC_LSFT);
33 unregister_code(KC_LCTL);
36 register_code(KC_LALT);
37 register_code(KC_PPLS);
38 unregister_code(KC_PPLS);
41 wait_ms(UNICODE_TYPE_DELAY);
45 void unicode_input_finish (void) {
49 unregister_code(KC_LALT);
52 register_code(KC_SPC);
53 unregister_code(KC_SPC);
58 void register_hex(uint16_t hex) {
59 for(int i = 3; i >= 0; i--) {
60 uint8_t digit = ((hex >> (i*4)) & 0xF);
61 register_code(hex_to_keycode(digit));
62 unregister_code(hex_to_keycode(digit));
66 bool process_unicode(uint16_t keycode, keyrecord_t *record) {
67 if (keycode > QK_UNICODE && record->event.pressed) {
68 uint16_t unicode = keycode & 0x7FFF;
69 unicode_input_start();
70 register_hex(unicode);
71 unicode_input_finish();
77 void qk_ucis_start(void) {
78 qk_ucis_state.count = 0;
79 qk_ucis_state.in_progress = true;
85 void qk_ucis_start_user(void) {
86 unicode_input_start();
88 unicode_input_finish();
91 static bool is_uni_seq(char *seq) {
94 for (i = 0; seq[i]; i++) {
96 if (('1' <= seq[i]) && (seq[i] <= '0'))
97 code = seq[i] - '1' + KC_1;
99 code = seq[i] - 'a' + KC_A;
101 if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
105 return (qk_ucis_state.codes[i] == KC_ENT ||
106 qk_ucis_state.codes[i] == KC_SPC);
109 __attribute__((weak))
110 void qk_ucis_symbol_fallback (void) {
111 for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
112 uint8_t code = qk_ucis_state.codes[i];
114 unregister_code(code);
115 wait_ms(UNICODE_TYPE_DELAY);
119 void register_ucis(const char *hex) {
120 for(int i = 0; hex[i]; i++) {
141 unregister_code (kc);
142 wait_ms (UNICODE_TYPE_DELAY);
147 bool process_ucis (uint16_t keycode, keyrecord_t *record) {
150 if (!qk_ucis_state.in_progress)
153 if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
154 !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
158 if (!record->event.pressed)
161 qk_ucis_state.codes[qk_ucis_state.count] = keycode;
162 qk_ucis_state.count++;
164 if (keycode == KC_BSPC) {
165 if (qk_ucis_state.count >= 2) {
166 qk_ucis_state.count -= 2;
169 qk_ucis_state.count--;
174 if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
175 bool symbol_found = false;
177 for (i = qk_ucis_state.count; i > 0; i--) {
178 register_code (KC_BSPC);
179 unregister_code (KC_BSPC);
180 wait_ms(UNICODE_TYPE_DELAY);
183 if (keycode == KC_ESC) {
184 qk_ucis_state.in_progress = false;
188 unicode_input_start();
189 for (i = 0; ucis_symbol_table[i].symbol; i++) {
190 if (is_uni_seq (ucis_symbol_table[i].symbol)) {
192 register_ucis(ucis_symbol_table[i].code + 2);
197 qk_ucis_symbol_fallback();
199 unicode_input_finish();
201 qk_ucis_state.in_progress = false;