process_unicode_common.c 10 KB

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