keychron_qmk_firmware/quantum/process_keycode/process_ucis.c

125 lines
3.5 KiB
C
Raw Normal View History

/* 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/>.
*/
#include "process_ucis.h"
2022-09-12 21:49:04 +06:00
#include "unicode.h"
#include "keycode.h"
#include "wait.h"
2022-12-14 08:44:36 +06:00
ucis_state_t ucis_state;
2022-12-14 08:44:36 +06:00
void ucis_start(void) {
ucis_state.count = 0;
ucis_state.in_progress = true;
2022-12-14 08:44:36 +06:00
ucis_start_user();
}
2022-12-14 08:44:36 +06:00
__attribute__((weak)) void ucis_start_user(void) {
2022-09-12 21:49:04 +06:00
register_unicode(0x2328); // ⌨
}
2022-12-14 08:44:36 +06:00
__attribute__((weak)) void ucis_success(uint8_t symbol_index) {}
static bool is_uni_seq(char *seq) {
2019-08-31 00:19:03 +06:00
uint8_t i;
for (i = 0; seq[i]; i++) {
uint16_t keycode;
if ('1' <= seq[i] && seq[i] <= '0') {
keycode = seq[i] - '1' + KC_1;
} else {
keycode = seq[i] - 'a' + KC_A;
}
2022-12-14 08:44:36 +06:00
if (i > ucis_state.count || ucis_state.codes[i] != keycode) {
return false;
}
2019-08-31 00:19:03 +06:00
}
2022-12-14 08:44:36 +06:00
return ucis_state.codes[i] == KC_ENTER || ucis_state.codes[i] == KC_SPACE;
}
2022-12-14 08:44:36 +06:00
__attribute__((weak)) void ucis_symbol_fallback(void) {
for (uint8_t i = 0; i < ucis_state.count - 1; i++) {
tap_code(ucis_state.codes[i]);
2019-08-31 00:19:03 +06:00
}
}
2022-12-14 08:44:36 +06:00
__attribute__((weak)) void ucis_cancel(void) {}
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]);
}
}
2019-08-31 00:19:03 +06:00
bool process_ucis(uint16_t keycode, keyrecord_t *record) {
2022-12-14 08:44:36 +06:00
if (!ucis_state.in_progress || !record->event.pressed) {
return true;
}
2021-11-04 11:22:17 +06:00
bool special = keycode == KC_SPACE || keycode == KC_ENTER || keycode == KC_ESCAPE || keycode == KC_BACKSPACE;
2022-12-14 08:44:36 +06:00
if (ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH && !special) {
2019-08-31 00:19:03 +06:00
return false;
}
2022-12-14 08:44:36 +06:00
ucis_state.codes[ucis_state.count] = keycode;
ucis_state.count++;
switch (keycode) {
2021-11-04 11:22:17 +06:00
case KC_BACKSPACE:
2022-12-14 08:44:36 +06:00
if (ucis_state.count >= 2) {
ucis_state.count -= 2;
2020-06-22 07:21:48 +06:00
return true;
} else {
2022-12-14 08:44:36 +06:00
ucis_state.count--;
2020-06-22 07:21:48 +06:00
return false;
}
2021-11-04 11:22:17 +06:00
case KC_SPACE:
case KC_ENTER:
case KC_ESCAPE:
2022-12-14 08:44:36 +06:00
for (uint8_t i = 0; i < ucis_state.count; i++) {
2022-09-12 21:49:04 +06:00
tap_code(KC_BACKSPACE);
2020-06-22 07:21:48 +06:00
}
2019-08-31 00:19:03 +06:00
2021-11-04 11:22:17 +06:00
if (keycode == KC_ESCAPE) {
2022-12-14 08:44:36 +06:00
ucis_state.in_progress = false;
ucis_cancel();
2020-06-22 07:21:48 +06:00
return false;
}
2019-08-31 00:19:03 +06:00
2020-06-22 07:21:48 +06: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) {
2022-12-14 08:44:36 +06:00
ucis_success(i);
2020-06-22 07:21:48 +06:00
} else {
2022-12-14 08:44:36 +06:00
ucis_symbol_fallback();
2019-08-31 00:19:03 +06:00
}
2022-12-14 08:44:36 +06:00
ucis_state.in_progress = false;
2020-06-22 07:21:48 +06:00
return false;
2020-06-22 07:21:48 +06:00
default:
return true;
}
}