src/joystick/SDL_joystick.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 30 Sep 2015 15:39:30 -0700
changeset 9884 26b595dea221
parent 9876 1496e502e51d
child 9916 9429bc102632
permissions -rw-r--r--
SDL - added new SDL_JoystickCurrentPowerLevel() API that returns the battery level of the selected joystick. Currently only implemented for XInput devices, other platforms are a TODO.

CR: Sam
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
    }
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
/*
slouken@6690
   377
 * Get the friendly name of this joystick
slouken@6690
   378
 */
slouken@6690
   379
const char *
slouken@6690
   380
SDL_JoystickName(SDL_Joystick * joystick)
slouken@6690
   381
{
slouken@6690
   382
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@6690
   383
        return (NULL);
slouken@6690
   384
    }
slouken@7191
   385
slouken@6690
   386
    return (joystick->name);
slouken@6690
   387
}
slouken@6690
   388
slouken@6690
   389
/*
slouken@0
   390
 * Close a joystick previously opened with SDL_JoystickOpen()
slouken@0
   391
 */
slouken@1895
   392
void
slouken@1895
   393
SDL_JoystickClose(SDL_Joystick * joystick)
slouken@0
   394
{
slouken@7191
   395
    SDL_Joystick *joysticklist;
slouken@7191
   396
    SDL_Joystick *joysticklistprev;
slouken@0
   397
slouken@6690
   398
    if (!joystick) {
slouken@1895
   399
        return;
slouken@1895
   400
    }
slouken@0
   401
slouken@1895
   402
    /* First decrement ref count */
slouken@1895
   403
    if (--joystick->ref_count > 0) {
slouken@1895
   404
        return;
slouken@1895
   405
    }
slouken@0
   406
slouken@6712
   407
    if (joystick == SDL_updating_joystick) {
slouken@6712
   408
        return;
slouken@6712
   409
    }
slouken@6712
   410
slouken@1895
   411
    SDL_SYS_JoystickClose(joystick);
icculus@9433
   412
    joystick->hwdata = NULL;
slouken@0
   413
slouken@7191
   414
    joysticklist = SDL_joysticks;
slouken@7191
   415
    joysticklistprev = NULL;
slouken@8920
   416
    while (joysticklist) {
slouken@8920
   417
        if (joystick == joysticklist) {
slouken@8920
   418
            if (joysticklistprev) {
slouken@7191
   419
                /* unlink this entry */
slouken@7191
   420
                joysticklistprev->next = joysticklist->next;
slouken@8920
   421
            } else {
slouken@7191
   422
                SDL_joysticks = joystick->next;
slouken@7191
   423
            }
slouken@7191
   424
            break;
slouken@7191
   425
        }
slouken@7191
   426
        joysticklistprev = joysticklist;
slouken@7191
   427
        joysticklist = joysticklist->next;
slouken@7191
   428
    }
slouken@7191
   429
slouken@7719
   430
    SDL_free(joystick->name);
slouken@0
   431
slouken@1895
   432
    /* Free the data associated with this joystick */
slouken@7719
   433
    SDL_free(joystick->axes);
slouken@7719
   434
    SDL_free(joystick->hats);
slouken@7719
   435
    SDL_free(joystick->balls);
slouken@7719
   436
    SDL_free(joystick->buttons);
slouken@1895
   437
    SDL_free(joystick);
slouken@0
   438
}
slouken@0
   439
slouken@1895
   440
void
slouken@1895
   441
SDL_JoystickQuit(void)
slouken@0
   442
{
slouken@6712
   443
    /* Make sure we're not getting called in the middle of updating joysticks */
slouken@6712
   444
    SDL_assert(!SDL_updating_joystick);
slouken@6712
   445
slouken@1895
   446
    /* Stop the event polling */
slouken@8920
   447
    while (SDL_joysticks) {
slouken@7191
   448
        SDL_joysticks->ref_count = 1;
slouken@6690
   449
        SDL_JoystickClose(SDL_joysticks);
slouken@7191
   450
    }
icculus@5856
   451
slouken@1895
   452
    /* Quit the joystick setup */
slouken@1895
   453
    SDL_SYS_JoystickQuit();
slouken@7360
   454
slouken@7360
   455
#if !SDL_EVENTS_DISABLED
slouken@7360
   456
    SDL_QuitSubSystem(SDL_INIT_EVENTS);
slouken@7360
   457
#endif
slouken@0
   458
}
slouken@0
   459
