]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_unicode_common.c
Merge commit '60b30c036397cb5627fa374bb930794b225daa29' as 'lib/lufa'
[qmk_firmware.git] / quantum / process_keycode / process_unicode_common.c
1 /* Copyright 2017 Jack Humbert
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 #include "process_unicode_common.h"
18 #include "eeprom.h"
19
20 static uint8_t input_mode;
21 uint8_t mods;
22
23 void set_unicode_input_mode(uint8_t os_target)
24 {
25   input_mode = os_target;
26   eeprom_update_byte(EECONFIG_UNICODEMODE, os_target);
27 }
28
29 uint8_t get_unicode_input_mode(void) {
30   return input_mode;
31 }
32
33 __attribute__((weak))
34 void unicode_input_start (void) {
35   // save current mods
36   mods = keyboard_report->mods;
37
38   // unregister all mods to start from clean state
39   if (mods & MOD_BIT(KC_LSFT)) unregister_code(KC_LSFT);
40   if (mods & MOD_BIT(KC_RSFT)) unregister_code(KC_RSFT);
41   if (mods & MOD_BIT(KC_LCTL)) unregister_code(KC_LCTL);
42   if (mods & MOD_BIT(KC_RCTL)) unregister_code(KC_RCTL);
43   if (mods & MOD_BIT(KC_LALT)) unregister_code(KC_LALT);
44   if (mods & MOD_BIT(KC_RALT)) unregister_code(KC_RALT);
45   if (mods & MOD_BIT(KC_LGUI)) unregister_code(KC_LGUI);
46   if (mods & MOD_BIT(KC_RGUI)) unregister_code(KC_RGUI);
47
48   switch(input_mode) {
49   case UC_OSX:
50     register_code(KC_LALT);
51     break;
52   case UC_LNX:
53     register_code(KC_LCTL);
54     register_code(KC_LSFT);
55     register_code(KC_U);
56     unregister_code(KC_U);
57     unregister_code(KC_LSFT);
58     unregister_code(KC_LCTL);
59     break;
60   case UC_WIN:
61     register_code(KC_LALT);
62     register_code(KC_PPLS);
63     unregister_code(KC_PPLS);
64     break;
65   case UC_WINC:
66     register_code(KC_RALT);
67     unregister_code(KC_RALT);
68     register_code(KC_U);
69     unregister_code(KC_U);
70   }
71   wait_ms(UNICODE_TYPE_DELAY);
72 }
73
74 __attribute__((weak))
75 void unicode_input_finish (void) {
76   switch(input_mode) {
77     case UC_OSX:
78     case UC_WIN:
79       unregister_code(KC_LALT);
80       break;
81     case UC_LNX:
82       register_code(KC_SPC);
83       unregister_code(KC_SPC);
84       break;
85   }
86
87   // reregister previously set mods
88   if (mods & MOD_BIT(KC_LSFT)) register_code(KC_LSFT);
89   if (mods & MOD_BIT(KC_RSFT)) register_code(KC_RSFT);
90   if (mods & MOD_BIT(KC_LCTL)) register_code(KC_LCTL);
91   if (mods & MOD_BIT(KC_RCTL)) register_code(KC_RCTL);
92   if (mods & MOD_BIT(KC_LALT)) register_code(KC_LALT);
93   if (mods & MOD_BIT(KC_RALT)) register_code(KC_RALT);
94   if (mods & MOD_BIT(KC_LGUI)) register_code(KC_LGUI);
95   if (mods & MOD_BIT(KC_RGUI)) register_code(KC_RGUI);
96 }
97
98 __attribute__((weak))
99 uint16_t hex_to_keycode(uint8_t hex)
100 {
101   if (hex == 0x0) {
102     return KC_0;
103   } else if (hex < 0xA) {
104     return KC_1 + (hex - 0x1);
105   } else {
106     return KC_A + (hex - 0xA);
107   }
108 }
109
110 void register_hex(uint16_t hex) {
111   for(int i = 3; i >= 0; i--) {
112     uint8_t digit = ((hex >> (i*4)) & 0xF);
113     register_code(hex_to_keycode(digit));
114     unregister_code(hex_to_keycode(digit));
115   }
116 }