src/joystick/windows/SDL_dxjoystick.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 03 Aug 2011 04:22:47 -0400
changeset 5591 17beb16fa838
parent 5535 96594ac5fd1a
child 6138 4c64952a58fb
permissions -rw-r--r--
Cleaned up CoInitialize() politics on Windows.
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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
static HINSTANCE DInputDLL = NULL;
slouken@2198
    69
slouken@2198
    70
slouken@2198
    71
/* local prototypes */
slouken@2198
    72
static void SetDIerror(const char *function, HRESULT code);
slouken@2198
    73
static BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE *
slouken@2198
    74
                                           pdidInstance, VOID * pContext);
slouken@2198
    75
static BOOL CALLBACK EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev,
slouken@2198
    76
                                            LPVOID pvRef);
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@2198
   486
    dipdw.diph.dwObj = 0;
slouken@2198
   487
    dipdw.diph.dwHow = DIPH_DEVICE;
slouken@2198
   488
    dipdw.dwData = INPUT_QSIZE;
slouken@2198
   489
slouken@2198
   490
    /* Set the buffer size */
slouken@2198
   491
    result =
slouken@2198
   492
        IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
slouken@2198
   493
                                        DIPROP_BUFFERSIZE, &dipdw.diph);
slouken@2198
   494
slouken@2198
   495
    if (result == DI_POLLEDDEVICE) {
slouken@2198
   496
        /* This device doesn't support buffering, so we're forced
slouken@2198
   497
         * to use less reliable polling. */
slouken@2198
   498
        joystick->hwdata->buffered = 0;
slouken@2198
   499
    } else if (FAILED(result)) {
slouken@2198
   500
        SetDIerror("IDirectInputDevice2::SetProperty", result);
slouken@2198
   501
        return (-1);
slouken@2198
   502
    }
slouken@2198
   503
slouken@1895
   504
    return (0);
slouken@1895
   505
}
slouken@1895
   506
slouken@2198
   507
static BOOL CALLBACK
slouken@2198
   508
EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev, LPVOID pvRef)
slouken@2198
   509
{
slouken@2198
   510
    SDL_Joystick *joystick = (SDL_Joystick *) pvRef;
slouken@2198
   511
    HRESULT result;
slouken@2198
   512
    input_t *in = &joystick->hwdata->Inputs[joystick->hwdata->NumInputs];
slouken@2198
   513
slouken@2198
   514
    in->ofs = dev->dwOfs;
slouken@2198
   515
slouken@2198
   516
    if (dev->dwType & DIDFT_BUTTON) {
slouken@2198
   517
        in->type = BUTTON;
slouken@2198
   518
        in->num = joystick->nbuttons;
slouken@2198
   519
        joystick->nbuttons++;
slouken@2198
   520
    } else if (dev->dwType & DIDFT_POV) {
slouken@2198
   521
        in->type = HAT;
slouken@2198
   522
        in->num = joystick->nhats;
slouken@2198
   523
        joystick->nhats++;
slouken@2198
   524
    } else if (dev->dwType & DIDFT_AXIS) {
slouken@2198
   525
        DIPROPRANGE diprg;
slouken@2198
   526
        DIPROPDWORD dilong;
slouken@2198
   527
slouken@2198
   528
        in->type = AXIS;
slouken@2198
   529
        in->num = joystick->naxes;
slouken@2198
   530
slouken@2198
   531
        diprg.diph.dwSize = sizeof(diprg);
slouken@2198
   532
        diprg.diph.dwHeaderSize = sizeof(diprg.diph);
slouken@2198
   533
        diprg.diph.dwObj = dev->dwOfs;
slouken@2198
   534
        diprg.diph.dwHow = DIPH_BYOFFSET;
slouken@2198
   535
        diprg.lMin = AXIS_MIN;
slouken@2198
   536
        diprg.lMax = AXIS_MAX;
slouken@2198
   537
slouken@2198
   538
        result =
slouken@2198
   539
            IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
slouken@2198
   540
                                            DIPROP_RANGE, &diprg.diph);
slouken@2198
   541
        if (FAILED(result)) {
slouken@2198
   542
            return DIENUM_CONTINUE;     /* don't use this axis */
slouken@2198
   543
        }
slouken@2198
   544
slouken@2198
   545
        /* Set dead zone to 0. */
slouken@2198
   546
        dilong.diph.dwSize = sizeof(dilong);
slouken@2198
   547
        dilong.diph.dwHeaderSize = sizeof(dilong.diph);
slouken@2198
   548
        dilong.diph.dwObj = dev->dwOfs;
slouken@2198
   549
        dilong.diph.dwHow = DIPH_BYOFFSET;
slouken@2198
   550
        dilong.dwData = 0;
slouken@2198
   551
        result =
slouken@2198
   552
            IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
slouken@2198
   553
                                            DIPROP_DEADZONE, &dilong.diph);
