feat(last-device): Update transport handling

This commit is contained in:
Nick Conway 2025-06-21 12:24:56 -04:00
parent 8ecdebfd68
commit 3d09a56aea
No known key found for this signature in database
GPG Key ID: AA850592E4C1D453
4 changed files with 25 additions and 19 deletions

View File

@ -10,7 +10,6 @@
behaviors {
/omit-if-no-ref/ last_dev: last_device {
compatible = "zmk,behavior-last-device";
label = "LAST_DEVICE";
#binding-cells = <0>;
};
};

View File

@ -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.
*/

View File

@ -17,26 +17,27 @@
#include <zmk/event_manager.h>
#include <zmk/events/ble_active_profile_changed.h>
#include <zmk/events/usb_conn_state_changed.h>
#include <zmk/events/endpoint_selection_changed.h>
#include <zmk/events/endpoint_changed.h>
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)

View File

@ -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)