From 2d0152fc9bf5de05fbcc4d5627a102e82f92a97a Mon Sep 17 00:00:00 2001 From: ReFil <31960031+ReFil@users.noreply.github.com> Date: Sun, 7 Apr 2024 19:41:48 +0100 Subject: [PATCH] Starting mode switching work --- app/include/zmk/trackpad.h | 5 ++++- app/src/hid.c | 13 +------------ app/src/hog.c | 14 +++++++++++++- app/src/trackpad.c | 33 ++++++++++++++++++++++++--------- app/src/usb_hid.c | 8 +++++++- 5 files changed, 49 insertions(+), 24 deletions(-) diff --git a/app/include/zmk/trackpad.h b/app/include/zmk/trackpad.h index 7d747d6c82d..175942a08dc 100644 --- a/app/include/zmk/trackpad.h +++ b/app/include/zmk/trackpad.h @@ -7,6 +7,7 @@ #pragma once #include +#include typedef uint8_t zmk_trackpad_finger_contacts_t; @@ -24,4 +25,6 @@ void zmk_trackpad_set_mouse_mode(bool mouse_mode); void zmk_trackpad_selective_set(uint8_t selective); -struct k_work_q *zmk_trackpad_work_q(); \ No newline at end of file +struct k_work_q *zmk_trackpad_work_q(); + +void zmk_trackpad_set_mode_report(uint8_t *report, struct zmk_endpoint_instance endpoint); \ No newline at end of file diff --git a/app/src/hid.c b/app/src/hid.c index bc4dabb6928..f9880a76766 100644 --- a/app/src/hid.c +++ b/app/src/hid.c @@ -548,20 +548,9 @@ struct zmk_hid_ptp_feature_mode_report *zmk_hid_ptp_get_feature_mode_report() { return &ptp_feature_mode_report; } -static void zmk_hid_trackpad_mouse_mode(struct k_work *work) { - if (ptp_feature_mode_report.mode == 3) - zmk_trackpad_set_mouse_mode(false); - else - zmk_trackpad_set_mouse_mode(true); -} - -K_WORK_DEFINE(mouse_mode_work, zmk_hid_trackpad_mouse_mode); - void zmk_hid_ptp_set_feature_mode_report(uint8_t mode) { - LOG_DBG("Setting mouse mode to: %d", mode); - + LOG_DBG("Setting mode report to: %d", mode); ptp_feature_mode_report.mode = mode; - k_work_submit(&mouse_mode_work); } struct zmk_hid_ptp_feature_certification_report *zmk_hid_ptp_get_feature_certification_report() { diff --git a/app/src/hog.c b/app/src/hog.c index 973ac3ccc42..11eb7ace8eb 100644 --- a/app/src/hog.c +++ b/app/src/hog.c @@ -21,6 +21,9 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #if IS_ENABLED(CONFIG_ZMK_HID_INDICATORS) #include #endif // IS_ENABLED(CONFIG_ZMK_HID_INDICATORS) +#if IS_ENABLED(CONFIG_ZMK_TRACKPAD) +#include +#endif enum { HIDS_REMOTE_WAKE = BIT(0), @@ -221,10 +224,19 @@ static ssize_t write_hids_trackpad_mode_feature_report(struct bt_conn *conn, return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN); } + int profile = zmk_ble_profile_index(bt_conn_get_dst(conn)); + if (profile < 0) { + return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY); + } + + struct zmk_endpoint_instance endpoint = {.transport = ZMK_TRANSPORT_BLE, + .ble = { + .profile_index = profile, + }}; uint8_t *report = (uint8_t *)buf; LOG_DBG("Mode report set ble %d", *report); - zmk_hid_ptp_set_feature_mode_report(*report); + zmk_trackpad_set_mode_report(report, endpoint); return len; } diff --git a/app/src/trackpad.c b/app/src/trackpad.c index 8228d98e6bb..dd25bc90bb2 100644 --- a/app/src/trackpad.c +++ b/app/src/trackpad.c @@ -2,8 +2,8 @@ #include #include -#include "zmk/mouse.h" -#include "zmk/trackpad.h" +#include +#include #include #include #include @@ -29,9 +29,11 @@ static bool enabled; static int8_t xDelta, yDelta, scrollDelta; -static struct zmk_ptp_finger fingers[5]; +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]; + #if IS_ENABLED(CONFIG_ZMK_TRACKPAD_WORK_QUEUE_DEDICATED) K_THREAD_STACK_DEFINE(trackpad_work_stack_area, CONFIG_ZMK_TRACKPAD_DEDICATED_THREAD_STACK_SIZE); static struct k_work_q trackpad_work_q; @@ -167,22 +169,19 @@ void zmk_trackpad_set_mouse_mode(bool mouse_mode) { mousemode = mouse_mode; sensor_attr_set(trackpad, SENSOR_CHAN_ALL, SENSOR_ATTR_CONFIGURATION, &attr); if (mouse_mode) { - // k_timer_stop(&trackpad_tick); - if (sensor_trigger_set(trackpad, &trigger, handle_mouse_mode) < 0) { LOG_ERR("can't set trigger mouse mode"); }; } else { zmk_hid_mouse_clear(); zmk_endpoints_send_mouse_report(); - // k_timer_start(&trackpad_tick, K_NO_WAIT, K_MSEC(CONFIG_ZMK_TRACKPAD_TICK_DURATION)); if (sensor_trigger_set(trackpad, &trigger, handle_trackpad_ptp) < 0) { LOG_ERR("can't set trigger"); }; } } -static int trackpad_init() { +static int trackpad_init(void) { #if IS_ENABLED(CONFIG_ZMK_TRACKPAD_WORK_QUEUE_DEDICATED) k_work_queue_start(&trackpad_work_q, trackpad_work_stack_area, @@ -196,13 +195,29 @@ static int trackpad_init() { 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) { - // zmk_trackpad_set_mouse_mode(true); - // zmk_hid_ptp_set_feature_mode_report(0); + k_work_submit(&mode_changed_work); return 0; } static ZMK_LISTENER(trackpad, trackpad_event_listener); static ZMK_SUBSCRIPTION(trackpad, zmk_endpoint_changed); +void zmk_trackpad_set_mode_report(uint8_t *report, struct zmk_endpoint_instance endpoint) { + int profile = zmk_endpoint_instance_to_index(endpoint); + LOG_DBG("Received report %d on endpoint %d", *report, profile); + mouse_modes[profile] = *report ? false : true; + k_work_submit(&mode_changed_work); +} + SYS_INIT(trackpad_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); \ No newline at end of file diff --git a/app/src/usb_hid.c b/app/src/usb_hid.c index 29ee744259a..32b8805c202 100644 --- a/app/src/usb_hid.c +++ b/app/src/usb_hid.c @@ -16,6 +16,9 @@ #if IS_ENABLED(CONFIG_ZMK_HID_INDICATORS) #include #endif // IS_ENABLED(CONFIG_ZMK_HID_INDICATORS) +#if IS_ENABLED(CONFIG_ZMK_TRACKPAD) +#include +#endif #include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -152,7 +155,10 @@ static int set_report_cb(const struct device *dev, struct usb_setup_packet *setu } else { struct zmk_hid_ptp_feature_mode_report *report = (struct zmk_hid_ptp_feature_mode_report *)*data; - zmk_hid_ptp_set_feature_mode_report(report->mode); + struct zmk_endpoint_instance endpoint = { + .transport = ZMK_TRANSPORT_USB, + }; + zmk_trackpad_set_mode_report(&report->mode, endpoint); } break; }