src/joystick/SDL_joystick.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 26 Aug 2016 12:18:08 -0700
changeset 10226 cb13d22b7f09
parent 9998 f67cf37e9cd4
child 10477 dc4bec0fc176
permissions -rw-r--r--
Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Updated the removal code to iterate over all joystick add messages instead of just the first one.
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@10226
   500
void SDL_PrivateJoystickAdded(int device_index)
slouken@10226
   501
{
slouken@10226
   502
#if !SDL_EVENTS_DISABLED
slouken@10226
   503
    SDL_Event event;
slouken@10226
   504
slouken@10226
   505
    event.type = SDL_JOYDEVICEADDED;
slouken@10226
   506
slouken@10226
   507
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@10226
   508
        event.jdevice.which = device_index;
slouken@10226
   509
        if ( (SDL_EventOK == NULL) ||
slouken@10226
   510
             (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
slouken@10226
   511
            SDL_PushEvent(&event);
slouken@10226
   512
        }
slouken@10226
   513
    }
slouken@10226
   514
#endif /* !SDL_EVENTS_DISABLED */
slouken@10226
   515
}
slouken@10226
   516
slouken@10226
   517
/*
slouken@10226
   518
 * If there is an existing add event in the queue, it needs to be modified
slouken@10226
   519
 * to have the right value for which, because the number of controllers in
slouken@10226
   520
 * the system is now one less.
slouken@10226
   521
 */
slouken@10226
   522
static void UpdateEventsForDeviceRemoval()
slouken@10226
   523
{
slouken@10226
   524
    int i, num_events;
slouken@10226
   525
    SDL_Event *events;
slouken@10226
   526
slouken@10226
   527
    num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED);
slouken@10226
   528
    if (num_events <= 0) {
slouken@10226
   529
        return;
slouken@10226
   530
    }
slouken@10226
   531
slouken@10226
   532
    events = SDL_stack_alloc(SDL_Event, num_events);
slouken@10226
   533
    if (!events) {
slouken@10226
   534
        return;
slouken@10226
   535
    }
slouken@10226
   536
slouken@10226
   537
    num_events = SDL_PeepEvents(events, num_events, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED);
slouken@10226
   538
    for (i = 0; i < num_events; ++i) {
slouken@10226
   539
        --events[i].jdevice.which;
slouken@10226
   540
    }
slouken@10226
   541
    SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0);
slouken@10226
   542
slouken@10226
   543
    SDL_stack_free(events);
slouken@10226
   544
}
slouken@10226
   545
slouken@10226
   546
void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
slouken@10226
   547
{
slouken@10226
   548
#if !SDL_EVENTS_DISABLED
slouken@10226
   549
    SDL_Event event;
slouken@10226
   550
slouken@10226
   551
    event.type = SDL_JOYDEVICEREMOVED;
slouken@10226
   552
slouken@10226
   553
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@10226
   554
        event.jdevice.which = device_instance;
slouken@10226
   555
        if ( (SDL_EventOK == NULL) ||
slouken@10226
   556
             (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
slouken@10226
   557
            SDL_PushEvent(&event);
slouken@10226
   558
        }
slouken@10226
   559
    }
slouken@10226
   560
slouken@10226
   561
    UpdateEventsForDeviceRemoval();
slouken@10226
   562
#endif /* !SDL_EVENTS_DISABLED */
slouken@10226
   563
}
slouken@10226
   564
slouken@1895
   565
int
slouken@1895
   566
SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
slouken@0
   567
{
slouken@1895
   568
    int posted;
slouken@0
   569
slouken@8920
   570
    /* Make sure we're not getting garbage or duplicate events */
slouken@6145
   571
    if (axis >= joystick->naxes) {
slouken@6145
   572
        return 0;
slouken@6145
   573
    }
slouken@8920
   574
    if (value == joystick->axes[axis]) {
slouken@8920
   575
        return 0;
slouken@8920
   576
    }
slouken@6145
   577
slouken@8903
   578
    /* We ignore events if we don't have keyboard focus, except for centering
slouken@8903
   579
     * events.
slouken@8903
   580
     */
slouken@8903
   581
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
slouken@8903
   582
        if ((value > 0 && value >= joystick->axes[axis]) ||
slouken@8903
   583
            (value < 0 && value <= joystick->axes[axis])) {
slouken@8903
   584
            return 0;
slouken@8903
   585
        }
slouken@8903
   586
    }
