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
|
//standard library usage
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h> //using common but unspecified addons to the standard (PRIu32...etc)
//posix additions
#include <signal.h>
#include <unistd.h>
//external libraries
#include <SDL.h>
void HANDLE_SDL_QUIT(int signal);
void axis_event( const SDL_ControllerAxisEvent sdl_event );
void button_event( const SDL_ControllerButtonEvent sdl_event );
void touchpad_event( const SDL_TouchFingerEvent sdl_event );
void sensor_event( const SDL_ControllerSensorEvent sdl_sensor_event );
enum {
BUTTONS = 1,
DPAD_ONLY = 1,
SHAPES_ONLY = 2,
ODD_BUTTONS_ONLY = 3,
AXIS = 2,
TRIGGERS_ONLY = 4,
JOYSTICKS_ONLY = 5,
TOUCHPAD = 4,
SENSOR = 8, //NOTE: Pretty sure gyro and accel are flipped in SDL
ACCEL_ONLY = 6,
GYRO_ONLY = 7,
BUTTONS_AXIS = 3,
BUTTONS_TOUCHPAD = 5,
BUTTONS_SENSOR = 9,
AXIS_TOUCHPAD = 6,
AXIS_SENSOR = 10,
TOUCHPAD_SENSOR = 12,
BUTTONS_AXIS_TOUCHPAD = 7,
BUTTONS_AXIS_SENSOR = 11,
BUTTONS_TOUCHPAD_SENSOR = 13,
AXIS_TOUCHPAD_SENSOR = 14,
EVERYTHING = 15
};
//timestamp, type, axis (i dont know how many yet), value, button (i dont know how many yet), state, x, y, sensor type, data (array of 3)
//double (casted), unsigned int, unsigned int, int, unsigned int, unsigned int, float, float, int, float[3]
double time;
unsigned int type;
float x, y, gyro[3], accel[3];
int sensor_type;
//axis
unsigned int left_joy_lr, left_joy_ud, right_joy_lr, right_joy_ud, left_trigger, right_trigger;
//buttons
unsigned int x_btn, circle_btn, triangle_btn, square_btn, right_joy_btn, left_joy_btn, touchpad_btn, dpad_up_btn, dpad_down_btn, dpad_right_btn, dpad_left_btn, ps_btn, share_btn, options_btn, r1_btn, l1_btn;
int main(int argc, char** argv)
{
//SDL disables CTRL+C to stop a program by default, I make it exit the program again
struct sigaction sa_struct;
sa_struct.sa_handler = &HANDLE_SDL_QUIT;
sa_struct.sa_flags = SA_RESTART; //needed for some system calls and I think it does no harm for the ones that dont need it?!?!
sigaction(SIGINT, &sa_struct, NULL);
//initialize controller with sensor support (I think this is everything that I need)
SDL_Init(SDL_INIT_GAMECONTROLLER | SDL_INIT_SENSOR | SDL_INIT_VIDEO); //video might be called implicitly by gamecontroller, but just in case because it is needed for input events
//make sure at least one controller is connected (at the moment I only want to write code to support one controller)
if (SDL_NumJoysticks() == 0) {
system("reset");
fprintf(stderr, "No DualShock4 controllers detected. This could be because of a loose or bad connection.\n");
exit(-1);
}
//opens a controller and sets a pointer to it
SDL_GameController* my_controller = SDL_GameControllerOpen(0);
//determines what features the user wants
if (argc == 1) {
system("reset");
char* device_string = SDL_GameControllerMapping(my_controller);
if (strstr(device_string, "03008fe54c050000cc09000000016800") != NULL) {
printf( "\n------------------------------------------------------------------------------\n" \
"A DualShock4 controller was found.\n\n\n" \
"Run the program again with an additional integer argument that denotes what features you want.\n" \
"Example: ./devout_sdl 12\n" \
"\n"
"Options:\n\n" \
"Buttons:\t\t\t\t 1\n" \
"Axis:\t\t\t\t\t 2\n" \
"TouchPad:\t\t\t\t 4\n" \
"Sensors:\t\t\t\t 8\n" \
"\n" \
"Buttons and Axis:\t\t\t 3\n" \
"Buttons and Touchpad:\t\t\t 5\n" \
"Buttons and Sensors:\t\t\t 9\n" \
"Axis and Touchpad:\t\t\t 6\n" \
"Axis and Sensors:\t\t\t 10\n" \
"Touchpad and Sensors:\t\t\t 12\n" \
"\n" \
"Buttons, Axis, and Touchpad:\t\t 7\n" \
"Buttons, Axis, and Sensors:\t\t 11\n" \
"Buttons, Touchpad, and Sensors:\t\t 13\n" \
"Axis, Touchpad, and Sensors:\t\t 14\n" \
"\n" \
"Buttons, Axis, Touchpad, and Sensors:\t 15\n" \
"\n" \
"Note: whenever the \"Buttons\" flag is used, an additional second integer argument can be passed denoting which buttons you want.\n" \
"By default all of the buttons are printed.\n" \
"Options for buttons:\n" \
"DPAD only:\t\t\t\t 1\n" \
"Shapes only:\t\t\t\t 2\n" \
"Other buttons only:\t\t\t 3\n" \
"\n\n" \
"Note: whenever the \"Axis\" flag is used, an additional second integer argument can be passed denoting which axis you want.\n" \
"By default all of the axis are printed.\n" \
"Options for axis:\n" \
"Triggers only:\t\t\t 4\n" \
"Joysticks only:\t\t\t 5\n" \
"\n\n" \
"Note: whenever the \"Sensors\" flag is used, an additional second integer argument can be passed denoting which sensor you want.\n" \
"By default all sensors are printed.\n" \
"Options for sensors:\n" \
"Gyro only:\t\t\t 6\n" \
"Accel only:\t\t\t 7\n" \
"\n" \
"Example: ./devout_sdl 15 2 7 4\n" \
"\n" \
"In the example above 15 as the first argument denotes the user wants to use all the capabilities of the controller including: buttons, axis, touchpad and sensors.\n" \
"The following 2, 7 and 4 are options to limit the output of buttons, sensors, and axis respectfully.\n" \
"A thing to note is that the order of the arguments \"2 7 4\" doesn't matter.\n" \
"The 2 will limit the buttons to shapes only, the 7 will limit the sensor to accel only, and the 4 will limit the axis to triggers only.\n" \
"\n\n" \
"Note: to omit the time from being printed, make the first argument a negative number.\n" \
"Example: ./devout_sdl -15 2 7 4\n" \
"The above example is the same as before but the time column will be omitted.\n" \
"\n\n" \
"This program was written using Simple DirectMedia Layer 2 (SDL2). A cross platform hardware abstraction layer.\n" \
"\n------------------------------------------------------------------------------\n");
}
return 0;
}
//read command line arguments
//options are: buttons, axis, touchpad, sensor or any combination of them
// 1 2 4 8 3, 5 - 7, 9 - 15
char* end_ptr; //used for following strtol() functions
long int arguments[4]; // 0 for features of device wanted, 1 - 3 for limiting what is printed of that/those features.
int bool_dpad_only = 0, bool_shapes_only = 0, bool_other_buttons_only = 0, bool_triggers_only = 0, bool_joysticks_only = 0, bool_gyro_only = 0, bool_accel_only = 0, bool_time = 1;
if (argc == 2) { //first arguument is the name of the command: "./devout_sdl"
arguments[0] = strtol(argv[1],&end_ptr,10);
if (arguments[0] < 0) {
bool_time = 0;
arguments[0] *= -1;
}
if (arguments[0] <= 0 || arguments[0] >= 16 ) {
fprintf(stderr, "An invalid first integer argument was passed. Only numbers 1-15 are supported.\n");
exit(-1);
}
} else if (argc > 2 && argc < 6 ) { //if they specify at least one option to limit the standard amount that is printed out no more than 5 options ever allowed (1 = ./devout_sdl, 2 = device feature, 3 - 5 (limiting buttons, axis and/or sensors)
arguments[0] = strtol(argv[1],&end_ptr,10);
if (arguments[0] < 0) {
bool_time = 0;
arguments[0] *= -1;
}
if (arguments[0] <= 0 || arguments[0] >= 16 ) {
fprintf(stderr, "An invalid first integer argument was passed. Only numbers 1-15 are supported.\n");
exit(-1);
}
for (int i = 0; i < argc - 2; ++i) {
arguments[i + 1] = strtol(argv[i + 2],&end_ptr,10);
if (arguments[i + 1] < 1 || arguments[i + 1] > 7) {
fprintf(stderr, "An invalid argument was passed. Only numbers 1-7 are supported to limit output.\n");
exit(-1);
}
switch(arguments[i + 1]) {
case DPAD_ONLY:
bool_dpad_only = 1;
break;
case SHAPES_ONLY:
bool_shapes_only = 1;
break;
case ODD_BUTTONS_ONLY:
bool_other_buttons_only = 1;
break;
case TRIGGERS_ONLY:
bool_triggers_only = 1;
break;
case JOYSTICKS_ONLY:
bool_joysticks_only = 1;
break;
case GYRO_ONLY:
bool_gyro_only = 1;
break;
case ACCEL_ONLY:
bool_accel_only = 1;
break;
default:
fprintf(stderr, "Should never make it here when trying to parse arguments from the user.\n");
exit(-1);
break;
}
}
} else {
fprintf(stderr, "Too many arguments passed. Only one argument is allowed, like this: ./devout_sdl 12\n");
exit(-1);
}
//enable sensors (gyro accel) for controller
if (SDL_GameControllerSetSensorEnabled(my_controller, SDL_SENSOR_GYRO, SDL_TRUE) != 0) {
fprintf(stderr, "An error occured trying to enable GYRO support.\n");
exit(-1);
}
if (SDL_GameControllerSetSensorEnabled(my_controller, SDL_SENSOR_ACCEL, SDL_TRUE) != 0) {
fprintf(stderr, "An error occured trying to enable ACCELEROMETER support.\n");
exit(-1);
}
//based on the events the user wants to print, print out the events as they occur
SDL_Event sdl_event;
//switch for each option, long and tedious, but easier to implement than the (albeit better) generic controller approach)
//The following commented out code can be added to every switch statement without a sensor so that it will print even if no new event is found.
/*
case SDL_CONTROLLERSENSORUPDATE: //included to update time since this updates automatically without user needing to press something, that way something is always printing on the screen
case SDL_SENSORUPDATE:
sensor_event( sdl_event.csensor );
break;
*/
switch (arguments[0]) {
case BUTTONS:
while (1) {
while(SDL_PollEvent(&sdl_event)) {
switch (sdl_event.type) {
case SDL_CONTROLLERBUTTONDOWN: //these are defined SDL_events.h
case SDL_CONTROLLERBUTTONUP:
button_event( sdl_event.cbutton );
break;
default: //will catch unneeded types
break;
}//switch(sdl_event.type)
} //while(SDL_PollEvent())
if (bool_time) {
printf("%7.3lf, ", time);
}
if (bool_dpad_only) {
printf("%d, %d, %d, %d, ", dpad_up_btn, dpad_down_btn, dpad_right_btn, dpad_left_btn);
} else if (bool_shapes_only) {
printf("%d, %d, %d, %d, ", x_btn, circle_btn, triangle_btn, square_btn);
} else if (bool_other_buttons_only) {
printf("%d, %d, %d, %d, %d, %d, %d, %d, ", right_joy_btn, left_joy_btn, touchpad_btn, ps_btn, share_btn, options_btn, r1_btn, l1_btn);
} else {
printf("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, ", x_btn, circle_btn, triangle_btn, square_btn, right_joy_btn, left_joy_btn, touchpad_btn, dpad_up_btn, dpad_down_btn, dpad_right_btn, dpad_left_btn, ps_btn, share_btn, options_btn, r1_btn, l1_btn);
}
puts("");
SDL_Delay(1);
SDL_GameControllerUpdate();
} //while(1)
break; //should never make it here
case AXIS:
while (1) {
while(SDL_PollEvent(&sdl_event)) {
switch (sdl_event.type) {
case SDL_CONTROLLERAXISMOTION:
axis_event( sdl_event.caxis );
break;
default: //will catch unneeded types
break;
}//switch(sdl_event.type)
} //while(SDL_PollEvent())
if (bool_time) {
printf("%7.3lf, ", time);
}
if (bool_triggers_only) {
printf("%5d, %5d, ", left_trigger, right_trigger);
} else if (bool_joysticks_only) {
printf("% 6d, %6d, % 6d, % 6d, ", left_joy_lr, left_joy_ud, right_joy_lr, right_joy_ud);
} else {
printf("% 6d, %6d, % 6d, % 6d, %5d, %5d, ",left_joy_lr, left_joy_ud, right_joy_lr, right_joy_ud, left_trigger, right_trigger);
}
puts("");
SDL_Delay(1);
SDL_GameControllerUpdate();
} //while(1)
break; //should never make it here
case TOUCHPAD:
while (1) {
while(SDL_PollEvent(&sdl_event)) {
switch (sdl_event.type) {
case SDL_CONTROLLERTOUCHPADDOWN:
case SDL_CONTROLLERTOUCHPADMOTION:
case SDL_CONTROLLERTOUCHPADUP:
case SDL_FINGERMOTION:
case SDL_FINGERDOWN:
case SDL_FINGERUP:
touchpad_event( sdl_event.tfinger );
break;
default: //will catch unneeded types
break;
}//switch(sdl_event.type)
} //while(SDL_PollEvent())
if (bool_time) {
printf("%7.3lf, ", time);
}
printf("%lf, %lf, ", x, y);
puts("");
SDL_Delay(5);
SDL_GameControllerUpdate();
} //while(1)
break; //should never make it here
case SENSOR:
while (1) {
while(SDL_PollEvent(&sdl_event)) {
switch (sdl_event.type) {
case SDL_CONTROLLERSENSORUPDATE:
case SDL_SENSORUPDATE:
sensor_event( sdl_event.csensor );
break;
default: //will catch unneeded types
break;
}//switch(sdl_event.type)
} //while(SDL_PollEvent())
if (bool_time) {
printf("%7.3lf, ", time);
}
if (bool_gyro_only) {
printf("% 10lf, % 10lf, % 10lf, ", gyro[0], gyro[1], gyro[2]);
} else if (bool_accel_only) {
printf("% 10lf, % 10lf, % 10lf, ", accel[0], accel[1], accel[2]);
} else {
printf("% 10lf, % 10lf, % 10lf, % 10lf, % 10lf, % 10lf, ", gyro[0], gyro[1], gyro[2], accel[0], accel[1], accel[2]);
}
puts("");
SDL_Delay(2);
SDL_GameControllerUpdate();
} //while(1)
break; //should never make it here
case BUTTONS_AXIS:
while (1) {
while(SDL_PollEvent(&sdl_event)) {
switch (sdl_event.type) {
case SDL_CONTROLLERBUTTONDOWN: //these are defined SDL_events.h
case SDL_CONTROLLERBUTTONUP:
button_event( sdl_event.cbutton );
break;
case SDL_CONTROLLERAXISMOTION:
axis_event( sdl_event.caxis );
break;
default: //will catch unneeded types
break;
}//switch(sdl_event.type)
} //while(SDL_PollEvent())
if (bool_time) {
printf("%7.3lf, ", time);
}
if (bool_dpad_only) {
printf("%d, %d, %d, %d, ", dpad_up_btn, dpad_down_btn, dpad_right_btn, dpad_left_btn);
} else if (bool_shapes_only) {
printf("%d, %d, %d, %d, ", x_btn, circle_btn, triangle_btn, square_btn);
} else if (bool_other_buttons_only) {
printf("%d, %d, %d, %d, %d, %d, %d, %d, ", right_joy_btn, left_joy_btn, touchpad_btn, ps_btn, share_btn, options_btn, r1_btn, l1_btn);
} else {
printf("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, ", x_btn, circle_btn, triangle_btn, square_btn, right_joy_btn, left_joy_btn, touchpad_btn, dpad_up_btn, dpad_down_btn, dpad_right_btn, dpad_left_btn, ps_btn, share_btn, options_btn, r1_btn, l1_btn);
}
if (bool_triggers_only) {
printf("%5d, %5d, ", left_trigger, right_trigger);
} else if (bool_joysticks_only) {
printf("% 6d, %6d, % 6d, % 6d, ", left_joy_lr, left_joy_ud, right_joy_lr, right_joy_ud);
} else {
printf("% 6d, %6d, % 6d, % 6d, %5d, %5d, ",left_joy_lr, left_joy_ud, right_joy_lr, right_joy_ud, left_trigger, right_trigger);
}
puts("");
SDL_Delay(1);
SDL_GameControllerUpdate();
} //while(1)
break; //should never make it here
case BUTTONS_TOUCHPAD:
while (1) {
while(SDL_PollEvent(&sdl_event)) {
switch (sdl_event.type) {
case SDL_CONTROLLERBUTTONDOWN: //these are defined SDL_events.h
case SDL_CONTROLLERBUTTONUP:
button_event( sdl_event.cbutton );
break;
case SDL_CONTROLLERTOUCHPADDOWN:
case SDL_CONTROLLERTOUCHPADMOTION:
case SDL_CONTROLLERTOUCHPADUP:
case SDL_FINGERMOTION:
case SDL_FINGERDOWN:
case SDL_FINGERUP:
touchpad_event( sdl_event.tfinger );
break;
default: //will catch unneeded types
break;
}//switch(sdl_event.type)
} //while(SDL_PollEvent())
if (bool_time) {
printf("%7.3lf, ", time);
}
if (bool_dpad_only) {
printf("%d, %d, %d, %d, ", dpad_up_btn, dpad_down_btn, dpad_right_btn, dpad_left_btn);
} else if (bool_shapes_only) {
printf("%d, %d, %d, %d, ", x_btn, circle_btn, triangle_btn, square_btn);
} else if (bool_other_buttons_only) {
printf("%d, %d, %d, %d, %d, %d, %d, %d, ", right_joy_btn, left_joy_btn, touchpad_btn, ps_btn, share_btn, options_btn, r1_btn, l1_btn);
} else {
printf("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, ", x_btn, circle_btn, triangle_btn, square_btn, right_joy_btn, left_joy_btn, touchpad_btn, dpad_up_btn, dpad_down_btn, dpad_right_btn, dpad_left_btn, ps_btn, share_btn, options_btn, r1_btn, l1_btn);
}
printf("%lf, %lf, ", x, y);
puts("");
SDL_Delay(2);
SDL_GameControllerUpdate();
} //while(1)
break; //should never make it here
case BUTTONS_SENSOR:
while (1) {
while(SDL_PollEvent(&sdl_event)) {
switch (sdl_event.type) {
case SDL_CONTROLLERBUTTONDOWN: //these are defined SDL_events.h
case SDL_CONTROLLERBUTTONUP:
button_event( sdl_event.cbutton );
break;
case SDL_CONTROLLERSENSORUPDATE:
case SDL_SENSORUPDATE:
sensor_event( sdl_event.csensor );
break;
default: //will catch unneeded types
break;
}//switch(sdl_event.type)
} //while(SDL_PollEvent())
if (bool_time) {
printf("%7.3lf, ", time);
}
if (bool_dpad_only) {
printf("%d, %d, %d, %d, ", dpad_up_btn, dpad_down_btn, dpad_right_btn, dpad_left_btn);
} else if (bool_shapes_only) {
printf("%d, %d, %d, %d, ", x_btn, circle_btn, triangle_btn, square_btn);
} else if (bool_other_buttons_only) {
printf("%d, %d, %d, %d, %d, %d, %d, %d, ", right_joy_btn, left_joy_btn, touchpad_btn, ps_btn, share_btn, options_btn, r1_btn, l1_btn);
} else {
printf("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, ", x_btn, circle_btn, triangle_btn, square_btn, right_joy_btn, left_joy_btn, touchpad_btn, dpad_up_btn, dpad_down_btn, dpad_right_btn, dpad_left_btn, ps_btn, share_btn, options_btn, r1_btn, l1_btn);
}
if (bool_gyro_only) {
printf("% 10lf, % 10lf, % 10lf, ", gyro[0], gyro[1], gyro[2]);
} else if (bool_accel_only) {
printf("% 10lf, % 10lf, % 10lf, ", accel[0], accel[1], accel[2]);
} else {
printf("% 10lf, % 10lf, % 10lf, % 10lf, % 10lf, % 10lf, ", gyro[0], gyro[1], gyro[2], accel[0], accel[1], accel[2]);
}
puts("");
SDL_Delay(1);
SDL_GameControllerUpdate();
} //while(1)
break; //should never make it here
case AXIS_TOUCHPAD:
while (1) {
while(SDL_PollEvent(&sdl_event)) {
switch (sdl_event.type) {
case SDL_CONTROLLERAXISMOTION:
axis_event( sdl_event.caxis );
break;
case SDL_CONTROLLERTOUCHPADDOWN:
case SDL_CONTROLLERTOUCHPADMOTION:
case SDL_CONTROLLERTOUCHPADUP:
case SDL_FINGERMOTION:
case SDL_FINGERDOWN:
case SDL_FINGERUP:
touchpad_event( sdl_event.tfinger );
break;
default: //will catch unneeded types
break;
}//switch(sdl_event.type)
} //while(SDL_PollEvent())
if (bool_time) {
printf("%7.3lf, ", time);
}
if (bool_triggers_only) {
printf("%5d, %5d, ", left_trigger, right_trigger);
} else if (bool_joysticks_only) {
printf("% 6d, %6d, % 6d, % 6d, ", left_joy_lr, left_joy_ud, right_joy_lr, right_joy_ud);
} else {
printf("% 6d, %6d, % 6d, % 6d, %5d, %5d, ",left_joy_lr, left_joy_ud, right_joy_lr, right_joy_ud, left_trigger, right_trigger);
}
printf("%lf, %lf, ", x, y);
puts("");
SDL_Delay(2);
SDL_GameControllerUpdate();
} //while(1)
break; //should never make it here
case AXIS_SENSOR:
while (1) {
while(SDL_PollEvent(&sdl_event)) {
switch (sdl_event.type) {
case SDL_CONTROLLERAXISMOTION:
axis_event( sdl_event.caxis );
break;
case SDL_CONTROLLERSENSORUPDATE:
case SDL_SENSORUPDATE:
sensor_event( sdl_event.csensor );
break;
default: //will catch unneeded types
break;
}//switch(sdl_event.type)
} //while(SDL_PollEvent())
if (bool_time) {
printf("%7.3lf, ", time);
}
if (bool_triggers_only) {
printf("%5d, %5d, ", left_trigger, right_trigger);
} else if (bool_joysticks_only) {
printf("% 6d, %6d, % 6d, % 6d, ", left_joy_lr, left_joy_ud, right_joy_lr, right_joy_ud);
} else {
printf("% 6d, %6d, % 6d, % 6d, %5d, %5d, ",left_joy_lr, left_joy_ud, right_joy_lr, right_joy_ud, left_trigger, right_trigger);
}
if (bool_gyro_only) {
printf("% 10lf, % 10lf, % 10lf, ", gyro[0], gyro[1], gyro[2]);
} else if (bool_accel_only) {
printf("% 10lf, % 10lf, % 10lf, ", accel[0], accel[1], accel[2]);
} else {
printf("% 10lf, % 10lf, % 10lf, % 10lf, % 10lf, % 10lf, ", gyro[0], gyro[1], gyro[2], accel[0], accel[1], accel[2]);
}
puts("");
SDL_Delay(2);
SDL_GameControllerUpdate();
} //while(1)
break; //should never make it here
case TOUCHPAD_SENSOR:
while (1) {
while(SDL_PollEvent(&sdl_event)) {
switch (sdl_event.type) {
case SDL_CONTROLLERSENSORUPDATE:
case SDL_SENSORUPDATE:
sensor_event( sdl_event.csensor );
break;
case SDL_CONTROLLERTOUCHPADDOWN:
case SDL_CONTROLLERTOUCHPADMOTION:
case SDL_CONTROLLERTOUCHPADUP:
case SDL_FINGERMOTION:
case SDL_FINGERDOWN:
case SDL_FINGERUP:
touchpad_event( sdl_event.tfinger );
break;
default: //will catch unneeded types
break;
}//switch(sdl_event.type)
} //while(SDL_PollEvent())
if (bool_time) {
printf("%7.3lf, ", time);
}
printf("%lf, %lf, ", x, y);
if (bool_gyro_only) {
printf("% 10lf, % 10lf, % 10lf, ", gyro[0], gyro[1], gyro[2]);
} else if (bool_accel_only) {
printf("% 10lf, % 10lf, % 10lf, ", accel[0], accel[1], accel[2]);
} else {
printf("% 10lf, % 10lf, % 10lf, % 10lf, % 10lf, % 10lf, ", gyro[0], gyro[1], gyro[2], accel[0], accel[1], accel[2]);
}
puts("");
SDL_Delay(2);
SDL_GameControllerUpdate();
} //while(1)
break; //should never make it here
case BUTTONS_AXIS_TOUCHPAD:
while (1) {
while(SDL_PollEvent(&sdl_event)) {
switch (sdl_event.type) {
case SDL_CONTROLLERBUTTONDOWN: //these are defined SDL_events.h
case SDL_CONTROLLERBUTTONUP:
button_event( sdl_event.cbutton );
break;
case SDL_CONTROLLERAXISMOTION:
axis_event( sdl_event.caxis );
break;
case SDL_CONTROLLERTOUCHPADDOWN:
case SDL_CONTROLLERTOUCHPADMOTION:
case SDL_CONTROLLERTOUCHPADUP:
case SDL_FINGERMOTION:
case SDL_FINGERDOWN:
case SDL_FINGERUP:
touchpad_event( sdl_event.tfinger );
break;
default: //will catch unneeded types
break;
}//switch(sdl_event.type)
} //while(SDL_PollEvent())
if (bool_time) {
printf("%7.3lf, ", time);
}
if (bool_dpad_only) {
printf("%d, %d, %d, %d, ", dpad_up_btn, dpad_down_btn, dpad_right_btn, dpad_left_btn);
} else if (bool_shapes_only) {
printf("%d, %d, %d, %d, ", x_btn, circle_btn, triangle_btn, square_btn);
} else if (bool_other_buttons_only) {
printf("%d, %d, %d, %d, %d, %d, %d, %d, ", right_joy_btn, left_joy_btn, touchpad_btn, ps_btn, share_btn, options_btn, r1_btn, l1_btn);
} else {
printf("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, ", x_btn, circle_btn, triangle_btn, square_btn, right_joy_btn, left_joy_btn, touchpad_btn, dpad_up_btn, dpad_down_btn, dpad_right_btn, dpad_left_btn, ps_btn, share_btn, options_btn, r1_btn, l1_btn);
}
if (bool_triggers_only) {
printf("%5d, %5d, ", left_trigger, right_trigger);
} else if (bool_joysticks_only) {
printf("% 6d, %6d, % 6d, % 6d, ", left_joy_lr, left_joy_ud, right_joy_lr, right_joy_ud);
} else {
printf("% 6d, %6d, % 6d, % 6d, %5d, %5d, ",left_joy_lr, left_joy_ud, right_joy_lr, right_joy_ud, left_trigger, right_trigger);
}
printf("%lf, %lf, ", x, y);
puts("");
SDL_Delay(1);
SDL_GameControllerUpdate();
} //while(1)
break; //should never make it here
case BUTTONS_AXIS_SENSOR:
while (1) {
while(SDL_PollEvent(&sdl_event)) {
switch (sdl_event.type) {
case SDL_CONTROLLERBUTTONDOWN: //these are defined SDL_events.h
case SDL_CONTROLLERBUTTONUP:
button_event( sdl_event.cbutton );
break;
case SDL_CONTROLLERAXISMOTION:
axis_event( sdl_event.caxis );
break;
case SDL_CONTROLLERSENSORUPDATE:
case SDL_SENSORUPDATE:
sensor_event( sdl_event.csensor );
break;
default: //will catch unneeded types
break;
}//switch(sdl_event.type)
} //while(SDL_PollEvent())
if (bool_time) {
printf("%7.3lf, ", time);
}
if (bool_dpad_only) {
printf("%d, %d, %d, %d, ", dpad_up_btn, dpad_down_btn, dpad_right_btn, dpad_left_btn);
} else if (bool_shapes_only) {
printf("%d, %d, %d, %d, ", x_btn, circle_btn, triangle_btn, square_btn);
} else if (bool_other_buttons_only) {
printf("%d, %d, %d, %d, %d, %d, %d, %d, ", right_joy_btn, left_joy_btn, touchpad_btn, ps_btn, share_btn, options_btn, r1_btn, l1_btn);
} else {
printf("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, ", x_btn, circle_btn, triangle_btn, square_btn, right_joy_btn, left_joy_btn, touchpad_btn, dpad_up_btn, dpad_down_btn, dpad_right_btn, dpad_left_btn, ps_btn, share_btn, options_btn, r1_btn, l1_btn);
}
if (bool_triggers_only) {
printf("%5d, %5d, ", left_trigger, right_trigger);
} else if (bool_joysticks_only) {
printf("% 6d, %6d, % 6d, % 6d, ", left_joy_lr, left_joy_ud, right_joy_lr, right_joy_ud);
} else {
printf("% 6d, %6d, % 6d, % 6d, %5d, %5d, ",left_joy_lr, left_joy_ud, right_joy_lr, right_joy_ud, left_trigger, right_trigger);
}
if (bool_gyro_only) {
printf("% 10lf, % 10lf, % 10lf, ", gyro[0], gyro[1], gyro[2]);
} else if (bool_accel_only) {
printf("% 10lf, % 10lf, % 10lf, ", accel[0], accel[1], accel[2]);
} else {
printf("% 10lf, % 10lf, % 10lf, % 10lf, % 10lf, % 10lf, ", gyro[0], gyro[1], gyro[2], accel[0], accel[1], accel[2]);
}
puts("");
SDL_Delay(1);
SDL_GameControllerUpdate();
} //while(1)
break; //should never make it here
case BUTTONS_TOUCHPAD_SENSOR:
while (1) {
while(SDL_PollEvent(&sdl_event)) {
switch (sdl_event.type) {
case SDL_CONTROLLERBUTTONDOWN: //these are defined SDL_events.h
case SDL_CONTROLLERBUTTONUP:
button_event( sdl_event.cbutton );
break;
case SDL_CONTROLLERSENSORUPDATE:
case SDL_SENSORUPDATE:
sensor_event( sdl_event.csensor );
break;
case SDL_CONTROLLERTOUCHPADDOWN:
case SDL_CONTROLLERTOUCHPADMOTION:
case SDL_CONTROLLERTOUCHPADUP:
case SDL_FINGERMOTION:
case SDL_FINGERDOWN:
case SDL_FINGERUP:
touchpad_event( sdl_event.tfinger );
break;
default: //will catch unneeded types
break;
}//switch(sdl_event.type)
} //while(SDL_PollEvent())
if (bool_time) {
printf("%7.3lf, ", time);
}
if (bool_dpad_only) {
printf("%d, %d, %d, %d, ", dpad_up_btn, dpad_down_btn, dpad_right_btn, dpad_left_btn);
} else if (bool_shapes_only) {
printf("%d, %d, %d, %d, ", x_btn, circle_btn, triangle_btn, square_btn);
} else if (bool_other_buttons_only) {
printf("%d, %d, %d, %d, %d, %d, %d, %d, ", right_joy_btn, left_joy_btn, touchpad_btn, ps_btn, share_btn, options_btn, r1_btn, l1_btn);
} else {
printf("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, ", x_btn, circle_btn, triangle_btn, square_btn, right_joy_btn, left_joy_btn, touchpad_btn, dpad_up_btn, dpad_down_btn, dpad_right_btn, dpad_left_btn, ps_btn, share_btn, options_btn, r1_btn, l1_btn);
}
printf("%lf, %lf, ", x, y);
if (bool_gyro_only) {
printf("% 10lf, % 10lf, % 10lf, ", gyro[0], gyro[1], gyro[2]);
} else if (bool_accel_only) {
printf("% 10lf, % 10lf, % 10lf, ", accel[0], accel[1], accel[2]);
} else {
printf("% 10lf, % 10lf, % 10lf, % 10lf, % 10lf, % 10lf, ", gyro[0], gyro[1], gyro[2], accel[0], accel[1], accel[2]);
}
puts("");
SDL_Delay(1);
SDL_GameControllerUpdate();
} //while(1)
break; //should never make it here
case AXIS_TOUCHPAD_SENSOR:
while (1) {
while(SDL_PollEvent(&sdl_event)) {
switch (sdl_event.type) {
case SDL_CONTROLLERAXISMOTION:
axis_event( sdl_event.caxis );
break;
case SDL_CONTROLLERSENSORUPDATE:
case SDL_SENSORUPDATE:
sensor_event( sdl_event.csensor );
break;
case SDL_CONTROLLERTOUCHPADDOWN:
case SDL_CONTROLLERTOUCHPADMOTION:
case SDL_CONTROLLERTOUCHPADUP:
case SDL_FINGERMOTION:
case SDL_FINGERDOWN:
case SDL_FINGERUP:
touchpad_event( sdl_event.tfinger );
break;
default: //will catch unneeded types
break;
}//switch(sdl_event.type)
} //while(SDL_PollEvent())
if (bool_time) {
printf("%7.3lf, ", time);
}
if (bool_triggers_only) {
printf("%5d, %5d, ", left_trigger, right_trigger);
} else if (bool_joysticks_only) {
printf("% 6d, %6d, % 6d, % 6d, ", left_joy_lr, left_joy_ud, right_joy_lr, right_joy_ud);
} else {
printf("% 6d, %6d, % 6d, % 6d, %5d, %5d, ",left_joy_lr, left_joy_ud, right_joy_lr, right_joy_ud, left_trigger, right_trigger);
}
printf("%lf, %lf, ", x, y);
if (bool_gyro_only) {
printf("% 10lf, % 10lf, % 10lf, ", gyro[0], gyro[1], gyro[2]);
} else if (bool_accel_only) {
printf("% 10lf, % 10lf, % 10lf, ", accel[0], accel[1], accel[2]);
} else {
printf("% 10lf, % 10lf, % 10lf, % 10lf, % 10lf, % 10lf, ", gyro[0], gyro[1], gyro[2], accel[0], accel[1], accel[2]);
}
puts("");
SDL_Delay(1);
SDL_GameControllerUpdate();
} //while(1)
break; //should never make it here
case EVERYTHING:
while (1) {
while(SDL_PollEvent(&sdl_event)) {
switch (sdl_event.type) {
case SDL_CONTROLLERBUTTONDOWN: //these are defined SDL_events.h
case SDL_CONTROLLERBUTTONUP:
button_event( sdl_event.cbutton );
break;
case SDL_CONTROLLERAXISMOTION:
axis_event( sdl_event.caxis );
break;
case SDL_CONTROLLERSENSORUPDATE:
case SDL_SENSORUPDATE:
sensor_event( sdl_event.csensor );
break;
case SDL_CONTROLLERTOUCHPADDOWN:
case SDL_CONTROLLERTOUCHPADMOTION:
case SDL_CONTROLLERTOUCHPADUP:
case SDL_FINGERMOTION:
case SDL_FINGERDOWN:
case SDL_FINGERUP:
touchpad_event( sdl_event.tfinger );
break;
default: //will catch unneeded types
break;
}//switch(sdl_event.type)
} //while(SDL_PollEvent())
if (bool_time) {
printf("%7.3lf, ", time);
}
if (bool_dpad_only) {
printf("%d, %d, %d, %d, ", dpad_up_btn, dpad_down_btn, dpad_right_btn, dpad_left_btn);
} else if (bool_shapes_only) {
printf("%d, %d, %d, %d, ", x_btn, circle_btn, triangle_btn, square_btn);
} else if (bool_other_buttons_only) {
printf("%d, %d, %d, %d, %d, %d, %d, %d, ", right_joy_btn, left_joy_btn, touchpad_btn, ps_btn, share_btn, options_btn, r1_btn, l1_btn);
} else {
printf("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, ", x_btn, circle_btn, triangle_btn, square_btn, right_joy_btn, left_joy_btn, touchpad_btn, dpad_up_btn, dpad_down_btn, dpad_right_btn, dpad_left_btn, ps_btn, share_btn, options_btn, r1_btn, l1_btn);
}
if (bool_triggers_only) {
printf("%5d, %5d, ", left_trigger, right_trigger);
} else if (bool_joysticks_only) {
printf("% 6d, %6d, % 6d, % 6d, ", left_joy_lr, left_joy_ud, right_joy_lr, right_joy_ud);
} else {
printf("% 6d, %6d, % 6d, % 6d, %5d, %5d, ",left_joy_lr, left_joy_ud, right_joy_lr, right_joy_ud, left_trigger, right_trigger);
}
printf("%lf, %lf, ", x, y);
if (bool_gyro_only) {
printf("% 10lf, % 10lf, % 10lf, ", gyro[0], gyro[1], gyro[2]);
} else if (bool_accel_only) {
printf("% 10lf, % 10lf, % 10lf, ", accel[0], accel[1], accel[2]);
} else {
printf("% 10lf, % 10lf, % 10lf, % 10lf, % 10lf, % 10lf, ", gyro[0], gyro[1], gyro[2], accel[0], accel[1], accel[2]);
}
puts("");
SDL_Delay(1);
SDL_GameControllerUpdate();
} //while(1)
break; //should never make it here
default:
fprintf(stderr, "Should not make it here from the argument switch statement!\n");
exit(-1);
} //switch(argument)
return 0;
}
void HANDLE_SDL_QUIT(int signal)
{
printf("\n");
exit(1);
}
void button_event( const SDL_ControllerButtonEvent sdl_event )
{
//buttons
//x_btn, circle_btn, triangle_btn, square_btn, right_joy_btn, left_joy_btn, touchpad_btn, dpad_up_btn, dpad_down_btn, dpad_right_btn, dpad_left_btn, ps_btn, share_btn, options_btn, r1_btn, l1_btn;
//time, button, state
time = (double) sdl_event.timestamp / 1000;
switch (sdl_event.button) {
case SDL_CONTROLLER_BUTTON_A: //found in controller.h
x_btn = sdl_event.state;
break;
case SDL_CONTROLLER_BUTTON_B:
circle_btn = sdl_event.state;
break;
case SDL_CONTROLLER_BUTTON_X:
square_btn = sdl_event.state;
break;
case SDL_CONTROLLER_BUTTON_Y:
triangle_btn = sdl_event.state;
break;
case SDL_CONTROLLER_BUTTON_BACK:
share_btn = sdl_event.state;
break;
case SDL_CONTROLLER_BUTTON_GUIDE:
ps_btn = sdl_event.state;
break;
case SDL_CONTROLLER_BUTTON_START:
options_btn = sdl_event.state;
break;
case SDL_CONTROLLER_BUTTON_LEFTSTICK:
left_joy_btn = sdl_event.state;
break;
case SDL_CONTROLLER_BUTTON_RIGHTSTICK:
right_joy_btn = sdl_event.state;
break;
case SDL_CONTROLLER_BUTTON_LEFTSHOULDER:
l1_btn = sdl_event.state;
break;
case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER:
r1_btn = sdl_event.state;
break;
case SDL_CONTROLLER_BUTTON_DPAD_UP:
dpad_up_btn = sdl_event.state;
break;
case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
dpad_down_btn = sdl_event.state;
break;
case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
dpad_left_btn = sdl_event.state;
break;
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
dpad_right_btn = sdl_event.state;
break;
case SDL_CONTROLLER_BUTTON_TOUCHPAD:
touchpad_btn = sdl_event.state;
break;
default:
break;
}
}
void axis_event( const SDL_ControllerAxisEvent sdl_event )
{
//axis
//left_joy_lr, left_joy_ud, right_joy_lr, right_joy_ud, left_trigger, right_trigger;
//
//time, axis, value
time = (double) sdl_event.timestamp / 1000;
switch (sdl_event.axis) {
case SDL_CONTROLLER_AXIS_LEFTX: //found in controller.h
left_joy_lr = sdl_event.value;
break;
case SDL_CONTROLLER_AXIS_LEFTY:
left_joy_ud = sdl_event.value;
break;
case SDL_CONTROLLER_AXIS_RIGHTX:
right_joy_lr = sdl_event.value;
break;
case SDL_CONTROLLER_AXIS_RIGHTY:
right_joy_ud = sdl_event.value;
break;
case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
left_trigger = sdl_event.value;
break;
case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
right_trigger = sdl_event.value;
break;
default:
break;
}
}
void touchpad_event( const SDL_TouchFingerEvent sdl_event )
{
//time, x, y (currently the y value does not work)
time = (double) sdl_event.timestamp / 1000;
x = sdl_event.x;
y = sdl_event.y;
}
void sensor_event( const SDL_ControllerSensorEvent sdl_event )
{
//time, sensor type, data (array of 3)
time = (double) sdl_event.timestamp / 1000;
if (sdl_event.sensor == SDL_SENSOR_GYRO) {
gyro[0] = sdl_event.data[0];
gyro[1] = sdl_event.data[1];
gyro[2] = sdl_event.data[2];
} else if (sdl_event.sensor == SDL_SENSOR_ACCEL) {
accel[0] = sdl_event.data[0];
accel[1] = sdl_event.data[1];
accel[2] = sdl_event.data[2];
}
}
|