diff --git a/app/src/behaviors/behavior_tap_dance.c b/app/src/behaviors/behavior_tap_dance.c index df651e0b..ad8d7f21 100644 --- a/app/src/behaviors/behavior_tap_dance.c +++ b/app/src/behaviors/behavior_tap_dance.c @@ -20,58 +20,123 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) -static bool tap_dance_started = false; -static int tap_counter = 0; -int64_t timestamp; +#define ZMK_BHV_TAP_DANCE_POSITION_FREE ULONG_MAX struct behavior_tap_dance_config { - int tapping_term_ms; + uint32_t tapping_term_ms; + struct zmk_behavior_binding behavior; }; +struct active_tap_dance { + // tap dance data + int tap_counter; + uint32_t position; + const struct behavior_tap_dance_config *config; + // timer data + bool timer_started; + bool timer_cancelled; + int64_t release_at; + struct k_delayed_work release_timer; +}; + +struct active_tap_dance the_tap_dance; + +static struct active_tap_dance *store_tap_dance(uint32_t position, + const struct behavior_tap_dance_config *config) { + struct active_tap_dance *const tap_dance = &the_tap_dance; + tap_dance->tap_counter = 0; + tap_dance->position = position; + tap_dance->config = config; + tap_dance->release_at = 0; + tap_dance->timer_cancelled = false; + tap_dance->timer_started = false; + return tap_dance; + return NULL; +} + + +static void clear_tap_dance(struct active_tap_dance *tap_dance) { + LOG_DBG("Clearing Tap Dance"); + tap_dance-> position = ZMK_BHV_TAP_DANCE_POSITION_FREE; +} + +static inline int press_tap_dance_behavior(struct active_tap_dance *tap_dance, + int64_t timestamp) { + LOG_DBG("Press Tap Dance Behavior"); + struct zmk_behavior_binding binding = { + .behavior_dev = tap_dance->config->behavior.behavior_dev, + }; + struct zmk_behavior_binding_event event = { + .position = tap_dance->position, + .timestamp = timestamp, + }; + return 0; + //return behavior_keymap_binding_pressed(&binding, event); +} + +static inline int release_tap_dance_behavior(struct active_tap_dance *tap_dance, + int64_t timestamp) { + LOG_DBG("Release Tap Dance Behavior"); + struct zmk_behavior_binding binding = { + .behavior_dev = tap_dance->config->behavior.behavior_dev, + }; + struct zmk_behavior_binding_event event = { + .position = tap_dance->position, + .timestamp = timestamp, + }; + + clear_tap_dance(tap_dance); + return 0; + //return behavior_keymap_binding_released(&binding, event); +} + +static int stop_timer(struct active_tap_dance *tap_dance) { + LOG_DBG("Stop Timer"); + int timer_cancel_result = k_delayed_work_cancel(&tap_dance->release_timer); + if (timer_cancel_result == -EINPROGRESS) { + tap_dance->timer_cancelled = true; + } + return timer_cancel_result; +} + static int on_tap_dance_binding_pressed(struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event) { + LOG_DBG("On Binding Pressed"); const struct device *dev = device_get_binding(binding->behavior_dev); const struct behavior_tap_dance_config *cfg = dev->config; - if (!tap_dance_started) { - tap_dance_started = true; - timestamp = k_uptime_get(); - LOG_DBG("timestamp at: %lld", timestamp); + struct active_tap_dance *tap_dance = &the_tap_dance; + if (tap_dance != NULL) { + stop_timer(tap_dance); + release_tap_dance_behavior(tap_dance, event.timestamp); } - else { - if (k_uptime_get() <= (timestamp + cfg->tapping_term_ms)){ - timestamp = k_uptime_get(); - LOG_DBG("Updated timestamp: %lld", timestamp); - } + tap_dance = store_tap_dance(event.position, cfg); + if (tap_dance == NULL) { + LOG_ERR("unable to store tap dance"); + return ZMK_BEHAVIOR_OPAQUE; } - int32_t tapping_term_ms_left = (timestamp + cfg->tapping_term_ms) - k_uptime_get(); - LOG_DBG("time left: %d", tapping_term_ms_left); - if (tapping_term_ms_left <= 0) { - LOG_DBG("TIME'S UP"); - LOG_DBG("Counter reached on press: %d", tap_counter); - tap_dance_started = false; - tap_counter = 0; - } - else { - ++ tap_counter; - } - LOG_DBG("Counter beginning at: %d", tap_counter); - - - return ZMK_BEHAVIOR_OPAQUE; - - + press_tap_dance_behavior(tap_dance, event.timestamp); + LOG_DBG("%d new tap_dance", event.position); + return ZMK_BEHAVIOR_OPAQUE; } static int on_tap_dance_binding_released(struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event) { - const struct device *dev = device_get_binding(binding->behavior_dev); - const struct behavior_tap_dance_config *cfg = dev->config; - int32_t tapping_term_ms_left = (timestamp + cfg->tapping_term_ms) - k_uptime_get(); - if (tapping_term_ms_left <= 0) { - LOG_DBG("Counter reached on release: %d", tap_counter); - tap_dance_started = false; - tap_counter = 0; + LOG_DBG("On Binding Released"); + struct active_tap_dance *tap_dance = &the_tap_dance; + if (tap_dance == NULL) { + LOG_ERR("ACTIVE STICKY KEY CLEARED TOO EARLY"); + return ZMK_BEHAVIOR_OPAQUE; + } + + // No other key was pressed. Start the timer. + tap_dance->timer_started = true; + tap_dance->release_at = event.timestamp + tap_dance->config->tapping_term_ms; + // adjust timer in case this behavior was queued by a hold-tap + int32_t ms_left = tap_dance->release_at - k_uptime_get(); + LOG_DBG("ms_left equal to: %d", ms_left); + if (ms_left > 0) { + k_delayed_work_submit(&tap_dance->release_timer, K_MSEC(ms_left)); } return ZMK_BEHAVIOR_OPAQUE; } @@ -81,7 +146,32 @@ static const struct behavior_driver_api behavior_tap_dance_driver_api = { .binding_released = on_tap_dance_binding_released, }; -static int behavior_tap_dance_init(const struct device *dev) { return 0; }; +void behavior_tap_dance_timer_handler(struct k_work *item) { + LOG_DBG("Timer Handler Called"); + LOG_DBG("Counter reached: %d", the_tap_dance.tap_counter); + struct active_tap_dance *tap_dance = + CONTAINER_OF(item, struct active_tap_dance, release_timer); + if (tap_dance->position == ZMK_BHV_TAP_DANCE_POSITION_FREE) { + return; + } + if (tap_dance->timer_cancelled) { + tap_dance->timer_cancelled = false; + } else { + release_tap_dance_behavior(tap_dance, tap_dance->release_at); + } +} + +static int behavior_tap_dance_init(const struct device *dev) { + static bool init_first_run = true; + if (init_first_run) { + k_delayed_work_init(&the_tap_dance.release_timer, + behavior_tap_dance_timer_handler); + LOG_DBG("Hello World"); + the_tap_dance.position = ZMK_BHV_TAP_DANCE_POSITION_FREE; + } + init_first_run = false; + return 0; +} struct behavior_tap_dance_data {}; static struct behavior_tap_dance_data behavior_tap_dance_data;