src/joystick/SDL_joystick.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 28 Nov 2012 11:52:38 -0800
changeset 6712 05f046f5886b
parent 6707 70eeb7e3ec2f
child 6735 486800c3f44c
permissions -rw-r--r--
Fixed crashes in new joystick code
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@0
    22
slouken@0
    23
/* This is the joystick API for Simple DirectMedia Layer */
slouken@0
    24
slouken@0
    25
#include "SDL_events.h"
slouken@1361
    26
#include "SDL_sysjoystick.h"
icculus@5861
    27
#include "SDL_assert.h"
icculus@5861
    28
slouken@1361
    29
#if !SDL_EVENTS_DISABLED
slouken@1361
    30
#include "../events/SDL_events_c.h"
slouken@0
    31
#endif
slouken@0
    32
slouken@6712
    33
static SDL_Joystick *SDL_joysticks = NULL;
slouken@6712
    34
static SDL_Joystick *SDL_updating_joystick = NULL;
slouken@0
    35
slouken@1895
    36
int
slouken@1895
    37
SDL_JoystickInit(void)
slouken@0
    38
{
slouken@1895
    39
    int status;
slouken@0
    40
slouken@1895
    41
    status = SDL_SYS_JoystickInit();
slouken@1895
    42
    if (status >= 0) {
slouken@6690
    43
      status = 0;
slouken@1895
    44
    }
slouken@1895
    45
    return (status);
slouken@0
    46
}
slouken@0
    47
slouken@0
    48
/*
slouken@0
    49
 * Count the number of joysticks attached to the system
slouken@0
    50
 */
slouken@1895
    51
int
slouken@1895
    52
SDL_NumJoysticks(void)
slouken@0
    53
{
slouken@6690
    54
    return SDL_SYS_NumJoysticks();
slouken@0
    55
}
slouken@0
    56
slouken@0
    57
/*
slouken@0
    58
 * Get the implementation dependent name of a joystick
slouken@0
    59
 */
slouken@1895
    60
const char *
slouken@6690
    61
SDL_JoystickNameForIndex(int device_index)
slouken@0
    62
{
slouken@6690
    63
    if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
slouken@6690
    64
        SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
slouken@1895
    65
        return (NULL);
slouken@1895
    66
    }
slouken@6707
    67
    return (SDL_SYS_JoystickNameForDeviceIndex(device_index));
slouken@0
    68
}
slouken@0
    69
slouken@0
    70
/*
slouken@0
    71
 * Open a joystick for use - the index passed as an argument refers to
slouken@0
    72
 * the N'th joystick on the system.  This index is the value which will
slouken@0
    73
 * identify this joystick in future joystick events.
slouken@0
    74
 *
slouken@0
    75
 * This function returns a joystick identifier, or NULL if an error occurred.
slouken@0
    76
 */
slouken@1895
    77
SDL_Joystick *
slouken@1895
    78
SDL_JoystickOpen(int device_index)
slouken@0
    79
{
slouken@1895
    80
    SDL_Joystick *joystick;
slouken@6690
    81
	SDL_Joystick *joysticklist;
slouken@6690
    82
	const char *joystickname = NULL;
slouken@0
    83
slouken@6690
    84
    if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
slouken@6690
    85
        SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
slouken@1895
    86
        return (NULL);
slouken@1895
    87
    }
slouken@0
    88
slouken@6690
    89
	joysticklist = SDL_joysticks;
slouken@6690
    90
    /* If the joystick is already open, return it 
slouken@6690
    91
	* it is important that we have a single joystick * for each instance id 
slouken@6690
    92
	*/
slouken@6690
    93
	while ( joysticklist )
slouken@6690
    94
	{
slouken@6690
    95
		if ( SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == joysticklist->instance_id ) {
slouken@6690
    96
				joystick = joysticklist;
slouken@6690
    97
				++joystick->ref_count;
slouken@6690
    98
				return (joystick);
slouken@6690
    99
		}
slouken@6690
   100
		joysticklist = joysticklist->next;
slouken@1895
   101
    }
slouken@0
   102
slouken@1895
   103
    /* Create and initialize the joystick */
slouken@1895
   104
    joystick = (SDL_Joystick *) SDL_malloc((sizeof *joystick));
icculus@3608
   105
    if (joystick == NULL) {
icculus@3608
   106
        SDL_OutOfMemory();
icculus@3608
   107
        return NULL;
slouken@1895
   108
    }
icculus@3608
   109
icculus@3608
   110
    SDL_memset(joystick, 0, (sizeof *joystick));
slouken@6690
   111
    if (SDL_SYS_JoystickOpen(joystick, device_index) < 0) {
icculus@3608
   112
        SDL_free(joystick);
icculus@3608
   113
        return NULL;
slouken@1895
   114
    }
