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 && code <= 0x10ffff && input_mode == UC_OSX) {
120 // Convert to UTF-16 surrogate pair
122 uint32_t lo = code & 0x3ff;
123 uint32_t hi = (code & 0xffc00) >> 10;
124 unicode_input_start();
125 register_hex32(hi + 0xd800);
126 register_hex32(lo + 0xdc00);
127 unicode_input_finish();
128 } else if ((code > 0x10ffff && input_mode == UC_OSX) || (code > 0xFFFFF && input_mode == UC_LNX)) {
129 // when character is out of range supported by the OS
130 unicode_map_input_error();
132 unicode_input_start();
133 register_hex32(code);
134 unicode_input_finish();
142 qk_ucis_state_t qk_ucis_state;
144 void qk_ucis_start(void) {
145 qk_ucis_state.count = 0;
146 qk_ucis_state.in_progress = true;
148 qk_ucis_start_user();
151 __attribute__((weak))
152 void qk_ucis_start_user(void) {
153 unicode_input_start();
154 register_hex(0x2328);
155 unicode_input_finish();
158 static bool is_uni_seq(char *seq) {
161 for (i = 0; seq[i]; i++) {
163 if (('1' <= seq[i]) && (seq[i] <= '0'))
164 code = seq[i] - '1' + KC_1;
166 code = seq[i] - 'a' + KC_A;
168 if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
172 return (qk_ucis_state.codes[i] == KC_ENT ||
173 qk_ucis_state.codes[i] == KC_SPC);
176 __attribute__((weak))
177 void qk_ucis_symbol_fallback (void) {
178 for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
179 uint8_t code = qk_ucis_state.codes[i];
181 unregister_code(code);
182 wait_ms(UNICODE_TYPE_DELAY);
186 void register_ucis(const char *hex) {
187 for(int i = 0; hex[i]; i++) {
208 unregister_code (kc);
209 wait_ms (UNICODE_TYPE_DELAY);
214 bool process_ucis (uint16_t keycode, keyrecord_t *record) {
217 if (!qk_ucis_state.in_progress)
220 if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
221 !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
225 if (!record->event.pressed)
228 qk_ucis_state.codes[qk_ucis_state.count] = keycode;
229 qk_ucis_state.count++;
231 if (keycode == KC_BSPC) {
232 if (qk_ucis_state.count >= 2) {
233 qk_ucis_state.count -= 2;
236 qk_ucis_state.count--;
241 if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
242 bool symbol_found = false;
244 for (i = qk_ucis_state.count; i > 0; i--) {
245 register_code (KC_BSPC);
246 unregister_code (KC_BSPC);
247 wait_ms(UNICODE_TYPE_DELAY);
250 if (keycode == KC_ESC) {
251 qk_ucis_state.in_progress = false;
255 unicode_input_start();
256 for (i = 0; ucis_symbol_table[i].symbol; i++) {
257 if (is_uni_seq (ucis_symbol_table[i].symbol)) {
259 register_ucis(ucis_symbol_table[i].code + 2);
264 qk_ucis_symbol_fallback();
266 unicode_input_finish();
268 qk_ucis_state.in_progress = false;