-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathoutput_usb.c
1304 lines (1092 loc) · 30 KB
/
output_usb.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2011-2019 by Jacob Alexander
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// ----- Includes -----
// Compiler Includes
#include <Lib/OutputLib.h>
// Project Includes
#include <cli.h>
#include <hidio_com.h>
#include <latency.h>
#include <led.h>
#include <print.h>
#include <scan_loop.h>
// USB Includes
#if defined(_avr_at_)
#include "avr/usb_keyboard_serial.h"
#elif defined(_kinetis_) || defined(_sam_)
#include "arm/usb_dev.h"
#include "arm/usb_keyboard.h"
#include "arm/usb_mouse.h"
#include "arm/usb_rawio.h"
#endif
// KLL
#include <kll_defs.h>
#include <kll.h>
// Interface Includes
#include <output_com.h>
// ----- Macros -----
// Used to build a bitmap lookup table from a byte addressable array
#define byteLookup( byte ) \
case (( byte ) * ( 8 )): bytePosition = byte; byteShift = 0; break; \
case (( byte ) * ( 8 ) + ( 1 )): bytePosition = byte; byteShift = 1; break; \
case (( byte ) * ( 8 ) + ( 2 )): bytePosition = byte; byteShift = 2; break; \
case (( byte ) * ( 8 ) + ( 3 )): bytePosition = byte; byteShift = 3; break; \
case (( byte ) * ( 8 ) + ( 4 )): bytePosition = byte; byteShift = 4; break; \
case (( byte ) * ( 8 ) + ( 5 )): bytePosition = byte; byteShift = 5; break; \
case (( byte ) * ( 8 ) + ( 6 )): bytePosition = byte; byteShift = 6; break; \
case (( byte ) * ( 8 ) + ( 7 )): bytePosition = byte; byteShift = 7; break
// ----- Enumerations -----
typedef enum {
OutputReset_None = 0, // Do nothing
OutputReset_Restart = 1, // Clear USB stack and restart the firmware
OutputReset_Bootloader = 2, // Clear USB stack and jump to bootloader
} OutputReset;
// ----- Function Declarations -----
void cliFunc_idle ( char* args );
void cliFunc_kbdProtocol( char* args );
void cliFunc_readLEDs ( char* args );
void cliFunc_usbAddr ( char* args );
void cliFunc_usbConf ( char* args );
void cliFunc_usbInitTime( char* args );
void cliFunc_usbErrors ( char* args );
// ----- Variables -----
// Output Module command dictionary
CLIDict_Entry( idle, "Show/set the HID Idle time (multiples of 4 ms)." );
CLIDict_Entry( kbdProtocol, "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode." );
CLIDict_Entry( readLEDs, "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc." );
CLIDict_Entry( usbAddr, "Shows the negotiated USB unique Id, given to device by host." );
CLIDict_Entry( usbConf, "Shows whether USB is configured or not." );
CLIDict_Entry( usbInitTime, "Displays the time in ms from usb_init() till the last setup call." );
CLIDict_Entry( usbErrors, "Displays number of usb errors since startup." );
CLIDict_Def( usbCLIDict, "USB Module Commands" ) = {
CLIDict_Item( idle ),
CLIDict_Item( kbdProtocol ),
CLIDict_Item( readLEDs ),
CLIDict_Item( usbAddr ),
CLIDict_Item( usbConf ),
CLIDict_Item( usbInitTime ),
CLIDict_Item( usbErrors ),
{ 0, 0, 0 } // Null entry for dictionary end
};
// USBKeys Keyboard Buffer
volatile USBKeys USBKeys_primary; // Primary send buffer
volatile USBKeys USBKeys_idle; // Idle timeout send buffer
// The number of keys sent to the usb in the array
volatile uint8_t USBKeys_Sent;
// 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
volatile uint8_t USBKeys_LEDs;
volatile uint8_t USBKeys_LEDs_prev;
// USBMouse Buffer
volatile USBMouse USBMouse_primary; // Primary mouse send buffer
// Protocol setting from the host.
// 0 - Boot Mode
// 1 - NKRO Mode (Default, unless set by a BIOS or boot interface)
volatile uint8_t USBKeys_Protocol = USBProtocol_define;
volatile uint8_t USBKeys_Protocol_New = USBProtocol_define;
volatile uint8_t USBKeys_Protocol_Change; // New value to set to USBKeys_Protocol if _Change is set
// the idle configuration, how often we send the report to the
// host (ms * 4) even when it hasn't changed
// 0 - Disables
volatile uint8_t USBKeys_Idle_Config = USBIdle_define;
// Count until idle timeout
volatile uint32_t USBKeys_Idle_Expiry;
volatile uint8_t USBKeys_Idle_Count;
// USB Init Time (ms) - usb_init()
volatile uint32_t USBInit_TimeStart;
volatile uint32_t USBInit_TimeEnd;
volatile uint16_t USBInit_Ticks;
// USB Address - Set by host, unique to the bus
volatile uint8_t USBDev_Address;
// USB Errors
volatile uint32_t USBStatus_FrameErrors;
// Scheduled USB resets, used to clear USB packets before bringing down the USB stack
// This is useful for OSs like Windows where then OS doesn't clear the current state
// after the keyboard is disconnected (i.e. Ctrl keeps being held until Ctrl is pressed again).
volatile static uint8_t Output_reset_schedule;
// Latency measurement resource
static uint8_t outputPeriodicLatencyResource;
static uint8_t outputPollLatencyResource;
// ----- Capabilities -----
// Set Boot Keyboard Protocol
void Output_kbdProtocolBoot_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
#if enableKeyboard_define == 1
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Initial:
// Only use capability on press
break;
case CapabilityState_Debug:
// Display capability name
print("Output_kbdProtocolBoot()");
return;
default:
return;
}
// Only set if necessary
if ( USBKeys_Protocol == 0 )
return;
// Flush the key buffers
USB_flushBuffers();
// Set the keyboard protocol to Boot Mode
USBKeys_Protocol_New = 0;
USBKeys_Protocol_Change = 1;
#endif
}
// Set NKRO Keyboard Protocol
void Output_kbdProtocolNKRO_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
#if enableKeyboard_define == 1
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Initial:
// Only use capability on press
break;
case CapabilityState_Debug:
// Display capability name
print("Output_kbdProtocolNKRO()");
return;
default:
return;
}
// Only set if necessary
if ( USBKeys_Protocol == 1 )
return;
// Flush the key buffers
USB_flushBuffers();
// Set the keyboard protocol to NKRO Mode
USBKeys_Protocol_New = 1;
USBKeys_Protocol_Change = 1;
#endif
}
// Toggle Keyboard Protocol
void Output_toggleKbdProtocol_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
#if enableKeyboard_define == 1
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Last:
// Only use capability on release
break;
case CapabilityState_Debug:
// Display capability name
print("Output_toggleKbdProtocol()");
return;
default:
return;
}
// Flush the key buffers
USB_flushBuffers();
// Toggle the keyboard protocol Mode
USBKeys_Protocol_New = !USBKeys_Protocol;
USBKeys_Protocol_Change = 1;
#endif
}
// Sends a Consumer Control code to the USB Output buffer
void Output_consCtrlSend_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
#if enableKeyboard_define == 1
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Initial:
// Indicate changed
USBKeys_primary.changed |= USBKeyChangeState_Consumer;
break;
case CapabilityState_Any:
// Only set consumer code
break;
case CapabilityState_Last:
// Clear consumer code
USBKeys_primary.changed |= USBKeyChangeState_Consumer;
USBKeys_primary.cons_ctrl = 0;
return;
case CapabilityState_Debug:
// Display capability name
print("Output_consCtrlSend(consCode)");
// Read arg if not set to 0
if ( args != 0 )
{
uint16_t key = *(uint16_t*)(&args[0]);
print(" -> ");
printInt16( key );
}
return;
default:
return;
}
// Set consumer control code
USBKeys_primary.cons_ctrl = *(uint16_t*)(&args[0]);
#endif
}
// Ignores the given key status update
// Used to prevent fall-through, this is the None keyword in KLL
void Output_noneSend_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Debug:
// Display capability name
print("Output_noneSend()");
return;
default:
return;
}
// Nothing to do, because that's the point :P
}
// Sends a System Control code to the USB Output buffer
void Output_sysCtrlSend_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
#if enableKeyboard_define == 1
CapabilityState cstate = KLL_CapabilityState( state, stateType );
switch ( cstate )
{
case CapabilityState_Initial:
// Indicate changed
USBKeys_primary.changed |= USBKeyChangeState_System;
break;
case CapabilityState_Any:
// Only set consumer code
break;
case CapabilityState_Last:
// Clear system code
USBKeys_primary.changed |= USBKeyChangeState_System;
USBKeys_primary.sys_ctrl = 0;
return;
case CapabilityState_Debug:
// Display capability name
print("Output_sysCtrlSend(sysCode)");
// Read arg if not set to 0
if ( args != 0 )
{
uint8_t key = args[0];
print(" -> ");
printInt8( key );
}
return;
default:
return;
}
// Set system control code
USBKeys_primary.sys_ctrl = args[0];
#endif
}
// Adds a single USB Code to the USB Output buffer
// Argument #1: USB Code
void Output_usbCodeSend_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
#if enableKeyboard_define == 1
CapabilityState cstate = KLL_CapabilityState( state, stateType );
// Depending on which mode the keyboard is in the USB needs Press/Hold/Release events
uint8_t keyPress = 0; // Default to key release
switch ( cstate )
{
case CapabilityState_Initial:
// Indicate changed
keyPress = 1;
break;
case CapabilityState_Last:
break;
case CapabilityState_Debug:
// Display capability name
print("Output_usbCodeSend(usbCode)");
// Read arg if not set to 0
if ( args != 0 )
{
uint8_t key = args[0];
print(" -> ");
printInt8( key );
}
return;
default:
return;
}
// Get the keycode from arguments
uint8_t key = args[0];
// Extra USB Debug
if ( Output_DebugMode > 1 )
{
print("\033[1;34mUSB\033[0m ");
printInt8( key );
print(" ");
switch ( cstate )
{
case CapabilityState_Initial:
print("\033[1;33mP\033[0m");
break;
case CapabilityState_Last:
print("\033[1;35mR\033[0m");
break;
default:
break;
}
print( NL );
}
// Depending on which mode the keyboard is in, USBKeys_Keys array is used differently
// Boot mode - Maximum of 6 byte codes
// NKRO mode - Each bit of the 26 byte corresponds to a key
// Bits 0 - 3 unused
// Bits 4 - 164 (bytes 0 - 20) correspond to USB Codes 4 - 164 (Keyboard Section)
// Bits 165 - 175 unused
// Bits 176 - 221 (bytes 22 - 27) correspond to USB Codes 176 - 221 (Keypad Section)
// Bits 222 - 223 unused
uint8_t bytePosition = 0;
uint8_t byteShift = 0;
switch ( USBKeys_Protocol )
{
case 0: // Boot Mode
// Set the modifier bit if this key is a modifier
if ( (key & 0xE0) == 0xE0 ) // AND with 0xE0 (Left Ctrl, first modifier)
{
if ( keyPress )
{
USBKeys_primary.modifiers |= 1 << (key ^ 0xE0); // Left shift 1 by key XOR 0xE0
}
else // Release
{
USBKeys_primary.modifiers &= ~(1 << (key ^ 0xE0)); // Left shift 1 by key XOR 0xE0
}
USBKeys_primary.changed |= USBKeyChangeState_Modifiers;
}
// Normal USB Code
else
{
// Determine if key was set
uint8_t keyFound = 0;
for ( uint8_t newkey = 0; newkey < USBKeys_Sent; newkey++ )
{
// On press, key already present, don't re-add
if ( keyPress && USBKeys_primary.keys[newkey] == key )
{
keyFound = 1;
break;
}
// On release, remove if found
if ( !keyPress && USBKeys_primary.keys[newkey] == key )
{
// Shift keys over
for ( uint8_t pos = newkey; pos < USBKeys_Sent - 1; pos++ )
{
USBKeys_primary.keys[pos] = USBKeys_primary.keys[pos + 1];
}
USBKeys_Sent--;
keyFound = 1;
USBKeys_primary.changed = USBKeyChangeState_Keys;
break;
}
}
// USB Key limit reached
if ( USBKeys_Sent >= USB_BOOT_MAX_KEYS )
{
warn_printNL("USB Key limit reached");
break;
}
// Add key if not already found in the buffer
if ( keyPress && !keyFound )
{
USBKeys_primary.keys[USBKeys_Sent++] = key;
USBKeys_primary.changed = USBKeyChangeState_Keys;
}
}
break;
case 1: // NKRO Mode
// Set the modifier bit if this key is a modifier
if ( (key & 0xE0) == 0xE0 ) // AND with 0xE0 (Left Ctrl, first modifier)
{
if ( keyPress )
{
USBKeys_primary.modifiers |= 1 << (key ^ 0xE0); // Left shift 1 by key XOR 0xE0
}
else // Release
{
USBKeys_primary.modifiers &= ~(1 << (key ^ 0xE0)); // Left shift 1 by key XOR 0xE0
}
USBKeys_primary.changed |= USBKeyChangeState_Modifiers;
break;
}
// Handle Keyboard and Keypad Sections
else if ( key >= 1 && key <= 221 )
{
// Lookup (otherwise division or multiple checks are needed to do alignment)
// Starting at 0th position, each byte has 8 bits
switch ( key )
{
// Keyboard Section
byteLookup( 0 );
byteLookup( 1 );
byteLookup( 2 );
byteLookup( 3 );
byteLookup( 4 );
byteLookup( 5 );
byteLookup( 6 );
byteLookup( 7 );
byteLookup( 8 );
byteLookup( 9 );
byteLookup( 10 );
byteLookup( 11 );
byteLookup( 12 );
byteLookup( 13 );
byteLookup( 14 );
byteLookup( 15 );
byteLookup( 16 );
byteLookup( 17 );
byteLookup( 18 );
byteLookup( 19 );
byteLookup( 20 );
// Padding
// XXX (HaaTa) Not necessary to include
//byteLookup( 21 );
// Keypad Section
byteLookup( 22 );
byteLookup( 23 );
byteLookup( 24 );
byteLookup( 25 );
byteLookup( 26 );
byteLookup( 27 );
}
USBKeys_primary.changed |= USBKeyChangeState_Keys;
}
// Received 0x00
// This is a special USB Code that internally indicates a "break"
// It is used to send "nothing" in order to break up sequences of USB Codes
else if ( key == 0x00 )
{
USBKeys_primary.changed |= USBKeyChangeState_Keys;
break;
}
// Invalid key
else
{
warn_print("USB Code not within 4-155 (0x4-0x9B), 157-164 (0x9D-0xA4), 176-221 (0xB0-0xDD) or 224-231 (0xE0-0xE7) NKRO Mode: ");
printHex( key );
print( NL );
break;
}
// Set/Unset
if ( keyPress )
{
USBKeys_primary.keys[bytePosition] |= (1 << byteShift);
USBKeys_Sent--;
}
else // Release
{
USBKeys_primary.keys[bytePosition] &= ~(1 << byteShift);
USBKeys_Sent++;
}
break;
}
#endif
}
void Output_usbCodeRelease_capability(TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args)
{
#if enableKeyboard_define == 1 && disable_usbCodeRelease_define == 0
CapabilityState cstate = KLL_CapabilityState(state, stateType);
// Release, only on initial trigger
switch (cstate)
{
case CapabilityState_Initial:
break;
case CapabilityState_Debug:
// Display capability name
print("Output_usbCodeRelease(usbCode)");
// Read arg if not set to 0
if (args != 0)
{
uint8_t key = args[0];
print(" -> ");
printInt8(key);
}
return;
case CapabilityState_Last:
default:
return;
}
// Force release
state = ScheduleType_R;
// Use capability to release key
Output_usbCodeSend_capability(trigger, state, stateType, args);
#endif
}
#if enableMouse_define == 1
// Sends a mouse command over the USB Output buffer
// XXX This function *will* be changing in the future
// If you use it, be prepared that your .kll files will break in the future (post KLL 0.5)
// Argument #1: USB Mouse Button (16 bit)
// Argument #2: USB X Axis (16 bit) relative
// Argument #3: USB Y Axis (16 bit) relative
void Output_usbMouse_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
// Determine which mouse button was sent
// The USB spec defines up to a max of 0xFFFF buttons
// The usual are:
// 1 - Button 1 - (Primary)
// 2 - Button 2 - (Secondary)
// 3 - Button 3 - (Tertiary)
uint16_t mouse_button = *(uint16_t*)(&args[0]);
// X/Y Relative Axis
int16_t mouse_x = *(int16_t*)(&args[2]);
int16_t mouse_y = *(int16_t*)(&args[4]);
// Adjust for bit shift
uint16_t mouse_button_shift = mouse_button - 1;
switch ( cstate )
{
case CapabilityState_Initial:
case CapabilityState_Any:
// Press/Hold
if ( mouse_button )
{
USBMouse_primary.buttons |= (1 << mouse_button_shift);
}
if ( mouse_x )
{
USBMouse_primary.relative_x = mouse_x;
}
if ( mouse_y )
{
USBMouse_primary.relative_y = mouse_y;
}
break;
case CapabilityState_Last:
// Release
if ( mouse_button )
{
USBMouse_primary.buttons &= ~(1 << mouse_button_shift);
}
break;
case CapabilityState_Debug:
// Display capability name
print("Output_usbMouse(mouseButton,relX,relY)");
return;
default:
return;
}
// Trigger updates
if ( mouse_button )
{
USBMouse_primary.changed |= USBMouseChangeState_Buttons;
}
if ( mouse_x || mouse_y )
{
USBMouse_primary.changed |= USBMouseChangeState_Relative;
}
}
// Sends a mouse wheel command over USB Output buffer
// XXX This function *will* be changing in the future
// If you use it, be prepared that your .kll files will break in the future (post KLL 0.5)
// Argument #1: USB Vertical Wheel (8 bit)
// Argument #2: USB Horizontal Wheel (8 bit)
void Output_usbMouseWheel_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
CapabilityState cstate = KLL_CapabilityState( state, stateType );
// Vertical and horizontal mouse wheels
int8_t wheel_vert = *(int8_t*)(&args[0]);
int8_t wheel_hori = *(int8_t*)(&args[2]);
switch ( cstate )
{
case CapabilityState_Initial:
case CapabilityState_Any:
// Press/Hold
if ( wheel_vert )
{
USBMouse_primary.vertwheel = wheel_vert;
USBMouse_primary.changed |= USBMouseChangeState_WheelVert;
}
if ( wheel_hori )
{
USBMouse_primary.horiwheel = wheel_hori;
USBMouse_primary.changed |= USBMouseChangeState_WheelHori;
}
break;
case CapabilityState_Debug:
// Display capability name
print("Output_usbMouseWheel(vert,hori)");
return;
default:
return;
}
}
#endif
// ----- Functions -----
// Flush Key buffers
void USB_flushBuffers()
{
// Zero out USBKeys buffers
memset( (void*)&USBKeys_primary, 0, sizeof( USBKeys ) );
memset( (void*)&USBKeys_idle, 0, sizeof( USBKeys ) );
// Clear idle timeout state
USBKeys_Idle_Expiry = 0;
USBKeys_Idle_Count = 0;
// Reset USBKeys_Keys size
USBKeys_Sent = 0;
// Clear mouse state
USBMouse_primary.buttons = 0;
USBMouse_primary.relative_x = 0;
USBMouse_primary.relative_y = 0;
USBMouse_primary.vertwheel = 0;
USBMouse_primary.horiwheel = 0;
USBMouse_primary.changed = 0;
// Make sure everything actually flushes
USBKeys_primary.changed = 1;
USBKeys_idle.changed = 1;
USBMouse_primary.changed = 1;
}
// USB Module Setup
inline void USB_setup()
{
// Reset frame error counter
USBStatus_FrameErrors = 0;
// Initialize the USB
// If a USB connection does not exist, just ignore it
// All usb related functions will non-fatally fail if called
// If the USB initialization is delayed, then functionality will just be delayed
usb_init();
// Register USB Output CLI dictionary
CLI_registerDictionary( usbCLIDict, usbCLIDictName );
// USB Protocol Transition variable
USBKeys_Protocol_Change = 0;
// Clear USB LEDs (may be set by the OS rather quickly)
USBKeys_LEDs_prev = 0;
USBKeys_LEDs = 0;
// Clear USB address
USBDev_Address = 0;
// Clear USB reset state
Output_reset_schedule = OutputReset_None;
// Flush key buffers
USB_flushBuffers();
// Check if we need to disable secure bootloader mode
// This is done by setting both 32 bit Kiibohd specific VBAT secure register regions
#if SecureBootloader_define == 0
#if ( defined(_kii_v1_) || defined(_kii_v2_) )
VBAT_SECURE1 = 0;
VBAT_SECURE2 = 0;
#elif defined(_kii_v3_)
GPBR_SECURE1 = 0;
GPBR_SECURE2 = 0;
#endif
#endif
#if enableRawIO_define == 1
// Setup HID-IO
HIDIO_setup();
#endif
// Latency resource allocation
outputPeriodicLatencyResource = Latency_add_resource("USBOutputPeri", LatencyOption_Ticks);
outputPollLatencyResource = Latency_add_resource("USBOutputPoll", LatencyOption_Ticks);
}
// USB Data Poll
inline void USB_poll()
{
// Start latency measurement
Latency_start_time( outputPollLatencyResource );
// USB status checks
// Non-standard USB state manipulation, usually does nothing
usb_device_check();
// Re-send last usb keyboard state if we've passed the expiry time
// And the HID IDLE is set
usb_keyboard_idle_update();
#if enableRawIO_define == 1
// HID-IO Process
HIDIO_process();
#endif
// End latency measurement
Latency_end_time( outputPollLatencyResource );
}
// Check if USB is ready
// Returns 1 if ready, 0 if not
uint8_t USB_ready()
{
#if !defined(_host_)
return usb_configured();
#else
return 1;
#endif
}
// Gather USB HID LED states
// Keeps track of previous state, and sends new state to PartialMap
void USB_indicator_update()
{
// Check each bit of the indicator byte
for ( uint8_t bit = 0; bit < LED_KANA_5; bit++ )
{
uint8_t id = bit + 1; // Conversion to USB HID Indicator code
uint8_t cur = USBKeys_LEDs & (1 << bit);
uint8_t prev = USBKeys_LEDs_prev & (1 << bit);
// Detect if off
if ( cur == 0 && cur == prev )
{
continue;
}
// Detect if on
else if ( cur && cur == prev )
{
// On
Macro_ledState( id, ScheduleType_On );
}
// Detect if press
else if ( cur )
{
// TODO (HaaTa): Temporary Lock led control
#if Scan_KiraKeyboard_define == 1 && !defined(_host_)
switch ( id )
{
case LED_NUM_LOCK_1:
Scan_numlock(cur);
break;
case LED_CAPS_LOCK_2:
Scan_capslock(cur);
break;
case LED_SCROLL_LOCK_3:
Scan_scrolllock(cur);
break;
default:
break;
}
#endif
// Activate
Macro_ledState( id, ScheduleType_A );
}
// Detect if release
else if ( prev )
{
// TODO (HaaTa): Temporary Lock led control
#if Scan_KiraKeyboard_define == 1 && !defined(_host_)
switch ( id )
{
case LED_NUM_LOCK_1:
Scan_numlock(cur);
break;
case LED_CAPS_LOCK_2:
Scan_capslock(cur);
break;
case LED_SCROLL_LOCK_3:
Scan_scrolllock(cur);
break;
default:
break;
}
#endif
// Deactivate
Macro_ledState( id, ScheduleType_D );
}
}
// Update for next state comparison
USBKeys_LEDs_prev = USBKeys_LEDs;
}
// Gather USB Suspend/Sleep status
// Send events accordingly to PartialMap depending on status
void USB_suspend_status_update()
{
// TODO
// usb_suspended() <- 1 if suspended
}
// USB Data Periodic
inline void USB_periodic()
{
// Start latency measurement
Latency_start_time( outputPeriodicLatencyResource );
// Check to see if we need to reset the USB buffers
switch ( Output_reset_schedule )
{
case OutputReset_Restart:
case OutputReset_Bootloader:
USB_flushBuffers();
break;
}
#if enableMouse_define == 1
// Process mouse actions
while ( USBMouse_primary.changed && USB_ready() )
{
usb_mouse_send();
}
#endif
#if enableKeyboard_define == 1
// Determine if we need to change the Kbd Protocol
if ( USBKeys_Protocol_Change )
{
// Clear current interface
usb_keyboard_clear( USBKeys_Protocol );
// Set new protocol
USBKeys_Protocol = USBKeys_Protocol_New;
USBKeys_Protocol_Change = 0;
}
// Boot Mode Only, unset stale keys
if ( USBKeys_Protocol == 0 )
{
for ( uint8_t c = USBKeys_Sent; c < USB_BOOT_MAX_KEYS; c++ )
{
USBKeys_primary.keys[c] = 0;
}
}
// Send keypresses while there are pending changes
while ( USBKeys_primary.changed && USB_ready() )
{
usb_keyboard_send( (USBKeys*)&USBKeys_primary, USBKeys_Protocol );
}