src/joystick/windows/SDL_dxjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 27 Nov 2012 09:19:09 -0800
changeset 6710 e650705e2c1d
parent 6709 3b43d3a9b7d5
child 6712 05f046f5886b
permissions -rw-r--r--
Alfred Reynolds - add scanning of XInput for controller connectedness so we can detect when the wireless controller turns on and off, the usb side doesn't change when the controller goes away
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
/*  helper func to create a hidden, message only window for the joystick detect thread
slouken@6690
   502
 */
slouken@6690
   503
HWND CreateHiddenJoystickDetectWindow() {
slouken@6690
   504
	WNDCLASSEX wincl;
slouken@6690
   505
	HWND hMessageWindow = 0;
slouken@6690
   506
	SDL_memset( &wincl, 0x0, sizeof(wincl) );
slouken@6690
   507
	wincl.hInstance = GetModuleHandle( NULL );
slouken@6690
   508
	wincl.lpszClassName = L"Message";
slouken@6690
   509
	wincl.lpfnWndProc = SDL_PrivateJoystickDetectProc;      // This function is called by windows
slouken@6690
   510
	wincl.cbSize = sizeof (WNDCLASSEX);
slouken@6690
   511
slouken@6690
   512
	if (!RegisterClassEx (&wincl))
slouken@6690
   513
		return 0;
slouken@6690
   514
slouken@6690
   515
	hMessageWindow = (HWND)CreateWindowEx( 0,  L"Message", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL );
slouken@6690
   516
	return hMessageWindow;
slouken@6690
   517
}
slouken@6690
   518
slouken@6690
   519
DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, \
slouken@6690
   520
	0xC0, 0x4F, 0xB9, 0x51, 0xED);
slouken@6690
   521
slouken@6690
   522
/* Function/thread to scan the system for joysticks.
slouken@6690
   523
 */
slouken@6690
   524
static int
slouken@6690
   525
SDL_JoystickThread(void *_data)
slouken@6690
   526
{
slouken@6690
   527
	HRESULT result = S_OK;	
slouken@6690
   528
	HWND messageWindow = 0;
slouken@6690
   529
	HDEVNOTIFY hNotify = 0;
slouken@6690
   530
	DEV_BROADCAST_DEVICEINTERFACE dbh;
slouken@6710
   531
	SDL_bool bOpenedXInputDevices[4];
slouken@6710
   532
slouken@6710
   533
	SDL_memset( bOpenedXInputDevices, 0x0, sizeof(bOpenedXInputDevices) );
slouken@6690
   534
slouken@6690
   535
	result = WIN_CoInitialize();
slouken@6690
   536
slouken@6690
   537
	messageWindow = CreateHiddenJoystickDetectWindow();
slouken@6690
   538
	if ( !messageWindow )
slouken@6690
   539
	{
slouken@6690
   540
		SDL_SetError("Failed to create message window for joystick autodetect.",
slouken@6690
   541
			GetLastError());
slouken@6690
   542
		return -1;
slouken@6690
   543
	}
slouken@6690
   544
slouken@6690
   545
	SDL_memset(&dbh, 0x0, sizeof(dbh));
slouken@6690
   546
slouken@6690
   547
	dbh.dbcc_size = sizeof(dbh);
slouken@6690
   548
	dbh.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
slouken@6690
   549
	dbh.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;
slouken@6690
   550
slouken@6690
   551
	hNotify = RegisterDeviceNotification( messageWindow, &dbh, DEVICE_NOTIFY_WINDOW_HANDLE );
slouken@6690
   552
	if ( !hNotify )
slouken@6690
   553
	{
slouken@6690
   554
		SDL_SetError("Failed to create notify device for joystick autodetect.",
slouken@6690
   555
			GetLastError());
slouken@6690
   556
		return -1;
slouken@6690
   557
	}
slouken@6690
   558
slouken@6690
   559
	SDL_LockMutex( s_mutexJoyStickEnum );
slouken@6690
   560
	while ( s_bJoystickThreadQuit == SDL_FALSE )
slouken@6690
   561
	{
slouken@6690
   562
		MSG messages;
slouken@6710
   563
		Uint8 userId;
slouken@6710
   564
		int nCurrentOpenedXInputDevices = 0;
slouken@6710
   565
		int nNewOpenedXInputDevices = 0;
slouken@6690
   566
		SDL_CondWaitTimeout( s_condJoystickThread, s_mutexJoyStickEnum, 300 );
slouken@6690
   567
slouken@6690
   568
		while ( s_bJoystickThreadQuit == SDL_FALSE && PeekMessage(&messages, messageWindow, 0, 0, PM_NOREMOVE) )
slouken@6690
   569
		{
slouken@6690
   570
			if ( GetMessage(&messages, messageWindow, 0, 0) != 0 )  {
slouken@6690
   571
				TranslateMessage(&messages);
slouken@6690
   572
				DispatchMessage(&messages);
slouken@6690
   573
			}
slouken@6690
   574
		}
slouken@6690
   575
slouken@6710
   576
		// scan for any change in XInput devices
slouken@6710
   577
		for ( userId = 0; userId < 4; userId++ )
slouken@6710
   578
		{
slouken@6710
   579
			XINPUT_CAPABILITIES	capabilities;
slouken@6710
   580
			DWORD result;
slouken@6710
   581
slouken@6710
   582
			if ( bOpenedXInputDevices[userId] == SDL_TRUE )
slouken@6710
   583
				nCurrentOpenedXInputDevices++;
slouken@6710
   584
slouken@6710
   585
			result = XINPUTGETCAPABILITIES( userId, XINPUT_FLAG_GAMEPAD, &capabilities );
slouken@6710
   586
			if ( result == ERROR_SUCCESS )
slouken@6710
   587
			{
slouken@6710
   588
				bOpenedXInputDevices[userId] = SDL_TRUE;
slouken@6710
   589
				nNewOpenedXInputDevices++;
slouken@6710
   590
			}
slouken@6710
   591
			else
slouken@6710
   592
			{
slouken@6710
   593
				bOpenedXInputDevices[userId] = SDL_FALSE;
slouken@6710
   594
			}
slouken@6710
   595
		}
slouken@6710
   596
slouken@6710
   597
slouken@6710
   598
		if ( s_pKnownJoystickGUIDs && ( s_bWindowsDeviceChanged || nNewOpenedXInputDevices != nCurrentOpenedXInputDevices ) )
slouken@6690
   599
		{
slouken@6690
   600
			SDL_Delay( 300 ); // wait for direct input to find out about this device
slouken@6690
   601
slouken@6690
   602
			s_bDeviceRemoved = SDL_TRUE;
slouken@6690
   603
			s_bDeviceAdded = SDL_TRUE;
slouken@6690
   604
			s_bWindowsDeviceChanged = SDL_FALSE;
slouken@6690
   605
		}
slouken@6690
   606
	}
slouken@6690
   607
	SDL_UnlockMutex( s_mutexJoyStickEnum );
slouken@6690
   608
slouken@6690
   609
	if ( hNotify )
slouken@6690
   610
		UnregisterDeviceNotification( hNotify );
slouken@6690
   611
slouken@6690
   612
	if ( messageWindow )
slouken@6690
   613
		DestroyWindow( messageWindow );
slouken@6690
   614
	messageWindow = 0;
slouken@6690
   615
	WIN_CoUninitialize();
slouken@6690
   616
	return 1;
slouken@6690
   617
}
slouken@6690
   618
slouken@6690
   619
slouken@1895
   620
/* Function to scan the system for joysticks.
slouken@1895
   621
 * This function should set SDL_numjoysticks to the number of available
slouken@1895
   622
 * joysticks.  Joystick 0 should be the system default joystick.
slouken@1895
   623
 * It should return 0, or -1 on an unrecoverable fatal error.
slouken@1895
   624
 */
slouken@1895
   625
int
slouken@1895
   626