slouken@0
   460
jorgen@7279
   461
static SDL_bool
jorgen@7279
   462
SDL_PrivateJoystickShouldIgnoreEvent()
jorgen@7279
   463
{
slouken@8920
   464
    if (SDL_joystick_allows_background_events) {
jorgen@7279
   465
        return SDL_FALSE;
jorgen@7279
   466
    }
jorgen@7279
   467
slouken@7337
   468
    if (SDL_WasInit(SDL_INIT_VIDEO)) {
slouken@7337
   469
        if (SDL_GetKeyboardFocus() == NULL) {
gabomdq@7677
   470
            /* Video is initialized and we don't have focus, ignore the event. */
slouken@7337
   471
            return SDL_TRUE;
slouken@7337
   472
        } else {
slouken@7337
   473
            return SDL_FALSE;
slouken@7337
   474
        }
jorgen@7279
   475
    }
jorgen@7279
   476
gabomdq@7677
   477
    /* Video subsystem wasn't initialized, always allow the event */
slouken@7337
   478
    return SDL_FALSE;
jorgen@7279
   479
}
jorgen@7279
   480
slouken@0
   481
/* These are global for SDL_sysjoystick.c and SDL_events.c */
slouken@0
   482
slouken@1895
   483
int
slouken@1895
   484
SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
slouken@0
   485
{
slouken@1895
   486
    int posted;
slouken@0
   487
slouken@8920
   488
    /* Make sure we're not getting garbage or duplicate events */
slouken@6145
   489
    if (axis >= joystick->naxes) {
slouken@6145
   490
        return 0;
slouken@6145
   491
    }
slouken@8920
   492
    if (value == joystick->axes[axis]) {
slouken@8920
   493
        return 0;
slouken@8920
   494
    }
slouken@6145
   495
slouken@8903
   496
    /* We ignore events if we don't have keyboard focus, except for centering
slouken@8903
   497
     * events.
slouken@8903
   498
     */
slouken@8903
   499
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
slouken@8903
   500
        if ((value > 0 && value >= joystick->axes[axis]) ||
slouken@8903
   501
            (value < 0 && value <= joystick->axes[axis])) {
slouken@8903
   502
            return 0;
slouken@8903
   503
        }
slouken@8903
   504
    }
slouken@8903
   505
slouken@1895
   506
    /* Update internal joystick state */
slouken@1895
   507
    joystick->axes[axis] = value;
slouken@0
   508
slouken@1895
   509
    /* Post the event, if desired */
slouken@1895
   510
    posted = 0;
slouken@1361
   511
#if !SDL_EVENTS_DISABLED
slouken@4429
   512
    if (SDL_GetEventState(SDL_JOYAXISMOTION) == SDL_ENABLE) {
slouken@1895
   513
        SDL_Event event;
slouken@1895
   514
        event.type = SDL_JOYAXISMOTION;
slouken@6690
   515
        event.jaxis.which = joystick->instance_id;
slouken@1895
   516
        event.jaxis.axis = axis;
slouken@1895
   517
        event.jaxis.value = value;
slouken@6690
   518
        posted = SDL_PushEvent(&event) == 1;
slouken@1895
   519
    }
slouken@1361
   520
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   521
    return (posted);
slouken@0
   522
}
slouken@0
   523
slouken@1895
   524
int
slouken@1895
   525
