diff --git a/app/src/trackpad.c b/app/src/trackpad.c index dd25bc90bb2..3e81c969484 100644 --- a/app/src/trackpad.c +++ b/app/src/trackpad.c @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include "drivers/sensor/gen4.h" LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -32,7 +34,7 @@ static int8_t xDelta, yDelta, scrollDelta; static struct zmk_ptp_finger fingers[CONFIG_ZMK_TRACKPAD_MAX_FINGERS]; static const struct zmk_ptp_finger empty_finger = {0}; -static bool mouse_modes[ZMK_ENDPOINT_COUNT]; +static bool mouse_modes[ZMK_ENDPOINT_COUNT] = {true}; #if IS_ENABLED(CONFIG_ZMK_TRACKPAD_WORK_QUEUE_DEDICATED) K_THREAD_STACK_DEFINE(trackpad_work_stack_area, CONFIG_ZMK_TRACKPAD_DEDICATED_THREAD_STACK_SIZE); @@ -181,6 +183,16 @@ void zmk_trackpad_set_mouse_mode(bool mouse_mode) { } } +static void process_mode_report(struct k_work *_work) { + bool state = mouse_modes[zmk_endpoint_instance_to_index(zmk_endpoints_selected())]; + if (mousemode != state) { + zmk_trackpad_set_mouse_mode(state); + zmk_hid_ptp_set_feature_mode_report(state ? 0 : 3); + } +} + +static K_WORK_DEFINE(mode_changed_work, process_mode_report); + static int trackpad_init(void) { #if IS_ENABLED(CONFIG_ZMK_TRACKPAD_WORK_QUEUE_DEDICATED) @@ -190,28 +202,39 @@ static int trackpad_init(void) { #endif button_mode = true; surface_mode = true; - zmk_trackpad_set_mouse_mode(true); + k_work_submit(&mode_changed_work); enabled = true; return 0; } -static void process_mode_report(struct k_work *_work) { - bool state = mouse_modes[zmk_endpoint_instance_to_index(zmk_endpoints_selected())]; - if (mousemode != state) { - zmk_trackpad_set_mouse_mode(state); - zmk_hid_ptp_set_feature_mode_report(state ? 0 : 3); - } -} - -static K_WORK_DEFINE(mode_changed_work, process_mode_report); - static int trackpad_event_listener(const zmk_event_t *eh) { + // Reset to mouse mode on usb disconnection + if (as_zmk_usb_conn_state_changed(eh)) { + struct zmk_usb_conn_state_changed *usb_state = as_zmk_usb_conn_state_changed(eh); + if (usb_state->conn_state == ZMK_USB_CONN_NONE) { + struct zmk_endpoint_instance endpoint = { + .transport = ZMK_TRANSPORT_USB, + }; + mouse_modes[zmk_endpoint_instance_to_index(endpoint)] = true; + } + } + // reset to mouse mode on BLE profile disconnection or unpairing + if (as_zmk_ble_active_profile_changed(eh)) { + struct zmk_ble_active_profile_changed *ble_state = as_zmk_ble_active_profile_changed(eh); + if (ble_state->open || !ble_state->connected) { + struct zmk_endpoint_instance endpoint = {.transport = ZMK_TRANSPORT_BLE, + .ble = {.profile_index = ble_state->index}}; + mouse_modes[zmk_endpoint_instance_to_index(endpoint)] = true; + } + } k_work_submit(&mode_changed_work); return 0; } static ZMK_LISTENER(trackpad, trackpad_event_listener); static ZMK_SUBSCRIPTION(trackpad, zmk_endpoint_changed); +static ZMK_SUBSCRIPTION(trackpad, zmk_usb_conn_state_changed); +static ZMK_SUBSCRIPTION(trackpad, zmk_ble_active_profile_changed); void zmk_trackpad_set_mode_report(uint8_t *report, struct zmk_endpoint_instance endpoint) { int profile = zmk_endpoint_instance_to_index(endpoint);