1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/hid.h>
#include <dt-bindings/zmk/modifiers.h>
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;
#define SET_MODIFIERS(mods) \
{ \
kp_report.body.modifiers = mods; \
LOG_DBG("Modifiers set to 0x%02X", kp_report.body.modifiers); \
}
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;
}
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;
}
#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; \
}
#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);
}
TOGGLE_KEYPAD(0U, code);
return 0;
};
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);
}
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;
};
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;
}
|