slouken@6690
   115
slouken@6707
   116
	joystickname = SDL_SYS_JoystickNameForDeviceIndex( device_index );
slouken@6690
   117
	if ( joystickname )
slouken@6690
   118
		joystick->name = SDL_strdup( joystickname );
slouken@6690
   119
	else
slouken@6690
   120
		joystick->name = NULL;
slouken@6690
   121
icculus@3608
   122
    if (joystick->naxes > 0) {
icculus@3608
   123
        joystick->axes = (Sint16 *) SDL_malloc
icculus@3608
   124
            (joystick->naxes * sizeof(Sint16));
icculus@3608
   125
    }
icculus@3608
   126
    if (joystick->nhats > 0) {
icculus@3608
   127
        joystick->hats = (Uint8 *) SDL_malloc
icculus@3608
   128
            (joystick->nhats * sizeof(Uint8));
icculus@3608
   129
    }
icculus@3608
   130
    if (joystick->nballs > 0) {
icculus@3608
   131
        joystick->balls = (struct balldelta *) SDL_malloc
icculus@3608
   132
            (joystick->nballs * sizeof(*joystick->balls));
icculus@3608
   133
    }
icculus@3608
   134
    if (joystick->nbuttons > 0) {
icculus@3608
   135
        joystick->buttons = (Uint8 *) SDL_malloc
icculus@3608
   136
            (joystick->nbuttons * sizeof(Uint8));
icculus@3608
   137
    }
icculus@3608
   138
    if (((joystick->naxes > 0) && !joystick->axes)
icculus@3608
   139
        || ((joystick->nhats > 0) && !joystick->hats)
icculus@3608
   140
        || ((joystick->nballs > 0) && !joystick->balls)
icculus@3608
   141
        || ((joystick->nbuttons > 0) && !joystick->buttons)) {
icculus@3608
   142
        SDL_OutOfMemory();
icculus@3608
   143
        SDL_JoystickClose(joystick);
icculus@3608
   144
        return NULL;
icculus@3608
   145
    }
icculus@3608
   146
    if (joystick->axes) {
icculus@3608
   147
        SDL_memset(joystick->axes, 0, joystick->naxes * sizeof(Sint16));
icculus@3608
   148
    }
icculus@3608
   149
    if (joystick->hats) {
icculus@3608
   150
        SDL_memset(joystick->hats, 0, joystick->nhats * sizeof(Uint8));
icculus@3608
   151
    }
icculus@3608
   152
    if (joystick->balls) {
icculus@3608
   153
        SDL_memset(joystick->balls, 0,
icculus@3608
   154
            joystick->nballs * sizeof(*joystick->balls));
icculus@3608
   155
    }
icculus@3608
   156
    if (joystick->buttons) {
icculus@3608
   157
        SDL_memset(joystick->buttons, 0, joystick->nbuttons * sizeof(Uint8));
icculus@3608
   158
    }
icculus@3608
   159
icculus@3608
   160
    /* Add joystick to list */
icculus@3608
   161
    ++joystick->ref_count;
slouken@6690
   162
	/* Link the joystick in the list */
slouken@6690
   163
	joystick->next = SDL_joysticks;
slouken@6690
   164
	SDL_joysticks = joystick;
slouken@6690
   165
slouken@6690
   166
	SDL_SYS_JoystickUpdate( joystick );
icculus@3608
   167
slouken@1895
   168
    return (joystick);
slouken@0
   169
}
slouken@0
   170
slouken@2713
   171
slouken@2713
   172
/*
slouken@2713
   173
 * Checks to make sure the joystick is valid.
slouken@2713
   174
 */
slouken@2713
   175
int
slouken@6690
   176
SDL_PrivateJoystickValid(SDL_Joystick * joystick)
slouken@0
   177
{
slouken@1895
   178
    int valid;
slouken@0
   179
slouken@6690
   180
    if ( joystick == NULL ) {
slouken@1895
   181
        SDL_SetError("Joystick hasn't been opened yet");
slouken@1895
   182
        valid = 0;
slouken@1895
   183
    } else {
slouken@1895
   184
        valid = 1;
slouken@1895
   185
    }
slouken@6690
   186
	
slouken@6690
   187
	if ( joystick && joystick->closed )
slouken@6690
   188
	{
slouken@6690
   189
		valid = 0;
slouken@6690
   190
	}
slouken@6690
   191
	
slouken@1895
   192
    return valid;
slouken@0
   193
}
slouken@0
   194
slouken@0
   195
/*
slouken@0
   196
 * Get the number of multi-dimensional axis controls on a joystick
slouken@0
   197
 */
