]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_unicode.c
process_unicode: Small refactor & linux fix
[qmk_firmware.git] / quantum / process_keycode / process_unicode.c
1 #include "process_unicode.h"
2
3 static uint8_t input_mode;
4
5 uint16_t hex_to_keycode(uint8_t hex)
6 {
7   if (hex == 0x0) {
8     return KC_0;
9   } else if (hex < 0xA) {
10     return KC_1 + (hex - 0x1);
11   } else {
12     return KC_A + (hex - 0xA);
13   }
14 }
15
16 void set_unicode_input_mode(uint8_t os_target)
17 {
18   input_mode = os_target;
19 }
20
21 void unicode_input_start (void) {
22   switch(input_mode) {
23   case UC_OSX:
24     register_code(KC_LALT);
25     break;
26   case UC_LNX:
27     register_code(KC_LCTL);
28     register_code(KC_LSFT);
29     register_code(KC_U);
30     unregister_code(KC_U);
31     unregister_code(KC_LSFT);
32     unregister_code(KC_LCTL);
33     break;
34   case UC_WIN:
35     register_code(KC_LALT);
36     register_code(KC_PPLS);
37     unregister_code(KC_PPLS);
38     break;
39   }
40 }
41
42 void unicode_input_finish (void) {
43   switch(input_mode) {
44   case UC_OSX:
45   case UC_WIN:
46     unregister_code(KC_LALT);
47     break;
48   case UC_LNX:
49     register_code(KC_SPC);
50     unregister_code(KC_SPC);
51     break;
52   }
53 }
54
55 void register_hex(uint16_t hex) {
56   for(int i = 3; i >= 0; i--) {
57     uint8_t digit = ((hex >> (i*4)) & 0xF);
58     register_code(hex_to_keycode(digit));
59     unregister_code(hex_to_keycode(digit));
60   }
61 }
62
63 bool process_unicode(uint16_t keycode, keyrecord_t *record) {
64   if (keycode > QK_UNICODE && record->event.pressed) {
65     uint16_t unicode = keycode & 0x7FFF;
66     unicode_input_start();
67     register_hex(unicode);
68     unicode_input_finish();
69   }
70   return true;
71 }