ucis.c
1940 bytes
1// Copyright 2023 QMK
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#include "ucis.h"
5#include "unicode.h"
6#include "action.h"
7
8uint8_t count = 0;
9bool active = false;
10char input[UCIS_MAX_INPUT_LENGTH] = {0};
11
12void ucis_start(void) {
13 count = 0;
14 active = true;
15
16 register_unicode(0x2328); // ⌨
17}
18
19bool ucis_active(void) {
20 return active;
21}
22
23uint8_t ucis_count(void) {
24 return count;
25}
26
27static char keycode_to_char(uint16_t keycode) {
28 if (keycode >= KC_A && keycode <= KC_Z) {
29 return 'a' + (keycode - KC_A);
30 } else if (keycode >= KC_1 && keycode <= KC_9) {
31 return '1' + (keycode - KC_1);
32 } else if (keycode == KC_0) {
33 return '0';
34 }
35 return 0;
36}
37
38bool ucis_add(uint16_t keycode) {
39 char c = keycode_to_char(keycode);
40 if (c) {
41 input[count++] = c;
42 return true;
43 }
44 return false;
45}
46
47bool ucis_remove_last(void) {
48 if (count) {
49 count--;
50 return true;
51 }
52
53 return false;
54}
55
56static bool match_mnemonic(char *mnemonic) {
57 for (uint8_t i = 0; input[i]; i++) {
58 if (i > count || input[i] != mnemonic[i]) {
59 return false;
60 }
61 }
62 return true;
63}
64
65void ucis_finish(void) {
66 uint8_t i = 0;
67 bool found = false;
68 for (; ucis_symbol_table[i].mnemonic; i++) {
69 if (match_mnemonic(ucis_symbol_table[i].mnemonic)) {
70 found = true;
71 break;
72 }
73 }
74
75 if (found) {
76 for (uint8_t j = 0; j <= count; j++) {
77 tap_code(KC_BACKSPACE);
78 }
79 register_ucis(i);
80 }
81
82 active = false;
83}
84
85void ucis_cancel(void) {
86 count = 0;
87 active = false;
88}
89
90void register_ucis(uint8_t index) {
91 const uint32_t *code_points = ucis_symbol_table[index].code_points;
92
93 for (int i = 0; i < UCIS_MAX_CODE_POINTS && code_points[i]; i++) {
94 register_unicode(code_points[i]);
95 }
96}