summaryrefslogtreecommitdiff
path: root/quantum/process_keycode
diff options
context:
space:
mode:
authorRyan <fauxpark@gmail.com>2023-08-27 13:30:19 +1000
committerGitHub <noreply@github.com>2023-08-27 13:30:19 +1000
commit70e34e491c297231a3f987fd69760d38e79dbfa4 (patch)
treea3fe26ea27c9d020142f4c248fa0bab5d32c1f9c /quantum/process_keycode
parent95681b8ff4a92aacd0249e124d34cf16e510175e (diff)
Unicode, Unicodemap and UCIS refactor (#21659)
Diffstat (limited to 'quantum/process_keycode')
-rw-r--r--quantum/process_keycode/process_ucis.c120
-rw-r--r--quantum/process_keycode/process_ucis.h42
-rw-r--r--quantum/process_keycode/process_unicode.c1
-rw-r--r--quantum/process_keycode/process_unicode.h1
-rw-r--r--quantum/process_keycode/process_unicode_common.c15
-rw-r--r--quantum/process_keycode/process_unicode_common.h1
-rw-r--r--quantum/process_keycode/process_unicodemap.c35
-rw-r--r--quantum/process_keycode/process_unicodemap.h7
8 files changed, 37 insertions, 185 deletions
diff --git a/quantum/process_keycode/process_ucis.c b/quantum/process_keycode/process_ucis.c
index 3aa09d5948..91a038bab1 100644
--- a/quantum/process_keycode/process_ucis.c
+++ b/quantum/process_keycode/process_ucis.c
@@ -15,110 +15,30 @@
15 */ 15 */
16 16
17#include "process_ucis.h" 17#include "process_ucis.h"
18#include "unicode.h" 18#include "ucis.h"
19#include "keycode.h" 19#include "keycodes.h"
20#include "wait.h"
21 20
22ucis_state_t ucis_state; 21bool process_ucis(uint16_t keycode, keyrecord_t *record) {
23 22 if (ucis_active() && record->event.pressed) {
24void ucis_start(void) { 23 bool special = keycode == KC_SPACE || keycode == KC_ENTER || keycode == KC_ESCAPE || keycode == KC_BACKSPACE;
25 ucis_state.count = 0; 24 if (ucis_count() >= UCIS_MAX_INPUT_LENGTH && !special) {
26 ucis_state.in_progress = true;
27
28 ucis_start_user();
29}
30
31__attribute__((weak)) void ucis_start_user(void) {
32 register_unicode(0x2328); // ⌨
33}
34
35__attribute__((weak)) void ucis_success(uint8_t symbol_index) {}
36
37static bool is_uni_seq(char *seq) {
38 uint8_t i;
39 for (i = 0; seq[i]; i++) {
40 uint16_t keycode;
41 if ('1' <= seq[i] && seq[i] <= '0') {
42 keycode = seq[i] - '1' + KC_1;
43 } else {
44 keycode = seq[i] - 'a' + KC_A;
45 }
46 if (i > ucis_state.count || ucis_state.codes[i] != keycode) {
47 return false; 25 return false;
48 } 26 }
49 }
50 return ucis_state.codes[i] == KC_ENTER || ucis_state.codes[i] == KC_SPACE;
51}
52
53__attribute__((weak)) void ucis_symbol_fallback(void) {
54 for (uint8_t i = 0; i < ucis_state.count - 1; i++) {
55 tap_code(ucis_state.codes[i]);
56 }
57}
58
59__attribute__((weak)) void ucis_cancel(void) {}
60
61void register_ucis(const uint32_t *code_points) {
62 for (int i = 0; i < UCIS_MAX_CODE_POINTS && code_points[i]; i++) {
63 register_unicode(code_points[i]);
64 }
65}
66 27
67bool process_ucis(uint16_t keycode, keyrecord_t *record) { 28 if (!ucis_add(keycode)) {
68 if (!ucis_state.in_progress || !record->event.pressed) { 29 switch (keycode) {
69 return true; 30 case KC_BACKSPACE:
70 } 31 return ucis_remove_last();
71 32 case KC_ESCAPE:
72 bool special = keycode == KC_SPACE || keycode == KC_ENTER || keycode == KC_ESCAPE || keycode == KC_BACKSPACE; 33 ucis_cancel();
73 if (ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH && !special) { 34 return false;
74 return false; 35 case KC_SPACE:
75 } 36 case KC_ENTER:
76 37 ucis_finish();
77 ucis_state.codes[ucis_state.count] = keycode; 38 return false;
78 ucis_state.count++;
79
80 switch (keycode) {
81 case KC_BACKSPACE:
82 if (ucis_state.count >= 2) {
83 ucis_state.count -= 2;
84 return true;
85 } else {
86 ucis_state.count--;
87 return false;
88 }
89
90 case KC_SPACE:
91 case KC_ENTER:
92 case KC_ESCAPE:
93 for (uint8_t i = 0; i < ucis_state.count; i++) {
94 tap_code(KC_BACKSPACE);
95 }
96
97 if (keycode == KC_ESCAPE) {
98 ucis_state.in_progress = false;
99 ucis_cancel();
100 return false;
101 }
102
103 uint8_t i;
104 bool symbol_found = false;
105 for (i = 0; ucis_symbol_table[i].symbol; i++) {
106 if (is_uni_seq(ucis_symbol_table[i].symbol)) {
107 symbol_found = true;
108 register_ucis(ucis_symbol_table[i].code_points);
109 break;
110 }
111 } 39 }
112 if (symbol_found) { 40 }
113 ucis_success(i);
114 } else {
115 ucis_symbol_fallback();
116 }
117
118 ucis_state.in_progress = false;
119 return false;
120
121 default:
122 return true;
123 } 41 }
42
43 return true;
124} 44}
diff --git a/quantum/process_keycode/process_ucis.h b/quantum/process_keycode/process_ucis.h
index 54eb9413d4..6282df7893 100644
--- a/quantum/process_keycode/process_ucis.h
+++ b/quantum/process_keycode/process_ucis.h
@@ -18,48 +18,6 @@
18 18
19#include <stdbool.h> 19#include <stdbool.h>
20#include <stdint.h> 20#include <stdint.h>
21
22#include "action.h" 21#include "action.h"
23 22
24#ifndef UCIS_MAX_SYMBOL_LENGTH
25# define UCIS_MAX_SYMBOL_LENGTH 32
26#endif
27#ifndef UCIS_MAX_CODE_POINTS
28# define UCIS_MAX_CODE_POINTS 3
29#endif
30
31typedef struct {
32 char * symbol;
33 uint32_t code_points[UCIS_MAX_CODE_POINTS];
34} ucis_symbol_t;
35
36typedef struct {
37 uint8_t count;
38 uint16_t codes[UCIS_MAX_SYMBOL_LENGTH];
39 bool in_progress : 1;
40} ucis_state_t;
41
42extern ucis_state_t ucis_state;
43
44// clang-format off
45
46#define UCIS_TABLE(...) \
47 { \
48 __VA_ARGS__, \
49 { NULL, {} } \
50 }
51#define UCIS_SYM(name, ...) \
52 { name, {__VA_ARGS__} }
53
54// clang-format on
55
56extern const ucis_symbol_t ucis_symbol_table[];
57
58void ucis_start(void);
59void ucis_start_user(void);
60void ucis_symbol_fallback(void);
61void ucis_success(uint8_t symbol_index);
62
63void register_ucis(const uint32_t *code_points);
64
65bool process_ucis(uint16_t keycode, keyrecord_t *record); 23bool process_ucis(uint16_t keycode, keyrecord_t *record);
diff --git a/quantum/process_keycode/process_unicode.c b/quantum/process_keycode/process_unicode.c
index 1ec76245a3..8ee6fcd7fc 100644
--- a/quantum/process_keycode/process_unicode.c
+++ b/quantum/process_keycode/process_unicode.c
@@ -16,6 +16,7 @@
16 16
17#include "process_unicode.h" 17#include "process_unicode.h"
18#include "unicode.h" 18#include "unicode.h"
19#include "keycodes.h"
19#include "quantum_keycodes.h" 20#include "quantum_keycodes.h"
20 21
21bool process_unicode(uint16_t keycode, keyrecord_t *record) { 22bool process_unicode(uint16_t keycode, keyrecord_t *record) {
diff --git a/quantum/process_keycode/process_unicode.h b/quantum/process_keycode/process_unicode.h
index 341bc8d861..9a51ffaf7c 100644
--- a/quantum/process_keycode/process_unicode.h
+++ b/quantum/process_keycode/process_unicode.h
@@ -18,7 +18,6 @@
18 18
19#include <stdbool.h> 19#include <stdbool.h>
20#include <stdint.h> 20#include <stdint.h>
21
22#include "action.h" 21#include "action.h"
23 22
24bool process_unicode(uint16_t keycode, keyrecord_t *record); 23bool process_unicode(uint16_t keycode, keyrecord_t *record);
diff --git a/quantum/process_keycode/process_unicode_common.c b/quantum/process_keycode/process_unicode_common.c
index a0b9010027..f43770977a 100644
--- a/quantum/process_keycode/process_unicode_common.c
+++ b/quantum/process_keycode/process_unicode_common.c
@@ -17,7 +17,8 @@
17#include "process_unicode_common.h" 17#include "process_unicode_common.h"
18#include "unicode.h" 18#include "unicode.h"
19#include "action_util.h" 19#include "action_util.h"
20#include "keycode.h" 20#include "keycodes.h"
21#include "modifiers.h"
21 22
22#if defined(UNICODE_ENABLE) 23#if defined(UNICODE_ENABLE)
23# include "process_unicode.h" 24# include "process_unicode.h"
@@ -32,10 +33,18 @@ bool process_unicode_common(uint16_t keycode, keyrecord_t *record) {
32 bool shifted = get_mods() & MOD_MASK_SHIFT; 33 bool shifted = get_mods() & MOD_MASK_SHIFT;
33 switch (keycode) { 34 switch (keycode) {
34 case QK_UNICODE_MODE_NEXT: 35 case QK_UNICODE_MODE_NEXT:
35 cycle_unicode_input_mode(shifted ? -1 : +1); 36 if (shifted) {
37 unicode_input_mode_step_reverse();
38 } else {
39 unicode_input_mode_step();
40 }
36 break; 41 break;
37 case QK_UNICODE_MODE_PREVIOUS: 42 case QK_UNICODE_MODE_PREVIOUS:
38 cycle_unicode_input_mode(shifted ? +1 : -1); 43 if (shifted) {
44 unicode_input_mode_step();
45 } else {
46 unicode_input_mode_step_reverse();
47 }
39 break; 48 break;
40 case QK_UNICODE_MODE_MACOS: 49 case QK_UNICODE_MODE_MACOS:
41 set_unicode_input_mode(UNICODE_MODE_MACOS); 50 set_unicode_input_mode(UNICODE_MODE_MACOS);
diff --git a/quantum/process_keycode/process_unicode_common.h b/quantum/process_keycode/process_unicode_common.h
index fd09a41818..0465830079 100644
--- a/quantum/process_keycode/process_unicode_common.h
+++ b/quantum/process_keycode/process_unicode_common.h
@@ -18,7 +18,6 @@
18 18
19#include <stdbool.h> 19#include <stdbool.h>
20#include <stdint.h> 20#include <stdint.h>
21
22#include "action.h" 21#include "action.h"
23 22
24bool process_unicode_common(uint16_t keycode, keyrecord_t *record); 23bool process_unicode_common(uint16_t keycode, keyrecord_t *record);
diff --git a/quantum/process_keycode/process_unicodemap.c b/quantum/process_keycode/process_unicodemap.c
index 195c093e6e..a85568521c 100644
--- a/quantum/process_keycode/process_unicodemap.c
+++ b/quantum/process_keycode/process_unicodemap.c
@@ -15,41 +15,12 @@
15 */ 15 */
16 16
17#include "process_unicodemap.h" 17#include "process_unicodemap.h"
18#include "unicode.h" 18#include "unicodemap.h"
19#include "quantum_keycodes.h" 19#include "keycodes.h"
20#include "keycode.h"
21#include "action_util.h"
22#include "host.h"
23
24__attribute__((weak)) uint16_t unicodemap_index(uint16_t keycode) {
25 if (keycode >= QK_UNICODEMAP_PAIR) {
26 // Keycode is a pair: extract index based on Shift / Caps Lock state
27 uint16_t index;
28
29 uint8_t mods = get_mods() | get_weak_mods();
30#ifndef NO_ACTION_ONESHOT
31 mods |= get_oneshot_mods();
32#endif
33
34 bool shift = mods & MOD_MASK_SHIFT;
35 bool caps = host_keyboard_led_state().caps_lock;
36 if (shift ^ caps) {
37 index = QK_UNICODEMAP_PAIR_GET_SHIFTED_INDEX(keycode);
38 } else {
39 index = QK_UNICODEMAP_PAIR_GET_UNSHIFTED_INDEX(keycode);
40 }
41
42 return index;
43 } else {
44 // Keycode is a regular index
45 return QK_UNICODEMAP_GET_INDEX(keycode);
46 }
47}
48 20
49bool process_unicodemap(uint16_t keycode, keyrecord_t *record) { 21bool process_unicodemap(uint16_t keycode, keyrecord_t *record) {
50 if (keycode >= QK_UNICODEMAP && keycode <= QK_UNICODEMAP_PAIR_MAX && record->event.pressed) { 22 if (keycode >= QK_UNICODEMAP && keycode <= QK_UNICODEMAP_PAIR_MAX && record->event.pressed) {
51 uint32_t code_point = pgm_read_dword(unicode_map + unicodemap_index(keycode)); 23 register_unicodemap(unicodemap_index(keycode));
52 register_unicode(code_point);
53 } 24 }
54 return true; 25 return true;
55} 26}
diff --git a/quantum/process_keycode/process_unicodemap.h b/quantum/process_keycode/process_unicodemap.h
index 5a3aeb0000..f07082e9ef 100644
--- a/quantum/process_keycode/process_unicodemap.h
+++ b/quantum/process_keycode/process_unicodemap.h
@@ -18,11 +18,6 @@
18 18
19#include <stdbool.h> 19#include <stdbool.h>
20#include <stdint.h> 20#include <stdint.h>
21
22#include "action.h" 21#include "action.h"
23#include "progmem.h"
24
25extern const uint32_t unicode_map[] PROGMEM;
26 22
27uint16_t unicodemap_index(uint16_t keycode); 23bool process_unicodemap(uint16_t keycode, keyrecord_t *record);
28bool process_unicodemap(uint16_t keycode, keyrecord_t *record);