SDL_PrivateJoystickHat(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
slouken@0
   526
{
slouken@1895
   527
    int posted;
slouken@0
   528
slouken@8920
   529
    /* Make sure we're not getting garbage or duplicate events */
slouken@6145
   530
    if (hat >= joystick->nhats) {
slouken@6145
   531
        return 0;
slouken@6145
   532
    }
slouken@8920
   533
    if (value == joystick->hats[hat]) {
slouken@8920
   534
        return 0;
slouken@8920
   535
    }
slouken@6145
   536
jorgen@7279
   537
    /* We ignore events if we don't have keyboard focus, except for centering
jorgen@7279
   538
     * events.
jorgen@7279
   539
     */
jorgen@7279
   540
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
slouken@8903
   541
        if (value != SDL_HAT_CENTERED) {
jorgen@7279
   542
            return 0;
jorgen@7279
   543
        }
jorgen@7279
   544
    }
jorgen@7279
   545
slouken@8903
   546
    /* Update internal joystick state */
slouken@8903
   547
    joystick->hats[hat] = value;
jorgen@7279
   548
slouken@1895
   549
    /* Post the event, if desired */
slouken@1895
   550
    posted = 0;
slouken@1361
   551
#if !SDL_EVENTS_DISABLED
slouken@4429
   552
    if (SDL_GetEventState(SDL_JOYHATMOTION) == SDL_ENABLE) {
slouken@1895
   553
        SDL_Event event;
slouken@1895
   554
        event.jhat.type = SDL_JOYHATMOTION;
slouken@6690
   555
        event.jhat.which = joystick->instance_id;
slouken@1895
   556
        event.jhat.hat = hat;
slouken@1895
   557
        event.jhat.value = value;
slouken@6690
   558
        posted = SDL_PushEvent(&event) == 1;
slouken@1895
   559
    }
slouken@1361
   560
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   561
    return (posted);
slouken@0
   562
}
slouken@0
   563
slouken@1895
   564
int
slouken@1895
   565
SDL_PrivateJoystickBall(SDL_Joystick * joystick, Uint8 ball,
slouken@1895
   566
                        Sint16 xrel, Sint16 yrel)
slouken@0
   567
{
slouken@1895
   568
    int posted;
slouken@0
   569
slouken@6145
   570
    /* Make sure we're not getting garbage events */
slouken@6145
   571
    if (ball >= joystick->nballs) {
slouken@6145
   572
        return 0;
slouken@6145
   573
    }
slouken@6145
   574
jorgen@7279
   575
    /* We ignore events if we don't have keyboard focus. */
jorgen@7279
   576
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
jorgen@7279
   577
        return 0;
jorgen@7279
   578
    }
jorgen@7279
   579
slouken@1895
   580
    /* Update internal mouse state */
slouken@1895
   581
    joystick->balls[ball].dx += xrel;
slouken@1895
   582
    joystick->balls[ball].dy += yrel;
slouken@0
   583
slouken@1895
   584
    /* Post the event, if desired */
slouken@1895
   585
    posted = 0;
slouken@1361
   586
#if !SDL_EVENTS_DISABLED
slouken@4429
   587
    if (SDL_GetEventState(SDL_JOYBALLMOTION) == SDL_ENABLE) {
slouken@1895
   588
        SDL_Event event;
slouken@1895
   589
        event.jball.type = SDL_JOYBALLMOTION;
slouken@6690
   590
        event.jball.which = joystick->instance_id;
slouken@1895
   591
        event.jball.ball = ball;
slouken@1895
   592
        event.jball.xrel = xrel;
slouken@1895
   593
        event.jball.yrel = yrel;
slouken@6690
   594
        posted = SDL_PushEvent(&event) == 1;
slouken@1895
   595
    }
slouken@1361
   596
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   597
    return (posted);
slouken@0
   598
}
slouken@0
   599
slouken@1895
   600
int
slouken@1895
   601
SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
slouken@0
   602
{
slouken@1895
   603
    int posted;
slouken@1361
   604
#if !SDL_EVENTS_DISABLED
slouken@1895
   605
    SDL_Event event;
slouken@0
   606
slouken@1895
   607
    switch (state) {
slouken@1895
   608
    case SDL_PRESSED:
slouken@1895
   609
        event.type = SDL_JOYBUTTONDOWN;
slouken@1895
   610
        break;
slouken@1895
   611
    case SDL_RELEASED:
slouken@1895
   612
        event.type = SDL_JOYBUTTONUP;
slouken@1895
   613
        break;
slouken@1895
   614
    default:
slouken@1895
   615
        /* Invalid state -- bail */
slouken@1895
   616
        return (0);
slouken@1895
   617
    }
slouken@1361
   618
#endif /* !SDL_EVENTS_DISABLED */
slouken@0
   619
slouken@8920
   620
    /* Make sure we're not getting garbage or duplicate events */
slouken@6145
   621
    if (button >= joystick->nbuttons) {
slouken@6145
   622
        return 0;
slouken@9884
   623
    }
slouken@9884
   624
    if (state == joystick->buttons[button]) {
slouken@9884
   625
        return 0;
slouken@9884
   626
    }
slouken@6145
   627
jorgen@7279
   628
    /* We ignore events if we don't have keyboard focus, except for button
jorgen@7279
   629
     * release. */
slouken@8903
   630
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
slouken@8903
   631
        if (state == SDL_PRESSED) {
slouken@8903
   632
            return 0;
slouken@8903
   633
        }
jorgen@7279
   634
    }
jorgen@7279
   635
slouken@1895
   636
    /* Update internal joystick state */
slouken@1895
   637
    joystick->buttons[button] = state;
slouken@0
   638
slouken@1895
   639
    /* Post the event, if desired */
slouken@1895
   640
    posted = 0;
slouken@1361
   641
#if !SDL_EVENTS_DISABLED
slouken@4429
   642
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@6690
   643
        event.jbutton.which = joystick->instance_id;
slouken@1895
   644
        event.jbutton.button = button;
slouken@1895
   645
        event.jbutton.state = state;
slouken@6690
   646
        posted = SDL_PushEvent(&event) == 1;
slouken@1895
   647
    }
slouken@1361
   648
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   649
    return (posted);
slouken@0
   650
}
slouken@0
   651
slouken@1895
   652
void
slouken@1895
   653
SDL_JoystickUpdate(void)
slouken@0
   654
{
slouken@7191
   655
    SDL_Joystick *joystick;
slouken@7191
   656
slouken@7191
   657
    joystick = SDL_joysticks;
slouken@8920
   658
    while (joystick) {
slouken@7191
   659
        SDL_Joystick *joysticknext;
slouken@7191
   660
        /* save off the next pointer, the Update call may cause a joystick removed event
slouken@7191
   661
         * and cause our joystick pointer to be freed
slouken@7191
   662
         */
slouken@7191
   663
        joysticknext = joystick->next;
slouken@0
   664
slouken@6712
   665
        SDL_updating_joystick = joystick;
slouken@6712
   666
slouken@8920
   667
        SDL_SYS_JoystickUpdate(joystick);
slouken@6690
   668
icculus@9433
   669
        if (joystick->force_recentering) {
slouken@7191
   670
            int i;
slouken@6690
   671
slouken@6712
   672
            /* Tell the app that everything is centered/unpressed...  */
slime73@9876
   673
            for (i = 0; i < joystick->naxes; i++) {
slouken@6690
   674
                SDL_PrivateJoystickAxis(joystick, i, 0);
slime73@9876
   675
            }
slouken@6690
   676
slime73@9876
   677
            for (i = 0; i < joystick->nbuttons; i++) {
slouken@6690
   678
                SDL_PrivateJoystickButton(joystick, i, 0);
slime73@9876
   679
            }
slouken@6690
   680
slime73@9876
   681
            for (i = 0; i < joystick->nhats; i++) {
slouken@6690
   682
                SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
slime73@9876
   683
            }
slouken@6712
   684
icculus@9433
   685
            joystick->force_recentering = SDL_FALSE;
slouken@7191
   686
        }
slouken@6690
   687
slouken@6712
   688
        SDL_updating_joystick = NULL;
slouken@6712
   689
slouken@6712
   690
        /* If the joystick was closed while updating, free it here */
slouken@8920
   691
        if (joystick->ref_count <= 0) {
slouken@6712
   692
            SDL_JoystickClose(joystick);
slouken@6712
   693
        }
slouken@6712
   694
slouken@7191
   695
        joystick = joysticknext;
slouken@7191
   696
    }
slouken@6690
   697
slouken@7191
   698
    /* this needs to happen AFTER walking the joystick list above, so that any
slouken@7191
   699
       dangling hardware data from removed devices can be free'd
slouken@7191
   700
     */
slouken@7191
   701
    SDL_SYS_JoystickDetect();
slouken@0
   702
}
slouken@0
   703