SDL_SYS_JoystickInit(void)
slouken@1895
   627
{
slouken@1895
   628
    HRESULT result;
slouken@2713
   629
    HINSTANCE instance;
slouken@1895
   630
icculus@5591
   631
    result = WIN_CoInitialize();
slouken@2198
   632
    if (FAILED(result)) {
slouken@2198
   633
        SetDIerror("CoInitialize", result);
slouken@1895
   634
        return (-1);
slouken@1895
   635
    }
slouken@1895
   636
icculus@5591
   637
    coinitialized = SDL_TRUE;
icculus@5591
   638
slouken@6690
   639
    result = CoCreateInstance(&CLSID_DirectInput8, NULL, CLSCTX_INPROC_SERVER,
slouken@6690
   640
                              &IID_IDirectInput8, (LPVOID)&dinput);
slouken@2198
   641
slouken@2198
   642
    if (FAILED(result)) {
icculus@5591
   643
        SDL_SYS_JoystickQuit();
slouken@2198
   644
        SetDIerror("CoCreateInstance", result);
slouken@2198
   645
        return (-1);
slouken@2198
   646
    }
slouken@2198
   647
slouken@2198
   648
    /* Because we used CoCreateInstance, we need to Initialize it, first. */
slouken@2713
   649
    instance = GetModuleHandle(NULL);
slouken@2713
   650
    if (instance == NULL) {
icculus@5591
   651
        SDL_SYS_JoystickQuit();
slouken@2713
   652
        SDL_SetError("GetModuleHandle() failed with error code %d.",
slouken@2713
   653
                     GetLastError());
slouken@2713
   654
        return (-1);
slouken@2713
   655
    }
slouken@6690
   656
    result = IDirectInput8_Initialize(dinput, instance, DIRECTINPUT_VERSION);
slouken@2198
   657
slouken@2198
   658
    if (FAILED(result)) {
icculus@5591
   659
        SDL_SYS_JoystickQuit();
slouken@2198
   660
        SetDIerror("IDirectInput::Initialize", result);
slouken@2198
   661
        return (-1);
slouken@2198
   662
    }
slouken@2198
   663
slouken@6690
   664
	s_mutexJoyStickEnum = SDL_CreateMutex();
slouken@6690
   665
	s_condJoystickThread = SDL_CreateCond();
slouken@6690
   666
	s_bDeviceAdded = SDL_TRUE; // force a scan of the system for joysticks this first time
slouken@6690
   667
	SDL_SYS_JoystickDetect();
slouken@1895
   668
slouken@6690
   669
	// try to load XInput support if available
slouken@6690
   670
	s_pXInputDLL = LoadLibrary( L"XInput1_3.dll" );
slouken@6690
   671
	if ( !s_pXInputDLL )
slouken@6690
   672
		s_pXInputDLL = LoadLibrary( L"bin\\XInput1_3.dll" );
slouken@6690
   673
	if ( s_pXInputDLL )
slouken@6690
   674
	{
slouken@6690
   675
		// 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
   676
		PC_XInputGetState = (XInputGetState_t)GetProcAddress( (HMODULE)s_pXInputDLL, (LPCSTR)100 );
slouken@6690
   677
		PC_XInputSetState = (XInputSetState_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputSetState" );
slouken@6690
   678
		PC_XInputGetCapabilities = (XInputGetCapabilities_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputGetCapabilities" );
slouken@6690
   679
		if ( !PC_XInputGetState || !PC_XInputSetState || !PC_XInputGetCapabilities )
slouken@6690
   680
		{
slouken@6690
   681
			SDL_SYS_JoystickQuit();
slouken@6690
   682
			SDL_SetError("GetProcAddress() failed when loading XInput.", GetLastError());
slouken@6690
   683
			return (-1);
slouken@6690
   684
		}
slouken@6690
   685
	}
slouken@1895
   686
slouken@2198
   687
slouken@6690
   688
	if ( !s_threadJoystick )
slouken@6690
   689
	{
slouken@6690
   690
		s_bJoystickThreadQuit = SDL_FALSE;
slouken@6690
   691
		/* spin up the thread to detect hotplug of devices */
slouken@6690
   692
#if defined(__WIN32__) && !defined(HAVE_LIBC)
slouken@6690
   693
#undef SDL_CreateThread
slouken@6690
   694
		s_threadJoystick= SDL_CreateThread( SDL_JoystickThread, "SDL_joystick", NULL, NULL, NULL );
slouken@6690
   695
#else
slouken@6690
   696
		s_threadJoystick = SDL_CreateThread( SDL_JoystickThread, "SDL_joystick", NULL );
slouken@6690
   697
#endif
slouken@6690
   698
	}
slouken@6690
   699
	    return SDL_SYS_NumJoysticks();
slouken@2198
   700
}
slouken@2198
   701
slouken@6707
   702
/* return the number of joysticks that are connected right now */
slouken@6707
   703
int SDL_SYS_NumJoysticks()
slouken@6707
   704
{
slouken@6707
   705
	int nJoysticks = 0;
slouken@6707
   706
	JoyStick_DeviceData *device = SYS_Joystick;
slouken@6707
   707
	while ( device )
slouken@6707
   708
	{
slouken@6707
   709
		nJoysticks++;
slouken@6707
   710
		device = device->pNext;
slouken@6707
   711
	}
slouken@6707
   712
slouken@6707
   713
	return nJoysticks;
slouken@6707
   714
}
slouken@6707
   715
slouken@6707
   716
static int s_iNewGUID = 0;
slouken@6707
   717
slouken@6707
   718
/* helper function for direct input, gets called for each connected joystick */
slouken@6707
   719
static BOOL CALLBACK
slouken@6707
   720
	EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
slouken@6707
   721
{
slouken@6707
   722
	JoyStick_DeviceData *pNewJoystick;
slouken@6707
   723
	SDL_bool bXInputDevice;
slouken@6707
   724
	pNewJoystick = *(JoyStick_DeviceData **)pContext;
slouken@6707
   725
	while ( pNewJoystick )
slouken@6707
   726
	{
slouken@6707
   727
		if ( !SDL_memcmp( &pNewJoystick->dxdevice.guidInstance, &pdidInstance->guidInstance, sizeof(pNewJoystick->dxdevice.guidInstance) ) )
slouken@6707
   728
		{
slouken@6707
   729
			if ( SYS_Joystick )
slouken@6707
   730
			{
slouken@6707
   731
				pNewJoystick->pNext = SYS_Joystick;
slouken@6707
   732
			}
slouken@6707
   733
			SYS_Joystick = pNewJoystick;
slouken@6707
   734
			/* if we are replacing the front of the list then update it */
slouken@6707
   735
			if ( pNewJoystick == *(JoyStick_DeviceData **)pContext ) 
slouken@6707
   736
			{
slouken@6707
   737
				*(JoyStick_DeviceData **)pContext = pNewJoystick->pNext;
slouken@6707
   738
			}
slouken@6707
   739
slouken@6707
   740
			s_pKnownJoystickGUIDs[ s_iNewGUID ] = pdidInstance->guidInstance;
slouken@6707
   741
			s_iNewGUID++;
slouken@6707
   742
			if ( s_iNewGUID < MAX_JOYSTICKS )
slouken@6707
   743
				return DIENUM_CONTINUE; // already have this joystick loaded, just keep going
slouken@6707
   744
			else
slouken@6707
   745
				return DIENUM_STOP; 
slouken@6707
   746
		}
slouken@6707
   747
slouken@6707
   748
		pNewJoystick = pNewJoystick->pNext;
slouken@6707
   749
	}
slouken@6707
   750
slouken@6707
   751
	s_bDeviceAdded = SDL_TRUE;
slouken@6707
   752
slouken@6707
   753
	bXInputDevice = IsXInputDevice( &pdidInstance->guidProduct );
slouken@6707
   754
slouken@6707
   755
	pNewJoystick = (JoyStick_DeviceData *)SDL_malloc( sizeof(JoyStick_DeviceData) );
slouken@6707
   756
slouken@6707
   757
	if ( bXInputDevice )
slouken@6707
   758
	{
slouken@6707
   759
		SDL_memset(&(pNewJoystick->dxdevice), 0x0,
slouken@6707
   760
			sizeof(DIDEVICEINSTANCE));
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
		SDL_memcpy(&(pNewJoystick->dxdevice), pdidInstance,
slouken@6707
   768
			sizeof(DIDEVICEINSTANCE));
slouken@6707
   769
	}
slouken@6707
   770
	pNewJoystick->joystickname = WIN_StringToUTF8(pdidInstance->tszProductName);
slouken@6707
   771
	pNewJoystick->send_add_event = 1;
slouken@6707
   772
	pNewJoystick->nInstanceID = ++s_nInstanceID;
slouken@6707
   773
	SDL_memcpy( &pNewJoystick->guid, &pdidInstance->guidProduct, sizeof(pNewJoystick->guid) );
slouken@6707
   774
	pNewJoystick->pNext = NULL;
slouken@6707
   775
slouken@6707
   776
	if ( SYS_Joystick )
slouken@6707
   777
	{
slouken@6707
   778
		pNewJoystick->pNext = SYS_Joystick;
slouken@6707
   779
	}
slouken@6707
   780
	SYS_Joystick = pNewJoystick;
slouken@6707
   781
slouken@6707
   782
	s_pKnownJoystickGUIDs[ s_iNewGUID ] = pdidInstance->guidInstance;
slouken@6707
   783
	s_iNewGUID++;
slouken@6707
   784
slouken@6707
   785
	if ( s_iNewGUID < MAX_JOYSTICKS )
slouken@6707
   786
		return DIENUM_CONTINUE; // already have this joystick loaded, just keep going
slouken@6707
   787
	else
slouken@6707
   788
		return DIENUM_STOP; 
slouken@6707
   789
}
slouken@6707
   790
slouken@6707
   791
/* detect any new joysticks being inserted into the system */
slouken@6707
   792
void SDL_SYS_JoystickDetect()
slouken@6707
   793
{
slouken@6707
   794
	HRESULT result;
slouken@6707
   795
	JoyStick_DeviceData *pCurList = NULL;
slouken@6707
   796
	/* only enum the devices if the joystick thread told us something changed */
slouken@6707
   797
	if ( s_bDeviceAdded || s_bDeviceRemoved )
slouken@6707
   798
	{
slouken@6707
   799
		s_bDeviceAdded = SDL_FALSE;
slouken@6707
   800
		s_bDeviceRemoved = SDL_FALSE;
slouken@6707
   801
slouken@6707
   802
		pCurList = SYS_Joystick;
slouken@6707
   803
		SYS_Joystick = NULL;
slouken@6707
   804
		s_iNewGUID = 0;
slouken@6707
   805
		SDL_mutexP( s_mutexJoyStickEnum );
slouken@6707
   806
slouken@6707
   807
		if ( !s_pKnownJoystickGUIDs )
slouken@6707
   808
			s_pKnownJoystickGUIDs = SDL_malloc( sizeof(GUID)*MAX_JOYSTICKS );
slouken@6707
   809
				
slouken@6707
   810
		SDL_memset( s_pKnownJoystickGUIDs, 0x0, sizeof(GUID)*MAX_JOYSTICKS );
slouken@6707
   811
slouken@6707
   812
		/* Look for joysticks, wheels, head trackers, gamepads, etc.. */
slouken@6707
   813
		result = IDirectInput8_EnumDevices(dinput,
slouken@6707
   814
			DI8DEVCLASS_GAMECTRL,
slouken@6707
   815
			EnumJoysticksCallback,
slouken@6707
   816
			&pCurList, DIEDFL_ATTACHEDONLY);
slouken@6707
   817
slouken@6707
   818
		SDL_mutexV( s_mutexJoyStickEnum );
slouken@6707
   819
	}
slouken@6707
   820
slouken@6707
   821
	if ( pCurList )
slouken@6707
   822
	{
slouken@6707
   823
		while ( pCurList )
slouken@6707
   824
		{
slouken@6707
   825
			JoyStick_DeviceData *pListNext = NULL;
slouken@6707
   826
#if !SDL_EVENTS_DISABLED
slouken@6707
   827
			SDL_Event event;
slouken@6707
   828
			event.type = SDL_JOYDEVICEREMOVED;
slouken@6707
   829
slouken@6707
   830
			if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@6707
   831
				event.jdevice.which = pCurList->nInstanceID;
slouken@6707
   832
				if ((SDL_EventOK == NULL)
slouken@6707
   833
					|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@6707
   834
						SDL_PushEvent(&event);
slouken@6707
   835
				}
slouken@6707
   836
			}
slouken@6707
   837
#endif // !SDL_EVENTS_DISABLED 
slouken@6707
   838
slouken@6707
   839
			pListNext = pCurList->pNext;
slouken@6707
   840
			SDL_free(pCurList->joystickname);
slouken@6707
   841
			SDL_free( pCurList );
slouken@6707
   842
			pCurList = pListNext;
slouken@6707
   843
		}
slouken@6707
   844
slouken@6707
   845
	}
slouken@6707
   846
slouken@6707
   847
	if ( s_bDeviceAdded )
slouken@6707
   848
	{
slouken@6707
   849
		JoyStick_DeviceData *pNewJoystick;
slouken@6707
   850
		int device_index = 0;
slouken@6707
   851
		s_bDeviceAdded = SDL_FALSE;
slouken@6707
   852
		pNewJoystick = SYS_Joystick;
slouken@6707
   853
		while ( pNewJoystick )
slouken@6707
   854
		{
slouken@6707
   855
			if ( pNewJoystick->send_add_event )
slouken@6707
   856
			{
slouken@6707
   857
#if !SDL_EVENTS_DISABLED
slouken@6707
   858
				SDL_Event event;
slouken@6707
   859
				event.type = SDL_JOYDEVICEADDED;
slouken@6707
   860
slouken@6707
   861
				if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@6707
   862
					event.jdevice.which = device_index;
slouken@6707
   863
					if ((SDL_EventOK == NULL)
slouken@6707
   864
						|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
slouken@6707
   865
							SDL_PushEvent(&event);
slouken@6707
   866
					}
slouken@6707
   867
				}
slouken@6707
   868
#endif /* !SDL_EVENTS_DISABLED */
slouken@6707
   869
				pNewJoystick->send_add_event = 0;
slouken@6707
   870
			}
slouken@6707
   871
			device_index++;
slouken@6707
   872
			pNewJoystick = pNewJoystick->pNext;
slouken@6707
   873
		}
slouken@6707
   874
	}
slouken@6707
   875
}
slouken@6707
   876
