]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_unicode.c
a71af5437bec2734440b06c7285027e1528152af
[qmk_firmware.git] / quantum / process_keycode / process_unicode.c
1 #include "process_unicode.h"
2
3 static uint8_t input_mode;
4
5 __attribute__((weak))
6 uint16_t hex_to_keycode(uint8_t hex)
7 {
8   if (hex == 0x0) {
9     return KC_0;
10   } else if (hex < 0xA) {
11     return KC_1 + (hex - 0x1);
12   } else {
13     return KC_A + (hex - 0xA);
14   }
15 }
16
17 void set_unicode_input_mode(uint8_t os_target)
18 {
19   input_mode = os_target;
20 }
21
22 uint8_t get_unicode_input_mode(void) {
23   return input_mode;
24 }
25
26 __attribute__((weak))
27 void unicode_input_start (void) {
28   switch(input_mode) {
29   case UC_OSX:
30     register_code(KC_LALT);
31     break;
32   case UC_LNX:
33     register_code(KC_LCTL);
34     register_code(KC_LSFT);
35     register_code(KC_U);
36     unregister_code(KC_U);
37     unregister_code(KC_LSFT);
38     unregister_code(KC_LCTL);
39     break;
40   case UC_WIN:
41     register_code(KC_LALT);
42     register_code(KC_PPLS);
43     unregister_code(KC_PPLS);
44     break;
45   }
46   wait_ms(UNICODE_TYPE_DELAY);
47 }
48
49 __attribute__((weak))
50 void unicode_input_finish (void) {
51   switch(input_mode) {
52   case UC_OSX:
53   case UC_WIN:
54     unregister_code(KC_LALT);
55     break;
56   case UC_LNX:
57     register_code(KC_SPC);
58     unregister_code(KC_SPC);
59     break;
60   }
61 }
62
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));
68   }
69 }
70
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();
77   }
78   return true;
79 }
80
81 #ifdef UNICODEMAP_ENABLE
82 __attribute__((weak))
83 const uint32_t PROGMEM unicode_map[] = {
84 };
85
86 void register_hex32(uint32_t hex) {
87   uint8_t onzerostart = 1;
88   for(int i = 7; i >= 0; i--) {
89     if (i <= 3) {
90       onzerostart = 0;
91     }
92     uint8_t digit = ((hex >> (i*4)) & 0xF);
93     if (digit == 0) {
94       if (onzerostart == 0) {
95         register_code(hex_to_keycode(digit));
96         unregister_code(hex_to_keycode(digit));
97       }
98     } else {
99       register_code(hex_to_keycode(digit));
100       unregister_code(hex_to_keycode(digit));
101       onzerostart = 0;
102     }
103   }
104 }
105
106 __attribute__((weak))
107 void unicode_map_input_error() {}
108
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();
117     } else {
118       unicode_input_start();
119       register_hex32(code);
120       unicode_input_finish();
121     }
122   }
123   return true;
124 }
125 #endif
126
127 #ifdef UCIS_ENABLE
128 qk_ucis_state_t qk_ucis_state;
129
130 void qk_ucis_start(void) {
131   qk_ucis_state.count = 0;
132   qk_ucis_state.in_progress = true;
133
134   qk_ucis_start_user();
135 }
136
137 __attribute__((weak))
138 void qk_ucis_start_user(void) {
139   unicode_input_start();
140   register_hex(0x2328);
141   unicode_input_finish();
142 }
143
144 static bool is_uni_seq(char *seq) {
145   uint8_t i;
146
147   for (i = 0; seq[i]; i++) {
148     uint16_t code;
149     if (('1' <= seq[i]) && (seq[i] <= '0'))
150       code = seq[i] - '1' + KC_1;
151     else
152       code = seq[i] - 'a' + KC_A;
153
154     if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
155       return false;
156   }
157
158   return (qk_ucis_state.codes[i] == KC_ENT ||
159           qk_ucis_state.codes[i] == KC_SPC);
160 }
161
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];
166     register_code(code);
167     unregister_code(code);
168     wait_ms(UNICODE_TYPE_DELAY);
169   }
170 }
171
172 void register_ucis(const char *hex) {
173   for(int i = 0; hex[i]; i++) {
174     uint8_t kc = 0;
175     char c = hex[i];
176
177     switch (c) {
178     case '0':
179       kc = KC_0;
180       break;
181     case '1' ... '9':
182       kc = c - '1' + KC_1;
183       break;
184     case 'a' ... 'f':
185       kc = c - 'a' + KC_A;
186       break;
187     case 'A' ... 'F':
188       kc = c - 'A' + KC_A;
189       break;
190     }
191
192     if (kc) {
193       register_code (kc);
194       unregister_code (kc);
195       wait_ms (UNICODE_TYPE_DELAY);
196     }
197   }
198 }
199
200 bool process_ucis (uint16_t keycode, keyrecord_t *record) {
201   uint8_t i;
202
203   if (!qk_ucis_state.in_progress)
204     return true;
205
206   if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
207       !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
208     return false;
209   }
210
211   if (!record->event.pressed)
212     return true;
213
214   qk_ucis_state.codes[qk_ucis_state.count] = keycode;
215   qk_ucis_state.count++;
216
217   if (keycode == KC_BSPC) {
218     if (qk_ucis_state.count >= 2) {
219       qk_ucis_state.count -= 2;
220       return true;
221     } else {
222       qk_ucis_state.count--;
223       return false;
224     }
225   }
226
227   if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
228     bool symbol_found = false;
229
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);
234     }
235
236     if (keycode == KC_ESC) {
237       qk_ucis_state.in_progress = false;
238       return false;
239     }
240
241     unicode_input_start();
242     for (i = 0; ucis_symbol_table[i].symbol; i++) {
243       if (is_uni_seq (ucis_symbol_table[i].symbol)) {
244         symbol_found = true;
245         register_ucis(ucis_symbol_table[i].code + 2);
246         break;
247       }
248     }
249     if (!symbol_found) {
250       qk_ucis_symbol_fallback();
251     }
252     unicode_input_finish();
253
254     qk_ucis_state.in_progress = false;
255     return false;
256   }
257   return true;
258 }
259 #endif