Commit 45fb778fe53323180942e982e0a4480a3f27c138

Ozkan Sezer 2023-08-02T20:40:24

adjusted coding style in SDL_os2_joystick.c to match rest of SDL better (cherry picked from commit 57f3c41b9727d8cfb701401bc539be45285a0399)

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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
diff --git a/src/joystick/os2/SDL_os2joystick.c b/src/joystick/os2/SDL_os2joystick.c
index 7f72b62..b7e0376 100644
--- a/src/joystick/os2/SDL_os2joystick.c
+++ b/src/joystick/os2/SDL_os2joystick.c
@@ -93,29 +93,29 @@ typedef struct {
 #include "../SDL_sysjoystick.h"
 #include "../SDL_joystick_c.h"
 
-static HFILE hJoyPort = NULLHANDLE;	/* Joystick GAME$ Port Address */
-#define MAX_JOYSTICKS	2	/* Maximum of two joysticks */
-#define MAX_AXES	4			/* each joystick can have up to 4 axes */
-#define MAX_BUTTONS	8		/* 8 buttons */
-#define MAX_HATS	0			/* 0 hats - OS/2 doesn't support it */
-#define MAX_BALLS	0			/* and 0 balls - OS/2 doesn't support it */
-#define MAX_JOYNAME	128	/* Joystick name may have 128 characters */
+static HFILE hJoyPort = NULLHANDLE; /* Joystick GAME$ Port Address */
+#define MAX_JOYSTICKS   2       /* Maximum of two joysticks */
+#define MAX_AXES        4       /* each joystick can have up to 4 axes */
+#define MAX_BUTTONS     8       /* 8 buttons */
+#define MAX_HATS        0       /* 0 hats - OS/2 doesn't support it */
+#define MAX_BALLS       0       /* and 0 balls - OS/2 doesn't support it */
+#define MAX_JOYNAME     128     /* Joystick name may have 128 characters */
 /* Calc Button Flag for buttons A to D */
 #define JOY_BUTTON_FLAG(n) (1<<n)
 
 /* Joystick data... hold information about detected devices */
 typedef struct SYS_JoyData_s
 {
-	Sint8	id;				/* Device ID          */
-	char	szDeviceName[MAX_JOYNAME];	/* Device Name        */
-	char	axes;				/* Number of axes     */
-	char	buttons;			/* Number of buttons  */
-	char	hats;				/* Number of buttons  */
-	char	balls;				/* Number of buttons  */
-	int	axes_min[MAX_AXES];		/* minimum callibration value for axes  */
-	int	axes_med[MAX_AXES];		/* medium callibration value for axes   */
-	int	axes_max[MAX_AXES];		/* maximum callibration value for axes  */
-	int	buttoncalc[4];			/* Used for buttons 5, 6, 7 and 8.      */
+    Sint8 id;                           /* Device ID          */
+    char  szDeviceName[MAX_JOYNAME];    /* Device Name        */
+    char  axes;                         /* Number of axes     */
+    char  buttons;                      /* Number of buttons  */
+    char  hats;                         /* Number of buttons  */
+    char  balls;                        /* Number of buttons  */
+    int   axes_min[MAX_AXES];           /* minimum callibration value for axes  */
+    int   axes_med[MAX_AXES];           /* medium callibration value for axes   */
+    int   axes_max[MAX_AXES];           /* maximum callibration value for axes  */
+    int   buttoncalc[4];                /* Used for buttons 5, 6, 7 and 8.      */
 } SYS_JoyData_t, *SYS_JoyData_p;
 
 static SYS_JoyData_t SYS_JoyData[MAX_JOYSTICKS];
@@ -123,25 +123,25 @@ static SYS_JoyData_t SYS_JoyData[MAX_JOYSTICKS];
 /* Structure used to convert data from OS/2 driver format to SDL format */
 struct _transaxes
 {
-	int offset;					/* Center Offset */
-	float scale1;				/* Center to left/up Scale */
-	float scale2;				/* Center to right/down Scale */
+    int   offset; /* Center Offset */
+    float scale1; /* Center to left/up Scale */
+    float scale2; /* Center to right/down Scale */
 };
 
 struct joystick_hwdata
 {
-	Sint8		id;
-	struct _transaxes	transaxes[MAX_AXES];
+    Sint8 id;
+    struct _transaxes transaxes[MAX_AXES];
 };
 
 /* Structure used to get values from Joystick Environment Variable */
 struct _joycfg
 {
-	char	name[MAX_JOYNAME];
-	unsigned int	axes;
-	unsigned int	buttons;
-	unsigned int	hats;
-	unsigned int	balls;
+    char  name[MAX_JOYNAME];
+    unsigned int  axes;
+    unsigned int  buttons;
+    unsigned int  hats;
+    unsigned int  balls;
 };
 
 /* OS/2 Implementation Function Prototypes */
@@ -154,223 +154,243 @@ static int joyGetEnv(struct _joycfg * joydata);
 static int numjoysticks = 0;
 
 /************************************************************************/
-/* Function to scan the system for joysticks.									*/
-/* Joystick 0 should be the system default joystick.			*/
-/* It should return 0, or -1 on an unrecoverable fatal error.				*/
+/* Function to scan the system for joysticks.                           */
+/* Joystick 0 should be the system default joystick.                    */
+/* It should return 0, or -1 on an unrecoverable fatal error.           */
 /************************************************************************/
 static int OS2_JoystickInit(void)
 {
-	APIRET rc;	/* Generic OS/2 return code */
-	GAME_PORT_STRUCT	stJoyStatus;		/* Joystick Status Structure */
-	GAME_PARM_STRUCT	stGameParms;		/* Joystick Parameter Structure */
-	GAME_CALIB_STRUCT	stGameCalib;		/* Calibration Struct */
-	ULONG ulDataLen;					/* Size of data */
-	ULONG ulLastTick;					/* Tick Counter for timing operations */
-	Uint8 maxdevs;						/* Maximum number of devices */
-	Uint8 numdevs;						/* Number of present devices */
-	Uint8 maxbut;						/* Maximum number of buttons... */
-	Uint8 i;						/* Temporary Count Vars */
-	Uint8 ucNewJoystickMask;				/* Mask for Joystick Detection */
-	struct _joycfg joycfg;					/* Joy Configuration from envvar */
-
-	/* Open GAME$ port */
-	if (joyPortOpen(&hJoyPort) < 0) return 0;	/* Cannot open... report no joystick */
-	/* Get Max Number of Devices */
-	ulDataLen = sizeof(stGameParms);
-	rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_GET_PARMS,
-			 NULL, 0, NULL, &stGameParms, ulDataLen, &ulDataLen); /* Ask device info */
-	if (rc != 0)
-	{
-		joyPortClose(&hJoyPort);
-		return SDL_SetError("Could not read joystick port.");
-	}
-	maxdevs = 0;
-	if (stGameParms.useA != 0) maxdevs++;
-	if (stGameParms.useB != 0) maxdevs++;
-	if (maxdevs > MAX_JOYSTICKS) maxdevs = MAX_JOYSTICKS;
-
-	/* Defines min/max axes values (callibration) */
-	ulDataLen = sizeof(stGameCalib);
-	rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_GET_CALIB,
-			 NULL, 0, NULL, &stGameCalib, ulDataLen, &ulDataLen);
-	if (rc != 0)
-	{
-		joyPortClose(&hJoyPort);
-		return SDL_SetError("Could not read callibration data.");
-	}
-
-	/* Determine how many joysticks are active */
-	numdevs = 0;	/* Points no device */
-	ucNewJoystickMask = 0x0F;	/* read all 4 joystick axis */
-	ulDataLen = sizeof(ucNewJoystickMask);
-	rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_PORT_RESET,
-			 &ucNewJoystickMask, ulDataLen, &ulDataLen, NULL, 0, NULL);
-	if (rc == 0)
-	{
-		ulDataLen = sizeof(stJoyStatus);
-		rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_PORT_GET,
-				 NULL, 0, NULL, &stJoyStatus, ulDataLen, &ulDataLen);
-		if (rc != 0)
-		{
-			joyPortClose(&hJoyPort);
-			return SDL_SetError("Could not call joystick port.");
-		}
-		ulLastTick = stJoyStatus.ulJs_Ticks;
-		while (stJoyStatus.ulJs_Ticks == ulLastTick)
-		{
-			rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_PORT_GET,
-					 NULL, 0, NULL, &stJoyStatus, ulDataLen, &ulDataLen);
-		}
-		if ((stJoyStatus.ucJs_JoyStickMask & 0x03) > 0) numdevs++;
-		if (((stJoyStatus.ucJs_JoyStickMask >> 2) & 0x03) > 0) numdevs++;
-	}
-
-	if (numdevs > maxdevs) numdevs = maxdevs;
-
-	/* If *any* joystick was detected... Let's configure SDL for them */
-	if (numdevs > 0)
-	{
-		/* Verify if it is a "user defined" joystick */
-		if (joyGetEnv(&joycfg))
-		{
-			GAME_3POS_STRUCT * axis[4];
-			axis[0] = &stGameCalib.Ax;
-			axis[1] = &stGameCalib.Ay;
-			axis[2] = &stGameCalib.Bx;
-			axis[3] = &stGameCalib.By;
-
-			/* Say it has one device only (user defined is always one device only) */
-			numdevs = 1;
-
-			/* Define Device 0 as... */
-			SYS_JoyData[0].id = 0;
-
-			/* Define Number of Axes... up to 4 */
-			if (joycfg.axes>MAX_AXES) joycfg.axes = MAX_AXES;
-			SYS_JoyData[0].axes = joycfg.axes;
-
-			/* Define number of buttons... 8 if 2 axes, 6 if 3 axes and 4 if 4 axes */
-			maxbut = MAX_BUTTONS;
-			if (joycfg.axes>2) maxbut -= ((joycfg.axes - 2)<<1); /* MAX_BUTTONS - 2*(axes-2) */
-			if (joycfg.buttons > maxbut) joycfg.buttons = maxbut;
-			SYS_JoyData[0].buttons = joycfg.buttons;
-
-			/* Define number of hats */
-			if (joycfg.hats > MAX_HATS) joycfg.hats = MAX_HATS;
-			SYS_JoyData[0].hats = joycfg.hats;
-
-			/* Define number of balls */
-			if (joycfg.balls > MAX_BALLS) joycfg.balls = MAX_BALLS;
-			SYS_JoyData[0].balls = joycfg.balls;
-
-			/* Initialize Axes Callibration Values */
-			for (i=0; i<joycfg.axes; i++)
-			{
-				SYS_JoyData[0].axes_min[i] = axis[i]->lower;
-				SYS_JoyData[0].axes_med[i] = axis[i]->centre;
-				SYS_JoyData[0].axes_max[i] = axis[i]->upper;
-			}
-			/* Initialize Buttons 5 to 8 structures */
-			if (joycfg.buttons>=5) SYS_JoyData[0].buttoncalc[0] = ((axis[2]->lower+axis[3]->centre)>>1);
-			if (joycfg.buttons>=6) SYS_JoyData[0].buttoncalc[1] = ((axis[3]->lower+axis[3]->centre)>>1);
-			if (joycfg.buttons>=7) SYS_JoyData[0].buttoncalc[2] = ((axis[2]->upper+axis[3]->centre)>>1);
-			if (joycfg.buttons>=8) SYS_JoyData[0].buttoncalc[3] = ((axis[3]->upper+axis[3]->centre)>>1);
-			/* Intialize Joystick Name */
-			SDL_strlcpy (SYS_JoyData[0].szDeviceName,joycfg.name, SDL_arraysize(SYS_JoyData[0].szDeviceName));
-		}
-		/* Default Init ... autoconfig */
-		else
-		{
-			/* if two devices were detected... configure as Joy1 4 axis and Joy2 2 axis */
-			if (numdevs == 2)
-			{
-				/* Define Device 0 as 4 axes, 4 buttons */
-				SYS_JoyData[0].id=0;
-				SYS_JoyData[0].axes = 4;
-				SYS_JoyData[0].buttons = 4;
-				SYS_JoyData[0].hats = 0;
-				SYS_JoyData[0].balls = 0;
-				SYS_JoyData[0].axes_min[0] = stGameCalib.Ax.lower;
-				SYS_JoyData[0].axes_med[0] = stGameCalib.Ax.centre;
-				SYS_JoyData[0].axes_max[0] = stGameCalib.Ax.upper;
-				SYS_JoyData[0].axes_min[1] = stGameCalib.Ay.lower;
-				SYS_JoyData[0].axes_med[1] = stGameCalib.Ay.centre;
-				SYS_JoyData[0].axes_max[1] = stGameCalib.Ay.upper;
-				SYS_JoyData[0].axes_min[2] = stGameCalib.Bx.lower;
-				SYS_JoyData[0].axes_med[2] = stGameCalib.Bx.centre;
-				SYS_JoyData[0].axes_max[2] = stGameCalib.Bx.upper;
-				SYS_JoyData[0].axes_min[3] = stGameCalib.By.lower;
-				SYS_JoyData[0].axes_med[3] = stGameCalib.By.centre;
-				SYS_JoyData[0].axes_max[3] = stGameCalib.By.upper;
-				/* Define Device 1 as 2 axes, 2 buttons */
-				SYS_JoyData[1].id=1;
-				SYS_JoyData[1].axes = 2;
-				SYS_JoyData[1].buttons = 2;
-				SYS_JoyData[1].hats = 0;
-				SYS_JoyData[1].balls = 0;
-				SYS_JoyData[1].axes_min[0] = stGameCalib.Bx.lower;
-				SYS_JoyData[1].axes_med[0] = stGameCalib.Bx.centre;
-				SYS_JoyData[1].axes_max[0] = stGameCalib.Bx.upper;
-				SYS_JoyData[1].axes_min[1] = stGameCalib.By.lower;
-				SYS_JoyData[1].axes_med[1] = stGameCalib.By.centre;
-				SYS_JoyData[1].axes_max[1] = stGameCalib.By.upper;
-			}
-			/* One joystick only? */
-			else
-			{
-				/* If it is joystick A... */
-				if ((stJoyStatus.ucJs_JoyStickMask & 0x03) > 0)
-				{
-					/* Define Device 0 as 2 axes, 4 buttons */
-					SYS_JoyData[0].id=0;
-					SYS_JoyData[0].axes = 2;
-					SYS_JoyData[0].buttons = 4;
-					SYS_JoyData[0].hats = 0;
-					SYS_JoyData[0].balls = 0;
-					SYS_JoyData[0].axes_min[0] = stGameCalib.Ax.lower;
-					SYS_JoyData[0].axes_med[0] = stGameCalib.Ax.centre;
-					SYS_JoyData[0].axes_max[0] = stGameCalib.Ax.upper;
-					SYS_JoyData[0].axes_min[1] = stGameCalib.Ay.lower;
-					SYS_JoyData[0].axes_med[1] = stGameCalib.Ay.centre;
-					SYS_JoyData[0].axes_max[1] = stGameCalib.Ay.upper;
-				}
-				/* If not, it is joystick B */
-				else
-				{
-					/* Define Device 1 as 2 axes, 2 buttons */
-					SYS_JoyData[0].id=1;
-					SYS_JoyData[0].axes = 2;
-					SYS_JoyData[0].buttons = 2;
-					SYS_JoyData[0].hats = 0;
-					SYS_JoyData[0].balls = 0;
-					SYS_JoyData[0].axes_min[0] = stGameCalib.Bx.lower;
-					SYS_JoyData[0].axes_med[0] = stGameCalib.Bx.centre;
-					SYS_JoyData[0].axes_max[0] = stGameCalib.Bx.upper;
-					SYS_JoyData[0].axes_min[1] = stGameCalib.By.lower;
-					SYS_JoyData[0].axes_med[1] = stGameCalib.By.centre;
-					SYS_JoyData[0].axes_max[1] = stGameCalib.By.upper;
-				}
-			}
-
-			/* Hack to define Joystick Port Names */
-			if (numdevs > maxdevs) numdevs = maxdevs;
-
-			for (i = 0; i < numdevs; i++)
-			{
-				SDL_snprintf(SYS_JoyData[i].szDeviceName,
-						SDL_arraysize(SYS_JoyData[i].szDeviceName),
-						"Default Joystick %c", 'A'+SYS_JoyData[i].id);
-			}
-		}
-	}
-	/* Return the number of devices found */
-	numjoysticks = numdevs;
-	return numdevs;
+    APIRET rc;  /* Generic OS/2 return code */
+    GAME_PORT_STRUCT  stJoyStatus;  /* Joystick Status Structure */
+    GAME_PARM_STRUCT  stGameParms;  /* Joystick Parameter Structure */
+    GAME_CALIB_STRUCT stGameCalib;  /* Calibration Struct */
+    ULONG ulDataLen;  /* Size of data */
+    ULONG ulLastTick; /* Tick Counter for timing operations */
+    Uint8 maxdevs;    /* Maximum number of devices */
+    Uint8 numdevs;    /* Number of present devices */
+    Uint8 maxbut;     /* Maximum number of buttons... */
+    Uint8 i;          /* Temporary Count Vars */
+    Uint8 ucNewJoystickMask;  /* Mask for Joystick Detection */
+    struct _joycfg joycfg;    /* Joy Configuration from envvar */
+
+    /* Open GAME$ port */
+    if (joyPortOpen(&hJoyPort) < 0) {
+        return 0; /* Cannot open... report no joystick */
+    }
+    /* Get Max Number of Devices */
+    ulDataLen = sizeof(stGameParms);
+    rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_GET_PARMS,
+                      NULL, 0, NULL, &stGameParms, ulDataLen, &ulDataLen); /* Ask device info */
+    if (rc != 0) {
+        joyPortClose(&hJoyPort);
+        return SDL_SetError("Could not read joystick port.");
+    }
+    maxdevs = 0;
+    if (stGameParms.useA != 0) {
+        maxdevs++;
+    }
+    if (stGameParms.useB != 0) {
+        maxdevs++;
+    }
+    if (maxdevs > MAX_JOYSTICKS) {
+        maxdevs = MAX_JOYSTICKS;
+    }
+
+    /* Defines min/max axes values (callibration) */
+    ulDataLen = sizeof(stGameCalib);
+    rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_GET_CALIB,
+                     NULL, 0, NULL, &stGameCalib, ulDataLen, &ulDataLen);
+    if (rc != 0) {
+        joyPortClose(&hJoyPort);
+        return SDL_SetError("Could not read callibration data.");
+    }
+
+    /* Determine how many joysticks are active */
+    numdevs = 0;  /* Points no device */
+    ucNewJoystickMask = 0x0F; /* read all 4 joystick axis */
+    ulDataLen = sizeof(ucNewJoystickMask);
+    rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_PORT_RESET,
+                     &ucNewJoystickMask, ulDataLen, &ulDataLen, NULL, 0, NULL);
+    if (rc == 0) {
+        ulDataLen = sizeof(stJoyStatus);
+        rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_PORT_GET,
+                         NULL, 0, NULL, &stJoyStatus, ulDataLen, &ulDataLen);
+        if (rc != 0) {
+            joyPortClose(&hJoyPort);
+            return SDL_SetError("Could not call joystick port.");
+        }
+        ulLastTick = stJoyStatus.ulJs_Ticks;
+        while (stJoyStatus.ulJs_Ticks == ulLastTick) {
+               rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_PORT_GET,
+                                NULL, 0, NULL, &stJoyStatus, ulDataLen, &ulDataLen);
+        }
+        if ((stJoyStatus.ucJs_JoyStickMask & 0x03) > 0) {
+            numdevs++;
+        }
+        if (((stJoyStatus.ucJs_JoyStickMask >> 2) & 0x03) > 0) {
+            numdevs++;
+        }
+    }
+
+    if (numdevs > maxdevs) {
+        numdevs = maxdevs;
+    }
+
+    /* If *any* joystick was detected... Let's configure SDL for them */
+    if (numdevs > 0) {
+        /* Verify if it is a "user defined" joystick */
+        if (joyGetEnv(&joycfg)) {
+            GAME_3POS_STRUCT * axis[4];
+            axis[0] = &stGameCalib.Ax;
+            axis[1] = &stGameCalib.Ay;
+            axis[2] = &stGameCalib.Bx;
+            axis[3] = &stGameCalib.By;
+
+            /* Say it has one device only (user defined is always one device only) */
+            numdevs = 1;
+
+            /* Define Device 0 as... */
+            SYS_JoyData[0].id = 0;
+
+            /* Define Number of Axes... up to 4 */
+            if (joycfg.axes>MAX_AXES) {
+                joycfg.axes = MAX_AXES;
+            }
+            SYS_JoyData[0].axes = joycfg.axes;
+
+            /* Define number of buttons... 8 if 2 axes, 6 if 3 axes and 4 if 4 axes */
+            maxbut = MAX_BUTTONS;
+            if (joycfg.axes > 2) {
+                maxbut -= ((joycfg.axes - 2) << 1);     /* MAX_BUTTONS - 2*(axes-2) */
+            }
+            if (joycfg.buttons > maxbut) {
+                joycfg.buttons = maxbut;
+            }
+            SYS_JoyData[0].buttons = joycfg.buttons;
+
+            /* Define number of hats */
+            if (joycfg.hats > MAX_HATS) {
+                joycfg.hats = MAX_HATS;
+            }
+            SYS_JoyData[0].hats = joycfg.hats;
+
+            /* Define number of balls */
+            if (joycfg.balls > MAX_BALLS) {
+                joycfg.balls = MAX_BALLS;
+            }
+            SYS_JoyData[0].balls = joycfg.balls;
+
+            /* Initialize Axes Callibration Values */
+            for (i = 0; i < joycfg.axes; i++) {
+                 SYS_JoyData[0].axes_min[i] = axis[i]->lower;
+                 SYS_JoyData[0].axes_med[i] = axis[i]->centre;
+                 SYS_JoyData[0].axes_max[i] = axis[i]->upper;
+            }
+            /* Initialize Buttons 5 to 8 structures */
+            if (joycfg.buttons >=5 ) {
+                SYS_JoyData[0].buttoncalc[0] = ((axis[2]->lower + axis[3]->centre) >> 1);
+            }
+            if (joycfg.buttons >=6 ) {
+                SYS_JoyData[0].buttoncalc[1] = ((axis[3]->lower + axis[3]->centre) >> 1);
+            }
+            if (joycfg.buttons >=7 ) {
+                SYS_JoyData[0].buttoncalc[2] = ((axis[2]->upper + axis[3]->centre) >> 1);
+            }
+            if (joycfg.buttons >=8 ) {
+                SYS_JoyData[0].buttoncalc[3] = ((axis[3]->upper + axis[3]->centre) >> 1);
+            }
+            /* Intialize Joystick Name */
+            SDL_strlcpy (SYS_JoyData[0].szDeviceName,joycfg.name, SDL_arraysize(SYS_JoyData[0].szDeviceName));
+        }
+        /* Default Init ... autoconfig */
+        else {
+            /* if two devices were detected... configure as Joy1 4 axis and Joy2 2 axis */
+            if (numdevs == 2) {
+                /* Define Device 0 as 4 axes, 4 buttons */
+                SYS_JoyData[0].id = 0;
+                SYS_JoyData[0].axes = 4;
+                SYS_JoyData[0].buttons = 4;
+                SYS_JoyData[0].hats = 0;
+                SYS_JoyData[0].balls = 0;
+                SYS_JoyData[0].axes_min[0] = stGameCalib.Ax.lower;
+                SYS_JoyData[0].axes_med[0] = stGameCalib.Ax.centre;
+                SYS_JoyData[0].axes_max[0] = stGameCalib.Ax.upper;
+                SYS_JoyData[0].axes_min[1] = stGameCalib.Ay.lower;
+                SYS_JoyData[0].axes_med[1] = stGameCalib.Ay.centre;
+                SYS_JoyData[0].axes_max[1] = stGameCalib.Ay.upper;
+                SYS_JoyData[0].axes_min[2] = stGameCalib.Bx.lower;
+                SYS_JoyData[0].axes_med[2] = stGameCalib.Bx.centre;
+                SYS_JoyData[0].axes_max[2] = stGameCalib.Bx.upper;
+                SYS_JoyData[0].axes_min[3] = stGameCalib.By.lower;
+                SYS_JoyData[0].axes_med[3] = stGameCalib.By.centre;
+                SYS_JoyData[0].axes_max[3] = stGameCalib.By.upper;
+                /* Define Device 1 as 2 axes, 2 buttons */
+                SYS_JoyData[1].id = 1;
+                SYS_JoyData[1].axes = 2;
+                SYS_JoyData[1].buttons = 2;
+                SYS_JoyData[1].hats = 0;
+                SYS_JoyData[1].balls = 0;
+                SYS_JoyData[1].axes_min[0] = stGameCalib.Bx.lower;
+                SYS_JoyData[1].axes_med[0] = stGameCalib.Bx.centre;
+                SYS_JoyData[1].axes_max[0] = stGameCalib.Bx.upper;
+                SYS_JoyData[1].axes_min[1] = stGameCalib.By.lower;
+                SYS_JoyData[1].axes_med[1] = stGameCalib.By.centre;
+                SYS_JoyData[1].axes_max[1] = stGameCalib.By.upper;
+            }
+            /* One joystick only? */
+            else {
+                /* If it is joystick A... */
+                if ((stJoyStatus.ucJs_JoyStickMask & 0x03) > 0) {
+                    /* Define Device 0 as 2 axes, 4 buttons */
+                    SYS_JoyData[0].id = 0;
+                    SYS_JoyData[0].axes = 2;
+                    SYS_JoyData[0].buttons = 4;
+                    SYS_JoyData[0].hats = 0;
+                    SYS_JoyData[0].balls = 0;
+                    SYS_JoyData[0].axes_min[0] = stGameCalib.Ax.lower;
+                    SYS_JoyData[0].axes_med[0] = stGameCalib.Ax.centre;
+                    SYS_JoyData[0].axes_max[0] = stGameCalib.Ax.upper;
+                    SYS_JoyData[0].axes_min[1] = stGameCalib.Ay.lower;
+                    SYS_JoyData[0].axes_med[1] = stGameCalib.Ay.centre;
+                    SYS_JoyData[0].axes_max[1] = stGameCalib.Ay.upper;
+                }
+                /* If not, it is joystick B */
+                else {
+                    /* Define Device 1 as 2 axes, 2 buttons */
+                    SYS_JoyData[0].id = 1;
+                    SYS_JoyData[0].axes = 2;
+                    SYS_JoyData[0].buttons = 2;
+                    SYS_JoyData[0].hats = 0;
+                    SYS_JoyData[0].balls = 0;
+                    SYS_JoyData[0].axes_min[0] = stGameCalib.Bx.lower;
+                    SYS_JoyData[0].axes_med[0] = stGameCalib.Bx.centre;
+                    SYS_JoyData[0].axes_max[0] = stGameCalib.Bx.upper;
+                    SYS_JoyData[0].axes_min[1] = stGameCalib.By.lower;
+                    SYS_JoyData[0].axes_med[1] = stGameCalib.By.centre;
+                    SYS_JoyData[0].axes_max[1] = stGameCalib.By.upper;
+                }
+            }
+
+            /* Hack to define Joystick Port Names */
+            if (numdevs > maxdevs) {
+                numdevs = maxdevs;
+            }
+
+            for (i = 0; i < numdevs; i++) {
+                SDL_snprintf(SYS_JoyData[i].szDeviceName, SDL_arraysize(SYS_JoyData[i].szDeviceName),
+                            "Default Joystick %c", 'A'+SYS_JoyData[i].id);
+            }
+        }
+    }
+
+    /* Return the number of devices found */
+    numjoysticks = numdevs;
+    return numdevs;
 }
 
 static int OS2_NumJoysticks(void)
 {
-	return numjoysticks;
+    return numjoysticks;
 }
 
 static void OS2_JoystickDetect(void)
