Skip to content

Commit

Permalink
Unicode, Unicodemap and UCIS refactor (qmk#21659)
Browse files Browse the repository at this point in the history
  • Loading branch information
fauxpark authored Aug 27, 2023
1 parent 95681b8 commit 70e34e4
Show file tree
Hide file tree
Showing 34 changed files with 1,179 additions and 369 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ define PARSE_TEST
ifeq ($$(TEST_NAME),all)
MATCHED_TESTS := $$(TEST_LIST)
else
MATCHED_TESTS := $$(foreach TEST, $$(TEST_LIST),$$(if $$(findstring $$(TEST_NAME), $$(notdir $$(TEST))), $$(TEST),))
MATCHED_TESTS := $$(foreach TEST, $$(TEST_LIST),$$(if $$(findstring x$$(TEST_NAME)x, x$$(notdir $$(TEST))x), $$(TEST),))
endif
$$(foreach TEST,$$(MATCHED_TESTS),$$(eval $$(call BUILD_TEST,$$(TEST),$$(TEST_TARGET))))
endef
Expand Down
6 changes: 4 additions & 2 deletions builddefs/common_features.mk
Original file line number Diff line number Diff line change
Expand Up @@ -784,13 +784,15 @@ endif
ifeq ($(strip $(UCIS_ENABLE)), yes)
OPT_DEFS += -DUCIS_ENABLE
UNICODE_COMMON := yes
SRC += $(QUANTUM_DIR)/process_keycode/process_ucis.c
SRC += $(QUANTUM_DIR)/process_keycode/process_ucis.c \
$(QUANTUM_DIR)/unicode/ucis.c
endif

ifeq ($(strip $(UNICODEMAP_ENABLE)), yes)
OPT_DEFS += -DUNICODEMAP_ENABLE
UNICODE_COMMON := yes
SRC += $(QUANTUM_DIR)/process_keycode/process_unicodemap.c
SRC += $(QUANTUM_DIR)/process_keycode/process_unicodemap.c \
$(QUANTUM_DIR)/unicode/unicodemap.c
endif

ifeq ($(strip $(UNICODE_ENABLE)), yes)
Expand Down
464 changes: 312 additions & 152 deletions docs/feature_unicode.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion keyboards/spaceman/2_milk/keymaps/emoji/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void dance_key_one (tap_dance_state_t *state, void *user_data) {
tap_code(KC_ENTER);
reset_tap_dance (state);
} else if (state->count == 2) {
cycle_unicode_input_mode(+1);
unicode_input_mode_step();
reset_tap_dance (state);
}
}
Expand Down
120 changes: 20 additions & 100 deletions quantum/process_keycode/process_ucis.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,110 +15,30 @@
*/

#include "process_ucis.h"
#include "unicode.h"
#include "keycode.h"
#include "wait.h"
#include "ucis.h"
#include "keycodes.h"

ucis_state_t ucis_state;

void ucis_start(void) {
ucis_state.count = 0;
ucis_state.in_progress = true;

ucis_start_user();
}

__attribute__((weak)) void ucis_start_user(void) {
register_unicode(0x2328); // ⌨
}

__attribute__((weak)) void ucis_success(uint8_t symbol_index) {}

static bool is_uni_seq(char *seq) {
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;
}
if (i > ucis_state.count || ucis_state.codes[i] != keycode) {
bool process_ucis(uint16_t keycode, keyrecord_t *record) {
if (ucis_active() && record->event.pressed) {
bool special = keycode == KC_SPACE || keycode == KC_ENTER || keycode == KC_ESCAPE || keycode == KC_BACKSPACE;
if (ucis_count() >= UCIS_MAX_INPUT_LENGTH && !special) {
return false;
}
}
return ucis_state.codes[i] == KC_ENTER || ucis_state.codes[i] == KC_SPACE;
}

__attribute__((weak)) void ucis_symbol_fallback(void) {
for (uint8_t i = 0; i < ucis_state.count - 1; i++) {
tap_code(ucis_state.codes[i]);
}
}

__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]);
}
}

