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