slouken@1895
   198
int
slouken@1895
   199
SDL_JoystickNumAxes(SDL_Joystick * joystick)
slouken@0
   200
{
slouken@6690
   201
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@1895
   202
        return (-1);
slouken@1895
   203
    }
slouken@1895
   204
    return (joystick->naxes);
slouken@0
   205
}
slouken@0
   206
slouken@0
   207
/*
slouken@0
   208
 * Get the number of hats on a joystick
slouken@0
   209
 */
slouken@1895
   210
int
slouken@1895
   211
SDL_JoystickNumHats(SDL_Joystick * joystick)
slouken@0
   212
{
slouken@6690
   213
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@1895
   214
        return (-1);
slouken@1895
   215
    }
slouken@1895
   216
    return (joystick->nhats);
slouken@0
   217
}
slouken@0
   218
slouken@0
   219
/*
slouken@0
   220
 * Get the number of trackballs on a joystick
slouken@0
   221
 */
slouken@1895
   222
int
slouken@1895
   223
SDL_JoystickNumBalls(SDL_Joystick * joystick)
slouken@0
   224
{
slouken@6690
   225
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@1895
   226
        return (-1);
slouken@1895
   227
    }
slouken@1895
   228
    return (joystick->nballs);
slouken@0
   229
}
slouken@0
   230
slouken@0
   231
/*
slouken@0
   232
 * Get the number of buttons on a joystick
slouken@0
   233
 */
slouken@1895
   234
int
slouken@1895
   235
SDL_JoystickNumButtons(SDL_Joystick * joystick)
slouken@0
   236
{
slouken@6690
   237
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@1895
   238
        return (-1);
slouken@1895
   239
    }
slouken@1895
   240
    return (joystick->nbuttons);
slouken@0
   241
}
slouken@0
   242
slouken@0
   243
/*
slouken@0
   244
 * Get the current state of an axis control on a joystick
slouken@0
   245
 */
slouken@1895
   246
Sint16
slouken@1895
   247
SDL_JoystickGetAxis(SDL_Joystick * joystick, int axis)
slouken@0
   248
{
slouken@1895
   249
    Sint16 state;
slouken@0
   250
slouken@6690
   251
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@1895
   252
        return (0);
slouken@1895
   253
    }
slouken@1895
   254
    if (axis < joystick->naxes) {
slouken@1895
   255
        state = joystick->axes[axis];
slouken@1895
   256
    } else {
slouken@1895
   257
        SDL_SetError("Joystick only has %d axes", joystick->naxes);
slouken@1895
   258
        state = 0;
slouken@1895
   259
    }
slouken@1895
   260
    return (state);
slouken@0
   261
}
slouken@0
   262
slouken@0
   263
/*
slouken@0
   264
 * Get the current state of a hat on a joystick
slouken@0
   265
 */
slouken@1895
   266
Uint8
slouken@1895
   267
SDL_JoystickGetHat(SDL_Joystick * joystick, int hat)
slouken@0
   268
{
slouken@1895
   269
    Uint8 state;
slouken@0
   270
slouken@6690
   271
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@1895
   272
        return (0);
slouken@1895
   273
    }
slouken@1895
   274
    if (hat < joystick->nhats) {
slouken@1895
   275
        state = joystick->hats[hat];
slouken@1895
   276
    } else {
slouken@1895
   277
        SDL_SetError("Joystick only has %d hats", joystick->nhats);
slouken@1895
   278
        state = 0;
slouken@1895
   279
    }
slouken@1895
   280
    return (state);
slouken@0
   281
}
slouken@0
   282
slouken@0
   283
/*
slouken@0
   284
 * Get the ball axis change since the last poll
slouken@0
   285
 */
slouken@1895
   286
int
slouken@1895
   287
SDL_JoystickGetBall(SDL_Joystick * joystick, int ball, int *dx, int *dy)
slouken@0
   288
{
slouken@1895
   289
    int retval;
slouken@0
   290
slouken@6690
   291
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@1895
   292
        return (-1);
slouken@1895
   293
    }
slouken@0
   294
slouken@1895
   295
    retval = 0;
slouken@1895
   296
    if (ball < joystick->nballs) {
slouken@1895
   297
        if (dx) {
slouken@1895
   298
            *dx = joystick->balls[ball].dx;
slouken@1895
   299
        }
slouken@1895
   300
        if (dy) {
slouken@1895
   301
            *dy = joystick->balls[ball].dy;
slouken@1895
   302
        }
slouken@1895
   303
        joystick->balls[ball].dx = 0;
slouken@1895
   304
        joystick->balls[ball].dy = 0;
slouken@1895
   305
    } else {
slouken@1895
   306
        SDL_SetError("Joystick only has %d balls", joystick->nballs);
slouken@1895
   307
        retval = -1;
slouken@1895
   308
    }
