From 808858502b6a21cd02287c134c018121bec0072e Mon Sep 17 00:00:00 2001 From: Alexander Krikun Date: Wed, 22 Sep 2021 00:27:52 +0300 Subject: [PATCH] Send mouse messages from dedicated thread --- app/src/endpoints.c | 4 ++++ app/src/hog.c | 23 +++++++++++++++++++++++ app/src/mouse/Kconfig | 2 +- app/src/mouse/key_listener.c | 4 +++- app/src/mouse/tick_listener.c | 13 ++++++------- 5 files changed, 37 insertions(+), 9 deletions(-) diff --git a/app/src/endpoints.c b/app/src/endpoints.c index 3e025291c6b..7aabc5d4e9e 100644 --- a/app/src/endpoints.c +++ b/app/src/endpoints.c @@ -161,7 +161,11 @@ int zmk_endpoints_send_mouse_report() { #if IS_ENABLED(CONFIG_ZMK_BLE) case ZMK_ENDPOINT_BLE: { +#if IS_ENABLED(CONFIG_ZMK_MOUSE_WORK_QUEUE_DEDICATED) + int err = zmk_hog_send_mouse_report_direct(&mouse_report->body); +#else int err = zmk_hog_send_mouse_report(&mouse_report->body); +#endif if (err) { LOG_ERR("FAILED TO SEND OVER HOG: %d", err); } diff --git a/app/src/hog.c b/app/src/hog.c index 259ab7442bd..e607e8c497a 100644 --- a/app/src/hog.c +++ b/app/src/hog.c @@ -327,6 +327,29 @@ int zmk_hog_send_mouse_report(struct zmk_hid_mouse_report_body *report) { return 0; }; +int zmk_hog_send_mouse_report_direct(struct zmk_hid_mouse_report_body *report) { + struct bt_conn *conn = destination_connection(); + if (conn == NULL) { + return 1; + } + + struct bt_gatt_notify_params notify_params = { + .attr = &hog_svc.attrs[13], + .data = report, + .len = sizeof(*report), + }; + + int err = bt_gatt_notify_cb(conn, ¬ify_params); + if (err) { + LOG_DBG("Error notifying %d", err); + return err; + } + + bt_conn_unref(conn); + + return 0; +}; + int zmk_hog_init(const struct device *_arg) { k_work_q_start(&hog_work_q, hog_q_stack, K_THREAD_STACK_SIZEOF(hog_q_stack), CONFIG_ZMK_BLE_THREAD_PRIORITY); diff --git a/app/src/mouse/Kconfig b/app/src/mouse/Kconfig index 213b305a9bd..57c57a4ff90 100644 --- a/app/src/mouse/Kconfig +++ b/app/src/mouse/Kconfig @@ -31,7 +31,7 @@ config ZMK_MOUSE_DEDICATED_THREAD_STACK_SIZE config ZMK_MOUSE_DEDICATED_THREAD_PRIORITY int "Thread priority for dedicated mouse thread/queue" - default 5 + default 3 endif # ZMK_MOUSE_WORK_QUEUE_DEDICATED diff --git a/app/src/mouse/key_listener.c b/app/src/mouse/key_listener.c index 56a84b45e04..713d032352a 100644 --- a/app/src/mouse/key_listener.c +++ b/app/src/mouse/key_listener.c @@ -49,13 +49,15 @@ static void mouse_tick_timer_handler(struct k_work *work) { zmk_hid_mouse_movement_set(0, 0); zmk_hid_mouse_scroll_set(0, 0); LOG_DBG("Raising mouse tick event"); - ZMK_EVENT_RAISE(zmk_mouse_tick(move_speed, scroll_speed, move_config, scroll_config, &start_time)); + ZMK_EVENT_RAISE( + zmk_mouse_tick(move_speed, scroll_speed, move_config, scroll_config, &start_time)); zmk_endpoints_send_mouse_report(); } K_WORK_DEFINE(mouse_tick, &mouse_tick_timer_handler); void mouse_timer_cb(struct k_timer *dummy) { + LOG_DBG("Submitting mouse work to queue"); k_work_submit_to_queue(zmk_mouse_work_q(), &mouse_tick); } diff --git a/app/src/mouse/tick_listener.c b/app/src/mouse/tick_listener.c index a14593840d8..9c76bd5d2a8 100644 --- a/app/src/mouse/tick_listener.c +++ b/app/src/mouse/tick_listener.c @@ -43,9 +43,8 @@ static int64_t ms_since_start(int64_t start, int64_t now, int64_t delay) { static float speed(const struct mouse_config *config, float max_speed, int64_t duration_ms) { // Calculate the speed based on MouseKeysAccel // See https://en.wikipedia.org/wiki/Mouse_keys - if (duration_ms > config->time_to_max_speed_ms - || config->time_to_max_speed_ms == 0 - || config->acceleration_exponent == 0) { + if (duration_ms > config->time_to_max_speed_ms || config->time_to_max_speed_ms == 0 || + config->acceleration_exponent == 0) { return max_speed; } float time_fraction = (float)duration_ms / config->time_to_max_speed_ms; @@ -80,12 +79,12 @@ static struct vector2d update_movement(struct vector2d *remainder, } static void mouse_tick_handler(const struct zmk_mouse_tick *tick) { - struct vector2d move = - update_movement(&move_remainder, &(tick->move_config), tick->max_move, tick->timestamp, tick->start_time); + struct vector2d move = update_movement(&move_remainder, &(tick->move_config), tick->max_move, + tick->timestamp, tick->start_time); zmk_hid_mouse_movement_update((int16_t)CLAMP(move.x, INT16_MIN, INT16_MAX), (int16_t)CLAMP(move.y, INT16_MIN, INT16_MAX)); - struct vector2d scroll = - update_movement(&scroll_remainder, &(tick->scroll_config), tick->max_scroll, tick->timestamp, tick->start_time); + struct vector2d scroll = update_movement(&scroll_remainder, &(tick->scroll_config), + tick->max_scroll, tick->timestamp, tick->start_time); zmk_hid_mouse_scroll_update((int8_t)CLAMP(scroll.x, INT8_MIN, INT8_MAX), (int8_t)CLAMP(scroll.y, INT8_MIN, INT8_MAX)); }