]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_unicode_common.c
Update KBD67 readme so that it mentions the KBD65 PCB (#5143)
[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 <ctype.h>
20 #include <string.h>
21
22 unicode_config_t unicode_config;
23 #if UNICODE_SELECTED_MODES != -1
24 static uint8_t selected[] = { UNICODE_SELECTED_MODES };
25 static uint8_t selected_count = sizeof selected / sizeof *selected;
26 static uint8_t selected_index;
27 #endif
28
29 void unicode_input_mode_init(void) {
30   unicode_config.raw = eeprom_read_byte(EECONFIG_UNICODEMODE);
31 #if UNICODE_SELECTED_MODES != -1
32   #if UNICODE_CYCLE_PERSIST
33   // Find input_mode in selected modes
34   uint8_t i;
35   for (i = 0; i < selected_count; i++) {
36     if (selected[i] == unicode_config.input_mode) {
37       selected_index = i;
38       break;
39     }
40   }
41   if (i == selected_count) {
42     // Not found: input_mode isn't selected, change to one that is
43     unicode_config.input_mode = selected[selected_index = 0];
44   }
45   #else
46   // Always change to the first selected input mode
47   unicode_config.input_mode = selected[selected_index = 0];
48   #endif
49 #endif
50   dprintf("Unicode input mode init to: %u\n", unicode_config.input_mode);
51 }
52
53 uint8_t get_unicode_input_mode(void) {
54   return unicode_config.input_mode;
55 }
56
57 void set_unicode_input_mode(uint8_t mode) {
58   unicode_config.input_mode = mode;
59   persist_unicode_input_mode();
60   dprintf("Unicode input mode set to: %u\n", unicode_config.input_mode);
61 }
62
63 void cycle_unicode_input_mode(uint8_t offset) {
64 #if UNICODE_SELECTED_MODES != -1
65   selected_index = (selected_index + offset) % selected_count;
66   unicode_config.input_mode = selected[selected_index];
67   #if UNICODE_CYCLE_PERSIST
68   persist_unicode_input_mode();
69   #endif
70   dprintf("Unicode input mode cycle to: %u\n", unicode_config.input_mode);
71 #endif
72 }
73
74 void persist_unicode_input_mode(void) {
75   eeprom_update_byte(EECONFIG_UNICODEMODE, unicode_config.input_mode);
76 }
77
78 static uint8_t saved_mods;
79
80 __attribute__((weak))
81 void unicode_input_start(void) {
82   saved_mods = get_mods(); // Save current mods
83   clear_mods(); // Unregister mods to start from a clean state
84
85   switch (unicode_config.input_mode) {
86   case UC_OSX:
87     register_code(UNICODE_OSX_KEY);
88     break;
89   case UC_LNX:
90     register_code(KC_LCTL);
91     register_code(KC_LSFT);
92     tap_code(KC_U); // TODO: Replace with tap_code16(LCTL(LSFT(KC_U))); and test
93     unregister_code(KC_LSFT);
94     unregister_code(KC_LCTL);
95     break;
96   case UC_WIN:
97     register_code(KC_LALT);
98     tap_code(KC_PPLS);
99     break;
100   case UC_WINC:
101     tap_code(UNICODE_WINC_KEY);
102     tap_code(KC_U);
103     break;
104   }
105
106   wait_ms(UNICODE_TYPE_DELAY);
107 }
108
109 __attribute__((weak))
110 void unicode_input_finish(void) {
111   switch (unicode_config.input_mode) {
112   case UC_OSX:
113     unregister_code(UNICODE_OSX_KEY);
114     break;
115   case UC_LNX:
116     tap_code(KC_SPC);
117     break;
118   case UC_WIN:
119     unregister_code(KC_LALT);
120     break;
121   case UC_WINC:
122     tap_code(KC_ENTER);
123     break;
124   }
125
126   set_mods(saved_mods); // Reregister previously set mods
127 }
128
129 __attribute__((weak))
130 uint16_t hex_to_keycode(uint8_t hex) {
131   if (hex == 0x0) {
132     return KC_0;
133   } else if (hex < 0xA) {
134     return KC_1 + (hex - 0x1);
135   } else {
136     return KC_A + (hex - 0xA);
137   }
138 }
139
140 void register_hex(uint16_t hex) {
141   for(int i = 3; i >= 0; i--) {
142     uint8_t digit = ((hex >> (i*4)) & 0xF);
143     tap_code(hex_to_keycode(digit));
144   }
145 }
146
147 void send_unicode_hex_string(const char *str) {
148   if (!str) { return; }
149
150   while (*str) {
151     // Find the next code point (token) in the string
152     for (; *str == ' '; str++);
153     size_t n = strcspn(str, " "); // Length of the current token
154     char code_point[n+1];
155     strncpy(code_point, str, n);
156     code_point[n] = '\0'; // Make sure it's null-terminated
157
158     // Normalize the code point: make all hex digits lowercase
159     for (char *p = code_point; *p; p++) {
160       *p = tolower((unsigned char)*p);
161     }
162
163     // Send the code point as a Unicode input string
164     unicode_input_start();
165     send_string(code_point);
166     unicode_input_finish();
167
168     str += n; // Move to the first ' ' (or '\0') after the current token
169   }
170 }
171
172 bool process_unicode_common(uint16_t keycode, keyrecord_t *record) {
173   if (record->event.pressed) {
174     switch (keycode) {
175     case UNICODE_MODE_FORWARD:
176       cycle_unicode_input_mode(+1);
177       break;
178     case UNICODE_MODE_REVERSE:
179       cycle_unicode_input_mode(-1);
180       break;
181
182     case UNICODE_MODE_OSX:
183       set_unicode_input_mode(UC_OSX);
184 #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_OSX)
185       static float song_osx[][2] = UNICODE_SONG_OSX;
186       PLAY_SONG(song_osx);
187 #endif
188       break;
189     case UNICODE_MODE_LNX:
190       set_unicode_input_mode(UC_LNX);
191 #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_LNX)
192       static float song_lnx[][2] = UNICODE_SONG_LNX;
193       PLAY_SONG(song_lnx);
194 #endif
195       break;
196     case UNICODE_MODE_WIN:
197       set_unicode_input_mode(UC_WIN);
198 #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_WIN)
199       static float song_win[][2] = UNICODE_SONG_WIN;
200       PLAY_SONG(song_win);
201 #endif
202       break;
203     case UNICODE_MODE_BSD:
204       set_unicode_input_mode(UC_BSD);
205 #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_BSD)
206       static float song_bsd[][2] = UNICODE_SONG_BSD;
207       PLAY_SONG(song_bsd);
208 #endif
209       break;
210     case UNICODE_MODE_WINC:
211       set_unicode_input_mode(UC_WINC);
212 #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_WINC)
213       static float song_winc[][2] = UNICODE_SONG_WINC;
214       PLAY_SONG(song_winc);
215 #endif
216       break;
217     }
218   }
219 #if   defined(UNICODE_ENABLE)
220   return process_unicode(keycode, record);
221 #elif defined(UNICODEMAP_ENABLE)
222   return process_unicodemap(keycode, record);
223 #elif defined(UCIS_ENABLE)
224   return process_ucis(keycode, record);
225 #else
226   return true;
227 #endif
228 }