src/joystick/windows/SDL_dxjoystick.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 31 Mar 2013 12:48:50 -0400
changeset 7037 3fedf1f25b94
parent 6993 33f1e384ed56
child 7133 87d4cb9bb66c
permissions -rw-r--r--
Make SDL_SetError and friends unconditionally return -1.

This lets us change things like this...

if (Failed) {
SDL_SetError("We failed");
return -1;
}

...into this...

if (Failed) {
return SDL_SetError("We failed");
}


Fixes Bugzilla #1778.
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@1895
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1895
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1895
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1895
    20
*/
slouken@1895
    21
#include "SDL_config.h"
slouken@1895
    22
slouken@1895
    23
#ifdef SDL_JOYSTICK_DINPUT
slouken@1895
    24
slouken@1895
    25
/* DirectInput joystick driver; written by Glenn Maynard, based on Andrei de
slouken@1895
    26
 * A. Formiga's WINMM driver. 
slouken@1895
    27
 *
slouken@1895
    28
 * Hats and sliders are completely untested; the app I'm writing this for mostly
slouken@1895
    29
 * doesn't use them and I don't own any joysticks with them. 
slouken@1895
    30
 *
slouken@1895
    31
 * We don't bother to use event notification here.  It doesn't seem to work
slouken@6690
    32
 * with polled devices, and it's fine to call IDirectInputDevice8_GetDeviceData and
slouken@1895
    33
 * let it return 0 events. */
slouken@1895
    34
slouken@1895
    35
#include "SDL_error.h"
icculus@6993
    36
#include "SDL_assert.h"
slouken@1895
    37
#include "SDL_events.h"
slouken@1895
    38
#include "SDL_joystick.h"
slouken@1895
    39
#include "../SDL_sysjoystick.h"
slouken@3013
    40
#define INITGUID /* Only set here, if set twice will cause mingw32 to break. */
slouken@2713
    41
#include "SDL_dxjoystick_c.h"
slouken@6690
    42
#include "SDL_thread.h"
slouken@6690
    43
#include "SDL_timer.h"
slouken@6690
    44
#include "SDL_mutex.h"
slouken@6690
    45
#include "SDL_events.h"
slouken@6971
    46
#include "SDL_hints.h"
slouken@6690
    47
#if !SDL_EVENTS_DISABLED
slouken@6690
    48
#include "../../events/SDL_events_c.h"
slouken@6690
    49
#endif
slouken@1895
    50
slouken@2761
    51
#ifndef DIDFT_OPTIONAL
slouken@2761
    52
#define DIDFT_OPTIONAL		0x80000000
slouken@2761
    53
#endif
slouken@2761
    54
slouken@2761
    55
slouken@1895
    56
#define INPUT_QSIZE	32      /* Buffer up to 32 input messages */
slouken@6690
    57
#define MAX_JOYSTICKS 8
slouken@1895
    58
#define AXIS_MIN	-32768  /* minimum value for axis coordinate */
slouken@1895
    59
#define AXIS_MAX	32767   /* maximum value for axis coordinate */
slouken@1895
    60
#define JOY_AXIS_THRESHOLD	(((AXIS_MAX)-(AXIS_MIN))/100)   /* 1% motion */
slouken@1895
    61
slouken@2198
    62
/* external variables referenced. */
slouken@2713
    63
extern HWND SDL_HelperWindow;
slouken@2198
    64
slouken@2198
    65
slouken@2198
    66
/* local variables */
icculus@5591
    67
static SDL_bool coinitialized = SDL_FALSE;
icculus@6716
    68
static LPDIRECTINPUT8 dinput = NULL;
slouken@6690
    69
static SDL_bool s_bDeviceAdded = SDL_FALSE;
slouken@6690
    70
static SDL_bool s_bDeviceRemoved = SDL_FALSE;
slouken@6974
    71
static SDL_JoystickID s_nInstanceID = -1;
slouken@6690
    72
static GUID *s_pKnownJoystickGUIDs = NULL;
slouken@6690
    73
static SDL_cond *s_condJoystickThread = NULL;
slouken@6690
    74
static SDL_mutex *s_mutexJoyStickEnum = NULL;
slouken@6690
    75
static SDL_Thread *s_threadJoystick = NULL;
slouken@6690
    76
static SDL_bool s_bJoystickThreadQuit = SDL_FALSE;
urkle@6965
    77
static SDL_bool s_bXInputEnabled = SDL_TRUE;
slouken@6690
    78
icculus@6991
    79
XInputGetState_t SDL_XInputGetState = NULL;
icculus@6991
    80
XInputSetState_t SDL_XInputSetState = NULL;
icculus@6991
    81
XInputGetCapabilities_t SDL_XInputGetCapabilities = NULL;
icculus@6991
    82
DWORD SDL_XInputVersion = 0;
icculus@6991
    83
icculus@6991
    84
static HANDLE s_pXInputDLL = 0;
icculus@6991
    85
static int s_XInputDLLRefCount = 0;
icculus@6991
    86
icculus@6991
    87
int
icculus@6991
    88
WIN_LoadXInputDLL(void)
icculus@6991
    89
{
icculus@6991
    90
    DWORD version = 0;
icculus@6991
    91
icculus@6991
    92
    if (s_pXInputDLL) {
icculus@6991
    93
        SDL_assert(s_XInputDLLRefCount > 0);
icculus@6991
    94
        s_XInputDLLRefCount++;
icculus@6991
    95
        return 0;  /* already loaded */
icculus@6991
    96
    }
icculus@6991
    97
icculus@6991
    98
    version = (1 << 16) | 4;
icculus@6991
    99
    s_pXInputDLL = LoadLibrary( L"XInput1_4.dll" );  // 1.4 Ships with Windows 8.
icculus@6991
   100
    if (!s_pXInputDLL) {
icculus@6991
   101
        version = (1 << 16) | 3;
icculus@6991
   102
        s_pXInputDLL = LoadLibrary( L"XInput1_3.dll" );  // 1.3 Ships with Vista and Win7, can be installed as a restributable component.
icculus@6991
   103
    }
icculus@6991
   104
    if (!s_pXInputDLL) {
icculus@6991
   105
        s_pXInputDLL = LoadLibrary( L"bin\\XInput1_3.dll" );
icculus@6991
   106
    }
icculus@6991
   107
    if (!s_pXInputDLL) {
icculus@6991
   108
        return -1;
icculus@6991
   109
    }
icculus@6991
   110
icculus@6991
   111
    SDL_assert(s_XInputDLLRefCount == 0);
icculus@6991
   112
    SDL_XInputVersion = version;
icculus@6991
   113
    s_XInputDLLRefCount = 1;
icculus@6991
   114
icculus@6991
   115
    /* 100 is the ordinal for _XInputGetStateEx, which returns the same struct as XinputGetState, but with extra data in wButtons for the guide button, we think... */
icculus@6991
   116
    SDL_XInputGetState = (XInputGetState_t)GetProcAddress( (HMODULE)s_pXInputDLL, (LPCSTR)100 );
icculus@6991
   117
    SDL_XInputSetState = (XInputSetState_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputSetState" );
icculus@6991
   118
    SDL_XInputGetCapabilities = (XInputGetCapabilities_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputGetCapabilities" );
icculus@6991
   119
    if ( !SDL_XInputGetState || !SDL_XInputSetState || !SDL_XInputGetCapabilities ) {
icculus@6991
   120
        WIN_UnloadXInputDLL();
icculus@6991
   121
        return -1;
icculus@6991
   122
    }
icculus@6991
   123
icculus@6991
   124
    return 0;
icculus@6991
   125
}
icculus@6991
   126
icculus@6991
   127
void
icculus@6991
   128
WIN_UnloadXInputDLL(void)
icculus@6991
   129
{
icculus@6991
   130
    if ( s_pXInputDLL ) {
icculus@6991
   131
        SDL_assert(s_XInputDLLRefCount > 0);
icculus@6991
   132
        if (--s_XInputDLLRefCount == 0) {
icculus@6991
   133
            FreeLibrary( s_pXInputDLL );
icculus@6991
   134
            s_pXInputDLL = NULL;
icculus@6991
   135
        }
icculus@6991
   136
    } else {
icculus@6991
   137
        SDL_assert(s_XInputDLLRefCount == 0);
icculus@6991
   138
    }
icculus@6991
   139
}
icculus@6991
   140
icculus@6991
   141
slouken@2198
   142
extern HRESULT(WINAPI * DInputCreate) (HINSTANCE hinst, DWORD dwVersion,
slouken@2198
   143
                                       LPDIRECTINPUT * ppDI,
slouken@2198
   144
                                       LPUNKNOWN punkOuter);
slouken@6690
   145
struct JoyStick_DeviceData_
slouken@6690
   146
{
slouken@6738
   147
	SDL_JoystickGUID guid;
slouken@6690
   148
	DIDEVICEINSTANCE dxdevice;
slouken@6690
   149
	char *joystickname;
slouken@6690
   150
	Uint8 send_add_event;
slouken@6974
   151
	SDL_JoystickID nInstanceID;
slouken@6707
   152
	SDL_bool bXInputDevice;
slouken@6690
   153
	Uint8 XInputUserId;
slouken@6690
   154
	struct JoyStick_DeviceData_ *pNext;
slouken@6690
   155
};
slouken@2198
   156
slouken@6690
   157
typedef struct JoyStick_DeviceData_ JoyStick_DeviceData;
slouken@6690
   158
slouken@6690
   159
static JoyStick_DeviceData *SYS_Joystick;    /* array to hold joystick ID values */
slouken@6690
   160
slouken@2198
   161
/* local prototypes */
icculus@7037
   162
static int SetDIerror(const char *function, HRESULT code);
slouken@2198
   163
static BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE *
slouken@2198
   164
                                           pdidInstance, VOID * pContext);
slouken@2198
   165
static BOOL CALLBACK EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev,
slouken@2198
   166
                                            LPVOID pvRef);
slouken@6220
   167
static void SortDevObjects(SDL_Joystick *joystick);
slouken@2198
   168
static Uint8 TranslatePOV(DWORD value);
slouken@2198
   169
static int SDL_PrivateJoystickAxis_Int(SDL_Joystick * joystick, Uint8 axis,
slouken@2198
   170
                                       Sint16 value);
slouken@2198
   171
static int SDL_PrivateJoystickHat_Int(SDL_Joystick * joystick, Uint8 hat,
slouken@2198
   172
                                      Uint8 value);
slouken@2198
   173
static int SDL_PrivateJoystickButton_Int(SDL_Joystick * joystick,
slouken@2198
   174
                                         Uint8 button, Uint8 state);
slouken@2198
   175
slouken@6690
   176
// Taken from Wine - Thanks! 
slouken@2760
   177
