src/joystick/SDL_joystick.c
author Alex Szpakowski <slime73@gmail.com>
Sun, 20 Sep 2015 23:08:36 -0300
changeset 9876 1496e502e51d
parent 9619 b94b6d0bff0f
child 9884 26b595dea221
permissions -rw-r--r--
Added initial support for MFi game controllers on iOS.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9619
     3
  Copyright (C) 1997-2015 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
    }
icculus@3608
   181
icculus@3608
   182
    /* Add joystick to list */
icculus@3608
   183
    ++joystick->ref_count;
slouken@7191
   184
    /* Link the joystick in the list */
slouken@7191
   185
    joystick->next = SDL_joysticks;
slouken@7191
   186
    SDL_joysticks = joystick;
slouken@6690
   187
slouken@8920
   188
    SDL_SYS_JoystickUpdate(joystick);
icculus@3608
   189
slouken@1895
   190
    return (joystick);
slouken@0
   191
}
slouken@0
   192
slouken@2713
   193
slouken@2713
   194
/*
slouken@2713
   195
 * Checks to make sure the joystick is valid.
slouken@2713
   196
 */
slouken@2713
   197
int
slouken@6690
   198
SDL_PrivateJoystickValid(SDL_Joystick * joystick)
slouken@0
   199
{
slouken@1895
   200
    int valid;
slouken@0
   201
slouken@8920
   202
    if (joystick == NULL) {
slouken@1895
   203
        SDL_SetError("Joystick hasn't been opened yet");
slouken@1895
   204
        valid = 0;
slouken@1895
   205
    } else {
slouken@1895
   206
        valid = 1;
slouken@1895
   207
    }
slouken@7191
   208
slouken@1895
   209
    return valid;
slouken@0
   210
}
slouken@0
   211
slouken@0
   212
/*
slouken@0
   213
 * Get the number of multi-dimensional axis controls on a joystick
slouken@0
   214
 */
slouken@1895
   215
int
slouken@1895
   216
SDL_JoystickNumAxes(SDL_Joystick * joystick)
slouken@0
   217
{
slouken@6690
   218
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@1895
   219
        return (-1);
slouken@1895
   220
    }
slouken@1895
   221
    return (joystick->naxes);
slouken@0
   222
}
slouken@0
   223
slouken@0
   224
/*
slouken@0
   225
 * Get the number of hats on a joystick
slouken@0
   226
 */
slouken@1895
   227
int
slouken@1895
   228
SDL_JoystickNumHats(SDL_Joystick * joystick)
slouken@0
   229
{
slouken@6690
   230
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@1895
   231
        return (-1);
slouken@1895
   232
    }
slouken@1895
   233
    return (joystick->nhats);
slouken@0
   234
}
slouken@0
   235
slouken@0
   236
/*
slouken@0
   237
 * Get the number of trackballs on a joystick
slouken@0
   238
 */
slouken@1895
   239
int
slouken@1895
   240
SDL_JoystickNumBalls(SDL_Joystick * joystick)
slouken@0
   241
{
slouken@6690
   242
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@1895
   243
        return (-1);
slouken@1895
   244
    }
slouken@1895
   245
    return (joystick->nballs);
slouken@0
   246
}
slouken@0
   247
slouken@0
   248
/*
slouken@0
   249
 * Get the number of buttons on a joystick
slouken@0
   250
 */
slouken@1895
   251
int
slouken@1895
   252
SDL_JoystickNumButtons(SDL_Joystick * joystick)
slouken@0
   253
{
slouken@6690
   254
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@1895
   255
        return (-1);
slouken@1895
   256
    }
slouken@1895
   257
    return (joystick->nbuttons);
slouken@0
   258
}
slouken@0
   259
slouken@0
   260
/*
slouken@0
   261
 * Get the current state of an axis control on a joystick
slouken@0
   262
 */
slouken@1895
   263
Sint16
slouken@1895
   264
