process_unicode_common.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 "utf8.h"
  19. unicode_config_t unicode_config;
  20. uint8_t unicode_saved_mods;
  21. bool unicode_saved_caps_lock;
  22. bool unicode_saved_num_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) {
  52. return unicode_config.input_mode;
  53. }
  54. void set_unicode_input_mode(uint8_t mode) {
  55. unicode_config.input_mode = mode;
  56. persist_unicode_input_mode();
  57. dprintf("Unicode input mode set to: %u\n", unicode_config.input_mode);
  58. }
  59. void cycle_unicode_input_mode(int8_t offset) {
  60. #if UNICODE_SELECTED_MODES != -1
  61. selected_index = (selected_index + offset) % selected_count;
  62. if (selected_index < 0) {
  63. selected_index += selected_count;
  64. }
  65. unicode_config.input_mode = selected[selected_index];
  66. # if UNICODE_CYCLE_PERSIST
  67. persist_unicode_input_mode();
  68. # endif
  69. dprintf("Unicode input mode cycle to: %u\n", unicode_config.input_mode);
  70. #endif
  71. }
  72. void persist_unicode_input_mode(void) {
  73. eeprom_update_byte(EECONFIG_UNICODEMODE, unicode_config.input_mode);
  74. }
  75. __attribute__((weak)) void unicode_input_start(void) {
  76. unicode_saved_caps_lock = host_keyboard_led_state().caps_lock;
  77. unicode_saved_num_lock = host_keyboard_led_state().num_lock;
  78. // Note the order matters here!
  79. // Need to do this before we mess around with the mods, or else
  80. // UNICODE_KEY_LNX (which is usually Ctrl-Shift-U) might not work
  81. // correctly in the shifted case.
  82. if (unicode_config.input_mode == UC_LNX && unicode_saved_caps_lock) {
  83. tap_code(KC_CAPS_LOCK);
  84. }
  85. unicode_saved_mods = get_mods(); // Save current mods
  86. clear_mods(); // Unregister mods to start from a clean state
  87. switch (unicode_config.input_mode) {
  88. case UC_MAC:
  89. register_code(UNICODE_KEY_MAC);
  90. break;
  91. case UC_LNX:
  92. tap_code16(UNICODE_KEY_LNX);
  93. break;
  94. case UC_WIN:
  95. // For increased reliability, use numpad keys for inputting digits
  96. if (!unicode_saved_num_lock) {
  97. tap_code(KC_NUM_LOCK);
  98. }
  99. register_code(KC_LEFT_ALT);
  100. wait_ms(UNICODE_TYPE_DELAY);
  101. tap_code(KC_KP_PLUS);
  102. break;
  103. case UC_WINC:
  104. tap_code(UNICODE_KEY_WINC);
  105. tap_code(KC_U);
  106. break;
  107. }
  108. wait_ms(UNICODE_TYPE_DELAY);
  109. }
  110. __attribute__((weak)) void unicode_input_finish(void) {
  111. switch (unicode_config.input_mode) {
  112. case UC_MAC:
  113. unregister_code(UNICODE_KEY_MAC);
  114. break;
  115. case UC_LNX:
  116. tap_code(KC_SPACE);
  117. if (unicode_saved_caps_lock) {
  118. tap_code(KC_CAPS_LOCK);
  119. }
  120. break;
  121. case UC_WIN:
  122. unregister_code(KC_LEFT_ALT);
  123. if (!unicode_saved_num_lock) {
  124. tap_code(KC_NUM_LOCK);
  125. }
  126. break;
  127. case UC_WINC:
  128. tap_code(KC_ENTER);
  129. break;
  130. }
  131. set_mods(unicode_saved_mods); // Reregister previously set mods
  132. }
  133. __attribute__((weak)) void unicode_input_cancel(void) {
  134. switch (unicode_config.input_mode) {
  135. case UC_MAC:
  136. unregister_code(UNICODE_KEY_MAC);
  137. break;
  138. case UC_LNX:
  139. tap_code(KC_ESCAPE);
  140. if (unicode_saved_caps_lock) {
  141. tap_code(KC_CAPS_LOCK);
  142. }
  143. break;
  144. case UC_WINC:
  145. tap_code(KC_ESCAPE);
  146. break;
  147. case UC_WIN:
  148. unregister_code(KC_LEFT_ALT);
  149. if (!unicode_saved_num_lock) {
  150. tap_code(KC_NUM_LOCK);
  151. }
  152. break;
  153. }
  154. set_mods(unicode_saved_mods); // Reregister previously set mods
  155. }
  156. // clang-format off
  157. static void send_nibble_wrapper(uint8_t digit) {
  158. if (unicode_config.input_mode == UC_WIN) {
  159. uint8_t kc = digit < 10
  160. ? KC_KP_1 + (10 + digit - 1) % 10
  161. : KC_A + (digit - 10);
  162. tap_code(kc);
  163. return;
  164. }
  165. send_nibble(digit);
  166. }
  167. // clang-format on
  168. void register_hex(uint16_t hex) {
  169. for (int i = 3; i >= 0; i--) {
  170. uint8_t digit = ((hex >> (i * 4)) & 0xF);
  171. send_nibble_wrapper(digit);
  172. }
  173. }
  174. void register_hex32(uint32_t hex) {
  175. bool onzerostart = true;
  176. for (int i = 7; i >= 0; i--) {
  177. if (i <= 3) {
  178. onzerostart = false;
  179. }
  180. uint8_t digit = ((hex >> (i * 4)) & 0xF);
  181. if (digit == 0) {
  182. if (!onzerostart) {
  183. send_nibble_wrapper(digit);
  184. }
  185. } else {
  186. send_nibble_wrapper(digit);
  187. onzerostart = false;
  188. }
  189. }
  190. }
  191. void register_unicode(uint32_t code_point) {
  192. if (code_point > 0x10FFFF || (code_point > 0xFFFF && unicode_config.input_mode == UC_WIN)) {
  193. // Code point out of range, do nothing
  194. return;
  195. }
  196. unicode_input_start();
  197. if (code_point > 0xFFFF && unicode_config.input_mode == UC_MAC) {
  198. // Convert code point to UTF-16 surrogate pair on macOS
  199. code_point -= 0x10000;
  200. uint32_t lo = code_point & 0x3FF, hi = (code_point & 0xFFC00) >> 10;
  201. register_hex32(hi + 0xD800);
  202. register_hex32(lo + 0xDC00);
  203. } else {
  204. register_hex32(code_point);
  205. }
  206. unicode_input_finish();
  207. }
  208. void send_unicode_string(const char *str) {
  209. if (!str) {
  210. return;
  211. }
  212. while (*str) {
  213. int32_t code_point = 0;
  214. str = decode_utf8(str, &code_point);
  215. if (code_point >= 0) {
  216. register_unicode(code_point);
  217. }
  218. }
  219. }
  220. // clang-format off
  221. static void audio_helper(void) {
  222. #ifdef AUDIO_ENABLE
  223. switch (get_unicode_input_mode()) {
  224. # ifdef UNICODE_SONG_MAC
  225. static float song_mac[][2] = UNICODE_SONG_MAC;
  226. case UC_MAC:
  227. PLAY_SONG(song_mac);
  228. break;
  229. # endif
  230. # ifdef UNICODE_SONG_LNX
  231. static float song_lnx[][2] = UNICODE_SONG_LNX;
  232. case UC_LNX:
  233. PLAY_SONG(song_lnx);
  234. break;
  235. # endif
  236. # ifdef UNICODE_SONG_WIN
  237. static float song_win[][2] = UNICODE_SONG_WIN;
  238. case UC_WIN:
  239. PLAY_SONG(song_win);
  240. break;
  241. # endif
  242. # ifdef UNICODE_SONG_BSD
  243. static float song_bsd[][2] = UNICODE_SONG_BSD;
  244. case UC_BSD:
  245. PLAY_SONG(song_bsd);
  246. break;
  247. # endif
  248. # ifdef UNICODE_SONG_WINC
  249. static float song_winc[][2] = UNICODE_SONG_WINC;
  250. case UC_WINC:
  251. PLAY_SONG(song_winc);
  252. break;
  253. # endif
  254. }
  255. #endif
  256. }
  257. // clang-format on
  258. bool process_unicode_common(uint16_t keycode, keyrecord_t *record) {
  259. if (record->event.pressed) {
  260. bool shifted = get_mods() & MOD_MASK_SHIFT;
  261. switch (keycode) {
  262. case UNICODE_MODE_FORWARD:
  263. cycle_unicode_input_mode(shifted ? -1 : +1);
  264. audio_helper();
  265. break;
  266. case UNICODE_MODE_REVERSE:
  267. cycle_unicode_input_mode(shifted ? +1 : -1);
  268. audio_helper();
  269. break;
  270. case UNICODE_MODE_MAC ... UNICODE_MODE_WINC: {
  271. // Keycodes and input modes follow the same ordering
  272. uint8_t delta = keycode - UNICODE_MODE_MAC;
  273. set_unicode_input_mode(UC_MAC + delta);
  274. audio_helper();
  275. break;
  276. }
  277. }
  278. }
  279. #if defined(UNICODE_ENABLE)
  280. return process_unicode(keycode, record);
  281. #elif defined(UNICODEMAP_ENABLE)
  282. return process_unicodemap(keycode, record);
  283. #elif defined(UCIS_ENABLE)
  284. return process_ucis(keycode, record);
  285. #else
  286. return true;
  287. #endif
  288. }