Browse Source

refactor(hid): Move hid logic into hid.c

Move the logic for picking the correct hid function into hid.c.
xmkb
Okke Formsma 4 years ago committed by Pete Johanson
parent
commit
57fca34dc0
  1. 4
      app/include/zmk/hid.h
  2. 20
      app/src/hid.c
  3. 37
      app/src/hid_listener.c

4
app/include/zmk/hid.h

@ -133,6 +133,7 @@ int zmk_hid_register_mods(zmk_mod_flags_t explicit_modifiers); @@ -133,6 +133,7 @@ int zmk_hid_register_mods(zmk_mod_flags_t explicit_modifiers);
int zmk_hid_unregister_mods(zmk_mod_flags_t explicit_modifiers);
int zmk_hid_implicit_modifiers_press(zmk_mod_flags_t implicit_modifiers);
int zmk_hid_implicit_modifiers_release();
int zmk_hid_keyboard_press(zmk_key_t key);
int zmk_hid_keyboard_release(zmk_key_t key);
void zmk_hid_keyboard_clear();
@ -141,5 +142,8 @@ int zmk_hid_consumer_press(zmk_key_t key); @@ -141,5 +142,8 @@ int zmk_hid_consumer_press(zmk_key_t key);
int zmk_hid_consumer_release(zmk_key_t key);
void zmk_hid_consumer_clear();
int zmk_hid_press(uint32_t usage);
int zmk_hid_release(uint32_t usage);
struct zmk_hid_keyboard_report *zmk_hid_get_keyboard_report();
struct zmk_hid_consumer_report *zmk_hid_get_consumer_report();

20
app/src/hid.c

@ -176,6 +176,26 @@ int zmk_hid_consumer_release(zmk_key_t code) { @@ -176,6 +176,26 @@ int zmk_hid_consumer_release(zmk_key_t code) {
return 0;
};
int zmk_hid_press(uint32_t usage) {
switch (ZMK_HID_USAGE_PAGE(usage)) {
case HID_USAGE_KEY:
return zmk_hid_keyboard_press(ZMK_HID_USAGE_ID(usage));
case HID_USAGE_CONSUMER:
return zmk_hid_consumer_press(ZMK_HID_USAGE_ID(usage));
}
return -EINVAL;
}
int zmk_hid_release(uint32_t usage) {
switch (ZMK_HID_USAGE_PAGE(usage)) {
case HID_USAGE_KEY:
return zmk_hid_keyboard_release(ZMK_HID_USAGE_ID(usage));
case HID_USAGE_CONSUMER:
return zmk_hid_consumer_release(ZMK_HID_USAGE_ID(usage));
}
return -EINVAL;
}
void zmk_hid_consumer_clear() { memset(&consumer_report.body, 0, sizeof(consumer_report.body)); }
struct zmk_hid_keyboard_report *zmk_hid_get_keyboard_report() {

37
app/src/hid_listener.c

@ -21,21 +21,10 @@ static int hid_listener_keycode_pressed(const struct zmk_keycode_state_changed * @@ -21,21 +21,10 @@ static int hid_listener_keycode_pressed(const struct zmk_keycode_state_changed *
LOG_DBG("usage_page 0x%02X keycode 0x%02X implicit_mods 0x%02X explicit_mods 0x%02X",
ev->usage_page, ev->keycode, ev->implicit_modifiers, ev->explicit_modifiers);
switch (ev->usage_page) {
case HID_USAGE_KEY:
err = zmk_hid_keyboard_press(ev->keycode);
if (err < 0) {
LOG_ERR("Unable to press keycode");
return err;
}
break;
case HID_USAGE_CONSUMER:
err = zmk_hid_consumer_press(ev->keycode);
if (err < 0) {
LOG_ERR("Unable to press keycode");
return err;
}
break;
err = zmk_hid_press(ZMK_HID_USAGE(ev->usage_page, ev->keycode));
if (err < 0) {
LOG_DBG("Unable to press keycode");
return err;
}
explicit_mods_changed = zmk_hid_register_mods(ev->explicit_modifiers);
implicit_mods_changed = zmk_hid_implicit_modifiers_press(ev->implicit_modifiers);
@ -56,20 +45,10 @@ static int hid_listener_keycode_released(const struct zmk_keycode_state_changed @@ -56,20 +45,10 @@ static int hid_listener_keycode_released(const struct zmk_keycode_state_changed
LOG_DBG("usage_page 0x%02X keycode 0x%02X implicit_mods 0x%02X explicit_mods 0x%02X",
ev->usage_page, ev->keycode, ev->implicit_modifiers, ev->explicit_modifiers);
switch (ev->usage_page) {
case HID_USAGE_KEY:
err = zmk_hid_keyboard_release(ev->keycode);
if (err < 0) {
LOG_ERR("Unable to release keycode");
return err;
}
break;
case HID_USAGE_CONSUMER:
err = zmk_hid_consumer_release(ev->keycode);
if (err < 0) {
LOG_ERR("Unable to release keycode");
return err;
}
err = zmk_hid_release(ZMK_HID_USAGE(ev->usage_page, ev->keycode));
if (err < 0) {
LOG_DBG("Unable to release keycode");
return err;
}
explicit_mods_changed = zmk_hid_unregister_mods(ev->explicit_modifiers);

Loading…
Cancel
Save