SDL_JoystickGetAxis(SDL_Joystick * joystick, int axis)
slouken@0
   265
{
slouken@1895
   266
    Sint16 state;
slouken@0
   267
slouken@6690
   268
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@1895
   269
        return (0);
slouken@1895
   270
    }
slouken@1895
   271
    if (axis < joystick->naxes) {
slouken@1895
   272
        state = joystick->axes[axis];
slouken@1895
   273
    } else {
slouken@1895
   274
        SDL_SetError("Joystick only has %d axes", joystick->naxes);
slouken@1895
   275
        state = 0;
slouken@1895
   276
    }
slouken@1895
   277
    return (state);
slouken@0
   278
}
slouken@0
   279
slouken@0
   280
/*
slouken@0
   281
 * Get the current state of a hat on a joystick
slouken@0
   282
 */
slouken@1895
   283
Uint8
slouken@1895
   284
SDL_JoystickGetHat(SDL_Joystick * joystick, int hat)
slouken@0
   285
{
slouken@1895
   286
    Uint8 state;
slouken@0
   287
slouken@6690
   288
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@1895
   289
        return (0);
slouken@1895
   290
    }
slouken@1895
   291
    if (hat < joystick->nhats) {
slouken@1895
   292
        state = joystick->hats[hat];
slouken@1895
   293
    } else {
slouken@1895
   294
        SDL_SetError("Joystick only has %d hats", joystick->nhats);
slouken@1895
   295
        state = 0;
slouken@1895
   296
    }
slouken@1895
   297
    return (state);
slouken@0
   298
}
slouken@0
   299
slouken@0
   300
/*
slouken@0
   301
 * Get the ball axis change since the last poll
slouken@0
   302
 */
slouken@1895
   303
int
slouken@1895
   304
SDL_JoystickGetBall(SDL_Joystick * joystick, int ball, int *dx, int *dy)
slouken@0
   305
{
slouken@1895
   306
    int retval;
slouken@0
   307
slouken@6690
   308
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@1895
   309
        return (-1);
slouken@1895
   310
    }
slouken@0
   311
slouken@1895
   312
    retval = 0;
slouken@1895
   313
    if (ball < joystick->nballs) {
slouken@1895
   314
        if (dx) {
slouken@1895
   315
            *dx = joystick->balls[ball].dx;
slouken@1895
   316
        }
slouken@1895
   317
        if (dy) {
slouken@1895
   318
            *dy = joystick->balls[ball].dy;
slouken@1895
   319
        }
slouken@1895
   320
        joystick->balls[ball].dx = 0;
slouken@1895
   321
        joystick->balls[ball].dy = 0;
slouken@1895
   322
    } else {
icculus@7037
   323
        return SDL_SetError("Joystick only has %d balls", joystick->nballs);
slouken@1895
   324
    }
slouken@1895
   325
    return (retval);
slouken@0
   326
}
slouken@0
   327
slouken@0
   328
/*
slouken@0
   329
 * Get the current state of a button on a joystick
slouken@0
   330
 */
slouken@1895
   331
Uint8
slouken@1895
   332
SDL_JoystickGetButton(SDL_Joystick * joystick, int button)
slouken@0
   333
{
slouken@1895
   334
    Uint8 state;
slouken@0
   335
slouken@6690
   336
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@1895
   337
        return (0);
slouken@1895
   338
    }
slouken@1895
   339
    if (button < joystick->nbuttons) {
slouken@1895
   340
        state = joystick->buttons[button];
slouken@1895
   341
    } else {
slouken@1895
   342
        SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
slouken@1895
   343
        state = 0;
slouken@1895
   344
    }
slouken@1895
   345
    return (state);
slouken@0
   346
}
slouken@0
   347
slouken@0
   348
/*
slouken@6690
   349
 * Return if the joystick in question is currently attached to the system,
philipp@7500
   350
 *  \return SDL_FALSE if not plugged in, SDL_TRUE if still present.
slouken@6690
   351
 */
slouken@6707
   352
SDL_bool
slouken@6707
   353