bool process_ucis(uint16_t keycode, keyrecord_t *record) {
if (!ucis_state.in_progress || !record->event.pressed) {
return true;
}

bool special = keycode == KC_SPACE || keycode == KC_ENTER || keycode == KC_ESCAPE || keycode == KC_BACKSPACE;
if (ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH && !special) {
return false;
}

ucis_state.codes[ucis_state.count] = keycode;
ucis_state.count++;

switch (keycode) {
case KC_BACKSPACE:
if (ucis_state.count >= 2) {
ucis_state.count -= 2;
return true;
} else {
ucis_state.count--;
return false;
}

case KC_SPACE:
case KC_ENTER:
case KC_ESCAPE:
for (uint8_t i = 0; i < ucis_state.count; i++) {
tap_code(KC_BACKSPACE);
}

if (keycode == KC_ESCAPE) {
ucis_state.in_progress = false;
ucis_cancel();
return false;
}

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 (!ucis_add(keycode)) {
switch (keycode) {
case KC_BACKSPACE:
return ucis_remove_last();
case KC_ESCAPE:
ucis_cancel();
return false;
case KC_SPACE:
case KC_ENTER:
ucis_finish();
return false;
}
if (symbol_found) {
ucis_success(i);
} else {
ucis_symbol_fallback();
}

ucis_state.in_progress = false;
return false;

default:
return true;
}
}

return true;
}
42 changes: 0 additions & 42 deletions quantum/process_keycode/process_ucis.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,6 @@

#include <stdbool.h>
#include <stdint.h>

#include "action.h"

#ifndef UCIS_MAX_SYMBOL_LENGTH
# define UCIS_MAX_SYMBOL_LENGTH 32
#endif
#ifndef UCIS_MAX_CODE_POINTS
# define UCIS_MAX_CODE_POINTS 3
#endif

typedef struct {
char * symbol;
uint32_t code_points[UCIS_MAX_CODE_POINTS];
} ucis_symbol_t;

typedef struct {
uint8_t count;
uint16_t codes[UCIS_MAX_SYMBOL_LENGTH];
bool in_progress : 1;
} ucis_state_t;

extern ucis_state_t ucis_state;

// clang-format off

#define UCIS_TABLE(...) \
{ \
__VA_ARGS__, \
{ NULL, {} } \
}
#define UCIS_SYM(name, ...) \
{ name, {__VA_ARGS__} }

// clang-format on

extern const ucis_symbol_t ucis_symbol_table[];

void ucis_start(void);
void ucis_start_user(void);
void ucis_symbol_fallback(void);
void ucis_success(uint8_t symbol_index);

void register_ucis(const uint32_t *code_points);

bool process_ucis(uint16_t keycode, keyrecord_t *record);
1 change: 1 addition & 0 deletions quantum/process_keycode/process_unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "process_unicode.h"
#include "unicode.h"
#include "keycodes.h"
#include "quantum_keycodes.h"

