process_unicode_common.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #if UNICODE_SELECTED_MODES != -1
  22. static uint8_t selected[] = { UNICODE_SELECTED_MODES };
  23. static uint8_t selected_count = sizeof selected / sizeof *selected;
  24. static uint8_t selected_index;
  25. #endif
  26. void unicode_input_mode_init(void) {
  27. unicode_config.raw = eeprom_read_byte(EECONFIG_UNICODEMODE);
  28. #if UNICODE_SELECTED_MODES != -1
  29. #if UNICODE_CYCLE_PERSIST
  30. // Find input_mode in selected modes
  31. uint8_t i;
  32. for (i = 0; i < selected_count; i++) {
  33. if (selected[i] == unicode_config.input_mode) {
  34. selected_index = i;
  35. break;
  36. }
  37. }
  38. if (i == selected_count) {
  39. // Not found: input_mode isn't selected, change to one that is
  40. unicode_config.input_mode = selected[selected_index = 0];
  41. }
  42. #else
  43. // Always change to the first selected input mode
  44. unicode_config.input_mode = selected[selected_index = 0];
  45. #endif
  46. #endif
  47. dprintf("Unicode input mode init to: %u\n", unicode_config.input_mode);
  48. }
  49. uint8_t get_unicode_input_mode(void) {
  50. return unicode_config.input_mode;
  51. }
  52. void set_unicode_input_mode(uint8_t mode) {
  53. unicode_config.input_mode = mode;
  54. persist_unicode_input_mode();
  55. dprintf("Unicode input mode set to: %u\n", unicode_config.input_mode);
  56. }
  57. void cycle_unicode_input_mode(uint8_t offset) {
  58. #if UNICODE_SELECTED_MODES != -1
  59. selected_index = (selected_index + offset) % selected_count;
  60. unicode_config.input_mode = selected[selected_index];
  61. #if UNICODE_CYCLE_PERSIST
  62. persist_unicode_input_mode();
  63. #endif
  64. dprintf("Unicode input mode cycle to: %u\n", unicode_config.input_mode);
  65. #endif
  66. }
  67. void persist_unicode_input_mode(void) {
  68. eeprom_update_byte(EECONFIG_UNICODEMODE, unicode_config.input_mode);
  69. }
  70. static uint8_t saved_mods;
  71. __attribute__((weak))
  72. void unicode_input_start(void) {
  73. 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_OSX_KEY);
  78. break;
  79. case UC_LNX:
  80. register_code(KC_LCTL);
  81. register_code(KC_LSFT);
  82. tap_code(KC_U); // TODO: Replace with tap_code16(LCTL(LSFT(KC_U))); and test
  83. unregister_code(KC_LSFT);
  84. unregister_code(KC_LCTL);
  85. break;
  86. case UC_WIN:
  87. register_code(KC_LALT);
  88. tap_code(KC_PPLS);
  89. break;
  90. case UC_WINC:
  91. tap_code(UNICODE_WINC_KEY);
  92. tap_code(KC_U);
  93. break;
  94. }
  95. wait_ms(UNICODE_TYPE_DELAY);
  96. }
  97. __attribute__((weak))
  98. void unicode_input_finish(void) {
  99. switch (unicode_config.input_mode) {
  100. case UC_OSX:
  101. unregister_code(UNICODE_OSX_KEY);
  102. break;
  103. case UC_LNX:
  104. tap_code(KC_SPC);
  105. break;
  106. case UC_WIN:
  107. unregister_code(KC_LALT);
  108. break;
  109. case UC_WINC:
  110. tap_code(KC_ENTER);
  111. break;
  112. }
  113. set_mods(saved_mods); // Reregister previously set mods
  114. }
  115. __attribute__((weak))
  116. uint16_t hex_to_keycode(uint8_t hex) {
  117. if (hex == 0x0) {
  118. return KC_0;
  119. } else if (hex < 0xA) {
  120. return KC_1 + (hex - 0x1);
  121. } else {
  122. return KC_A + (hex - 0xA);
  123. }
  124. }
  125. void register_hex(uint16_t hex) {
  126. for(int i = 3; i >= 0; i--) {
  127. uint8_t digit = ((hex >> (i*4)) & 0xF);
  128. tap_code(hex_to_keycode(digit));
  129. }
  130. }
  131. void send_unicode_hex_string(const char *str) {
  132. if (!str) { return; }
  133. while (*str) {
  134. // Find the next code point (token) in the string
  135. for (; *str == ' '; str++);
  136. size_t n = strcspn(str, " "); // Length of the current token
  137. char code_point[n+1];
  138. strncpy(code_point, str, n);
  139. code_point[n] = '\0'; // Make sure it's null-terminated
  140. // Normalize the code point: make all hex digits lowercase
  141. for (char *p = code_point; *p; p++) {
  142. *p = tolower((unsigned char)*p);
  143. }
  144. // Send the code point as a Unicode input string
  145. unicode_input_start();
  146. send_string(code_point);
  147. unicode_input_finish();
  148. str += n; // Move to the first ' ' (or '\0') after the current token
  149. }
  150. }
  151. bool process_unicode_common(uint16_t keycode, keyrecord_t *record) {
  152. if (record->event.pressed) {
  153. switch (keycode) {
  154. case UNICODE_MODE_FORWARD:
  155. cycle_unicode_input_mode(+1);
  156. break;
  157. case UNICODE_MODE_REVERSE:
  158. cycle_unicode_input_mode(-1);
  159. break;
  160. case UNICODE_MODE_OSX:
  161. set_unicode_input_mode(UC_OSX);
  162. #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_OSX)
  163. static float song_osx[][2] = UNICODE_SONG_OSX;
  164. PLAY_SONG(song_osx);
  165. #endif
  166. break;
  167. case UNICODE_MODE_LNX:
  168. set_unicode_input_mode(UC_LNX);
  169. #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_LNX)
  170. static float song_lnx[][2] = UNICODE_SONG_LNX;
  171. PLAY_SONG(song_lnx);
  172. #endif
  173. break;
  174. case UNICODE_MODE_WIN:
  175. set_unicode_input_mode(UC_WIN);
  176. #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_WIN)
  177. static float song_win[][2] = UNICODE_SONG_WIN;
  178. PLAY_SONG(song_win);
  179. #endif
  180. break;
  181. case UNICODE_MODE_BSD:
  182. set_unicode_input_mode(UC_BSD);
  183. #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_BSD)
  184. static float song_bsd[][2] = UNICODE_SONG_BSD;
  185. PLAY_SONG(song_bsd);
  186. #endif
  187. break;
  188. case UNICODE_MODE_WINC:
  189. set_unicode_input_mode(UC_WINC);
  190. #if defined(AUDIO_ENABLE) && defined(UNICODE_SONG_WINC)
  191. static float song_winc[][2] = UNICODE_SONG_WINC;
  192. PLAY_SONG(song_winc);
  193. #endif
  194. break;
  195. }
  196. }
  197. #if defined(UNICODE_ENABLE)
  198. return process_unicode(keycode, record);
  199. #elif defined(UNICODEMAP_ENABLE)
  200. return process_unicodemap(keycode, record);
  201. #elif defined(UCIS_ENABLE)
  202. return process_ucis(keycode, record);
  203. #else
  204. return true;
  205. #endif
  206. }