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