src/joystick/win32/SDL_dxjoystick.c
author Edgar Simo <bobbens@gmail.com>
Mon, 25 Aug 2008 17:34:58 +0000
changeset 2717 50bc882455e5
parent 2713 0906692aa6a4
child 2760 02aa80d7905f
permissions -rw-r--r--
Merge of force feedback branch r4039.
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@2198
    43
/* an ISO hack for VisualC++ */
slouken@2198
    44
#ifdef _MSC_VER
slouken@2198
    45
#define   snprintf	_snprintf
slouken@2198
    46
#endif
slouken@1895
    47
slouken@1895
    48
#define INPUT_QSIZE	32      /* Buffer up to 32 input messages */
slouken@1895
    49
#define MAX_JOYSTICKS	8
slouken@1895
    50
#define AXIS_MIN	-32768  /* minimum value for axis coordinate */
slouken@1895
    51
#define AXIS_MAX	32767   /* maximum value for axis coordinate */
slouken@1895
    52
#define JOY_AXIS_THRESHOLD	(((AXIS_MAX)-(AXIS_MIN))/100)   /* 1% motion */
slouken@1895
    53
slouken@2198
    54
/* external variables referenced. */
slouken@2713
    55
extern HWND SDL_HelperWindow;
slouken@2198
    56
slouken@2198
    57
slouken@2198
    58
/* local variables */
slouken@2198
    59
static LPDIRECTINPUT dinput = NULL;
slouken@2198
    60
extern HRESULT(WINAPI * DInputCreate) (HINSTANCE hinst, DWORD dwVersion,
slouken@2198
    61
                                       LPDIRECTINPUT * ppDI,
slouken@2198
    62
                                       LPUNKNOWN punkOuter);
slouken@2198
    63
static DIDEVICEINSTANCE SYS_Joystick[MAX_JOYSTICKS];    /* array to hold joystick ID values */
slouken@2198
    64
static int SYS_NumJoysticks;
slouken@2198
    65
static HINSTANCE DInputDLL = NULL;
slouken@2198
    66
slouken@2198
    67
slouken@2198
    68
/* local prototypes */
slouken@2198
    69
static void SetDIerror(const char *function, HRESULT code);
slouken@2198
    70
static BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE *
slouken@2198
    71
                                           pdidInstance, VOID * pContext);
slouken@2198
    72
static BOOL CALLBACK EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev,
slouken@2198
    73
                                            LPVOID pvRef);
slouken@2198
    74
static Uint8 TranslatePOV(DWORD value);
slouken@2198
    75
static int SDL_PrivateJoystickAxis_Int(SDL_Joystick * joystick, Uint8 axis,
slouken@2198
    76
                                       Sint16 value);
slouken@2198
    77
static int SDL_PrivateJoystickHat_Int(SDL_Joystick * joystick, Uint8 hat,
slouken@2198
    78
                                      Uint8 value);
slouken@2198
    79
static int SDL_PrivateJoystickButton_Int(SDL_Joystick * joystick,
slouken@2198
    80
                                         Uint8 button, Uint8 state);
slouken@2198
    81
slouken@2198
    82
slouken@1895
    83
/* Convert a DirectInput return code to a text message */
slouken@1895
    84
static void
slouken@2198
    85
SetDIerror(const char *function, HRESULT code)
slouken@1895
    86
{
slouken@2198
    87
    SDL_SetError("%s() [%s]: %s", function,
bobbens@2717
    88
                 DXGetErrorString8A(code), DXGetErrorDescription8A(code));
slouken@1895
    89
}
slouken@1895
    90
slouken@1895
    91
slouken@1895
    92
/* Function to scan the system for joysticks.
slouken@1895
    93
 * This function should set SDL_numjoysticks to the number of available
slouken@1895
    94
 * joysticks.  Joystick 0 should be the system default joystick.
slouken@1895
    95
 * It should return 0, or -1 on an unrecoverable fatal error.
slouken@1895
    96
 */
slouken@1895
    97
int
slouken@1895
    98