SDL_JoystickGetAttached(SDL_Joystick * joystick)
slouken@6690
   354
{
slouken@7191
   355
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@6707
   356
        return SDL_FALSE;
slouken@6690
   357
    }
slouken@6690
   358
slouken@7191
   359
    return SDL_SYS_JoystickAttached(joystick);
slouken@6690
   360
}
slouken@6690
   361
slouken@6690
   362
/*
slouken@6690
   363
 * Get the instance id for this opened joystick
slouken@6690
   364
 */
slouken@7191
   365
SDL_JoystickID
slouken@6707
   366
SDL_JoystickInstanceID(SDL_Joystick * joystick)
slouken@6690
   367
{
slouken@7191
   368
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@6690
   369
        return (-1);
slouken@6690
   370
    }
slouken@6690
   371
slouken@7191
   372
    return (joystick->instance_id);
slouken@6690
   373
}
slouken@6690
   374
slouken@6690
   375
/*
slouken@6690
   376
 * Get the friendly name of this joystick
slouken@6690
   377
 */
slouken@6690
   378
const char *
slouken@6690
   379
SDL_JoystickName(SDL_Joystick * joystick)
slouken@6690
   380
{
slouken@6690
   381
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@6690
   382
        return (NULL);
slouken@6690
   383
    }
slouken@7191
   384
slouken@6690
   385
    return (joystick->name);
slouken@6690
   386
}
slouken@6690
   387
slouken@6690
   388
/*
slouken@0
   389
 * Close a joystick previously opened with SDL_JoystickOpen()
slouken@0
   390
 */
slouken@1895
   391
void
slouken@1895
   392
SDL_JoystickClose(SDL_Joystick * joystick)
slouken@0
   393
{
slouken@7191
   394
    SDL_Joystick *joysticklist;
slouken@7191
   395
    SDL_Joystick *joysticklistprev;
slouken@0
   396
slouken@6690
   397
    if (!joystick) {
slouken@1895
   398
        return;
slouken@1895
   399
    }
slouken@0
   400
slouken@1895
   401
    /* First decrement ref count */
slouken@1895
   402
    if (--joystick->ref_count > 0) {
slouken@1895
   403
        return;
slouken@1895
   404
    }
slouken@0
   405
slouken@6712
   406
    if (joystick == SDL_updating_joystick) {
slouken@6712
   407
        return;
slouken@6712
   408
    }
slouken@6712
   409
slouken@1895
   410
    SDL_SYS_JoystickClose(joystick);
icculus@9433
   411
    joystick->hwdata = NULL;
slouken@0
   412
slouken@7191
   413
    joysticklist = SDL_joysticks;
slouken@7191
   414
    joysticklistprev = NULL;
slouken@8920
   415
    while (joysticklist) {
slouken@8920
   416
        if (joystick == joysticklist) {
slouken@8920
   417
            if (joysticklistprev) {
slouken@7191
   418
                /* unlink this entry */
slouken@7191
   419
                joysticklistprev->next = joysticklist->next;
slouken@8920
   420
            } else {
slouken@7191
   421
                SDL_joysticks = joystick->next;
slouken@7191
   422
            }
slouken@7191
   423
            break;
slouken@7191
   424
        }
slouken@7191
   425
        joysticklistprev = joysticklist;
slouken@7191
   426
        joysticklist = joysticklist->next;
slouken@7191
   427
    }
slouken@7191
   428
slouken@7719
   429
    SDL_free(joystick->name);
slouken@0
   430
slouken@1895
   431
    /* Free the data associated with this joystick */
slouken@7719
   432
    SDL_free(joystick->axes);
slouken@7719
   433
    SDL_free(joystick->hats);
slouken@7719
   434
    SDL_free(joystick->balls);
slouken@7719
   435
    SDL_free(joystick->buttons);
slouken@1895
   436
    SDL_free(joystick);
slouken@0
   437
}
slouken@0
   438
slouken@1895
   439
void
slouken@1895
   440
