src/joystick/SDL_joystick.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 01 Sep 2011 04:25:15 -0400
changeset 5856 173d28fd4be4
parent 5535 96594ac5fd1a
child 5859 db254478ed2d
permissions -rw-r--r--
Clean up any opened joysticks during SDL_JoystickQuit().

Otherwise, these leak memory and maybe operating system handles.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@0
    22
slouken@0
    23
/* This is the joystick API for Simple DirectMedia Layer */
slouken@0
    24
slouken@0
    25
#include "SDL_events.h"
slouken@1361
    26
#include "SDL_sysjoystick.h"
slouken@1361
    27
#include "SDL_joystick_c.h"
slouken@1361
    28
#if !SDL_EVENTS_DISABLED
slouken@1361
    29
#include "../events/SDL_events_c.h"
slouken@0
    30
#endif
slouken@0
    31
slouken@0
    32
Uint8 SDL_numjoysticks = 0;
slouken@0
    33
SDL_Joystick **SDL_joysticks = NULL;
slouken@0
    34
static SDL_Joystick *default_joystick = NULL;
slouken@0
    35
slouken@1895
    36
int
slouken@1895
    37
SDL_JoystickInit(void)
slouken@0
    38
{
slouken@1895
    39
    int arraylen;
slouken@1895
    40
    int status;
slouken@0
    41
slouken@1895
    42
    SDL_numjoysticks = 0;
slouken@1895
    43
    status = SDL_SYS_JoystickInit();
slouken@1895
    44
    if (status >= 0) {
slouken@1895
    45
        arraylen = (status + 1) * sizeof(*SDL_joysticks);
slouken@1895
    46
        SDL_joysticks = (SDL_Joystick **) SDL_malloc(arraylen);
slouken@1895
    47
        if (SDL_joysticks == NULL) {
slouken@1895
    48
            SDL_numjoysticks = 0;
slouken@1895
    49
        } else {
slouken@1895
    50
            SDL_memset(SDL_joysticks, 0, arraylen);
slouken@1895
    51
            SDL_numjoysticks = status;
slouken@1895
    52
        }
slouken@1895
    53
        status = 0;
slouken@1895
    54
    }
slouken@1895
    55
    default_joystick = NULL;
slouken@1895
    56
    return (status);
slouken@0
    57
}
slouken@0
    58
slouken@0
    59
/*
slouken@0
    60
 * Count the number of joysticks attached to the system
slouken@0
    61
 */
slouken@1895
    62
int
slouken@1895
    63
SDL_NumJoysticks(void)
slouken@0
    64
{
slouken@1895
    65
    return SDL_numjoysticks;
slouken@0
    66
}
slouken@0
    67
slouken@0
    68
/*
slouken@0
    69
 * Get the implementation dependent name of a joystick
slouken@0
    70
 */
slouken@1895
    71
const char *
slouken@1895
    72
SDL_JoystickName(int device_index)
slouken@0
    73
{
slouken@1895
    74
    if ((device_index < 0) || (device_index >= SDL_numjoysticks)) {
slouken@1895
    75
        SDL_SetError("There are %d joysticks available", SDL_numjoysticks);
slouken@1895
    76
        return (NULL);
slouken@1895
    77
    }
slouken@1895
    78
    return (SDL_SYS_JoystickName(device_index));
slouken@0
    79
}
slouken@0
    80
slouken@0
    81
/*
slouken@0
    82
 * Open a joystick for use - the index passed as an argument refers to
slouken@0
    83
 * the N'th joystick on the system.  This index is the value which will
slouken@0
    84
 * identify this joystick in future joystick events.
slouken@0
    85
 *
slouken@0
    86
 * This function returns a joystick identifier, or NULL if an error occurred.
slouken@0
    87
 */
slouken@1895
    88
SDL_Joystick *
slouken@1895
    89
