1 #include "process_unicode.h"
2 #include "action_util.h"
4 static uint8_t input_mode;
8 uint16_t hex_to_keycode(uint8_t hex)
12 } else if (hex < 0xA) {
13 return KC_1 + (hex - 0x1);
15 return KC_A + (hex - 0xA);
19 void set_unicode_input_mode(uint8_t os_target)
21 input_mode = os_target;
24 uint8_t get_unicode_input_mode(void) {
29 void unicode_input_start (void) {
31 mods = keyboard_report->mods;
33 // unregister all mods to start from clean state
34 if (mods & MOD_BIT(KC_LSFT)) unregister_code(KC_LSFT);
35 if (mods & MOD_BIT(KC_RSFT)) unregister_code(KC_RSFT);
36 if (mods & MOD_BIT(KC_LCTL)) unregister_code(KC_LCTL);
37 if (mods & MOD_BIT(KC_RCTL)) unregister_code(KC_RCTL);
38 if (mods & MOD_BIT(KC_LALT)) unregister_code(KC_LALT);
39 if (mods & MOD_BIT(KC_RALT)) unregister_code(KC_RALT);
40 if (mods & MOD_BIT(KC_LGUI)) unregister_code(KC_LGUI);
41 if (mods & MOD_BIT(KC_RGUI)) unregister_code(KC_RGUI);
45 register_code(KC_LALT);
48 register_code(KC_LCTL);
49 register_code(KC_LSFT);
51 unregister_code(KC_U);
52 unregister_code(KC_LSFT);
53 unregister_code(KC_LCTL);
56 register_code(KC_LALT);
57 register_code(KC_PPLS);
58 unregister_code(KC_PPLS);
61 register_code(KC_RALT);
62 unregister_code(KC_RALT);
64 unregister_code(KC_U);
66 wait_ms(UNICODE_TYPE_DELAY);
70 void unicode_input_finish (void) {
74 unregister_code(KC_LALT);
77 register_code(KC_SPC);
78 unregister_code(KC_SPC);
82 // reregister previously set mods
83 if (mods & MOD_BIT(KC_LSFT)) register_code(KC_LSFT);
84 if (mods & MOD_BIT(KC_RSFT)) register_code(KC_RSFT);
85 if (mods & MOD_BIT(KC_LCTL)) register_code(KC_LCTL);
86 if (mods & MOD_BIT(KC_RCTL)) register_code(KC_RCTL);
87 if (mods & MOD_BIT(KC_LALT)) register_code(KC_LALT);
88 if (mods & MOD_BIT(KC_RALT)) register_code(KC_RALT);
89 if (mods & MOD_BIT(KC_LGUI)) register_code(KC_LGUI);
90 if (mods & MOD_BIT(KC_RGUI)) register_code(KC_RGUI);
93 void register_hex(uint16_t hex) {
94 for(int i = 3; i >= 0; i--) {
95 uint8_t digit = ((hex >> (i*4)) & 0xF);
96 register_code(hex_to_keycode(digit));
97 unregister_code(hex_to_keycode(digit));
101 bool process_unicode(uint16_t keycode, keyrecord_t *record) {
102 if (keycode > QK_UNICODE && record->event.pressed) {
103 uint16_t unicode = keycode & 0x7FFF;
104 unicode_input_start();
105 register_hex(unicode);
106 unicode_input_finish();
111 #ifdef UNICODEMAP_ENABLE
112 __attribute__((weak))
113 const uint32_t PROGMEM unicode_map[] = {
116 void register_hex32(uint32_t hex) {
117 uint8_t onzerostart = 1;
118 for(int i = 7; i >= 0; i--) {
122 uint8_t digit = ((hex >> (i*4)) & 0xF);
124 if (onzerostart == 0) {
125 register_code(hex_to_keycode(digit));
126 unregister_code(hex_to_keycode(digit));
129 register_code(hex_to_keycode(digit));
130 unregister_code(hex_to_keycode(digit));
136 __attribute__((weak))
137 void unicode_map_input_error() {}
139 bool process_unicode_map(uint16_t keycode, keyrecord_t *record) {
140 if ((keycode & QK_UNICODE_MAP) == QK_UNICODE_MAP && record->event.pressed) {
141 const uint32_t* map = unicode_map;
142 uint16_t index = keycode & 0x7FF;
143 uint32_t code = pgm_read_dword_far(&map[index]);
144 if ((code > 0xFFFF && input_mode == UC_OSX) || (code > 0xFFFFF && input_mode == UC_LNX)) {
145 // when character is out of range supported by the OS
146 unicode_map_input_error();
148 unicode_input_start();
149 register_hex32(code);
150 unicode_input_finish();
158 qk_ucis_state_t qk_ucis_state;
160 void qk_ucis_start(void) {
161 qk_ucis_state.count = 0;
162 qk_ucis_state.in_progress = true;
164 qk_ucis_start_user();
167 __attribute__((weak))
168 void qk_ucis_start_user(void) {
169 unicode_input_start();
170 register_hex(0x2328);
171 unicode_input_finish();
174 static bool is_uni_seq(char *seq) {
177 for (i = 0; seq[i]; i++) {
179 if (('1' <= seq[i]) && (seq[i] <= '0'))
180 code = seq[i] - '1' + KC_1;
182 code = seq[i] - 'a' + KC_A;
184 if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
188 return (qk_ucis_state.codes[i] == KC_ENT ||
189 qk_ucis_state.codes[i] == KC_SPC);
192 __attribute__((weak))
193 void qk_ucis_symbol_fallback (void) {
194 for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
195 uint8_t code = qk_ucis_state.codes[i];
197 unregister_code(code);
198 wait_ms(UNICODE_TYPE_DELAY);
202 void register_ucis(const char *hex) {
203 for(int i = 0; hex[i]; i++) {
224 unregister_code (kc);
225 wait_ms (UNICODE_TYPE_DELAY);
230 bool process_ucis (uint16_t keycode, keyrecord_t *record) {
233 if (!qk_ucis_state.in_progress)
236 if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
237 !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
241 if (!record->event.pressed)
244 qk_ucis_state.codes[qk_ucis_state.count] = keycode;
245 qk_ucis_state.count++;
247 if (keycode == KC_BSPC) {
248 if (qk_ucis_state.count >= 2) {
249 qk_ucis_state.count -= 2;
252 qk_ucis_state.count--;
257 if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
258 bool symbol_found = false;
260 for (i = qk_ucis_state.count; i > 0; i--) {
261 register_code (KC_BSPC);
262 unregister_code (KC_BSPC);
263 wait_ms(UNICODE_TYPE_DELAY);
266 if (keycode == KC_ESC) {
267 qk_ucis_state.in_progress = false;
271 unicode_input_start();
272 for (i = 0; ucis_symbol_table[i].symbol; i++) {
273 if (is_uni_seq (ucis_symbol_table[i].symbol)) {
275 register_ucis(ucis_symbol_table[i].code + 2);
280 qk_ucis_symbol_fallback();
282 unicode_input_finish();
284 qk_ucis_state.in_progress = false;