diff options
author | Cameron Larsen <44081018+cameronjlarsen@users.noreply.github.com> | 2023-04-03 15:43:12 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-04 08:43:12 +1000 |
commit | 246cf99b4b029b6d36e82a0c4c3aed11abb2ff5e (patch) | |
tree | 828e0c97813b18008c03fd5cb59a4f2f22badeff /users/cameronjlarsen/features/oneshot.c | |
parent | eb52984a94f70b95811fbc0853c158c68c40ea4f (diff) |
Personal keymaps for crkbd and kyria (#20009)
Diffstat (limited to 'users/cameronjlarsen/features/oneshot.c')
-rw-r--r-- | users/cameronjlarsen/features/oneshot.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/users/cameronjlarsen/features/oneshot.c b/users/cameronjlarsen/features/oneshot.c new file mode 100644 index 0000000000..799b7c1aa2 --- /dev/null +++ b/users/cameronjlarsen/features/oneshot.c @@ -0,0 +1,66 @@ +/* Copyright 2022 Cameron Larsen <camjlarsen@gmail.com> + * + * 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/>. + */ +#include "oneshot.h" + +void update_oneshot(oneshot_state *state, uint16_t mod, uint16_t trigger, uint16_t keycode, keyrecord_t *record) { + if (keycode == trigger) { + if (record->event.pressed) { + // Trigger keydown + if (*state == os_up_unqueued) { + register_code(mod); + } + *state = os_down_unused; + } else { + // Trigger keyup + switch (*state) { + case os_down_unused: + // If we didn't use the mod while trigger was held, queue it. + *state = os_up_queued; + break; + case os_down_used: + // If we did use the mod while trigger was held, unregister it. + *state = os_up_unqueued; + unregister_code(mod); + break; + default: + break; + } + } + } else { + if (record->event.pressed) { + if (is_oneshot_cancel_key(keycode) && *state != os_up_unqueued) { + // Cancel oneshot on designated cancel keydown. + *state = os_up_unqueued; + unregister_code(mod); + } + } else { + if (!is_oneshot_ignored_key(keycode)) { + // On non-ignored keyup, consider the oneshot used. + switch (*state) { + case os_down_unused: + *state = os_down_used; + break; + case os_up_queued: + *state = os_up_unqueued; + unregister_code(mod); + break; + default: + break; + } + } + } + } +} |