summaryrefslogtreecommitdiffstats
path: root/users/ericgebhart/extensions/swapper.c
blob: 988a99e602503c7013685f8d2c917408dbca674d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
  Copyright 2022 Eric Gebhart <e.a.gebhart@gmail.com, @possumvibes,@Callum.

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
// Derived from swapper by @Possumvibes and @Callum

#include "swapper.h"

swapper_state_t swapper_states[] = {
#ifdef SWAPPER_ENABLE
#include "swapper.def"
#endif
};
uint8_t         NUM_SWAPPER_STATES = sizeof(swapper_states) / sizeof(swapper_state_t);

// Based on https://github.com/callum-oakley/qmk_firmware/tree/master/users/callum
void process_swappers(uint16_t keycode, keyrecord_t *record) {
#ifdef SWAPPER_ENABLE
  swapper_state_t *curr_state = NULL;
  for (int i = 0; i < NUM_SWAPPER_STATES; ++i) {
    curr_state = &swapper_states[i];

    if (keycode == curr_state->forward_trigger) {
      if (record->event.pressed) {
        if (!curr_state->active) {
          curr_state->active = true;
          register_code16(curr_state->mod);
        }
        register_code16(curr_state->forward);
      } else {
        unregister_code16(curr_state->forward);
        // Don't unregister curr_state->mod until some other key is hit or released.
      }
    } else if (curr_state->active && keycode == curr_state->reverse_trigger) {
      if (record->event.pressed) {
        register_code16(curr_state->reverse);
      } else {
        unregister_code16(curr_state->reverse);
      }
    } else if (curr_state->active) {
      unregister_code16(curr_state->mod);
      curr_state->active = false;
    }
  }
#endif
}