slouken@1895
   309
    return (retval);
slouken@0
   310
}
slouken@0
   311
slouken@0
   312
/*
slouken@0
   313
 * Get the current state of a button on a joystick
slouken@0
   314
 */
slouken@1895
   315
Uint8
slouken@1895
   316
SDL_JoystickGetButton(SDL_Joystick * joystick, int button)
slouken@0
   317
{
slouken@1895
   318
    Uint8 state;
slouken@0
   319
slouken@6690
   320
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@1895
   321
        return (0);
slouken@1895
   322
    }
slouken@1895
   323
    if (button < joystick->nbuttons) {
slouken@1895
   324
        state = joystick->buttons[button];
slouken@1895
   325
    } else {
slouken@1895
   326
        SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
slouken@1895
   327
        state = 0;
slouken@1895
   328
    }
slouken@1895
   329
    return (state);
slouken@0
   330
}
slouken@0
   331
slouken@0
   332
/*
slouken@6690
   333
 * Return if the joystick in question is currently attached to the system,
slouken@6690
   334
 *  \return 0 if not plugged in, 1 if still present.
slouken@6690
   335
 */
slouken@6707
   336
SDL_bool
slouken@6707
   337
SDL_JoystickGetAttached(SDL_Joystick * joystick)
slouken@6690
   338
{
slouken@6690
   339
	if (!SDL_PrivateJoystickValid(joystick)) {
slouken@6707
   340
        return SDL_FALSE;
slouken@6690
   341
    }
slouken@6690
   342
slouken@6690
   343
	return SDL_SYS_JoystickAttached(joystick);
slouken@6690
   344
}
slouken@6690
   345
slouken@6690
   346
/*
slouken@6690
   347
 * Get the instance id for this opened joystick
slouken@6690
   348
 */
slouken@6690
   349
SDL_JoystickID 
slouken@6707
   350
SDL_JoystickInstanceID(SDL_Joystick * joystick)
slouken@6690
   351
{
slouken@6690
   352
	if (!SDL_PrivateJoystickValid(joystick)) {
slouken@6690
   353
        return (-1);
slouken@6690
   354
    }
slouken@6690
   355
slouken@6690
   356
	return (joystick->instance_id);
slouken@6690
   357
}
slouken@6690
   358
slouken@6690
   359
/*
slouken@6690
   360
 * Get the friendly name of this joystick
slouken@6690
   361
 */
slouken@6690
   362
const char *
slouken@6690
   363
SDL_JoystickName(SDL_Joystick * joystick)
slouken@6690
   364
{
slouken@6690
   365
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@6690
   366
        return (NULL);
slouken@6690
   367
    }
slouken@6690
   368
	
slouken@6690
   369
    return (joystick->name);
slouken@6690
   370
}
slouken@6690
   371
slouken@6690
   372
/*
slouken@0
   373
 * Close a joystick previously opened with SDL_JoystickOpen()
slouken@0
   374
 */
slouken@1895
   375
void
slouken@1895
   376
SDL_JoystickClose(SDL_Joystick * joystick)
slouken@0
   377
{
slouken@6690
   378
	SDL_Joystick *joysticklist;
slouken@6690
   379
	SDL_Joystick *joysticklistprev;
slouken@0
   380
slouken@6690
   381
    if (!joystick) {
slouken@1895
   382
        return;
slouken@1895
   383
    }
slouken@0
   384
slouken@1895
   385
    /* First decrement ref count */
slouken@1895
   386
    if (--joystick->ref_count > 0) {
slouken@1895
   387
        return;
slouken@1895
   388
    }
slouken@0
   389
slouken@6712
   390
    if (joystick == SDL_updating_joystick) {
slouken@6712
   391
        return;
slouken@6712
   392
    }
slouken@6712
   393
slouken@1895
   394
    SDL_SYS_JoystickClose(joystick);
slouken@6690
   395
	
slouken@6690
   396
	joysticklist = SDL_joysticks;
slouken@6690
   397
	joysticklistprev = NULL;
slouken@6690
   398
	while ( joysticklist )
slouken@6690
   399
	{
slouken@6690
   400
		if (joystick == joysticklist) 
slouken@6690
   401
		{
slouken@6690
   402
			if ( joysticklistprev )
slouken@6690
   403
			{
slouken@6690
   404
				// unlink this entry
slouken@6690
   405
				joysticklistprev->next = joysticklist->next;
slouken@6690
   406
			}
slouken@6690
   407
			else
slouken@6690
   408
			{
slouken@6690
   409
				SDL_joysticks = joystick->next;
slouken@6690
   410
			}
slouken@0
   411
slouken@6690
   412
			break;
slouken@6690
   413
		}
slouken@6690
   414
		joysticklistprev = joysticklist;
slouken@6690
   415
		joysticklist = joysticklist->next;
slouken@6690
   416
	}
slouken@6690
   417
	
slouken@6690
   418
	if (joystick->name)
slouken@6690
   419
		SDL_free(joystick->name);
slouken@0
   420
slouken@1895
   421
    /* Free the data associated with this joystick */
slouken@1895
   422
    if (joystick->axes) {
slouken@1895
   423
        SDL_free(joystick->axes);
slouken@1895
   424
    }
slouken@1895
   425
    if (joystick->hats) {
slouken@1895
   426
        SDL_free(joystick->hats);
slouken@1895
   427
    }
slouken@1895
   428
    if (joystick->balls) {
slouken@1895
   429
        SDL_free(joystick->balls);
slouken@1895
   430
    }
slouken@1895
   431
    if (joystick->buttons) {
slouken@1895
   432
        SDL_free(joystick->buttons);
slouken@1895
   433
    }
slouken@1895
   434
    SDL_free(joystick);
slouken@0
   435
}
slouken@0
   436