SDL_SYS_JoystickInit(void)
slouken@1895
    99
{
slouken@1895
   100
    HRESULT result;
slouken@2713
   101
    HINSTANCE instance;
slouken@1895
   102
slouken@1895
   103
    SYS_NumJoysticks = 0;
slouken@1895
   104
slouken@2198
   105
    result = CoInitialize(NULL);
slouken@2198
   106
    if (FAILED(result)) {
slouken@2198
   107
        SetDIerror("CoInitialize", result);
slouken@1895
   108
        return (-1);
slouken@1895
   109
    }
slouken@1895
   110
slouken@2198
   111
    result = CoCreateInstance(&CLSID_DirectInput, NULL, CLSCTX_INPROC_SERVER,
slouken@2198
   112
                              &IID_IDirectInput, &dinput);
slouken@2198
   113
slouken@2198
   114
    if (FAILED(result)) {
slouken@2198
   115
        SetDIerror("CoCreateInstance", result);
slouken@2198
   116
        return (-1);
slouken@2198
   117
    }
slouken@2198
   118
slouken@2198
   119
    /* Because we used CoCreateInstance, we need to Initialize it, first. */
slouken@2713
   120
    instance = GetModuleHandle(NULL);
slouken@2713
   121
    if (instance == NULL) {
slouken@2713
   122
        SDL_SetError("GetModuleHandle() failed with error code %d.",
slouken@2713
   123
                     GetLastError());
slouken@2713
   124
        return (-1);
slouken@2713
   125
    }
slouken@2713
   126
    result = IDirectInput_Initialize(dinput, instance, DIRECTINPUT_VERSION);
slouken@2198
   127
slouken@2198
   128
    if (FAILED(result)) {
slouken@2198
   129
        SetDIerror("IDirectInput::Initialize", result);
slouken@2198
   130
        return (-1);
slouken@2198
   131
    }
slouken@2198
   132
slouken@2198
   133
    /* Look for joysticks, wheels, head trackers, gamepads, etc.. */
slouken@1895
   134
    result = IDirectInput_EnumDevices(dinput,
slouken@1895
   135
                                      DIDEVTYPE_JOYSTICK,
slouken@1895
   136
                                      EnumJoysticksCallback,
slouken@1895
   137
                                      NULL, DIEDFL_ATTACHEDONLY);
slouken@1895
   138
slouken@1895
   139
    return SYS_NumJoysticks;
slouken@1895
   140
}
slouken@1895
   141
slouken@2198
   142
static BOOL CALLBACK
slouken@2198
   143
EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
slouken@2198
   144
{
slouken@2713
   145
    SDL_memcpy(&SYS_Joystick[SYS_NumJoysticks], pdidInstance,
slouken@2713
   146
               sizeof(DIDEVICEINSTANCE));
slouken@2198
   147
    SYS_NumJoysticks++;
slouken@2198
   148
slouken@2198
   149
    if (SYS_NumJoysticks >= MAX_JOYSTICKS)
slouken@2198
   150
        return DIENUM_STOP;
slouken@2198
   151
slouken@2198
   152
    return DIENUM_CONTINUE;
slouken@2198
   153
}
slouken@2198
   154
slouken@1895
   155
/* Function to get the device-dependent name of a joystick */
slouken@1895
   156
const char *
slouken@1895
   157
SDL_SYS_JoystickName(int index)
slouken@1895
   158
{
slouken@1895
   159
        /***-> test for invalid index ? */
slouken@1895
   160
    return (SYS_Joystick[index].tszProductName);
slouken@1895
   161
}
slouken@1895
   162
slouken@1895
   163
/* Function to open a joystick for use.
slouken@1895
   164
   The joystick to open is specified by the index field of the joystick.
slouken@1895
   165
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@1895
   166
   It returns 0, or -1 if there is an error.
slouken@1895
   167
 */
slouken@1895
   168
int
slouken@1895
   169
SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
slouken@1895
   170
{
slouken@1895
   171
    HRESULT result;
slouken@1895
   172
    LPDIRECTINPUTDEVICE device;
slouken@2198
   173
    DIPROPDWORD dipdw;
slouken@2198
   174
slouken@2713
   175
    SDL_memset(&dipdw, 0, sizeof(DIPROPDWORD));
slouken@2198
   176
    dipdw.diph.dwSize = sizeof(DIPROPDWORD);
slouken@2198
   177
    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
slouken@2198
   178
slouken@1895
   179
slouken@1895
   180
    /* allocate memory for system specific hardware data */
slouken@1895
   181
    joystick->hwdata =
slouken@2713
   182
        (struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata));
slouken@1895
   183
    if (joystick->hwdata == NULL) {
slouken@1895
   184
        SDL_OutOfMemory();
slouken@1895
   185
        return (-1);
slouken@1895
   186
    }
slouken@2713
   187
    SDL_memset(joystick->hwdata, 0, sizeof(struct joystick_hwdata));
slouken@1895
   188
    joystick->hwdata->buffered = 1;
slouken@2198
   189
    joystick->hwdata->Capabilities.dwSize = sizeof(DIDEVCAPS);
slouken@1895
   190
slouken@1895
   191
    result =
slouken@1895
   192
        IDirectInput_CreateDevice(dinput,
slouken@1895
   193
                                  &SYS_Joystick[joystick->index].
slouken@1895
   194
                                  guidInstance, &device, NULL);
slouken@2198
   195
    if (FAILED(result)) {
slouken@2198
   196
        SetDIerror("IDirectInput::CreateDevice", result);
slouken@1895
   197
        return (-1);
slouken@1895
   198
    }
slouken@1895
   199
slouken@2198
   200
    /* Now get the IDirectInputDevice2 interface, instead. */
slouken@1895
   201
    result = IDirectInputDevice_QueryInterface(device,
slouken@1895
   202
                                               &IID_IDirectInputDevice2,
slouken@1895
   203
                                               (LPVOID *) & joystick->
slouken@1895
   204
                                               hwdata->InputDevice);
slouken@2198
   205
    /* We are done with this object.  Use the stored one from now on. */
slouken@1895
   206
    IDirectInputDevice_Release(device);
slouken@2198
   207
slouken@2198
   208
    if (FAILED(result)) {
slouken@2198
   209
        SetDIerror("IDirectInputDevice::QueryInterface", result);
slouken@1895
   210
        return (-1);
slouken@1895
   211
    }
slouken@1895
   212
slouken@2198
   213
    /* Aquire shared access. Exclusive access is required for forces,
slouken@2198
   214
     * though. */
slouken@1895
   215
    result =
slouken@1895
   216
        IDirectInputDevice2_SetCooperativeLevel(joystick->hwdata->
slouken@2713
   217
                                                InputDevice, SDL_HelperWindow,
slouken@2198
   218
                                                DISCL_EXCLUSIVE |
slouken@1895
   219
                                                DISCL_BACKGROUND);
slouken@2198
   220
    if (FAILED(result)) {
slouken@2198
   221
        SetDIerror("IDirectInputDevice2::SetCooperativeLevel", result);
slouken@1895
   222
        return (-1);
slouken@1895
   223
    }
slouken@1895
   224
slouken@2198
   225
    /* Use the extended data structure: DIJOYSTATE2. */
slouken@1895
   226
    result =
slouken@1895
   227
        IDirectInputDevice2_SetDataFormat(joystick->hwdata->InputDevice,
slouken@2198
   228
                                          &c_dfDIJoystick2);
slouken@2198
   229
    if (FAILED(result)) {
slouken@2198
   230
        SetDIerror("IDirectInputDevice2::SetDataFormat", result);
slouken@1895
   231
        return (-1);
slouken@1895
   232
    }
slouken@1895
   233
slouken@2198
   234
    /* Get device capabilities */
slouken@2198
   235
    result =
slouken@2198
   236
        IDirectInputDevice2_GetCapabilities(joystick->hwdata->InputDevice,
slouken@2198
   237
                                            &joystick->hwdata->Capabilities);
slouken@1895
   238
