]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_ucis.c
Add user-overridable callback for cancelling UCIS input (#5564)
[qmk_firmware.git] / quantum / process_keycode / process_ucis.c
1 /* Copyright 2017 Jack Humbert
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 #include "process_ucis.h"
18
19 qk_ucis_state_t qk_ucis_state;
20
21 void qk_ucis_start(void) {
22   qk_ucis_state.count = 0;
23   qk_ucis_state.in_progress = true;
24
25   qk_ucis_start_user();
26 }
27
28 __attribute__((weak))
29 void qk_ucis_start_user(void) {
30   unicode_input_start();
31   register_hex(0x2328);
32   unicode_input_finish();
33 }
34
35 __attribute__((weak))
36 void qk_ucis_success(uint8_t symbol_index) {
37 }
38
39 static bool is_uni_seq(char *seq) {
40   uint8_t i;
41
42   for (i = 0; seq[i]; i++) {
43     uint16_t code;
44     if (('1' <= seq[i]) && (seq[i] <= '0'))
45       code = seq[i] - '1' + KC_1;
46     else
47       code = seq[i] - 'a' + KC_A;
48
49     if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
50       return false;
51   }
52
53   return (qk_ucis_state.codes[i] == KC_ENT ||
54           qk_ucis_state.codes[i] == KC_SPC);
55 }
56
57 __attribute__((weak))
58 void qk_ucis_symbol_fallback (void) {
59   for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
60     uint8_t code = qk_ucis_state.codes[i];
61     register_code(code);
62     unregister_code(code);
63     wait_ms(UNICODE_TYPE_DELAY);
64   }
65 }
66
67 __attribute__((weak))
68 void qk_ucis_cancel(void) {
69 }
70
71 void register_ucis(const char *hex) {
72   for(int i = 0; hex[i]; i++) {
73     uint8_t kc = 0;
74     char c = hex[i];
75
76     switch (c) {
77     case '0':
78       kc = KC_0;
79       break;
80     case '1' ... '9':
81       kc = c - '1' + KC_1;
82       break;
83     case 'a' ... 'f':
84       kc = c - 'a' + KC_A;
85       break;
86     case 'A' ... 'F':
87       kc = c - 'A' + KC_A;
88       break;
89     }
90
91     if (kc) {
92       register_code (kc);
93       unregister_code (kc);
94       wait_ms (UNICODE_TYPE_DELAY);
95     }
96   }
97 }
98
99 bool process_ucis (uint16_t keycode, keyrecord_t *record) {
100   uint8_t i;
101
102   if (!qk_ucis_state.in_progress)
103     return true;
104
105   if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
106       !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
107     return false;
108   }
109
110   if (!record->event.pressed)
111     return true;
112
113   qk_ucis_state.codes[qk_ucis_state.count] = keycode;
114   qk_ucis_state.count++;
115
116   if (keycode == KC_BSPC) {
117     if (qk_ucis_state.count >= 2) {
118       qk_ucis_state.count -= 2;
119       return true;
120     } else {
121       qk_ucis_state.count--;
122       return false;
123     }
124   }
125
126   if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
127     bool symbol_found = false;
128
129     for (i = qk_ucis_state.count; i > 0; i--) {
130       register_code (KC_BSPC);
131       unregister_code (KC_BSPC);
132       wait_ms(UNICODE_TYPE_DELAY);
133     }
134
135     if (keycode == KC_ESC) {
136       qk_ucis_state.in_progress = false;
137       qk_ucis_cancel();
138       return false;
139     }
140
141     unicode_input_start();
142     for (i = 0; ucis_symbol_table[i].symbol; i++) {
143       if (is_uni_seq (ucis_symbol_table[i].symbol)) {
144         symbol_found = true;
145         register_ucis(ucis_symbol_table[i].code + 2);
146         break;
147       }
148     }
149     if (!symbol_found) {
150       qk_ucis_symbol_fallback();
151     }
152     unicode_input_finish();
153
154     if (symbol_found) {
155       qk_ucis_success(i);
156     }
157
158     qk_ucis_state.in_progress = false;
159     return false;
160   }
161   return true;
162 }