slouken@8903
   587
slouken@1895
   588
    /* Update internal joystick state */
slouken@1895
   589
    joystick->axes[axis] = value;
slouken@0
   590
slouken@1895
   591
    /* Post the event, if desired */
slouken@1895
   592
    posted = 0;
slouken@1361
   593
#if !SDL_EVENTS_DISABLED
slouken@4429
   594
    if (SDL_GetEventState(SDL_JOYAXISMOTION) == SDL_ENABLE) {
slouken@1895
   595
        SDL_Event event;
slouken@1895
   596
        event.type = SDL_JOYAXISMOTION;
slouken@6690
   597
        event.jaxis.which = joystick->instance_id;
slouken@1895
   598
        event.jaxis.axis = axis;
slouken@1895
   599
        event.jaxis.value = value;
slouken@6690
   600
        posted = SDL_PushEvent(&event) == 1;
slouken@1895
   601
    }
slouken@1361
   602
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   603
    return (posted);
slouken@0
   604
}
slouken@0
   605
slouken@1895
   606
int
slouken@1895
   607
SDL_PrivateJoystickHat(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
slouken@0
   608
{
slouken@1895
   609
    int posted;
slouken@0
   610
slouken@8920
   611
    /* Make sure we're not getting garbage or duplicate events */
slouken@6145
   612
    if (hat >= joystick->nhats) {
slouken@6145
   613
        return 0;
slouken@6145
   614
    }
slouken@8920
   615
    if (value == joystick->hats[hat]) {
slouken@8920
   616
        return 0;
slouken@8920
   617
    }
slouken@6145
   618
jorgen@7279
   619
    /* We ignore events if we don't have keyboard focus, except for centering
jorgen@7279
   620
     * events.
jorgen@7279
   621
     */
jorgen@7279
   622
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
slouken@8903
   623
        if (value != SDL_HAT_CENTERED) {
jorgen@7279
   624
            return 0;
jorgen@7279
   625
        }
jorgen@7279
   626
    }
jorgen@7279
   627
slouken@8903
   628
    /* Update internal joystick state */
slouken@8903
   629
    joystick->hats[hat] = value;
jorgen@7279
   630
slouken@1895
   631
    /* Post the event, if desired */
slouken@1895
   632
    posted = 0;
slouken@1361
   633
#if !SDL_EVENTS_DISABLED
slouken@4429
   634
    if (SDL_GetEventState(SDL_JOYHATMOTION) == SDL_ENABLE) {
slouken@1895
   635
        SDL_Event event;
slouken@1895
   636
        event.jhat.type = SDL_JOYHATMOTION;
slouken@6690
   637
        event.jhat.which = joystick->instance_id;
slouken@1895
   638
        event.jhat.hat = hat;
slouken@1895
   639
        event.jhat.value = value;
slouken@6690
   640
        posted = SDL_PushEvent(&event) == 1;
slouken@1895
   641
    }
slouken@1361
   642
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   643
    return (posted);
slouken@0
   644
}
slouken@0
   645
slouken@1895
   646
int
slouken@1895
   647
SDL_PrivateJoystickBall(SDL_Joystick * joystick, Uint8 ball,
slouken@1895
   648
                        Sint16 xrel, Sint16 yrel)
slouken@0
   649
{
slouken@1895
   650
    int posted;
slouken@0
   651
slouken@6145
   652
    /* Make sure we're not getting garbage events */
slouken@6145
   653
    if (ball >= joystick->nballs) {
slouken@6145
   654
        return 0;
slouken@6145
   655
    }
slouken@6145
   656
jorgen@7279
   657
    /* We ignore events if we don't have keyboard focus. */
jorgen@7279
   658
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
jorgen@7279
   659
        return 0;
jorgen@7279
   660
    }
jorgen@7279
   661
slouken@1895
   662
    /* Update internal mouse state */
slouken@1895
   663
    joystick->balls[ball].dx += xrel;
slouken@1895
   664
    joystick->balls[ball].dy += yrel;
slouken@0
   665
slouken@1895
   666
    /* Post the event, if desired */
slouken@1895
   667
    posted = 0;
slouken@1361
   668
