2017-03-28 22:20:36 +00: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-15 21:36:31 +00:00
|
|
|
#include "process_ucis.h"
|
|
|
|
|
|
|
|
qk_ucis_state_t qk_ucis_state;
|
|
|
|
|
|
|
|
void qk_ucis_start(void) {
|
2019-08-30 18:19:03 +00:00
|
|
|
qk_ucis_state.count = 0;
|
|
|
|
qk_ucis_state.in_progress = true;
|
2017-02-15 21:36:31 +00:00
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
qk_ucis_start_user();
|
2017-02-15 21:36:31 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
__attribute__((weak)) void qk_ucis_start_user(void) {
|
|
|
|
unicode_input_start();
|
2020-06-18 07:07:34 +00:00
|
|
|
register_hex(0x2328); // ⌨
|
2019-08-30 18:19:03 +00:00
|
|
|
unicode_input_finish();
|
2017-02-15 21:36:31 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
__attribute__((weak)) void qk_ucis_success(uint8_t symbol_index) {}
|
2018-10-03 21:38:02 +00:00
|
|
|
|
2017-02-15 21:36:31 +00:00
|
|
|
static bool is_uni_seq(char *seq) {
|
2019-08-30 18:19:03 +00:00
|
|
|
uint8_t i;
|
|
|
|
for (i = 0; seq[i]; i++) {
|
2020-06-18 07:07:34 +00:00
|
|
|
uint16_t keycode;
|
|
|
|
if ('1' <= seq[i] && seq[i] <= '0') {
|
|
|
|
keycode = seq[i] - '1' + KC_1;
|
|
|
|
} else {
|
|
|
|
keycode = seq[i] - 'a' + KC_A;
|
|
|
|
}
|
|
|
|
if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != keycode) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-30 18:19:03 +00:00
|
|
|
}
|
2021-11-04 05:22:17 +00:00
|
|
|
return qk_ucis_state.codes[i] == KC_ENTER || qk_ucis_state.codes[i] == KC_SPACE;
|
2017-02-15 21:36:31 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
__attribute__((weak)) void qk_ucis_symbol_fallback(void) {
|
|
|
|
for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
|
2020-06-18 07:07:34 +00:00
|
|
|
uint8_t keycode = qk_ucis_state.codes[i];
|
|
|
|
register_code(keycode);
|
|
|
|
unregister_code(keycode);
|
2019-08-30 18:19:03 +00:00
|
|
|
wait_ms(UNICODE_TYPE_DELAY);
|
|
|
|
}
|
2017-02-15 21:36:31 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
__attribute__((weak)) void qk_ucis_cancel(void) {}
|
2019-07-16 05:53:04 +00:00
|
|
|
|
2020-06-18 07:07:34 +00:00
|
|
|
void register_ucis(const uint32_t *code_points) {
|
|
|
|
for (int i = 0; i < UCIS_MAX_CODE_POINTS && code_points[i]; i++) {
|
|
|
|
register_unicode(code_points[i]);
|
|
|
|
wait_ms(UNICODE_TYPE_DELAY);
|
2017-02-15 21:36:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
bool process_ucis(uint16_t keycode, keyrecord_t *record) {
|
2020-06-18 07:07:34 +00:00
|
|
|
if (!qk_ucis_state.in_progress || !record->event.pressed) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-15 21:36:31 +00:00
|
|
|
|
2021-11-04 05:22:17 +00:00
|
|
|
bool special = keycode == KC_SPACE || keycode == KC_ENTER || keycode == KC_ESCAPE || keycode == KC_BACKSPACE;
|
2020-06-18 07:07:34 +00:00
|
|
|
if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH && !special) {
|
2019-08-30 18:19:03 +00:00
|
|
|
return false;
|
2017-02-15 21:36:31 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
qk_ucis_state.codes[qk_ucis_state.count] = keycode;
|
|
|
|
qk_ucis_state.count++;
|
2017-02-15 21:36:31 +00:00
|
|
|
|
2020-06-18 07:07:34 +00:00
|
|
|
switch (keycode) {
|
2021-11-04 05:22:17 +00:00
|
|
|
case KC_BACKSPACE:
|
2020-06-22 01:21:48 +00:00
|
|
|
if (qk_ucis_state.count >= 2) {
|
|
|
|
qk_ucis_state.count -= 2;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
qk_ucis_state.count--;
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-15 21:36:31 +00:00
|
|
|
|
2021-11-04 05:22:17 +00:00
|
|
|
case KC_SPACE:
|
|
|
|
case KC_ENTER:
|
|
|
|
case KC_ESCAPE:
|
2020-06-22 01:21:48 +00:00
|
|
|
for (uint8_t i = 0; i < qk_ucis_state.count; i++) {
|
2021-11-04 05:22:17 +00:00
|
|
|
register_code(KC_BACKSPACE);
|
|
|
|
unregister_code(KC_BACKSPACE);
|
2020-06-22 01:21:48 +00:00
|
|
|
wait_ms(UNICODE_TYPE_DELAY);
|
|
|
|
}
|
2019-08-30 18:19:03 +00:00
|
|
|
|
2021-11-04 05:22:17 +00:00
|
|
|
if (keycode == KC_ESCAPE) {
|
2020-06-22 01:21:48 +00:00
|
|
|
qk_ucis_state.in_progress = false;
|
|
|
|
qk_ucis_cancel();
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-30 18:19:03 +00:00
|
|
|
|
2020-06-22 01:21:48 +00:00
|
|
|
uint8_t i;
|
|
|
|
bool symbol_found = false;
|
|
|
|
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_points);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (symbol_found) {
|
|
|
|
qk_ucis_success(i);
|
|
|
|
} else {
|
|
|
|
qk_ucis_symbol_fallback();
|
2019-08-30 18:19:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 01:21:48 +00:00
|
|
|
qk_ucis_state.in_progress = false;
|
|
|
|
return false;
|
2020-06-18 07:07:34 +00:00
|
|
|
|
2020-06-22 01:21:48 +00:00
|
|
|
default:
|
|
|
|
return true;
|
2018-10-03 21:38:02 +00:00
|
|
|
}
|
2017-03-28 22:20:36 +00:00
|
|
|
}
|