@@ -379,18 +399,18 @@ static void OS2_JoystickDetect(void)
 
 static const char *OS2_JoystickGetDeviceName(int device_index)
 {
-	/* No need to verify if device exists, already done in upper layer */
-	return SYS_JoyData[device_index].szDeviceName;
+    /* No need to verify if device exists, already done in upper layer */
+    return SYS_JoyData[device_index].szDeviceName;
 }
 
 static const char *OS2_JoystickGetDevicePath(int device_index)
 {
-	return NULL;
+    return NULL;
 }
 
 static int OS2_JoystickGetDevicePlayerIndex(int device_index)
 {
-	return -1;
+    return -1;
 }
 
 static void OS2_JoystickSetDevicePlayerIndex(int device_index, int player_index)
@@ -406,80 +426,75 @@ static SDL_JoystickGUID OS2_JoystickGetDeviceGUID(int device_index)
 
 static SDL_JoystickID OS2_JoystickGetDeviceInstanceID(int device_index)
 {
-	return device_index;
+    return device_index;
 }
 
 /******************************************************************************/
-/* Function to open a joystick for use.													*/
-/* The joystick to open is specified by the device index.		*/
-/* This should fill the nbuttons and naxes fields of the joystick structure.	*/
-/* It returns 0, or -1 if there is an error.												*/
+/* Function to open a joystick for use.                                       */
+/* The joystick to open is specified by the device index.                     */
+/* This should fill the nbuttons and naxes fields of the joystick structure.  */
+/* It returns 0, or -1 if there is an error.                                  */
 /******************************************************************************/
 static int OS2_JoystickOpen(SDL_Joystick *joystick, int device_index)
 {
-	int index;		/* Index shortcut for index in joystick structure */
-	int i;			/* Generic Counter */
-
-	/* allocate memory for system specific hardware data */
-	joystick->hwdata = (struct joystick_hwdata *) SDL_calloc(1, sizeof(*joystick->hwdata));
-	if (!joystick->hwdata)
-	{
-		return SDL_OutOfMemory();
-	}
-
-	/* ShortCut Pointer */
-	index = device_index;
-	joystick->instance_id = device_index;
-
-	/* Define offsets and scales for all axes */
-	joystick->hwdata->id = SYS_JoyData[index].id;
-	for (i = 0; i < MAX_AXES; ++i)
-	{
-		if ((i < 2) || i < SYS_JoyData[index].axes)
-		{
-			joystick->hwdata->transaxes[i].offset = ((SDL_JOYSTICK_AXIS_MAX + SDL_JOYSTICK_AXIS_MIN)>>1) - SYS_JoyData[index].axes_med[i];
-			joystick->hwdata->transaxes[i].scale1 = (float)SDL_abs((SDL_JOYSTICK_AXIS_MIN/SYS_JoyData[index].axes_min[i]));
-			joystick->hwdata->transaxes[i].scale2 = (float)SDL_abs((SDL_JOYSTICK_AXIS_MAX/SYS_JoyData[index].axes_max[i]));
-		}
-		else
-		{
-			joystick->hwdata->transaxes[i].offset = 0;
-			joystick->hwdata->transaxes[i].scale1 = 1.0f; /* Just in case */
-			joystick->hwdata->transaxes[i].scale2 = 1.0f; /* Just in case */
-		}
-	}
-
-	/* fill nbuttons, naxes, and nhats fields */
-	joystick->nbuttons = SYS_JoyData[index].buttons;
-	joystick->naxes = SYS_JoyData[index].axes;
-
-	/* joystick->nhats = SYS_JoyData[index].hats; */
-	joystick->nhats = 0; /* No support for hats at this time */
-
-	/* joystick->nballs = SYS_JoyData[index].balls; */
-	joystick->nballs = 0; /* No support for balls at this time */
-
-	return 0;
+    int index;  /* Index shortcut for index in joystick structure */
+    int i;      /* Generic Counter */
+
+    /* allocate memory for system specific hardware data */
+    joystick->hwdata = (struct joystick_hwdata *) SDL_calloc(1, sizeof(*joystick->hwdata));
+    if (!joystick->hwdata) {
+        return SDL_OutOfMemory();
+    }
+
+    /* ShortCut Pointer */
+    index = device_index;
+    joystick->instance_id = device_index;
+
+    /* Define offsets and scales for all axes */
+    joystick->hwdata->id = SYS_JoyData[index].id;
+    for (i = 0; i < MAX_AXES; ++i) {
+        if ((i < 2) || i < SYS_JoyData[index].axes) {
+            joystick->hwdata->transaxes[i].offset = ((SDL_JOYSTICK_AXIS_MAX + SDL_JOYSTICK_AXIS_MIN)>>1) - SYS_JoyData[index].axes_med[i];
+            joystick->hwdata->transaxes[i].scale1 = (float)SDL_abs((SDL_JOYSTICK_AXIS_MIN/SYS_JoyData[index].axes_min[i]));
+            joystick->hwdata->transaxes[i].scale2 = (float)SDL_abs((SDL_JOYSTICK_AXIS_MAX/SYS_JoyData[index].axes_max[i]));
+        } else {
+            joystick->hwdata->transaxes[i].offset = 0;
+            joystick->hwdata->transaxes[i].scale1 = 1.0f; /* Just in case */
+            joystick->hwdata->transaxes[i].scale2 = 1.0f; /* Just in case */
+        }
+    }
+
+    /* fill nbuttons, naxes, and nhats fields */
+    joystick->nbuttons = SYS_JoyData[index].buttons;
+    joystick->naxes = SYS_JoyData[index].axes;
+
+    /* joystick->nhats = SYS_JoyData[index].hats; */
+    joystick->nhats = 0; /* No support for hats at this time */
+
+    /* joystick->nballs = SYS_JoyData[index].balls; */
+    joystick->nballs = 0; /* No support for balls at this time */
+
+    return 0;
 }
 
 static int OS2_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
 {
-	return SDL_Unsupported();
+    return SDL_Unsupported();
 }
 
 static int OS2_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
 {
-	return SDL_Unsupported();
+    return SDL_Unsupported();
 }
 
 static Uint32 OS2_JoystickGetCapabilities(SDL_Joystick *joystick)
 {
-	return 0;
+    return 0;
 }
 
 static int OS2_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
 {
-	return SDL_Unsupported();
+    return SDL_Unsupported();
 }
 
 static int OS2_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
