src/joystick/SDL_joystick.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1612 97d0966f4bf7
child 2489 96adc8025331
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 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));
slouken@1895
   117
    if (joystick != NULL) {
slouken@1895
   118
        SDL_memset(joystick, 0, (sizeof *joystick));
slouken@1895
   119
        joystick->index = device_index;
slouken@1895
   120
        if (SDL_SYS_JoystickOpen(joystick) < 0) {
slouken@1895
   121
            SDL_free(joystick);
slouken@1895
   122
            joystick = NULL;
slouken@1895
   123
        } else {
slouken@1895
   124
            if (joystick->naxes > 0) {
slouken@1895
   125
                joystick->axes = (Sint16 *) SDL_malloc
slouken@1895
   126
                    (joystick->naxes * sizeof(Sint16));
slouken@1895
   127
            }
slouken@1895
   128
            if (joystick->nhats > 0) {
slouken@1895
   129
                joystick->hats = (Uint8 *) SDL_malloc
slouken@1895
   130
                    (joystick->nhats * sizeof(Uint8));
slouken@1895
   131
            }
slouken@1895
   132
            if (joystick->nballs > 0) {
slouken@1895
   133
                joystick->balls = (struct balldelta *) SDL_malloc
slouken@1895
   134
                    (joystick->nballs * sizeof(*joystick->balls));
slouken@1895
   135
            }
slouken@1895
   136
            if (joystick->nbuttons > 0) {
slouken@1895
   137
                joystick->buttons = (Uint8 *) SDL_malloc
slouken@1895
   138
                    (joystick->nbuttons * sizeof(Uint8));
slouken@1895
   139
            }
slouken@1895
   140
            if (((joystick->naxes > 0) && !joystick->axes)
slouken@1895
   141
                || ((joystick->nhats > 0) && !joystick->hats)
slouken@1895
   142
                || ((joystick->nballs > 0) && !joystick->balls)
slouken@1895
   143
                || ((joystick->nbuttons > 0) && !joystick->buttons)) {
slouken@1895
   144
                SDL_OutOfMemory();
slouken@1895
   145
                SDL_JoystickClose(joystick);
slouken@1895
   146
                joystick = NULL;
slouken@1895
   147
            }
slouken@1895
   148
            if (joystick->axes) {
slouken@1895
   149
                SDL_memset(joystick->axes, 0,
slouken@1895
   150
                           joystick->naxes * sizeof(Sint16));
slouken@1895
   151
            }
slouken@1895
   152
            if (joystick->hats) {
slouken@1895
   153
                SDL_memset(joystick->hats, 0,
slouken@1895
   154
                           joystick->nhats * sizeof(Uint8));
slouken@1895
   155
            }
slouken@1895
   156
            if (joystick->balls) {
slouken@1895
   157
                SDL_memset(joystick->balls, 0,
slouken@1895
   158
                           joystick->nballs * sizeof(*joystick->balls));
slouken@1895
   159
            }
slouken@1895
   160
            if (joystick->buttons) {
slouken@1895
   161
                SDL_memset(joystick->buttons, 0,
slouken@1895
   162
                           joystick->nbuttons * sizeof(Uint8));
slouken@1895
   163
            }
slouken@1895
   164
        }
slouken@1895
   165
    }
slouken@1895
   166
    if (joystick) {
slouken@1895
   167
        /* Add joystick to list */
slouken@1895
   168
        ++joystick->ref_count;
slouken@1895
   169
        SDL_Lock_EventThread();
slouken@1895
   170
        for (i = 0; SDL_joysticks[i]; ++i)
slouken@1895
   171
            /* Skip to next joystick */ ;
slouken@1895
   172
        SDL_joysticks[i] = joystick;
slouken@1895
   173
        SDL_Unlock_EventThread();
slouken@1895
   174
    }
slouken@1895
   175
    return (joystick);
slouken@0
   176
}
slouken@0
   177
slouken@0
   178
/*
slouken@0
   179
 * Returns 1 if the joystick has been opened, or 0 if it has not.
slouken@0
   180
 */
