process_ucis.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. if (!qk_ucis_state.in_progress)
  83. return true;
  84. if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH &&
  85. !(keycode == KC_BSPC || keycode == KC_ESC || keycode == KC_SPC || keycode == KC_ENT)) {
  86. return false;
  87. }
  88. if (!record->event.pressed)
  89. return true;
  90. qk_ucis_state.codes[qk_ucis_state.count] = keycode;
  91. qk_ucis_state.count++;
  92. if (keycode == KC_BSPC) {
  93. if (qk_ucis_state.count >= 2) {
  94. qk_ucis_state.count -= 2;
  95. return true;
  96. } else {
  97. qk_ucis_state.count--;
  98. return false;
  99. }
  100. }
  101. if (keycode == KC_ENT || keycode == KC_SPC || keycode == KC_ESC) {
  102. bool symbol_found = false;
  103. for (i = qk_ucis_state.count; i > 0; i--) {
  104. register_code (KC_BSPC);
  105. unregister_code (KC_BSPC);
  106. wait_ms(UNICODE_TYPE_DELAY);
  107. }
  108. if (keycode == KC_ESC) {
  109. qk_ucis_state.in_progress = false;
  110. return false;
  111. }
  112. unicode_input_start();
  113. for (i = 0; ucis_symbol_table[i].symbol; i++) {
  114. if (is_uni_seq (ucis_symbol_table[i].symbol)) {
  115. symbol_found = true;
  116. register_ucis(ucis_symbol_table[i].code + 2);
  117. break;
  118. }
  119. }
  120. if (!symbol_found) {
  121. qk_ucis_symbol_fallback();
  122. }
  123. unicode_input_finish();
  124. if (symbol_found) {
  125. qk_ucis_success(i);
  126. }
  127. qk_ucis_state.in_progress = false;
  128. return false;
  129. }
  130. return true;
  131. }