process_unicode_common.c 9.9 KB

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