comparison src/joystick/os2/SDL_sysjoystick.c @ 1190:173c063d4f55

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