1
10
13
14
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
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
148
149
150
151
153
154
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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
275
276
277
288
289
290
291
292
293
294
295
296
297
298
299
300
301
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
324
325
329
333
334
335
336
337
338
344
345
346
348
349
350
351
352
353
354
361
362
363
364
365
366
373
374
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
446
447
450
451
452
453
454
455
456
457
458
459
460
461
462
468
469
470
471
472
479
480
483
484
485
486
487
488
489
490
491
492
493
496
497
500
501
502
503
504
509
510
513
514
515
516
517
518
519
520
521
525
526
529
530
531
532
534
535
536
537
538
539
540
541
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
575
576
579
580
581
582
583
584
...
...
...
#define UX_SOURCE_CODE
#include "ux_api.h"
#include "ux_host_class_hid.h"
#include "ux_host_class_hid_keyboard.h"
#include "ux_host_stack.h"
...
...
VOID _ux_host_class_hid_keyboard_callback(UX_HOST_CLASS_HID_REPORT_CALLBACK *callback)
{
/* ... */
const ULONG alternate_key_bits[] = {
UX_HID_KEYBOARD_STATE_CAPS_LOCK,
UX_HID_KEYBOARD_STATE_NUM_LOCK,
UX_HID_KEYBOARD_STATE_SCROLL_LOCK,
UX_HID_KEYBOARD_STATE_LEFT_CTRL,
UX_HID_KEYBOARD_STATE_LEFT_SHIFT,
UX_HID_KEYBOARD_STATE_LEFT_ALT,
UX_HID_KEYBOARD_STATE_LEFT_GUI,
UX_HID_KEYBOARD_STATE_RIGHT_CTRL,
UX_HID_KEYBOARD_STATE_RIGHT_SHIFT,
UX_HID_KEYBOARD_STATE_RIGHT_ALT,
UX_HID_KEYBOARD_STATE_RIGHT_GUI,
...};
#define ALTERNATE_KEY_BITS_IDX_CAPS_LOCK ( 0)
#define ALTERNATE_KEY_BITS_IDX_NUM_LOCK ( 1)
#define ALTERNATE_KEY_BITS_IDX_SCROLL_LOCK ( 2)
#define ALTERNATE_KEY_BITS_IDX_LEFT_CTRL ( 3)
#define ALTERNATE_KEY_BITS_IDX_LEFT_SHIFT ( 4)
#define ALTERNATE_KEY_BITS_IDX_LEFT_ALT ( 5)
#define ALTERNATE_KEY_BITS_IDX_LEFT_GUI ( 6)
#define ALTERNATE_KEY_BITS_IDX_RIGHT_CTRL ( 7)
#define ALTERNATE_KEY_BITS_IDX_RIGHT_SHIFT ( 8)
#define ALTERNATE_KEY_BITS_IDX_RIGHT_ALT ( 9)
#define ALTERNATE_KEY_BITS_IDX_RIGHT_GUI (10)
#define GET_ALTERNATE_KEY_BITS_IDX(usage) ((usage) - UX_HID_MODIFIER_KEY_LEFT_CONTROL + ALTERNATE_KEY_BITS_IDX_LEFT_CTRL)
#define KEY_STATE_REGULAR (11)
#define KEY_STATE_NO_KEY (12)
#define KEY_UP ( 0)
#define KEY_KEEP ( 1)
#define KEY_DOWN ( 2)
#define KEY_DEL ( 3)
18 defines
UX_HOST_CLASS_HID_CLIENT *hid_client;
UX_HOST_CLASS_HID_KEYBOARD *keyboard_instance;
UX_HOST_CLASS_HID_KEYBOARD_LAYOUT *keyboard_layout;
UCHAR *keypad_array;
ULONG *array_head;
ULONG *array_tail;
ULONG *array_end;
ULONG *array_start;
ULONG *report_buffer;
ULONG *report_buffer_end;
ULONG keyboard_char = 0;
ULONG shift_on;
ULONG capslock_on;
ULONG numlock_on;
ULONG key_usage;
ULONG key_value;
/* ... */
UINT key_state;
#if !defined(UX_HOST_CLASS_HID_KEYBOARD_EVENTS_KEY_CHANGES_MODE)
UCHAR *previous_lock_key_states;
UCHAR *current_lock_key_states;/* ... */
#else
UINT i, i_save, new_count;
UCHAR *state_usage;
UCHAR *state_value;
UCHAR *state_action;/* ... */
#endif
hid_client = callback -> ux_host_class_hid_report_callback_client;
keyboard_instance = (UX_HOST_CLASS_HID_KEYBOARD *) hid_client -> ux_host_class_hid_client_local_instance;
report_buffer = (ULONG *)callback -> ux_host_class_hid_report_callback_buffer;
report_buffer_end = &report_buffer[callback -> ux_host_class_hid_report_callback_actual_length];
#if !defined(UX_HOST_CLASS_HID_KEYBOARD_EVENTS_KEY_CHANGES_MODE)
previous_lock_key_states = &keyboard_instance -> ux_host_class_hid_keyboard_key_state[0];
/* ... */
current_lock_key_states = &keyboard_instance -> ux_host_class_hid_keyboard_key_state[3];
_ux_utility_memory_set(current_lock_key_states, 0, 3);
while(report_buffer < report_buffer_end)
{
key_usage = *report_buffer ++;
key_value = *report_buffer ++;
key_usage &= 0xFF;
#else
state_usage = keyboard_instance -> ux_host_class_hid_keyboard_key_state;
state_value = state_usage + keyboard_instance -> ux_host_class_hid_keyboard_key_count * 2;
state_action = state_value + keyboard_instance -> ux_host_class_hid_keyboard_key_count * 2;
_ux_utility_memory_set(state_usage + keyboard_instance -> ux_host_class_hid_keyboard_key_count, 0, keyboard_instance -> ux_host_class_hid_keyboard_key_count);
_ux_utility_memory_set(state_value + keyboard_instance -> ux_host_class_hid_keyboard_key_count, 0, keyboard_instance -> ux_host_class_hid_keyboard_key_count);
_ux_utility_memory_set(state_action, KEY_DEL, keyboard_instance -> ux_host_class_hid_keyboard_key_count * 2);
new_count = keyboard_instance -> ux_host_class_hid_keyboard_key_count;
while(report_buffer < report_buffer_end)
{
key_usage = *report_buffer ++;
key_value = *report_buffer ++;
key_usage &= 0xFF;
key_value &= 0xFF;
if (key_usage <= UX_HID_KEYBOARD_PHANTOM_STATE)
continue;
for (i = 0; i < keyboard_instance -> ux_host_class_hid_keyboard_key_count; i ++)
{
if (state_usage[i] == key_usage)
{
state_action[i] = (state_value[i] == key_value) ? KEY_KEEP : (key_value ? KEY_DOWN : KEY_UP);
state_value[i] = key_value;
break;
}if (state_usage[i] == key_usage) { ... }
}for (i = 0; i < keyboard_instance -> ux_host_class_hid_keyboard_key_count; i ++) { ... }
if (i == keyboard_instance -> ux_host_class_hid_keyboard_key_count)
{
state_usage [new_count] = key_usage;
state_value [new_count] = key_value;
state_action[new_count] = key_value ? KEY_DOWN : KEY_KEEP;
new_count ++;
}if (i == keyboard_instance -> ux_host_class_hid_keyboard_key_count) { ... }
}while (report_buffer < report_buffer_end) { ... }
i_save = 0;
for (i = 0; i < new_count; i ++)
{
key_usage = state_usage[i];
key_value = state_value[i];
key_state = state_action[i];
if (key_usage == 0)
continue;
if (key_state == KEY_DEL)
{
state_usage[i] = 0;
state_action[i] = KEY_UP;
}if (key_state == KEY_DEL) { ... }
else
{
if (i_save < i)
{
state_usage[i_save] = key_usage;
state_value[i_save] = key_value;
}if (i_save < i) { ... }
i_save ++;
}else { ... }
if (state_action[i] == KEY_KEEP)
continue;
keyboard_instance -> ux_host_class_hid_keyboard_alternate_key_state &= ~(UX_HID_KEYBOARD_STATE_FUNCTION | UX_HID_KEYBOARD_STATE_KEY_UP);
#endif
switch(key_usage)
{
/* ... */
case UX_HID_MODIFIER_KEY_LEFT_SHIFT :
case UX_HID_MODIFIER_KEY_RIGHT_SHIFT :
case UX_HID_MODIFIER_KEY_LEFT_ALT :
case UX_HID_MODIFIER_KEY_RIGHT_ALT :
case UX_HID_MODIFIER_KEY_RIGHT_CONTROL :
case UX_HID_MODIFIER_KEY_LEFT_CONTROL :
case UX_HID_MODIFIER_KEY_RIGHT_GUI :
case UX_HID_MODIFIER_KEY_LEFT_GUI :
key_state = GET_ALTERNATE_KEY_BITS_IDX(key_usage);
if (key_value > 0)
keyboard_instance -> ux_host_class_hid_keyboard_alternate_key_state |= alternate_key_bits[key_state];
else
keyboard_instance -> ux_host_class_hid_keyboard_alternate_key_state &= ~alternate_key_bits[key_state];
break;
/* ... */
case UX_HID_MODIFIER_KEY_LEFT_GUI :
case UX_HID_LED_KEY_CAPS_LOCK :
key_state = ALTERNATE_KEY_BITS_IDX_CAPS_LOCK;
break;
case UX_HID_LED_KEY_CAPS_LOCK :
case UX_HID_LED_KEY_NUM_LOCK :
key_state = ALTERNATE_KEY_BITS_IDX_NUM_LOCK;
break;
case UX_HID_LED_KEY_NUM_LOCK :
case UX_HID_LED_KEY_SCROLL_LOCK :
key_state = ALTERNATE_KEY_BITS_IDX_SCROLL_LOCK;
break;
#if !defined(UX_HOST_CLASS_HID_KEYBOARD_EVENTS_KEY_CHANGES_MODE)
case UX_HID_KEYBOARD_NO_KEY :
case UX_HID_KEYBOARD_PHANTOM_STATE :
key_state = KEY_STATE_NO_KEY;
break;/* ... */
#endif
/* ... */
default :
key_state = KEY_STATE_REGULAR;
if (keyboard_instance -> ux_host_class_hid_keyboard_keys_decode_disable == UX_TRUE)
{
keyboard_char = key_value;
break;
}if (keyboard_instance -> ux_host_class_hid_keyboard_keys_decode_disable == UX_TRUE) { ... }
keyboard_layout = keyboard_instance -> ux_host_class_hid_keyboard_layout;
if (key_value > keyboard_layout -> ux_host_class_hid_keyboard_layout_keys_upper_range)
{
key_state = KEY_STATE_NO_KEY;
break;
}if (key_value > keyboard_layout -> ux_host_class_hid_keyboard_layout_keys_upper_range) { ... }
/* ... */
shift_on = (keyboard_instance -> ux_host_class_hid_keyboard_alternate_key_state & UX_HID_KEYBOARD_STATE_SHIFT) ? UX_TRUE : UX_FALSE;
capslock_on = (keyboard_instance -> ux_host_class_hid_keyboard_alternate_key_state & UX_HID_KEYBOARD_STATE_CAPS_LOCK) ? UX_TRUE : UX_FALSE;
numlock_on = (keyboard_instance -> ux_host_class_hid_keyboard_alternate_key_state & UX_HID_KEYBOARD_STATE_NUM_LOCK) ? UX_TRUE : UX_FALSE;
if (key_value >= keyboard_layout -> ux_host_class_hid_keyboard_layout_letters_lower_range &&
key_value <= keyboard_layout -> ux_host_class_hid_keyboard_layout_letters_upper_range)
{
if (shift_on != capslock_on)
keyboard_char = keyboard_layout -> ux_host_class_hid_keyboard_layout_shift_array[key_value];
else
keyboard_char = keyboard_layout -> ux_host_class_hid_keyboard_layout_regular_array[key_value];
break;
}if (key_value >= keyboard_layout -> ux_host_class_hid_keyboard_layout_letters_lower_range && key_value <= keyboard_layout -> ux_host_class_hid_keyboard_layout_letters_upper_range) { ... }
if (key_value >= keyboard_layout -> ux_host_class_hid_keyboard_layout_keypad_lower_range &&
key_value <= keyboard_layout -> ux_host_class_hid_keyboard_layout_keypad_upper_range)
{
if (numlock_on)
keypad_array = keyboard_layout -> ux_host_class_hid_keyboard_layout_numlock_on_array;
else
keypad_array = keyboard_layout -> ux_host_class_hid_keyboard_layout_numlock_off_array;
keyboard_char = keypad_array[key_value -
keyboard_layout -> ux_host_class_hid_keyboard_layout_keypad_lower_range];
break;
}if (key_value >= keyboard_layout -> ux_host_class_hid_keyboard_layout_keypad_lower_range && key_value <= keyboard_layout -> ux_host_class_hid_keyboard_layout_keypad_upper_range) { ... }
if (shift_on)
keyboard_char = keyboard_layout -> ux_host_class_hid_keyboard_layout_shift_array[key_value];
else
keyboard_char = keyboard_layout -> ux_host_class_hid_keyboard_layout_regular_array[key_value];
break;
default
}switch (key_usage) { ... }
#if defined(UX_HOST_CLASS_HID_KEYBOARD_EVENTS_KEY_CHANGES_MODE)
if (state_action[i] == KEY_UP)
#if defined(UX_HOST_CLASS_HID_KEYBOARD_EVENTS_KEY_CHANGES_MODE_REPORT_KEY_DOWN_ONLY)
continue;/* ... */
#else
keyboard_instance -> ux_host_class_hid_keyboard_alternate_key_state |= UX_HID_KEYBOARD_STATE_KEY_UP;/* ... */
#endif/* ... */
#endif
if (key_state == KEY_STATE_NO_KEY)
continue;
if (key_state <= ALTERNATE_KEY_BITS_IDX_SCROLL_LOCK)
{
if (keyboard_instance -> ux_host_class_hid_keyboard_keys_decode_disable == UX_TRUE)
{
keyboard_char = key_value;
}if (keyboard_instance -> ux_host_class_hid_keyboard_keys_decode_disable == UX_TRUE) { ... }
else
{
#if !defined(UX_HOST_CLASS_HID_KEYBOARD_EVENTS_KEY_CHANGES_MODE)
current_lock_key_states[key_state] = (UCHAR)key_value;
/* ... */
if (previous_lock_key_states[key_state] == 0)/* ... */
#elif !defined(UX_HOST_CLASS_HID_KEYBOARD_EVENTS_KEY_CHANGES_MODE_REPORT_KEY_DOWN_ONLY)
if (state_action[i] == KEY_DOWN)/* ... */
#endif
{
if (keyboard_instance -> ux_host_class_hid_keyboard_alternate_key_state & alternate_key_bits[key_state])
keyboard_instance -> ux_host_class_hid_keyboard_alternate_key_state &= (ULONG)~alternate_key_bits[key_state];
else
keyboard_instance -> ux_host_class_hid_keyboard_alternate_key_state |= alternate_key_bits[key_state];
#if defined(UX_HOST_STANDALONE)
keyboard_instance -> ux_host_class_hid_keyboard_out_state = UX_STATE_WAIT;/* ... */
#else
_ux_host_semaphore_put(&keyboard_instance -> ux_host_class_hid_keyboard_semaphore);/* ... */
#endif
...}
#if defined(UX_HOST_CLASS_HID_KEYBOARD_EVENTS_KEY_CHANGES_MODE) && defined(UX_HOST_CLASS_HID_KEYBOARD_EVENTS_KEY_CHANGES_MODE_REPORT_LOCK_KEYS)
keyboard_char = key_usage;
keyboard_instance -> ux_host_class_hid_keyboard_alternate_key_state |= UX_HID_KEYBOARD_STATE_FUNCTION;
/* ... */
#else
continue;/* ... */
#endif
}else { ... }
}if (key_state <= ALTERNATE_KEY_BITS_IDX_SCROLL_LOCK) { ... }
else if (key_state < KEY_STATE_REGULAR)
{
#if defined(UX_HOST_CLASS_HID_KEYBOARD_EVENTS_KEY_CHANGES_MODE) && defined(UX_HOST_CLASS_HID_KEYBOARD_EVENTS_KEY_CHANGES_MODE_REPORT_MODIFIER_KEYS)
keyboard_char = key_usage;
keyboard_instance -> ux_host_class_hid_keyboard_alternate_key_state |= UX_HID_KEYBOARD_STATE_FUNCTION;/* ... */
#else
continue;/* ... */
#endif
}else if (key_state < KEY_STATE_REGULAR) { ... }
/* ... */
array_start = keyboard_instance -> ux_host_class_hid_keyboard_usage_array;
array_end = array_start + UX_HOST_CLASS_HID_KEYBOARD_USAGE_ARRAY_LENGTH;
array_head = keyboard_instance -> ux_host_class_hid_keyboard_usage_array_head;
array_tail = keyboard_instance -> ux_host_class_hid_keyboard_usage_array_tail;
/* ... */
if ((array_head + 2) >= array_end)
array_head = array_start;
else
array_head += 2;
if (array_head != array_tail)
{
*keyboard_instance -> ux_host_class_hid_keyboard_usage_array_head = keyboard_char;
*(keyboard_instance -> ux_host_class_hid_keyboard_usage_array_head + 1) = keyboard_instance -> ux_host_class_hid_keyboard_alternate_key_state;
keyboard_instance -> ux_host_class_hid_keyboard_usage_array_head = array_head;
}if (array_head != array_tail) { ... }
else
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_BUFFER_OVERFLOW);
}for (i = 0; i < new_count; i ++) { ... }
#if !defined(UX_HOST_CLASS_HID_KEYBOARD_EVENTS_KEY_CHANGES_MODE)
/* ... */
_ux_utility_memory_copy(previous_lock_key_states, current_lock_key_states, 3); /* ... */
#else
_ux_utility_memory_set(state_usage + i_save, 0, keyboard_instance -> ux_host_class_hid_keyboard_key_count - i_save); /* ... */
#endif
return;
}{ ... }