src/joystick/windows/SDL_dxjoystick.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 24 Aug 2012 19:34:28 -0400
changeset 6403 8d34c6248f4a
parent 6220 c36934808194
child 6690 9548c8a58103
permissions -rwxr-xr-x
Fixed a bunch of compiler warnings with Cygwin/MingW.
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@1895
    32
 * with polled devices, and it's fine to call IDirectInputDevice2_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@1895
    39
#include "../SDL_joystick_c.h"
slouken@3013
    40
#define INITGUID /* Only set here, if set twice will cause mingw32 to break. */
slouken@2713
    41
#include "SDL_dxjoystick_c.h"
slouken@2198
    42
slouken@1895
    43
slouken@2761
    44
#ifndef DIDFT_OPTIONAL
slouken@2761
    45
#define DIDFT_OPTIONAL		0x80000000
slouken@2761
    46
#endif
slouken@2761
    47
slouken@2761
    48
slouken@1895
    49
#define INPUT_QSIZE	32      /* Buffer up to 32 input messages */
slouken@1895
    50
#define MAX_JOYSTICKS	8
slouken@1895
    51
#define AXIS_MIN	-32768  /* minimum value for axis coordinate */
slouken@1895
    52
#define AXIS_MAX	32767   /* maximum value for axis coordinate */
slouken@1895
    53
#define JOY_AXIS_THRESHOLD	(((AXIS_MAX)-(AXIS_MIN))/100)   /* 1% motion */
slouken@1895
    54
slouken@2198
    55
/* external variables referenced. */
slouken@2713
    56
extern HWND SDL_HelperWindow;
slouken@2198
    57
slouken@2198
    58
slouken@2198
    59
/* local variables */
icculus@5591
    60
static SDL_bool coinitialized = SDL_FALSE;
slouken@2198
    61
static LPDIRECTINPUT dinput = NULL;
slouken@2198
    62
extern HRESULT(WINAPI * DInputCreate) (HINSTANCE hinst, DWORD dwVersion,
slouken@2198
    63
                                       LPDIRECTINPUT * ppDI,
slouken@2198
    64
                                       LPUNKNOWN punkOuter);
slouken@2198
    65
static DIDEVICEINSTANCE SYS_Joystick[MAX_JOYSTICKS];    /* array to hold joystick ID values */
slouken@5090
    66
static char *SYS_JoystickNames[MAX_JOYSTICKS];
slouken@2198
    67
static int SYS_NumJoysticks;
slouken@2198
    68
slouken@2198
    69
slouken@2198
    70
/* local prototypes */
slouken@2198
    71
static void SetDIerror(const char *function, HRESULT code);
slouken@2198
    72
static BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE *
slouken@2198
    73
                                           pdidInstance, VOID * pContext);
slouken@2198
    74
static BOOL CALLBACK EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev,
slouken@2198
    75
                                            LPVOID pvRef);
slouken@6220
    76
static void SortDevObjects(SDL_Joystick *joystick);
slouken@2198
    77
static Uint8 TranslatePOV(DWORD value);
slouken@2198
    78
static int SDL_PrivateJoystickAxis_Int(SDL_Joystick * joystick, Uint8 axis,
slouken@2198
    79
                                       Sint16 value);
slouken@2198
    80
static int SDL_PrivateJoystickHat_Int(SDL_Joystick * joystick, Uint8 hat,
slouken@2198
    81
                                      Uint8 value);
slouken@2198
    82
static int SDL_PrivateJoystickButton_Int(SDL_Joystick * joystick,
slouken@2198
    83
                                         Uint8 button, Uint8 state);
slouken@2198
    84
slouken@2760
    85
/* Taken from Wine - Thanks! */
slouken@2760
    86
