src/joystick/SDL_joystick.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5262 b530ef003506
child 5856 173d28fd4be4
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
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
{
slouken@1895
   406
    /* Stop the event polling */
slouken@1895
   407
    SDL_numjoysticks = 0;
slouken@0
   408
slouken@1895
   409
    /* Quit the joystick setup */
slouken@1895
   410
    SDL_SYS_JoystickQuit();
slouken@1895
   411
    if (SDL_joysticks) {
slouken@1895
   412
        SDL_free(SDL_joysticks);
slouken@1895
   413
        SDL_joysticks = NULL;
slouken@1895
   414
    }
slouken@0
   415
}
slouken@0
   416
slouken@0
   417
slouken@0
   418
/* These are global for SDL_sysjoystick.c and SDL_events.c */
slouken@0
   419
slouken@1895
   420
int
slouken@1895
   421
SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
slouken@0
   422
{
slouken@1895
   423
    int posted;
slouken@0
   424
slouken@1895
   425
    /* Update internal joystick state */
slouken@1895
   426
    joystick->axes[axis] = value;
slouken@0
   427
slouken@1895
   428
    /* Post the event, if desired */
slouken@1895
   429
    posted = 0;
slouken@1361
   430
#if !SDL_EVENTS_DISABLED
slouken@4429
   431
    if (SDL_GetEventState(SDL_JOYAXISMOTION) == SDL_ENABLE) {
slouken@1895
   432
        SDL_Event event;
slouken@1895
   433
        event.type = SDL_JOYAXISMOTION;
slouken@1895
   434
        event.jaxis.which = joystick->index;
slouken@1895
   435
        event.jaxis.axis = axis;
slouken@1895
   436
        event.jaxis.value = value;
slouken@1895
   437
        if ((SDL_EventOK == NULL)
slouken@1895
   438
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@1895
   439
            posted = 1;
slouken@1895
   440
            SDL_PushEvent(&event);
slouken@1895
   441
        }
slouken@1895
   442
    }
slouken@1361
   443
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   444
    return (posted);
slouken@0
   445
}
slouken@0
   446
slouken@1895
   447
int
slouken@1895
   448