SDL_JoystickQuit(void)
slouken@0
   441
{
slouken@6712
   442
    /* Make sure we're not getting called in the middle of updating joysticks */
slouken@6712
   443
    SDL_assert(!SDL_updating_joystick);
slouken@6712
   444
slouken@1895
   445
    /* Stop the event polling */
slouken@8920
   446
    while (SDL_joysticks) {
slouken@7191
   447
        SDL_joysticks->ref_count = 1;
slouken@6690
   448
        SDL_JoystickClose(SDL_joysticks);
slouken@7191
   449
    }
icculus@5856
   450
slouken@1895
   451
    /* Quit the joystick setup */
slouken@1895
   452
    SDL_SYS_JoystickQuit();
slouken@7360
   453
slouken@7360
   454
#if !SDL_EVENTS_DISABLED
slouken@7360
   455
    SDL_QuitSubSystem(SDL_INIT_EVENTS);
slouken@7360
   456
#endif
slouken@0
   457
}
slouken@0
   458
slouken@0
   459
jorgen@7279
   460
static SDL_bool
jorgen@7279
   461
SDL_PrivateJoystickShouldIgnoreEvent()
jorgen@7279
   462
{
slouken@8920
   463
    if (SDL_joystick_allows_background_events) {
jorgen@7279
   464
        return SDL_FALSE;
jorgen@7279
   465
    }
jorgen@7279
   466
slouken@7337
   467
    if (SDL_WasInit(SDL_INIT_VIDEO)) {
slouken@7337
   468
        if (SDL_GetKeyboardFocus() == NULL) {
gabomdq@7677
   469
            /* Video is initialized and we don't have focus, ignore the event. */
slouken@7337
   470
            return SDL_TRUE;
slouken@7337
   471
        } else {
slouken@7337
   472
            return SDL_FALSE;
slouken@7337
   473
        }
jorgen@7279
   474
    }
jorgen@7279
   475
gabomdq@7677
   476
    /* Video subsystem wasn't initialized, always allow the event */
slouken@7337
   477
    return SDL_FALSE;
jorgen@7279
   478
}
jorgen@7279
   479
slouken@0
   480
/* These are global for SDL_sysjoystick.c and SDL_events.c */
slouken@0
   481
slouken@1895
   482
int
slouken@1895
   483
SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
slouken@0
   484
{
slouken@1895
   485
    int posted;
slouken@0
   486
slouken@8920
   487
    /* Make sure we're not getting garbage or duplicate events */
slouken@6145
   488
    if (axis >= joystick->naxes) {
slouken@6145
   489
        return 0;
slouken@6145
   490
    }
slouken@8920
   491
    if (value == joystick->axes[axis]) {
slouken@8920
   492
        return 0;
slouken@8920
   493
    }
slouken@6145
   494
slouken@8903
   495
    /* We ignore events if we don't have keyboard focus, except for centering
slouken@8903
   496
     * events.
slouken@8903
   497
     */
slouken@8903
   498
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
slouken@8903
   499
        if ((value > 0 && value >= joystick->axes[axis]) ||
slouken@8903
   500
            (value < 0 && value <= joystick->axes[axis])) {
slouken@8903
   501
            return 0;
slouken@8903
   502
        }
slouken@8903
   503
    }
slouken@8903
   504
slouken@1895
   505
    /* Update internal joystick state */
slouken@1895
   506
    joystick->axes[axis] = value;
slouken@0
   507
slouken@1895
   508
    /* Post the event, if desired */
slouken@1895
   509
    posted = 0;
slouken@1361
   510
#if !SDL_EVENTS_DISABLED
slouken@4429
   511
    if (SDL_GetEventState(SDL_JOYAXISMOTION) == SDL_ENABLE) {
slouken@1895
   512
        SDL_Event event;
slouken@1895
   513
        event.type = SDL_JOYAXISMOTION;
slouken@6690
   514
        event.jaxis.which = joystick->instance_id;
slouken@1895
   515
        event.jaxis.axis = axis;
slouken@1895
   516
        event.jaxis.value = value;
slouken@6690
   517
        posted = SDL_PushEvent(&event) == 1;
slouken@1895
   518
    }
