Browse Source

refactor(hid_listener): pass event struct instead of parameters

xmkb
Okke Formsma 4 years ago committed by Pete Johanson
parent
commit
df4a5c8613
  1. 36
      app/src/hid_listener.c

36
app/src/hid_listener.c

@ -16,46 +16,44 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -16,46 +16,44 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <dt-bindings/zmk/hid_usage_pages.h>
#include <zmk/endpoints.h>
static int hid_listener_keycode_pressed(uint16_t usage_page, uint32_t keycode,
zmk_mod_flags_t implicit_modifiers) {
static int hid_listener_keycode_pressed(const struct keycode_state_changed *ev) {
int err;
LOG_DBG("usage_page 0x%02X keycode 0x%02X mods 0x%02X", usage_page, keycode,
implicit_modifiers);
switch (usage_page) {
LOG_DBG("usage_page 0x%02X keycode 0x%02X mods 0x%02X", ev->usage_page, ev->keycode,
ev->implicit_modifiers);
switch (ev->usage_page) {
case HID_USAGE_KEY:
err = zmk_hid_keyboard_press(keycode);
err = zmk_hid_keyboard_press(ev->keycode);
if (err) {
LOG_ERR("Unable to press keycode");
return err;
}
break;
case HID_USAGE_CONSUMER:
err = zmk_hid_consumer_press(keycode);
err = zmk_hid_consumer_press(ev->keycode);
if (err) {
LOG_ERR("Unable to press keycode");
return err;
}
break;
}
zmk_hid_implicit_modifiers_press(implicit_modifiers);
return zmk_endpoints_send_report(usage_page);
zmk_hid_implicit_modifiers_press(ev->implicit_modifiers);
return zmk_endpoints_send_report(ev->usage_page);
}
static int hid_listener_keycode_released(uint16_t usage_page, uint32_t keycode,
zmk_mod_flags_t implicit_modifiers) {
static int hid_listener_keycode_released(const struct keycode_state_changed *ev) {
int err;
LOG_DBG("usage_page 0x%02X keycode 0x%02X mods 0x%02X", usage_page, keycode,
implicit_modifiers);
switch (usage_page) {
LOG_DBG("usage_page 0x%02X keycode 0x%02X mods 0x%02X", ev->usage_page, ev->keycode,
ev->implicit_modifiers);
switch (ev->usage_page) {
case HID_USAGE_KEY:
err = zmk_hid_keyboard_release(keycode);
err = zmk_hid_keyboard_release(ev->keycode);
if (err) {
LOG_ERR("Unable to release keycode");
return err;
}
break;
case HID_USAGE_CONSUMER:
err = zmk_hid_consumer_release(keycode);
err = zmk_hid_consumer_release(ev->keycode);
if (err) {
LOG_ERR("Unable to release keycode");
return err;
@ -67,16 +65,16 @@ static int hid_listener_keycode_released(uint16_t usage_page, uint32_t keycode, @@ -67,16 +65,16 @@ static int hid_listener_keycode_released(uint16_t usage_page, uint32_t keycode,
// Solving this would require keeping track of which key's implicit modifiers are currently
// active and only releasing modifiers at that time.
zmk_hid_implicit_modifiers_release();
return zmk_endpoints_send_report(usage_page);
return zmk_endpoints_send_report(ev->usage_page);
}
int hid_listener(const zmk_event_t *eh) {
const struct zmk_keycode_state_changed *ev = as_zmk_keycode_state_changed(eh);
if (ev) {
if (ev->state) {
hid_listener_keycode_pressed(ev->usage_page, ev->keycode, ev->implicit_modifiers);
hid_listener_keycode_pressed(ev);
} else {
hid_listener_keycode_released(ev->usage_page, ev->keycode, ev->implicit_modifiers);
hid_listener_keycode_released(ev);
}
}
return 0;

Loading…
Cancel
Save