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