process_unicode_common.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 <string.h>
  19. #include <ctype.h>
  20. static uint8_t input_mode;
  21. uint8_t mods;
  22. void set_unicode_input_mode(uint8_t os_target) {
  23. input_mode = os_target;
  24. eeprom_update_byte(EECONFIG_UNICODEMODE, os_target);
  25. }
  26. uint8_t get_unicode_input_mode(void) {
  27. return input_mode;
  28. }
  29. void unicode_input_mode_init(void) {
  30. static bool first_flag = false;
  31. if (!first_flag) {
  32. input_mode = eeprom_read_byte(EECONFIG_UNICODEMODE);
  33. first_flag = true;
  34. }
  35. }
  36. __attribute__((weak))
  37. void unicode_input_start (void) {
  38. // save current mods
  39. mods = keyboard_report->mods;
  40. // unregister all mods to start from clean state
  41. if (mods & MOD_BIT(KC_LSFT)) unregister_code(KC_LSFT);
  42. if (mods & MOD_BIT(KC_RSFT)) unregister_code(KC_RSFT);
  43. if (mods & MOD_BIT(KC_LCTL)) unregister_code(KC_LCTL);
  44. if (mods & MOD_BIT(KC_RCTL)) unregister_code(KC_RCTL);
  45. if (mods & MOD_BIT(KC_LALT)) unregister_code(KC_LALT);
  46. if (mods & MOD_BIT(KC_RALT)) unregister_code(KC_RALT);
  47. if (mods & MOD_BIT(KC_LGUI)) unregister_code(KC_LGUI);
  48. if (mods & MOD_BIT(KC_RGUI)) unregister_code(KC_RGUI);
  49. switch(input_mode) {
  50. case UC_OSX:
  51. register_code(KC_LALT);
  52. break;
  53. case UC_OSX_RALT:
  54. register_code(KC_RALT);
  55. break;
  56. case UC_LNX:
  57. register_code(KC_LCTL);
  58. register_code(KC_LSFT);
  59. register_code(KC_U);
  60. unregister_code(KC_U);
  61. unregister_code(KC_LSFT);
  62. unregister_code(KC_LCTL);
  63. break;
  64. case UC_WIN:
  65. register_code(KC_LALT);
  66. register_code(KC_PPLS);
  67. unregister_code(KC_PPLS);
  68. break;
  69. case UC_WINC:
  70. register_code(KC_RALT);
  71. unregister_code(KC_RALT);
  72. register_code(KC_U);
  73. unregister_code(KC_U);
  74. }
  75. wait_ms(UNICODE_TYPE_DELAY);
  76. }
  77. __attribute__((weak))
  78. void unicode_input_finish (void) {
  79. switch(input_mode) {
  80. case UC_OSX:
  81. case UC_WIN:
  82. unregister_code(KC_LALT);
  83. break;
  84. case UC_OSX_RALT:
  85. unregister_code(KC_RALT);
  86. break;
  87. case UC_LNX:
  88. register_code(KC_SPC);
  89. unregister_code(KC_SPC);
  90. break;
  91. }
  92. // reregister previously set mods
  93. if (mods & MOD_BIT(KC_LSFT)) register_code(KC_LSFT);
  94. if (mods & MOD_BIT(KC_RSFT)) register_code(KC_RSFT);
  95. if (mods & MOD_BIT(KC_LCTL)) register_code(KC_LCTL);
  96. if (mods & MOD_BIT(KC_RCTL)) register_code(KC_RCTL);
  97. if (mods & MOD_BIT(KC_LALT)) register_code(KC_LALT);
  98. if (mods & MOD_BIT(KC_RALT)) register_code(KC_RALT);
  99. if (mods & MOD_BIT(KC_LGUI)) register_code(KC_LGUI);
  100. if (mods & MOD_BIT(KC_RGUI)) register_code(KC_RGUI);
  101. }
  102. __attribute__((weak))
  103. uint16_t hex_to_keycode(uint8_t hex) {
  104. if (hex == 0x0) {
  105. return KC_0;
  106. } else if (hex < 0xA) {
  107. return KC_1 + (hex - 0x1);
  108. } else {
  109. return KC_A + (hex - 0xA);
  110. }
  111. }
  112. void register_hex(uint16_t hex) {
  113. for(int i = 3; i >= 0; i--) {
  114. uint8_t digit = ((hex >> (i*4)) & 0xF);
  115. register_code(hex_to_keycode(digit));
  116. unregister_code(hex_to_keycode(digit));
  117. }
  118. }
  119. void send_unicode_hex_string(const char *str) {
  120. if (!str) { return; } // Safety net
  121. while (*str) {
  122. // Find the next code point (token) in the string
  123. for (; *str == ' '; str++);
  124. size_t n = strcspn(str, " "); // Length of the current token
  125. char code_point[n+1];
  126. strncpy(code_point, str, n);
  127. code_point[n] = '\0'; // Make sure it's null-terminated
  128. // Normalize the code point: make all hex digits lowercase
  129. for (char *p = code_point; *p; p++) {
  130. *p = tolower((unsigned char)*p);
  131. }
  132. // Send the code point as a Unicode input string
  133. unicode_input_start();
  134. send_string(code_point);
  135. unicode_input_finish();
  136. str += n; // Move to the first ' ' (or '\0') after the current token
  137. }
  138. }