slouken@6707
   877
/* we need to poll if we have pending hotplug device changes or connected devices */
slouken@6707
   878
SDL_bool SDL_SYS_JoystickNeedsPolling()
slouken@6707
   879
{
slouken@6707
   880
	/* we have a new device or one was pulled, we need to think this frame please */
slouken@6707
   881
	if ( s_bDeviceAdded || s_bDeviceRemoved )
slouken@6707
   882
		return SDL_TRUE;
slouken@6707
   883
slouken@6707
   884
	return SDL_FALSE;
slouken@6707
   885
}
slouken@6707
   886
slouken@1895
   887
/* Function to get the device-dependent name of a joystick */
slouken@1895
   888
const char *
slouken@6707
   889
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
slouken@1895
   890
{
slouken@6690
   891
	JoyStick_DeviceData *device = SYS_Joystick;
slouken@6690
   892
slouken@6707
   893
	for (; device_index > 0; device_index--)
slouken@6690
   894
		device = device->pNext;
slouken@6690
   895
slouken@6690
   896
	return device->joystickname;
slouken@1895
   897
}
slouken@1895
   898
slouken@6707
   899
/* Function to perform the mapping between current device instance and this joysticks instance id */
slouken@6707
   900
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
slouken@6707
   901
{
slouken@6707
   902
	JoyStick_DeviceData *device = SYS_Joystick;
slouken@6707
   903
	int index;
slouken@6707
   904
slouken@6707
   905
	for (index = device_index; index > 0; index--)
slouken@6707
   906
		device = device->pNext;
slouken@6707
   907
slouken@6707
   908
	return device->nInstanceID;
slouken@6707
   909
}
slouken@6707
   910
slouken@1895
   911
/* Function to open a joystick for use.
slouken@1895
   912
   The joystick to open is specified by the index field of the joystick.
slouken@1895
   913
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@1895
   914
   It returns 0, or -1 if there is an error.
slouken@1895
   915
 */
slouken@1895
   916
int
slouken@6690
   917
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
slouken@1895
   918
{
slouken@1895
   919
    HRESULT result;
slouken@1895
   920
    LPDIRECTINPUTDEVICE device;
slouken@2198
   921
    DIPROPDWORD dipdw;
slouken@6690
   922
	JoyStick_DeviceData *joystickdevice = SYS_Joystick;
slouken@6690
   923
slouken@6690
   924
	for (; device_index > 0; device_index--)
slouken@6690
   925
		joystickdevice = joystickdevice->pNext;
slouken@2198
   926
slouken@2713
   927
    SDL_memset(&dipdw, 0, sizeof(DIPROPDWORD));
slouken@2198
   928
    dipdw.diph.dwSize = sizeof(DIPROPDWORD);
slouken@2198
   929
    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
slouken@2198
   930
slouken@1895
   931
    /* allocate memory for system specific hardware data */
slouken@6698
   932
	joystick->instance_id = joystickdevice->nInstanceID;
slouken@1895
   933
    joystick->hwdata =
slouken@2713
   934
        (struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata));
slouken@1895
   935
    if (joystick->hwdata == NULL) {
slouken@1895
   936
        SDL_OutOfMemory();
slouken@1895
   937
        return (-1);
slouken@1895
   938
    }
slouken@2713
   939
    SDL_memset(joystick->hwdata, 0, sizeof(struct joystick_hwdata));
slouken@1895
   940
    joystick->hwdata->buffered = 1;
slouken@6690
   941
	joystick->hwdata->removed = 0;
slouken@2198
   942
    joystick->hwdata->Capabilities.dwSize = sizeof(DIDEVCAPS);
slouken@6690
   943
	joystick->hwdata->guid = joystickdevice->guid;
slouken@1895
   944
slouken@6690
   945
	if ( joystickdevice->bXInputDevice )