slouken@2198
   239
    if (FAILED(result)) {
slouken@2198
   240
        SetDIerror("IDirectInputDevice2::GetCapabilities", result);
slouken@2198
   241
        return (-1);
slouken@2198
   242
    }
slouken@2198
   243
slouken@2198
   244
    /* Force capable? */
slouken@2198
   245
    if (joystick->hwdata->Capabilities.dwFlags & DIDC_FORCEFEEDBACK) {
slouken@2198
   246
slouken@2198
   247
        result = IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
slouken@2198
   248
slouken@2198
   249
        if (FAILED(result)) {
slouken@2198
   250
            SetDIerror("IDirectInputDevice2::Acquire", result);
slouken@2198
   251
            return (-1);
slouken@2198
   252
        }
slouken@2198
   253
slouken@2198
   254
        /* reset all accuators. */
slouken@2198
   255
        result =
slouken@2198
   256
            IDirectInputDevice2_SendForceFeedbackCommand(joystick->hwdata->
slouken@2198
   257
                                                         InputDevice,
slouken@2198
   258
                                                         DISFFC_RESET);
slouken@2198
   259
slouken@2198
   260
        if (FAILED(result)) {
slouken@2198
   261
            SetDIerror("IDirectInputDevice2::SendForceFeedbackCommand",
slouken@2198
   262
                       result);
slouken@2198
   263
            return (-1);
slouken@2198
   264
        }
slouken@2198
   265
slouken@2198
   266
        result = IDirectInputDevice2_Unacquire(joystick->hwdata->InputDevice);
slouken@2198
   267
slouken@2198
   268
        if (FAILED(result)) {
slouken@2198
   269
            SetDIerror("IDirectInputDevice2::Unacquire", result);
slouken@2198
   270
            return (-1);
slouken@2198
   271
        }
slouken@2198
   272
slouken@2198
   273
        /* Turn on auto-centering for a ForceFeedback device (until told
slouken@2198
   274
         * otherwise). */
slouken@1895
   275
        dipdw.diph.dwObj = 0;
slouken@1895
   276
        dipdw.diph.dwHow = DIPH_DEVICE;
slouken@2198
   277
        dipdw.dwData = DIPROPAUTOCENTER_ON;
slouken@2198
   278
slouken@1895
   279
        result =
slouken@1895
   280
            IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
slouken@2198
   281
                                            DIPROP_AUTOCENTER, &dipdw.diph);
slouken@1895
   282
slouken@2198
   283
        if (FAILED(result)) {
slouken@2198
   284
            SetDIerror("IDirectInputDevice2::SetProperty", result);
slouken@1895
   285
            return (-1);
slouken@1895
   286
        }
slouken@1895
   287
    }
slouken@1895
   288
slouken@2198
   289
    /* What buttons and axes does it have? */
slouken@2198
   290
    IDirectInputDevice2_EnumObjects(joystick->hwdata->InputDevice,
slouken@2198
   291
                                    EnumDevObjectsCallback, joystick,
slouken@2198
   292
                                    DIDFT_BUTTON | DIDFT_AXIS | DIDFT_POV);
slouken@2198
   293
slouken@2198
   294
    dipdw.diph.dwObj = 0;
slouken@2198
   295
    dipdw.diph.dwHow = DIPH_DEVICE;
slouken@2198
   296
    dipdw.dwData = INPUT_QSIZE;
slouken@2198
   297
slouken@2198
   298
    /* Set the buffer size */
slouken@2198
   299
    result =
slouken@2198
   300
        IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
slouken@2198
   301
                                        DIPROP_BUFFERSIZE, &dipdw.diph);
slouken@2198
   302
slouken@2198
   303
    if (result == DI_POLLEDDEVICE) {
slouken@2198
   304
        /* This device doesn't support buffering, so we're forced
slouken@2198
   305
         * to use less reliable polling. */
slouken@2198
   306
        joystick->hwdata->buffered = 0;
slouken@2198
   307
    } else if (FAILED(result)) {
slouken@2198
   308
        SetDIerror("IDirectInputDevice2::SetProperty", result);
slouken@2198
   309
        return (-1);
slouken@2198
   310
    }