slouken@1895
   704
int
slouken@1895
   705
SDL_JoystickEventState(int state)
slouken@0
   706
{
slouken@1361
   707
#if SDL_EVENTS_DISABLED
slouken@6753
   708
    return SDL_DISABLE;
slouken@0
   709
#else
slouken@4429
   710
    const Uint32 event_list[] = {
slouken@1895
   711
        SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION,
slouken@6690
   712
        SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED
slouken@1895
   713
    };
slouken@1895
   714
    unsigned int i;
slouken@0
   715
slouken@1895
   716
    switch (state) {
slouken@1895
   717
    case SDL_QUERY:
slouken@6753
   718
        state = SDL_DISABLE;
slouken@1895
   719
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@1895
   720
            state = SDL_EventState(event_list[i], SDL_QUERY);
slouken@1895
   721
            if (state == SDL_ENABLE) {
slouken@1895
   722
                break;
slouken@1895
   723
            }
slouken@1895
   724
        }
slouken@1895
   725
        break;
slouken@1895
   726
    default:
slouken@1895
   727
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@1895
   728
            SDL_EventState(event_list[i], state);
slouken@1895
   729
        }
slouken@1895
   730
        break;
slouken@1895
   731
    }
slouken@1895
   732
    return (state);
slouken@1361
   733
#endif /* SDL_EVENTS_DISABLED */
slouken@0
   734
}
slouken@1895
   735
slouken@6693
   736
/* return the guid for this index */
slouken@6738
   737
SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
slouken@6690
   738
{
slouken@7294
   739
    if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
slouken@7294
   740
        SDL_JoystickGUID emptyGUID;
slouken@7294
   741
        SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
slouken@8920
   742
        SDL_zero(emptyGUID);
slouken@7294
   743
        return emptyGUID;
slouken@7294
   744
    }
slouken@8920
   745
    return SDL_SYS_JoystickGetDeviceGUID(device_index);
slouken@6690
   746
}
slouken@6690
   747
slouken@6693
   748
/* return the guid for this opened device */
slouken@6738
   749
SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick * joystick)
slouken@6690
   750
{
slouken@7789
   751
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@7789
   752
        SDL_JoystickGUID emptyGUID;
slouken@8920
   753
        SDL_zero(emptyGUID);
slouken@7789
   754
        return emptyGUID;
slouken@7789
   755
    }
slouken@8920
   756
    return SDL_SYS_JoystickGetGUID(joystick);
slouken@6690
   757
}
slouken@6690
   758
slouken@6690
   759
/* convert the guid to a printable string */
slouken@8920
   760
void SDL_JoystickGetGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID)
slouken@6690
   761
{
slouken@7191
   762
    static const char k_rgchHexToASCII[] = "0123456789abcdef";
slouken@7191
   763
    int i;
slouken@6690
   764
icculus@6748
   765
    if ((pszGUID == NULL) || (cbGUID <= 0)) {
icculus@6748
   766
        return;
icculus@6748
   767
    }
icculus@6748
   768
slouken@8920
   769
    for (i = 0; i < sizeof(guid.data) && i < (cbGUID-1)/2; i++) {
slouken@7191
   770
        /* each input byte writes 2 ascii chars, and might write a null byte. */
slouken@7191
   771
        /* If we don't have room for next input byte, stop */
slouken@7191
   772
        unsigned char c = guid.data[i];
slouken@6690
   773
slouken@8920
   774
        *pszGUID++ = k_rgchHexToASCII[c >> 4];
slouken@8920
   775
        *pszGUID++ = k_rgchHexToASCII[c & 0x0F];
slouken@7191
   776
    }
slouken@7191
   777
    *pszGUID = '\0';
slouken@6690
   778
}
slouken@6690
   779
