Mercurial > sdl-ios-xcode
annotate src/joystick/SDL_joystick.c @ 1361:19418e4422cb
New configure-based build system. Still work in progress, but much improved
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 16 Feb 2006 10:11:48 +0000 |
parents | c71e05b4dc2e |
children | c0a74f199ecf |
rev | line source |
---|---|
0 | 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:
769
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 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:
769
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 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:
769
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 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:
769
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
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:
769
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:
769
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 /* This is the joystick API for Simple DirectMedia Layer */ | |
24 | |
25 #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
|
26 #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
|
27 #include "SDL_joystick_c.h" |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
28 #if !SDL_EVENTS_DISABLED |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
29 #include "../events/SDL_events_c.h" |
0 | 30 #endif |
31 | |
32 /* This is used for Quake III Arena */ | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
33 #if SDL_EVENTS_DISABLED |
0 | 34 #define SDL_Lock_EventThread() |
35 #define SDL_Unlock_EventThread() | |
36 #endif | |
37 | |
38 Uint8 SDL_numjoysticks = 0; | |
39 SDL_Joystick **SDL_joysticks = NULL; | |
40 static SDL_Joystick *default_joystick = NULL; | |
41 | |
42 int SDL_JoystickInit(void) | |
43 { | |
44 int arraylen; | |
45 int status; | |
46 | |
47 SDL_numjoysticks = 0; | |
48 status = SDL_SYS_JoystickInit(); | |
49 if ( status >= 0 ) { | |
50 arraylen = (status+1)*sizeof(*SDL_joysticks); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
51 SDL_joysticks = (SDL_Joystick **)SDL_malloc(arraylen); |
0 | 52 if ( SDL_joysticks == NULL ) { |
53 SDL_numjoysticks = 0; | |
54 } else { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
55 SDL_memset(SDL_joysticks, 0, arraylen); |
716
f25e3334d583
SDL_JoystickInit: If malloc() fails, pretend no joysticks were detected.
Ryan C. Gordon <icculus@icculus.org>
parents:
297
diff
changeset
|
56 SDL_numjoysticks = status; |
0 | 57 } |
58 status = 0; | |
59 } | |
60 default_joystick = NULL; | |
61 return(status); | |
62 } | |
63 | |
64 /* | |
65 * Count the number of joysticks attached to the system | |
66 */ | |
67 int SDL_NumJoysticks(void) | |
68 { | |
69 return SDL_numjoysticks; | |
70 } | |
71 | |
72 /* | |
73 * Get the implementation dependent name of a joystick | |
74 */ | |
75 const char *SDL_JoystickName(int device_index) | |
76 { | |
77 if ( (device_index < 0) || (device_index >= SDL_numjoysticks) ) { | |
78 SDL_SetError("There are %d joysticks available", | |
79 SDL_numjoysticks); | |
80 return(NULL); | |
81 } | |
82 return(SDL_SYS_JoystickName(device_index)); | |
83 } | |
84 | |
85 /* | |
86 * Open a joystick for use - the index passed as an argument refers to | |
87 * the N'th joystick on the system. This index is the value which will | |
88 * identify this joystick in future joystick events. | |
89 * | |
90 * This function returns a joystick identifier, or NULL if an error occurred. | |
91 */ | |
92 SDL_Joystick *SDL_JoystickOpen(int device_index) | |
93 { | |
94 int i; | |
95 SDL_Joystick *joystick; | |
96 | |
97 if ( (device_index < 0) || (device_index >= SDL_numjoysticks) ) { | |
98 SDL_SetError("There are %d joysticks available", | |
99 SDL_numjoysticks); | |
100 return(NULL); | |
101 } | |
102 | |
103 /* If the joystick is already open, return it */ | |
104 for ( i=0; SDL_joysticks[i]; ++i ) { | |
105 if ( device_index == SDL_joysticks[i]->index ) { | |
106 joystick = SDL_joysticks[i]; | |
107 ++joystick->ref_count; | |
108 return(joystick); | |
109 } | |
110 } | |
111 | |
112 /* Create and initialize the joystick */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
113 joystick = (SDL_Joystick *)SDL_malloc((sizeof *joystick)); |
0 | 114 if ( joystick != NULL ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
115 SDL_memset(joystick, 0, (sizeof *joystick)); |
0 | 116 joystick->index = device_index; |
117 if ( SDL_SYS_JoystickOpen(joystick) < 0 ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
118 SDL_free(joystick); |
0 | 119 joystick = NULL; |
120 } else { | |
121 if ( joystick->naxes > 0 ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
122 joystick->axes = (Sint16 *)SDL_malloc |
0 | 123 (joystick->naxes*sizeof(Sint16)); |
124 } | |
125 if ( joystick->nhats > 0 ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
126 joystick->hats = (Uint8 *)SDL_malloc |
0 | 127 (joystick->nhats*sizeof(Uint8)); |
128 } | |
129 if ( joystick->nballs > 0 ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
130 joystick->balls = (struct balldelta *)SDL_malloc |
0 | 131 (joystick->nballs*sizeof(*joystick->balls)); |
132 } | |
133 if ( joystick->nbuttons > 0 ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
134 joystick->buttons = (Uint8 *)SDL_malloc |
0 | 135 (joystick->nbuttons*sizeof(Uint8)); |
136 } | |
137 if ( ((joystick->naxes > 0) && !joystick->axes) | |
138 || ((joystick->nhats > 0) && !joystick->hats) | |
139 || ((joystick->nballs > 0) && !joystick->balls) | |
140 || ((joystick->nbuttons > 0) && !joystick->buttons)) { | |
141 SDL_OutOfMemory(); | |
142 SDL_JoystickClose(joystick); | |
143 joystick = NULL; | |
144 } | |
145 if ( joystick->axes ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
146 SDL_memset(joystick->axes, 0, |
0 | 147 joystick->naxes*sizeof(Sint16)); |
148 } | |
149 if ( joystick->hats ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
150 SDL_memset(joystick->hats, 0, |
0 | 151 joystick->nhats*sizeof(Uint8)); |
152 } | |
153 if ( joystick->balls ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
154 SDL_memset(joystick->balls, 0, |
0 | 155 joystick->nballs*sizeof(*joystick->balls)); |
156 } | |
157 if ( joystick->buttons ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
158 SDL_memset(joystick->buttons, 0, |
0 | 159 joystick->nbuttons*sizeof(Uint8)); |
160 } | |
161 } | |
162 } | |
163 if ( joystick ) { | |
164 /* Add joystick to list */ | |
165 ++joystick->ref_count; | |
166 SDL_Lock_EventThread(); | |
167 for ( i=0; SDL_joysticks[i]; ++i ) | |
168 /* Skip to next joystick */; | |
169 SDL_joysticks[i] = joystick; | |
170 SDL_Unlock_EventThread(); | |
171 } | |
172 return(joystick); | |
173 } | |
174 | |
175 /* | |
176 * Returns 1 if the joystick has been opened, or 0 if it has not. | |
177 */ | |
178 int SDL_JoystickOpened(int device_index) | |
179 { | |
180 int i, opened; | |
181 | |
182 opened = 0; | |
183 for ( i=0; SDL_joysticks[i]; ++i ) { | |
184 if ( SDL_joysticks[i]->index == (Uint8)device_index ) { | |
185 opened = 1; | |
186 break; | |
187 } | |
188 } | |
189 return(opened); | |
190 } | |
191 | |
192 static int ValidJoystick(SDL_Joystick **joystick) | |
193 { | |
194 int valid; | |
195 | |
196 if ( *joystick == NULL ) { | |
197 *joystick = default_joystick; | |
198 } | |
199 if ( *joystick == NULL ) { | |
200 SDL_SetError("Joystick hasn't been opened yet"); | |
201 valid = 0; | |
202 } else { | |
203 valid = 1; | |
204 } | |
205 return valid; | |
206 } | |
207 | |
208 /* | |
209 * Get the device index of an opened joystick. | |
210 */ | |
211 int SDL_JoystickIndex(SDL_Joystick *joystick) | |
212 { | |
213 if ( ! ValidJoystick(&joystick) ) { | |
214 return(-1); | |
215 } | |
216 return(joystick->index); | |
217 } | |
218 | |
219 /* | |
220 * Get the number of multi-dimensional axis controls on a joystick | |
221 */ | |
222 int SDL_JoystickNumAxes(SDL_Joystick *joystick) | |
223 { | |
224 if ( ! ValidJoystick(&joystick) ) { | |
225 return(-1); | |
226 } | |
227 return(joystick->naxes); | |
228 } | |
229 | |
230 /* | |
231 * Get the number of hats on a joystick | |
232 */ | |
233 int SDL_JoystickNumHats(SDL_Joystick *joystick) | |
234 { | |
235 if ( ! ValidJoystick(&joystick) ) { | |
236 return(-1); | |
237 } | |
238 return(joystick->nhats); | |
239 } | |
240 | |
241 /* | |
242 * Get the number of trackballs on a joystick | |
243 */ | |
244 int SDL_JoystickNumBalls(SDL_Joystick *joystick) | |
245 { | |
246 if ( ! ValidJoystick(&joystick) ) { | |
247 return(-1); | |
248 } | |
249 return(joystick->nballs); | |
250 } | |
251 | |
252 /* | |
253 * Get the number of buttons on a joystick | |
254 */ | |
255 int SDL_JoystickNumButtons(SDL_Joystick *joystick) | |
256 { | |
257 if ( ! ValidJoystick(&joystick) ) { | |
258 return(-1); | |
259 } | |
260 return(joystick->nbuttons); | |
261 } | |
262 | |
263 /* | |
264 * Get the current state of an axis control on a joystick | |
265 */ | |
266 Sint16 SDL_JoystickGetAxis(SDL_Joystick *joystick, int axis) | |
267 { | |
268 Sint16 state; | |
269 | |
270 if ( ! ValidJoystick(&joystick) ) { | |
271 return(0); | |
272 } | |
273 if ( axis < joystick->naxes ) { | |
274 state = joystick->axes[axis]; | |
275 } else { | |
276 SDL_SetError("Joystick only has %d axes", joystick->naxes); | |
277 state = 0; | |
278 } | |
279 return(state); | |
280 } | |
281 | |
282 /* | |
283 * Get the current state of a hat on a joystick | |
284 */ | |
285 Uint8 SDL_JoystickGetHat(SDL_Joystick *joystick, int hat) | |
286 { | |
287 Uint8 state; | |
288 | |
289 if ( ! ValidJoystick(&joystick) ) { | |
290 return(0); | |
291 } | |
292 if ( hat < joystick->nhats ) { | |
293 state = joystick->hats[hat]; | |
294 } else { | |
295 SDL_SetError("Joystick only has %d hats", joystick->nhats); | |
296 state = 0; | |
297 } | |
298 return(state); | |
299 } | |
300 | |
301 /* | |
302 * Get the ball axis change since the last poll | |
303 */ | |
304 int SDL_JoystickGetBall(SDL_Joystick *joystick, int ball, int *dx, int *dy) | |
305 { | |
306 int retval; | |
307 | |
308 if ( ! ValidJoystick(&joystick) ) { | |
309 return(-1); | |
310 } | |
311 | |
312 retval = 0; | |
313 if ( ball < joystick->nballs ) { | |
314 if ( dx ) { | |
315 *dx = joystick->balls[ball].dx; | |
316 } | |
317 if ( dy ) { | |
318 *dy = joystick->balls[ball].dy; | |
319 } | |
320 joystick->balls[ball].dx = 0; | |
321 joystick->balls[ball].dy = 0; | |
322 } else { | |
323 SDL_SetError("Joystick only has %d balls", joystick->nballs); | |
324 retval = -1; | |
325 } | |
326 return(retval); | |
327 } | |
328 | |
329 /* | |
330 * Get the current state of a button on a joystick | |
331 */ | |
332 Uint8 SDL_JoystickGetButton(SDL_Joystick *joystick, int button) | |
333 { | |
334 Uint8 state; | |
335 | |
336 if ( ! ValidJoystick(&joystick) ) { | |
337 return(0); | |
338 } | |
339 if ( button < joystick->nbuttons ) { | |
340 state = joystick->buttons[button]; | |
341 } else { | |
342 SDL_SetError("Joystick only has %d buttons",joystick->nbuttons); | |
343 state = 0; | |
344 } | |
345 return(state); | |
346 } | |
347 | |
348 /* | |
349 * Close a joystick previously opened with SDL_JoystickOpen() | |
350 */ | |
351 void SDL_JoystickClose(SDL_Joystick *joystick) | |
352 { | |
353 int i; | |
354 | |
355 if ( ! ValidJoystick(&joystick) ) { | |
356 return; | |
357 } | |
358 | |
359 /* First decrement ref count */ | |
360 if ( --joystick->ref_count > 0 ) { | |
361 return; | |
362 } | |
363 | |
364 /* Lock the event queue - prevent joystick polling */ | |
365 SDL_Lock_EventThread(); | |
366 | |
367 if ( joystick == default_joystick ) { | |
368 default_joystick = NULL; | |
369 } | |
370 SDL_SYS_JoystickClose(joystick); | |
371 | |
372 /* Remove joystick from list */ | |
373 for ( i=0; SDL_joysticks[i]; ++i ) { | |
374 if ( joystick == SDL_joysticks[i] ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
375 SDL_memcpy(&SDL_joysticks[i], &SDL_joysticks[i+1], |
0 | 376 (SDL_numjoysticks-i)*sizeof(joystick)); |
377 break; | |
378 } | |
379 } | |
380 | |
381 /* Let the event thread keep running */ | |
382 SDL_Unlock_EventThread(); | |
383 | |
384 /* Free the data associated with this joystick */ | |
385 if ( joystick->axes ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
386 SDL_free(joystick->axes); |
0 | 387 } |
388 if ( joystick->hats ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
389 SDL_free(joystick->hats); |
0 | 390 } |
391 if ( joystick->balls ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
392 SDL_free(joystick->balls); |
0 | 393 } |
394 if ( joystick->buttons ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
395 SDL_free(joystick->buttons); |
0 | 396 } |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
397 SDL_free(joystick); |
0 | 398 } |
399 | |
400 void SDL_JoystickQuit(void) | |
401 { | |
402 /* Stop the event polling */ | |
403 SDL_Lock_EventThread(); | |
404 SDL_numjoysticks = 0; | |
405 SDL_Unlock_EventThread(); | |
406 | |
407 /* Quit the joystick setup */ | |
408 SDL_SYS_JoystickQuit(); | |
409 if ( SDL_joysticks ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
410 SDL_free(SDL_joysticks); |
0 | 411 SDL_joysticks = NULL; |
412 } | |
413 } | |
414 | |
415 | |
416 /* These are global for SDL_sysjoystick.c and SDL_events.c */ | |
417 | |
418 int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value) | |
419 { | |
420 int posted; | |
421 | |
422 /* Update internal joystick state */ | |
423 joystick->axes[axis] = value; | |
424 | |
425 /* Post the event, if desired */ | |
426 posted = 0; | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
427 #if !SDL_EVENTS_DISABLED |
0 | 428 if ( SDL_ProcessEvents[SDL_JOYAXISMOTION] == SDL_ENABLE ) { |
429 SDL_Event event; | |
430 event.type = SDL_JOYAXISMOTION; | |
431 event.jaxis.which = joystick->index; | |
432 event.jaxis.axis = axis; | |
433 event.jaxis.value = value; | |
434 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { | |
435 posted = 1; | |
436 SDL_PushEvent(&event); | |
437 } | |
438 } | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
439 #endif /* !SDL_EVENTS_DISABLED */ |
0 | 440 return(posted); |
441 } | |
442 | |
443 int SDL_PrivateJoystickHat(SDL_Joystick *joystick, Uint8 hat, Uint8 value) | |
444 { | |
445 int posted; | |
446 | |
447 /* Update internal joystick state */ | |
448 joystick->hats[hat] = value; | |
449 | |
450 /* Post the event, if desired */ | |
451 posted = 0; | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
452 #if !SDL_EVENTS_DISABLED |
0 | 453 if ( SDL_ProcessEvents[SDL_JOYHATMOTION] == SDL_ENABLE ) { |
454 SDL_Event event; | |
455 event.jhat.type = SDL_JOYHATMOTION; | |
456 event.jhat.which = joystick->index; | |
457 event.jhat.hat = hat; | |
458 event.jhat.value = value; | |
459 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { | |
460 posted = 1; | |
461 SDL_PushEvent(&event); | |
462 } | |
463 } | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
464 #endif /* !SDL_EVENTS_DISABLED */ |
0 | 465 return(posted); |
466 } | |
467 | |
468 int SDL_PrivateJoystickBall(SDL_Joystick *joystick, Uint8 ball, | |
469 Sint16 xrel, Sint16 yrel) | |
470 { | |
471 int posted; | |
472 | |
473 /* Update internal mouse state */ | |
474 joystick->balls[ball].dx += xrel; | |
475 joystick->balls[ball].dy += yrel; | |
476 | |
477 /* Post the event, if desired */ | |
478 posted = 0; | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
479 #if !SDL_EVENTS_DISABLED |
0 | 480 if ( SDL_ProcessEvents[SDL_JOYBALLMOTION] == SDL_ENABLE ) { |
481 SDL_Event event; | |
482 event.jball.type = SDL_JOYBALLMOTION; | |
483 event.jball.which = joystick->index; | |
484 event.jball.ball = ball; | |
485 event.jball.xrel = xrel; | |
486 event.jball.yrel = yrel; | |
487 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { | |
488 posted = 1; | |
489 SDL_PushEvent(&event); | |
490 } | |
491 } | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
492 #endif /* !SDL_EVENTS_DISABLED */ |
0 | 493 return(posted); |
494 } | |
495 | |
496 int SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state) | |
497 { | |
498 int posted; | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
499 #if !SDL_EVENTS_DISABLED |
0 | 500 SDL_Event event; |
501 | |
502 switch ( state ) { | |
503 case SDL_PRESSED: | |
504 event.type = SDL_JOYBUTTONDOWN; | |
505 break; | |
506 case SDL_RELEASED: | |
507 event.type = SDL_JOYBUTTONUP; | |
508 break; | |
509 default: | |
510 /* Invalid state -- bail */ | |
511 return(0); | |
512 } | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
513 #endif /* !SDL_EVENTS_DISABLED */ |
0 | 514 |
515 /* Update internal joystick state */ | |
516 joystick->buttons[button] = state; | |
517 | |
518 /* Post the event, if desired */ | |
519 posted = 0; | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
520 #if !SDL_EVENTS_DISABLED |
0 | 521 if ( SDL_ProcessEvents[event.type] == SDL_ENABLE ) { |
522 event.jbutton.which = joystick->index; | |
523 event.jbutton.button = button; | |
524 event.jbutton.state = state; | |
525 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { | |
526 posted = 1; | |
527 SDL_PushEvent(&event); | |
528 } | |
529 } | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
530 #endif /* !SDL_EVENTS_DISABLED */ |
0 | 531 return(posted); |
532 } | |
533 | |
534 void SDL_JoystickUpdate(void) | |
535 { | |
536 int i; | |
537 | |
538 for ( i=0; SDL_joysticks[i]; ++i ) { | |
539 SDL_SYS_JoystickUpdate(SDL_joysticks[i]); | |
540 } | |
541 } | |
542 | |
543 int SDL_JoystickEventState(int state) | |
544 { | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
545 #if SDL_EVENTS_DISABLED |
0 | 546 return SDL_IGNORE; |
547 #else | |
548 const Uint8 event_list[] = { | |
549 SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION, | |
550 SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, | |
551 }; | |
552 int i; | |
553 | |
554 switch (state) { | |
555 case SDL_QUERY: | |
556 state = SDL_IGNORE; | |
557 for ( i=0; i<SDL_TABLESIZE(event_list); ++i ) { | |
558 state = SDL_EventState(event_list[i],SDL_QUERY); | |
559 if ( state == SDL_ENABLE ) { | |
560 break; | |
561 } | |
562 } | |
563 break; | |
564 default: | |
565 for ( i=0; i<SDL_TABLESIZE(event_list); ++i ) { | |
566 SDL_EventState(event_list[i], state); | |
567 } | |
568 break; | |
569 } | |
570 return(state); | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
571 #endif /* SDL_EVENTS_DISABLED */ |
0 | 572 } |