]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_unicode.c
Merge branch 'master' into coderkun_neo2
[qmk_firmware.git] / quantum / process_keycode / process_unicode.c
1 #include "process_unicode.h"
2
3 static uint8_t input_mode;
4
5 uint16_t hex_to_keycode(uint8_t hex)
6 {
7   if (hex == 0x0) {
8     return KC_0;
9   } else if (hex < 0xA) {
10     return KC_1 + (hex - 0x1);
11   } else {
12     return KC_A + (hex - 0xA);
13   }
14 }
15
16 void set_unicode_input_mode(uint8_t os_target)
17 {
18   input_mode = os_target;
19 }
20
21 uint8_t get_unicode_input_mode(void) {
22   return input_mode;
23 }
24
25 __attribute__((weak))
26 void unicode_input_start (void) {
27   switch(input_mode) {
28   case UC_OSX:
29     register_code(KC_LALT);
30     break;
31   case UC_LNX:
32     register_code(KC_LCTL);
33     register_code(KC_LSFT);
34     register_code(KC_U);
35     unregister_code(KC_U);
36     unregister_code(KC_LSFT);
37     unregister_code(KC_LCTL);
38     break;
39   case UC_WIN:
40     register_code(KC_LALT);
41     register_code(KC_PPLS);
42     unregister_code(KC_PPLS);
43     break;
44   }
45   wait_ms(UNICODE_TYPE_DELAY);
46 }
47
48 __attribute__((weak))
49 void unicode_input_finish (void) {
50   switch(input_mode) {
51   case UC_OSX:
52   case UC_WIN:
53     unregister_code(KC_LALT);
54     break;
55   case UC_LNX:
56     register_code(KC_SPC);
57     unregister_code(KC_SPC);
58     break;
59   }
60 }
61
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));
67   }
68 }
69
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();
76   }
77   return true;
78 }
79
80 #ifdef UCIS_ENABLE
81 qk_ucis_state_t qk_ucis_state;
82
83 void qk_ucis_start(void) {
84   qk_ucis_state.count = 0;
85   qk_ucis_state.in_progress = true;
86
87   qk_ucis_start_user();
88 }
89
90 __attribute__((weak))
91 void qk_ucis_start_user(void) {
92   unicode_input_start();
93   register_hex(0x2328);
94   unicode_input_finish();
95 }
96
97 static bool is_uni_seq(char *seq) {
98   uint8_t i;
99
100   for (i = 0; seq[i]; i++) {
101     uint16_t code;
102     if (('1' <= seq[i]) && (seq[i] <= '0'))
103       code = seq[i] - '1' + KC_1;
104     else
105       code = seq[i] - 'a' + KC_A;
106
107     if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
108       return false;
109   }
110
111   return (qk_ucis_state.codes[i] == KC_ENT ||
112           qk_ucis_state.codes[i] == KC_SPC);
113 }
114
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];
119     register_code(code);
120     unregister_code(code);
121     wait_ms(UNICODE_TYPE_DELAY);
122   }
123 }
124
125 void register_ucis(const char *hex) {
126   for(int i = 0; hex[i]; i++) {
127     uint8_t kc = 0;
128     char c = hex[i];
129
130     switch (c) {
131     case '0':
132       kc = KC_0;
133       break;
134     case '1' ... '9':
135       kc = c - '1' + KC_1;
136       break;
137     case 'a' ... 'f':
138       kc = c - 'a' + KC_A;
139       break;
140     case 'A' ... 'F':
141       kc = c - 'A' + KC_A;
142       break;
143     }
144
145     if (kc) {
146       register_code (kc);
147       unregister_code (kc);
148       wait_ms (UNICODE_TYPE_DELAY);
149     }
150   }
151 }
152
153 bool process_ucis (uint16_t keycode, keyrecord_t *record) {
154   uint8_t i;
155
156   if (!qk_ucis_state.in_progress)
157     return true;
158
159   if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
160       !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
161     return false;
162   }
163
164   if (!record->event.pressed)
165     return true;
166
167   qk_ucis_state.codes[qk_ucis_state.count] = keycode;
168   qk_ucis_state.count++;
169
170   if (keycode == KC_BSPC) {
171     if (qk_ucis_state.count >= 2) {
172       qk_ucis_state.count -= 2;
173       return true;
174     } else {
175       qk_ucis_state.count--;
176       return false;
177     }
178   }
179
180   if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
181     bool symbol_found = false;
182
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);
187     }
188
189     if (keycode == KC_ESC) {
190       qk_ucis_state.in_progress = false;
191       return false;
192     }
193
194     unicode_input_start();
195     for (i = 0; ucis_symbol_table[i].symbol; i++) {
196       if (is_uni_seq (ucis_symbol_table[i].symbol)) {
197         symbol_found = true;
198         register_ucis(ucis_symbol_table[i].code + 2);
199         break;
200       }
201     }
202     if (!symbol_found) {
203       qk_ucis_symbol_fallback();
204     }
205     unicode_input_finish();
206
207     qk_ucis_state.in_progress = false;
208     return false;
209   }
210   return true;
211 }
212 #endif