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