comparison src/joystick/os2/SDL_sysjoystick.c @ 1895:c121d94672cb

SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 10 Jul 2006 21:04:37 +0000
parents 92947e3a18db
children 99210400e8b9
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
36 #include "SDL_joystick.h" 36 #include "SDL_joystick.h"
37 #include "SDL_events.h" 37 #include "SDL_events.h"
38 #include "../SDL_sysjoystick.h" 38 #include "../SDL_sysjoystick.h"
39 #include "../SDL_joystick_c.h" 39 #include "../SDL_joystick_c.h"
40 40
41 HFILE hJoyPort = NULL; /* Joystick GAME$ Port Address */ 41 HFILE hJoyPort = NULL; /* Joystick GAME$ Port Address */
42 #define MAX_JOYSTICKS 2 /* Maximum of two joysticks */ 42 #define MAX_JOYSTICKS 2 /* Maximum of two joysticks */
43 #define MAX_AXES 4 /* each joystick can have up to 4 axes */ 43 #define MAX_AXES 4 /* each joystick can have up to 4 axes */
44 #define MAX_BUTTONS 8 /* 8 buttons */ 44 #define MAX_BUTTONS 8 /* 8 buttons */
45 #define MAX_HATS 0 /* 0 hats - OS/2 doesn't support it */ 45 #define MAX_HATS 0 /* 0 hats - OS/2 doesn't support it */
46 #define MAX_BALLS 0 /* and 0 balls - OS/2 doesn't support it */ 46 #define MAX_BALLS 0 /* and 0 balls - OS/2 doesn't support it */
47 #define AXIS_MIN -32768 /* minimum value for axes coordinate */ 47 #define AXIS_MIN -32768 /* minimum value for axes coordinate */
48 #define AXIS_MAX 32767 /* maximum value for axes coordinate */ 48 #define AXIS_MAX 32767 /* maximum value for axes coordinate */
49 #define MAX_JOYNAME 128 /* Joystick name may have 128 characters */ 49 #define MAX_JOYNAME 128 /* Joystick name may have 128 characters */
50 /* limit axes to 256 possible positions to filter out noise */ 50 /* limit axes to 256 possible positions to filter out noise */
51 #define JOY_AXIS_THRESHOLD (((AXIS_MAX)-(AXIS_MIN))/256) 51 #define JOY_AXIS_THRESHOLD (((AXIS_MAX)-(AXIS_MIN))/256)
52 /* Calc Button Flag for buttons A to D */ 52 /* Calc Button Flag for buttons A to D */
53 #define JOY_BUTTON_FLAG(n) (1<<n) 53 #define JOY_BUTTON_FLAG(n) (1<<n)
54 54
55 /* Joystick data... hold information about detected devices */ 55 /* Joystick data... hold information about detected devices */
56 typedef struct SYS_JoyData_s 56 typedef struct SYS_JoyData_s
57 { 57 {
58 Sint8 id; // Device ID 58 Sint8 id; // Device ID
59 char szDeviceName[MAX_JOYNAME]; // Device Name 59 char szDeviceName[MAX_JOYNAME]; // Device Name
60 char axes; // Number of axes 60 char axes; // Number of axes
61 char buttons; // Number of buttons 61 char buttons; // Number of buttons
62 char hats; // Number of buttons 62 char hats; // Number of buttons
63 char balls; // Number of buttons 63 char balls; // Number of buttons
64 int axes_min[MAX_AXES]; // minimum callibration value for axes 64 int axes_min[MAX_AXES]; // minimum callibration value for axes
65 int axes_med[MAX_AXES]; // medium callibration value for axes 65 int axes_med[MAX_AXES]; // medium callibration value for axes
66 int axes_max[MAX_AXES]; // maximum callibration value for axes 66 int axes_max[MAX_AXES]; // maximum callibration value for axes
67 int buttoncalc[4]; // Used for buttons 5, 6, 7 and 8. 67 int buttoncalc[4]; // Used for buttons 5, 6, 7 and 8.
68 } SYS_JoyData_t, *SYS_JoyData_p; 68 } SYS_JoyData_t, *SYS_JoyData_p;
69 69
70 SYS_JoyData_t SYS_JoyData[MAX_JOYSTICKS]; 70 SYS_JoyData_t SYS_JoyData[MAX_JOYSTICKS];
71 71
72 72
73 /* Structure used to convert data from OS/2 driver format to SDL format */ 73 /* Structure used to convert data from OS/2 driver format to SDL format */
74 struct joystick_hwdata 74 struct joystick_hwdata
75 { 75 {
76 Sint8 id; 76 Sint8 id;
77 struct _transaxes 77 struct _transaxes
78 { 78 {
79 int offset; /* Center Offset */ 79 int offset; /* Center Offset */
80 float scale1; /* Center to left/up Scale */ 80 float scale1; /* Center to left/up Scale */
81 float scale2; /* Center to right/down Scale */ 81 float scale2; /* Center to right/down Scale */
82 } transaxes[MAX_AXES]; 82 } transaxes[MAX_AXES];
83 }; 83 };
84 84
85 /* Structure used to get values from Joystick Environment Variable */ 85 /* Structure used to get values from Joystick Environment Variable */
86 struct _joycfg 86 struct _joycfg
87 { 87 {
88 char name[MAX_JOYNAME]; 88 char name[MAX_JOYNAME];
89 unsigned int axes; 89 unsigned int axes;
90 unsigned int buttons; 90 unsigned int buttons;
91 unsigned int hats; 91 unsigned int hats;
92 unsigned int balls; 92 unsigned int balls;
93 }; 93 };
94 94
95 /* OS/2 Implementation Function Prototypes */ 95 /* OS/2 Implementation Function Prototypes */
96 APIRET joyPortOpen(HFILE * hGame); 96 APIRET joyPortOpen(HFILE * hGame);
97 void joyPortClose(HFILE * hGame); 97 void joyPortClose(HFILE * hGame);
98 int joyGetData(char *joyenv, char *name, char stopchar, size_t maxchars); 98 int joyGetData(char *joyenv, char *name, char stopchar, size_t maxchars);
99 int joyGetEnv(struct _joycfg * joydata); 99 int joyGetEnv(struct _joycfg *joydata);
100 100
101 101
102 102
103 /************************************************************************/ 103 /************************************************************************/
104 /* Function to scan the system for joysticks. */ 104 /* Function to scan the system for joysticks. */
105 /* This function should set SDL_numjoysticks to the number of available */ 105 /* This function should set SDL_numjoysticks to the number of available */
106 /* joysticks. Joystick 0 should be the system default joystick. */ 106 /* joysticks. Joystick 0 should be the system default joystick. */
107 /* It should return 0, or -1 on an unrecoverable fatal error. */ 107 /* It should return 0, or -1 on an unrecoverable fatal error. */
108 /************************************************************************/ 108 /************************************************************************/
109 int SDL_SYS_JoystickInit(void) 109 int
110 { 110 SDL_SYS_JoystickInit(void)
111 APIRET rc; /* Generic OS/2 return code */ 111 {
112 GAME_PORT_STRUCT stJoyStatus; /* Joystick Status Structure */ 112 APIRET rc; /* Generic OS/2 return code */
113 GAME_PARM_STRUCT stGameParms; /* Joystick Parameter Structure */ 113 GAME_PORT_STRUCT stJoyStatus; /* Joystick Status Structure */
114 GAME_CALIB_STRUCT stGameCalib; /* Calibration Struct */ 114 GAME_PARM_STRUCT stGameParms; /* Joystick Parameter Structure */
115 ULONG ulDataLen; /* Size of data */ 115 GAME_CALIB_STRUCT stGameCalib; /* Calibration Struct */
116 ULONG ulLastTick; /* Tick Counter for timing operations */ 116 ULONG ulDataLen; /* Size of data */
117 Uint8 maxdevs; /* Maximum number of devices */ 117 ULONG ulLastTick; /* Tick Counter for timing operations */
118 Uint8 numdevs; /* Number of present devices */ 118 Uint8 maxdevs; /* Maximum number of devices */
119 Uint8 maxbut; /* Maximum number of buttons... */ 119 Uint8 numdevs; /* Number of present devices */
120 Uint8 i; /* Temporary Count Vars */ 120 Uint8 maxbut; /* Maximum number of buttons... */
121 Uint8 ucNewJoystickMask; /* Mask for Joystick Detection */ 121 Uint8 i; /* Temporary Count Vars */
122 struct _joycfg joycfg; /* Joy Configuration from envvar */ 122 Uint8 ucNewJoystickMask; /* Mask for Joystick Detection */
123 struct _joycfg joycfg; /* Joy Configuration from envvar */
123 124
124 125
125 /* Get Max Number of Devices */ 126 /* Get Max Number of Devices */
126 rc = joyPortOpen(&hJoyPort); /* Open GAME$ port */ 127 rc = joyPortOpen(&hJoyPort); /* Open GAME$ port */
127 if (rc != 0) return 0; /* Cannot open... report no joystick */ 128 if (rc != 0)
128 ulDataLen = sizeof(stGameParms); 129 return 0; /* Cannot open... report no joystick */
129 rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_GET_PARMS, 130 ulDataLen = sizeof(stGameParms);
130 NULL, 0, NULL, &stGameParms, ulDataLen, &ulDataLen); /* Ask device info */ 131 rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_GET_PARMS, NULL, 0, NULL, &stGameParms, ulDataLen, &ulDataLen); /* Ask device info */
131 if (rc != 0) 132 if (rc != 0) {
132 { 133 joyPortClose(&hJoyPort);
133 joyPortClose(&hJoyPort); 134 SDL_SetError("Could not read joystick port.");
134 SDL_SetError("Could not read joystick port."); 135 return -1;
135 return -1; 136 }
136 } 137 if (stGameParms.useA != 0)
137 if (stGameParms.useA != 0) maxdevs++; 138 maxdevs++;
138 if (stGameParms.useB != 0) maxdevs++; 139 if (stGameParms.useB != 0)
139 if ( maxdevs > MAX_JOYSTICKS ) maxdevs = MAX_JOYSTICKS; 140 maxdevs++;
141 if (maxdevs > MAX_JOYSTICKS)
142 maxdevs = MAX_JOYSTICKS;
140 143
141 /* Defines min/max axes values (callibration) */ 144 /* Defines min/max axes values (callibration) */
142 ulDataLen = sizeof(stGameCalib); 145 ulDataLen = sizeof(stGameCalib);
143 rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_GET_CALIB, 146 rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_GET_CALIB,
144 NULL, 0, NULL, &stGameCalib, ulDataLen, &ulDataLen); 147 NULL, 0, NULL, &stGameCalib, ulDataLen, &ulDataLen);
145 if (rc != 0) 148 if (rc != 0) {
146 { 149 joyPortClose(&hJoyPort);
147 joyPortClose(&hJoyPort); 150 SDL_SetError("Could not read callibration data.");
148 SDL_SetError("Could not read callibration data."); 151 return -1;
149 return -1; 152 }
150 }
151 153
152 /* Determine how many joysticks are active */ 154 /* Determine how many joysticks are active */
153 numdevs = 0; /* Points no device */ 155 numdevs = 0; /* Points no device */
154 ucNewJoystickMask = 0x0F; /* read all 4 joystick axis */ 156 ucNewJoystickMask = 0x0F; /* read all 4 joystick axis */
155 ulDataLen = sizeof(ucNewJoystickMask); 157 ulDataLen = sizeof(ucNewJoystickMask);
156 rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_PORT_RESET, 158 rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_PORT_RESET,
157 &ucNewJoystickMask, ulDataLen, &ulDataLen, NULL, 0, NULL); 159 &ucNewJoystickMask, ulDataLen, &ulDataLen, NULL, 0,
158 if (rc == 0) 160 NULL);
159 { 161 if (rc == 0) {
160 ulDataLen = sizeof(stJoyStatus); 162 ulDataLen = sizeof(stJoyStatus);
161 rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_PORT_GET, 163 rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_PORT_GET,
162 NULL, 0, NULL, &stJoyStatus, ulDataLen, &ulDataLen); 164 NULL, 0, NULL, &stJoyStatus, ulDataLen, &ulDataLen);
163 if (rc != 0) 165 if (rc != 0) {
164 { 166 joyPortClose(&hJoyPort);
165 joyPortClose(&hJoyPort); 167 SDL_SetError("Could not call joystick port.");
166 SDL_SetError("Could not call joystick port."); 168 return -1;
167 return -1; 169 }
168 } 170 ulLastTick = stJoyStatus.ulJs_Ticks;
169 ulLastTick = stJoyStatus.ulJs_Ticks; 171 while (stJoyStatus.ulJs_Ticks == ulLastTick) {
170 while (stJoyStatus.ulJs_Ticks == ulLastTick) 172 rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_PORT_GET,
171 { 173 NULL, 0, NULL, &stJoyStatus, ulDataLen,
172 rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_PORT_GET, 174 &ulDataLen);
173 NULL, 0, NULL, &stJoyStatus, ulDataLen, &ulDataLen); 175 }
174 } 176 if ((stJoyStatus.ucJs_JoyStickMask & 0x03) > 0)
175 if ((stJoyStatus.ucJs_JoyStickMask & 0x03) > 0) numdevs++; 177 numdevs++;
176 if (((stJoyStatus.ucJs_JoyStickMask >> 2) & 0x03) > 0) numdevs++; 178 if (((stJoyStatus.ucJs_JoyStickMask >> 2) & 0x03) > 0)
177 } 179 numdevs++;
178 180 }
179 if (numdevs>maxdevs) numdevs=maxdevs; 181
182 if (numdevs > maxdevs)
183 numdevs = maxdevs;
180 184
181 /* If *any* joystick was detected... Let's configure SDL for them */ 185 /* If *any* joystick was detected... Let's configure SDL for them */
182 if (numdevs > 0) 186 if (numdevs > 0) {
183 { 187 /* Verify if it is a "user defined" joystick */
184 /* Verify if it is a "user defined" joystick */ 188 if (joyGetEnv(&joycfg)) {
185 if (joyGetEnv(&joycfg)) 189 GAME_3POS_STRUCT *axis[4];
186 { 190 axis[0] = &stGameCalib.Ax;
187 GAME_3POS_STRUCT * axis[4]; 191 axis[1] = &stGameCalib.Ay;
188 axis[0] = &stGameCalib.Ax; 192 axis[2] = &stGameCalib.Bx;
189 axis[1] = &stGameCalib.Ay; 193 axis[3] = &stGameCalib.By;
190 axis[2] = &stGameCalib.Bx; 194 /* Say it has one device only (user defined is always one device only) */
191 axis[3] = &stGameCalib.By; 195 numdevs = 1;
192 /* Say it has one device only (user defined is always one device only) */ 196 /* Define Device 0 as... */
193 numdevs = 1; 197 SYS_JoyData[0].id = 0;
194 /* Define Device 0 as... */ 198 /* Define Number of Axes... up to 4 */
195 SYS_JoyData[0].id=0; 199 if (joycfg.axes > MAX_AXES)
196 /* Define Number of Axes... up to 4 */ 200 joycfg.axes = MAX_AXES;
197 if (joycfg.axes>MAX_AXES) joycfg.axes = MAX_AXES; 201 SYS_JoyData[0].axes = joycfg.axes;
198 SYS_JoyData[0].axes = joycfg.axes; 202 /* Define number of buttons... 8 if 2 axes, 6 if 3 axes and 4 if 4 axes */
199 /* Define number of buttons... 8 if 2 axes, 6 if 3 axes and 4 if 4 axes */ 203 maxbut = MAX_BUTTONS;
200 maxbut = MAX_BUTTONS; 204 if (joycfg.axes > 2)
201 if (joycfg.axes>2) maxbut-=((joycfg.axes-2)<<1); /* MAX_BUTTONS - 2*(axes-2) */ 205 maxbut -= ((joycfg.axes - 2) << 1); /* MAX_BUTTONS - 2*(axes-2) */
202 if (joycfg.buttons > maxbut) joycfg.buttons = maxbut; 206 if (joycfg.buttons > maxbut)
203 SYS_JoyData[0].buttons = joycfg.buttons; 207 joycfg.buttons = maxbut;
204 /* Define number of hats */ 208 SYS_JoyData[0].buttons = joycfg.buttons;
205 if (joycfg.hats > MAX_HATS) joycfg.hats = MAX_HATS; 209 /* Define number of hats */
206 SYS_JoyData[0].hats = joycfg.hats; 210 if (joycfg.hats > MAX_HATS)
207 /* Define number of balls */ 211 joycfg.hats = MAX_HATS;
208 if (joycfg.balls > MAX_BALLS) joycfg.balls = MAX_BALLS; 212 SYS_JoyData[0].hats = joycfg.hats;
209 SYS_JoyData[0].balls = joycfg.balls; 213 /* Define number of balls */
210 /* Initialize Axes Callibration Values */ 214 if (joycfg.balls > MAX_BALLS)
211 for (i=0; i<joycfg.axes; i++) 215 joycfg.balls = MAX_BALLS;
212 { 216 SYS_JoyData[0].balls = joycfg.balls;
213 SYS_JoyData[0].axes_min[i] = axis[i]->lower; 217 /* Initialize Axes Callibration Values */
214 SYS_JoyData[0].axes_med[i] = axis[i]->centre; 218 for (i = 0; i < joycfg.axes; i++) {
215 SYS_JoyData[0].axes_max[i] = axis[i]->upper; 219 SYS_JoyData[0].axes_min[i] = axis[i]->lower;
216 } 220 SYS_JoyData[0].axes_med[i] = axis[i]->centre;
217 /* Initialize Buttons 5 to 8 structures */ 221 SYS_JoyData[0].axes_max[i] = axis[i]->upper;
218 if (joycfg.buttons>=5) SYS_JoyData[0].buttoncalc[0]=((axis[2]->lower+axis[3]->centre)>>1); 222 }
219 if (joycfg.buttons>=6) SYS_JoyData[0].buttoncalc[1]=((axis[3]->lower+axis[3]->centre)>>1); 223 /* Initialize Buttons 5 to 8 structures */
220 if (joycfg.buttons>=7) SYS_JoyData[0].buttoncalc[2]=((axis[2]->upper+axis[3]->centre)>>1); 224 if (joycfg.buttons >= 5)
221 if (joycfg.buttons>=8) SYS_JoyData[0].buttoncalc[3]=((axis[3]->upper+axis[3]->centre)>>1); 225 SYS_JoyData[0].buttoncalc[0] =
222 /* Intialize Joystick Name */ 226 ((axis[2]->lower + axis[3]->centre) >> 1);
223 SDL_strlcpy (SYS_JoyData[0].szDeviceName,joycfg.name, SDL_arraysize(SYS_JoyData[0].szDeviceName)); 227 if (joycfg.buttons >= 6)
224 } 228 SYS_JoyData[0].buttoncalc[1] =
225 /* Default Init ... autoconfig */ 229 ((axis[3]->lower + axis[3]->centre) >> 1);
226 else 230 if (joycfg.buttons >= 7)
227 { 231 SYS_JoyData[0].buttoncalc[2] =
228 /* if two devices were detected... configure as Joy1 4 axis and Joy2 2 axis */ 232 ((axis[2]->upper + axis[3]->centre) >> 1);
229 if (numdevs==2) 233 if (joycfg.buttons >= 8)
230 { 234 SYS_JoyData[0].buttoncalc[3] =
231 /* Define Device 0 as 4 axes, 4 buttons */ 235 ((axis[3]->upper + axis[3]->centre) >> 1);
232 SYS_JoyData[0].id=0; 236 /* Intialize Joystick Name */
233 SYS_JoyData[0].axes = 4; 237 SDL_strlcpy(SYS_JoyData[0].szDeviceName, joycfg.name,
234 SYS_JoyData[0].buttons = 4; 238 SDL_arraysize(SYS_JoyData[0].szDeviceName));
235 SYS_JoyData[0].hats = 0; 239 }
236 SYS_JoyData[0].balls = 0; 240 /* Default Init ... autoconfig */
237 SYS_JoyData[0].axes_min[0] = stGameCalib.Ax.lower; 241 else {
238 SYS_JoyData[0].axes_med[0] = stGameCalib.Ax.centre; 242 /* if two devices were detected... configure as Joy1 4 axis and Joy2 2 axis */
239 SYS_JoyData[0].axes_max[0] = stGameCalib.Ax.upper; 243 if (numdevs == 2) {
240 SYS_JoyData[0].axes_min[1] = stGameCalib.Ay.lower; 244 /* Define Device 0 as 4 axes, 4 buttons */
241 SYS_JoyData[0].axes_med[1] = stGameCalib.Ay.centre; 245 SYS_JoyData[0].id = 0;
242 SYS_JoyData[0].axes_max[1] = stGameCalib.Ay.upper; 246 SYS_JoyData[0].axes = 4;
243 SYS_JoyData[0].axes_min[2] = stGameCalib.Bx.lower; 247 SYS_JoyData[0].buttons = 4;
244 SYS_JoyData[0].axes_med[2] = stGameCalib.Bx.centre; 248 SYS_JoyData[0].hats = 0;
245 SYS_JoyData[0].axes_max[2] = stGameCalib.Bx.upper; 249 SYS_JoyData[0].balls = 0;
246 SYS_JoyData[0].axes_min[3] = stGameCalib.By.lower; 250 SYS_JoyData[0].axes_min[0] = stGameCalib.Ax.lower;
247 SYS_JoyData[0].axes_med[3] = stGameCalib.By.centre; 251 SYS_JoyData[0].axes_med[0] = stGameCalib.Ax.centre;
248 SYS_JoyData[0].axes_max[3] = stGameCalib.By.upper; 252 SYS_JoyData[0].axes_max[0] = stGameCalib.Ax.upper;
249 /* Define Device 1 as 2 axes, 2 buttons */ 253 SYS_JoyData[0].axes_min[1] = stGameCalib.Ay.lower;
250 SYS_JoyData[1].id=1; 254 SYS_JoyData[0].axes_med[1] = stGameCalib.Ay.centre;
251 SYS_JoyData[1].axes = 2; 255 SYS_JoyData[0].axes_max[1] = stGameCalib.Ay.upper;
252 SYS_JoyData[1].buttons = 2; 256 SYS_JoyData[0].axes_min[2] = stGameCalib.Bx.lower;
253 SYS_JoyData[1].hats = 0; 257 SYS_JoyData[0].axes_med[2] = stGameCalib.Bx.centre;
254 SYS_JoyData[1].balls = 0; 258 SYS_JoyData[0].axes_max[2] = stGameCalib.Bx.upper;
255 SYS_JoyData[1].axes_min[0] = stGameCalib.Bx.lower; 259 SYS_JoyData[0].axes_min[3] = stGameCalib.By.lower;
256 SYS_JoyData[1].axes_med[0] = stGameCalib.Bx.centre; 260 SYS_JoyData[0].axes_med[3] = stGameCalib.By.centre;
257 SYS_JoyData[1].axes_max[0] = stGameCalib.Bx.upper; 261 SYS_JoyData[0].axes_max[3] = stGameCalib.By.upper;
258 SYS_JoyData[1].axes_min[1] = stGameCalib.By.lower; 262 /* Define Device 1 as 2 axes, 2 buttons */
259 SYS_JoyData[1].axes_med[1] = stGameCalib.By.centre; 263 SYS_JoyData[1].id = 1;
260 SYS_JoyData[1].axes_max[1] = stGameCalib.By.upper; 264 SYS_JoyData[1].axes = 2;
261 } 265 SYS_JoyData[1].buttons = 2;
262 /* One joystick only? */ 266 SYS_JoyData[1].hats = 0;
263 else 267 SYS_JoyData[1].balls = 0;
264 { 268 SYS_JoyData[1].axes_min[0] = stGameCalib.Bx.lower;
265 /* If it is joystick A... */ 269 SYS_JoyData[1].axes_med[0] = stGameCalib.Bx.centre;
266 if ((stJoyStatus.ucJs_JoyStickMask & 0x03) > 0) 270 SYS_JoyData[1].axes_max[0] = stGameCalib.Bx.upper;
267 { 271 SYS_JoyData[1].axes_min[1] = stGameCalib.By.lower;
268 /* Define Device 0 as 2 axes, 4 buttons */ 272 SYS_JoyData[1].axes_med[1] = stGameCalib.By.centre;
269 SYS_JoyData[0].id=0; 273 SYS_JoyData[1].axes_max[1] = stGameCalib.By.upper;
270 SYS_JoyData[0].axes = 2; 274 }
271 SYS_JoyData[0].buttons = 4; 275 /* One joystick only? */
272 SYS_JoyData[0].hats = 0; 276 else {
273 SYS_JoyData[0].balls = 0; 277 /* If it is joystick A... */
274 SYS_JoyData[0].axes_min[0] = stGameCalib.Ax.lower; 278 if ((stJoyStatus.ucJs_JoyStickMask & 0x03) > 0) {
275 SYS_JoyData[0].axes_med[0] = stGameCalib.Ax.centre; 279 /* Define Device 0 as 2 axes, 4 buttons */
276 SYS_JoyData[0].axes_max[0] = stGameCalib.Ax.upper; 280 SYS_JoyData[0].id = 0;
277 SYS_JoyData[0].axes_min[1] = stGameCalib.Ay.lower; 281 SYS_JoyData[0].axes = 2;
278 SYS_JoyData[0].axes_med[1] = stGameCalib.Ay.centre; 282 SYS_JoyData[0].buttons = 4;
279 SYS_JoyData[0].axes_max[1] = stGameCalib.Ay.upper; 283 SYS_JoyData[0].hats = 0;
280 } 284 SYS_JoyData[0].balls = 0;
281 /* If not, it is joystick B */ 285 SYS_JoyData[0].axes_min[0] = stGameCalib.Ax.lower;
282 else 286 SYS_JoyData[0].axes_med[0] = stGameCalib.Ax.centre;
283 { 287 SYS_JoyData[0].axes_max[0] = stGameCalib.Ax.upper;
284 /* Define Device 1 as 2 axes, 2 buttons */ 288 SYS_JoyData[0].axes_min[1] = stGameCalib.Ay.lower;
285 SYS_JoyData[0].id=1; 289 SYS_JoyData[0].axes_med[1] = stGameCalib.Ay.centre;
286 SYS_JoyData[0].axes = 2; 290 SYS_JoyData[0].axes_max[1] = stGameCalib.Ay.upper;
287 SYS_JoyData[0].buttons = 2;
288 SYS_JoyData[0].hats = 0;
289 SYS_JoyData[0].balls = 0;
290 SYS_JoyData[0].axes_min[0] = stGameCalib.Bx.lower;
291 SYS_JoyData[0].axes_med[0] = stGameCalib.Bx.centre;
292 SYS_JoyData[0].axes_max[0] = stGameCalib.Bx.upper;
293 SYS_JoyData[0].axes_min[1] = stGameCalib.By.lower;
294 SYS_JoyData[0].axes_med[1] = stGameCalib.By.centre;
295 SYS_JoyData[0].axes_max[1] = stGameCalib.By.upper;
296 }
297 }
298 /* Hack to define Joystick Port Names */
299 if ( numdevs > maxdevs ) numdevs = maxdevs;
300 for (i=0; i<numdevs; i++)
301 SDL_snprintf (SYS_JoyData[i].szDeviceName, SDL_arraysize(SYS_JoyData[i].szDeviceName), "Default Joystick %c", 'A'+SYS_JoyData[i].id);
302
303 } 291 }
304 } 292 /* If not, it is joystick B */
293 else {
294 /* Define Device 1 as 2 axes, 2 buttons */
295 SYS_JoyData[0].id = 1;
296 SYS_JoyData[0].axes = 2;
297 SYS_JoyData[0].buttons = 2;
298 SYS_JoyData[0].hats = 0;
299 SYS_JoyData[0].balls = 0;
300 SYS_JoyData[0].axes_min[0] = stGameCalib.Bx.lower;
301 SYS_JoyData[0].axes_med[0] = stGameCalib.Bx.centre;
302 SYS_JoyData[0].axes_max[0] = stGameCalib.Bx.upper;
303 SYS_JoyData[0].axes_min[1] = stGameCalib.By.lower;
304 SYS_JoyData[0].axes_med[1] = stGameCalib.By.centre;
305 SYS_JoyData[0].axes_max[1] = stGameCalib.By.upper;
306 }
307 }
308 /* Hack to define Joystick Port Names */
309 if (numdevs > maxdevs)
310 numdevs = maxdevs;
311 for (i = 0; i < numdevs; i++)
312 SDL_snprintf(SYS_JoyData[i].szDeviceName,
313 SDL_arraysize(SYS_JoyData[i].szDeviceName),
314 "Default Joystick %c", 'A' + SYS_JoyData[i].id);
315
316 }
317 }
305 /* Return the number of devices found */ 318 /* Return the number of devices found */
306 return(numdevs); 319 return (numdevs);
307 } 320 }
308 321
309 322
310 /***********************************************************/ 323 /***********************************************************/
311 /* Function to get the device-dependent name of a joystick */ 324 /* Function to get the device-dependent name of a joystick */
312 /***********************************************************/ 325 /***********************************************************/
313 const char *SDL_SYS_JoystickName(int index) 326 const char *
327 SDL_SYS_JoystickName(int index)
314 { 328 {
315 /* No need to verify if device exists, already done in upper layer */ 329 /* No need to verify if device exists, already done in upper layer */
316 return(SYS_JoyData[index].szDeviceName); 330 return (SYS_JoyData[index].szDeviceName);
317 } 331 }
318 332
319 333
320 334
321 /******************************************************************************/ 335 /******************************************************************************/
322 /* Function to open a joystick for use. */ 336 /* Function to open a joystick for use. */
323 /* The joystick to open is specified by the index field of the joystick. */ 337 /* The joystick to open is specified by the index field of the joystick. */
324 /* This should fill the nbuttons and naxes fields of the joystick structure. */ 338 /* This should fill the nbuttons and naxes fields of the joystick structure. */
325 /* It returns 0, or -1 if there is an error. */ 339 /* It returns 0, or -1 if there is an error. */
326 /******************************************************************************/ 340 /******************************************************************************/
327 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick) 341 int
328 { 342 SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
329 int index; /* Index shortcut for index in joystick structure */ 343 {
330 int i; /* Generic Counter */ 344 int index; /* Index shortcut for index in joystick structure */
345 int i; /* Generic Counter */
331 346
332 /* allocate memory for system specific hardware data */ 347 /* allocate memory for system specific hardware data */
333 joystick->hwdata = (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata)); 348 joystick->hwdata =
334 if (joystick->hwdata == NULL) 349 (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata));
335 { 350 if (joystick->hwdata == NULL) {
336 SDL_OutOfMemory(); 351 SDL_OutOfMemory();
337 return(-1); 352 return (-1);
338 } 353 }
339 /* Reset Hardware Data */ 354 /* Reset Hardware Data */
340 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); 355 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
341 356
342 /* ShortCut Pointer */ 357 /* ShortCut Pointer */
343 index = joystick->index; 358 index = joystick->index;
344 /* Define offsets and scales for all axes */ 359 /* Define offsets and scales for all axes */
345 joystick->hwdata->id = SYS_JoyData[index].id; 360 joystick->hwdata->id = SYS_JoyData[index].id;
346 for ( i = 0; i < MAX_AXES; ++i ) 361 for (i = 0; i < MAX_AXES; ++i) {
347 { 362 if ((i < 2) || i < SYS_JoyData[index].axes) {
348 if ( (i<2) || i < SYS_JoyData[index].axes ) 363 joystick->hwdata->transaxes[i].offset =
349 { 364 ((AXIS_MAX + AXIS_MIN) >> 1) - SYS_JoyData[index].axes_med[i];
350 joystick->hwdata->transaxes[i].offset = ((AXIS_MAX + AXIS_MIN)>>1) - SYS_JoyData[index].axes_med[i]; 365 //joystick->hwdata->transaxes[i].scale = (float)((AXIS_MAX - AXIS_MIN)/(SYS_JoyData[index].axes_max[i]-SYS_JoyData[index].axes_min[i]));
351 //joystick->hwdata->transaxes[i].scale = (float)((AXIS_MAX - AXIS_MIN)/(SYS_JoyData[index].axes_max[i]-SYS_JoyData[index].axes_min[i])); 366 joystick->hwdata->transaxes[i].scale1 =
352 joystick->hwdata->transaxes[i].scale1 = (float)abs((AXIS_MIN/SYS_JoyData[index].axes_min[i])); 367 (float) abs((AXIS_MIN / SYS_JoyData[index].axes_min[i]));
353 joystick->hwdata->transaxes[i].scale2 = (float)abs((AXIS_MAX/SYS_JoyData[index].axes_max[i])); 368 joystick->hwdata->transaxes[i].scale2 =
354 } 369 (float) abs((AXIS_MAX / SYS_JoyData[index].axes_max[i]));
355 else 370 } else {
356 { 371 joystick->hwdata->transaxes[i].offset = 0;
357 joystick->hwdata->transaxes[i].offset = 0; 372 //joystick->hwdata->transaxes[i].scale = 1.0; /* Just in case */
358 //joystick->hwdata->transaxes[i].scale = 1.0; /* Just in case */ 373 joystick->hwdata->transaxes[i].scale1 = 1.0; /* Just in case */
359 joystick->hwdata->transaxes[i].scale1 = 1.0; /* Just in case */ 374 joystick->hwdata->transaxes[i].scale2 = 1.0; /* Just in case */
360 joystick->hwdata->transaxes[i].scale2 = 1.0; /* Just in case */ 375 }
361 } 376 }
362 }
363 377
364 /* fill nbuttons, naxes, and nhats fields */ 378 /* fill nbuttons, naxes, and nhats fields */
365 joystick->nbuttons = SYS_JoyData[index].buttons; 379 joystick->nbuttons = SYS_JoyData[index].buttons;
366 joystick->naxes = SYS_JoyData[index].axes; 380 joystick->naxes = SYS_JoyData[index].axes;
367 /* joystick->nhats = SYS_JoyData[index].hats; */ 381 /* joystick->nhats = SYS_JoyData[index].hats; */
368 joystick->nhats = 0; /* No support for hats at this time */ 382 joystick->nhats = 0; /* No support for hats at this time */
369 /* joystick->nballs = SYS_JoyData[index].balls; */ 383 /* joystick->nballs = SYS_JoyData[index].balls; */
370 joystick->nballs = 0; /* No support for balls at this time */ 384 joystick->nballs = 0; /* No support for balls at this time */
371 return 0; 385 return 0;
372 } 386 }
373 387
374 388
375 389
376 /***************************************************************************/ 390 /***************************************************************************/
377 /* Function to update the state of a joystick - called as a device poll. */ 391 /* Function to update the state of a joystick - called as a device poll. */
378 /* This function shouldn't update the joystick structure directly, */ 392 /* This function shouldn't update the joystick structure directly, */
379 /* but instead should call SDL_PrivateJoystick*() to deliver events */ 393 /* but instead should call SDL_PrivateJoystick*() to deliver events */
380 /* and update joystick device state. */ 394 /* and update joystick device state. */
381 /***************************************************************************/ 395 /***************************************************************************/
382 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick) 396 void
383 { 397 SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
384 APIRET rc; /* Generic OS/2 return code */ 398 {
385 int index; /* index shortcurt to joystick index */ 399 APIRET rc; /* Generic OS/2 return code */
386 int i; /* Generic counter */ 400 int index; /* index shortcurt to joystick index */
387 int normbut; /* Number of buttons reported by joystick */ 401 int i; /* Generic counter */
388 int corr; /* Correction for button names */ 402 int normbut; /* Number of buttons reported by joystick */
389 Sint16 value, change; /* Values used to update axis values */ 403 int corr; /* Correction for button names */
390 struct _transaxes *transaxes; /* Shortcut for Correction structure */ 404 Sint16 value, change; /* Values used to update axis values */
391 Uint32 pos[MAX_AXES]; /* Vector to inform the Axis status */ 405 struct _transaxes *transaxes; /* Shortcut for Correction structure */
392 ULONG ulDataLen; /* Size of data */ 406 Uint32 pos[MAX_AXES]; /* Vector to inform the Axis status */
393 GAME_STATUS_STRUCT stGameStatus; /* Joystick Status Structure */ 407 ULONG ulDataLen; /* Size of data */
394 408 GAME_STATUS_STRUCT stGameStatus; /* Joystick Status Structure */
395 ulDataLen = sizeof(stGameStatus); 409
396 rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_GET_STATUS, 410 ulDataLen = sizeof(stGameStatus);
397 NULL, 0, NULL, &stGameStatus, ulDataLen, &ulDataLen); 411 rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_GET_STATUS,
398 if (rc != 0) 412 NULL, 0, NULL, &stGameStatus, ulDataLen, &ulDataLen);
399 { 413 if (rc != 0) {
400 SDL_SetError("Could not read joystick status."); 414 SDL_SetError("Could not read joystick status.");
401 return; /* Could not read data */ 415 return; /* Could not read data */
402 } 416 }
403 417
404 /* Shortcut pointer */ 418 /* Shortcut pointer */
405 index = joystick->index; 419 index = joystick->index;
406 /* joystick motion events */ 420 /* joystick motion events */
407 421
408 if (SYS_JoyData[index].id == 0) 422 if (SYS_JoyData[index].id == 0) {
409 { 423 pos[0] = stGameStatus.curdata.A.x;
410 pos[0] = stGameStatus.curdata.A.x; 424 pos[1] = stGameStatus.curdata.A.y;
411 pos[1] = stGameStatus.curdata.A.y; 425 if (SYS_JoyData[index].axes >= 3)
412 if (SYS_JoyData[index].axes >= 3) pos[2] = stGameStatus.curdata.B.x; 426 pos[2] = stGameStatus.curdata.B.x;
413 else pos[2]=0; 427 else
414 if (SYS_JoyData[index].axes >= 4) pos[3] = stGameStatus.curdata.B.y; 428 pos[2] = 0;
415 else pos[3]=0; 429 if (SYS_JoyData[index].axes >= 4)
416 pos[4]=0; /* OS/2 basic drivers do not support more than 4 axes joysticks */ 430 pos[3] = stGameStatus.curdata.B.y;
417 pos[5]=0; 431 else
418 } 432 pos[3] = 0;
419 else if (SYS_JoyData[index].id == 1) 433 pos[4] = 0; /* OS/2 basic drivers do not support more than 4 axes joysticks */
420 { 434 pos[5] = 0;
421 pos[0] = stGameStatus.curdata.B.x; 435 } else if (SYS_JoyData[index].id == 1) {
422 pos[1] = stGameStatus.curdata.B.y; 436 pos[0] = stGameStatus.curdata.B.x;
423 pos[2]=0; 437 pos[1] = stGameStatus.curdata.B.y;
424 pos[3]=0; 438 pos[2] = 0;
425 pos[4]=0; 439 pos[3] = 0;
426 pos[5]=0; 440 pos[4] = 0;
427 } 441 pos[5] = 0;
442 }
428 443
429 /* Corrects the movements using the callibration */ 444 /* Corrects the movements using the callibration */
430 transaxes = joystick->hwdata->transaxes; 445 transaxes = joystick->hwdata->transaxes;
431 for (i = 0; i < joystick->naxes; i++) 446 for (i = 0; i < joystick->naxes; i++) {
432 { 447 value = pos[i] + transaxes[i].offset;
433 value = pos[i] + transaxes[i].offset; 448 if (value < 0) {
434 if (value<0) 449 value *= transaxes[i].scale1;
435 { 450 if (value > 0)
436 value*=transaxes[i].scale1; 451 value = AXIS_MIN;
437 if (value>0) value = AXIS_MIN; 452 } else {
438 } 453 value *= transaxes[i].scale2;
439 else 454 if (value < 0)
440 { 455 value = AXIS_MAX;
441 value*=transaxes[i].scale2; 456 }
442 if (value<0) value = AXIS_MAX; 457 change = (value - joystick->axes[i]);
443 } 458 if ((change < -JOY_AXIS_THRESHOLD) || (change > JOY_AXIS_THRESHOLD)) {
444 change = (value - joystick->axes[i]); 459 SDL_PrivateJoystickAxis(joystick, (Uint8) i, (Sint16) value);
445 if ( (change < -JOY_AXIS_THRESHOLD) || (change > JOY_AXIS_THRESHOLD) ) 460 }
446 { 461 }
447 SDL_PrivateJoystickAxis(joystick, (Uint8)i, (Sint16)value);
448 }
449 }
450 462
451 /* joystick button A to D events */ 463 /* joystick button A to D events */
452 if (SYS_JoyData[index].id == 1) corr = 2; 464 if (SYS_JoyData[index].id == 1)
453 else corr = 0; 465 corr = 2;
454 normbut=4; /* Number of normal buttons */ 466 else
455 if (joystick->nbuttons<normbut) normbut = joystick->nbuttons; 467 corr = 0;
456 for ( i = corr; (i-corr) < normbut; ++i ) 468 normbut = 4; /* Number of normal buttons */
457 { 469 if (joystick->nbuttons < normbut)
458 /* 470 normbut = joystick->nbuttons;
459 Button A: 1110 0000 471 for (i = corr; (i - corr) < normbut; ++i) {
460 Button B: 1101 0000 472 /*
461 Button C: 1011 0000 473 Button A: 1110 0000
462 Button D: 0111 0000 474 Button B: 1101 0000
463 */ 475 Button C: 1011 0000
464 if ( (~stGameStatus.curdata.butMask)>>4 & JOY_BUTTON_FLAG(i) ) 476 Button D: 0111 0000
465 { 477 */
466 if ( ! joystick->buttons[i-corr] ) 478 if ((~stGameStatus.curdata.butMask) >> 4 & JOY_BUTTON_FLAG(i)) {
467 { 479 if (!joystick->buttons[i - corr]) {
468 SDL_PrivateJoystickButton(joystick, (Uint8)(i-corr), SDL_PRESSED); 480 SDL_PrivateJoystickButton(joystick, (Uint8) (i - corr),
469 } 481 SDL_PRESSED);
470 } 482 }
471 else 483 } else {
472 { 484 if (joystick->buttons[i - corr]) {
473 if ( joystick->buttons[i-corr] ) 485 SDL_PrivateJoystickButton(joystick, (Uint8) (i - corr),
474 { 486 SDL_RELEASED);
475 SDL_PrivateJoystickButton(joystick, (Uint8)(i-corr), SDL_RELEASED); 487 }
476 } 488 }
477 } 489 }
478 }
479 490
480 /* Joystick button E to H buttons */ 491 /* Joystick button E to H buttons */
481 /* 492 /*
482 Button E: Axis 2 X Left 493 Button E: Axis 2 X Left
483 Button F: Axis 2 Y Up 494 Button F: Axis 2 Y Up
484 Button G: Axis 2 X Right 495 Button G: Axis 2 X Right
485 Button H: Axis 2 Y Down 496 Button H: Axis 2 Y Down
486 */ 497 */
487 if (joystick->nbuttons>=5) 498 if (joystick->nbuttons >= 5) {
488 { 499 if (stGameStatus.curdata.B.x < SYS_JoyData[index].buttoncalc[0])
489 if (stGameStatus.curdata.B.x < SYS_JoyData[index].buttoncalc[0]) SDL_PrivateJoystickButton(joystick, (Uint8)4, SDL_PRESSED); 500 SDL_PrivateJoystickButton(joystick, (Uint8) 4, SDL_PRESSED);
490 else SDL_PrivateJoystickButton(joystick, (Uint8)4, SDL_RELEASED); 501 else
491 } 502 SDL_PrivateJoystickButton(joystick, (Uint8) 4, SDL_RELEASED);
492 if (joystick->nbuttons>=6) 503 }
493 { 504 if (joystick->nbuttons >= 6) {
494 if (stGameStatus.curdata.B.y < SYS_JoyData[index].buttoncalc[1]) SDL_PrivateJoystickButton(joystick, (Uint8)5, SDL_PRESSED); 505 if (stGameStatus.curdata.B.y < SYS_JoyData[index].buttoncalc[1])
495 else SDL_PrivateJoystickButton(joystick, (Uint8)5, SDL_RELEASED); 506 SDL_PrivateJoystickButton(joystick, (Uint8) 5, SDL_PRESSED);
496 } 507 else
497 if (joystick->nbuttons>=7) 508 SDL_PrivateJoystickButton(joystick, (Uint8) 5, SDL_RELEASED);
498 { 509 }
499 if (stGameStatus.curdata.B.x > SYS_JoyData[index].buttoncalc[2]) SDL_PrivateJoystickButton(joystick, (Uint8)6, SDL_PRESSED); 510 if (joystick->nbuttons >= 7) {
500 else SDL_PrivateJoystickButton(joystick, (Uint8)6, SDL_RELEASED); 511 if (stGameStatus.curdata.B.x > SYS_JoyData[index].buttoncalc[2])
501 } 512 SDL_PrivateJoystickButton(joystick, (Uint8) 6, SDL_PRESSED);
502 if (joystick->nbuttons>=8) 513 else
503 { 514 SDL_PrivateJoystickButton(joystick, (Uint8) 6, SDL_RELEASED);
504 if (stGameStatus.curdata.B.y > SYS_JoyData[index].buttoncalc[3]) SDL_PrivateJoystickButton(joystick, (Uint8)7, SDL_PRESSED); 515 }
505 else SDL_PrivateJoystickButton(joystick, (Uint8)7, SDL_RELEASED); 516 if (joystick->nbuttons >= 8) {
506 } 517 if (stGameStatus.curdata.B.y > SYS_JoyData[index].buttoncalc[3])
518 SDL_PrivateJoystickButton(joystick, (Uint8) 7, SDL_PRESSED);
519 else
520 SDL_PrivateJoystickButton(joystick, (Uint8) 7, SDL_RELEASED);
521 }
507 522
508 /* joystick hat events */ 523 /* joystick hat events */
509 /* Not Supported under OS/2 */ 524 /* Not Supported under OS/2 */
510 /* joystick ball events */ 525 /* joystick ball events */
511 /* Not Supported under OS/2 */ 526 /* Not Supported under OS/2 */
514 529
515 530
516 /******************************************/ 531 /******************************************/
517 /* Function to close a joystick after use */ 532 /* Function to close a joystick after use */
518 /******************************************/ 533 /******************************************/
519 void SDL_SYS_JoystickClose(SDL_Joystick *joystick) 534 void
520 { 535 SDL_SYS_JoystickClose(SDL_Joystick * joystick)
521 if (joystick->hwdata != NULL) 536 {
522 { 537 if (joystick->hwdata != NULL) {
523 /* free system specific hardware data */ 538 /* free system specific hardware data */
524 SDL_free(joystick->hwdata); 539 SDL_free(joystick->hwdata);
525 } 540 }
526 } 541 }
527 542
528 543
529 544
530 /********************************************************************/ 545 /********************************************************************/
531 /* Function to perform any system-specific joystick related cleanup */ 546 /* Function to perform any system-specific joystick related cleanup */
532 /********************************************************************/ 547 /********************************************************************/
533 void SDL_SYS_JoystickQuit(void) 548 void
534 { 549 SDL_SYS_JoystickQuit(void)
535 joyPortClose(&hJoyPort); 550 {
551 joyPortClose(&hJoyPort);
536 } 552 }
537 553
538 554
539 555
540 /************************/ 556 /************************/
545 561
546 562
547 /*****************************************/ 563 /*****************************************/
548 /* Open Joystick Port, if not opened yet */ 564 /* Open Joystick Port, if not opened yet */
549 /*****************************************/ 565 /*****************************************/
550 APIRET joyPortOpen(HFILE * hGame) 566 APIRET
551 { 567 joyPortOpen(HFILE * hGame)
552 APIRET rc; /* Generic Return Code */ 568 {
553 ULONG ulAction; /* ? */ 569 APIRET rc; /* Generic Return Code */
554 ULONG ulVersion; /* Version of joystick driver */ 570 ULONG ulAction; /* ? */
555 ULONG ulDataLen; /* Size of version data */ 571 ULONG ulVersion; /* Version of joystick driver */
572 ULONG ulDataLen; /* Size of version data */
556 573
557 /* Verifies if joyport is not already open... */ 574 /* Verifies if joyport is not already open... */
558 if (*hGame != NULL) return 0; 575 if (*hGame != NULL)
576 return 0;
559 /* Open GAME$ for read */ 577 /* Open GAME$ for read */
560 rc = DosOpen((PSZ)GAMEPDDNAME, hGame, &ulAction, 0, FILE_READONLY, 578 rc = DosOpen((PSZ) GAMEPDDNAME, hGame, &ulAction, 0, FILE_READONLY,
561 FILE_OPEN, OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, NULL); 579 FILE_OPEN, OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, NULL);
562 if (rc != 0) 580 if (rc != 0) {
563 { 581 SDL_SetError("Could not open Joystick Port.");
564 SDL_SetError("Could not open Joystick Port."); 582 return -1;
565 return -1; 583 }
566 } 584
567
568 /* Get Joystick Driver Version... must be 2.0 or higher */ 585 /* Get Joystick Driver Version... must be 2.0 or higher */
569 ulVersion = 0; 586 ulVersion = 0;
570 ulDataLen = sizeof(ulVersion); 587 ulDataLen = sizeof(ulVersion);
571 rc = DosDevIOCtl( *hGame, IOCTL_CAT_USER, GAME_GET_VERSION, 588 rc = DosDevIOCtl(*hGame, IOCTL_CAT_USER, GAME_GET_VERSION,
572 NULL, 0, NULL, &ulVersion, ulDataLen, &ulDataLen); 589 NULL, 0, NULL, &ulVersion, ulDataLen, &ulDataLen);
573 if (rc != 0) 590 if (rc != 0) {
574 { 591 joyPortClose(hGame);
575 joyPortClose(hGame); 592 SDL_SetError("Could not get Joystick Driver version.");
576 SDL_SetError("Could not get Joystick Driver version."); 593 return -1;
577 return -1; 594 }
578 } 595 if (ulVersion < GAME_VERSION) {
579 if (ulVersion < GAME_VERSION) 596 joyPortClose(hGame);
580 { 597 SDL_SetError
581 joyPortClose(hGame); 598 ("Driver too old. At least IBM driver version 2.0 required.");
582 SDL_SetError("Driver too old. At least IBM driver version 2.0 required."); 599 return -1;
583 return -1; 600 }
584 } 601 return 0;
585 return 0;
586 } 602 }
587 603
588 604
589 605
590 /****************************/ 606 /****************************/
591 /* Close JoyPort, if opened */ 607 /* Close JoyPort, if opened */
592 /****************************/ 608 /****************************/
593 void joyPortClose(HFILE * hGame) 609 void
594 { 610 joyPortClose(HFILE * hGame)
595 if (*hGame != NULL) DosClose(*hGame); 611 {
596 *hGame = NULL; 612 if (*hGame != NULL)
613 DosClose(*hGame);
614 *hGame = NULL;
597 } 615 }
598 616
599 617
600 618
601 /***************************/ 619 /***************************/
602 /* Get SDL Joystick EnvVar */ 620 /* Get SDL Joystick EnvVar */
603 /***************************/ 621 /***************************/
604 int joyGetEnv(struct _joycfg * joydata) 622 int
605 { 623 joyGetEnv(struct _joycfg *joydata)
606 char *joyenv; /* Pointer to tested character */ 624 {
607 char tempnumber[5]; /* Temporary place to put numeric texts */ 625 char *joyenv; /* Pointer to tested character */
608 626 char tempnumber[5]; /* Temporary place to put numeric texts */
609 joyenv = SDL_getenv("SDL_OS2_JOYSTICK"); 627
610 if (joyenv == NULL) return 0; 628 joyenv = SDL_getenv("SDL_OS2_JOYSTICK");
629 if (joyenv == NULL)
630 return 0;
611 /* Joystick Environment is defined! */ 631 /* Joystick Environment is defined! */
612 while (*joyenv==' ' && *joyenv!=0) joyenv++; /* jump spaces... */ 632 while (*joyenv == ' ' && *joyenv != 0)
633 joyenv++; /* jump spaces... */
613 /* If the string name starts with '... get if fully */ 634 /* If the string name starts with '... get if fully */
614 if (*joyenv=='\'') joyenv+=joyGetData(++joyenv,joydata->name,'\'',sizeof(joydata->name)); 635 if (*joyenv == '\'')
636 joyenv +=
637 joyGetData(++joyenv, joydata->name, '\'', sizeof(joydata->name));
615 /* If not, get it until the next space */ 638 /* If not, get it until the next space */
616 else if (*joyenv=='\"') joyenv+=joyGetData(++joyenv,joydata->name,'\"',sizeof(joydata->name)); 639 else if (*joyenv == '\"')
617 else joyenv+=joyGetData(joyenv,joydata->name,' ',sizeof(joydata->name)); 640 joyenv +=
641 joyGetData(++joyenv, joydata->name, '\"', sizeof(joydata->name));
642 else
643 joyenv +=
644 joyGetData(joyenv, joydata->name, ' ', sizeof(joydata->name));
618 /* Now get the number of axes */ 645 /* Now get the number of axes */
619 while (*joyenv==' ' && *joyenv!=0) joyenv++; /* jump spaces... */ 646 while (*joyenv == ' ' && *joyenv != 0)
620 joyenv+=joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber)); 647 joyenv++; /* jump spaces... */
621 joydata->axes = atoi(tempnumber); 648 joyenv += joyGetData(joyenv, tempnumber, ' ', sizeof(tempnumber));
649 joydata->axes = atoi(tempnumber);
622 /* Now get the number of buttons */ 650 /* Now get the number of buttons */
623 while (*joyenv==' ' && *joyenv!=0) joyenv++; /* jump spaces... */ 651 while (*joyenv == ' ' && *joyenv != 0)
624 joyenv+=joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber)); 652 joyenv++; /* jump spaces... */
625 joydata->buttons = atoi(tempnumber); 653 joyenv += joyGetData(joyenv, tempnumber, ' ', sizeof(tempnumber));
654 joydata->buttons = atoi(tempnumber);
626 /* Now get the number of hats */ 655 /* Now get the number of hats */
627 while (*joyenv==' ' && *joyenv!=0) joyenv++; /* jump spaces... */ 656 while (*joyenv == ' ' && *joyenv != 0)
628 joyenv+=joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber)); 657 joyenv++; /* jump spaces... */
629 joydata->hats = atoi(tempnumber); 658 joyenv += joyGetData(joyenv, tempnumber, ' ', sizeof(tempnumber));
659 joydata->hats = atoi(tempnumber);
630 /* Now get the number of balls */ 660 /* Now get the number of balls */
631 while (*joyenv==' ' && *joyenv!=0) joyenv++; /* jump spaces... */ 661 while (*joyenv == ' ' && *joyenv != 0)
632 joyenv+=joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber)); 662 joyenv++; /* jump spaces... */
633 joydata->balls = atoi(tempnumber); 663 joyenv += joyGetData(joyenv, tempnumber, ' ', sizeof(tempnumber));
634 return 1; 664 joydata->balls = atoi(tempnumber);
665 return 1;
635 } 666 }
636 667
637 668
638 669
639 /************************************************************************/ 670 /************************************************************************/
640 /* Get a text from in the string starting in joyenv until it finds */ 671 /* Get a text from in the string starting in joyenv until it finds */
641 /* the stopchar or maxchars is reached. The result is placed in name. */ 672 /* the stopchar or maxchars is reached. The result is placed in name. */
642 /************************************************************************/ 673 /************************************************************************/
643 int joyGetData(char *joyenv, char *name, char stopchar, size_t maxchars) 674 int
644 { 675 joyGetData(char *joyenv, char *name, char stopchar, size_t maxchars)
645 char *nameptr; /* Pointer to the selected character */ 676 {
646 int chcnt=0; /* Count how many characters where copied */ 677 char *nameptr; /* Pointer to the selected character */
647 678 int chcnt = 0; /* Count how many characters where copied */
648 nameptr=name; 679
649 while (*joyenv!=stopchar && *joyenv!=0) 680 nameptr = name;
650 { 681 while (*joyenv != stopchar && *joyenv != 0) {
651 if (nameptr<(name+(maxchars-1))) 682 if (nameptr < (name + (maxchars - 1))) {
652 { 683 *nameptr = *joyenv; /* Only copy if smaller than maximum */
653 *nameptr = *joyenv; /* Only copy if smaller than maximum */ 684 nameptr++;
654 nameptr++; 685 }
655 } 686 chcnt++;
656 chcnt++; 687 joyenv++;
657 joyenv++; 688 }
658 } 689 if (*joyenv == stopchar) {
659 if (*joyenv==stopchar) 690 joyenv++; /* Jump stopchar */
660 { 691 chcnt++;
661 joyenv++; /* Jump stopchar */ 692 }
662 chcnt++; 693 *nameptr = 0; /* Mark last byte */
663 } 694 return chcnt;
664 *nameptr = 0; /* Mark last byte */
665 return chcnt;
666 } 695 }
667 696
668 #endif /* SDL_JOYSTICK_OS2 */ 697 #endif /* SDL_JOYSTICK_OS2 */
698 /* vi: set ts=4 sw=4 expandtab: */