bool process_unicode(uint16_t keycode, keyrecord_t *record) {
Expand Down
1 change: 0 additions & 1 deletion quantum/process_keycode/process_unicode.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include <stdbool.h>
#include <stdint.h>

#include "action.h"

bool process_unicode(uint16_t keycode, keyrecord_t *record);
15 changes: 12 additions & 3 deletions quantum/process_keycode/process_unicode_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
#include "process_unicode_common.h"
#include "unicode.h"
#include "action_util.h"
#include "keycode.h"
#include "keycodes.h"
#include "modifiers.h"

#if defined(UNICODE_ENABLE)
# include "process_unicode.h"
Expand All @@ -32,10 +33,18 @@ bool process_unicode_common(uint16_t keycode, keyrecord_t *record) {
bool shifted = get_mods() & MOD_MASK_SHIFT;
switch (keycode) {
case QK_UNICODE_MODE_NEXT:
cycle_unicode_input_mode(shifted ? -1 : +1);
if (shifted) {
unicode_input_mode_step_reverse();
} else {
unicode_input_mode_step();
}
break;
case QK_UNICODE_MODE_PREVIOUS:
cycle_unicode_input_mode(shifted ? +1 : -1);
if (shifted) {
unicode_input_mode_step();
} else {
unicode_input_mode_step_reverse();
}
break;
case QK_UNICODE_MODE_MACOS:
set_unicode_input_mode(UNICODE_MODE_MACOS);
Expand Down
1 change: 0 additions & 1 deletion quantum/process_keycode/process_unicode_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include <stdbool.h>
#include <stdint.h>

#include "action.h"

bool process_unicode_common(uint16_t keycode, keyrecord_t *record);
35 changes: 3 additions & 32 deletions quantum/process_keycode/process_unicodemap.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,12 @@
*/

#include "process_unicodemap.h"
#include "unicode.h"
#include "quantum_keycodes.h"
#include "keycode.h"
#include "action_util.h"
#include "host.h"

__attribute__((weak)) uint16_t unicodemap_index(uint16_t keycode) {
if (keycode >= QK_UNICODEMAP_PAIR) {
// Keycode is a pair: extract index based on Shift / Caps Lock state
uint16_t index;

uint8_t mods = get_mods() | get_weak_mods();
#ifndef NO_ACTION_ONESHOT
mods |= get_oneshot_mods();
#endif

bool shift = mods & MOD_MASK_SHIFT;
bool caps = host_keyboard_led_state().caps_lock;
if (shift ^ caps) {
index = QK_UNICODEMAP_PAIR_GET_SHIFTED_INDEX(keycode);
} else {
index = QK_UNICODEMAP_PAIR_GET_UNSHIFTED_INDEX(keycode);
}

return index;
} else {
// Keycode is a regular index
return QK_UNICODEMAP_GET_INDEX(keycode);
}
}
#include "unicodemap.h"
#include "keycodes.h"

bool process_unicodemap(uint16_t keycode, keyrecord_t *record) {
if (keycode >= QK_UNICODEMAP && keycode <= QK_UNICODEMAP_PAIR_MAX && record->event.pressed) {
uint32_t code_point = pgm_read_dword(unicode_map + unicodemap_index(keycode));
register_unicode(code_point);
register_unicodemap(unicodemap_index(keycode));
}
return true;
}
7 changes: 1 addition & 6 deletions quantum/process_keycode/process_unicodemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@

#include <stdbool.h>
#include <stdint.h>

#include "action.h"
#include "progmem.h"

extern const uint32_t unicode_map[] PROGMEM;

uint16_t unicodemap_index(uint16_t keycode);
bool process_unicodemap(uint16_t keycode, keyrecord_t *record);
bool process_unicodemap(uint16_t keycode, keyrecord_t *record);
4 changes: 0 additions & 4 deletions quantum/quantum.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@
# include "process_unicode_common.h"
#endif

#ifdef UNICODE_ENABLE
# include "process_unicode.h"
#endif

#ifdef VELOCIKEY_ENABLE
# include "velocikey.h"
#endif
Expand Down
12 changes: 6 additions & 6 deletions quantum/quantum.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ extern layer_state_t layer_state;
# include "leader.h"
#endif

#ifdef UCIS_ENABLE
# include "process_ucis.h"
#ifdef UNICODE_COMMON_ENABLE
# include "unicode.h"
#endif

#ifdef UNICODEMAP_ENABLE
# include "process_unicodemap.h"
#ifdef UCIS_ENABLE
# include "ucis.h"
#endif

#ifdef UNICODE_COMMON_ENABLE
# include "unicode.h"
#ifdef UNICODEMAP_ENABLE
# include "unicodemap.h"
#endif

#ifdef KEY_OVERRIDE_ENABLE
Expand Down
2 changes: 1 addition & 1 deletion quantum/quantum_keycodes_legacy.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@
#define GUI_TOG QK_MAGIC_TOGGLE_GUI

#define X(i) UM(i)
#define XP(i, j) UM(i, j)
#define XP(i, j) UP(i, j)
Loading

0 comments on commit 70e34e4

Please sign in to comment.