@@ -489,133 +504,145 @@ static int OS2_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int 
 
 static int OS2_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
 {
-	return SDL_Unsupported();
+    return SDL_Unsupported();
 }
 
 /***************************************************************************/
-/* Function to update the state of a joystick - called as a device poll.	*/
-/* This function shouldn't update the joystick structure directly,			*/
-/* but instead should call SDL_PrivateJoystick*() to deliver events			*/
-/* and update joystick device state.													*/
+/* Function to update the state of a joystick - called as a device poll.   */
+/* This function shouldn't update the joystick structure directly,         */
+/* but instead should call SDL_PrivateJoystick*() to deliver events        */
+/* and update joystick device state.                                       */
 /***************************************************************************/
 static void OS2_JoystickUpdate(SDL_Joystick *joystick)
 {
-	APIRET rc;	/* Generic OS/2 return code */
-	int index;					/* index shortcurt to joystick index */
-	int i;						/* Generic counter */
-	int normbut;					/* Number of buttons reported by joystick */
-	int corr;							/* Correction for button names */
-	Sint16 value;					/* Values used to update axis values */
-	struct _transaxes *transaxes;			/* Shortcut for Correction structure */
-	Uint32 pos[MAX_AXES];				/* Vector to inform the Axis status */
-	ULONG ulDataLen;					/* Size of data */
-	GAME_STATUS_STRUCT stGameStatus;		/* Joystick Status Structure */
-
-	ulDataLen = sizeof(stGameStatus);
-	rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_GET_STATUS,
-			 NULL, 0, NULL, &stGameStatus, ulDataLen, &ulDataLen);
-	if (rc != 0)
-	{
-		SDL_SetError("Could not read joystick status.");
-		return; /* Could not read data */
-	}
-
-	/* Shortcut pointer */
-	index = joystick->instance_id;
-
-	/* joystick motion events */
-
-	if (SYS_JoyData[index].id == 0)
-	{
-		pos[0] = stGameStatus.curdata.A.x;
-		pos[1] = stGameStatus.curdata.A.y;
-		if (SYS_JoyData[index].axes >= 3) pos[2] = stGameStatus.curdata.B.x;
-		else pos[2] = 0;
-		if (SYS_JoyData[index].axes >= 4) pos[3] = stGameStatus.curdata.B.y;
-		else pos[3] = 0;
-		/* OS/2 basic drivers do not support more than 4 axes joysticks */
-	}
-	else if (SYS_JoyData[index].id == 1)
-	{
-		pos[0] = stGameStatus.curdata.B.x;
-		pos[1] = stGameStatus.curdata.B.y;
-		pos[2] = 0;
-		pos[3] = 0;
-	}
-
-	/* Corrects the movements using the callibration */
-	transaxes = joystick->hwdata->transaxes;
-	for (i = 0; i < joystick->naxes; i++)
-	{
-		value = pos[i] + transaxes[i].offset;
-		if (value < 0)
-		{
-			value *= transaxes[i].scale1;
-			if (value > 0) value = SDL_JOYSTICK_AXIS_MIN;
-		}
-		else
-		{
-			value *= transaxes[i].scale2;
-			if (value < 0) value = SDL_JOYSTICK_AXIS_MAX;
-		}
-		SDL_PrivateJoystickAxis(joystick, (Uint8)i, (Sint16)value);
-	}
-
-	/* joystick button A to D events */
-	if (SYS_JoyData[index].id == 1) corr = 2;
-	else corr = 0;
-	normbut = 4;	/* Number of normal buttons */
-	if (joystick->nbuttons < normbut) normbut = joystick->nbuttons;
-	for (i = corr; (i-corr) < normbut; ++i)
-	{
-		/*
-		Button A: 1110 0000
-		Button B: 1101 0000
-		Button C: 1011 0000
-		Button D: 0111 0000
-		*/
-		if ((~stGameStatus.curdata.butMask)>>4 & JOY_BUTTON_FLAG(i))
-		{
-			SDL_PrivateJoystickButton(joystick, (Uint8)(i-corr), SDL_PRESSED);
-		}
-		else
-		{
-			SDL_PrivateJoystickButton(joystick, (Uint8)(i-corr), SDL_RELEASED);
-		}
-	}
-
-	/* Joystick button E to H buttons */
-	/*
-	Button E: Axis 2 X Left
-	Button F: Axis 2 Y Up
-	Button G: Axis 2 X Right
-	Button H: Axis 2 Y Down
-	*/
-	if (joystick->nbuttons >= 5)
-	{
-		if (stGameStatus.curdata.B.x < SYS_JoyData[index].buttoncalc[0]) SDL_PrivateJoystickButton(joystick, (Uint8)4, SDL_PRESSED);
-		else SDL_PrivateJoystickButton(joystick, (Uint8)4, SDL_RELEASED);
-	}
-	if (joystick->nbuttons >= 6)
-	{
-		if (stGameStatus.curdata.B.y < SYS_JoyData[index].buttoncalc[1]) SDL_PrivateJoystickButton(joystick, (Uint8)5, SDL_PRESSED);
-		else SDL_PrivateJoystickButton(joystick, (Uint8)5, SDL_RELEASED);
-	}
-	if (joystick->nbuttons >= 7)
-	{
-		if (stGameStatus.curdata.B.x > SYS_JoyData[index].buttoncalc[2]) SDL_PrivateJoystickButton(joystick, (Uint8)6, SDL_PRESSED);
-		else SDL_PrivateJoystickButton(joystick, (Uint8)6, SDL_RELEASED);
-	}
-	if (joystick->nbuttons >= 8)
-	{
-		if (stGameStatus.curdata.B.y > SYS_JoyData[index].buttoncalc[3]) SDL_PrivateJoystickButton(joystick, (Uint8)7, SDL_PRESSED);
-		else SDL_PrivateJoystickButton(joystick, (Uint8)7, SDL_RELEASED);
-	}
-
-	/* joystick hat events */
-	/* Not Supported under OS/2 */
-	/* joystick ball events */
-	/* Not Supported under OS/2 */
+    APIRET rc;  /* Generic OS/2 return code */
+    int index;  /* index shortcurt to joystick index */
+    int i;      /* Generic counter */
+    int normbut;    /* Number of buttons reported by joystick */
+    int corr;       /* Correction for button names */
+    Sint16 value;   /* Values used to update axis values */
+    struct _transaxes *transaxes; /* Shortcut for Correction structure */
+    Uint32 pos[MAX_AXES];         /* Vector to inform the Axis status  */
+    ULONG ulDataLen;              /* Size of data */
+    GAME_STATUS_STRUCT stGameStatus;    /* Joystick Status Structure   */
+
+    ulDataLen = sizeof(stGameStatus);
+    rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_GET_STATUS,
+                      NULL, 0, NULL, &stGameStatus, ulDataLen, &ulDataLen);
+    if (rc != 0) {
+        SDL_SetError("Could not read joystick status.");
+        return; /* Could not read data */
+    }
+
+    /* Shortcut pointer */
+    index = joystick->instance_id;
+
+    /* joystick motion events */
+
+    if (SYS_JoyData[index].id == 0) {
+        pos[0] = stGameStatus.curdata.A.x;
+        pos[1] = stGameStatus.curdata.A.y;
+        if (SYS_JoyData[index].axes >= 3) {
+            pos[2] = stGameStatus.curdata.B.x;
+        } else {
+            pos[2] = 0;
+        }
+        if (SYS_JoyData[index].axes >= 4) {
+            pos[3] = stGameStatus.curdata.B.y;
+        } else {
+            pos[3] = 0;
+        }
+        /* OS/2 basic drivers do not support more than 4 axes joysticks */
+    }
+    else if (SYS_JoyData[index].id == 1) {
+        pos[0] = stGameStatus.curdata.B.x;
+        pos[1] = stGameStatus.curdata.B.y;
+        pos[2] = 0;
+        pos[3] = 0;
+    }
+
+    /* Corrects the movements using the callibration */
+    transaxes = joystick->hwdata->transaxes;
+    for (i = 0; i < joystick->naxes; i++) {
+        value = pos[i] + transaxes[i].offset;
+        if (value < 0) {
+            value *= transaxes[i].scale1;
+            if (value > 0) {
+                value = SDL_JOYSTICK_AXIS_MIN;
+            }
+        } else {
+            value *= transaxes[i].scale2;
+            if (value < 0) {
+                value = SDL_JOYSTICK_AXIS_MAX;
+            }
+        }
+        SDL_PrivateJoystickAxis(joystick, (Uint8)i, (Sint16)value);
+    }
+
+    /* joystick button A to D events */
+    if (SYS_JoyData[index].id == 1) {
+        corr = 2;
+    } else {
+        corr = 0;
+    }
+    normbut = 4;    /* Number of normal buttons */
+    if (joystick->nbuttons < normbut) {
+        normbut = joystick->nbuttons;
+    }
+    for (i = corr; (i-corr) < normbut; ++i) {
+        /*
+        Button A: 1110 0000
+        Button B: 1101 0000
+        Button C: 1011 0000
+        Button D: 0111 0000
+        */
+        if ((~stGameStatus.curdata.butMask)>>4 & JOY_BUTTON_FLAG(i)) {
+            SDL_PrivateJoystickButton(joystick, (Uint8)(i-corr), SDL_PRESSED);
+        } else {
+            SDL_PrivateJoystickButton(joystick, (Uint8)(i-corr), SDL_RELEASED);
+        }
+    }
+
+    /* Joystick button E to H buttons */
+    /*
+    Button E: Axis 2 X Left
+    Button F: Axis 2 Y Up
+    Button G: Axis 2 X Right
+    Button H: Axis 2 Y Down
+    */
+    if (joystick->nbuttons >= 5) {
+        if (stGameStatus.curdata.B.x < SYS_JoyData[index].buttoncalc[0]) {
+            SDL_PrivateJoystickButton(joystick, (Uint8)4, SDL_PRESSED);
+        } else {
+            SDL_PrivateJoystickButton(joystick, (Uint8)4, SDL_RELEASED);
+        }
+    }
+    if (joystick->nbuttons >= 6) {
+        if (stGameStatus.curdata.B.y < SYS_JoyData[index].buttoncalc[1]) {
+            SDL_PrivateJoystickButton(joystick, (Uint8)5, SDL_PRESSED);
+        } else {
+            SDL_PrivateJoystickButton(joystick, (Uint8)5, SDL_RELEASED);
+        }
+    }
+    if (joystick->nbuttons >= 7) {
+        if (stGameStatus.curdata.B.x > SYS_JoyData[index].buttoncalc[2]) {
+            SDL_PrivateJoystickButton(joystick, (Uint8)6, SDL_PRESSED);
+        } else {
+            SDL_PrivateJoystickButton(joystick, (Uint8)6, SDL_RELEASED);
+        }
+    }
+    if (joystick->nbuttons >= 8) {
+        if (stGameStatus.curdata.B.y > SYS_JoyData[index].buttoncalc[3]) {
+            SDL_PrivateJoystickButton(joystick, (Uint8)7, SDL_PRESSED);
+        } else {
+           SDL_PrivateJoystickButton(joystick, (Uint8)7, SDL_RELEASED);
+        }
+    }
+
+    /* joystick hat events */
+    /* Not Supported under OS/2 */
+    /* joystick ball events */
+    /* Not Supported under OS/2 */
 }
 
 /******************************************/