SDL_JoystickOpen(int device_index)
slouken@0
    90
{
slouken@1895
    91
    int i;
slouken@1895
    92
    SDL_Joystick *joystick;
slouken@0
    93
slouken@1895
    94
    if ((device_index < 0) || (device_index >= SDL_numjoysticks)) {
slouken@1895
    95
        SDL_SetError("There are %d joysticks available", SDL_numjoysticks);
slouken@1895
    96
        return (NULL);
slouken@1895
    97
    }
slouken@0
    98
slouken@1895
    99
    /* If the joystick is already open, return it */
slouken@1895
   100
    for (i = 0; SDL_joysticks[i]; ++i) {
slouken@1895
   101
        if (device_index == SDL_joysticks[i]->index) {
slouken@1895
   102
            joystick = SDL_joysticks[i];
slouken@1895
   103
            ++joystick->ref_count;
slouken@1895
   104
            return (joystick);
slouken@1895
   105
        }
slouken@1895
   106
    }
slouken@0
   107
slouken@1895
   108
    /* Create and initialize the joystick */
slouken@1895
   109
    joystick = (SDL_Joystick *) SDL_malloc((sizeof *joystick));
icculus@3608
   110
    if (joystick == NULL) {
icculus@3608
   111
        SDL_OutOfMemory();
icculus@3608
   112
        return NULL;
slouken@1895
   113
    }
icculus@3608
   114
icculus@3608
   115
    SDL_memset(joystick, 0, (sizeof *joystick));
icculus@3608
   116
    joystick->index = device_index;
icculus@3608
   117
    if (SDL_SYS_JoystickOpen(joystick) < 0) {
icculus@3608
   118
        SDL_free(joystick);
icculus@3608
   119
        return NULL;
slouken@1895
   120
    }
icculus@3608
   121
    if (joystick->naxes > 0) {
icculus@3608
   122
        joystick->axes = (Sint16 *) SDL_malloc
icculus@3608
   123
            (joystick->naxes * sizeof(Sint16));
icculus@3608
   124
    }
icculus@3608
   125
    if (joystick->nhats > 0) {
icculus@3608
   126
        joystick->hats = (Uint8 *) SDL_malloc
icculus@3608
   127
            (joystick->nhats * sizeof(Uint8));
icculus@3608
   128
    }
icculus@3608
   129
    if (joystick->nballs > 0) {
icculus@3608
   130
        joystick->balls = (struct balldelta *) SDL_malloc
icculus@3608
   131
            (joystick->nballs * sizeof(*joystick->balls));
icculus@3608
   132
    }
icculus@3608
   133
    if (joystick->nbuttons > 0) {
icculus@3608
   134
        joystick->buttons = (Uint8 *) SDL_malloc
icculus@3608
   135
            (joystick->nbuttons * sizeof(Uint8));
icculus@3608
   136
    }
icculus@3608
   137
    if (((joystick->naxes > 0) && !joystick->axes)
icculus@3608
   138
        || ((joystick->nhats > 0) && !joystick->hats)
icculus@3608
   139
        || ((joystick->nballs > 0) && !joystick->balls)
icculus@3608
   140
        || ((joystick->nbuttons > 0) && !joystick->buttons)) {
icculus@3608
   141
        SDL_OutOfMemory();
icculus@3608
   142
        SDL_JoystickClose(joystick);
icculus@3608
   143
        return NULL;
icculus@3608
   144
    }
icculus@3608
   145
    if (joystick->axes) {
icculus@3608
   146
        SDL_memset(joystick->axes, 0, joystick->naxes * sizeof(Sint16));
icculus@3608
   147
    }
icculus@3608
   148
    if (joystick->hats) {
icculus@3608
   149
        SDL_memset(joystick->hats, 0, joystick->nhats * sizeof(Uint8));
icculus@3608
   150
    }
icculus@3608
   151
    if (joystick->balls) {
icculus@3608
   152
        SDL_memset(joystick->balls, 0,
icculus@3608
   153
            joystick->nballs * sizeof(*joystick->balls));
icculus@3608
   154
    }
icculus@3608
   155
    if (joystick->buttons) {
icculus@3608
   156
        SDL_memset(joystick->buttons, 0, joystick->nbuttons * sizeof(Uint8));
icculus@3608
   157
    }
icculus@3608
   158
icculus@3608
   159
    /* Add joystick to list */
icculus@3608
   160
    ++joystick->ref_count;
icculus@3608
   161
    for (i = 0; SDL_joysticks[i]; ++i)
icculus@3608
   162
        /* Skip to next joystick */ ;
icculus@3608
   163
    SDL_joysticks[i] = joystick;
icculus@3608
   164
slouken@1895
   165
    return (joystick);
slouken@0
   166
}
slouken@0
   167