slouken@2198
   554
        if (FAILED(result)) {
slouken@2198
   555
            return DIENUM_CONTINUE;     /* don't use this axis */
slouken@2198
   556
        }
slouken@2198
   557
slouken@2198
   558
        joystick->naxes++;
slouken@2198
   559
    } else {
slouken@2198
   560
        /* not supported at this time */
slouken@2198
   561
        return DIENUM_CONTINUE;
slouken@2198
   562
    }
slouken@2198
   563
slouken@2198
   564
    joystick->hwdata->NumInputs++;
slouken@2198
   565
slouken@2198
   566
    if (joystick->hwdata->NumInputs == MAX_INPUTS) {
slouken@2198
   567
        return DIENUM_STOP;     /* too many */
slouken@2198
   568
    }
slouken@2198
   569
slouken@2198
   570
    return DIENUM_CONTINUE;
slouken@2198
   571
}
slouken@2198
   572
slouken@2198
   573
/* Function to update the state of a joystick - called as a device poll.
slouken@2198
   574
 * This function shouldn't update the joystick structure directly,
slouken@2198
   575
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@2198
   576
 * and update joystick device state.
slouken@2198
   577
 */
slouken@2198
   578
void
slouken@2198
   579
SDL_SYS_JoystickUpdate_Polled(SDL_Joystick * joystick)
slouken@2198
   580
{
slouken@2198
   581
    DIJOYSTATE2 state;
slouken@2198
   582
    HRESULT result;
slouken@2198
   583
    int i;
slouken@2198
   584
slouken@2198
   585
    result =
slouken@2198
   586
        IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice,
slouken@2198
   587
                                           sizeof(DIJOYSTATE2), &state);
slouken@2198
   588
    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
slouken@2198
   589
        IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
slouken@2198
   590
        result =
slouken@2198
   591
            IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice,
slouken@2198
   592
                                               sizeof(DIJOYSTATE2), &state);
slouken@2198
   593
    }
slouken@2198
   594
slouken@2198
   595
    /* Set each known axis, button and POV. */
slouken@2198
   596
    for (i = 0; i < joystick->hwdata->NumInputs; ++i) {
slouken@2198
   597
        const input_t *in = &joystick->hwdata->Inputs[i];
slouken@2198
   598
slouken@2198
   599
        switch (in->type) {
slouken@2198
   600
        case AXIS:
slouken@2198
   601
            switch (in->ofs) {
slouken@2198
   602
            case DIJOFS_X:
slouken@2198
   603
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   604
                                            (Sint16) state.lX);
slouken@2198
   605
                break;
slouken@2198
   606
            case DIJOFS_Y:
slouken@2198
   607
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   608
                                            (Sint16) state.lY);
slouken@2198
   609
                break;
slouken@2198
   610
            case DIJOFS_Z:
slouken@2198
   611
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   612
                                            (Sint16) state.lZ);
slouken@2198
   613
                break;
slouken@2198
   614
            case DIJOFS_RX:
slouken@2198
   615
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   616
                                            (Sint16) state.lRx);
slouken@2198
   617
                break;
slouken@2198
   618
            case DIJOFS_RY:
slouken@2198
   619
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   620
                                            (Sint16) state.lRy);
slouken@2198
   621
                break;
slouken@2198
   622
            case DIJOFS_RZ:
slouken@2198
   623
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   624
                                            (Sint16) state.lRz);
slouken@2198
   625
                break;
slouken@2198
   626
            case DIJOFS_SLIDER(0):
slouken@2198
   627
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   628
                                            (Sint16) state.rglSlider[0]);
slouken@2198
   629
                break;
slouken@2198
   630
            case DIJOFS_SLIDER(1):
slouken@2198
   631
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   632
                                            (Sint16) state.rglSlider[1]);
slouken@2198
   633
                break;
slouken@2198
   634
            }
slouken@2198
   635
slouken@2198
   636
            break;
slouken@2198
   637
slouken@2198
   638
        case BUTTON:
slouken@2198
   639
            SDL_PrivateJoystickButton_Int(joystick, in->num,
slouken@3013
   640
                                          (Uint8) (state.
slouken@3013
   641
                                                   rgbButtons[in->ofs -
slouken@3013
   642
                                                              DIJOFS_BUTTON0]
slouken@2198
   643
                                                   ? SDL_PRESSED :
slouken@2198
   644
                                                   SDL_RELEASED));
slouken@2198
   645
            break;
slouken@2198
   646
        case HAT:
