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