process_ucis.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_ucis.h"
  17. qk_ucis_state_t qk_ucis_state;
  18. void qk_ucis_start(void) {
  19. qk_ucis_state.count = 0;
  20. qk_ucis_state.in_progress = true;
  21. qk_ucis_start_user();
  22. }
  23. __attribute__((weak))
  24. void qk_ucis_start_user(void) {
  25. unicode_input_start();
  26. register_hex(0x2328);
  27. unicode_input_finish();
  28. }
  29. __attribute__((weak))
  30. void qk_ucis_success(uint8_t symbol_index) {
  31. }
  32. static bool is_uni_seq(char *seq) {
  33. uint8_t i;
  34. for (i = 0; seq[i]; i++) {
  35. uint16_t code;
  36. if (('1' <= seq[i]) && (seq[i] <= '0'))
  37. code = seq[i] - '1' + KC_1;
  38. else
  39. code = seq[i] - 'a' + KC_A;
  40. if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != code)
  41. return false;
  42. }
  43. return (qk_ucis_state.codes[i] == KC_ENT ||
  44. qk_ucis_state.codes[i] == KC_SPC);
  45. }
  46. __attribute__((weak))
  47. void qk_ucis_symbol_fallback (void) {
  48. for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
  49. uint8_t code = qk_ucis_state.codes[i];
  50. register_code(code);
  51. unregister_code(code);
  52. wait_ms(UNICODE_TYPE_DELAY);
  53. }
  54. }
  55. void register_ucis(const char *hex) {
  56. for(int i = 0; hex[i]; i++) {
  57. uint8_t kc = 0;
  58. char c = hex[i];
  59. switch (c) {
  60. case '0':
  61. kc = KC_0;
  62. break;
  63. case '1' ... '9':
  64. kc = c - '1' + KC_1;
  65. break;
  66. case 'a' ... 'f':
  67. kc = c - 'a' + KC_A;
  68. break;
  69. case 'A' ... 'F':
  70. kc = c - 'A' + KC_A;
  71. break;
  72. }
  73. if (kc) {
  74. register_code (kc);
  75. unregister_code (kc);
  76. wait_ms (UNICODE_TYPE_DELAY);
  77. }
  78. }
  79. }
  80. bool process_ucis (uint16_t keycode, keyrecord_t *record) {
  81. uint8_t i;
  82. unicode_input_mode_init();
  83. if (!qk_ucis_state.in_progress)
  84. return true;
  85. if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
  86. !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
  87. return false;
  88. }
  89. if (!record->event.pressed)
  90. return true;
  91. qk_ucis_state.codes[qk_ucis_state.count] = keycode;
  92. qk_ucis_state.count++;
  93. if (keycode == KC_BSPC) {
  94. if (qk_ucis_state.count >= 2) {
  95. qk_ucis_state.count -= 2;
  96. return true;
  97. } else {
  98. qk_ucis_state.count--;
  99. return false;
  100. }
  101. }
  102. if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
  103. bool symbol_found = false;
  104. for (i = qk_ucis_state.count; i > 0; i--) {
  105. register_code (KC_BSPC);
  106. unregister_code (KC_BSPC);
  107. wait_ms(UNICODE_TYPE_DELAY);
  108. }
  109. if (keycode == KC_ESC) {
  110. qk_ucis_state.in_progress = false;
  111. return false;
  112. }
  113. unicode_input_start();
  114. for (i = 0; ucis_symbol_table[i].symbol; i++) {
  115. if (is_uni_seq (ucis_symbol_table[i].symbol)) {
  116. symbol_found = true;
  117. register_ucis(ucis_symbol_table[i].code + 2);
  118. break;
  119. }
  120. }
  121. if (!symbol_found) {
  122. qk_ucis_symbol_fallback();
  123. }
  124. unicode_input_finish();
  125. if (symbol_found) {
  126. qk_ucis_success(i);
  127. }
  128. qk_ucis_state.in_progress = false;
  129. return false;
  130. }
  131. return true;
  132. }