src/joystick/SDL_joystick.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 01 Sep 2011 14:02:12 -0400
changeset 5861 b781fa6468aa
parent 5859 db254478ed2d
child 6095 32d8fad8d7ad
permissions -rw-r--r--
Patched to compile.
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"
icculus@5861
    28
#include "SDL_assert.h"
icculus@5861
    29
slouken@1361
    30
#if !SDL_EVENTS_DISABLED
slouken@1361
    31
#include "../events/SDL_events_c.h"
slouken@0
    32
#endif
slouken@0
    33
slouken@0
    34
Uint8 SDL_numjoysticks = 0;
slouken@0
    35
SDL_Joystick **SDL_joysticks = NULL;
slouken@0
    36
slouken@1895
    37
int
slouken@1895
    38
SDL_JoystickInit(void)
slouken@0
    39
{
slouken@1895
    40
    int arraylen;
slouken@1895
    41
    int status;
slouken@0
    42
slouken@1895
    43
    SDL_numjoysticks = 0;
slouken@1895
    44
    status = SDL_SYS_JoystickInit();
slouken@1895
    45
    if (status >= 0) {
slouken@1895
    46
        arraylen = (status + 1) * sizeof(*SDL_joysticks);
slouken@1895
    47
        SDL_joysticks = (SDL_Joystick **) SDL_malloc(arraylen);
slouken@1895
    48
        if (SDL_joysticks == NULL) {
slouken@1895
    49
            SDL_numjoysticks = 0;
slouken@1895
    50
        } else {
slouken@1895
    51
            SDL_memset(SDL_joysticks, 0, arraylen);
slouken@1895
    52
            SDL_numjoysticks = status;
slouken@1895
    53
        }
slouken@1895
    54
        status = 0;
slouken@1895
    55
    }
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
        SDL_SetError("Joystick hasn't been opened yet");
slouken@1895
   197
        valid = 0;
slouken@1895
   198
    } else {
slouken@1895
   199
        valid = 1;
slouken@1895
   200
    }
slouken@1895
   201
    return valid;
slouken@0
   202
}
slouken@0
   203
slouken@0
   204
/*
slouken@0
   205
 * Get the device index of an opened joystick.
slouken@0
   206
 */
slouken@1895
   207
int
slouken@1895
   208
SDL_JoystickIndex(SDL_Joystick * joystick)
slouken@0
   209
{
slouken@2713
   210
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   211
        return (-1);
slouken@1895
   212
    }
slouken@1895
   213
    return (joystick->index);
slouken@0
   214
}
slouken@0
   215
slouken@0
   216
/*
slouken@0
   217
 * Get the number of multi-dimensional axis controls on a joystick
slouken@0
   218
 */
slouken@1895
   219
int
slouken@1895
   220
SDL_JoystickNumAxes(SDL_Joystick * joystick)
slouken@0
   221
{
slouken@2713
   222
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   223
        return (-1);
slouken@1895
   224
    }
slouken@1895
   225
    return (joystick->naxes);
slouken@0
   226
}
slouken@0
   227
slouken@0
   228
/*
slouken@0
   229
 * Get the number of hats on a joystick
slouken@0
   230
 */
slouken@1895
   231
int
slouken@1895
   232
SDL_JoystickNumHats(SDL_Joystick * joystick)
slouken@0
   233
{
slouken@2713
   234
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   235
        return (-1);
slouken@1895
   236
    }
slouken@1895
   237
    return (joystick->nhats);
slouken@0
   238
}
slouken@0
   239
slouken@0
   240
/*
slouken@0
   241
 * Get the number of trackballs on a joystick
slouken@0
   242
 */
slouken@1895
   243
int
slouken@1895
   244
SDL_JoystickNumBalls(SDL_Joystick * joystick)
slouken@0
   245
{
slouken@2713
   246
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   247
        return (-1);
slouken@1895
   248
    }
slouken@1895
   249
    return (joystick->nballs);
slouken@0
   250
}
slouken@0
   251
slouken@0
   252