slouken@1895
   437
void
slouken@1895
   438
SDL_JoystickQuit(void)
slouken@0
   439
{
slouken@6712
   440
    /* Make sure we're not getting called in the middle of updating joysticks */
slouken@6712
   441
    SDL_assert(!SDL_updating_joystick);
slouken@6712
   442
slouken@1895
   443
    /* Stop the event polling */
slouken@6690
   444
	while ( SDL_joysticks )
slouken@6690
   445
	{
slouken@6690
   446
		SDL_joysticks->ref_count = 1;
slouken@6690
   447
        SDL_JoystickClose(SDL_joysticks);
slouken@6690
   448
 	}
icculus@5856
   449
slouken@1895
   450
    /* Quit the joystick setup */
slouken@1895
   451
    SDL_SYS_JoystickQuit();
slouken@0
   452
}
slouken@0
   453
slouken@0
   454
slouken@0
   455
/* These are global for SDL_sysjoystick.c and SDL_events.c */
slouken@0
   456
slouken@1895
   457
int
slouken@1895
   458
SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
slouken@0
   459
{
slouken@1895
   460
    int posted;
slouken@0
   461
slouken@6145
   462
    /* Make sure we're not getting garbage events */
slouken@6145
   463
    if (axis >= joystick->naxes) {
slouken@6145
   464
        return 0;
slouken@6145
   465
    }
slouken@6145
   466
slouken@1895
   467
    /* Update internal joystick state */
slouken@1895
   468
    joystick->axes[axis] = value;
slouken@0
   469
slouken@1895
   470
    /* Post the event, if desired */
slouken@1895
   471
    posted = 0;
slouken@1361
   472
#if !SDL_EVENTS_DISABLED
slouken@4429
   473
    if (SDL_GetEventState(SDL_JOYAXISMOTION) == SDL_ENABLE) {
slouken@1895
   474
        SDL_Event event;
slouken@1895
   475
        event.type = SDL_JOYAXISMOTION;
slouken@6690
   476
        event.jaxis.which = joystick->instance_id;
slouken@1895
   477
        event.jaxis.axis = axis;
slouken@1895
   478
        event.jaxis.value = value;
slouken@6690
   479
        posted = SDL_PushEvent(&event) == 1;
slouken@1895
   480
    }
slouken@1361
   481
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   482
    return (posted);
slouken@0
   483
}
slouken@0
   484
slouken@1895
   485
int
slouken@1895
   486