slouken@0
   168
/*
slouken@0
   169
 * Returns 1 if the joystick has been opened, or 0 if it has not.
slouken@0
   170
 */
slouken@1895
   171
int
slouken@1895
   172
SDL_JoystickOpened(int device_index)
slouken@0
   173
{
slouken@1895
   174
    int i, opened;
slouken@0
   175
slouken@1895
   176
    opened = 0;
slouken@1895
   177
    for (i = 0; SDL_joysticks[i]; ++i) {
slouken@1895
   178
        if (SDL_joysticks[i]->index == (Uint8) device_index) {
slouken@1895
   179
            opened = 1;
slouken@1895
   180
            break;
slouken@1895
   181
        }
slouken@1895
   182
    }
slouken@1895
   183
    return (opened);
slouken@0
   184
}
slouken@0
   185
slouken@2713
   186
slouken@2713
   187
/*
slouken@2713
   188
 * Checks to make sure the joystick is valid.
slouken@2713
   189
 */
slouken@2713
   190
int
slouken@2713
   191
SDL_PrivateJoystickValid(SDL_Joystick ** joystick)
slouken@0
   192
{
slouken@1895
   193
    int valid;
slouken@0
   194
slouken@1895
   195
    if (*joystick == NULL) {
slouken@1895
   196
        *joystick = default_joystick;
slouken@1895
   197
    }
slouken@1895
   198
    if (*joystick == NULL) {
slouken@1895
   199
        SDL_SetError("Joystick hasn't been opened yet");
slouken@1895
   200
        valid = 0;
slouken@1895
   201
    } else {
slouken@1895
   202
        valid = 1;
slouken@1895
   203
    }
slouken@1895
   204
    return valid;
slouken@0
   205
}
slouken@0
   206
slouken@0
   207
/*
slouken@0
   208
 * Get the device index of an opened joystick.
slouken@0
   209
 */
slouken@1895
   210
int
slouken@1895
   211
SDL_JoystickIndex(SDL_Joystick * joystick)
slouken@0
   212
{
slouken@2713
   213
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   214
        return (-1);
slouken@1895
   215
    }
slouken@1895
   216
    return (joystick->index);
slouken@0
   217
}
slouken@0
   218
slouken@0
   219
/*
slouken@0
   220
 * Get the number of multi-dimensional axis controls on a joystick
slouken@0
   221
 */
slouken@1895
   222
int
slouken@1895
   223
SDL_JoystickNumAxes(SDL_Joystick * joystick)
slouken@0
   224
{
slouken@2713
   225
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   226
        return (-1);
slouken@1895
   227
    }
slouken@1895
   228
    return (joystick->naxes);
slouken@0
   229
}
slouken@0
   230
slouken@0
   231
/*
slouken@0
   232
 * Get the number of hats on a joystick
slouken@0
   233
 */
slouken@1895
   234
int
slouken@1895
   235
SDL_JoystickNumHats(SDL_Joystick * joystick)
slouken@0
   236
{
slouken@2713
   237
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   238
        return (-1);
slouken@1895
   239
    }
slouken@1895
   240
    return (joystick->nhats);
slouken@0
   241
}
slouken@0
   242
slouken@0
   243
/*
slouken@0
   244
 * Get the number of trackballs on a joystick
slouken@0
   245
 */
slouken@1895
   246
int
slouken@1895
   247
SDL_JoystickNumBalls(SDL_Joystick * joystick)
slouken@0
   248
{
slouken@2713
   249
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   250
        return (-1);
slouken@1895
   251
    }
slouken@1895
   252
    return (joystick->nballs);
