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