process_ucis.c 4.0 KB

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