process_unicode_common.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 <ctype.h>
  19. #include <string.h>
  20. unicode_config_t unicode_config;
  21. uint8_t unicode_saved_mods;
  22. #if UNICODE_SELECTED_MODES != -1
  23. static uint8_t selected[] = { UNICODE_SELECTED_MODES };
  24. static uint8_t selected_count = sizeof selected / sizeof *selected;
  25. static uint8_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. uint8_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(uint8_t offset) {
  59. #if UNICODE_SELECTED_MODES != -1
  60. selected_index = (selected_index + offset) % selected_count;
  61. unicode_config.input_mode = selected[selected_index];
  62. #if UNICODE_CYCLE_PERSIST
  63. persist_unicode_input_mode();
  64. #endif
  65. dprintf("Unicode input mode cycle to: %u\n", unicode_config.input_mode);
  66. #endif
  67. }
  68. void persist_unicode_input_mode(void) {
  69. eeprom_update_byte(EECONFIG_UNICODEMODE, unicode_config.input_mode);
  70. }
  71. __attribute__((weak))
  72. void unicode_input_start(void) {
  73. unicode_saved_mods = get_mods(); // Save current mods
  74. clear_mods(); // Unregister mods to start from a clean state
  75. switch (unicode_config.input_mode) {
  76. case UC_OSX:
  77. register_code(UNICODE_KEY_OSX);
  78. break;
  79. case UC_LNX:
  80. tap_code16(UNICODE_KEY_LNX);
  81. break;
  82. case UC_WIN:
  83. register_code(KC_LALT);
  84. tap_code(KC_PPLS);
  85. break;
  86. case UC_WINC:
  87. tap_code(UNICODE_KEY_WINC);
  88. tap_code(KC_U);
  89. break;
  90. }
  91. wait_ms(UNICODE_TYPE_DELAY);
  92. }
  93. __attribute__((weak))
  94. void unicode_input_finish(void) {
  95. switch (unicode_config.input_mode) {
  96. case UC_OSX:
  97. unregister_code(UNICODE_KEY_OSX);
  98. break;
  99. case UC_LNX:
  100. tap_code(KC_SPC);
  101. break;
  102. case UC_WIN:
  103. unregister_code(KC_LALT);
  104. break;
  105. case UC_WINC:
  106. tap_code(KC_ENTER);
  107. break;
  108. }
  109. set_mods(unicode_saved_mods); // Reregister previously set mods
  110. }
  111. __attribute__((weak))
  112. void unicode_input_cancel(void) {
  113. switch (unicode_config.input_mode) {
  114. case UC_OSX:
  115. unregister_code(UNICODE_KEY_OSX);
  116. break;
  117. case UC_LNX:
  118. case UC_WINC:
  119. tap_code(KC_ESC);
  120. break;
  121. case UC_WIN:
  122. unregister_code(KC_LALT);
  123. break;
  124. }
  125. set_mods(unicode_saved_mods); // Reregister previously set mods
  126. }
  127. __attribute__((weak))
  128. uint16_t hex_to_keycode(uint8_t hex) {
  129. if (hex == 0x0) {
  130. return KC_0;
  131. } else if (hex < 0xA) {
  132. return KC_1 + (hex - 0x1);
  133. } else {
  134. return KC_A + (hex - 0xA);
  135. }
  136. }
  137. void register_hex(uint16_t hex) {
  138. for(int i = 3; i >= 0; i--) {
  139. uint8_t digit = ((hex >> (i*4)) & 0xF);
  140. tap_code(hex_to_keycode(digit));
  141. }
  142. }
  143. void send_unicode_hex_string(const char *str) {
  144. if (!str) { return; }
  145. while (*str) {
  146. // Find the next code point (token) in the string
  147. for (; *str == ' '; str++);
  148. size_t n = strcspn(str, " "); // Length of the current token
  149. char code_point[n+1];
  150. strncpy(code_point, str, n);
  151. code_point[n] = '\0'; // Make sure it's null-terminated
  152. // Normalize the code point: make all hex digits lowercase
  153. for (char *p = code_point; *p; p++) {
  154. *p = tolower((unsigned char)*p);
  155. }
  156. // Send the code point as a Unicode input string
  157. unicode_input_start();
  158. send_string(code_point);
  159. unicode_input_finish();
  160. str += n; // Move to the first ' ' (or '\0') after the current token
  161. }
  162. }
  163. bool process_unicode_common(uint16_t keycode, keyrecord_t *record) {
  164. if (record->event.pressed) {
  165. switch (keycode) {
  166. case UNICODE_MODE_FORWARD:
  167. cycle_unicode_input_mode(+1);
  168. break;
  169. case UNICODE_MODE_REVERSE:
  170. cycle_unicode_input_mode(-1);
  171. break;
  172. case UNICODE_MODE_OSX:
  173. set_unicode_input_mode(UC_OSX);
  174. #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_OSX)
  175. static float song_osx[][2] = UNICODE_SONG_OSX;
  176. PLAY_SONG(song_osx);
  177. #endif
  178. break;
  179. case UNICODE_MODE_LNX:
  180. set_unicode_input_mode(UC_LNX);
  181. #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_LNX)
  182. static float song_lnx[][2] = UNICODE_SONG_LNX;
  183. PLAY_SONG(song_lnx);
  184. #endif
  185. break;
  186. case UNICODE_MODE_WIN:
  187. set_unicode_input_mode(UC_WIN);
  188. #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_WIN)
  189. static float song_win[][2] = UNICODE_SONG_WIN;
  190. PLAY_SONG(song_win);
  191. #endif
  192. break;
  193. case UNICODE_MODE_BSD:
  194. set_unicode_input_mode(UC_BSD);
  195. #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_BSD)
  196. static float song_bsd[][2] = UNICODE_SONG_BSD;
  197. PLAY_SONG(song_bsd);
  198. #endif
  199. break;
  200. case UNICODE_MODE_WINC:
  201. set_unicode_input_mode(UC_WINC);
  202. #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_WINC)
  203. static float song_winc[][2] = UNICODE_SONG_WINC;
  204. PLAY_SONG(song_winc);
  205. #endif
  206. break;
  207. }
  208. }
  209. #if defined(UNICODE_ENABLE)
  210. return process_unicode(keycode, record);
  211. #elif defined(UNICODEMAP_ENABLE)
  212. return process_unicodemap(keycode, record);
  213. #elif defined(UCIS_ENABLE)
  214. return process_ucis(keycode, record);
  215. #else
  216. return true;
  217. #endif
  218. }