slouken@1361
   519
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   520
    return (posted);
slouken@0
   521
}
slouken@0
   522
slouken@1895
   523
int
slouken@1895
   524
SDL_PrivateJoystickHat(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
slouken@0
   525
{
slouken@1895
   526
    int posted;
slouken@0
   527
slouken@8920
   528
    /* Make sure we're not getting garbage or duplicate events */
slouken@6145
   529
    if (hat >= joystick->nhats) {
slouken@6145
   530
        return 0;
slouken@6145
   531
    }
slouken@8920
   532
    if (value == joystick->hats[hat]) {
slouken@8920
   533
        return 0;
slouken@8920
   534
    }
slouken@6145
   535
jorgen@7279
   536
    /* We ignore events if we don't have keyboard focus, except for centering
jorgen@7279
   537
     * events.
jorgen@7279
   538
     */
jorgen@7279
   539
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
slouken@8903
   540
        if (value != SDL_HAT_CENTERED) {
jorgen@7279
   541
            return 0;
jorgen@7279
   542
        }
jorgen@7279
   543
    }
jorgen@7279
   544
slouken@8903
   545
    /* Update internal joystick state */
slouken@8903
   546
    joystick->hats[hat] = value;
jorgen@7279
   547
slouken@1895
   548
    /* Post the event, if desired */
slouken@1895
   549
    posted = 0;
slouken@1361
   550
#if !SDL_EVENTS_DISABLED
slouken@4429
   551
    if (SDL_GetEventState(SDL_JOYHATMOTION) == SDL_ENABLE) {
slouken@1895
   552
        SDL_Event event;
slouken@1895
   553
        event.jhat.type = SDL_JOYHATMOTION;
slouken@6690
   554
        event.jhat.which = joystick->instance_id;
slouken@1895
   555
        event.jhat.hat = hat;
slouken@1895
   556
        event.jhat.value = value;
slouken@6690
   557
        posted = SDL_PushEvent(&event) == 1;
slouken@1895
   558
    }
slouken@1361
   559
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   560
    return (posted);
slouken@0
   561
}
slouken@0
   562
slouken@1895
   563
int
slouken@1895
   564
SDL_PrivateJoystickBall(SDL_Joystick * joystick, Uint8 ball,
slouken@1895
   565
                        Sint16 xrel, Sint16 yrel)
slouken@0
   566
{
slouken@1895
   567
    int posted;
slouken@0
   568
slouken@6145
   569
    /* Make sure we're not getting garbage events */
slouken@6145
   570
    if (ball >= joystick->nballs) {
slouken@6145
   571
        return 0;
slouken@6145
   572
    }
slouken@6145
   573
jorgen@7279
   574
    /* We ignore events if we don't have keyboard focus. */
jorgen@7279
   575
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
jorgen@7279
   576
        return 0;
jorgen@7279
   577
    }
jorgen@7279
   578
slouken@1895
   579
    /* Update internal mouse state */
slouken@1895
   580
    joystick->balls[ball].dx += xrel;
slouken@1895
   581
    joystick->balls[ball].dy += yrel;
slouken@0
   582
slouken@1895
   583
    /* Post the event, if desired */
slouken@1895
   584
    posted = 0;
slouken@1361
   585
#if !SDL_EVENTS_DISABLED
slouken@4429
   586
    if (SDL_GetEventState(SDL_JOYBALLMOTION) == SDL_ENABLE) {
slouken@1895
   587
        SDL_Event event;
slouken@1895
   588
        event.jball.type = SDL_JOYBALLMOTION;
slouken@6690
   589
        event.jball.which = joystick->instance_id;
slouken@1895
   590
        event.jball.ball = ball;
slouken@1895
   591
        event.jball.xrel = xrel;
slouken@1895
   592
        event.jball.yrel = yrel;
slouken@6690
   593
        posted = SDL_PushEvent(&event) == 1;
slouken@1895
   594
    }