slouken@0
   253
}
slouken@0
   254
slouken@0
   255
/*
slouken@0
   256
 * Get the number of buttons on a joystick
slouken@0
   257
 */
slouken@1895
   258
int
slouken@1895
   259
SDL_JoystickNumButtons(SDL_Joystick * joystick)
slouken@0
   260
{
slouken@2713
   261
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   262
        return (-1);
slouken@1895
   263
    }
slouken@1895
   264
    return (joystick->nbuttons);
slouken@0
   265
}
slouken@0
   266
slouken@0
   267
/*
slouken@0
   268
 * Get the current state of an axis control on a joystick
slouken@0
   269
 */
slouken@1895
   270
Sint16
slouken@1895
   271
SDL_JoystickGetAxis(SDL_Joystick * joystick, int axis)
slouken@0
   272
{
slouken@1895
   273
    Sint16 state;
slouken@0
   274
slouken@2713
   275
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   276
        return (0);
slouken@1895
   277
    }
slouken@1895
   278
    if (axis < joystick->naxes) {
slouken@1895
   279
        state = joystick->axes[axis];
slouken@1895
   280
    } else {
slouken@1895
   281
        SDL_SetError("Joystick only has %d axes", joystick->naxes);
slouken@1895
   282
        state = 0;
slouken@1895
   283
    }
slouken@1895
   284
    return (state);
slouken@0
   285
}
slouken@0
   286
slouken@0
   287
/*
slouken@0
   288
 * Get the current state of a hat on a joystick
slouken@0
   289
 */
slouken@1895
   290
Uint8
slouken@1895
   291
SDL_JoystickGetHat(SDL_Joystick * joystick, int hat)
slouken@0
   292
{
slouken@1895
   293
    Uint8 state;
slouken@0
   294
slouken@2713
   295
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   296
        return (0);
slouken@1895
   297
    }
slouken@1895
   298
    if (hat < joystick->nhats) {
slouken@1895
   299
        state = joystick->hats[hat];
slouken@1895
   300
    } else {
slouken@1895
   301
        SDL_SetError("Joystick only has %d hats", joystick->nhats);
slouken@1895
   302
        state = 0;
slouken@1895
   303
    }
slouken@1895
   304
    return (state);
slouken@0
   305
}
slouken@0
   306
slouken@0
   307
/*
slouken@0
   308
 * Get the ball axis change since the last poll
slouken@0
   309
 */
slouken@1895
   310
int
slouken@1895
   311
SDL_JoystickGetBall(SDL_Joystick * joystick, int ball, int *dx, int *dy)
slouken@0
   312
{
slouken@1895
   313
    int retval;
slouken@0
   314
slouken@2713
   315
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   316
        return (-1);
slouken@1895
   317
    }
slouken@0
   318
slouken@1895
   319
    retval = 0;
slouken@1895
   320
    if (ball < joystick->nballs) {
slouken@1895
   321
        if (dx) {
slouken@1895
   322
            *dx = joystick->balls[ball].dx;
slouken@1895
   323
        }
slouken@1895
   324
        if (dy) {
slouken@1895
   325
            *dy = joystick->balls[ball].dy;
slouken@1895
   326
        }
slouken@1895
   327
        joystick->balls[ball].dx = 0;
slouken@1895
   328
        joystick->balls[ball].dy = 0;
slouken@1895
   329
    } else {
slouken@1895
   330
        SDL_SetError("Joystick only has %d balls", joystick->nballs);
slouken@1895
   331
        retval = -1;
slouken@1895
   332
    }
slouken@1895
   333
    return (retval);
slouken@0
   334
}
slouken@0
   335
slouken@0
   336
/*
slouken@0
   337
 * Get the current state of a button on a joystick
slouken@0
   338
 */
slouken@1895
   339
Uint8
slouken@1895
   340
SDL_JoystickGetButton(SDL_Joystick * joystick, int button)
slouken@0
   341
{
slouken@1895
   342
    Uint8 state;
slouken@0
   343
slouken@2713
   344
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   345
        return (0);
slouken@1895
   346
    }
