-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathlogitacker_keyboard_map.c
327 lines (272 loc) · 12.3 KB
/
logitacker_keyboard_map.c
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "helper.h"
#include "nrf_log_ctrl.h"
#include "stdlib.h"
#include "logitacker_keyboard_map.h"
#include "utf.h"
#define NRF_LOG_MODULE_NAME LOGITACKER_KEYBOARD_MAP
#include "nrf_log.h"
NRF_LOG_MODULE_REGISTER();
/* maps the given HID keycode to a string representation */
void modcode_to_str(char * p_result, const HID_mod_code_t modcode) {
ASSERT(p_result);
p_result[0] = 0;
if (modcode == 0x00) {
strcat(p_result, "NONE");
return;
}
uint8_t token_count = 0;
if ((modcode & HID_MOD_KEY_LEFT_CONTROL) > 0) {
if (token_count == 0) strcat(p_result, "(LEFT_CONTROL");
token_count++;
}
if ((modcode & HID_MOD_KEY_LEFT_SHIFT) > 0) {
if (token_count == 0) {
strcat(p_result, "(LEFT_SHIFT");
} else {
strcat(p_result, " | LEFT_SHIFT");
}
token_count++;
}
if ((modcode & HID_MOD_KEY_LEFT_ALT) > 0) {
if (token_count == 0) {
strcat(p_result, "(LEFT_ALT");
} else {
NRF_LOG_INFO("LEFT_ALT appended");
strcat(p_result, " | LEFT_ALT");
}
token_count++;
}
if ((modcode & HID_MOD_KEY_LEFT_GUI) > 0) {
if (token_count == 0) {
strcat(p_result, "(LEFT_GUI");
} else {
strcat(p_result, " | LEFT_GUI");
}
token_count++;
}
if ((modcode & HID_MOD_KEY_RIGHT_CONTROL) > 0) {
if (token_count == 0) {
strcat(p_result, "(RIGHT_CONTROL");
} else {
strcat(p_result, " | RIGHT_CONTROL");
}
token_count++;
}
if ((modcode & HID_MOD_KEY_RIGHT_SHIFT) > 0) {
if (token_count == 0) {
strcat(p_result, "(RIGHT_SHIFT");
} else {
strcat(p_result, " | RIGHT_SHIFT");
}
token_count++;
}
if ((modcode & HID_MOD_KEY_RIGHT_ALT) > 0) {
if (token_count == 0) {
strcat(p_result, "(RIGHT_ALT");
} else {
strcat(p_result, " | RIGHT_ALT");
}
token_count++;
}
if ((modcode & HID_MOD_KEY_RIGHT_GUI) > 0) {
if (token_count == 0) {
strcat(p_result, "(RIGHT_GUI");
} else {
strcat(p_result, " | RIGHT_GUI");
}
}
strcat(p_result, ")");
}
/* maps the given HID keycode to a string representation */
char* keycode_to_str(logitacker_keyboard_map_hid_keys_t keycode) {
switch (keycode) {
ALL_KEYCODES(KEYCODE_SWITCH_CASE)
default:
return "UNKNOWN HID KEY";
}
}
/* maps the given HID keycode string to hid keycode */
#define KEYCODE_IF_STRCMP(nameval, val) if (strcmp(STRINGIFY(nameval), key_str) == 0) return val;
logitacker_keyboard_map_hid_keys_t str_to_keycode(char * key_str) {
ALIAS_KEYCODES(KEYCODE_IF_STRCMP)
ALL_KEYCODES(KEYCODE_IF_STRCMP)
NRF_LOG_INFO("No mapping for %s", nrf_log_push(key_str));
return 0x00; // NONE
}
uint32_t logitacker_keyboard_map_combo_str_to_hid_report(char const *in_str,
hid_keyboard_report_t *p_out_report,
logitacker_keyboard_map_lang_t in_layout) {
VERIFY_TRUE(in_str != NULL, NRF_ERROR_NULL);
VERIFY_TRUE(p_out_report != NULL, NRF_ERROR_NULL);
//clear output report
memset(p_out_report, 0x00, sizeof(hid_keyboard_report_t));
//hid_keyboard_report_t tmp_report = {0};
char tmp[256];
char * str_copy = tmp;
uint8_t resultKeyCount = 0; // keeps track of number of keys added to the report (max are 6)
strncpy(str_copy, in_str, sizeof(tmp)-1);
// tokenize str
char * token;
int i = 0;
while (resultKeyCount < 6 && (token = helper_strsep(&str_copy, " ")) != NULL) {
NRF_LOG_INFO("Token %d: %s", i++, nrf_log_push(token));
// if the token has length 1, it is likely an ASCII char and we want to stay language agnostic (f.e. a 'Y' for DE layout should result in HID_KEY_Z)
if (strlen(token) == 1) {
// if [A-Z] turn to lower
if (token[0] >= 'A' && token[0] <= 'Z') token[0] += 0x20; // turn lower
//convert char to wchar
char tokenStr[2] = {token[0], 0x00};
uint32_t c_utf; //stores decoded unicode codepoint (wchar) of next UTF-8 rune
utf8DecodeRune(tokenStr, 0, &c_utf);
// retrieve HID reports for current wchar (language agnostic)
hid_keyboard_report_t *p_hid_report_sequence = NULL;
uint32_t hid_report_sequence_len = 0;
uint32_t err = logitacker_keyboard_map_wc_to_hid_reports(&p_hid_report_sequence, &hid_report_sequence_len, in_layout, c_utf);
if (err != NRF_SUCCESS || hid_report_sequence_len == 0) continue; // skip this token if it doesn't result in a report sequence
// we only regard the first report of the sequence in our combo (no dead key support on this path, multiple calls to press have to be used to emulate dead keys)
p_out_report->mod |= p_hid_report_sequence[0].mod; //copy modifier
for (uint8_t keypos=0; keypos < 6 && resultKeyCount < 6; keypos++) {
uint8_t keyCode = p_hid_report_sequence[0].keys[keypos];
if (keyCode != 0x00) {
// no empty key, add to resulting report
p_out_report->keys[resultKeyCount++] = keyCode;
}
}
continue; // go on with next token
} // end of handling of single char tokens
// try to map token directly to HID keycode
uint8_t keyCode = str_to_keycode(token);
if (keyCode != 0x00) {
// if we have a modifier key (0xe0..0xe7) handle it like this, otherwise handle it as hid_key
if (keyCode > 0xdf && keyCode < 0xe8) {
// translate keyCode corresponding to modifier key to respective mask bit in modifier byte of output report
switch (keyCode) {
case HID_KEY_RIGHTALT:
p_out_report->mod |= HID_MOD_KEY_RIGHT_ALT;
break;
case HID_KEY_RIGHTCTRL:
p_out_report->mod |= HID_MOD_KEY_RIGHT_CONTROL;
break;
case HID_KEY_RIGHTSHIFT:
p_out_report->mod |= HID_MOD_KEY_RIGHT_SHIFT;
break;
case HID_KEY_RIGHTMETA:
p_out_report->mod |= HID_MOD_KEY_RIGHT_GUI;
break;
case HID_KEY_LEFTALT:
p_out_report->mod |= HID_MOD_KEY_LEFT_ALT;
break;
case HID_KEY_LEFTCTRL:
p_out_report->mod |= HID_MOD_KEY_LEFT_CONTROL;
break;
case HID_KEY_LEFTSHIFT:
p_out_report->mod |= HID_MOD_KEY_LEFT_SHIFT;
break;
case HID_KEY_LEFTMETA:
p_out_report->mod |= HID_MOD_KEY_LEFT_GUI;
break;
default:
break;
}
} else {
// no empty key, add to resulting report
p_out_report->keys[resultKeyCount++] = keyCode;
}
}
}
return NRF_SUCCESS;
}
#define LAYOUT_SWITCH_CASE(nameval, val) case nameval: {*p_out_report_seq=(void*)val; *out_rep_seq_len=sizeof(val) ;return NRF_SUCCESS; }
/* maps the given wchar to respective HID report sequence (currently only US,DE layout) */
uint32_t logitacker_keyboard_map_wc_to_hid_reports(hid_keyboard_report_t **p_out_report_seq, uint32_t *out_rep_seq_len,
logitacker_keyboard_map_lang_t in_layout, wchar_t in_rune) {
if (in_layout == LANGUAGE_LAYOUT_US) {
switch (in_rune) {
LAYOUT_US(LAYOUT_SWITCH_CASE)
default:
return NRF_ERROR_INVALID_PARAM;
}
} else if (in_layout == LANGUAGE_LAYOUT_DE) {
switch (in_rune) {
LAYOUT_DE(LAYOUT_SWITCH_CASE)
default:
return NRF_ERROR_INVALID_PARAM;
}
} else if (in_layout == LANGUAGE_LAYOUT_DA) {
switch (in_rune) {
LAYOUT_DA(LAYOUT_SWITCH_CASE)
default:
return NRF_ERROR_INVALID_PARAM;
}
} else if (in_layout == LANGUAGE_LAYOUT_FR) {
switch (in_rune) {
LAYOUT_FR(LAYOUT_SWITCH_CASE)
default:
return NRF_ERROR_INVALID_PARAM;
}
} else {
return NRF_ERROR_INVALID_PARAM;
}
return NRF_SUCCESS;
}
/* Iterator, parses a rune from null terminated in_str on every call and returns an array of HID keyboard reports
* which hold the needed key-presses to produce this rune (with respect to language layout
* - input p_ctx : context with iterator data (f.e. current position in string)
* - input in_str : the UTF-8 encoded string to use (support for non ASCII characters like 'Ü' depends on language layout)
* - output p_out_next_report_seq : pointer to array of resulting keyboard reports for current rune
* - output out_next_rep_seq_len : overall size of resulting hid_keyboard_report_t[]
* - input in_layout : keyboard language layout to use (currently LANGUAGE_LAYOUT_US / LANGUAGE_LAYOUT_DE)
*/
// ToDo: append a key release report to every sequence after a rune
uint32_t logitacker_keyboard_map_u8_str_to_hid_reports(logitacker_keyboard_map_u8_str_parser_ctx_t *p_ctx, char const *in_str,
hid_keyboard_report_t **p_out_next_report_seq,
uint32_t *out_next_rep_seq_len,
logitacker_keyboard_map_lang_t in_layout) {
// ToDo: error checks for NULL params
if (p_ctx->p_pos == NULL) {
// first run, set to start of string
p_ctx->p_pos = in_str;
p_ctx->append_release = false;
}
// check if we need to add a key release frame for this iteration
if (p_ctx->append_release) {
*p_out_next_report_seq = (void*) HID_REPORT_SEQUENCE_RELEASE;
*out_next_rep_seq_len = sizeof(HID_REPORT_SEQUENCE_RELEASE);
p_ctx->append_release = false;
return NRF_SUCCESS;
}
// if byte at p_pos is 0x00 we reached the end of the zero terminated string during last call and return an error this time
if (*p_ctx->p_pos == 0x00) {
// reached end of string
p_ctx->p_pos = NULL; //reusable
*p_out_next_report_seq = (void*) HID_REPORT_SEQUENCE_RELEASE;
*out_next_rep_seq_len = sizeof(HID_REPORT_SEQUENCE_RELEASE);
return NRF_ERROR_NULL;
}
uint32_t c_utf; //stores decoded unicode codepoint (wchar) of next UTF-8 rune
//read UTF-8 rune and advance p_pos to next one
p_ctx->p_pos = utf8DecodeRune(p_ctx->p_pos, 0, &c_utf);
//ToDo: check if c_utf contains an error value
// map rune to output reports
if (logitacker_keyboard_map_wc_to_hid_reports(p_out_next_report_seq, out_next_rep_seq_len, in_layout, c_utf) == NRF_SUCCESS) {
p_ctx->append_release=true;
// reports are updated, let's return success
return NRF_SUCCESS;
} else {
// something went wrong, likely we can't translate, we return success anyways, but with a KEY_RELEASE report
// sequence (this allows going on with the remaining string, in case a mapping for a single rune is missing)
*p_out_next_report_seq = (void*) HID_REPORT_SEQUENCE_RELEASE;
*out_next_rep_seq_len = sizeof(HID_REPORT_SEQUENCE_RELEASE);
return NRF_SUCCESS;
}
}
logitacker_keyboard_map_lang_t logitacker_keyboard_map_lang_from_str(char * lang_str) {
if (lang_str == NULL) goto lab_default;
if (strcmp(lang_str, "de") == 0 || strcmp(lang_str, "DE") == 0 ) return LANGUAGE_LAYOUT_DE;
if (strcmp(lang_str, "us") == 0 || strcmp(lang_str, "US") == 0 ) return LANGUAGE_LAYOUT_US;
if (strcmp(lang_str, "da") == 0 || strcmp(lang_str, "DA") == 0 ) return LANGUAGE_LAYOUT_DA;
if (strcmp(lang_str, "fr") == 0 || strcmp(lang_str, "FR") == 0 ) return LANGUAGE_LAYOUT_FR;
lab_default:
NRF_LOG_WARNING("unknown language layout '%s' ... using 'us' as default", nrf_log_push(lang_str));
return LANGUAGE_LAYOUT_US; // default
}