slouken@1895
   181
int
slouken@1895
   182
SDL_JoystickOpened(int device_index)
slouken@0
   183
{
slouken@1895
   184
    int i, opened;
slouken@0
   185
slouken@1895
   186
    opened = 0;
slouken@1895
   187
    for (i = 0; SDL_joysticks[i]; ++i) {
slouken@1895
   188
        if (SDL_joysticks[i]->index == (Uint8) device_index) {
slouken@1895
   189
            opened = 1;
slouken@1895
   190
            break;
slouken@1895
   191
        }
slouken@1895
   192
    }
slouken@1895
   193
    return (opened);
slouken@0
   194
}
slouken@0
   195
slouken@1895
   196
static int
slouken@1895
   197
ValidJoystick(SDL_Joystick ** joystick)
slouken@0
   198
{
slouken@1895
   199
    int valid;
slouken@0
   200
slouken@1895
   201
    if (*joystick == NULL) {
slouken@1895
   202
        *joystick = default_joystick;
slouken@1895
   203
    }
slouken@1895
   204
    if (*joystick == NULL) {
slouken@1895
   205
        SDL_SetError("Joystick hasn't been opened yet");
slouken@1895
   206
        valid = 0;
slouken@1895
   207
    } else {
slouken@1895
   208
        valid = 1;
slouken@1895
   209
    }
slouken@1895
   210
    return valid;
slouken@0
   211
}
slouken@0
   212
slouken@0
   213
/*
slouken@0
   214
 * Get the device index of an opened joystick.
slouken@0
   215
 */
slouken@1895
   216
int
slouken@1895
   217
SDL_JoystickIndex(SDL_Joystick * joystick)
slouken@0
   218
{
slouken@1895
   219
    if (!ValidJoystick(&joystick)) {
slouken@1895
   220
        return (-1);
slouken@1895
   221
    }
slouken@1895
   222
    return (joystick->index);
slouken@0
   223
}
slouken@0
   224
slouken@0
   225
/*
slouken@0
   226
 * Get the number of multi-dimensional axis controls on a joystick
slouken@0
   227
 */
slouken@1895
   228
int
slouken@1895
   229
SDL_JoystickNumAxes(SDL_Joystick * joystick)
slouken@0
   230
{
slouken@1895
   231
    if (!ValidJoystick(&joystick)) {
slouken@1895
   232
        return (-1);
slouken@1895
   233
    }
slouken@1895
   234
    return (joystick->naxes);
slouken@0
   235
}
slouken@0
   236
slouken@0
   237
/*
slouken@0
   238
 * Get the number of hats on a joystick
slouken@0
   239
 */
slouken@1895
   240
int
slouken@1895
   241
SDL_JoystickNumHats(SDL_Joystick * joystick)
slouken@0
   242
{
slouken@1895
   243
    if (!ValidJoystick(&joystick)) {
slouken@1895
   244
        return (-1);
slouken@1895
   245
    }
slouken@1895
   246
    return (joystick->nhats);
slouken@0
   247
}
slouken@0
   248
slouken@0
   249
/*
slouken@0
   250
 * Get the number of trackballs on a joystick
slouken@0
   251
 */
slouken@1895
   252
int
slouken@1895
   253
SDL_JoystickNumBalls(SDL_Joystick * joystick)
slouken@0
   254
{
slouken@1895
   255
    if (!ValidJoystick(&joystick)) {
slouken@1895
   256
        return (-1);
slouken@1895
   257
    }
slouken@1895
   258
    return (joystick->nballs);
slouken@0
   259
}
slouken@0
   260
slouken@0
   261
/*
slouken@0
   262
 * Get the number of buttons on a joystick
slouken@0
   263
 */
slouken@1895
   264
int
slouken@1895
   265
SDL_JoystickNumButtons(SDL_Joystick * joystick)
slouken@0
   266
{
slouken@1895
   267
    if (!ValidJoystick(&joystick)) {
slouken@1895
   268
        return (-1);
slouken@1895
   269
    }
slouken@1895
   270
    return (joystick->nbuttons);
slouken@0
   271
}
slouken@0
   272
