src/joystick/SDL_joystick.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 25 Aug 2008 09:55:03 +0000
changeset 2713 0906692aa6a4
parent 1895 c121d94672cb
child 2859 99210400e8b9
permissions -rw-r--r--
Final merge of Google Summer of Code 2008 work...

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