slouken@1895
   347
    if (button < joystick->nbuttons) {
slouken@1895
   348
        state = joystick->buttons[button];
slouken@1895
   349
    } else {
slouken@1895
   350
        SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
slouken@1895
   351
        state = 0;
slouken@1895
   352
    }
slouken@1895
   353
    return (state);
slouken@0
   354
}
slouken@0
   355
slouken@0
   356
/*
slouken@0
   357
 * Close a joystick previously opened with SDL_JoystickOpen()
slouken@0
   358
 */
slouken@1895
   359
void
slouken@1895
   360
SDL_JoystickClose(SDL_Joystick * joystick)
slouken@0
   361
{
slouken@1895
   362
    int i;
slouken@0
   363
slouken@2713
   364
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   365
        return;
slouken@1895
   366
    }
slouken@0
   367
slouken@1895
   368
    /* First decrement ref count */
slouken@1895
   369
    if (--joystick->ref_count > 0) {
slouken@1895
   370
        return;
slouken@1895
   371
    }
slouken@0
   372
slouken@1895
   373
    if (joystick == default_joystick) {
slouken@1895
   374
        default_joystick = NULL;
slouken@1895
   375
    }
slouken@1895
   376
    SDL_SYS_JoystickClose(joystick);
slouken@0
   377
slouken@1895
   378
    /* Remove joystick from list */
slouken@1895
   379
    for (i = 0; SDL_joysticks[i]; ++i) {
slouken@1895
   380
        if (joystick == SDL_joysticks[i]) {
slouken@3369
   381
            SDL_memmove(&SDL_joysticks[i], &SDL_joysticks[i + 1],
slouken@3369
   382
                        (SDL_numjoysticks - i) * sizeof(joystick));
slouken@1895
   383
            break;
slouken@1895
   384
        }
slouken@1895
   385
    }
slouken@0
   386
slouken@1895
   387
    /* Free the data associated with this joystick */
slouken@1895
   388
    if (joystick->axes) {
slouken@1895
   389
        SDL_free(joystick->axes);
slouken@1895
   390
    }
slouken@1895
   391
    if (joystick->hats) {
slouken@1895
   392
        SDL_free(joystick->hats);
slouken@1895
   393
    }
slouken@1895
   394
    if (joystick->balls) {
slouken@1895
   395
        SDL_free(joystick->balls);
slouken@1895
   396
    }
slouken@1895
   397
    if (joystick->buttons) {
slouken@1895
   398
        SDL_free(joystick->buttons);
slouken@1895
   399
    }
slouken@1895
   400
    SDL_free(joystick);
slouken@0
   401
}
slouken@0
   402
slouken@1895
   403
void
slouken@1895
   404
SDL_JoystickQuit(void)
slouken@0
   405
{
icculus@5856
   406
    const int numsticks = SDL_numjoysticks;
icculus@5856
   407
    int i;
icculus@5856
   408
slouken@1895
   409
    /* Stop the event polling */
slouken@1895
   410
    SDL_numjoysticks = 0;
slouken@0
   411
icculus@5856
   412
    SDL_assert( (SDL_joysticks == NULL) == (numsticks == 0) );
icculus@5856
   413
icculus@5856
   414
    for (i = 0; i < numsticks; i++) {
icculus@5856
   415
        SDL_Joystick *stick = SDL_joysticks[i];
icculus@5856
   416
        if (stick && (stick->ref_count >= 1)) {
icculus@5856
   417
            stick->ref_count = 1;
icculus@5856
   418
            SDL_JoystickClose(stick);
icculus@5856
   419
        }
icculus@5856
   420
    }
icculus@5856
   421
slouken@1895
   422
    /* Quit the joystick setup */
slouken@1895
   423
    SDL_SYS_JoystickQuit();
slouken@1895
   424
    if (SDL_joysticks) {
slouken@1895
   425
        SDL_free(SDL_joysticks);
slouken@1895
   426
        SDL_joysticks = NULL;
slouken@1895
   427
    }
slouken@0
   428
}
slouken@0
   429
slouken@0
   430
