process_ucis.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. __attribute__((weak))
  56. void qk_ucis_cancel(void) {
  57. }
  58. void register_ucis(const char *hex) {
  59. for(int i = 0; hex[i]; i++) {
  60. uint8_t kc = 0;
  61. char c = hex[i];
  62. switch (c) {
  63. case '0':
  64. kc = KC_0;
  65. break;
  66. case '1' ... '9':
  67. kc = c - '1' + KC_1;
  68. break;
  69. case 'a' ... 'f':
  70. kc = c - 'a' + KC_A;
  71. break;
  72. case 'A' ... 'F':
  73. kc = c - 'A' + KC_A;
  74. break;
  75. }
  76. if (kc) {
  77. register_code (kc);
  78. unregister_code (kc);
  79. wait_ms (UNICODE_TYPE_DELAY);
  80. }
  81. }
  82. }
  83. bool process_ucis (uint16_t keycode, keyrecord_t *record) {
  84. uint8_t i;
  85. if (!qk_ucis_state.in_progress)
  86. return true;
  87. if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
  88. !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
  89. return false;
  90. }
  91. if (!record->event.pressed)
  92. return true;
  93. qk_ucis_state.codes[qk_ucis_state.count] = keycode;
  94. qk_ucis_state.count++;
  95. if (keycode == KC_BSPC) {
  96. if (qk_ucis_state.count >= 2) {
  97. qk_ucis_state.count -= 2;
  98. return true;
  99. } else {
  100. qk_ucis_state.count--;
  101. return false;
  102. }
  103. }
  104. if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
  105. bool symbol_found = false;
  106. for (i = qk_ucis_state.count; i > 0; i--) {
  107. register_code (KC_BSPC);
  108. unregister_code (KC_BSPC);
  109. wait_ms(UNICODE_TYPE_DELAY);
  110. }
  111. if (keycode == KC_ESC) {
  112. qk_ucis_state.in_progress = false;
  113. qk_ucis_cancel();
  114. return false;
  115. }
  116. unicode_input_start();
  117. for (i = 0; ucis_symbol_table[i].symbol; i++) {
  118. if (is_uni_seq (ucis_symbol_table[i].symbol)) {
  119. symbol_found = true;
  120. register_ucis(ucis_symbol_table[i].code + 2);
  121. break;
  122. }
  123. }
  124. if (!symbol_found) {
  125. qk_ucis_symbol_fallback();
  126. }
  127. unicode_input_finish();
  128. if (symbol_found) {
  129. qk_ucis_success(i);
  130. }
  131. qk_ucis_state.in_progress = false;
  132. return false;
  133. }
  134. return true;
  135. }