@@ -623,8 +650,8 @@ static void OS2_JoystickUpdate(SDL_Joystick *joystick)
 /******************************************/
 static void OS2_JoystickClose(SDL_Joystick *joystick)
 {
-	/* free system specific hardware data */
-	SDL_free(joystick->hwdata);
+    /* free system specific hardware data */
+    SDL_free(joystick->hwdata);
 }
 
 /********************************************************************/
@@ -632,12 +659,12 @@ static void OS2_JoystickClose(SDL_Joystick *joystick)
 /********************************************************************/
 static void OS2_JoystickQuit(void)
 {
-	joyPortClose(&hJoyPort);
+    joyPortClose(&hJoyPort);
 }
 
 static SDL_bool OS2_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
 {
-	return SDL_FALSE;
+    return SDL_FALSE;
 }
 
 
@@ -650,38 +677,38 @@ static SDL_bool OS2_JoystickGetGamepadMapping(int device_index, SDL_GamepadMappi
 /*****************************************/
 static int joyPortOpen(HFILE * hGame)
 {
-	APIRET	rc;				/* Generic Return Code */
-	ULONG		ulAction;		/* ? */
-	ULONG		ulVersion;		/* Version of joystick driver */
-	ULONG		ulDataLen;		/* Size of version data */
-
-	/* Verifies if joyport is not already open... */
-	if (*hGame != NULLHANDLE) return 0;
-
-	/* Open GAME$ for read */
-	rc = DosOpen("GAME$   ", hGame, &ulAction, 0, FILE_READONLY,
-		     FILE_OPEN, OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, NULL);
-	if (rc != 0)
-	{
-		return SDL_SetError("Could not open Joystick Port.");
-	}
-
-	/* Get Joystick Driver Version... must be 2.0 or higher */
-	ulVersion = 0;
-	ulDataLen = sizeof(ulVersion);
-	rc = DosDevIOCtl(*hGame, IOCTL_CAT_USER, GAME_GET_VERSION,
-			 NULL, 0, NULL, &ulVersion, ulDataLen, &ulDataLen);
-	if (rc != 0)
-	{
-		joyPortClose(hGame);
-		return SDL_SetError("Could not get Joystick Driver version.");
-	}
-	if (ulVersion < 0x20)
-	{
-		joyPortClose(hGame);
-		return SDL_SetError("Driver too old. At least IBM driver version 2.0 required.");
-	}
-	return 0;
+    APIRET  rc; /* Generic Return Code */
+    ULONG ulAction;   /* ? */
+    ULONG ulVersion;  /* Version of joystick driver */
+    ULONG ulDataLen;  /* Size of version data */
+
+    /* Verifies if joyport is not already open... */
+    if (*hGame != NULLHANDLE) {
+        return 0;
+    }
+
+    /* Open GAME$ for read */
+    rc = DosOpen("GAME$   ", hGame, &ulAction, 0, FILE_READONLY,
+                 FILE_OPEN, OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, NULL);
+    if (rc != 0) {
+        return SDL_SetError("Could not open Joystick Port.");
+    }
+
+    /* Get Joystick Driver Version... must be 2.0 or higher */
+    ulVersion = 0;
+    ulDataLen = sizeof(ulVersion);
+    rc = DosDevIOCtl(*hGame, IOCTL_CAT_USER, GAME_GET_VERSION,
+                     NULL, 0, NULL, &ulVersion, ulDataLen, &ulDataLen);
+    if (rc != 0) {
+        joyPortClose(hGame);
+        return SDL_SetError("Could not get Joystick Driver version.");
+    }
+    if (ulVersion < 0x20) {
+        joyPortClose(hGame);
+        return SDL_SetError("Driver too old. At least IBM driver version 2.0 required.");
+    }
+
+    return 0;
 }
 
 /****************************/
@@ -689,8 +716,10 @@ static int joyPortOpen(HFILE * hGame)
 /****************************/
 static void joyPortClose(HFILE * hGame)
 {
-	if (*hGame != NULLHANDLE) DosClose(*hGame);
-	*hGame = NULLHANDLE;
+    if (*hGame != NULLHANDLE) {
+        DosClose(*hGame);
+    }
+    *hGame = NULLHANDLE;
 }
 
 /***************************/
@@ -698,106 +727,111 @@ static void joyPortClose(HFILE * hGame)
 /***************************/
 static int joyGetEnv(struct _joycfg * joydata)
 {
-	const char *joyenv;		/* Pointer to tested character */
-	char tempnumber[5];		/* Temporary place to put numeric texts */
-
-	joyenv = SDL_getenv("SDL_OS2_JOYSTICK");
-	if (joyenv == NULL) {
-		return 0;
-	}
-
-	/* Joystick Environment is defined! */
-	while (*joyenv == ' ' && *joyenv != 0) {
-		joyenv++; /* jump spaces... */
-	}
-
-	/* If the string name starts with '... get if fully */
-	if (*joyenv == '\'') {
-		joyenv++;
-		joyenv += joyGetData(joyenv,joydata->name,'\'',sizeof(joydata->name));
-	}
-	/* If not, get it until the next space */
-	else if (*joyenv == '\"') {
-		joyenv++;
-		joyenv += joyGetData(joyenv,joydata->name,'\"',sizeof(joydata->name));
-	}
-	else {
-		joyenv += joyGetData(joyenv,joydata->name, ' ',sizeof(joydata->name));
-	}
-
-	/* Now get the number of axes */
-	while (*joyenv == ' ' && *joyenv != 0) joyenv++; /* jump spaces... */
-	joyenv += joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber));
-	joydata->axes = SDL_atoi(tempnumber);
-
-	/* Now get the number of buttons */
-	while (*joyenv == ' ' && *joyenv != 0) joyenv++; /* jump spaces... */
-	joyenv += joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber));
-	joydata->buttons = SDL_atoi(tempnumber);
-
-	/* Now get the number of hats */
-	while (*joyenv == ' ' && *joyenv != 0) joyenv++; /* jump spaces... */
-	joyenv += joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber));
-	joydata->hats = SDL_atoi(tempnumber);
-
-	/* Now get the number of balls */
-	while (*joyenv==' ' && *joyenv != 0) joyenv++; /* jump spaces... */
-	joyenv += joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber));
-	joydata->balls = SDL_atoi(tempnumber);
-	return 1;
+    const char *joyenv; /* Pointer to tested character */
+    char tempnumber[5]; /* Temporary place to put numeric texts */
+
+    joyenv = SDL_getenv("SDL_OS2_JOYSTICK");
+    if (joyenv == NULL) {
+        return 0;
+    }
+
+    /* Joystick Environment is defined! */
+    while (*joyenv == ' ' && *joyenv != 0) {
+        joyenv++; /* jump spaces... */
+    }
+
+    /* If the string name starts with '... get if fully */
+    if (*joyenv == '\'') {
+        joyenv++;
+        joyenv += joyGetData(joyenv,joydata->name,'\'',sizeof(joydata->name));
+    }
+    /* If not, get it until the next space */
+    else if (*joyenv == '\"') {
+        joyenv++;
+        joyenv += joyGetData(joyenv,joydata->name,'\"',sizeof(joydata->name));
+    }
+    else {
+        joyenv += joyGetData(joyenv,joydata->name, ' ',sizeof(joydata->name));
+    }
+
+    /* Now get the number of axes */
+    while (*joyenv == ' ' && *joyenv != 0) {
+        joyenv++; /* jump spaces... */
+    }
+    joyenv += joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber));
+    joydata->axes = SDL_atoi(tempnumber);
+
+    /* Now get the number of buttons */
+    while (*joyenv == ' ' && *joyenv != 0) {
+        joyenv++; /* jump spaces... */
+    }
+    joyenv += joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber));
+    joydata->buttons = SDL_atoi(tempnumber);
+
+    /* Now get the number of hats */
+    while (*joyenv == ' ' && *joyenv != 0) {
+        joyenv++; /* jump spaces... */
+    }
+    joyenv += joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber));
+    joydata->hats = SDL_atoi(tempnumber);
+
+    /* Now get the number of balls */
+    while (*joyenv==' ' && *joyenv != 0) {
+        joyenv++; /* jump spaces... */
+    }
+    joyenv += joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber));
+    joydata->balls = SDL_atoi(tempnumber);
+
+    return 1;
 }
 
 /************************************************************************/
