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