slouken@6690
   780
slouken@7191
   781
/*-----------------------------------------------------------------------------
slouken@7191
   782
 * Purpose: Returns the 4 bit nibble for a hex character
slouken@7191
   783
 * Input  : c -
slouken@7191
   784
 * Output : unsigned char
slouken@7191
   785
 *-----------------------------------------------------------------------------*/
slouken@8920
   786
static unsigned char nibble(char c)
slouken@6690
   787
{
slouken@8920
   788
    if ((c >= '0') && (c <= '9')) {
slouken@7191
   789
        return (unsigned char)(c - '0');
slouken@7191
   790
    }
slouken@6690
   791
slouken@8920
   792
    if ((c >= 'A') && (c <= 'F')) {
slouken@7191
   793
        return (unsigned char)(c - 'A' + 0x0a);
slouken@7191
   794
    }
slouken@6690
   795
slouken@8920
   796
    if ((c >= 'a') && (c <= 'f')) {
slouken@7191
   797
        return (unsigned char)(c - 'a' + 0x0a);
slouken@7191
   798
    }
slouken@6690
   799
slouken@7191
   800
    /* received an invalid character, and no real way to return an error */
slouken@8920
   801
    /* AssertMsg1(false, "Q_nibble invalid hex character '%c' ", c); */
slouken@7191
   802
    return 0;
slouken@6690
   803
}
slouken@6690
   804
slouken@6690
   805
slouken@6690
   806
/* convert the string version of a joystick guid to the struct */
slouken@6738
   807
SDL_JoystickGUID SDL_JoystickGetGUIDFromString(const char *pchGUID)
slouken@6690
   808
{
slouken@7191
   809
    SDL_JoystickGUID guid;
slouken@7191
   810
    int maxoutputbytes= sizeof(guid);
slouken@8920
   811
    size_t len = SDL_strlen(pchGUID);
slouken@7191
   812
    Uint8 *p;
slouken@7922
   813
    size_t i;
slouken@6690
   814
slouken@7191
   815
    /* Make sure it's even */
slouken@8920
   816
    len = (len) & ~0x1;
slouken@6690
   817
slouken@8920
   818
    SDL_memset(&guid, 0x00, sizeof(guid));
slouken@6690
   819
slouken@7191
   820
    p = (Uint8 *)&guid;
slouken@8920
   821
    for (i = 0; (i < len) && ((p - (Uint8 *)&guid) < maxoutputbytes); i+=2, p++) {
slouken@8920
   822
        *p = (nibble(pchGUID[i]) << 4) | nibble(pchGUID[i+1]);
slouken@7191
   823
    }
slouken@6690
   824
slouken@7191
   825
    return guid;
slouken@6690
   826
}
slouken@6690
   827
slouken@6690
   828
slouken@9884
   829
/* update the power level for this joystick */
slouken@9884
   830
void SDL_PrivateJoystickBatteryLevel(SDL_Joystick * joystick, SDL_JoystickPowerLevel ePowerLevel)
slouken@9884
   831
{
slouken@9884
   832
    joystick->epowerlevel = ePowerLevel;
slouken@9884
   833
}
slouken@9884
   834
slouken@9884
   835
slouken@9884
   836
/* return its power level */
slouken@9884
   837
SDL_JoystickPowerLevel SDL_JoystickCurrentPowerLevel(SDL_Joystick * joystick)
slouken@9884
   838
{
slouken@9884
   839
    if (!SDL_PrivateJoystickValid(joystick)) {
slouken@9884
   840
        return (SDL_JOYSTICK_POWER_UNKNOWN);
slouken@9884
   841
    }
slouken@9884
   842
    return joystick->epowerlevel;
slouken@9884
   843
}
slouken@9884
   844
slouken@9884
   845
slouken@1895
   846
/* vi: set ts=4 sw=4 expandtab: */