/*
slouken@0
   253
 * Get the number of buttons on a joystick
slouken@0
   254
 */
slouken@1895
   255
int
slouken@1895
   256
SDL_JoystickNumButtons(SDL_Joystick * joystick)
slouken@0
   257
{
slouken@2713
   258
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   259
        return (-1);
slouken@1895
   260
    }
slouken@1895
   261
    return (joystick->nbuttons);
slouken@0
   262
}
slouken@0
   263
slouken@0
   264
/*
slouken@0
   265
 * Get the current state of an axis control on a joystick
slouken@0
   266
 */
slouken@1895
   267
Sint16
slouken@1895
   268
SDL_JoystickGetAxis(SDL_Joystick * joystick, int axis)
slouken@0
   269
{
slouken@1895
   270
    Sint16 state;
slouken@0
   271
slouken@2713
   272
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   273
        return (0);
slouken@1895
   274
    }
slouken@1895
   275
    if (axis < joystick->naxes) {
slouken@1895
   276
        state = joystick->axes[axis];
slouken@1895
   277
    } else {
slouken@1895
   278
        SDL_SetError("Joystick only has %d axes", joystick->naxes);
slouken@1895
   279
        state = 0;
slouken@1895
   280
    }
slouken@1895
   281
    return (state);
slouken@0
   282
}
slouken@0
   283
slouken@0
   284
/*
slouken@0
   285
 * Get the current state of a hat on a joystick
slouken@0
   286
 */
slouken@1895
   287
Uint8
slouken@1895
   288
SDL_JoystickGetHat(SDL_Joystick * joystick, int hat)
slouken@0
   289
{
slouken@1895
   290
    Uint8 state;
slouken@0
   291
slouken@2713
   292
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   293
        return (0);
slouken@1895
   294
    }
slouken@1895
   295
    if (hat < joystick->nhats) {
slouken@1895
   296
        state = joystick->hats[hat];
slouken@1895
   297
    } else {
slouken@1895
   298
        SDL_SetError("Joystick only has %d hats", joystick->nhats);
slouken@1895
   299
        state = 0;
slouken@1895
   300
    }
slouken@1895
   301
    return (state);
slouken@0
   302
}
slouken@0
   303
slouken@0
   304
/*
slouken@0
   305
 * Get the ball axis change since the last poll
slouken@0
   306
 */
slouken@1895
   307
int
slouken@1895
   308
SDL_JoystickGetBall(SDL_Joystick * joystick, int ball, int *dx, int *dy)
slouken@0
   309
{
slouken@1895
   310
    int retval;
slouken@0
   311
slouken@2713
   312
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   313
        return (-1);
slouken@1895
   314
    }
slouken@0
   315
slouken@1895
   316
    retval = 0;
slouken@1895
   317
    if (ball < joystick->nballs) {
slouken@1895
   318
        if (dx) {
slouken@1895
   319
            *dx = joystick->balls[ball].dx;
slouken@1895
   320
        }
slouken@1895
   321
        if (dy) {
slouken@1895
   322
            *dy = joystick->balls[ball].dy;
slouken@1895
   323
        }
slouken@1895
   324
        joystick->balls[ball].dx = 0;
slouken@1895
   325
        joystick->balls[ball].dy = 0;
slouken@1895
   326
    } else {
slouken@1895
   327
        SDL_SetError("Joystick only has %d balls", joystick->nballs);
slouken@1895
   328
        retval = -1;
slouken@1895
   329
    }
slouken@1895
   330
    return (retval);
slouken@0
   331
}
slouken@0
   332
slouken@0
   333
/*
slouken@0
   334
 * Get the current state of a button on a joystick
slouken@0
   335
 */
slouken@1895
   336
Uint8
slouken@1895
   337
SDL_JoystickGetButton(SDL_Joystick * joystick, int button)
slouken@0
   338
{
slouken@1895
   339
    Uint8 state;
slouken@0
   340
slouken@2713
   341
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   342
        return (0);
slouken@1895
   343
    }
