From 246cf99b4b029b6d36e82a0c4c3aed11abb2ff5e Mon Sep 17 00:00:00 2001 From: Cameron Larsen <44081018+cameronjlarsen@users.noreply.github.com> Date: Mon, 3 Apr 2023 15:43:12 -0700 Subject: Personal keymaps for crkbd and kyria (#20009) --- users/cameronjlarsen/features/oneshot.c | 66 +++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 users/cameronjlarsen/features/oneshot.c (limited to 'users/cameronjlarsen/features/oneshot.c') 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 + * + * 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 . + */ +#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; + } + } + } + } +} -- cgit v1.2.3