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