slouken@2198
   647
            {
slouken@2198
   648
                Uint8 pos = TranslatePOV(state.rgdwPOV[in->ofs -
slouken@2198
   649
                                                       DIJOFS_POV(0)]);
slouken@2198
   650
                SDL_PrivateJoystickHat_Int(joystick, in->num, pos);
slouken@2198
   651
                break;
slouken@2198
   652
            }
slouken@2198
   653
        }
slouken@2198
   654
    }
slouken@2198
   655
}
slouken@2198
   656
slouken@2198
   657
void
slouken@2198
   658
SDL_SYS_JoystickUpdate_Buffered(SDL_Joystick * joystick)
slouken@2198
   659
{
slouken@2198
   660
    int i;
slouken@2198
   661
    HRESULT result;
slouken@2198
   662
    DWORD numevents;
slouken@2198
   663
    DIDEVICEOBJECTDATA evtbuf[INPUT_QSIZE];
slouken@2198
   664
slouken@2198
   665
    numevents = INPUT_QSIZE;
slouken@2198
   666
    result =
slouken@2198
   667
        IDirectInputDevice2_GetDeviceData(joystick->hwdata->InputDevice,
slouken@2198
   668
                                          sizeof(DIDEVICEOBJECTDATA), evtbuf,
slouken@2198
   669
                                          &numevents, 0);
slouken@2198
   670
    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
slouken@2198
   671
        IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
slouken@2198
   672
        result =
slouken@2198
   673
            IDirectInputDevice2_GetDeviceData(joystick->hwdata->InputDevice,
slouken@2198
   674
                                              sizeof(DIDEVICEOBJECTDATA),
slouken@2198
   675
                                              evtbuf, &numevents, 0);
slouken@2198
   676
    }
slouken@2198
   677
slouken@2198
   678
    /* Handle the events or punt */
slouken@2198
   679
    if (FAILED(result))
slouken@2198
   680
        return;
slouken@2198
   681
slouken@2198
   682
    for (i = 0; i < (int) numevents; ++i) {
slouken@2198
   683
        int j;
slouken@2198
   684
slouken@2198
   685
        for (j = 0; j < joystick->hwdata->NumInputs; ++j) {
slouken@2198
   686
            const input_t *in = &joystick->hwdata->Inputs[j];
slouken@2198
   687
slouken@2198
   688
            if (evtbuf[i].dwOfs != in->ofs)
slouken@2198
   689
                continue;
slouken@2198
   690
slouken@2198
   691
            switch (in->type) {
slouken@2198
   692
            case AXIS:
slouken@2198
   693
                SDL_PrivateJoystickAxis(joystick, in->num,
slouken@2198
   694
                                        (Sint16) evtbuf[i].dwData);
slouken@2198
   695
                break;
slouken@2198
   696
            case BUTTON:
slouken@2198
   697
                SDL_PrivateJoystickButton(joystick, in->num,
slouken@3013
   698
                                          (Uint8) (evtbuf[i].
slouken@3013
   699
                                                   dwData ? SDL_PRESSED :
slouken@2198
   700
                                                   SDL_RELEASED));
slouken@2198
   701
                break;
slouken@2198
   702
            case HAT:
slouken@2198
   703
                {
slouken@2198
   704
                    Uint8 pos = TranslatePOV(evtbuf[i].dwData);
slouken@2198
   705
                    SDL_PrivateJoystickHat(joystick, in->num, pos);
slouken@2198
   706
                }
slouken@2198
   707
            }
slouken@2198
   708
        }
slouken@2198
   709
    }
slouken@2198
   710
}
slouken@2198
   711
slouken@2198
   712
slouken@1895
   713
static Uint8
slouken@1895
   714
TranslatePOV(DWORD value)
slouken@1895
   715
{
slouken@1895
   716
    const int HAT_VALS[] = {
slouken@1895
   717
        SDL_HAT_UP,
slouken@1895
   718
        SDL_HAT_UP | SDL_HAT_RIGHT,
slouken@1895
   719
        SDL_HAT_RIGHT,
slouken@1895
   720
        SDL_HAT_DOWN | SDL_HAT_RIGHT,
slouken@1895
   721
        SDL_HAT_DOWN,
slouken@1895
   722
        SDL_HAT_DOWN | SDL_HAT_LEFT,
slouken@1895
   723
        SDL_HAT_LEFT,
slouken@1895
   724
        SDL_HAT_UP | SDL_HAT_LEFT
slouken@1895
   725
    };
slouken@1895
   726
slouken@1895
   727
    if (LOWORD(value) == 0xFFFF)
slouken@1895
   728
        return SDL_HAT_CENTERED;
slouken@1895
   729
slouken@1895
   730
    /* Round the value up: */
slouken@1895
   731
    value += 4500 / 2;
slouken@1895
   732
    value %= 36000;
slouken@1895
   733
    value /= 4500;
slouken@1895
   734
slouken@1895
   735
    if (value >= 8)
slouken@1895
   736
        return SDL_HAT_CENTERED;        /* shouldn't happen */
slouken@1895
   737
slouken@1895
   738
    return HAT_VALS[value];
slouken@1895
   739
}
slouken@1895
   740