slouken@1361
   595
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   596
    return (posted);
slouken@0
   597
}
slouken@0
   598
slouken@1895
   599
int
slouken@1895
   600
SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
slouken@0
   601
{
slouken@1895
   602
    int posted;
slouken@1361
   603
#if !SDL_EVENTS_DISABLED
slouken@1895
   604
    SDL_Event event;
slouken@0
   605
slouken@1895
   606
    switch (state) {
slouken@1895
   607
    case SDL_PRESSED:
slouken@1895
   608
        event.type = SDL_JOYBUTTONDOWN;
slouken@1895
   609
        break;
slouken@1895
   610
    case SDL_RELEASED:
slouken@1895
   611
        event.type = SDL_JOYBUTTONUP;
slouken@1895
   612
        break;
slouken@1895
   613
    default:
slouken@1895
   614
        /* Invalid state -- bail */
slouken@1895
   615
        return (0);
slouken@1895
   616
    }
slouken@1361
   617
#endif /* !SDL_EVENTS_DISABLED */
slouken@0
   618
slouken@8920
   619
    /* Make sure we're not getting garbage or duplicate events */
slouken@6145
   620
    if (button >= joystick->nbuttons) {
slouken@6145
   621
        return 0;
slouken@8920
   622
	}
slouken@8920
   623
	if (state == joystick->buttons[button]) {
slouken@8920
   624
		return 0;
slouken@8920
   625
	}
slouken@6145
   626
jorgen@7279
   627
    /* We ignore events if we don't have keyboard focus, except for button
jorgen@7279
   628
     * release. */
slouken@8903
   629
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
slouken@8903
   630
        if (state == SDL_PRESSED) {
slouken@8903
   631
            return 0;
slouken@8903
   632
        }
jorgen@7279
   633
    }
jorgen@7279
   634
slouken@1895
   635
    /* Update internal joystick state */
slouken@1895
   636
    joystick->buttons[button] = state;
slouken@0
   637
slouken@1895
   638
    /* Post the event, if desired */
slouken@1895
   639
    posted = 0;
slouken@1361
   640
#if !SDL_EVENTS_DISABLED
slouken@4429
   641
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@6690
   642
        event.jbutton.which = joystick->instance_id;
slouken@1895
   643
        event.jbutton.button = button;
slouken@1895
   644
        event.jbutton.state = state;
slouken@6690
   645
        posted = SDL_PushEvent(&event) == 1;
slouken@1895
   646
    }
slouken@1361
   647
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   648
    return (posted);
slouken@0
   649
}
slouken@0
   650
slouken@1895
   651
void
slouken@1895
   652
