You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.8 KiB

  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 keycode;
  33. if ('1' <= seq[i] && seq[i] <= '0') {
  34. keycode = seq[i] - '1' + KC_1;
  35. } else {
  36. keycode = seq[i] - 'a' + KC_A;
  37. }
  38. if (i > qk_ucis_state.count || qk_ucis_state.codes[i] != keycode) {
  39. return false;
  40. }
  41. }
  42. return qk_ucis_state.codes[i] == KC_ENTER || qk_ucis_state.codes[i] == KC_SPACE;
  43. }
  44. __attribute__((weak)) void qk_ucis_symbol_fallback(void) {
  45. for (uint8_t i = 0; i < qk_ucis_state.count - 1; i++) {
  46. uint8_t keycode = qk_ucis_state.codes[i];
  47. register_code(keycode);
  48. unregister_code(keycode);
  49. wait_ms(UNICODE_TYPE_DELAY);
  50. }
  51. }
  52. __attribute__((weak)) void qk_ucis_cancel(void) {}
  53. void register_ucis(const uint32_t *code_points) {
  54. for (int i = 0; i < UCIS_MAX_CODE_POINTS && code_points[i]; i++) {
  55. register_unicode(code_points[i]);
  56. wait_ms(UNICODE_TYPE_DELAY);
  57. }
  58. }
  59. bool process_ucis(uint16_t keycode, keyrecord_t *record) {
  60. if (!qk_ucis_state.in_progress || !record->event.pressed) {
  61. return true;
  62. }
  63. bool special = keycode == KC_SPACE || keycode == KC_ENTER || keycode == KC_ESCAPE || keycode == KC_BACKSPACE;
  64. if (qk_ucis_state.count >= UCIS_MAX_SYMBOL_LENGTH && !special) {
  65. return false;
  66. }
  67. qk_ucis_state.codes[qk_ucis_state.count] = keycode;
  68. qk_ucis_state.count++;
  69. switch (keycode) {
  70. case KC_BACKSPACE:
  71. if (qk_ucis_state.count >= 2) {
  72. qk_ucis_state.count -= 2;
  73. return true;
  74. } else {
  75. qk_ucis_state.count--;
  76. return false;
  77. }
  78. case KC_SPACE:
  79. case KC_ENTER:
  80. case KC_ESCAPE:
  81. for (uint8_t i = 0; i < qk_ucis_state.count; i++) {
  82. register_code(KC_BACKSPACE);
  83. unregister_code(KC_BACKSPACE);
  84. wait_ms(UNICODE_TYPE_DELAY);
  85. }
  86. if (keycode == KC_ESCAPE) {
  87. qk_ucis_state.in_progress = false;
  88. qk_ucis_cancel();
  89. return false;
  90. }
  91. uint8_t i;
  92. bool symbol_found = false;
  93. for (i = 0; ucis_symbol_table[i].symbol; i++) {
  94. if (is_uni_seq(ucis_symbol_table[i].symbol)) {
  95. symbol_found = true;
  96. register_ucis(ucis_symbol_table[i].code_points);
  97. break;
  98. }
  99. }
  100. if (symbol_found) {
  101. qk_ucis_success(i);
  102. } else {
  103. qk_ucis_symbol_fallback();
  104. }
  105. qk_ucis_state.in_progress = false;
  106. return false;
  107. default:
  108. return true;
  109. }
  110. }