From 3d09a56aea5ce4dd99983f6e90f11a401652ae05 Mon Sep 17 00:00:00 2001 From: Nick Conway Date: Sat, 21 Jun 2025 12:24:56 -0400 Subject: [PATCH] feat(last-device): Update transport handling --- app/dts/behaviors/last_device.dtsi | 1 - app/include/zmk/endpoints.h | 2 ++ app/src/behaviors/behavior_last_device.c | 37 ++++++++++++------------ app/src/endpoints.c | 4 +++ 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/app/dts/behaviors/last_device.dtsi b/app/dts/behaviors/last_device.dtsi index 81447cb31..9402cf7dc 100644 --- a/app/dts/behaviors/last_device.dtsi +++ b/app/dts/behaviors/last_device.dtsi @@ -10,7 +10,6 @@ behaviors { /omit-if-no-ref/ last_dev: last_device { compatible = "zmk,behavior-last-device"; - label = "LAST_DEVICE"; #binding-cells = <0>; }; }; diff --git a/app/include/zmk/endpoints.h b/app/include/zmk/endpoints.h index e20d19160..4e151e0ae 100644 --- a/app/include/zmk/endpoints.h +++ b/app/include/zmk/endpoints.h @@ -63,6 +63,8 @@ int zmk_endpoint_instance_to_index(struct zmk_endpoint_instance endpoint); int zmk_endpoints_select_transport(enum zmk_transport transport); int zmk_endpoints_toggle_transport(void); +int zmk_preferred_transport(); + /** * Gets the currently-selected endpoint. */ diff --git a/app/src/behaviors/behavior_last_device.c b/app/src/behaviors/behavior_last_device.c index 73e249b41..a18ae7486 100644 --- a/app/src/behaviors/behavior_last_device.c +++ b/app/src/behaviors/behavior_last_device.c @@ -17,26 +17,27 @@ #include #include #include -#include +#include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #define DT_DRV_COMPAT zmk_behavior_last_device -int8_t last_device; -bool skip_next_endpoint_change = false; +static int8_t last_device; +static bool skip_next_endpoint_change = false; static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event) { + LOG_DBG("Last device pressed %d", last_device); if (last_device == -1) { LOG_DBG("Toggling output"); - zmk_endpoints_toggle(); + zmk_endpoints_toggle_transport(); } else { LOG_DBG("Switching to last ble device: %d", last_device); zmk_ble_prof_select(last_device); - if (zmk_endpoints_selected() == ZMK_ENDPOINT_USB) { + if (zmk_endpoints_selected().transport == ZMK_TRANSPORT_USB) { LOG_DBG("Toggling output"); - zmk_endpoints_toggle(); + zmk_endpoints_toggle_transport(); } } @@ -50,17 +51,15 @@ static const struct behavior_driver_api behavior_last_device_driver_api = { }; static void update_last_device_ble_index(uint8_t profile) { - if (!zmk_ble_profile_is_open(profile)) { - last_device = profile; - LOG_DBG("Last device set to %d", last_device); - } + last_device = profile; + LOG_DBG("Last device set to %d", last_device); }; static int last_device_listener(const zmk_event_t *eh) { - if (as_zmk_endpoint_selection_changed(eh) != NULL) { - if (zmk_preferred_endpoint() == zmk_endpoints_selected()) { + if (as_zmk_endpoint_changed(eh) != NULL) { + if (zmk_preferred_transport() == zmk_endpoints_selected().transport) { if (!skip_next_endpoint_change) { - if (zmk_endpoints_selected() == ZMK_ENDPOINT_USB) { + if (zmk_endpoints_selected().transport == ZMK_TRANSPORT_USB) { update_last_device_ble_index(zmk_ble_active_profile_index()); } else { last_device = -1; @@ -69,8 +68,8 @@ static int last_device_listener(const zmk_event_t *eh) { } else { skip_next_endpoint_change = false; } - } else if (zmk_endpoints_selected() == ZMK_ENDPOINT_BLE && - zmk_preferred_endpoint() == ZMK_ENDPOINT_USB) { + } else if (zmk_endpoints_selected().transport == ZMK_TRANSPORT_BLE && + zmk_preferred_transport() == ZMK_TRANSPORT_USB) { LOG_DBG("USB disconnected"); update_last_device_ble_index(zmk_ble_last_profile_index()); } else { @@ -78,19 +77,21 @@ static int last_device_listener(const zmk_event_t *eh) { skip_next_endpoint_change = true; } } + if (as_zmk_ble_active_profile_changed(eh) != NULL && - zmk_endpoints_selected() == ZMK_ENDPOINT_BLE) { + zmk_endpoints_selected().transport == ZMK_TRANSPORT_BLE) { update_last_device_ble_index(zmk_ble_last_profile_index()); } + return 0; } ZMK_LISTENER(last_device_listener, last_device_listener); -ZMK_SUBSCRIPTION(last_device_listener, zmk_endpoint_selection_changed); +ZMK_SUBSCRIPTION(last_device_listener, zmk_endpoint_changed); ZMK_SUBSCRIPTION(last_device_listener, zmk_ble_active_profile_changed); #define LAST_DEVICE_INST(n) \ - DEVICE_DT_INST_DEFINE(n, behavior_last_device_init, NULL, NULL, NULL, APPLICATION, \ + BEHAVIOR_DT_INST_DEFINE(n, behavior_last_device_init, NULL, NULL, NULL, POST_KERNEL, \ CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_last_device_driver_api); DT_INST_FOREACH_STATUS_OKAY(LAST_DEVICE_INST) diff --git a/app/src/endpoints.c b/app/src/endpoints.c index cfb28b64d..b5bff8b9b 100644 --- a/app/src/endpoints.c +++ b/app/src/endpoints.c @@ -31,6 +31,10 @@ static struct zmk_endpoint_instance last_instance = {}; static enum zmk_transport preferred_transport = ZMK_TRANSPORT_USB; /* Used if multiple endpoints are ready */ +int zmk_preferred_transport() { + return preferred_transport; +} + static void update_current_endpoint(void); #if IS_ENABLED(CONFIG_SETTINGS)