slouken@0
   431
/* These are global for SDL_sysjoystick.c and SDL_events.c */
slouken@0
   432
slouken@1895
   433
int
slouken@1895
   434
SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
slouken@0
   435
{
slouken@1895
   436
    int posted;
slouken@0
   437
slouken@1895
   438
    /* Update internal joystick state */
slouken@1895
   439
    joystick->axes[axis] = value;
slouken@0
   440
slouken@1895
   441
    /* Post the event, if desired */
slouken@1895
   442
    posted = 0;
slouken@1361
   443
#if !SDL_EVENTS_DISABLED
slouken@4429
   444
    if (SDL_GetEventState(SDL_JOYAXISMOTION) == SDL_ENABLE) {
slouken@1895
   445
        SDL_Event event;
slouken@1895
   446
        event.type = SDL_JOYAXISMOTION;
slouken@1895
   447
        event.jaxis.which = joystick->index;
slouken@1895
   448
        event.jaxis.axis = axis;
slouken@1895
   449
        event.jaxis.value = value;
slouken@1895
   450
        if ((SDL_EventOK == NULL)
slouken@1895
   451
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@1895
   452
            posted = 1;
slouken@1895
   453
            SDL_PushEvent(&event);
slouken@1895
   454
        }
slouken@1895
   455
    }
slouken@1361
   456
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   457
    return (posted);
slouken@0
   458
}
slouken@0
   459
slouken@1895
   460
int
slouken@1895
   461
SDL_PrivateJoystickHat(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
slouken@0
   462
{
slouken@1895
   463
    int posted;
slouken@0
   464
slouken@1895
   465
    /* Update internal joystick state */
slouken@1895
   466
    joystick->hats[hat] = value;
slouken@0
   467
slouken@1895
   468
    /* Post the event, if desired */
slouken@1895
   469
    posted = 0;
slouken@1361
   470
#if !SDL_EVENTS_DISABLED
slouken@4429
   471
    if (SDL_GetEventState(SDL_JOYHATMOTION) == SDL_ENABLE) {
slouken@1895
   472
        SDL_Event event;
slouken@1895
   473
        event.jhat.type = SDL_JOYHATMOTION;
slouken@1895
   474
        event.jhat.which = joystick->index;
slouken@1895
   475
        event.jhat.hat = hat;
slouken@1895
   476
        event.jhat.value = value;
slouken@1895
   477
        if ((SDL_EventOK == NULL)
slouken@1895
   478
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@1895
   479
            posted = 1;
slouken@1895
   480
            SDL_PushEvent(&event);
slouken@1895
   481
        }
slouken@1895
   482
    }
slouken@1361
   483
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   484
    return (posted);
slouken@0
   485
}
slouken@0
   486
slouken@1895
   487
int
slouken@1895
   488
SDL_PrivateJoystickBall(SDL_Joystick * joystick, Uint8 ball,
slouken@1895
   489
                        Sint16 xrel, Sint16 yrel)
slouken@0
   490
{
slouken@1895
   491
    int posted;
slouken@0
   492
slouken@1895
   493
    /* Update internal mouse state */
slouken@1895
   494
    joystick->balls[ball].dx += xrel;
slouken@1895
   495
    joystick->balls[ball].dy += yrel;
slouken@0
   496
slouken@1895
   497
    /* Post the event, if desired */
slouken@1895
   498
    posted = 0;
slouken@1361
   499
#if !SDL_EVENTS_DISABLED
slouken@4429
   500
    if (SDL_GetEventState(SDL_JOYBALLMOTION) == SDL_ENABLE) {
slouken@1895
   501
        SDL_Event event;
slouken@1895
   502
        event.jball.type = SDL_JOYBALLMOTION;
slouken@1895
   503
        event.jball.which = joystick->index;
slouken@1895
   504
        event.jball.ball = ball;
slouken@1895
   505
        event.jball.xrel = xrel;
slouken@1895
   506
        event.jball.yrel = yrel;
slouken@1895
   507
        if ((SDL_EventOK == NULL)
slouken@1895
   508
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@1895
   509
            posted = 1;
slouken@1895
   510
            SDL_PushEvent(&event);
slouken@1895
   511
        }
slouken@1895
   512
    }
slouken@1361
   513
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   514
    return (posted);
slouken@0
   515
}
slouken@0
   516
slouken@1895
   517
int
slouken@1895
   518
SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
slouken@0
   519
{
slouken@1895
   520
    int posted;
slouken@1361
   521
#if !SDL_EVENTS_DISABLED
slouken@1895
   522
    SDL_Event event;
slouken@0
   523
slouken@1895
   524
    switch (state) {
slouken@1895
   525
    case SDL_PRESSED:
slouken@1895
   526
        event.type = SDL_JOYBUTTONDOWN;
slouken@1895
   527
        break;
slouken@1895
   528
    case SDL_RELEASED:
slouken@1895
   529
        event.type = SDL_JOYBUTTONUP;
slouken@1895
   530
        break;
slouken@1895
   531
    default:
slouken@1895
   532
        /* Invalid state -- bail */
slouken@1895
   533
        return (0);
slouken@1895
   534
    }
slouken@1361
   535
#endif /* !SDL_EVENTS_DISABLED */
slouken@0
   536
slouken@1895
   537
    /* Update internal joystick state */
slouken@1895
   538
    joystick->buttons[button] = state;
slouken@0
   539
slouken@1895
   540
    /* Post the event, if desired */
slouken@1895
   541
    posted = 0;
slouken@1361
   542
#if !SDL_EVENTS_DISABLED
slouken@4429
   543
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@1895
   544
        event.jbutton.which = joystick->index;
slouken@1895
   545
        event.jbutton.button = button;
slouken@1895
   546
        event.jbutton.state = state;
slouken@1895
   547
        if ((SDL_EventOK == NULL)
slouken@1895
   548
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@1895
   549
            posted = 1;
slouken@1895
   550
            SDL_PushEvent(&event);
slouken@1895
   551
        }
slouken@1895
   552
    }
slouken@1361
   553
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   554
    return (posted);
slouken@0
   555
}
slouken@0
   556
