2020-09-10 13:57:44 +10:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 The ZMK Contributors
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*/
|
|
|
|
|
2020-06-03 01:23:22 +10:00
|
|
|
#include <logging/log.h>
|
|
|
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
2020-05-15 23:41:06 +10:00
|
|
|
|
2020-06-03 01:23:22 +10:00
|
|
|
#include <zmk/hid.h>
|
2020-11-03 16:21:20 +11:00
|
|
|
#include <dt-bindings/zmk/modifiers.h>
|
2020-10-26 15:30:24 +11:00
|
|
|
#include <dt-bindings/zmk/hid_usage_pages.h>
|
2020-05-15 23:41:06 +10:00
|
|
|
|
2020-10-28 04:42:00 +11:00
|
|
|
static struct zmk_hid_keypad_report kp_report = {
|
|
|
|
.report_id = 1, .body = {.modifiers = 0, ._reserved = 0, .keys = {0}}};
|
2020-09-14 12:53:24 +10:00
|
|
|
|
2020-10-28 05:42:41 +11:00
|
|
|
static struct zmk_hid_consumer_report consumer_report = {.report_id = 2, .body = {.keys = {0}}};
|
2020-09-14 12:53:24 +10:00
|
|
|
|
2020-09-03 06:30:09 +10:00
|
|
|
// 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
|
|
|
|
2020-09-03 06:30:09 +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
|
|
|
|
2020-09-03 06:30:09 +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);
|
2020-05-28 13:14:09 +10:00
|
|
|
return 0;
|
2020-05-23 12:47:03 +10:00
|
|
|
}
|
|
|
|
|
2020-09-03 06:30:09 +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);
|
2020-05-28 13:14:09 +10:00
|
|
|
return 0;
|
2020-05-23 12:47:03 +10:00
|
|
|
}
|
|
|
|
|
2020-10-28 09:35:44 +11: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
|
|
|
}
|
2020-06-03 01:23:22 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
#define TOGGLE_CONSUMER(match, val) \
|
2020-10-28 05:42:41 +11:00
|
|
|
for (int idx = 0; idx < ZMK_HID_CONSUMER_NKRO_SIZE; idx++) { \
|
2020-09-14 12:53:24 +10:00
|
|
|
if (consumer_report.body.keys[idx] != match) { \
|
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
consumer_report.body.keys[idx] = val; \
|
|
|
|
break; \
|
2020-06-23 01:06:01 +10:00
|
|
|
}
|
|
|
|
|
2020-09-03 06:30:09 +10:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
int zmk_hid_keypad_press(zmk_key code) {
|
2020-10-26 15:30:24 +11:00
|
|
|
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
|
|
|
}
|
2020-10-28 09:35:44 +11:00
|
|
|
TOGGLE_KEYPAD(0U, code);
|
2020-06-23 01:06:01 +10:00
|
|
|
return 0;
|
2020-05-15 23:41:06 +10:00
|
|
|
};
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
int zmk_hid_keypad_release(zmk_key code) {
|
2020-10-26 15:30:24 +11:00
|
|
|
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
|
|
|
}
|
2020-10-28 09:35:44 +11:00
|
|
|
TOGGLE_KEYPAD(code, 0U);
|
2020-06-23 01:06:01 +10:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2020-10-12 10:38:39 +11:00
|
|
|
void zmk_hid_keypad_clear() { memset(&kp_report.body, 0, sizeof(kp_report.body)); }
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
int zmk_hid_consumer_press(zmk_key code) {
|
2020-06-23 01:06:01 +10:00
|
|
|
TOGGLE_CONSUMER(0U, code);
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
int zmk_hid_consumer_release(zmk_key code) {
|
2020-06-23 01:06:01 +10:00
|
|
|
TOGGLE_CONSUMER(code, 0U);
|
|
|
|
return 0;
|
2020-05-15 23:41:06 +10:00
|
|
|
};
|
|
|
|
|
2020-10-12 10:38:39 +11:00
|
|
|
void zmk_hid_consumer_clear() { memset(&consumer_report.body, 0, sizeof(consumer_report.body)); }
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
struct zmk_hid_keypad_report *zmk_hid_get_keypad_report() {
|
2020-06-03 01:23:22 +10:00
|
|
|
return &kp_report;
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
struct zmk_hid_consumer_report *zmk_hid_get_consumer_report() {
|
2020-06-03 01:23:22 +10:00
|
|
|
return &consumer_report;
|
2020-05-18 23:11:46 +10:00
|
|
|
}
|