Mercurial > sdl-ios-xcode
annotate src/joystick/os2/SDL_sysjoystick.c @ 1312:c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 01 Feb 2006 06:32:25 +0000 |
parents | 173c063d4f55 |
children | 3692456e7b0f |
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 <stdlib.h> | |
26 #include <stdio.h> | |
27 #include <mem.h> | |
28 | |
29 #define INCL_DOSDEVICES | |
30 #define INCL_DOSDEVIOCTL | |
31 #define INCL_DOSMEMMGR | |
32 #include <os2.h> | |
33 #include "joyos2.h" | |
34 | |
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 */ | |
220 strcpy (SYS_JoyData[0].szDeviceName,joycfg.name); | |
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; | |
297 for (i=0; i<numdevs; i++) sprintf (SYS_JoyData[i].szDeviceName,"Default Joystick %c",'A'+SYS_JoyData[i].id); | |
298 } | |
299 } | |
300 /* Return the number of devices found */ | |
301 return(numdevs); | |
302 } | |
303 | |
304 | |
305 | |
306 /***********************************************************/ | |
307 /* Function to get the device-dependent name of a joystick */ | |
308 /***********************************************************/ | |
309 const char *SDL_SYS_JoystickName(int index) | |
310 { | |
311 /* No need to verify if device exists, already done in upper layer */ | |
312 return(SYS_JoyData[index].szDeviceName); | |
313 } | |
314 | |
315 | |
316 | |
317 /******************************************************************************/ | |
318 /* Function to open a joystick for use. */ | |
319 /* The joystick to open is specified by the index field of the joystick. */ | |
320 /* This should fill the nbuttons and naxes fields of the joystick structure. */ | |
321 /* It returns 0, or -1 if there is an error. */ | |
322 /******************************************************************************/ | |
323 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick) | |
324 { | |
325 int index; /* Index shortcut for index in joystick structure */ | |
326 int i; /* Generic Counter */ | |
327 | |
328 /* allocate memory for system specific hardware data */ | |
329 joystick->hwdata = (struct joystick_hwdata *) malloc(sizeof(*joystick->hwdata)); | |
330 if (joystick->hwdata == NULL) | |
331 { | |
332 SDL_OutOfMemory(); | |
333 return(-1); | |
334 } | |
335 /* Reset Hardware Data */ | |
336 memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); | |
337 | |
338 /* ShortCut Pointer */ | |
339 index = joystick->index; | |
340 /* Define offsets and scales for all axes */ | |
341 joystick->hwdata->id = SYS_JoyData[index].id; | |
342 for ( i = 0; i < MAX_AXES; ++i ) | |
343 { | |
344 if ( (i<2) || i < SYS_JoyData[index].axes ) | |
345 { | |
346 joystick->hwdata->transaxes[i].offset = ((AXIS_MAX + AXIS_MIN)>>1) - SYS_JoyData[index].axes_med[i]; | |
347 //joystick->hwdata->transaxes[i].scale = (float)((AXIS_MAX - AXIS_MIN)/(SYS_JoyData[index].axes_max[i]-SYS_JoyData[index].axes_min[i])); | |
348 joystick->hwdata->transaxes[i].scale1 = (float)abs((AXIS_MIN/SYS_JoyData[index].axes_min[i])); | |
349 joystick->hwdata->transaxes[i].scale2 = (float)abs((AXIS_MAX/SYS_JoyData[index].axes_max[i])); | |
350 } | |
351 else | |
352 { | |
353 joystick->hwdata->transaxes[i].offset = 0; | |
354 //joystick->hwdata->transaxes[i].scale = 1.0; /* Just in case */ | |
355 joystick->hwdata->transaxes[i].scale1 = 1.0; /* Just in case */ | |
356 joystick->hwdata->transaxes[i].scale2 = 1.0; /* Just in case */ | |
357 } | |
358 } | |
359 | |
360 /* fill nbuttons, naxes, and nhats fields */ | |
361 joystick->nbuttons = SYS_JoyData[index].buttons; | |
362 joystick->naxes = SYS_JoyData[index].axes; | |
363 /* joystick->nhats = SYS_JoyData[index].hats; */ | |
364 joystick->nhats = 0; /* No support for hats at this time */ | |
365 /* joystick->nballs = SYS_JoyData[index].balls; */ | |
366 joystick->nballs = 0; /* No support for balls at this time */ | |
367 return 0; | |
368 } | |
369 | |
370 | |
371 | |
372 /***************************************************************************/ | |
373 /* Function to update the state of a joystick - called as a device poll. */ | |
374 /* This function shouldn't update the joystick structure directly, */ | |
375 /* but instead should call SDL_PrivateJoystick*() to deliver events */ | |
376 /* and update joystick device state. */ | |
377 /***************************************************************************/ | |
378 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick) | |
379 { | |
380 APIRET rc; /* Generic OS/2 return code */ | |
381 int index; /* index shortcurt to joystick index */ | |
382 int i; /* Generic counter */ | |
383 int normbut; /* Number of buttons reported by joystick */ | |
384 int corr; /* Correction for button names */ | |
385 Sint16 value, change; /* Values used to update axis values */ | |
386 struct _transaxes *transaxes; /* Shortcut for Correction structure */ | |
387 Uint32 pos[MAX_AXES]; /* Vector to inform the Axis status */ | |
388 ULONG ulDataLen; /* Size of data */ | |
389 GAME_STATUS_STRUCT stGameStatus; /* Joystick Status Structure */ | |
390 | |
391 ulDataLen = sizeof(stGameStatus); | |
392 rc = DosDevIOCtl(hJoyPort, IOCTL_CAT_USER, GAME_GET_STATUS, | |
393 NULL, 0, NULL, &stGameStatus, ulDataLen, &ulDataLen); | |
394 if (rc != 0) | |
395 { | |
396 SDL_SetError("Could not read joystick status."); | |
397 return; /* Could not read data */ | |
398 } | |
399 | |
400 /* Shortcut pointer */ | |
401 index = joystick->index; | |
402 /* joystick motion events */ | |
403 | |
404 if (SYS_JoyData[index].id == 0) | |
405 { | |
406 pos[0] = stGameStatus.curdata.A.x; | |
407 pos[1] = stGameStatus.curdata.A.y; | |
408 if (SYS_JoyData[index].axes >= 3) pos[2] = stGameStatus.curdata.B.x; | |
409 else pos[2]=0; | |
410 if (SYS_JoyData[index].axes >= 4) pos[3] = stGameStatus.curdata.B.y; | |
411 else pos[3]=0; | |
412 pos[4]=0; /* OS/2 basic drivers do not support more than 4 axes joysticks */ | |
413 pos[5]=0; | |
414 } | |
415 else if (SYS_JoyData[index].id == 1) | |
416 { | |
417 pos[0] = stGameStatus.curdata.B.x; | |
418 pos[1] = stGameStatus.curdata.B.y; | |
419 pos[2]=0; | |
420 pos[3]=0; | |
421 pos[4]=0; | |
422 pos[5]=0; | |
423 } | |
424 | |
425 /* Corrects the movements using the callibration */ | |
426 transaxes = joystick->hwdata->transaxes; | |
427 for (i = 0; i < joystick->naxes; i++) | |
428 { | |
429 value = pos[i] + transaxes[i].offset; | |
430 if (value<0) | |
431 { | |
432 value*=transaxes[i].scale1; | |
433 if (value>0) value = AXIS_MIN; | |
434 } | |
435 else | |
436 { | |
437 value*=transaxes[i].scale2; | |
438 if (value<0) value = AXIS_MAX; | |
439 } | |
440 change = (value - joystick->axes[i]); | |
441 if ( (change < -JOY_AXIS_THRESHOLD) || (change > JOY_AXIS_THRESHOLD) ) | |
442 { | |
443 SDL_PrivateJoystickAxis(joystick, (Uint8)i, (Sint16)value); | |
444 } | |
445 } | |
446 | |
447 /* joystick button A to D events */ | |
448 if (SYS_JoyData[index].id == 1) corr = 2; | |
449 else corr = 0; | |
450 normbut=4; /* Number of normal buttons */ | |
451 if (joystick->nbuttons<normbut) normbut = joystick->nbuttons; | |
452 for ( i = corr; (i-corr) < normbut; ++i ) | |
453 { | |
454 /* | |
455 Button A: 1110 0000 | |
456 Button B: 1101 0000 | |
457 Button C: 1011 0000 | |
458 Button D: 0111 0000 | |
459 */ | |
460 if ( (~stGameStatus.curdata.butMask)>>4 & JOY_BUTTON_FLAG(i) ) | |
461 { | |
462 if ( ! joystick->buttons[i-corr] ) | |
463 { | |
464 SDL_PrivateJoystickButton(joystick, (Uint8)(i-corr), SDL_PRESSED); | |
465 } | |
466 } | |
467 else | |
468 { | |
469 if ( joystick->buttons[i-corr] ) | |
470 { | |
471 SDL_PrivateJoystickButton(joystick, (Uint8)(i-corr), SDL_RELEASED); | |
472 } | |
473 } | |
474 } | |
475 | |
476 /* Joystick button E to H buttons */ | |
477 /* | |
478 Button E: Axis 2 X Left | |
479 Button F: Axis 2 Y Up | |
480 Button G: Axis 2 X Right | |
481 Button H: Axis 2 Y Down | |
482 */ | |
483 if (joystick->nbuttons>=5) | |
484 { | |
485 if (stGameStatus.curdata.B.x < SYS_JoyData[index].buttoncalc[0]) SDL_PrivateJoystickButton(joystick, (Uint8)4, SDL_PRESSED); | |
486 else SDL_PrivateJoystickButton(joystick, (Uint8)4, SDL_RELEASED); | |
487 } | |
488 if (joystick->nbuttons>=6) | |
489 { | |
490 if (stGameStatus.curdata.B.y < SYS_JoyData[index].buttoncalc[1]) SDL_PrivateJoystickButton(joystick, (Uint8)5, SDL_PRESSED); | |
491 else SDL_PrivateJoystickButton(joystick, (Uint8)5, SDL_RELEASED); | |
492 } | |
493 if (joystick->nbuttons>=7) | |
494 { | |
495 if (stGameStatus.curdata.B.x > SYS_JoyData[index].buttoncalc[2]) SDL_PrivateJoystickButton(joystick, (Uint8)6, SDL_PRESSED); | |
496 else SDL_PrivateJoystickButton(joystick, (Uint8)6, SDL_RELEASED); | |
497 } | |
498 if (joystick->nbuttons>=8) | |
499 { | |
500 if (stGameStatus.curdata.B.y > SYS_JoyData[index].buttoncalc[3]) SDL_PrivateJoystickButton(joystick, (Uint8)7, SDL_PRESSED); | |
501 else SDL_PrivateJoystickButton(joystick, (Uint8)7, SDL_RELEASED); | |
502 } | |
503 | |
504 /* joystick hat events */ | |
505 /* Not Supported under OS/2 */ | |
506 /* joystick ball events */ | |
507 /* Not Supported under OS/2 */ | |
508 } | |
509 | |
510 | |
511 | |
512 /******************************************/ | |
513 /* Function to close a joystick after use */ | |
514 /******************************************/ | |
515 void SDL_SYS_JoystickClose(SDL_Joystick *joystick) | |
516 { | |
517 if (joystick->hwdata != NULL) | |
518 { | |
519 /* free system specific hardware data */ | |
520 free(joystick->hwdata); | |
521 } | |
522 } | |
523 | |
524 | |
525 | |
526 /********************************************************************/ | |
527 /* Function to perform any system-specific joystick related cleanup */ | |
528 /********************************************************************/ | |
529 void SDL_SYS_JoystickQuit(void) | |
530 { | |
531 joyPortClose(&hJoyPort); | |
532 } | |
533 | |
534 | |
535 | |
536 /************************/ | |
537 /************************/ | |
538 /* OS/2 Implementations */ | |
539 /************************/ | |
540 /************************/ | |
541 | |
542 | |
543 /*****************************************/ | |
544 /* Open Joystick Port, if not opened yet */ | |
545 /*****************************************/ | |
546 APIRET joyPortOpen(HFILE * hGame) | |
547 { | |
548 APIRET rc; /* Generic Return Code */ | |
549 ULONG ulAction; /* ? */ | |
550 ULONG ulVersion; /* Version of joystick driver */ | |
551 ULONG ulDataLen; /* Size of version data */ | |
552 | |
553 /* Verifies if joyport is not already open... */ | |
554 if (*hGame != NULL) return 0; | |
555 /* Open GAME$ for read */ | |
556 rc = DosOpen((PSZ)GAMEPDDNAME, hGame, &ulAction, 0, FILE_READONLY, | |
557 FILE_OPEN, OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, NULL); | |
558 if (rc != 0) | |
559 { | |
560 SDL_SetError("Could not open Joystick Port."); | |
561 return -1; | |
562 } | |
563 | |
564 /* Get Joystick Driver Version... must be 2.0 or higher */ | |
565 ulVersion = 0; | |
566 ulDataLen = sizeof(ulVersion); | |
567 rc = DosDevIOCtl( *hGame, IOCTL_CAT_USER, GAME_GET_VERSION, | |
568 NULL, 0, NULL, &ulVersion, ulDataLen, &ulDataLen); | |
569 if (rc != 0) | |
570 { | |
571 joyPortClose(hGame); | |
572 SDL_SetError("Could not get Joystick Driver version."); | |
573 return -1; | |
574 } | |
575 if (ulVersion < GAME_VERSION) | |
576 { | |
577 joyPortClose(hGame); | |
578 SDL_SetError("Driver too old. At least IBM driver version 2.0 required."); | |
579 return -1; | |
580 } | |
581 return 0; | |
582 } | |
583 | |
584 | |
585 | |
586 /****************************/ | |
587 /* Close JoyPort, if opened */ | |
588 /****************************/ | |
589 void joyPortClose(HFILE * hGame) | |
590 { | |
591 if (*hGame != NULL) DosClose(*hGame); | |
592 *hGame = NULL; | |
593 } | |
594 | |
595 | |
596 | |
597 /***************************/ | |
598 /* Get SDL Joystick EnvVar */ | |
599 /***************************/ | |
600 int joyGetEnv(struct _joycfg * joydata) | |
601 { | |
602 char *joyenv; /* Pointer to tested character */ | |
603 char tempnumber[5]; /* Temporary place to put numeric texts */ | |
604 | |
605 joyenv = getenv("SDL_OS2_JOYSTICK"); | |
606 if (joyenv == NULL) return 0; | |
607 /* Joystick Environment is defined! */ | |
608 while (*joyenv==' ' && *joyenv!=0) joyenv++; /* jump spaces... */ | |
609 /* If the string name starts with '... get if fully */ | |
610 if (*joyenv=='\'') joyenv+=joyGetData(++joyenv,joydata->name,'\'',sizeof(joydata->name)); | |
611 /* If not, get it until the next space */ | |
612 else if (*joyenv=='\"') joyenv+=joyGetData(++joyenv,joydata->name,'\"',sizeof(joydata->name)); | |
613 else joyenv+=joyGetData(joyenv,joydata->name,' ',sizeof(joydata->name)); | |
614 /* Now get the number of axes */ | |
615 while (*joyenv==' ' && *joyenv!=0) joyenv++; /* jump spaces... */ | |
616 joyenv+=joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber)); | |
617 joydata->axes = atoi(tempnumber); | |
618 /* Now get the number of buttons */ | |
619 while (*joyenv==' ' && *joyenv!=0) joyenv++; /* jump spaces... */ | |
620 joyenv+=joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber)); | |
621 joydata->buttons = atoi(tempnumber); | |
622 /* Now get the number of hats */ | |
623 while (*joyenv==' ' && *joyenv!=0) joyenv++; /* jump spaces... */ | |
624 joyenv+=joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber)); | |
625 joydata->hats = atoi(tempnumber); | |
626 /* Now get the number of balls */ | |
627 while (*joyenv==' ' && *joyenv!=0) joyenv++; /* jump spaces... */ | |
628 joyenv+=joyGetData(joyenv,tempnumber,' ',sizeof(tempnumber)); | |
629 joydata->balls = atoi(tempnumber); | |
630 return 1; | |
631 } | |
632 | |
633 | |
634 | |
635 /************************************************************************/ | |
636 /* Get a text from in the string starting in joyenv until it finds */ | |
637 /* the stopchar or maxchars is reached. The result is placed in name. */ | |
638 /************************************************************************/ | |
639 int joyGetData(char *joyenv, char *name, char stopchar, size_t maxchars) | |
640 { | |
641 char *nameptr; /* Pointer to the selected character */ | |
642 int chcnt=0; /* Count how many characters where copied */ | |
643 | |
644 nameptr=name; | |
645 while (*joyenv!=stopchar && *joyenv!=0) | |
646 { | |
647 if (nameptr<(name+(maxchars-1))) | |
648 { | |
649 *nameptr = *joyenv; /* Only copy if smaller than maximum */ | |
650 nameptr++; | |
651 } | |
652 chcnt++; | |
653 joyenv++; | |
654 } | |
655 if (*joyenv==stopchar) | |
656 { | |
657 joyenv++; /* Jump stopchar */ | |
658 chcnt++; | |
659 } | |
660 *nameptr = 0; /* Mark last byte */ | |
661 return chcnt; | |
662 } | |
663 | |
664 |