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