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