slouken@0
   273
/*
slouken@0
   274
 * Get the current state of an axis control on a joystick
slouken@0
   275
 */
slouken@1895
   276
Sint16
slouken@1895
   277
SDL_JoystickGetAxis(SDL_Joystick * joystick, int axis)
slouken@0
   278
{
slouken@1895
   279
    Sint16 state;
slouken@0
   280
slouken@1895
   281
    if (!ValidJoystick(&joystick)) {
slouken@1895
   282
        return (0);
slouken@1895
   283
    }
slouken@1895
   284
    if (axis < joystick->naxes) {
slouken@1895
   285
        state = joystick->axes[axis];
slouken@1895
   286
    } else {
slouken@1895
   287
        SDL_SetError("Joystick only has %d axes", joystick->naxes);
slouken@1895
   288
        state = 0;
slouken@1895
   289
    }
slouken@1895
   290
    return (state);
slouken@0
   291
}
slouken@0
   292
slouken@0
   293
/*
slouken@0
   294
 * Get the current state of a hat on a joystick
slouken@0
   295
 */
slouken@1895
   296
Uint8
slouken@1895
   297
SDL_JoystickGetHat(SDL_Joystick * joystick, int hat)
slouken@0
   298
{
slouken@1895
   299
    Uint8 state;
slouken@0
   300
slouken@1895
   301
    if (!ValidJoystick(&joystick)) {
slouken@1895
   302
        return (0);
slouken@1895
   303
    }
slouken@1895
   304
    if (hat < joystick->nhats) {
slouken@1895
   305
        state = joystick->hats[hat];
slouken@1895
   306
    } else {
slouken@1895
   307
        SDL_SetError("Joystick only has %d hats", joystick->nhats);
slouken@1895
   308
        state = 0;
slouken@1895
   309
    }
slouken@1895
   310
    return (state);
slouken@0
   311
}
slouken@0
   312
slouken@0
   313
/*
slouken@0
   314
 * Get the ball axis change since the last poll
slouken@0
   315
 */
slouken@1895
   316
int
slouken@1895
   317
SDL_JoystickGetBall(SDL_Joystick * joystick, int ball, int *dx, int *dy)
slouken@0
   318
{
slouken@1895
   319
    int retval;
slouken@0
   320
slouken@1895
   321
    if (!ValidJoystick(&joystick)) {
slouken@1895
   322
        return (-1);
slouken@1895
   323
    }
slouken@0
   324
slouken@1895
   325
    retval = 0;
slouken@1895
   326
    if (ball < joystick->nballs) {
slouken@1895
   327
        if (dx) {
slouken@1895
   328
            *dx = joystick->balls[ball].dx;
slouken@1895
   329
        }
slouken@1895
   330
        if (dy) {
slouken@1895
   331
            *dy = joystick->balls[ball].dy;
slouken@1895
   332
        }
slouken@1895
   333
        joystick->balls[ball].dx = 0;
slouken@1895
   334
        joystick->balls[ball].dy = 0;
slouken@1895
   335
    } else {
slouken@1895
   336
        SDL_SetError("Joystick only has %d balls", joystick->nballs);
slouken@1895
   337
        retval = -1;
slouken@1895
   338
    }
slouken@1895
   339
    return (retval);
slouken@0
   340
}
slouken@0
   341
slouken@0
   342
/*
slouken@0
   343
 * Get the current state of a button on a joystick
slouken@0
   344
 */
slouken@1895
   345
Uint8
slouken@1895
   346
SDL_JoystickGetButton(SDL_Joystick * joystick, int button)
slouken@0
   347
{
slouken@1895
   348
    Uint8 state;
slouken@0
   349
slouken@1895
   350
    if (!ValidJoystick(&joystick)) {
slouken@1895
   351
        return (0);
slouken@1895
   352
    }
slouken@1895
   353
    if (button < joystick->nbuttons) {
slouken@1895
   354
        state = joystick->buttons[button];
slouken@1895
   355
    } else {
slouken@1895
   356
        SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
slouken@1895
   357
        state = 0;
slouken@1895
   358
    }
slouken@1895
   359
    return (state);
slouken@0
   360
}
slouken@0
   361