slouken@6690
   946
	{
slouken@6690
   947
		XINPUT_CAPABILITIES	capabilities;
slouken@6690
   948
		Uint8 userId = 0;
slouken@6690
   949
		DWORD result;
slouken@6690
   950
		JoyStick_DeviceData *joysticklist = SYS_Joystick;
slouken@6690
   951
		// scan the opened joysticks and pick the next free xinput userid for this one
slouken@6690
   952
		for( ; joysticklist; joysticklist = joysticklist->pNext)
slouken@6690
   953
		{
slouken@6690
   954
			if ( joysticklist->bXInputDevice && joysticklist->XInputUserId == userId )
slouken@6690
   955
				userId++;
slouken@6690
   956
		}
slouken@1895
   957
slouken@6690
   958
		if ( XINPUTGETCAPABILITIES )
slouken@6690
   959
		{
slouken@6690
   960
			result = XINPUTGETCAPABILITIES( userId, XINPUT_FLAG_GAMEPAD, &capabilities );
slouken@6690
   961
			if ( result == ERROR_SUCCESS )
slouken@6690
   962
			{
slouken@6690
   963
				SDL_bool bIsSupported = SDL_FALSE;
slouken@6690
   964
				// Current version of XInput mistakenly returns 0 as the Type. Ignore it and ensure the subtype is a gamepad.
slouken@6690
   965
				bIsSupported = ( capabilities.SubType == XINPUT_DEVSUBTYPE_GAMEPAD );
slouken@2198
   966
slouken@6690
   967
				if ( !bIsSupported )
slouken@6690
   968
				{
slouken@6690
   969
					joystickdevice->bXInputDevice = SDL_FALSE;
slouken@6690
   970
				}
slouken@6690
   971
				else
slouken@6690
   972
				{
slouken@6690
   973
					// valid
slouken@6690
   974
					joystick->hwdata->bXInputDevice = SDL_TRUE;
slouken@6690
   975
					SDL_memset( joystick->hwdata->XInputState, 0x0, sizeof(joystick->hwdata->XInputState) );
slouken@6690
   976
					joystickdevice->XInputUserId = userId;
slouken@6690
   977
					joystick->hwdata->userid = userId;
slouken@6690
   978
					joystick->hwdata->currentXInputSlot = 0;
slouken@6690
   979
					// The XInput API has a hard coded button/axis mapping, so we just match it
slouken@6690
   980
					joystick->naxes = 6;
slouken@6690
   981
					joystick->nbuttons = 15;
slouken@6690
   982
					joystick->nballs = 0;
slouken@6690
   983
					joystick->nhats = 0;
slouken@6690
   984
				}
slouken@6690
   985
			}
slouken@6690
   986
			else
slouken@6690
   987
			{
slouken@6690
   988
				joystickdevice->bXInputDevice = SDL_FALSE;
slouken@6690
   989
			}
slouken@6690
   990
		}
slouken@6690
   991
		else
slouken@6690
   992
		{
slouken@6690
   993
			joystickdevice->bXInputDevice = SDL_FALSE;
slouken@6690
   994
		}
slouken@6690
   995
	}
slouken@1895
   996
slouken@6690
   997
	if ( joystickdevice->bXInputDevice == SDL_FALSE )
slouken@6690
   998
	{
slouken@6690
   999
		joystick->hwdata->bXInputDevice = SDL_FALSE;
slouken@1895
  1000
slouken@6690
  1001
		result =
slouken@6690
  1002
			IDirectInput8_CreateDevice(dinput,
slouken@6690
  1003
									  &(joystickdevice->dxdevice.guidInstance), &device, NULL);
slouken@6690
  1004
		if (FAILED(result)) {
slouken@6690
  1005
			SetDIerror("IDirectInput::CreateDevice", result);
slouken@6690
  1006
			return (-1);
slouken@6690
  1007
		}
slouken@1895
  1008
slouken@6690
  1009
		/* Now get the IDirectInputDevice2 interface, instead. */
slouken@6690
  1010
		result = IDirectInputDevice8_QueryInterface(device,
slouken@6690
  1011
												   &IID_IDirectInputDevice8,
slouken@6690
  1012
												   (LPVOID *) & joystick->
slouken@6690
  1013
												   hwdata->InputDevice);
slouken@6690
  1014
		/* We are done with this object.  Use the stored one from now on. */
slouken@6690
  1015
		IDirectInputDevice8_Release(device);
slouken@1895
  1016
slouken@6690
  1017
		if (FAILED(result)) {
slouken@6690
  1018
			SetDIerror("IDirectInputDevice::QueryInterface", result);
slouken@6690
  1019
			return (-1);
slouken@6690
  1020
		}
slouken@2198
  1021
slouken@6690
  1022
		/* Aquire shared access. Exclusive access is required for forces,
slouken@6690
  1023
		 * though. */
slouken@6690
  1024
		result =
slouken@6690
  1025
			IDirectInputDevice8_SetCooperativeLevel(joystick->hwdata->
slouken@6690
  1026
													InputDevice, SDL_HelperWindow,
slouken@6690
  1027
													DISCL_NONEXCLUSIVE |
slouken@6690
  1028
													DISCL_BACKGROUND);
slouken@6690
  1029
		if (FAILED(result)) {
slouken@6690
  1030
			SetDIerror("IDirectInputDevice2::SetCooperativeLevel", result);
slouken@6690
  1031
			return (-1);
slouken@6690
  1032
		}
slouken@2198
  1033
slouken@6690
  1034
		/* Use the extended data structure: DIJOYSTATE2. */
slouken@6690
  1035
		result =
slouken@6690
  1036
			IDirectInputDevice8_SetDataFormat(joystick->hwdata->InputDevice,
slouken@6690
  1037
											  &c_dfDIJoystick2);
slouken@6690
  1038
		if (FAILED(result)) {
slouken@6690
  1039
			SetDIerror("IDirectInputDevice2::SetDataFormat", result);
slouken@6690
  1040
			return (-1);
slouken@6690
  1041
		}
slouken@2198
  1042
slouken@6690
  1043
		/* Get device capabilities */
slouken@6690
  1044
		result =
slouken@6690
  1045
			IDirectInputDevice8_GetCapabilities(joystick->hwdata->InputDevice,
slouken@6690
  1046
												&joystick->hwdata->Capabilities);
slouken@2198
  1047
slouken@6690
  1048
		if (FAILED(result)) {
slouken@6690
  1049
			SetDIerror("IDirectInputDevice2::GetCapabilities", result);
slouken@6690
  1050
			return (-1);
slouken@6690
  1051
		}
slouken@2198
  1052
slouken@6690
  1053
		/* Force capable? */
slouken@6690
  1054
		if (joystick->hwdata->Capabilities.dwFlags & DIDC_FORCEFEEDBACK) {
slouken@2198
  1055
slouken@6690
  1056
			result = IDirectInputDevice8_Acquire(joystick->hwdata->InputDevice);
slouken@2198
  1057
slouken@6690
  1058
			if (FAILED(result)) {
slouken@6690
  1059
				SetDIerror("IDirectInputDevice2::Acquire", result);
slouken@6690
  1060
				return (-1);
slouken@6690
  1061
			}
slouken@2198
  1062
slouken@6690
  1063
			/* reset all accuators. */
slouken@6690
  1064
			result =
slouken@6690
  1065
				IDirectInputDevice8_SendForceFeedbackCommand(joystick->hwdata->
slouken@6690
  1066
															 InputDevice,
slouken@6690
  1067
															 DISFFC_RESET);
slouken@2198
  1068
slouken@6690
  1069
			/* Not necessarily supported, ignore if not supported.
slouken@6690
  1070
			if (FAILED(result)) {
slouken@6690
  1071
				SetDIerror("IDirectInputDevice2::SendForceFeedbackCommand",
slouken@6690
  1072
						   result);
slouken@6690
  1073
				return (-1);
slouken@6690
  1074
			}
slouken@6690
  1075
			*/
slouken@1895
  1076
slouken@6690
  1077
			result = IDirectInputDevice8_Unacquire(joystick->hwdata->InputDevice);
slouken@1895
  1078
slouken@6690
  1079
			if (FAILED(result)) {
slouken@6690
  1080
				SetDIerror("IDirectInputDevice2::Unacquire", result);
slouken@6690
  1081
				return (-1);
slouken@6690
  1082
			}
slouken@2198
  1083
slouken@6690
  1084
			/* Turn on auto-centering for a ForceFeedback device (until told
slouken@6690
  1085
			 * otherwise). */
slouken@6690
  1086
			dipdw.diph.dwObj = 0;
slouken@6690
  1087
			dipdw.diph.dwHow = DIPH_DEVICE;
slouken@6690
  1088
			dipdw.dwData = DIPROPAUTOCENTER_ON;
slouken@6220
  1089
slouken@6690
  1090
			result =
slouken@6690
  1091
				IDirectInputDevice8_SetProperty(joystick->hwdata->InputDevice,
slouken@6690
  1092
												DIPROP_AUTOCENTER, &dipdw.diph);
slouken@2198
  1093
slouken@6690
  1094
			/* Not necessarily supported, ignore if not supported.
slouken@6690
  1095
			if (FAILED(result)) {
slouken@6690
  1096
				SetDIerror("IDirectInputDevice2::SetProperty", result);
slouken@6690
  1097
				return (-1);
slouken@6690
  1098
			}
slouken@6690
  1099
			*/
slouken@6690
  1100
		}
slouken@2198
  1101
slouken@6690
  1102
		/* What buttons and axes does it have? */
slouken@6690
  1103
		IDirectInputDevice8_EnumObjects(joystick->hwdata->InputDevice,
slouken@6690
  1104
										EnumDevObjectsCallback, joystick,
slouken@6690
  1105
										DIDFT_BUTTON | DIDFT_AXIS | DIDFT_POV);
slouken@2198
  1106
slouken@6690
  1107
		/* Reorder the input objects. Some devices do not report the X axis as
slouken@6690
  1108
		 * the first axis, for example. */
slouken@6690
  1109
		SortDevObjects(joystick);
slouken@6690
  1110
slouken@6690
  1111
		dipdw.diph.dwObj = 0;
slouken@6690
  1112
		dipdw.diph.dwHow = DIPH_DEVICE;
slouken@6690
  1113
		dipdw.dwData = INPUT_QSIZE;
slouken@6690
  1114
slouken@6690
  1115
		/* Set the buffer size */
slouken@6690
  1116
		result =
slouken@6690
  1117
			IDirectInputDevice8_SetProperty(joystick->hwdata->InputDevice,
slouken@6690
  1118
											DIPROP_BUFFERSIZE, &dipdw.diph);
slouken@6690
  1119
slouken@6690
  1120
		if (result == DI_POLLEDDEVICE) {
slouken@6690
  1121
			/* This device doesn't support buffering, so we're forced
slouken@6690
  1122
			 * to use less reliable polling. */
slouken@6690
  1123
			joystick->hwdata->buffered = 0;
slouken@6690
  1124
		} else if (FAILED(result)) {
slouken@6690
  1125
			SetDIerror("IDirectInputDevice2::SetProperty", result);
slouken@6690
  1126
			return (-1);
slouken@6690
  1127
		}
slouken@6690
  1128
	}