slouken@2198
   311
slouken@1895
   312
    return (0);
slouken@1895
   313
}
slouken@1895
   314
slouken@2198
   315
static BOOL CALLBACK
slouken@2198
   316
EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev, LPVOID pvRef)
slouken@2198
   317
{
slouken@2198
   318
    SDL_Joystick *joystick = (SDL_Joystick *) pvRef;
slouken@2198
   319
    HRESULT result;
slouken@2198
   320
    input_t *in = &joystick->hwdata->Inputs[joystick->hwdata->NumInputs];
slouken@2198
   321
slouken@2198
   322
    in->ofs = dev->dwOfs;
slouken@2198
   323
slouken@2198
   324
    if (dev->dwType & DIDFT_BUTTON) {
slouken@2198
   325
        in->type = BUTTON;
slouken@2198
   326
        in->num = joystick->nbuttons;
slouken@2198
   327
        joystick->nbuttons++;
slouken@2198
   328
    } else if (dev->dwType & DIDFT_POV) {
slouken@2198
   329
        in->type = HAT;
slouken@2198
   330
        in->num = joystick->nhats;
slouken@2198
   331
        joystick->nhats++;
slouken@2198
   332
    } else if (dev->dwType & DIDFT_AXIS) {
slouken@2198
   333
        DIPROPRANGE diprg;
slouken@2198
   334
        DIPROPDWORD dilong;
slouken@2198
   335
slouken@2198
   336
        in->type = AXIS;
slouken@2198
   337
        in->num = joystick->naxes;
slouken@2198
   338
slouken@2198
   339
        diprg.diph.dwSize = sizeof(diprg);
slouken@2198
   340
        diprg.diph.dwHeaderSize = sizeof(diprg.diph);
slouken@2198
   341
        diprg.diph.dwObj = dev->dwOfs;
slouken@2198
   342
        diprg.diph.dwHow = DIPH_BYOFFSET;
slouken@2198
   343
        diprg.lMin = AXIS_MIN;
slouken@2198
   344
        diprg.lMax = AXIS_MAX;
slouken@2198
   345
slouken@2198
   346
        result =
slouken@2198
   347
            IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
slouken@2198
   348
                                            DIPROP_RANGE, &diprg.diph);
slouken@2198
   349
        if (FAILED(result)) {
slouken@2198
   350
            return DIENUM_CONTINUE;     /* don't use this axis */
slouken@2198
   351
        }
slouken@2198
   352
slouken@2198
   353
        /* Set dead zone to 0. */
slouken@2198
   354
        dilong.diph.dwSize = sizeof(dilong);
slouken@2198
   355
        dilong.diph.dwHeaderSize = sizeof(dilong.diph);
slouken@2198
   356
        dilong.diph.dwObj = dev->dwOfs;
slouken@2198
   357
        dilong.diph.dwHow = DIPH_BYOFFSET;
slouken@2198
   358
        dilong.dwData = 0;
slouken@2198
   359
        result =
slouken@2198
   360
            IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
slouken@2198
   361
                                            DIPROP_DEADZONE, &dilong.diph);
slouken@2198
   362
        if (FAILED(result)) {
slouken@2198
   363
            return DIENUM_CONTINUE;     /* don't use this axis */
slouken@2198
   364
        }
slouken@2198
   365
slouken@2198
   366
        joystick->naxes++;
slouken@2198
   367
    } else {
slouken@2198
   368
        /* not supported at this time */
slouken@2198
   369
        return DIENUM_CONTINUE;
slouken@2198
   370
    }
slouken@2198
   371
slouken@2198
   372
    joystick->hwdata->NumInputs++;
slouken@2198
   373
slouken@2198
   374
    if (joystick->hwdata->NumInputs == MAX_INPUTS) {
slouken@2198
   375
        return DIENUM_STOP;     /* too many */
slouken@2198
   376
    }
slouken@2198
   377
slouken@2198
   378
    return DIENUM_CONTINUE;
slouken@2198
   379
}
slouken@2198
   380
slouken@2198
   381