SDL_PrivateJoystickHat(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
slouken@0
   487
{
slouken@1895
   488
    int posted;
slouken@0
   489
slouken@6145
   490
    /* Make sure we're not getting garbage events */
slouken@6145
   491
    if (hat >= joystick->nhats) {
slouken@6145
   492
        return 0;
slouken@6145
   493
    }
slouken@6145
   494
slouken@1895
   495
    /* Update internal joystick state */
slouken@1895
   496
    joystick->hats[hat] = value;
slouken@0
   497
slouken@1895
   498
    /* Post the event, if desired */
slouken@1895
   499
    posted = 0;
slouken@1361
   500
#if !SDL_EVENTS_DISABLED
slouken@4429
   501
    if (SDL_GetEventState(SDL_JOYHATMOTION) == SDL_ENABLE) {
slouken@1895
   502
        SDL_Event event;
slouken@1895
   503
        event.jhat.type = SDL_JOYHATMOTION;
slouken@6690
   504
        event.jhat.which = joystick->instance_id;
slouken@1895
   505
        event.jhat.hat = hat;
slouken@1895
   506
        event.jhat.value = value;
slouken@6690
   507
        posted = SDL_PushEvent(&event) == 1;
slouken@1895
   508
    }
slouken@1361
   509
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   510
    return (posted);
slouken@0
   511
}
slouken@0
   512
slouken@1895
   513
int
slouken@1895
   514
SDL_PrivateJoystickBall(SDL_Joystick * joystick, Uint8 ball,
slouken@1895
   515
                        Sint16 xrel, Sint16 yrel)
slouken@0
   516
{
slouken@1895
   517
    int posted;
slouken@0
   518
slouken@6145
   519
    /* Make sure we're not getting garbage events */
slouken@6145
   520
    if (ball >= joystick->nballs) {
slouken@6145
   521
        return 0;
slouken@6145
   522
    }
slouken@6145
   523
slouken@1895
   524
    /* Update internal mouse state */
slouken@1895
   525
    joystick->balls[ball].dx += xrel;
slouken@1895
   526
    joystick->balls[ball].dy += yrel;
slouken@0
   527
slouken@1895
   528
    /* Post the event, if desired */
slouken@1895
   529
    posted = 0;
slouken@1361
   530
#if !SDL_EVENTS_DISABLED
slouken@4429
   531
    if (SDL_GetEventState(SDL_JOYBALLMOTION) == SDL_ENABLE) {
slouken@1895
   532
        SDL_Event event;
slouken@1895
   533
        event.jball.type = SDL_JOYBALLMOTION;
slouken@6690
   534
        event.jball.which = joystick->instance_id;
slouken@1895
   535
        event.jball.ball = ball;
slouken@1895
   536
        event.jball.xrel = xrel;
slouken@1895
   537
        event.jball.yrel = yrel;
slouken@6690
   538
        posted = SDL_PushEvent(&event) == 1;
slouken@1895
   539
    }
slouken@1361
   540
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   541
    return (posted);
slouken@0
   542
}
slouken@0
   543
slouken@1895
   544
int
slouken@1895
   545
SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
slouken@0
   546
{
slouken@1895
   547
    int posted;
slouken@1361
   548
#if !SDL_EVENTS_DISABLED
slouken@1895
   549
    SDL_Event event;
slouken@0
   550
slouken@1895
   551
    switch (state) {
slouken@1895
   552
    case SDL_PRESSED:
slouken@1895
   553
        event.type = SDL_JOYBUTTONDOWN;
slouken@1895
   554
        break;
slouken@1895
   555
    case SDL_RELEASED:
slouken@1895
   556
        event.type = SDL_JOYBUTTONUP;
slouken@1895
   557
        break;
slouken@1895
   558
    default:
slouken@1895
   559
        /* Invalid state -- bail */
slouken@1895
   560
        return (0);
slouken@1895
   561
    }
slouken@1361
   562
#endif /* !SDL_EVENTS_DISABLED */
slouken@0
   563
slouken@6145
   564
    /* Make sure we're not getting garbage events */
slouken@6145
   565
    if (button >= joystick->nbuttons) {
slouken@6145
   566
        return 0;
slouken@6145
   567
    }
slouken@6145
   568
slouken@1895
   569
    /* Update internal joystick state */
slouken@1895
   570
    joystick->buttons[button] = state;
slouken@0
   571
slouken@1895
   572
    /* Post the event, if desired */
slouken@1895
   573
    posted = 0;
slouken@1361
   574
#if !SDL_EVENTS_DISABLED
slouken@4429
   575
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@6690
   576
        event.jbutton.which = joystick->instance_id;
slouken@1895
   577
        event.jbutton.button = button;
slouken@1895
   578
        event.jbutton.state = state;
slouken@6690
   579
        posted = SDL_PushEvent(&event) == 1;
slouken@1895
   580
    }
slouken@1361
   581
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   582
    return (posted);
slouken@0
   583
}
slouken@0
   584
slouken@1895
   585
void
slouken@1895
   586
