diff --git a/Makefile b/Makefile index c77eadf97d5..9f2e4636a4b 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/builddefs/common_features.mk b/builddefs/common_features.mk index 60fcdc6b93d..094eda6fef6 100644 --- a/builddefs/common_features.mk +++ b/builddefs/common_features.mk @@ -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) diff --git a/docs/feature_unicode.md b/docs/feature_unicode.md index 312a0332570..341084f9260 100644 --- a/docs/feature_unicode.md +++ b/docs/feature_unicode.md @@ -1,48 +1,77 @@ -# Unicode Support +# Unicode :id=unicode -Unicode characters can be input straight from your keyboard! There are some limitations, however. +With a little help from your OS, practically any Unicode character can be input using your keyboard. -In order to enable Unicode support on your keyboard, you will need to do the following: +## Caveats :id=caveats -1. Choose one of three supported Unicode implementations: [Basic Unicode](#basic-unicode), [Unicode Map](#unicode-map), [UCIS](#ucis). -2. Find which [input mode](#input-modes) is the best match for your operating system and setup. -3. [Set](#setting-the-input-mode) the appropriate input mode (or modes) in your configuration. -4. Add Unicode keycodes to your keymap. +There are some limitations to this feature. Because there is no "standard" method of Unicode input across all operating systems, each of them require their own setup process on both the host *and* in the firmware, which may involve installation of additional software. This also means Unicode input will not "just work" when the keyboard is plugged into another device. +## Usage :id=usage -## 1. Methods :id=methods +The core Unicode API can be used purely programmatically. However, there are also additional subsystems which build on top of it and come with keycodes to make things easier. See below for more details. -QMK supports three different methods for enabling Unicode input and adding Unicode characters to your keymap. Each has its pros and cons in terms of flexibility and ease of use. Choose the one that best fits your use case. +Add the following to your keymap's `rules.mk`: -The Basic method should be enough for most users. However, if you need a wider range of supported characters (including emoji, rare symbols etc.), you should use Unicode Map. +```make +UNICODE_COMMON = yes +``` -
+## Basic Configuration :id=basic-configuration -### 1.1. Basic Unicode :id=basic-unicode +Add the following to your `config.h`: -The easiest to use method, albeit somewhat limited. It stores Unicode characters as keycodes in the keymap itself, so it only supports code points up to `0x7FFF`. This covers characters for most modern languages (including East Asian), as well as symbols, but it doesn't cover emoji. +|Define |Default |Description | +|------------------------|------------------|--------------------------------------------------------------------------------| +|`UNICODE_KEY_MAC` |`KC_LEFT_ALT` |The key to hold when beginning a Unicode sequence with the macOS input mode | +|`UNICODE_KEY_LNX` |`LCTL(LSFT(KC_U))`|The key to tap when beginning a Unicode sequence with the Linux input mode | +|`UNICODE_KEY_WINC` |`KC_RIGHT_ALT` |The key to hold when beginning a Unicode sequence with the WinCompose input mode| +|`UNICODE_SELECTED_MODES`|`-1` |A comma separated list of input modes for cycling through | +|`UNICODE_CYCLE_PERSIST` |`true` |Whether to persist the current Unicode input mode to EEPROM | +|`UNICODE_TYPE_DELAY` |`10` |The amount of time to wait, in milliseconds, between Unicode sequence keystrokes| -Add the following to your `rules.mk`: +### Audio Feedback :id=audio-feedback + +If you have the [Audio](feature_audio.md) feature enabled on your board, you can configure it to play sounds when the input mode is changed. + +Add the following to your `config.h`: + +|Define |Default|Description | +|-------------------|-------|-----------------------------------------------------------| +|`UNICODE_SONG_MAC` |*n/a* |The song to play when the macOS input mode is selected | +|`UNICODE_SONG_LNX` |*n/a* |The song to play when the Linux input mode is selected | +|`UNICODE_SONG_BSD` |*n/a* |The song to play when the BSD input mode is selected | +|`UNICODE_SONG_WIN` |*n/a* |The song to play when the Windows input mode is selected | +|`UNICODE_SONG_WINC`|*n/a* |The song to play when the WinCompose input mode is selected| + +## Input Subsystems :id=input-subsystems + +Each of these subsystems have their own pros and cons in terms of flexibility and ease of use. Choose the one that best fits your needs. + + + +### ** Basic ** + +This is the easiest to use, albeit somewhat limited. It supports code points up to `U+7FFF`, which covers characters for most modern languages (including East Asian), as well as many symbols, but does not include emoji. + +To enable Basic Unicode, add the following to your `rules.mk`: ```make UNICODE_ENABLE = yes ``` -Then add `UC(c)` keycodes to your keymap, where _c_ is the code point of the desired character (preferably in hexadecimal, up to 4 digits long). For example, `UC(0x40B)` will output [Ћ](https://unicode-table.com/en/040B/), and `UC(0x30C4)` will output [ツ](https://unicode-table.com/en/30C4). +You can then add `UC(c)` keycodes to your keymap, where *c* is the code point of the desired character (in hexadecimal - the `U+` prefix will not work). For example, `UC(0x40B)` will output [Ћ](https://unicode-table.com/en/040B/), and `UC(0x30C4)` will output [ツ](https://unicode-table.com/en/30C4). -
+### ** Unicode Map ** -### 1.2. Unicode Map :id=unicode-map +Unicode Map supports all possible code points (up to `U+10FFFF`). Here, the code points are stored in a separate mapping table (which may contain at most 16,384 entries), instead of directly in the keymap. -In addition to standard character ranges, this method also covers emoji, ancient scripts, rare symbols etc. In fact, all possible code points (up to `0x10FFFF`) are supported. Here, Unicode characters are stored in a separate mapping table. You need to maintain a `unicode_map` array in your keymap file, which may contain at most 16384 entries. - -Add the following to your `rules.mk`: +To enable Unicode Map, add the following to your `rules.mk`: ```make UNICODEMAP_ENABLE = yes ``` -Then add `UM(i)` keycodes to your keymap, where _i_ is the desired character's index in the mapping table. This can be a numeric value, but it's recommended to keep the indices in an enum and access them by name. +Then, you will need to create a mapping table in your `keymap.c`, and (optionally) an enum for naming the array indices, like so: ```c enum unicode_names { @@ -51,242 +80,373 @@ enum unicode_names { SNEK }; -const uint32_t unicode_map[] PROGMEM = { +const uint32_t PROGMEM unicode_map[] = { [BANG] = 0x203D, // ‽ [IRONY] = 0x2E2E, // ⸮ [SNEK] = 0x1F40D, // 🐍 }; ``` -Then you can use `UM(BANG)`, `UM(SNEK)` etc. in your keymap. +Finally, add `UM(i)` keycodes to your keymap, where *i* is an index into the `unicode_map[]` array. If you defined the enum above, you can use those names instead, for example `UM(BANG)` or `UM(SNEK)`. -#### Lower and Upper Case +#### Lower and Upper Case Pairs :id=unicodemap-pairs -Characters often come in lower and upper case pairs, such as å and Å. To make inputting these characters easier, you can use `UP(i, j)` in your keymap, where _i_ and _j_ are the mapping table indices of the lower and upper case character, respectively. If you're holding down Shift or have Caps Lock turned on when you press the key, the second (upper case) character will be inserted; otherwise, the first (lower case) version will appear. +Some writing systems have lowercase and uppercase variants of each character, such as å and Å. To make inputting these characters easier, you can use the `UP(i, j)` keycode in your keymap, where *i* and *j* are the mapping table indices of the lowercase and uppercase characters, respectively. If you're holding down Shift or have Caps Lock turned on when you press the key, the uppercase character will be inserted; otherwise, the lowercase character will be inserted. -This is most useful when creating a keymap for an international layout with special characters. Instead of having to put the lower and upper case versions of a character on separate keys, you can have them both on the same key by using `UP()`. This helps blend Unicode keys in with regular alphas. +```c +const uint32_t PROGMEM unicode_map[] = { + [AE_LOWER] = 0x00E6, // æ + [AE_UPPER] = 0x00C6, // Æ +}; +``` -Due to keycode size constraints, _i_ and _j_ can each only refer to one of the first 128 characters in your `unicode_map`. In other words, 0 ≤ _i_ ≤ 127 and 0 ≤ _j_ ≤ 127. This is enough for most use cases, but if you'd like to customize the index calculation, you can override the [`unicodemap_index()`](https://github.com/qmk/qmk_firmware/blob/71f640d47ee12c862c798e1f56392853c7b1c1a8/quantum/process_keycode/process_unicodemap.c#L36) function. This also allows you to, say, check Ctrl instead of Shift/Caps. +This is most useful when creating a keymap for an international layout with special characters. Instead of having to put the lower and upper case versions of a character on separate keys, you can have them both on the same key. This helps blend Unicode keys in with regular keycodes. -
+Due to keycode size constraints, *i* and *j* can each only refer to one of the first 128 characters in your `unicode_map`. In other words, 0 ≤ *i* ≤ 127 and 0 ≤ *j* ≤ 127. -### 1.3. UCIS :id=ucis +### ** UCIS ** -This method also supports all possible code points. As with the Unicode Map method, you need to maintain a mapping table in your keymap file. However, there are no built-in keycodes for this feature — you have to create a custom keycode or function that invokes this functionality. +As with Unicode Map, the UCIS method also supports all possible code points, and requires the use of a mapping table. However, it works much differently - Unicode characters are input by replacing a typed mnemonic. -Add the following to your `rules.mk`: +To enable UCIS, add the following to your keymap's `rules.mk`: ```make UCIS_ENABLE = yes ``` -Then define a table like this in your keymap file: +Then, create a mapping table in your `keymap.c`: ```c const ucis_symbol_t ucis_symbol_table[] = UCIS_TABLE( UCIS_SYM("poop", 0x1F4A9), // 💩 UCIS_SYM("rofl", 0x1F923), // 🤣 - UCIS_SYM("cuba", 0x1F1E8, 0x1F1FA), // 🇨🇺 + UCIS_SYM("ukr", 0x1F1FA, 0x1F1E6), // 🇺🇦 UCIS_SYM("look", 0x0CA0, 0x005F, 0x0CA0) // ಠ_ಠ ); ``` -By default, each table entry may be up to 3 code points long. This number can be changed by adding `#define UCIS_MAX_CODE_POINTS n` to your `config.h` file. +By default, each table entry may be up to three code points long. This can be changed by adding `#define UCIS_MAX_CODE_POINTS n` to your keymap's `config.h`. -To use UCIS input, call `ucis_start()`. Then, type the mnemonic for the character (such as "rofl") and hit Space, Enter or Esc. QMK should erase the "rofl" text and insert the laughing emoji. +To invoke UCIS input, the `ucis_start()` function must first be called (for example, in a custom "Unicode" keycode). Then, type the mnemonic for the mapping table entry (such as "rofl"), and hit Space or Enter. The "rofl" text will be backspaced and the emoji inserted. -#### Customization + -There are several functions that you can define in your keymap to customize the functionality of this feature. +## Input Modes :id=input-modes -* `void ucis_start_user(void)` – This runs when you call the "start" function, and can be used to provide feedback. By default, it types out a keyboard emoji. -* `void ucis_success(uint8_t symbol_index)` – This runs when the input has matched something and has completed. By default, it doesn't do anything. -* `void ucis_symbol_fallback (void)` – This runs when the input doesn't match anything. By default, it falls back to trying that input as a Unicode code. +Unicode input works by typing a sequence of characters, similar to a macro. However, since this sequence depends on your OS, you will need to prepare both your host machine and QMK to recognise and send the correct Unicode input sequences respectively. -You can find the default implementations of these functions in [`process_ucis.c`](https://github.com/qmk/qmk_firmware/blob/master/quantum/process_keycode/process_ucis.c). - - -## 2. Input Modes :id=input-modes - -Unicode input in QMK works by inputting a sequence of characters to the OS, sort of like a macro. Unfortunately, the way this is done differs for each platform. Specifically, each platform requires a different combination of keys to trigger Unicode input. Therefore, a corresponding input mode has to be set in QMK. - -The following input modes are available: - -* **`UNICODE_MODE_MACOS`**: macOS built-in Unicode hex input. Supports code points up to `0x10FFFF` (all possible code points). - - To enable, go to _System Preferences > Keyboard > Input Sources_, add _Unicode Hex Input_ to the list (it's under _Other_), then activate it from the input dropdown in the Menu Bar. - By default, this mode uses the left Option key (`KC_LALT`) for Unicode input, but this can be changed by defining [`UNICODE_KEY_MAC`](#input-key-configuration) with a different keycode. - - !> Using the _Unicode Hex Input_ input source may disable some Option-based shortcuts, such as Option+Left and Option+Right. - -* **`UNICODE_MODE_LINUX`**: Linux built-in IBus Unicode input. Supports code points up to `0x10FFFF` (all possible code points). - - Enabled by default and works almost anywhere on IBus-enabled distros. Without IBus, this mode works under GTK apps, but rarely anywhere else. - By default, this mode uses Ctrl+Shift+U (`LCTL(LSFT(KC_U))`) to start Unicode input, but this can be changed by defining [`UNICODE_KEY_LNX`](#input-key-configuration) with a different keycode. This might be required for IBus versions ≥1.5.15, where Ctrl+Shift+U behavior is consolidated into Ctrl+Shift+E. - - Users who wish support in non-GTK apps without IBus may need to resort to a more indirect method, such as creating a custom keyboard layout ([more on this method](#custom-linux-layout)). - -* **`UNICODE_MODE_WINDOWS`**: _(not recommended)_ Windows built-in hex numpad Unicode input. Supports code points up to `0xFFFF`. - - To enable, create a registry key under `HKEY_CURRENT_USER\Control Panel\Input Method` of type `REG_SZ` called `EnableHexNumpad` and set its value to `1`. This can be done from the Command Prompt by running `reg add "HKCU\Control Panel\Input Method" -v EnableHexNumpad -t REG_SZ -d 1` with administrator privileges. Reboot afterwards. - This mode is not recommended because of reliability and compatibility issues; use the `UNICODE_MODE_WINCOMPOSE` mode instead. - -* **`UNICODE_MODE_BSD`**: _(non implemented)_ Unicode input under BSD. Not implemented at this time. If you're a BSD user and want to help add support for it, please [open an issue on GitHub](https://github.com/qmk/qmk_firmware/issues). - -* **`UNICODE_MODE_WINCOMPOSE`**: Windows Unicode input using [WinCompose](https://github.com/samhocevar/wincompose). As of v0.9.0, supports code points up to `0x10FFFF` (all possible code points). - - To enable, install the [latest release](https://github.com/samhocevar/wincompose/releases/latest). Once installed, WinCompose will automatically run on startup. This mode works reliably under all version of Windows supported by the app. - By default, this mode uses right Alt (`KC_RALT`) as the Compose key, but this can be changed in the WinCompose settings and by defining [`UNICODE_KEY_WINC`](#input-key-configuration) with a different keycode. - - -## 3. Setting the Input Mode :id=setting-the-input-mode - -To set your desired input mode, add the following define to your `config.h`: +To set the list of enabled input modes, add the `UNICODE_SELECTED_MODES` define to your keymap's `config.h`, for example: ```c #define UNICODE_SELECTED_MODES UNICODE_MODE_LINUX +// or +#define UNICODE_SELECTED_MODES UNICODE_MODE_MAC, UNICODE_MODE_WINCOMPOSE ``` -This example sets the board's default input mode to `UNICODE_MODE_LINUX`. You can replace this with `UNICODE_MODE_MACOS`, `UNICODE_MODE_WINCOMPOSE`, or any of the other modes listed [above](#input-modes). The board will automatically use the selected mode on startup, unless you manually switch to another mode (see [below](#keycodes)). +These modes can then be cycled through using the `UC_NEXT` and `UC_PREV` keycodes. You can also switch to any input mode, even if it is not specified in `UNICODE_SELECTED_MODES`, using their respective keycodes. -You can also select multiple input modes, which allows you to easily cycle through them using the `UC_NEXT`/`UC_PREV` keycodes. +If your keyboard has working EEPROM, it will remember the last used input mode and continue using it on the next power up. This can be disabled by defining `UNICODE_CYCLE_PERSIST` to `false`. -```c -#define UNICODE_SELECTED_MODES UNICODE_MODE_MACOS, UNICODE_MODE_LINUX, UNICODE_MODE_WINCOMPOSE + + +### ** macOS ** + +**Mode Name:** `UNICODE_MODE_MAC` + +macOS has built-in support for Unicode input as its own input source. It supports all possible code points by way of surrogate pairs for code points above `U+FFFF`. + +To enable, go to **System Preferences → Keyboard → Input Sources**, then add Unicode Hex Input to the list (under Other), and activate it from the input dropdown in the menu bar. Note that this may disable some Option-based shortcuts such as Option+Left and Option+Right. + +### ** Linux (IBus) ** + +**Mode Name:** `UNICODE_MODE_LINUX` + +For Linux distros with IBus, Unicode input is enabled by default, supports all possible code points, and works almost anywhere. Without IBus, it works under GTK apps, but rarely anywhere else. + +Users who would like support in non-GTK apps without IBus may need to resort to a more indirect method, such as creating a custom keyboard layout. + +### ** Windows (WinCompose) ** + +**Mode Name:** `UNICODE_MODE_WINCOMPOSE` + +This mode requires a third-party tool called [WinCompose](https://github.com/samhocevar/wincompose). It supports all possible code points, and is the recommended input mode for Windows. + +To enable, install the [latest release from GitHub](https://github.com/samhocevar/wincompose/releases/latest). Once installed, it will automatically run on startup. This works reliably under all versions of Windows supported by WinCompose. + +### ** Windows (HexNumpad) ** + +**Mode Name:** `UNICODE_MODE_WINDOWS` + +!> This input mode is *not* the "Alt code" system. Alt codes are not Unicode; they instead follow [the Windows-1252 character set](https://en.wikipedia.org/wiki/Alt_code). + +This is Windows' built-in hex numpad Unicode input mode. It only supports code points up to `U+FFFF`, and is not recommended due to reliability and compatibility issues. + +To enable, run the following as an administrator, then reboot: + +``` +reg add "HKCU\Control Panel\Input Method" -v EnableHexNumpad -t REG_SZ -d 1 ``` -Note that the values are separated by commas. The board will remember the last used input mode and will continue using it on next power-up. You can disable this and force it to always start with the first mode in the list by adding `#define UNICODE_CYCLE_PERSIST false` to your `config.h`. +### ** Emacs ** -#### Keycodes +**Mode Name:** `UNICODE_MODE_EMACS` -You can switch the input mode at any time by using the following keycodes. Adding these to your keymap allows you to quickly switch to a specific input mode, including modes not listed in `UNICODE_SELECTED_MODES`. +Emacs supports code point input with the `insert-char` command. -|Keycode |Alias |Input Mode |Description | -|----------------------------|---------|-------------------------|-----------------------------------------------------------------------------| -|`QK_UNICODE_MODE_NEXT` |`UC_NEXT`|Next in list |Cycle through selected modes, reverse direction when Shift is held | -|`QK_UNICODE_MODE_PREVIOUS` |`UC_PREV`|Prev in list |Cycle through selected modes in reverse, forward direction when Shift is held| -|`QK_UNICODE_MODE_MACOS` |`UC_MAC` |`UNICODE_MODE_MACOS` |Switch to macOS input | -|`QK_UNICODE_MODE_LINUX` |`UC_LINX`|`UNICODE_MODE_LINUX` |Switch to Linux input | -|`QK_UNICODE_MODE_WINDOWS` |`UC_WIN` |`UNICODE_MODE_WINDOWS` |Switch to Windows input | -|`QK_UNICODE_MODE_BSD` |`UC_BSD` |`UNICODE_MODE_BSD` |Switch to BSD input _(not implemented)_ | -|`QK_UNICODE_MODE_WINCOMPOSE`|`UC_WINC`|`UNICODE_MODE_WINCOMPOSE`|Switch to Windows input using WinCompose | -|`QK_UNICODE_MODE_EMACS` |`UC_EMAC`|`UNICODE_MODE_EMACS` |Switch to emacs (`C-x-8 RET`) | +### ** BSD ** -You can also switch the input mode by calling `set_unicode_input_mode(x)` in your code, where _x_ is one of the above input mode constants (e.g. `UNICODE_MODE_LINUX`). +**Mode Name:** `UNICODE_MODE_BSD` -?> Using `UNICODE_SELECTED_MODES` is preferable to calling `set_unicode_input_mode()` in `matrix_init_user()` or similar functions, since it's better integrated into the Unicode system and has the added benefit of avoiding unnecessary writes to EEPROM. +Not currently implemented. If you're a BSD user and want to contribute support for this input mode, please [feel free](contributing.md)! -#### Audio Feedback + -If you have the [Audio feature](feature_audio.md) enabled on the board, you can set melodies to be played when you press the above keys. That way you can have some audio feedback when switching input modes. +## Keycodes :id=keycodes -For instance, you can add these definitions to your `config.h` file: +|Key |Aliases |Description | +|----------------------------|---------|----------------------------------------------------------------| +|`UC(c)` | |Send Unicode code point `c`, up to `0x7FFF` | +|`UM(i)` | |Send Unicode code point at index `i` in `unicode_map` | +|`UP(i, j)` | |Send Unicode code point at index `i`, or `j` if Shift/Caps is on| +|`QK_UNICODE_MODE_NEXT` |`UC_NEXT`|Cycle through selected input modes | +|`QK_UNICODE_MODE_PREVIOUS` |`UC_PREV`|Cycle through selected input modes in reverse | +|`QK_UNICODE_MODE_MACOS` |`UC_MAC` |Switch to macOS input | +|`QK_UNICODE_MODE_LINUX` |`UC_LINX`|Switch to Linux input | +|`QK_UNICODE_MODE_WINDOWS` |`UC_WIN` |Switch to Windows input | +|`QK_UNICODE_MODE_BSD` |`UC_BSD` |Switch to BSD input (not implemented) | +|`QK_UNICODE_MODE_WINCOMPOSE`|`UC_WINC`|Switch to Windows input using WinCompose | +|`QK_UNICODE_MODE_EMACS` |`UC_EMAC`|Switch to emacs (`C-x-8 RET`) | -```c -#define UNICODE_SONG_MAC AUDIO_ON_SOUND -#define UNICODE_SONG_LNX UNICODE_LINUX -#define UNICODE_SONG_BSD TERMINAL_SOUND -#define UNICODE_SONG_WIN UNICODE_WINDOWS -#define UNICODE_SONG_WINC UNICODE_WINDOWS -``` +## API :id=api +### `uint8_t get_unicode_input_mode(void)` :id=api-get-unicode-input-mode -## Additional Customization +Get the current Unicode input mode. -Because Unicode is a large and versatile feature, there are a number of options you can customize to make it work better on your system. +#### Return Value :id=api-get-unicode-input-mode-return-value -### Start and Finish Input Functions +The currently active Unicode input mode. -The functions for starting and finishing Unicode input on your platform can be overridden locally. Possible uses include customizing input mode behavior if you don't use the default keys, or adding extra visual/audio feedback to Unicode input. +--- -* `void unicode_input_start(void)` – This sends the initial sequence that tells your platform to enter Unicode input mode. For example, it holds the left Alt key followed by Num+ on Windows, and presses the `UNICODE_KEY_LNX` combination (default: Ctrl+Shift+U) on Linux. -* `void unicode_input_finish(void)` – This is called to exit Unicode input mode, for example by pressing Space or releasing the Alt key. +### `void set_unicode_input_mode(uint8_t mode)` :id=api-set-unicode-input-mode -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). +Set the Unicode input mode. -### Input Mode Callbacks +#### Arguments :id=api-set-unicode-input-mode-arguments -There are callbacks functions available that are called whenever the unicode input mode changes. The new input mode is passed to the function. + - `uint8_t mode` + The input mode to set. -|Callback |Description | -|---------------------------------------------------|-----------------------------------------------------| -| `unicode_input_mode_set_kb(uint8_t input_mode)` | Callback for unicode input mode set, for keyboard. | -| `unicode_input_mode_set_user(uint8_t input_mode)` | Callback for unicode input mode set, for users. | +--- -This feature can be used, for instance, to implement LED indicators for the current unicode input mode. +### `void unicode_input_mode_step(void)` : id=api-unicode-input-mode-step -### Input Key Configuration +Change to the next Unicode input mode. -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). +--- -|Define |Type |Default |Example | -|------------------|----------|------------------|-------------------------------------------| -|`UNICODE_KEY_MAC` |`uint8_t` |`KC_LALT` |`#define UNICODE_KEY_MAC KC_RALT` | -|`UNICODE_KEY_LNX` |`uint16_t`|`LCTL(LSFT(KC_U))`|`#define UNICODE_KEY_LNX LCTL(LSFT(KC_E))`| -|`UNICODE_KEY_WINC`|`uint8_t` |`KC_RALT` |`#define UNICODE_KEY_WINC KC_RGUI` | +### `void unicode_input_mode_step_reverse(void)` : id=api-unicode-input-mode-step-reverse +Change to the previous Unicode input mode. -## Sending Unicode Strings +--- -QMK provides several functions that allow you to send Unicode input to the host programmatically: +### `void unicode_input_mode_set_user(uint8_t input_mode)` :id=api-unicode-input-mode-set-user -### `send_unicode_string()` +User-level callback, invoked when the input mode is changed. -This function is much like `send_string()`, but it allows you to input UTF-8 characters directly. It supports all code points, provided the selected input mode also supports it. Make sure your `keymap.c` file is formatted using UTF-8 encoding. +#### Arguments :id=api-unicode-input-mode-set-user-arguments -```c -send_unicode_string("(ノಠ痊ಠ)ノ彡┻━┻"); -``` + - `uint8_t input_mode` + The new input mode. -Example uses include sending Unicode strings when a key is pressed, as described in [Macros](feature_macros.md). +--- -## Additional Language Support +### `void unicode_input_mode_set_kb(uint8_t input_mode)` :id=api-unicode-input-mode-set-kb -In `quantum/keymap_extras`, you'll see various language files — these work the same way as the ones for alternative layouts such as Colemak or BÉPO. When you include one of these language headers, you gain access to keycodes specific to that language / national layout. Such keycodes are defined by a 2-letter country/language code, followed by an underscore and a 4-letter abbreviation of the character to which the key corresponds. For example, including `keymap_french.h` and using `FR_UGRV` in your keymap will output `ù` when typed on a system with a native French AZERTY layout. +Keyboard-level callback, invoked when the input mode is changed. -If the primary system layout you use on your machine is different from US ANSI, using these language-specific keycodes can help your QMK keymaps better match what will actually be output on the screen. However, keep in mind that these keycodes are just aliases for the corresponding default US keycodes under the hood, and that the HID protocol used by keyboards is itself inherently based on US ANSI. +#### Arguments :id=api-unicode-input-mode-set-kb-arguments + - `uint8_t input_mode` + The new input mode. -## International Characters on Windows +--- -### AutoHotkey +### `void unicode_input_start(void)` :id=api-unicode-input-start -The method does not require Unicode support in the keyboard itself but instead depends on [AutoHotkey](https://autohotkey.com) running in the background. +Begin the Unicode input sequence. The exact behavior depends on the currently selected input mode: -First you need to select a modifier combination that is not in use by any of your programs. -Ctrl+Alt+Win is not used very widely and should therefore be perfect for this. -There is a macro defined for a mod-tab combo `LCAG_T`. -Add this mod-tab combo to a key on your keyboard, e.g.: `LCAG_T(KC_TAB)`. -This makes the key behave like a tab key if pressed and released immediately but changes it to the modifier if used with another key. + - **macOS**: Hold `UNICODE_KEY_MAC` + - **Linux**: Tap `UNICODE_KEY_LNX` + - **WinCompose**: Tap `UNICODE_KEY_WINC`, then U + - **HexNumpad**: Hold Left Alt, then tap Numpad + + - **Emacs**: Tap Ctrl+X, then 8, then Enter -In the default script of AutoHotkey you can define custom hotkeys. +This function is weakly defined, and can be overridden in user code. - <^ { [ r, R, U1F923 ] }; -``` + - **macOS**: Release `UNICODE_KEY_MAC` + - **Linux**: Tap Escape + - **WinCompose**: Tap Escape + - **HexNumpad**: Release Left Alt + - **Emacs**: Tap Ctrl+G -Save the file and run the command `setxkbmap us` to reload the layout. +This function is weakly defined, and can be overridden in user code. -You can define one custom character for key defined in the layout, and another if you populate the fourth layer. Additional layers up to 8th are also possible. +--- -This method is specific to the computer on which you set the custom layout. The custom keys will be available only when Xorg is running. To avoid accidents, you should always reload the layout using `setxkbmap`, otherwise an invalid layout could prevent you from logging into your system, locking you out. +### `void register_unicode(uint32_t code_point)` :id=api-register-unicode + +Input a single Unicode character. A surrogate pair will be sent if required by the input mode. + +#### Arguments :id=api-register-unicode-arguments + + - `uint32_t code_point` + The code point of the character to send. + +--- + +### `void send_unicode_string(const char *str)` :id=api-send-unicode-string + +Send a string containing Unicode characters. + +#### Arguments :id=api-send-unicode-string-arguments + + - `const char *str` + The string to send. + +--- + +### `uint8_t unicodemap_index(uint16_t keycode)` :id=api-unicodemap-index + +Get the index into the `unicode_map` array for the given keycode, respecting shift state for pair keycodes. + +#### Arguments :id=api-unicodemap-index-arguments + + - `uint16_t keycode` + The Unicode Map keycode to get the index of. + +#### Return Value :id=api-unicodemap-index-return-value + +An index into the `unicode_map` array. + +--- + +### `uint32_t unicodemap_get_code_point(uint8_t index)` :id=api-unicodemap-get-code-point + +Get the code point for the given index in the `unicode_map` array. + +#### Arguments :id=unicodemap-get-code-point-arguments + + - `uint8_t index` + The index into the `unicode_map` array. + +#### Return Value :id=unicodemap-get-code-point-return-value + +A Unicode code point value. + +--- + +### `void register_unicodemap(uint8_t index)` :id=api-register-unicodemap + +Send the code point for the given index in the `unicode_map` array. + +#### Arguments :id=api-register-unicodemap-arguments + + - `uint8_t index` + The index into the `unicode_map` array. + +--- + +### `void ucis_start(void)` :id=api-ucis-start + +Begin the input sequence. + +--- + +### `bool ucis_active(void)` :id=api-ucis-active + +Whether UCIS is currently active. + +#### Return Value :id=api-ucis-active-return-value + +`true` if UCIS is active. + +--- + +### `uint8_t ucis_count(void)` :id=api-ucis-count + +Get the number of characters in the input sequence buffer. + +#### Return Value :id=api-ucis-count-return-value + +The current input sequence buffer length. + +--- + +### `bool ucis_add(uint16_t keycode)` :id=api-ucis-add + +Add the given keycode to the input sequence buffer. + +#### Arguments :id=api-ucis-add-arguments + + - `uint16_t keycode` + The keycode to add. Must be between `KC_A` and `KC_Z`, or `KC_1` and `KC_0`. + +#### Return Value :id=api-ucis-add-return-value + +`true` if the keycode was added. + +--- + +### `bool ucis_remove_last(void)` :id=api-ucis-remove-last + +Remove the last character from the input sequence buffer. + +#### Return Value :id=api-ucis-remove-last + +`true` if the sequence was not empty. + +--- + +### `void ucis_finish(void)` :id=api-ucis-finish + +Mark the input sequence as complete, and attempt to match. + +--- + +### `void ucis_cancel(void)` :id=api-ucis-cancel + +Cancel the input sequence. + +--- + +### `void register_ucis(void)` :id=api-register-ucis + +Send the code point(s) for the given UCIS index. + +#### Arguments :id=api-register-ucis-arguments + + - `uint8_t index` + The index into the UCIS symbol table. diff --git a/keyboards/spaceman/2_milk/keymaps/emoji/keymap.c b/keyboards/spaceman/2_milk/keymaps/emoji/keymap.c index 8859573ce5d..26ac20ad5ce 100644 --- a/keyboards/spaceman/2_milk/keymaps/emoji/keymap.c +++ b/keyboards/spaceman/2_milk/keymaps/emoji/keymap.c @@ -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); } } diff --git a/quantum/process_keycode/process_ucis.c b/quantum/process_keycode/process_ucis.c index 3aa09d5948f..91a038bab1c 100644 --- a/quantum/process_keycode/process_ucis.c +++ b/quantum/process_keycode/process_ucis.c @@ -15,110 +15,30 @@ */ #include "process_ucis.h" -#include "unicode.h" -#include "keycode.h" -#include "wait.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) { - 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]); - } -} +#include "ucis.h" +#include "keycodes.h" 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 (symbol_found) { - ucis_success(i); - } else { - ucis_symbol_fallback(); - } - - ucis_state.in_progress = false; + 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; + } - default: - return true; + 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; + } + } } + + return true; } diff --git a/quantum/process_keycode/process_ucis.h b/quantum/process_keycode/process_ucis.h index 54eb9413d41..6282df78932 100644 --- a/quantum/process_keycode/process_ucis.h +++ b/quantum/process_keycode/process_ucis.h @@ -18,48 +18,6 @@ #include #include - #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); diff --git a/quantum/process_keycode/process_unicode.c b/quantum/process_keycode/process_unicode.c index 1ec76245a34..8ee6fcd7fca 100644 --- a/quantum/process_keycode/process_unicode.c +++ b/quantum/process_keycode/process_unicode.c @@ -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) { diff --git a/quantum/process_keycode/process_unicode.h b/quantum/process_keycode/process_unicode.h index 341bc8d861d..9a51ffaf7c9 100644 --- a/quantum/process_keycode/process_unicode.h +++ b/quantum/process_keycode/process_unicode.h @@ -18,7 +18,6 @@ #include #include - #include "action.h" bool 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 a0b90100276..f43770977aa 100644 --- a/quantum/process_keycode/process_unicode_common.c +++ b/quantum/process_keycode/process_unicode_common.c @@ -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" @@ -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); diff --git a/quantum/process_keycode/process_unicode_common.h b/quantum/process_keycode/process_unicode_common.h index fd09a41818f..04658300793 100644 --- a/quantum/process_keycode/process_unicode_common.h +++ b/quantum/process_keycode/process_unicode_common.h @@ -18,7 +18,6 @@ #include #include - #include "action.h" bool 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 195c093e6e5..a85568521c5 100644 --- a/quantum/process_keycode/process_unicodemap.c +++ b/quantum/process_keycode/process_unicodemap.c @@ -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; } diff --git a/quantum/process_keycode/process_unicodemap.h b/quantum/process_keycode/process_unicodemap.h index 5a3aeb00006..f07082e9efa 100644 --- a/quantum/process_keycode/process_unicodemap.h +++ b/quantum/process_keycode/process_unicodemap.h @@ -18,11 +18,6 @@ #include #include - #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); diff --git a/quantum/quantum.c b/quantum/quantum.c index 3d440e0de89..3323a5adb62 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -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 diff --git a/quantum/quantum.h b/quantum/quantum.h index 017844c558d..4d183e755f2 100644 --- a/quantum/quantum.h +++ b/quantum/quantum.h @@ -97,16 +97,16 @@ extern layer_state_t layer_state; # include "leader.h" #endif +#ifdef UNICODE_COMMON_ENABLE +# include "unicode.h" +#endif + #ifdef UCIS_ENABLE -# include "process_ucis.h" +# include "ucis.h" #endif #ifdef UNICODEMAP_ENABLE -# include "process_unicodemap.h" -#endif - -#ifdef UNICODE_COMMON_ENABLE -# include "unicode.h" +# include "unicodemap.h" #endif #ifdef KEY_OVERRIDE_ENABLE diff --git a/quantum/quantum_keycodes_legacy.h b/quantum/quantum_keycodes_legacy.h index ad078cdad5c..260ac1c8a44 100644 --- a/quantum/quantum_keycodes_legacy.h +++ b/quantum/quantum_keycodes_legacy.h @@ -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) diff --git a/quantum/unicode/ucis.c b/quantum/unicode/ucis.c new file mode 100644 index 00000000000..32be7852065 --- /dev/null +++ b/quantum/unicode/ucis.c @@ -0,0 +1,96 @@ +// Copyright 2023 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "ucis.h" +#include "unicode.h" +#include "action.h" + +uint8_t count = 0; +bool active = false; +char input[UCIS_MAX_INPUT_LENGTH] = {0}; + +void ucis_start(void) { + count = 0; + active = true; + + register_unicode(0x2328); // ⌨ +} + +bool ucis_active(void) { + return active; +} + +uint8_t ucis_count(void) { + return count; +} + +static char keycode_to_char(uint16_t keycode) { + if (keycode >= KC_A && keycode <= KC_Z) { + return 'a' + (keycode - KC_A); + } else if (keycode >= KC_1 && keycode <= KC_9) { + return '1' + (keycode - KC_1); + } else if (keycode == KC_0) { + return '0'; + } + return 0; +} + +bool ucis_add(uint16_t keycode) { + char c = keycode_to_char(keycode); + if (c) { + input[count++] = c; + return true; + } + return false; +} + +bool ucis_remove_last(void) { + if (count) { + count--; + return true; + } + + return false; +} + +static bool match_mnemonic(char *mnemonic) { + for (uint8_t i = 0; input[i]; i++) { + if (i > count || input[i] != mnemonic[i]) { + return false; + } + } + return true; +} + +void ucis_finish(void) { + uint8_t i = 0; + bool found = false; + for (; ucis_symbol_table[i].mnemonic; i++) { + if (match_mnemonic(ucis_symbol_table[i].mnemonic)) { + found = true; + break; + } + } + + if (found) { + for (uint8_t j = 0; j <= count; j++) { + tap_code(KC_BACKSPACE); + } + register_ucis(i); + } + + active = false; +} + +void ucis_cancel(void) { + count = 0; + active = false; +} + +void register_ucis(uint8_t index) { + const uint32_t *code_points = ucis_symbol_table[index].code_points; + + for (int i = 0; i < UCIS_MAX_CODE_POINTS && code_points[i]; i++) { + register_unicode(code_points[i]); + } +} diff --git a/quantum/unicode/ucis.h b/quantum/unicode/ucis.h new file mode 100644 index 00000000000..5a4fa267841 --- /dev/null +++ b/quantum/unicode/ucis.h @@ -0,0 +1,97 @@ +// Copyright 2023 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include + +/** + * \file + * + * \defgroup ucis UCIS + * \{ + */ + +#ifndef UCIS_MAX_INPUT_LENGTH +# define UCIS_MAX_INPUT_LENGTH 32 +#endif + +#ifndef UCIS_MAX_CODE_POINTS +# define UCIS_MAX_CODE_POINTS 3 +#endif + +typedef struct { + char* mnemonic; + uint32_t code_points[UCIS_MAX_CODE_POINTS]; +} ucis_symbol_t; + +// clang-format off + +#define UCIS_TABLE(...) { \ + __VA_ARGS__, \ + { NULL, {} } \ +} + +#define UCIS_SYM(name, ...) { \ + .mnemonic = name, \ + .code_points = {__VA_ARGS__} \ +} + +// clang-format on + +extern const ucis_symbol_t ucis_symbol_table[]; + +/** + * \brief Begin the input sequence. + */ +void ucis_start(void); + +/** + * \brief Whether UCIS is currently active. + * + * \return `true` if UCIS is active. + */ +bool ucis_active(void); + +/** + * \brief Get the number of characters in the input sequence buffer. + * + * \return The current input sequence buffer length. + */ +uint8_t ucis_count(void); + +/** + * \brief Add the given keycode to the input sequence buffer. + * + * \param keycode The keycode to add. Must be between `KC_A` and `KC_Z`, or `KC_1` and `KC_0`. + * + * \return `true` if the keycode was added. + */ +bool ucis_add(uint16_t keycode); + +/** + * \brief Remove the last character from the input sequence. + * + * \return `true` if the sequence was not empty. + */ +bool ucis_remove_last(void); + +/** + * Mark the input sequence as complete, and attempt to match. + */ +void ucis_finish(void); + +/** + * \brief Cancel the input sequence. + */ +void ucis_cancel(void); + +/** + * Send the code point(s) for the given UCIS index. + * + * \param index The index into the UCIS symbol table. + */ +void register_ucis(uint8_t index); + +/** \} */ diff --git a/quantum/unicode/unicode.c b/quantum/unicode/unicode.c index f92ba51984d..78a4cad5859 100644 --- a/quantum/unicode/unicode.c +++ b/quantum/unicode/unicode.c @@ -73,16 +73,8 @@ static int8_t selected_count = ARRAY_SIZE(selected); static int8_t selected_index; #endif -/** \brief unicode input mode set at user level - * - * Run user code on unicode input mode change - */ __attribute__((weak)) void unicode_input_mode_set_user(uint8_t input_mode) {} -/** \brief unicode input mode set at keyboard level - * - * Run keyboard code on unicode input mode change - */ __attribute__((weak)) void unicode_input_mode_set_kb(uint8_t input_mode) { unicode_input_mode_set_user(input_mode); } @@ -172,6 +164,10 @@ uint8_t get_unicode_input_mode(void) { return unicode_config.input_mode; } +static void persist_unicode_input_mode(void) { + eeprom_update_byte(EECONFIG_UNICODEMODE, unicode_config.input_mode); +} + void set_unicode_input_mode(uint8_t mode) { unicode_config.input_mode = mode; persist_unicode_input_mode(); @@ -182,26 +178,34 @@ void set_unicode_input_mode(uint8_t mode) { dprintf("Unicode input mode set to: %u\n", unicode_config.input_mode); } -void cycle_unicode_input_mode(int8_t offset) { +static void cycle_unicode_input_mode(int8_t offset) { #if UNICODE_SELECTED_MODES != -1 selected_index = (selected_index + offset) % selected_count; if (selected_index < 0) { selected_index += selected_count; } + unicode_config.input_mode = selected[selected_index]; + # if UNICODE_CYCLE_PERSIST persist_unicode_input_mode(); # endif + # ifdef AUDIO_ENABLE unicode_play_song(unicode_config.input_mode); # endif + unicode_input_mode_set_kb(unicode_config.input_mode); dprintf("Unicode input mode cycle to: %u\n", unicode_config.input_mode); #endif } -void persist_unicode_input_mode(void) { - eeprom_update_byte(EECONFIG_UNICODEMODE, unicode_config.input_mode); +void unicode_input_mode_step(void) { + cycle_unicode_input_mode(1); +} + +void unicode_input_mode_step_reverse(void) { + cycle_unicode_input_mode(-1); } __attribute__((weak)) void unicode_input_start(void) { diff --git a/quantum/unicode/unicode.h b/quantum/unicode/unicode.h index fc0e9d3d2b7..90a54c8b18b 100644 --- a/quantum/unicode/unicode.h +++ b/quantum/unicode/unicode.h @@ -19,6 +19,13 @@ #include #include "unicode_keycodes.h" +/** + * \file + * + * \defgroup unicode Unicode + * \{ + */ + typedef union { uint8_t raw; struct { @@ -41,21 +48,87 @@ enum unicode_input_modes { UNICODE_MODE_COUNT // Number of available input modes (always leave at the end) }; -void unicode_input_mode_init(void); -uint8_t get_unicode_input_mode(void); -void set_unicode_input_mode(uint8_t mode); -void cycle_unicode_input_mode(int8_t offset); -void persist_unicode_input_mode(void); +void unicode_input_mode_init(void); +/** + * \brief Get the current Unicode input mode. + * + * \return The currently active Unicode input mode. + */ +uint8_t get_unicode_input_mode(void); + +/** + * \brief Set the Unicode input mode. + * + * \param mode The input mode to set. + */ +void set_unicode_input_mode(uint8_t mode); + +/** + * \brief Change to the next Unicode input mode. + */ +void unicode_input_mode_step(void); + +/** + * \brief Change to the previous Unicode input mode. + */ +void unicode_input_mode_step_reverse(void); + +/** + * \brief User-level callback, invoked when the input mode is changed. + * + * \param input_mode The new input mode. + */ void unicode_input_mode_set_user(uint8_t input_mode); + +/** + * \brief Keyboard-level callback, invoked when the input mode is changed. + * + * \param input_mode The new input mode. + */ void unicode_input_mode_set_kb(uint8_t input_mode); +/** + * \brief Begin the Unicode input sequence. The exact behavior depends on the currently selected input mode. + */ void unicode_input_start(void); + +/** + * \brief Complete the Unicode input sequence. The exact behavior depends on the currently selected input mode. + */ void unicode_input_finish(void); + +/** + * \brief Cancel the Unicode input sequence. The exact behavior depends on the currently selected input mode. + */ void unicode_input_cancel(void); +/** + * \brief Send a 16-bit hex number. + * + * \param hex The number to send. + */ void register_hex(uint16_t hex); + +/** + * \brief Send a 32-bit hex number. + * + * \param hex The number to send. + */ void register_hex32(uint32_t hex); + +/** + * \brief Input a single Unicode character. A surrogate pair will be sent if required by the input mode. + * + * \param code_point The code point of the character to send. + */ void register_unicode(uint32_t code_point); +/** + * \brief Send a string containing Unicode characters. + * + * \param str The string to send. + */ void send_unicode_string(const char *str); + +/** \} */ diff --git a/quantum/unicode/unicodemap.c b/quantum/unicode/unicodemap.c new file mode 100644 index 00000000000..2f618056a2e --- /dev/null +++ b/quantum/unicode/unicodemap.c @@ -0,0 +1,43 @@ +// Copyright 2023 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "unicodemap.h" +#include "unicode.h" +#include "keycodes.h" +#include "quantum_keycodes.h" +#include "modifiers.h" +#include "host.h" +#include "action_util.h" + +uint8_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); + } +} + +uint32_t unicodemap_get_code_point(uint8_t index) { + return pgm_read_dword(unicode_map + index); +} + +void register_unicodemap(uint8_t index) { + register_unicode(unicodemap_get_code_point(index)); +} diff --git a/quantum/unicode/unicodemap.h b/quantum/unicode/unicodemap.h new file mode 100644 index 00000000000..00d2aec8fad --- /dev/null +++ b/quantum/unicode/unicodemap.h @@ -0,0 +1,43 @@ +// Copyright 2023 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include "progmem.h" + +/** + * \file + * + * \defgroup unicodemap Unicode Map + * \{ + */ + +extern const uint32_t unicode_map[] PROGMEM; + +/** + * \brief Get the index into the `unicode_map` array for the given keycode, respecting shift state for pair keycodes. + * + * \param keycode The Unicode Map keycode to get the index of. + * + * \return An index into the `unicode_map` array. + */ +uint8_t unicodemap_index(uint16_t keycode); + +/** + * \brief Get the code point for the given index in the `unicode_map` array. + * + * \param index The index into the `unicode_map` array. + * + * \return A Unicode code point value. + */ +uint32_t unicodemap_get_code_point(uint8_t index); + +/** + * \brief Send the code point for the given index in the `unicode_map` array. + * + * \param index The index into the `unicode_map` array. + */ +void register_unicodemap(uint8_t index); + +/** \} */ diff --git a/tests/test_common/test_driver.cpp b/tests/test_common/test_driver.cpp index f1c52cb7b62..0495da8205a 100644 --- a/tests/test_common/test_driver.cpp +++ b/tests/test_common/test_driver.cpp @@ -60,7 +60,10 @@ void TestDriver::send_extra(report_extra_t* report) { namespace internal { void expect_unicode_code_point(TestDriver& driver, uint32_t code_point) { testing::InSequence seq; - EXPECT_REPORT(driver, (KC_LCTL, KC_LSFT, KC_U)); + EXPECT_REPORT(driver, (KC_LEFT_CTRL, KC_LEFT_SHIFT)); + EXPECT_REPORT(driver, (KC_LEFT_CTRL, KC_LEFT_SHIFT, KC_U)); + EXPECT_REPORT(driver, (KC_LEFT_CTRL, KC_LEFT_SHIFT)); + EXPECT_EMPTY_REPORT(driver); bool print_zero = false; for (int i = 7; i >= 0; --i) { @@ -71,10 +74,12 @@ void expect_unicode_code_point(TestDriver& driver, uint32_t code_point) { const uint8_t digit = (code_point >> (i * 4)) & 0xf; if (digit || print_zero) { EXPECT_REPORT(driver, (hex_digit_to_keycode(digit))); + EXPECT_EMPTY_REPORT(driver); print_zero = true; } } - EXPECT_REPORT(driver, (KC_SPC)); + EXPECT_REPORT(driver, (KC_SPACE)); + EXPECT_EMPTY_REPORT(driver); } } // namespace internal diff --git a/tests/unicode/config.h b/tests/unicode/config.h new file mode 100644 index 00000000000..16f95f7f8db --- /dev/null +++ b/tests/unicode/config.h @@ -0,0 +1,8 @@ +// Copyright 2023 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "test_common.h" + +#define UNICODE_SELECTED_MODES UNICODE_MODE_LINUX, UNICODE_MODE_MACOS diff --git a/tests/unicode/test.mk b/tests/unicode/test.mk new file mode 100644 index 00000000000..1afc79be559 --- /dev/null +++ b/tests/unicode/test.mk @@ -0,0 +1,5 @@ +# -------------------------------------------------------------------------------- +# Keep this file, even if it is empty, as a marker that this folder contains tests +# -------------------------------------------------------------------------------- + +UNICODE_COMMON = yes diff --git a/tests/unicode/test_unicode.cpp b/tests/unicode/test_unicode.cpp new file mode 100644 index 00000000000..a8500e3ba1d --- /dev/null +++ b/tests/unicode/test_unicode.cpp @@ -0,0 +1,86 @@ +// Copyright 2023 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "keyboard_report_util.hpp" +#include "keycode.h" +#include "test_common.hpp" +#include "test_keymap_key.hpp" + +using testing::_; + +class Unicode : public TestFixture {}; + +TEST_F(Unicode, sends_bmp_unicode_sequence) { + TestDriver driver; + + set_unicode_input_mode(UNICODE_MODE_LINUX); + + EXPECT_UNICODE(driver, 0x03A8); // Ψ + register_unicode(0x03A8); + + VERIFY_AND_CLEAR(driver); +} + +TEST_F(Unicode, sends_smp_unicode_sequence) { + TestDriver driver; + + set_unicode_input_mode(UNICODE_MODE_LINUX); + + EXPECT_UNICODE(driver, 0x1F9D9); // 🧙 + register_unicode(0x1F9D9); + + VERIFY_AND_CLEAR(driver); +} + +TEST_F(Unicode, sends_surrogate_pair_for_macos) { + TestDriver driver; + + set_unicode_input_mode(UNICODE_MODE_MACOS); + + // EXPECT_UNICODE() assumes Linux input mode + { + testing::InSequence s; + + // Alt+D83EDDD9 🧙 + EXPECT_REPORT(driver, (KC_LEFT_ALT)); + EXPECT_REPORT(driver, (KC_D, KC_LEFT_ALT)); + EXPECT_REPORT(driver, (KC_LEFT_ALT)); + EXPECT_REPORT(driver, (KC_8, KC_LEFT_ALT)); + EXPECT_REPORT(driver, (KC_LEFT_ALT)); + EXPECT_REPORT(driver, (KC_3, KC_LEFT_ALT)); + EXPECT_REPORT(driver, (KC_LEFT_ALT)); + EXPECT_REPORT(driver, (KC_E, KC_LEFT_ALT)); + EXPECT_REPORT(driver, (KC_LEFT_ALT)); + EXPECT_REPORT(driver, (KC_D, KC_LEFT_ALT)); + EXPECT_REPORT(driver, (KC_LEFT_ALT)); + EXPECT_REPORT(driver, (KC_D, KC_LEFT_ALT)); + EXPECT_REPORT(driver, (KC_LEFT_ALT)); + EXPECT_REPORT(driver, (KC_D, KC_LEFT_ALT)); + EXPECT_REPORT(driver, (KC_LEFT_ALT)); + EXPECT_REPORT(driver, (KC_9, KC_LEFT_ALT)); + EXPECT_REPORT(driver, (KC_LEFT_ALT)); + EXPECT_EMPTY_REPORT(driver); + } + + register_unicode(0x1F9D9); + + VERIFY_AND_CLEAR(driver); +} + +TEST_F(Unicode, sends_unicode_string) { + TestDriver driver; + + set_unicode_input_mode(UNICODE_MODE_LINUX); + + { + testing::InSequence s; + + EXPECT_UNICODE(driver, 0xFF31); + EXPECT_UNICODE(driver, 0xFF2D); + EXPECT_UNICODE(driver, 0xFF2B); + EXPECT_UNICODE(driver, 0xFF01); + } + send_unicode_string("QMK!"); + + VERIFY_AND_CLEAR(driver); +} diff --git a/tests/unicode/unicode_basic/config.h b/tests/unicode/unicode_basic/config.h new file mode 100644 index 00000000000..16f95f7f8db --- /dev/null +++ b/tests/unicode/unicode_basic/config.h @@ -0,0 +1,8 @@ +// Copyright 2023 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "test_common.h" + +#define UNICODE_SELECTED_MODES UNICODE_MODE_LINUX, UNICODE_MODE_MACOS diff --git a/tests/unicode/unicode_basic/test.mk b/tests/unicode/unicode_basic/test.mk new file mode 100644 index 00000000000..f53051dce67 --- /dev/null +++ b/tests/unicode/unicode_basic/test.mk @@ -0,0 +1,5 @@ +# -------------------------------------------------------------------------------- +# Keep this file, even if it is empty, as a marker that this folder contains tests +# -------------------------------------------------------------------------------- + +UNICODE_ENABLE = yes diff --git a/tests/unicode/unicode_basic/test_unicode_basic.cpp b/tests/unicode/unicode_basic/test_unicode_basic.cpp new file mode 100644 index 00000000000..598b57277f3 --- /dev/null +++ b/tests/unicode/unicode_basic/test_unicode_basic.cpp @@ -0,0 +1,26 @@ +// Copyright 2023 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "keyboard_report_util.hpp" +#include "keycode.h" +#include "test_common.hpp" +#include "test_keymap_key.hpp" + +using testing::_; + +class UnicodeBasic : public TestFixture {}; + +TEST_F(UnicodeBasic, sends_unicode_sequence) { + TestDriver driver; + + set_unicode_input_mode(UNICODE_MODE_LINUX); + + auto key_uc = KeymapKey(0, 0, 0, UC(0x03A8)); // Ψ + + set_keymap({key_uc}); + + EXPECT_UNICODE(driver, 0x03A8); + tap_key(key_uc); + + VERIFY_AND_CLEAR(driver); +} diff --git a/tests/unicode/unicode_map/config.h b/tests/unicode/unicode_map/config.h new file mode 100644 index 00000000000..0d86922f0da --- /dev/null +++ b/tests/unicode/unicode_map/config.h @@ -0,0 +1,8 @@ +// Copyright 2023 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "test_common.h" + +#define UNICODE_SELECTED_MODES UNICODE_MODE_LINUX diff --git a/tests/unicode/unicode_map/test.mk b/tests/unicode/unicode_map/test.mk new file mode 100644 index 00000000000..e6d00347998 --- /dev/null +++ b/tests/unicode/unicode_map/test.mk @@ -0,0 +1,5 @@ +# -------------------------------------------------------------------------------- +# Keep this file, even if it is empty, as a marker that this folder contains tests +# -------------------------------------------------------------------------------- + +UNICODEMAP_ENABLE = yes diff --git a/tests/unicode/unicode_map/test_unicode_map.cpp b/tests/unicode/unicode_map/test_unicode_map.cpp new file mode 100644 index 00000000000..cacd8f3c757 --- /dev/null +++ b/tests/unicode/unicode_map/test_unicode_map.cpp @@ -0,0 +1,54 @@ +// Copyright 2023 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "keyboard_report_util.hpp" +#include "keycode.h" +#include "test_common.hpp" +#include "test_keymap_key.hpp" + +using testing::_; + +const uint32_t PROGMEM unicode_map[] = { + 0x03A8, // Ψ + 0x2318 // ⌘ +}; + +class UnicodeMap : public TestFixture {}; + +TEST_F(UnicodeMap, sends_unicodemap_code_point_from_keycode) { + TestDriver driver; + + auto key_um = KeymapKey(0, 0, 0, UM(0)); + + set_keymap({key_um}); + + EXPECT_UNICODE(driver, 0x03A8); + tap_key(key_um); + + VERIFY_AND_CLEAR(driver); +} + +TEST_F(UnicodeMap, sends_unicodemap_pair_from_keycode) { + TestDriver driver; + + auto key_shift = KeymapKey(0, 0, 0, KC_LEFT_SHIFT); + auto key_up = KeymapKey(0, 1, 0, UP(0, 1)); + + set_keymap({key_shift, key_up}); + + EXPECT_UNICODE(driver, 0x03A8); + tap_key(key_up); + + EXPECT_REPORT(driver, (KC_LEFT_SHIFT)); + key_shift.press(); + run_one_scan_loop(); + + EXPECT_UNICODE(driver, 0x2318); + tap_key(key_up); + + EXPECT_NO_REPORT(driver); + key_shift.release(); + run_one_scan_loop(); + + VERIFY_AND_CLEAR(driver); +} diff --git a/tests/unicode/unicode_ucis/config.h b/tests/unicode/unicode_ucis/config.h new file mode 100644 index 00000000000..0d86922f0da --- /dev/null +++ b/tests/unicode/unicode_ucis/config.h @@ -0,0 +1,8 @@ +// Copyright 2023 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "test_common.h" + +#define UNICODE_SELECTED_MODES UNICODE_MODE_LINUX diff --git a/tests/unicode/unicode_ucis/test.mk b/tests/unicode/unicode_ucis/test.mk new file mode 100644 index 00000000000..d7ac6517583 --- /dev/null +++ b/tests/unicode/unicode_ucis/test.mk @@ -0,0 +1,5 @@ +# -------------------------------------------------------------------------------- +# Keep this file, even if it is empty, as a marker that this folder contains tests +# -------------------------------------------------------------------------------- + +UCIS_ENABLE = yes diff --git a/tests/unicode/unicode_ucis/test_unicode_ucis.cpp b/tests/unicode/unicode_ucis/test_unicode_ucis.cpp new file mode 100644 index 00000000000..acc6329f857 --- /dev/null +++ b/tests/unicode/unicode_ucis/test_unicode_ucis.cpp @@ -0,0 +1,221 @@ +// Copyright 2023 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "keyboard_report_util.hpp" +#include "keycode.h" +#include "test_common.hpp" +#include "test_keymap_key.hpp" + +using testing::_; + +// clang-format off +const ucis_symbol_t ucis_symbol_table[] = UCIS_TABLE( + UCIS_SYM("qmk", 0x03A8) // Ψ +); +// clang-format on + +class UnicodeUCIS : public TestFixture {}; + +TEST_F(UnicodeUCIS, matches_sequence) { + TestDriver driver; + + auto key_q = KeymapKey(0, 0, 0, KC_Q); + auto key_m = KeymapKey(0, 1, 0, KC_M); + auto key_k = KeymapKey(0, 2, 0, KC_K); + auto key_enter = KeymapKey(0, 3, 0, KC_ENTER); + + set_keymap({key_q, key_m, key_k, key_enter}); + + EXPECT_UNICODE(driver, 0x2328); // ⌨ + ucis_start(); + + EXPECT_EQ(ucis_active(), true); + EXPECT_EQ(ucis_count(), 0); + + EXPECT_REPORT(driver, (KC_Q)); + EXPECT_EMPTY_REPORT(driver); + tap_key(key_q); + EXPECT_EQ(ucis_count(), 1); + + EXPECT_REPORT(driver, (KC_M)); + EXPECT_EMPTY_REPORT(driver); + tap_key(key_m); + EXPECT_EQ(ucis_count(), 2); + + EXPECT_REPORT(driver, (KC_K)); + EXPECT_EMPTY_REPORT(driver); + tap_key(key_k); + EXPECT_EQ(ucis_count(), 3); + + EXPECT_REPORT(driver, (KC_BACKSPACE)).Times(4); + EXPECT_EMPTY_REPORT(driver).Times(4); + EXPECT_UNICODE(driver, 0x03A8); + tap_key(key_enter); + + EXPECT_EQ(ucis_active(), false); + + VERIFY_AND_CLEAR(driver); +} + +TEST_F(UnicodeUCIS, cancels_sequence) { + TestDriver driver; + + auto key_q = KeymapKey(0, 0, 0, KC_Q); + auto key_m = KeymapKey(0, 1, 0, KC_M); + auto key_k = KeymapKey(0, 2, 0, KC_K); + auto key_escape = KeymapKey(0, 3, 0, KC_ESCAPE); + + set_keymap({key_q, key_m, key_k, key_escape}); + + EXPECT_UNICODE(driver, 0x2328); // ⌨ + ucis_start(); + + EXPECT_EQ(ucis_active(), true); + EXPECT_EQ(ucis_count(), 0); + + EXPECT_REPORT(driver, (KC_Q)); + EXPECT_EMPTY_REPORT(driver); + tap_key(key_q); + EXPECT_EQ(ucis_count(), 1); + + EXPECT_REPORT(driver, (KC_M)); + EXPECT_EMPTY_REPORT(driver); + tap_key(key_m); + EXPECT_EQ(ucis_count(), 2); + + EXPECT_REPORT(driver, (KC_K)); + EXPECT_EMPTY_REPORT(driver); + tap_key(key_k); + EXPECT_EQ(ucis_count(), 3); + + EXPECT_NO_REPORT(driver); + tap_key(key_escape); + + EXPECT_EQ(ucis_active(), false); + + VERIFY_AND_CLEAR(driver); +} + +TEST_F(UnicodeUCIS, matches_sequence_with_corrected_typo) { + TestDriver driver; + + auto key_q = KeymapKey(0, 0, 0, KC_Q); + auto key_m = KeymapKey(0, 1, 0, KC_M); + auto key_j = KeymapKey(0, 2, 0, KC_J); + auto key_k = KeymapKey(0, 3, 0, KC_K); + auto key_backspace = KeymapKey(0, 4, 0, KC_BACKSPACE); + auto key_enter = KeymapKey(0, 5, 0, KC_ENTER); + + set_keymap({key_q, key_m, key_j, key_k, key_backspace, key_enter}); + + EXPECT_UNICODE(driver, 0x2328); // ⌨ + ucis_start(); + + EXPECT_EQ(ucis_active(), true); + EXPECT_EQ(ucis_count(), 0); + + EXPECT_REPORT(driver, (KC_Q)); + EXPECT_EMPTY_REPORT(driver); + tap_key(key_q); + EXPECT_EQ(ucis_count(), 1); + + EXPECT_REPORT(driver, (KC_M)); + EXPECT_EMPTY_REPORT(driver); + tap_key(key_m); + EXPECT_EQ(ucis_count(), 2); + + EXPECT_REPORT(driver, (KC_J)); + EXPECT_EMPTY_REPORT(driver); + tap_key(key_j); + EXPECT_EQ(ucis_count(), 3); + + EXPECT_REPORT(driver, (KC_BACKSPACE)); + EXPECT_EMPTY_REPORT(driver); + tap_key(key_backspace); + EXPECT_EQ(ucis_count(), 2); + + EXPECT_REPORT(driver, (KC_K)); + EXPECT_EMPTY_REPORT(driver); + tap_key(key_k); + EXPECT_EQ(ucis_count(), 3); + + EXPECT_REPORT(driver, (KC_BACKSPACE)).Times(4); + EXPECT_EMPTY_REPORT(driver).Times(4); + EXPECT_UNICODE(driver, 0x03A8); + tap_key(key_enter); + + EXPECT_EQ(ucis_active(), false); + + VERIFY_AND_CLEAR(driver); +} + +TEST_F(UnicodeUCIS, does_not_match_longer_sequence) { + TestDriver driver; + + auto key_q = KeymapKey(0, 0, 0, KC_Q); + auto key_m = KeymapKey(0, 1, 0, KC_M); + auto key_k = KeymapKey(0, 2, 0, KC_K); + auto key_enter = KeymapKey(0, 3, 0, KC_ENTER); + + set_keymap({key_q, key_m, key_k, key_enter}); + + EXPECT_UNICODE(driver, 0x2328); // ⌨ + ucis_start(); + + EXPECT_EQ(ucis_active(), true); + EXPECT_EQ(ucis_count(), 0); + + EXPECT_REPORT(driver, (KC_Q)); + EXPECT_EMPTY_REPORT(driver); + tap_key(key_q); + EXPECT_EQ(ucis_count(), 1); + + EXPECT_REPORT(driver, (KC_M)); + EXPECT_EMPTY_REPORT(driver); + tap_key(key_m); + EXPECT_EQ(ucis_count(), 2); + + EXPECT_REPORT(driver, (KC_K)); + EXPECT_EMPTY_REPORT(driver); + tap_key(key_k); + EXPECT_EQ(ucis_count(), 3); + + EXPECT_REPORT(driver, (KC_K)); + EXPECT_EMPTY_REPORT(driver); + tap_key(key_k); + EXPECT_EQ(ucis_count(), 4); + + EXPECT_NO_REPORT(driver); + tap_key(key_enter); + + EXPECT_EQ(ucis_active(), false); + + VERIFY_AND_CLEAR(driver); +} + +TEST_F(UnicodeUCIS, does_not_match_shorter_sequence) { + TestDriver driver; + + auto key_q = KeymapKey(0, 0, 0, KC_Q); + auto key_enter = KeymapKey(0, 1, 0, KC_ENTER); + + set_keymap({key_q, key_enter}); + + EXPECT_UNICODE(driver, 0x2328); // ⌨ + ucis_start(); + + EXPECT_EQ(ucis_active(), true); + EXPECT_EQ(ucis_count(), 0); + + EXPECT_REPORT(driver, (KC_Q)); + EXPECT_EMPTY_REPORT(driver); + tap_key(key_q); + EXPECT_EQ(ucis_count(), 1); + + EXPECT_NO_REPORT(driver); + tap_key(key_enter); + + EXPECT_EQ(ucis_active(), false); + + VERIFY_AND_CLEAR(driver); +}