process_ucis.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include "unicode.h"
  18. #include "keycode.h"
  19. #include "wait.h"
  20. qk_ucis_state_t qk_ucis_state;
  21. void qk_ucis_start(void) {
  22. qk_ucis_state.count = 0;
  23. qk_ucis_state.in_progress = true;
  24. qk_ucis_start_user();
  25. }
  26. __attribute__((weak)) void qk_ucis_start_user(void) {
  27. register_unicode(0x2328); // ⌨
  28. }
  29. __attribute__((weak)) void qk_ucis_success(uint8_t symbol_index) {}
  30. static bool is_uni_seq(char *seq) {
  31. uint8_t i;
  32. for (i = 0; seq[i]; i++) {
  33. uint16_t keycode;
  34. if ('1' <= seq[i] && seq[i] <= '0') {
  35. keycode = seq[i] - '1' + KC_1;
  36. } else {
  37. keycode = seq[i] - 'a' + KC_A;
  38. }
  39. if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != keycode) {
  40. return false;
  41. }
  42. }
  43. return qk_ucis_state.codes[i] == KC_ENTER || qk_ucis_state.codes[i] == KC_SPACE;
  44. }
  45. __attribute__((weak)) void qk_ucis_symbol_fallback(void) {
  46. for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
  47. tap_code(qk_ucis_state.codes[i]);
  48. }
  49. }
  50. __attribute__((weak)) void qk_ucis_cancel(void) {}
  51. void register_ucis(const uint32_t *code_points) {
  52. for (int i = 0; i < UCIS_MAX_CODE_POINTS && code_points[i]; i++) {
  53. register_unicode(code_points[i]);
  54. }
  55. }
  56. bool process_ucis(uint16_t keycode, keyrecord_t *record) {
  57. if (!qk_ucis_state.in_progress || !record->event.pressed) {
  58. return true;
  59. }
  60. bool special = keycode == KC_SPACE || keycode == KC_ENTER || keycode == KC_ESCAPE || keycode == KC_BACKSPACE;
  61. if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH && !special) {
  62. return false;
  63. }
  64. qk_ucis_state.codes[qk_ucis_state.count] = keycode;
  65. qk_ucis_state.count++;
  66. switch (keycode) {
  67. case KC_BACKSPACE:
  68. if (qk_ucis_state.count >= 2) {
  69. qk_ucis_state.count -= 2;
  70. return true;
  71. } else {
  72. qk_ucis_state.count--;
  73. return false;
  74. }
  75. case KC_SPACE:
  76. case KC_ENTER:
  77. case KC_ESCAPE:
  78. for (uint8_t i = 0; i < qk_ucis_state.count; i++) {
  79. tap_code(KC_BACKSPACE);
  80. }
  81. if (keycode == KC_ESCAPE) {
  82. qk_ucis_state.in_progress = false;
  83. qk_ucis_cancel();
  84. return false;
  85. }
  86. uint8_t i;
  87. bool symbol_found = false;
  88. for (i = 0; ucis_symbol_table[i].symbol; i++) {
  89. if (is_uni_seq(ucis_symbol_table[i].symbol)) {
  90. symbol_found = true;
  91. register_ucis(ucis_symbol_table[i].code_points);
  92. break;
  93. }
  94. }
  95. if (symbol_found) {
  96. qk_ucis_success(i);
  97. } else {
  98. qk_ucis_symbol_fallback();
  99. }
  100. qk_ucis_state.in_progress = false;
  101. return false;
  102. default:
  103. return true;
  104. }
  105. }