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 +++++++++++++++++++++++++++++++++ users/cameronjlarsen/features/oneshot.h | 41 ++++++++++++++++++++ users/cameronjlarsen/rules.mk | 5 +++ 3 files changed, 112 insertions(+) create mode 100644 users/cameronjlarsen/features/oneshot.c create mode 100644 users/cameronjlarsen/features/oneshot.h create mode 100644 users/cameronjlarsen/rules.mk (limited to 'users') 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; + } + } + } + } +} diff --git a/users/cameronjlarsen/features/oneshot.h b/users/cameronjlarsen/features/oneshot.h new file mode 100644 index 0000000000..7fac7b5d58 --- /dev/null +++ b/users/cameronjlarsen/features/oneshot.h @@ -0,0 +1,41 @@ +// The GPLv2 License (GPLv2) +// +// Copyright (c) 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 . +#pragma once + +#include QMK_KEYBOARD_H + +// Represents the four states a oneshot key can be in +typedef enum { + os_up_unqueued, + os_up_queued, + os_down_unused, + os_down_used, +} oneshot_state; + +// Custom oneshot mod implementation that doesn't rely on timers. If a mod is +// used while it is held it will be unregistered on keyup as normal, otherwise +// it will be queued and only released after the next non-mod keyup. +void update_oneshot(oneshot_state *state, uint16_t mod, uint16_t trigger, uint16_t keycode, keyrecord_t *record); + +// To be implemented by the consumer. Defines keys to cancel oneshot mods. +bool is_oneshot_cancel_key(uint16_t keycode); + +// To be implemented by the consumer. Defines keys to ignore when determining +// whether a oneshot mod has been used. Setting this to modifiers and layer +// change keys allows stacking multiple oneshot modifiers, and carrying them +// between layers. +bool is_oneshot_ignored_key(uint16_t keycode); diff --git a/users/cameronjlarsen/rules.mk b/users/cameronjlarsen/rules.mk new file mode 100644 index 0000000000..59d14ca61f --- /dev/null +++ b/users/cameronjlarsen/rules.mk @@ -0,0 +1,5 @@ +CUSTOM_ONESHOT_ENABLE ?= no +ifeq ($(strip $(CUSTOM_ONESHOT_ENABLE)), yes) + SRC += $(USER_PATH)/features/oneshot.c + OPT_DEFS += -DCUSTOM_ONESHOT_ENABLE +endif -- cgit v1.2.3