aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonstantin Đorđević <vomindoraan@gmail.com>2019-05-03 18:33:00 +0200
committerDrashna Jaelre <drashna@live.com>2019-05-03 09:33:00 -0700
commitbdc8d89e6b8f49528b716d9bd3343a4f9e9327bd (patch)
tree72673090e6ebc1669df953b83213231d7ffb8375
parent41beecfc1847bd0039c508753a212fa18f207e3f (diff)
downloadqmk_firmware-bdc8d89e6b8f49528b716d9bd3343a4f9e9327bd.tar.gz
qmk_firmware-bdc8d89e6b8f49528b716d9bd3343a4f9e9327bd.zip
New keycode macro (XP) for shifted character pairs using UNICODEMAP + bug fixes and improvements (#4803)
* Expose unicode_saved_mods * Add UNICODEMAP shift pair functionality and XS keycode * Add XS to keycode reference documentation * Pick pair index based on both Shift and Caps Lock state * Add XS to Unicode feature docs * Clean up process_unicode* headers * Extract unicode_map index calculation into function * Pick pair index as XOR rather than OR of Shift and Caps states * unicode_input_start() has to be called before the unicode_map index is calculated * Replace unicodemap_input_error() with more generic unicode_input_cancel() * Replace register+tap+unregister with tap_code16(LCTL(LSFT(KC_U))) * UNICODE_OSX_KEY → UNICODE_KEY_OSX, UNICODE_WINC_KEY → UNICODE_KEY_WINC * Make keycode range checks more robust * Fix keycode range checks for different input modes * Add UNICODE_KEY_LNX, update docs * QK_UNICODEMAP_SHIFT → QK_UNICODEMAP_PAIR * XS → XP, update docs * Tweak Unicode docs * Use recently added MOD_MASK_SHIFT and IS_HOST_LED_ON helpers * Update Unicode table in docs/keycodes.md * Update Unicode docs per review comments * Replace references to Mac OS X with macOS in Unicode docs * As of v0.9.0, WinCompose supports all possible code points * Expand descriptions in XP docs * Update keycode table and cycling docs * Further expand cycling docs
-rw-r--r--docs/feature_unicode.md83
-rw-r--r--docs/keycodes.md16
-rw-r--r--quantum/process_keycode/process_unicode.c6
-rw-r--r--quantum/process_keycode/process_unicode.h2
-rw-r--r--quantum/process_keycode/process_unicode_common.c38
-rw-r--r--quantum/process_keycode/process_unicode_common.h13
-rw-r--r--quantum/process_keycode/process_unicodemap.c36
-rw-r--r--quantum/process_keycode/process_unicodemap.h4
-rw-r--r--quantum/quantum_keycodes.h7
-rw-r--r--users/konstantin/config.h2
10 files changed, 133 insertions, 74 deletions
diff --git a/docs/feature_unicode.md b/docs/feature_unicode.md
index 1bc3c89d2..778cdc69c 100644
--- a/docs/feature_unicode.md
+++ b/docs/feature_unicode.md
@@ -4,11 +4,11 @@ There are three Unicode keymap definition methods available in QMK:
4 4
5## `UNICODE_ENABLE` 5## `UNICODE_ENABLE`
6 6
7Supports Unicode up to `0x7FFF`. This covers characters for most modern languages, as well as symbols, but it doesn't cover emoji. The keycode function is `UC(c)` in the keymap file, where _c_ is the code point's number (preferably hexadecimal, up to 4 digits long). For example: `UC(0x45B)`, `UC(0x30C4)`. 7Supports Unicode up to `0x7FFF`. This covers characters for most modern languages, as well as symbols, but it doesn't cover emoji. The keycode function is `UC(c)` in the keymap, where _c_ is the code point's number (preferably hexadecimal, up to 4 digits long). For example: `UC(0x45B)`, `UC(0x30C4)`.
8 8
9## `UNICODEMAP_ENABLE` 9## `UNICODEMAP_ENABLE`
10 10
11Supports Unicode up to `0x10FFFF` (all possible code points). You need to maintain a separate mapping table `const uint32_t PROGMEM unicode_map[] = {...}` in your keymap file. The keycode function is `X(i)`, where _i_ is an array index into the mapping table. The table may contain at most 1024 entries. 11Supports Unicode up to `0x10FFFF` (all possible code points). You need to maintain a separate mapping table `const uint32_t PROGMEM unicode_map[] = {...}` in your keymap file. The keycode function is `X(i)`, where _i_ is an array index into the mapping table. The table may contain at most 16384 entries.
12 12
13You may want to have an enum to make referencing easier. So, you could add something like this to your keymap file: 13You may want to have an enum to make referencing easier. So, you could add something like this to your keymap file:
14 14
@@ -26,13 +26,21 @@ const uint32_t PROGMEM unicode_map[] = {
26}; 26};
27``` 27```
28 28
29Then you can use `X(BANG)` etc. in your keymap. 29Then you can use `X(BANG)`, `X(SNEK)` etc. in your keymap.
30
31### Lower and Upper Case
32
33Characters often come in lower and upper case pairs, for example: å, Å. To make inputting these characters easier, you can use `XP(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.
34
35This 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 `XP`. This blends Unicode keys in with regular alphas.
36
37Due 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#L40) function. This also allows you to, say, check Ctrl instead of Shift/Caps.
30 38
31## `UCIS_ENABLE` 39## `UCIS_ENABLE`
32 40
33Supports Unicode up to `0x10FFFF` (all possible code points). As with `UNICODEMAP`, you need to maintain a mapping table in your keymap file. However, there are no built-in keycodes for this feature — you will have to add a keycode or function that calls `qk_ucis_start()`. Once this function's been called, you can type the corresponding mnemonic for your character, then hit Space or Enter to complete it, or Esc to cancel. If the mnemonic matches an entry in your table, the typed text will automatically be erased and the corresponding Unicode character inserted. 41Supports Unicode up to `0x10FFFF` (all possible code points). As with `UNICODEMAP`, you need to maintain a mapping table in your keymap file. However, there are no built-in keycodes for this feature — you have to add a keycode or function that calls `qk_ucis_start()`. Once this function has been called, you can type the corresponding mnemonic for your character, then hit Space or Enter to complete it, or Esc to cancel. If the mnemonic matches an entry in your table, the typed text will automatically be erased and the corresponding Unicode character inserted.
34 42
35For instance, you would define a table like this in your keymap file: 43For instance, you could define a table like this in your keymap file:
36 44
37```c 45```c
38const qk_ucis_symbol_t ucis_symbol_table[] = UCIS_TABLE( 46const qk_ucis_symbol_t ucis_symbol_table[] = UCIS_TABLE(
@@ -42,7 +50,7 @@ const qk_ucis_symbol_t ucis_symbol_table[] = UCIS_TABLE(
42); 50);
43``` 51```
44 52
45You call `qk_ucis_start()`, then type "rofl" and hit Enter. QMK should erase the "rofl" text and input the laughing emoji. 53To use it, call `qk_ucis_start()`, then type "rofl" and hit Enter. QMK should erase the "rofl" text and insert the laughing emoji.
46 54
47### Customization 55### Customization
48 56
@@ -60,28 +68,29 @@ Unicode input in QMK works by inputting a sequence of characters to the OS, sort
60 68
61The following input modes are available: 69The following input modes are available:
62 70
63* **`UC_OSX`**: Mac OS X built-in Unicode hex input. Supports code points up to `0xFFFF` (`0x10FFFF` with `UNICODEMAP`). 71* **`UC_OSX`**: macOS built-in Unicode hex input. Supports code points up to `0xFFFF` (`0x10FFFF` with `UNICODEMAP`).
64 72
65 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. 73 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.
66 By default, this mode uses the left Option key (`KC_LALT`), but this can be changed by defining [`UNICODE_OSX_KEY`](#input-key-configuration) with another keycode. 74 By default, this mode uses the left Option key (`KC_LALT`) for Unicode input, but this can be changed by defining [`UNICODE_KEY_OSX`](#input-key-configuration) with another keycode.
67 75
68 **Note:** Using the _Unicode Hex Input_ input source may disable some Option based shortcuts, such as: Option + Left Arrow (`moveWordLeftAndModifySelection`) and Option + Right Arrow (`moveWordRightAndModifySelection`). 76 !> Using the _Unicode Hex Input_ input source may disable some Option based shortcuts, such as Option + Left Arrow and Option + Right Arrow.
69 77
70* **`UC_LNX`**: Linux built-in IBus Unicode input. Supports code points up to `0x10FFFF` (all possible code points). 78* **`UC_LNX`**: Linux built-in IBus Unicode input. Supports code points up to `0x10FFFF` (all possible code points).
71 79
72 Enabled by default and works almost anywhere on IBus-enabled distros. Without IBus, this mode works under GTK apps, but rarely anywhere else. 80 Enabled by default and works almost anywhere on IBus-enabled distros. Without IBus, this mode works under GTK apps, but rarely anywhere else.
81 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 another keycode. This might be required for IBus versions ≥1.5.15, where Ctrl+Shift+U behavior is consolidated into Ctrl+Shift+E.
73 82
74* **`UC_WIN`**: _(not recommended)_ Windows built-in hex numpad Unicode input. Supports code points up to `0xFFFF`. 83* **`UC_WIN`**: _(not recommended)_ Windows built-in hex numpad Unicode input. Supports code points up to `0xFFFF`.
75 84
76 To enable, create a registry key under `HKEY_CURRENT_USER\Control Panel\Input Method\EnableHexNumpad` 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. Afterwards, reboot. 85 To enable, create a registry key under `HKEY_CURRENT_USER\Control Panel\Input Method\EnableHexNumpad` 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.
77 This mode is not recommended because of reliability and compatibility issues; use the `UC_WINC` mode instead. 86 This mode is not recommended because of reliability and compatibility issues; use the `UC_WINC` mode instead.
78 87
79* **`UC_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). 88* **`UC_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).
80 89
81* **`UC_WINC`**: Windows Unicode input using [WinCompose](https://github.com/samhocevar/wincompose). As of v0.8.2, supports code points up to `0xFFFFF` (all currently assigned code points). 90* **`UC_WINC`**: 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).
82 91
83 To enable, install the [latest release](https://github.com/samhocevar/wincompose/releases/latest). Once installed, WinCompose will automatically run on startup. Works reliably under all version of Windows supported by the app. 92 To enable, install the [latest release](https://github.com/samhocevar/wincompose/releases/latest). Once installed, WinCompose will automatically run on startup. Works reliably under all version of Windows supported by the app.
84 By default, this mode uses the right Alt key (`KC_RALT`), but this can be changed in the WinCompose settings and by defining [`UNICODE_WINC_KEY`](#input-key-configuration) with another keycode. 93 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 another keycode.
85 94
86### Switching Input Modes 95### Switching Input Modes
87 96
@@ -89,17 +98,17 @@ There are two ways to set the input mode for Unicode: by keycode or by function.
89 98
90You can switch the input mode at any time by using one of the following keycodes. The easiest way is to add the ones you use to your keymap. 99You can switch the input mode at any time by using one of the following keycodes. The easiest way is to add the ones you use to your keymap.
91 100
92|Keycode |Alias |Input mode |Description | 101|Keycode |Alias |Input Mode |Description |
93|-----------------------|---------|-------------|-----------------------------------------| 102|----------------------|---------|------------|--------------------------------------------------------------|
94|`UNICODE_MODE_FORWARD` |`UC_MOD` | |Cycles forwards through the available modes. [(Disabled by default)](#input-method-cycling)| 103|`UNICODE_MODE_FORWARD`|`UC_MOD` |Next in list|[Cycle](#input-mode-cycling) through selected modes |
95|`UNICODE_MODE_REVERSE` |`UC_RMOD`| |Cycles forwards through the available modes. [(Disabled by default)](#input-method-cycling)| 104|`UNICODE_MODE_REVERSE`|`UC_RMOD`|Prev in list|[Cycle](#input-mode-cycling) through selected modes in reverse|
96|`UNICODE_MODE_OSX` |`UC_M_OS`|`UC_OSX` |Switch to Mac OS X input. | 105|`UNICODE_MODE_OSX` |`UC_M_OS`|`UC_OSX` |Switch to macOS input |
97|`UNICODE_MODE_LNX` |`UC_M_LN`|`UC_LNX` |Switch to Linux input. | 106|`UNICODE_MODE_LNX` |`UC_M_LN`|`UC_LNX` |Switch to Linux input |
98|`UNICODE_MODE_WIN` |`UC_M_WI`|`UC_WIN` |Switch to Windows input. | 107|`UNICODE_MODE_WIN` |`UC_M_WI`|`UC_WIN` |Switch to Windows input |
99|`UNICODE_MODE_BSD` |`UC_M_BS`|`UC_BSD` |Switch to BSD input (not implemented). | 108|`UNICODE_MODE_BSD` |`UC_M_BS`|`UC_BSD` |Switch to BSD input (not implemented) |
100|`UNICODE_MODE_WINC` |`UC_M_WC`|`UC_WINC` |Switch to Windows input using WinCompose.| 109|`UNICODE_MODE_WINC` |`UC_M_WC`|`UC_WINC` |Switch to Windows input using WinCompose |
101 110
102You 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. `UC_LNX`). Since the function only needs to be called once, it's recommended that you do it in `eeconfig_init_user` (or a similar function). For example: 111You 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. `UC_LNX`). Since the function only needs to be called once, it's recommended that you do it in `eeconfig_init_user()` (or a similar function). For example:
103 112
104```c 113```c
105void eeconfig_init_user(void) { 114void eeconfig_init_user(void) {
@@ -123,35 +132,45 @@ For instance, you can add these definitions to your `config.h` file:
123 132
124### Additional Customization 133### Additional Customization
125 134
126Because Unicode is such a large and variable feature, there are a number of options that you can customize to work better on your system. 135Because Unicode is a large and versatile feature, there are a number of options you can customize to make it work better on your system.
127 136
128#### Start and Finish input functions 137#### Start and Finish Input Functions
129 138
130The 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. 139The 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.
131 140
132* `void unicode_input_start(void)` – This sends the initial sequence that tells your platform to enter Unicode input mode. For example, it presses Ctrl+Shift+U on Linux and holds the Option key on Mac. 141* `void unicode_input_start(void)` – This sends the initial sequence that tells your platform to enter Unicode input mode. For example, it presses Ctrl+Shift+U on Linux and holds the Option key on macOS.
133* `void unicode_input_finish(void)` – This is called to exit Unicode input mode, for example by pressing Space or releasing the Option key. 142* `void unicode_input_finish(void)` – This is called to exit Unicode input mode, for example by pressing Space or releasing the Option key.
134 143
135You 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). 144You 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).
136 145
137
138#### Input Key Configuration 146#### Input Key Configuration
139 147
140Additionally, you can customize the keys used to trigger the unicode input for macOS and WinCompose by adding defines to your `config.h` 148You 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).
149
150|Define |Type |Default |Example |
151|------------------|----------|------------------|-------------------------------------------|
152|`UNICODE_KEY_OSX` |`uint8_t` |`KC_LALT` |`#define UNICODE_KEY_OSX KC_RALT` |
153|`UNICODE_KEY_LNX` |`uint16_t`|`LCTL(LSFT(KC_U))`|`#define UNICODE_KEY_LNX LCTL(LSFT(KC_E))`|
154|`UNICODE_KEY_WINC`|`uint8_t` |`KC_RALT` |`#define UNICODE_KEY_WINC KC_RGUI` |
155
156#### Input Mode Cycling
157
158You can choose which input modes are available for cycling through. By default, this is disabled. If you want to enable it, limiting it to just the modes you use makes sense. Note that the values in the list are comma-delimited.
141 159
142```c 160```c
143#define UNICODE_OSX_KEY KC_LALT 161#define UNICODE_SELECTED_MODES UC_OSX, UC_LNX, UC_WIN, UC_WINC
144#define UNICODE_WINC_KEY KC_RALT
145``` 162```
146 163
147#### Input Method Cycling 164You can cycle through the selected modes by using the `UC_MOD`/`UC_RMOD` keycodes, or by calling `cycle_unicode_input_mode(offset)` in your code (`offset` is how many modes to move forward by, so +1 corresponds to `UC_MOD`).
148 165
149Also, you can choose which input methods are availble for cycling through. By default, this is disabled. But if you want to enabled it, then limiting it to just those modes makes sense. Note that `UNICODE_SELECTED_MODES` define is comma delimited. 166By default, when the keyboard boots, it will initialize the input mode to the last one you used. You can disable this and make it start with the first mode in the list every time by adding the following to your `config.h`:
150 167
151```c 168```c
152#define UNICODE_SELECTED_MODES UC_OSX, UC_LNX, UC_WIN, UC_BSD, UC_WINC 169#define UNICODE_CYCLE_PERSIST false
153``` 170```
154 171
172!> Using `UNICODE_SELECTED_MODES` means you don't have to initially set the input mode in `matrix_init_user()` (or a similar function); the Unicode system will do that for you on startup. This has the added benefit of avoiding unnecessary writes to EEPROM.
173
155## `send_unicode_hex_string` 174## `send_unicode_hex_string`
156 175
157To type multiple characters for things like (ノಠ痊ಠ)ノ彡┻━┻, you can use `send_unicode_hex_string()` much like `SEND_STRING()` except you would use hex values separate by spaces. 176To type multiple characters for things like (ノಠ痊ಠ)ノ彡┻━┻, you can use `send_unicode_hex_string()` much like `SEND_STRING()` except you would use hex values separate by spaces.
diff --git a/docs/keycodes.md b/docs/keycodes.md
index 91578414d..c0e6aa5c6 100644
--- a/docs/keycodes.md
+++ b/docs/keycodes.md
@@ -450,7 +450,15 @@ This is a reference only. Each group of keys links to the page documenting their
450 450
451## [Unicode Support](feature_unicode.md) 451## [Unicode Support](feature_unicode.md)
452 452
453|Key |Description | 453|Key |Aliases |Description |
454|-------|---------------------------------------------------------------------------| 454|----------------------|---------|----------------------------------------------------------------|
455|`UC(c)`|Send Unicode code point `c` (`UNICODE_ENABLE`) | 455|`UC(c)` | |Send Unicode code point `c` |
456|`X(i)` |Send Unicode code point at index `i` in `unicode_map` (`UNICODEMAP_ENABLE`)| 456|`X(i)` | |Send Unicode code point at index `i` in `unicode_map` |
457|`XP(i, j)` | |Send Unicode code point at index `i`, or `j` if Shift/Caps is on|
458|`UNICODE_MODE_FORWARD`|`UC_MOD` |Cycle through selected input modes |
459|`UNICODE_MODE_REVERSE`|`UC_RMOD`|Cycle through selected input modes in reverse |
460|`UNICODE_MODE_OSX` |`UC_M_OS`|Switch to macOS input |
461|`UNICODE_MODE_LNX` |`UC_M_LN`|Switch to Linux input |
462|`UNICODE_MODE_WIN` |`UC_M_WI`|Switch to Windows input |
463|`UNICODE_MODE_BSD` |`UC_M_BS`|Switch to BSD input (not implemented) |
464|`UNICODE_MODE_WINC` |`UC_M_WC`|Switch to Windows input using WinCompose |
diff --git a/quantum/process_keycode/process_unicode.c b/quantum/process_keycode/process_unicode.c
index 19beb8452..2c914013a 100644
--- a/quantum/process_keycode/process_unicode.c
+++ b/quantum/process_keycode/process_unicode.c
@@ -13,15 +13,15 @@
13 * You should have received a copy of the GNU General Public License 13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */ 15 */
16
16#include "process_unicode.h" 17#include "process_unicode.h"
17#include "action_util.h" 18#include "action_util.h"
18#include "eeprom.h" 19#include "eeprom.h"
19 20
20bool process_unicode(uint16_t keycode, keyrecord_t *record) { 21bool process_unicode(uint16_t keycode, keyrecord_t *record) {
21 if (keycode > QK_UNICODE && record->event.pressed) { 22 if (keycode >= QK_UNICODE && keycode <= QK_UNICODE_MAX && record->event.pressed) {
22 uint16_t unicode = keycode & 0x7FFF;
23 unicode_input_start(); 23 unicode_input_start();
24 register_hex(unicode); 24 register_hex(keycode & 0x7FFF);
25 unicode_input_finish(); 25 unicode_input_finish();
26 } 26 }
27 return true; 27 return true;
diff --git a/quantum/process_keycode/process_unicode.h b/quantum/process_keycode/process_unicode.h
index 0913e9910..22765ad56 100644
--- a/quantum/process_keycode/process_unicode.h
+++ b/quantum/process_keycode/process_unicode.h
@@ -13,9 +13,9 @@
13 * You should have received a copy of the GNU General Public License 13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */ 15 */
16
16#pragma once 17#pragma once
17 18
18#include "quantum.h"
19#include "process_unicode_common.h" 19#include "process_unicode_common.h"
20 20
21bool process_unicode(uint16_t keycode, keyrecord_t *record); 21bool 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 d0a9cf232..21ac2291d 100644
--- a/quantum/process_keycode/process_unicode_common.c
+++ b/quantum/process_keycode/process_unicode_common.c
@@ -20,6 +20,8 @@
20#include <string.h> 20#include <string.h>
21 21
22unicode_config_t unicode_config; 22unicode_config_t unicode_config;
23uint8_t unicode_saved_mods;
24
23#if UNICODE_SELECTED_MODES != -1 25#if UNICODE_SELECTED_MODES != -1
24static uint8_t selected[] = { UNICODE_SELECTED_MODES }; 26static uint8_t selected[] = { UNICODE_SELECTED_MODES };
25static uint8_t selected_count = sizeof selected / sizeof *selected; 27static uint8_t selected_count = sizeof selected / sizeof *selected;
@@ -75,30 +77,24 @@ void persist_unicode_input_mode(void) {
75 eeprom_update_byte(EECONFIG_UNICODEMODE, unicode_config.input_mode); 77 eeprom_update_byte(EECONFIG_UNICODEMODE, unicode_config.input_mode);
76} 78}
77 79
78static uint8_t saved_mods;
79
80__attribute__((weak)) 80__attribute__((weak))
81void unicode_input_start(void) { 81void unicode_input_start(void) {
82 saved_mods = get_mods(); // Save current mods 82 unicode_saved_mods = get_mods(); // Save current mods
83 clear_mods(); // Unregister mods to start from a clean state 83 clear_mods(); // Unregister mods to start from a clean state
84 84
85 switch (unicode_config.input_mode) { 85 switch (unicode_config.input_mode) {
86 case UC_OSX: 86 case UC_OSX:
87 register_code(UNICODE_OSX_KEY); 87 register_code(UNICODE_KEY_OSX);
88 break; 88 break;
89 case UC_LNX: 89 case UC_LNX:
90 register_code(KC_LCTL); 90 tap_code16(UNICODE_KEY_LNX);
91 register_code(KC_LSFT);
92 tap_code(KC_U); // TODO: Replace with tap_code16(LCTL(LSFT(KC_U))); and test
93 unregister_code(KC_LSFT);
94 unregister_code(KC_LCTL);
95 break; 91 break;
96 case UC_WIN: 92 case UC_WIN:
97 register_code(KC_LALT); 93 register_code(KC_LALT);
98 tap_code(KC_PPLS); 94 tap_code(KC_PPLS);
99 break; 95 break;
100 case UC_WINC: 96 case UC_WINC:
101 tap_code(UNICODE_WINC_KEY); 97 tap_code(UNICODE_KEY_WINC);
102 tap_code(KC_U); 98 tap_code(KC_U);
103 break; 99 break;
104 } 100 }
@@ -110,7 +106,7 @@ __attribute__((weak))
110void unicode_input_finish(void) { 106void unicode_input_finish(void) {
111 switch (unicode_config.input_mode) { 107 switch (unicode_config.input_mode) {
112 case UC_OSX: 108 case UC_OSX:
113 unregister_code(UNICODE_OSX_KEY); 109 unregister_code(UNICODE_KEY_OSX);
114 break; 110 break;
115 case UC_LNX: 111 case UC_LNX:
116 tap_code(KC_SPC); 112 tap_code(KC_SPC);
@@ -123,7 +119,25 @@ void unicode_input_finish(void) {
123 break; 119 break;
124 } 120 }
125 121
126 set_mods(saved_mods); // Reregister previously set mods 122 set_mods(unicode_saved_mods); // Reregister previously set mods
123}
124
125__attribute__((weak))
126void unicode_input_cancel(void) {
127 switch (unicode_config.input_mode) {
128 case UC_OSX:
129 unregister_code(UNICODE_KEY_OSX);
130 break;
131 case UC_LNX:
132 case UC_WINC:
133 tap_code(KC_ESC);
134 break;
135 case UC_WIN:
136 unregister_code(KC_LALT);
137 break;
138 }
139
140 set_mods(unicode_saved_mods); // Reregister previously set mods
127} 141}
128 142
129__attribute__((weak)) 143__attribute__((weak))
diff --git a/quantum/process_keycode/process_unicode_common.h b/quantum/process_keycode/process_unicode_common.h
index e608ab76b..7340800e5 100644
--- a/quantum/process_keycode/process_unicode_common.h
+++ b/quantum/process_keycode/process_unicode_common.h
@@ -23,11 +23,14 @@
23#endif 23#endif
24 24
25// Keycodes used for starting Unicode input on different platforms 25// Keycodes used for starting Unicode input on different platforms
26#ifndef UNICODE_OSX_KEY 26#ifndef UNICODE_KEY_OSX
27 #define UNICODE_OSX_KEY KC_LALT 27 #define UNICODE_KEY_OSX KC_LALT
28#endif 28#endif
29#ifndef UNICODE_WINC_KEY 29#ifndef UNICODE_KEY_LNX
30 #define UNICODE_WINC_KEY KC_RALT 30 #define UNICODE_KEY_LNX LCTL(LSFT(KC_U))
31#endif
32#ifndef UNICODE_KEY_WINC
33 #define UNICODE_KEY_WINC KC_RALT
31#endif 34#endif
32 35
33// Comma-delimited, ordered list of input modes selected for use (e.g. in cycle) 36// Comma-delimited, ordered list of input modes selected for use (e.g. in cycle)
@@ -63,6 +66,7 @@ typedef union {
63} unicode_config_t; 66} unicode_config_t;
64 67
65extern unicode_config_t unicode_config; 68extern unicode_config_t unicode_config;
69extern uint8_t unicode_saved_mods;
66 70
67void unicode_input_mode_init(void); 71void unicode_input_mode_init(void);
68uint8_t get_unicode_input_mode(void); 72uint8_t get_unicode_input_mode(void);
@@ -72,6 +76,7 @@ void persist_unicode_input_mode(void);
72 76
73void unicode_input_start(void); 77void unicode_input_start(void);
74void unicode_input_finish(void); 78void unicode_input_finish(void);
79void unicode_input_cancel(void);
75 80
76void register_hex(uint16_t hex); 81void register_hex(uint16_t hex);
77void send_unicode_hex_string(const char *str); 82void send_unicode_hex_string(const char *str);
diff --git a/quantum/process_keycode/process_unicodemap.c b/quantum/process_keycode/process_unicodemap.c
index 327402761..b88787986 100644
--- a/quantum/process_keycode/process_unicodemap.c
+++ b/quantum/process_keycode/process_unicodemap.c
@@ -15,7 +15,6 @@
15 */ 15 */
16 16
17#include "process_unicodemap.h" 17#include "process_unicodemap.h"
18#include "process_unicode_common.h"
19 18
20void register_hex32(uint32_t hex) { 19void register_hex32(uint32_t hex) {
21 bool onzerostart = true; 20 bool onzerostart = true;
@@ -38,28 +37,39 @@ void register_hex32(uint32_t hex) {
38} 37}
39 38
40__attribute__((weak)) 39__attribute__((weak))
41void unicodemap_input_error() {} 40uint16_t unicodemap_index(uint16_t keycode) {
41 if (keycode >= QK_UNICODEMAP_PAIR) {
42 // Keycode is a pair: extract index based on Shift / Caps Lock state
43 uint16_t index = keycode - QK_UNICODEMAP_PAIR;
44
45 bool shift = unicode_saved_mods & MOD_MASK_SHIFT, caps = IS_HOST_LED_ON(USB_LED_CAPS_LOCK);
46 if (shift ^ caps) { index >>= 7; }
47
48 return index & 0x7F;
49 } else {
50 // Keycode is a regular index
51 return keycode - QK_UNICODEMAP;
52 }
53}
42 54
43bool process_unicodemap(uint16_t keycode, keyrecord_t *record) { 55bool process_unicodemap(uint16_t keycode, keyrecord_t *record) {
44 if ((keycode & QK_UNICODEMAP) == QK_UNICODEMAP && record->event.pressed) { 56 if (keycode >= QK_UNICODEMAP && keycode <= QK_UNICODEMAP_PAIR_MAX && record->event.pressed) {
45 uint16_t index = keycode - QK_UNICODEMAP; 57 unicode_input_start();
46 uint32_t code = pgm_read_dword(unicode_map + index); 58
59 uint32_t code = pgm_read_dword(unicode_map + unicodemap_index(keycode));
47 uint8_t input_mode = get_unicode_input_mode(); 60 uint8_t input_mode = get_unicode_input_mode();
48 61
49 if (code > 0xFFFF && code <= 0x10FFFF && input_mode == UC_OSX) { 62 if (code > 0x10FFFF || (code > 0xFFFF && input_mode == UC_WIN)) {
50 // Convert to UTF-16 surrogate pair 63 // Character is out of range supported by the platform
64 unicode_input_cancel();
65 } else if (code > 0xFFFF && input_mode == UC_OSX) {
66 // Convert to UTF-16 surrogate pair on Mac
51 code -= 0x10000; 67 code -= 0x10000;
52 uint32_t lo = code & 0x3FF, hi = (code & 0xFFC00) >> 10; 68 uint32_t lo = code & 0x3FF, hi = (code & 0xFFC00) >> 10;
53
54 unicode_input_start();
55 register_hex32(hi + 0xD800); 69 register_hex32(hi + 0xD800);
56 register_hex32(lo + 0xDC00); 70 register_hex32(lo + 0xDC00);
57 unicode_input_finish(); 71 unicode_input_finish();
58 } else if ((code > 0x10FFFF && input_mode == UC_OSX) || (code > 0xFFFFF && input_mode == UC_LNX)) {
59 // Character is out of range supported by the OS
60 unicodemap_input_error();
61 } else { 72 } else {
62 unicode_input_start();
63 register_hex32(code); 73 register_hex32(code);
64 unicode_input_finish(); 74 unicode_input_finish();
65 } 75 }
diff --git a/quantum/process_keycode/process_unicodemap.h b/quantum/process_keycode/process_unicodemap.h
index fe4f97915..51709c5dc 100644
--- a/quantum/process_keycode/process_unicodemap.h
+++ b/quantum/process_keycode/process_unicodemap.h
@@ -16,10 +16,10 @@
16 16
17#pragma once 17#pragma once
18 18
19#include "quantum.h"
20#include "process_unicode_common.h" 19#include "process_unicode_common.h"
21 20
22extern const uint32_t PROGMEM unicode_map[]; 21extern const uint32_t PROGMEM unicode_map[];
23 22
24void unicodemap_input_error(void); 23void register_hex32(uint32_t hex);
24uint16_t unicodemap_index(uint16_t keycode);
25bool process_unicodemap(uint16_t keycode, keyrecord_t *record); 25bool process_unicodemap(uint16_t keycode, keyrecord_t *record);
diff --git a/quantum/quantum_keycodes.h b/quantum/quantum_keycodes.h
index 19bd7c216..779c355ef 100644
--- a/quantum/quantum_keycodes.h
+++ b/quantum/quantum_keycodes.h
@@ -87,7 +87,9 @@ enum quantum_keycodes {
87#endif 87#endif
88#ifdef UNICODEMAP_ENABLE 88#ifdef UNICODEMAP_ENABLE
89 QK_UNICODEMAP = 0x8000, 89 QK_UNICODEMAP = 0x8000,
90 QK_UNICODEMAP_MAX = 0x83FF, 90 QK_UNICODEMAP_MAX = 0xBFFF,
91 QK_UNICODEMAP_PAIR = 0xC000,
92 QK_UNICODEMAP_PAIR_MAX = 0xFFFF,
91#endif 93#endif
92 94
93 // Loose keycodes - to be used directly 95 // Loose keycodes - to be used directly
@@ -712,7 +714,8 @@ enum quantum_keycodes {
712#endif 714#endif
713#ifdef UNICODEMAP_ENABLE 715#ifdef UNICODEMAP_ENABLE
714 // Allows Unicode input up to 0x10FFFF, requires unicode_map 716 // Allows Unicode input up to 0x10FFFF, requires unicode_map
715 #define X(i) (QK_UNICODEMAP | (i)) 717 #define X(i) (QK_UNICODEMAP | (i))
718 #define XP(i, j) (QK_UNICODEMAP_PAIR | ((i) & 0x7F) | (((j) & 0x7F) << 7)) // 127 max i and j
716#endif 719#endif
717 720
718#define UC_MOD UNICODE_MODE_FORWARD 721#define UC_MOD UNICODE_MODE_FORWARD
diff --git a/users/konstantin/config.h b/users/konstantin/config.h
index f18c4a70e..4ca19f824 100644
--- a/users/konstantin/config.h
+++ b/users/konstantin/config.h
@@ -19,4 +19,4 @@
19 19
20#define UNICODE_CYCLE_PERSIST false 20#define UNICODE_CYCLE_PERSIST false
21#define UNICODE_SELECTED_MODES UC_WINC, UC_WIN, UC_LNX 21#define UNICODE_SELECTED_MODES UC_WINC, UC_WIN, UC_LNX
22#define UNICODE_WINC_KEY KC_RGUI 22#define UNICODE_KEY_WINC KC_RGUI