process_steno.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* Copyright 2017, 2022 Joseph Wasson, Vladislav Kucheriavykh
  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_steno.h"
  17. #include "quantum_keycodes.h"
  18. #include "keymap_steno.h"
  19. #include <string.h>
  20. #ifdef VIRTSER_ENABLE
  21. # include "virtser.h"
  22. #endif
  23. #ifdef STENO_ENABLE_ALL
  24. # include "eeprom.h"
  25. #endif
  26. // All steno keys that have been pressed to form this chord,
  27. // stored in MAX_STROKE_SIZE groups of 8-bit arrays.
  28. static uint8_t chord[MAX_STROKE_SIZE] = {0};
  29. // The number of physical keys actually being held down.
  30. // This is not always equal to the number of 1 bits in `chord` because it is possible to
  31. // simultaneously press down four keys, then release three of those four keys and then press yet
  32. // another key while the fourth finger is still holding down its key.
  33. // At the end of this scenario given as an example, `chord` would have five bits set to 1 but
  34. // `n_pressed_keys` would be set to 2 because there are only two keys currently being pressed down.
  35. static int8_t n_pressed_keys = 0;
  36. #ifdef STENO_ENABLE_ALL
  37. static steno_mode_t mode;
  38. #elif defined(STENO_ENABLE_GEMINI)
  39. static const steno_mode_t mode = STENO_MODE_GEMINI;
  40. #elif defined(STENO_ENABLE_BOLT)
  41. static const steno_mode_t mode = STENO_MODE_BOLT;
  42. #endif
  43. static inline void steno_clear_chord(void) {
  44. memset(chord, 0, sizeof(chord));
  45. }
  46. #ifdef STENO_ENABLE_GEMINI
  47. # ifdef VIRTSER_ENABLE
  48. void send_steno_chord_gemini(void) {
  49. // Set MSB to 1 to indicate the start of packet
  50. chord[0] |= 0x80;
  51. for (uint8_t i = 0; i < GEMINI_STROKE_SIZE; ++i) {
  52. virtser_send(chord[i]);
  53. }
  54. }
  55. # else
  56. # pragma message "VIRTSER_ENABLE = yes is required for Gemini PR to work properly out of the box!"
  57. # endif // VIRTSER_ENABLE
  58. /**
  59. * @precondition: `key` is pressed
  60. */
  61. bool add_gemini_key_to_chord(uint8_t key) {
  62. // Although each group of the packet is 8 bits long, the MSB is reserved
  63. // to indicate whether that byte is the first byte of the packet (MSB=1)
  64. // or one of the remaining five bytes of the packet (MSB=0).
  65. // As a consequence, only 7 out of the 8 bits are left to be used as a bit array
  66. // for the steno keys of that group.
  67. const int group_idx = key / 7;
  68. const int intra_group_idx = key - group_idx * 7;
  69. // The 0th steno key of the group has bit=0b01000000, the 1st has bit=0b00100000, etc.
  70. const uint8_t bit = 1 << (6 - intra_group_idx);
  71. chord[group_idx] |= bit;
  72. return false;
  73. }
  74. #endif // STENO_ENABLE_GEMINI
  75. #ifdef STENO_ENABLE_BOLT
  76. # define TXB_GRP0 0b00000000
  77. # define TXB_GRP1 0b01000000
  78. # define TXB_GRP2 0b10000000
  79. # define TXB_GRP3 0b11000000
  80. # define TXB_GRPMASK 0b11000000
  81. # define TXB_GET_GROUP(code) ((code & TXB_GRPMASK) >> 6)
  82. static const uint8_t boltmap[64] PROGMEM = {TXB_NUL, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_S_L, TXB_S_L, TXB_T_L, TXB_K_L, TXB_P_L, TXB_W_L, TXB_H_L, TXB_R_L, TXB_A_L, TXB_O_L, TXB_STR, TXB_STR, TXB_NUL, TXB_NUL, TXB_NUL, TXB_STR, TXB_STR, TXB_E_R, TXB_U_R, TXB_F_R, TXB_R_R, TXB_P_R, TXB_B_R, TXB_L_R, TXB_G_R, TXB_T_R, TXB_S_R, TXB_D_R, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_Z_R};
  83. # ifdef VIRTSER_ENABLE
  84. static void send_steno_chord_bolt(void) {
  85. for (uint8_t i = 0; i < BOLT_STROKE_SIZE; ++i) {
  86. // TX Bolt uses variable length packets where each byte corresponds to a bit array of certain keys.
  87. // If a user chorded the keys of the first group with keys of the last group, for example, there
  88. // would be bytes of 0x00 in `chord` for the middle groups which we mustn't send.
  89. if (chord[i]) {
  90. virtser_send(chord[i]);
  91. }
  92. }
  93. // Sending a null packet is not always necessary, but it is simpler and more reliable
  94. // to unconditionally send it every time instead of keeping track of more states and
  95. // creating more branches in the execution of the program.
  96. virtser_send(0);
  97. }
  98. # else
  99. # pragma message "VIRTSER_ENABLE = yes is required for TX Bolt to work properly out of the box!"
  100. # endif // VIRTSER_ENABLE
  101. /**
  102. * @precondition: `key` is pressed
  103. */
  104. static bool add_bolt_key_to_chord(uint8_t key) {
  105. uint8_t boltcode = pgm_read_byte(boltmap + key);
  106. chord[TXB_GET_GROUP(boltcode)] |= boltcode;
  107. return false;
  108. }
  109. #endif // STENO_ENABLE_BOLT
  110. #ifdef STENO_COMBINEDMAP
  111. /* Used to look up when pressing the middle row key to combine two consonant or vowel keys */
  112. static const uint16_t combinedmap_first[] PROGMEM = {STN_S1, STN_TL, STN_PL, STN_HL, STN_FR, STN_PR, STN_LR, STN_TR, STN_DR, STN_A, STN_E};
  113. static const uint16_t combinedmap_second[] PROGMEM = {STN_S2, STN_KL, STN_WL, STN_RL, STN_RR, STN_BR, STN_GR, STN_SR, STN_ZR, STN_O, STN_U};
  114. #endif
  115. #ifdef STENO_ENABLE_ALL
  116. void steno_init() {
  117. if (!eeconfig_is_enabled()) {
  118. eeconfig_init();
  119. }
  120. mode = eeprom_read_byte(EECONFIG_STENOMODE);
  121. }
  122. void steno_set_mode(steno_mode_t new_mode) {
  123. steno_clear_chord();
  124. mode = new_mode;
  125. eeprom_update_byte(EECONFIG_STENOMODE, mode);
  126. }
  127. #endif // STENO_ENABLE_ALL
  128. /* override to intercept chords right before they get sent.
  129. * return zero to suppress normal sending behavior.
  130. */
  131. __attribute__((weak)) bool send_steno_chord_user(steno_mode_t mode, uint8_t chord[MAX_STROKE_SIZE]) {
  132. return true;
  133. }
  134. __attribute__((weak)) bool postprocess_steno_user(uint16_t keycode, keyrecord_t *record, steno_mode_t mode, uint8_t chord[MAX_STROKE_SIZE], int8_t n_pressed_keys) {
  135. return true;
  136. }
  137. __attribute__((weak)) bool process_steno_user(uint16_t keycode, keyrecord_t *record) {
  138. return true;
  139. }
  140. bool process_steno(uint16_t keycode, keyrecord_t *record) {
  141. if (keycode < QK_STENO || keycode > QK_STENO_MAX) {
  142. return true; // Not a steno key, pass it further along the chain
  143. /*
  144. * Clearing or sending the chord state is not necessary as we intentionally ignore whatever
  145. * normal keyboard keys the user may have tapped while chording steno keys.
  146. */
  147. }
  148. if (IS_NOEVENT(record->event)) {
  149. return true;
  150. }
  151. if (!process_steno_user(keycode, record)) {
  152. return false; // User fully processed the steno key themselves
  153. }
  154. switch (keycode) {
  155. #ifdef STENO_ENABLE_ALL
  156. case QK_STENO_BOLT:
  157. if (IS_PRESSED(record->event)) {
  158. steno_set_mode(STENO_MODE_BOLT);
  159. }
  160. return false;
  161. case QK_STENO_GEMINI:
  162. if (IS_PRESSED(record->event)) {
  163. steno_set_mode(STENO_MODE_GEMINI);
  164. }
  165. return false;
  166. #endif // STENO_ENABLE_ALL
  167. #ifdef STENO_COMBINEDMAP
  168. case QK_STENO_COMB ... QK_STENO_COMB_MAX: {
  169. bool first_result = process_steno(combinedmap_first[keycode - QK_STENO_COMB], record);
  170. bool second_result = process_steno(combinedmap_second[keycode - QK_STENO_COMB], record);
  171. return first_result && second_result;
  172. }
  173. #endif // STENO_COMBINEDMAP
  174. case STN__MIN ... STN__MAX:
  175. if (IS_PRESSED(record->event)) {
  176. n_pressed_keys++;
  177. switch (mode) {
  178. #ifdef STENO_ENABLE_BOLT
  179. case STENO_MODE_BOLT:
  180. add_bolt_key_to_chord(keycode - QK_STENO);
  181. break;
  182. #endif // STENO_ENABLE_BOLT
  183. #ifdef STENO_ENABLE_GEMINI
  184. case STENO_MODE_GEMINI:
  185. add_gemini_key_to_chord(keycode - QK_STENO);
  186. break;
  187. #endif // STENO_ENABLE_GEMINI
  188. default:
  189. return false;
  190. }
  191. if (!postprocess_steno_user(keycode, record, mode, chord, n_pressed_keys)) {
  192. return false;
  193. }
  194. } else { // is released
  195. n_pressed_keys--;
  196. if (!postprocess_steno_user(keycode, record, mode, chord, n_pressed_keys)) {
  197. return false;
  198. }
  199. if (n_pressed_keys > 0) {
  200. // User hasn't released all keys yet,
  201. // so the chord cannot be sent
  202. return false;
  203. }
  204. n_pressed_keys = 0;
  205. if (!send_steno_chord_user(mode, chord)) {
  206. steno_clear_chord();
  207. return false;
  208. }
  209. switch (mode) {
  210. #if defined(STENO_ENABLE_BOLT) && defined(VIRTSER_ENABLE)
  211. case STENO_MODE_BOLT:
  212. send_steno_chord_bolt();
  213. break;
  214. #endif // STENO_ENABLE_BOLT && VIRTSER_ENABLE
  215. #if defined(STENO_ENABLE_GEMINI) && defined(VIRTSER_ENABLE)
  216. case STENO_MODE_GEMINI:
  217. send_steno_chord_gemini();
  218. break;
  219. #endif // STENO_ENABLE_GEMINI && VIRTSER_ENABLE
  220. default:
  221. break;
  222. }
  223. steno_clear_chord();
  224. }
  225. break;
  226. }
  227. return false;
  228. }