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.

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