]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_ucis.c
Refactor quantum/split_common/i2c.c, quantum/split_common/serial.c (#4522)
[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   unicode_input_mode_init();
99
100   if (!qk_ucis_state.in_progress)
101     return true;
102
103   if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
104       !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
105     return false;
106   }
107
108   if (!record->event.pressed)
109     return true;
110
111   qk_ucis_state.codes[qk_ucis_state.count] = keycode;
112   qk_ucis_state.count++;
113
114   if (keycode == KC_BSPC) {
115     if (qk_ucis_state.count >= 2) {
116       qk_ucis_state.count -= 2;
117       return true;
118     } else {
119       qk_ucis_state.count--;
120       return false;
121     }
122   }
123
124   if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
125     bool symbol_found = false;
126
127     for (i = qk_ucis_state.count; i > 0; i--) {
128       register_code (KC_BSPC);
129       unregister_code (KC_BSPC);
130       wait_ms(UNICODE_TYPE_DELAY);
131     }
132
133     if (keycode == KC_ESC) {
134       qk_ucis_state.in_progress = false;
135       return false;
136     }
137
138     unicode_input_start();
139     for (i = 0; ucis_symbol_table[i].symbol; i++) {
140       if (is_uni_seq (ucis_symbol_table[i].symbol)) {
141         symbol_found = true;
142         register_ucis(ucis_symbol_table[i].code + 2);
143         break;
144       }
145     }
146     if (!symbol_found) {
147       qk_ucis_symbol_fallback();
148     }
149     unicode_input_finish();
150
151     if (symbol_found) {
152       qk_ucis_success(i);
153     }
154
155     qk_ucis_state.in_progress = false;
156     return false;
157   }
158   return true;
159 }