DIOBJECTDATAFORMAT dfDIJoystick2[] = {
slouken@3013
    87
  { &GUID_XAxis,DIJOFS_X,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
    88
  { &GUID_YAxis,DIJOFS_Y,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
    89
  { &GUID_ZAxis,DIJOFS_Z,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
    90
  { &GUID_RxAxis,DIJOFS_RX,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
    91
  { &GUID_RyAxis,DIJOFS_RY,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
    92
  { &GUID_RzAxis,DIJOFS_RZ,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
    93
  { &GUID_Slider,DIJOFS_SLIDER(0),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
    94
  { &GUID_Slider,DIJOFS_SLIDER(1),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
    95
  { &GUID_POV,DIJOFS_POV(0),DIDFT_OPTIONAL|DIDFT_POV|DIDFT_ANYINSTANCE,0},
slouken@3013
    96
  { &GUID_POV,DIJOFS_POV(1),DIDFT_OPTIONAL|DIDFT_POV|DIDFT_ANYINSTANCE,0},
slouken@3013
    97
  { &GUID_POV,DIJOFS_POV(2),DIDFT_OPTIONAL|DIDFT_POV|DIDFT_ANYINSTANCE,0},
slouken@3013
    98
  { &GUID_POV,DIJOFS_POV(3),DIDFT_OPTIONAL|DIDFT_POV|DIDFT_ANYINSTANCE,0},
slouken@3013
    99
  { NULL,DIJOFS_BUTTON(0),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   100
  { NULL,DIJOFS_BUTTON(1),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   101
  { NULL,DIJOFS_BUTTON(2),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   102
  { NULL,DIJOFS_BUTTON(3),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   103
  { NULL,DIJOFS_BUTTON(4),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   104
  { NULL,DIJOFS_BUTTON(5),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   105
  { NULL,DIJOFS_BUTTON(6),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   106
  { NULL,DIJOFS_BUTTON(7),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   107
  { NULL,DIJOFS_BUTTON(8),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   108
  { NULL,DIJOFS_BUTTON(9),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   109
  { NULL,DIJOFS_BUTTON(10),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   110
  { NULL,DIJOFS_BUTTON(11),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   111
  { NULL,DIJOFS_BUTTON(12),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   112
  { NULL,DIJOFS_BUTTON(13),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   113
  { NULL,DIJOFS_BUTTON(14),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   114
  { NULL,DIJOFS_BUTTON(15),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   115
  { NULL,DIJOFS_BUTTON(16),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   116
  { NULL,DIJOFS_BUTTON(17),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   117
  { NULL,DIJOFS_BUTTON(18),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   118
  { NULL,DIJOFS_BUTTON(19),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   119
  { NULL,DIJOFS_BUTTON(20),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   120
  { NULL,DIJOFS_BUTTON(21),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   121
  { NULL,DIJOFS_BUTTON(22),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   122
  { NULL,DIJOFS_BUTTON(23),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   123
  { NULL,DIJOFS_BUTTON(24),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   124
  { NULL,DIJOFS_BUTTON(25),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   125
  { NULL,DIJOFS_BUTTON(26),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   126
  { NULL,DIJOFS_BUTTON(27),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   127
  { NULL,DIJOFS_BUTTON(28),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   128
  { NULL,DIJOFS_BUTTON(29),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   129
  { NULL,DIJOFS_BUTTON(30),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   130
  { NULL,DIJOFS_BUTTON(31),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   131
  { NULL,DIJOFS_BUTTON(32),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   132
  { NULL,DIJOFS_BUTTON(33),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   133
  { NULL,DIJOFS_BUTTON(34),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   134
  { NULL,DIJOFS_BUTTON(35),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   135
  { NULL,DIJOFS_BUTTON(36),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   136
  { NULL,DIJOFS_BUTTON(37),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   137
  { NULL,DIJOFS_BUTTON(38),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   138
  { NULL,DIJOFS_BUTTON(39),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   139
  { NULL,DIJOFS_BUTTON(40),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   140
  { NULL,DIJOFS_BUTTON(41),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   141
  { NULL,DIJOFS_BUTTON(42),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   142
  { NULL,DIJOFS_BUTTON(43),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   143
  { NULL,DIJOFS_BUTTON(44),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   144
  { NULL,DIJOFS_BUTTON(45),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   145
  { NULL,DIJOFS_BUTTON(46),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   146
  { NULL,DIJOFS_BUTTON(47),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   147
  { NULL,DIJOFS_BUTTON(48),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   148
  { NULL,DIJOFS_BUTTON(49),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   149
  { NULL,DIJOFS_BUTTON(50),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   150
  { NULL,DIJOFS_BUTTON(51),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   151
  { NULL,DIJOFS_BUTTON(52),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   152
  { NULL,DIJOFS_BUTTON(53),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   153
  { NULL,DIJOFS_BUTTON(54),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   154
  { NULL,DIJOFS_BUTTON(55),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   155
  { NULL,DIJOFS_BUTTON(56),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   156
  { NULL,DIJOFS_BUTTON(57),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   157
  { NULL,DIJOFS_BUTTON(58),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   158
  { NULL,DIJOFS_BUTTON(59),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   159
  { NULL,DIJOFS_BUTTON(60),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   160
  { NULL,DIJOFS_BUTTON(61),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   161
  { NULL,DIJOFS_BUTTON(62),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   162
  { NULL,DIJOFS_BUTTON(63),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   163
  { NULL,DIJOFS_BUTTON(64),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   164
  { NULL,DIJOFS_BUTTON(65),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   165
  { NULL,DIJOFS_BUTTON(66),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   166
  { NULL,DIJOFS_BUTTON(67),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   167
  { NULL,DIJOFS_BUTTON(68),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   168
  { NULL,DIJOFS_BUTTON(69),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   169
  { NULL,DIJOFS_BUTTON(70),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   170
  { NULL,DIJOFS_BUTTON(71),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   171
  { NULL,DIJOFS_BUTTON(72),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   172
  { NULL,DIJOFS_BUTTON(73),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   173
  { NULL,DIJOFS_BUTTON(74),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   174
  { NULL,DIJOFS_BUTTON(75),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   175
  { NULL,DIJOFS_BUTTON(76),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   176
  { NULL,DIJOFS_BUTTON(77),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   177
  { NULL,DIJOFS_BUTTON(78),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   178
  { NULL,DIJOFS_BUTTON(79),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   179
  { NULL,DIJOFS_BUTTON(80),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   180
  { NULL,DIJOFS_BUTTON(81),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   181
  { NULL,DIJOFS_BUTTON(82),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   182
  { NULL,DIJOFS_BUTTON(83),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   183
  { NULL,DIJOFS_BUTTON(84),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   184
  { NULL,DIJOFS_BUTTON(85),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   185
  { NULL,DIJOFS_BUTTON(86),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   186
  { NULL,DIJOFS_BUTTON(87),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   187
  { NULL,DIJOFS_BUTTON(88),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   188
  { NULL,DIJOFS_BUTTON(89),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   189
  { NULL,DIJOFS_BUTTON(90),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   190
  { NULL,DIJOFS_BUTTON(91),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   191
  { NULL,DIJOFS_BUTTON(92),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   192
  { NULL,DIJOFS_BUTTON(93),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   193
  { NULL,DIJOFS_BUTTON(94),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   194
  { NULL,DIJOFS_BUTTON(95),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   195
  { NULL,DIJOFS_BUTTON(96),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   196
  { NULL,DIJOFS_BUTTON(97),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   197
  { NULL,DIJOFS_BUTTON(98),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   198
  { NULL,DIJOFS_BUTTON(99),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   199
  { NULL,DIJOFS_BUTTON(100),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   200
  { NULL,DIJOFS_BUTTON(101),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   201
  { NULL,DIJOFS_BUTTON(102),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   202
  { NULL,DIJOFS_BUTTON(103),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   203
  { NULL,DIJOFS_BUTTON(104),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   204
  { NULL,DIJOFS_BUTTON(105),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   205
  { NULL,DIJOFS_BUTTON(106),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   206
  { NULL,DIJOFS_BUTTON(107),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   207
  { NULL,DIJOFS_BUTTON(108),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   208
  { NULL,DIJOFS_BUTTON(109),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   209
  { NULL,DIJOFS_BUTTON(110),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   210
  { NULL,DIJOFS_BUTTON(111),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   211
  { NULL,DIJOFS_BUTTON(112),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   212
  { NULL,DIJOFS_BUTTON(113),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   213
  { NULL,DIJOFS_BUTTON(114),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   214
  { NULL,DIJOFS_BUTTON(115),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   215
  { NULL,DIJOFS_BUTTON(116),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   216
  { NULL,DIJOFS_BUTTON(117),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   217
  { NULL,DIJOFS_BUTTON(118),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   218
  { NULL,DIJOFS_BUTTON(119),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   219
  { NULL,DIJOFS_BUTTON(120),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   220
  { NULL,DIJOFS_BUTTON(121),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   221
  { NULL,DIJOFS_BUTTON(122),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   222
  { NULL,DIJOFS_BUTTON(123),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   223
  { NULL,DIJOFS_BUTTON(124),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   224
  { NULL,DIJOFS_BUTTON(125),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   225
  { NULL,DIJOFS_BUTTON(126),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   226
  { NULL,DIJOFS_BUTTON(127),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
slouken@3013
   227
  { &GUID_XAxis,FIELD_OFFSET(DIJOYSTATE2,lVX),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   228
  { &GUID_YAxis,FIELD_OFFSET(DIJOYSTATE2,lVY),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   229
  { &GUID_ZAxis,FIELD_OFFSET(DIJOYSTATE2,lVZ),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   230
  { &GUID_RxAxis,FIELD_OFFSET(DIJOYSTATE2,lVRx),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   231
  { &GUID_RyAxis,FIELD_OFFSET(DIJOYSTATE2,lVRy),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   232
  { &GUID_RzAxis,FIELD_OFFSET(DIJOYSTATE2,lVRz),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   233
  { &GUID_Slider,FIELD_OFFSET(DIJOYSTATE2,rglVSlider[0]),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   234
  { &GUID_Slider,FIELD_OFFSET(DIJOYSTATE2,rglVSlider[1]),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   235
  { &GUID_XAxis,FIELD_OFFSET(DIJOYSTATE2,lAX),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   236
  { &GUID_YAxis,FIELD_OFFSET(DIJOYSTATE2,lAY),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   237
  { &GUID_ZAxis,FIELD_OFFSET(DIJOYSTATE2,lAZ),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   238
  { &GUID_RxAxis,FIELD_OFFSET(DIJOYSTATE2,lARx),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   239
  { &GUID_RyAxis,FIELD_OFFSET(DIJOYSTATE2,lARy),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   240
  { &GUID_RzAxis,FIELD_OFFSET(DIJOYSTATE2,lARz),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   241
  { &GUID_Slider,FIELD_OFFSET(DIJOYSTATE2,rglASlider[0]),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   242
  { &GUID_Slider,FIELD_OFFSET(DIJOYSTATE2,rglASlider[1]),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   243
  { &GUID_XAxis,FIELD_OFFSET(DIJOYSTATE2,lFX),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   244
  { &GUID_YAxis,FIELD_OFFSET(DIJOYSTATE2,lFY),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   245
  { &GUID_ZAxis,FIELD_OFFSET(DIJOYSTATE2,lFZ),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   246
  { &GUID_RxAxis,FIELD_OFFSET(DIJOYSTATE2,lFRx),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   247
  { &GUID_RyAxis,FIELD_OFFSET(DIJOYSTATE2,lFRy),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   248
  { &GUID_RzAxis,FIELD_OFFSET(DIJOYSTATE2,lFRz),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   249
  { &GUID_Slider,FIELD_OFFSET(DIJOYSTATE2,rglFSlider[0]),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@3013
   250
  { &GUID_Slider,FIELD_OFFSET(DIJOYSTATE2,rglFSlider[1]),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
slouken@2760
   251
};
slouken@2760
   252
slouken@2760
   253
const DIDATAFORMAT c_dfDIJoystick2 = {
slouken@2760
   254
    sizeof(DIDATAFORMAT),
slouken@2760
   255
    sizeof(DIOBJECTDATAFORMAT),
slouken@2760
   256
    DIDF_ABSAXIS,
slouken@2760
   257
    sizeof(DIJOYSTATE2),
slouken@2760
   258
    SDL_arraysize(dfDIJoystick2),
slouken@2760
   259
    dfDIJoystick2
slouken@2760
   260
};
slouken@2760
   261
slouken@2198
   262
slouken@1895
   263
/* Convert a DirectInput return code to a text message */
slouken@1895
   264
static void
slouken@2198
   265
SetDIerror(const char *function, HRESULT code)
slouken@1895
   266
{
slouken@2760
   267
    /*
slouken@3013
   268
    SDL_SetError("%s() [%s]: %s", function,
slouken@3013
   269
                 DXGetErrorString9A(code), DXGetErrorDescription9A(code));
slouken@2760
   270
     */
slouken@2760
   271
    SDL_SetError("%s() DirectX error %d", function, code);
slouken@1895
   272
}
slouken@1895
   273
slouken@1895
   274
slouken@1895
   275
/* Function to scan the system for joysticks.
slouken@1895
   276
 * This function should set SDL_numjoysticks to the number of available
slouken@1895
   277
 * joysticks.  Joystick 0 should be the system default joystick.
slouken@1895
   278
 * It should return 0, or -1 on an unrecoverable fatal error.
slouken@1895
   279
 */
slouken@1895
   280
int
slouken@1895
   281
SDL_SYS_JoystickInit(void)
slouken@1895
   282
{
slouken@1895
   283
    HRESULT result;
slouken@2713
   284
    HINSTANCE instance;
slouken@1895
   285
slouken@1895
   286
    SYS_NumJoysticks = 0;
slouken@1895
   287
icculus@5591
   288
    result = WIN_CoInitialize();
slouken@2198
   289
    if (FAILED(result)) {
slouken@2198
   290
        SetDIerror("CoInitialize", result);
slouken@1895
   291
        return (-1);
slouken@1895
   292
    }
slouken@1895
   293
icculus@5591
   294
    coinitialized = SDL_TRUE;
icculus@5591
   295
slouken@2198
   296
    result = CoCreateInstance(&CLSID_DirectInput, NULL, CLSCTX_INPROC_SERVER,
slouken@3013
   297
                              &IID_IDirectInput, (LPVOID)&dinput);
slouken@2198
   298
slouken@2198
   299
    if (FAILED(result)) {
icculus@5591
   300
        SDL_SYS_JoystickQuit();
slouken@2198
   301
        SetDIerror("CoCreateInstance", result);
slouken@2198
   302
        return (-1);
slouken@2198
   303
    }
slouken@2198
   304
slouken@2198
   305
    /* Because we used CoCreateInstance, we need to Initialize it, first. */
slouken@2713
   306
    instance = GetModuleHandle(NULL);
slouken@2713
   307
    if (instance == NULL) {
icculus@5591
   308
        SDL_SYS_JoystickQuit();
slouken@2713
   309
        SDL_SetError("GetModuleHandle() failed with error code %d.",
slouken@2713
   310
                     GetLastError());
slouken@2713
   311
        return (-1);
slouken@2713
   312
    }
slouken@2713
   313
    result = IDirectInput_Initialize(dinput, instance, DIRECTINPUT_VERSION);
slouken@2198
   314
slouken@2198
   315
    if (FAILED(result)) {
icculus@5591
   316
        SDL_SYS_JoystickQuit();
slouken@2198
   317
        SetDIerror("IDirectInput::Initialize", result);
slouken@2198
   318
        return (-1);
slouken@2198
   319
    }
slouken@2198
   320
slouken@2198
   321
    /* Look for joysticks, wheels, head trackers, gamepads, etc.. */
slouken@1895
   322
    result = IDirectInput_EnumDevices(dinput,
slouken@1895
   323
                                      DIDEVTYPE_JOYSTICK,
slouken@1895
   324
                                      EnumJoysticksCallback,
slouken@1895
   325
                                      NULL, DIEDFL_ATTACHEDONLY);
slouken@1895
   326
slouken@1895
   327
    return SYS_NumJoysticks;
slouken@1895
   328
}
slouken@1895
   329
slouken@2198
   330
static BOOL CALLBACK
slouken@2198
   331
EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
slouken@2198
   332
{
slouken@2713
   333
    SDL_memcpy(&SYS_Joystick[SYS_NumJoysticks], pdidInstance,
slouken@2713
   334
               sizeof(DIDEVICEINSTANCE));
slouken@5090
   335
    SYS_JoystickNames[SYS_NumJoysticks] = WIN_StringToUTF8(pdidInstance->tszProductName);
slouken@2198
   336
    SYS_NumJoysticks++;
slouken@2198
   337
slouken@2198
   338
    if (SYS_NumJoysticks >= MAX_JOYSTICKS)
slouken@2198
   339
        return DIENUM_STOP;
slouken@2198
   340
slouken@2198
   341
    return DIENUM_CONTINUE;
slouken@2198
   342
}
slouken@2198
   343
slouken@1895
   344
/* Function to get the device-dependent name of a joystick */
slouken@1895
   345
const char *
slouken@1895
   346
SDL_SYS_JoystickName(int index)
slouken@1895
   347
{
slouken@5090
   348
    return SYS_JoystickNames[index];
slouken@1895
   349
}
slouken@1895
   350
slouken@1895
   351
/* Function to open a joystick for use.
slouken@1895
   352
   The joystick to open is specified by the index field of the joystick.
slouken@1895
   353
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@1895
   354
   It returns 0, or -1 if there is an error.
slouken@1895
   355
 */
slouken@1895
   356
int
slouken@1895
   357
SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
slouken@1895
   358
{
slouken@1895
   359
    HRESULT result;
slouken@1895
   360
    LPDIRECTINPUTDEVICE device;
slouken@2198
   361
    DIPROPDWORD dipdw;
slouken@2198
   362
slouken@2713
   363
    SDL_memset(&dipdw, 0, sizeof(DIPROPDWORD));
slouken@2198
   364
    dipdw.diph.dwSize = sizeof(DIPROPDWORD);
slouken@2198
   365
    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
slouken@2198
   366
slouken@1895
   367
slouken@1895
   368
    /* allocate memory for system specific hardware data */
slouken@1895
   369
    joystick->hwdata =
slouken@2713
   370
        (struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata));
slouken@1895
   371
    if (joystick->hwdata == NULL) {
slouken@1895
   372
        SDL_OutOfMemory();
slouken@1895
   373
        return (-1);
slouken@1895
   374
    }
slouken@2713
   375
    SDL_memset(joystick->hwdata, 0, sizeof(struct joystick_hwdata));
slouken@1895
   376
    joystick->hwdata->buffered = 1;
slouken@2198
   377
    joystick->hwdata->Capabilities.dwSize = sizeof(DIDEVCAPS);
slouken@1895
   378
slouken@1895
   379
    result =
slouken@1895
   380
        IDirectInput_CreateDevice(dinput,
slouken@3013
   381
                                  &SYS_Joystick[joystick->index].
slouken@3013
   382
                                  guidInstance, &device, NULL);
slouken@2198
   383
    if (FAILED(result)) {
slouken@2198
   384
        SetDIerror("IDirectInput::CreateDevice", result);
slouken@1895
   385
        return (-1);
slouken@1895
   386
    }
slouken@1895
   387
slouken@2198
   388
    /* Now get the IDirectInputDevice2 interface, instead. */
slouken@1895
   389
    result = IDirectInputDevice_QueryInterface(device,
slouken@1895
   390
                                               &IID_IDirectInputDevice2,
slouken@3013
   391
                                               (LPVOID *) & joystick->
slouken@3013
   392
                                               hwdata->InputDevice);
slouken@2198
   393
    /* We are done with this object.  Use the stored one from now on. */
slouken@1895
   394
    IDirectInputDevice_Release(device);
slouken@2198
   395
slouken@2198
   396
    if (FAILED(result)) {
slouken@2198
   397
        SetDIerror("IDirectInputDevice::QueryInterface", result);
slouken@1895
   398
        return (-1);
slouken@1895
   399
    }
slouken@1895
   400
slouken@2198
   401
    /* Aquire shared access. Exclusive access is required for forces,
slouken@2198
   402
     * though. */
slouken@1895
   403
    result =
slouken@3013
   404
        IDirectInputDevice2_SetCooperativeLevel(joystick->hwdata->
slouken@3013
   405
                                                InputDevice, SDL_HelperWindow,
slouken@2198
   406
                                                DISCL_EXCLUSIVE |
slouken@1895
   407
                                                DISCL_BACKGROUND);
slouken@2198
   408
    if (FAILED(result)) {
slouken@2198
   409
        SetDIerror("IDirectInputDevice2::SetCooperativeLevel", result);
slouken@1895
   410
        return (-1);
slouken@1895
   411
    }
slouken@1895
   412
slouken@2198
   413
    /* Use the extended data structure: DIJOYSTATE2. */
slouken@1895
   414
    result =
slouken@1895
   415
        IDirectInputDevice2_SetDataFormat(joystick->hwdata->InputDevice,
slouken@2198
   416
                                          &c_dfDIJoystick2);
slouken@2198
   417
    if (FAILED(result)) {
slouken@2198
   418
        SetDIerror("IDirectInputDevice2::SetDataFormat", result);
slouken@1895
   419
        return (-1);
slouken@1895
   420
    }
slouken@1895
   421
slouken@2198
   422
    /* Get device capabilities */
slouken@2198
   423
    result =
slouken@2198
   424
        IDirectInputDevice2_GetCapabilities(joystick->hwdata->InputDevice,
slouken@2198
   425
                                            &joystick->hwdata->Capabilities);
slouken@1895
   426
slouken@2198
   427
    if (FAILED(result)) {
slouken@2198
   428
        SetDIerror("IDirectInputDevice2::GetCapabilities", result);
slouken@2198
   429
        return (-1);
slouken@2198
   430
    }
slouken@2198
   431
slouken@2198
   432
    /* Force capable? */
slouken@2198
   433
    if (joystick->hwdata->Capabilities.dwFlags & DIDC_FORCEFEEDBACK) {
slouken@2198
   434
slouken@2198
   435
        result = IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
slouken@2198
   436
slouken@2198
   437
        if (FAILED(result)) {
slouken@2198
   438
            SetDIerror("IDirectInputDevice2::Acquire", result);
slouken@2198
   439
            return (-1);
slouken@2198
   440
        }
slouken@2198
   441
slouken@2198
   442
        /* reset all accuators. */
slouken@2198
   443
        result =
slouken@3013
   444
            IDirectInputDevice2_SendForceFeedbackCommand(joystick->hwdata->
slouken@3013
   445
                                                         InputDevice,
slouken@2198
   446
                                                         DISFFC_RESET);
slouken@2198
   447
bobbens@4423
   448
        /* Not necessarily supported, ignore if not supported.
slouken@2198
   449
        if (FAILED(result)) {
slouken@2198
   450
            SetDIerror("IDirectInputDevice2::SendForceFeedbackCommand",
slouken@2198
   451
                       result);
slouken@2198
   452
            return (-1);
slouken@2198
   453
        }
bobbens@4423
   454
        */
slouken@2198
   455
slouken@2198
   456
        result = IDirectInputDevice2_Unacquire(joystick->hwdata->InputDevice);
slouken@2198
   457
slouken@2198
   458
        if (FAILED(result)) {
slouken@2198
   459
            SetDIerror("IDirectInputDevice2::Unacquire", result);
slouken@2198
   460
            return (-1);
slouken@2198
   461
        }
slouken@2198
   462
slouken@2198
   463
        /* Turn on auto-centering for a ForceFeedback device (until told
slouken@2198
   464
         * otherwise). */
slouken@1895
   465
        dipdw.diph.dwObj = 0;
slouken@1895
   466
        dipdw.diph.dwHow = DIPH_DEVICE;
slouken@2198
   467
        dipdw.dwData = DIPROPAUTOCENTER_ON;
slouken@2198
   468
slouken@1895
   469
        result =
slouken@1895
   470
            IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
slouken@2198
   471
                                            DIPROP_AUTOCENTER, &dipdw.diph);
slouken@1895
   472
bobbens@4423
   473
        /* Not necessarily supported, ignore if not supported.
slouken@2198
   474
        if (FAILED(result)) {
slouken@2198
   475
            SetDIerror("IDirectInputDevice2::SetProperty", result);
slouken@1895
   476
            return (-1);
slouken@1895
   477
        }
bobbens@4423
   478
        */
slouken@1895
   479
    }
slouken@1895
   480
slouken@2198
   481
    /* What buttons and axes does it have? */
slouken@2198
   482
    IDirectInputDevice2_EnumObjects(joystick->hwdata->InputDevice,
slouken@2198
   483
                                    EnumDevObjectsCallback, joystick,
slouken@2198
   484
                                    DIDFT_BUTTON | DIDFT_AXIS | DIDFT_POV);
slouken@2198
   485
slouken@6220
   486
	/* Reorder the input objects. Some devices do not report the X axis as
slouken@6220
   487
	 * the first axis, for example. */
slouken@6220
   488
	SortDevObjects(joystick);
slouken@6220
   489
slouken@2198
   490
    dipdw.diph.dwObj = 0;
slouken@2198
   491
    dipdw.diph.dwHow = DIPH_DEVICE;
slouken@2198
   492
    dipdw.dwData = INPUT_QSIZE;
slouken@2198
   493
slouken@2198
   494
    /* Set the buffer size */
slouken@2198
   495
    result =
slouken@2198
   496
        IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
slouken@2198
   497
                                        DIPROP_BUFFERSIZE, &dipdw.diph);
slouken@2198
   498
slouken@2198
   499
    if (result == DI_POLLEDDEVICE) {
slouken@2198
   500
        /* This device doesn't support buffering, so we're forced
slouken@2198
   501
         * to use less reliable polling. */
slouken@2198
   502
        joystick->hwdata->buffered = 0;
slouken@2198
   503
    } else if (FAILED(result)) {
slouken@2198
   504
        SetDIerror("IDirectInputDevice2::SetProperty", result);
slouken@2198
   505
        return (-1);
slouken@2198
   506
    }
slouken@2198
   507
slouken@1895
   508
    return (0);
slouken@1895
   509
}
slouken@1895
   510
slouken@6220
   511
/* Sort using the data offset into the DInput struct.
slouken@6220
   512
 * This gives a reasonable ordering for the inputs. */
slouken@6220
   513
static int
slouken@6220
   514
SortDevFunc(const void *a, const void *b)
slouken@6220
   515
{
slouken@6220
   516
	const input_t *inputA = (const input_t*)a;
slouken@6220
   517
	const input_t *inputB = (const input_t*)b;
slouken@6220
   518
slouken@6220
   519
	if (inputA->ofs < inputB->ofs)
slouken@6220
   520
		return -1;
slouken@6220
   521
	if (inputA->ofs > inputB->ofs)
slouken@6220
   522
		return 1;
slouken@6220
   523
	return 0;
slouken@6220
   524
}
slouken@6220
   525
slouken@6220
   526
/* Sort the input objects and recalculate the indices for each input. */
slouken@6220
   527
static void
slouken@6220
   528
SortDevObjects(SDL_Joystick *joystick)
slouken@6220
   529
{
slouken@6220
   530
	input_t *inputs = joystick->hwdata->Inputs;
slouken@6220
   531
	int nButtons = 0;
slouken@6220
   532
	int nHats = 0;
slouken@6220
   533
	int nAxis = 0;
slouken@6220
   534
	int n;
slouken@6220
   535
slouken@6220
   536
	SDL_qsort(inputs, joystick->hwdata->NumInputs, sizeof(input_t), SortDevFunc);
slouken@6220
   537
slouken@6220
   538
	for (n = 0; n < joystick->hwdata->NumInputs; n++)
slouken@6220
   539
	{
slouken@6220
   540
		switch (inputs[n].type)
slouken@6220
   541
		{
slouken@6220
   542
		case BUTTON:
slouken@6220
   543
			inputs[n].num = nButtons;
slouken@6220
   544
			nButtons++;
slouken@6220
   545
			break;
slouken@6220
   546
slouken@6220
   547
		case HAT:
slouken@6220
   548
			inputs[n].num = nHats;
slouken@6220
   549
			nHats++;
slouken@6220
   550
			break;
slouken@6220
   551
slouken@6220
   552
		case AXIS:
slouken@6220
   553
			inputs[n].num = nAxis;
slouken@6220
   554
			nAxis++;
slouken@6220
   555
			break;
slouken@6220
   556
		}
slouken@6220
   557
	}
slouken@6220
   558
}
slouken@6220
   559
slouken@2198
   560
static BOOL CALLBACK
slouken@2198
   561
EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev, LPVOID pvRef)
slouken@2198
   562
{
slouken@2198
   563
    SDL_Joystick *joystick = (SDL_Joystick *) pvRef;
slouken@2198
   564
    HRESULT result;
slouken@2198
   565
    input_t *in = &joystick->hwdata->Inputs[joystick->hwdata->NumInputs];
slouken@2198
   566
slouken@2198
   567
    in->ofs = dev->dwOfs;
slouken@2198
   568
slouken@2198
   569
    if (dev->dwType & DIDFT_BUTTON) {
slouken@2198
   570
        in->type = BUTTON;
slouken@2198
   571
        in->num = joystick->nbuttons;
slouken@2198
   572
        joystick->nbuttons++;
slouken@2198
   573
    } else if (dev->dwType & DIDFT_POV) {
slouken@2198
   574
        in->type = HAT;
slouken@2198
   575
        in->num = joystick->nhats;
slouken@2198
   576
        joystick->nhats++;
slouken@2198
   577
    } else if (dev->dwType & DIDFT_AXIS) {
slouken@2198
   578
        DIPROPRANGE diprg;
slouken@2198
   579
        DIPROPDWORD dilong;
slouken@2198
   580
slouken@2198
   581
        in->type = AXIS;
slouken@2198
   582
        in->num = joystick->naxes;
slouken@2198
   583
slouken@2198
   584
        diprg.diph.dwSize = sizeof(diprg);
slouken@2198
   585
        diprg.diph.dwHeaderSize = sizeof(diprg.diph);
slouken@2198
   586
        diprg.diph.dwObj = dev->dwOfs;
slouken@2198
   587
        diprg.diph.dwHow = DIPH_BYOFFSET;
slouken@2198
   588
        diprg.lMin = AXIS_MIN;
slouken@2198
   589
        diprg.lMax = AXIS_MAX;
slouken@2198
   590
slouken@2198
   591
        result =
slouken@2198
   592
            IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
slouken@2198
   593
                                            DIPROP_RANGE, &diprg.diph);
slouken@2198
   594
        if (FAILED(result)) {
slouken@2198
   595
            return DIENUM_CONTINUE;     /* don't use this axis */
slouken@2198
   596
        }
slouken@2198
   597
slouken@2198
   598
        /* Set dead zone to 0. */
slouken@2198
   599
        dilong.diph.dwSize = sizeof(dilong);
slouken@2198
   600
        dilong.diph.dwHeaderSize = sizeof(dilong.diph);
slouken@2198
   601
        dilong.diph.dwObj = dev->dwOfs;
slouken@2198
   602
        dilong.diph.dwHow = DIPH_BYOFFSET;
slouken@2198
   603
        dilong.dwData = 0;
slouken@2198
   604
        result =
slouken@2198
   605
            IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
slouken@2198
   606
                                            DIPROP_DEADZONE, &dilong.diph);
slouken@2198
   607
        if (FAILED(result)) {
slouken@2198
   608
            return DIENUM_CONTINUE;     /* don't use this axis */
slouken@2198
   609
        }
slouken@2198
   610
slouken@2198
   611
        joystick->naxes++;
slouken@2198
   612
    } else {
slouken@2198
   613
        /* not supported at this time */
slouken@2198
   614
        return DIENUM_CONTINUE;
slouken@2198
   615
    }
slouken@2198
   616
slouken@2198
   617
    joystick->hwdata->NumInputs++;
slouken@2198
   618
slouken@2198
   619
    if (joystick->hwdata->NumInputs == MAX_INPUTS) {
slouken@2198
   620
        return DIENUM_STOP;     /* too many */
slouken@2198
   621
    }
slouken@2198
   622
slouken@2198
   623
    return DIENUM_CONTINUE;
slouken@2198
   624
}
slouken@2198
   625
slouken@2198
   626
/* Function to update the state of a joystick - called as a device poll.
slouken@2198
   627
 * This function shouldn't update the joystick structure directly,
slouken@2198
   628
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@2198
   629
 * and update joystick device state.
slouken@2198
   630
 */
slouken@2198
   631
void
slouken@2198
   632
SDL_SYS_JoystickUpdate_Polled(SDL_Joystick * joystick)
slouken@2198
   633
{
slouken@2198
   634
    DIJOYSTATE2 state;
slouken@2198
   635
    HRESULT result;
slouken@2198
   636
    int i;
slouken@2198
   637
slouken@2198
   638
    result =
slouken@2198
   639
        IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice,
slouken@2198
   640
                                           sizeof(DIJOYSTATE2), &state);
slouken@2198
   641
    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
slouken@2198
   642
        IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
slouken@2198
   643
        result =
slouken@2198
   644
            IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice,
slouken@2198
   645
                                               sizeof(DIJOYSTATE2), &state);
slouken@2198
   646
    }
slouken@2198
   647
slouken@2198
   648
    /* Set each known axis, button and POV. */
slouken@2198
   649
    for (i = 0; i < joystick->hwdata->NumInputs; ++i) {
slouken@2198
   650
        const input_t *in = &joystick->hwdata->Inputs[i];
slouken@2198
   651
slouken@2198
   652
        switch (in->type) {
slouken@2198
   653
        case AXIS:
slouken@2198
   654
            switch (in->ofs) {
slouken@2198
   655
            case DIJOFS_X:
slouken@2198
   656
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   657
                                            (Sint16) state.lX);
slouken@2198
   658
                break;
slouken@2198
   659
            case DIJOFS_Y:
slouken@2198
   660
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   661
                                            (Sint16) state.lY);
slouken@2198
   662
                break;
slouken@2198
   663
            case DIJOFS_Z:
slouken@2198
   664
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   665
                                            (Sint16) state.lZ);
slouken@2198
   666
                break;
slouken@2198
   667
            case DIJOFS_RX:
slouken@2198
   668
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   669
                                            (Sint16) state.lRx);
slouken@2198
   670
                break;
slouken@2198
   671
            case DIJOFS_RY:
slouken@2198
   672
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   673
                                            (Sint16) state.lRy);
slouken@2198
   674
                break;
slouken@2198
   675
            case DIJOFS_RZ:
slouken@2198
   676
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   677
                                            (Sint16) state.lRz);
slouken@2198
   678
                break;
slouken@2198
   679
            case DIJOFS_SLIDER(0):
slouken@2198
   680
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   681
                                            (Sint16) state.rglSlider[0]);
slouken@2198
   682
                break;
slouken@2198
   683
            case DIJOFS_SLIDER(1):
slouken@2198
   684
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   685
                                            (Sint16) state.rglSlider[1]);
slouken@2198
   686
                break;
slouken@2198
   687
            }
slouken@2198
   688
slouken@2198
   689
            break;
slouken@2198
   690
slouken@2198
   691
        case BUTTON:
slouken@2198
   692
            SDL_PrivateJoystickButton_Int(joystick, in->num,
slouken@3013
   693
                                          (Uint8) (state.
slouken@3013
   694
                                                   rgbButtons[in->ofs -
slouken@3013
   695
                                                              DIJOFS_BUTTON0]
slouken@2198
   696
                                                   ? SDL_PRESSED :
slouken@2198
   697
                                                   SDL_RELEASED));
slouken@2198
   698
            break;
slouken@2198
   699
        case HAT:
slouken@2198
   700
            {
slouken@2198
   701
                Uint8 pos = TranslatePOV(state.rgdwPOV[in->ofs -
slouken@2198
   702
                                                       DIJOFS_POV(0)]);
slouken@2198
   703
                SDL_PrivateJoystickHat_Int(joystick, in->num, pos);
slouken@2198
   704
                break;
slouken@2198
   705
            }
slouken@2198
   706
        }
slouken@2198
   707
    }
slouken@2198
   708
}
slouken@2198
   709
slouken@2198
   710
void
slouken@2198
   711
SDL_SYS_JoystickUpdate_Buffered(SDL_Joystick * joystick)
slouken@2198
   712
{
slouken@2198
   713
    int i;
slouken@2198
   714
    HRESULT result;
slouken@2198
   715
    DWORD numevents;
slouken@2198
   716
    DIDEVICEOBJECTDATA evtbuf[INPUT_QSIZE];
slouken@2198
   717
slouken@2198
   718
    numevents = INPUT_QSIZE;
slouken@2198
   719
    result =
slouken@2198
   720
        IDirectInputDevice2_GetDeviceData(joystick->hwdata->InputDevice,
slouken@2198
   721
                                          sizeof(DIDEVICEOBJECTDATA), evtbuf,
slouken@2198
   722
                                          &numevents, 0);
slouken@2198
   723
    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
slouken@2198
   724
        IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
slouken@2198
   725
        result =
slouken@2198
   726
            IDirectInputDevice2_GetDeviceData(joystick->hwdata->InputDevice,
slouken@2198
   727
                                              sizeof(DIDEVICEOBJECTDATA),
slouken@2198
   728
                                              evtbuf, &numevents, 0);
slouken@2198
   729
    }
slouken@2198
   730
slouken@2198
   731
    /* Handle the events or punt */
slouken@2198
   732
    if (FAILED(result))
slouken@2198
   733
        return;
slouken@2198
   734
slouken@2198
   735
    for (i = 0; i < (int) numevents; ++i) {
slouken@2198
   736
        int j;
slouken@2198
   737
slouken@2198
   738
        for (j = 0; j < joystick->hwdata->NumInputs; ++j) {
slouken@2198
   739
            const input_t *in = &joystick->hwdata->Inputs[j];
slouken@2198
   740
slouken@2198
   741
            if (evtbuf[i].dwOfs != in->ofs)
slouken@2198
   742
                continue;
slouken@2198
   743
slouken@2198
   744
            switch (in->type) {
slouken@2198
   745
            case AXIS:
slouken@2198
   746
                SDL_PrivateJoystickAxis(joystick, in->num,
slouken@2198
   747
                                        (Sint16) evtbuf[i].dwData);
slouken@2198
   748
                break;
slouken@2198
   749
            case BUTTON:
slouken@2198
   750
                SDL_PrivateJoystickButton(joystick, in->num,
slouken@3013
   751
                                          (Uint8) (evtbuf[i].
slouken@3013
   752
                                                   dwData ? SDL_PRESSED :
slouken@2198
   753
                                                   SDL_RELEASED));
slouken@2198
   754
                break;
slouken@2198
   755
            case HAT:
slouken@2198
   756
                {
slouken@2198
   757
                    Uint8 pos = TranslatePOV(evtbuf[i].dwData);
slouken@2198
   758
                    SDL_PrivateJoystickHat(joystick, in->num, pos);
slouken@2198
   759
                }
slouken@2198
   760
            }
slouken@2198
   761
        }
slouken@2198
   762
    }
slouken@2198
   763
}
slouken@2198
   764
slouken@2198
   765
slouken@1895
   766
static Uint8
slouken@1895
   767
TranslatePOV(DWORD value)
slouken@1895
   768
{
slouken@1895
   769
    const int HAT_VALS[] = {
slouken@1895
   770
        SDL_HAT_UP,
slouken@1895
   771
        SDL_HAT_UP | SDL_HAT_RIGHT,
slouken@1895
   772
        SDL_HAT_RIGHT,
slouken@1895
   773
        SDL_HAT_DOWN | SDL_HAT_RIGHT,
slouken@1895
   774
        SDL_HAT_DOWN,
slouken@1895
   775
        SDL_HAT_DOWN | SDL_HAT_LEFT,
slouken@1895
   776
        SDL_HAT_LEFT,
slouken@1895
   777
        SDL_HAT_UP | SDL_HAT_LEFT
slouken@1895
   778
    };
slouken@1895
   779
slouken@1895
   780
    if (LOWORD(value) == 0xFFFF)
slouken@1895
   781
        return SDL_HAT_CENTERED;
slouken@1895
   782
slouken@1895
   783
    /* Round the value up: */
slouken@1895
   784
    value += 4500 / 2;
slouken@1895
   785
    value %= 36000;
slouken@1895
   786
    value /= 4500;
slouken@1895
   787
slouken@1895
   788
    if (value >= 8)
slouken@1895
   789
        return SDL_HAT_CENTERED;        /* shouldn't happen */
slouken@1895
   790
slouken@1895
   791
    return HAT_VALS[value];
slouken@1895
   792
}
slouken@1895
   793
slouken@1895
   794
/* SDL_PrivateJoystick* doesn't discard duplicate events, so we need to
slouken@1895
   795
 * do it. */
slouken@1895
   796
static int
slouken@1895
   797
SDL_PrivateJoystickAxis_Int(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
slouken@1895
   798
{
slouken@1895
   799
    if (joystick->axes[axis] != value)
slouken@1895
   800
        return SDL_PrivateJoystickAxis(joystick, axis, value);
slouken@1895
   801
    return 0;
slouken@1895
   802
}
slouken@1895
   803
slouken@1895
   804
static int
slouken@1895
   805
SDL_PrivateJoystickHat_Int(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
slouken@1895
   806
{
slouken@1895
   807
    if (joystick->hats[hat] != value)
slouken@1895
   808
        return SDL_PrivateJoystickHat(joystick, hat, value);
slouken@1895
   809
    return 0;
slouken@1895
   810
}
slouken@1895
   811
slouken@1895
   812
static int
slouken@1895
   813
SDL_PrivateJoystickButton_Int(SDL_Joystick * joystick, Uint8 button,
slouken@1895
   814
                              Uint8 state)
slouken@1895
   815
{
slouken@1895
   816
    if (joystick->buttons[button] != state)
slouken@1895
   817
        return SDL_PrivateJoystickButton(joystick, button, state);
slouken@1895
   818
    return 0;
slouken@1895
   819
}
slouken@1895
   820
slouken@1895
   821
void
slouken@1895
   822
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
slouken@1895
   823
{
slouken@1895
   824
    HRESULT result;
slouken@1895
   825
slouken@1895
   826
    result = IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
slouken@1895
   827
    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
slouken@1895
   828
        IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
slouken@1895
   829
        IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
slouken@1895
   830
    }
slouken@1895
   831
slouken@1895
   832
    if (joystick->hwdata->buffered)
slouken@1895
   833
        SDL_SYS_JoystickUpdate_Buffered(joystick);
slouken@1895
   834
    else
slouken@1895
   835
        SDL_SYS_JoystickUpdate_Polled(joystick);
slouken@1895
   836
}
slouken@1895
   837
slouken@1895
   838
/* Function to close a joystick after use */
slouken@1895
   839
void
slouken@1895
   840
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
slouken@1895
   841
{
slouken@1895
   842
    IDirectInputDevice2_Unacquire(joystick->hwdata->InputDevice);
slouken@1895
   843
    IDirectInputDevice2_Release(joystick->hwdata->InputDevice);
slouken@1895
   844
slouken@1895
   845
    if (joystick->hwdata != NULL) {
slouken@1895
   846
        /* free system specific hardware data */
slouken@2713
   847
        SDL_free(joystick->hwdata);
slouken@1895
   848
    }
slouken@1895
   849
}
slouken@1895
   850
slouken@1895
   851
/* Function to perform any system-specific joystick related cleanup */
slouken@1895
   852
void
slouken@1895
   853
SDL_SYS_JoystickQuit(void)
slouken@1895
   854
{
slouken@5090
   855
    int i;
slouken@5090
   856
slouken@5090
   857
    for (i = 0; i < SDL_arraysize(SYS_JoystickNames); ++i) {
slouken@5090
   858
        if (SYS_JoystickNames[i]) {
slouken@5090
   859
            SDL_free(SYS_JoystickNames[i]);
slouken@5090
   860
            SYS_JoystickNames[i] = NULL;
slouken@5090
   861
        }
slouken@5090
   862
    }
slouken@5090
   863
icculus@5591
   864
    if (dinput != NULL) {
icculus@5591
   865
        IDirectInput_Release(dinput);
icculus@5591
   866
        dinput = NULL;
icculus@5591
   867
    }
icculus@5591
   868
icculus@5591
   869
    if (coinitialized) {
icculus@5591
   870
        WIN_CoUninitialize();
icculus@5591
   871
        coinitialized = SDL_FALSE;
icculus@5591
   872
    }
slouken@1895
   873
}
slouken@1895
   874
slouken@1895
   875
#endif /* SDL_JOYSTICK_DINPUT */
slouken@3575
   876
slouken@1895
   877
/* vi: set ts=4 sw=4 expandtab: */