Skip to content

Commit f2f7571

Browse files
committed
key_actions: implement bare bones keystroke actions and layers
Signed-off-by: perigoso <perigoso@riseup.net>
1 parent b5dd4c2 commit f2f7571

4 files changed

Lines changed: 381 additions & 0 deletions

File tree

config/base.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
source = [
22
'protocol/protocol.c',
33
'driver/pixart/pixart_pmw.c',
4+
'key_actions/key_actions.c',
45
]

src/key_actions/key_actions.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* SPDX-FileCopyrightText: 2021 Rafael Silva <perigoso@riseup.net>
4+
*/
5+
6+
#include "key_actions/key_actions.h"
7+
#include "util/data.h"
8+
9+
struct key_actions_return_t key_actions_update(struct key_actions_config_t config)
10+
{
11+
u8 keycode_index = 0;
12+
struct key_actions_return_t return_data = {};
13+
14+
for (size_t i = 0; i < config.key_list_size; i++) {
15+
switch (config.active_layer[i].node_action) {
16+
case layer_node_none:
17+
break;
18+
19+
case layer_node_regular:
20+
if (config.key_list[i]->key_status == key_pressed) {
21+
if (keycode_index < 6)
22+
return_data.keycodes[keycode_index++] = config.active_layer[i].data[0];
23+
return_data.data_present = 1;
24+
}
25+
break;
26+
27+
case layer_node_tap_hold:
28+
if (config.key_list[i]->key_status == key_pressed) {
29+
if (config.key_list[i]->key_t_delta > config.tap_interval) {
30+
config.active_layer[i].data[3] = 0;
31+
if (keycode_index < 6)
32+
return_data.keycodes[keycode_index++] = config.active_layer[i].data[1];
33+
return_data.data_present = 1;
34+
} else {
35+
config.active_layer[i].data[3] = 1;
36+
}
37+
} else {
38+
if (config.active_layer[i].data[3]) {
39+
config.active_layer[i].data[3] = 0;
40+
if (keycode_index < 6)
41+
return_data.keycodes[keycode_index++] = config.active_layer[i].data[0];
42+
return_data.data_present = 1;
43+
}
44+
}
45+
46+
break;
47+
48+
case layer_node_set_layer:
49+
if (config.key_list[i]->key_status == key_pressed) {
50+
key_actions_load_layer(config, config.active_layer[i].data[0]);
51+
return_data.data_present = 1;
52+
}
53+
break;
54+
55+
case layer_node_mouse_buttons:
56+
if (config.key_list[i]->key_status == key_pressed) {
57+
u8 but_no = config.active_layer[i].data[0];
58+
if (but_no >= 0 && but_no <= 5)
59+
return_data.mouse_buttons |= BIT(but_no);
60+
return_data.data_present = 1;
61+
}
62+
break;
63+
}
64+
}
65+
66+
return return_data;
67+
}
68+
69+
void key_actions_load_layer(struct key_actions_config_t config, u8 layer_no)
70+
{
71+
if (layer_no >= config.layer_list_size)
72+
return;
73+
74+
for (size_t i = 0; i < config.key_list_size; i++) config.active_layer[i] = config.layer_list[layer_no][i];
75+
}

src/key_actions/key_actions.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* SPDX-FileCopyrightText: 2021 Rafael Silva <perigoso@riseup.net>
4+
*/
5+
6+
#pragma once
7+
8+
#include "util/types.h"
9+
10+
enum key_status_t {
11+
key_released = 0b0,
12+
key_pressed = 0b1,
13+
};
14+
15+
struct key_t {
16+
u16 key_status : 1;
17+
u16 key_t_delta : 15; /* time delta since change, used to determine tap and hold behaviour*/
18+
};
19+
20+
enum layer_node_actions_t {
21+
layer_node_none, /* no action */
22+
layer_node_regular, /* regular keystroke, send keycode on data[0] */
23+
layer_node_tap_hold, /* send keycode on data[0] on tap, keycode on data[1] on hold */
24+
layer_node_set_layer, /* set layer specified on data[0] */
25+
layer_node_mouse_buttons, /* send mouse button, index on data[0] (1-5) */
26+
};
27+
28+
struct layer_node_t {
29+
u8 node_action;
30+
u8 data[3]; /* data use depends on action */
31+
};
32+
33+
struct key_actions_config_t {
34+
struct key_t **key_list; /* array of pointers to key_t structs, these are provided by low level platform drivers */
35+
u8 key_list_size; /* number of key_t elements */
36+
struct layer_node_t **layer_list; /* array of layers (arrays of layer_node_t, size must be the same as key_list_size) */
37+
u8 layer_list_size; /* number of available layers on layer_list */
38+
struct layer_node_t *active_layer; /* used internally, array of layer_node_t, size must be the same as key_list_size */
39+
u16 tap_interval; /* setting for tap/hold behaviour, keystrokes longer than tap_interval (ms) are holds */
40+
};
41+
42+
struct key_actions_return_t {
43+
u8 data_present; /* if there is data returned */
44+
u8 keycodes[6]; /* keycode array for hid keyboard report */
45+
u8 mouse_buttons; /* mouse button state */
46+
};
47+
48+
struct key_actions_return_t key_actions_update(struct key_actions_config_t config);
49+
void key_actions_load_layer(struct key_actions_config_t config, u8 layer_no);