slouken@1895
   557
void
slouken@1895
   558
SDL_JoystickUpdate(void)
slouken@0
   559
{
slouken@1895
   560
    int i;
slouken@0
   561
slouken@1895
   562
    for (i = 0; SDL_joysticks[i]; ++i) {
slouken@1895
   563
        SDL_SYS_JoystickUpdate(SDL_joysticks[i]);
slouken@1895
   564
    }
slouken@0
   565
}
slouken@0
   566
slouken@1895
   567
int
slouken@1895
   568
SDL_JoystickEventState(int state)
slouken@0
   569
{
slouken@1361
   570
#if SDL_EVENTS_DISABLED
slouken@1895
   571
    return SDL_IGNORE;
slouken@0
   572
#else
slouken@4429
   573
    const Uint32 event_list[] = {
slouken@1895
   574
        SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION,
slouken@1895
   575
        SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP,
slouken@1895
   576
    };
slouken@1895
   577
    unsigned int i;
slouken@0
   578
slouken@1895
   579
    switch (state) {
slouken@1895
   580
    case SDL_QUERY:
slouken@1895
   581
        state = SDL_IGNORE;
slouken@1895
   582
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@1895
   583
            state = SDL_EventState(event_list[i], SDL_QUERY);
slouken@1895
   584
            if (state == SDL_ENABLE) {
slouken@1895
   585
                break;
slouken@1895
   586
            }
slouken@1895
   587
        }
slouken@1895
   588
        break;
slouken@1895
   589
    default:
slouken@1895
   590
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@1895
   591
            SDL_EventState(event_list[i], state);
slouken@1895
   592
        }
slouken@1895
   593
        break;
slouken@1895
   594
    }
slouken@1895
   595
    return (state);
slouken@1361
   596
#endif /* SDL_EVENTS_DISABLED */
slouken@0
   597
}
slouken@1895
   598
slouken@1895
   599
/* vi: set ts=4 sw=4 expandtab: */