slouken@1895
   344
    if (button < joystick->nbuttons) {
slouken@1895
   345
        state = joystick->buttons[button];
slouken@1895
   346
    } else {
slouken@1895
   347
        SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
slouken@1895
   348
        state = 0;
slouken@1895
   349
    }
slouken@1895
   350
    return (state);
slouken@0
   351
}
slouken@0
   352
slouken@0
   353
/*
slouken@0
   354
 * Close a joystick previously opened with SDL_JoystickOpen()
slouken@0
   355
 */
slouken@1895
   356
void
slouken@1895
   357
SDL_JoystickClose(SDL_Joystick * joystick)
slouken@0
   358
{
slouken@1895
   359
    int i;
slouken@0
   360
slouken@2713
   361
    if (!SDL_PrivateJoystickValid(&joystick)) {
slouken@1895
   362
        return;
slouken@1895
   363
    }
slouken@0
   364
slouken@1895
   365
    /* First decrement ref count */
slouken@1895
   366
    if (--joystick->ref_count > 0) {
slouken@1895
   367
        return;
slouken@1895
   368
    }
slouken@0
   369
slouken@1895
   370
    SDL_SYS_JoystickClose(joystick);
slouken@0
   371
slouken@1895
   372
    /* Remove joystick from list */
slouken@1895
   373
    for (i = 0; SDL_joysticks[i]; ++i) {
slouken@1895
   374
        if (joystick == SDL_joysticks[i]) {
slouken@3369
   375
            SDL_memmove(&SDL_joysticks[i], &SDL_joysticks[i + 1],
slouken@3369
   376
                        (SDL_numjoysticks - i) * sizeof(joystick));
slouken@1895
   377
            break;
slouken@1895
   378
        }
slouken@1895
   379
    }
slouken@0
   380
slouken@1895
   381
    /* Free the data associated with this joystick */
slouken@1895
   382
    if (joystick->axes) {
slouken@1895
   383
        SDL_free(joystick->axes);
slouken@1895
   384
    }
slouken@1895
   385
    if (joystick->hats) {
slouken@1895
   386
        SDL_free(joystick->hats);
slouken@1895
   387
    }
slouken@1895
   388
    if (joystick->balls) {
slouken@1895
   389
        SDL_free(joystick->balls);
slouken@1895
   390
    }
slouken@1895
   391
    if (joystick->buttons) {
slouken@1895
   392
        SDL_free(joystick->buttons);
slouken@1895
   393
    }
slouken@1895
   394
    SDL_free(joystick);
slouken@0
   395
}
slouken@0
   396
slouken@1895
   397
void
slouken@1895
   398
SDL_JoystickQuit(void)
slouken@0
   399
{
icculus@5856
   400
    const int numsticks = SDL_numjoysticks;
icculus@5856
   401
    int i;
icculus@5856
   402
slouken@1895
   403
    /* Stop the event polling */
slouken@1895
   404
    SDL_numjoysticks = 0;
slouken@0
   405
icculus@5856
   406
    SDL_assert( (SDL_joysticks == NULL) == (numsticks == 0) );
icculus@5856
   407
icculus@5856
   408
    for (i = 0; i < numsticks; i++) {
icculus@5856
   409
        SDL_Joystick *stick = SDL_joysticks[i];
icculus@5856
   410
        if (stick && (stick->ref_count >= 1)) {
icculus@5856
   411
            stick->ref_count = 1;
icculus@5856
   412
            SDL_JoystickClose(stick);
icculus@5856
   413
        }
icculus@5856
   414
    }
icculus@5856
   415
slouken@1895
   416
    /* Quit the joystick setup */
slouken@1895
   417
    SDL_SYS_JoystickQuit();
slouken@1895
   418
    if (SDL_joysticks) {
slouken@1895
   419
        SDL_free(SDL_joysticks);
slouken@1895
   420
        SDL_joysticks = NULL;
slouken@1895
   421
    }
slouken@0
   422
}
slouken@0
   423
slouken@0
   424
slouken@0
   425
/* These are global for SDL_sysjoystick.c and SDL_events.c */
slouken@0
   426
