src/joystick/SDL_joystick.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 25 Mar 2010 01:08:26 -0700
changeset 4429 faa9fc8e7f67
parent 3697 f7b03b6838cb
child 5123 dc0dfdd58f27
permissions -rw-r--r--
General improvements for user custom event registration

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