-/* Get a text from in the string starting in joyenv until it finds		*/
-/* the stopchar or maxchars is reached. The result is placed in name.	*/
+/* Get a text from in the string starting in joyenv until it finds      */
+/* the stopchar or maxchars is reached. The result is placed in name.   */
 /************************************************************************/
 static int joyGetData(const char *joyenv, char *name, char stopchar, size_t maxchars)
 {
-	char *nameptr;			/* Pointer to the selected character */
-	int chcnt = 0;			/* Count how many characters where copied */
-
-	nameptr = name;
-	while (*joyenv!=stopchar && *joyenv!=0)
-	{
-		if (nameptr < (name + (maxchars-1)))
-		{
-			*nameptr = *joyenv; /* Only copy if smaller than maximum */
-			nameptr++;
-		}
-		chcnt++;
-		joyenv++;
-	}
-	if (*joyenv == stopchar)
-	{
-		joyenv++; /* Jump stopchar */
-		chcnt++;
-	}
-	*nameptr = 0; /* Mark last byte */
-	return chcnt;
+    char *nameptr;  /* Pointer to the selected character */
+    int chcnt = 0;  /* Count how many characters where copied */
+
+    nameptr = name;
+    while (*joyenv!=stopchar && *joyenv != 0) {
+        if (nameptr < (name + (maxchars-1))) {
+            *nameptr = *joyenv; /* Only copy if smaller than maximum */
+            nameptr++;
+        }
+        chcnt++;
+        joyenv++;
+    }
+    if (*joyenv == stopchar) {
+        joyenv++; /* Jump stopchar */
+        chcnt++;
+    }
+    *nameptr = 0; /* Mark last byte */
+    return chcnt;
 }
 
