src/joystick/windows/SDL_dxjoystick.c
changeset 5062 e8916fe9cfc8
parent 4423 7953336267c2
child 5090 327f181542f1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/joystick/windows/SDL_dxjoystick.c	Thu Jan 20 18:04:05 2011 -0800
     1.3 @@ -0,0 +1,802 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2010 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#ifdef SDL_JOYSTICK_DINPUT
    1.28 +
    1.29 +/* DirectInput joystick driver; written by Glenn Maynard, based on Andrei de
    1.30 + * A. Formiga's WINMM driver. 
    1.31 + *
    1.32 + * Hats and sliders are completely untested; the app I'm writing this for mostly
    1.33 + * doesn't use them and I don't own any joysticks with them. 
    1.34 + *
    1.35 + * We don't bother to use event notification here.  It doesn't seem to work
    1.36 + * with polled devices, and it's fine to call IDirectInputDevice2_GetDeviceData and
    1.37 + * let it return 0 events. */
    1.38 +
    1.39 +#include "SDL_error.h"
    1.40 +#include "SDL_events.h"
    1.41 +#include "SDL_joystick.h"
    1.42 +#include "../SDL_sysjoystick.h"
    1.43 +#include "../SDL_joystick_c.h"
    1.44 +#define INITGUID /* Only set here, if set twice will cause mingw32 to break. */
    1.45 +#include "SDL_dxjoystick_c.h"
    1.46 +
    1.47 +
    1.48 +#ifndef DIDFT_OPTIONAL
    1.49 +#define DIDFT_OPTIONAL		0x80000000
    1.50 +#endif
    1.51 +
    1.52 +
    1.53 +#define INPUT_QSIZE	32      /* Buffer up to 32 input messages */
    1.54 +#define MAX_JOYSTICKS	8
    1.55 +#define AXIS_MIN	-32768  /* minimum value for axis coordinate */
    1.56 +#define AXIS_MAX	32767   /* maximum value for axis coordinate */
    1.57 +#define JOY_AXIS_THRESHOLD	(((AXIS_MAX)-(AXIS_MIN))/100)   /* 1% motion */
    1.58 +
    1.59 +/* external variables referenced. */
    1.60 +extern HWND SDL_HelperWindow;
    1.61 +
    1.62 +
    1.63 +/* local variables */
    1.64 +static LPDIRECTINPUT dinput = NULL;
    1.65 +extern HRESULT(WINAPI * DInputCreate) (HINSTANCE hinst, DWORD dwVersion,
    1.66 +                                       LPDIRECTINPUT * ppDI,
    1.67 +                                       LPUNKNOWN punkOuter);
    1.68 +static DIDEVICEINSTANCE SYS_Joystick[MAX_JOYSTICKS];    /* array to hold joystick ID values */
    1.69 +static int SYS_NumJoysticks;
    1.70 +static HINSTANCE DInputDLL = NULL;
    1.71 +
    1.72 +
    1.73 +/* local prototypes */
    1.74 +static void SetDIerror(const char *function, HRESULT code);
    1.75 +static BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE *
    1.76 +                                           pdidInstance, VOID * pContext);
    1.77 +static BOOL CALLBACK EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev,
    1.78 +                                            LPVOID pvRef);
    1.79 +static Uint8 TranslatePOV(DWORD value);
    1.80 +static int SDL_PrivateJoystickAxis_Int(SDL_Joystick * joystick, Uint8 axis,
    1.81 +                                       Sint16 value);
    1.82 +static int SDL_PrivateJoystickHat_Int(SDL_Joystick * joystick, Uint8 hat,
    1.83 +                                      Uint8 value);
    1.84 +static int SDL_PrivateJoystickButton_Int(SDL_Joystick * joystick,
    1.85 +                                         Uint8 button, Uint8 state);
    1.86 +
    1.87 +/* Taken from Wine - Thanks! */
    1.88 +DIOBJECTDATAFORMAT dfDIJoystick2[] = {
    1.89 +  { &GUID_XAxis,DIJOFS_X,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
    1.90 +  { &GUID_YAxis,DIJOFS_Y,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
    1.91 +  { &GUID_ZAxis,DIJOFS_Z,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
    1.92 +  { &GUID_RxAxis,DIJOFS_RX,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
    1.93 +  { &GUID_RyAxis,DIJOFS_RY,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
    1.94 +  { &GUID_RzAxis,DIJOFS_RZ,DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
    1.95 +  { &GUID_Slider,DIJOFS_SLIDER(0),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
    1.96 +  { &GUID_Slider,DIJOFS_SLIDER(1),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
    1.97 +  { &GUID_POV,DIJOFS_POV(0),DIDFT_OPTIONAL|DIDFT_POV|DIDFT_ANYINSTANCE,0},
    1.98 +  { &GUID_POV,DIJOFS_POV(1),DIDFT_OPTIONAL|DIDFT_POV|DIDFT_ANYINSTANCE,0},
    1.99 +  { &GUID_POV,DIJOFS_POV(2),DIDFT_OPTIONAL|DIDFT_POV|DIDFT_ANYINSTANCE,0},
   1.100 +  { &GUID_POV,DIJOFS_POV(3),DIDFT_OPTIONAL|DIDFT_POV|DIDFT_ANYINSTANCE,0},
   1.101 +  { NULL,DIJOFS_BUTTON(0),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.102 +  { NULL,DIJOFS_BUTTON(1),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.103 +  { NULL,DIJOFS_BUTTON(2),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.104 +  { NULL,DIJOFS_BUTTON(3),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.105 +  { NULL,DIJOFS_BUTTON(4),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.106 +  { NULL,DIJOFS_BUTTON(5),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.107 +  { NULL,DIJOFS_BUTTON(6),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.108 +  { NULL,DIJOFS_BUTTON(7),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.109 +  { NULL,DIJOFS_BUTTON(8),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.110 +  { NULL,DIJOFS_BUTTON(9),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.111 +  { NULL,DIJOFS_BUTTON(10),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.112 +  { NULL,DIJOFS_BUTTON(11),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.113 +  { NULL,DIJOFS_BUTTON(12),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.114 +  { NULL,DIJOFS_BUTTON(13),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.115 +  { NULL,DIJOFS_BUTTON(14),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.116 +  { NULL,DIJOFS_BUTTON(15),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.117 +  { NULL,DIJOFS_BUTTON(16),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.118 +  { NULL,DIJOFS_BUTTON(17),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.119 +  { NULL,DIJOFS_BUTTON(18),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.120 +  { NULL,DIJOFS_BUTTON(19),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.121 +  { NULL,DIJOFS_BUTTON(20),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.122 +  { NULL,DIJOFS_BUTTON(21),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.123 +  { NULL,DIJOFS_BUTTON(22),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.124 +  { NULL,DIJOFS_BUTTON(23),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.125 +  { NULL,DIJOFS_BUTTON(24),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.126 +  { NULL,DIJOFS_BUTTON(25),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.127 +  { NULL,DIJOFS_BUTTON(26),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.128 +  { NULL,DIJOFS_BUTTON(27),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.129 +  { NULL,DIJOFS_BUTTON(28),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.130 +  { NULL,DIJOFS_BUTTON(29),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.131 +  { NULL,DIJOFS_BUTTON(30),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.132 +  { NULL,DIJOFS_BUTTON(31),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.133 +  { NULL,DIJOFS_BUTTON(32),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.134 +  { NULL,DIJOFS_BUTTON(33),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.135 +  { NULL,DIJOFS_BUTTON(34),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.136 +  { NULL,DIJOFS_BUTTON(35),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.137 +  { NULL,DIJOFS_BUTTON(36),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.138 +  { NULL,DIJOFS_BUTTON(37),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.139 +  { NULL,DIJOFS_BUTTON(38),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.140 +  { NULL,DIJOFS_BUTTON(39),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.141 +  { NULL,DIJOFS_BUTTON(40),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.142 +  { NULL,DIJOFS_BUTTON(41),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.143 +  { NULL,DIJOFS_BUTTON(42),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.144 +  { NULL,DIJOFS_BUTTON(43),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.145 +  { NULL,DIJOFS_BUTTON(44),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.146 +  { NULL,DIJOFS_BUTTON(45),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.147 +  { NULL,DIJOFS_BUTTON(46),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.148 +  { NULL,DIJOFS_BUTTON(47),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.149 +  { NULL,DIJOFS_BUTTON(48),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.150 +  { NULL,DIJOFS_BUTTON(49),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.151 +  { NULL,DIJOFS_BUTTON(50),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.152 +  { NULL,DIJOFS_BUTTON(51),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.153 +  { NULL,DIJOFS_BUTTON(52),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.154 +  { NULL,DIJOFS_BUTTON(53),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.155 +  { NULL,DIJOFS_BUTTON(54),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.156 +  { NULL,DIJOFS_BUTTON(55),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.157 +  { NULL,DIJOFS_BUTTON(56),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.158 +  { NULL,DIJOFS_BUTTON(57),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.159 +  { NULL,DIJOFS_BUTTON(58),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.160 +  { NULL,DIJOFS_BUTTON(59),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.161 +  { NULL,DIJOFS_BUTTON(60),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.162 +  { NULL,DIJOFS_BUTTON(61),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.163 +  { NULL,DIJOFS_BUTTON(62),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.164 +  { NULL,DIJOFS_BUTTON(63),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.165 +  { NULL,DIJOFS_BUTTON(64),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.166 +  { NULL,DIJOFS_BUTTON(65),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.167 +  { NULL,DIJOFS_BUTTON(66),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.168 +  { NULL,DIJOFS_BUTTON(67),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.169 +  { NULL,DIJOFS_BUTTON(68),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.170 +  { NULL,DIJOFS_BUTTON(69),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.171 +  { NULL,DIJOFS_BUTTON(70),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.172 +  { NULL,DIJOFS_BUTTON(71),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.173 +  { NULL,DIJOFS_BUTTON(72),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.174 +  { NULL,DIJOFS_BUTTON(73),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.175 +  { NULL,DIJOFS_BUTTON(74),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.176 +  { NULL,DIJOFS_BUTTON(75),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.177 +  { NULL,DIJOFS_BUTTON(76),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.178 +  { NULL,DIJOFS_BUTTON(77),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.179 +  { NULL,DIJOFS_BUTTON(78),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.180 +  { NULL,DIJOFS_BUTTON(79),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.181 +  { NULL,DIJOFS_BUTTON(80),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.182 +  { NULL,DIJOFS_BUTTON(81),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.183 +  { NULL,DIJOFS_BUTTON(82),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.184 +  { NULL,DIJOFS_BUTTON(83),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.185 +  { NULL,DIJOFS_BUTTON(84),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.186 +  { NULL,DIJOFS_BUTTON(85),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.187 +  { NULL,DIJOFS_BUTTON(86),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.188 +  { NULL,DIJOFS_BUTTON(87),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.189 +  { NULL,DIJOFS_BUTTON(88),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.190 +  { NULL,DIJOFS_BUTTON(89),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.191 +  { NULL,DIJOFS_BUTTON(90),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.192 +  { NULL,DIJOFS_BUTTON(91),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.193 +  { NULL,DIJOFS_BUTTON(92),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.194 +  { NULL,DIJOFS_BUTTON(93),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.195 +  { NULL,DIJOFS_BUTTON(94),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.196 +  { NULL,DIJOFS_BUTTON(95),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.197 +  { NULL,DIJOFS_BUTTON(96),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.198 +  { NULL,DIJOFS_BUTTON(97),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.199 +  { NULL,DIJOFS_BUTTON(98),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.200 +  { NULL,DIJOFS_BUTTON(99),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.201 +  { NULL,DIJOFS_BUTTON(100),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.202 +  { NULL,DIJOFS_BUTTON(101),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.203 +  { NULL,DIJOFS_BUTTON(102),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.204 +  { NULL,DIJOFS_BUTTON(103),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.205 +  { NULL,DIJOFS_BUTTON(104),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.206 +  { NULL,DIJOFS_BUTTON(105),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.207 +  { NULL,DIJOFS_BUTTON(106),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.208 +  { NULL,DIJOFS_BUTTON(107),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.209 +  { NULL,DIJOFS_BUTTON(108),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.210 +  { NULL,DIJOFS_BUTTON(109),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.211 +  { NULL,DIJOFS_BUTTON(110),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.212 +  { NULL,DIJOFS_BUTTON(111),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.213 +  { NULL,DIJOFS_BUTTON(112),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.214 +  { NULL,DIJOFS_BUTTON(113),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.215 +  { NULL,DIJOFS_BUTTON(114),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.216 +  { NULL,DIJOFS_BUTTON(115),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.217 +  { NULL,DIJOFS_BUTTON(116),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.218 +  { NULL,DIJOFS_BUTTON(117),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.219 +  { NULL,DIJOFS_BUTTON(118),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.220 +  { NULL,DIJOFS_BUTTON(119),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.221 +  { NULL,DIJOFS_BUTTON(120),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.222 +  { NULL,DIJOFS_BUTTON(121),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.223 +  { NULL,DIJOFS_BUTTON(122),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.224 +  { NULL,DIJOFS_BUTTON(123),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.225 +  { NULL,DIJOFS_BUTTON(124),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.226 +  { NULL,DIJOFS_BUTTON(125),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.227 +  { NULL,DIJOFS_BUTTON(126),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.228 +  { NULL,DIJOFS_BUTTON(127),DIDFT_OPTIONAL|DIDFT_BUTTON|DIDFT_ANYINSTANCE,0},
   1.229 +  { &GUID_XAxis,FIELD_OFFSET(DIJOYSTATE2,lVX),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.230 +  { &GUID_YAxis,FIELD_OFFSET(DIJOYSTATE2,lVY),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.231 +  { &GUID_ZAxis,FIELD_OFFSET(DIJOYSTATE2,lVZ),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.232 +  { &GUID_RxAxis,FIELD_OFFSET(DIJOYSTATE2,lVRx),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.233 +  { &GUID_RyAxis,FIELD_OFFSET(DIJOYSTATE2,lVRy),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.234 +  { &GUID_RzAxis,FIELD_OFFSET(DIJOYSTATE2,lVRz),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.235 +  { &GUID_Slider,FIELD_OFFSET(DIJOYSTATE2,rglVSlider[0]),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.236 +  { &GUID_Slider,FIELD_OFFSET(DIJOYSTATE2,rglVSlider[1]),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.237 +  { &GUID_XAxis,FIELD_OFFSET(DIJOYSTATE2,lAX),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.238 +  { &GUID_YAxis,FIELD_OFFSET(DIJOYSTATE2,lAY),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.239 +  { &GUID_ZAxis,FIELD_OFFSET(DIJOYSTATE2,lAZ),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.240 +  { &GUID_RxAxis,FIELD_OFFSET(DIJOYSTATE2,lARx),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.241 +  { &GUID_RyAxis,FIELD_OFFSET(DIJOYSTATE2,lARy),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.242 +  { &GUID_RzAxis,FIELD_OFFSET(DIJOYSTATE2,lARz),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.243 +  { &GUID_Slider,FIELD_OFFSET(DIJOYSTATE2,rglASlider[0]),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.244 +  { &GUID_Slider,FIELD_OFFSET(DIJOYSTATE2,rglASlider[1]),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.245 +  { &GUID_XAxis,FIELD_OFFSET(DIJOYSTATE2,lFX),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.246 +  { &GUID_YAxis,FIELD_OFFSET(DIJOYSTATE2,lFY),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.247 +  { &GUID_ZAxis,FIELD_OFFSET(DIJOYSTATE2,lFZ),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.248 +  { &GUID_RxAxis,FIELD_OFFSET(DIJOYSTATE2,lFRx),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.249 +  { &GUID_RyAxis,FIELD_OFFSET(DIJOYSTATE2,lFRy),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.250 +  { &GUID_RzAxis,FIELD_OFFSET(DIJOYSTATE2,lFRz),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.251 +  { &GUID_Slider,FIELD_OFFSET(DIJOYSTATE2,rglFSlider[0]),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.252 +  { &GUID_Slider,FIELD_OFFSET(DIJOYSTATE2,rglFSlider[1]),DIDFT_OPTIONAL|DIDFT_AXIS|DIDFT_ANYINSTANCE,0},
   1.253 +};
   1.254 +
   1.255 +const DIDATAFORMAT c_dfDIJoystick2 = {
   1.256 +    sizeof(DIDATAFORMAT),
   1.257 +    sizeof(DIOBJECTDATAFORMAT),
   1.258 +    DIDF_ABSAXIS,
   1.259 +    sizeof(DIJOYSTATE2),
   1.260 +    SDL_arraysize(dfDIJoystick2),
   1.261 +    dfDIJoystick2
   1.262 +};
   1.263 +
   1.264 +
   1.265 +/* Convert a DirectInput return code to a text message */
   1.266 +static void
   1.267 +SetDIerror(const char *function, HRESULT code)
   1.268 +{
   1.269 +    /*
   1.270 +    SDL_SetError("%s() [%s]: %s", function,
   1.271 +                 DXGetErrorString9A(code), DXGetErrorDescription9A(code));
   1.272 +     */
   1.273 +    SDL_SetError("%s() DirectX error %d", function, code);
   1.274 +}
   1.275 +
   1.276 +
   1.277 +/* Function to scan the system for joysticks.
   1.278 + * This function should set SDL_numjoysticks to the number of available
   1.279 + * joysticks.  Joystick 0 should be the system default joystick.
   1.280 + * It should return 0, or -1 on an unrecoverable fatal error.
   1.281 + */
   1.282 +int
   1.283 +SDL_SYS_JoystickInit(void)
   1.284 +{
   1.285 +    HRESULT result;
   1.286 +    HINSTANCE instance;
   1.287 +
   1.288 +    SYS_NumJoysticks = 0;
   1.289 +
   1.290 +    result = CoInitialize(NULL);
   1.291 +    if (FAILED(result)) {
   1.292 +        SetDIerror("CoInitialize", result);
   1.293 +        return (-1);
   1.294 +    }
   1.295 +
   1.296 +    result = CoCreateInstance(&CLSID_DirectInput, NULL, CLSCTX_INPROC_SERVER,
   1.297 +                              &IID_IDirectInput, (LPVOID)&dinput);
   1.298 +
   1.299 +    if (FAILED(result)) {
   1.300 +        SetDIerror("CoCreateInstance", result);
   1.301 +        return (-1);
   1.302 +    }
   1.303 +
   1.304 +    /* Because we used CoCreateInstance, we need to Initialize it, first. */
   1.305 +    instance = GetModuleHandle(NULL);
   1.306 +    if (instance == NULL) {
   1.307 +        SDL_SetError("GetModuleHandle() failed with error code %d.",
   1.308 +                     GetLastError());
   1.309 +        return (-1);
   1.310 +    }
   1.311 +    result = IDirectInput_Initialize(dinput, instance, DIRECTINPUT_VERSION);
   1.312 +
   1.313 +    if (FAILED(result)) {
   1.314 +        SetDIerror("IDirectInput::Initialize", result);
   1.315 +        return (-1);
   1.316 +    }
   1.317 +
   1.318 +    /* Look for joysticks, wheels, head trackers, gamepads, etc.. */
   1.319 +    result = IDirectInput_EnumDevices(dinput,
   1.320 +                                      DIDEVTYPE_JOYSTICK,
   1.321 +                                      EnumJoysticksCallback,
   1.322 +                                      NULL, DIEDFL_ATTACHEDONLY);
   1.323 +
   1.324 +    return SYS_NumJoysticks;
   1.325 +}
   1.326 +
   1.327 +static BOOL CALLBACK
   1.328 +EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
   1.329 +{
   1.330 +    SDL_memcpy(&SYS_Joystick[SYS_NumJoysticks], pdidInstance,
   1.331 +               sizeof(DIDEVICEINSTANCE));
   1.332 +    SYS_NumJoysticks++;
   1.333 +
   1.334 +    if (SYS_NumJoysticks >= MAX_JOYSTICKS)
   1.335 +        return DIENUM_STOP;
   1.336 +
   1.337 +    return DIENUM_CONTINUE;
   1.338 +}
   1.339 +
   1.340 +/* Function to get the device-dependent name of a joystick */
   1.341 +const char *
   1.342 +SDL_SYS_JoystickName(int index)
   1.343 +{
   1.344 +        /***-> test for invalid index ? */
   1.345 +    return (SYS_Joystick[index].tszProductName);
   1.346 +}
   1.347 +
   1.348 +/* Function to open a joystick for use.
   1.349 +   The joystick to open is specified by the index field of the joystick.
   1.350 +   This should fill the nbuttons and naxes fields of the joystick structure.
   1.351 +   It returns 0, or -1 if there is an error.
   1.352 + */
   1.353 +int
   1.354 +SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
   1.355 +{
   1.356 +    HRESULT result;
   1.357 +    LPDIRECTINPUTDEVICE device;
   1.358 +    DIPROPDWORD dipdw;
   1.359 +
   1.360 +    SDL_memset(&dipdw, 0, sizeof(DIPROPDWORD));
   1.361 +    dipdw.diph.dwSize = sizeof(DIPROPDWORD);
   1.362 +    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
   1.363 +
   1.364 +
   1.365 +    /* allocate memory for system specific hardware data */
   1.366 +    joystick->hwdata =
   1.367 +        (struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata));
   1.368 +    if (joystick->hwdata == NULL) {
   1.369 +        SDL_OutOfMemory();
   1.370 +        return (-1);
   1.371 +    }
   1.372 +    SDL_memset(joystick->hwdata, 0, sizeof(struct joystick_hwdata));
   1.373 +    joystick->hwdata->buffered = 1;
   1.374 +    joystick->hwdata->Capabilities.dwSize = sizeof(DIDEVCAPS);
   1.375 +
   1.376 +    result =
   1.377 +        IDirectInput_CreateDevice(dinput,
   1.378 +                                  &SYS_Joystick[joystick->index].
   1.379 +                                  guidInstance, &device, NULL);
   1.380 +    if (FAILED(result)) {
   1.381 +        SetDIerror("IDirectInput::CreateDevice", result);
   1.382 +        return (-1);
   1.383 +    }
   1.384 +
   1.385 +    /* Now get the IDirectInputDevice2 interface, instead. */
   1.386 +    result = IDirectInputDevice_QueryInterface(device,
   1.387 +                                               &IID_IDirectInputDevice2,
   1.388 +                                               (LPVOID *) & joystick->
   1.389 +                                               hwdata->InputDevice);
   1.390 +    /* We are done with this object.  Use the stored one from now on. */
   1.391 +    IDirectInputDevice_Release(device);
   1.392 +
   1.393 +    if (FAILED(result)) {
   1.394 +        SetDIerror("IDirectInputDevice::QueryInterface", result);
   1.395 +        return (-1);
   1.396 +    }
   1.397 +
   1.398 +    /* Aquire shared access. Exclusive access is required for forces,
   1.399 +     * though. */
   1.400 +    result =
   1.401 +        IDirectInputDevice2_SetCooperativeLevel(joystick->hwdata->
   1.402 +                                                InputDevice, SDL_HelperWindow,
   1.403 +                                                DISCL_EXCLUSIVE |
   1.404 +                                                DISCL_BACKGROUND);
   1.405 +    if (FAILED(result)) {
   1.406 +        SetDIerror("IDirectInputDevice2::SetCooperativeLevel", result);
   1.407 +        return (-1);
   1.408 +    }
   1.409 +
   1.410 +    /* Use the extended data structure: DIJOYSTATE2. */
   1.411 +    result =
   1.412 +        IDirectInputDevice2_SetDataFormat(joystick->hwdata->InputDevice,
   1.413 +                                          &c_dfDIJoystick2);
   1.414 +    if (FAILED(result)) {
   1.415 +        SetDIerror("IDirectInputDevice2::SetDataFormat", result);
   1.416 +        return (-1);
   1.417 +    }
   1.418 +
   1.419 +    /* Get device capabilities */
   1.420 +    result =
   1.421 +        IDirectInputDevice2_GetCapabilities(joystick->hwdata->InputDevice,
   1.422 +                                            &joystick->hwdata->Capabilities);
   1.423 +
   1.424 +    if (FAILED(result)) {
   1.425 +        SetDIerror("IDirectInputDevice2::GetCapabilities", result);
   1.426 +        return (-1);
   1.427 +    }
   1.428 +
   1.429 +    /* Force capable? */
   1.430 +    if (joystick->hwdata->Capabilities.dwFlags & DIDC_FORCEFEEDBACK) {
   1.431 +
   1.432 +        result = IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
   1.433 +
   1.434 +        if (FAILED(result)) {
   1.435 +            SetDIerror("IDirectInputDevice2::Acquire", result);
   1.436 +            return (-1);
   1.437 +        }
   1.438 +
   1.439 +        /* reset all accuators. */
   1.440 +        result =
   1.441 +            IDirectInputDevice2_SendForceFeedbackCommand(joystick->hwdata->
   1.442 +                                                         InputDevice,
   1.443 +                                                         DISFFC_RESET);
   1.444 +
   1.445 +        /* Not necessarily supported, ignore if not supported.
   1.446 +        if (FAILED(result)) {
   1.447 +            SetDIerror("IDirectInputDevice2::SendForceFeedbackCommand",
   1.448 +                       result);
   1.449 +            return (-1);
   1.450 +        }
   1.451 +        */
   1.452 +
   1.453 +        result = IDirectInputDevice2_Unacquire(joystick->hwdata->InputDevice);
   1.454 +
   1.455 +        if (FAILED(result)) {
   1.456 +            SetDIerror("IDirectInputDevice2::Unacquire", result);
   1.457 +            return (-1);
   1.458 +        }
   1.459 +
   1.460 +        /* Turn on auto-centering for a ForceFeedback device (until told
   1.461 +         * otherwise). */
   1.462 +        dipdw.diph.dwObj = 0;
   1.463 +        dipdw.diph.dwHow = DIPH_DEVICE;
   1.464 +        dipdw.dwData = DIPROPAUTOCENTER_ON;
   1.465 +
   1.466 +        result =
   1.467 +            IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
   1.468 +                                            DIPROP_AUTOCENTER, &dipdw.diph);
   1.469 +
   1.470 +        /* Not necessarily supported, ignore if not supported.
   1.471 +        if (FAILED(result)) {
   1.472 +            SetDIerror("IDirectInputDevice2::SetProperty", result);
   1.473 +            return (-1);
   1.474 +        }
   1.475 +        */
   1.476 +    }
   1.477 +
   1.478 +    /* What buttons and axes does it have? */
   1.479 +    IDirectInputDevice2_EnumObjects(joystick->hwdata->InputDevice,
   1.480 +                                    EnumDevObjectsCallback, joystick,
   1.481 +                                    DIDFT_BUTTON | DIDFT_AXIS | DIDFT_POV);
   1.482 +
   1.483 +    dipdw.diph.dwObj = 0;
   1.484 +    dipdw.diph.dwHow = DIPH_DEVICE;
   1.485 +    dipdw.dwData = INPUT_QSIZE;
   1.486 +
   1.487 +    /* Set the buffer size */
   1.488 +    result =
   1.489 +        IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
   1.490 +                                        DIPROP_BUFFERSIZE, &dipdw.diph);
   1.491 +
   1.492 +    if (result == DI_POLLEDDEVICE) {
   1.493 +        /* This device doesn't support buffering, so we're forced
   1.494 +         * to use less reliable polling. */
   1.495 +        joystick->hwdata->buffered = 0;
   1.496 +    } else if (FAILED(result)) {
   1.497 +        SetDIerror("IDirectInputDevice2::SetProperty", result);
   1.498 +        return (-1);
   1.499 +    }
   1.500 +
   1.501 +    return (0);
   1.502 +}
   1.503 +
   1.504 +static BOOL CALLBACK
   1.505 +EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev, LPVOID pvRef)
   1.506 +{
   1.507 +    SDL_Joystick *joystick = (SDL_Joystick *) pvRef;
   1.508 +    HRESULT result;
   1.509 +    input_t *in = &joystick->hwdata->Inputs[joystick->hwdata->NumInputs];
   1.510 +
   1.511 +    in->ofs = dev->dwOfs;
   1.512 +
   1.513 +    if (dev->dwType & DIDFT_BUTTON) {
   1.514 +        in->type = BUTTON;
   1.515 +        in->num = joystick->nbuttons;
   1.516 +        joystick->nbuttons++;
   1.517 +    } else if (dev->dwType & DIDFT_POV) {
   1.518 +        in->type = HAT;
   1.519 +        in->num = joystick->nhats;
   1.520 +        joystick->nhats++;
   1.521 +    } else if (dev->dwType & DIDFT_AXIS) {
   1.522 +        DIPROPRANGE diprg;
   1.523 +        DIPROPDWORD dilong;
   1.524 +
   1.525 +        in->type = AXIS;
   1.526 +        in->num = joystick->naxes;
   1.527 +
   1.528 +        diprg.diph.dwSize = sizeof(diprg);
   1.529 +        diprg.diph.dwHeaderSize = sizeof(diprg.diph);
   1.530 +        diprg.diph.dwObj = dev->dwOfs;
   1.531 +        diprg.diph.dwHow = DIPH_BYOFFSET;
   1.532 +        diprg.lMin = AXIS_MIN;
   1.533 +        diprg.lMax = AXIS_MAX;
   1.534 +
   1.535 +        result =
   1.536 +            IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
   1.537 +                                            DIPROP_RANGE, &diprg.diph);
   1.538 +        if (FAILED(result)) {
   1.539 +            return DIENUM_CONTINUE;     /* don't use this axis */
   1.540 +        }
   1.541 +
   1.542 +        /* Set dead zone to 0. */
   1.543 +        dilong.diph.dwSize = sizeof(dilong);
   1.544 +        dilong.diph.dwHeaderSize = sizeof(dilong.diph);
   1.545 +        dilong.diph.dwObj = dev->dwOfs;
   1.546 +        dilong.diph.dwHow = DIPH_BYOFFSET;
   1.547 +        dilong.dwData = 0;
   1.548 +        result =
   1.549 +            IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
   1.550 +                                            DIPROP_DEADZONE, &dilong.diph);
   1.551 +        if (FAILED(result)) {
   1.552 +            return DIENUM_CONTINUE;     /* don't use this axis */
   1.553 +        }
   1.554 +
   1.555 +        joystick->naxes++;
   1.556 +    } else {
   1.557 +        /* not supported at this time */
   1.558 +        return DIENUM_CONTINUE;
   1.559 +    }
   1.560 +
   1.561 +    joystick->hwdata->NumInputs++;
   1.562 +
   1.563 +    if (joystick->hwdata->NumInputs == MAX_INPUTS) {
   1.564 +        return DIENUM_STOP;     /* too many */
   1.565 +    }
   1.566 +
   1.567 +    return DIENUM_CONTINUE;
   1.568 +}
   1.569 +
   1.570 +/* Function to update the state of a joystick - called as a device poll.
   1.571 + * This function shouldn't update the joystick structure directly,
   1.572 + * but instead should call SDL_PrivateJoystick*() to deliver events
   1.573 + * and update joystick device state.
   1.574 + */
   1.575 +void
   1.576 +SDL_SYS_JoystickUpdate_Polled(SDL_Joystick * joystick)
   1.577 +{
   1.578 +    DIJOYSTATE2 state;
   1.579 +    HRESULT result;
   1.580 +    int i;
   1.581 +
   1.582 +    result =
   1.583 +        IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice,
   1.584 +                                           sizeof(DIJOYSTATE2), &state);
   1.585 +    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
   1.586 +        IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
   1.587 +        result =
   1.588 +            IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice,
   1.589 +                                               sizeof(DIJOYSTATE2), &state);
   1.590 +    }
   1.591 +
   1.592 +    /* Set each known axis, button and POV. */
   1.593 +    for (i = 0; i < joystick->hwdata->NumInputs; ++i) {
   1.594 +        const input_t *in = &joystick->hwdata->Inputs[i];
   1.595 +
   1.596 +        switch (in->type) {
   1.597 +        case AXIS:
   1.598 +            switch (in->ofs) {
   1.599 +            case DIJOFS_X:
   1.600 +                SDL_PrivateJoystickAxis_Int(joystick, in->num,
   1.601 +                                            (Sint16) state.lX);
   1.602 +                break;
   1.603 +            case DIJOFS_Y:
   1.604 +                SDL_PrivateJoystickAxis_Int(joystick, in->num,
   1.605 +                                            (Sint16) state.lY);
   1.606 +                break;
   1.607 +            case DIJOFS_Z:
   1.608 +                SDL_PrivateJoystickAxis_Int(joystick, in->num,
   1.609 +                                            (Sint16) state.lZ);
   1.610 +                break;
   1.611 +            case DIJOFS_RX:
   1.612 +                SDL_PrivateJoystickAxis_Int(joystick, in->num,
   1.613 +                                            (Sint16) state.lRx);
   1.614 +                break;
   1.615 +            case DIJOFS_RY:
   1.616 +                SDL_PrivateJoystickAxis_Int(joystick, in->num,
   1.617 +                                            (Sint16) state.lRy);
   1.618 +                break;
   1.619 +            case DIJOFS_RZ:
   1.620 +                SDL_PrivateJoystickAxis_Int(joystick, in->num,
   1.621 +                                            (Sint16) state.lRz);
   1.622 +                break;
   1.623 +            case DIJOFS_SLIDER(0):
   1.624 +                SDL_PrivateJoystickAxis_Int(joystick, in->num,
   1.625 +                                            (Sint16) state.rglSlider[0]);
   1.626 +                break;
   1.627 +            case DIJOFS_SLIDER(1):
   1.628 +                SDL_PrivateJoystickAxis_Int(joystick, in->num,
   1.629 +                                            (Sint16) state.rglSlider[1]);
   1.630 +                break;
   1.631 +            }
   1.632 +
   1.633 +            break;
   1.634 +
   1.635 +        case BUTTON:
   1.636 +            SDL_PrivateJoystickButton_Int(joystick, in->num,
   1.637 +                                          (Uint8) (state.
   1.638 +                                                   rgbButtons[in->ofs -
   1.639 +                                                              DIJOFS_BUTTON0]
   1.640 +                                                   ? SDL_PRESSED :
   1.641 +                                                   SDL_RELEASED));
   1.642 +            break;
   1.643 +        case HAT:
   1.644 +            {
   1.645 +                Uint8 pos = TranslatePOV(state.rgdwPOV[in->ofs -
   1.646 +                                                       DIJOFS_POV(0)]);
   1.647 +                SDL_PrivateJoystickHat_Int(joystick, in->num, pos);
   1.648 +                break;
   1.649 +            }
   1.650 +        }
   1.651 +    }
   1.652 +}
   1.653 +
   1.654 +void
   1.655 +SDL_SYS_JoystickUpdate_Buffered(SDL_Joystick * joystick)
   1.656 +{
   1.657 +    int i;
   1.658 +    HRESULT result;
   1.659 +    DWORD numevents;
   1.660 +    DIDEVICEOBJECTDATA evtbuf[INPUT_QSIZE];
   1.661 +
   1.662 +    numevents = INPUT_QSIZE;
   1.663 +    result =
   1.664 +        IDirectInputDevice2_GetDeviceData(joystick->hwdata->InputDevice,
   1.665 +                                          sizeof(DIDEVICEOBJECTDATA), evtbuf,
   1.666 +                                          &numevents, 0);
   1.667 +    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
   1.668 +        IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
   1.669 +        result =
   1.670 +            IDirectInputDevice2_GetDeviceData(joystick->hwdata->InputDevice,
   1.671 +                                              sizeof(DIDEVICEOBJECTDATA),
   1.672 +                                              evtbuf, &numevents, 0);
   1.673 +    }
   1.674 +
   1.675 +    /* Handle the events or punt */
   1.676 +    if (FAILED(result))
   1.677 +        return;
   1.678 +
   1.679 +    for (i = 0; i < (int) numevents; ++i) {
   1.680 +        int j;
   1.681 +
   1.682 +        for (j = 0; j < joystick->hwdata->NumInputs; ++j) {
   1.683 +            const input_t *in = &joystick->hwdata->Inputs[j];
   1.684 +
   1.685 +            if (evtbuf[i].dwOfs != in->ofs)
   1.686 +                continue;
   1.687 +
   1.688 +            switch (in->type) {
   1.689 +            case AXIS:
   1.690 +                SDL_PrivateJoystickAxis(joystick, in->num,
   1.691 +                                        (Sint16) evtbuf[i].dwData);
   1.692 +                break;
   1.693 +            case BUTTON:
   1.694 +                SDL_PrivateJoystickButton(joystick, in->num,
   1.695 +                                          (Uint8) (evtbuf[i].
   1.696 +                                                   dwData ? SDL_PRESSED :
   1.697 +                                                   SDL_RELEASED));
   1.698 +                break;
   1.699 +            case HAT:
   1.700 +                {
   1.701 +                    Uint8 pos = TranslatePOV(evtbuf[i].dwData);
   1.702 +                    SDL_PrivateJoystickHat(joystick, in->num, pos);
   1.703 +                }
   1.704 +            }
   1.705 +        }
   1.706 +    }
   1.707 +}
   1.708 +
   1.709 +
   1.710 +static Uint8
   1.711 +TranslatePOV(DWORD value)
   1.712 +{
   1.713 +    const int HAT_VALS[] = {
   1.714 +        SDL_HAT_UP,
   1.715 +        SDL_HAT_UP | SDL_HAT_RIGHT,
   1.716 +        SDL_HAT_RIGHT,
   1.717 +        SDL_HAT_DOWN | SDL_HAT_RIGHT,
   1.718 +        SDL_HAT_DOWN,
   1.719 +        SDL_HAT_DOWN | SDL_HAT_LEFT,
   1.720 +        SDL_HAT_LEFT,
   1.721 +        SDL_HAT_UP | SDL_HAT_LEFT
   1.722 +    };
   1.723 +
   1.724 +    if (LOWORD(value) == 0xFFFF)
   1.725 +        return SDL_HAT_CENTERED;
   1.726 +
   1.727 +    /* Round the value up: */
   1.728 +    value += 4500 / 2;
   1.729 +    value %= 36000;
   1.730 +    value /= 4500;
   1.731 +
   1.732 +    if (value >= 8)
   1.733 +        return SDL_HAT_CENTERED;        /* shouldn't happen */
   1.734 +
   1.735 +    return HAT_VALS[value];
   1.736 +}
   1.737 +
   1.738 +/* SDL_PrivateJoystick* doesn't discard duplicate events, so we need to
   1.739 + * do it. */
   1.740 +static int
   1.741 +SDL_PrivateJoystickAxis_Int(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
   1.742 +{
   1.743 +    if (joystick->axes[axis] != value)
   1.744 +        return SDL_PrivateJoystickAxis(joystick, axis, value);
   1.745 +    return 0;
   1.746 +}
   1.747 +
   1.748 +static int
   1.749 +SDL_PrivateJoystickHat_Int(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
   1.750 +{
   1.751 +    if (joystick->hats[hat] != value)
   1.752 +        return SDL_PrivateJoystickHat(joystick, hat, value);
   1.753 +    return 0;
   1.754 +}
   1.755 +
   1.756 +static int
   1.757 +SDL_PrivateJoystickButton_Int(SDL_Joystick * joystick, Uint8 button,
   1.758 +                              Uint8 state)
   1.759 +{
   1.760 +    if (joystick->buttons[button] != state)
   1.761 +        return SDL_PrivateJoystickButton(joystick, button, state);
   1.762 +    return 0;
   1.763 +}
   1.764 +
   1.765 +void
   1.766 +SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
   1.767 +{
   1.768 +    HRESULT result;
   1.769 +
   1.770 +    result = IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
   1.771 +    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
   1.772 +        IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
   1.773 +        IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
   1.774 +    }
   1.775 +
   1.776 +    if (joystick->hwdata->buffered)
   1.777 +        SDL_SYS_JoystickUpdate_Buffered(joystick);
   1.778 +    else
   1.779 +        SDL_SYS_JoystickUpdate_Polled(joystick);
   1.780 +}
   1.781 +
   1.782 +/* Function to close a joystick after use */
   1.783 +void
   1.784 +SDL_SYS_JoystickClose(SDL_Joystick * joystick)
   1.785 +{
   1.786 +    IDirectInputDevice2_Unacquire(joystick->hwdata->InputDevice);
   1.787 +    IDirectInputDevice2_Release(joystick->hwdata->InputDevice);
   1.788 +
   1.789 +    if (joystick->hwdata != NULL) {
   1.790 +        /* free system specific hardware data */
   1.791 +        SDL_free(joystick->hwdata);
   1.792 +    }
   1.793 +}
   1.794 +
   1.795 +/* Function to perform any system-specific joystick related cleanup */
   1.796 +void
   1.797 +SDL_SYS_JoystickQuit(void)
   1.798 +{
   1.799 +    IDirectInput_Release(dinput);
   1.800 +    dinput = NULL;
   1.801 +}
   1.802 +
   1.803 +#endif /* SDL_JOYSTICK_DINPUT */
   1.804 +
   1.805 +/* vi: set ts=4 sw=4 expandtab: */