1 /* Copyright 2017 Jack Humbert
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.
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.
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/>.
17 #include "process_unicode_common.h"
22 static uint8_t input_mode;
25 void set_unicode_input_mode(uint8_t os_target)
27 input_mode = os_target;
28 eeprom_update_byte(EECONFIG_UNICODEMODE, os_target);
31 uint8_t get_unicode_input_mode(void) {
36 void unicode_input_start (void) {
38 mods = keyboard_report->mods;
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);
52 register_code(KC_LALT);
55 register_code(KC_RALT);
58 register_code(KC_LCTL);
59 register_code(KC_LSFT);
61 unregister_code(KC_U);
62 unregister_code(KC_LSFT);
63 unregister_code(KC_LCTL);
66 register_code(KC_LALT);
67 register_code(KC_PPLS);
68 unregister_code(KC_PPLS);
71 register_code(KC_RALT);
72 unregister_code(KC_RALT);
74 unregister_code(KC_U);
76 wait_ms(UNICODE_TYPE_DELAY);
80 void unicode_input_finish (void) {
84 unregister_code(KC_LALT);
87 unregister_code(KC_RALT);
90 register_code(KC_SPC);
91 unregister_code(KC_SPC);
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);
106 __attribute__((weak))
107 uint16_t hex_to_keycode(uint8_t hex)
111 } else if (hex < 0xA) {
112 return KC_1 + (hex - 0x1);
114 return KC_A + (hex - 0xA);
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));
126 void send_unicode_hex_string(const char *str)
128 if (!str) { return; } // Safety net
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
138 // Normalize the code point: make all hex digits lowercase
139 for (char *p = code_point; *p; p++) {
140 *p = tolower((unsigned char)*p);
143 // Send the code point as a Unicode input string
144 unicode_input_start();
145 send_string(code_point);
146 unicode_input_finish();
148 str += n; // Move to the first ' ' (or '\0') after the current token