]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_ucis.c
Usbasploader bootloader option addition (#6304)
[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 void register_ucis(const char *hex) {
68   for(int i = 0; hex[i]; i++) {
69     uint8_t kc = 0;
70     char c = hex[i];
71
72     switch (c) {
73     case '0':
74       kc = KC_0;
75       break;
76     case '1' ... '9':
77       kc = c - '1' + KC_1;
78       break;
79     case 'a' ... 'f':
80       kc = c - 'a' + KC_A;
81       break;
82     case 'A' ... 'F':
83       kc = c - 'A' + KC_A;
84       break;
85     }
86
87     if (kc) {
88       register_code (kc);
89       unregister_code (kc);
90       wait_ms (UNICODE_TYPE_DELAY);
91     }
92   }
93 }
94
95 bool process_ucis (uint16_t keycode, keyrecord_t *record) {
96   uint8_t i;
97
98   if (!qk_ucis_state.in_progress)
99     return true;
100
101   if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
102       !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
103     return false;
104   }
105
106   if (!record->event.pressed)
107     return true;
108
109   qk_ucis_state.codes[qk_ucis_state.count] = keycode;
110   qk_ucis_state.count++;
111
112   if (keycode == KC_BSPC) {
113     if (qk_ucis_state.count >= 2) {
114       qk_ucis_state.count -= 2;
115       return true;
116     } else {
117       qk_ucis_state.count--;
118       return false;
119     }
120   }
121
122   if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
123     bool symbol_found = false;
124
125     for (i = qk_ucis_state.count; i > 0; i--) {
126       register_code (KC_BSPC);
127       unregister_code (KC_BSPC);
128       wait_ms(UNICODE_TYPE_DELAY);
129     }
130
131     if (keycode == KC_ESC) {
132       qk_ucis_state.in_progress = false;
133       return false;
134     }
135
136     unicode_input_start();
137     for (i = 0; ucis_symbol_table[i].symbol; i++) {
138       if (is_uni_seq (ucis_symbol_table[i].symbol)) {
139         symbol_found = true;
140         register_ucis(ucis_symbol_table[i].code + 2);
141         break;
142       }
143     }
144     if (!symbol_found) {
145       qk_ucis_symbol_fallback();
146     }
147     unicode_input_finish();
148
149     if (symbol_found) {
150       qk_ucis_success(i);
151     }
152
153     qk_ucis_state.in_progress = false;
154     return false;
155   }
156   return true;
157 }