zmk_mf68/app/src/hid.c

118 lines
4.6 KiB
C
Raw Normal View History

/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
2020-05-15 23:41:06 +10:00
#include <zmk/hid.h>
#include <dt-bindings/zmk/modifiers.h>
#include <dt-bindings/zmk/hid_usage_pages.h>
2020-05-15 23:41:06 +10:00
static struct zmk_hid_keypad_report kp_report = {
.report_id = 1, .body = {.modifiers = 0, ._reserved = 0, .keys = {0}}};
static struct zmk_hid_consumer_report consumer_report = {.report_id = 2, .body = {.keys = {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 explicit_modifiers = 0;
2020-05-19 13:34:36 +10:00
#define SET_MODIFIERS(mods) \
{ \
kp_report.body.modifiers = mods; \
LOG_DBG("Modifiers set to 0x%02X", kp_report.body.modifiers); \
}
2020-05-19 13:34:36 +10:00
int zmk_hid_register_mod(zmk_mod modifier) {
explicit_modifier_counts[modifier]++;
LOG_DBG("Modifier %d count %d", modifier, explicit_modifier_counts[modifier]);
WRITE_BIT(explicit_modifiers, modifier, true);
SET_MODIFIERS(explicit_modifiers);
return 0;
2020-05-23 12:47:03 +10:00
}
int zmk_hid_unregister_mod(zmk_mod modifier) {
if (explicit_modifier_counts[modifier] <= 0) {
LOG_ERR("Tried to unregister modifier %d too often", modifier);
return -EINVAL;
}
explicit_modifier_counts[modifier]--;
LOG_DBG("Modifier %d count: %d", modifier, explicit_modifier_counts[modifier]);
if (explicit_modifier_counts[modifier] == 0) {
LOG_DBG("Modifier %d released", modifier);
WRITE_BIT(explicit_modifiers, modifier, false);
}
SET_MODIFIERS(explicit_modifiers);
return 0;
2020-05-23 12:47:03 +10:00
}
#define TOGGLE_KEYPAD(match, val) \
for (int idx = 0; idx < ZMK_HID_KEYPAD_NKRO_SIZE; idx++) { \
if (kp_report.body.keys[idx] != match) { \
continue; \
} \
kp_report.body.keys[idx] = val; \
break; \
2020-05-15 23:41:06 +10:00
}
#define TOGGLE_CONSUMER(match, val) \
for (int idx = 0; idx < ZMK_HID_CONSUMER_NKRO_SIZE; idx++) { \
if (consumer_report.body.keys[idx] != match) { \
continue; \
} \
consumer_report.body.keys[idx] = val; \
break; \
}
int zmk_hid_implicit_modifiers_press(zmk_mod_flags implicit_modifiers) {
SET_MODIFIERS(explicit_modifiers | implicit_modifiers);
return 0;
}
int zmk_hid_implicit_modifiers_release() {
SET_MODIFIERS(explicit_modifiers);
return 0;
}
int zmk_hid_keypad_press(zmk_key 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);
2020-05-19 13:34:36 +10:00
}
TOGGLE_KEYPAD(0U, code);
return 0;
2020-05-15 23:41:06 +10:00
};
int zmk_hid_keypad_release(zmk_key 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);
2020-05-19 13:34:36 +10:00
}
TOGGLE_KEYPAD(code, 0U);
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;
};
int zmk_hid_consumer_release(zmk_key code) {
TOGGLE_CONSUMER(code, 0U);
return 0;
2020-05-15 23:41:06 +10:00
};
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;
}
struct zmk_hid_consumer_report *zmk_hid_get_consumer_report() {
return &consumer_report;
}