Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send_unicode_string(): Add support for code points > 0xFFFF #8236

Merged
merged 1 commit into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/feature_unicode.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ By default, when the keyboard boots, it will initialize the input mode to the la

## `send_unicode_string()`

This function is much like `send_string()` but allows you to input UTF-8 characters directly, currently up to code point U+FFFF. Make sure your `keymap.c` is formatted in UTF-8 encoding.
This function is much like `send_string()` but allows you to input UTF-8 characters directly, and supports all code points (provided the selected input method also supports it). Make sure your `keymap.c` is formatted in UTF-8 encoding.

```c
send_unicode_string("(ノಠ痊ಠ)ノ彡┻━┻");
Expand All @@ -210,7 +210,6 @@ send_unicode_hex_string("0028 30CE 0CA0 75CA 0CA0 0029 30CE 5F61 253B 2501 253B"
```

An easy way to convert your Unicode string to this format is by using [this site](https://r12a.github.io/app-conversion/), and taking the result in the "Hex/UTF-32" section.
Unlike `send_unicode_string()` this function supports code points up to U+10FFFF.

## Additional Language Support

Expand Down
24 changes: 20 additions & 4 deletions quantum/process_keycode/process_unicode_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,24 @@ void register_hex(uint16_t hex) {
}
}

void register_hex32(uint32_t hex) {
bool onzerostart = true;
for (int i = 7; i >= 0; i--) {
if (i <= 3) {
onzerostart = false;
}
uint8_t digit = ((hex >> (i * 4)) & 0xF);
if (digit == 0) {
if (!onzerostart) {
tap_code(hex_to_keycode(digit));
}
} else {
tap_code(hex_to_keycode(digit));
onzerostart = false;
}
}
}

void send_unicode_hex_string(const char *str) {
if (!str) {
return;
Expand Down Expand Up @@ -192,9 +210,7 @@ const char *decode_utf8(const char *str, int32_t *code_point) {
*code_point = ((int32_t)(str[0] & 0x0F) << 12) | ((int32_t)(str[1] & 0x3F) << 6) | ((int32_t)(str[2] & 0x3F) << 0);
next = str + 3;
} else if ((str[0] & 0xF8) == 0xF0 && (str[0] <= 0xF4)) { // U+10000-10FFFF
// Skip for now - register_hex() only takes a uint16
//*code_point = ((int32_t)(str[0] & 0x07) << 18) | ((int32_t)(str[1] & 0x3F) << 12) | ((int32_t)(str[2] & 0x3F) << 6) | ((int32_t)(str[3] & 0x3F) << 0);
*code_point = -1;
*code_point = ((int32_t)(str[0] & 0x07) << 18) | ((int32_t)(str[1] & 0x3F) << 12) | ((int32_t)(str[2] & 0x3F) << 6) | ((int32_t)(str[3] & 0x3F) << 0);
next = str + 4;
} else {
*code_point = -1;
Expand All @@ -221,7 +237,7 @@ void send_unicode_string(const char *str) {

if (code_point >= 0) {
unicode_input_start();
register_hex(code_point);
register_hex32(code_point);
unicode_input_finish();
}
}
Expand Down
1 change: 1 addition & 0 deletions quantum/process_keycode/process_unicode_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void unicode_input_finish(void);
void unicode_input_cancel(void);

void register_hex(uint16_t hex);
void register_hex32(uint32_t hex);
void send_unicode_hex_string(const char *str);
void send_unicode_string(const char *str);

Expand Down
20 changes: 0 additions & 20 deletions quantum/process_keycode/process_unicodemap.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,6 @@

#include "process_unicodemap.h"

void register_hex32(uint32_t hex) {
bool onzerostart = true;
for (int i = 7; i >= 0; i--) {
if (i <= 3) {
onzerostart = false;
}
uint8_t digit = ((hex >> (i * 4)) & 0xF);
if (digit == 0) {
if (!onzerostart) {
register_code(hex_to_keycode(digit));
unregister_code(hex_to_keycode(digit));
}
} else {
register_code(hex_to_keycode(digit));
unregister_code(hex_to_keycode(digit));
onzerostart = false;
}
}
}

__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
Expand Down
1 change: 0 additions & 1 deletion quantum/process_keycode/process_unicodemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@

extern const uint32_t PROGMEM unicode_map[];

void register_hex32(uint32_t hex);
uint16_t unicodemap_index(uint16_t keycode);
bool process_unicodemap(uint16_t keycode, keyrecord_t *record);