-SDL_JoystickDriver SDL_OS2_JoystickDriver =
-{
-	OS2_JoystickInit,
-	OS2_NumJoysticks,
-	OS2_JoystickDetect,
-	OS2_JoystickGetDeviceName,
-	OS2_JoystickGetDevicePath,
-	OS2_JoystickGetDevicePlayerIndex,
-	OS2_JoystickSetDevicePlayerIndex,
-	OS2_JoystickGetDeviceGUID,
-	OS2_JoystickGetDeviceInstanceID,
-	OS2_JoystickOpen,
-	OS2_JoystickRumble,
-	OS2_JoystickRumbleTriggers,
-	OS2_JoystickGetCapabilities,
-	OS2_JoystickSetLED,
-	OS2_JoystickSendEffect,
-	OS2_JoystickSetSensorsEnabled,
-	OS2_JoystickUpdate,
-	OS2_JoystickClose,
-	OS2_JoystickQuit,
-	OS2_JoystickGetGamepadMapping
+SDL_JoystickDriver SDL_OS2_JoystickDriver = {
+    OS2_JoystickInit,
+    OS2_NumJoysticks,
+    OS2_JoystickDetect,
+    OS2_JoystickGetDeviceName,
+    OS2_JoystickGetDevicePath,
+    OS2_JoystickGetDevicePlayerIndex,
+    OS2_JoystickSetDevicePlayerIndex,
+    OS2_JoystickGetDeviceGUID,
+    OS2_JoystickGetDeviceInstanceID,
+    OS2_JoystickOpen,
+    OS2_JoystickRumble,
+    OS2_JoystickRumbleTriggers,
+    OS2_JoystickGetCapabilities,
+    OS2_JoystickSetLED,
+    OS2_JoystickSendEffect,
+    OS2_JoystickSetSensorsEnabled,
+    OS2_JoystickUpdate,
+    OS2_JoystickClose,
+    OS2_JoystickQuit,
+    OS2_JoystickGetGamepadMapping
 };
 
 #endif /* SDL_JOYSTICK_OS2 */