DIOBJECTDATAFORMAT dfDIJoystick2[] = {
slouken@3013
   178
  { &GUID_XAxis,DIJOFS_X,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   179
  { &GUID_YAxis,DIJOFS_Y,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   180
  { &GUID_ZAxis,DIJOFS_Z,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   181
  { &GUID_RxAxis,DIJOFS_RX,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   182
  { &GUID_RyAxis,DIJOFS_RY,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   183
  { &GUID_RzAxis,DIJOFS_RZ,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   184
  { &GUID_Slider,DIJOFS_SLIDER(0),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   185
  { &GUID_Slider,DIJOFS_SLIDER(1),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   186
  { &GUID_POV,DIJOFS_POV(0),DIDFT_OPTIONAL|DIDFT_POV|DIDFT_ANYINSTANCE,0},
slouken@3013
   187
  { &GUID_POV,DIJOFS_POV(1),DIDFT_OPTIONAL|DIDFT_POV|DIDFT_ANYINSTANCE,0},
slouken@3013
   188
  { &GUID_POV,DIJOFS_POV(2),DIDFT_OPTIONAL|DIDFT_POV|DIDFT_ANYINSTANCE,0},
slouken@3013
   189
  { &GUID_POV,DIJOFS_POV(3),DIDFT_OPTIONAL|DIDFT_POV|DIDFT_ANYINSTANCE,0},
slouken@3013
   190
  { NULL,DIJOFS_BUTTON(0),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   191
  { NULL,DIJOFS_BUTTON(1),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   192
  { NULL,DIJOFS_BUTTON(2),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   193
  { NULL,DIJOFS_BUTTON(3),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   194
  { NULL,DIJOFS_BUTTON(4),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   195
  { NULL,DIJOFS_BUTTON(5),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   196
  { NULL,DIJOFS_BUTTON(6),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   197
  { NULL,DIJOFS_BUTTON(7),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   198
  { NULL,DIJOFS_BUTTON(8),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   199
  { NULL,DIJOFS_BUTTON(9),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   200
  { NULL,DIJOFS_BUTTON(10),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   201
  { NULL,DIJOFS_BUTTON(11),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   202
  { NULL,DIJOFS_BUTTON(12),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   203
  { NULL,DIJOFS_BUTTON(13),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   204
  { NULL,DIJOFS_BUTTON(14),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   205
  { NULL,DIJOFS_BUTTON(15),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   206
  { NULL,DIJOFS_BUTTON(16),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   207
  { NULL,DIJOFS_BUTTON(17),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   208
  { NULL,DIJOFS_BUTTON(18),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   209
  { NULL,DIJOFS_BUTTON(19),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   210
  { NULL,DIJOFS_BUTTON(20),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   211
  { NULL,DIJOFS_BUTTON(21),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   212
  { NULL,DIJOFS_BUTTON(22),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   213
  { NULL,DIJOFS_BUTTON(23),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   214
  { NULL,DIJOFS_BUTTON(24),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   215
  { NULL,DIJOFS_BUTTON(25),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   216
  { NULL,DIJOFS_BUTTON(26),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   217
  { NULL,DIJOFS_BUTTON(27),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   218
  { NULL,DIJOFS_BUTTON(28),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   219
  { NULL,DIJOFS_BUTTON(29),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   220
  { NULL,DIJOFS_BUTTON(30),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   221
  { NULL,DIJOFS_BUTTON(31),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   222
  { NULL,DIJOFS_BUTTON(32),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   223
  { NULL,DIJOFS_BUTTON(33),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   224
  { NULL,DIJOFS_BUTTON(34),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   225
  { NULL,DIJOFS_BUTTON(35),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   226
  { NULL,DIJOFS_BUTTON(36),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   227
  { NULL,DIJOFS_BUTTON(37),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   228
  { NULL,DIJOFS_BUTTON(38),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   229
  { NULL,DIJOFS_BUTTON(39),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   230
  { NULL,DIJOFS_BUTTON(40),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   231
  { NULL,DIJOFS_BUTTON(41),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   232
  { NULL,DIJOFS_BUTTON(42),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   233
  { NULL,DIJOFS_BUTTON(43),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   234
  { NULL,DIJOFS_BUTTON(44),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   235
  { NULL,DIJOFS_BUTTON(45),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   236
  { NULL,DIJOFS_BUTTON(46),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   237
  { NULL,DIJOFS_BUTTON(47),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   238
  { NULL,DIJOFS_BUTTON(48),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   239
  { NULL,DIJOFS_BUTTON(49),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   240
  { NULL,DIJOFS_BUTTON(50),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   241
  { NULL,DIJOFS_BUTTON(51),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   242
  { NULL,DIJOFS_BUTTON(52),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   243
  { NULL,DIJOFS_BUTTON(53),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   244
  { NULL,DIJOFS_BUTTON(54),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   245
  { NULL,DIJOFS_BUTTON(55),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   246
  { NULL,DIJOFS_BUTTON(56),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   247
  { NULL,DIJOFS_BUTTON(57),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   248
  { NULL,DIJOFS_BUTTON(58),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   249
  { NULL,DIJOFS_BUTTON(59),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   250
  { NULL,DIJOFS_BUTTON(60),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   251
  { NULL,DIJOFS_BUTTON(61),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   252
  { NULL,DIJOFS_BUTTON(62),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   253
  { NULL,DIJOFS_BUTTON(63),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   254
  { NULL,DIJOFS_BUTTON(64),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   255
  { NULL,DIJOFS_BUTTON(65),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   256
  { NULL,DIJOFS_BUTTON(66),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   257
  { NULL,DIJOFS_BUTTON(67),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   258
  { NULL,DIJOFS_BUTTON(68),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   259
  { NULL,DIJOFS_BUTTON(69),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   260
  { NULL,DIJOFS_BUTTON(70),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   261
  { NULL,DIJOFS_BUTTON(71),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   262
  { NULL,DIJOFS_BUTTON(72),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   263
  { NULL,DIJOFS_BUTTON(73),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   264
  { NULL,DIJOFS_BUTTON(74),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   265
  { NULL,DIJOFS_BUTTON(75),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   266
  { NULL,DIJOFS_BUTTON(76),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   267
  { NULL,DIJOFS_BUTTON(77),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   268
  { NULL,DIJOFS_BUTTON(78),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   269
  { NULL,DIJOFS_BUTTON(79),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   270
  { NULL,DIJOFS_BUTTON(80),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   271
  { NULL,DIJOFS_BUTTON(81),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   272
  { NULL,DIJOFS_BUTTON(82),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   273
  { NULL,DIJOFS_BUTTON(83),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   274
  { NULL,DIJOFS_BUTTON(84),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   275
  { NULL,DIJOFS_BUTTON(85),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   276
  { NULL,DIJOFS_BUTTON(86),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   277
  { NULL,DIJOFS_BUTTON(87),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   278
  { NULL,DIJOFS_BUTTON(88),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   279
  { NULL,DIJOFS_BUTTON(89),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   280
  { NULL,DIJOFS_BUTTON(90),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   281
  { NULL,DIJOFS_BUTTON(91),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   282
  { NULL,DIJOFS_BUTTON(92),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   283
  { NULL,DIJOFS_BUTTON(93),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   284
  { NULL,DIJOFS_BUTTON(94),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   285
  { NULL,DIJOFS_BUTTON(95),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   286
  { NULL,DIJOFS_BUTTON(96),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   287
  { NULL,DIJOFS_BUTTON(97),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   288
  { NULL,DIJOFS_BUTTON(98),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   289
  { NULL,DIJOFS_BUTTON(99),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   290
  { NULL,DIJOFS_BUTTON(100),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   291
  { NULL,DIJOFS_BUTTON(101),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   292
  { NULL,DIJOFS_BUTTON(102),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   293
  { NULL,DIJOFS_BUTTON(103),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   294
  { NULL,DIJOFS_BUTTON(104),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   295
  { NULL,DIJOFS_BUTTON(105),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   296
  { NULL,DIJOFS_BUTTON(106),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   297
  { NULL,DIJOFS_BUTTON(107),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   298
  { NULL,DIJOFS_BUTTON(108),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   299
  { NULL,DIJOFS_BUTTON(109),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   300
  { NULL,DIJOFS_BUTTON(110),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   301
  { NULL,DIJOFS_BUTTON(111),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   302
  { NULL,DIJOFS_BUTTON(112),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   303
  { NULL,DIJOFS_BUTTON(113),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   304
  { NULL,DIJOFS_BUTTON(114),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   305
  { NULL,DIJOFS_BUTTON(115),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   306
  { NULL,DIJOFS_BUTTON(116),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   307
  { NULL,DIJOFS_BUTTON(117),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   308
  { NULL,DIJOFS_BUTTON(118),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   309
  { NULL,DIJOFS_BUTTON(119),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   310
  { NULL,DIJOFS_BUTTON(120),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   311
  { NULL,DIJOFS_BUTTON(121),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   312
  { NULL,DIJOFS_BUTTON(122),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   313
  { NULL,DIJOFS_BUTTON(123),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   314
  { NULL,DIJOFS_BUTTON(124),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   315
  { NULL,DIJOFS_BUTTON(125),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   316
  { NULL,DIJOFS_BUTTON(126),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   317
  { NULL,DIJOFS_BUTTON(127),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   318
  { &GUID_XAxis,FIELD_OFFSET(DIJOYSTATE2,lVX),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   319
  { &GUID_YAxis,FIELD_OFFSET(DIJOYSTATE2,lVY),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   320
  { &GUID_ZAxis,FIELD_OFFSET(DIJOYSTATE2,lVZ),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   321
  { &GUID_RxAxis,FIELD_OFFSET(DIJOYSTATE2,lVRx),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   322
  { &GUID_RyAxis,FIELD_OFFSET(DIJOYSTATE2,lVRy),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   323
  { &GUID_RzAxis,FIELD_OFFSET(DIJOYSTATE2,lVRz),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   324
  { &GUID_Slider,FIELD_OFFSET(DIJOYSTATE2,rglVSlider[0]),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   325
  { &GUID_Slider,FIELD_OFFSET(DIJOYSTATE2,rglVSlider[1]),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   326
  { &GUID_XAxis,FIELD_OFFSET(DIJOYSTATE2,lAX),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   327
  { &GUID_YAxis,FIELD_OFFSET(DIJOYSTATE2,lAY),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   328
  { &GUID_ZAxis,FIELD_OFFSET(DIJOYSTATE2,lAZ),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   329
  { &GUID_RxAxis,FIELD_OFFSET(DIJOYSTATE2,lARx),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   330
  { &GUID_RyAxis,FIELD_OFFSET(DIJOYSTATE2,lARy),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   331
  { &GUID_RzAxis,FIELD_OFFSET(DIJOYSTATE2,lARz),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   332
  { &GUID_Slider,FIELD_OFFSET(DIJOYSTATE2,rglASlider[0]),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   333
  { &GUID_Slider,FIELD_OFFSET(DIJOYSTATE2,rglASlider[1]),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   334
  { &GUID_XAxis,FIELD_OFFSET(DIJOYSTATE2,lFX),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   335
  { &GUID_YAxis,FIELD_OFFSET(DIJOYSTATE2,lFY),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   336
  { &GUID_ZAxis,FIELD_OFFSET(DIJOYSTATE2,lFZ),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   337
  { &GUID_RxAxis,FIELD_OFFSET(DIJOYSTATE2,lFRx),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   338
  { &GUID_RyAxis,FIELD_OFFSET(DIJOYSTATE2,lFRy),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   339
  { &GUID_RzAxis,FIELD_OFFSET(DIJOYSTATE2,lFRz),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   340
  { &GUID_Slider,FIELD_OFFSET(DIJOYSTATE2,rglFSlider[0]),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   341
  { &GUID_Slider,FIELD_OFFSET(DIJOYSTATE2,rglFSlider[1]),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@2760
   342
};
slouken@2760
   343
slouken@2760
   344
const DIDATAFORMAT c_dfDIJoystick2 = {
slouken@2760
   345
    sizeof(DIDATAFORMAT),
slouken@2760
   346
    sizeof(DIOBJECTDATAFORMAT),
slouken@2760
   347
    DIDF_ABSAXIS,
slouken@2760
   348
    sizeof(DIJOYSTATE2),
slouken@2760
   349
    SDL_arraysize(dfDIJoystick2),
slouken@2760
   350
    dfDIJoystick2
slouken@2760
   351
};
slouken@2760
   352
slouken@2198
   353
slouken@1895
   354
/* Convert a DirectInput return code to a text message */
icculus@7037
   355
static int
slouken@2198
   356
SetDIerror(const char *function, HRESULT code)
slouken@1895
   357
{
slouken@2760
   358
    /*
icculus@7037
   359
    return SDL_SetError("%s() [%s]: %s", function,
slouken@3013
   360
                 DXGetErrorString9A(code), DXGetErrorDescription9A(code));
slouken@2760
   361
     */
icculus@7037
   362
    return SDL_SetError("%s() DirectX error %d", function, code);
slouken@1895
   363
}
slouken@1895
   364
slouken@1895
   365
slouken@6690
   366
#define SAFE_RELEASE(p)                             \
slouken@6690
   367
{                                                   \
slouken@6690
   368
	if (p) {                                        \
slouken@6690
   369
	(p)->lpVtbl->Release((p));                  \
slouken@6690
   370
	(p) = 0;                                    \
slouken@6690
   371
	}                                               \
slouken@6690
   372
}
slouken@6690
   373
slouken@6690
   374
slouken@6690
   375
DEFINE_GUID(CLSID_WbemLocator,   0x4590f811,0x1d3a,0x11d0,0x89,0x1F,0x00,0xaa,0x00,0x4b,0x2e,0x24);
slouken@6690
   376
DEFINE_GUID(IID_IWbemLocator,    0xdc12a687,0x737f,0x11cf,0x88,0x4d,0x00,0xaa,0x00,0x4b,0x2e,0x24);
slouken@6690
   377
slouken@6690
   378
//-----------------------------------------------------------------------------
slouken@6690
   379
//
slouken@6690
   380
// code from MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/ee417014(v=vs.85).aspx
slouken@6690
   381
//
slouken@6690
   382
// Enum each PNP device using WMI and check each device ID to see if it contains 
slouken@6690
   383
// "IG_" (ex. "VID_045E&PID_028E&IG_00").  If it does, then it's an XInput device
slouken@6690
   384
// Unfortunately this information can not be found by just using DirectInput 
slouken@6690
   385
//-----------------------------------------------------------------------------
slouken@6690
   386
BOOL IsXInputDevice( const GUID* pGuidProductFromDirectInput )
slouken@6690
   387
{
slouken@6690
   388
	IWbemLocator*           pIWbemLocator  = NULL;
slouken@6690
   389
	IEnumWbemClassObject*   pEnumDevices   = NULL;
slouken@6690
   390
	IWbemClassObject*       pDevices[20];
slouken@6690
   391
	IWbemServices*          pIWbemServices = NULL;
slouken@6690
   392
	DWORD                   uReturned      = 0;
slouken@6690
   393
	BSTR                    bstrNamespace  = NULL;
slouken@6690
   394
	BSTR                    bstrDeviceID   = NULL;
slouken@6690
   395
	BSTR                    bstrClassName  = NULL;
slouken@6690
   396
	SDL_bool                bIsXinputDevice= SDL_FALSE;
slouken@6690
   397
	UINT                    iDevice        = 0;
slouken@6690
   398
	VARIANT                 var;
slouken@6690
   399
	HRESULT                 hr;
slouken@6690
   400
	DWORD bCleanupCOM;
slouken@6690
   401
urkle@6965
   402
    if (!s_bXInputEnabled)
urkle@6965
   403
    {
urkle@6965
   404
        return SDL_FALSE;
urkle@6965
   405
    }
urkle@6965
   406
slouken@6690
   407
	SDL_memset( pDevices, 0x0, sizeof(pDevices) );
slouken@6690
   408
slouken@6690
   409
	// CoInit if needed
slouken@6690
   410
	hr = CoInitialize(NULL);
slouken@6690
   411
	bCleanupCOM = SUCCEEDED(hr);
slouken@6690
   412
slouken@6690
   413
	// Create WMI
slouken@6690
   414
	hr = CoCreateInstance( &CLSID_WbemLocator,
slouken@6690
   415
		NULL,
slouken@6690
   416
		CLSCTX_INPROC_SERVER,
slouken@6690
   417
		&IID_IWbemLocator,
slouken@6690
   418
		(LPVOID*) &pIWbemLocator);
slouken@6690
   419
	if( FAILED(hr) || pIWbemLocator == NULL )
slouken@6690
   420
		goto LCleanup;
slouken@6690
   421
slouken@6690
   422
	bstrNamespace = SysAllocString( L"\\\\.\\root\\cimv2" );if( bstrNamespace == NULL ) goto LCleanup;        
slouken@6690
   423
	bstrClassName = SysAllocString( L"Win32_PNPEntity" );   if( bstrClassName == NULL ) goto LCleanup;        
slouken@6690
   424
	bstrDeviceID  = SysAllocString( L"DeviceID" );          if( bstrDeviceID == NULL )  goto LCleanup;        
slouken@6690
   425
	
slouken@6690
   426
	// Connect to WMI 
slouken@6690
   427
	hr = IWbemLocator_ConnectServer( pIWbemLocator, bstrNamespace, NULL, NULL, 0L, 
slouken@6690
   428
		0L, NULL, NULL, &pIWbemServices );
slouken@6690
   429
	if( FAILED(hr) || pIWbemServices == NULL )
slouken@6690
   430
		goto LCleanup;
slouken@6690
   431
slouken@6690
   432
	// Switch security level to IMPERSONATE. 
slouken@6690
   433
	CoSetProxyBlanket( (IUnknown *)pIWbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, 
slouken@6690
   434
		RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE );                    
slouken@6690
   435
slouken@6690
   436
	hr = IWbemServices_CreateInstanceEnum( pIWbemServices, bstrClassName, 0, NULL, &pEnumDevices ); 
slouken@6690
   437
	if( FAILED(hr) || pEnumDevices == NULL )
slouken@6690
   438
		goto LCleanup;
slouken@6690
   439
slouken@6690
   440
	// Loop over all devices
slouken@6690
   441
	for( ;; )
slouken@6690
   442
	{
slouken@6690
   443
		// Get 20 at a time
slouken@6690
   444
		hr = IEnumWbemClassObject_Next( pEnumDevices, 10000, 20, pDevices, &uReturned );
slouken@6690
   445
		if( FAILED(hr) )
slouken@6690
   446
			goto LCleanup;
slouken@6690
   447
		if( uReturned == 0 )
slouken@6690
   448
			break;
slouken@6690
   449
slouken@6690
   450
		for( iDevice=0; iDevice<uReturned; iDevice++ )
slouken@6690
   451
		{
slouken@6690
   452
			// For each device, get its device ID
slouken@6690
   453
			hr = IWbemClassObject_Get( pDevices[iDevice], bstrDeviceID, 0L, &var, NULL, NULL );
slouken@6690
   454
			if(  SUCCEEDED( hr ) && var.vt == VT_BSTR && var.bstrVal != NULL )
slouken@6690
   455
			{
slouken@6690
   456
				// Check if the device ID contains "IG_".  If it does, then it's an XInput device
slouken@6690
   457
				// This information can not be found from DirectInput 
slouken@6690
   458
				char *pDeviceString = WIN_StringToUTF8( var.bstrVal );
slouken@6690
   459
				if( SDL_strstr( pDeviceString, "IG_" ) )
slouken@6690
   460
				{
slouken@6690
   461
					// If it does, then get the VID/PID from var.bstrVal
slouken@6690
   462
					long dwPid = 0, dwVid = 0;
slouken@6690
   463
					char * strPid = NULL;
slouken@6690
   464
					DWORD dwVidPid = 0;
slouken@6690
   465
					char * strVid = SDL_strstr( pDeviceString, "VID_" );
slouken@6690
   466
					if( strVid )
slouken@6690
   467
					{
slouken@6690
   468
						dwVid = SDL_strtol( strVid + 4, NULL, 16 );
slouken@6690
   469
					}
slouken@6690
   470
					strPid = SDL_strstr( pDeviceString, "PID_" );
slouken@6690
   471
					if( strPid  )
slouken@6690
   472
					{
slouken@6690
   473
						dwPid = SDL_strtol( strPid + 4, NULL, 16 );
slouken@6690
   474
					}
slouken@6690
   475
slouken@6690
   476
					// Compare the VID/PID to the DInput device
slouken@6690
   477
					dwVidPid = MAKELONG( dwVid, dwPid );
slouken@6690
   478
					if( dwVidPid == pGuidProductFromDirectInput->Data1 )
slouken@6690
   479
					{
slouken@6690
   480
						bIsXinputDevice = SDL_TRUE;
slouken@6690
   481
					}
slouken@6690
   482
				}
slouken@6690
   483
				if ( pDeviceString )
slouken@6690
   484
					SDL_free( pDeviceString );
slouken@6690
   485
slouken@6690
   486
				if ( bIsXinputDevice )
slouken@6690
   487
					break;
slouken@6690
   488
			}   
slouken@6690
   489
			SAFE_RELEASE( pDevices[iDevice] );
slouken@6690
   490
		}
slouken@6690
   491
	}
slouken@6690
   492
	
slouken@6690
   493
LCleanup:
slouken@6690
   494
slouken@6690
   495
	for( iDevice=0; iDevice<20; iDevice++ )
slouken@6690
   496
		SAFE_RELEASE( pDevices[iDevice] );
slouken@6690
   497
	SAFE_RELEASE( pEnumDevices );
slouken@6690
   498
	SAFE_RELEASE( pIWbemLocator );
slouken@6690
   499
	SAFE_RELEASE( pIWbemServices );
slouken@6690
   500
slouken@6690
   501
	if ( bstrNamespace )
slouken@6690
   502
		SysFreeString( bstrNamespace );
slouken@6690
   503
	if ( bstrClassName )
slouken@6690
   504
		SysFreeString( bstrClassName );
slouken@6690
   505
	if ( bstrDeviceID )
slouken@6690
   506
		SysFreeString( bstrDeviceID );
slouken@6690
   507
slouken@6690
   508
	if( bCleanupCOM )
slouken@6690
   509
		CoUninitialize();
slouken@6690
   510
		
slouken@6690
   511
	return bIsXinputDevice;
slouken@6690
   512
}
slouken@6690
   513
slouken@6690
   514
slouken@6690
   515
static SDL_bool s_bWindowsDeviceChanged = SDL_FALSE;
slouken@6690
   516
slouken@6690
   517
/* windowproc for our joystick detect thread message only window, to detect any usb device addition/removal
slouken@6690
   518
 */
slouken@6690
   519
LRESULT CALLBACK SDL_PrivateJoystickDetectProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)    {
slouken@6690
   520
	switch (message)    {
slouken@6690
   521
	case WM_DEVICECHANGE:
slouken@6690
   522
		switch (wParam) {
slouken@6690
   523
		case DBT_DEVICEARRIVAL:
slouken@6690
   524
			if (((DEV_BROADCAST_HDR*)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)    {
slouken@6690
   525
				s_bWindowsDeviceChanged = SDL_TRUE;
slouken@6690
   526
			}
slouken@6690
   527
			break;
slouken@6690
   528
		case DBT_DEVICEREMOVECOMPLETE:
slouken@6690
   529
			if (((DEV_BROADCAST_HDR*)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)    {
slouken@6690
   530
				s_bWindowsDeviceChanged = SDL_TRUE;
slouken@6690
   531
			}
slouken@6690
   532
			break;
slouken@6690
   533
		}
slouken@6690
   534
		return 0;
slouken@6690
   535
	}
slouken@6690
   536
slouken@6690
   537
	return DefWindowProc (hwnd, message, wParam, lParam);
slouken@6690
   538
}
slouken@6690
   539
slouken@6690
   540
slouken@6690
   541
DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, \
slouken@6690
   542
	0xC0, 0x4F, 0xB9, 0x51, 0xED);
slouken@6690
   543
slouken@6690
   544
/* Function/thread to scan the system for joysticks.
slouken@6690
   545
 */
slouken@6690
   546
static int
slouken@6690
   547
SDL_JoystickThread(void *_data)
slouken@6690
   548
{
slouken@6690
   549
	HRESULT result = S_OK;	
slouken@6690
   550
	HWND messageWindow = 0;
slouken@6690
   551
	HDEVNOTIFY hNotify = 0;
slouken@6690
   552
	DEV_BROADCAST_DEVICEINTERFACE dbh;
slouken@6710
   553
	SDL_bool bOpenedXInputDevices[4];
slouken@6712
   554
	WNDCLASSEX wincl;
slouken@6710
   555
slouken@6710
   556
	SDL_memset( bOpenedXInputDevices, 0x0, sizeof(bOpenedXInputDevices) );
slouken@6690
   557
slouken@6690
   558
	result = WIN_CoInitialize();
slouken@6690
   559
slouken@6712
   560
	SDL_memset( &wincl, 0x0, sizeof(wincl) );
slouken@6712
   561
	wincl.hInstance = GetModuleHandle( NULL );
slouken@6712
   562
	wincl.lpszClassName = L"Message";
slouken@6712
   563
	wincl.lpfnWndProc = SDL_PrivateJoystickDetectProc;      // This function is called by windows
slouken@6712
   564
	wincl.cbSize = sizeof (WNDCLASSEX);
slouken@6712
   565
slouken@6712
   566
	if (!RegisterClassEx (&wincl))
slouken@6712
   567
	{		
icculus@7037
   568
		return SDL_SetError("Failed to create register class for joystick autodetect.", GetLastError());
slouken@6712
   569
	}
slouken@6712
   570
slouken@6712
   571
	messageWindow = (HWND)CreateWindowEx( 0,  L"Message", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL );
slouken@6690
   572
	if ( !messageWindow )
slouken@6690
   573
	{
icculus@7037
   574
		return SDL_SetError("Failed to create message window for joystick autodetect.", GetLastError());
slouken@6690
   575
	}
slouken@6690
   576
slouken@6690
   577
	SDL_memset(&dbh, 0x0, sizeof(dbh));
slouken@6690
   578
slouken@6690
   579
	dbh.dbcc_size = sizeof(dbh);
slouken@6690
   580
	dbh.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
slouken@6690
   581
	dbh.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;
slouken@6690
   582
slouken@6690
   583
	hNotify = RegisterDeviceNotification( messageWindow, &dbh, DEVICE_NOTIFY_WINDOW_HANDLE );
slouken@6690
   584
	if ( !hNotify )
slouken@6690
   585
	{
icculus@7037
   586
		return SDL_SetError("Failed to create notify device for joystick autodetect.", GetLastError());
slouken@6690
   587
	}
slouken@6690
   588
slouken@6690
   589
	SDL_LockMutex( s_mutexJoyStickEnum );
slouken@6690
   590
	while ( s_bJoystickThreadQuit == SDL_FALSE )
slouken@6690
   591
	{
slouken@6690
   592
		MSG messages;
slouken@6710
   593
		Uint8 userId;
slouken@6710
   594
		int nCurrentOpenedXInputDevices = 0;
slouken@6710
   595
		int nNewOpenedXInputDevices = 0;
slouken@6690
   596
		SDL_CondWaitTimeout( s_condJoystickThread, s_mutexJoyStickEnum, 300 );
slouken@6690
   597
slouken@6690
   598
		while ( s_bJoystickThreadQuit == SDL_FALSE && PeekMessage(&messages, messageWindow, 0, 0, PM_NOREMOVE) )
slouken@6690
   599
		{
slouken@6690
   600
			if ( GetMessage(&messages, messageWindow, 0, 0) != 0 )  {
slouken@6690
   601
				TranslateMessage(&messages);
slouken@6690
   602
				DispatchMessage(&messages);
slouken@6690
   603
			}
slouken@6690
   604
		}
slouken@6690
   605
urkle@6965
   606
		if ( s_bXInputEnabled && XINPUTGETCAPABILITIES )
slouken@6710
   607
		{
slouken@6782
   608
			// scan for any change in XInput devices
slouken@6782
   609
			for ( userId = 0; userId < 4; userId++ )
slouken@6782
   610
			{
slouken@6782
   611
				XINPUT_CAPABILITIES	capabilities;
slouken@6782
   612
				DWORD result;
slouken@6710
   613
slouken@6782
   614
				if ( bOpenedXInputDevices[userId] == SDL_TRUE )
slouken@6782
   615
					nCurrentOpenedXInputDevices++;
slouken@6710
   616
slouken@6782
   617
				result = XINPUTGETCAPABILITIES( userId, XINPUT_FLAG_GAMEPAD, &capabilities );
slouken@6782
   618
				if ( result == ERROR_SUCCESS )
slouken@6782
   619
				{
slouken@6782
   620
					bOpenedXInputDevices[userId] = SDL_TRUE;
slouken@6782
   621
					nNewOpenedXInputDevices++;
slouken@6782
   622
				}
slouken@6782
   623
				else
slouken@6782
   624
				{
slouken@6782
   625
					bOpenedXInputDevices[userId] = SDL_FALSE;
slouken@6782
   626
				}
slouken@6710
   627
			}
slouken@6710
   628
		}
slouken@6710
   629
slouken@6710
   630
		if ( s_pKnownJoystickGUIDs && ( s_bWindowsDeviceChanged || nNewOpenedXInputDevices != nCurrentOpenedXInputDevices ) )
slouken@6690
   631
		{
slouken@6690
   632
			SDL_Delay( 300 ); // wait for direct input to find out about this device
slouken@6690
   633
slouken@6690
   634
			s_bDeviceRemoved = SDL_TRUE;
slouken@6690
   635
			s_bDeviceAdded = SDL_TRUE;
slouken@6690
   636
			s_bWindowsDeviceChanged = SDL_FALSE;
slouken@6690
   637
		}
slouken@6690
   638
	}
slouken@6690
   639
	SDL_UnlockMutex( s_mutexJoyStickEnum );
slouken@6690
   640
slouken@6690
   641
	if ( hNotify )
slouken@6690
   642
		UnregisterDeviceNotification( hNotify );
slouken@6690
   643
slouken@6690
   644
	if ( messageWindow )
slouken@6690
   645
		DestroyWindow( messageWindow );
slouken@6712
   646
slouken@6712
   647
	UnregisterClass( wincl.lpszClassName, wincl.hInstance );
slouken@6690
   648
	messageWindow = 0;
slouken@6690
   649
	WIN_CoUninitialize();
slouken@6690
   650
	return 1;
slouken@6690
   651
}
slouken@6690
   652
slouken@6690
   653
slouken@1895
   654
/* Function to scan the system for joysticks.
slouken@1895
   655
 * This function should set SDL_numjoysticks to the number of available
slouken@1895
   656
 * joysticks.  Joystick 0 should be the system default joystick.
slouken@1895
   657
 * It should return 0, or -1 on an unrecoverable fatal error.
slouken@1895
   658
 */
slouken@1895
   659
int
slouken@1895
   660
SDL_SYS_JoystickInit(void)
slouken@1895
   661
{
slouken@1895
   662
    HRESULT result;
slouken@2713
   663
    HINSTANCE instance;
icculus@6990
   664
	const char *env = SDL_GetHint(SDL_HINT_XINPUT_ENABLED);
urkle@6965
   665
	if (env && !SDL_atoi(env)) {
urkle@6965
   666
		s_bXInputEnabled = SDL_FALSE;
urkle@6965
   667
	}
slouken@1895
   668
icculus@5591
   669
    result = WIN_CoInitialize();
slouken@2198
   670
    if (FAILED(result)) {
icculus@7037
   671
        return SetDIerror("CoInitialize", result);
slouken@1895
   672
    }
slouken@1895
   673
icculus@5591
   674
    coinitialized = SDL_TRUE;
icculus@5591
   675
slouken@6690
   676
    result = CoCreateInstance(&CLSID_DirectInput8, NULL, CLSCTX_INPROC_SERVER,
slouken@6690
   677
                              &IID_IDirectInput8, (LPVOID)&dinput);
slouken@2198
   678
slouken@2198
   679
    if (FAILED(result)) {
icculus@5591
   680
        SDL_SYS_JoystickQuit();
icculus@7037
   681
        return SetDIerror("CoCreateInstance", result);
slouken@2198
   682
    }
slouken@2198
   683
slouken@2198
   684
    /* Because we used CoCreateInstance, we need to Initialize it, first. */
slouken@2713
   685
    instance = GetModuleHandle(NULL);
slouken@2713
   686
    if (instance == NULL) {
icculus@5591
   687
        SDL_SYS_JoystickQuit();
icculus@7037
   688
        return SDL_SetError("GetModuleHandle() failed with error code %d.", GetLastError());
slouken@2713
   689
    }
slouken@6690
   690
    result = IDirectInput8_Initialize(dinput, instance, DIRECTINPUT_VERSION);
slouken@2198
   691
slouken@2198
   692
    if (FAILED(result)) {
icculus@5591
   693
        SDL_SYS_JoystickQuit();
icculus@7037
   694
        return SetDIerror("IDirectInput::Initialize", result);
slouken@2198
   695
    }
slouken@2198
   696
icculus@6990
   697
    s_mutexJoyStickEnum = SDL_CreateMutex();
icculus@6990
   698
    s_condJoystickThread = SDL_CreateCond();
icculus@6990
   699
    s_bDeviceAdded = SDL_TRUE; // force a scan of the system for joysticks this first time
icculus@6990
   700
    SDL_SYS_JoystickDetect();
slouken@1895
   701
icculus@6990
   702
    if ((s_bXInputEnabled) && (WIN_LoadXInputDLL() == -1)) {
icculus@6990
   703
        s_bXInputEnabled = SDL_FALSE;  /* oh well. */
urkle@6965
   704
    }
slouken@1895
   705
slouken@6690
   706
	if ( !s_threadJoystick )
slouken@6690
   707
	{
slouken@6690
   708
		s_bJoystickThreadQuit = SDL_FALSE;
slouken@6690
   709
		/* spin up the thread to detect hotplug of devices */
slouken@6690
   710
#if defined(__WIN32__) && !defined(HAVE_LIBC)
slouken@6690
   711
#undef SDL_CreateThread
slouken@6690
   712
		s_threadJoystick= SDL_CreateThread( SDL_JoystickThread, "SDL_joystick", NULL, NULL, NULL );
slouken@6690
   713
#else
slouken@6690
   714
		s_threadJoystick = SDL_CreateThread( SDL_JoystickThread, "SDL_joystick", NULL );
slouken@6690
   715
#endif
slouken@6690
   716
	}
slouken@6690
   717
	    return SDL_SYS_NumJoysticks();
slouken@2198
   718
}
slouken@2198
   719
slouken@6707
   720
/* return the number of joysticks that are connected right now */
slouken@6707
   721
int SDL_SYS_NumJoysticks()
slouken@6707
   722
{
slouken@6707
   723
	int nJoysticks = 0;
slouken@6707
   724
	JoyStick_DeviceData *device = SYS_Joystick;
slouken@6707
   725
	while ( device )
slouken@6707
   726
	{
slouken@6707
   727
		nJoysticks++;
slouken@6707
   728
		device = device->pNext;
slouken@6707
   729
	}
slouken@6707
   730
slouken@6707
   731
	return nJoysticks;
slouken@6707
   732
}
slouken@6707
   733
slouken@6707
   734
static int s_iNewGUID = 0;
slouken@6707
   735
slouken@6707
   736
/* helper function for direct input, gets called for each connected joystick */
slouken@6707
   737
static BOOL CALLBACK
slouken@6707
   738
	EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
slouken@6707
   739
{
slouken@6707
   740
	JoyStick_DeviceData *pNewJoystick;
slouken@6712
   741
	JoyStick_DeviceData *pPrevJoystick = NULL;
slouken@6707
   742
	SDL_bool bXInputDevice;
slouken@6707
   743
	pNewJoystick = *(JoyStick_DeviceData **)pContext;
slouken@6707
   744
	while ( pNewJoystick )
slouken@6707
   745
	{
slouken@6707
   746
		if ( !SDL_memcmp( &pNewJoystick->dxdevice.guidInstance, &pdidInstance->guidInstance, sizeof(pNewJoystick->dxdevice.guidInstance) ) )
slouken@6707
   747
		{
slouken@6707
   748
			/* if we are replacing the front of the list then update it */
slouken@6707
   749
			if ( pNewJoystick == *(JoyStick_DeviceData **)pContext ) 
slouken@6707
   750
			{
slouken@6707
   751
				*(JoyStick_DeviceData **)pContext = pNewJoystick->pNext;
slouken@6707
   752
			}
slouken@6712
   753
			else if ( pPrevJoystick )
slouken@6712
   754
			{
slouken@6712
   755
				pPrevJoystick->pNext = pNewJoystick->pNext;
slouken@6712
   756
			}
slouken@6712
   757
slouken@6712
   758
			pNewJoystick->pNext = SYS_Joystick;
slouken@6712
   759
			SYS_Joystick = pNewJoystick;
slouken@6707
   760
slouken@6707
   761
			s_pKnownJoystickGUIDs[ s_iNewGUID ] = pdidInstance->guidInstance;
slouken@6707
   762
			s_iNewGUID++;
slouken@6707
   763
			if ( s_iNewGUID < MAX_JOYSTICKS )
slouken@6707
   764
				return DIENUM_CONTINUE; // already have this joystick loaded, just keep going
slouken@6707
   765
			else
slouken@6707
   766
				return DIENUM_STOP; 
slouken@6707
   767
		}
slouken@6707
   768
slouken@6712
   769
		pPrevJoystick = pNewJoystick;
slouken@6707
   770
		pNewJoystick = pNewJoystick->pNext;
slouken@6707
   771
	}
slouken@6707
   772
slouken@6707
   773
	s_bDeviceAdded = SDL_TRUE;
slouken@6707
   774
slouken@6707
   775
	bXInputDevice = IsXInputDevice( &pdidInstance->guidProduct );
slouken@6707
   776
slouken@6707
   777
	pNewJoystick = (JoyStick_DeviceData *)SDL_malloc( sizeof(JoyStick_DeviceData) );
slouken@6707
   778
slouken@6707
   779
	if ( bXInputDevice )
slouken@6707
   780
	{
slouken@6707
   781
		pNewJoystick->bXInputDevice = SDL_TRUE;
slouken@6707
   782
		pNewJoystick->XInputUserId = INVALID_XINPUT_USERID;
slouken@6707
   783
	}
slouken@6707
   784
	else
slouken@6707
   785
	{
slouken@6707
   786
		pNewJoystick->bXInputDevice = SDL_FALSE;
slouken@6707
   787
	}
slouken@6712
   788
	
slouken@6712
   789
	SDL_memcpy(&(pNewJoystick->dxdevice), pdidInstance,
slouken@6712
   790
		sizeof(DIDEVICEINSTANCE));
slouken@6712
   791
slouken@6707
   792
	pNewJoystick->joystickname = WIN_StringToUTF8(pdidInstance->tszProductName);
slouken@6707
   793
	pNewJoystick->send_add_event = 1;
slouken@6707
   794
	pNewJoystick->nInstanceID = ++s_nInstanceID;
slouken@6707
   795
	SDL_memcpy( &pNewJoystick->guid, &pdidInstance->guidProduct, sizeof(pNewJoystick->guid) );
slouken@6707
   796
	pNewJoystick->pNext = NULL;
slouken@6707
   797
slouken@6707
   798
	if ( SYS_Joystick )
slouken@6707
   799
	{
slouken@6707
   800
		pNewJoystick->pNext = SYS_Joystick;
slouken@6707
   801
	}
slouken@6707
   802
	SYS_Joystick = pNewJoystick;
slouken@6707
   803
slouken@6707
   804
	s_pKnownJoystickGUIDs[ s_iNewGUID ] = pdidInstance->guidInstance;
slouken@6707
   805
	s_iNewGUID++;
slouken@6707
   806
slouken@6707
   807
	if ( s_iNewGUID < MAX_JOYSTICKS )
slouken@6707
   808
		return DIENUM_CONTINUE; // already have this joystick loaded, just keep going
slouken@6707
   809
	else
slouken@6707
   810
		return DIENUM_STOP; 
slouken@6707
   811
}
slouken@6707
   812
slouken@6707
   813
/* detect any new joysticks being inserted into the system */
slouken@6707
   814
void SDL_SYS_JoystickDetect()
slouken@6707
   815
{
slouken@6707
   816
	HRESULT result;
slouken@6707
   817
	JoyStick_DeviceData *pCurList = NULL;
slouken@6707
   818
	/* only enum the devices if the joystick thread told us something changed */
slouken@6707
   819
	if ( s_bDeviceAdded || s_bDeviceRemoved )
slouken@6707
   820
	{
slouken@6707
   821
		s_bDeviceAdded = SDL_FALSE;
slouken@6707
   822
		s_bDeviceRemoved = SDL_FALSE;
slouken@6707
   823
slouken@6707
   824
		pCurList = SYS_Joystick;
slouken@6707
   825
		SYS_Joystick = NULL;
slouken@6707
   826
		s_iNewGUID = 0;
slouken@6977
   827
		SDL_LockMutex( s_mutexJoyStickEnum );
slouken@6707
   828
slouken@6707
   829
		if ( !s_pKnownJoystickGUIDs )
slouken@6707
   830
			s_pKnownJoystickGUIDs = SDL_malloc( sizeof(GUID)*MAX_JOYSTICKS );
slouken@6707
   831
				
slouken@6707
   832
		SDL_memset( s_pKnownJoystickGUIDs, 0x0, sizeof(GUID)*MAX_JOYSTICKS );
slouken@6707
   833
slouken@6707
   834
		/* Look for joysticks, wheels, head trackers, gamepads, etc.. */
slouken@6707
   835
		result = IDirectInput8_EnumDevices(dinput,
slouken@6707
   836
			DI8DEVCLASS_GAMECTRL,
slouken@6707
   837
			EnumJoysticksCallback,
slouken@6707
   838
			&pCurList, DIEDFL_ATTACHEDONLY);
slouken@6707
   839
slouken@6977
   840
		SDL_UnlockMutex( s_mutexJoyStickEnum );
slouken@6707
   841
	}
slouken@6707
   842
slouken@6707
   843
	if ( pCurList )
slouken@6707
   844
	{
slouken@6707
   845
		while ( pCurList )
slouken@6707
   846
		{
slouken@6707
   847
			JoyStick_DeviceData *pListNext = NULL;
slouken@6707
   848
#if !SDL_EVENTS_DISABLED
slouken@6707
   849
			SDL_Event event;
slouken@6707
   850
			event.type = SDL_JOYDEVICEREMOVED;
slouken@6707
   851
slouken@6707
   852
			if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@6707
   853
				event.jdevice.which = pCurList->nInstanceID;
slouken@6707
   854
				if ((SDL_EventOK == NULL)
slouken@6707
   855
					|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@6707
   856
						SDL_PushEvent(&event);
slouken@6707
   857
				}
slouken@6707
   858
			}
slouken@6707
   859
#endif // !SDL_EVENTS_DISABLED 
slouken@6707
   860
slouken@6707
   861
			pListNext = pCurList->pNext;
slouken@6707
   862
			SDL_free(pCurList->joystickname);
slouken@6707
   863
			SDL_free( pCurList );
slouken@6707
   864
			pCurList = pListNext;
slouken@6707
   865
		}
slouken@6707
   866
slouken@6707
   867
	}
slouken@6707
   868
slouken@6707
   869
	if ( s_bDeviceAdded )
slouken@6707
   870
	{
slouken@6707
   871
		JoyStick_DeviceData *pNewJoystick;
slouken@6707
   872
		int device_index = 0;
slouken@6707
   873
		s_bDeviceAdded = SDL_FALSE;
slouken@6707
   874
		pNewJoystick = SYS_Joystick;
slouken@6707
   875
		while ( pNewJoystick )
slouken@6707
   876
		{
slouken@6707
   877
			if ( pNewJoystick->send_add_event )
slouken@6707
   878
			{
slouken@6707
   879
#if !SDL_EVENTS_DISABLED
slouken@6707
   880
				SDL_Event event;
slouken@6707
   881
				event.type = SDL_JOYDEVICEADDED;
slouken@6707
   882
slouken@6707
   883
				if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@6707
   884
					event.jdevice.which = device_index;
slouken@6707
   885
					if ((SDL_EventOK == NULL)
slouken@6707
   886
						|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@6707
   887
							SDL_PushEvent(&event);
slouken@6707
   888
					}
slouken@6707
   889
				}
slouken@6707
   890
#endif /* !SDL_EVENTS_DISABLED */
slouken@6707
   891
				pNewJoystick->send_add_event = 0;
slouken@6707
   892
			}
slouken@6707
   893
			device_index++;
slouken@6707
   894
			pNewJoystick = pNewJoystick->pNext;
slouken@6707
   895
		}
slouken@6707
   896
	}
slouken@6707
   897
}
slouken@6707
   898
slouken@6707
   899
/* we need to poll if we have pending hotplug device changes or connected devices */
slouken@6707
   900
SDL_bool SDL_SYS_JoystickNeedsPolling()
slouken@6707
   901
{
slouken@6707
   902
	/* we have a new device or one was pulled, we need to think this frame please */
slouken@6707
   903
	if ( s_bDeviceAdded || s_bDeviceRemoved )
slouken@6707
   904
		return SDL_TRUE;
slouken@6707
   905
slouken@6707
   906
	return SDL_FALSE;
slouken@6707
   907
}
slouken@6707
   908
slouken@1895
   909
/* Function to get the device-dependent name of a joystick */
slouken@1895
   910
const char *
slouken@6707
   911
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
slouken@1895
   912
{
slouken@6690
   913
	JoyStick_DeviceData *device = SYS_Joystick;
slouken@6690
   914
slouken@6707
   915
	for (; device_index > 0; device_index--)
slouken@6690
   916
		device = device->pNext;
slouken@6690
   917
slouken@6690
   918
	return device->joystickname;
slouken@1895
   919
}
slouken@1895
   920
slouken@6707
   921
/* Function to perform the mapping between current device instance and this joysticks instance id */
slouken@6707
   922
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
slouken@6707
   923
{
slouken@6707
   924
	JoyStick_DeviceData *device = SYS_Joystick;
slouken@6707
   925
	int index;
slouken@6707
   926
slouken@6707
   927
	for (index = device_index; index > 0; index--)
slouken@6707
   928
		device = device->pNext;
slouken@6707
   929
slouken@6707
   930
	return device->nInstanceID;
slouken@6707
   931
}
slouken@6707
   932
slouken@1895
   933
/* Function to open a joystick for use.
slouken@1895
   934
   The joystick to open is specified by the index field of the joystick.
slouken@1895
   935
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@1895
   936
   It returns 0, or -1 if there is an error.
slouken@1895
   937
 */
slouken@1895
   938
int
slouken@6690
   939
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
slouken@1895
   940
{
slouken@1895
   941
    HRESULT result;
icculus@6716
   942
    LPDIRECTINPUTDEVICE8 device;
slouken@2198
   943
    DIPROPDWORD dipdw;
slouken@6690
   944
	JoyStick_DeviceData *joystickdevice = SYS_Joystick;
slouken@6690
   945
slouken@6690
   946
	for (; device_index > 0; device_index--)
slouken@6690
   947
		joystickdevice = joystickdevice->pNext;
slouken@2198
   948
slouken@2713
   949
    SDL_memset(&dipdw, 0, sizeof(DIPROPDWORD));
slouken@2198
   950
    dipdw.diph.dwSize = sizeof(DIPROPDWORD);
slouken@2198
   951
    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
slouken@2198
   952
slouken@1895
   953
    /* allocate memory for system specific hardware data */
slouken@6698
   954
	joystick->instance_id = joystickdevice->nInstanceID;
urkle@6965
   955
    joystick->closed = 0;
slouken@1895
   956
    joystick->hwdata =
slouken@2713
   957
        (struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata));
slouken@1895
   958
    if (joystick->hwdata == NULL) {
icculus@7037
   959
        return SDL_OutOfMemory();
slouken@1895
   960
    }
slouken@2713
   961
    SDL_memset(joystick->hwdata, 0, sizeof(struct joystick_hwdata));
slouken@1895
   962
    joystick->hwdata->buffered = 1;
slouken@6690
   963
	joystick->hwdata->removed = 0;
slouken@2198
   964
    joystick->hwdata->Capabilities.dwSize = sizeof(DIDEVCAPS);
slouken@6690
   965
	joystick->hwdata->guid = joystickdevice->guid;
slouken@1895
   966
slouken@6690
   967
	if ( joystickdevice->bXInputDevice )
slouken@6690
   968
	{
slouken@6690
   969
		XINPUT_CAPABILITIES	capabilities;
slouken@6690
   970
		Uint8 userId = 0;
slouken@6690
   971
		DWORD result;
slouken@6690
   972
		JoyStick_DeviceData *joysticklist = SYS_Joystick;
slouken@6690
   973
		// scan the opened joysticks and pick the next free xinput userid for this one
slouken@6690
   974
		for( ; joysticklist; joysticklist = joysticklist->pNext)
slouken@6690
   975
		{
slouken@6690
   976
			if ( joysticklist->bXInputDevice && joysticklist->XInputUserId == userId )
slouken@6690
   977
				userId++;
slouken@6690
   978
		}
slouken@1895
   979
urkle@6965
   980
		if ( s_bXInputEnabled && XINPUTGETCAPABILITIES )
slouken@6690
   981
		{
slouken@6690
   982
			result = XINPUTGETCAPABILITIES( userId, XINPUT_FLAG_GAMEPAD, &capabilities );
slouken@6690
   983
			if ( result == ERROR_SUCCESS )
slouken@6690
   984
			{
icculus@6990
   985
                const SDL_bool bIs14OrLater = (SDL_XInputVersion >= ((1<<16)|4));
slouken@6690
   986
				SDL_bool bIsSupported = SDL_FALSE;
slouken@6690
   987
				// Current version of XInput mistakenly returns 0 as the Type. Ignore it and ensure the subtype is a gamepad.
slouken@6690
   988
				bIsSupported = ( capabilities.SubType == XINPUT_DEVSUBTYPE_GAMEPAD );
slouken@2198
   989
slouken@6690
   990
				if ( !bIsSupported )
slouken@6690
   991
				{
slouken@6690
   992
					joystickdevice->bXInputDevice = SDL_FALSE;
slouken@6690
   993
				}
slouken@6690
   994
				else
slouken@6690
   995
				{
slouken@6690
   996
					// valid
slouken@6690
   997
					joystick->hwdata->bXInputDevice = SDL_TRUE;
icculus@6990
   998
                    if ((!bIs14OrLater) || (capabilities.Flags & XINPUT_CAPS_FFB_SUPPORTED)) {
icculus@6990
   999
					    joystick->hwdata->bXInputHaptic = SDL_TRUE;
icculus@6990
  1000
                    }
slouken@6690
  1001
					SDL_memset( joystick->hwdata->XInputState, 0x0, sizeof(joystick->hwdata->XInputState) );
slouken@6690
  1002
					joystickdevice->XInputUserId = userId;
slouken@6690
  1003
					joystick->hwdata->userid = userId;
slouken@6690
  1004
					joystick->hwdata->currentXInputSlot = 0;
slouken@6690
  1005
					// The XInput API has a hard coded button/axis mapping, so we just match it
slouken@6690
  1006
					joystick->naxes = 6;
slouken@6690
  1007
					joystick->nbuttons = 15;
slouken@6690
  1008
					joystick->nballs = 0;
slouken@6690
  1009
					joystick->nhats = 0;
slouken@6690
  1010
				}
slouken@6690
  1011
			}
slouken@6690
  1012
			else
slouken@6690
  1013
			{
slouken@6690
  1014
				joystickdevice->bXInputDevice = SDL_FALSE;
slouken@6690
  1015
			}
slouken@6690
  1016
		}
slouken@6690
  1017
		else
slouken@6690
  1018
		{
slouken@6690
  1019
			joystickdevice->bXInputDevice = SDL_FALSE;
slouken@6690
  1020
		}
slouken@6690
  1021
	}
slouken@1895
  1022
slouken@6690
  1023
	if ( joystickdevice->bXInputDevice == SDL_FALSE )
slouken@6690
  1024
	{
slouken@6690
  1025
		joystick->hwdata->bXInputDevice = SDL_FALSE;
slouken@1895
  1026
slouken@6690
  1027
		result =
slouken@6690
  1028
			IDirectInput8_CreateDevice(dinput,
slouken@6690
  1029
									  &(joystickdevice->dxdevice.guidInstance), &device, NULL);
slouken@6690
  1030
		if (FAILED(result)) {
icculus@7037
  1031
			return SetDIerror("IDirectInput::CreateDevice", result);
slouken@6690
  1032
		}
slouken@1895
  1033
icculus@6716
  1034
		/* Now get the IDirectInputDevice8 interface, instead. */
slouken@6690
  1035
		result = IDirectInputDevice8_QueryInterface(device,
slouken@6690
  1036
												   &IID_IDirectInputDevice8,
slouken@6690
  1037
												   (LPVOID *) & joystick->
slouken@6690
  1038
												   hwdata->InputDevice);
slouken@6690
  1039
		/* We are done with this object.  Use the stored one from now on. */
slouken@6690
  1040
		IDirectInputDevice8_Release(device);
slouken@1895
  1041
slouken@6690
  1042
		if (FAILED(result)) {
icculus@7037
  1043
			return SetDIerror("IDirectInputDevice8::QueryInterface", result);
slouken@6690
  1044
		}
slouken@2198
  1045
slouken@6690
  1046
		/* Aquire shared access. Exclusive access is required for forces,
slouken@6690
  1047
		 * though. */
slouken@6690
  1048
		result =
slouken@6690
  1049
			IDirectInputDevice8_SetCooperativeLevel(joystick->hwdata->
slouken@6690
  1050
													InputDevice, SDL_HelperWindow,
slouken@6690
  1051
													DISCL_NONEXCLUSIVE |
slouken@6690
  1052
													DISCL_BACKGROUND);
slouken@6690
  1053
		if (FAILED(result)) {
icculus@7037
  1054
			return SetDIerror("IDirectInputDevice8::SetCooperativeLevel", result);
slouken@6690
  1055
		}
slouken@2198
  1056
slouken@6690
  1057
		/* Use the extended data structure: DIJOYSTATE2. */
slouken@6690
  1058
		result =
slouken@6690
  1059
			IDirectInputDevice8_SetDataFormat(joystick->hwdata->InputDevice,
slouken@6690
  1060
											  &c_dfDIJoystick2);
slouken@6690
  1061
		if (FAILED(result)) {
icculus@7037
  1062
			return SetDIerror("IDirectInputDevice8::SetDataFormat", result);
slouken@6690
  1063
		}
slouken@2198
  1064
slouken@6690
  1065
		/* Get device capabilities */
slouken@6690
  1066
		result =
slouken@6690
  1067
			IDirectInputDevice8_GetCapabilities(joystick->hwdata->InputDevice,
slouken@6690
  1068
												&joystick->hwdata->Capabilities);
slouken@2198
  1069
slouken@6690
  1070
		if (FAILED(result)) {
icculus@7037
  1071
			return SetDIerror("IDirectInputDevice8::GetCapabilities", result);
slouken@6690
  1072
		}
slouken@2198
  1073
slouken@6690
  1074
		/* Force capable? */
slouken@6690
  1075
		if (joystick->hwdata->Capabilities.dwFlags & DIDC_FORCEFEEDBACK) {
slouken@2198
  1076
slouken@6690
  1077
			result = IDirectInputDevice8_Acquire(joystick->hwdata->InputDevice);
slouken@2198
  1078
slouken@6690
  1079
			if (FAILED(result)) {
icculus@7037
  1080
				return SetDIerror("IDirectInputDevice8::Acquire", result);
slouken@6690
  1081
			}
slouken@2198
  1082
slouken@6690
  1083
			/* reset all accuators. */
slouken@6690
  1084
			result =
slouken@6690
  1085
				IDirectInputDevice8_SendForceFeedbackCommand(joystick->hwdata->
slouken@6690
  1086
															 InputDevice,
slouken@6690
  1087
															 DISFFC_RESET);
slouken@2198
  1088
slouken@6690
  1089
			/* Not necessarily supported, ignore if not supported.
slouken@6690
  1090
			if (FAILED(result)) {
icculus@7037
  1091
				return SetDIerror("IDirectInputDevice8::SendForceFeedbackCommand", result);
slouken@6690
  1092
			}
slouken@6690
  1093
			*/
slouken@1895
  1094
slouken@6690
  1095
			result = IDirectInputDevice8_Unacquire(joystick->hwdata->InputDevice);
slouken@1895
  1096
slouken@6690
  1097
			if (FAILED(result)) {
icculus@7037
  1098
				return SetDIerror("IDirectInputDevice8::Unacquire", result);
slouken@6690
  1099
			}
slouken@2198
  1100
slouken@6690
  1101
			/* Turn on auto-centering for a ForceFeedback device (until told
slouken@6690
  1102
			 * otherwise). */
slouken@6690
  1103
			dipdw.diph.dwObj = 0;
slouken@6690
  1104
			dipdw.diph.dwHow = DIPH_DEVICE;
slouken@6690
  1105
			dipdw.dwData = DIPROPAUTOCENTER_ON;
slouken@6220
  1106
slouken@6690
  1107
			result =
slouken@6690
  1108
				IDirectInputDevice8_SetProperty(joystick->hwdata->InputDevice,
slouken@6690
  1109
												DIPROP_AUTOCENTER, &dipdw.diph);
slouken@2198
  1110
slouken@6690
  1111
			/* Not necessarily supported, ignore if not supported.
slouken@6690
  1112
			if (FAILED(result)) {
icculus@7037
  1113
				return SetDIerror("IDirectInputDevice8::SetProperty", result);
slouken@6690
  1114
			}
slouken@6690
  1115
			*/
slouken@6690
  1116
		}
slouken@2198
  1117
slouken@6690
  1118
		/* What buttons and axes does it have? */
slouken@6690
  1119
		IDirectInputDevice8_EnumObjects(joystick->hwdata->InputDevice,
slouken@6690
  1120
										EnumDevObjectsCallback, joystick,
slouken@6690
  1121
										DIDFT_BUTTON | DIDFT_AXIS | DIDFT_POV);
slouken@2198
  1122
slouken@6690
  1123
		/* Reorder the input objects. Some devices do not report the X axis as
slouken@6690
  1124
		 * the first axis, for example. */
slouken@6690
  1125
		SortDevObjects(joystick);
slouken@6690
  1126
slouken@6690
  1127
		dipdw.diph.dwObj = 0;
slouken@6690
  1128
		dipdw.diph.dwHow = DIPH_DEVICE;
slouken@6690
  1129
		dipdw.dwData = INPUT_QSIZE;
slouken@6690
  1130
slouken@6690
  1131
		/* Set the buffer size */
slouken@6690
  1132
		result =
slouken@6690
  1133
			IDirectInputDevice8_SetProperty(joystick->hwdata->InputDevice,
slouken@6690
  1134
											DIPROP_BUFFERSIZE, &dipdw.diph);
slouken@6690
  1135
slouken@6690
  1136
		if (result == DI_POLLEDDEVICE) {
slouken@6690
  1137
			/* This device doesn't support buffering, so we're forced
slouken@6690
  1138
			 * to use less reliable polling. */
slouken@6690
  1139
			joystick->hwdata->buffered = 0;
slouken@6690
  1140
		} else if (FAILED(result)) {
icculus@7037
  1141
			return SetDIerror("IDirectInputDevice8::SetProperty", result);
slouken@6690
  1142
		}
slouken@6690
  1143
	}
slouken@1895
  1144
    return (0);
slouken@1895
  1145
}
slouken@1895
  1146
slouken@6707
  1147
/* return true if this joystick is plugged in right now */
slouken@6707
  1148
SDL_bool SDL_SYS_JoystickAttached( SDL_Joystick * joystick )
slouken@6707
  1149
{
slouken@6707
  1150
	return joystick->closed == 0 && joystick->hwdata->removed == 0;
slouken@6707
  1151
}
slouken@6707
  1152
slouken@6707
  1153
slouken@6220
  1154
/* Sort using the data offset into the DInput struct.
slouken@6220
  1155
 * This gives a reasonable ordering for the inputs. */
slouken@6220
  1156
static int
slouken@6220
  1157
SortDevFunc(const void *a, const void *b)
slouken@6220
  1158
{
slouken@6220
  1159
	const input_t *inputA = (const input_t*)a;
slouken@6220
  1160
	const input_t *inputB = (const input_t*)b;
slouken@6220
  1161
slouken@6220
  1162
	if (inputA->ofs < inputB->ofs)
slouken@6220
  1163
		return -1;
slouken@6220
  1164
	if (inputA->ofs > inputB->ofs)
slouken@6220
  1165
		return 1;
slouken@6220
  1166
	return 0;
slouken@6220
  1167
}
slouken@6220
  1168
slouken@6220
  1169
/* Sort the input objects and recalculate the indices for each input. */
slouken@6220
  1170
static void
slouken@6220
  1171
SortDevObjects(SDL_Joystick *joystick)
slouken@6220
  1172
{
slouken@6220
  1173
	input_t *inputs = joystick->hwdata->Inputs;
slouken@6220
  1174
	int nButtons = 0;
slouken@6220
  1175
	int nHats = 0;
slouken@6220
  1176
	int nAxis = 0;
slouken@6220
  1177
	int n;
slouken@6220
  1178
slouken@6220
  1179
	SDL_qsort(inputs, joystick->hwdata->NumInputs, sizeof(input_t), SortDevFunc);
slouken@6220
  1180
slouken@6220
  1181
	for (n = 0; n < joystick->hwdata->NumInputs; n++)
slouken@6220
  1182
	{
slouken@6220
  1183
		switch (inputs[n].type)
slouken@6220
  1184
		{
slouken@6220
  1185
		case BUTTON:
slouken@6220
  1186
			inputs[n].num = nButtons;
slouken@6220
  1187
			nButtons++;
slouken@6220
  1188
			break;
slouken@6220
  1189
slouken@6220
  1190
		case HAT:
slouken@6220
  1191
			inputs[n].num = nHats;
slouken@6220
  1192
			nHats++;
slouken@6220
  1193
			break;
slouken@6220
  1194
slouken@6220
  1195
		case AXIS:
slouken@6220
  1196
			inputs[n].num = nAxis;
slouken@6220
  1197
			nAxis++;
slouken@6220
  1198
			break;
slouken@6220
  1199
		}
slouken@6220
  1200
	}
slouken@6220
  1201
}
slouken@6220
  1202
slouken@2198
  1203
static BOOL CALLBACK
slouken@2198
  1204
EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev, LPVOID pvRef)
slouken@2198
  1205
{
slouken@2198
  1206
    SDL_Joystick *joystick = (SDL_Joystick *) pvRef;
slouken@2198
  1207
    HRESULT result;
slouken@2198
  1208
    input_t *in = &joystick->hwdata->Inputs[joystick->hwdata->NumInputs];
slouken@2198
  1209
slouken@2198
  1210
    if (dev->dwType & DIDFT_BUTTON) {
slouken@2198
  1211
        in->type = BUTTON;
slouken@2198
  1212
        in->num = joystick->nbuttons;
slouken@6690
  1213
		in->ofs = DIJOFS_BUTTON( in->num );
slouken@2198
  1214
        joystick->nbuttons++;
slouken@2198
  1215
    } else if (dev->dwType & DIDFT_POV) {
slouken@2198
  1216
        in->type = HAT;
slouken@2198
  1217
        in->num = joystick->nhats;
slouken@6690
  1218
		in->ofs = DIJOFS_POV( in->num );
slouken@2198
  1219
        joystick->nhats++;
slouken@2198
  1220
    } else if (dev->dwType & DIDFT_AXIS) {
slouken@2198
  1221
        DIPROPRANGE diprg;
slouken@2198
  1222
        DIPROPDWORD dilong;
slouken@2198
  1223
slouken@2198
  1224
        in->type = AXIS;
slouken@2198
  1225
        in->num = joystick->naxes;
slouken@6690
  1226
		// work our the axis this guy maps too, thanks for the code icculus!
slouken@6690
  1227
		if ( !SDL_memcmp( &dev->guidType, &GUID_XAxis, sizeof(dev->guidType) ) )
slouken@6690
  1228
			in->ofs = DIJOFS_X;
slouken@6690
  1229
		else if ( !SDL_memcmp( &dev->guidType, &GUID_YAxis, sizeof(dev->guidType) ) )
slouken@6690
  1230
			in->ofs = DIJOFS_Y;
slouken@6690
  1231
		else if ( !SDL_memcmp( &dev->guidType, &GUID_ZAxis, sizeof(dev->guidType) ) )
slouken@6690
  1232
			in->ofs = DIJOFS_Z;
slouken@6690
  1233
		else if ( !SDL_memcmp( &dev->guidType, &GUID_RxAxis, sizeof(dev->guidType) ) )
slouken@6690
  1234
			in->ofs = DIJOFS_RX;
slouken@6690
  1235
		else if ( !SDL_memcmp( &dev->guidType, &GUID_RyAxis, sizeof(dev->guidType) ) )
slouken@6690
  1236
			in->ofs = DIJOFS_RY;
slouken@6690
  1237
		else if ( !SDL_memcmp( &dev->guidType, &GUID_RzAxis, sizeof(dev->guidType) ) )
slouken@6690
  1238
			in->ofs = DIJOFS_RZ;
slouken@6690
  1239
		else if ( !SDL_memcmp( &dev->guidType, &GUID_Slider, sizeof(dev->guidType) ) )
slouken@6690
  1240
		{
slouken@6690
  1241
			in->ofs = DIJOFS_SLIDER( joystick->hwdata->NumSliders );
slouken@6690
  1242
			++joystick->hwdata->NumSliders;
slouken@6690
  1243
		}
slouken@6690
  1244
		else 
slouken@6690
  1245
		{
slouken@6690
  1246
			 return DIENUM_CONTINUE; // not an axis we can grok
slouken@6690
  1247
		}
slouken@2198
  1248
slouken@2198
  1249
        diprg.diph.dwSize = sizeof(diprg);
slouken@2198
  1250
        diprg.diph.dwHeaderSize = sizeof(diprg.diph);
slouken@6690
  1251
        diprg.diph.dwObj = dev->dwType;
slouken@6690
  1252
        diprg.diph.dwHow = DIPH_BYID;
slouken@2198
  1253
        diprg.lMin = AXIS_MIN;
slouken@2198
  1254
        diprg.lMax = AXIS_MAX;
slouken@2198
  1255
slouken@2198
  1256
        result =
slouken@6690
  1257
            IDirectInputDevice8_SetProperty(joystick->hwdata->InputDevice,
slouken@2198
  1258
                                            DIPROP_RANGE, &diprg.diph);
slouken@2198
  1259
        if (FAILED(result)) {
slouken@2198
  1260
            return DIENUM_CONTINUE;     /* don't use this axis */
slouken@2198
  1261
        }
slouken@2198
  1262
slouken@2198
  1263
        /* Set dead zone to 0. */
slouken@2198
  1264
        dilong.diph.dwSize = sizeof(dilong);
slouken@2198
  1265
        dilong.diph.dwHeaderSize = sizeof(dilong.diph);
slouken@6690
  1266
        dilong.diph.dwObj = dev->dwType;
slouken@6690
  1267
        dilong.diph.dwHow = DIPH_BYID;
slouken@2198
  1268
        dilong.dwData = 0;
slouken@2198
  1269
        result =
slouken@6690
  1270
            IDirectInputDevice8_SetProperty(joystick->hwdata->InputDevice,
slouken@2198
  1271
                                            DIPROP_DEADZONE, &dilong.diph);
slouken@2198
  1272
        if (FAILED(result)) {
slouken@2198
  1273
            return DIENUM_CONTINUE;     /* don't use this axis */
slouken@2198
  1274
        }
slouken@2198
  1275
slouken@2198
  1276
        joystick->naxes++;
slouken@2198
  1277
    } else {
slouken@2198
  1278
        /* not supported at this time */
slouken@2198
  1279
        return DIENUM_CONTINUE;
slouken@2198
  1280
    }
slouken@2198
  1281
slouken@2198
  1282
    joystick->hwdata->NumInputs++;
slouken@2198
  1283
slouken@2198
  1284
    if (joystick->hwdata->NumInputs == MAX_INPUTS) {
slouken@2198
  1285
        return DIENUM_STOP;     /* too many */
slouken@2198
  1286
    }
slouken@2198
  1287
slouken@2198
  1288
    return DIENUM_CONTINUE;
slouken@2198
  1289
}
slouken@2198
  1290
slouken@2198
  1291
/* Function to update the state of a joystick - called as a device poll.
slouken@2198
  1292
 * This function shouldn't update the joystick structure directly,
slouken@2198
  1293
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@2198
  1294
 * and update joystick device state.
slouken@2198
  1295
 */
slouken@2198
  1296
void
slouken@2198
  1297
SDL_SYS_JoystickUpdate_Polled(SDL_Joystick * joystick)
slouken@2198
  1298
{
slouken@2198
  1299
    DIJOYSTATE2 state;
slouken@2198
  1300
    HRESULT result;
slouken@2198
  1301
    int i;
slouken@2198
  1302
slouken@2198
  1303
    result =
slouken@6690
  1304
        IDirectInputDevice8_GetDeviceState(joystick->hwdata->InputDevice,
slouken@2198
  1305
                                           sizeof(DIJOYSTATE2), &state);
slouken@2198
  1306
    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
slouken@6690
  1307
        IDirectInputDevice8_Acquire(joystick->hwdata->InputDevice);
slouken@2198
  1308
        result =
slouken@6690
  1309
            IDirectInputDevice8_GetDeviceState(joystick->hwdata->InputDevice,
slouken@2198
  1310
                                               sizeof(DIJOYSTATE2), &state);
slouken@2198
  1311
    }
slouken@2198
  1312
slouken@6690
  1313
	if ( result != DI_OK )
slouken@6690
  1314
	{
slouken@6690
  1315
		joystick->hwdata->send_remove_event = 1;
slouken@6690
  1316
		joystick->hwdata->removed = 1;
slouken@6690
  1317
		return;
slouken@6690
  1318
	}
slouken@6690
  1319
slouken@2198
  1320
    /* Set each known axis, button and POV. */
slouken@2198
  1321
    for (i = 0; i < joystick->hwdata->NumInputs; ++i) {
slouken@2198
  1322
        const input_t *in = &joystick->hwdata->Inputs[i];
slouken@2198
  1323
slouken@2198
  1324
        switch (in->type) {
slouken@2198
  1325
        case AXIS:
slouken@2198
  1326
            switch (in->ofs) {
slouken@2198
  1327
            case DIJOFS_X:
slouken@2198
  1328
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
  1329
                                            (Sint16) state.lX);
slouken@2198
  1330
                break;
slouken@2198
  1331
            case DIJOFS_Y:
slouken@2198
  1332
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
  1333
                                            (Sint16) state.lY);
slouken@2198
  1334
                break;
slouken@2198
  1335
            case DIJOFS_Z:
slouken@2198
  1336
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
  1337
                                            (Sint16) state.lZ);
slouken@2198
  1338
                break;
slouken@2198
  1339
            case DIJOFS_RX:
slouken@2198
  1340
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
  1341
                                            (Sint16) state.lRx);
slouken@2198
  1342
                break;
slouken@2198
  1343
            case DIJOFS_RY:
slouken@2198
  1344
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
  1345
                                            (Sint16) state.lRy);
slouken@2198
  1346
                break;
slouken@2198
  1347
            case DIJOFS_RZ:
slouken@2198
  1348
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
  1349
                                            (Sint16) state.lRz);
slouken@2198
  1350
                break;
slouken@2198
  1351
            case DIJOFS_SLIDER(0):
slouken@2198
  1352
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
  1353
                                            (Sint16) state.rglSlider[0]);
slouken@2198
  1354
                break;
slouken@2198
  1355
            case DIJOFS_SLIDER(1):
slouken@2198
  1356
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
  1357
                                            (Sint16) state.rglSlider[1]);
slouken@2198
  1358
                break;
slouken@2198
  1359
            }
slouken@2198
  1360
slouken@2198
  1361
            break;
slouken@2198
  1362
slouken@2198
  1363
        case BUTTON:
slouken@2198
  1364
            SDL_PrivateJoystickButton_Int(joystick, in->num,
slouken@3013
  1365
                                          (Uint8) (state.
slouken@3013
  1366
                                                   rgbButtons[in->ofs -
slouken@3013
  1367
                                                              DIJOFS_BUTTON0]
slouken@2198
  1368
                                                   ? SDL_PRESSED :
slouken@2198
  1369
                                                   SDL_RELEASED));
slouken@2198
  1370
            break;
slouken@2198
  1371
        case HAT:
slouken@2198
  1372
            {
slouken@2198
  1373
                Uint8 pos = TranslatePOV(state.rgdwPOV[in->ofs -
slouken@2198
  1374
                                                       DIJOFS_POV(0)]);
slouken@2198
  1375
                SDL_PrivateJoystickHat_Int(joystick, in->num, pos);
slouken@2198
  1376
                break;
slouken@2198
  1377
            }
slouken@2198
  1378
        }
slouken@2198
  1379
    }
slouken@2198
  1380
}
slouken@2198
  1381
slouken@2198
  1382
void
slouken@2198
  1383
SDL_SYS_JoystickUpdate_Buffered(SDL_Joystick * joystick)
slouken@2198
  1384
{
slouken@2198
  1385
    int i;
slouken@2198
  1386
    HRESULT result;
slouken@2198
  1387
    DWORD numevents;
slouken@2198
  1388
    DIDEVICEOBJECTDATA evtbuf[INPUT_QSIZE];
slouken@2198
  1389
slouken@2198
  1390
    numevents = INPUT_QSIZE;
slouken@2198
  1391
    result =
slouken@6690
  1392
        IDirectInputDevice8_GetDeviceData(joystick->hwdata->InputDevice,
slouken@2198
  1393
                                          sizeof(DIDEVICEOBJECTDATA), evtbuf,
slouken@2198
  1394
                                          &numevents, 0);
slouken@2198
  1395
    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
slouken@6690
  1396
        IDirectInputDevice8_Acquire(joystick->hwdata->InputDevice);
slouken@2198
  1397
        result =
slouken@6690
  1398
            IDirectInputDevice8_GetDeviceData(joystick->hwdata->InputDevice,
slouken@2198
  1399
                                              sizeof(DIDEVICEOBJECTDATA),
slouken@2198
  1400
                                              evtbuf, &numevents, 0);
slouken@2198
  1401
    }
slouken@2198
  1402
slouken@2198
  1403
    /* Handle the events or punt */
slouken@2198
  1404
    if (FAILED(result))
slouken@6690
  1405
	{
slouken@6690
  1406
		joystick->hwdata->send_remove_event = 1;
slouken@6690
  1407
		joystick->hwdata->removed = 1;
slouken@2198
  1408
        return;
slouken@6690
  1409
	}
slouken@2198
  1410
slouken@2198
  1411
    for (i = 0; i < (int) numevents; ++i) {
slouken@2198
  1412
        int j;
slouken@2198
  1413
slouken@2198
  1414
        for (j = 0; j < joystick->hwdata->NumInputs; ++j) {
slouken@2198
  1415
            const input_t *in = &joystick->hwdata->Inputs[j];
slouken@2198
  1416
slouken@2198
  1417
            if (evtbuf[i].dwOfs != in->ofs)
slouken@2198
  1418
                continue;
slouken@2198
  1419
slouken@2198
  1420
            switch (in->type) {
slouken@2198
  1421
            case AXIS:
slouken@2198
  1422
                SDL_PrivateJoystickAxis(joystick, in->num,
slouken@2198
  1423
                                        (Sint16) evtbuf[i].dwData);
slouken@2198
  1424
                break;
slouken@2198
  1425
            case BUTTON:
slouken@2198
  1426
                SDL_PrivateJoystickButton(joystick, in->num,
slouken@3013
  1427
                                          (Uint8) (evtbuf[i].
slouken@3013
  1428
                                                   dwData ? SDL_PRESSED :
slouken@2198
  1429
                                                   SDL_RELEASED));
slouken@2198
  1430
                break;
slouken@2198
  1431
            case HAT:
slouken@2198
  1432
                {
slouken@2198
  1433
                    Uint8 pos = TranslatePOV(evtbuf[i].dwData);
slouken@2198
  1434
                    SDL_PrivateJoystickHat(joystick, in->num, pos);
slouken@2198
  1435
                }
slouken@2198
  1436
            }
slouken@2198
  1437
        }
slouken@2198
  1438
    }
slouken@2198
  1439
}
slouken@2198
  1440
slouken@2198
  1441
slouken@6690
  1442
/* Function to return > 0 if a bit array of buttons differs after applying a mask
slouken@6690
  1443
*/
slouken@6690
  1444
int ButtonChanged( int ButtonsNow, int ButtonsPrev, int ButtonMask )
slouken@6690
  1445
{
slouken@6690
  1446
	return ( ButtonsNow & ButtonMask ) != ( ButtonsPrev & ButtonMask );
slouken@6690
  1447
}
slouken@6690
  1448
slouken@6690
  1449
/* Function to update the state of a XInput style joystick.
slouken@6690
  1450
*/
slouken@6690
  1451
void
slouken@6690
  1452
SDL_SYS_JoystickUpdate_XInput(SDL_Joystick * joystick)
slouken@6690
  1453
{
slouken@6690
  1454
	HRESULT result;
slouken@6690
  1455
slouken@6690
  1456
	if ( !XINPUTGETSTATE )
slouken@6690
  1457
		return;
slouken@6690
  1458
slouken@6690
  1459
	result = XINPUTGETSTATE( joystick->hwdata->userid, &joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot] );
slouken@6690
  1460
	if ( result == ERROR_DEVICE_NOT_CONNECTED )
slouken@6690
  1461
	{
slouken@6690
  1462
		joystick->hwdata->send_remove_event = 1;
slouken@6690
  1463
		joystick->hwdata->removed = 1;
slouken@6690
  1464
		return;
slouken@6690
  1465
	}
slouken@6690
  1466
slouken@6690
  1467
	// only fire events if the data changed from last time
slouken@6690
  1468
	if ( joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot].dwPacketNumber != 0 
slouken@6690
  1469
		&& joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot].dwPacketNumber != joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot^1].dwPacketNumber )
slouken@6690
  1470
	{
slouken@6690
  1471
		XINPUT_STATE_EX *pXInputState = &joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot];
slouken@6690
  1472
		XINPUT_STATE_EX *pXInputStatePrev = &joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot ^ 1];
slouken@6690
  1473
slouken@6690
  1474
		SDL_PrivateJoystickAxis(joystick, 0, (Sint16)pXInputState->Gamepad.sThumbLX );
slouken@6690
  1475
		SDL_PrivateJoystickAxis(joystick, 1, (Sint16)(-1*pXInputState->Gamepad.sThumbLY-1) );
slouken@6690
  1476
		SDL_PrivateJoystickAxis(joystick, 2, (Sint16)pXInputState->Gamepad.sThumbRX );
slouken@6690
  1477
		SDL_PrivateJoystickAxis(joystick, 3, (Sint16)(-1*pXInputState->Gamepad.sThumbRY-1) );
slouken@6690
  1478
		SDL_PrivateJoystickAxis(joystick, 4, (Sint16)((int)pXInputState->Gamepad.bLeftTrigger*32767/255) );
slouken@6690
  1479
		SDL_PrivateJoystickAxis(joystick, 5, (Sint16)((int)pXInputState->Gamepad.bRightTrigger*32767/255) );
slouken@6690
  1480
slouken@6690
  1481
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_UP ) )
slouken@6690
  1482
			SDL_PrivateJoystickButton(joystick, 0, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1483
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_DOWN ) )
slouken@6690
  1484
			SDL_PrivateJoystickButton(joystick, 1, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1485
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_LEFT ) )
slouken@6690
  1486
			SDL_PrivateJoystickButton(joystick, 2, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1487
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_RIGHT ) )
slouken@6690
  1488
			SDL_PrivateJoystickButton(joystick, 3, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1489
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_START ) )
slouken@6690
  1490
			SDL_PrivateJoystickButton(joystick, 4, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_START ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1491
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_BACK ) )
slouken@6690
  1492
			SDL_PrivateJoystickButton(joystick, 5, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_BACK ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1493
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_LEFT_THUMB ) )
slouken@6690
  1494
			SDL_PrivateJoystickButton(joystick, 6, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1495
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_RIGHT_THUMB ) )
slouken@6690
  1496
			SDL_PrivateJoystickButton(joystick, 7, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1497
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_LEFT_SHOULDER ) )
slouken@6690
  1498
			SDL_PrivateJoystickButton(joystick, 8, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1499
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_RIGHT_SHOULDER ) )
slouken@6690
  1500
			SDL_PrivateJoystickButton(joystick, 9, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1501
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_A ) )
slouken@6690
  1502
			SDL_PrivateJoystickButton(joystick, 10, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_A ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1503
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_B ) )
slouken@6690
  1504
			SDL_PrivateJoystickButton(joystick, 11, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_B ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1505
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_X ) )
slouken@6690
  1506
			SDL_PrivateJoystickButton(joystick, 12, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_X ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1507
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_Y ) )
slouken@6690
  1508
			SDL_PrivateJoystickButton(joystick, 13, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_Y ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1509
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons,  0x400 ) )
slouken@6690
  1510
			SDL_PrivateJoystickButton(joystick, 14, pXInputState->Gamepad.wButtons & 0x400 ? SDL_PRESSED :	SDL_RELEASED ); // 0x400 is the undocumented code for the guide button
slouken@6690
  1511
slouken@6690
  1512
		joystick->hwdata->currentXInputSlot ^= 1;
slouken@6690
  1513
slouken@6690
  1514
	}
slouken@6690
  1515
}
slouken@6690
  1516
slouken@6690
  1517
slouken@1895
  1518
static Uint8
slouken@1895
  1519
TranslatePOV(DWORD value)
slouken@1895
  1520
{
slouken@1895
  1521
    const int HAT_VALS[] = {
slouken@1895
  1522
        SDL_HAT_UP,
slouken@1895
  1523
        SDL_HAT_UP | SDL_HAT_RIGHT,
slouken@1895
  1524
        SDL_HAT_RIGHT,
slouken@1895
  1525
        SDL_HAT_DOWN | SDL_HAT_RIGHT,
slouken@1895
  1526
        SDL_HAT_DOWN,
slouken@1895
  1527
        SDL_HAT_DOWN | SDL_HAT_LEFT,
slouken@1895
  1528
        SDL_HAT_LEFT,
slouken@1895
  1529
        SDL_HAT_UP | SDL_HAT_LEFT
slouken@1895
  1530
    };
slouken@1895
  1531
slouken@1895
  1532
    if (LOWORD(value) == 0xFFFF)
slouken@1895
  1533
        return SDL_HAT_CENTERED;
slouken@1895
  1534
slouken@1895
  1535
    /* Round the value up: */
slouken@1895
  1536
    value += 4500 / 2;
slouken@1895
  1537
    value %= 36000;
slouken@1895
  1538
    value /= 4500;
slouken@1895
  1539
slouken@1895
  1540
    if (value >= 8)
slouken@1895
  1541
        return SDL_HAT_CENTERED;        /* shouldn't happen */
slouken@1895
  1542
slouken@1895
  1543
    return HAT_VALS[value];
slouken@1895
  1544
}
slouken@1895
  1545
slouken@1895
  1546
/* SDL_PrivateJoystick* doesn't discard duplicate events, so we need to
slouken@1895
  1547
 * do it. */
slouken@1895
  1548
static int
slouken@1895
  1549
SDL_PrivateJoystickAxis_Int(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
slouken@1895
  1550
{
slouken@1895
  1551
    if (joystick->axes[axis] != value)
slouken@1895
  1552
        return SDL_PrivateJoystickAxis(joystick, axis, value);
slouken@1895
  1553
    return 0;
slouken@1895
  1554
}
slouken@1895
  1555
slouken@1895
  1556
static int
slouken@1895
  1557
SDL_PrivateJoystickHat_Int(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
slouken@1895
  1558
{
slouken@1895
  1559
    if (joystick->hats[hat] != value)
slouken@1895
  1560
        return SDL_PrivateJoystickHat(joystick, hat, value);
slouken@1895
  1561
    return 0;
slouken@1895
  1562
}
slouken@1895
  1563
slouken@1895
  1564
static int
slouken@1895
  1565
SDL_PrivateJoystickButton_Int(SDL_Joystick * joystick, Uint8 button,
slouken@1895
  1566
                              Uint8 state)
slouken@1895
  1567
{
slouken@1895
  1568
    if (joystick->buttons[button] != state)
slouken@1895
  1569
        return SDL_PrivateJoystickButton(joystick, button, state);
slouken@1895
  1570
    return 0;
slouken@1895
  1571
}
slouken@1895
  1572
slouken@1895
  1573
void
slouken@1895
  1574
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
slouken@1895
  1575
{
slouken@1895
  1576
    HRESULT result;
slouken@1895
  1577
slouken@6690
  1578
	if ( joystick->closed || !joystick->hwdata )
slouken@6690
  1579
		return;
slouken@1895
  1580
slouken@6690
  1581
	if (joystick->hwdata->bXInputDevice)
slouken@6690
  1582
	{
slouken@6690
  1583
		SDL_SYS_JoystickUpdate_XInput(joystick);
slouken@6690
  1584
	}
slouken@6690
  1585
	else
slouken@6690
  1586
	{
slouken@6690
  1587
		result = IDirectInputDevice8_Poll(joystick->hwdata->InputDevice);
slouken@6690
  1588
		if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
slouken@6690
  1589
			IDirectInputDevice8_Acquire(joystick->hwdata->InputDevice);
slouken@6690
  1590
			IDirectInputDevice8_Poll(joystick->hwdata->InputDevice);
slouken@6690
  1591
		}
slouken@6690
  1592
slouken@6690
  1593
		if (joystick->hwdata->buffered)
slouken@6690
  1594
			SDL_SYS_JoystickUpdate_Buffered(joystick);
slouken@6690
  1595
		else
slouken@6690
  1596
			SDL_SYS_JoystickUpdate_Polled(joystick);
slouken@6690
  1597
	}
slouken@6690
  1598
slouken@6690
  1599
	if ( joystick->hwdata->removed )
slouken@6690
  1600
	{
slouken@6690
  1601
		joystick->closed = 1;
slouken@6690
  1602
		joystick->uncentered = 1;
slouken@6690
  1603
	}
slouken@1895
  1604
}
slouken@1895
  1605
slouken@1895
  1606
/* Function to close a joystick after use */
slouken@1895
  1607
void
slouken@1895
  1608
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
slouken@1895
  1609
{
slouken@6690
  1610
	if ( joystick->hwdata->bXInputDevice )
slouken@6690
  1611
	{
slouken@6690
  1612
		JoyStick_DeviceData *joysticklist = SYS_Joystick;
slouken@6690
  1613
		// scan the opened joysticks and clear the userid for this instance
slouken@6690
  1614
		for( ; joysticklist; joysticklist = joysticklist->pNext)
slouken@6690
  1615
		{
slouken@6690
  1616
			if ( joysticklist->bXInputDevice && joysticklist->nInstanceID == joystick->instance_id )
slouken@6690
  1617
			{
slouken@6690
  1618
				joysticklist->XInputUserId = INVALID_XINPUT_USERID;
slouken@6690
  1619
			}
slouken@6690
  1620
		}
slouken@6690
  1621
slouken@6690
  1622
	}
slouken@6690
  1623
	else
slouken@6690
  1624
	{
slouken@6690
  1625
	    IDirectInputDevice8_Unacquire(joystick->hwdata->InputDevice);
slouken@6690
  1626
	    IDirectInputDevice8_Release(joystick->hwdata->InputDevice);
slouken@6690
  1627
	}
slouken@1895
  1628
slouken@1895
  1629
    if (joystick->hwdata != NULL) {
slouken@1895
  1630
        /* free system specific hardware data */
slouken@2713
  1631
        SDL_free(joystick->hwdata);
slouken@1895
  1632
    }
slouken@6690
  1633
slouken@6690
  1634
	joystick->closed = 1;
slouken@1895
  1635
}
slouken@1895
  1636
slouken@1895
  1637
/* Function to perform any system-specific joystick related cleanup */
slouken@1895
  1638
void
slouken@1895
  1639
SDL_SYS_JoystickQuit(void)
slouken@1895
  1640
{
slouken@6690
  1641
	JoyStick_DeviceData *device = SYS_Joystick;
slouken@5090
  1642
slouken@6690
  1643
	while ( device )
slouken@6690
  1644
	{
slouken@6690
  1645
		JoyStick_DeviceData *device_next = device->pNext;
slouken@6690
  1646
		SDL_free(device->joystickname);
slouken@6690
  1647
		SDL_free(device);
slouken@6690
  1648
		device = device_next;
slouken@6690
  1649
	}
slouken@6690
  1650
	SYS_Joystick = NULL;
slouken@6690
  1651
slouken@6690
  1652
	if ( s_threadJoystick )
slouken@6690
  1653
	{
slouken@6690
  1654
		SDL_LockMutex( s_mutexJoyStickEnum );
slouken@6690
  1655
		s_bJoystickThreadQuit = SDL_TRUE;
slouken@6690
  1656
		SDL_CondBroadcast( s_condJoystickThread ); // signal the joystick thread to quit
slouken@6690
  1657
		SDL_UnlockMutex( s_mutexJoyStickEnum );
slouken@6690
  1658
		SDL_WaitThread( s_threadJoystick, NULL ); // wait for it to bugger off
slouken@6690
  1659
slouken@6690
  1660
		SDL_DestroyMutex( s_mutexJoyStickEnum );
slouken@6690
  1661
		SDL_DestroyCond( s_condJoystickThread );
slouken@6690
  1662
		s_condJoystickThread= NULL;
slouken@6690
  1663
		s_mutexJoyStickEnum = NULL;
slouken@6690
  1664
		s_threadJoystick = NULL;
slouken@6690
  1665
	}
slouken@5090
  1666
icculus@5591
  1667
    if (dinput != NULL) {
slouken@6690
  1668
        IDirectInput8_Release(dinput);
icculus@5591
  1669
        dinput = NULL;
icculus@5591
  1670
    }
icculus@5591
  1671
icculus@5591
  1672
    if (coinitialized) {
icculus@5591
  1673
        WIN_CoUninitialize();
icculus@5591
  1674
        coinitialized = SDL_FALSE;
icculus@5591
  1675
    }
slouken@6690
  1676
slouken@6690
  1677
	if ( s_pKnownJoystickGUIDs )
slouken@6690
  1678
	{
slouken@6690
  1679
		SDL_free( s_pKnownJoystickGUIDs );
slouken@6690
  1680
		s_pKnownJoystickGUIDs = NULL;
slouken@6690
  1681
	}
slouken@6690
  1682
icculus@6990
  1683
    if (s_bXInputEnabled) {
icculus@6990
  1684
        WIN_UnloadXInputDLL();
icculus@6990
  1685
    }
slouken@6690
  1686
}
slouken@6690
  1687
slouken@6690
  1688
slouken@6690
  1689
/* return the stable device guid for this device index */
slouken@6738
  1690
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
slouken@6690
  1691
{
slouken@6690
  1692
	JoyStick_DeviceData *device = SYS_Joystick;
slouken@6690
  1693
	int index;
slouken@6690
  1694
slouken@6690
  1695
	for (index = device_index; index > 0; index--)
slouken@6690
  1696
		device = device->pNext;
slouken@6690
  1697
slouken@6690
  1698
	return device->guid;
slouken@6690
  1699
}
slouken@6690
  1700
slouken@6738
  1701
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
slouken@6707
  1702
{
slouken@6707
  1703
	return joystick->hwdata->guid;
slouken@6707
  1704
}
slouken@6707
  1705
slouken@6707
  1706
/* return SDL_TRUE if this device is using XInput */
slouken@6707
  1707
SDL_bool SDL_SYS_IsXInputDeviceIndex(int device_index)
slouken@6690
  1708
{
slouken@6690
  1709
	JoyStick_DeviceData *device = SYS_Joystick;
slouken@6690
  1710
	int index;
slouken@6690
  1711
slouken@6690
  1712
	for (index = device_index; index > 0; index--)
slouken@6690
  1713
		device = device->pNext;
slouken@6690
  1714
slouken@6690
  1715
	return device->bXInputDevice;
slouken@6690
  1716
}
slouken@6690
  1717
slouken@1895
  1718
#endif /* SDL_JOYSTICK_DINPUT */
slouken@3575
  1719
slouken@1895
  1720
/* vi: set ts=4 sw=4 expandtab: */