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