Skip to content

Commit

Permalink
feat(endpoints): clear HID report on endpoint change
Browse files Browse the repository at this point in the history
This prevents stuck keys when switching endpoints by clearing
everything in the HID report and sending one last report before
switching to the new endpoint.
  • Loading branch information
joelspadin committed Oct 11, 2020
1 parent 3b33205 commit e8f77b4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/include/zmk/hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ int zmk_hid_register_mods(zmk_mod_flags modifiers);
int zmk_hid_unregister_mods(zmk_mod_flags modifiers);
int zmk_hid_keypad_press(zmk_key key);
int zmk_hid_keypad_release(zmk_key key);
void zmk_hid_keypad_clear();

int zmk_hid_consumer_press(zmk_key key);
int zmk_hid_consumer_release(zmk_key key);
void zmk_hid_consumer_clear();

struct zmk_hid_keypad_report *zmk_hid_get_keypad_report();
struct zmk_hid_consumer_report *zmk_hid_get_consumer_report();
12 changes: 11 additions & 1 deletion app/src/endpoints.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,21 @@ static enum endpoint get_selected_endpoint() {
return ENDPOINT_USB;
}

static void disconnect_current_endpoint() {
zmk_hid_keypad_clear();
zmk_hid_consumer_clear();

zmk_endpoints_send_report(USAGE_KEYPAD);
zmk_endpoints_send_report(USAGE_CONSUMER);
}

static int endpoint_listener(const struct zmk_event_header *eh) {
enum endpoint new_endpoint = get_selected_endpoint();

if (new_endpoint != current_endpoint) {
// TODO: send null report on previous endpoint
/* Cancel all current keypresses so keys don't stay held on the old endpoint. */
disconnect_current_endpoint();

current_endpoint = new_endpoint;
LOG_INF("Endpoint changed: %d", current_endpoint);
}
Expand Down
8 changes: 8 additions & 0 deletions app/src/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ int zmk_hid_keypad_release(zmk_key code) {
return 0;
};

void zmk_hid_keypad_clear() {
memset(&kp_report.body, 0, sizeof(kp_report.body));
}

int zmk_hid_consumer_press(zmk_key code) {
TOGGLE_CONSUMER(0U, code);
return 0;
Expand All @@ -104,6 +108,10 @@ int zmk_hid_consumer_release(zmk_key code) {
return 0;
};

void zmk_hid_consumer_clear() {
memset(&consumer_report.body, 0, sizeof(consumer_report.body));
}

struct zmk_hid_keypad_report *zmk_hid_get_keypad_report() {
return &kp_report;
}
Expand Down

0 comments on commit e8f77b4

Please sign in to comment.