src/joystick/SDL_joystick.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 06 Jun 2013 18:20:06 -0700
changeset 7294 0923de98a63a
parent 7279 f7805b13b485
child 7297 c43b23f0c000
permissions -rw-r--r--
Fixed crash trying to get the GUID of an invalid joystick index
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
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6753
diff changeset
     3
  Copyright (C) 1997-2013 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"
5861
b781fa6468aa Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 5859
diff changeset
    27
#include "SDL_assert.h"
7279
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
    28
#include "SDL_hints.h"
5861
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
6712
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
    34
static SDL_Joystick *SDL_joysticks = NULL;
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
    35
static SDL_Joystick *SDL_updating_joystick = NULL;
0
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 status;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    41
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
    42
    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
    43
    if (status >= 0) {
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
    44
      status = 0;
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
    45
    }
c121d94672cb SDL 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
    return (status);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    47
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    48
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    49
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    50
 * Count the number of joysticks attached to the system
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    51
 */
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
    52
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
    53
SDL_NumJoysticks(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    54
{
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
    55
    return SDL_SYS_NumJoysticks();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    56
}
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
 * Get the implementation dependent name of a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    60
 */
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
    61
const char *
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
    62
SDL_JoystickNameForIndex(int device_index)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    63
{
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
    64
    if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
    65
        SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
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
    66
        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
    67
    }
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6693
diff changeset
    68
    return (SDL_SYS_JoystickNameForDeviceIndex(device_index));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    69
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    70
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    71
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    72
 * Open a joystick for use - the index passed as an argument refers to
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    73
 * 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
    74
 * identify this joystick in future joystick events.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    75
 *
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    76
 * This function returns a joystick identifier, or NULL if an error occurred.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    77
 */
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
    78
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
    79
SDL_JoystickOpen(int device_index)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    80
{
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
    81
    SDL_Joystick *joystick;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
    82
    SDL_Joystick *joysticklist;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
    83
    const char *joystickname = NULL;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    84
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
    85
    if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
    86
        SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
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
    87
        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
    88
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    89
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
    90
    joysticklist = SDL_joysticks;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
    91
    /* If the joystick is already open, return it
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
    92
    * it is important that we have a single joystick * for each instance id
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
    93
    */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
    94
    while ( joysticklist )
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
    95
    {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
    96
        if ( SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == joysticklist->instance_id ) {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
    97
                joystick = joysticklist;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
    98
                ++joystick->ref_count;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
    99
                return (joystick);
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   100
        }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   101
        joysticklist = joysticklist->next;
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
   102
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   103
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
   104
    /* 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
   105
    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
   106
    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
   107
        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
   108
        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
   109
    }
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
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_memset(joystick, 0, (sizeof *joystick));
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   112
    if (SDL_SYS_JoystickOpen(joystick, device_index) < 0) {
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
   113
        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
   114
        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
   115
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   116
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   117
    joystickname = SDL_SYS_JoystickNameForDeviceIndex( device_index );
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   118
    if ( joystickname )
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   119
        joystick->name = SDL_strdup( joystickname );
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   120
    else
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   121
        joystick->name = NULL;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   122
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
   123
    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
   124
        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
   125
            (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
   126
    }
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
    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
   128
        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
   129
            (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
   130
    }
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
    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
   132
        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
   133
            (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
   134
    }
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
    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
   136
        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
   137
            (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
   138
    }
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
   139
    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
   140
        || ((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
   141
        || ((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
   142
        || ((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
   143
        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
   144
        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
   145
        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
   146
    }
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
    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
   148
        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
   149
    }
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   150
    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
   151
        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
   152
    }
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
   153
    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
   154
        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
   155
            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
   156
    }
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
    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
   158
        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
   159
    }
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
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
    /* 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
   162
    ++joystick->ref_count;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   163
    /* Link the joystick in the list */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   164
    joystick->next = SDL_joysticks;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   165
    SDL_joysticks = joystick;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   166
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   167
    SDL_SYS_JoystickUpdate( 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
   168
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
   169
    return (joystick);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   170
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   171
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   172
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   173
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   174
 * 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
   175
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   176
int
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   177
SDL_PrivateJoystickValid(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   178
{
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
   179
    int valid;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   180
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   181
    if ( joystick == NULL ) {
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
   182
        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
   183
        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
   184
    } 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
   185
        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
   186
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   187
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   188
    if ( joystick && joystick->closed )
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   189
    {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   190
        valid = 0;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   191
    }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
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
    return valid;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   194
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   195
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   196
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   197
 * Get the number of multi-dimensional axis controls on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   198
 */
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
   199
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
   200
SDL_JoystickNumAxes(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   201
{
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   202
    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
   203
        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
   204
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   205
    return (joystick->naxes);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   206
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   207
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   208
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   209
 * Get the number of hats on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   210
 */
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
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
   212
SDL_JoystickNumHats(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   213
{
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   214
    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
   215
        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
   216
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   217
    return (joystick->nhats);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   218
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   219
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   220
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   221
 * Get the number of trackballs on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   222
 */
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
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
   224
SDL_JoystickNumBalls(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   225
{
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   226
    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
   227
        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
   228
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   229
    return (joystick->nballs);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   230
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   231
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   232
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   233
 * Get the number of buttons on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   234
 */
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
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
   236
SDL_JoystickNumButtons(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   237
{
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   238
    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
   239
        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
   240
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   241
    return (joystick->nbuttons);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   242
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   243
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   244
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   245
 * Get the current state of an axis control on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   246
 */
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
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
   248
SDL_JoystickGetAxis(SDL_Joystick * joystick, int axis)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   249
{
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
   250
    Sint16 state;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   251
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   252
    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
   253
        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
   254
    }
c121d94672cb SDL 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
    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
   256
        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
   257
    } 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
   258
        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
   259
        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
   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 (state);
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 a hat 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
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
   268
SDL_JoystickGetHat(SDL_Joystick * joystick, int hat)
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
    Uint8 state;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   271
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
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 (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
   276
        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
   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 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
   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 ball axis change since the last poll
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
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
   288
SDL_JoystickGetBall(SDL_Joystick * joystick, int ball, int *dx, int *dy)
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
    int retval;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   291
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
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 (-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
   294
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   295
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
   296
    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
   297
    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
   298
        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
   299
            *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
   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
        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
   302
            *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
   303
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   304
        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
   305
        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
   306
    } else {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   307
        return SDL_SetError("Joystick only has %d balls", joystick->nballs);
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
   308
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   309
    return (retval);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   310
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   311
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   312
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   313
 * Get the current state of a button on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   314
 */
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
   315
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
   316
SDL_JoystickGetButton(SDL_Joystick * joystick, int button)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   317
{
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
   318
    Uint8 state;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   319
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   320
    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
   321
        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
   322
    }
c121d94672cb SDL 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
    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
   324
        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
   325
    } 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
   326
        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
   327
        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
   328
    }
c121d94672cb SDL 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
    return (state);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   330
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   331
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   332
/*
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   333
 * Return if the joystick in question is currently attached to the system,
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   334
 *  \return 0 if not plugged in, 1 if still present.
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   335
 */
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6693
diff changeset
   336
SDL_bool
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6693
diff changeset
   337
SDL_JoystickGetAttached(SDL_Joystick * joystick)
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   338
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   339
    if (!SDL_PrivateJoystickValid(joystick)) {
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6693
diff changeset
   340
        return SDL_FALSE;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   341
    }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   342
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   343
    return SDL_SYS_JoystickAttached(joystick);
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   344
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   345
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   346
/*
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   347
 * Get the instance id for this opened joystick
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   348
 */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   349
SDL_JoystickID
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6693
diff changeset
   350
SDL_JoystickInstanceID(SDL_Joystick * joystick)
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   351
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   352
    if (!SDL_PrivateJoystickValid(joystick)) {
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   353
        return (-1);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   354
    }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   355
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   356
    return (joystick->instance_id);
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   357
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   358
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   359
/*
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   360
 * Get the friendly name of this joystick
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   361
 */
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   362
const char *
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   363
SDL_JoystickName(SDL_Joystick * joystick)
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   364
{
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   365
    if (!SDL_PrivateJoystickValid(joystick)) {
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   366
        return (NULL);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   367
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   368
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   369
    return (joystick->name);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   370
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   371
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   372
/*
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   373
 * Close a joystick previously opened with SDL_JoystickOpen()
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   374
 */
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
   375
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
   376
SDL_JoystickClose(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   377
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   378
    SDL_Joystick *joysticklist;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   379
    SDL_Joystick *joysticklistprev;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   380
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   381
    if (!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
   382
        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
   383
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   384
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
   385
    /* 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
   386
    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
   387
        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
   388
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   389
6712
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   390
    if (joystick == SDL_updating_joystick) {
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   391
        return;
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   392
    }
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   393
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
   394
    SDL_SYS_JoystickClose(joystick);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   395
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   396
    joysticklist = SDL_joysticks;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   397
    joysticklistprev = NULL;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   398
    while ( joysticklist )
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   399
    {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   400
        if (joystick == joysticklist)
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   401
        {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   402
            if ( joysticklistprev )
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   403
            {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   404
                /* unlink this entry */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   405
                joysticklistprev->next = joysticklist->next;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   406
            }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   407
            else
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   408
            {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   409
                SDL_joysticks = joystick->next;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   410
            }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   411
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   412
            break;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   413
        }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   414
        joysticklistprev = joysticklist;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   415
        joysticklist = joysticklist->next;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   416
    }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   417
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   418
    if (joystick->name)
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   419
        SDL_free(joystick->name);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   420
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
   421
    /* 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
   422
    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
   423
        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
   424
    }
c121d94672cb SDL 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
    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
   426
        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
   427
    }
c121d94672cb SDL 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
    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
   429
        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
   430
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   431
    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
   432
        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
   433
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   434
    SDL_free(joystick);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   435
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   436
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
   437
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
   438
SDL_JoystickQuit(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   439
{
6712
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   440
    /* Make sure we're not getting called in the middle of updating joysticks */
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   441
    SDL_assert(!SDL_updating_joystick);
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   442
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
   443
    /* Stop the event polling */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   444
    while ( SDL_joysticks )
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   445
    {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   446
        SDL_joysticks->ref_count = 1;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   447
        SDL_JoystickClose(SDL_joysticks);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   448
    }
5856
173d28fd4be4 Clean up any opened joysticks during SDL_JoystickQuit().
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   449
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
   450
    /* 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
   451
    SDL_SYS_JoystickQuit();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   452
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   453
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   454
7279
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   455
static SDL_bool
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   456
SDL_PrivateJoystickShouldIgnoreEvent()
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   457
{
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   458
    const char *hint;
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   459
    if (SDL_GetKeyboardFocus() != NULL) {
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   460
        return SDL_FALSE;
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   461
    }
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   462
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   463
    hint = SDL_GetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS);
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   464
    if (hint && *hint == '1') {
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   465
        return SDL_FALSE;
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   466
    }
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   467
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   468
    return SDL_TRUE;
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   469
}
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   470
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   471
/* These are global for SDL_sysjoystick.c and SDL_events.c */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   472
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
   473
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
   474
SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   475
{
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
   476
    int posted;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   477
6145
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   478
    /* 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
   479
    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
   480
        return 0;
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   481
    }
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   482
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
   483
    /* Update internal joystick state */
6735
486800c3f44c Don't spam events if the axis values haven't changed
Sam Lantinga <slouken@libsdl.org>
parents: 6712
diff changeset
   484
    if (value == joystick->axes[axis]) {
486800c3f44c Don't spam events if the axis values haven't changed
Sam Lantinga <slouken@libsdl.org>
parents: 6712
diff changeset
   485
        return 0;
486800c3f44c Don't spam events if the axis values haven't changed
Sam Lantinga <slouken@libsdl.org>
parents: 6712
diff changeset
   486
    }
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
   487
    joystick->axes[axis] = value;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   488
7279
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   489
    /* We ignore events if we don't have keyboard focus, except for centering
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   490
     * events.
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   491
     */
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   492
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   493
        if (!(joystick->closed && joystick->uncentered)) {
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   494
            return 0;
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   495
        }
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   496
    }
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   497
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
   498
    /* 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
   499
    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
   500
#if !SDL_EVENTS_DISABLED
4429
faa9fc8e7f67 General improvements for user custom event registration
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   501
    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
   502
        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
   503
        event.type = SDL_JOYAXISMOTION;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   504
        event.jaxis.which = joystick->instance_id;
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
   505
        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
   506
        event.jaxis.value = value;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   507
        posted = SDL_PushEvent(&event) == 1;
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
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   509
#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
   510
    return (posted);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   511
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   512
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
   513
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
   514
SDL_PrivateJoystickHat(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   515
{
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
   516
    int posted;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   517
6145
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   518
    /* 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
   519
    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
   520
        return 0;
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   521
    }
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   522
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
   523
    /* 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
   524
    joystick->hats[hat] = value;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   525
7279
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   526
    /* We ignore events if we don't have keyboard focus, except for centering
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   527
     * events.
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   528
     */
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   529
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   530
        if (!(joystick->closed && joystick->uncentered)) {
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   531
            return 0;
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   532
        }
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   533
    }
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   534
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   535
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
   536
    /* 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
   537
    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
   538
#if !SDL_EVENTS_DISABLED
4429
faa9fc8e7f67 General improvements for user custom event registration
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   539
    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
   540
        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
   541
        event.jhat.type = SDL_JOYHATMOTION;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   542
        event.jhat.which = joystick->instance_id;
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
   543
        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
   544
        event.jhat.value = value;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   545
        posted = SDL_PushEvent(&event) == 1;
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
   546
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   547
#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
   548
    return (posted);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   549
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   550
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
   551
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
   552
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
   553
                        Sint16 xrel, Sint16 yrel)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   554
{
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
   555
    int posted;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   556
6145
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   557
    /* 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
   558
    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
   559
        return 0;
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   560
    }
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   561
7279
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   562
    /* We ignore events if we don't have keyboard focus. */
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   563
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   564
        return 0;
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   565
    }
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   566
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
   567
    /* 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
   568
    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
   569
    joystick->balls[ball].dy += yrel;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   570
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
   571
    /* 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
   572
    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
   573
#if !SDL_EVENTS_DISABLED
4429
faa9fc8e7f67 General improvements for user custom event registration
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   574
    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
   575
        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
   576
        event.jball.type = SDL_JOYBALLMOTION;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   577
        event.jball.which = joystick->instance_id;
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
   578
        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
   579
        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
   580
        event.jball.yrel = yrel;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   581
        posted = SDL_PushEvent(&event) == 1;
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
   582
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   583
#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
   584
    return (posted);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   585
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   586
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
   587
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
   588
SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   589
{
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
   590
    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
   591
#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
   592
    SDL_Event event;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   593
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
   594
    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
   595
    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
   596
        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
   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
    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
   599
        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
   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
        /* 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
   603
        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
   604
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   605
#endif /* !SDL_EVENTS_DISABLED */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   606
6145
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   607
    /* 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
   608
    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
   609
        return 0;
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   610
    }
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   611
7279
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   612
    /* We ignore events if we don't have keyboard focus, except for button
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   613
     * release. */
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   614
    if (SDL_PrivateJoystickShouldIgnoreEvent() && event.type == SDL_JOYBUTTONDOWN) {
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   615
        return 0;
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   616
    }
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   617
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
   618
    /* 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
   619
    joystick->buttons[button] = state;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   620
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
   621
    /* 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
   622
    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
   623
#if !SDL_EVENTS_DISABLED
4429
faa9fc8e7f67 General improvements for user custom event registration
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   624
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   625
        event.jbutton.which = joystick->instance_id;
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
   626
        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
   627
        event.jbutton.state = state;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   628
        posted = SDL_PushEvent(&event) == 1;
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
   629
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   630
#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
   631
    return (posted);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   632
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   633
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
   634
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
   635
SDL_JoystickUpdate(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   636
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   637
    SDL_Joystick *joystick;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   638
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   639
    joystick = SDL_joysticks;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   640
    while ( joystick )
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   641
    {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   642
        SDL_Joystick *joysticknext;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   643
        /* save off the next pointer, the Update call may cause a joystick removed event
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   644
         * and cause our joystick pointer to be freed
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   645
         */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   646
        joysticknext = joystick->next;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   647
6712
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   648
        SDL_updating_joystick = joystick;
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   649
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   650
        SDL_SYS_JoystickUpdate( joystick );
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   651
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   652
        if ( joystick->closed && joystick->uncentered )
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   653
        {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   654
            int i;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   655
6712
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   656
            /* Tell the app that everything is centered/unpressed...  */
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   657
            for (i = 0; i < joystick->naxes; i++)
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   658
                SDL_PrivateJoystickAxis(joystick, i, 0);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   659
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   660
            for (i = 0; i < joystick->nbuttons; i++)
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   661
                SDL_PrivateJoystickButton(joystick, i, 0);
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   662
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   663
            for (i = 0; i < joystick->nhats; i++)
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   664
                SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
6712
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   665
7279
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   666
            joystick->uncentered = 0;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   667
        }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   668
6712
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   669
        SDL_updating_joystick = NULL;
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   670
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   671
        /* If the joystick was closed while updating, free it here */
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   672
        if ( joystick->ref_count <= 0 ) {
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   673
            SDL_JoystickClose(joystick);
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   674
        }
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   675
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   676
        joystick = joysticknext;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   677
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   678
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   679
    /* this needs to happen AFTER walking the joystick list above, so that any
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   680
       dangling hardware data from removed devices can be free'd
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   681
     */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   682
    SDL_SYS_JoystickDetect();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   683
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   684
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
   685
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
   686
SDL_JoystickEventState(int state)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   687
{
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   688
#if SDL_EVENTS_DISABLED
6753
332c36fd0fd7 Only check SDL_SYS_JoystickNeedsPolling() if we know we don't need to poll for other reasons. This avoids a select() syscall on Linux if it isn't necessary.
Sam Lantinga <slouken@libsdl.org>
parents: 6748
diff changeset
   689
    return SDL_DISABLE;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   690
#else
4429
faa9fc8e7f67 General improvements for user custom event registration
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   691
    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
   692
        SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION,
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   693
        SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED
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
   694
    };
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   695
    unsigned int i;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   696
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
   697
    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
   698
    case SDL_QUERY:
6753
332c36fd0fd7 Only check SDL_SYS_JoystickNeedsPolling() if we know we don't need to poll for other reasons. This avoids a select() syscall on Linux if it isn't necessary.
Sam Lantinga <slouken@libsdl.org>
parents: 6748
diff changeset
   699
        state = SDL_DISABLE;
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
   700
        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
   701
            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
   702
            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
   703
                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
   704
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   705
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   706
        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
   707
    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
   708
        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
   709
            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
   710
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   711
        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
   712
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   713
    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
   714
#endif /* SDL_EVENTS_DISABLED */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   715
}
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
   716
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   717
/* return 1 if you want to run the joystick update loop this frame, used by hotplug support */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   718
SDL_bool
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   719
SDL_PrivateJoystickNeedsPolling()
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   720
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   721
    if (SDL_joysticks != NULL) {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   722
        return SDL_TRUE;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   723
    } else {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   724
        return SDL_SYS_JoystickNeedsPolling();
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   725
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   726
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   727
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   728
6693
3c98e4ec2180 Fixed iOS joystick support for new API
Sam Lantinga <slouken@libsdl.org>
parents: 6690
diff changeset
   729
/* return the guid for this index */
6738
b408d5a406a3 Changes from Alfred:
Sam Lantinga <slouken@libsdl.org>
parents: 6735
diff changeset
   730
SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   731
{
7294
0923de98a63a Fixed crash trying to get the GUID of an invalid joystick index
Sam Lantinga <slouken@libsdl.org>
parents: 7279
diff changeset
   732
    if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
0923de98a63a Fixed crash trying to get the GUID of an invalid joystick index
Sam Lantinga <slouken@libsdl.org>
parents: 7279
diff changeset
   733
        SDL_JoystickGUID emptyGUID;
0923de98a63a Fixed crash trying to get the GUID of an invalid joystick index
Sam Lantinga <slouken@libsdl.org>
parents: 7279
diff changeset
   734
        SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
0923de98a63a Fixed crash trying to get the GUID of an invalid joystick index
Sam Lantinga <slouken@libsdl.org>
parents: 7279
diff changeset
   735
        SDL_zero( emptyGUID );
0923de98a63a Fixed crash trying to get the GUID of an invalid joystick index
Sam Lantinga <slouken@libsdl.org>
parents: 7279
diff changeset
   736
        return emptyGUID;
0923de98a63a Fixed crash trying to get the GUID of an invalid joystick index
Sam Lantinga <slouken@libsdl.org>
parents: 7279
diff changeset
   737
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   738
    return SDL_SYS_JoystickGetDeviceGUID( device_index );
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   739
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   740
6693
3c98e4ec2180 Fixed iOS joystick support for new API
Sam Lantinga <slouken@libsdl.org>
parents: 6690
diff changeset
   741
/* return the guid for this opened device */
6738
b408d5a406a3 Changes from Alfred:
Sam Lantinga <slouken@libsdl.org>
parents: 6735
diff changeset
   742
SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick * joystick)
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   743
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   744
    return SDL_SYS_JoystickGetGUID( joystick );
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   745
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   746
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   747
/* convert the guid to a printable string */
6738
b408d5a406a3 Changes from Alfred:
Sam Lantinga <slouken@libsdl.org>
parents: 6735
diff changeset
   748
void SDL_JoystickGetGUIDString( SDL_JoystickGUID guid, char *pszGUID, int cbGUID )
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   749
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   750
    static const char k_rgchHexToASCII[] = "0123456789abcdef";
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   751
    int i;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   752
6748
80a6aac67ad9 Minor sanity checking and tweaks in SDL_JoystickGetGUIDString().
Ryan C. Gordon <icculus@icculus.org>
parents: 6738
diff changeset
   753
    if ((pszGUID == NULL) || (cbGUID <= 0)) {
80a6aac67ad9 Minor sanity checking and tweaks in SDL_JoystickGetGUIDString().
Ryan C. Gordon <icculus@icculus.org>
parents: 6738
diff changeset
   754
        return;
80a6aac67ad9 Minor sanity checking and tweaks in SDL_JoystickGetGUIDString().
Ryan C. Gordon <icculus@icculus.org>
parents: 6738
diff changeset
   755
    }
80a6aac67ad9 Minor sanity checking and tweaks in SDL_JoystickGetGUIDString().
Ryan C. Gordon <icculus@icculus.org>
parents: 6738
diff changeset
   756
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   757
    for ( i = 0; i < sizeof(guid.data) && i < (cbGUID-1); i++ )
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   758
    {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   759
        /* each input byte writes 2 ascii chars, and might write a null byte. */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   760
        /* If we don't have room for next input byte, stop */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   761
        unsigned char c = guid.data[i];
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   762
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   763
        *pszGUID++ = k_rgchHexToASCII[ c >> 4 ];
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   764
        *pszGUID++ = k_rgchHexToASCII[ c & 0x0F ];
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   765
    }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   766
    *pszGUID = '\0';
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   767
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   768
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   769
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   770
/*-----------------------------------------------------------------------------
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   771
 * Purpose: Returns the 4 bit nibble for a hex character
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   772
 * Input  : c -
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   773
 * Output : unsigned char
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   774
 *-----------------------------------------------------------------------------*/
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   775
static unsigned char nibble( char c )
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   776
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   777
    if ( ( c >= '0' ) &&
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   778
        ( c <= '9' ) )
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   779
    {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   780
        return (unsigned char)(c - '0');
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   781
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   782
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   783
    if ( ( c >= 'A' ) &&
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   784
        ( c <= 'F' ) )
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   785
    {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   786
        return (unsigned char)(c - 'A' + 0x0a);
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   787
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   788
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   789
    if ( ( c >= 'a' ) &&
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   790
        ( c <= 'f' ) )
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   791
    {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   792
        return (unsigned char)(c - 'a' + 0x0a);
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   793
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   794
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   795
    /* received an invalid character, and no real way to return an error */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   796
    /* AssertMsg1( false, "Q_nibble invalid hex character '%c' ", c ); */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   797
    return 0;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   798
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   799
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   800
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   801
/* convert the string version of a joystick guid to the struct */
6738
b408d5a406a3 Changes from Alfred:
Sam Lantinga <slouken@libsdl.org>
parents: 6735
diff changeset
   802
SDL_JoystickGUID SDL_JoystickGetGUIDFromString(const char *pchGUID)
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   803
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   804
    SDL_JoystickGUID guid;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   805
    int maxoutputbytes= sizeof(guid);
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   806
    int len = SDL_strlen( pchGUID );
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   807
    Uint8 *p;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   808
    int i;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   809
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   810
    /* Make sure it's even */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   811
    len = ( len ) & ~0x1;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   812
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   813
    SDL_memset( &guid, 0x00, sizeof(guid) );
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   814
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   815
    p = (Uint8 *)&guid;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   816
    for ( i = 0;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   817
        ( i < len ) && ( ( p - (Uint8 *)&guid ) < maxoutputbytes );
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   818
        i+=2, p++ )
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   819
    {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   820
        *p = ( nibble( pchGUID[i] ) << 4 ) | nibble( pchGUID[i+1] );
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   821
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   822
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   823
    return guid;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   824
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   825
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   826
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
   827
/* vi: set ts=4 sw=4 expandtab: */