slouken@1895
  1129
    return (0);
slouken@1895
  1130
}
slouken@1895
  1131
slouken@6707
  1132
/* return true if this joystick is plugged in right now */
slouken@6707
  1133
SDL_bool SDL_SYS_JoystickAttached( SDL_Joystick * joystick )
slouken@6707
  1134
{
slouken@6707
  1135
	return joystick->closed == 0 && joystick->hwdata->removed == 0;
slouken@6707
  1136
}
slouken@6707
  1137
slouken@6707
  1138
slouken@6220
  1139
/* Sort using the data offset into the DInput struct.
slouken@6220
  1140
 * This gives a reasonable ordering for the inputs. */
slouken@6220
  1141
static int
slouken@6220
  1142
SortDevFunc(const void *a, const void *b)
slouken@6220
  1143
{
slouken@6220
  1144
	const input_t *inputA = (const input_t*)a;
slouken@6220
  1145
	const input_t *inputB = (const input_t*)b;
slouken@6220
  1146
slouken@6220
  1147
	if (inputA->ofs < inputB->ofs)
slouken@6220
  1148
		return -1;
slouken@6220
  1149
	if (inputA->ofs > inputB->ofs)
slouken@6220
  1150
		return 1;
slouken@6220
  1151
	return 0;
slouken@6220
  1152
}
slouken@6220
  1153
slouken@6220
  1154
/* Sort the input objects and recalculate the indices for each input. */
slouken@6220
  1155
static void
slouken@6220
  1156
SortDevObjects(SDL_Joystick *joystick)
slouken@6220
  1157
{
slouken@6220
  1158
	input_t *inputs = joystick->hwdata->Inputs;
slouken@6220
  1159
	int nButtons = 0;
slouken@6220
  1160
	int nHats = 0;
slouken@6220
  1161
	int nAxis = 0;
slouken@6220
  1162
	int n;
slouken@6220
  1163
slouken@6220
  1164
	SDL_qsort(inputs, joystick->hwdata->NumInputs, sizeof(input_t), SortDevFunc);
slouken@6220
  1165
slouken@6220
  1166
	for (n = 0; n < joystick->hwdata->NumInputs; n++)
slouken@6220
  1167
	{
slouken@6220
  1168
		switch (inputs[n].type)
slouken@6220
  1169
		{
slouken@6220
  1170
		case BUTTON:
slouken@6220
  1171
			inputs[n].num = nButtons;
slouken@6220
  1172
			nButtons++;
slouken@6220
  1173
			break;
slouken@6220
  1174
slouken@6220
  1175
		case HAT:
slouken@6220
  1176
			inputs[n].num = nHats;
slouken@6220
  1177
			nHats++;
slouken@6220
  1178
			break;
slouken@6220
  1179
slouken@6220
  1180
		case AXIS:
slouken@6220
  1181
			inputs[n].num = nAxis;
slouken@6220
  1182
			nAxis++;
slouken@6220
  1183
			break;
slouken@6220
  1184
		}
slouken@6220
  1185
	}
slouken@6220
  1186
}
slouken@6220
  1187
slouken@2198
  1188
static BOOL CALLBACK
slouken@2198
  1189
EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev, LPVOID pvRef)
slouken@2198
  1190
{
slouken@2198
  1191
    SDL_Joystick *joystick = (SDL_Joystick *) pvRef;
slouken@2198
  1192
    HRESULT result;
slouken@2198
  1193
    input_t *in = &joystick->hwdata->Inputs[joystick->hwdata->NumInputs];
slouken@2198
  1194
slouken@2198
  1195
    if (dev->dwType & DIDFT_BUTTON) {
slouken@2198
  1196
        in->type = BUTTON;
slouken@2198
  1197
        in->num = joystick->nbuttons;
slouken@6690
  1198
		in->ofs = DIJOFS_BUTTON( in->num );
slouken@2198
  1199
        joystick->nbuttons++;
slouken@2198
  1200
    } else if (dev->dwType & DIDFT_POV) {
slouken@2198
  1201
        in->type = HAT;
slouken@2198
  1202
        in->num = joystick->nhats;
slouken@6690
  1203
		in->ofs = DIJOFS_POV( in->num );
slouken@2198
  1204
        joystick->nhats++;
slouken@2198
  1205
    } else if (dev->dwType & DIDFT_AXIS) {
slouken@2198
  1206
        DIPROPRANGE diprg;
slouken@2198
  1207
        DIPROPDWORD dilong;
slouken@2198
  1208
slouken@2198
  1209
        in->type = AXIS;
slouken@2198
  1210
        in->num = joystick->naxes;
slouken@6690
  1211
		// work our the axis this guy maps too, thanks for the code icculus!
slouken@6690
  1212
		if ( !SDL_memcmp( &dev->guidType, &GUID_XAxis, sizeof(dev->guidType) ) )
slouken@6690
  1213
			in->ofs = DIJOFS_X;
slouken@6690
  1214
		else if ( !SDL_memcmp( &dev->guidType, &GUID_YAxis, sizeof(dev->guidType) ) )
slouken@6690
  1215
			in->ofs = DIJOFS_Y;
slouken@6690
  1216
		else if ( !SDL_memcmp( &dev->guidType, &GUID_ZAxis, sizeof(dev->guidType) ) )
slouken@6690
  1217
			in->ofs = DIJOFS_Z;
slouken@6690
  1218
		else if ( !SDL_memcmp( &dev->guidType, &GUID_RxAxis, sizeof(dev->guidType) ) )
slouken@6690
  1219
			in->ofs = DIJOFS_RX;
slouken@6690
  1220
		else if ( !SDL_memcmp( &dev->guidType, &GUID_RyAxis, sizeof(dev->guidType) ) )
slouken@6690
  1221
			in->ofs = DIJOFS_RY;
slouken@6690
  1222
		else if ( !SDL_memcmp( &dev->guidType, &GUID_RzAxis, sizeof(dev->guidType) ) )
slouken@6690
  1223
			in->ofs = DIJOFS_RZ;
slouken@6690
  1224
		else if ( !SDL_memcmp( &dev->guidType, &GUID_Slider, sizeof(dev->guidType) ) )
slouken@6690
  1225
		{
slouken@6690
  1226
			in->ofs = DIJOFS_SLIDER( joystick->hwdata->NumSliders );
slouken@6690
  1227
			++joystick->hwdata->NumSliders;
slouken@6690
  1228
		}
slouken@6690
  1229
		else 
slouken@6690
  1230
		{
slouken@6690
  1231
			 return DIENUM_CONTINUE; // not an axis we can grok
slouken@6690
  1232
		}
slouken@2198
  1233
slouken@2198
  1234
        diprg.diph.dwSize = sizeof(diprg);
slouken@2198
  1235
        diprg.diph.dwHeaderSize = sizeof(diprg.diph);
slouken@6690
  1236
        diprg.diph.dwObj = dev->dwType;
slouken@6690
  1237
        diprg.diph.dwHow = DIPH_BYID;
slouken@2198
  1238
        diprg.lMin = AXIS_MIN;
slouken@2198
  1239
        diprg.lMax = AXIS_MAX;
slouken@2198
  1240
slouken@2198
  1241
        result =
slouken@6690
  1242
            IDirectInputDevice8_SetProperty(joystick->hwdata->InputDevice,
slouken@2198
  1243
                                            DIPROP_RANGE, &diprg.diph);
slouken@2198
  1244
        if (FAILED(result)) {
slouken@2198
  1245
            return DIENUM_CONTINUE;     /* don't use this axis */
slouken@2198
  1246
        }
slouken@2198
  1247
slouken@2198
  1248
        /* Set dead zone to 0. */
slouken@2198
  1249
        dilong.diph.dwSize = sizeof(dilong);
slouken@2198
  1250
        dilong.diph.dwHeaderSize = sizeof(dilong.diph);
slouken@6690
  1251
        dilong.diph.dwObj = dev->dwType;
slouken@6690
  1252
        dilong.diph.dwHow = DIPH_BYID;
slouken@2198
  1253
        dilong.dwData = 0;
slouken@2198
  1254
        result =
slouken@6690
  1255
            IDirectInputDevice8_SetProperty(joystick->hwdata->InputDevice,
slouken@2198
  1256
                                            DIPROP_DEADZONE, &dilong.diph);
slouken@2198
  1257
        if (FAILED(result)) {
slouken@2198
  1258
            return DIENUM_CONTINUE;     /* don't use this axis */
slouken@2198
  1259
        }
slouken@2198
  1260
slouken@2198
  1261
        joystick->naxes++;
slouken@2198
  1262
    } else {
slouken@2198
  1263
        /* not supported at this time */
slouken@2198
  1264
        return DIENUM_CONTINUE;
slouken@2198
  1265
    }
slouken@2198
  1266
slouken@2198
  1267
    joystick->hwdata->NumInputs++;
slouken@2198
  1268
slouken@2198
  1269
    if (joystick->hwdata->NumInputs == MAX_INPUTS) {
slouken@2198
  1270
        return DIENUM_STOP;     /* too many */
slouken@2198
  1271
    }
slouken@2198
  1272
slouken@2198
  1273
    return DIENUM_CONTINUE;
slouken@2198
  1274
}
slouken@2198
  1275