/* Function to update the state of a joystick - called as a device poll.
slouken@2198
   382
 * This function shouldn't update the joystick structure directly,
slouken@2198
   383
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@2198
   384
 * and update joystick device state.
slouken@2198
   385
 */
slouken@2198
   386
void
slouken@2198
   387
SDL_SYS_JoystickUpdate_Polled(SDL_Joystick * joystick)
slouken@2198
   388
{
slouken@2198
   389
    DIJOYSTATE2 state;
slouken@2198
   390
    HRESULT result;
slouken@2198
   391
    int i;
slouken@2198
   392
slouken@2198
   393
    result =
slouken@2198
   394
        IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice,
slouken@2198
   395
                                           sizeof(DIJOYSTATE2), &state);
slouken@2198
   396
    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
slouken@2198
   397
        IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
slouken@2198
   398
        result =
slouken@2198
   399
            IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice,
slouken@2198
   400
                                               sizeof(DIJOYSTATE2), &state);
slouken@2198
   401
    }
slouken@2198
   402
slouken@2198
   403
    /* Set each known axis, button and POV. */
slouken@2198
   404
    for (i = 0; i < joystick->hwdata->NumInputs; ++i) {
slouken@2198
   405
        const input_t *in = &joystick->hwdata->Inputs[i];
slouken@2198
   406
slouken@2198
   407
        switch (in->type) {
slouken@2198
   408
        case AXIS:
slouken@2198
   409
            switch (in->ofs) {
slouken@2198
   410
            case DIJOFS_X:
slouken@2198
   411
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   412
                                            (Sint16) state.lX);
slouken@2198
   413
                break;
slouken@2198
   414
            case DIJOFS_Y:
slouken@2198
   415
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   416
                                            (Sint16) state.lY);
slouken@2198
   417
                break;
slouken@2198
   418
            case DIJOFS_Z:
slouken@2198
   419
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   420
                                            (Sint16) state.lZ);
slouken@2198
   421
                break;
slouken@2198
   422
            case DIJOFS_RX:
slouken@2198
   423
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   424
                                            (Sint16) state.lRx);
slouken@2198
   425
                break;
slouken@2198
   426
            case DIJOFS_RY:
slouken@2198
   427
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   428
                                            (Sint16) state.lRy);
slouken@2198
   429
                break;
slouken@2198
   430
            case DIJOFS_RZ:
slouken@2198
   431
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   432
                                            (Sint16) state.lRz);
slouken@2198
   433
                break;
slouken@2198
   434
            case DIJOFS_SLIDER(0):
slouken@2198
   435
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   436
                                            (Sint16) state.rglSlider[0]);
slouken@2198
   437
                break;
slouken@2198
   438
            case DIJOFS_SLIDER(1):
slouken@2198
   439
                SDL_PrivateJoystickAxis_Int(joystick, in->num,
slouken@2198
   440
                                            (Sint16) state.rglSlider[1]);
slouken@2198
   441
                break;
slouken@2198
   442
            }
slouken@2198
   443
slouken@2198
   444
            break;
slouken@2198
   445
slouken@2198
   446
        case BUTTON:
slouken@2198
   447
            SDL_PrivateJoystickButton_Int(joystick, in->num,
slouken@2198
   448
                                          (Uint8) (state.
slouken@2198
   449
                                                   rgbButtons[in->ofs -
slouken@2198
   450
                                                              DIJOFS_BUTTON0]
slouken@2198
   451
                                                   ? SDL_PRESSED :
slouken@2198
   452
                                                   SDL_RELEASED));
slouken@2198
   453
            break;
slouken@2198
   454
        case HAT:
slouken@2198
   455
            {
slouken@2198
   456
                Uint8 pos = TranslatePOV(state.rgdwPOV[in->ofs -
slouken@2198
   457
                                                       DIJOFS_POV(0)]);
slouken@2198
   458
                SDL_PrivateJoystickHat_Int(joystick, in->num, pos);
slouken@2198
   459
                break;
slouken@2198
   460
            }
slouken@2198
   461
        }
slouken@2198
   462
    }