slouken@1895
   427
int
slouken@1895
   428
SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
slouken@0
   429
{
slouken@1895
   430
    int posted;
slouken@0
   431
slouken@1895
   432
    /* Update internal joystick state */
slouken@1895
   433
    joystick->axes[axis] = value;
slouken@0
   434
slouken@1895
   435
    /* Post the event, if desired */
slouken@1895
   436
    posted = 0;
slouken@1361
   437
#if !SDL_EVENTS_DISABLED
slouken@4429
   438
    if (SDL_GetEventState(SDL_JOYAXISMOTION) == SDL_ENABLE) {
slouken@1895
   439
        SDL_Event event;
slouken@1895
   440
        event.type = SDL_JOYAXISMOTION;
slouken@1895
   441
        event.jaxis.which = joystick->index;
slouken@1895
   442
        event.jaxis.axis = axis;
slouken@1895
   443
        event.jaxis.value = value;
slouken@1895
   444
        if ((SDL_EventOK == NULL)
slouken@1895
   445
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@1895
   446
            posted = 1;
slouken@1895
   447
            SDL_PushEvent(&event);
slouken@1895
   448
        }
slouken@1895
   449
    }
slouken@1361
   450
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   451
    return (posted);
slouken@0
   452
}
slouken@0
   453
slouken@1895
   454
int
slouken@1895
   455
SDL_PrivateJoystickHat(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
slouken@0
   456
{
slouken@1895
   457
    int posted;
slouken@0
   458
slouken@1895
   459
    /* Update internal joystick state */
slouken@1895
   460
    joystick->hats[hat] = value;
slouken@0
   461
slouken@1895
   462
    /* Post the event, if desired */
slouken@1895
   463
    posted = 0;
slouken@1361
   464
#if !SDL_EVENTS_DISABLED
slouken@4429
   465
    if (SDL_GetEventState(SDL_JOYHATMOTION) == SDL_ENABLE) {
slouken@1895
   466
        SDL_Event event;
slouken@1895
   467
        event.jhat.type = SDL_JOYHATMOTION;
slouken@1895
   468
        event.jhat.which = joystick->index;
slouken@1895
   469
        event.jhat.hat = hat;
slouken@1895
   470
        event.jhat.value = value;
slouken@1895
   471
        if ((SDL_EventOK == NULL)
slouken@1895
   472
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@1895
   473
            posted = 1;
slouken@1895
   474
            SDL_PushEvent(&event);
slouken@1895
   475
        }
slouken@1895
   476
    }
slouken@1361
   477
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   478
    return (posted);
slouken@0
   479
}
slouken@0
   480
slouken@1895
   481
int
slouken@1895
   482
SDL_PrivateJoystickBall(SDL_Joystick * joystick, Uint8 ball,
slouken@1895
   483
                        Sint16 xrel, Sint16 yrel)
slouken@0
   484
{
slouken@1895
   485
    int posted;
slouken@0
   486
slouken@1895
   487
    /* Update internal mouse state */
slouken@1895
   488
    joystick->balls[ball].dx += xrel;
slouken@1895
   489
    joystick->balls[ball].dy += yrel;
slouken@0
   490
slouken@1895
   491
    /* Post the event, if desired */
slouken@1895
   492
    posted = 0;
slouken@1361
   493
#if !SDL_EVENTS_DISABLED
slouken@4429
   494
    if (SDL_GetEventState(SDL_JOYBALLMOTION) == SDL_ENABLE) {
slouken@1895
   495
        SDL_Event event;
slouken@1895
   496
        event.jball.type = SDL_JOYBALLMOTION;
slouken@1895
   497
        event.jball.which = joystick->index;
slouken@1895
   498
        event.jball.ball = ball;
slouken@1895
   499
        event.jball.xrel = xrel;
slouken@1895
   500
        event.jball.yrel = yrel;
slouken@1895
   501
        if ((SDL_EventOK == NULL)
slouken@1895
   502
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@1895
   503
            posted = 1;
slouken@1895
   504
            SDL_PushEvent(&event);
slouken@1895
   505
        }
slouken@1895
   506
    }
slouken@1361
   507
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   508
    return (posted);
slouken@0
   509
}
slouken@0
   510
slouken@1895
   511
int
slouken@1895
   512
SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
slouken@0
   513
{
slouken@1895
   514
    int posted;
slouken@1361
   515
#if !SDL_EVENTS_DISABLED
slouken@1895
   516
    SDL_Event event;
slouken@0
   517
slouken@1895
   518
    switch (state) {
slouken@1895
   519
    case SDL_PRESSED:
slouken@1895
   520
        event.type = SDL_JOYBUTTONDOWN;
slouken@1895
   521
        break;
slouken@1895
   522
    case SDL_RELEASED:
slouken@1895
   523
        event.type = SDL_JOYBUTTONUP;
slouken@1895
   524
        break;
slouken@1895
   525
    default:
slouken@1895
   526
        /* Invalid state -- bail */
slouken@1895
   527
        return (0);
slouken@1895
   528
    }
slouken@1361
   529
#endif /* !SDL_EVENTS_DISABLED */
slouken@0
   530
slouken@1895
   531
    /* Update internal joystick state */
slouken@1895
   532
    joystick->buttons[button] = state;
slouken@0
   533
slouken@1895
   534
    /* Post the event, if desired */
slouken@1895
   535
    posted = 0;
slouken@1361
   536
#if !SDL_EVENTS_DISABLED
slouken@4429
   537
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@1895
   538
        event.jbutton.which = joystick->index;
slouken@1895
   539
        event.jbutton.button = button;
slouken@1895
   540
        event.jbutton.state = state;
slouken@1895
   541
        if ((SDL_EventOK == NULL)
slouken@1895
   542
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@1895
   543
            posted = 1;
slouken@1895
   544
            SDL_PushEvent(&event);
slouken@1895
   545
        }
slouken@1895
   546
    }
slouken@1361
   547
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   548
    return (posted);
slouken@0
   549
}
slouken@0
   550