slouken@2198
  1276
/* Function to update the state of a joystick - called as a device poll.
slouken@2198
  1277
 * This function shouldn't update the joystick structure directly,
slouken@2198
  1278
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@2198
  1279
 * and update joystick device state.
slouken@2198
  1280
 */
slouken@2198
  1281
void
slouken@2198
  1282
SDL_SYS_JoystickUpdate_Polled(SDL_Joystick * joystick)
slouken@2198
  1283
{
slouken@2198
  1284
    DIJOYSTATE2 state;
slouken@2198
  1285
    HRESULT result;
slouken@2198
  1286
    int i;
slouken@2198
  1287
slouken@2198
  1288
    result =
slouken@6690
  1289
        IDirectInputDevice8_GetDeviceState(joystick->hwdata->InputDevice,
slouken@2198
  1290
                                           sizeof(DIJOYSTATE2), &state);
slouken@2198
  1291
    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
slouken@6690
  1292
        IDirectInputDevice8_Acquire(joystick->hwdata->InputDevice);
slouken@2198
  1293
        result =
slouken@6690
  1294
            IDirectInputDevice8_GetDeviceState(joystick->hwdata->InputDevice,
slouken@2198
  1295
                                               sizeof(DIJOYSTATE2), &state);
slouken@2198
  1296
    }
slouken@2198
  1297
slouken@6690
  1298
	if ( result != DI_OK )
slouken@6690
  1299
	{
slouken@6690
  1300
		joystick->hwdata->send_remove_event = 1;
slouken@6690
  1301
		joystick->hwdata->removed = 1;
slouken@6690
  1302
		return;
slouken@6690
  1303
	}
slouken@6690
  1304
slouken@2198
  1305
    /* Set each known axis, button and POV. */
slouken@2198
  1306
    for (i = 0; i < joystick->hwdata->NumInputs; ++i) {
slouken@2198
  1307
        const input_t *in = &joystick->hwdata->Inputs[i];
slouken@2198
  1308
slouken@2198
  1309
        switch (in->type) {
slouken@2198
  1310
        case AXIS:
slouken@2198
  1311
            switch (in->ofs) {
slouken@2198
  1312
            case DIJOFS_X:
slouken@2198
  1313
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
  1314
                                            (Sint16) state.lX);
slouken@2198
  1315
                break;
slouken@2198
  1316
            case DIJOFS_Y:
slouken@2198
  1317
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
  1318
                                            (Sint16) state.lY);
slouken@2198
  1319
                break;
slouken@2198
  1320
            case DIJOFS_Z:
slouken@2198
  1321
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
  1322
                                            (Sint16) state.lZ);
slouken@2198
  1323
                break;
slouken@2198
  1324
            case DIJOFS_RX:
slouken@2198
  1325
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
  1326
                                            (Sint16) state.lRx);
slouken@2198
  1327
                break;
slouken@2198
  1328
            case DIJOFS_RY:
slouken@2198
  1329
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
  1330
                                            (Sint16) state.lRy);
slouken@2198
  1331
                break;
slouken@2198
  1332
            case DIJOFS_RZ:
slouken@2198
  1333
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
  1334
                                            (Sint16) state.lRz);
slouken@2198
  1335
                break;
slouken@2198
  1336
            case DIJOFS_SLIDER(0):
slouken@2198
  1337
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
  1338
                                            (Sint16) state.rglSlider[0]);
slouken@2198
  1339
                break;
slouken@2198
  1340
            case DIJOFS_SLIDER(1):
slouken@2198
  1341
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
  1342
                                            (Sint16) state.rglSlider[1]);
slouken@2198
  1343
                break;
slouken@2198
  1344
            }
slouken@2198
  1345
slouken@2198
  1346
            break;
slouken@2198
  1347
slouken@2198
  1348
        case BUTTON:
slouken@2198
  1349
            SDL_PrivateJoystickButton_Int(joystick, in->num,
slouken@3013
  1350
                                          (Uint8) (state.
slouken@3013
  1351
                                                   rgbButtons[in->ofs -
slouken@3013
  1352
                                                              DIJOFS_BUTTON0]
slouken@2198
  1353
                                                   ? SDL_PRESSED :
slouken@2198
  1354
                                                   SDL_RELEASED));
slouken@2198
  1355
            break;
slouken@2198
  1356
        case HAT:
slouken@2198
  1357
            {
slouken@2198
  1358
                Uint8 pos = TranslatePOV(state.rgdwPOV[in->ofs -
slouken@2198
  1359
                                                       DIJOFS_POV(0)]);
slouken@2198
  1360
                SDL_PrivateJoystickHat_Int(joystick, in->num, pos);
slouken@2198
  1361
                break;
slouken@2198
  1362
            }
slouken@2198
  1363
        }
slouken@2198
  1364
    }
slouken@2198
  1365
}
slouken@2198
  1366
slouken@2198
  1367
void
slouken@2198
  1368
SDL_SYS_JoystickUpdate_Buffered(SDL_Joystick * joystick)
slouken@2198
  1369
{
slouken@2198
  1370
    int i;
slouken@2198
  1371
    HRESULT result;
slouken@2198
  1372
    DWORD numevents;
slouken@2198
  1373
    DIDEVICEOBJECTDATA evtbuf[INPUT_QSIZE];
slouken@2198
  1374
slouken@2198
  1375
    numevents = INPUT_QSIZE;
slouken@2198
  1376
    result =
slouken@6690
  1377
        IDirectInputDevice8_GetDeviceData(joystick->hwdata->InputDevice,
slouken@2198
  1378
                                          sizeof(DIDEVICEOBJECTDATA), evtbuf,
slouken@2198
  1379
                                          &numevents, 0);
slouken@2198
  1380
    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
slouken@6690
  1381
        IDirectInputDevice8_Acquire(joystick->hwdata->InputDevice);
slouken@2198
  1382
        result =
slouken@6690
  1383
            IDirectInputDevice8_GetDeviceData(joystick->hwdata->InputDevice,
slouken@2198
  1384
                                              sizeof(DIDEVICEOBJECTDATA),
slouken@2198
  1385
                                              evtbuf, &numevents, 0);
slouken@2198
  1386
    }
slouken@2198
  1387
slouken@2198
  1388
    /* Handle the events or punt */
slouken@2198
  1389
    if (FAILED(result))
slouken@6690
  1390
	{
slouken@6690
  1391
		joystick->hwdata->send_remove_event = 1;
slouken@6690
  1392
		joystick->hwdata->removed = 1;
slouken@2198
  1393
        return;
slouken@6690
  1394
	}
slouken@2198
  1395
