process_steno.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* Copyright 2017 Joseph Wasson
  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 "eeprom.h"
  19. #include "keymap_steno.h"
  20. #include "virtser.h"
  21. #include <string.h>
  22. // TxBolt Codes
  23. #define TXB_NUL 0
  24. #define TXB_S_L 0b00000001
  25. #define TXB_T_L 0b00000010
  26. #define TXB_K_L 0b00000100
  27. #define TXB_P_L 0b00001000
  28. #define TXB_W_L 0b00010000
  29. #define TXB_H_L 0b00100000
  30. #define TXB_R_L 0b01000001
  31. #define TXB_A_L 0b01000010
  32. #define TXB_O_L 0b01000100
  33. #define TXB_STR 0b01001000
  34. #define TXB_E_R 0b01010000
  35. #define TXB_U_R 0b01100000
  36. #define TXB_F_R 0b10000001
  37. #define TXB_R_R 0b10000010
  38. #define TXB_P_R 0b10000100
  39. #define TXB_B_R 0b10001000
  40. #define TXB_L_R 0b10010000
  41. #define TXB_G_R 0b10100000
  42. #define TXB_T_R 0b11000001
  43. #define TXB_S_R 0b11000010
  44. #define TXB_D_R 0b11000100
  45. #define TXB_Z_R 0b11001000
  46. #define TXB_NUM 0b11010000
  47. #define TXB_GRP0 0b00000000
  48. #define TXB_GRP1 0b01000000
  49. #define TXB_GRP2 0b10000000
  50. #define TXB_GRP3 0b11000000
  51. #define TXB_GRPMASK 0b11000000
  52. #define TXB_GET_GROUP(code) ((code & TXB_GRPMASK) >> 6)
  53. #define BOLT_STATE_SIZE 4
  54. #define GEMINI_STATE_SIZE 6
  55. #define MAX_STATE_SIZE GEMINI_STATE_SIZE
  56. static uint8_t state[MAX_STATE_SIZE] = {0};
  57. static uint8_t chord[MAX_STATE_SIZE] = {0};
  58. static int8_t pressed = 0;
  59. static steno_mode_t mode;
  60. 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};
  61. #ifdef STENO_COMBINEDMAP
  62. /* Used to look up when pressing the middle row key to combine two consonant or vowel keys */
  63. 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};
  64. 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};
  65. #endif
  66. static void steno_clear_state(void) {
  67. memset(state, 0, sizeof(state));
  68. memset(chord, 0, sizeof(chord));
  69. }
  70. static void send_steno_state(uint8_t size, bool send_empty) {
  71. for (uint8_t i = 0; i < size; ++i) {
  72. if (chord[i] || send_empty) {
  73. #ifdef VIRTSER_ENABLE
  74. virtser_send(chord[i]);
  75. #endif
  76. }
  77. }
  78. }
  79. void steno_init() {
  80. if (!eeconfig_is_enabled()) {
  81. eeconfig_init();
  82. }
  83. mode = eeprom_read_byte(EECONFIG_STENOMODE);
  84. }
  85. void steno_set_mode(steno_mode_t new_mode) {
  86. steno_clear_state();
  87. mode = new_mode;
  88. eeprom_update_byte(EECONFIG_STENOMODE, mode);
  89. }
  90. /* override to intercept chords right before they get sent.
  91. * return zero to suppress normal sending behavior.
  92. */
  93. __attribute__((weak)) bool send_steno_chord_user(steno_mode_t mode, uint8_t chord[6]) {
  94. return true;
  95. }
  96. __attribute__((weak)) bool postprocess_steno_user(uint16_t keycode, keyrecord_t *record, steno_mode_t mode, uint8_t chord[6], int8_t pressed) {
  97. return true;
  98. }
  99. __attribute__((weak)) bool process_steno_user(uint16_t keycode, keyrecord_t *record) {
  100. return true;
  101. }
  102. static void send_steno_chord(void) {
  103. if (send_steno_chord_user(mode, chord)) {
  104. switch (mode) {
  105. case STENO_MODE_BOLT:
  106. send_steno_state(BOLT_STATE_SIZE, false);
  107. #ifdef VIRTSER_ENABLE
  108. virtser_send(0); // terminating byte
  109. #endif
  110. break;
  111. case STENO_MODE_GEMINI:
  112. chord[0] |= 0x80; // Indicate start of packet
  113. send_steno_state(GEMINI_STATE_SIZE, true);
  114. break;
  115. }
  116. }
  117. steno_clear_state();
  118. }
  119. uint8_t *steno_get_state(void) {
  120. return &state[0];
  121. }
  122. uint8_t *steno_get_chord(void) {
  123. return &chord[0];
  124. }
  125. static bool update_state_bolt(uint8_t key, bool press) {
  126. uint8_t boltcode = pgm_read_byte(boltmap + key);
  127. if (press) {
  128. state[TXB_GET_GROUP(boltcode)] |= boltcode;
  129. chord[TXB_GET_GROUP(boltcode)] |= boltcode;
  130. } else {
  131. state[TXB_GET_GROUP(boltcode)] &= ~boltcode;
  132. }
  133. return false;
  134. }
  135. static bool update_state_gemini(uint8_t key, bool press) {
  136. int idx = key / 7;
  137. uint8_t bit = 1 << (6 - (key % 7));
  138. if (press) {
  139. state[idx] |= bit;
  140. chord[idx] |= bit;
  141. } else {
  142. state[idx] &= ~bit;
  143. }
  144. return false;
  145. }
  146. bool process_steno(uint16_t keycode, keyrecord_t *record) {
  147. switch (keycode) {
  148. case QK_STENO_BOLT:
  149. if (!process_steno_user(keycode, record)) {
  150. return false;
  151. }
  152. if (IS_PRESSED(record->event)) {
  153. steno_set_mode(STENO_MODE_BOLT);
  154. }
  155. return false;
  156. case QK_STENO_GEMINI:
  157. if (!process_steno_user(keycode, record)) {
  158. return false;
  159. }
  160. if (IS_PRESSED(record->event)) {
  161. steno_set_mode(STENO_MODE_GEMINI);
  162. }
  163. return false;
  164. #ifdef STENO_COMBINEDMAP
  165. case QK_STENO_COMB ... QK_STENO_COMB_MAX: {
  166. uint8_t result;
  167. result = process_steno(combinedmap_first[keycode - QK_STENO_COMB], record);
  168. result &= process_steno(combinedmap_second[keycode - QK_STENO_COMB], record);
  169. return result;
  170. }
  171. #endif
  172. case STN__MIN ... STN__MAX:
  173. if (!process_steno_user(keycode, record)) {
  174. return false;
  175. }
  176. switch (mode) {
  177. case STENO_MODE_BOLT:
  178. update_state_bolt(keycode - QK_STENO, IS_PRESSED(record->event));
  179. break;
  180. case STENO_MODE_GEMINI:
  181. update_state_gemini(keycode - QK_STENO, IS_PRESSED(record->event));
  182. break;
  183. }
  184. // allow postprocessing hooks
  185. if (postprocess_steno_user(keycode, record, mode, chord, pressed)) {
  186. if (IS_PRESSED(record->event)) {
  187. ++pressed;
  188. } else {
  189. --pressed;
  190. if (pressed <= 0) {
  191. pressed = 0;
  192. send_steno_chord();
  193. }
  194. }
  195. }
  196. return false;
  197. }
  198. return true;
  199. }