slouken@1895
   551
void
slouken@1895
   552
SDL_JoystickUpdate(void)
slouken@0
   553
{
slouken@1895
   554
    int i;
slouken@0
   555
slouken@1895
   556
    for (i = 0; SDL_joysticks[i]; ++i) {
slouken@1895
   557
        SDL_SYS_JoystickUpdate(SDL_joysticks[i]);
slouken@1895
   558
    }
slouken@0
   559
}
slouken@0
   560
slouken@1895
   561
int
slouken@1895
   562
SDL_JoystickEventState(int state)
slouken@0
   563
{
slouken@1361
   564
#if SDL_EVENTS_DISABLED
slouken@1895
   565
    return SDL_IGNORE;
slouken@0
   566
#else
slouken@4429
   567
    const Uint32 event_list[] = {
slouken@1895
   568
        SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION,
slouken@1895
   569
        SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP,
slouken@1895
   570
    };
slouken@1895
   571
    unsigned int i;
slouken@0
   572
slouken@1895
   573
    switch (state) {
slouken@1895
   574
    case SDL_QUERY:
slouken@1895
   575
        state = SDL_IGNORE;
slouken@1895
   576
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@1895
   577
            state = SDL_EventState(event_list[i], SDL_QUERY);
slouken@1895
   578
            if (state == SDL_ENABLE) {
slouken@1895
   579
                break;
slouken@1895
   580
            }
slouken@1895
   581
        }
slouken@1895
   582
        break;
slouken@1895
   583
    default:
slouken@1895
   584
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@1895
   585
            SDL_EventState(event_list[i], state);
slouken@1895
   586
        }
slouken@1895
   587
        break;
slouken@1895
   588
    }
slouken@1895
   589
    return (state);
slouken@1361
   590
#endif /* SDL_EVENTS_DISABLED */
slouken@0
   591
}
slouken@1895
   592
slouken@1895
   593
/* vi: set ts=4 sw=4 expandtab: */