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