process_unicode_common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #include "process_unicode_common.h"
  17. #include "eeprom.h"
  18. #include <ctype.h>
  19. #include <string.h>
  20. unicode_config_t unicode_config;
  21. uint8_t unicode_saved_mods;
  22. bool unicode_saved_caps_lock;
  23. bool unicode_saved_num_lock;
  24. #if UNICODE_SELECTED_MODES != -1
  25. static uint8_t selected[] = {UNICODE_SELECTED_MODES};
  26. static int8_t selected_count = sizeof selected / sizeof *selected;
  27. static int8_t selected_index;
  28. #endif
  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. int8_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. uint8_t get_unicode_input_mode(void) {
  53. return unicode_config.input_mode;
  54. }
  55. void set_unicode_input_mode(uint8_t mode) {
  56. unicode_config.input_mode = mode;
  57. persist_unicode_input_mode();
  58. dprintf("Unicode input mode set to: %u\n", unicode_config.input_mode);
  59. }
  60. void cycle_unicode_input_mode(int8_t offset) {
  61. #if UNICODE_SELECTED_MODES != -1
  62. selected_index = (selected_index + offset) % selected_count;
  63. if (selected_index < 0) {
  64. selected_index += selected_count;
  65. }
  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. void persist_unicode_input_mode(void) {
  74. eeprom_update_byte(EECONFIG_UNICODEMODE, unicode_config.input_mode);
  75. }
  76. __attribute__((weak)) void unicode_input_start(void) {
  77. unicode_saved_caps_lock = host_keyboard_led_state().caps_lock;
  78. unicode_saved_num_lock = host_keyboard_led_state().num_lock;
  79. // Note the order matters here!
  80. // Need to do this before we mess around with the mods, or else
  81. // UNICODE_KEY_LNX (which is usually Ctrl-Shift-U) might not work
  82. // correctly in the shifted case.
  83. if (unicode_config.input_mode == UC_LNX && unicode_saved_caps_lock) {
  84. tap_code(KC_CAPS_LOCK);
  85. }
  86. unicode_saved_mods = get_mods(); // Save current mods
  87. clear_mods(); // Unregister mods to start from a clean state
  88. switch (unicode_config.input_mode) {
  89. case UC_MAC:
  90. register_code(UNICODE_KEY_MAC);
  91. break;
  92. case UC_LNX:
  93. tap_code16(UNICODE_KEY_LNX);
  94. break;
  95. case UC_WIN:
  96. // For increased reliability, use numpad keys for inputting digits
  97. if (!unicode_saved_num_lock) {
  98. tap_code(KC_NUM_LOCK);
  99. }
  100. register_code(KC_LEFT_ALT);
  101. wait_ms(UNICODE_TYPE_DELAY);
  102. tap_code(KC_KP_PLUS);
  103. break;
  104. case UC_WINC:
  105. tap_code(UNICODE_KEY_WINC);
  106. tap_code(KC_U);
  107. break;
  108. }
  109. wait_ms(UNICODE_TYPE_DELAY);
  110. }
  111. __attribute__((weak)) void unicode_input_finish(void) {
  112. switch (unicode_config.input_mode) {
  113. case UC_MAC:
  114. unregister_code(UNICODE_KEY_MAC);
  115. break;
  116. case UC_LNX:
  117. tap_code(KC_SPACE);
  118. if (unicode_saved_caps_lock) {
  119. tap_code(KC_CAPS_LOCK);
  120. }
  121. break;
  122. case UC_WIN:
  123. unregister_code(KC_LEFT_ALT);
  124. if (!unicode_saved_num_lock) {
  125. tap_code(KC_NUM_LOCK);
  126. }
  127. break;
  128. case UC_WINC:
  129. tap_code(KC_ENTER);
  130. break;
  131. }
  132. set_mods(unicode_saved_mods); // Reregister previously set mods
  133. }
  134. __attribute__((weak)) void unicode_input_cancel(void) {
  135. switch (unicode_config.input_mode) {
  136. case UC_MAC:
  137. unregister_code(UNICODE_KEY_MAC);
  138. break;
  139. case UC_LNX:
  140. tap_code(KC_ESCAPE);
  141. if (unicode_saved_caps_lock) {
  142. tap_code(KC_CAPS_LOCK);
  143. }
  144. break;
  145. case UC_WINC:
  146. tap_code(KC_ESCAPE);
  147. break;
  148. case UC_WIN:
  149. unregister_code(KC_LEFT_ALT);
  150. if (!unicode_saved_num_lock) {
  151. tap_code(KC_NUM_LOCK);
  152. }
  153. break;
  154. }
  155. set_mods(unicode_saved_mods); // Reregister previously set mods
  156. }
  157. // clang-format off
  158. static void send_nibble_wrapper(uint8_t digit) {
  159. if (unicode_config.input_mode == UC_WIN) {
  160. uint8_t kc = digit < 10
  161. ? KC_KP_1 + (10 + digit - 1) % 10
  162. : KC_A + (digit - 10);
  163. tap_code(kc);
  164. return;
  165. }
  166. send_nibble(digit);
  167. }
  168. // clang-format on
  169. void register_hex(uint16_t hex) {
  170. for (int i = 3; i >= 0; i--) {
  171. uint8_t digit = ((hex >> (i * 4)) & 0xF);
  172. send_nibble_wrapper(digit);
  173. }
  174. }
  175. void register_hex32(uint32_t hex) {
  176. bool onzerostart = true;
  177. for (int i = 7; i >= 0; i--) {
  178. if (i <= 3) {
  179. onzerostart = false;
  180. }
  181. uint8_t digit = ((hex >> (i * 4)) & 0xF);
  182. if (digit == 0) {
  183. if (!onzerostart) {
  184. send_nibble_wrapper(digit);
  185. }
  186. } else {
  187. send_nibble_wrapper(digit);
  188. onzerostart = false;
  189. }
  190. }
  191. }
  192. void register_unicode(uint32_t code_point) {
  193. if (code_point > 0x10FFFF || (code_point > 0xFFFF && unicode_config.input_mode == UC_WIN)) {
  194. // Code point out of range, do nothing
  195. return;
  196. }
  197. unicode_input_start();
  198. if (code_point > 0xFFFF && unicode_config.input_mode == UC_MAC) {
  199. // Convert code point to UTF-16 surrogate pair on macOS
  200. code_point -= 0x10000;
  201. uint32_t lo = code_point & 0x3FF, hi = (code_point & 0xFFC00) >> 10;
  202. register_hex32(hi + 0xD800);
  203. register_hex32(lo + 0xDC00);
  204. } else {
  205. register_hex32(code_point);
  206. }
  207. unicode_input_finish();
  208. }
  209. // clang-format off
  210. void send_unicode_hex_string(const char *str) {
  211. if (!str) {
  212. return;
  213. }
  214. while (*str) {
  215. // Find the next code point (token) in the string
  216. for (; *str == ' '; str++); // Skip leading spaces
  217. size_t n = strcspn(str, " "); // Length of the current token
  218. char code_point[n+1];
  219. strncpy(code_point, str, n); // Copy token into buffer
  220. code_point[n] = '\0'; // Make sure it's null-terminated
  221. // Normalize the code point: make all hex digits lowercase
  222. for (char *p = code_point; *p; p++) {
  223. *p = tolower((unsigned char)*p);
  224. }
  225. // Send the code point as a Unicode input string
  226. unicode_input_start();
  227. send_string(code_point);
  228. unicode_input_finish();
  229. str += n; // Move to the first ' ' (or '\0') after the current token
  230. }
  231. }
  232. // clang-format on
  233. // Borrowed from https://nullprogram.com/blog/2017/10/06/
  234. static const char *decode_utf8(const char *str, int32_t *code_point) {
  235. const char *next;
  236. if (str[0] < 0x80) { // U+0000-007F
  237. *code_point = str[0];
  238. next = str + 1;
  239. } else if ((str[0] & 0xE0) == 0xC0) { // U+0080-07FF
  240. *code_point = ((int32_t)(str[0] & 0x1F) << 6) | ((int32_t)(str[1] & 0x3F) << 0);
  241. next = str + 2;
  242. } else if ((str[0] & 0xF0) == 0xE0) { // U+0800-FFFF
  243. *code_point = ((int32_t)(str[0] & 0x0F) << 12) | ((int32_t)(str[1] & 0x3F) << 6) | ((int32_t)(str[2] & 0x3F) << 0);
  244. next = str + 3;
  245. } else if ((str[0] & 0xF8) == 0xF0 && (str[0] <= 0xF4)) { // U+10000-10FFFF
  246. *code_point = ((int32_t)(str[0] & 0x07) << 18) | ((int32_t)(str[1] & 0x3F) << 12) | ((int32_t)(str[2] & 0x3F) << 6) | ((int32_t)(str[3] & 0x3F) << 0);
  247. next = str + 4;
  248. } else {
  249. *code_point = -1;
  250. next = str + 1;
  251. }
  252. // part of a UTF-16 surrogate pair - invalid
  253. if (*code_point >= 0xD800 && *code_point <= 0xDFFF) {
  254. *code_point = -1;
  255. }
  256. return next;
  257. }
  258. void send_unicode_string(const char *str) {
  259. if (!str) {
  260. return;
  261. }
  262. while (*str) {
  263. int32_t code_point = 0;
  264. str = decode_utf8(str, &code_point);
  265. if (code_point >= 0) {
  266. register_unicode(code_point);
  267. }
  268. }
  269. }
  270. // clang-format off
  271. static void audio_helper(void) {
  272. #ifdef AUDIO_ENABLE
  273. switch (get_unicode_input_mode()) {
  274. # ifdef UNICODE_SONG_MAC
  275. static float song_mac[][2] = UNICODE_SONG_MAC;
  276. case UC_MAC:
  277. PLAY_SONG(song_mac);
  278. break;
  279. # endif
  280. # ifdef UNICODE_SONG_LNX
  281. static float song_lnx[][2] = UNICODE_SONG_LNX;
  282. case UC_LNX:
  283. PLAY_SONG(song_lnx);
  284. break;
  285. # endif
  286. # ifdef UNICODE_SONG_WIN
  287. static float song_win[][2] = UNICODE_SONG_WIN;
  288. case UC_WIN:
  289. PLAY_SONG(song_win);
  290. break;
  291. # endif
  292. # ifdef UNICODE_SONG_BSD
  293. static float song_bsd[][2] = UNICODE_SONG_BSD;
  294. case UC_BSD:
  295. PLAY_SONG(song_bsd);
  296. break;
  297. # endif
  298. # ifdef UNICODE_SONG_WINC
  299. static float song_winc[][2] = UNICODE_SONG_WINC;
  300. case UC_WINC:
  301. PLAY_SONG(song_winc);
  302. break;
  303. # endif
  304. }
  305. #endif
  306. }
  307. // clang-format on
  308. bool process_unicode_common(uint16_t keycode, keyrecord_t *record) {
  309. if (record->event.pressed) {
  310. bool shifted = get_mods() & MOD_MASK_SHIFT;
  311. switch (keycode) {
  312. case UNICODE_MODE_FORWARD:
  313. cycle_unicode_input_mode(shifted ? -1 : +1);
  314. audio_helper();
  315. break;
  316. case UNICODE_MODE_REVERSE:
  317. cycle_unicode_input_mode(shifted ? +1 : -1);
  318. audio_helper();
  319. break;
  320. case UNICODE_MODE_MAC ... UNICODE_MODE_WINC: {
  321. // Keycodes and input modes follow the same ordering
  322. uint8_t delta = keycode - UNICODE_MODE_MAC;
  323. set_unicode_input_mode(UC_MAC + delta);
  324. audio_helper();
  325. break;
  326. }
  327. }
  328. }
  329. #if defined(UNICODE_ENABLE)
  330. return process_unicode(keycode, record);
  331. #elif defined(UNICODEMAP_ENABLE)
  332. return process_unicodemap(keycode, record);
  333. #elif defined(UCIS_ENABLE)
  334. return process_ucis(keycode, record);
  335. #else
  336. return true;
  337. #endif
  338. }