SDL_JoystickUpdate(void)
slouken@0
   587
{
slouken@6690
   588
	SDL_Joystick *joystick;
slouken@6690
   589
		
slouken@6690
   590
	joystick = SDL_joysticks;
slouken@6690
   591
	while ( joystick )
slouken@6690
   592
	{
slouken@6690
   593
		SDL_Joystick *joysticknext;
slouken@6690
   594
		/* save off the next pointer, the Update call may cause a joystick removed event
slouken@6690
   595
		 * and cause our joystick pointer to be freed
slouken@6690
   596
		 */
slouken@6690
   597
		joysticknext = joystick->next;
slouken@0
   598
slouken@6712
   599
        SDL_updating_joystick = joystick;
slouken@6712
   600
slouken@6690
   601
        SDL_SYS_JoystickUpdate( joystick );
slouken@6690
   602
slouken@6690
   603
		if ( joystick->closed && joystick->uncentered )
slouken@6690
   604
		{
slouken@6690
   605
			int i;
slouken@6690
   606
			joystick->uncentered = 0;
slouken@6690
   607
slouken@6712
   608
            /* Tell the app that everything is centered/unpressed...  */
slouken@6690
   609
            for (i = 0; i < joystick->naxes; i++)
slouken@6690
   610
                SDL_PrivateJoystickAxis(joystick, i, 0);
slouken@6690
   611
slouken@6690
   612
            for (i = 0; i < joystick->nbuttons; i++)
slouken@6690
   613
                SDL_PrivateJoystickButton(joystick, i, 0);
slouken@6690
   614
slouken@6690
   615
            for (i = 0; i < joystick->nhats; i++)
slouken@6690
   616
                SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
slouken@6712
   617
slouken@6690
   618
		}
slouken@6690
   619
slouken@6712
   620
        SDL_updating_joystick = NULL;
slouken@6712
   621
slouken@6712
   622
        /* If the joystick was closed while updating, free it here */
slouken@6712
   623
        if ( joystick->ref_count <= 0 ) {
slouken@6712
   624
            SDL_JoystickClose(joystick);
slouken@6712
   625
        }
slouken@6712
   626
slouken@6690
   627
		joystick = joysticknext;
slouken@6690
   628
	}
slouken@6690
   629
slouken@6690
   630
	SDL_SYS_JoystickDetect();
slouken@0
   631
}
slouken@0
   632
slouken@1895
   633
int
slouken@1895
   634
SDL_JoystickEventState(int state)
slouken@0
   635
{
slouken@1361
   636
#if SDL_EVENTS_DISABLED
slouken@1895
   637
    return SDL_IGNORE;
slouken@0
   638
#else
slouken@4429
   639
    const Uint32 event_list[] = {
slouken@1895
   640
        SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION,
slouken@6690
   641
        SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED
slouken@1895
   642
    };
slouken@1895
   643
    unsigned int i;
slouken@0
   644
slouken@1895
   645
    switch (state) {
slouken@1895
   646
    case SDL_QUERY:
slouken@1895
   647
        state = SDL_IGNORE;
slouken@1895
   648
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@1895
   649
            state = SDL_EventState(event_list[i], SDL_QUERY);
slouken@1895
   650
            if (state == SDL_ENABLE) {
slouken@1895
   651
                break;
slouken@1895
   652
            }
slouken@1895
   653
        }
slouken@1895
   654
        break;
slouken@1895
   655
    default:
slouken@1895
   656
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@1895
   657
            SDL_EventState(event_list[i], state);
slouken@1895
   658
        }
slouken@1895
   659
        break;
slouken@1895
   660
    }
slouken@1895
   661
    return (state);
slouken@1361
   662
#endif /* SDL_EVENTS_DISABLED */
slouken@0
   663
}
slouken@1895
   664
slouken@6690
   665
/* return 1 if you want to run the joystick update loop this frame, used by hotplug support */
slouken@6707
   666
SDL_bool 
slouken@6690
   667
SDL_PrivateJoystickNeedsPolling()
slouken@6690
   668
{
slouken@6690
   669
	if ( SDL_SYS_JoystickNeedsPolling() )
slouken@6690
   670
	{
slouken@6690
   671
		// sys layer needs us to think
slouken@6707
   672
		return SDL_TRUE;
slouken@6690
   673
	}
slouken@6690
   674
	else
slouken@6690
   675
	{
slouken@6690
   676
		// otherwise only do it if a joystick is opened
slouken@6690
   677
		return SDL_joysticks != NULL; 
slouken@6690
   678
	}
slouken@6690
   679
}
slouken@6690
   680
slouken@6690
   681
slouken@6693
   682
/* return the guid for this index */
slouken@6707
   683
JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
slouken@6690
   684
{
slouken@6707
   685
	return SDL_SYS_JoystickGetDeviceGUID( device_index );
slouken@6690
   686
}
slouken@6690
   687
slouken@6693
   688
/* return the guid for this opened device */
slouken@6690
   689
JoystickGUID SDL_JoystickGetGUID(SDL_Joystick * joystick)
slouken@6690
   690
{
slouken@6707
   691
	return SDL_SYS_JoystickGetGUID( joystick );
slouken@6690
   692
}
slouken@6690
   693
slouken@6690
   694
/* convert the guid to a printable string */
slouken@6690
   695