#if !SDL_EVENTS_DISABLED
slouken@4429
   669
    if (SDL_GetEventState(SDL_JOYBALLMOTION) == SDL_ENABLE) {
slouken@1895
   670
        SDL_Event event;
slouken@1895
   671
        event.jball.type = SDL_JOYBALLMOTION;
slouken@6690
   672
        event.jball.which = joystick->instance_id;
slouken@1895
   673
        event.jball.ball = ball;
slouken@1895
   674
        event.jball.xrel = xrel;
slouken@1895
   675
        event.jball.yrel = yrel;
slouken@6690
   676
        posted = SDL_PushEvent(&event) == 1;
slouken@1895
   677
    }
slouken@1361
   678
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   679
    return (posted);
slouken@0
   680
}
slouken@0
   681
slouken@1895
   682
int
slouken@1895
   683
SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
slouken@0
   684
{
slouken@1895
   685
    int posted;
slouken@1361
   686
#if !SDL_EVENTS_DISABLED
slouken@1895
   687
    SDL_Event event;
slouken@0
   688
slouken@1895
   689
    switch (state) {
slouken@1895
   690
    case SDL_PRESSED:
slouken@1895
   691
        event.type = SDL_JOYBUTTONDOWN;
slouken@1895
   692
        break;
slouken@1895
   693
    case SDL_RELEASED:
slouken@1895
   694
        event.type = SDL_JOYBUTTONUP;
slouken@1895
   695
        break;
slouken@1895
   696
    default:
slouken@1895
   697
        /* Invalid state -- bail */
slouken@1895
   698
        return (0);
slouken@1895
   699
    }
slouken@1361
   700
#endif /* !SDL_EVENTS_DISABLED */
slouken@0
   701
slouken@8920
   702
    /* Make sure we're not getting garbage or duplicate events */
slouken@6145
   703
    if (button >= joystick->nbuttons) {
slouken@6145
   704
        return 0;
slouken@9884
   705
    }
slouken@9884
   706
    if (state == joystick->buttons[button]) {
slouken@9884
   707
        return 0;
slouken@9884
   708
    }
slouken@6145
   709
jorgen@7279
   710
    /* We ignore events if we don't have keyboard focus, except for button
jorgen@7279
   711
     * release. */
slouken@8903
   712
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
slouken@8903
   713
        if (state == SDL_PRESSED) {
slouken@8903
   714
            return 0;
slouken@8903
   715
        }
jorgen@7279
   716
    }
jorgen@7279
   717
slouken@1895
   718
    /* Update internal joystick state */
slouken@1895
   719
    joystick->buttons[button] = state;
slouken@0
   720
slouken@1895
   721
    /* Post the event, if desired */
slouken@1895
   722
    posted = 0;
slouken@1361
   723
#if !SDL_EVENTS_DISABLED
slouken@4429
   724
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@6690
   725
        event.jbutton.which = joystick->instance_id;
slouken@1895
   726
        event.jbutton.button = button;
slouken@1895
   727
        event.jbutton.state = state;
slouken@6690
   728
        posted = SDL_PushEvent(&event) == 1;
slouken@1895
   729
    }
slouken@1361
   730
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   731
    return (posted);
slouken@0
   732
}
slouken@0
   733
slouken@1895
   734
void
slouken@1895
   735
SDL_JoystickUpdate(void)
slouken@0
   736
{
slouken@7191
   737
    SDL_Joystick *joystick;
slouken@7191
   738
slouken@7191
   739
    joystick = SDL_joysticks;
slouken@8920
   740
    while (joystick) {
slouken@7191
   741
        SDL_Joystick *joysticknext;
slouken@7191
   742
        /* save off the next pointer, the Update call may cause a joystick removed event
slouken@7191
   743
         * and cause our joystick pointer to be freed
slouken@7191
   744
         */
slouken@7191
   745
        joysticknext = joystick->next;
slouken@0
   746
slouken@6712
   747
        SDL_updating_joystick = joystick;
slouken@6712
   748
slouken@8920
   749
        SDL_SYS_JoystickUpdate(joystick);
slouken@6690
   750
icculus@9433
   751
        if (joystick->force_recentering) {
slouken@7191
   752
            int i;
slouken@6690
   753
slouken@6712
   754
            /* Tell the app that everything is centered/unpressed...  */
slime73@9876
   755
            for (i = 0; i < joystick->naxes; i++) {
slouken@6690
   756
                SDL_PrivateJoystickAxis(joystick, i, 0);
slime73@9876
   757
            }
slouken@6690
   758
slime73@9876
   759
            for (i = 0; i < joystick->nbuttons; i++) {
slouken@6690
   760
                SDL_PrivateJoystickButton(joystick, i, 0);
slime73@9876
   761
            }
slouken@6690
   762
slime73@9876
   763
            for (i = 0; i < joystick->nhats; i++) {
slouken@6690
   764
                SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
slime73@9876
   765
            }
slouken@6712
   766
icculus@9433
   767
            joystick->force_recentering = SDL_FALSE;
slouken@7191
   768
        }
slouken@6690
   769
slouken@6712
   770
        SDL_updating_joystick = NULL;
slouken@6712
   771
slouken@6712
   772
        /* If the joystick was closed while updating, free it here */
slouken@8920
   773
        if (joystick->ref_count <= 0) {
slouken@6712
   774
            SDL_JoystickClose(joystick);
slouken@6712
   775
        }
slouken@6712
   776
slouken@7191
   777
        joystick = joysticknext;
slouken@7191
   778
    }
slouken@6690
   779
slouken@7191
   780
    /* this needs to happen AFTER walking the joystick list above, so that any
slouken@7191
   781
       dangling hardware data from removed devices can be free'd
slouken@7191
   782
     */
slouken@7191
   783
    SDL_SYS_JoystickDetect();
slouken@0
   784
}
slouken@0
   785
