Skip to content

Commit

Permalink
Unicode common cleanup (#17)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Fix spacing

Co-Authored-By: drashna <[email protected]>

* Because I missed it!

Co-Authored-By: drashna <[email protected]>

* Fix spacing

Co-Authored-By: drashna <[email protected]>

* SPAAAAAAAAAACing

Co-Authored-By: drashna <[email protected]>

* white spaces

Co-Authored-By: drashna <[email protected]>

* Add BSD for future compatibility

* Thought I fixed that!

Co-Authored-By: drashna <[email protected]>

* non-breaking

Co-Authored-By: drashna <[email protected]>

* Considered that

Co-Authored-By: drashna <[email protected]>

* Yuuup

Co-Authored-By: drashna <[email protected]>

* consistency

Co-Authored-By: drashna <[email protected]>

* white spaces .... copied from elsewhere

Co-Authored-By: drashna <[email protected]>

* white spaces

Co-Authored-By: drashna <[email protected]>

* white spaces

Co-Authored-By: drashna <[email protected]>

* 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 ([email protected])

* Willingly give Drashna the rest of my Unicode doc improvements

* Wordsmithing

Co-Authored-By: drashna <[email protected]>

* 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
  • Loading branch information
drashna committed Dec 6, 2018
1 parent 41e6877 commit dd36eba
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 41 deletions.
6 changes: 2 additions & 4 deletions docs/feature_unicode.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
39 changes: 15 additions & 24 deletions quantum/process_keycode/process_unicode_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,39 @@

#include "process_unicode_common.h"
#include "eeprom.h"
#include <string.h>
#include <ctype.h>
#include <string.h>

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
clear_mods(); // Unregister mods to start from a clean state

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;
Expand All @@ -61,23 +59,23 @@ 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);
}

__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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
35 changes: 24 additions & 11 deletions quantum/process_keycode/process_unicode_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions quantum/process_keycode/process_unicodemap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down

0 comments on commit dd36eba

Please sign in to comment.