]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_unicode.c
d8a0f667cd63d2b0aabfedc68ee2942eab688ff3
[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 void unicode_input_start (void) {
22   switch(input_mode) {
23   case UC_OSX:
24     register_code(KC_LALT);
25     break;
26   case UC_LNX:
27     register_code(KC_LCTL);
28     register_code(KC_LSFT);
29     register_code(KC_U);
30     unregister_code(KC_U);
31     unregister_code(KC_LSFT);
32     unregister_code(KC_LCTL);
33     break;
34   case UC_WIN:
35     register_code(KC_LALT);
36     register_code(KC_PPLS);
37     unregister_code(KC_PPLS);
38     break;
39   }
40 }
41
42 void unicode_input_finish (void) {
43   switch(input_mode) {
44   case UC_OSX:
45   case UC_WIN:
46     unregister_code(KC_LALT);
47     break;
48   case UC_LNX:
49     register_code(KC_SPC);
50     unregister_code(KC_SPC);
51     break;
52   }
53 }
54
55 void register_hex(uint16_t hex) {
56   for(int i = 3; i >= 0; i--) {
57     uint8_t digit = ((hex >> (i*4)) & 0xF);
58     register_code(hex_to_keycode(digit));
59     unregister_code(hex_to_keycode(digit));
60   }
61 }
62
63 bool process_unicode(uint16_t keycode, keyrecord_t *record) {
64   if (keycode > QK_UNICODE && record->event.pressed) {
65     uint16_t unicode = keycode & 0x7FFF;
66     unicode_input_start();
67     register_hex(unicode);
68     unicode_input_finish();
69   }
70   return true;
71 }
72
73 #ifdef UCIS_ENABLE
74 void qk_ucis_start(void) {
75   qk_ucis_state.count = 0;
76   qk_ucis_state.in_progress = true;
77
78   qk_ucis_start_user();
79 }
80
81 __attribute__((weak))
82 void qk_ucis_start_user(void) {
83   unicode_input_start();
84   register_hex(0x2328);
85   unicode_input_finish();
86 }
87
88 static bool is_uni_seq(char *seq) {
89   uint8_t i;
90
91   for (i = 0; seq[i]; i++) {
92     uint16_t code;
93     if (('1' <= seq[i]) && (seq[i] <= '0'))
94       code = seq[i] - '1' + KC_1;
95     else
96       code = seq[i] - 'a' + KC_A;
97
98     if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
99       return false;
100   }
101
102   return (qk_ucis_state.codes[i] == KC_ENT ||
103           qk_ucis_state.codes[i] == KC_SPC);
104 }
105
106 __attribute__((weak))
107 void qk_ucis_symbol_fallback (void) {
108   for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
109     uint8_t code = qk_ucis_state.codes[i];
110     register_code(code);
111     unregister_code(code);
112   }
113 }
114
115 void register_ucis(const char *hex) {
116   for(int i = 0; hex[i]; i++) {
117     uint8_t kc = 0;
118     char c = hex[i];
119
120     switch (c) {
121     case '0':
122       kc = KC_0;
123       break;
124     case '1' ... '9':
125       kc = c - '1' + KC_1;
126       break;
127     case 'a' ... 'f':
128       kc = c - 'a' + KC_A;
129       break;
130     case 'A' ... 'F':
131       kc = c - 'A' + KC_A;
132       break;
133     }
134
135     if (kc) {
136       register_code (kc);
137       unregister_code (kc);
138     }
139   }
140 }
141
142 bool process_ucis (uint16_t keycode, keyrecord_t *record) {
143   uint8_t i;
144
145   if (!qk_ucis_state.in_progress)
146     return true;
147
148   if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
149       !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
150     return false;
151   }
152
153   if (!record->event.pressed)
154     return true;
155
156   qk_ucis_state.codes[qk_ucis_state.count] = keycode;
157   qk_ucis_state.count++;
158
159   if (keycode == KC_BSPC) {
160     if (qk_ucis_state.count >= 2) {
161       qk_ucis_state.count -= 2;
162       return true;
163     } else {
164       qk_ucis_state.count--;
165       return false;
166     }
167   }
168
169   if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
170     bool symbol_found = false;
171
172     for (i = qk_ucis_state.count; i > 0; i--) {
173       register_code (KC_BSPC);
174       unregister_code (KC_BSPC);
175     }
176
177     if (keycode == KC_ESC) {
178       qk_ucis_state.in_progress = false;
179       return false;
180     }
181
182     unicode_input_start();
183     for (i = 0; ucis_symbol_table[i].symbol; i++) {
184       if (is_uni_seq (ucis_symbol_table[i].symbol)) {
185         symbol_found = true;
186         register_ucis(ucis_symbol_table[i].code + 2);
187         break;
188       }
189     }
190     if (!symbol_found) {
191       qk_ucis_symbol_fallback();
192     }
193     unicode_input_finish();
194
195     qk_ucis_state.in_progress = false;
196     return false;
197   }
198   return true;
199 }
200 #endif