SDL_JoystickUpdate(void)
slouken@0
   653
{
slouken@7191
   654
    SDL_Joystick *joystick;
slouken@7191
   655
slouken@7191
   656
    joystick = SDL_joysticks;
slouken@8920
   657
    while (joystick) {
slouken@7191
   658
        SDL_Joystick *joysticknext;
slouken@7191
   659
        /* save off the next pointer, the Update call may cause a joystick removed event
slouken@7191
   660
         * and cause our joystick pointer to be freed
slouken@7191
   661
         */
slouken@7191
   662
        joysticknext = joystick->next;
slouken@0
   663
slouken@6712
   664
        SDL_updating_joystick = joystick;
slouken@6712
   665
slouken@8920
   666
        SDL_SYS_JoystickUpdate(joystick);
slouken@6690
   667
icculus@9433
   668
        if (joystick->force_recentering) {
slouken@7191
   669
            int i;
slouken@6690
   670
slouken@6712
   671
            /* Tell the app that everything is centered/unpressed...  */
slime73@9876
   672
            for (i = 0; i < joystick->naxes; i++) {
slouken@6690
   673
                SDL_PrivateJoystickAxis(joystick, i, 0);
slime73@9876
   674
            }
slouken@6690
   675
slime73@9876
   676
            for (i = 0; i < joystick->nbuttons; i++) {
slouken@6690
   677
                SDL_PrivateJoystickButton(joystick, i, 0);
slime73@9876
   678
            }
slouken@6690
   679
slime73@9876
   680
            for (i = 0; i < joystick->nhats; i++) {
slouken@6690
   681
                SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
slime73@9876
   682
            }
slouken@6712
   683
icculus@9433
   684
            joystick->force_recentering = SDL_FALSE;
slouken@7191
   685
        }
slouken@6690
   686
slouken@6712
   687
        SDL_updating_joystick = NULL;
slouken@6712
   688
slouken@6712
   689
        /* If the joystick was closed while updating, free it here */
slouken@8920
   690
        if (joystick->ref_count <= 0) {
slouken@6712
   691
            SDL_JoystickClose(joystick);
slouken@6712
   692
        }
slouken@6712
   693
slouken@7191
   694
        joystick = joysticknext;
slouken@7191
   695
    }
slouken@6690
   696
slouken@7191
   697
    /* this needs to happen AFTER walking the joystick list above, so that any
slouken@7191
   698
       dangling hardware data from removed devices can be free'd
slouken@7191
   699
     */
slouken@7191
   700
    SDL_SYS_JoystickDetect();
slouken@0
   701
}
slouken@0
   702
slouken@1895
   703
int
slouken@1895
   704
SDL_JoystickEventState(int state)
slouken@0
   705
{
slouken@1361
   706
#if SDL_EVENTS_DISABLED
slouken@6753
   707
    return SDL_DISABLE;
slouken@0
   708
#else
slouken@4429
   709
    const Uint32 event_list[] = {
slouken@1895
   710
        SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION,
slouken@6690
   711
        SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED
slouken@1895
   712
    };
slouken@1895
   713
    unsigned int i;
slouken@0
   714
slouken@1895
   715
    switch (state) {
slouken@1895
   716
    case SDL_QUERY:
slouken@6753
   717
        state = SDL_DISABLE;
slouken@1895
   718
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@1895
   719
            state = SDL_EventState(event_list[i], SDL_QUERY);
slouken@1895
   720
            if (state == SDL_ENABLE) {
slouken@1895
   721
                break;
slouken@1895
   722
            }
slouken@1895
   723
        }
slouken@1895
   724
        break;
slouken@1895
   725
    default:
slouken@1895
   726
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@1895
   727
            SDL_EventState(event_list[i], state);
slouken@1895
   728
        }
slouken@1895
   729
        break;
slouken@1895
   730
    }
slouken@1895
   731
    return (state);
slouken@1361
   732
#endif /* SDL_EVENTS_DISABLED */
slouken@0
   733
}
slouken@1895
   734
slouken@6693
   735
/* return the guid for this index */
slouken@6738
   736
SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
slouken@6690
   737
{
slouken@7294
   738
    if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
slouken@7294
   739
        SDL_JoystickGUID emptyGUID;
slouken@7294
   740
        SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
slouken@8920
   741
        SDL_zero(emptyGUID);
slouken@7294
   742
        return emptyGUID;
slouken@7294
   743
    }
slouken@8920
   744
    return SDL_SYS_JoystickGetDeviceGUID(device_index);
slouken@6690
   745
}
slouken@6690
   746
slouken@6693
   747
/* return the guid for this opened device */
slouken@6738
   748
SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick * joystick)
slouken@6690
   749
{
slouken@7789
   750
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@7789
   751
        SDL_JoystickGUID emptyGUID;
slouken@8920
   752
        SDL_zero(emptyGUID);
slouken@7789
   753
        return emptyGUID;
slouken@7789
   754
    }
slouken@8920
   755
    return SDL_SYS_JoystickGetGUID(joystick);
slouken@6690
   756
}
slouken@6690
   757
slouken@6690
   758
/* convert the guid to a printable string */
slouken@8920
   759
