logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

ucis.c (1940B)


  1. // Copyright 2023 QMK
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "ucis.h"
  4. #include "unicode.h"
  5. #include "action.h"
  6. uint8_t count = 0;
  7. bool active = false;
  8. char input[UCIS_MAX_INPUT_LENGTH] = {0};
  9. void ucis_start(void) {
  10. count = 0;
  11. active = true;
  12. register_unicode(0x2328); // ⌨
  13. }
  14. bool ucis_active(void) {
  15. return active;
  16. }
  17. uint8_t ucis_count(void) {
  18. return count;
  19. }
  20. static char keycode_to_char(uint16_t keycode) {
  21. if (keycode >= KC_A && keycode <= KC_Z) {
  22. return 'a' + (keycode - KC_A);
  23. } else if (keycode >= KC_1 && keycode <= KC_9) {
  24. return '1' + (keycode - KC_1);
  25. } else if (keycode == KC_0) {
  26. return '0';
  27. }
  28. return 0;
  29. }
  30. bool ucis_add(uint16_t keycode) {
  31. char c = keycode_to_char(keycode);
  32. if (c) {
  33. input[count++] = c;
  34. return true;
  35. }
  36. return false;
  37. }
  38. bool ucis_remove_last(void) {
  39. if (count) {
  40. count--;
  41. return true;
  42. }
  43. return false;
  44. }
  45. static bool match_mnemonic(char *mnemonic) {
  46. for (uint8_t i = 0; input[i]; i++) {
  47. if (i > count || input[i] != mnemonic[i]) {
  48. return false;
  49. }
  50. }
  51. return true;
  52. }
  53. void ucis_finish(void) {
  54. uint8_t i = 0;
  55. bool found = false;
  56. for (; ucis_symbol_table[i].mnemonic; i++) {
  57. if (match_mnemonic(ucis_symbol_table[i].mnemonic)) {
  58. found = true;
  59. break;
  60. }
  61. }
  62. if (found) {
  63. for (uint8_t j = 0; j <= count; j++) {
  64. tap_code(KC_BACKSPACE);
  65. }
  66. register_ucis(i);
  67. }
  68. active = false;
  69. }
  70. void ucis_cancel(void) {
  71. count = 0;
  72. active = false;
  73. }
  74. void register_ucis(uint8_t index) {
  75. const uint32_t *code_points = ucis_symbol_table[index].code_points;
  76. for (int i = 0; i < UCIS_MAX_CODE_POINTS && code_points[i]; i++) {
  77. register_unicode(code_points[i]);
  78. }
  79. }