src/util/keycodes.h

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* SPDX-FileCopyrightText: 2021 Filipe Laíns <lains@riseup.net>
4+
*/
5+
6+
#pragma once
7+
8+
enum keycodes_t {
9+
keycode_none = 0x00, /* No key pressed */
10+
keycode_err_rollover = 0x01, /* Keyboard Error Roll Over ("Phantom Key") */
11+
keycode_err_post_fail = 0x02, /* Keyboard POST Fail */
12+
keycode_error_undefined = 0x03, /* Keyboard Error Undefined */
13+
keycode_A = 0x04, /* Keyboard a and A */
14+
keycode_B = 0x05, /* Keyboard b and B */
15+
keycode_C = 0x06, /* Keyboard c and C */
16+
keycode_D = 0x07, /* Keyboard d and D */
17+
keycode_E = 0x08, /* Keyboard e and E */
18+
keycode_F = 0x09, /* Keyboard f and F */
19+
keycode_G = 0x0a, /* Keyboard g and G */
20+
keycode_H = 0x0b, /* Keyboard h and H */
21+
keycode_I = 0x0c, /* Keyboard i and I */
22+
keycode_J = 0x0d, /* Keyboard j and J */
23+
keycode_K = 0x0e, /* Keyboard k and K */
24+
keycode_L = 0x0f, /* Keyboard l and L */
25+
keycode_M = 0x10, /* Keyboard m and M */
26+
keycode_N = 0x11, /* Keyboard n and N */
27+
keycode_O = 0x12, /* Keyboard o and O */
28+
keycode_P = 0x13, /* Keyboard p and P */
29+
keycode_Q = 0x14, /* Keyboard q and Q */
30+
keycode_R = 0x15, /* Keyboard r and R */
31+
keycode_S = 0x16, /* Keyboard s and S */
32+
keycode_T = 0x17, /* Keyboard t and T */
33+
keycode_U = 0x18, /* Keyboard u and U */
34+
keycode_V = 0x19, /* Keyboard v and V */
35+
keycode_W = 0x1a, /* Keyboard w and W */
36+
keycode_X = 0x1b, /* Keyboard x and X */
37+
keycode_Y = 0x1c, /* Keyboard y and Y */
38+
keycode_Z = 0x1d, /* Keyboard z and Z */
39+
keycode_1 = 0x1e, /* Keyboard 1 and ! */
40+
keycode_2 = 0x1f, /* Keyboard 2 and @ */
41+
keycode_3 = 0x20, /* Keyboard 3 and # */
42+
keycode_4 = 0x21, /* Keyboard 4 and $ */
43+
keycode_5 = 0x22, /* Keyboard 5 and % */
44+
keycode_6 = 0x23, /* Keyboard 6 and ^ */
45+
keycode_7 = 0x24, /* Keyboard 7 and &amp; */
46+
keycode_8 = 0x25, /* Keyboard 8 and * */
47+
keycode_9 = 0x26, /* Keyboard 9 and ( */
48+
keycode_0 = 0x27, /* Keyboard 0 and ) */
49+
keycode_enter = 0x28, /* Keyboard Return (ENTER) */
50+
keycode_esc = 0x29, /* Keyboard ESCAPE */
51+
keycode_backspace = 0x2a, /* Keyboard DELETE (Backspace) */
52+
keycode_tab = 0x2b, /* Keyboard Tab */
53+
keycode_space = 0x2c, /* Keyboard Spacebar */
54+
keycode_minus = 0x2d, /* Keyboard - and _ */
55+
keycode_equal = 0x2e, /* Keyboard = and + */
56+
keycode_left_brace = 0x2f, /* Keyboard [ and { */
57+
keycode_right_brace = 0x30, /* Keyboard ] and } */
58+
keycode_backslash = 0x31, /* Keyboard \ and | */
59+
keycode_hashtilde = 0x32, /* Keyboard Non-US # and ~ */
60+
keycode_semicolon = 0x33, /* Keyboard ; and : */
61+
keycode_apostrophe = 0x34, /* Keyboard ' and " */
62+
keycode_grave = 0x35, /* Keyboard ` and ~ */
63+
keycode_comma = 0x36, /* Keyboard , and LESSTHAN &lt; */
64+
keycode_period = 0x37, /* Keyboard . and > */
65+
keycode_forwardslash = 0x38, /* Keyboard / and ? */
66+
keycode_capslock = 0x39, /* Keyboard Caps Lock */
67+
keycode_f1 = 0x3a, /* Keyboard F1 */
68+
keycode_f2 = 0x3b, /* Keyboard F2 */
69+
keycode_f3 = 0x3c, /* Keyboard F3 */
70+
keycode_f4 = 0x3d, /* Keyboard F4 */
71+
keycode_f5 = 0x3e, /* Keyboard F5 */
72+
keycode_f6 = 0x3f, /* Keyboard F6 */
73+
keycode_f7 = 0x40, /* Keyboard F7 */
74+
keycode_f8 = 0x41, /* Keyboard F8 */
75+
keycode_f9 = 0x42, /* Keyboard F9 */
76+
keycode_f10 = 0x43, /* Keyboard F10 */
77+
keycode_f11 = 0x44, /* Keyboard F11 */
78+
keycode_f12 = 0x45, /* Keyboard F12 */
79+
keycode_printscr = 0x46, /* Keyboard Print Screen */
80+
keycode_scrolllock = 0x47, /* Keyboard Scroll Lock */
81+
keycode_pause = 0x48, /* Keyboard Pause */
82+
keycode_insert = 0x49, /* Keyboard Insert */
83+
keycode_home = 0x4a, /* Keyboard Home */
84+
keycode_pageup = 0x4b, /* Keyboard Page Up */
85+
keycode_delete = 0x4c, /* Keyboard Delete Forward */
86+
keycode_end = 0x4d, /* Keyboard End */
87+
keycode_pagedown = 0x4e, /* Keyboard Page Down */
88+
keycode_right_arrow = 0x4f, /* Keyboard Right Arrow */
89+
keycode_left_arrow = 0x50, /* Keyboard Left Arrow */
90+
keycode_down_arrow = 0x51, /* Keyboard Down Arrow */
91+
keycode_up_arrow = 0x52, /* Keyboard Up Arrow */
92+
keycode_np_numlock = 0x53, /* Keyboard Num Lock and Clear */
93+
keycode_np_slash = 0x54, /* Keypad / */
94+
keycode_np_asterisk = 0x55, /* Keypad * */
95+
keycode_np_minus = 0x56, /* Keypad - */
96+
keycode_np_plus = 0x57, /* Keypad + */
97+
keycode_np_enter = 0x58, /* Keypad ENTER */
98+
keycode_np_1 = 0x59, /* Keypad 1 and End */
99+
keycode_np_2 = 0x5a, /* Keypad 2 and Down Arrow */
100+
keycode_np_3 = 0x5b, /* Keypad 3 and PageDn */
101+
keycode_np_4 = 0x5c, /* Keypad 4 and Left Arrow */
102+
keycode_np_5 = 0x5d, /* Keypad 5 */
103+
keycode_np_6 = 0x5e, /* Keypad 6 and Right Arrow */
104+
keycode_np_7 = 0x5f, /* Keypad 7 and Home */
105+
keycode_np_8 = 0x60, /* Keypad 8 and Up Arrow */
106+
keycode_np_9 = 0x61, /* Keypad 9 and Page Up */
107+
keycode_np_0 = 0x62, /* Keypad 0 and Insert */
108+
keycode_np_dot = 0x63, /* Keypad . and Delete */
109+
keycode_102nd = 0x64, /* Keyboard Non-US \ and | */
110+
keycode_compose = 0x65, /* Keyboard Application */
111+
keycode_power = 0x66, /* Keyboard Power */
112+
keycode_np_equalqual = 0x67, /* Keypad = */
113+
keycode_f13 = 0x68, /* Keyboard F13 */
114+
keycode_f14 = 0x69, /* Keyboard F14 */
115+
keycode_f15 = 0x6a, /* Keyboard F15 */
116+
keycode_f16 = 0x6b, /* Keyboard F16 */
117+
keycode_f17 = 0x6c, /* Keyboard F17 */
118+
keycode_f18 = 0x6d, /* Keyboard F18 */
119+
keycode_f19 = 0x6e, /* Keyboard F19 */
120+
keycode_f20 = 0x6f, /* Keyboard F20 */
121+
keycode_f21 = 0x70, /* Keyboard F21 */
122+
keycode_f22 = 0x71, /* Keyboard F22 */
123+
keycode_f23 = 0x72, /* Keyboard F23 */
124+
keycode_f24 = 0x73, /* Keyboard F24 */
125+
keycode_open = 0x74, /* Keyboard Execute */
126+
keycode_help = 0x75, /* Keyboard Help */
127+
keycode_menu = 0x76, /* Keyboard Menu */
128+
keycode_select = 0x77, /* Keyboard Select */
129+
keycode_stop = 0x78, /* Keyboard Stop */
130+
keycode_again = 0x79, /* Keyboard Again */
131+
keycode_undo = 0x7a, /* Keyboard Undo */
132+
keycode_cut = 0x7b, /* Keyboard Cut */
133+
keycode_copy = 0x7c, /* Keyboard Copy */
134+
keycode_paste = 0x7d, /* Keyboard Paste */
135+
keycode_find = 0x7e, /* Keyboard Find */
136+
keycode_mute = 0x7f, /* Keyboard Mute */
137+
keycode_volume_up = 0x80, /* Keyboard Volume Up */
138+
keycode_volume_down = 0x81, /* Keyboard Volume Down */
139+
keycode_capslock_locking = 0x82, /* Keyboard Locking Caps Lock */
140+
keycode_numlock_locking = 0x83, /* Keyboard Locking Num Lock */
141+
keycode_scrolllock_locking = 0x84, /* Keyboard Locking Scroll Lock */
142+
keycode_br_comma = 0x85, /* Keypad Brazilian Comma */
143+
keycode_br_equal = 0x86, /* Keyboard Brazilian Equal Sign */
144+
keycode_ro = 0x87, /* Keyboard International1 */
145+
keycode_katakana_hiragana = 0x88, /* Keyboard International2 */
146+
keycode_yen = 0x89, /* Keyboard International3 */
147+
keycode_henken = 0x8a, /* Keyboard International4 */
148+
keycode_muhenkan = 0x8b, /* Keyboard International5 */
149+
keycode_jp_comma = 0x8c, /* Keyboard International6 */
150+
151+
// 0x8d Keyboard International7
152+
// 0x8e Keyboard International8
153+
// 0x8f Keyboard International9
154+
155+
keycode_hanguel = 0x90, /* Keyboard LANG1 */
156+
keycode_hanja = 0x91, /* Keyboard LANG2 */
157+
keycode_katakana = 0x92, /* Keyboard LANG3 */
158+
keycode_hiragana = 0x93, /* Keyboard LANG4 */
159+
keycode_zenkaku_hankaku = 0x94, /* Keyboard LANG5 */
160+
161+
// 0x95 Keyboard LANG6
162+
// 0x96 Keyboard LANG7
163+
// 0x97 Keyboard LANG8
164+
// 0x98 Keyboard LANG9
165+
// 0x99 Keyboard Alternate Erase
166+
// 0x9a Keyboard SysReq/Attention
167+
// 0x9b Keyboard Cancel
168+
// 0x9c Keyboard Clear
169+
// 0x9d Keyboard Prior
170+
// 0x9e Keyboard Return
171+
// 0x9f Keyboard Separator
172+
// 0xa0 Keyboard Out
173+
// 0xa1 Keyboard Oper
174+
// 0xa2 Keyboard Clear/Again
175+
// 0xa3 Keyboard CrSel/Props
176+
// 0xa4 Keyboard ExSel
177+
178+
// 0xb0 Keypad 00
179+
// 0xb1 Keypad 000
180+
// 0xb2 Thousands Separator
181+
// 0xb3 Decimal Separator
182+
// 0xb4 Currency Unit
183+
// 0xb5 Currency Sub-unit
184+
185+
keycode_np_left_parentheses = 0xb6, /* Keypad ( */
186+
keycode_np_right_parentheses = 0xb7, /* Keypad ) */
187+
188+
// 0xb8 Keypad {
189+
// 0xb9 Keypad }
190+
// 0xba Keypad Tab
191+
// 0xbb Keypad Backspace
192+
// 0xbc Keypad A
193+
// 0xbd Keypad B
194+
// 0xbe Keypad C
195+
// 0xbf Keypad D
196+
// 0xc0 Keypad E
197+
// 0xc1 Keypad F
198+
// 0xc2 Keypad XOR
199+
// 0xc3 Keypad ^
200+
// 0xc4 Keypad %
201+
// 0xc5 Keypad <
202+
// 0xc6 Keypad >
203+
// 0xc7 Keypad &
204+
// 0xc8 Keypad &&
205+
// 0xc9 Keypad |
206+
// 0xca Keypad ||
207+
// 0xcb Keypad :
208+
// 0xcc Keypad #
209+
// 0xcd Keypad Space
210+
// 0xce Keypad @
211+
// 0xcf Keypad !
212+
// 0xd0 Keypad Memory Store
213+
// 0xd1 Keypad Memory Recall
214+
// 0xd2 Keypad Memory Clear
215+
// 0xd3 Keypad Memory Add
216+
// 0xd4 Keypad Memory Subtract
217+
// 0xd5 Keypad Memory Multiply
218+
// 0xd6 Keypad Memory Divide
219+
// 0xd7 Keypad +/-
220+
// 0xd8 Keypad Clear
221+
// 0xd9 Keypad Clear Entry
222+
// 0xda Keypad Binary
223+
// 0xdb Keypad Octal
224+
// 0xdc Keypad Decimal
225+
// 0xdd Keypad Hexadecimal
226+
227+
keycode_left_control = 0xe0, /* Keyboard Left Control */
228+
keycode_left_shift = 0xe1, /* Keyboard Left Shift */
229+
keycode_left_alt = 0xe2, /* Keyboard Left Alt */
230+
keycode_left_meta = 0xe3, /* Keyboard Left GUI */
231+
keycode_right_control = 0xe4, /* Keyboard Right Control */
232+
keycode_right_shift = 0xe5, /* Keyboard Right Shift */
233+
keycode_right_alt = 0xe6, /* Keyboard Right Alt */
234+
keycode_right_meta = 0xe7, /* Keyboard Right GUI */
235+
236+
keycode_media_play_pause = 0xe8, /* Keyboard Media Play/Plause */
237+
keycode_media_stop_cd = 0xe9, /* Keyboard Media StopCD */
238+
keycode_media_previous = 0xea, /* Keyboard Media Previous */
239+
keycode_media_next = 0xeb, /* Keyboard Media Next */
240+
keycode_media_eject_cd = 0xec, /* Keyboard Media Eject CD */
241+
keycode_media_volume_up = 0xed, /* Keyboard Media Volume UP */
242+
keycode_media_volume_down = 0xee, /* Keyboard Media Volume Down */
243+
keycode_media_mute = 0xef, /* Keyboard Media Mute */
244+
keycode_media_www = 0xf0, /* Keyboard Media WWW Web Browser */
245+
keycode_media_back = 0xf1, /* Keyboard Media Back */
246+
keycode_media_forward = 0xf2, /* Keyboard Media Forward */
247+
keycode_media_stop = 0xf3, /* Keyboard Media Stop */
248+
keycode_media_find = 0xf4, /* Keyboard Media Find */
249+
keycode_media_scrollup = 0xf5, /* Keyboard Media Scroll Up */
250+
keycode_media_Scrolldown = 0xf6, /* Keyboard Media Scroll Down */
251+
keycode_media_edit = 0xf7, /* Keyboard Media Edit */
252+
keycode_media_sleep = 0xf8, /* Keyboard Media Sleep */
253+
keycode_media_coffee = 0xf9, /* Keyboard Media Coffee */
254+
keycode_media_refresh = 0xfa, /* Keyboard Media Refresh */
255+
keycode_media_calculator = 0xfb, /* Keyboard Media Calc */
256+
};

0 commit comments

Comments
 (0)