src/joystick/SDL_joystick.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 09 Apr 2015 22:33:39 -0400
branchiOS-improvements
changeset 9542 618662dc9e82
parent 9433 bd062398b648
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Closing the iOS-improvements branch.
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
8149
681eb46b8ac4 Fixed bug 2374 - Update copyright for 2014...
Sam Lantinga <slouken@libsdl.org>
parents: 8093
diff changeset
     3
  Copyright (C) 1997-2014 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
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7922
diff changeset
    21
#include "../SDL_internal.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
7337
5ba1fafc6a94 Only check for keyboard focus if the video subsystem was initialized.
Sam Lantinga <slouken@libsdl.org>
parents: 7307
diff changeset
    25
#include "SDL.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    26
#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
    27
#include "SDL_sysjoystick.h"
5861
b781fa6468aa Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 5859
diff changeset
    28
#include "SDL_assert.h"
7279
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
    29
#include "SDL_hints.h"
5861
b781fa6468aa Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 5859
diff changeset
    30
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    31
#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
    32
#include "../events/SDL_events_c.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    33
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    34
7337
5ba1fafc6a94 Only check for keyboard focus if the video subsystem was initialized.
Sam Lantinga <slouken@libsdl.org>
parents: 7307
diff changeset
    35
static SDL_bool SDL_joystick_allows_background_events = SDL_FALSE;
6712
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
    36
static SDL_Joystick *SDL_joysticks = NULL;
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
    37
static SDL_Joystick *SDL_updating_joystick = NULL;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    38
7432
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7360
diff changeset
    39
static void
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7360
diff changeset
    40
SDL_JoystickAllowBackgroundEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7360
diff changeset
    41
{
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7360
diff changeset
    42
    if (hint && *hint == '1') {
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7360
diff changeset
    43
        SDL_joystick_allows_background_events = SDL_TRUE;
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7360
diff changeset
    44
    } else {
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7360
diff changeset
    45
        SDL_joystick_allows_background_events = SDL_FALSE;
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7360
diff changeset
    46
    }
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7360
diff changeset
    47
}
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7360
diff changeset
    48
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
    49
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
    50
SDL_JoystickInit(void)
0
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 status;
7500
6b484ff38f86 Corrected internal documentation in source.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7432
diff changeset
    53
7432
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7360
diff changeset
    54
    /* See if we should allow joystick events while in the background */
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7360
diff changeset
    55
    SDL_AddHintCallback(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7360
diff changeset
    56
                        SDL_JoystickAllowBackgroundEventsChanged, NULL);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    57
7360
b86947ea2749 Make it possible to use SDL events separately from the video subsystem.
Sam Lantinga <slouken@libsdl.org>
parents: 7337
diff changeset
    58
#if !SDL_EVENTS_DISABLED
b86947ea2749 Make it possible to use SDL events separately from the video subsystem.
Sam Lantinga <slouken@libsdl.org>
parents: 7337
diff changeset
    59
    if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) {
b86947ea2749 Make it possible to use SDL events separately from the video subsystem.
Sam Lantinga <slouken@libsdl.org>
parents: 7337
diff changeset
    60
        return -1;
b86947ea2749 Make it possible to use SDL events separately from the video subsystem.
Sam Lantinga <slouken@libsdl.org>
parents: 7337
diff changeset
    61
    }
b86947ea2749 Make it possible to use SDL events separately from the video subsystem.
Sam Lantinga <slouken@libsdl.org>
parents: 7337
diff changeset
    62
