1 #include "process_unicode.h"
3 static uint8_t input_mode;
6 uint16_t hex_to_keycode(uint8_t hex)
10 } else if (hex < 0xA) {
11 return KC_1 + (hex - 0x1);
13 return KC_A + (hex - 0xA);
17 void set_unicode_input_mode(uint8_t os_target)
19 input_mode = os_target;
22 uint8_t get_unicode_input_mode(void) {
27 void unicode_input_start (void) {
30 register_code(KC_LALT);
33 register_code(KC_LCTL);
34 register_code(KC_LSFT);
36 unregister_code(KC_U);
37 unregister_code(KC_LSFT);
38 unregister_code(KC_LCTL);
41 register_code(KC_LALT);
42 register_code(KC_PPLS);
43 unregister_code(KC_PPLS);
46 wait_ms(UNICODE_TYPE_DELAY);
50 void unicode_input_finish (void) {
54 unregister_code(KC_LALT);
57 register_code(KC_SPC);
58 unregister_code(KC_SPC);
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));
71 bool process_unicode(uint16_t keycode, keyrecord_t *record) {
72 if (keycode > QK_UNICODE && record->event.pressed) {
73 uint16_t unicode = keycode & 0x7FFF;
74 unicode_input_start();
75 register_hex(unicode);
76 unicode_input_finish();
82 qk_ucis_state_t qk_ucis_state;
84 void qk_ucis_start(void) {
85 qk_ucis_state.count = 0;
86 qk_ucis_state.in_progress = true;
92 void qk_ucis_start_user(void) {
93 unicode_input_start();
95 unicode_input_finish();
98 static bool is_uni_seq(char *seq) {
101 for (i = 0; seq[i]; i++) {
103 if (('1' <= seq[i]) && (seq[i] <= '0'))
104 code = seq[i] - '1' + KC_1;
106 code = seq[i] - 'a' + KC_A;
108 if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
112 return (qk_ucis_state.codes[i] == KC_ENT ||
113 qk_ucis_state.codes[i] == KC_SPC);
116 __attribute__((weak))
117 void qk_ucis_symbol_fallback (void) {
118 for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
119 uint8_t code = qk_ucis_state.codes[i];
121 unregister_code(code);
122 wait_ms(UNICODE_TYPE_DELAY);
126 void register_ucis(const char *hex) {
127 for(int i = 0; hex[i]; i++) {
148 unregister_code (kc);
149 wait_ms (UNICODE_TYPE_DELAY);
154 bool process_ucis (uint16_t keycode, keyrecord_t *record) {
157 if (!qk_ucis_state.in_progress)
160 if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
161 !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
165 if (!record->event.pressed)
168 qk_ucis_state.codes[qk_ucis_state.count] = keycode;
169 qk_ucis_state.count++;
171 if (keycode == KC_BSPC) {
172 if (qk_ucis_state.count >= 2) {
173 qk_ucis_state.count -= 2;
176 qk_ucis_state.count--;
181 if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
182 bool symbol_found = false;
184 for (i = qk_ucis_state.count; i > 0; i--) {
185 register_code (KC_BSPC);
186 unregister_code (KC_BSPC);
187 wait_ms(UNICODE_TYPE_DELAY);
190 if (keycode == KC_ESC) {
191 qk_ucis_state.in_progress = false;
195 unicode_input_start();
196 for (i = 0; ucis_symbol_table[i].symbol; i++) {
197 if (is_uni_seq (ucis_symbol_table[i].symbol)) {
199 register_ucis(ucis_symbol_table[i].code + 2);
204 qk_ucis_symbol_fallback();
206 unicode_input_finish();
208 qk_ucis_state.in_progress = false;