slouken@0
   362
/*
slouken@0
   363
 * Close a joystick previously opened with SDL_JoystickOpen()
slouken@0
   364
 */
slouken@1895
   365
void
slouken@1895
   366
SDL_JoystickClose(SDL_Joystick * joystick)
slouken@0
   367
{
slouken@1895
   368
    int i;
slouken@0
   369
slouken@1895
   370
    if (!ValidJoystick(&joystick)) {
slouken@1895
   371
        return;
slouken@1895
   372
    }
slouken@0
   373
slouken@1895
   374
    /* First decrement ref count */
slouken@1895
   375
    if (--joystick->ref_count > 0) {
slouken@1895
   376
        return;
slouken@1895
   377
    }
slouken@0
   378
slouken@1895
   379
    /* Lock the event queue - prevent joystick polling */
slouken@1895
   380
    SDL_Lock_EventThread();
slouken@0
   381
slouken@1895
   382
    if (joystick == default_joystick) {
slouken@1895
   383
        default_joystick = NULL;
slouken@1895
   384
    }
slouken@1895
   385
    SDL_SYS_JoystickClose(joystick);
slouken@0
   386
slouken@1895
   387
    /* Remove joystick from list */
slouken@1895
   388
    for (i = 0; SDL_joysticks[i]; ++i) {
slouken@1895
   389
        if (joystick == SDL_joysticks[i]) {
slouken@1895
   390
            SDL_memcpy(&SDL_joysticks[i], &SDL_joysticks[i + 1],
slouken@1895
   391
                       (SDL_numjoysticks - i) * sizeof(joystick));
slouken@1895
   392
            break;
slouken@1895
   393
        }
slouken@1895
   394
    }
slouken@0
   395
slouken@1895
   396
    /* Let the event thread keep running */
slouken@1895
   397
    SDL_Unlock_EventThread();
slouken@0
   398
slouken@1895
   399
    /* Free the data associated with this joystick */
slouken@1895
   400
    if (joystick->axes) {
slouken@1895
   401
        SDL_free(joystick->axes);
slouken@1895
   402
    }
slouken@1895
   403
    if (joystick->hats) {
slouken@1895
   404
        SDL_free(joystick->hats);
slouken@1895
   405
    }
slouken@1895
   406
    if (joystick->balls) {
slouken@1895
   407
        SDL_free(joystick->balls);
slouken@1895
   408
    }
slouken@1895
   409
    if (joystick->buttons) {
slouken@1895
   410
        SDL_free(joystick->buttons);
slouken@1895
   411
    }
slouken@1895
   412
    SDL_free(joystick);
slouken@0
   413
}
slouken@0
   414
slouken@1895
   415
void
slouken@1895
   416
SDL_JoystickQuit(void)
slouken@0
   417
{
slouken@1895
   418
    /* Stop the event polling */
slouken@1895
   419
    SDL_Lock_EventThread();
slouken@1895
   420
    SDL_numjoysticks = 0;
slouken@1895
   421
    SDL_Unlock_EventThread();
slouken@0
   422
slouken@1895
   423
    /* Quit the joystick setup */
slouken@1895
   424
    SDL_SYS_JoystickQuit();
slouken@1895
   425
    if (SDL_joysticks) {
slouken@1895
   426
        SDL_free(SDL_joysticks);
slouken@1895
   427
        SDL_joysticks = NULL;
slouken@1895
   428
    }
slouken@0
   429
}
slouken@0
   430
slouken@0
   431
slouken@0
   432
/* These are global for SDL_sysjoystick.c and SDL_events.c */
slouken@0
   433
slouken@1895
   434
int
slouken@1895
   435
SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
slouken@0
   436
{
slouken@1895
   437
    int posted;
slouken@0
   438
slouken@1895
   439
    /* Update internal joystick state */
slouken@1895
   440
    joystick->axes[axis] = value;
slouken@0
   441
slouken@1895
   442
    /* Post the event, if desired */
slouken@1895
   443
    posted = 0;
slouken@1361
   444
#if !SDL_EVENTS_DISABLED
slouken@1895
   445
    if (SDL_ProcessEvents[SDL_JOYAXISMOTION] == SDL_ENABLE) {
slouken@1895
   446
        SDL_Event event;
slouken@1895
   447
        event.type = SDL_JOYAXISMOTION;
slouken@1895
   448
        event.jaxis.which = joystick->index;
slouken@1895
   449
        event.jaxis.axis = axis;
slouken@1895
   450
        event.jaxis.value = value;
slouken@1895
   451
        if ((SDL_EventOK == NULL)
slouken@1895
   452
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@1895
   453
            posted = 1;
slouken@1895
   454
            SDL_PushEvent(&event);
slouken@1895
   455
        }
slouken@1895
   456
    }
slouken@1361
   457
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   458
    return (posted);
slouken@0
   459
}
slouken@0
   460
slouken@1895
   461
int
slouken@1895
   462