slouken@2198
   463
}
slouken@2198
   464
slouken@2198
   465
void
slouken@2198
   466
SDL_SYS_JoystickUpdate_Buffered(SDL_Joystick * joystick)
slouken@2198
   467
{
slouken@2198
   468
    int i;
slouken@2198
   469
    HRESULT result;
slouken@2198
   470
    DWORD numevents;
slouken@2198
   471
    DIDEVICEOBJECTDATA evtbuf[INPUT_QSIZE];
slouken@2198
   472
slouken@2198
   473
    numevents = INPUT_QSIZE;
slouken@2198
   474
    result =
slouken@2198
   475
        IDirectInputDevice2_GetDeviceData(joystick->hwdata->InputDevice,
slouken@2198
   476
                                          sizeof(DIDEVICEOBJECTDATA), evtbuf,
slouken@2198
   477
                                          &numevents, 0);
slouken@2198
   478
    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
slouken@2198
   479
        IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
slouken@2198
   480
        result =
slouken@2198
   481
            IDirectInputDevice2_GetDeviceData(joystick->hwdata->InputDevice,
slouken@2198
   482
                                              sizeof(DIDEVICEOBJECTDATA),
slouken@2198
   483
                                              evtbuf, &numevents, 0);
slouken@2198
   484
    }
slouken@2198
   485
slouken@2198
   486
    /* Handle the events or punt */
slouken@2198
   487
    if (FAILED(result))
slouken@2198
   488
        return;
slouken@2198
   489
slouken@2198
   490
    for (i = 0; i < (int) numevents; ++i) {
slouken@2198
   491
        int j;
slouken@2198
   492
slouken@2198
   493
        for (j = 0; j < joystick->hwdata->NumInputs; ++j) {
slouken@2198
   494
            const input_t *in = &joystick->hwdata->Inputs[j];
slouken@2198
   495
slouken@2198
   496
            if (evtbuf[i].dwOfs != in->ofs)
slouken@2198
   497
                continue;
slouken@2198
   498
slouken@2198
   499
            switch (in->type) {
slouken@2198
   500
            case AXIS:
slouken@2198
   501
                SDL_PrivateJoystickAxis(joystick, in->num,
slouken@2198
   502
                                        (Sint16) evtbuf[i].dwData);
slouken@2198
   503
                break;
slouken@2198
   504
            case BUTTON:
slouken@2198
   505
                SDL_PrivateJoystickButton(joystick, in->num,
slouken@2198
   506
                                          (Uint8) (evtbuf[i].
slouken@2198
   507
                                                   dwData ? SDL_PRESSED :
slouken@2198
   508
                                                   SDL_RELEASED));
slouken@2198
   509
                break;
slouken@2198
   510
            case HAT:
slouken@2198
   511
                {
slouken@2198
   512
                    Uint8 pos = TranslatePOV(evtbuf[i].dwData);
slouken@2198
   513
                    SDL_PrivateJoystickHat(joystick, in->num, pos);
slouken@2198
   514
                }
slouken@2198
   515
            }
slouken@2198
   516
        }
slouken@2198
   517
    }
slouken@2198
   518
}
slouken@2198
   519
slouken@2198
   520
slouken@1895
   521
static Uint8
slouken@1895
   522
TranslatePOV(DWORD value)
slouken@1895
   523
{
slouken@1895
   524
    const int HAT_VALS[] = {
slouken@1895
   525
        SDL_HAT_UP,
slouken@1895
   526
        SDL_HAT_UP | SDL_HAT_RIGHT,
slouken@1895
   527
        SDL_HAT_RIGHT,
slouken@1895
   528
        SDL_HAT_DOWN | SDL_HAT_RIGHT,
slouken@1895
   529
        SDL_HAT_DOWN,
slouken@1895
   530
        SDL_HAT_DOWN | SDL_HAT_LEFT,
slouken@1895
   531
        SDL_HAT_LEFT,
slouken@1895
   532
        SDL_HAT_UP | SDL_HAT_LEFT
slouken@1895
   533
    };
slouken@1895
   534
slouken@1895
   535
    if (LOWORD(value) == 0xFFFF)
slouken@1895
   536
        return SDL_HAT_CENTERED;
slouken@1895
   537
slouken@1895
   538
    /* Round the value up: */
slouken@1895
   539
    value += 4500 / 2;
slouken@1895
   540
    value %= 36000;
slouken@1895
   541
    value /= 4500;
slouken@1895
   542
slouken@1895
   543
    if (value >= 8)
slouken@1895
   544
        return SDL_HAT_CENTERED;        /* shouldn't happen */
slouken@1895
   545
slouken@1895
   546
    return HAT_VALS[value];
slouken@1895
   547
}
slouken@1895
   548
