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;
21 uint8_t get_unicode_input_mode(void) {
26 void unicode_input_start (void) {
29 register_code(KC_LALT);
32 register_code(KC_LCTL);
33 register_code(KC_LSFT);
35 unregister_code(KC_U);
36 unregister_code(KC_LSFT);
37 unregister_code(KC_LCTL);
40 register_code(KC_LALT);
41 register_code(KC_PPLS);
42 unregister_code(KC_PPLS);
45 wait_ms(UNICODE_TYPE_DELAY);
49 void unicode_input_finish (void) {
53 unregister_code(KC_LALT);
56 register_code(KC_SPC);
57 unregister_code(KC_SPC);
62 void register_hex(uint16_t hex) {
63 for(int i = 3; i >= 0; i--) {
64 uint8_t digit = ((hex >> (i*4)) & 0xF);
65 register_code(hex_to_keycode(digit));
66 unregister_code(hex_to_keycode(digit));
70 bool process_unicode(uint16_t keycode, keyrecord_t *record) {
71 if (keycode > QK_UNICODE && record->event.pressed) {
72 uint16_t unicode = keycode & 0x7FFF;
73 unicode_input_start();
74 register_hex(unicode);
75 unicode_input_finish();
81 qk_ucis_state_t qk_ucis_state;
83 void qk_ucis_start(void) {
84 qk_ucis_state.count = 0;
85 qk_ucis_state.in_progress = true;
91 void qk_ucis_start_user(void) {
92 unicode_input_start();
94 unicode_input_finish();
97 static bool is_uni_seq(char *seq) {
100 for (i = 0; seq[i]; i++) {
102 if (('1' <= seq[i]) && (seq[i] <= '0'))
103 code = seq[i] - '1' + KC_1;
105 code = seq[i] - 'a' + KC_A;
107 if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
111 return (qk_ucis_state.codes[i] == KC_ENT ||
112 qk_ucis_state.codes[i] == KC_SPC);
115 __attribute__((weak))
116 void qk_ucis_symbol_fallback (void) {
117 for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
118 uint8_t code = qk_ucis_state.codes[i];
120 unregister_code(code);
121 wait_ms(UNICODE_TYPE_DELAY);
125 void register_ucis(const char *hex) {
126 for(int i = 0; hex[i]; i++) {
147 unregister_code (kc);
148 wait_ms (UNICODE_TYPE_DELAY);
153 bool process_ucis (uint16_t keycode, keyrecord_t *record) {
156 if (!qk_ucis_state.in_progress)
159 if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
160 !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
164 if (!record->event.pressed)
167 qk_ucis_state.codes[qk_ucis_state.count] = keycode;
168 qk_ucis_state.count++;
170 if (keycode == KC_BSPC) {
171 if (qk_ucis_state.count >= 2) {
172 qk_ucis_state.count -= 2;
175 qk_ucis_state.count--;
180 if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
181 bool symbol_found = false;
183 for (i = qk_ucis_state.count; i > 0; i--) {
184 register_code (KC_BSPC);
185 unregister_code (KC_BSPC);
186 wait_ms(UNICODE_TYPE_DELAY);
189 if (keycode == KC_ESC) {
190 qk_ucis_state.in_progress = false;
194 unicode_input_start();
195 for (i = 0; ucis_symbol_table[i].symbol; i++) {
196 if (is_uni_seq (ucis_symbol_table[i].symbol)) {
198 register_ucis(ucis_symbol_table[i].code + 2);
203 qk_ucis_symbol_fallback();
205 unicode_input_finish();
207 qk_ucis_state.in_progress = false;