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 register_code(KC_RALT);
47 unregister_code(KC_RALT);
49 unregister_code(KC_U);
51 wait_ms(UNICODE_TYPE_DELAY);
55 void unicode_input_finish (void) {
59 unregister_code(KC_LALT);
62 register_code(KC_SPC);
63 unregister_code(KC_SPC);
68 void register_hex(uint16_t hex) {
69 for(int i = 3; i >= 0; i--) {
70 uint8_t digit = ((hex >> (i*4)) & 0xF);
71 register_code(hex_to_keycode(digit));
72 unregister_code(hex_to_keycode(digit));
76 bool process_unicode(uint16_t keycode, keyrecord_t *record) {
77 if (keycode > QK_UNICODE && record->event.pressed) {
78 uint16_t unicode = keycode & 0x7FFF;
79 unicode_input_start();
80 register_hex(unicode);
81 unicode_input_finish();
86 #ifdef UNICODEMAP_ENABLE
88 const uint32_t PROGMEM unicode_map[] = {
91 void register_hex32(uint32_t hex) {
92 uint8_t onzerostart = 1;
93 for(int i = 7; i >= 0; i--) {
97 uint8_t digit = ((hex >> (i*4)) & 0xF);
99 if (onzerostart == 0) {
100 register_code(hex_to_keycode(digit));
101 unregister_code(hex_to_keycode(digit));
104 register_code(hex_to_keycode(digit));
105 unregister_code(hex_to_keycode(digit));
111 __attribute__((weak))
112 void unicode_map_input_error() {}
114 bool process_unicode_map(uint16_t keycode, keyrecord_t *record) {
115 if ((keycode & QK_UNICODE_MAP) == QK_UNICODE_MAP && record->event.pressed) {
116 const uint32_t* map = unicode_map;
117 uint16_t index = keycode & 0x7FF;
118 uint32_t code = pgm_read_dword_far(&map[index]);
119 if ((code > 0xFFFF && input_mode == UC_OSX) || (code > 0xFFFFF && input_mode == UC_LNX)) {
120 // when character is out of range supported by the OS
121 unicode_map_input_error();
123 unicode_input_start();
124 register_hex32(code);
125 unicode_input_finish();
133 qk_ucis_state_t qk_ucis_state;
135 void qk_ucis_start(void) {
136 qk_ucis_state.count = 0;
137 qk_ucis_state.in_progress = true;
139 qk_ucis_start_user();
142 __attribute__((weak))
143 void qk_ucis_start_user(void) {
144 unicode_input_start();
145 register_hex(0x2328);
146 unicode_input_finish();
149 static bool is_uni_seq(char *seq) {
152 for (i = 0; seq[i]; i++) {
154 if (('1' <= seq[i]) && (seq[i] <= '0'))
155 code = seq[i] - '1' + KC_1;
157 code = seq[i] - 'a' + KC_A;
159 if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
163 return (qk_ucis_state.codes[i] == KC_ENT ||
164 qk_ucis_state.codes[i] == KC_SPC);
167 __attribute__((weak))
168 void qk_ucis_symbol_fallback (void) {
169 for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
170 uint8_t code = qk_ucis_state.codes[i];
172 unregister_code(code);
173 wait_ms(UNICODE_TYPE_DELAY);
177 void register_ucis(const char *hex) {
178 for(int i = 0; hex[i]; i++) {
199 unregister_code (kc);
200 wait_ms (UNICODE_TYPE_DELAY);
205 bool process_ucis (uint16_t keycode, keyrecord_t *record) {
208 if (!qk_ucis_state.in_progress)
211 if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
212 !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
216 if (!record->event.pressed)
219 qk_ucis_state.codes[qk_ucis_state.count] = keycode;
220 qk_ucis_state.count++;
222 if (keycode == KC_BSPC) {
223 if (qk_ucis_state.count >= 2) {
224 qk_ucis_state.count -= 2;
227 qk_ucis_state.count--;
232 if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
233 bool symbol_found = false;
235 for (i = qk_ucis_state.count; i > 0; i--) {
236 register_code (KC_BSPC);
237 unregister_code (KC_BSPC);
238 wait_ms(UNICODE_TYPE_DELAY);
241 if (keycode == KC_ESC) {
242 qk_ucis_state.in_progress = false;
246 unicode_input_start();
247 for (i = 0; ucis_symbol_table[i].symbol; i++) {
248 if (is_uni_seq (ucis_symbol_table[i].symbol)) {
250 register_ucis(ucis_symbol_table[i].code + 2);
255 qk_ucis_symbol_fallback();
257 unicode_input_finish();
259 qk_ucis_state.in_progress = false;