src/joystick/SDL_joystick.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 01 Jan 2012 16:58:00 -0500
changeset 6145 964d13f789e2
parent 6138 4c64952a58fb
child 6690 9548c8a58103
permissions -rwxr-xr-x
Fixed bug 1337 - joystick crash due to heap corruption with btnx
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
6138
4c64952a58fb Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6098
diff changeset
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    20
*/
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1379
diff changeset
    21
#include "SDL_config.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    22
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    23
/* This is the joystick API for Simple DirectMedia Layer */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    24
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
#include "SDL_events.h"
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    26
#include "SDL_sysjoystick.h"
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    27
#include "SDL_joystick_c.h"
5861
b781fa6468aa Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 5859
diff changeset
    28
#include "SDL_assert.h"
b781fa6468aa Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 5859
diff changeset
    29
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    30
#if !SDL_EVENTS_DISABLED
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    31
#include "../events/SDL_events_c.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    32
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    33
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    34
Uint8 SDL_numjoysticks = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    35
SDL_Joystick **SDL_joysticks = NULL;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    36
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    37
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    38
SDL_JoystickInit(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    39
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    40
    int arraylen;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    41
    int status;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    42
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    43
    SDL_numjoysticks = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    44
    status = SDL_SYS_JoystickInit();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    45
    if (status >= 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    46
        arraylen = (status + 1) * sizeof(*SDL_joysticks);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    47
        SDL_joysticks = (SDL_Joystick **) SDL_malloc(arraylen);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    48
        if (SDL_joysticks == NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    49
            SDL_numjoysticks = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    50
        } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    51
            SDL_memset(SDL_joysticks, 0, arraylen);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    52
            SDL_numjoysticks = status;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    53
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    54
        status = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    55
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    56
    return (status);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    57
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    58
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    59
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    60
 * Count the number of joysticks attached to the system
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    61
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    62
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    63
SDL_NumJoysticks(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    64
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    65
    return SDL_numjoysticks;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    66
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    67
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    68
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    69
 * Get the implementation dependent name of a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    70
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    71
const char *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    72
SDL_JoystickName(int device_index)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    73
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    74
    if ((device_index < 0) || (device_index >= SDL_numjoysticks)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    75
        SDL_SetError("There are %d joysticks available", SDL_numjoysticks);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    76
        return (NULL);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    77
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    78
    return (SDL_SYS_JoystickName(device_index));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    79
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    80
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    81
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    82
 * Open a joystick for use - the index passed as an argument refers to
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    83
 * the N'th joystick on the system.  This index is the value which will
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    84
 * identify this joystick in future joystick events.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    85
 *
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    86
 * This function returns a joystick identifier, or NULL if an error occurred.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    87
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    88
SDL_Joystick *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    89
SDL_JoystickOpen(int device_index)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    90
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    91
    int i;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    92
    SDL_Joystick *joystick;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    93
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    94
    if ((device_index < 0) || (device_index >= SDL_numjoysticks)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    95
        SDL_SetError("There are %d joysticks available", SDL_numjoysticks);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    96
        return (NULL);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    97
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    98
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    99
    /* If the joystick is already open, return it */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   100
    for (i = 0; SDL_joysticks[i]; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   101
        if (device_index == SDL_joysticks[i]->index) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   102
            joystick = SDL_joysticks[i];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   103
            ++joystick->ref_count;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   104
            return (joystick);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   105
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   106
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   107
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   108
    /* Create and initialize the joystick */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   109
    joystick = (SDL_Joystick *) SDL_malloc((sizeof *joystick));
3608
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   110
    if (joystick == NULL) {
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   111
        SDL_OutOfMemory();
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   112
        return NULL;
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   113
    }
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   114
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   115
    SDL_memset(joystick, 0, (sizeof *joystick));
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   116
    joystick->index = device_index;
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   117
    if (SDL_SYS_JoystickOpen(joystick) < 0) {
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   118
        SDL_free(joystick);
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   119
        return NULL;
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   120
    }
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   121
    if (joystick->naxes > 0) {
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   122
        joystick->axes = (Sint16 *) SDL_malloc
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   123
            (joystick->naxes * sizeof(Sint16));
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   124
    }
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   125
    if (joystick->nhats > 0) {
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   126
        joystick->hats = (Uint8 *) SDL_malloc
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   127
            (joystick->nhats * sizeof(Uint8));
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   128
    }
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   129
    if (joystick->nballs > 0) {
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   130
        joystick->balls = (struct balldelta *) SDL_malloc
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   131
            (joystick->nballs * sizeof(*joystick->balls));
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   132
    }
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   133
    if (joystick->nbuttons > 0) {
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   134
        joystick->buttons = (Uint8 *) SDL_malloc
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   135
            (joystick->nbuttons * sizeof(Uint8));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   136
    }
3608
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   137
    if (((joystick->naxes > 0) && !joystick->axes)
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   138
        || ((joystick->nhats > 0) && !joystick->hats)
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   139
        || ((joystick->nballs > 0) && !joystick->balls)
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   140
        || ((joystick->nbuttons > 0) && !joystick->buttons)) {
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   141
        SDL_OutOfMemory();
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   142
        SDL_JoystickClose(joystick);
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   143
        return NULL;
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   144
    }
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   145
    if (joystick->axes) {
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   146
        SDL_memset(joystick->axes, 0, joystick->naxes * sizeof(Sint16));
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   147
    }
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   148
    if (joystick->hats) {
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   149
        SDL_memset(joystick->hats, 0, joystick->nhats * sizeof(Uint8));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   150
    }
3608
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   151
    if (joystick->balls) {
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   152
        SDL_memset(joystick->balls, 0,
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   153
            joystick->nballs * sizeof(*joystick->balls));
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   154
    }
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   155
    if (joystick->buttons) {
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   156
        SDL_memset(joystick->buttons, 0, joystick->nbuttons * sizeof(Uint8));
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   157
    }
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   158
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   159
    /* Add joystick to list */
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   160
    ++joystick->ref_count;
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   161
    for (i = 0; SDL_joysticks[i]; ++i)
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   162
        /* Skip to next joystick */ ;
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   163
    SDL_joysticks[i] = joystick;
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   164
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   165
    return (joystick);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   166
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   167
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   168
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   169
 * Returns 1 if the joystick has been opened, or 0 if it has not.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   170
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   171
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   172
SDL_JoystickOpened(int device_index)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   173
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   174
    int i, opened;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   175
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   176
    opened = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   177
    for (i = 0; SDL_joysticks[i]; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   178
        if (SDL_joysticks[i]->index == (Uint8) device_index) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   179
            opened = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   180
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   181
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   182
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   183
    return (opened);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   184
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   185
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   186
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   187
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   188
 * Checks to make sure the joystick is valid.
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   189
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   190
int
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   191
SDL_PrivateJoystickValid(SDL_Joystick ** joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   192
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   193
    int valid;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   194
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   195
    if (*joystick == NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   196
        SDL_SetError("Joystick hasn't been opened yet");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   197
        valid = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   198
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   199
        valid = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   200
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   201
    return valid;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   202
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   203
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   204
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   205
 * Get the device index of an opened joystick.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   206
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   207
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   208
SDL_JoystickIndex(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   209
{
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   210
    if (!SDL_PrivateJoystickValid(&joystick)) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   211
        return (-1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   212
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   213
    return (joystick->index);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   214
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   215
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   216
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   217
 * Get the number of multi-dimensional axis controls on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   218
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   219
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   220
SDL_JoystickNumAxes(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   221
{
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   222
    if (!SDL_PrivateJoystickValid(&joystick)) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   223
        return (-1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   224
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   225
    return (joystick->naxes);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   226
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   227
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   228
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   229
 * Get the number of hats on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   230
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   231
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   232
SDL_JoystickNumHats(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   233
{
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   234
    if (!SDL_PrivateJoystickValid(&joystick)) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   235
        return (-1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   236
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   237
    return (joystick->nhats);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   238
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   239
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   240
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   241
 * Get the number of trackballs on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   242
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   243
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   244
SDL_JoystickNumBalls(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   245
{
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   246
    if (!SDL_PrivateJoystickValid(&joystick)) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   247
        return (-1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   248
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   249
    return (joystick->nballs);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   250
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   251
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   252
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   253
 * Get the number of buttons on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   254
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   255
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   256
SDL_JoystickNumButtons(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   257
{
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   258
    if (!SDL_PrivateJoystickValid(&joystick)) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   259
        return (-1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   260
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   261
    return (joystick->nbuttons);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   262
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   263
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   264
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   265
 * Get the current state of an axis control on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   266
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   267
Sint16
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   268
SDL_JoystickGetAxis(SDL_Joystick * joystick, int axis)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   269
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   270
    Sint16 state;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   271
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   272
    if (!SDL_PrivateJoystickValid(&joystick)) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   273
        return (0);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   274
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   275
    if (axis < joystick->naxes) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   276
        state = joystick->axes[axis];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   277
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   278
        SDL_SetError("Joystick only has %d axes", joystick->naxes);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   279
        state = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   280
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   281
    return (state);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   282
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   283
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   284
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   285
 * Get the current state of a hat on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   286
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   287
Uint8
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   288
SDL_JoystickGetHat(SDL_Joystick * joystick, int hat)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   289
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   290
    Uint8 state;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   291
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   292
    if (!SDL_PrivateJoystickValid(&joystick)) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   293
        return (0);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   294
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   295
    if (hat < joystick->nhats) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   296
        state = joystick->hats[hat];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   297
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   298
        SDL_SetError("Joystick only has %d hats", joystick->nhats);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   299
        state = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   300
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   301
    return (state);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   302
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   303
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   304
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   305
 * Get the ball axis change since the last poll
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   306
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   307
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   308
SDL_JoystickGetBall(SDL_Joystick * joystick, int ball, int *dx, int *dy)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   309
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   310
    int retval;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   311
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   312
    if (!SDL_PrivateJoystickValid(&joystick)) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   313
        return (-1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   314
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   315
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   316
    retval = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   317
    if (ball < joystick->nballs) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   318
        if (dx) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   319
            *dx = joystick->balls[ball].dx;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   320
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   321
        if (dy) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   322
            *dy = joystick->balls[ball].dy;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   323
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   324
        joystick->balls[ball].dx = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   325
        joystick->balls[ball].dy = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   326
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   327
        SDL_SetError("Joystick only has %d balls", joystick->nballs);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   328
        retval = -1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   329
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   330
    return (retval);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   331
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   332
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   333
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   334
 * Get the current state of a button on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   335
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   336
Uint8
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   337
SDL_JoystickGetButton(SDL_Joystick * joystick, int button)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   338
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   339
    Uint8 state;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   340
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   341
    if (!SDL_PrivateJoystickValid(&joystick)) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   342
        return (0);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   343
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   344
    if (button < joystick->nbuttons) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   345
        state = joystick->buttons[button];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   346
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   347
        SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   348
        state = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   349
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   350
    return (state);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   351
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   352
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   353
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   354
 * Close a joystick previously opened with SDL_JoystickOpen()
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   355
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   356
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   357
SDL_JoystickClose(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   358
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   359
    int i;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   360
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   361
    if (!SDL_PrivateJoystickValid(&joystick)) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   362
        return;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   363
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   364
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   365
    /* First decrement ref count */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   366
    if (--joystick->ref_count > 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   367
        return;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   368
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   369
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   370
    SDL_SYS_JoystickClose(joystick);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   371
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   372
    /* Remove joystick from list */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   373
    for (i = 0; SDL_joysticks[i]; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   374
        if (joystick == SDL_joysticks[i]) {
3369
71bb88152e3f Debian patch: 218_joystick_memmove.diff
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   375
            SDL_memmove(&SDL_joysticks[i], &SDL_joysticks[i + 1],
71bb88152e3f Debian patch: 218_joystick_memmove.diff
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   376
                        (SDL_numjoysticks - i) * sizeof(joystick));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   377
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   378
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   379
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   380
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   381
    /* Free the data associated with this joystick */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   382
    if (joystick->axes) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   383
        SDL_free(joystick->axes);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   384
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   385
    if (joystick->hats) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   386
        SDL_free(joystick->hats);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   387
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   388
    if (joystick->balls) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   389
        SDL_free(joystick->balls);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   390
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   391
    if (joystick->buttons) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   392
        SDL_free(joystick->buttons);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   393
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   394
    SDL_free(joystick);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   395
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   396
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   397
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   398
SDL_JoystickQuit(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   399
{
5856
173d28fd4be4 Clean up any opened joysticks during SDL_JoystickQuit().
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   400
    const int numsticks = SDL_numjoysticks;
173d28fd4be4 Clean up any opened joysticks during SDL_JoystickQuit().
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   401
    int i;
173d28fd4be4 Clean up any opened joysticks during SDL_JoystickQuit().
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   402
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   403
    /* Stop the event polling */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   404
    SDL_numjoysticks = 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   405
6098
c781fe3628a1 It works better if you compile it.
Sam Lantinga <slouken@libsdl.org>
parents: 6097
diff changeset
   406
    for (i = numsticks; i--; ) {
6097
4ea2b4d4937a Better fix, iterate backwards over the array so we don't care whether the close code shuffles things down.
Sam Lantinga <slouken@libsdl.org>
parents: 6096
diff changeset
   407
        SDL_Joystick *stick = SDL_joysticks[i];
5856
173d28fd4be4 Clean up any opened joysticks during SDL_JoystickQuit().
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   408
        if (stick && (stick->ref_count >= 1)) {
173d28fd4be4 Clean up any opened joysticks during SDL_JoystickQuit().
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   409
            stick->ref_count = 1;
173d28fd4be4 Clean up any opened joysticks during SDL_JoystickQuit().
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   410
            SDL_JoystickClose(stick);
173d28fd4be4 Clean up any opened joysticks during SDL_JoystickQuit().
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   411
        }
173d28fd4be4 Clean up any opened joysticks during SDL_JoystickQuit().
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   412
    }
173d28fd4be4 Clean up any opened joysticks during SDL_JoystickQuit().
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   413
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   414
    /* Quit the joystick setup */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   415
    SDL_SYS_JoystickQuit();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   416
    if (SDL_joysticks) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   417
        SDL_free(SDL_joysticks);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   418
        SDL_joysticks = NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   419
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   420
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   421
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   422
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   423
/* These are global for SDL_sysjoystick.c and SDL_events.c */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   424
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   425
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   426
SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   427
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   428
    int posted;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   429
6145
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   430
    /* Make sure we're not getting garbage events */
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   431
    if (axis >= joystick->naxes) {
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   432
        return 0;
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   433
    }
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   434
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   435
    /* Update internal joystick state */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   436
    joystick->axes[axis] = value;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   437
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   438
    /* Post the event, if desired */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   439
    posted = 0;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   440
#if !SDL_EVENTS_DISABLED
4429
faa9fc8e7f67 General improvements for user custom event registration
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   441
    if (SDL_GetEventState(SDL_JOYAXISMOTION) == SDL_ENABLE) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   442
        SDL_Event event;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   443
        event.type = SDL_JOYAXISMOTION;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   444
        event.jaxis.which = joystick->index;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   445
        event.jaxis.axis = axis;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   446
        event.jaxis.value = value;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   447
        if ((SDL_EventOK == NULL)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   448
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   449
            posted = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   450
            SDL_PushEvent(&event);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   451
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   452
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   453
#endif /* !SDL_EVENTS_DISABLED */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   454
    return (posted);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   455
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   456
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   457
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   458
SDL_PrivateJoystickHat(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   459
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   460
    int posted;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   461
6145
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   462
    /* Make sure we're not getting garbage events */
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   463
    if (hat >= joystick->nhats) {
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   464
        return 0;
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   465
    }
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   466
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   467
    /* Update internal joystick state */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   468
    joystick->hats[hat] = value;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   469
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   470
    /* Post the event, if desired */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   471
    posted = 0;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   472
#if !SDL_EVENTS_DISABLED
4429
faa9fc8e7f67 General improvements for user custom event registration
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   473
    if (SDL_GetEventState(SDL_JOYHATMOTION) == SDL_ENABLE) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   474
        SDL_Event event;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   475
        event.jhat.type = SDL_JOYHATMOTION;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   476
        event.jhat.which = joystick->index;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   477
        event.jhat.hat = hat;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   478
        event.jhat.value = value;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   479
        if ((SDL_EventOK == NULL)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   480
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   481
            posted = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   482
            SDL_PushEvent(&event);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   483
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   484
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   485
#endif /* !SDL_EVENTS_DISABLED */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   486
    return (posted);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   487
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   488
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   489
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   490
SDL_PrivateJoystickBall(SDL_Joystick * joystick, Uint8 ball,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   491
                        Sint16 xrel, Sint16 yrel)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   492
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   493
    int posted;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   494
6145
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   495
    /* Make sure we're not getting garbage events */
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   496
    if (ball >= joystick->nballs) {
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   497
        return 0;
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   498
    }
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   499
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   500
    /* Update internal mouse state */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   501
    joystick->balls[ball].dx += xrel;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   502
    joystick->balls[ball].dy += yrel;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   503
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   504
    /* Post the event, if desired */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   505
    posted = 0;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   506
#if !SDL_EVENTS_DISABLED
4429
faa9fc8e7f67 General improvements for user custom event registration
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   507
    if (SDL_GetEventState(SDL_JOYBALLMOTION) == SDL_ENABLE) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   508
        SDL_Event event;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   509
        event.jball.type = SDL_JOYBALLMOTION;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   510
        event.jball.which = joystick->index;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   511
        event.jball.ball = ball;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   512
        event.jball.xrel = xrel;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   513
        event.jball.yrel = yrel;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   514
        if ((SDL_EventOK == NULL)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   515
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   516
            posted = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   517
            SDL_PushEvent(&event);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   518
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   519
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   520
#endif /* !SDL_EVENTS_DISABLED */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   521
    return (posted);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   522
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   523
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   524
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   525
SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   526
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   527
    int posted;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   528
#if !SDL_EVENTS_DISABLED
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   529
    SDL_Event event;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   530
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   531
    switch (state) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   532
    case SDL_PRESSED:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   533
        event.type = SDL_JOYBUTTONDOWN;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   534
        break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   535
    case SDL_RELEASED:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   536
        event.type = SDL_JOYBUTTONUP;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   537
        break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   538
    default:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   539
        /* Invalid state -- bail */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   540
        return (0);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   541
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   542
#endif /* !SDL_EVENTS_DISABLED */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   543
6145
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   544
    /* Make sure we're not getting garbage events */
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   545
    if (button >= joystick->nbuttons) {
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   546
        return 0;
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   547
    }
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   548
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   549
    /* Update internal joystick state */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   550
    joystick->buttons[button] = state;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   551
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   552
    /* Post the event, if desired */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   553
    posted = 0;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   554
#if !SDL_EVENTS_DISABLED
4429
faa9fc8e7f67 General improvements for user custom event registration
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   555
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   556
        event.jbutton.which = joystick->index;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   557
        event.jbutton.button = button;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   558
        event.jbutton.state = state;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   559
        if ((SDL_EventOK == NULL)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   560
            || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   561
            posted = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   562
            SDL_PushEvent(&event);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   563
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   564
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   565
#endif /* !SDL_EVENTS_DISABLED */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   566
    return (posted);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   567
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   568
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   569
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   570
SDL_JoystickUpdate(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   571
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   572
    int i;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   573
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   574
    for (i = 0; SDL_joysticks[i]; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   575
        SDL_SYS_JoystickUpdate(SDL_joysticks[i]);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   576
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   577
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   578
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   579
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   580
SDL_JoystickEventState(int state)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   581
{
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   582
#if SDL_EVENTS_DISABLED
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   583
    return SDL_IGNORE;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   584
#else
4429
faa9fc8e7f67 General improvements for user custom event registration
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   585
    const Uint32 event_list[] = {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   586
        SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   587
        SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   588
    };
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   589
    unsigned int i;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   590
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   591
    switch (state) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   592
    case SDL_QUERY:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   593
        state = SDL_IGNORE;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   594
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   595
            state = SDL_EventState(event_list[i], SDL_QUERY);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   596
            if (state == SDL_ENABLE) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   597
                break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   598
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   599
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   600
        break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   601
    default:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   602
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   603
            SDL_EventState(event_list[i], state);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   604
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   605
        break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   606
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   607
    return (state);
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   608
#endif /* SDL_EVENTS_DISABLED */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   609
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   610
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   611
/* vi: set ts=4 sw=4 expandtab: */