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