]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_unicodemap.c
Fix UNICODE_MAP input_mode problem
[qmk_firmware.git] / quantum / process_keycode / process_unicodemap.c
1 #include "process_unicodemap.h"
2 #include "process_unicode_common.h"
3
4 __attribute__((weak))
5 const uint32_t PROGMEM unicode_map[] = {
6 };
7
8 void register_hex32(uint32_t hex) {
9   bool onzerostart = true;
10   for(int i = 7; i >= 0; i--) {
11     if (i <= 3) {
12       onzerostart = false;
13     }
14     uint8_t digit = ((hex >> (i*4)) & 0xF);
15     if (digit == 0) {
16       if (!onzerostart) {
17         register_code(hex_to_keycode(digit));
18         unregister_code(hex_to_keycode(digit));
19       }
20     } else {
21       register_code(hex_to_keycode(digit));
22       unregister_code(hex_to_keycode(digit));
23       onzerostart = false;
24     }
25   }
26 }
27
28 __attribute__((weak))
29 void unicode_map_input_error() {}
30
31 bool process_unicode_map(uint16_t keycode, keyrecord_t *record) {
32   uint8_t input_mode = get_unicode_input_mode();
33   if ((keycode & QK_UNICODE_MAP) == QK_UNICODE_MAP && record->event.pressed) {
34     const uint32_t* map = unicode_map;
35     uint16_t index = keycode - QK_UNICODE_MAP;
36     uint32_t code = pgm_read_dword_far(&map[index]);
37     if (code > 0xFFFF && code <= 0x10ffff && input_mode == UC_OSX) {
38       // Convert to UTF-16 surrogate pair
39       code -= 0x10000;
40       uint32_t lo = code & 0x3ff;
41       uint32_t hi = (code & 0xffc00) >> 10;
42       unicode_input_start();
43       register_hex32(hi + 0xd800);
44       register_hex32(lo + 0xdc00);
45       unicode_input_finish();
46     } else if ((code > 0x10ffff && input_mode == UC_OSX) || (code > 0xFFFFF && input_mode == UC_LNX)) {
47       // when character is out of range supported by the OS
48       unicode_map_input_error();
49     } else {
50       unicode_input_start();
51       register_hex32(code);
52       unicode_input_finish();
53     }
54   }
55   return true;
56 }