process_unicode_common.c 10 KB

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