slouken@2198
  1396
    for (i = 0; i < (int) numevents; ++i) {
slouken@2198
  1397
        int j;
slouken@2198
  1398
slouken@2198
  1399
        for (j = 0; j < joystick->hwdata->NumInputs; ++j) {
slouken@2198
  1400
            const input_t *in = &joystick->hwdata->Inputs[j];
slouken@2198
  1401
slouken@2198
  1402
            if (evtbuf[i].dwOfs != in->ofs)
slouken@2198
  1403
                continue;
slouken@2198
  1404
slouken@2198
  1405
            switch (in->type) {
slouken@2198
  1406
            case AXIS:
slouken@2198
  1407
                SDL_PrivateJoystickAxis(joystick, in->num,
slouken@2198
  1408
                                        (Sint16) evtbuf[i].dwData);
slouken@2198
  1409
                break;
slouken@2198
  1410
            case BUTTON:
slouken@2198
  1411
                SDL_PrivateJoystickButton(joystick, in->num,
slouken@3013
  1412
                                          (Uint8) (evtbuf[i].
slouken@3013
  1413
                                                   dwData ? SDL_PRESSED :
slouken@2198
  1414
                                                   SDL_RELEASED));
slouken@2198
  1415
                break;
slouken@2198
  1416
            case HAT:
slouken@2198
  1417
                {
slouken@2198
  1418
                    Uint8 pos = TranslatePOV(evtbuf[i].dwData);
slouken@2198
  1419
                    SDL_PrivateJoystickHat(joystick, in->num, pos);
slouken@2198
  1420
                }
slouken@2198
  1421
            }
slouken@2198
  1422
        }
slouken@2198
  1423
    }
slouken@2198
  1424
}
slouken@2198
  1425
slouken@2198
  1426
slouken@6690
  1427
/* Function to return > 0 if a bit array of buttons differs after applying a mask
slouken@6690
  1428
*/
slouken@6690
  1429
int ButtonChanged( int ButtonsNow, int ButtonsPrev, int ButtonMask )
slouken@6690
  1430
{
slouken@6690
  1431
	return ( ButtonsNow & ButtonMask ) != ( ButtonsPrev & ButtonMask );
slouken@6690
  1432
}
slouken@6690
  1433
slouken@6690
  1434
/* Function to update the state of a XInput style joystick.
slouken@6690
  1435
*/
slouken@6690
  1436
void
slouken@6690
  1437
SDL_SYS_JoystickUpdate_XInput(SDL_Joystick * joystick)
slouken@6690
  1438
{
slouken@6690
  1439
	HRESULT result;
slouken@6690
  1440
slouken@6690
  1441
	if ( !XINPUTGETSTATE )
slouken@6690
  1442
		return;
slouken@6690
  1443
slouken@6690
  1444
	result = XINPUTGETSTATE( joystick->hwdata->userid, &joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot] );
slouken@6690
  1445
	if ( result == ERROR_DEVICE_NOT_CONNECTED )
slouken@6690
  1446
	{
slouken@6690
  1447
		joystick->hwdata->send_remove_event = 1;
slouken@6690
  1448
		joystick->hwdata->removed = 1;
slouken@6690
  1449
		return;
slouken@6690
  1450
	}
slouken@6690
  1451
slouken@6690
  1452
	// only fire events if the data changed from last time
slouken@6690
  1453
	if ( joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot].dwPacketNumber != 0 
slouken@6690
  1454
		&& joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot].dwPacketNumber != joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot^1].dwPacketNumber )
slouken@6690
  1455
	{
slouken@6690
  1456
		XINPUT_STATE_EX *pXInputState = &joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot];
slouken@6690
  1457
		XINPUT_STATE_EX *pXInputStatePrev = &joystick->hwdata->XInputState[joystick->hwdata->currentXInputSlot ^ 1];
slouken@6690
  1458
slouken@6690
  1459
		SDL_PrivateJoystickAxis(joystick, 0, (Sint16)pXInputState->Gamepad.sThumbLX );
slouken@6690
  1460
		SDL_PrivateJoystickAxis(joystick, 1, (Sint16)(-1*pXInputState->Gamepad.sThumbLY-1) );
slouken@6690
  1461
		SDL_PrivateJoystickAxis(joystick, 2, (Sint16)pXInputState->Gamepad.sThumbRX );
slouken@6690
  1462
		SDL_PrivateJoystickAxis(joystick, 3, (Sint16)(-1*pXInputState->Gamepad.sThumbRY-1) );
slouken@6690
  1463
		SDL_PrivateJoystickAxis(joystick, 4, (Sint16)((int)pXInputState->Gamepad.bLeftTrigger*32767/255) );
slouken@6690
  1464
		SDL_PrivateJoystickAxis(joystick, 5, (Sint16)((int)pXInputState->Gamepad.bRightTrigger*32767/255) );
slouken@6690
  1465
slouken@6690
  1466
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_UP ) )
slouken@6690
  1467
			SDL_PrivateJoystickButton(joystick, 0, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1468
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_DOWN ) )
slouken@6690
  1469
			SDL_PrivateJoystickButton(joystick, 1, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1470
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_LEFT ) )
slouken@6690
  1471
			SDL_PrivateJoystickButton(joystick, 2, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1472
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_DPAD_RIGHT ) )
slouken@6690
  1473
			SDL_PrivateJoystickButton(joystick, 3, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1474
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_START ) )
slouken@6690
  1475
			SDL_PrivateJoystickButton(joystick, 4, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_START ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1476
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_BACK ) )
slouken@6690
  1477
			SDL_PrivateJoystickButton(joystick, 5, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_BACK ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1478
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_LEFT_THUMB ) )
slouken@6690
  1479
			SDL_PrivateJoystickButton(joystick, 6, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1480
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_RIGHT_THUMB ) )
slouken@6690
  1481
			SDL_PrivateJoystickButton(joystick, 7, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1482
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_LEFT_SHOULDER ) )
slouken@6690
  1483
			SDL_PrivateJoystickButton(joystick, 8, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1484
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_RIGHT_SHOULDER ) )
slouken@6690
  1485
			SDL_PrivateJoystickButton(joystick, 9, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1486
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_A ) )
slouken@6690
  1487
			SDL_PrivateJoystickButton(joystick, 10, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_A ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1488
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_B ) )
slouken@6690
  1489
			SDL_PrivateJoystickButton(joystick, 11, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_B ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1490
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_X ) )
slouken@6690
  1491
			SDL_PrivateJoystickButton(joystick, 12, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_X ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1492
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons, XINPUT_GAMEPAD_Y ) )
slouken@6690
  1493
			SDL_PrivateJoystickButton(joystick, 13, pXInputState->Gamepad.wButtons & XINPUT_GAMEPAD_Y ? SDL_PRESSED :	SDL_RELEASED );
slouken@6690
  1494
		if ( ButtonChanged( pXInputState->Gamepad.wButtons, pXInputStatePrev->Gamepad.wButtons,  0x400 ) )
slouken@6690
  1495
			SDL_PrivateJoystickButton(joystick, 14, pXInputState->Gamepad.wButtons & 0x400 ? SDL_PRESSED :	SDL_RELEASED ); // 0x400 is the undocumented code for the guide button
slouken@6690
  1496
slouken@6690
  1497
		joystick->hwdata->currentXInputSlot ^= 1;
slouken@6690
  1498
slouken@6690
  1499
	}
slouken@6690
  1500
}
slouken@6690
  1501
slouken@6690
  1502
slouken@1895
  1503
static Uint8
slouken@1895
  1504
TranslatePOV(DWORD value)
slouken@1895
  1505
{
slouken@1895
  1506
    const int HAT_VALS[] = {
slouken@1895
  1507
        SDL_HAT_UP,
slouken@1895
  1508
        SDL_HAT_UP | SDL_HAT_RIGHT,
slouken@1895
  1509
        SDL_HAT_RIGHT,
slouken@1895
  1510
        SDL_HAT_DOWN | SDL_HAT_RIGHT,
slouken@1895
  1511
        SDL_HAT_DOWN,
slouken@1895
  1512
        SDL_HAT_DOWN | SDL_HAT_LEFT,
slouken@1895
  1513
        SDL_HAT_LEFT,
slouken@1895
  1514
        SDL_HAT_UP | SDL_HAT_LEFT
slouken@1895
  1515
    };
slouken@1895
  1516
slouken@1895
  1517
    if (LOWORD(value) == 0xFFFF)
slouken@1895
  1518
        return SDL_HAT_CENTERED;
slouken@1895
  1519
slouken@1895
  1520
    /* Round the value up: */
slouken@1895
  1521
    value += 4500 / 2;
slouken@1895
  1522
    value %= 36000;
slouken@1895
  1523
    value /= 4500;
slouken@1895
  1524
slouken@1895
  1525
    if (value >= 8)
slouken@1895
  1526
        return SDL_HAT_CENTERED;        /* shouldn't happen */
slouken@1895
  1527
slouken@1895
  1528
    return HAT_VALS[value];
slouken@1895
  1529
}
slouken@1895
  1530
slouken@1895
  1531
/* SDL_PrivateJoystick* doesn't discard duplicate events, so we need to
slouken@1895
  1532
 * do it. */
slouken@1895
  1533
static int
slouken@1895
  1534
