Skip to content

Commit e4bf832

Browse files
authored
Add unicode mode change callbacks (#18235)
1 parent b9effc9 commit e4bf832

File tree

6 files changed

+63
-28
lines changed

6 files changed

+63
-28
lines changed

docs/feature_unicode.md

+11
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,17 @@ The functions for starting and finishing Unicode input on your platform can be o
206206
207207
You can find the default implementations of these functions in [`process_unicode_common.c`](https://github.com/qmk/qmk_firmware/blob/master/quantum/process_keycode/process_unicode_common.c).
208208
209+
### Input Mode Callbacks
210+
211+
There are callbacks functions available that are called whenever the unicode input mode changes. The new input mode is passed to the function.
212+
213+
|Callback |Description |
214+
|---------------------------------------------------|-----------------------------------------------------|
215+
| `unicode_input_mode_set_kb(uint8_t input_mode)` | Callback for unicode input mode set, for keyboard. |
216+
| `unicode_input_mode_set_user(uint8_t input_mode)` | Callback for unicode input mode set, for users. |
217+
218+
This feature can be used, for instance, to implement LED indicators for the current unicode input mode.
219+
209220
### Input Key Configuration
210221
211222
You can customize the keys used to trigger Unicode input for macOS, Linux and WinCompose by adding corresponding defines to your `config.h`. The default values match the platforms' default settings, so you shouldn't need to change this unless Unicode input isn't working, or you want to use a different key (e.g. in order to free up left or right Alt).

quantum/process_keycode/process_unicode_common.c

+17
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ static int8_t selected_count = ARRAY_SIZE(selected);
2929
static int8_t selected_index;
3030
#endif
3131

32+
/** \brief Uunicode input mode set at user level
33+
*
34+
* Run user code on unicode input mode change
35+
*/
36+
__attribute__((weak)) void unicode_input_mode_set_user(uint8_t input_mode) {}
37+
38+
/** \brief unicode input mode set at keyboard level
39+
*
40+
* Run keyboard code on unicode input mode change
41+
*/
42+
__attribute__((weak)) void unicode_input_mode_set_kb(uint8_t input_mode) {
43+
unicode_input_mode_set_user(input_mode);
44+
}
45+
3246
void unicode_input_mode_init(void) {
3347
unicode_config.raw = eeprom_read_byte(EECONFIG_UNICODEMODE);
3448
#if UNICODE_SELECTED_MODES != -1
@@ -50,6 +64,7 @@ void unicode_input_mode_init(void) {
5064
unicode_config.input_mode = selected[selected_index = 0];
5165
# endif
5266
#endif
67+
unicode_input_mode_set_kb(unicode_config.input_mode);
5368
dprintf("Unicode input mode init to: %u\n", unicode_config.input_mode);
5469
}
5570

@@ -60,6 +75,7 @@ uint8_t get_unicode_input_mode(void) {
6075
void set_unicode_input_mode(uint8_t mode) {
6176
unicode_config.input_mode = mode;
6277
persist_unicode_input_mode();
78+
unicode_input_mode_set_kb(mode);
6379
dprintf("Unicode input mode set to: %u\n", unicode_config.input_mode);
6480
}
6581

@@ -73,6 +89,7 @@ void cycle_unicode_input_mode(int8_t offset) {
7389
# if UNICODE_CYCLE_PERSIST
7490
persist_unicode_input_mode();
7591
# endif
92+
unicode_input_mode_set_kb(unicode_config.input_mode);
7693
dprintf("Unicode input mode cycle to: %u\n", unicode_config.input_mode);
7794
#endif
7895
}

quantum/process_keycode/process_unicode_common.h

+3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ void unicode_input_start(void);
8787
void unicode_input_finish(void);
8888
void unicode_input_cancel(void);
8989

90+
void unicode_input_mode_set_user(uint8_t input_mode);
91+
void unicode_input_mode_set_kb(uint8_t input_mode);
92+
9093
void register_hex(uint16_t hex);
9194
void register_hex32(uint32_t hex);
9295
void register_unicode(uint32_t code_point);

users/spidey3/layer_rgb.c

+11-20
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ void do_rgb_layers(layer_state_t state, uint8_t start, uint8_t end) {
112112
}
113113
}
114114

115-
void do_rgb_unicode(void) {
116-
uint8_t uc_mode = get_unicode_input_mode();
115+
void do_rgb_unicode(uint8_t uc_mode) {
117116
for (uint8_t i = 0; i < UC__COUNT; i++) {
118117
bool is_on = i == uc_mode;
119118
rgblight_set_layer_state(UNICODE_OFFSET + i, is_on);
@@ -123,7 +122,7 @@ void do_rgb_unicode(void) {
123122
void do_rgb_all(void) {
124123
do_rgb_layers(default_layer_state, LAYER_BASE_DEFAULT, LAYER_BASE_REGULAR);
125124
do_rgb_layers(layer_state, LAYER_BASE_REGULAR, LAYER_BASE_END);
126-
do_rgb_unicode();
125+
do_rgb_unicode(get_unicode_input_mode());
127126
rgblight_set_layer_state(MISC_OFFSET + 0, spi_gflock);
128127
rgblight_set_layer_state(MISC_OFFSET + 1, spi_replace_mode != SPI_NORMAL);
129128
}
@@ -148,7 +147,7 @@ extern rgblight_status_t rgblight_status;
148147
# define STARTUP_ANIMATION_CYCLE_STEP 2
149148
# define STARTUP_ANIMATION_RAMP_TO_STEPS 70
150149
# define STARTUP_ANIMATION_STEP_TIME 10
151-
# define STARTUP_ANIMATION_INITIAL_DELAY 0 // milliseconds, must be < 255 * STEP_TIME
150+
# define STARTUP_ANIMATION_INITIAL_DELAY 0 // milliseconds, must be < 255 * STEP_TIME
152151

153152
// clang-format off
154153
typedef enum {
@@ -382,6 +381,13 @@ bool led_update_user_rgb(led_t led_state) {
382381
return true;
383382
}
384383

384+
#if defined(UNICODE_COMMON_ENABLE)
385+
void unicode_input_mode_set_user_rgb(uint8_t input_mode) {
386+
rgb_layer_ack(ACK_MEH);
387+
do_rgb_unicode(input_mode);
388+
}
389+
#endif
390+
385391
void rgb_layer_ack_yn(bool yn) { rgb_layer_ack(yn ? ACK_YES : ACK_NO); }
386392

387393
void rgb_layer_ack(layer_ack_t n) {
@@ -458,7 +464,7 @@ void post_process_record_user_rgb(uint16_t keycode, keyrecord_t *record) {
458464
break;
459465

460466
case RGB_TOG:
461-
// Hack - we only get called on the press for RGB_TOG,
467+
// Hack - we only get called on the press for RGB_TOG,
462468
// but the flag is only flipped on the release...
463469
rgb_layer_ack_yn(!rgblight_config.enable);
464470
break;
@@ -476,20 +482,5 @@ void post_process_record_user_rgb(uint16_t keycode, keyrecord_t *record) {
476482
rgb_layer_ack_yn(keymap_config.nkro);
477483
break;
478484
#endif
479-
480-
#if defined(UNICODE_COMMON_ENABLE)
481-
case UC_M_MA:
482-
case UC_M_LN:
483-
case UC_M_WI:
484-
case UC_M_BS:
485-
case UC_M_WC:
486-
case UC_M_EM:
487-
488-
case UC_MOD:
489-
case UC_RMOD:
490-
rgb_layer_ack(ACK_MEH);
491-
do_rgb_unicode();
492-
break;
493-
#endif
494485
}
495486
}

users/spidey3/spidey3.c

+13-5
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,22 @@ bool process_record_glyph_replacement(uint16_t keycode, keyrecord_t *record, uin
9898
clear_oneshot_mods();
9999
#endif
100100

101-
bool caps = host_keyboard_led_state().caps_lock;
101+
bool caps = host_keyboard_led_state().caps_lock;
102102
uint32_t base = ((shifted == caps) ? baseAlphaLower : baseAlphaUpper);
103103
_register(base + (keycode - KC_A));
104104
set_mods(temp_mod);
105105
}
106106
return false;
107107
case KC_0:
108-
if (shifted) { // skip shifted numbers, so that we can still use symbols etc.
108+
if (shifted) { // skip shifted numbers, so that we can still use symbols etc.
109109
return true;
110110
}
111111
if (record->event.pressed) {
112112
_register(zeroGlyph);
113113
}
114114
return false;
115115
case KC_1 ... KC_9:
116-
if (shifted) { // skip shifted numbers, so that we can still use symbols etc.
116+
if (shifted) { // skip shifted numbers, so that we can still use symbols etc.
117117
return true;
118118
}
119119
if (record->event.pressed) {
@@ -122,7 +122,7 @@ bool process_record_glyph_replacement(uint16_t keycode, keyrecord_t *record, uin
122122
return false;
123123
case KC_SPACE:
124124
if (record->event.pressed) {
125-
_register(spaceGlyph); // em space
125+
_register(spaceGlyph); // em space
126126
}
127127
return false;
128128
}
@@ -338,7 +338,7 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
338338
set_mods(mods);
339339
return false;
340340
}
341-
} else { // on release of KC_BSPC
341+
} else { // on release of KC_BSPC
342342
// In case KC_DEL is still being sent even after the release of KC_BSPC
343343
if (delkey_registered) {
344344
unregister_code(KC_DEL);
@@ -387,3 +387,11 @@ bool led_update_user(led_t led_state) {
387387
return true;
388388
#endif
389389
}
390+
391+
#if defined(UNICODE_COMMON_ENABLE)
392+
void unicode_input_mode_set_user(uint8_t input_mode) {
393+
# ifdef RGBLIGHT_ENABLE
394+
unicode_input_mode_set_user_rgb(input_mode);
395+
# endif
396+
}
397+
#endif

users/spidey3/spidey3.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ enum userspace_layers {
1717
};
1818

1919
enum custom_keycodes {
20-
CH_CPNL = SAFE_RANGE, // AL Control Panel
21-
CH_ASST, // AL Context-aware Desktop Assistant
22-
CH_SUSP, // Suspend
20+
CH_CPNL = SAFE_RANGE, // AL Control Panel
21+
CH_ASST, // AL Context-aware Desktop Assistant
22+
CH_SUSP, // Suspend
2323

2424
SPI_NORMAL,
2525
SPI_WIDE,
@@ -65,6 +65,11 @@ void rgb_layer_ack(layer_ack_t n);
6565
void rgb_layer_ack_yn(bool yn);
6666
void clear_rgb_layers(void);
6767
void shutdown_user_rgb(void);
68+
69+
# if defined(UNICODE_COMMON_ENABLE)
70+
void unicode_input_mode_set_user_rgb(uint8_t input_mode);
71+
# endif
72+
6873
#endif
6974

7075
#ifdef UNICODEMAP_ENABLE

0 commit comments

Comments
 (0)