#endif /* !SDL_EVENTS_DISABLED */
b86947ea2749 Make it possible to use SDL events separately from the video subsystem.
Sam Lantinga <slouken@libsdl.org>
parents: 7337
diff changeset
    63
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
    64
    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
    65
    if (status >= 0) {
7337
5ba1fafc6a94 Only check for keyboard focus if the video subsystem was initialized.
Sam Lantinga <slouken@libsdl.org>
parents: 7307
diff changeset
    66
        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
    67
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    68
    return (status);
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
 * Count the number of joysticks attached to the system
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    73
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
    74
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
    75
SDL_NumJoysticks(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    76
{
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
    77
    return SDL_SYS_NumJoysticks();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    78
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    79
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    80
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    81
 * Get the implementation dependent name of a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    82
 */
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
    83
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
    84
SDL_JoystickNameForIndex(int device_index)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    85
{
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
    86
    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
    87
        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
    88
        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
    89
    }
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6693
diff changeset
    90
    return (SDL_SYS_JoystickNameForDeviceIndex(device_index));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    91
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    92
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    93
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    94
 * Open a joystick for use - the index passed as an argument refers to
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    95
 * 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
    96
 * identify this joystick in future joystick events.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    97
 *
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    98
 * This function returns a joystick identifier, or NULL if an error occurred.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    99
 */
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
   100
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
   101
SDL_JoystickOpen(int device_index)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   102
{
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
   103
    SDL_Joystick *joystick;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   104
    SDL_Joystick *joysticklist;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   105
    const char *joystickname = NULL;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   106
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   107
    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
   108
        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
   109
        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
   110
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   111
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   112
    joysticklist = SDL_joysticks;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   113
    /* 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
   114
    * 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
   115
    */
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   116
    while (joysticklist) {
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   117
        if (SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == joysticklist->instance_id) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   118
                joystick = joysticklist;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   119
                ++joystick->ref_count;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   120
                return (joystick);
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   121
        }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   122
        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
   123
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   124
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
   125
    /* 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
   126
    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
   127
    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
   128
        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
   129
        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
   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
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
    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
   133
    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
   134
        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
   135
        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
   136
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   137
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   138
    joystickname = SDL_SYS_JoystickNameForDeviceIndex(device_index);
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   139
    if (joystickname)
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   140
        joystick->name = SDL_strdup(joystickname);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   141
    else
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   142
        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
   143
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
   144
    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
   145
        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
   146
            (joystick->naxes * sizeof(Sint16));
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   147
    }
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   148
    if (joystick->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
   149
        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
   150
            (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
   151
    }
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   152
    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
   153
        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
   154
            (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
   155
    }
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
    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
   157
        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
   158
            (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
   159
    }
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
   160
    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
   161
        || ((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
   162
        || ((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
   163
        || ((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
   164
        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
   165
        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
   166
        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
   167
    }
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
    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
   169
        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
   170
    }
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   171
    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
   172
        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
   173
    }
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
   174
    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
   175
        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
   176
            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
   177
    }
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   178
    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
   179
        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
   180
    }
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   181
a5a37f850d83 Merged r3787:3788 from branches/SDL-1.2: better failures for joystick opening.
Ryan C. Gordon <icculus@icculus.org>
parents: 3369
diff changeset
   182
    /* 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
   183
    ++joystick->ref_count;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   184
    /* 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
   185
    joystick->next = SDL_joysticks;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   186
    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
   187
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   188
    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
   189
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
   190
    return (joystick);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   191
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   192
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   193
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   194
/*
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   195
 * 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
   196
 */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
   197
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
   198
SDL_PrivateJoystickValid(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   199
{
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
   200
    int valid;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   201
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   202
    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
   203
        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
   204
        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
   205
    } 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
   206
        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
   207
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   208
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
   209
    return valid;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   210
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   211
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   212
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   213
 * Get the number of multi-dimensional axis controls on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   214
 */
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
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
   216
SDL_JoystickNumAxes(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   217
{
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   218
    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
   219
        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
   220
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   221
    return (joystick->naxes);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   222
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   223
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   224
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   225
 * Get the number of hats on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   226
 */
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
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
   228
SDL_JoystickNumHats(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   229
{
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   230
    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
   231
        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
   232
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   233
    return (joystick->nhats);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   234
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   235
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   236
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   237
 * Get the number of trackballs on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   238
 */
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
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
   240
SDL_JoystickNumBalls(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   241
{
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   242
    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
   243
        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
   244
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   245
    return (joystick->nballs);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   246
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   247
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   248
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   249
 * Get the number of buttons on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   250
 */
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
   251
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
   252
SDL_JoystickNumButtons(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   253
{
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   254
    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
   255
        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
   256
    }
c121d94672cb SDL 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
    return (joystick->nbuttons);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   258
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   259
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   260
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   261
 * Get the current state of an axis control on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   262
 */
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
   263
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
   264
SDL_JoystickGetAxis(SDL_Joystick * joystick, int axis)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   265
{
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
   266
    Sint16 state;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   267
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   268
    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
   269
        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
   270
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   271
    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
   272
        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
   273
    } 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
   274
        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
   275
        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
   276
    }
c121d94672cb SDL 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
    return (state);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   278
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   279
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   280
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   281
 * Get the current state of a hat on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   282
 */
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
   283
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
   284
SDL_JoystickGetHat(SDL_Joystick * joystick, int hat)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   285
{
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
   286
    Uint8 state;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   287
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   288
    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
   289
        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
   290
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   291
    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
   292
        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
   293
    } 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
   294
        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
   295
        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
   296
    }
c121d94672cb SDL 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
    return (state);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   298
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   299
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   300
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   301
 * Get the ball axis change since the last poll
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   302
 */
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
   303
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
   304
SDL_JoystickGetBall(SDL_Joystick * joystick, int ball, int *dx, int *dy)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   305
{
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
   306
    int retval;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   307
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   308
    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
   309
        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
   310
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   311
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
   312
    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
   313
    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
   314
        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
   315
            *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
   316
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   317
        if (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
   318
            *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
   319
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   320
        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
   321
        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
   322
    } else {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   323
        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
   324
    }
c121d94672cb SDL 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
    return (retval);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   326
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   327
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   328
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   329
 * Get the current state of a button on a joystick
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   330
 */
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
   331
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
   332
SDL_JoystickGetButton(SDL_Joystick * joystick, int button)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   333
{
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
   334
    Uint8 state;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   335
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   336
    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
   337
        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
   338
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   339
    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
   340
        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
   341
    } 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
   342
        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
   343
        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
   344
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   345
    return (state);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   346
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   347
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   348
/*
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   349
 * Return if the joystick in question is currently attached to the system,
7500
6b484ff38f86 Corrected internal documentation in source.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7432
diff changeset
   350
 *  \return SDL_FALSE if not plugged in, SDL_TRUE if still present.
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
 */
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6693
diff changeset
   352
SDL_bool
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6693
diff changeset
   353
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
   354
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   355
    if (!SDL_PrivateJoystickValid(joystick)) {
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6693
diff changeset
   356
        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
   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
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   359
    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
   360
}
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
/*
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   363
 * 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
   364
 */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   365
SDL_JoystickID
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6693
diff changeset
   366
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
   367
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   368
    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
   369
        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
   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
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   372
    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
   373
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   374
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   375
/*
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   376
 * 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
   377
 */
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   378
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
   379
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
   380
{
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 (!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
   382
        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
   383
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   384
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   385
    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
   386
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   387
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   388
/*
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   389
 * Close a joystick previously opened with SDL_JoystickOpen()
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   390
 */
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
   391
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
   392
SDL_JoystickClose(SDL_Joystick * joystick)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   393
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   394
    SDL_Joystick *joysticklist;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   395
    SDL_Joystick *joysticklistprev;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   396
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   397
    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
   398
        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
   399
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   400
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
   401
    /* 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
   402
    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
   403
        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
   404
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   405
6712
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   406
    if (joystick == SDL_updating_joystick) {
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   407
        return;
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   408
    }
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   409
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
   410
    SDL_SYS_JoystickClose(joystick);
9433
bd062398b648 Cleanups in the joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8920
diff changeset
   411
    joystick->hwdata = NULL;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   412
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   413
    joysticklist = SDL_joysticks;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   414
    joysticklistprev = NULL;
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   415
    while (joysticklist) {
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   416
        if (joystick == joysticklist) {
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   417
            if (joysticklistprev) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   418
                /* unlink this entry */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   419
                joysticklistprev->next = joysticklist->next;
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   420
            } else {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   421
                SDL_joysticks = joystick->next;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   422
            }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   423
            break;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   424
        }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   425
        joysticklistprev = joysticklist;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   426
        joysticklist = joysticklist->next;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   427
    }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   428
7719
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7677
diff changeset
   429
    SDL_free(joystick->name);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   430
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
   431
    /* Free the data associated with this joystick */
7719
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7677
diff changeset
   432
    SDL_free(joystick->axes);
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7677
diff changeset
   433
    SDL_free(joystick->hats);
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7677
diff changeset
   434
    SDL_free(joystick->balls);
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7677
diff changeset
   435
    SDL_free(joystick->buttons);
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
   436
    SDL_free(joystick);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   437
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   438
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
   439
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
   440
SDL_JoystickQuit(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   441
{
6712
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   442
    /* 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
   443
    SDL_assert(!SDL_updating_joystick);
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   444
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
   445
    /* Stop the event polling */
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   446
    while (SDL_joysticks) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   447
        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
   448
        SDL_JoystickClose(SDL_joysticks);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   449
    }
5856
173d28fd4be4 Clean up any opened joysticks during SDL_JoystickQuit().
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   450
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
   451
    /* 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
   452
    SDL_SYS_JoystickQuit();
7360
b86947ea2749 Make it possible to use SDL events separately from the video subsystem.
Sam Lantinga <slouken@libsdl.org>
parents: 7337
diff changeset
   453
b86947ea2749 Make it possible to use SDL events separately from the video subsystem.
Sam Lantinga <slouken@libsdl.org>
parents: 7337
diff changeset
   454
#if !SDL_EVENTS_DISABLED
b86947ea2749 Make it possible to use SDL events separately from the video subsystem.
Sam Lantinga <slouken@libsdl.org>
parents: 7337
diff changeset
   455
    SDL_QuitSubSystem(SDL_INIT_EVENTS);
b86947ea2749 Make it possible to use SDL events separately from the video subsystem.
Sam Lantinga <slouken@libsdl.org>
parents: 7337
diff changeset
   456
#endif
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   457
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   458
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   459
7279
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   460
static SDL_bool
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   461
SDL_PrivateJoystickShouldIgnoreEvent()
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   462
{
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   463
    if (SDL_joystick_allows_background_events) {
7279
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   464
        return SDL_FALSE;
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   465
    }
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   466
7337
5ba1fafc6a94 Only check for keyboard focus if the video subsystem was initialized.
Sam Lantinga <slouken@libsdl.org>
parents: 7307
diff changeset
   467
    if (SDL_WasInit(SDL_INIT_VIDEO)) {
5ba1fafc6a94 Only check for keyboard focus if the video subsystem was initialized.
Sam Lantinga <slouken@libsdl.org>
parents: 7307
diff changeset
   468
        if (SDL_GetKeyboardFocus() == NULL) {
7677
871d43c6968a OCD fixes: Adds a space before */
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7662
diff changeset
   469
            /* Video is initialized and we don't have focus, ignore the event. */
7337
5ba1fafc6a94 Only check for keyboard focus if the video subsystem was initialized.
Sam Lantinga <slouken@libsdl.org>
parents: 7307
diff changeset
   470
            return SDL_TRUE;
5ba1fafc6a94 Only check for keyboard focus if the video subsystem was initialized.
Sam Lantinga <slouken@libsdl.org>
parents: 7307
diff changeset
   471
        } else {
5ba1fafc6a94 Only check for keyboard focus if the video subsystem was initialized.
Sam Lantinga <slouken@libsdl.org>
parents: 7307
diff changeset
   472
            return SDL_FALSE;
5ba1fafc6a94 Only check for keyboard focus if the video subsystem was initialized.
Sam Lantinga <slouken@libsdl.org>
parents: 7307
diff changeset
   473
        }
7279
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   474
    }
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   475
7677
871d43c6968a OCD fixes: Adds a space before */
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7662
diff changeset
   476
    /* Video subsystem wasn't initialized, always allow the event */
7337
5ba1fafc6a94 Only check for keyboard focus if the video subsystem was initialized.
Sam Lantinga <slouken@libsdl.org>
parents: 7307
diff changeset
   477
    return SDL_FALSE;
7279
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   478
}
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   479
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   480
/* These are global for SDL_sysjoystick.c and SDL_events.c */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   481
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
   482
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
   483
SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   484
{
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
   485
    int posted;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   486
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   487
    /* Make sure we're not getting garbage or duplicate events */
6145
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   488
    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
   489
        return 0;
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   490
    }
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   491
    if (value == joystick->axes[axis]) {
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   492
        return 0;
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   493
    }
6145
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   494
8903
8e09eceea61e You shouldn't get axis and hat events when your application doesn't have focus (unless you use the SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS hint)
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   495
    /* We ignore events if we don't have keyboard focus, except for centering
8e09eceea61e You shouldn't get axis and hat events when your application doesn't have focus (unless you use the SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS hint)
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   496
     * events.
8e09eceea61e You shouldn't get axis and hat events when your application doesn't have focus (unless you use the SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS hint)
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   497
     */
8e09eceea61e You shouldn't get axis and hat events when your application doesn't have focus (unless you use the SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS hint)
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   498
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
8e09eceea61e You shouldn't get axis and hat events when your application doesn't have focus (unless you use the SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS hint)
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   499
        if ((value > 0 && value >= joystick->axes[axis]) ||
8e09eceea61e You shouldn't get axis and hat events when your application doesn't have focus (unless you use the SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS hint)
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   500
            (value < 0 && value <= joystick->axes[axis])) {
8e09eceea61e You shouldn't get axis and hat events when your application doesn't have focus (unless you use the SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS hint)
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   501
            return 0;
8e09eceea61e You shouldn't get axis and hat events when your application doesn't have focus (unless you use the SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS hint)
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   502
        }
8e09eceea61e You shouldn't get axis and hat events when your application doesn't have focus (unless you use the SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS hint)
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   503
    }
8e09eceea61e You shouldn't get axis and hat events when your application doesn't have focus (unless you use the SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS hint)
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   504
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
    /* 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
   506
    joystick->axes[axis] = value;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   507
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
    /* 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
   509
    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
   510
#if !SDL_EVENTS_DISABLED
4429
faa9fc8e7f67 General improvements for user custom event registration
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   511
    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
   512
        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
   513
        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
   514
        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
   515
        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
   516
        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
   517
        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
   518
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   519
#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
   520
    return (posted);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   521
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
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
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
   524
SDL_PrivateJoystickHat(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   525
{
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
   526
    int posted;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   527
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   528
    /* Make sure we're not getting garbage or duplicate events */
6145
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   529
    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
   530
        return 0;
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   531
    }
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   532
    if (value == joystick->hats[hat]) {
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   533
        return 0;
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   534
    }
6145
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   535
7279
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   536
    /* 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
   537
     * events.
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   538
     */
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   539
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
8903
8e09eceea61e You shouldn't get axis and hat events when your application doesn't have focus (unless you use the SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS hint)
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   540
        if (value != SDL_HAT_CENTERED) {
7279
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   541
            return 0;
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   542
        }
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   543
    }
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   544
8903
8e09eceea61e You shouldn't get axis and hat events when your application doesn't have focus (unless you use the SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS hint)
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   545
    /* Update internal joystick state */
8e09eceea61e You shouldn't get axis and hat events when your application doesn't have focus (unless you use the SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS hint)
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   546
    joystick->hats[hat] = value;
7279
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   547
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
    /* 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
   549
    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
   550
#if !SDL_EVENTS_DISABLED
4429
faa9fc8e7f67 General improvements for user custom event registration
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   551
    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
   552
        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
   553
        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
   554
        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
   555
        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
   556
        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
   557
        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
   558
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   559
#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
   560
    return (posted);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   561
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   562
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
   563
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
   564
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
   565
                        Sint16 xrel, Sint16 yrel)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
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
    int posted;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   568
6145
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   569
    /* 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
   570
    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
   571
        return 0;
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   572
    }
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   573
7279
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   574
    /* 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
   575
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   576
        return 0;
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   577
    }
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   578
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   579
    /* 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
   580
    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
   581
    joystick->balls[ball].dy += yrel;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   582
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   583
    /* 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
   584
    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
   585
#if !SDL_EVENTS_DISABLED
4429
faa9fc8e7f67 General improvements for user custom event registration
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   586
    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
   587
        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
   588
        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
   589
        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
   590
        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
   591
        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
   592
        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
   593
        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
   594
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   595
#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
   596
    return (posted);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   597
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   598
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
   599
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
   600
SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   601
{
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
   602
    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
   603
#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
   604
    SDL_Event event;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   605
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
   606
    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
   607
    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
   608
        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
   609
        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
   610
    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
   611
        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
   612
        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
   613
    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
   614
        /* 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
   615
        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
   616
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   617
#endif /* !SDL_EVENTS_DISABLED */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   618
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   619
    /* Make sure we're not getting garbage or duplicate events */
6145
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   620
    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
   621
        return 0;
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   622
	}
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   623
	if (state == joystick->buttons[button]) {
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   624
		return 0;
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   625
	}
6145
964d13f789e2 Fixed bug 1337 - joystick crash due to heap corruption with btnx
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   626
7279
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   627
    /* 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
   628
     * release. */
8903
8e09eceea61e You shouldn't get axis and hat events when your application doesn't have focus (unless you use the SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS hint)
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   629
    if (SDL_PrivateJoystickShouldIgnoreEvent()) {
8e09eceea61e You shouldn't get axis and hat events when your application doesn't have focus (unless you use the SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS hint)
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   630
        if (state == SDL_PRESSED) {
8e09eceea61e You shouldn't get axis and hat events when your application doesn't have focus (unless you use the SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS hint)
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   631
            return 0;
8e09eceea61e You shouldn't get axis and hat events when your application doesn't have focus (unless you use the SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS hint)
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   632
        }
7279
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   633
    }
f7805b13b485 Joystick: Only send joy events when focused.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   634
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
   635
    /* 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
   636
    joystick->buttons[button] = state;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   637
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
   638
    /* 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
   639
    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
   640
#if !SDL_EVENTS_DISABLED
4429
faa9fc8e7f67 General improvements for user custom event registration
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   641
    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
   642
        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
   643
        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
   644
        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
   645
        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
   646
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   647
#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
   648
    return (posted);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   649
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   650
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
   651
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
   652
SDL_JoystickUpdate(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   653
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   654
    SDL_Joystick *joystick;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   655
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   656
    joystick = SDL_joysticks;
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   657
    while (joystick) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   658
        SDL_Joystick *joysticknext;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   659
        /* 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
   660
         * 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
   661
         */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   662
        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
   663
6712
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   664
        SDL_updating_joystick = joystick;
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   665
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   666
        SDL_SYS_JoystickUpdate(joystick);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   667
9433
bd062398b648 Cleanups in the joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8920
diff changeset
   668
        if (joystick->force_recentering) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   669
            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
   670
6712
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   671
            /* 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
   672
            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
   673
                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
   674
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   675
            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
   676
                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
   677
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   678
            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
   679
                SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
6712
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   680
9433
bd062398b648 Cleanups in the joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8920
diff changeset
   681
            joystick->force_recentering = SDL_FALSE;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   682
        }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   683
6712
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   684
        SDL_updating_joystick = NULL;
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   685
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   686
        /* If the joystick was closed while updating, free it here */
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   687
        if (joystick->ref_count <= 0) {
6712
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   688
            SDL_JoystickClose(joystick);
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   689
        }
05f046f5886b Fixed crashes in new joystick code
Sam Lantinga <slouken@libsdl.org>
parents: 6707
diff changeset
   690
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   691
        joystick = joysticknext;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   692
    }
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
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   694
    /* 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
   695
       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
   696
     */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   697
    SDL_SYS_JoystickDetect();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   698
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   699
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
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
   701
SDL_JoystickEventState(int state)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   702
{
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   703
#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
   704
    return SDL_DISABLE;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   705
#else
4429
faa9fc8e7f67 General improvements for user custom event registration
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   706
    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
   707
        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
   708
        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
   709
    };
c121d94672cb SDL 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
    unsigned int i;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   711
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
   712
    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
   713
    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
   714
        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
   715
        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
   716
            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
   717
            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
   718
                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
   719
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   720
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   721
        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
   722
    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
   723
        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
   724
            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
   725
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   726
        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
   727
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1612
diff changeset
   728
    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
   729
#endif /* SDL_EVENTS_DISABLED */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   730
}
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
   731
6693
3c98e4ec2180 Fixed iOS joystick support for new API
Sam Lantinga <slouken@libsdl.org>
parents: 6690
diff changeset
   732
/* return the guid for this index */
6738
b408d5a406a3 Changes from Alfred:
Sam Lantinga <slouken@libsdl.org>
parents: 6735
diff changeset
   733
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
   734
{
7294
0923de98a63a Fixed crash trying to get the GUID of an invalid joystick index
Sam Lantinga <slouken@libsdl.org>
parents: 7279
diff changeset
   735
    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
   736
        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
   737
        SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   738
        SDL_zero(emptyGUID);
7294
0923de98a63a Fixed crash trying to get the GUID of an invalid joystick index
Sam Lantinga <slouken@libsdl.org>
parents: 7279
diff changeset
   739
        return emptyGUID;
0923de98a63a Fixed crash trying to get the GUID of an invalid joystick index
Sam Lantinga <slouken@libsdl.org>
parents: 7279
diff changeset
   740
    }
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   741
    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
   742
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   743
6693
3c98e4ec2180 Fixed iOS joystick support for new API
Sam Lantinga <slouken@libsdl.org>
parents: 6690
diff changeset
   744
/* return the guid for this opened device */
6738
b408d5a406a3 Changes from Alfred:
Sam Lantinga <slouken@libsdl.org>
parents: 6735
diff changeset
   745
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
   746
{
7789
a18c12b02b69 Check for NULL joystick in SDL_JoystickGetGUID()
Sam Lantinga <slouken@libsdl.org>
parents: 7731
diff changeset
   747
    if (!SDL_PrivateJoystickValid(joystick)) {
a18c12b02b69 Check for NULL joystick in SDL_JoystickGetGUID()
Sam Lantinga <slouken@libsdl.org>
parents: 7731
diff changeset
   748
        SDL_JoystickGUID emptyGUID;
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   749
        SDL_zero(emptyGUID);
7789
a18c12b02b69 Check for NULL joystick in SDL_JoystickGetGUID()
Sam Lantinga <slouken@libsdl.org>
parents: 7731
diff changeset
   750
        return emptyGUID;
a18c12b02b69 Check for NULL joystick in SDL_JoystickGetGUID()
Sam Lantinga <slouken@libsdl.org>
parents: 7731
diff changeset
   751
    }
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   752
    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
   753
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   754
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   755
/* convert the guid to a printable string */
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   756
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
   757
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   758
    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
   759
    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
   760
6748
80a6aac67ad9 Minor sanity checking and tweaks in SDL_JoystickGetGUIDString().
Ryan C. Gordon <icculus@icculus.org>
parents: 6738
diff changeset
   761
    if ((pszGUID == NULL) || (cbGUID <= 0)) {
80a6aac67ad9 Minor sanity checking and tweaks in SDL_JoystickGetGUIDString().
Ryan C. Gordon <icculus@icculus.org>
parents: 6738
diff changeset
   762
        return;
80a6aac67ad9 Minor sanity checking and tweaks in SDL_JoystickGetGUIDString().
Ryan C. Gordon <icculus@icculus.org>
parents: 6738
diff changeset
   763
    }
80a6aac67ad9 Minor sanity checking and tweaks in SDL_JoystickGetGUIDString().
Ryan C. Gordon <icculus@icculus.org>
parents: 6738
diff changeset
   764
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   765
    for (i = 0; i < sizeof(guid.data) && i < (cbGUID-1)/2; i++) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   766
        /* 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
   767
        /* 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
   768
        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
   769
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   770
        *pszGUID++ = k_rgchHexToASCII[c >> 4];
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   771
        *pszGUID++ = k_rgchHexToASCII[c & 0x0F];
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   772
    }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   773
    *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
   774
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   775
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
/*-----------------------------------------------------------------------------
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   778
 * 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
   779
 * Input  : c -
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   780
 * Output : unsigned char
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   781
 *-----------------------------------------------------------------------------*/
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   782
static unsigned char nibble(char c)
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   783
{
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   784
    if ((c >= '0') && (c <= '9')) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   785
        return (unsigned char)(c - '0');
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   786
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   787
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   788
    if ((c >= 'A') && (c <= 'F')) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   789
        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
   790
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   791
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   792
    if ((c >= 'a') && (c <= 'f')) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   793
        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
   794
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   795
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   796
    /* received an invalid character, and no real way to return an error */
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   797
    /* AssertMsg1(false, "Q_nibble invalid hex character '%c' ", c); */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   798
    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
   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
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   802
/* 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
   803
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
   804
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   805
    SDL_JoystickGUID guid;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   806
    int maxoutputbytes= sizeof(guid);
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   807
    size_t len = SDL_strlen(pchGUID);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   808
    Uint8 *p;
7922
6ebd3a9aee8c Fixed signed/unsigned warning
Sam Lantinga <slouken@libsdl.org>
parents: 7870
diff changeset
   809
    size_t 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
   810
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   811
    /* Make sure it's even */
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   812
    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
   813
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   814
    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
   815
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   816
    p = (Uint8 *)&guid;
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   817
    for (i = 0; (i < len) && ((p - (Uint8 *)&guid) < maxoutputbytes); i+=2, p++) {
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8903
diff changeset
   818
        *p = (nibble(pchGUID[i]) << 4) | nibble(pchGUID[i+1]);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   819
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   820
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7086
diff changeset
   821
    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
   822
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   823
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6145
diff changeset
   824
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
   825
/* vi: set ts=4 sw=4 expandtab: */