Implemented Rinh's suggestion to remove deadlocks

This commit is contained in:
Dmitry Tsykunov 2021-05-02 16:18:41 +03:00 committed by Alexander Krikun
parent b27b8e35ec
commit 2d2a8bd60d

View file

@ -71,16 +71,32 @@ static int hid_listener_keycode_released(const struct zmk_keycode_state_changed
return zmk_endpoints_send_report(ev->usage_page); return zmk_endpoints_send_report(ev->usage_page);
} }
static int mouse_is_moving_counter = 0; static void zmk_mouse_work(struct k_work *work) {
int rc = zmk_endpoints_send_mouse_report();
if (rc != 0) {
LOG_ERR("Failed to send mouse report, error: %d", rc);
}
}
void mouse_timer_cb(struct k_timer *dummy); K_WORK_DEFINE(mouse_work, &zmk_mouse_work);
K_TIMER_DEFINE(mouse_timer, mouse_timer_cb, NULL); void mouse_timer_cb(struct k_timer *dummy) { k_work_submit(&mouse_work); }
void mouse_timer_cb(struct k_timer *dummy) { K_TIMER_DEFINE(mouse_timer, mouse_timer_cb, mouse_timer_cb);
if (mouse_is_moving_counter != 0) {
zmk_endpoints_send_mouse_report(); static int mouse_timer_ref_count = 0;
k_timer_start(&mouse_timer, K_MSEC(10), K_NO_WAIT);
void mouse_timer_ref() {
if (mouse_timer_ref_count == 0) {
k_timer_start(&mouse_timer, K_NO_WAIT, K_MSEC(10));
}
mouse_timer_ref_count += 1;
}
void mouse_timer_unref() {
mouse_timer_ref_count -= 1;
if (mouse_timer_ref_count == 0) {
k_timer_stop(&mouse_timer);
} }
} }
@ -92,10 +108,8 @@ static int hid_listener_mouse_pressed(const struct zmk_mouse_state_changed *ev)
LOG_ERR("Unable to press button"); LOG_ERR("Unable to press button");
return err; return err;
} }
// race condition? mouse_timer_ref();
mouse_is_moving_counter += 1; return 0;
k_timer_start(&mouse_timer, K_MSEC(10), K_NO_WAIT);
return zmk_endpoints_send_mouse_report();
} }
static int hid_listener_mouse_released(const struct zmk_mouse_state_changed *ev) { static int hid_listener_mouse_released(const struct zmk_mouse_state_changed *ev) {
@ -106,9 +120,8 @@ static int hid_listener_mouse_released(const struct zmk_mouse_state_changed *ev)
LOG_ERR("Unable to release button"); LOG_ERR("Unable to release button");
return err; return err;
} }
// race condition? mouse_timer_unref();
mouse_is_moving_counter -= 1; return 0;
return zmk_endpoints_send_mouse_report();
} }
int hid_listener(const zmk_event_t *eh) { int hid_listener(const zmk_event_t *eh) {