process_steno.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2017, 2022 Joseph Wasson, Vladislav Kucheriavykh
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "process_steno.h"
  4. #include "steno.h"
  5. #include "progmem.h"
  6. #ifdef STENO_ENABLE_BOLT
  7. void steno_add_key_to_chord_bolt(uint8_t chord[MAX_STROKE_SIZE], uint8_t key);
  8. void steno_send_chord_bolt(uint8_t chord[MAX_STROKE_SIZE]);
  9. #endif
  10. #ifdef STENO_ENABLE_GEMINI
  11. void steno_add_key_to_chord_gemini(uint8_t chord[MAX_STROKE_SIZE], uint8_t key);
  12. void steno_send_chord_gemini(uint8_t chord[MAX_STROKE_SIZE]);
  13. #endif
  14. // All steno keys that have been pressed to form this chord,
  15. // stored in MAX_STROKE_SIZE groups of 8-bit arrays.
  16. extern uint8_t steno_current_chord[MAX_STROKE_SIZE];
  17. // The number of physical keys actually being held down.
  18. // This is not always equal to the number of 1 bits in `chord` because it is possible to
  19. // simultaneously press down four keys, then release three of those four keys and then press yet
  20. // another key while the fourth finger is still holding down its key.
  21. // At the end of this scenario given as an example, `chord` would have five bits set to 1 but
  22. // `n_pressed_keys` would be set to 2 because there are only two keys currently being pressed down.
  23. static int8_t n_pressed_keys = 0;
  24. #ifdef STENO_COMBINEDMAP
  25. // Used to look up when pressing the middle row key to combine two consonant or vowel keys
  26. static const uint16_t combinedmap_first[] PROGMEM = {QK_STENO_S1, QK_STENO_TL, QK_STENO_PL, QK_STENO_HL, QK_STENO_FR, QK_STENO_PR, QK_STENO_LR, QK_STENO_TR, QK_STENO_DR, QK_STENO_A, QK_STENO_E};
  27. static const uint16_t combinedmap_second[] PROGMEM = {QK_STENO_S2, QK_STENO_KL, QK_STENO_WL, QK_STENO_RL, QK_STENO_RR, QK_STENO_BR, QK_STENO_GR, QK_STENO_SR, QK_STENO_ZR, QK_STENO_O, QK_STENO_U};
  28. static bool process_steno_combinedmap(uint16_t keycode, keyrecord_t *record) {
  29. uint16_t first_keycode = pgm_read_word(&combinedmap_first[keycode - QK_STENO_S3]);
  30. uint16_t second_keycode = pgm_read_word(&combinedmap_second[keycode - QK_STENO_S3]);
  31. bool first_result = process_steno(first_keycode, record);
  32. bool second_result = process_steno(second_keycode, record);
  33. return first_result && second_result;
  34. }
  35. #endif
  36. /* override to intercept chords right before they get sent.
  37. * return zero to suppress normal sending behavior.
  38. */
  39. __attribute__((weak)) bool send_steno_chord_user(steno_mode_t mode, uint8_t chord[MAX_STROKE_SIZE]) {
  40. return true;
  41. }
  42. __attribute__((weak)) bool post_process_steno_user(uint16_t keycode, keyrecord_t *record, steno_mode_t mode, uint8_t chord[MAX_STROKE_SIZE], int8_t n_pressed_keys) {
  43. return true;
  44. }
  45. __attribute__((weak)) bool process_steno_user(uint16_t keycode, keyrecord_t *record) {
  46. return true;
  47. }
  48. static void steno_add_keycode_to_chord(uint16_t keycode) {
  49. switch (steno_get_mode()) {
  50. #ifdef STENO_ENABLE_BOLT
  51. case STENO_MODE_BOLT:
  52. steno_add_key_to_chord_bolt(steno_current_chord, keycode - QK_STENO_FUNCTION);
  53. break;
  54. #endif
  55. #ifdef STENO_ENABLE_GEMINI
  56. case STENO_MODE_GEMINI:
  57. steno_add_key_to_chord_gemini(steno_current_chord, keycode - QK_STENO_FUNCTION);
  58. break;
  59. #endif
  60. default:
  61. break;
  62. }
  63. }
  64. static void steno_send_chord(void) {
  65. switch (steno_get_mode()) {
  66. #ifdef STENO_ENABLE_BOLT
  67. case STENO_MODE_BOLT:
  68. steno_send_chord_bolt(steno_current_chord);
  69. break;
  70. #endif
  71. #ifdef STENO_ENABLE_GEMINI
  72. case STENO_MODE_GEMINI:
  73. steno_send_chord_gemini(steno_current_chord);
  74. break;
  75. #endif
  76. default:
  77. break;
  78. }
  79. }
  80. bool process_steno(uint16_t keycode, keyrecord_t *record) {
  81. if (!IS_STENO_KEYCODE(keycode)) {
  82. // Clearing or sending the chord state is not necessary as we intentionally ignore whatever
  83. // normal keyboard keys the user may have tapped while chording steno keys.
  84. return true; // pass keycode further along the chain
  85. }
  86. if (IS_NOEVENT(record->event)) {
  87. return true;
  88. }
  89. if (!process_steno_user(keycode, record)) {
  90. return false; // User fully processed the steno key themselves
  91. }
  92. switch (keycode) {
  93. #if NUM_STENO_PROTOCOLS > 1
  94. case QK_STENO_MODE_NEXT:
  95. if (record->event.pressed) {
  96. steno_mode_next();
  97. }
  98. return false;
  99. case QK_STENO_MODE_PREVIOUS:
  100. if (record->event.pressed) {
  101. steno_mode_previous();
  102. }
  103. return false;
  104. case QK_STENO_MODE_BOLT:
  105. if (record->event.pressed) {
  106. steno_set_mode(STENO_MODE_BOLT);
  107. }
  108. return false;
  109. case QK_STENO_MODE_GEMINI:
  110. if (record->event.pressed) {
  111. steno_set_mode(STENO_MODE_GEMINI);
  112. }
  113. return false;
  114. #endif
  115. #ifdef STENO_COMBINEDMAP
  116. case QK_STENO_S3 ... QK_STENO_EU:
  117. return process_steno_combinedmap(keycode, record);
  118. #endif
  119. case QK_STENO_FUNCTION ... QK_STENO_ZR:
  120. if (record->event.pressed) {
  121. n_pressed_keys++;
  122. steno_add_keycode_to_chord(keycode);
  123. if (!post_process_steno_user(keycode, record, steno_get_mode(), steno_current_chord, n_pressed_keys)) {
  124. return false;
  125. }
  126. } else { // is released
  127. n_pressed_keys--;
  128. if (!post_process_steno_user(keycode, record, steno_get_mode(), steno_current_chord, n_pressed_keys)) {
  129. return false;
  130. }
  131. if (n_pressed_keys > 0) {
  132. // User hasn't released all keys yet,
  133. // so the chord cannot be sent
  134. return false;
  135. }
  136. n_pressed_keys = 0;
  137. if (!send_steno_chord_user(steno_get_mode(), steno_current_chord)) {
  138. steno_clear_chord();
  139. return false;
  140. }
  141. steno_send_chord();
  142. steno_clear_chord();
  143. }
  144. return false;
  145. default:
  146. break;
  147. }
  148. return false;
  149. }