SDL_PrivateJoystickAxis_Int(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
slouken@1895
  1535
{
slouken@1895
  1536
    if (joystick->axes[axis] != value)
slouken@1895
  1537
        return SDL_PrivateJoystickAxis(joystick, axis, value);
slouken@1895
  1538
    return 0;
slouken@1895
  1539
}
slouken@1895
  1540
slouken@1895
  1541
static int
slouken@1895
  1542
SDL_PrivateJoystickHat_Int(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
slouken@1895
  1543
{
slouken@1895
  1544
    if (joystick->hats[hat] != value)
slouken@1895
  1545
        return SDL_PrivateJoystickHat(joystick, hat, value);
slouken@1895
  1546
    return 0;
slouken@1895
  1547
}
slouken@1895
  1548
slouken@1895
  1549
static int
slouken@1895
  1550
SDL_PrivateJoystickButton_Int(SDL_Joystick * joystick, Uint8 button,
slouken@1895
  1551
                              Uint8 state)
slouken@1895
  1552
{
slouken@1895
  1553
    if (joystick->buttons[button] != state)
slouken@1895
  1554
        return SDL_PrivateJoystickButton(joystick, button, state);
slouken@1895
  1555
    return 0;
slouken@1895
  1556
}
slouken@1895
  1557
slouken@1895
  1558
void
slouken@1895
  1559
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
slouken@1895
  1560
{
slouken@1895
  1561
    HRESULT result;
slouken@1895
  1562
slouken@6690
  1563
	if ( joystick->closed || !joystick->hwdata )
slouken@6690
  1564
		return;
slouken@1895
  1565
slouken@6690
  1566
	if (joystick->hwdata->bXInputDevice)
slouken@6690
  1567
	{
slouken@6690
  1568
		SDL_SYS_JoystickUpdate_XInput(joystick);
slouken@6690
  1569
	}
slouken@6690
  1570
	else
slouken@6690
  1571
	{
slouken@6690
  1572
		result = IDirectInputDevice8_Poll(joystick->hwdata->InputDevice);
slouken@6690
  1573
		if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
slouken@6690
  1574
			IDirectInputDevice8_Acquire(joystick->hwdata->InputDevice);
slouken@6690
  1575
			IDirectInputDevice8_Poll(joystick->hwdata->InputDevice);
slouken@6690
  1576
		}
slouken@6690
  1577
slouken@6690
  1578
		if (joystick->hwdata->buffered)
slouken@6690
  1579
			SDL_SYS_JoystickUpdate_Buffered(joystick);
slouken@6690
  1580
		else
slouken@6690
  1581
			SDL_SYS_JoystickUpdate_Polled(joystick);
slouken@6690
  1582
	}
slouken@6690
  1583
slouken@6690
  1584
	if ( joystick->hwdata->removed )
slouken@6690
  1585
	{
slouken@6690
  1586
		joystick->closed = 1;
slouken@6690
  1587
		joystick->uncentered = 1;
slouken@6690
  1588
	}
slouken@1895
  1589
}
slouken@1895
  1590
slouken@1895
  1591
/* Function to close a joystick after use */
slouken@1895
  1592
void
slouken@1895
  1593
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
slouken@1895
  1594
{
slouken@6690
  1595
	if ( joystick->hwdata->bXInputDevice )
slouken@6690
  1596
	{
slouken@6690
  1597
		JoyStick_DeviceData *joysticklist = SYS_Joystick;
slouken@6690
  1598
		// scan the opened joysticks and clear the userid for this instance
slouken@6690
  1599
		for( ; joysticklist; joysticklist = joysticklist->pNext)
slouken@6690
  1600
		{
slouken@6690
  1601
			if ( joysticklist->bXInputDevice && joysticklist->nInstanceID == joystick->instance_id )
slouken@6690
  1602
			{
slouken@6690
  1603
				joysticklist->XInputUserId = INVALID_XINPUT_USERID;
slouken@6690
  1604
			}
slouken@6690
  1605
		}
slouken@6690
  1606
slouken@6690
  1607
	}
slouken@6690
  1608
	else
slouken@6690
  1609
	{
slouken@6690
  1610
	    IDirectInputDevice8_Unacquire(joystick->hwdata->InputDevice);
slouken@6690
  1611
	    IDirectInputDevice8_Release(joystick->hwdata->InputDevice);
slouken@6690
  1612
	}
slouken@1895
  1613
slouken@1895
  1614
    if (joystick->hwdata != NULL) {
slouken@1895
  1615
        /* free system specific hardware data */
slouken@2713
  1616
        SDL_free(joystick->hwdata);
slouken@1895
  1617
    }
slouken@6690
  1618
slouken@6690
  1619
	joystick->closed = 1;
slouken@1895
  1620
}
slouken@1895
  1621
slouken@1895
  1622
/* Function to perform any system-specific joystick related cleanup */
slouken@1895
  1623
void
slouken@1895
  1624
SDL_SYS_JoystickQuit(void)
slouken@1895
  1625
{
slouken@6690
  1626
	JoyStick_DeviceData *device = SYS_Joystick;
slouken@5090
  1627
slouken@6690
  1628
	while ( device )
slouken@6690
  1629
	{
slouken@6690
  1630
		JoyStick_DeviceData *device_next = device->pNext;
slouken@6690
  1631
		SDL_free(device->joystickname);
slouken@6690
  1632
		SDL_free(device);
slouken@6690
  1633
		device = device_next;
slouken@6690
  1634
	}
slouken@6690
  1635
	SYS_Joystick = NULL;
slouken@6690
  1636
slouken@6690
  1637
	if ( s_threadJoystick )
slouken@6690
  1638
	{
slouken@6690
  1639
		SDL_LockMutex( s_mutexJoyStickEnum );
slouken@6690
  1640
		s_bJoystickThreadQuit = SDL_TRUE;
slouken@6690
  1641
		SDL_CondBroadcast( s_condJoystickThread ); // signal the joystick thread to quit
slouken@6690
  1642
		SDL_UnlockMutex( s_mutexJoyStickEnum );
slouken@6690
  1643
		SDL_WaitThread( s_threadJoystick, NULL ); // wait for it to bugger off
slouken@6690
  1644
slouken@6690
  1645
		SDL_DestroyMutex( s_mutexJoyStickEnum );
slouken@6690
  1646
		SDL_DestroyCond( s_condJoystickThread );
slouken@6690
  1647
		s_condJoystickThread= NULL;
slouken@6690
  1648
		s_mutexJoyStickEnum = NULL;
slouken@6690
  1649
		s_threadJoystick = NULL;
slouken@6690
  1650
	}
slouken@5090
  1651
icculus@5591
  1652
    if (dinput != NULL) {
slouken@6690
  1653
        IDirectInput8_Release(dinput);
icculus@5591
  1654
        dinput = NULL;
icculus@5591
  1655
    }
icculus@5591
  1656
icculus@5591
  1657
    if (coinitialized) {
icculus@5591
  1658
        WIN_CoUninitialize();
icculus@5591
  1659
        coinitialized = SDL_FALSE;
icculus@5591
  1660
    }
slouken@6690
  1661
slouken@6690
  1662
	if ( s_pKnownJoystickGUIDs )
slouken@6690
  1663
	{
slouken@6690
  1664
		SDL_free( s_pKnownJoystickGUIDs );
slouken@6690
  1665
		s_pKnownJoystickGUIDs = NULL;
slouken@6690
  1666
	}
slouken@6690
  1667
slouken@6690
  1668
	if ( s_pXInputDLL )
slouken@6690
  1669
	{
slouken@6690
  1670
		FreeLibrary( s_pXInputDLL );
slouken@6690
  1671
		s_pXInputDLL = NULL;
slouken@6690
  1672
	}
slouken@6690
  1673
}
slouken@6690
  1674
slouken@6690
  1675
slouken@6690
  1676
/* return the stable device guid for this device index */
slouken@6707
  1677
JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
slouken@6690
  1678
{
slouken@6690
  1679
	JoyStick_DeviceData *device = SYS_Joystick;
slouken@6690
  1680
	int index;
slouken@6690
  1681
slouken@6690
  1682
	for (index = device_index; index > 0; index--)
slouken@6690
  1683
		device = device->pNext;
slouken@6690
  1684
slouken@6690
  1685
	return device->guid;
slouken@6690
  1686
}
slouken@6690
  1687
slouken@6707
  1688
JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
slouken@6707
  1689
{
slouken@6707
  1690
	return joystick->hwdata->guid;
slouken@6707
  1691
}
slouken@6707
  1692
slouken@6707
  1693
/* return SDL_TRUE if this device is using XInput */
slouken@6707
  1694
SDL_bool SDL_SYS_IsXInputDeviceIndex(int device_index)
slouken@6690
  1695
{
slouken@6690
  1696
	JoyStick_DeviceData *device = SYS_Joystick;
slouken@6690
  1697
	int index;
slouken@6690
  1698
slouken@6690
  1699
	for (index = device_index; index > 0; index--)
slouken@6690
  1700
		device = device->pNext;
slouken@6690
  1701
slouken@6690
  1702
	return device->bXInputDevice;
slouken@6690
  1703
}
slouken@6690
  1704
slouken@1895
  1705
#endif /* SDL_JOYSTICK_DINPUT */
slouken@3575
  1706
slouken@1895
  1707
/* vi: set ts=4 sw=4 expandtab: */