void SDL_JoystickGetGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID)
slouken@6690
   760
{
slouken@7191
   761
    static const char k_rgchHexToASCII[] = "0123456789abcdef";
slouken@7191
   762
    int i;
slouken@6690
   763
icculus@6748
   764
    if ((pszGUID == NULL) || (cbGUID <= 0)) {
icculus@6748
   765
        return;
icculus@6748
   766
    }
icculus@6748
   767
slouken@8920
   768
    for (i = 0; i < sizeof(guid.data) && i < (cbGUID-1)/2; i++) {
slouken@7191
   769
        /* each input byte writes 2 ascii chars, and might write a null byte. */
slouken@7191
   770
        /* If we don't have room for next input byte, stop */
slouken@7191
   771
        unsigned char c = guid.data[i];
slouken@6690
   772
slouken@8920
   773
        *pszGUID++ = k_rgchHexToASCII[c >> 4];
slouken@8920
   774
        *pszGUID++ = k_rgchHexToASCII[c & 0x0F];
slouken@7191
   775
    }
slouken@7191
   776
    *pszGUID = '\0';
slouken@6690
   777
}
slouken@6690
   778
slouken@6690
   779
slouken@7191
   780
/*-----------------------------------------------------------------------------
slouken@7191
   781
 * Purpose: Returns the 4 bit nibble for a hex character
slouken@7191
   782
 * Input  : c -
slouken@7191
   783
 * Output : unsigned char
slouken@7191
   784
 *-----------------------------------------------------------------------------*/
slouken@8920
   785
static unsigned char nibble(char c)
slouken@6690
   786
{
slouken@8920
   787
    if ((c >= '0') && (c <= '9')) {
slouken@7191
   788
        return (unsigned char)(c - '0');
slouken@7191
   789
    }
slouken@6690
   790
slouken@8920
   791
    if ((c >= 'A') && (c <= 'F')) {
slouken@7191
   792
        return (unsigned char)(c - 'A' + 0x0a);
slouken@7191
   793
    }
slouken@6690
   794
slouken@8920
   795
    if ((c >= 'a') && (c <= 'f')) {
slouken@7191
   796
        return (unsigned char)(c - 'a' + 0x0a);
slouken@7191
   797
    }
slouken@6690
   798
slouken@7191
   799
    /* received an invalid character, and no real way to return an error */
slouken@8920
   800
    /* AssertMsg1(false, "Q_nibble invalid hex character '%c' ", c); */
slouken@7191
   801
    return 0;
slouken@6690
   802
}
slouken@6690
   803
slouken@6690
   804
slouken@6690
   805
/* convert the string version of a joystick guid to the struct */
slouken@6738
   806
SDL_JoystickGUID SDL_JoystickGetGUIDFromString(const char *pchGUID)
slouken@6690
   807
{
slouken@7191
   808
    SDL_JoystickGUID guid;
slouken@7191
   809
    int maxoutputbytes= sizeof(guid);
slouken@8920
   810
    size_t len = SDL_strlen(pchGUID);
slouken@7191
   811
    Uint8 *p;
slouken@7922
   812
    size_t i;
slouken@6690
   813
slouken@7191
   814
    /* Make sure it's even */
slouken@8920
   815
    len = (len) & ~0x1;
slouken@6690
   816
slouken@8920
   817
    SDL_memset(&guid, 0x00, sizeof(guid));
slouken@6690
   818
slouken@7191
   819
    p = (Uint8 *)&guid;
slouken@8920
   820
    for (i = 0; (i < len) && ((p - (Uint8 *)&guid) < maxoutputbytes); i+=2, p++) {
slouken@8920
   821
        *p = (nibble(pchGUID[i]) << 4) | nibble(pchGUID[i+1]);
slouken@7191
   822
    }
slouken@6690
   823
slouken@7191
   824
    return guid;
slouken@6690
   825
}
slouken@6690
   826
slouken@6690
   827
slouken@1895
   828
/* vi: set ts=4 sw=4 expandtab: */