logo

qmk_firmware

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

process_steno.c (9447B)


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