diff options
Diffstat (limited to 'quantum/process_keycode/process_unicode.c')
| -rw-r--r-- | quantum/process_keycode/process_unicode.c | 295 |
1 files changed, 20 insertions, 275 deletions
diff --git a/quantum/process_keycode/process_unicode.c b/quantum/process_keycode/process_unicode.c index a30e93ae3..678a15234 100644 --- a/quantum/process_keycode/process_unicode.c +++ b/quantum/process_keycode/process_unicode.c | |||
| @@ -1,105 +1,29 @@ | |||
| 1 | /* Copyright 2016 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 | */ | ||
| 1 | #include "process_unicode.h" | 16 | #include "process_unicode.h" |
| 2 | #include "action_util.h" | 17 | #include "action_util.h" |
| 3 | 18 | ||
| 4 | static uint8_t input_mode; | 19 | static uint8_t first_flag = 0; |
| 5 | uint8_t mods; | ||
| 6 | |||
| 7 | __attribute__((weak)) | ||
| 8 | uint16_t hex_to_keycode(uint8_t hex) | ||
| 9 | { | ||
| 10 | if (hex == 0x0) { | ||
| 11 | return KC_0; | ||
| 12 | } else if (hex < 0xA) { | ||
| 13 | return KC_1 + (hex - 0x1); | ||
| 14 | } else { | ||
| 15 | return KC_A + (hex - 0xA); | ||
| 16 | } | ||
| 17 | } | ||
| 18 | |||
| 19 | void set_unicode_input_mode(uint8_t os_target) | ||
| 20 | { | ||
| 21 | input_mode = os_target; | ||
| 22 | } | ||
| 23 | |||
| 24 | uint8_t get_unicode_input_mode(void) { | ||
| 25 | return input_mode; | ||
| 26 | } | ||
| 27 | |||
| 28 | __attribute__((weak)) | ||
| 29 | void unicode_input_start (void) { | ||
| 30 | // save current mods | ||
| 31 | mods = keyboard_report->mods; | ||
| 32 | |||
| 33 | // unregister all mods to start from clean state | ||
| 34 | if (mods & MOD_BIT(KC_LSFT)) unregister_code(KC_LSFT); | ||
| 35 | if (mods & MOD_BIT(KC_RSFT)) unregister_code(KC_RSFT); | ||
| 36 | if (mods & MOD_BIT(KC_LCTL)) unregister_code(KC_LCTL); | ||
| 37 | if (mods & MOD_BIT(KC_RCTL)) unregister_code(KC_RCTL); | ||
| 38 | if (mods & MOD_BIT(KC_LALT)) unregister_code(KC_LALT); | ||
| 39 | if (mods & MOD_BIT(KC_RALT)) unregister_code(KC_RALT); | ||
| 40 | if (mods & MOD_BIT(KC_LGUI)) unregister_code(KC_LGUI); | ||
| 41 | if (mods & MOD_BIT(KC_RGUI)) unregister_code(KC_RGUI); | ||
| 42 | |||
| 43 | switch(input_mode) { | ||
| 44 | case UC_OSX: | ||
| 45 | register_code(KC_LALT); | ||
| 46 | break; | ||
| 47 | case UC_LNX: | ||
| 48 | register_code(KC_LCTL); | ||
| 49 | register_code(KC_LSFT); | ||
| 50 | register_code(KC_U); | ||
| 51 | unregister_code(KC_U); | ||
| 52 | unregister_code(KC_LSFT); | ||
| 53 | unregister_code(KC_LCTL); | ||
| 54 | break; | ||
| 55 | case UC_WIN: | ||
| 56 | register_code(KC_LALT); | ||
| 57 | register_code(KC_PPLS); | ||
| 58 | unregister_code(KC_PPLS); | ||
| 59 | break; | ||
| 60 | case UC_WINC: | ||
| 61 | register_code(KC_RALT); | ||
| 62 | unregister_code(KC_RALT); | ||
| 63 | register_code(KC_U); | ||
| 64 | unregister_code(KC_U); | ||
| 65 | } | ||
| 66 | wait_ms(UNICODE_TYPE_DELAY); | ||
| 67 | } | ||
| 68 | |||
| 69 | __attribute__((weak)) | ||
| 70 | void unicode_input_finish (void) { | ||
| 71 | switch(input_mode) { | ||
| 72 | case UC_OSX: | ||
| 73 | case UC_WIN: | ||
| 74 | unregister_code(KC_LALT); | ||
| 75 | break; | ||
| 76 | case UC_LNX: | ||
| 77 | register_code(KC_SPC); | ||
| 78 | unregister_code(KC_SPC); | ||
| 79 | break; | ||
| 80 | } | ||
| 81 | |||
| 82 | // reregister previously set mods | ||
| 83 | if (mods & MOD_BIT(KC_LSFT)) register_code(KC_LSFT); | ||
| 84 | if (mods & MOD_BIT(KC_RSFT)) register_code(KC_RSFT); | ||
| 85 | if (mods & MOD_BIT(KC_LCTL)) register_code(KC_LCTL); | ||
| 86 | if (mods & MOD_BIT(KC_RCTL)) register_code(KC_RCTL); | ||
| 87 | if (mods & MOD_BIT(KC_LALT)) register_code(KC_LALT); | ||
| 88 | if (mods & MOD_BIT(KC_RALT)) register_code(KC_RALT); | ||
| 89 | if (mods & MOD_BIT(KC_LGUI)) register_code(KC_LGUI); | ||
| 90 | if (mods & MOD_BIT(KC_RGUI)) register_code(KC_RGUI); | ||
| 91 | } | ||
| 92 | |||
| 93 | void register_hex(uint16_t hex) { | ||
| 94 | for(int i = 3; i >= 0; i--) { | ||
| 95 | uint8_t digit = ((hex >> (i*4)) & 0xF); | ||
| 96 | register_code(hex_to_keycode(digit)); | ||
| 97 | unregister_code(hex_to_keycode(digit)); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | 20 | ||
| 101 | bool process_unicode(uint16_t keycode, keyrecord_t *record) { | 21 | bool process_unicode(uint16_t keycode, keyrecord_t *record) { |
| 102 | if (keycode > QK_UNICODE && record->event.pressed) { | 22 | if (keycode > QK_UNICODE && record->event.pressed) { |
| 23 | if (first_flag == 0) { | ||
| 24 | set_unicode_input_mode(eeprom_read_byte(EECONFIG_UNICODEMODE)); | ||
| 25 | first_flag = 1; | ||
| 26 | } | ||
| 103 | uint16_t unicode = keycode & 0x7FFF; | 27 | uint16_t unicode = keycode & 0x7FFF; |
| 104 | unicode_input_start(); | 28 | unicode_input_start(); |
| 105 | register_hex(unicode); | 29 | register_hex(unicode); |
| @@ -108,182 +32,3 @@ bool process_unicode(uint16_t keycode, keyrecord_t *record) { | |||
| 108 | return true; | 32 | return true; |
| 109 | } | 33 | } |
| 110 | 34 | ||
| 111 | #ifdef UNICODEMAP_ENABLE | ||
| 112 | __attribute__((weak)) | ||
| 113 | const uint32_t PROGMEM unicode_map[] = { | ||
| 114 | }; | ||
| 115 | |||
| 116 | void register_hex32(uint32_t hex) { | ||
| 117 | uint8_t onzerostart = 1; | ||
| 118 | for(int i = 7; i >= 0; i--) { | ||
| 119 | if (i <= 3) { | ||
| 120 | onzerostart = 0; | ||
| 121 | } | ||
| 122 | uint8_t digit = ((hex >> (i*4)) & 0xF); | ||
| 123 | if (digit == 0) { | ||
| 124 | if (onzerostart == 0) { | ||
| 125 | register_code(hex_to_keycode(digit)); | ||
| 126 | unregister_code(hex_to_keycode(digit)); | ||
| 127 | } | ||
| 128 | } else { | ||
| 129 | register_code(hex_to_keycode(digit)); | ||
| 130 | unregister_code(hex_to_keycode(digit)); | ||
| 131 | onzerostart = 0; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | __attribute__((weak)) | ||
| 137 | void unicode_map_input_error() {} | ||
| 138 | |||
| 139 | bool process_unicode_map(uint16_t keycode, keyrecord_t *record) { | ||
| 140 | if ((keycode & QK_UNICODE_MAP) == QK_UNICODE_MAP && record->event.pressed) { | ||
| 141 | const uint32_t* map = unicode_map; | ||
| 142 | uint16_t index = keycode & 0x7FF; | ||
| 143 | uint32_t code = pgm_read_dword_far(&map[index]); | ||
| 144 | if ((code > 0xFFFF && input_mode == UC_OSX) || (code > 0xFFFFF && input_mode == UC_LNX)) { | ||
| 145 | // when character is out of range supported by the OS | ||
| 146 | unicode_map_input_error(); | ||
| 147 | } else { | ||
| 148 | unicode_input_start(); | ||
| 149 | register_hex32(code); | ||
| 150 | unicode_input_finish(); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | return true; | ||
| 154 | } | ||
| 155 | #endif | ||
| 156 | |||
| 157 | #ifdef UCIS_ENABLE | ||
| 158 | qk_ucis_state_t qk_ucis_state; | ||
| 159 | |||
| 160 | void qk_ucis_start(void) { | ||
| 161 | qk_ucis_state.count = 0; | ||
| 162 | qk_ucis_state.in_progress = true; | ||
| 163 | |||
| 164 | qk_ucis_start_user(); | ||
| 165 | } | ||
| 166 | |||
| 167 | __attribute__((weak)) | ||
| 168 | void qk_ucis_start_user(void) { | ||
| 169 | unicode_input_start(); | ||
| 170 | register_hex(0x2328); | ||
| 171 | unicode_input_finish(); | ||
| 172 | } | ||
| 173 | |||
| 174 | static bool is_uni_seq(char *seq) { | ||
| 175 | uint8_t i; | ||
| 176 | |||
| 177 | for (i = 0; seq[i]; i++) { | ||
| 178 | uint16_t code; | ||
| 179 | if (('1' <= seq[i]) && (seq[i] <= '0')) | ||
| 180 | code = seq[i] - '1' + KC_1; | ||
| 181 | else | ||
| 182 | code = seq[i] - 'a' + KC_A; | ||
| 183 | |||
| 184 | if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code) | ||
| 185 | return false; | ||
| 186 | } | ||
| 187 | |||
| 188 | return (qk_ucis_state.codes[i] == KC_ENT || | ||
| 189 | qk_ucis_state.codes[i] == KC_SPC); | ||
| 190 | } | ||
| 191 | |||
| 192 | __attribute__((weak)) | ||
| 193 | void qk_ucis_symbol_fallback (void) { | ||
| 194 | for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) { | ||
| 195 | uint8_t code = qk_ucis_state.codes[i]; | ||
| 196 | register_code(code); | ||
| 197 | unregister_code(code); | ||
| 198 | wait_ms(UNICODE_TYPE_DELAY); | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | void register_ucis(const char *hex) { | ||
| 203 | for(int i = 0; hex[i]; i++) { | ||
| 204 | uint8_t kc = 0; | ||
| 205 | char c = hex[i]; | ||
| 206 | |||
| 207 | switch (c) { | ||
| 208 | case '0': | ||
| 209 | kc = KC_0; | ||
| 210 | break; | ||
| 211 | case '1' ... '9': | ||
| 212 | kc = c - '1' + KC_1; | ||
| 213 | break; | ||
| 214 | case 'a' ... 'f': | ||
| 215 | kc = c - 'a' + KC_A; | ||
| 216 | break; | ||
| 217 | case 'A' ... 'F': | ||
| 218 | kc = c - 'A' + KC_A; | ||
| 219 | break; | ||
| 220 | } | ||
| 221 | |||
| 222 | if (kc) { | ||
| 223 | register_code (kc); | ||
| 224 | unregister_code (kc); | ||
| 225 | wait_ms (UNICODE_TYPE_DELAY); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | bool process_ucis (uint16_t keycode, keyrecord_t *record) { | ||
| 231 | uint8_t i; | ||
| 232 | |||
| 233 | if (!qk_ucis_state.in_progress) | ||
| 234 | return true; | ||
| 235 | |||
| 236 | if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH && | ||
| 237 | !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) { | ||
| 238 | return false; | ||
| 239 | } | ||
| 240 | |||
| 241 | if (!record->event.pressed) | ||
| 242 | return true; | ||
| 243 | |||
| 244 | qk_ucis_state.codes[qk_ucis_state.count] = keycode; | ||
| 245 | qk_ucis_state.count++; | ||
| 246 | |||
| 247 | if (keycode == KC_BSPC) { | ||
| 248 | if (qk_ucis_state.count >= 2) { | ||
| 249 | qk_ucis_state.count -= 2; | ||
| 250 | return true; | ||
| 251 | } else { | ||
| 252 | qk_ucis_state.count--; | ||
| 253 | return false; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) { | ||
| 258 | bool symbol_found = false; | ||
| 259 | |||
| 260 | for (i = qk_ucis_state.count; i > 0; i--) { | ||
| 261 | register_code (KC_BSPC); | ||
| 262 | unregister_code (KC_BSPC); | ||
| 263 | wait_ms(UNICODE_TYPE_DELAY); | ||
| 264 | } | ||
| 265 | |||
| 266 | if (keycode == KC_ESC) { | ||
| 267 | qk_ucis_state.in_progress = false; | ||
| 268 | return false; | ||
| 269 | } | ||
| 270 | |||
| 271 | unicode_input_start(); | ||
| 272 | for (i = 0; ucis_symbol_table[i].symbol; i++) { | ||
| 273 | if (is_uni_seq (ucis_symbol_table[i].symbol)) { | ||
| 274 | symbol_found = true; | ||
| 275 | register_ucis(ucis_symbol_table[i].code + 2); | ||
| 276 | break; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | if (!symbol_found) { | ||
| 280 | qk_ucis_symbol_fallback(); | ||
| 281 | } | ||
| 282 | unicode_input_finish(); | ||
| 283 | |||
| 284 | qk_ucis_state.in_progress = false; | ||
| 285 | return false; | ||
| 286 | } | ||
| 287 | return true; | ||
| 288 | } | ||
| 289 | #endif | ||
