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();
81 #ifdef UNICODEMAP_ENABLE
83 const uint32_t PROGMEM unicode_map[] = {
86 void register_hex32(uint32_t hex) {
87 uint8_t onzerostart = 1;
88 for(int i = 7; i >= 0; i--) {
92 uint8_t digit = ((hex >> (i*4)) & 0xF);
94 if (onzerostart == 0) {
95 register_code(hex_to_keycode(digit));
96 unregister_code(hex_to_keycode(digit));
99 register_code(hex_to_keycode(digit));
100 unregister_code(hex_to_keycode(digit));
106 __attribute__((weak))
107 void unicode_map_input_error() {}
109 bool process_unicode_map(uint16_t keycode, keyrecord_t *record) {
110 if ((keycode & QK_UNICODE_MAP) == QK_UNICODE_MAP && record->event.pressed) {
111 const uint32_t* map = unicode_map;
112 uint16_t index = keycode & 0x7FF;
113 uint32_t code = pgm_read_dword_far(&map[index]);
114 if ((code > 0xFFFF && input_mode == UC_OSX) || (code > 0xFFFFF && input_mode == UC_LNX)) {
115 // when character is out of range supported by the OS
116 unicode_map_input_error();
118 unicode_input_start();
119 register_hex32(code);
120 unicode_input_finish();
128 qk_ucis_state_t qk_ucis_state;
130 void qk_ucis_start(void) {
131 qk_ucis_state.count = 0;
132 qk_ucis_state.in_progress = true;
134 qk_ucis_start_user();
137 __attribute__((weak))
138 void qk_ucis_start_user(void) {
139 unicode_input_start();
140 register_hex(0x2328);
141 unicode_input_finish();
144 static bool is_uni_seq(char *seq) {
147 for (i = 0; seq[i]; i++) {
149 if (('1' <= seq[i]) && (seq[i] <= '0'))
150 code = seq[i] - '1' + KC_1;
152 code = seq[i] - 'a' + KC_A;
154 if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
158 return (qk_ucis_state.codes[i] == KC_ENT ||
159 qk_ucis_state.codes[i] == KC_SPC);
162 __attribute__((weak))
163 void qk_ucis_symbol_fallback (void) {
164 for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
165 uint8_t code = qk_ucis_state.codes[i];
167 unregister_code(code);
168 wait_ms(UNICODE_TYPE_DELAY);
172 void register_ucis(const char *hex) {
173 for(int i = 0; hex[i]; i++) {
194 unregister_code (kc);
195 wait_ms (UNICODE_TYPE_DELAY);
200 bool process_ucis (uint16_t keycode, keyrecord_t *record) {
203 if (!qk_ucis_state.in_progress)
206 if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
207 !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
211 if (!record->event.pressed)
214 qk_ucis_state.codes[qk_ucis_state.count] = keycode;
215 qk_ucis_state.count++;
217 if (keycode == KC_BSPC) {
218 if (qk_ucis_state.count >= 2) {
219 qk_ucis_state.count -= 2;
222 qk_ucis_state.count--;
227 if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
228 bool symbol_found = false;
230 for (i = qk_ucis_state.count; i > 0; i--) {
231 register_code (KC_BSPC);
232 unregister_code (KC_BSPC);
233 wait_ms(UNICODE_TYPE_DELAY);
236 if (keycode == KC_ESC) {
237 qk_ucis_state.in_progress = false;
241 unicode_input_start();
242 for (i = 0; ucis_symbol_table[i].symbol; i++) {
243 if (is_uni_seq (ucis_symbol_table[i].symbol)) {
245 register_ucis(ucis_symbol_table[i].code + 2);
250 qk_ucis_symbol_fallback();
252 unicode_input_finish();
254 qk_ucis_state.in_progress = false;