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 unicode_config_t unicode_config;
23 uint8_t unicode_saved_mods;
25 #if UNICODE_SELECTED_MODES != -1
26 static uint8_t selected[] = { UNICODE_SELECTED_MODES };
27 static uint8_t selected_count = sizeof selected / sizeof *selected;
28 static uint8_t selected_index;
31 void unicode_input_mode_init(void) {
32 unicode_config.raw = eeprom_read_byte(EECONFIG_UNICODEMODE);
33 #if UNICODE_SELECTED_MODES != -1
34 #if UNICODE_CYCLE_PERSIST
35 // Find input_mode in selected modes
37 for (i = 0; i < selected_count; i++) {
38 if (selected[i] == unicode_config.input_mode) {
43 if (i == selected_count) {
44 // Not found: input_mode isn't selected, change to one that is
45 unicode_config.input_mode = selected[selected_index = 0];
48 // Always change to the first selected input mode
49 unicode_config.input_mode = selected[selected_index = 0];
52 dprintf("Unicode input mode init to: %u\n", unicode_config.input_mode);
55 uint8_t get_unicode_input_mode(void) {
56 return unicode_config.input_mode;
59 void set_unicode_input_mode(uint8_t mode) {
60 unicode_config.input_mode = mode;
61 persist_unicode_input_mode();
62 dprintf("Unicode input mode set to: %u\n", unicode_config.input_mode);
65 void cycle_unicode_input_mode(uint8_t offset) {
66 #if UNICODE_SELECTED_MODES != -1
67 selected_index = (selected_index + offset) % selected_count;
68 unicode_config.input_mode = selected[selected_index];
69 #if UNICODE_CYCLE_PERSIST
70 persist_unicode_input_mode();
72 dprintf("Unicode input mode cycle to: %u\n", unicode_config.input_mode);
76 void persist_unicode_input_mode(void) {
77 eeprom_update_byte(EECONFIG_UNICODEMODE, unicode_config.input_mode);
81 void unicode_input_start(void) {
82 unicode_saved_mods = get_mods(); // Save current mods
83 clear_mods(); // Unregister mods to start from a clean state
85 switch (unicode_config.input_mode) {
87 register_code(UNICODE_KEY_OSX);
90 tap_code16(UNICODE_KEY_LNX);
93 register_code(KC_LALT);
97 tap_code(UNICODE_KEY_WINC);
102 wait_ms(UNICODE_TYPE_DELAY);
105 __attribute__((weak))
106 void unicode_input_finish(void) {
107 switch (unicode_config.input_mode) {
109 unregister_code(UNICODE_KEY_OSX);
115 unregister_code(KC_LALT);
122 set_mods(unicode_saved_mods); // Reregister previously set mods
125 __attribute__((weak))
126 void unicode_input_cancel(void) {
127 switch (unicode_config.input_mode) {
129 unregister_code(UNICODE_KEY_OSX);
136 unregister_code(KC_LALT);
140 set_mods(unicode_saved_mods); // Reregister previously set mods
143 __attribute__((weak))
144 uint16_t hex_to_keycode(uint8_t hex) {
147 } else if (hex < 0xA) {
148 return KC_1 + (hex - 0x1);
150 return KC_A + (hex - 0xA);
154 void register_hex(uint16_t hex) {
155 for(int i = 3; i >= 0; i--) {
156 uint8_t digit = ((hex >> (i*4)) & 0xF);
157 tap_code(hex_to_keycode(digit));
161 void send_unicode_hex_string(const char *str) {
162 if (!str) { return; }
165 // Find the next code point (token) in the string
166 for (; *str == ' '; str++);
167 size_t n = strcspn(str, " "); // Length of the current token
168 char code_point[n+1];
169 strncpy(code_point, str, n);
170 code_point[n] = '\0'; // Make sure it's null-terminated
172 // Normalize the code point: make all hex digits lowercase
173 for (char *p = code_point; *p; p++) {
174 *p = tolower((unsigned char)*p);
177 // Send the code point as a Unicode input string
178 unicode_input_start();
179 send_string(code_point);
180 unicode_input_finish();
182 str += n; // Move to the first ' ' (or '\0') after the current token
186 bool process_unicode_common(uint16_t keycode, keyrecord_t *record) {
187 if (record->event.pressed) {
189 case UNICODE_MODE_FORWARD:
190 cycle_unicode_input_mode(+1);
192 case UNICODE_MODE_REVERSE:
193 cycle_unicode_input_mode(-1);
196 case UNICODE_MODE_OSX:
197 set_unicode_input_mode(UC_OSX);
198 #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_OSX)
199 static float song_osx[][2] = UNICODE_SONG_OSX;
203 case UNICODE_MODE_LNX:
204 set_unicode_input_mode(UC_LNX);
205 #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_LNX)
206 static float song_lnx[][2] = UNICODE_SONG_LNX;
210 case UNICODE_MODE_WIN:
211 set_unicode_input_mode(UC_WIN);
212 #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_WIN)
213 static float song_win[][2] = UNICODE_SONG_WIN;
217 case UNICODE_MODE_BSD:
218 set_unicode_input_mode(UC_BSD);
219 #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_BSD)
220 static float song_bsd[][2] = UNICODE_SONG_BSD;
224 case UNICODE_MODE_WINC:
225 set_unicode_input_mode(UC_WINC);
226 #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_WINC)
227 static float song_winc[][2] = UNICODE_SONG_WINC;
228 PLAY_SONG(song_winc);
233 #if defined(UNICODE_ENABLE)
234 return process_unicode(keycode, record);
235 #elif defined(UNICODEMAP_ENABLE)
236 return process_unicodemap(keycode, record);
237 #elif defined(UCIS_ENABLE)
238 return process_ucis(keycode, record);