swapper.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. Copyright 2022 Eric Gebhart <e.a.gebhart@gmail.com, @possumvibes,@Callum.
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // Derived from swapper by @Possumvibes and @Callum
  15. #include "swapper.h"
  16. swapper_state_t swapper_states[] = {
  17. #ifdef SWAPPER_ENABLE
  18. #include "swapper.def"
  19. #endif
  20. };
  21. uint8_t NUM_SWAPPER_STATES = sizeof(swapper_states) / sizeof(swapper_state_t);
  22. // Based on https://github.com/callum-oakley/qmk_firmware/tree/master/users/callum
  23. void process_swappers(uint16_t keycode, keyrecord_t *record) {
  24. #ifdef SWAPPER_ENABLE
  25. swapper_state_t *curr_state = NULL;
  26. for (int i = 0; i < NUM_SWAPPER_STATES; ++i) {
  27. curr_state = &swapper_states[i];
  28. if (keycode == curr_state->forward_trigger) {
  29. if (record->event.pressed) {
  30. if (!curr_state->active) {
  31. curr_state->active = true;
  32. register_code16(curr_state->mod);
  33. }
  34. register_code16(curr_state->forward);
  35. } else {
  36. unregister_code16(curr_state->forward);
  37. // Don't unregister curr_state->mod until some other key is hit or released.
  38. }
  39. } else if (curr_state->active && keycode == curr_state->reverse_trigger) {
  40. if (record->event.pressed) {
  41. register_code16(curr_state->reverse);
  42. } else {
  43. unregister_code16(curr_state->reverse);
  44. }
  45. } else if (curr_state->active) {
  46. unregister_code16(curr_state->mod);
  47. curr_state->active = false;
  48. }
  49. }
  50. #endif
  51. }