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