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 qk_ucis_state_t qk_ucis_state;
79 void qk_ucis_start(void) {
80 qk_ucis_state.count = 0;
81 qk_ucis_state.in_progress = true;
87 void qk_ucis_start_user(void) {
88 unicode_input_start();
90 unicode_input_finish();
93 static bool is_uni_seq(char *seq) {
96 for (i = 0; seq[i]; i++) {
98 if (('1' <= seq[i]) && (seq[i] <= '0'))
99 code = seq[i] - '1' + KC_1;
101 code = seq[i] - 'a' + KC_A;
103 if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
107 return (qk_ucis_state.codes[i] == KC_ENT ||
108 qk_ucis_state.codes[i] == KC_SPC);
111 __attribute__((weak))
112 void qk_ucis_symbol_fallback (void) {
113 for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
114 uint8_t code = qk_ucis_state.codes[i];
116 unregister_code(code);
117 wait_ms(UNICODE_TYPE_DELAY);
121 void register_ucis(const char *hex) {
122 for(int i = 0; hex[i]; i++) {
143 unregister_code (kc);
144 wait_ms (UNICODE_TYPE_DELAY);
149 bool process_ucis (uint16_t keycode, keyrecord_t *record) {
152 if (!qk_ucis_state.in_progress)
155 if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
156 !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
160 if (!record->event.pressed)
163 qk_ucis_state.codes[qk_ucis_state.count] = keycode;
164 qk_ucis_state.count++;
166 if (keycode == KC_BSPC) {
167 if (qk_ucis_state.count >= 2) {
168 qk_ucis_state.count -= 2;
171 qk_ucis_state.count--;
176 if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
177 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);
185 if (keycode == KC_ESC) {
186 qk_ucis_state.in_progress = false;
190 unicode_input_start();
191 for (i = 0; ucis_symbol_table[i].symbol; i++) {
192 if (is_uni_seq (ucis_symbol_table[i].symbol)) {
194 register_ucis(ucis_symbol_table[i].code + 2);
199 qk_ucis_symbol_fallback();
201 unicode_input_finish();
203 qk_ucis_state.in_progress = false;