slouken@1895
   741
/* SDL_PrivateJoystick* doesn't discard duplicate events, so we need to
slouken@1895
   742
 * do it. */
slouken@1895
   743
static int
slouken@1895
   744
SDL_PrivateJoystickAxis_Int(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
slouken@1895
   745
{
slouken@1895
   746
    if (joystick->axes[axis] != value)
slouken@1895
   747
        return SDL_PrivateJoystickAxis(joystick, axis, value);
slouken@1895
   748
    return 0;
slouken@1895
   749
}
slouken@1895
   750
slouken@1895
   751
static int
slouken@1895
   752
SDL_PrivateJoystickHat_Int(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
slouken@1895
   753
{
slouken@1895
   754
    if (joystick->hats[hat] != value)
slouken@1895
   755
        return SDL_PrivateJoystickHat(joystick, hat, value);
slouken@1895
   756
    return 0;
slouken@1895
   757
}
slouken@1895
   758
slouken@1895
   759
static int
slouken@1895
   760
SDL_PrivateJoystickButton_Int(SDL_Joystick * joystick, Uint8 button,
slouken@1895
   761
                              Uint8 state)
slouken@1895
   762
{
slouken@1895
   763
    if (joystick->buttons[button] != state)
slouken@1895
   764
        return SDL_PrivateJoystickButton(joystick, button, state);
slouken@1895
   765
    return 0;
slouken@1895
   766
}
slouken@1895
   767
slouken@1895
   768
void
slouken@1895
   769
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
slouken@1895
   770
{
slouken@1895
   771
    HRESULT result;
slouken@1895
   772
slouken@1895
   773
    result = IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
slouken@1895
   774
    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
slouken@1895
   775
        IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
slouken@1895
   776
        IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
slouken@1895
   777
    }
slouken@1895
   778
slouken@1895
   779
    if (joystick->hwdata->buffered)
slouken@1895
   780
        SDL_SYS_JoystickUpdate_Buffered(joystick);
slouken@1895
   781
    else
slouken@1895
   782
        SDL_SYS_JoystickUpdate_Polled(joystick);
slouken@1895
   783
}
slouken@1895
   784
slouken@1895
   785
/* Function to close a joystick after use */
slouken@1895
   786
void
slouken@1895
   787
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
slouken@1895
   788
{
slouken@1895
   789
    IDirectInputDevice2_Unacquire(joystick->hwdata->InputDevice);
slouken@1895
   790
    IDirectInputDevice2_Release(joystick->hwdata->InputDevice);
slouken@1895
   791
slouken@1895
   792
    if (joystick->hwdata != NULL) {
slouken@1895
   793
        /* free system specific hardware data */
slouken@2713
   794
        SDL_free(joystick->hwdata);
slouken@1895
   795
    }
slouken@1895
   796
}
slouken@1895
   797
slouken@1895
   798
/* Function to perform any system-specific joystick related cleanup */
slouken@1895
   799
void
slouken@1895
   800
SDL_SYS_JoystickQuit(void)
slouken@1895
   801
{
slouken@5090
   802
    int i;
slouken@5090
   803
slouken@5090
   804
    for (i = 0; i < SDL_arraysize(SYS_JoystickNames); ++i) {
slouken@5090
   805
        if (SYS_JoystickNames[i]) {
slouken@5090
   806
            SDL_free(SYS_JoystickNames[i]);
slouken@5090
   807
            SYS_JoystickNames[i] = NULL;
slouken@5090
   808
        }
slouken@5090
   809
    }
slouken@5090
   810
icculus@5591
   811
    if (dinput != NULL) {
icculus@5591
   812
        IDirectInput_Release(dinput);
icculus@5591
   813
        dinput = NULL;
icculus@5591
   814
    }
icculus@5591
   815
icculus@5591
   816
    if (coinitialized) {
icculus@5591
   817
        WIN_CoUninitialize();
icculus@5591
   818
        coinitialized = SDL_FALSE;
icculus@5591
   819
    }
slouken@1895
   820
}
slouken@1895
   821
slouken@1895
   822
#endif /* SDL_JOYSTICK_DINPUT */
slouken@3575
   823
slouken@1895
   824
/* vi: set ts=4 sw=4 expandtab: */