SDL_PrivateJoystickHat(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
slouken@0
   463
{
slouken@1895
   464
    int posted;
slouken@0
   465
slouken@1895
   466
    /* Update internal joystick state */
slouken@1895
   467
    joystick->hats[hat] = value;
slouken@0
   468
slouken@1895
   469
    /* Post the event, if desired */
slouken@1895
   470
    posted = 0;
slouken@1361
   471
#if !SDL_EVENTS_DISABLED
slouken@1895
   472
    if (SDL_ProcessEvents[SDL_JOYHATMOTION] == SDL_ENABLE) {
slouken@1895
   473
        SDL_Event event;
slouken@1895
   474
        event.jhat.type = SDL_JOYHATMOTION;
slouken@1895
   475
        event.jhat.which = joystick->index;
slouken@1895
   476
        event.jhat.hat = hat;
slouken@1895
   477
        event.jhat.value = value;
slouken@1895
   478
        if ((SDL_EventOK == NULL)
slouken@1895
   479
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@1895
   480
            posted = 1;
slouken@1895
   481
            SDL_PushEvent(&event);
slouken@1895
   482
        }
slouken@1895
   483
    }
slouken@1361
   484
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   485
    return (posted);
slouken@0
   486
}
slouken@0
   487
slouken@1895
   488
int
slouken@1895
   489
SDL_PrivateJoystickBall(SDL_Joystick * joystick, Uint8 ball,
slouken@1895
   490
                        Sint16 xrel, Sint16 yrel)
slouken@0
   491
{
slouken@1895
   492
    int posted;
slouken@0
   493
slouken@1895
   494
    /* Update internal mouse state */
slouken@1895
   495
    joystick->balls[ball].dx += xrel;
slouken@1895
   496
    joystick->balls[ball].dy += yrel;
slouken@0
   497
slouken@1895
   498
    /* Post the event, if desired */
slouken@1895
   499
    posted = 0;
slouken@1361
   500
#if !SDL_EVENTS_DISABLED
slouken@1895
   501
    if (SDL_ProcessEvents[SDL_JOYBALLMOTION] == SDL_ENABLE) {
slouken@1895
   502
        SDL_Event event;
slouken@1895
   503
        event.jball.type = SDL_JOYBALLMOTION;
slouken@1895
   504
        event.jball.which = joystick->index;
slouken@1895
   505
        event.jball.ball = ball;
slouken@1895
   506
        event.jball.xrel = xrel;
slouken@1895
   507
        event.jball.yrel = yrel;
slouken@1895
   508
        if ((SDL_EventOK == NULL)
slouken@1895
   509
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@1895
   510
            posted = 1;
slouken@1895
   511
            SDL_PushEvent(&event);
slouken@1895
   512
        }
slouken@1895
   513
    }
slouken@1361
   514
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   515
    return (posted);
slouken@0
   516
}
slouken@0
   517
slouken@1895
   518
int
slouken@1895
   519
SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
slouken@0
   520
{
slouken@1895
   521
    int posted;
slouken@1361
   522
#if !SDL_EVENTS_DISABLED
slouken@1895
   523
    SDL_Event event;
slouken@0
   524
slouken@1895
   525
    switch (state) {
slouken@1895
   526
    case SDL_PRESSED:
slouken@1895
   527
        event.type = SDL_JOYBUTTONDOWN;
slouken@1895
   528
        break;
slouken@1895
   529
    case SDL_RELEASED:
slouken@1895
   530
        event.type = SDL_JOYBUTTONUP;
slouken@1895
   531
        break;
slouken@1895
   532
    default:
slouken@1895
   533
        /* Invalid state -- bail */
slouken@1895
   534
        return (0);
slouken@1895
   535
    }
slouken@1361
   536
#endif /* !SDL_EVENTS_DISABLED */
slouken@0
   537
slouken@1895
   538
    /* Update internal joystick state */
slouken@1895
   539
    joystick->buttons[button] = state;
slouken@0
   540
slouken@1895
   541
    /* Post the event, if desired */
slouken@1895
   542
    posted = 0;
slouken@1361
   543
#if !SDL_EVENTS_DISABLED
slouken@1895
   544
    if (SDL_ProcessEvents[event.type] == SDL_ENABLE) {
slouken@1895
   545
        event.jbutton.which = joystick->index;
slouken@1895
   546
        event.jbutton.button = button;
slouken@1895
   547
        event.jbutton.state = state;
slouken@1895
   548
        if ((SDL_EventOK == NULL)
slouken@1895
   549
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@1895
   550
            posted = 1;
slouken@1895
   551
            SDL_PushEvent(&event);
slouken@1895
   552
        }
slouken@1895
   553
    }
slouken@1361
   554
#endif /* !SDL_EVENTS_DISABLED */
slouken@1895
   555
    return (posted);
slouken@0
   556
}
slouken@0
   557
slouken@1895
   558
void
slouken@1895
   559
SDL_JoystickUpdate(void)
slouken@0
   560
{
slouken@1895
   561
    int i;
slouken@0
   562
slouken@1895
   563
    for (i = 0; SDL_joysticks[i]; ++i) {
slouken@1895
   564
        SDL_SYS_JoystickUpdate(SDL_joysticks[i]);
slouken@1895
   565
    }
slouken@0
   566
}
slouken@0
   567
slouken@1895
   568
int
slouken@1895
   569
SDL_JoystickEventState(int state)
slouken@0
   570
{
slouken@1361
   571
#if SDL_EVENTS_DISABLED
slouken@1895
   572
    return SDL_IGNORE;
slouken@0
   573
#else
slouken@1895
   574
    const Uint8 event_list[] = {
slouken@1895
   575
        SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION,
slouken@1895
   576
        SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP,
slouken@1895
   577
    };
slouken@1895
   578
    unsigned int i;
slouken@0
   579
slouken@1895
   580
    switch (state) {
slouken@1895
   581
    case SDL_QUERY:
slouken@1895
   582
        state = SDL_IGNORE;
slouken@1895
   583
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@1895
   584
            state = SDL_EventState(event_list[i], SDL_QUERY);
slouken@1895
   585
            if (state == SDL_ENABLE) {
slouken@1895
   586
                break;
slouken@1895
   587
            }
slouken@1895
   588
        }
slouken@1895
   589
        break;
slouken@1895
   590
    default:
slouken@1895
   591
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@1895
   592
            SDL_EventState(event_list[i], state);
slouken@1895
   593
        }
slouken@1895
   594
        break;
slouken@1895
   595
    }
slouken@1895
   596
    return (state);
slouken@1361
   597
#endif /* SDL_EVENTS_DISABLED */
slouken@0
   598
}
slouken@1895
   599
slouken@1895
   600
/* vi: set ts=4 sw=4 expandtab: */