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