src/joystick/windows/SDL_dxjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Jan 2011 21:20:30 -0800
changeset 5090 327f181542f1
parent 5062 e8916fe9cfc8
child 5262 b530ef003506
permissions -rw-r--r--
Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.

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