char *SDL_JoystickGetGUIDString(JoystickGUID guid)
slouken@6690
   696
{
slouken@6690
   697
	static const char k_rgchHexToASCII[] = "0123456789abcdef";
slouken@6690
   698
	char *pchOut = NULL;
slouken@6690
   699
	char *pchString = NULL;
slouken@6690
   700
	int i;
slouken@6690
   701
	pchString = SDL_malloc(33); // 16 bytes
slouken@6690
   702
	if ( !pchString )
slouken@6690
   703
	{
slouken@6690
   704
		SDL_OutOfMemory();
slouken@6690
   705
		return NULL;
slouken@6690
   706
	}
slouken@6690
   707
	
slouken@6690
   708
	pchOut = pchString;
slouken@6690
   709
slouken@6690
   710
	for ( i = 0; i < sizeof(guid); i++ )
slouken@6690
   711
	{
slouken@6690
   712
		// each input byte writes 2 ascii chars, and might write a null byte.
slouken@6690
   713
		// If we don't have room for next input byte, stop
slouken@6690
   714
		unsigned char c = guid.data[i];
slouken@6690
   715
slouken@6690
   716
		*pchOut++ = k_rgchHexToASCII[ c >> 4 ];
slouken@6690
   717
		*pchOut++ = k_rgchHexToASCII[ c & 0x0F ];
slouken@6690
   718
	}
slouken@6690
   719
	*pchOut = '\0';
slouken@6690
   720
	return pchString;
slouken@6690
   721
}
slouken@6690
   722
slouken@6690
   723
slouken@6690
   724
//-----------------------------------------------------------------------------
slouken@6690
   725
// Purpose: Returns the 4 bit nibble for a hex character
slouken@6690
   726
// Input  : c - 
slouken@6690
   727
// Output : unsigned char
slouken@6690
   728
//-----------------------------------------------------------------------------
slouken@6690
   729
static unsigned char nibble( char c )
slouken@6690
   730
{
slouken@6690
   731
	if ( ( c >= '0' ) &&
slouken@6690
   732
		( c <= '9' ) )
slouken@6690
   733
	{
slouken@6690
   734
		return (unsigned char)(c - '0');
slouken@6690
   735
	}
slouken@6690
   736
slouken@6690
   737
	if ( ( c >= 'A' ) &&
slouken@6690
   738
		( c <= 'F' ) )
slouken@6690
   739
	{
slouken@6690
   740
		return (unsigned char)(c - 'A' + 0x0a);
slouken@6690
   741
	}
slouken@6690
   742
slouken@6690
   743
	if ( ( c >= 'a' ) &&
slouken@6690
   744
		( c <= 'f' ) )
slouken@6690
   745
	{
slouken@6690
   746
		return (unsigned char)(c - 'a' + 0x0a);
slouken@6690
   747
	}
slouken@6690
   748
slouken@6690
   749
	// received an invalid character, and no real way to return an error
slouken@6690
   750
	// AssertMsg1( false, "Q_nibble invalid hex character '%c' ", c );
slouken@6690
   751
	return 0;
slouken@6690
   752
}
slouken@6690
   753
slouken@6690
   754
slouken@6690
   755
/* convert the string version of a joystick guid to the struct */
slouken@6690
   756
JoystickGUID SDL_JoystickGetGUIDFromString(const char *pchGUID)
slouken@6690
   757
{
slouken@6690
   758
	JoystickGUID guid;
slouken@6690
   759
	int maxoutputbytes= sizeof(guid);
slouken@6690
   760
	int len = SDL_strlen( pchGUID );
slouken@6690
   761
	Uint8 *p;
slouken@6690
   762
	int i;
slouken@6690
   763
slouken@6690
   764
	// Make sure it's even
slouken@6690
   765
	len = ( len ) & ~0x1;
slouken@6690
   766
slouken@6690
   767
	SDL_memset( &guid, 0x00, sizeof(guid) );
slouken@6690
   768
slouken@6690
   769
	p = (Uint8 *)&guid;
slouken@6690
   770
	for ( i = 0; 
slouken@6690
   771
		( i < len ) && ( ( p - (Uint8 *)&guid ) < maxoutputbytes ); 
slouken@6690
   772
		i+=2, p++ )
slouken@6690
   773
	{
slouken@6690
   774
		*p = ( nibble( pchGUID[i] ) << 4 ) | nibble( pchGUID[i+1] );		
slouken@6690
   775
	}
slouken@6690
   776
slouken@6690
   777
	return guid;
slouken@6690
   778
}
slouken@6690
   779
slouken@6690
   780
slouken@1895
   781
/* vi: set ts=4 sw=4 expandtab: */