1 /* Copyright 2017 Jack Humbert
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.
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.
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/>.
17 #include "process_ucis.h"
19 qk_ucis_state_t qk_ucis_state;
21 void qk_ucis_start(void) {
22 qk_ucis_state.count = 0;
23 qk_ucis_state.in_progress = true;
29 void qk_ucis_start_user(void) {
30 unicode_input_start();
32 unicode_input_finish();
36 void qk_ucis_success(uint8_t symbol_index) {
39 static bool is_uni_seq(char *seq) {
42 for (i = 0; seq[i]; i++) {
44 if (('1' <= seq[i]) && (seq[i] <= '0'))
45 code = seq[i] - '1' + KC_1;
47 code = seq[i] - 'a' + KC_A;
49 if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
53 return (qk_ucis_state.codes[i] == KC_ENT ||
54 qk_ucis_state.codes[i] == KC_SPC);
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];
62 unregister_code(code);
63 wait_ms(UNICODE_TYPE_DELAY);
67 void register_ucis(const char *hex) {
68 for(int i = 0; hex[i]; i++) {
90 wait_ms (UNICODE_TYPE_DELAY);
95 bool process_ucis (uint16_t keycode, keyrecord_t *record) {
98 if (!qk_ucis_state.in_progress)
101 if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
102 !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
106 if (!record->event.pressed)
109 qk_ucis_state.codes[qk_ucis_state.count] = keycode;
110 qk_ucis_state.count++;
112 if (keycode == KC_BSPC) {
113 if (qk_ucis_state.count >= 2) {
114 qk_ucis_state.count -= 2;
117 qk_ucis_state.count--;
122 if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
123 bool symbol_found = false;
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);
131 if (keycode == KC_ESC) {
132 qk_ucis_state.in_progress = false;
136 unicode_input_start();
137 for (i = 0; ucis_symbol_table[i].symbol; i++) {
138 if (is_uni_seq (ucis_symbol_table[i].symbol)) {
140 register_ucis(ucis_symbol_table[i].code + 2);
145 qk_ucis_symbol_fallback();
147 unicode_input_finish();
153 qk_ucis_state.in_progress = false;