Parent directory

process_steno.c

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