process_unicode_common.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. clear_weak_mods();
  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. void send_unicode_string(const char *str) {
  210. if (!str) {
  211. return;
  212. }
  213. while (*str) {
  214. int32_t code_point = 0;
  215. str = decode_utf8(str, &code_point);
  216. if (code_point >= 0) {
  217. register_unicode(code_point);
  218. }
  219. }
  220. }
  221. // clang-format off
  222. static void audio_helper(void) {
  223. #ifdef AUDIO_ENABLE
  224. switch (get_unicode_input_mode()) {
  225. # ifdef UNICODE_SONG_MAC
  226. static float song_mac[][2] = UNICODE_SONG_MAC;
  227. case UC_MAC:
  228. PLAY_SONG(song_mac);
  229. break;
  230. # endif
  231. # ifdef UNICODE_SONG_LNX
  232. static float song_lnx[][2] = UNICODE_SONG_LNX;
  233. case UC_LNX:
  234. PLAY_SONG(song_lnx);
  235. break;
  236. # endif
  237. # ifdef UNICODE_SONG_WIN
  238. static float song_win[][2] = UNICODE_SONG_WIN;
  239. case UC_WIN:
  240. PLAY_SONG(song_win);
  241. break;
  242. # endif
  243. # ifdef UNICODE_SONG_BSD
  244. static float song_bsd[][2] = UNICODE_SONG_BSD;
  245. case UC_BSD:
  246. PLAY_SONG(song_bsd);
  247. break;
  248. # endif
  249. # ifdef UNICODE_SONG_WINC
  250. static float song_winc[][2] = UNICODE_SONG_WINC;
  251. case UC_WINC:
  252. PLAY_SONG(song_winc);
  253. break;
  254. # endif
  255. }
  256. #endif
  257. }
  258. // clang-format on
  259. bool process_unicode_common(uint16_t keycode, keyrecord_t *record) {
  260. if (record->event.pressed) {
  261. bool shifted = get_mods() & MOD_MASK_SHIFT;
  262. switch (keycode) {
  263. case UNICODE_MODE_FORWARD:
  264. cycle_unicode_input_mode(shifted ? -1 : +1);
  265. audio_helper();
  266. break;
  267. case UNICODE_MODE_REVERSE:
  268. cycle_unicode_input_mode(shifted ? +1 : -1);
  269. audio_helper();
  270. break;
  271. case UNICODE_MODE_MAC ... UNICODE_MODE_WINC: {
  272. // Keycodes and input modes follow the same ordering
  273. uint8_t delta = keycode - UNICODE_MODE_MAC;
  274. set_unicode_input_mode(UC_MAC + delta);
  275. audio_helper();
  276. break;
  277. }
  278. }
  279. }
  280. #if defined(UNICODE_ENABLE)
  281. return process_unicode(keycode, record);
  282. #elif defined(UNICODEMAP_ENABLE)
  283. return process_unicodemap(keycode, record);
  284. #elif defined(UCIS_ENABLE)
  285. return process_ucis(keycode, record);
  286. #else
  287. return true;
  288. #endif
  289. }