process_unicode_common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 = ARRAY_SIZE(selected);
  26. static int8_t selected_index;
  27. #endif
  28. /** \brief Uunicode input mode set at user level
  29. *
  30. * Run user code on unicode input mode change
  31. */
  32. __attribute__((weak)) void unicode_input_mode_set_user(uint8_t input_mode) {}
  33. /** \brief unicode input mode set at keyboard level
  34. *
  35. * Run keyboard code on unicode input mode change
  36. */
  37. __attribute__((weak)) void unicode_input_mode_set_kb(uint8_t input_mode) {
  38. unicode_input_mode_set_user(input_mode);
  39. }
  40. void unicode_input_mode_init(void) {
  41. unicode_config.raw = eeprom_read_byte(EECONFIG_UNICODEMODE);
  42. #if UNICODE_SELECTED_MODES != -1
  43. # if UNICODE_CYCLE_PERSIST
  44. // Find input_mode in selected modes
  45. int8_t i;
  46. for (i = 0; i < selected_count; i++) {
  47. if (selected[i] == unicode_config.input_mode) {
  48. selected_index = i;
  49. break;
  50. }
  51. }
  52. if (i == selected_count) {
  53. // Not found: input_mode isn't selected, change to one that is
  54. unicode_config.input_mode = selected[selected_index = 0];
  55. }
  56. # else
  57. // Always change to the first selected input mode
  58. unicode_config.input_mode = selected[selected_index = 0];
  59. # endif
  60. #endif
  61. unicode_input_mode_set_kb(unicode_config.input_mode);
  62. dprintf("Unicode input mode init to: %u\n", unicode_config.input_mode);
  63. }
  64. uint8_t get_unicode_input_mode(void) {
  65. return unicode_config.input_mode;
  66. }
  67. void set_unicode_input_mode(uint8_t mode) {
  68. unicode_config.input_mode = mode;
  69. persist_unicode_input_mode();
  70. unicode_input_mode_set_kb(mode);
  71. dprintf("Unicode input mode set to: %u\n", unicode_config.input_mode);
  72. }
  73. void cycle_unicode_input_mode(int8_t offset) {
  74. #if UNICODE_SELECTED_MODES != -1
  75. selected_index = (selected_index + offset) % selected_count;
  76. if (selected_index < 0) {
  77. selected_index += selected_count;
  78. }
  79. unicode_config.input_mode = selected[selected_index];
  80. # if UNICODE_CYCLE_PERSIST
  81. persist_unicode_input_mode();
  82. # endif
  83. unicode_input_mode_set_kb(unicode_config.input_mode);
  84. dprintf("Unicode input mode cycle to: %u\n", unicode_config.input_mode);
  85. #endif
  86. }
  87. void persist_unicode_input_mode(void) {
  88. eeprom_update_byte(EECONFIG_UNICODEMODE, unicode_config.input_mode);
  89. }
  90. __attribute__((weak)) void unicode_input_start(void) {
  91. unicode_saved_caps_lock = host_keyboard_led_state().caps_lock;
  92. unicode_saved_num_lock = host_keyboard_led_state().num_lock;
  93. // Note the order matters here!
  94. // Need to do this before we mess around with the mods, or else
  95. // UNICODE_KEY_LNX (which is usually Ctrl-Shift-U) might not work
  96. // correctly in the shifted case.
  97. if (unicode_config.input_mode == UC_LNX && unicode_saved_caps_lock) {
  98. tap_code(KC_CAPS_LOCK);
  99. }
  100. unicode_saved_mods = get_mods(); // Save current mods
  101. clear_mods(); // Unregister mods to start from a clean state
  102. clear_weak_mods();
  103. switch (unicode_config.input_mode) {
  104. case UC_MAC:
  105. register_code(UNICODE_KEY_MAC);
  106. break;
  107. case UC_LNX:
  108. tap_code16(UNICODE_KEY_LNX);
  109. break;
  110. case UC_WIN:
  111. // For increased reliability, use numpad keys for inputting digits
  112. if (!unicode_saved_num_lock) {
  113. tap_code(KC_NUM_LOCK);
  114. }
  115. register_code(KC_LEFT_ALT);
  116. wait_ms(UNICODE_TYPE_DELAY);
  117. tap_code(KC_KP_PLUS);
  118. break;
  119. case UC_WINC:
  120. tap_code(UNICODE_KEY_WINC);
  121. tap_code(KC_U);
  122. break;
  123. case UC_EMACS:
  124. // The usual way to type unicode in emacs is C-x-8 <RET> then the unicode number in hex
  125. tap_code16(LCTL(KC_X));
  126. tap_code16(KC_8);
  127. tap_code16(KC_ENTER);
  128. break;
  129. }
  130. wait_ms(UNICODE_TYPE_DELAY);
  131. }
  132. __attribute__((weak)) void unicode_input_finish(void) {
  133. switch (unicode_config.input_mode) {
  134. case UC_MAC:
  135. unregister_code(UNICODE_KEY_MAC);
  136. break;
  137. case UC_LNX:
  138. tap_code(KC_SPACE);
  139. if (unicode_saved_caps_lock) {
  140. tap_code(KC_CAPS_LOCK);
  141. }
  142. break;
  143. case UC_WIN:
  144. unregister_code(KC_LEFT_ALT);
  145. if (!unicode_saved_num_lock) {
  146. tap_code(KC_NUM_LOCK);
  147. }
  148. break;
  149. case UC_WINC:
  150. tap_code(KC_ENTER);
  151. break;
  152. case UC_EMACS:
  153. tap_code16(KC_ENTER);
  154. break;
  155. }
  156. set_mods(unicode_saved_mods); // Reregister previously set mods
  157. }
  158. __attribute__((weak)) void unicode_input_cancel(void) {
  159. switch (unicode_config.input_mode) {
  160. case UC_MAC:
  161. unregister_code(UNICODE_KEY_MAC);
  162. break;
  163. case UC_LNX:
  164. tap_code(KC_ESCAPE);
  165. if (unicode_saved_caps_lock) {
  166. tap_code(KC_CAPS_LOCK);
  167. }
  168. break;
  169. case UC_WINC:
  170. tap_code(KC_ESCAPE);
  171. break;
  172. case UC_WIN:
  173. unregister_code(KC_LEFT_ALT);
  174. if (!unicode_saved_num_lock) {
  175. tap_code(KC_NUM_LOCK);
  176. }
  177. break;
  178. case UC_EMACS:
  179. tap_code16(LCTL(KC_G)); // C-g cancels
  180. break;
  181. }
  182. set_mods(unicode_saved_mods); // Reregister previously set mods
  183. }
  184. // clang-format off
  185. static void send_nibble_wrapper(uint8_t digit) {
  186. if (unicode_config.input_mode == UC_WIN) {
  187. uint8_t kc = digit < 10
  188. ? KC_KP_1 + (10 + digit - 1) % 10
  189. : KC_A + (digit - 10);
  190. tap_code(kc);
  191. return;
  192. }
  193. send_nibble(digit);
  194. }
  195. // clang-format on
  196. void register_hex(uint16_t hex) {
  197. for (int i = 3; i >= 0; i--) {
  198. uint8_t digit = ((hex >> (i * 4)) & 0xF);
  199. send_nibble_wrapper(digit);
  200. }
  201. }
  202. void register_hex32(uint32_t hex) {
  203. bool onzerostart = true;
  204. for (int i = 7; i >= 0; i--) {
  205. if (i <= 3) {
  206. onzerostart = false;
  207. }
  208. uint8_t digit = ((hex >> (i * 4)) & 0xF);
  209. if (digit == 0) {
  210. if (!onzerostart) {
  211. send_nibble_wrapper(digit);
  212. }
  213. } else {
  214. send_nibble_wrapper(digit);
  215. onzerostart = false;
  216. }
  217. }
  218. }
  219. void register_unicode(uint32_t code_point) {
  220. if (code_point > 0x10FFFF || (code_point > 0xFFFF && unicode_config.input_mode == UC_WIN)) {
  221. // Code point out of range, do nothing
  222. return;
  223. }
  224. unicode_input_start();
  225. if (code_point > 0xFFFF && unicode_config.input_mode == UC_MAC) {
  226. // Convert code point to UTF-16 surrogate pair on macOS
  227. code_point -= 0x10000;
  228. uint32_t lo = code_point & 0x3FF, hi = (code_point & 0xFFC00) >> 10;
  229. register_hex32(hi + 0xD800);
  230. register_hex32(lo + 0xDC00);
  231. } else {
  232. register_hex32(code_point);
  233. }
  234. unicode_input_finish();
  235. }
  236. void send_unicode_string(const char *str) {
  237. if (!str) {
  238. return;
  239. }
  240. while (*str) {
  241. int32_t code_point = 0;
  242. str = decode_utf8(str, &code_point);
  243. if (code_point >= 0) {
  244. register_unicode(code_point);
  245. }
  246. }
  247. }
  248. // clang-format off
  249. static void audio_helper(void) {
  250. #ifdef AUDIO_ENABLE
  251. switch (get_unicode_input_mode()) {
  252. # ifdef UNICODE_SONG_MAC
  253. static float song_mac[][2] = UNICODE_SONG_MAC;
  254. case UC_MAC:
  255. PLAY_SONG(song_mac);
  256. break;
  257. # endif
  258. # ifdef UNICODE_SONG_LNX
  259. static float song_lnx[][2] = UNICODE_SONG_LNX;
  260. case UC_LNX:
  261. PLAY_SONG(song_lnx);
  262. break;
  263. # endif
  264. # ifdef UNICODE_SONG_WIN
  265. static float song_win[][2] = UNICODE_SONG_WIN;
  266. case UC_WIN:
  267. PLAY_SONG(song_win);
  268. break;
  269. # endif
  270. # ifdef UNICODE_SONG_BSD
  271. static float song_bsd[][2] = UNICODE_SONG_BSD;
  272. case UC_BSD:
  273. PLAY_SONG(song_bsd);
  274. break;
  275. # endif
  276. # ifdef UNICODE_SONG_WINC
  277. static float song_winc[][2] = UNICODE_SONG_WINC;
  278. case UC_WINC:
  279. PLAY_SONG(song_winc);
  280. break;
  281. # endif
  282. }
  283. #endif
  284. }
  285. // clang-format on
  286. bool process_unicode_common(uint16_t keycode, keyrecord_t *record) {
  287. if (record->event.pressed) {
  288. bool shifted = get_mods() & MOD_MASK_SHIFT;
  289. switch (keycode) {
  290. case UNICODE_MODE_FORWARD:
  291. cycle_unicode_input_mode(shifted ? -1 : +1);
  292. audio_helper();
  293. break;
  294. case UNICODE_MODE_REVERSE:
  295. cycle_unicode_input_mode(shifted ? +1 : -1);
  296. audio_helper();
  297. break;
  298. case UNICODE_MODE_MAC:
  299. set_unicode_input_mode(UC_MAC);
  300. audio_helper();
  301. break;
  302. case UNICODE_MODE_LNX:
  303. set_unicode_input_mode(UC_LNX);
  304. audio_helper();
  305. break;
  306. case UNICODE_MODE_WIN:
  307. set_unicode_input_mode(UC_WIN);
  308. audio_helper();
  309. break;
  310. case UNICODE_MODE_BSD:
  311. set_unicode_input_mode(UC_BSD);
  312. audio_helper();
  313. break;
  314. case UNICODE_MODE_WINC:
  315. set_unicode_input_mode(UC_WINC);
  316. audio_helper();
  317. break;
  318. case UNICODE_MODE_EMACS:
  319. set_unicode_input_mode(UC_EMACS);
  320. audio_helper();
  321. break;
  322. }
  323. }
  324. #if defined(UNICODE_ENABLE)
  325. return process_unicode(keycode, record);
  326. #elif defined(UNICODEMAP_ENABLE)
  327. return process_unicodemap(keycode, record);
  328. #elif defined(UCIS_ENABLE)
  329. return process_ucis(keycode, record);
  330. #else
  331. return true;
  332. #endif
  333. }