slouken@1895
   549
/* SDL_PrivateJoystick* doesn't discard duplicate events, so we need to
slouken@1895
   550
 * do it. */
slouken@1895
   551
static int
slouken@1895
   552
SDL_PrivateJoystickAxis_Int(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
slouken@1895
   553
{
slouken@1895
   554
    if (joystick->axes[axis] != value)
slouken@1895
   555
        return SDL_PrivateJoystickAxis(joystick, axis, value);
slouken@1895
   556
    return 0;
slouken@1895
   557
}
slouken@1895
   558
slouken@1895
   559
static int
slouken@1895
   560
SDL_PrivateJoystickHat_Int(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
slouken@1895
   561
{
slouken@1895
   562
    if (joystick->hats[hat] != value)
slouken@1895
   563
        return SDL_PrivateJoystickHat(joystick, hat, value);
slouken@1895
   564
    return 0;
slouken@1895
   565
}
slouken@1895
   566
slouken@1895
   567
static int
slouken@1895
   568
SDL_PrivateJoystickButton_Int(SDL_Joystick * joystick, Uint8 button,
slouken@1895
   569
                              Uint8 state)
slouken@1895
   570
{
slouken@1895
   571
    if (joystick->buttons[button] != state)
slouken@1895
   572
        return SDL_PrivateJoystickButton(joystick, button, state);
slouken@1895
   573
    return 0;
slouken@1895
   574
}
slouken@1895
   575
slouken@1895
   576
void
slouken@1895
   577
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
slouken@1895
   578
{
slouken@1895
   579
    HRESULT result;
slouken@1895
   580
slouken@1895
   581
    result = IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
slouken@1895
   582
    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
slouken@1895
   583
        IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
slouken@1895
   584
        IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
slouken@1895
   585
    }
slouken@1895
   586
slouken@1895
   587
    if (joystick->hwdata->buffered)
slouken@1895
   588
        SDL_SYS_JoystickUpdate_Buffered(joystick);
slouken@1895
   589
    else
slouken@1895
   590
        SDL_SYS_JoystickUpdate_Polled(joystick);
slouken@1895
   591
}
slouken@1895
   592
slouken@1895
   593
/* Function to close a joystick after use */
slouken@1895
   594
void
slouken@1895
   595
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
slouken@1895
   596
{
slouken@1895
   597
    IDirectInputDevice2_Unacquire(joystick->hwdata->InputDevice);
slouken@1895
   598
    IDirectInputDevice2_Release(joystick->hwdata->InputDevice);
slouken@1895
   599
slouken@1895
   600
    if (joystick->hwdata != NULL) {
slouken@1895
   601
        /* free system specific hardware data */
slouken@2713
   602
        SDL_free(joystick->hwdata);
slouken@1895
   603
    }
slouken@1895
   604
}
slouken@1895
   605
slouken@1895
   606
/* Function to perform any system-specific joystick related cleanup */
slouken@1895
   607
void
slouken@1895
   608
SDL_SYS_JoystickQuit(void)
slouken@1895
   609
{
slouken@1895
   610
    IDirectInput_Release(dinput);
slouken@1895
   611
    dinput = NULL;
slouken@1895
   612
}
slouken@1895
   613
slouken@1895
   614
#endif /* SDL_JOYSTICK_DINPUT */
slouken@1895
   615
/* vi: set ts=4 sw=4 expandtab: */