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