2017-03-29 04:20:36 +06:00
|
|
|
/* Copyright 2017 Jack Humbert
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-02-16 03:36:31 +06:00
|
|
|
#include "process_ucis.h"
|
|
|
|
|
|
|
|
qk_ucis_state_t qk_ucis_state;
|
|
|
|
|
|
|
|
void qk_ucis_start(void) {
|
2019-08-31 00:19:03 +06:00
|
|
|
qk_ucis_state.count = 0;
|
|
|
|
qk_ucis_state.in_progress = true;
|
2017-02-16 03:36:31 +06:00
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
qk_ucis_start_user();
|
2017-02-16 03:36:31 +06:00
|
|
|
}
|
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
__attribute__((weak)) void qk_ucis_start_user(void) {
|
|
|
|
unicode_input_start();
|
|
|
|
register_hex(0x2328);
|
|
|
|
unicode_input_finish();
|
2017-02-16 03:36:31 +06:00
|
|
|
}
|
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
__attribute__((weak)) void qk_ucis_success(uint8_t symbol_index) {}
|
2018-10-04 03:38:02 +06:00
|
|
|
|
2017-02-16 03:36:31 +06:00
|
|
|
static bool is_uni_seq(char *seq) {
|
2019-08-31 00:19:03 +06:00
|
|
|
uint8_t i;
|
2017-02-16 03:36:31 +06:00
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
for (i = 0; seq[i]; i++) {
|
|
|
|
uint16_t code;
|
|
|
|
if (('1' <= seq[i]) && (seq[i] <= '0'))
|
|
|
|
code = seq[i] - '1' + KC_1;
|
|
|
|
else
|
|
|
|
code = seq[i] - 'a' + KC_A;
|
2017-02-16 03:36:31 +06:00
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code) return false;
|
|
|
|
}
|
2017-02-16 03:36:31 +06:00
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
return (qk_ucis_state.codes[i] == KC_ENT || qk_ucis_state.codes[i] == KC_SPC);
|
2017-02-16 03:36:31 +06:00
|
|
|
}
|
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
__attribute__((weak)) void qk_ucis_symbol_fallback(void) {
|
|
|
|
for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
|
|
|
|
uint8_t code = qk_ucis_state.codes[i];
|
|
|
|
register_code(code);
|
|
|
|
unregister_code(code);
|
|
|
|
wait_ms(UNICODE_TYPE_DELAY);
|
|
|
|
}
|
2017-02-16 03:36:31 +06:00
|
|
|
}
|
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
__attribute__((weak)) void qk_ucis_cancel(void) {}
|
2019-07-16 11:53:04 +06:00
|
|
|
|
2017-02-16 03:36:31 +06:00
|
|
|
void register_ucis(const char *hex) {
|
2019-08-31 00:19:03 +06:00
|
|
|
for (int i = 0; hex[i]; i++) {
|
|
|
|
uint8_t kc = 0;
|
|
|
|
char c = hex[i];
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case '0':
|
|
|
|
kc = KC_0;
|
|
|
|
break;
|
|
|
|
case '1' ... '9':
|
|
|
|
kc = c - '1' + KC_1;
|
|
|
|
break;
|
|
|
|
case 'a' ... 'f':
|
|
|
|
kc = c - 'a' + KC_A;
|
|
|
|
break;
|
|
|
|
case 'A' ... 'F':
|
|
|
|
kc = c - 'A' + KC_A;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kc) {
|
|
|
|
register_code(kc);
|
|
|
|
unregister_code(kc);
|
|
|
|
wait_ms(UNICODE_TYPE_DELAY);
|
|
|
|
}
|
2017-02-16 03:36:31 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
bool process_ucis(uint16_t keycode, keyrecord_t *record) {
|
|
|
|
uint8_t i;
|
2018-11-20 06:37:49 +06:00
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
if (!qk_ucis_state.in_progress) return true;
|
2017-02-16 03:36:31 +06:00
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH && !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
|
|
|
|
return false;
|
2017-02-16 03:36:31 +06:00
|
|
|
}
|
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
if (!record->event.pressed) return true;
|
2017-02-16 03:36:31 +06:00
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
qk_ucis_state.codes[qk_ucis_state.count] = keycode;
|
|
|
|
qk_ucis_state.count++;
|
2017-02-16 03:36:31 +06:00
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
if (keycode == KC_BSPC) {
|
|
|
|
if (qk_ucis_state.count >= 2) {
|
|
|
|
qk_ucis_state.count -= 2;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
qk_ucis_state.count--;
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-16 03:36:31 +06:00
|
|
|
}
|
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
|
|
|
|
bool symbol_found = false;
|
|
|
|
|
|
|
|
for (i = qk_ucis_state.count; i > 0; i--) {
|
|
|
|
register_code(KC_BSPC);
|
|
|
|
unregister_code(KC_BSPC);
|
|
|
|
wait_ms(UNICODE_TYPE_DELAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keycode == KC_ESC) {
|
|
|
|
qk_ucis_state.in_progress = false;
|
|
|
|
qk_ucis_cancel();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unicode_input_start();
|
|
|
|
for (i = 0; ucis_symbol_table[i].symbol; i++) {
|
|
|
|
if (is_uni_seq(ucis_symbol_table[i].symbol)) {
|
|
|
|
symbol_found = true;
|
|
|
|
register_ucis(ucis_symbol_table[i].code + 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!symbol_found) {
|
|
|
|
qk_ucis_symbol_fallback();
|
|
|
|
}
|
|
|
|
unicode_input_finish();
|
|
|
|
|
|
|
|
if (symbol_found) {
|
|
|
|
qk_ucis_success(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
qk_ucis_state.in_progress = false;
|
|
|
|
return false;
|
2018-10-04 03:38:02 +06:00
|
|
|
}
|
2019-08-31 00:19:03 +06:00
|
|
|
return true;
|
2017-03-29 04:20:36 +06:00
|
|
|
}
|