slouken@1895
   786
int
slouken@1895
   787
SDL_JoystickEventState(int state)
slouken@0
   788
{
slouken@1361
   789
#if SDL_EVENTS_DISABLED
slouken@6753
   790
    return SDL_DISABLE;
slouken@0
   791
#else
slouken@4429
   792
    const Uint32 event_list[] = {
slouken@1895
   793
        SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION,
slouken@6690
   794
        SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED
slouken@1895
   795
    };
slouken@1895
   796
    unsigned int i;
slouken@0
   797
slouken@1895
   798
    switch (state) {
slouken@1895
   799
    case SDL_QUERY:
slouken@6753
   800
        state = SDL_DISABLE;
slouken@1895
   801
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@1895
   802
            state = SDL_EventState(event_list[i], SDL_QUERY);
slouken@1895
   803
            if (state == SDL_ENABLE) {
slouken@1895
   804
                break;
slouken@1895
   805
            }
slouken@1895
   806
        }
slouken@1895
   807
        break;
slouken@1895
   808
    default:
slouken@1895
   809
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@1895
   810
            SDL_EventState(event_list[i], state);
slouken@1895
   811
        }
slouken@1895
   812
        break;
slouken@1895
   813
    }
slouken@1895
   814
    return (state);
slouken@1361
   815
#endif /* SDL_EVENTS_DISABLED */
slouken@0
   816
}
slouken@1895
   817
slouken@6693
   818
/* return the guid for this index */
slouken@6738
   819
SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
slouken@6690
   820
{
slouken@7294
   821
    if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
slouken@7294
   822
        SDL_JoystickGUID emptyGUID;
slouken@7294
   823
        SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
slouken@8920
   824
        SDL_zero(emptyGUID);
slouken@7294
   825
        return emptyGUID;
slouken@7294
   826
    }
slouken@8920
   827
    return SDL_SYS_JoystickGetDeviceGUID(device_index);
slouken@6690
   828
}
slouken@6690
   829
slouken@6693
   830
/* return the guid for this opened device */
slouken@6738
   831
SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick * joystick)
slouken@6690
   832
{
slouken@7789
   833
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@7789
   834
        SDL_JoystickGUID emptyGUID;
slouken@8920
   835
        SDL_zero(emptyGUID);
slouken@7789
   836
        return emptyGUID;
slouken@7789
   837
    }
slouken@8920
   838
    return SDL_SYS_JoystickGetGUID(joystick);
slouken@6690
   839
}
slouken@6690
   840
slouken@6690
   841
/* convert the guid to a printable string */
slouken@8920
   842
