process_unicode_common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. case UC_EMACS:
  109. // The usual way to type unicode in emacs is C-x-8 <RET> then the unicode number in hex
  110. tap_code16(LCTL(KC_X));
  111. tap_code16(KC_8);
  112. tap_code16(KC_ENTER);
  113. break;
  114. }
  115. wait_ms(UNICODE_TYPE_DELAY);
  116. }
  117. __attribute__((weak)) void unicode_input_finish(void) {
  118. switch (unicode_config.input_mode) {
  119. case UC_MAC:
  120. unregister_code(UNICODE_KEY_MAC);
  121. break;
  122. case UC_LNX:
  123. tap_code(KC_SPACE);
  124. if (unicode_saved_caps_lock) {
  125. tap_code(KC_CAPS_LOCK);
  126. }
  127. break;
  128. case UC_WIN:
  129. unregister_code(KC_LEFT_ALT);
  130. if (!unicode_saved_num_lock) {
  131. tap_code(KC_NUM_LOCK);
  132. }
  133. break;
  134. case UC_WINC:
  135. tap_code(KC_ENTER);
  136. break;
  137. case UC_EMACS:
  138. tap_code16(KC_ENTER);
  139. break;
  140. }
  141. set_mods(unicode_saved_mods); // Reregister previously set mods
  142. }
  143. __attribute__((weak)) void unicode_input_cancel(void) {
  144. switch (unicode_config.input_mode) {
  145. case UC_MAC:
  146. unregister_code(UNICODE_KEY_MAC);
  147. break;
  148. case UC_LNX:
  149. tap_code(KC_ESCAPE);
  150. if (unicode_saved_caps_lock) {
  151. tap_code(KC_CAPS_LOCK);
  152. }
  153. break;
  154. case UC_WINC:
  155. tap_code(KC_ESCAPE);
  156. break;
  157. case UC_WIN:
  158. unregister_code(KC_LEFT_ALT);
  159. if (!unicode_saved_num_lock) {
  160. tap_code(KC_NUM_LOCK);
  161. }
  162. break;
  163. case UC_EMACS:
  164. tap_code16(LCTL(KC_G)); // C-g cancels
  165. break;
  166. }
  167. set_mods(unicode_saved_mods); // Reregister previously set mods
  168. }
  169. // clang-format off
  170. static void send_nibble_wrapper(uint8_t digit) {
  171. if (unicode_config.input_mode == UC_WIN) {
  172. uint8_t kc = digit < 10
  173. ? KC_KP_1 + (10 + digit - 1) % 10
  174. : KC_A + (digit - 10);
  175. tap_code(kc);
  176. return;
  177. }
  178. send_nibble(digit);
  179. }
  180. // clang-format on
  181. void register_hex(uint16_t hex) {
  182. for (int i = 3; i >= 0; i--) {
  183. uint8_t digit = ((hex >> (i * 4)) & 0xF);
  184. send_nibble_wrapper(digit);
  185. }
  186. }
  187. void register_hex32(uint32_t hex) {
  188. bool onzerostart = true;
  189. for (int i = 7; i >= 0; i--) {
  190. if (i <= 3) {
  191. onzerostart = false;
  192. }
  193. uint8_t digit = ((hex >> (i * 4)) & 0xF);
  194. if (digit == 0) {
  195. if (!onzerostart) {
  196. send_nibble_wrapper(digit);
  197. }
  198. } else {
  199. send_nibble_wrapper(digit);
  200. onzerostart = false;
  201. }
  202. }
  203. }
  204. void register_unicode(uint32_t code_point) {
  205. if (code_point > 0x10FFFF || (code_point > 0xFFFF && unicode_config.input_mode == UC_WIN)) {
  206. // Code point out of range, do nothing
  207. return;
  208. }
  209. unicode_input_start();
  210. if (code_point > 0xFFFF && unicode_config.input_mode == UC_MAC) {
  211. // Convert code point to UTF-16 surrogate pair on macOS
  212. code_point -= 0x10000;
  213. uint32_t lo = code_point & 0x3FF, hi = (code_point & 0xFFC00) >> 10;
  214. register_hex32(hi + 0xD800);
  215. register_hex32(lo + 0xDC00);
  216. } else {
  217. register_hex32(code_point);
  218. }
  219. unicode_input_finish();
  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:
  284. set_unicode_input_mode(UC_MAC);
  285. audio_helper();
  286. break;
  287. case UNICODE_MODE_LNX:
  288. set_unicode_input_mode(UC_LNX);
  289. audio_helper();
  290. break;
  291. case UNICODE_MODE_WIN:
  292. set_unicode_input_mode(UC_WIN);
  293. audio_helper();
  294. break;
  295. case UNICODE_MODE_BSD:
  296. set_unicode_input_mode(UC_BSD);
  297. audio_helper();
  298. break;
  299. case UNICODE_MODE_WINC:
  300. set_unicode_input_mode(UC_WINC);
  301. audio_helper();
  302. break;
  303. case UNICODE_MODE_EMACS:
  304. set_unicode_input_mode(UC_EMACS);
  305. audio_helper();
  306. break;
  307. }
  308. }
  309. #if defined(UNICODE_ENABLE)
  310. return process_unicode(keycode, record);
  311. #elif defined(UNICODEMAP_ENABLE)
  312. return process_unicodemap(keycode, record);
  313. #elif defined(UCIS_ENABLE)
  314. return process_ucis(keycode, record);
  315. #else
  316. return true;
  317. #endif
  318. }