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