void SDL_JoystickGetGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID)
slouken@6690
   843
{
slouken@7191
   844
    static const char k_rgchHexToASCII[] = "0123456789abcdef";
slouken@7191
   845
    int i;
slouken@6690
   846
icculus@6748
   847
    if ((pszGUID == NULL) || (cbGUID <= 0)) {
icculus@6748
   848
        return;
icculus@6748
   849
    }
icculus@6748
   850
slouken@8920
   851
    for (i = 0; i < sizeof(guid.data) && i < (cbGUID-1)/2; i++) {
slouken@7191
   852
        /* each input byte writes 2 ascii chars, and might write a null byte. */
slouken@7191
   853
        /* If we don't have room for next input byte, stop */
slouken@7191
   854
        unsigned char c = guid.data[i];
slouken@6690
   855
slouken@8920
   856
        *pszGUID++ = k_rgchHexToASCII[c >> 4];
slouken@8920
   857
        *pszGUID++ = k_rgchHexToASCII[c & 0x0F];
slouken@7191
   858
    }
slouken@7191
   859
    *pszGUID = '\0';
slouken@6690
   860
}
slouken@6690
   861
slouken@6690
   862
slouken@7191
   863
/*-----------------------------------------------------------------------------
slouken@7191
   864
 * Purpose: Returns the 4 bit nibble for a hex character
slouken@7191
   865
 * Input  : c -
slouken@7191
   866
 * Output : unsigned char
slouken@7191
   867
 *-----------------------------------------------------------------------------*/
slouken@8920
   868
static unsigned char nibble(char c)
slouken@6690
   869
{
slouken@8920
   870
    if ((c >= '0') && (c <= '9')) {
slouken@7191
   871
        return (unsigned char)(c - '0');
slouken@7191
   872
    }
slouken@6690
   873
slouken@8920
   874
    if ((c >= 'A') && (c <= 'F')) {
slouken@7191
   875
        return (unsigned char)(c - 'A' + 0x0a);
slouken@7191
   876
    }
slouken@6690
   877
slouken@8920
   878
    if ((c >= 'a') && (c <= 'f')) {
slouken@7191
   879
        return (unsigned char)(c - 'a' + 0x0a);
slouken@7191
   880
    }
slouken@6690
   881
slouken@7191
   882
    /* received an invalid character, and no real way to return an error */
slouken@8920
   883
    /* AssertMsg1(false, "Q_nibble invalid hex character '%c' ", c); */
slouken@7191
   884
    return 0;
slouken@6690
   885
}
slouken@6690
   886
slouken@6690
   887
slouken@6690
   888
/* convert the string version of a joystick guid to the struct */
slouken@6738
   889
SDL_JoystickGUID SDL_JoystickGetGUIDFromString(const char *pchGUID)
slouken@6690
   890
{
slouken@7191
   891
    SDL_JoystickGUID guid;
slouken@7191
   892
    int maxoutputbytes= sizeof(guid);
slouken@8920
   893
    size_t len = SDL_strlen(pchGUID);
slouken@7191
   894
    Uint8 *p;
slouken@7922
   895
    size_t i;
slouken@6690
   896
slouken@7191
   897
    /* Make sure it's even */
slouken@8920
   898
    len = (len) & ~0x1;
slouken@6690
   899
slouken@8920
   900
    SDL_memset(&guid, 0x00, sizeof(guid));
slouken@6690
   901
slouken@7191
   902
    p = (Uint8 *)&guid;
slouken@8920
   903
    for (i = 0; (i < len) && ((p - (Uint8 *)&guid) < maxoutputbytes); i+=2, p++) {
slouken@8920
   904
        *p = (nibble(pchGUID[i]) << 4) | nibble(pchGUID[i+1]);
slouken@7191
   905
    }
slouken@6690
   906
slouken@7191
   907
    return guid;
slouken@6690
   908
}
slouken@6690
   909
slouken@6690
   910
slouken@9884
   911
/* update the power level for this joystick */
slouken@9884
   912
void SDL_PrivateJoystickBatteryLevel(SDL_Joystick * joystick, SDL_JoystickPowerLevel ePowerLevel)
slouken@9884
   913
{
slouken@9884
   914
    joystick->epowerlevel = ePowerLevel;
slouken@9884
   915
}
slouken@9884
   916
slouken@9884
   917
slouken@9884
   918
/* return its power level */
slouken@9884
   919
SDL_JoystickPowerLevel SDL_JoystickCurrentPowerLevel(SDL_Joystick * joystick)
slouken@9884
   920
{
slouken@9884
   921
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@9884
   922
        return (SDL_JOYSTICK_POWER_UNKNOWN);
slouken@9884
   923
    }
slouken@9884
   924
    return joystick->epowerlevel;
slouken@9884
   925
}
slouken@9884
   926
slouken@9884
   927
slouken@1895
   928
/* vi: set ts=4 sw=4 expandtab: */