From dd36ebadc23119e9f740e088dcbf35bc49e89afa Mon Sep 17 00:00:00 2001 From: Drashna Jaelre Date: Sun, 25 Nov 2018 13:06:29 -0800 Subject: [PATCH] Unicode common cleanup (#17) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Standardize the Unicode EEPROM code * Remove unicode init from process_record_* functions * Add unicode init to where it belongs: matrix_init_quantum * Move Unicode proccessing to unicode common * Add audio feedback to input mode keys to drive konstantin up a wall * Tap_code cleanup * Update keycodes * Update unicode documentation * Update unicode keycodes for consistency/easier merge * Add Audio Feedback section * Remove Functions from feature page And link to the file instead. Link to specific lines later on. * Fix white spaces Co-Authored-By: drashna * Fix spacing Co-Authored-By: drashna * Because I missed it! Co-Authored-By: drashna * Fix spacing Co-Authored-By: drashna * SPAAAAAAAAAACing Co-Authored-By: drashna * white spaces Co-Authored-By: drashna * Add BSD for future compatibility * Thought I fixed that! Co-Authored-By: drashna * non-breaking Co-Authored-By: drashna * Considered that Co-Authored-By: drashna * Yuuup Co-Authored-By: drashna * consistency Co-Authored-By: drashna * white spaces .... copied from elsewhere Co-Authored-By: drashna * white spaces Co-Authored-By: drashna * white spaces Co-Authored-By: drashna * Update keycode defines * Fix Linux Song * Update all of the songs * Cleanup * Move and update check to ensure only one unicode method is enabled * Update quantum/quantum_keycodes.h * Update documentation * Wordsmithing and cleanup * Format unicode_common (#13) * case alignment * process_record_unicode_common → process_unicode_common * Move song arrays into function where they're used, align preprocessor directives * Swap the order of UC_WIN and UC_BSD * Update Unicode docs * Reorder Unicode mode stuff to match the order of input mode constants * Fix capitalization in doc subtitle * Readd BSD and OSX_RALT songs * Reword BSD note in docs * Readd BSD keycode description * Reword explanation of input on different platforms * Steal vomindoraan's input mode documentation Co-Authored-By: vomindoraan (vomindoraan@gmail.com) * Willingly give Drashna the rest of my Unicode doc improvements * Wordsmithing Co-Authored-By: drashna * Rearrange process_unicode_common functions * Make Unicode input mode constants (UC_*) an enum * Simplify unicode_input_start/finish code * Make the key used for WinCompose configurable * Remove UC_OSX_RALT in favor of setting the key with UNICODE_OSX_KEY * Update Unicode input mode doc * Add descriptions and rearrange definitions in process_unicode_common.h * Add registry command to Unicode docs + misc updates * Reword an explanation in Unicode docs * Add TODO comment * Remove trailing whitespace --- docs/feature_unicode.md | 6 +-- .../process_keycode/process_unicode_common.c | 39 +++++++------------ .../process_keycode/process_unicode_common.h | 35 +++++++++++------ quantum/process_keycode/process_unicodemap.c | 4 +- 4 files changed, 43 insertions(+), 41 deletions(-) diff --git a/docs/feature_unicode.md b/docs/feature_unicode.md index 86923a54ec59..648504f99029 100644 --- a/docs/feature_unicode.md +++ b/docs/feature_unicode.md @@ -64,8 +64,7 @@ The following input modes are available: * **`UC_OSX`**: Mac OS X built-in Unicode hex input. Supports code points up to `0xFFFF` (`0x10FFFF` with `UNICODEMAP`). 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. - -* **`UC_OSX_RALT`**: Same as `UC_OSX`, but sends the Right Alt/Option key for Unicode input. + By default, this mode uses the left Option key (`KC_LALT`), but this can be changed by defining `UNICODE_OSX_KEY` with another keycode. * **`UC_LNX`**: Linux built-in IBus Unicode input. Supports all possible code points (`0x10FFFF`). @@ -81,7 +80,7 @@ The following input modes are available: * **`UC_WINC`**: Windows Unicode input using [WinCompose](https://github.com/samhocevar/wincompose). As of v0.8.2, supports code points up to `0xFFFFF`. 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. - This mode relies on the Compose key being set to Right Alt (`KC_RALT`). If you change it to something else in the WinCompose settings, this mode will not work. + 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` with another keycode. ### Switching Input Modes @@ -118,7 +117,6 @@ For instance, you can add these definitions to your `config.h` file: #define UNICODE_SONG_BSD MARIO_GAMEOVER #define UNICODE_SONG_WIN UNICODE_WINDOWS #define UNICODE_SONG_WINC UNICODE_WINDOWS -#define UNICODE_SONG_OSX_RALT COIN_SOUND ``` ### Additional Customization diff --git a/quantum/process_keycode/process_unicode_common.c b/quantum/process_keycode/process_unicode_common.c index 12315c0922ba..82ae0a5ad4eb 100644 --- a/quantum/process_keycode/process_unicode_common.c +++ b/quantum/process_keycode/process_unicode_common.c @@ -16,25 +16,26 @@ #include "process_unicode_common.h" #include "eeprom.h" -#include #include +#include unicode_config_t unicode_config; -static uint8_t saved_mods; -void set_unicode_input_mode(uint8_t os_target) { - unicode_config.input_mode = os_target; - eeprom_update_byte(EECONFIG_UNICODEMODE, os_target); +void unicode_input_mode_init(void) { + unicode_config.raw = eeprom_read_byte(EECONFIG_UNICODEMODE); } uint8_t get_unicode_input_mode(void) { return unicode_config.input_mode; } -void unicode_input_mode_init(void) { - unicode_config.raw = eeprom_read_byte(EECONFIG_UNICODEMODE); +void set_unicode_input_mode(uint8_t mode) { + unicode_config.input_mode = mode; + eeprom_update_byte(EECONFIG_UNICODEMODE, mode); } +static uint8_t saved_mods; + __attribute__((weak)) void unicode_input_start(void) { saved_mods = get_mods(); // Save current mods @@ -42,15 +43,12 @@ void unicode_input_start(void) { switch (unicode_config.input_mode) { case UC_OSX: - register_code(KC_LALT); - break; - case UC_OSX_RALT: - register_code(KC_RALT); + register_code(UNICODE_OSX_KEY); break; case UC_LNX: register_code(KC_LCTL); register_code(KC_LSFT); - tap_code(KC_U); + tap_code(KC_U); // TODO: Replace with tap_code16(LCTL(LSFT(KC_U))); and test unregister_code(KC_LSFT); unregister_code(KC_LCTL); break; @@ -61,10 +59,11 @@ void unicode_input_start(void) { tap_code(KC_PPLS); break; case UC_WINC: - tap_code(KC_RALT); + tap_code(UNICODE_WINC_KEY); tap_code(KC_U); break; } + wait_ms(UNICODE_TYPE_DELAY); } @@ -72,12 +71,11 @@ __attribute__((weak)) void unicode_input_finish(void) { switch (unicode_config.input_mode) { case UC_OSX: + unregister_code(UNICODE_OSX_KEY); + break; case UC_WIN: unregister_code(KC_LALT); break; - case UC_OSX_RALT: - unregister_code(KC_RALT); - break; case UC_LNX: tap_code(KC_SPC); break; @@ -105,7 +103,7 @@ void register_hex(uint16_t hex) { } void send_unicode_hex_string(const char *str) { - if (!str) { return; } // Safety net + if (!str) { return; } while (*str) { // Find the next code point (token) in the string @@ -165,13 +163,6 @@ bool process_unicode_common(uint16_t keycode, keyrecord_t *record) { #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_WINC) static float song_winc[][2] = UNICODE_SONG_WINC; PLAY_SONG(song_winc); -#endif - break; - case UNICODE_MODE_OSX_RALT: - set_unicode_input_mode(UC_OSX_RALT); -#if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_OSX_RALT) - static float song_osx_ralt[][2] = UNICODE_SONG_OSX_RALT; - PLAY_SONG(song_osx_ralt); #endif break; } diff --git a/quantum/process_keycode/process_unicode_common.h b/quantum/process_keycode/process_unicode_common.h index 8fdc312eb32e..056c1a8518df 100644 --- a/quantum/process_keycode/process_unicode_common.h +++ b/quantum/process_keycode/process_unicode_common.h @@ -22,37 +22,50 @@ #error "Cannot enable more than one Unicode method (UNICODE, UNICODEMAP, UCIS) at the same time" #endif +// Keycodes used for starting Unicode input on different platforms +#ifndef UNICODE_OSX_KEY + #define UNICODE_OSX_KEY KC_LALT +#endif +#ifndef UNICODE_WINC_KEY + #define UNICODE_WINC_KEY KC_RALT +#endif + +// Delay between starting Unicode input and sending a sequence, in ms #ifndef UNICODE_TYPE_DELAY #define UNICODE_TYPE_DELAY 10 #endif +enum unicode_input_modes { + UC_OSX, // Mac OS X using Unicode Hex Input + UC_LNX, // Linux using IBus + UC_WIN, // Windows using EnableHexNumpad + UC_BSD, // BSD (not implemented) + UC_WINC, // Windows using WinCompose (https://github.com/samhocevar/wincompose) + UC__COUNT // Number of available input modes (always leave at the end) +}; + typedef union { uint32_t raw; struct { - uint8_t input_mode :8; + uint8_t input_mode : 8; }; } unicode_config_t; extern unicode_config_t unicode_config; -void set_unicode_input_mode(uint8_t os_target); -uint8_t get_unicode_input_mode(void); void unicode_input_mode_init(void); +uint8_t get_unicode_input_mode(void); +void set_unicode_input_mode(uint8_t mode); + void unicode_input_start(void); void unicode_input_finish(void); + void register_hex(uint16_t hex); void send_unicode_hex_string(const char *str); -bool process_unicode_common(uint16_t keycode, keyrecord_t *record); -#define UC_OSX 0 // Mac OS X -#define UC_LNX 1 // Linux -#define UC_WIN 2 // Windows 'HexNumpad' -#define UC_BSD 3 // BSD (not implemented) -#define UC_WINC 4 // WinCompose https://github.com/samhocevar/wincompose -#define UC_OSX_RALT 5 // Mac OS X using Right Alt key for Unicode Compose +bool process_unicode_common(uint16_t keycode, keyrecord_t *record); #define UC_BSPC UC(0x0008) - #define UC_SPC UC(0x0020) #define UC_EXLM UC(0x0021) diff --git a/quantum/process_keycode/process_unicodemap.c b/quantum/process_keycode/process_unicodemap.c index 47c27b911716..75f35112b133 100644 --- a/quantum/process_keycode/process_unicodemap.c +++ b/quantum/process_keycode/process_unicodemap.c @@ -50,7 +50,7 @@ bool process_unicode_map(uint16_t keycode, keyrecord_t *record) { const uint32_t* map = unicode_map; uint16_t index = keycode - QK_UNICODE_MAP; uint32_t code = pgm_read_dword(&map[index]); - if (code > 0xFFFF && code <= 0x10ffff && (input_mode == UC_OSX || input_mode == UC_OSX_RALT)) { + if (code > 0xFFFF && code <= 0x10ffff && input_mode == UC_OSX) { // Convert to UTF-16 surrogate pair code -= 0x10000; uint32_t lo = code & 0x3ff; @@ -59,7 +59,7 @@ bool process_unicode_map(uint16_t keycode, keyrecord_t *record) { register_hex32(hi + 0xd800); register_hex32(lo + 0xdc00); unicode_input_finish(); - } else if ((code > 0x10ffff && (input_mode == UC_OSX || input_mode == UC_OSX_RALT)) || (code > 0xFFFFF && input_mode == UC_LNX)) { + } else if ((code > 0x10ffff && input_mode == UC_OSX) || (code > 0xFFFFF && input_mode == UC_LNX)) { // when character is out of range supported by the OS unicode_map_input_error(); } else {