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