]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_unicode.c
split up unicode systems into different files
[qmk_firmware.git] / quantum / process_keycode / process_unicode.c
1 #include "process_unicode.h"
2 #include "action_util.h"
3
4 static uint8_t input_mode;
5 uint8_t mods;
6
7 void set_unicode_input_mode(uint8_t os_target)
8 {
9   input_mode = os_target;
10 }
11
12 uint8_t get_unicode_input_mode(void) {
13   return input_mode;
14 }
15
16 __attribute__((weak))
17 void unicode_input_start (void) {
18   // save current mods
19   mods = keyboard_report->mods;
20
21   // unregister all mods to start from clean state
22   if (mods & MOD_BIT(KC_LSFT)) unregister_code(KC_LSFT);
23   if (mods & MOD_BIT(KC_RSFT)) unregister_code(KC_RSFT);
24   if (mods & MOD_BIT(KC_LCTL)) unregister_code(KC_LCTL);
25   if (mods & MOD_BIT(KC_RCTL)) unregister_code(KC_RCTL);
26   if (mods & MOD_BIT(KC_LALT)) unregister_code(KC_LALT);
27   if (mods & MOD_BIT(KC_RALT)) unregister_code(KC_RALT);
28   if (mods & MOD_BIT(KC_LGUI)) unregister_code(KC_LGUI);
29   if (mods & MOD_BIT(KC_RGUI)) unregister_code(KC_RGUI);
30
31   switch(input_mode) {
32   case UC_OSX:
33     register_code(KC_LALT);
34     break;
35   case UC_LNX:
36     register_code(KC_LCTL);
37     register_code(KC_LSFT);
38     register_code(KC_U);
39     unregister_code(KC_U);
40     unregister_code(KC_LSFT);
41     unregister_code(KC_LCTL);
42     break;
43   case UC_WIN:
44     register_code(KC_LALT);
45     register_code(KC_PPLS);
46     unregister_code(KC_PPLS);
47     break;
48   case UC_WINC:
49     register_code(KC_RALT);
50     unregister_code(KC_RALT);
51     register_code(KC_U);
52     unregister_code(KC_U);
53   }
54   wait_ms(UNICODE_TYPE_DELAY);
55 }
56
57 __attribute__((weak))
58 void unicode_input_finish (void) {
59   switch(input_mode) {
60     case UC_OSX:
61     case UC_WIN:
62       unregister_code(KC_LALT);
63       break;
64     case UC_LNX:
65       register_code(KC_SPC);
66       unregister_code(KC_SPC);
67       break;
68   }
69
70   // reregister previously set mods
71   if (mods & MOD_BIT(KC_LSFT)) register_code(KC_LSFT);
72   if (mods & MOD_BIT(KC_RSFT)) register_code(KC_RSFT);
73   if (mods & MOD_BIT(KC_LCTL)) register_code(KC_LCTL);
74   if (mods & MOD_BIT(KC_RCTL)) register_code(KC_RCTL);
75   if (mods & MOD_BIT(KC_LALT)) register_code(KC_LALT);
76   if (mods & MOD_BIT(KC_RALT)) register_code(KC_RALT);
77   if (mods & MOD_BIT(KC_LGUI)) register_code(KC_LGUI);
78   if (mods & MOD_BIT(KC_RGUI)) register_code(KC_RGUI);
79 }
80
81 void register_hex(uint16_t hex) {
82   for(int i = 3; i >= 0; i--) {
83     uint8_t digit = ((hex >> (i*4)) & 0xF);
84     register_code(hex_to_keycode(digit));
85     unregister_code(hex_to_keycode(digit));
86   }
87 }
88
89 bool process_unicode(uint16_t keycode, keyrecord_t *record) {
90   if (keycode > QK_UNICODE && record->event.pressed) {
91     uint16_t unicode = keycode & 0x7FFF;
92     unicode_input_start();
93     register_hex(unicode);
94     unicode_input_finish();
95   }
96   return true;
97 }
98