]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_unicode_common.c
Fix Unicode EEPROM handling so it is consistent. (#4066)
[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 #include <string.h>
20 #include <ctype.h>
21
22 static uint8_t input_mode;
23 uint8_t mods;
24
25 void set_unicode_input_mode(uint8_t os_target) {
26   input_mode = os_target;
27   eeprom_update_byte(EECONFIG_UNICODEMODE, os_target);
28 }
29
30 uint8_t get_unicode_input_mode(void) {
31   return input_mode;
32 }
33
34 void unicode_input_mode_init(void) {
35   static bool first_flag = false;
36   if (!first_flag) {
37     input_mode = eeprom_read_byte(EECONFIG_UNICODEMODE);
38     first_flag = true;
39   }
40 }
41
42 __attribute__((weak))
43 void unicode_input_start (void) {
44   // save current mods
45   mods = keyboard_report->mods;
46
47   // unregister all mods to start from clean state
48   if (mods & MOD_BIT(KC_LSFT)) unregister_code(KC_LSFT);
49   if (mods & MOD_BIT(KC_RSFT)) unregister_code(KC_RSFT);
50   if (mods & MOD_BIT(KC_LCTL)) unregister_code(KC_LCTL);
51   if (mods & MOD_BIT(KC_RCTL)) unregister_code(KC_RCTL);
52   if (mods & MOD_BIT(KC_LALT)) unregister_code(KC_LALT);
53   if (mods & MOD_BIT(KC_RALT)) unregister_code(KC_RALT);
54   if (mods & MOD_BIT(KC_LGUI)) unregister_code(KC_LGUI);
55   if (mods & MOD_BIT(KC_RGUI)) unregister_code(KC_RGUI);
56
57   switch(input_mode) {
58   case UC_OSX:
59     register_code(KC_LALT);
60     break;
61   case UC_OSX_RALT:
62     register_code(KC_RALT);
63     break;
64   case UC_LNX:
65     register_code(KC_LCTL);
66     register_code(KC_LSFT);
67     register_code(KC_U);
68     unregister_code(KC_U);
69     unregister_code(KC_LSFT);
70     unregister_code(KC_LCTL);
71     break;
72   case UC_WIN:
73     register_code(KC_LALT);
74     register_code(KC_PPLS);
75     unregister_code(KC_PPLS);
76     break;
77   case UC_WINC:
78     register_code(KC_RALT);
79     unregister_code(KC_RALT);
80     register_code(KC_U);
81     unregister_code(KC_U);
82   }
83   wait_ms(UNICODE_TYPE_DELAY);
84 }
85
86 __attribute__((weak))
87 void unicode_input_finish (void) {
88   switch(input_mode) {
89     case UC_OSX:
90     case UC_WIN:
91       unregister_code(KC_LALT);
92       break;
93     case UC_OSX_RALT:
94       unregister_code(KC_RALT);
95       break;
96     case UC_LNX:
97       register_code(KC_SPC);
98       unregister_code(KC_SPC);
99       break;
100   }
101
102   // reregister previously set mods
103   if (mods & MOD_BIT(KC_LSFT)) register_code(KC_LSFT);
104   if (mods & MOD_BIT(KC_RSFT)) register_code(KC_RSFT);
105   if (mods & MOD_BIT(KC_LCTL)) register_code(KC_LCTL);
106   if (mods & MOD_BIT(KC_RCTL)) register_code(KC_RCTL);
107   if (mods & MOD_BIT(KC_LALT)) register_code(KC_LALT);
108   if (mods & MOD_BIT(KC_RALT)) register_code(KC_RALT);
109   if (mods & MOD_BIT(KC_LGUI)) register_code(KC_LGUI);
110   if (mods & MOD_BIT(KC_RGUI)) register_code(KC_RGUI);
111 }
112
113 __attribute__((weak))
114 uint16_t hex_to_keycode(uint8_t hex) {
115   if (hex == 0x0) {
116     return KC_0;
117   } else if (hex < 0xA) {
118     return KC_1 + (hex - 0x1);
119   } else {
120     return KC_A + (hex - 0xA);
121   }
122 }
123
124 void register_hex(uint16_t hex) {
125   for(int i = 3; i >= 0; i--) {
126     uint8_t digit = ((hex >> (i*4)) & 0xF);
127     register_code(hex_to_keycode(digit));
128     unregister_code(hex_to_keycode(digit));
129   }
130 }
131
132 void send_unicode_hex_string(const char *str) {
133   if (!str) { return; } // Safety net
134
135   while (*str) {
136     // Find the next code point (token) in the string
137     for (; *str == ' '; str++);
138     size_t n = strcspn(str, " "); // Length of the current token
139     char code_point[n+1];
140     strncpy(code_point, str, n);
141     code_point[n] = '\0'; // Make sure it's null-terminated
142
143     // Normalize the code point: make all hex digits lowercase
144     for (char *p = code_point; *p; p++) {
145       *p = tolower((unsigned char)*p);
146     }
147
148     // Send the code point as a Unicode input string
149     unicode_input_start();
150     send_string(code_point);
151     unicode_input_finish();
152
153     str += n; // Move to the first ' ' (or '\0') after the current token
154   }
155 }