Add files via upload
This commit is contained in:
parent
dd4e0d9567
commit
892ffb8028
1 changed files with 136 additions and 89 deletions
225
app/src/hid.c
225
app/src/hid.c
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#include "zmk/keys.h"
|
||||
#include <logging/log.h>
|
||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||
|
||||
|
@ -15,28 +16,30 @@ static struct zmk_hid_keyboard_report keyboard_report = {
|
|||
|
||||
static struct zmk_hid_consumer_report consumer_report = {.report_id = 2, .body = {.keys = {0}}};
|
||||
|
||||
static struct zmk_hid_mouse_report mouse_report = {
|
||||
.report_id = 4, .body = {.buttons = 0, .x = 0, .y = 0, .scroll_x = 0, .scroll_y = 0}};
|
||||
|
||||
// Keep track of how often a modifier was pressed.
|
||||
// Only release the modifier if the count is 0.
|
||||
static int explicit_modifier_counts[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
static zmk_mod_flags_t explicit_modifiers = 0;
|
||||
static zmk_mod_flags_t implicit_modifiers = 0;
|
||||
static zmk_mod_flags_t masked_modifiers = 0;
|
||||
|
||||
#define SET_MODIFIERS(mods) \
|
||||
{ \
|
||||
keyboard_report.body.modifiers = mods; \
|
||||
keyboard_report.body.modifiers = (mods & ~masked_modifiers) | implicit_modifiers; \
|
||||
LOG_DBG("Modifiers set to 0x%02X", keyboard_report.body.modifiers); \
|
||||
}
|
||||
|
||||
#define GET_MODIFIERS (keyboard_report.body.modifiers)
|
||||
|
||||
zmk_mod_flags_t zmk_hid_get_explicit_mods() { return explicit_modifiers; }
|
||||
|
||||
int zmk_hid_register_mod(zmk_mod_t modifier) {
|
||||
explicit_modifier_counts[modifier]++;
|
||||
LOG_DBG("Modifier %d count %d", modifier, explicit_modifier_counts[modifier]);
|
||||
WRITE_BIT(explicit_modifiers, modifier, true);
|
||||
zmk_mod_flags_t current = GET_MODIFIERS;
|
||||
SET_MODIFIERS(explicit_modifiers);
|
||||
return 0;
|
||||
return current == GET_MODIFIERS ? 0 : 1;
|
||||
}
|
||||
|
||||
int zmk_hid_unregister_mod(zmk_mod_t modifier) {
|
||||
|
@ -50,30 +53,68 @@ int zmk_hid_unregister_mod(zmk_mod_t modifier) {
|
|||
LOG_DBG("Modifier %d released", modifier);
|
||||
WRITE_BIT(explicit_modifiers, modifier, false);
|
||||
}
|
||||
zmk_mod_flags_t current = GET_MODIFIERS;
|
||||
SET_MODIFIERS(explicit_modifiers);
|
||||
return 0;
|
||||
return current == GET_MODIFIERS ? 0 : 1;
|
||||
}
|
||||
|
||||
bool zmk_hid_mod_is_pressed(zmk_mod_t modifier) {
|
||||
zmk_mod_flags_t mod_flag = 1 << modifier;
|
||||
return (zmk_hid_get_explicit_mods() & mod_flag) == mod_flag;
|
||||
}
|
||||
|
||||
int zmk_hid_register_mods(zmk_mod_flags_t modifiers) {
|
||||
int ret = 0;
|
||||
for (zmk_mod_t i = 0; i < 8; i++) {
|
||||
if (modifiers & (1 << i)) {
|
||||
zmk_hid_register_mod(i);
|
||||
ret += zmk_hid_register_mod(i);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int zmk_hid_unregister_mods(zmk_mod_flags_t modifiers) {
|
||||
int ret = 0;
|
||||
for (zmk_mod_t i = 0; i < 8; i++) {
|
||||
if (modifiers & (1 << i)) {
|
||||
zmk_hid_unregister_mod(i);
|
||||
ret += zmk_hid_unregister_mod(i);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if IS_ENABLED(CONFIG_ZMK_HID_REPORT_TYPE_NKRO)
|
||||
|
||||
#define TOGGLE_KEYBOARD(code, val) WRITE_BIT(keyboard_report.body.keys[code / 8], code % 8, val)
|
||||
|
||||
static inline int select_keyboard_usage(zmk_key_t usage) {
|
||||
if (usage > ZMK_HID_KEYBOARD_NKRO_MAX_USAGE) {
|
||||
return -EINVAL;
|
||||
}
|
||||
TOGGLE_KEYBOARD(usage, 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int deselect_keyboard_usage(zmk_key_t usage) {
|
||||
if (usage > ZMK_HID_KEYBOARD_NKRO_MAX_USAGE) {
|
||||
return -EINVAL;
|
||||
}
|
||||
TOGGLE_KEYBOARD(usage, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline bool check_keyboard_usage(zmk_key_t usage) {
|
||||
if (usage > ZMK_HID_KEYBOARD_NKRO_MAX_USAGE) {
|
||||
return false;
|
||||
}
|
||||
return keyboard_report.body.keys[usage / 8] & (1 << (usage % 8));
|
||||
}
|
||||
|
||||
#elif IS_ENABLED(CONFIG_ZMK_HID_REPORT_TYPE_HKRO)
|
||||
|
||||
#define TOGGLE_KEYBOARD(match, val) \
|
||||
for (int idx = 0; idx < ZMK_HID_KEYBOARD_NKRO_SIZE; idx++) { \
|
||||
for (int idx = 0; idx < CONFIG_ZMK_HID_KEYBOARD_REPORT_SIZE; idx++) { \
|
||||
if (keyboard_report.body.keys[idx] != match) { \
|
||||
continue; \
|
||||
} \
|
||||
|
@ -83,8 +124,33 @@ int zmk_hid_unregister_mods(zmk_mod_flags_t modifiers) {
|
|||
} \
|
||||
}
|
||||
|
||||
static inline int select_keyboard_usage(zmk_key_t usage) {
|
||||
TOGGLE_KEYBOARD(0U, usage);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int deselect_keyboard_usage(zmk_key_t usage) {
|
||||
TOGGLE_KEYBOARD(usage, 0U);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int check_keyboard_usage(zmk_key_t usage) {
|
||||
for (int idx = 0; idx < CONFIG_ZMK_HID_KEYBOARD_REPORT_SIZE; idx++) {
|
||||
if (keyboard_report.body.keys[idx] == usage) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#else
|
||||
#error "A proper HID report type must be selected"
|
||||
#endif
|
||||
|
||||
#define TOGGLE_CONSUMER(match, val) \
|
||||
for (int idx = 0; idx < ZMK_HID_CONSUMER_NKRO_SIZE; idx++) { \
|
||||
COND_CODE_1(IS_ENABLED(CONFIG_ZMK_HID_CONSUMER_REPORT_USAGES_BASIC), \
|
||||
(if (val > 0xFF) { return -ENOTSUP; }), ()) \
|
||||
for (int idx = 0; idx < CONFIG_ZMK_HID_CONSUMER_REPORT_SIZE; idx++) { \
|
||||
if (consumer_report.body.keys[idx] != match) { \
|
||||
continue; \
|
||||
} \
|
||||
|
@ -94,21 +160,39 @@ int zmk_hid_unregister_mods(zmk_mod_flags_t modifiers) {
|
|||
} \
|
||||
}
|
||||
|
||||
int zmk_hid_implicit_modifiers_press(zmk_mod_flags_t implicit_modifiers) {
|
||||
SET_MODIFIERS(explicit_modifiers | implicit_modifiers);
|
||||
return 0;
|
||||
int zmk_hid_implicit_modifiers_press(zmk_mod_flags_t new_implicit_modifiers) {
|
||||
implicit_modifiers = new_implicit_modifiers;
|
||||
zmk_mod_flags_t current = GET_MODIFIERS;
|
||||
SET_MODIFIERS(explicit_modifiers);
|
||||
return current == GET_MODIFIERS ? 0 : 1;
|
||||
}
|
||||
|
||||
int zmk_hid_implicit_modifiers_release() {
|
||||
implicit_modifiers = 0;
|
||||
zmk_mod_flags_t current = GET_MODIFIERS;
|
||||
SET_MODIFIERS(explicit_modifiers);
|
||||
return 0;
|
||||
return current == GET_MODIFIERS ? 0 : 1;
|
||||
}
|
||||
|
||||
int zmk_hid_masked_modifiers_set(zmk_mod_flags_t new_masked_modifiers) {
|
||||
masked_modifiers = new_masked_modifiers;
|
||||
zmk_mod_flags_t current = GET_MODIFIERS;
|
||||
SET_MODIFIERS(explicit_modifiers);
|
||||
return current == GET_MODIFIERS ? 0 : 1;
|
||||
}
|
||||
|
||||
int zmk_hid_masked_modifiers_clear() {
|
||||
masked_modifiers = 0;
|
||||
zmk_mod_flags_t current = GET_MODIFIERS;
|
||||
SET_MODIFIERS(explicit_modifiers);
|
||||
return current == GET_MODIFIERS ? 0 : 1;
|
||||
}
|
||||
|
||||
int zmk_hid_keyboard_press(zmk_key_t code) {
|
||||
if (code >= HID_USAGE_KEY_KEYBOARD_LEFTCONTROL && code <= HID_USAGE_KEY_KEYBOARD_RIGHT_GUI) {
|
||||
return zmk_hid_register_mod(code - HID_USAGE_KEY_KEYBOARD_LEFTCONTROL);
|
||||
}
|
||||
TOGGLE_KEYBOARD(0U, code);
|
||||
select_keyboard_usage(code);
|
||||
return 0;
|
||||
};
|
||||
|
||||
|
@ -116,10 +200,17 @@ int zmk_hid_keyboard_release(zmk_key_t code) {
|
|||
if (code >= HID_USAGE_KEY_KEYBOARD_LEFTCONTROL && code <= HID_USAGE_KEY_KEYBOARD_RIGHT_GUI) {
|
||||
return zmk_hid_unregister_mod(code - HID_USAGE_KEY_KEYBOARD_LEFTCONTROL);
|
||||
}
|
||||
TOGGLE_KEYBOARD(code, 0U);
|
||||
deselect_keyboard_usage(code);
|
||||
return 0;
|
||||
};
|
||||
|
||||
bool zmk_hid_keyboard_is_pressed(zmk_key_t code) {
|
||||
if (code >= HID_USAGE_KEY_KEYBOARD_LEFTCONTROL && code <= HID_USAGE_KEY_KEYBOARD_RIGHT_GUI) {
|
||||
return zmk_hid_mod_is_pressed(code - HID_USAGE_KEY_KEYBOARD_LEFTCONTROL);
|
||||
}
|
||||
return check_keyboard_usage(code);
|
||||
}
|
||||
|
||||
void zmk_hid_keyboard_clear() { memset(&keyboard_report.body, 0, sizeof(keyboard_report.body)); }
|
||||
|
||||
int zmk_hid_consumer_press(zmk_key_t code) {
|
||||
|
@ -134,85 +225,45 @@ int zmk_hid_consumer_release(zmk_key_t code) {
|
|||
|
||||
void zmk_hid_consumer_clear() { memset(&consumer_report.body, 0, sizeof(consumer_report.body)); }
|
||||
|
||||
// Keep track of how often a button was pressed.
|
||||
// Only release the button if the count is 0.
|
||||
static int explicit_button_counts[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
static zmk_mod_flags_t explicit_buttons = 0;
|
||||
|
||||
#define SET_MOUSE_BUTTONS(btns) \
|
||||
{ \
|
||||
mouse_report.body.buttons = btns; \
|
||||
LOG_DBG("Mouse buttons set to 0x%02X", mouse_report.body.buttons); \
|
||||
}
|
||||
|
||||
int zmk_hid_mouse_button_press(zmk_mouse_button_t button) {
|
||||
explicit_button_counts[button]++;
|
||||
LOG_DBG("Button %d count %d", button, explicit_button_counts[button]);
|
||||
WRITE_BIT(explicit_buttons, button, true);
|
||||
SET_MOUSE_BUTTONS(explicit_buttons);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int zmk_hid_mouse_button_release(zmk_mouse_button_t button) {
|
||||
if (explicit_button_counts[button] <= 0) {
|
||||
LOG_ERR("Tried to release button %d too often", button);
|
||||
return -EINVAL;
|
||||
}
|
||||
explicit_button_counts[button]--;
|
||||
LOG_DBG("Button %d count: %d", button, explicit_button_counts[button]);
|
||||
if (explicit_button_counts[button] == 0) {
|
||||
LOG_DBG("Button %d released", button);
|
||||
WRITE_BIT(explicit_buttons, button, false);
|
||||
}
|
||||
SET_MOUSE_BUTTONS(explicit_buttons);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int zmk_hid_mouse_buttons_press(zmk_mouse_button_flags_t buttons) {
|
||||
for (zmk_mod_t i = 0; i < 16; i++) {
|
||||
if (buttons & (1 << i)) {
|
||||
zmk_hid_mouse_button_press(i);
|
||||
bool zmk_hid_consumer_is_pressed(zmk_key_t key) {
|
||||
for (int idx = 0; idx < CONFIG_ZMK_HID_CONSUMER_REPORT_SIZE; idx++) {
|
||||
if (consumer_report.body.keys[idx] == key) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
int zmk_hid_mouse_buttons_release(zmk_mouse_button_flags_t buttons) {
|
||||
for (zmk_mod_t i = 0; i < 16; i++) {
|
||||
if (buttons & (1 << i)) {
|
||||
zmk_hid_mouse_button_release(i);
|
||||
}
|
||||
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 0;
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
void zmk_hid_mouse_movement_set(int16_t x, int16_t y) {
|
||||
mouse_report.body.x = x;
|
||||
mouse_report.body.y = y;
|
||||
LOG_DBG("Mouse movement set to 0x%02X 0x%02X ", mouse_report.body.x, mouse_report.body.y);
|
||||
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_mouse_movement_update(int16_t x, int16_t y) {
|
||||
mouse_report.body.x += x;
|
||||
mouse_report.body.y += y;
|
||||
LOG_DBG("Mouse movement updated to 0x%02X 0x%02X ", mouse_report.body.x, mouse_report.body.y);
|
||||
bool zmk_hid_is_pressed(uint32_t usage) {
|
||||
switch (ZMK_HID_USAGE_PAGE(usage)) {
|
||||
case HID_USAGE_KEY:
|
||||
return zmk_hid_keyboard_is_pressed(ZMK_HID_USAGE_ID(usage));
|
||||
case HID_USAGE_CONSUMER:
|
||||
return zmk_hid_consumer_is_pressed(ZMK_HID_USAGE_ID(usage));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void zmk_hid_mouse_scroll_set(int8_t x, int8_t y) {
|
||||
mouse_report.body.scroll_x = x;
|
||||
mouse_report.body.scroll_y = y;
|
||||
LOG_DBG("Mouse scroll set to 0x%02X 0x%02X ", mouse_report.body.scroll_x,
|
||||
mouse_report.body.scroll_y);
|
||||
}
|
||||
|
||||
void zmk_hid_mouse_scroll_update(int8_t x, int8_t y) {
|
||||
mouse_report.body.scroll_x += x;
|
||||
mouse_report.body.scroll_y += y;
|
||||
LOG_DBG("Mouse scroll updated to 0x%02X 0x%02X ", mouse_report.body.scroll_x,
|
||||
mouse_report.body.scroll_y);
|
||||
}
|
||||
void zmk_hid_mouse_clear() { memset(&mouse_report.body, 0, sizeof(mouse_report.body)); }
|
||||
|
||||
struct zmk_hid_keyboard_report *zmk_hid_get_keyboard_report() {
|
||||
return &keyboard_report;
|
||||
}
|
||||
|
@ -220,7 +271,3 @@ struct zmk_hid_keyboard_report *zmk_hid_get_keyboard_report() {
|
|||
struct zmk_hid_consumer_report *zmk_hid_get_consumer_report() {
|
||||
return &consumer_report;
|
||||
}
|
||||
|
||||
struct zmk_hid_mouse_report *zmk_hid_get_mouse_report() {
|
||||
return &mouse_report;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue