oneshot.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* Copyright 2022 Cameron Larsen <camjlarsen@gmail.com>
  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 "oneshot.h"
  17. void update_oneshot(oneshot_state *state, uint16_t mod, uint16_t trigger, uint16_t keycode, keyrecord_t *record) {
  18. if (keycode == trigger) {
  19. if (record->event.pressed) {
  20. // Trigger keydown
  21. if (*state == os_up_unqueued) {
  22. register_code(mod);
  23. }
  24. *state = os_down_unused;
  25. } else {
  26. // Trigger keyup
  27. switch (*state) {
  28. case os_down_unused:
  29. // If we didn't use the mod while trigger was held, queue it.
  30. *state = os_up_queued;
  31. break;
  32. case os_down_used:
  33. // If we did use the mod while trigger was held, unregister it.
  34. *state = os_up_unqueued;
  35. unregister_code(mod);
  36. break;
  37. default:
  38. break;
  39. }
  40. }
  41. } else {
  42. if (record->event.pressed) {
  43. if (is_oneshot_cancel_key(keycode) && *state != os_up_unqueued) {
  44. // Cancel oneshot on designated cancel keydown.
  45. *state = os_up_unqueued;
  46. unregister_code(mod);
  47. }
  48. } else {
  49. if (!is_oneshot_ignored_key(keycode)) {
  50. // On non-ignored keyup, consider the oneshot used.
  51. switch (*state) {
  52. case os_down_unused:
  53. *state = os_down_used;
  54. break;
  55. case os_up_queued:
  56. *state = os_up_unqueued;
  57. unregister_code(mod);
  58. break;
  59. default:
  60. break;
  61. }
  62. }
  63. }
  64. }
  65. }