SDL_PrivateJoystickHat(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
slouken@0
   449
{
slouken@1895
   450
    int posted;
slouken@0
   451
slouken@1895
   452
    /* Update internal joystick state */
slouken@1895
   453
    joystick->hats[hat] = value;
slouken@0
   454
slouken@1895
   455
    /* Post the event, if desired */
slouken@1895
   456
    posted = 0;
slouken@1361
   457
#if !SDL_EVENTS_DISABLED
slouken@4429
   458
    if (SDL_GetEventState(SDL_JOYHATMOTION) == SDL_ENABLE) {
slouken@1895
   459
        SDL_Event event;
slouken@1895
   460
        event.jhat.type = SDL_JOYHATMOTION;
slouken@1895
   461
        event.jhat.which = joystick->index;
slouken@1895
   462
        event.jhat.hat = hat;
slouken@1895
   463
        event.jhat.value = value;
slouken@1895
   464
        if ((SDL_EventOK == NULL)
slouken@1895
   465
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@1895
   466
            posted = 1;
slouken@1895
   467
            SDL_PushEvent(&event);
slouken@1895
   468
        }
slouken@1895
   469
    }
slouken@1361
   470
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   471
    return (posted);
slouken@0
   472
}
slouken@0
   473
slouken@1895
   474
int
slouken@1895
   475
SDL_PrivateJoystickBall(SDL_Joystick * joystick, Uint8 ball,
slouken@1895
   476
                        Sint16 xrel, Sint16 yrel)
slouken@0
   477
{
slouken@1895
   478
    int posted;
slouken@0
   479
slouken@1895
   480
    /* Update internal mouse state */
slouken@1895
   481
    joystick->balls[ball].dx += xrel;
slouken@1895
   482
    joystick->balls[ball].dy += yrel;
slouken@0
   483
slouken@1895
   484
    /* Post the event, if desired */
slouken@1895
   485
    posted = 0;
slouken@1361
   486
#if !SDL_EVENTS_DISABLED
slouken@4429
   487
    if (SDL_GetEventState(SDL_JOYBALLMOTION) == SDL_ENABLE) {
slouken@1895
   488
        SDL_Event event;
slouken@1895
   489
        event.jball.type = SDL_JOYBALLMOTION;
slouken@1895
   490
        event.jball.which = joystick->index;
slouken@1895
   491
        event.jball.ball = ball;
slouken@1895
   492
        event.jball.xrel = xrel;
slouken@1895
   493
        event.jball.yrel = yrel;
slouken@1895
   494
        if ((SDL_EventOK == NULL)
slouken@1895
   495
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@1895
   496
            posted = 1;
slouken@1895
   497
            SDL_PushEvent(&event);
slouken@1895
   498
        }
slouken@1895
   499
    }
slouken@1361
   500
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   501
    return (posted);
slouken@0
   502
}
slouken@0
   503
slouken@1895
   504
int
slouken@1895
   505
SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
slouken@0
   506
{
slouken@1895
   507
    int posted;
slouken@1361
   508
#if !SDL_EVENTS_DISABLED
slouken@1895
   509
    SDL_Event event;
slouken@0
   510
slouken@1895
   511
    switch (state) {
slouken@1895
   512
    case SDL_PRESSED:
slouken@1895
   513
        event.type = SDL_JOYBUTTONDOWN;
slouken@1895
   514
        break;
slouken@1895
   515
    case SDL_RELEASED:
slouken@1895
   516
        event.type = SDL_JOYBUTTONUP;
slouken@1895
   517
        break;
slouken@1895
   518
    default:
slouken@1895
   519
        /* Invalid state -- bail */
slouken@1895
   520
        return (0);
slouken@1895
   521
    }
slouken@1361
   522
#endif /* !SDL_EVENTS_DISABLED */
slouken@0
   523
slouken@1895
   524
    /* Update internal joystick state */
slouken@1895
   525
    joystick->buttons[button] = state;
slouken@0
   526
slouken@1895
   527
    /* Post the event, if desired */
slouken@1895
   528
    posted = 0;
slouken@1361
   529
#if !SDL_EVENTS_DISABLED
slouken@4429
   530
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@1895
   531
        event.jbutton.which = joystick->index;
slouken@1895
   532
        event.jbutton.button = button;
slouken@1895
   533
        event.jbutton.state = state;
slouken@1895
   534
        if ((SDL_EventOK == NULL)
slouken@1895
   535
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@1895
   536
            posted = 1;
slouken@1895
   537
            SDL_PushEvent(&event);
slouken@1895
   538
        }
slouken@1895
   539
    }
slouken@1361
   540
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   541
    return (posted);
slouken@0
   542
}
slouken@0
   543
slouken@1895
   544
void
slouken@1895
   545
SDL_JoystickUpdate(void)
slouken@0
   546
{
slouken@1895
   547
    int i;
slouken@0
   548
slouken@1895
   549
    for (i = 0; SDL_joysticks[i]; ++i) {
slouken@1895
   550
        SDL_SYS_JoystickUpdate(SDL_joysticks[i]);
slouken@1895
   551
    }
slouken@0
   552
}
slouken@0
   553
slouken@1895
   554
int
slouken@1895
   555
SDL_JoystickEventState(int state)
slouken@0
   556
{
slouken@1361
   557
#if SDL_EVENTS_DISABLED
slouken@1895
   558
    return SDL_IGNORE;
slouken@0
   559
#else
slouken@4429
   560
    const Uint32 event_list[] = {
slouken@1895
   561
        SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION,
slouken@1895
   562
        SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP,
slouken@1895
   563
    };
slouken@1895
   564
    unsigned int i;
slouken@0
   565
slouken@1895
   566
    switch (state) {
slouken@1895
   567
    case SDL_QUERY:
slouken@1895
   568
        state = SDL_IGNORE;
slouken@1895
   569
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@1895
   570
            state = SDL_EventState(event_list[i], SDL_QUERY);
slouken@1895
   571
            if (state == SDL_ENABLE) {
slouken@1895
   572
                break;
slouken@1895
   573
            }
slouken@1895
   574
        }
slouken@1895
   575
        break;
slouken@1895
   576
    default:
slouken@1895
   577
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@1895
   578
            SDL_EventState(event_list[i], state);
slouken@1895
   579
        }
slouken@1895
   580
        break;
slouken@1895
   581
    }
slouken@1895
   582
    return (state);
slouken@1361
   583
#endif /* SDL_EVENTS_DISABLED */
slouken@0
   584
}
slouken@1895
   585
slouken@1895
   586
/* vi: set ts=4 sw=4 expandtab: */