src/haptic/windows/SDL_syshaptic.c
author Gabriel Jacobo <gabomdq@gmail.com>
Mon, 24 Feb 2014 10:25:02 -0300
changeset 8247 fd220c4df418
parent 8185 c2996dc8ef6b
child 8281 f280c6b5d08c
permissions -rw-r--r--
Zero out haptic linked list items on creation

(thanks to Turo Lamminen for the report!)
slouken@2713
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@2713
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@2713
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@2713
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@2713
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@2713
    22
slouken@2713
    23
#ifdef SDL_HAPTIC_DINPUT
slouken@2713
    24
icculus@6990
    25
#include "SDL_assert.h"
icculus@7481
    26
#include "SDL_thread.h"
icculus@7481
    27
#include "SDL_mutex.h"
icculus@7481
    28
#include "SDL_timer.h"
icculus@6990
    29
#include "SDL_hints.h"
slouken@2713
    30
#include "SDL_haptic.h"
slouken@2713
    31
#include "../SDL_syshaptic.h"
slouken@2713
    32
#include "SDL_joystick.h"
slouken@2713
    33
#include "../../joystick/SDL_sysjoystick.h"     /* For the real SDL_Joystick */
slouken@5062
    34
#include "../../joystick/windows/SDL_dxjoystick_c.h"      /* For joystick hwdata */
slouken@2713
    35
icculus@8179
    36
#include "SDL_syshaptic_c.h"
slouken@2713
    37
slouken@2713
    38
/*
slouken@2713
    39
 * List of available haptic devices.
slouken@2713
    40
 */
icculus@8179
    41
typedef struct SDL_hapticlist_item
slouken@2713
    42
{
slouken@2713
    43
    DIDEVICEINSTANCE instance;
slouken@5090
    44
    char *name;
slouken@2713
    45
    SDL_Haptic *haptic;
slouken@2713
    46
    DIDEVCAPS capabilities;
slouken@7191
    47
    Uint8 bXInputHaptic; /* Supports force feedback via XInput. */
slouken@7191
    48
    Uint8 userid; /* XInput userid index for this joystick */
icculus@8179
    49
    struct SDL_hapticlist_item *next;
icculus@8179
    50
} SDL_hapticlist_item;
slouken@2713
    51
slouken@2713
    52
slouken@2713
    53
/*
slouken@2713
    54
 * Haptic system hardware data.
slouken@2713
    55
 */
slouken@2713
    56
struct haptic_hwdata
slouken@2713
    57
{
icculus@6716
    58
    LPDIRECTINPUTDEVICE8 device;
slouken@2713
    59
    DWORD axes[3];              /* Axes to use. */
slouken@7319
    60
    SDL_bool is_joystick;       /* Device is loaded as joystick. */
slouken@7191
    61
    Uint8 bXInputHaptic; /* Supports force feedback via XInput. */
slouken@7191
    62
    Uint8 userid; /* XInput userid index for this joystick */
icculus@7481
    63
    SDL_Thread *thread;
icculus@7481
    64
    SDL_mutex *mutex;
icculus@7481
    65
    volatile Uint32 stopTicks;
icculus@7481
    66
    volatile int stopThread;
slouken@2713
    67
};
slouken@2713
    68
slouken@2713
    69
slouken@2713
    70
/*
slouken@2713
    71
 * Haptic system effect data.
slouken@2713
    72
 */
slouken@2713
    73
struct haptic_hweffect
slouken@2713
    74
{
slouken@2713
    75
    DIEFFECT effect;
slouken@2713
    76
    LPDIRECTINPUTEFFECT ref;
icculus@6990
    77
    XINPUT_VIBRATION vibration;
slouken@2713
    78
};
slouken@2713
    79
slouken@2713
    80
slouken@2713
    81
/*
slouken@2713
    82
 * Internal stuff.
slouken@2713
    83
 */
icculus@5591
    84
static SDL_bool coinitialized = SDL_FALSE;
icculus@6716
    85
static LPDIRECTINPUT8 dinput = NULL;
icculus@6990
    86
static SDL_bool loaded_xinput = SDL_FALSE;
icculus@8179
    87
static SDL_hapticlist_item *SDL_hapticlist = NULL;
icculus@8179
    88
static SDL_hapticlist_item *SDL_hapticlist_tail = NULL;
icculus@8179
    89
static int numhaptics = 0;
slouken@2713
    90
slouken@2713
    91
/*
slouken@2713
    92
 * External stuff.
slouken@2713
    93
 */
slouken@2713
    94
extern HWND SDL_HelperWindow;
slouken@2713
    95
slouken@2713
    96
slouken@2713
    97
/*
slouken@2713
    98
 * Prototypes.
slouken@2713
    99
 */
icculus@7037
   100
static int DI_SetError(const char *str, HRESULT err);
slouken@2713
   101
static int DI_GUIDIsSame(const GUID * a, const GUID * b);
slouken@2713
   102
static int SDL_SYS_HapticOpenFromInstance(SDL_Haptic * haptic,
slouken@2713
   103
                                          DIDEVICEINSTANCE instance);
icculus@6716
   104
static int SDL_SYS_HapticOpenFromDevice8(SDL_Haptic * haptic,
slouken@7319
   105
                                         LPDIRECTINPUTDEVICE8 device8,
slouken@7319
   106
                                         SDL_bool is_joystick);
icculus@8179
   107
static int SDL_SYS_HapticOpenFromXInput(SDL_Haptic * haptic, const Uint8 userid);
slouken@2713
   108
static DWORD DIGetTriggerButton(Uint16 button);
slouken@2713
   109
static int SDL_SYS_SetDirection(DIEFFECT * effect, SDL_HapticDirection * dir,
slouken@2713
   110
                                int naxes);
slouken@2713
   111
static int SDL_SYS_ToDIEFFECT(SDL_Haptic * haptic, DIEFFECT * dest,
slouken@2713
   112
                              SDL_HapticEffect * src);
slouken@2713
   113
static void SDL_SYS_HapticFreeDIEFFECT(DIEFFECT * effect, int type);
slouken@2713
   114
static REFGUID SDL_SYS_HapticEffectType(SDL_HapticEffect * effect);
icculus@7481
   115
static int SDLCALL SDL_RunXInputHaptic(void *arg);
icculus@7481
   116
slouken@2713
   117
/* Callbacks. */
slouken@2713
   118
static BOOL CALLBACK EnumHapticsCallback(const DIDEVICEINSTANCE *
slouken@2713
   119
                                         pdidInstance, VOID * pContext);
slouken@2713
   120
static BOOL CALLBACK DI_EffectCallback(LPCDIEFFECTINFO pei, LPVOID pv);
slouken@2713
   121
slouken@2713
   122
slouken@7191
   123
/*
slouken@2713
   124
 * Like SDL_SetError but for DX error codes.
slouken@2713
   125
 */
icculus@7037
   126
static int
slouken@2713
   127
DI_SetError(const char *str, HRESULT err)
slouken@2713
   128
{
slouken@2786
   129
    /*
slouken@2786
   130
       SDL_SetError("Haptic: %s - %s: %s", str,
slouken@2786
   131
       DXGetErrorString8A(err), DXGetErrorDescription8A(err));
slouken@2786
   132
     */
icculus@7037
   133
    return SDL_SetError("Haptic error %s", str);
slouken@2713
   134
}
slouken@2713
   135
slouken@2713
   136
slouken@2713
   137
/*
slouken@2713
   138
 * Checks to see if two GUID are the same.
slouken@2713
   139
 */
slouken@2713
   140
static int
slouken@2713
   141
DI_GUIDIsSame(const GUID * a, const GUID * b)
slouken@2713
   142
{
icculus@6403
   143
    return (SDL_memcmp(a, b, sizeof (GUID)) == 0);
slouken@2713
   144
}
slouken@2713
   145
slouken@2713
   146
slouken@2713
   147
/*
slouken@2713
   148
 * Initializes the haptic subsystem.
slouken@2713
   149
 */
slouken@2713
   150
int
slouken@2713
   151
SDL_SYS_HapticInit(void)
slouken@2713
   152
{
icculus@6990
   153
    const char *env = SDL_GetHint(SDL_HINT_XINPUT_ENABLED);
slouken@2713
   154
    HRESULT ret;
slouken@2713
   155
    HINSTANCE instance;
slouken@2713
   156
slouken@2713
   157
    if (dinput != NULL) {       /* Already open. */
icculus@7037
   158
        return SDL_SetError("Haptic: SubSystem already open.");
slouken@2713
   159
    }
slouken@2713
   160
icculus@5591
   161
    ret = WIN_CoInitialize();
slouken@2713
   162
    if (FAILED(ret)) {
icculus@7037
   163
        return DI_SetError("Coinitialize", ret);
slouken@2713
   164
    }
slouken@2713
   165
icculus@5591
   166
    coinitialized = SDL_TRUE;
icculus@5591
   167
icculus@6716
   168
    ret = CoCreateInstance(&CLSID_DirectInput8, NULL, CLSCTX_INPROC_SERVER,
icculus@6716
   169
                           &IID_IDirectInput8, (LPVOID) & dinput);
slouken@2713
   170
    if (FAILED(ret)) {
icculus@5591
   171
        SDL_SYS_HapticQuit();
icculus@7037
   172
        return DI_SetError("CoCreateInstance", ret);
slouken@2713
   173
    }
slouken@2713
   174
slouken@2713
   175
    /* Because we used CoCreateInstance, we need to Initialize it, first. */
slouken@2713
   176
    instance = GetModuleHandle(NULL);
slouken@2713
   177
    if (instance == NULL) {
icculus@5591
   178
        SDL_SYS_HapticQuit();
icculus@7037
   179
        return SDL_SetError("GetModuleHandle() failed with error code %d.",
icculus@7037
   180
                            GetLastError());
slouken@2713
   181
    }
icculus@6716
   182
    ret = IDirectInput8_Initialize(dinput, instance, DIRECTINPUT_VERSION);
slouken@2713
   183
    if (FAILED(ret)) {
icculus@5591
   184
        SDL_SYS_HapticQuit();
icculus@7037
   185
        return DI_SetError("Initializing DirectInput device", ret);
slouken@2713
   186
    }
slouken@2713
   187
slouken@2713
   188
    /* Look for haptic devices. */
icculus@6716
   189
    ret = IDirectInput8_EnumDevices(dinput,
slouken@2714
   190
                                   0,
slouken@2713
   191
                                   EnumHapticsCallback,
slouken@2713
   192
                                   NULL,
slouken@2713
   193
                                   DIEDFL_FORCEFEEDBACK |
slouken@2713
   194
                                   DIEDFL_ATTACHEDONLY);
slouken@2713
   195
    if (FAILED(ret)) {
icculus@5591
   196
        SDL_SYS_HapticQuit();
icculus@7037
   197
        return DI_SetError("Enumerating DirectInput devices", ret);
slouken@2713
   198
    }
slouken@2713
   199
icculus@6990
   200
    if (!env || SDL_atoi(env)) {
icculus@6990
   201
        loaded_xinput = (WIN_LoadXInputDLL() == 0);
icculus@6990
   202
    }
icculus@6990
   203
icculus@6990
   204
    if (loaded_xinput) {
icculus@6990
   205
        DWORD i;
icculus@8179
   206
        for (i = 0; i < SDL_XINPUT_MAX_DEVICES; i++) {
icculus@8179
   207
            XInputHaptic_MaybeAddDevice(i);
icculus@6990
   208
        }
icculus@6990
   209
    }
icculus@6990
   210
icculus@8179
   211
    return numhaptics;
slouken@2713
   212
}
slouken@2713
   213
icculus@8179
   214
icculus@8179
   215
int
icculus@8179
   216
DirectInputHaptic_MaybeAddDevice(const DIDEVICEINSTANCE * pdidInstance)
icculus@8179
   217
{
icculus@8179
   218
    HRESULT ret;
icculus@8179
   219
    LPDIRECTINPUTDEVICE8 device;
icculus@8179
   220
    const DWORD needflags = DIDC_ATTACHED | DIDC_FORCEFEEDBACK;
icculus@8179
   221
    DIDEVCAPS capabilities;
icculus@8179
   222
    SDL_hapticlist_item *item = NULL;
icculus@8179
   223
icculus@8185
   224
    if (dinput == NULL) {
icculus@8185
   225
        return -1;  /* not initialized. We'll pick these up on enumeration if we init later. */
icculus@8185
   226
    }
icculus@8185
   227
icculus@8179
   228
    /* Make sure we don't already have it */
icculus@8179
   229
    for (item = SDL_hapticlist; item; item = item->next) {
icculus@8179
   230
        if ( (!item->bXInputHaptic) && (SDL_memcmp(&item->instance, pdidInstance, sizeof (*pdidInstance)) == 0) ) {
icculus@8179
   231
            return -1;  /* Already added */
icculus@8179
   232
        }
icculus@8179
   233
    }
icculus@8179
   234
icculus@8179
   235
    /* Open the device */
icculus@8179
   236
    ret = IDirectInput8_CreateDevice(dinput, &pdidInstance->guidInstance, &device, NULL);
icculus@8179
   237
    if (FAILED(ret)) {
icculus@8179
   238
        /* DI_SetError("Creating DirectInput device",ret); */
icculus@8179
   239
        return -1;
icculus@8179
   240
    }
icculus@8179
   241
icculus@8179
   242
    /* Get capabilities. */
icculus@8179
   243
    SDL_zero(capabilities);
icculus@8179
   244
    capabilities.dwSize = sizeof (DIDEVCAPS);
icculus@8179
   245
    ret = IDirectInputDevice8_GetCapabilities(device, &capabilities);
icculus@8179
   246
    IDirectInputDevice8_Release(device);
icculus@8179
   247
    if (FAILED(ret)) {
icculus@8179
   248
        /* DI_SetError("Getting device capabilities",ret); */
icculus@8179
   249
        return -1;
icculus@8179
   250
    }
icculus@8179
   251
icculus@8179
   252
    if ((capabilities.dwFlags & needflags) != needflags) {
icculus@8179
   253
        return -1;  /* not a device we can use. */
icculus@8179
   254
    }
icculus@8179
   255
gabomdq@8247
   256
    item = (SDL_hapticlist_item *)SDL_calloc(1, sizeof(SDL_hapticlist_item));
icculus@8179
   257
    if (item == NULL) {
icculus@8179
   258
        return SDL_OutOfMemory();
icculus@8179
   259
    }
icculus@8179
   260
icculus@8179
   261
    item->name = WIN_StringToUTF8(pdidInstance->tszProductName);
icculus@8179
   262
    if (!item->name) {
icculus@8179
   263
        SDL_free(item);
icculus@8179
   264
        return -1;
icculus@8179
   265
    }
icculus@8179
   266
icculus@8179
   267
    /* Copy the instance over, useful for creating devices. */
icculus@8179
   268
    SDL_memcpy(&item->instance, pdidInstance, sizeof (DIDEVICEINSTANCE));
icculus@8179
   269
    SDL_memcpy(&item->capabilities, &capabilities, sizeof (capabilities));
icculus@8179
   270
icculus@8179
   271
    if (SDL_hapticlist_tail == NULL) {
icculus@8179
   272
        SDL_hapticlist = SDL_hapticlist_tail = item;
icculus@8179
   273
    } else {
icculus@8179
   274
        SDL_hapticlist_tail->next = item;
icculus@8179
   275
        SDL_hapticlist_tail = item;
icculus@8179
   276
    }
icculus@8179
   277
icculus@8179
   278
    /* Device has been added. */
icculus@8179
   279
    ++numhaptics;
icculus@8179
   280
icculus@8179
   281
    return numhaptics;
icculus@8179
   282
}
icculus@8179
   283
icculus@8179
   284
icculus@8179
   285
int
icculus@8179
   286
DirectInputHaptic_MaybeRemoveDevice(const DIDEVICEINSTANCE * pdidInstance)
icculus@8179
   287
{
icculus@8179
   288
    SDL_hapticlist_item *item;
icculus@8179
   289
    SDL_hapticlist_item *prev = NULL;
icculus@8179
   290
icculus@8185
   291
    if (dinput == NULL) {
icculus@8185
   292
        return -1;  /* not initialized, ignore this. */
icculus@8185
   293
    }
icculus@8185
   294
icculus@8179
   295
    for (item = SDL_hapticlist; item != NULL; item = item->next) {
icculus@8179
   296
        if ( (!item->bXInputHaptic) && (SDL_memcmp(&item->instance, pdidInstance, sizeof (*pdidInstance)) == 0) ) {
icculus@8179
   297
            /* found it, remove it. */
icculus@8179
   298
            const int retval = item->haptic ? item->haptic->index : -1;
icculus@8179
   299
            if (prev != NULL) {
icculus@8179
   300
                prev->next = item->next;
icculus@8179
   301
            } else {
icculus@8179
   302
                SDL_assert(SDL_hapticlist == item);
icculus@8179
   303
                SDL_hapticlist = item->next;
icculus@8179
   304
            }
icculus@8179
   305
            if (item == SDL_hapticlist_tail) {
icculus@8179
   306
                SDL_hapticlist_tail = prev;
icculus@8179
   307
            }
icculus@8179
   308
            --numhaptics;
icculus@8179
   309
            /* !!! TODO: Send a haptic remove event? */
icculus@8179
   310
            SDL_free(item);
icculus@8179
   311
            return retval;
icculus@8179
   312
        }
icculus@8179
   313
        prev = item;
icculus@8179
   314
    }
icculus@8179
   315
icculus@8179
   316
    return -1;
icculus@8179
   317
}
icculus@8179
   318
icculus@8179
   319
icculus@8179
   320
int
icculus@8179
   321
XInputHaptic_MaybeAddDevice(const DWORD dwUserid)
icculus@8179
   322
{
icculus@8179
   323
    const Uint8 userid = (Uint8) dwUserid;
icculus@8179
   324
    XINPUT_CAPABILITIES caps;
icculus@8179
   325
    const SDL_bool bIs14OrLater = (SDL_XInputVersion >= ((1<<16)|4));
icculus@8179
   326
    SDL_hapticlist_item *item;
icculus@8179
   327
icculus@8179
   328
    if ((!loaded_xinput) || (dwUserid >= SDL_XINPUT_MAX_DEVICES)) {
icculus@8179
   329
        return -1;
icculus@8179
   330
    }
icculus@8179
   331
icculus@8179
   332
    /* Make sure we don't already have it */
icculus@8179
   333
    for (item = SDL_hapticlist; item; item = item->next) {
icculus@8179
   334
        if ((item->bXInputHaptic) && (item->userid == userid)) {
icculus@8179
   335
            return -1;  /* Already added */
icculus@8179
   336
        }
icculus@8179
   337
    }
icculus@8179
   338
icculus@8179
   339
    if (XINPUTGETCAPABILITIES(dwUserid, XINPUT_FLAG_GAMEPAD, &caps) != ERROR_SUCCESS) {
icculus@8179
   340
        return -1;  /* maybe controller isn't plugged in. */
icculus@8179
   341
    }
icculus@8179
   342
icculus@8179
   343
    /* XInput < 1.4 is probably only for original XBox360 controllers,
icculus@8179
   344
        which don't offer the flag, and always have force feedback */
icculus@8179
   345
    if ( (bIs14OrLater) && ((caps.Flags & XINPUT_CAPS_FFB_SUPPORTED) == 0) ) {
icculus@8179
   346
        return -1;  /* no force feedback on this device. */
icculus@8179
   347
    }
icculus@8179
   348
icculus@8179
   349
    item = (SDL_hapticlist_item *)SDL_malloc( sizeof(SDL_hapticlist_item));
icculus@8179
   350
    if (item == NULL) {
icculus@8179
   351
        return SDL_OutOfMemory();
icculus@8179
   352
    }
icculus@8179
   353
icculus@8179
   354
    SDL_zerop(item);
icculus@8179
   355
icculus@8179
   356
    /* !!! FIXME: I'm not bothering to query for a real name right now (can we even?) */
icculus@8179
   357
    {
icculus@8179
   358
        char buf[64];
icculus@8179
   359
        SDL_snprintf(buf, sizeof (buf), "XInput Controller #%u", (unsigned int) (userid+1));
icculus@8179
   360
        item->name = SDL_strdup(buf);
icculus@8179
   361
    }
icculus@8179
   362
icculus@8179
   363
    if (!item->name) {
icculus@8179
   364
        SDL_free(item);
icculus@8179
   365
        return -1;
icculus@8179
   366
    }
icculus@8179
   367
icculus@8179
   368
    /* Copy the instance over, useful for creating devices. */
icculus@8179
   369
    item->bXInputHaptic = 1;
icculus@8179
   370
    item->userid = userid;
icculus@8179
   371
icculus@8179
   372
    if (SDL_hapticlist_tail == NULL) {
icculus@8179
   373
        SDL_hapticlist = SDL_hapticlist_tail = item;
icculus@8179
   374
    } else {
icculus@8179
   375
        SDL_hapticlist_tail->next = item;
icculus@8179
   376
        SDL_hapticlist_tail = item;
icculus@8179
   377
    }
icculus@8179
   378
icculus@8179
   379
    /* Device has been added. */
icculus@8179
   380
    ++numhaptics;
icculus@8179
   381
icculus@8179
   382
    return numhaptics;
icculus@8179
   383
}
icculus@8179
   384
icculus@8179
   385
icculus@8179
   386
int
icculus@8179
   387
XInputHaptic_MaybeRemoveDevice(const DWORD dwUserid)
icculus@8179
   388
{
icculus@8179
   389
    const Uint8 userid = (Uint8) dwUserid;
icculus@8179
   390
    SDL_hapticlist_item *item;
icculus@8179
   391
    SDL_hapticlist_item *prev = NULL;
icculus@8179
   392
icculus@8179
   393
    if ((!loaded_xinput) || (dwUserid >= SDL_XINPUT_MAX_DEVICES)) {
icculus@8179
   394
        return -1;
icculus@8179
   395
    }
icculus@8179
   396
icculus@8179
   397
    for (item = SDL_hapticlist; item != NULL; item = item->next) {
icculus@8179
   398
        if ((item->bXInputHaptic) && (item->userid == userid)) {
icculus@8179
   399
            /* found it, remove it. */
icculus@8179
   400
            const int retval = item->haptic ? item->haptic->index : -1;
icculus@8179
   401
            if (prev != NULL) {
icculus@8179
   402
                prev->next = item->next;
icculus@8179
   403
            } else {
icculus@8179
   404
                SDL_assert(SDL_hapticlist == item);
icculus@8179
   405
                SDL_hapticlist = item->next;
icculus@8179
   406
            }
icculus@8179
   407
            if (item == SDL_hapticlist_tail) {
icculus@8179
   408
                SDL_hapticlist_tail = prev;
icculus@8179
   409
            }
icculus@8179
   410
            --numhaptics;
icculus@8179
   411
            /* !!! TODO: Send a haptic remove event? */
icculus@8179
   412
            SDL_free(item);
icculus@8179
   413
            return retval;
icculus@8179
   414
        }
icculus@8179
   415
        prev = item;
icculus@8179
   416
    }
icculus@8179
   417
icculus@8179
   418
    return -1;
icculus@8179
   419
}
icculus@8179
   420
icculus@8179
   421
slouken@2713
   422
/*
slouken@2713
   423
 * Callback to find the haptic devices.
slouken@2713
   424
 */
slouken@2713
   425
static BOOL CALLBACK
slouken@2713
   426
EnumHapticsCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
slouken@2713
   427
{
icculus@8179
   428
    (void) pContext;
icculus@8179
   429
    DirectInputHaptic_MaybeAddDevice(pdidInstance);
icculus@8179
   430
    return DIENUM_CONTINUE;  /* continue enumerating */
icculus@8179
   431
}
slouken@2713
   432
slouken@2713
   433
icculus@8179
   434
int
icculus@8179
   435
SDL_SYS_NumHaptics()
icculus@8179
   436
{
icculus@8179
   437
    return numhaptics;
icculus@8179
   438
}
icculus@8179
   439
icculus@8179
   440
static SDL_hapticlist_item *
icculus@8179
   441
HapticByDevIndex(int device_index)
icculus@8179
   442
{
icculus@8179
   443
    SDL_hapticlist_item *item = SDL_hapticlist;
icculus@8179
   444
icculus@8179
   445
    if ((device_index < 0) || (device_index >= numhaptics)) {
icculus@8179
   446
        return NULL;
slouken@2713
   447
    }
slouken@2713
   448
icculus@8179
   449
    while (device_index > 0) {
icculus@8179
   450
        SDL_assert(item != NULL);
urkle@8183
   451
        --device_index;
icculus@8179
   452
        item = item->next;
slouken@2713
   453
    }
slouken@2713
   454
icculus@8179
   455
    return item;
slouken@2713
   456
}
slouken@2713
   457
slouken@2713
   458
/*
slouken@2713
   459
 * Return the name of a haptic device, does not need to be opened.
slouken@2713
   460
 */
slouken@2713
   461
const char *
slouken@2713
   462
SDL_SYS_HapticName(int index)
slouken@2713
   463
{
icculus@8179
   464
    SDL_hapticlist_item *item = HapticByDevIndex(index);
icculus@8179
   465
    return item->name;
slouken@2713
   466
}
slouken@2713
   467
slouken@2713
   468
slouken@2713
   469
/*
slouken@2713
   470
 * Callback to get all supported effects.
slouken@2713
   471
 */
slouken@2713
   472
#define EFFECT_TEST(e,s)               \
slouken@2713
   473
if (DI_GUIDIsSame(&pei->guid, &(e)))   \
slouken@2713
   474
   haptic->supported |= (s)
slouken@2713
   475
static BOOL CALLBACK
slouken@2713
   476
DI_EffectCallback(LPCDIEFFECTINFO pei, LPVOID pv)
slouken@2713
   477
{
slouken@2713
   478
    /* Prepare the haptic device. */
slouken@2713
   479
    SDL_Haptic *haptic = (SDL_Haptic *) pv;
slouken@2713
   480
slouken@2713
   481
    /* Get supported. */
slouken@2713
   482
    EFFECT_TEST(GUID_Spring, SDL_HAPTIC_SPRING);
slouken@2713
   483
    EFFECT_TEST(GUID_Damper, SDL_HAPTIC_DAMPER);
slouken@2713
   484
    EFFECT_TEST(GUID_Inertia, SDL_HAPTIC_INERTIA);
slouken@2713
   485
    EFFECT_TEST(GUID_Friction, SDL_HAPTIC_FRICTION);
slouken@2713
   486
    EFFECT_TEST(GUID_ConstantForce, SDL_HAPTIC_CONSTANT);
slouken@2713
   487
    EFFECT_TEST(GUID_CustomForce, SDL_HAPTIC_CUSTOM);
slouken@2713
   488
    EFFECT_TEST(GUID_Sine, SDL_HAPTIC_SINE);
icculus@7621
   489
    /* !!! FIXME: put this back when we have more bits in 2.1 */
gabomdq@7678
   490
    /* EFFECT_TEST(GUID_Square, SDL_HAPTIC_SQUARE); */
slouken@2713
   491
    EFFECT_TEST(GUID_Triangle, SDL_HAPTIC_TRIANGLE);
slouken@2713
   492
    EFFECT_TEST(GUID_SawtoothUp, SDL_HAPTIC_SAWTOOTHUP);
slouken@2713
   493
    EFFECT_TEST(GUID_SawtoothDown, SDL_HAPTIC_SAWTOOTHDOWN);
slouken@2713
   494
    EFFECT_TEST(GUID_RampForce, SDL_HAPTIC_RAMP);
slouken@2713
   495
slouken@2713
   496
    /* Check for more. */
slouken@2713
   497
    return DIENUM_CONTINUE;
slouken@2713
   498
}
slouken@2713
   499
slouken@2713
   500
slouken@2713
   501
/*
slouken@2713
   502
 * Callback to get supported axes.
slouken@2713
   503
 */
slouken@2713
   504
static BOOL CALLBACK
slouken@2713
   505
DI_DeviceObjectCallback(LPCDIDEVICEOBJECTINSTANCE dev, LPVOID pvRef)
slouken@2713
   506
{
slouken@2713
   507
    SDL_Haptic *haptic = (SDL_Haptic *) pvRef;
slouken@2713
   508
slouken@2713
   509
    if ((dev->dwType & DIDFT_AXIS) && (dev->dwFlags & DIDOI_FFACTUATOR)) {
slouken@2713
   510
slouken@2713
   511
        haptic->hwdata->axes[haptic->naxes] = dev->dwOfs;
slouken@2713
   512
        haptic->naxes++;
slouken@2713
   513
slouken@2713
   514
        /* Currently using the artificial limit of 3 axes. */
slouken@2713
   515
        if (haptic->naxes >= 3) {
slouken@2713
   516
            return DIENUM_STOP;
slouken@2713
   517
        }
slouken@2713
   518
    }
slouken@2713
   519
slouken@2713
   520
    return DIENUM_CONTINUE;
slouken@2713
   521
}
slouken@2713
   522
slouken@2713
   523
slouken@2713
   524
/*
slouken@2713
   525
 * Opens the haptic device from the file descriptor.
slouken@2713
   526
 *
slouken@2713
   527
 *    Steps:
slouken@2713
   528
 *       - Open temporary DirectInputDevice interface.
icculus@6716
   529
 *       - Create DirectInputDevice8 interface.
slouken@2713
   530
 *       - Release DirectInputDevice interface.
icculus@6716
   531
 *       - Call SDL_SYS_HapticOpenFromDevice8
slouken@2713
   532
 */
slouken@2713
   533
static int
slouken@2713
   534
SDL_SYS_HapticOpenFromInstance(SDL_Haptic * haptic, DIDEVICEINSTANCE instance)
slouken@2713
   535
{
slouken@2713
   536
    HRESULT ret;
slouken@2713
   537
    int ret2;
icculus@6716
   538
    LPDIRECTINPUTDEVICE8 device;
slouken@7319
   539
    LPDIRECTINPUTDEVICE8 device8;
slouken@2713
   540
slouken@2713
   541
    /* Open the device */
icculus@6716
   542
    ret = IDirectInput8_CreateDevice(dinput, &instance.guidInstance,
slouken@2713
   543
                                    &device, NULL);
slouken@2713
   544
    if (FAILED(ret)) {
slouken@2713
   545
        DI_SetError("Creating DirectInput device", ret);
slouken@7319
   546
        return -1;
slouken@2713
   547
    }
slouken@2713
   548
icculus@6716
   549
    /* Now get the IDirectInputDevice8 interface, instead. */
icculus@6716
   550
    ret = IDirectInputDevice8_QueryInterface(device,
icculus@6716
   551
                                            &IID_IDirectInputDevice8,
slouken@7319
   552
                                            (LPVOID *) &device8);
slouken@2713
   553
    /* Done with the temporary one now. */
icculus@6716
   554
    IDirectInputDevice8_Release(device);
slouken@2713
   555
    if (FAILED(ret)) {
slouken@2713
   556
        DI_SetError("Querying DirectInput interface", ret);
slouken@7319
   557
        return -1;
slouken@2713
   558
    }
slouken@2713
   559
slouken@7319
   560
    ret2 = SDL_SYS_HapticOpenFromDevice8(haptic, device8, SDL_FALSE);
slouken@2713
   561
    if (ret2 < 0) {
slouken@7319
   562
        IDirectInputDevice8_Release(device8);
slouken@7319
   563
        return -1;
slouken@2713
   564
    }
slouken@2713
   565
slouken@2713
   566
    return 0;
slouken@2713
   567
}
slouken@2713
   568
icculus@6990
   569
static int
icculus@8179
   570
SDL_SYS_HapticOpenFromXInput(SDL_Haptic *haptic, const Uint8 userid)
icculus@6990
   571
{
icculus@7481
   572
    char threadName[32];
icculus@6990
   573
    XINPUT_VIBRATION vibration = { 0, 0 };  /* stop any current vibration */
icculus@6990
   574
    XINPUTSETSTATE(userid, &vibration);
icculus@6990
   575
icculus@7621
   576
    haptic->supported = SDL_HAPTIC_LEFTRIGHT;
icculus@6990
   577
icculus@6990
   578
    haptic->neffects = 1;
icculus@6990
   579
    haptic->nplaying = 1;
icculus@6990
   580
icculus@6990
   581
    /* Prepare effects memory. */
icculus@6990
   582
    haptic->effects = (struct haptic_effect *)
icculus@6990
   583
        SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
icculus@6990
   584
    if (haptic->effects == NULL) {
icculus@7037
   585
        return SDL_OutOfMemory();
icculus@6990
   586
    }
icculus@6990
   587
    /* Clear the memory */
icculus@6990
   588
    SDL_memset(haptic->effects, 0,
icculus@6990
   589
               sizeof(struct haptic_effect) * haptic->neffects);
icculus@6990
   590
icculus@6990
   591
    haptic->hwdata = (struct haptic_hwdata *) SDL_malloc(sizeof(*haptic->hwdata));
icculus@6990
   592
    if (haptic->hwdata == NULL) {
icculus@6990
   593
        SDL_free(haptic->effects);
icculus@6990
   594
        haptic->effects = NULL;
icculus@7037
   595
        return SDL_OutOfMemory();
icculus@6990
   596
    }
icculus@6990
   597
    SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
icculus@6990
   598
icculus@6990
   599
    haptic->hwdata->bXInputHaptic = 1;
icculus@6990
   600
    haptic->hwdata->userid = userid;
icculus@6990
   601
icculus@7481
   602
    haptic->hwdata->mutex = SDL_CreateMutex();
icculus@7481
   603
    if (haptic->hwdata->mutex == NULL) {
icculus@7481
   604
        SDL_free(haptic->effects);
icculus@7481
   605
        SDL_free(haptic->hwdata);
icculus@7481
   606
        haptic->effects = NULL;
icculus@7481
   607
        return SDL_SetError("Couldn't create XInput haptic mutex");
icculus@7481
   608
    }
icculus@7481
   609
icculus@7481
   610
    SDL_snprintf(threadName, sizeof (threadName), "SDLXInputDev%d", (int) userid);
icculus@7481
   611
icculus@7481
   612
#if defined(__WIN32__) && !defined(HAVE_LIBC)  /* !!! FIXME: this is nasty. */
icculus@7481
   613
    #undef SDL_CreateThread
icculus@8094
   614
    #if SDL_DYNAMIC_API
icculus@8094
   615
    haptic->hwdata->thread = SDL_CreateThread_REAL(SDL_RunXInputHaptic, threadName, haptic->hwdata, NULL, NULL);
icculus@8094
   616
    #else
icculus@7481
   617
    haptic->hwdata->thread = SDL_CreateThread(SDL_RunXInputHaptic, threadName, haptic->hwdata, NULL, NULL);
icculus@8094
   618
    #endif
icculus@7481
   619
#else
icculus@7481
   620
    haptic->hwdata->thread = SDL_CreateThread(SDL_RunXInputHaptic, threadName, haptic->hwdata);
icculus@7481
   621
#endif
icculus@7481
   622
    if (haptic->hwdata->thread == NULL) {
icculus@7481
   623
        SDL_DestroyMutex(haptic->hwdata->mutex);
icculus@7481
   624
        SDL_free(haptic->effects);
icculus@7481
   625
        SDL_free(haptic->hwdata);
icculus@7481
   626
        haptic->effects = NULL;
icculus@7481
   627
        return SDL_SetError("Couldn't create XInput haptic thread");
icculus@7481
   628
    }
icculus@7481
   629
icculus@6990
   630
    return 0;
icculus@6990
   631
 }
slouken@2713
   632
slouken@2713
   633
/*
slouken@2713
   634
 * Opens the haptic device from the file descriptor.
slouken@2713
   635
 *
slouken@2713
   636
 *    Steps:
slouken@2713
   637
 *       - Set cooperative level.
slouken@2713
   638
 *       - Set data format.
slouken@2713
   639
 *       - Acquire exclusiveness.
slouken@2713
   640
 *       - Reset actuators.
philipp@7132
   641
 *       - Get supported features.
slouken@2713
   642
 */
slouken@2713
   643
static int
icculus@6716
   644
SDL_SYS_HapticOpenFromDevice8(SDL_Haptic * haptic,
slouken@7319
   645
                              LPDIRECTINPUTDEVICE8 device8, SDL_bool is_joystick)
slouken@2713
   646
{
slouken@2713
   647
    HRESULT ret;
slouken@2713
   648
    DIPROPDWORD dipdw;
slouken@2713
   649
slouken@7319
   650
    /* Allocate the hwdata */
slouken@7319
   651
    haptic->hwdata = (struct haptic_hwdata *)SDL_malloc(sizeof(*haptic->hwdata));
slouken@7319
   652
    if (haptic->hwdata == NULL) {
slouken@7319
   653
        return SDL_OutOfMemory();
slouken@7319
   654
    }
slouken@7319
   655
    SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
slouken@7319
   656
icculus@6716
   657
    /* We'll use the device8 from now on. */
icculus@6716
   658
    haptic->hwdata->device = device8;
slouken@7319
   659
    haptic->hwdata->is_joystick = is_joystick;
slouken@2713
   660
slouken@2713
   661
    /* Grab it exclusively to use force feedback stuff. */
icculus@6716
   662
    ret = IDirectInputDevice8_SetCooperativeLevel(haptic->hwdata->device,
slouken@2713
   663
                                                  SDL_HelperWindow,
slouken@2713
   664
                                                  DISCL_EXCLUSIVE |
slouken@2713
   665
                                                  DISCL_BACKGROUND);
slouken@2713
   666
    if (FAILED(ret)) {
slouken@2713
   667
        DI_SetError("Setting cooperative level to exclusive", ret);
slouken@2713
   668
        goto acquire_err;
slouken@2713
   669
    }
slouken@2713
   670
slouken@2713
   671
    /* Set data format. */
icculus@6716
   672
    ret = IDirectInputDevice8_SetDataFormat(haptic->hwdata->device,
slouken@2713
   673
                                            &c_dfDIJoystick2);
slouken@2713
   674
    if (FAILED(ret)) {
slouken@2713
   675
        DI_SetError("Setting data format", ret);
slouken@2713
   676
        goto acquire_err;
slouken@2713
   677
    }
slouken@2713
   678
slouken@2713
   679
    /* Get number of axes. */
icculus@6716
   680
    ret = IDirectInputDevice8_EnumObjects(haptic->hwdata->device,
slouken@2713
   681
                                          DI_DeviceObjectCallback,
slouken@2713
   682
                                          haptic, DIDFT_AXIS);
slouken@2713
   683
    if (FAILED(ret)) {
slouken@2713
   684
        DI_SetError("Getting device axes", ret);
slouken@2713
   685
        goto acquire_err;
slouken@2713
   686
    }
slouken@2713
   687
slouken@2713
   688
    /* Acquire the device. */
icculus@6716
   689
    ret = IDirectInputDevice8_Acquire(haptic->hwdata->device);
slouken@2713
   690
    if (FAILED(ret)) {
slouken@2713
   691
        DI_SetError("Acquiring DirectInput device", ret);
slouken@2713
   692
        goto acquire_err;
slouken@2713
   693
    }
slouken@2713
   694
slouken@2713
   695
    /* Reset all actuators - just in case. */
icculus@6716
   696
    ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
slouken@2713
   697
                                                       DISFFC_RESET);
slouken@2713
   698
    if (FAILED(ret)) {
slouken@2713
   699
        DI_SetError("Resetting device", ret);
slouken@2713
   700
        goto acquire_err;
slouken@2713
   701
    }
slouken@2713
   702
slouken@2713
   703
    /* Enabling actuators. */
icculus@6716
   704
    ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
slouken@2713
   705
                                                       DISFFC_SETACTUATORSON);
slouken@2713
   706
    if (FAILED(ret)) {
slouken@2713
   707
        DI_SetError("Enabling actuators", ret);
slouken@2713
   708
        goto acquire_err;
slouken@2713
   709
    }
slouken@2713
   710
slouken@2713
   711
    /* Get supported effects. */
icculus@6716
   712
    ret = IDirectInputDevice8_EnumEffects(haptic->hwdata->device,
slouken@2713
   713
                                          DI_EffectCallback, haptic,
slouken@2713
   714
                                          DIEFT_ALL);
slouken@2713
   715
    if (FAILED(ret)) {
slouken@2713
   716
        DI_SetError("Enumerating supported effects", ret);
slouken@2713
   717
        goto acquire_err;
slouken@2713
   718
    }
slouken@2713
   719
    if (haptic->supported == 0) {       /* Error since device supports nothing. */
slouken@2713
   720
        SDL_SetError("Haptic: Internal error on finding supported effects.");
slouken@2713
   721
        goto acquire_err;
slouken@2713
   722
    }
slouken@2713
   723
slouken@2713
   724
    /* Check autogain and autocenter. */
slouken@2713
   725
    dipdw.diph.dwSize = sizeof(DIPROPDWORD);
slouken@2713
   726
    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
slouken@2713
   727
    dipdw.diph.dwObj = 0;
slouken@2713
   728
    dipdw.diph.dwHow = DIPH_DEVICE;
slouken@2713
   729
    dipdw.dwData = 10000;
icculus@6716
   730
    ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device,
slouken@2713
   731
                                          DIPROP_FFGAIN, &dipdw.diph);
slouken@2713
   732
    if (!FAILED(ret)) {         /* Gain is supported. */
slouken@2713
   733
        haptic->supported |= SDL_HAPTIC_GAIN;
slouken@2713
   734
    }
slouken@2713
   735
    dipdw.diph.dwObj = 0;
slouken@2713
   736
    dipdw.diph.dwHow = DIPH_DEVICE;
slouken@2713
   737
    dipdw.dwData = DIPROPAUTOCENTER_OFF;
icculus@6716
   738
    ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device,
slouken@2713
   739
                                          DIPROP_AUTOCENTER, &dipdw.diph);
slouken@2713
   740
    if (!FAILED(ret)) {         /* Autocenter is supported. */
slouken@2713
   741
        haptic->supported |= SDL_HAPTIC_AUTOCENTER;
slouken@2713
   742
    }
slouken@2713
   743
slouken@2713
   744
    /* Status is always supported. */
slouken@2713
   745
    haptic->supported |= SDL_HAPTIC_STATUS | SDL_HAPTIC_PAUSE;
slouken@2713
   746
slouken@2713
   747
    /* Check maximum effects. */
slouken@2713
   748
    haptic->neffects = 128;     /* This is not actually supported as thus under windows,
slouken@2713
   749
                                   there is no way to tell the number of EFFECTS that a
slouken@2713
   750
                                   device can hold, so we'll just use a "random" number
slouken@2713
   751
                                   instead and put warnings in SDL_haptic.h */
slouken@2713
   752
    haptic->nplaying = 128;     /* Even more impossible to get this then neffects. */
slouken@2713
   753
slouken@2713
   754
    /* Prepare effects memory. */
slouken@2713
   755
    haptic->effects = (struct haptic_effect *)
slouken@2713
   756
        SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
slouken@2713
   757
    if (haptic->effects == NULL) {
slouken@2713
   758
        SDL_OutOfMemory();
slouken@2713
   759
        goto acquire_err;
slouken@2713
   760
    }
slouken@2713
   761
    /* Clear the memory */
slouken@2713
   762
    SDL_memset(haptic->effects, 0,
slouken@2713
   763
               sizeof(struct haptic_effect) * haptic->neffects);
slouken@2713
   764
slouken@2713
   765
    return 0;
slouken@2713
   766
slouken@2713
   767
    /* Error handling */
slouken@2713
   768
  acquire_err:
icculus@6716
   769
    IDirectInputDevice8_Unacquire(haptic->hwdata->device);
slouken@2713
   770
    return -1;
slouken@2713
   771
slouken@2713
   772
}
slouken@2713
   773
slouken@2713
   774
slouken@2713
   775
/*
slouken@2713
   776
 * Opens a haptic device for usage.
slouken@2713
   777
 */
slouken@2713
   778
int
slouken@2713
   779
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
slouken@2713
   780
{
icculus@8179
   781
    SDL_hapticlist_item *item = HapticByDevIndex(haptic->index);
icculus@8179
   782
    return (item->bXInputHaptic) ? SDL_SYS_HapticOpenFromXInput(haptic, item->userid) : SDL_SYS_HapticOpenFromInstance(haptic, item->instance);
slouken@2713
   783
}
slouken@2713
   784
slouken@2713
   785
slouken@2713
   786
/*
slouken@2713
   787
 * Opens a haptic device from first mouse it finds for usage.
slouken@2713
   788
 */
slouken@2713
   789
int
slouken@2713
   790
SDL_SYS_HapticMouse(void)
slouken@2713
   791
{
icculus@8179
   792
    SDL_hapticlist_item *item;
urkle@8183
   793
    int index = 0;
slouken@2713
   794
slouken@2713
   795
    /* Grab the first mouse haptic device we find. */
icculus@8179
   796
    for (item = SDL_hapticlist; item != NULL; item = item->next) {
icculus@8179
   797
        SDL_assert(index >= 0);
icculus@8179
   798
        if (item->capabilities.dwDevType == DI8DEVCLASS_POINTER ) {
icculus@8179
   799
            return index;
slouken@2713
   800
        }
urkle@8183
   801
        ++index;
slouken@2713
   802
    }
slouken@2713
   803
slouken@2713
   804
    return -1;
slouken@2713
   805
}
slouken@2713
   806
slouken@2713
   807
slouken@2713
   808
/*
slouken@2713
   809
 * Checks to see if a joystick has haptic features.
slouken@2713
   810
 */
slouken@2713
   811
int
slouken@2713
   812
SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
slouken@2713
   813
{
icculus@6990
   814
    const struct joystick_hwdata *hwdata = joystick->hwdata;
icculus@6990
   815
    return ( (hwdata->bXInputHaptic) ||
icculus@6990
   816
             ((hwdata->Capabilities.dwFlags & DIDC_FORCEFEEDBACK) != 0) );
slouken@2713
   817
}
slouken@2713
   818
slouken@2713
   819
slouken@2713
   820
/*
icculus@7708
   821
 * Checks to see if the haptic device and joystick are in reality the same.
slouken@2713
   822
 */
slouken@2713
   823
int
slouken@2713
   824
SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
slouken@2713
   825
{
icculus@7848
   826
    if (joystick->hwdata->bXInputHaptic != haptic->hwdata->bXInputHaptic) {
icculus@7846
   827
        return 0;  /* one is XInput, one is not; not the same device. */
icculus@7846
   828
    } else if (joystick->hwdata->bXInputHaptic) {  /* XInput */
icculus@7846
   829
        return (haptic->hwdata->userid == joystick->hwdata->userid);
icculus@7846
   830
    } else {  /* DirectInput */
icculus@6990
   831
        HRESULT ret;
icculus@6990
   832
        DIDEVICEINSTANCE hap_instance, joy_instance;
slouken@2713
   833
icculus@6990
   834
        hap_instance.dwSize = sizeof(DIDEVICEINSTANCE);
icculus@6990
   835
        joy_instance.dwSize = sizeof(DIDEVICEINSTANCE);
icculus@6990
   836
icculus@6990
   837
        /* Get the device instances. */
icculus@6990
   838
        ret = IDirectInputDevice8_GetDeviceInfo(haptic->hwdata->device,
slouken@2713
   839
                                            &hap_instance);
icculus@6990
   840
        if (FAILED(ret)) {
icculus@6990
   841
            return 0;
icculus@6990
   842
        }
icculus@6990
   843
        ret = IDirectInputDevice8_GetDeviceInfo(joystick->hwdata->InputDevice,
icculus@6990
   844
                                                &joy_instance);
icculus@6990
   845
        if (FAILED(ret)) {
icculus@6990
   846
            return 0;
icculus@6990
   847
        }
icculus@6990
   848
icculus@6990
   849
        if (DI_GUIDIsSame(&hap_instance.guidInstance, &joy_instance.guidInstance))
icculus@6990
   850
            return 1;
slouken@2713
   851
    }
slouken@2713
   852
slouken@2713
   853
    return 0;
slouken@2713
   854
}
slouken@2713
   855
slouken@2713
   856
slouken@2713
   857
/*
slouken@2713
   858
 * Opens a SDL_Haptic from a SDL_Joystick.
slouken@2713
   859
 */
slouken@2713
   860
int
slouken@2713
   861
SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
slouken@2713
   862
{
icculus@8179
   863
    SDL_hapticlist_item *item;
urkle@8183
   864
    int index = 0;
slouken@5358
   865
slouken@5358
   866
    /* Since it comes from a joystick we have to try to match it with a haptic device on our haptic list. */
icculus@6990
   867
    if (joystick->hwdata->bXInputDevice) {
icculus@6990
   868
        const Uint8 userid = joystick->hwdata->userid;
icculus@8179
   869
        for (item = SDL_hapticlist; item != NULL; item = item->next) {
icculus@8179
   870
            if ((item->bXInputHaptic) && (item->userid == userid)) {
icculus@6990
   871
                SDL_assert(joystick->hwdata->bXInputHaptic);
icculus@8179
   872
                haptic->index = index;
icculus@8179
   873
                return SDL_SYS_HapticOpenFromXInput(haptic, userid);
icculus@6990
   874
            }
urkle@8183
   875
            ++index;
slouken@5358
   876
        }
icculus@6990
   877
    } else {
icculus@8179
   878
        HRESULT idret;
icculus@8179
   879
        DIDEVICEINSTANCE joy_instance;
icculus@8179
   880
icculus@8179
   881
        joy_instance.dwSize = sizeof(DIDEVICEINSTANCE);
icculus@8179
   882
        idret = IDirectInputDevice8_GetDeviceInfo(joystick->hwdata->InputDevice, &joy_instance);
icculus@8179
   883
        if (FAILED(idret)) {
icculus@8179
   884
            return -1;
icculus@8179
   885
        }
icculus@8179
   886
icculus@8179
   887
        for (item = SDL_hapticlist; item != NULL; item = item->next) {
icculus@8179
   888
            if (DI_GUIDIsSame(&item->instance.guidInstance, &joy_instance.guidInstance)) {
icculus@8179
   889
                haptic->index = index;
slouken@7319
   890
                return SDL_SYS_HapticOpenFromDevice8(haptic, joystick->hwdata->InputDevice, SDL_TRUE);
icculus@6990
   891
            }
urkle@8183
   892
            ++index;
slouken@5358
   893
        }
slouken@5358
   894
    }
icculus@8179
   895
slouken@7319
   896
    /* No match to our haptic list */
slouken@7319
   897
    return -1;
slouken@2713
   898
}
slouken@2713
   899
slouken@2713
   900
slouken@2713
   901
/*
slouken@2713
   902
 * Closes the haptic device.
slouken@2713
   903
 */
slouken@2713
   904
void
slouken@2713
   905
SDL_SYS_HapticClose(SDL_Haptic * haptic)
slouken@2713
   906
{
slouken@2713
   907
    if (haptic->hwdata) {
slouken@2713
   908
slouken@2713
   909
        /* Free effects. */
slouken@2713
   910
        SDL_free(haptic->effects);
slouken@2713
   911
        haptic->effects = NULL;
slouken@2713
   912
        haptic->neffects = 0;
slouken@2713
   913
slouken@2713
   914
        /* Clean up */
icculus@7481
   915
        if (haptic->hwdata->bXInputHaptic) {
icculus@7481
   916
            haptic->hwdata->stopThread = 1;
icculus@7481
   917
            SDL_WaitThread(haptic->hwdata->thread, NULL);
icculus@7481
   918
            SDL_DestroyMutex(haptic->hwdata->mutex);
icculus@7481
   919
        } else {
icculus@6990
   920
            IDirectInputDevice8_Unacquire(haptic->hwdata->device);
icculus@6990
   921
            /* Only release if isn't grabbed by a joystick. */
icculus@6990
   922
            if (haptic->hwdata->is_joystick == 0) {
icculus@6990
   923
                IDirectInputDevice8_Release(haptic->hwdata->device);
icculus@6990
   924
            }
slouken@2713
   925
        }
slouken@2713
   926
slouken@2713
   927
        /* Free */
slouken@2713
   928
        SDL_free(haptic->hwdata);
slouken@2713
   929
        haptic->hwdata = NULL;
slouken@2713
   930
    }
slouken@2713
   931
}
slouken@2713
   932
slouken@2713
   933
slouken@7191
   934
/*
slouken@2713
   935
 * Clean up after system specific haptic stuff
slouken@2713
   936
 */
slouken@2713
   937
void
slouken@2713
   938
SDL_SYS_HapticQuit(void)
slouken@2713
   939
{
icculus@8179
   940
    SDL_hapticlist_item *item;
icculus@8179
   941
    SDL_hapticlist_item *next = NULL;
slouken@5090
   942
icculus@6990
   943
    if (loaded_xinput) {
icculus@6990
   944
        WIN_UnloadXInputDLL();
icculus@6990
   945
        loaded_xinput = SDL_FALSE;
icculus@6990
   946
    }
icculus@6990
   947
icculus@8179
   948
    for (item = SDL_hapticlist; item; item = next) {
icculus@8179
   949
        /* Opened and not closed haptics are leaked, this is on purpose.
icculus@8179
   950
         * Close your haptic devices after usage. */
icculus@8179
   951
        next = item->next;
icculus@8179
   952
        SDL_free(item->name);
icculus@8179
   953
        SDL_free(item);
slouken@5090
   954
    }
slouken@5090
   955
icculus@5591
   956
    if (dinput != NULL) {
icculus@6716
   957
        IDirectInput8_Release(dinput);
icculus@5591
   958
        dinput = NULL;
icculus@5591
   959
    }
icculus@5591
   960
icculus@5591
   961
    if (coinitialized) {
icculus@5591
   962
        WIN_CoUninitialize();
icculus@5591
   963
        coinitialized = SDL_FALSE;
icculus@5591
   964
    }
slouken@2713
   965
}
slouken@2713
   966
slouken@2713
   967
slouken@2713
   968
/*
slouken@2713
   969
 * Converts an SDL trigger button to an DIEFFECT trigger button.
slouken@2713
   970
 */
slouken@2713
   971
static DWORD
slouken@2713
   972
DIGetTriggerButton(Uint16 button)
slouken@2713
   973
{
slouken@2713
   974
    DWORD dwTriggerButton;
slouken@2713
   975
slouken@2713
   976
    dwTriggerButton = DIEB_NOTRIGGER;
slouken@2713
   977
slouken@2713
   978
    if (button != 0) {
slouken@2713
   979
        dwTriggerButton = DIJOFS_BUTTON(button - 1);
slouken@2713
   980
    }
slouken@2713
   981
slouken@2713
   982
    return dwTriggerButton;
slouken@2713
   983
}
slouken@2713
   984
slouken@2713
   985
slouken@2713
   986
/*
slouken@2713
   987
 * Sets the direction.
slouken@2713
   988
 */
slouken@2713
   989
static int
slouken@2713
   990
SDL_SYS_SetDirection(DIEFFECT * effect, SDL_HapticDirection * dir, int naxes)
slouken@2713
   991
{
slouken@2713
   992
    LONG *rglDir;
slouken@2713
   993
slouken@2713
   994
    /* Handle no axes a part. */
slouken@2713
   995
    if (naxes == 0) {
slouken@2713
   996
        effect->dwFlags |= DIEFF_SPHERICAL;     /* Set as default. */
slouken@2713
   997
        effect->rglDirection = NULL;
slouken@2713
   998
        return 0;
slouken@2713
   999
    }
slouken@2713
  1000
slouken@2713
  1001
    /* Has axes. */
slouken@2713
  1002
    rglDir = SDL_malloc(sizeof(LONG) * naxes);
slouken@2713
  1003
    if (rglDir == NULL) {
icculus@7037
  1004
        return SDL_OutOfMemory();
slouken@2713
  1005
    }
slouken@2713
  1006
    SDL_memset(rglDir, 0, sizeof(LONG) * naxes);
slouken@2713
  1007
    effect->rglDirection = rglDir;
slouken@2713
  1008
slouken@2713
  1009
    switch (dir->type) {
slouken@2713
  1010
    case SDL_HAPTIC_POLAR:
slouken@2713
  1011
        effect->dwFlags |= DIEFF_POLAR;
slouken@2713
  1012
        rglDir[0] = dir->dir[0];
slouken@2713
  1013
        return 0;
slouken@2713
  1014
    case SDL_HAPTIC_CARTESIAN:
slouken@2713
  1015
        effect->dwFlags |= DIEFF_CARTESIAN;
slouken@2713
  1016
        rglDir[0] = dir->dir[0];
slouken@2713
  1017
        if (naxes > 1)
slouken@2713
  1018
            rglDir[1] = dir->dir[1];
slouken@2713
  1019
        if (naxes > 2)
slouken@2713
  1020
            rglDir[2] = dir->dir[2];
slouken@2713
  1021
        return 0;
slouken@2713
  1022
    case SDL_HAPTIC_SPHERICAL:
slouken@2713
  1023
        effect->dwFlags |= DIEFF_SPHERICAL;
slouken@2713
  1024
        rglDir[0] = dir->dir[0];
slouken@2713
  1025
        if (naxes > 1)
slouken@2713
  1026
            rglDir[1] = dir->dir[1];
slouken@2713
  1027
        if (naxes > 2)
slouken@2713
  1028
            rglDir[2] = dir->dir[2];
slouken@2713
  1029
        return 0;
slouken@2713
  1030
slouken@2713
  1031
    default:
icculus@7037
  1032
        return SDL_SetError("Haptic: Unknown direction type.");
slouken@2713
  1033
    }
slouken@2713
  1034
}
slouken@2713
  1035
slouken@2713
  1036
#define CONVERT(x)   (((x) > 0x7FFF) ? 10000 : ((x)*10000) / 0x7FFF)
slouken@2713
  1037
/*
slouken@2713
  1038
 * Creates the DIEFFECT from a SDL_HapticEffect.
slouken@2713
  1039
 */
slouken@2713
  1040
static int
slouken@2713
  1041
SDL_SYS_ToDIEFFECT(SDL_Haptic * haptic, DIEFFECT * dest,
slouken@2713
  1042
                   SDL_HapticEffect * src)
slouken@2713
  1043
{
slouken@2713
  1044
    int i;
slouken@2713
  1045
    DICONSTANTFORCE *constant;
slouken@2713
  1046
    DIPERIODIC *periodic;
slouken@2713
  1047
    DICONDITION *condition;     /* Actually an array of conditions - one per axis. */
slouken@2713
  1048
    DIRAMPFORCE *ramp;
slouken@2713
  1049
    DICUSTOMFORCE *custom;
slouken@2713
  1050
    DIENVELOPE *envelope;
slouken@2713
  1051
    SDL_HapticConstant *hap_constant;
slouken@2713
  1052
    SDL_HapticPeriodic *hap_periodic;
slouken@2713
  1053
    SDL_HapticCondition *hap_condition;
slouken@2713
  1054
    SDL_HapticRamp *hap_ramp;
slouken@2713
  1055
    SDL_HapticCustom *hap_custom;
slouken@2713
  1056
    DWORD *axes;
slouken@2713
  1057
slouken@2713
  1058
    /* Set global stuff. */
slouken@2713
  1059
    SDL_memset(dest, 0, sizeof(DIEFFECT));
slouken@2713
  1060
    dest->dwSize = sizeof(DIEFFECT);    /* Set the structure size. */
slouken@2713
  1061
    dest->dwSamplePeriod = 0;   /* Not used by us. */
slouken@2713
  1062
    dest->dwGain = 10000;       /* Gain is set globally, not locally. */
slouken@2713
  1063
    dest->dwFlags = DIEFF_OBJECTOFFSETS;        /* Seems obligatory. */
slouken@2713
  1064
slouken@2713
  1065
    /* Envelope. */
slouken@2713
  1066
    envelope = SDL_malloc(sizeof(DIENVELOPE));
slouken@2713
  1067
    if (envelope == NULL) {
icculus@7037
  1068
        return SDL_OutOfMemory();
slouken@2713
  1069
    }
slouken@2713
  1070
    SDL_memset(envelope, 0, sizeof(DIENVELOPE));
slouken@2713
  1071
    dest->lpEnvelope = envelope;
slouken@2713
  1072
    envelope->dwSize = sizeof(DIENVELOPE);      /* Always should be this. */
slouken@2713
  1073
slouken@2713
  1074
    /* Axes. */
slouken@2713
  1075
    dest->cAxes = haptic->naxes;
slouken@2713
  1076
    if (dest->cAxes > 0) {
slouken@2713
  1077
        axes = SDL_malloc(sizeof(DWORD) * dest->cAxes);
slouken@2713
  1078
        if (axes == NULL) {
icculus@7037
  1079
            return SDL_OutOfMemory();
slouken@2713
  1080
        }
slouken@2713
  1081
        axes[0] = haptic->hwdata->axes[0];      /* Always at least one axis. */
slouken@2713
  1082
        if (dest->cAxes > 1) {
slouken@2713
  1083
            axes[1] = haptic->hwdata->axes[1];
slouken@2713
  1084
        }
slouken@2713
  1085
        if (dest->cAxes > 2) {
slouken@2713
  1086
            axes[2] = haptic->hwdata->axes[2];
slouken@2713
  1087
        }
slouken@2713
  1088
        dest->rgdwAxes = axes;
slouken@2713
  1089
    }
slouken@2713
  1090
slouken@2713
  1091
philipp@7132
  1092
    /* The big type handling switch, even bigger then Linux's version. */
slouken@2713
  1093
    switch (src->type) {
slouken@2713
  1094
    case SDL_HAPTIC_CONSTANT:
slouken@2713
  1095
        hap_constant = &src->constant;
slouken@2713
  1096
        constant = SDL_malloc(sizeof(DICONSTANTFORCE));
slouken@2713
  1097
        if (constant == NULL) {
icculus@7037
  1098
            return SDL_OutOfMemory();
slouken@2713
  1099
        }
slouken@2713
  1100
        SDL_memset(constant, 0, sizeof(DICONSTANTFORCE));
slouken@2713
  1101
slouken@2713
  1102
        /* Specifics */
slouken@2713
  1103
        constant->lMagnitude = CONVERT(hap_constant->level);
slouken@2713
  1104
        dest->cbTypeSpecificParams = sizeof(DICONSTANTFORCE);
slouken@2713
  1105
        dest->lpvTypeSpecificParams = constant;
slouken@2713
  1106
slouken@2713
  1107
        /* Generics */
slouken@2713
  1108
        dest->dwDuration = hap_constant->length * 1000; /* In microseconds. */
slouken@2713
  1109
        dest->dwTriggerButton = DIGetTriggerButton(hap_constant->button);
slouken@2713
  1110
        dest->dwTriggerRepeatInterval = hap_constant->interval;
slouken@2713
  1111
        dest->dwStartDelay = hap_constant->delay * 1000;        /* In microseconds. */
slouken@2713
  1112
slouken@2713
  1113
        /* Direction. */
slouken@2713
  1114
        if (SDL_SYS_SetDirection(dest, &hap_constant->direction, dest->cAxes)
slouken@2713
  1115
            < 0) {
slouken@2713
  1116
            return -1;
slouken@2713
  1117
        }
slouken@2713
  1118
slouken@2713
  1119
        /* Envelope */
slouken@2713
  1120
        if ((hap_constant->attack_length == 0)
slouken@2713
  1121
            && (hap_constant->fade_length == 0)) {
slouken@2713
  1122
            SDL_free(dest->lpEnvelope);
slouken@2713
  1123
            dest->lpEnvelope = NULL;
slouken@2713
  1124
        } else {
slouken@2713
  1125
            envelope->dwAttackLevel = CONVERT(hap_constant->attack_level);
slouken@2713
  1126
            envelope->dwAttackTime = hap_constant->attack_length * 1000;
slouken@2713
  1127
            envelope->dwFadeLevel = CONVERT(hap_constant->fade_level);
slouken@2713
  1128
            envelope->dwFadeTime = hap_constant->fade_length * 1000;
slouken@2713
  1129
        }
slouken@2713
  1130
slouken@2713
  1131
        break;
slouken@2713
  1132
slouken@2713
  1133
    case SDL_HAPTIC_SINE:
icculus@7621
  1134
    /* !!! FIXME: put this back when we have more bits in 2.1 */
gabomdq@7678
  1135
    /* case SDL_HAPTIC_SQUARE: */
slouken@2713
  1136
    case SDL_HAPTIC_TRIANGLE:
slouken@2713
  1137
    case SDL_HAPTIC_SAWTOOTHUP:
slouken@2713
  1138
    case SDL_HAPTIC_SAWTOOTHDOWN:
slouken@2713
  1139
        hap_periodic = &src->periodic;
slouken@2713
  1140
        periodic = SDL_malloc(sizeof(DIPERIODIC));
slouken@2713
  1141
        if (periodic == NULL) {
icculus@7037
  1142
            return SDL_OutOfMemory();
slouken@2713
  1143
        }
slouken@2713
  1144
        SDL_memset(periodic, 0, sizeof(DIPERIODIC));
slouken@2713
  1145
slouken@2713
  1146
        /* Specifics */
slouken@2713
  1147
        periodic->dwMagnitude = CONVERT(hap_periodic->magnitude);
slouken@2713
  1148
        periodic->lOffset = CONVERT(hap_periodic->offset);
slouken@2713
  1149
        periodic->dwPhase = hap_periodic->phase;
slouken@2713
  1150
        periodic->dwPeriod = hap_periodic->period * 1000;
slouken@2713
  1151
        dest->cbTypeSpecificParams = sizeof(DIPERIODIC);
slouken@2713
  1152
        dest->lpvTypeSpecificParams = periodic;
slouken@2713
  1153
slouken@2713
  1154
        /* Generics */
slouken@2713
  1155
        dest->dwDuration = hap_periodic->length * 1000; /* In microseconds. */
slouken@2713
  1156
        dest->dwTriggerButton = DIGetTriggerButton(hap_periodic->button);
slouken@2713
  1157
        dest->dwTriggerRepeatInterval = hap_periodic->interval;
slouken@2713
  1158
        dest->dwStartDelay = hap_periodic->delay * 1000;        /* In microseconds. */
slouken@2713
  1159
slouken@2713
  1160
        /* Direction. */
slouken@2713
  1161
        if (SDL_SYS_SetDirection(dest, &hap_periodic->direction, dest->cAxes)
slouken@2713
  1162
            < 0) {
slouken@2713
  1163
            return -1;
slouken@2713
  1164
        }
slouken@2713
  1165
slouken@2713
  1166
        /* Envelope */
slouken@2713
  1167
        if ((hap_periodic->attack_length == 0)
slouken@2713
  1168
            && (hap_periodic->fade_length == 0)) {
slouken@2713
  1169
            SDL_free(dest->lpEnvelope);
slouken@2713
  1170
            dest->lpEnvelope = NULL;
slouken@2713
  1171
        } else {
slouken@2713
  1172
            envelope->dwAttackLevel = CONVERT(hap_periodic->attack_level);
slouken@2713
  1173
            envelope->dwAttackTime = hap_periodic->attack_length * 1000;
slouken@2713
  1174
            envelope->dwFadeLevel = CONVERT(hap_periodic->fade_level);
slouken@2713
  1175
            envelope->dwFadeTime = hap_periodic->fade_length * 1000;
slouken@2713
  1176
        }
slouken@2713
  1177
slouken@2713
  1178
        break;
slouken@2713
  1179
slouken@2713
  1180
    case SDL_HAPTIC_SPRING:
slouken@2713
  1181
    case SDL_HAPTIC_DAMPER:
slouken@2713
  1182
    case SDL_HAPTIC_INERTIA:
slouken@2713
  1183
    case SDL_HAPTIC_FRICTION:
slouken@2713
  1184
        hap_condition = &src->condition;
slouken@2713
  1185
        condition = SDL_malloc(sizeof(DICONDITION) * dest->cAxes);
slouken@2713
  1186
        if (condition == NULL) {
icculus@7037
  1187
            return SDL_OutOfMemory();
slouken@2713
  1188
        }
slouken@2713
  1189
        SDL_memset(condition, 0, sizeof(DICONDITION));
slouken@2713
  1190
slouken@2713
  1191
        /* Specifics */
slouken@2713
  1192
        for (i = 0; i < (int) dest->cAxes; i++) {
slouken@2713
  1193
            condition[i].lOffset = CONVERT(hap_condition->center[i]);
slouken@2713
  1194
            condition[i].lPositiveCoefficient =
slouken@2713
  1195
                CONVERT(hap_condition->right_coeff[i]);
slouken@2713
  1196
            condition[i].lNegativeCoefficient =
slouken@2713
  1197
                CONVERT(hap_condition->left_coeff[i]);
slouken@2713
  1198
            condition[i].dwPositiveSaturation =
slouken@2713
  1199
                CONVERT(hap_condition->right_sat[i]);
slouken@2713
  1200
            condition[i].dwNegativeSaturation =
slouken@2713
  1201
                CONVERT(hap_condition->left_sat[i]);
slouken@2713
  1202
            condition[i].lDeadBand = CONVERT(hap_condition->deadband[i]);
slouken@2713
  1203
        }
slouken@2713
  1204
        dest->cbTypeSpecificParams = sizeof(DICONDITION) * dest->cAxes;
slouken@2713
  1205
        dest->lpvTypeSpecificParams = condition;
slouken@2713
  1206
slouken@2713
  1207
        /* Generics */
slouken@2713
  1208
        dest->dwDuration = hap_condition->length * 1000;        /* In microseconds. */
slouken@2713
  1209
        dest->dwTriggerButton = DIGetTriggerButton(hap_condition->button);
slouken@2713
  1210
        dest->dwTriggerRepeatInterval = hap_condition->interval;
slouken@2713
  1211
        dest->dwStartDelay = hap_condition->delay * 1000;       /* In microseconds. */
slouken@2713
  1212
slouken@2713
  1213
        /* Direction. */
slouken@2713
  1214
        if (SDL_SYS_SetDirection(dest, &hap_condition->direction, dest->cAxes)
slouken@2713
  1215
            < 0) {
slouken@2713
  1216
            return -1;
slouken@2713
  1217
        }
slouken@2713
  1218
slouken@2713
  1219
        /* Envelope - Not actually supported by most CONDITION implementations. */
slouken@2713
  1220
        SDL_free(dest->lpEnvelope);
slouken@2713
  1221
        dest->lpEnvelope = NULL;
slouken@2713
  1222
slouken@2713
  1223
        break;
slouken@2713
  1224
slouken@2713
  1225
    case SDL_HAPTIC_RAMP:
slouken@2713
  1226
        hap_ramp = &src->ramp;
slouken@2713
  1227
        ramp = SDL_malloc(sizeof(DIRAMPFORCE));
slouken@2713
  1228
        if (ramp == NULL) {
icculus@7037
  1229
            return SDL_OutOfMemory();
slouken@2713
  1230
        }
slouken@2713
  1231
        SDL_memset(ramp, 0, sizeof(DIRAMPFORCE));
slouken@2713
  1232
slouken@2713
  1233
        /* Specifics */
slouken@2713
  1234
        ramp->lStart = CONVERT(hap_ramp->start);
slouken@2713
  1235
        ramp->lEnd = CONVERT(hap_ramp->end);
slouken@2713
  1236
        dest->cbTypeSpecificParams = sizeof(DIRAMPFORCE);
slouken@2713
  1237
        dest->lpvTypeSpecificParams = ramp;
slouken@2713
  1238
slouken@2713
  1239
        /* Generics */
slouken@2713
  1240
        dest->dwDuration = hap_ramp->length * 1000;     /* In microseconds. */
slouken@2713
  1241
        dest->dwTriggerButton = DIGetTriggerButton(hap_ramp->button);
slouken@2713
  1242
        dest->dwTriggerRepeatInterval = hap_ramp->interval;
slouken@2713
  1243
        dest->dwStartDelay = hap_ramp->delay * 1000;    /* In microseconds. */
slouken@2713
  1244
slouken@2713
  1245
        /* Direction. */
slouken@2713
  1246
        if (SDL_SYS_SetDirection(dest, &hap_ramp->direction, dest->cAxes) < 0) {
slouken@2713
  1247
            return -1;
slouken@2713
  1248
        }
slouken@2713
  1249
slouken@2713
  1250
        /* Envelope */
slouken@2713
  1251
        if ((hap_ramp->attack_length == 0) && (hap_ramp->fade_length == 0)) {
slouken@2713
  1252
            SDL_free(dest->lpEnvelope);
slouken@2713
  1253
            dest->lpEnvelope = NULL;
slouken@2713
  1254
        } else {
slouken@2713
  1255
            envelope->dwAttackLevel = CONVERT(hap_ramp->attack_level);
slouken@2713
  1256
            envelope->dwAttackTime = hap_ramp->attack_length * 1000;
slouken@2713
  1257
            envelope->dwFadeLevel = CONVERT(hap_ramp->fade_level);
slouken@2713
  1258
            envelope->dwFadeTime = hap_ramp->fade_length * 1000;
slouken@2713
  1259
        }
slouken@2713
  1260
slouken@2713
  1261
        break;
slouken@2713
  1262
slouken@2713
  1263
    case SDL_HAPTIC_CUSTOM:
slouken@2713
  1264
        hap_custom = &src->custom;
slouken@2713
  1265
        custom = SDL_malloc(sizeof(DICUSTOMFORCE));
slouken@2713
  1266
        if (custom == NULL) {
icculus@7037
  1267
            return SDL_OutOfMemory();
slouken@2713
  1268
        }
slouken@2713
  1269
        SDL_memset(custom, 0, sizeof(DICUSTOMFORCE));
slouken@2713
  1270
slouken@2713
  1271
        /* Specifics */
slouken@2713
  1272
        custom->cChannels = hap_custom->channels;
slouken@2713
  1273
        custom->dwSamplePeriod = hap_custom->period * 1000;
slouken@2713
  1274
        custom->cSamples = hap_custom->samples;
slouken@2713
  1275
        custom->rglForceData =
slouken@2713
  1276
            SDL_malloc(sizeof(LONG) * custom->cSamples * custom->cChannels);
slouken@2713
  1277
        for (i = 0; i < hap_custom->samples * hap_custom->channels; i++) {      /* Copy data. */
slouken@2713
  1278
            custom->rglForceData[i] = CONVERT(hap_custom->data[i]);
slouken@2713
  1279
        }
slouken@2713
  1280
        dest->cbTypeSpecificParams = sizeof(DICUSTOMFORCE);
slouken@2713
  1281
        dest->lpvTypeSpecificParams = custom;
slouken@2713
  1282
slouken@2713
  1283
        /* Generics */
slouken@2713
  1284
        dest->dwDuration = hap_custom->length * 1000;   /* In microseconds. */
slouken@2713
  1285
        dest->dwTriggerButton = DIGetTriggerButton(hap_custom->button);
slouken@2713
  1286
        dest->dwTriggerRepeatInterval = hap_custom->interval;
slouken@2713
  1287
        dest->dwStartDelay = hap_custom->delay * 1000;  /* In microseconds. */
slouken@2713
  1288
slouken@2713
  1289
        /* Direction. */
slouken@2713
  1290
        if (SDL_SYS_SetDirection(dest, &hap_custom->direction, dest->cAxes) <
slouken@2713
  1291
            0) {
slouken@2713
  1292
            return -1;
slouken@2713
  1293
        }
slouken@2713
  1294
slouken@2713
  1295
        /* Envelope */
slouken@2713
  1296
        if ((hap_custom->attack_length == 0)
slouken@2713
  1297
            && (hap_custom->fade_length == 0)) {
slouken@2713
  1298
            SDL_free(dest->lpEnvelope);
slouken@2713
  1299
            dest->lpEnvelope = NULL;
slouken@2713
  1300
        } else {
slouken@2713
  1301
            envelope->dwAttackLevel = CONVERT(hap_custom->attack_level);
slouken@2713
  1302
            envelope->dwAttackTime = hap_custom->attack_length * 1000;
slouken@2713
  1303
            envelope->dwFadeLevel = CONVERT(hap_custom->fade_level);
slouken@2713
  1304
            envelope->dwFadeTime = hap_custom->fade_length * 1000;
slouken@2713
  1305
        }
slouken@2713
  1306
slouken@2713
  1307
        break;
slouken@2713
  1308
slouken@2713
  1309
slouken@2713
  1310
    default:
icculus@7037
  1311
        return SDL_SetError("Haptic: Unknown effect type.");
slouken@2713
  1312
    }
slouken@2713
  1313
slouken@2713
  1314
    return 0;
slouken@2713
  1315
}
slouken@2713
  1316
slouken@2713
  1317
slouken@2713
  1318
/*
slouken@2713
  1319
 * Frees an DIEFFECT allocated by SDL_SYS_ToDIEFFECT.
slouken@2713
  1320
 */
slouken@2713
  1321
static void
slouken@2713
  1322
SDL_SYS_HapticFreeDIEFFECT(DIEFFECT * effect, int type)
slouken@2713
  1323
{
slouken@2713
  1324
    DICUSTOMFORCE *custom;
slouken@2713
  1325
slouken@7719
  1326
    SDL_free(effect->lpEnvelope);
slouken@7719
  1327
    effect->lpEnvelope = NULL;
slouken@7719
  1328
    SDL_free(effect->rgdwAxes);
slouken@7719
  1329
    effect->rgdwAxes = NULL;
slouken@2713
  1330
    if (effect->lpvTypeSpecificParams != NULL) {
slouken@2713
  1331
        if (type == SDL_HAPTIC_CUSTOM) {        /* Must free the custom data. */
slouken@2713
  1332
            custom = (DICUSTOMFORCE *) effect->lpvTypeSpecificParams;
slouken@2713
  1333
            SDL_free(custom->rglForceData);
slouken@2713
  1334
            custom->rglForceData = NULL;
slouken@2713
  1335
        }
slouken@2713
  1336
        SDL_free(effect->lpvTypeSpecificParams);
slouken@2713
  1337
        effect->lpvTypeSpecificParams = NULL;
slouken@2713
  1338
    }
slouken@7719
  1339
    SDL_free(effect->rglDirection);
slouken@7719
  1340
    effect->rglDirection = NULL;
slouken@2713
  1341
}
slouken@2713
  1342
slouken@2713
  1343
slouken@2713
  1344
/*
slouken@2713
  1345
 * Gets the effect type from the generic SDL haptic effect wrapper.
slouken@2713
  1346
 */
slouken@2713
  1347
static REFGUID
slouken@2713
  1348
SDL_SYS_HapticEffectType(SDL_HapticEffect * effect)
slouken@2713
  1349
{
slouken@2713
  1350
    switch (effect->type) {
slouken@2713
  1351
    case SDL_HAPTIC_CONSTANT:
slouken@2713
  1352
        return &GUID_ConstantForce;
slouken@2713
  1353
slouken@2713
  1354
    case SDL_HAPTIC_RAMP:
slouken@2713
  1355
        return &GUID_RampForce;
slouken@2713
  1356
icculus@7621
  1357
    /* !!! FIXME: put this back when we have more bits in 2.1 */
gabomdq@7678
  1358
    /* case SDL_HAPTIC_SQUARE:
gabomdq@7677
  1359
        return &GUID_Square; */
slouken@2713
  1360
slouken@2713
  1361
    case SDL_HAPTIC_SINE:
slouken@2713
  1362
        return &GUID_Sine;
slouken@2713
  1363
slouken@2713
  1364
    case SDL_HAPTIC_TRIANGLE:
slouken@2713
  1365
        return &GUID_Triangle;
slouken@2713
  1366
slouken@2713
  1367
    case SDL_HAPTIC_SAWTOOTHUP:
slouken@2713
  1368
        return &GUID_SawtoothUp;
slouken@2713
  1369
slouken@2713
  1370
    case SDL_HAPTIC_SAWTOOTHDOWN:
slouken@2713
  1371
        return &GUID_SawtoothDown;
slouken@2713
  1372
slouken@2713
  1373
    case SDL_HAPTIC_SPRING:
slouken@2713
  1374
        return &GUID_Spring;
slouken@2713
  1375
slouken@2713
  1376
    case SDL_HAPTIC_DAMPER:
slouken@2713
  1377
        return &GUID_Damper;
slouken@2713
  1378
slouken@2713
  1379
    case SDL_HAPTIC_INERTIA:
slouken@2713
  1380
        return &GUID_Inertia;
slouken@2713
  1381
slouken@2713
  1382
    case SDL_HAPTIC_FRICTION:
slouken@2713
  1383
        return &GUID_Friction;
slouken@2713
  1384
slouken@2713
  1385
    case SDL_HAPTIC_CUSTOM:
slouken@2713
  1386
        return &GUID_CustomForce;
slouken@2713
  1387
slouken@2713
  1388
    default:
slouken@2713
  1389
        return NULL;
slouken@2713
  1390
    }
slouken@2713
  1391
}
slouken@2713
  1392
slouken@2713
  1393
slouken@2713
  1394
/*
slouken@2713
  1395
 * Creates a new haptic effect.
slouken@2713
  1396
 */
slouken@2713
  1397
int
slouken@2713
  1398
SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
slouken@2713
  1399
                        SDL_HapticEffect * base)
slouken@2713
  1400
{
slouken@2713
  1401
    HRESULT ret;
icculus@7850
  1402
    REFGUID type = SDL_SYS_HapticEffectType(base);
slouken@2713
  1403
icculus@7850
  1404
    if ((type == NULL) && (!haptic->hwdata->bXInputHaptic)) {
icculus@7884
  1405
        SDL_SetError("Haptic: Unknown effect type.");
icculus@7850
  1406
        goto err_hweffect;
slouken@2713
  1407
    }
slouken@2713
  1408
slouken@2713
  1409
    /* Alloc the effect. */
slouken@2713
  1410
    effect->hweffect = (struct haptic_hweffect *)
slouken@2713
  1411
        SDL_malloc(sizeof(struct haptic_hweffect));
slouken@2713
  1412
    if (effect->hweffect == NULL) {
slouken@2713
  1413
        SDL_OutOfMemory();
slouken@2713
  1414
        goto err_hweffect;
slouken@2713
  1415
    }
slouken@2713
  1416
icculus@6990
  1417
    SDL_zerop(effect->hweffect);
icculus@6990
  1418
icculus@6990
  1419
    if (haptic->hwdata->bXInputHaptic) {
icculus@7621
  1420
        SDL_assert(base->type == SDL_HAPTIC_LEFTRIGHT);  /* should catch this at higher level */
icculus@6990
  1421
        return SDL_SYS_HapticUpdateEffect(haptic, effect, base);
icculus@6990
  1422
    }
icculus@6990
  1423
slouken@2713
  1424
    /* Get the effect. */
slouken@2713
  1425
    if (SDL_SYS_ToDIEFFECT(haptic, &effect->hweffect->effect, base) < 0) {
slouken@2713
  1426
        goto err_effectdone;
slouken@2713
  1427
    }
slouken@2713
  1428
slouken@2713
  1429
    /* Create the actual effect. */
icculus@6716
  1430
    ret = IDirectInputDevice8_CreateEffect(haptic->hwdata->device, type,
slouken@2713
  1431
                                           &effect->hweffect->effect,
slouken@2713
  1432
                                           &effect->hweffect->ref, NULL);
slouken@2713
  1433
    if (FAILED(ret)) {
slouken@2713
  1434
        DI_SetError("Unable to create effect", ret);
slouken@2713
  1435
        goto err_effectdone;
slouken@2713
  1436
    }
slouken@2713
  1437
slouken@2713
  1438
    return 0;
slouken@2713
  1439
slouken@2713
  1440
  err_effectdone:
slouken@2713
  1441
    SDL_SYS_HapticFreeDIEFFECT(&effect->hweffect->effect, base->type);
slouken@2713
  1442
  err_hweffect:
slouken@7719
  1443
    SDL_free(effect->hweffect);
slouken@7719
  1444
    effect->hweffect = NULL;
slouken@2713
  1445
    return -1;
slouken@2713
  1446
}
slouken@2713
  1447
slouken@2713
  1448
slouken@2713
  1449
/*
slouken@2713
  1450
 * Updates an effect.
slouken@2713
  1451
 */
slouken@2713
  1452
int
slouken@2713
  1453
SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
slouken@2713
  1454
                           struct haptic_effect *effect,
slouken@2713
  1455
                           SDL_HapticEffect * data)
slouken@2713
  1456
{
slouken@2713
  1457
    HRESULT ret;
slouken@2713
  1458
    DWORD flags;
slouken@2713
  1459
    DIEFFECT temp;
slouken@2713
  1460
icculus@6990
  1461
    if (haptic->hwdata->bXInputHaptic) {
icculus@6990
  1462
        XINPUT_VIBRATION *vib = &effect->hweffect->vibration;
icculus@7621
  1463
        SDL_assert(data->type == SDL_HAPTIC_LEFTRIGHT);
icculus@7621
  1464
        vib->wLeftMotorSpeed = data->leftright.large_magnitude;
icculus@7621
  1465
        vib->wRightMotorSpeed = data->leftright.small_magnitude;
icculus@7623
  1466
        SDL_LockMutex(haptic->hwdata->mutex);
icculus@7623
  1467
        if (haptic->hwdata->stopTicks) {  /* running right now? Update it. */
icculus@7623
  1468
            XINPUTSETSTATE(haptic->hwdata->userid, vib);
icculus@7623
  1469
        }
icculus@7623
  1470
        SDL_UnlockMutex(haptic->hwdata->mutex);
icculus@6990
  1471
        return 0;
icculus@6990
  1472
    }
icculus@6990
  1473
slouken@2713
  1474
    /* Get the effect. */
slouken@2713
  1475
    SDL_memset(&temp, 0, sizeof(DIEFFECT));
slouken@2713
  1476
    if (SDL_SYS_ToDIEFFECT(haptic, &temp, data) < 0) {
slouken@2713
  1477
        goto err_update;
slouken@2713
  1478
    }
slouken@2713
  1479
slouken@2713
  1480
    /* Set the flags.  Might be worthwhile to diff temp with loaded effect and
slouken@2713
  1481
     *  only change those parameters. */
slouken@2713
  1482
    flags = DIEP_DIRECTION |
slouken@2713
  1483
        DIEP_DURATION |
slouken@2713
  1484
        DIEP_ENVELOPE |
slouken@2713
  1485
        DIEP_STARTDELAY |
slouken@2713
  1486
        DIEP_TRIGGERBUTTON |
slouken@2713
  1487
        DIEP_TRIGGERREPEATINTERVAL | DIEP_TYPESPECIFICPARAMS;
slouken@2713
  1488
slouken@2713
  1489
    /* Create the actual effect. */
slouken@2713
  1490
    ret =
slouken@2713
  1491
        IDirectInputEffect_SetParameters(effect->hweffect->ref, &temp, flags);
slouken@2713
  1492
    if (FAILED(ret)) {
slouken@2713
  1493
        DI_SetError("Unable to update effect", ret);
slouken@2713
  1494
        goto err_update;
slouken@2713
  1495
    }
slouken@2713
  1496
slouken@2713
  1497
    /* Copy it over. */
slouken@2713
  1498
    SDL_SYS_HapticFreeDIEFFECT(&effect->hweffect->effect, data->type);
slouken@2713
  1499
    SDL_memcpy(&effect->hweffect->effect, &temp, sizeof(DIEFFECT));
slouken@2713
  1500
slouken@2713
  1501
    return 0;
slouken@2713
  1502
slouken@2713
  1503
  err_update:
slouken@2713
  1504
    SDL_SYS_HapticFreeDIEFFECT(&temp, data->type);
slouken@2713
  1505
    return -1;
slouken@2713
  1506
}
slouken@2713
  1507
slouken@2713
  1508
slouken@2713
  1509
/*
slouken@2713
  1510
 * Runs an effect.
slouken@2713
  1511
 */
slouken@2713
  1512
int
slouken@2713
  1513
SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
slouken@2713
  1514
                        Uint32 iterations)
slouken@2713
  1515
{
slouken@2713
  1516
    HRESULT ret;
slouken@2713
  1517
    DWORD iter;
slouken@2713
  1518
icculus@6990
  1519
    if (haptic->hwdata->bXInputHaptic) {
icculus@6990
  1520
        XINPUT_VIBRATION *vib = &effect->hweffect->vibration;
icculus@7621
  1521
        SDL_assert(effect->effect.type == SDL_HAPTIC_LEFTRIGHT);  /* should catch this at higher level */
icculus@7481
  1522
        SDL_LockMutex(haptic->hwdata->mutex);
icculus@7847
  1523
        if(effect->effect.leftright.length == SDL_HAPTIC_INFINITY || iterations == SDL_HAPTIC_INFINITY) {
icculus@7847
  1524
            haptic->hwdata->stopTicks = SDL_HAPTIC_INFINITY;
icculus@7855
  1525
        } else if ((!effect->effect.leftright.length) || (!iterations)) {
icculus@7855
  1526
            /* do nothing. Effect runs for zero milliseconds. */
icculus@7847
  1527
        } else {
icculus@7847
  1528
            haptic->hwdata->stopTicks = SDL_GetTicks() + (effect->effect.leftright.length * iterations);
icculus@7855
  1529
            if ((haptic->hwdata->stopTicks == SDL_HAPTIC_INFINITY) || (haptic->hwdata->stopTicks == 0)) {
icculus@7855
  1530
                haptic->hwdata->stopTicks = 1;  /* fix edge cases. */
icculus@7855
  1531
            }
icculus@7847
  1532
        }
icculus@7481
  1533
        SDL_UnlockMutex(haptic->hwdata->mutex);
icculus@7481
  1534
        return (XINPUTSETSTATE(haptic->hwdata->userid, vib) == ERROR_SUCCESS) ? 0 : -1;
icculus@6990
  1535
    }
icculus@6990
  1536
slouken@2713
  1537
    /* Check if it's infinite. */
slouken@2713
  1538
    if (iterations == SDL_HAPTIC_INFINITY) {
slouken@2713
  1539
        iter = INFINITE;
slouken@2713
  1540
    } else
slouken@2713
  1541
        iter = iterations;
slouken@2713
  1542
slouken@2713
  1543
    /* Run the effect. */
slouken@2713
  1544
    ret = IDirectInputEffect_Start(effect->hweffect->ref, iter, 0);
slouken@2713
  1545
    if (FAILED(ret)) {
icculus@7037
  1546
        return DI_SetError("Running the effect", ret);
slouken@2713
  1547
    }
slouken@2713
  1548
slouken@2713
  1549
    return 0;
slouken@2713
  1550
}
slouken@2713
  1551
slouken@2713
  1552
slouken@2713
  1553
/*
slouken@2713
  1554
 * Stops an effect.
slouken@2713
  1555
 */
slouken@2713
  1556
int
slouken@2713
  1557
SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
slouken@2713
  1558
{
slouken@2713
  1559
    HRESULT ret;
slouken@2713
  1560
icculus@6990
  1561
    if (haptic->hwdata->bXInputHaptic) {
icculus@6990
  1562
        XINPUT_VIBRATION vibration = { 0, 0 };
icculus@7481
  1563
        SDL_LockMutex(haptic->hwdata->mutex);
icculus@7481
  1564
        haptic->hwdata->stopTicks = 0;
icculus@7481
  1565
        SDL_UnlockMutex(haptic->hwdata->mutex);
icculus@7481
  1566
        return (XINPUTSETSTATE(haptic->hwdata->userid, &vibration) == ERROR_SUCCESS) ? 0 : -1;
icculus@6990
  1567
    }
icculus@6990
  1568
slouken@2713
  1569
    ret = IDirectInputEffect_Stop(effect->hweffect->ref);
slouken@2713
  1570
    if (FAILED(ret)) {
icculus@7037
  1571
        return DI_SetError("Unable to stop effect", ret);
slouken@2713
  1572
    }
slouken@2713
  1573
slouken@2713
  1574
    return 0;
slouken@2713
  1575
}
slouken@2713
  1576
slouken@2713
  1577
slouken@2713
  1578
/*
slouken@2713
  1579
 * Frees the effect.
slouken@2713
  1580
 */
slouken@2713
  1581
void
slouken@2713
  1582
SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
slouken@2713
  1583
{
slouken@2713
  1584
    HRESULT ret;
slouken@2713
  1585
icculus@6990
  1586
    if (haptic->hwdata->bXInputHaptic) {
icculus@6990
  1587
        SDL_SYS_HapticStopEffect(haptic, effect);
icculus@6990
  1588
    } else {
icculus@6990
  1589
        ret = IDirectInputEffect_Unload(effect->hweffect->ref);
icculus@6990
  1590
        if (FAILED(ret)) {
icculus@6990
  1591
            DI_SetError("Removing effect from the device", ret);
icculus@6990
  1592
        }
icculus@6990
  1593
        SDL_SYS_HapticFreeDIEFFECT(&effect->hweffect->effect,
icculus@6990
  1594
                                   effect->effect.type);
slouken@2713
  1595
    }
slouken@2713
  1596
    SDL_free(effect->hweffect);
slouken@2713
  1597
    effect->hweffect = NULL;
slouken@2713
  1598
}
slouken@2713
  1599
slouken@2713
  1600
slouken@2713
  1601
/*
slouken@2713
  1602
 * Gets the status of a haptic effect.
slouken@2713
  1603
 */
slouken@2713
  1604
int
slouken@2713
  1605
SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic,
slouken@2713
  1606
                              struct haptic_effect *effect)
slouken@2713
  1607
{
slouken@2713
  1608
    HRESULT ret;
slouken@2713
  1609
    DWORD status;
slouken@2713
  1610
slouken@2713
  1611
    ret = IDirectInputEffect_GetEffectStatus(effect->hweffect->ref, &status);
slouken@2713
  1612
    if (FAILED(ret)) {
icculus@7037
  1613
        return DI_SetError("Getting effect status", ret);
slouken@2713
  1614
    }
slouken@2713
  1615
slouken@2713
  1616
    if (status == 0)
slouken@2713
  1617
        return SDL_FALSE;
slouken@2713
  1618
    return SDL_TRUE;
slouken@2713
  1619
}
slouken@2713
  1620
slouken@2713
  1621
slouken@2713
  1622
/*
slouken@2713
  1623
 * Sets the gain.
slouken@2713
  1624
 */
slouken@2713
  1625
int
slouken@2713
  1626
SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
slouken@2713
  1627
{
slouken@2713
  1628
    HRESULT ret;
slouken@2713
  1629
    DIPROPDWORD dipdw;
slouken@2713
  1630
slouken@2713
  1631
    /* Create the weird structure thingy. */
slouken@2713
  1632
    dipdw.diph.dwSize = sizeof(DIPROPDWORD);
slouken@2713
  1633
    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
slouken@2713
  1634
    dipdw.diph.dwObj = 0;
slouken@2713
  1635
    dipdw.diph.dwHow = DIPH_DEVICE;
slouken@2713
  1636
    dipdw.dwData = gain * 100;  /* 0 to 10,000 */
slouken@2713
  1637
slouken@2713
  1638
    /* Try to set the autocenter. */
icculus@6716
  1639
    ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device,
slouken@2713
  1640
                                          DIPROP_FFGAIN, &dipdw.diph);
slouken@2713
  1641
    if (FAILED(ret)) {
icculus@7037
  1642
        return DI_SetError("Setting gain", ret);
slouken@2713
  1643
    }
slouken@2713
  1644
slouken@2713
  1645
    return 0;
slouken@2713
  1646
}
slouken@2713
  1647
slouken@2713
  1648
slouken@2713
  1649
/*
slouken@2713
  1650
 * Sets the autocentering.
slouken@2713
  1651
 */
slouken@2713
  1652
int
slouken@2713
  1653
SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
slouken@2713
  1654
{
slouken@2713
  1655
    HRESULT ret;
slouken@2713
  1656
    DIPROPDWORD dipdw;
slouken@2713
  1657
slouken@2713
  1658
    /* Create the weird structure thingy. */
slouken@2713
  1659
    dipdw.diph.dwSize = sizeof(DIPROPDWORD);
slouken@2713
  1660
    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
slouken@2713
  1661
    dipdw.diph.dwObj = 0;
slouken@2713
  1662
    dipdw.diph.dwHow = DIPH_DEVICE;
slouken@2713
  1663
    dipdw.dwData = (autocenter == 0) ? DIPROPAUTOCENTER_OFF :
slouken@2713
  1664
        DIPROPAUTOCENTER_ON;
slouken@2713
  1665
slouken@2713
  1666
    /* Try to set the autocenter. */
icculus@6716
  1667
    ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device,
slouken@2713
  1668
                                          DIPROP_AUTOCENTER, &dipdw.diph);
slouken@2713
  1669
    if (FAILED(ret)) {
icculus@7037
  1670
        return DI_SetError("Setting autocenter", ret);
slouken@2713
  1671
    }
slouken@2713
  1672
slouken@2713
  1673
    return 0;
slouken@2713
  1674
}
slouken@2713
  1675
slouken@2713
  1676
slouken@2713
  1677
/*
slouken@2713
  1678
 * Pauses the device.
slouken@2713
  1679
 */
slouken@2713
  1680
int
slouken@2713
  1681
SDL_SYS_HapticPause(SDL_Haptic * haptic)
slouken@2713
  1682
{
slouken@2713
  1683
    HRESULT ret;
slouken@2713
  1684
slouken@2713
  1685
    /* Pause the device. */
icculus@6716
  1686
    ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
slouken@2713
  1687
                                                       DISFFC_PAUSE);
slouken@2713
  1688
    if (FAILED(ret)) {
icculus@7037
  1689
        return DI_SetError("Pausing the device", ret);
slouken@2713
  1690
    }
slouken@2713
  1691
slouken@2713
  1692
    return 0;
slouken@2713
  1693
}
slouken@2713
  1694
slouken@2713
  1695
slouken@2713
  1696
/*
slouken@2713
  1697
 * Pauses the device.
slouken@2713
  1698
 */
slouken@2713
  1699
int
slouken@2713
  1700
SDL_SYS_HapticUnpause(SDL_Haptic * haptic)
slouken@2713
  1701
{
slouken@2713
  1702
    HRESULT ret;
slouken@2713
  1703
slouken@2713
  1704
    /* Unpause the device. */
icculus@6716
  1705
    ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
slouken@2713
  1706
                                                       DISFFC_CONTINUE);
slouken@2713
  1707
    if (FAILED(ret)) {
icculus@7037
  1708
        return DI_SetError("Pausing the device", ret);
slouken@2713
  1709
    }
slouken@2713
  1710
slouken@2713
  1711
    return 0;
slouken@2713
  1712
}
slouken@2713
  1713
slouken@2713
  1714
slouken@2713
  1715
/*
slouken@2713
  1716
 * Stops all the playing effects on the device.
slouken@2713
  1717
 */
slouken@2713
  1718
int
bobbens@2715
  1719
SDL_SYS_HapticStopAll(SDL_Haptic * haptic)
slouken@2713
  1720
{
slouken@2713
  1721
    HRESULT ret;
slouken@2713
  1722
icculus@6990
  1723
    if (haptic->hwdata->bXInputHaptic) {
icculus@6990
  1724
        XINPUT_VIBRATION vibration = { 0, 0 };
icculus@7481
  1725
        SDL_LockMutex(haptic->hwdata->mutex);
icculus@7481
  1726
        haptic->hwdata->stopTicks = 0;
icculus@7481
  1727
        SDL_UnlockMutex(haptic->hwdata->mutex);
icculus@7481
  1728
        return (XINPUTSETSTATE(haptic->hwdata->userid, &vibration) == ERROR_SUCCESS) ? 0 : -1;
icculus@6990
  1729
    }
icculus@6990
  1730
slouken@2713
  1731
    /* Try to stop the effects. */
icculus@6716
  1732
    ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
slouken@2713
  1733
                                                       DISFFC_STOPALL);
slouken@2713
  1734
    if (FAILED(ret)) {
icculus@7037
  1735
        return DI_SetError("Stopping the device", ret);
slouken@2713
  1736
    }
slouken@2713
  1737
slouken@2713
  1738
    return 0;
slouken@2713
  1739
}
slouken@2713
  1740
icculus@7481
  1741
icculus@7481
  1742
/* !!! FIXME: this is a hack, remove this later. */
icculus@7481
  1743
/* Since XInput doesn't offer a way to vibrate for X time, we hook into
icculus@7481
  1744
 *  SDL_PumpEvents() to check if it's time to stop vibrating with some
icculus@7481
  1745
 *  frequency.
icculus@7481
  1746
 * In practice, this works for 99% of use cases. But in an ideal world,
icculus@7481
  1747
 *  we do this in a separate thread so that:
icculus@7481
  1748
 *    - we aren't bound to when the app chooses to pump the event queue.
icculus@7481
  1749
 *    - we aren't adding more polling to the event queue
icculus@7481
  1750
 *    - we can emulate all the haptic effects correctly (start on a delay,
icculus@7481
  1751
 *      mix multiple effects, etc).
icculus@7481
  1752
 *
icculus@7481
  1753
 * Mostly, this is here to get rumbling to work, and all the other features
icculus@7481
  1754
 *  are absent in the XInput path for now.  :(
icculus@7481
  1755
 */
icculus@7481
  1756
static int SDLCALL
icculus@7481
  1757
SDL_RunXInputHaptic(void *arg)
icculus@7481
  1758
{
icculus@7481
  1759
    struct haptic_hwdata *hwdata = (struct haptic_hwdata *) arg;
icculus@7481
  1760
icculus@7481
  1761
    while (!hwdata->stopThread) {
icculus@7481
  1762
        SDL_Delay(50);
icculus@7481
  1763
        SDL_LockMutex(hwdata->mutex);
icculus@7481
  1764
        /* If we're currently running and need to stop... */
icculus@7849
  1765
        if (hwdata->stopTicks) {
slouken@7858
  1766
            if ((hwdata->stopTicks != SDL_HAPTIC_INFINITY) && SDL_TICKS_PASSED(SDL_GetTicks(), hwdata->stopTicks)) {
icculus@7847
  1767
                XINPUT_VIBRATION vibration = { 0, 0 };
icculus@7847
  1768
                hwdata->stopTicks = 0;
icculus@7847
  1769
                XINPUTSETSTATE(hwdata->userid, &vibration);
icculus@7847
  1770
            }
icculus@7481
  1771
        }
icculus@7481
  1772
        SDL_UnlockMutex(hwdata->mutex);
icculus@7481
  1773
    }
icculus@7481
  1774
icculus@7481
  1775
    return 0;
icculus@7481
  1776
}
icculus@7481
  1777
slouken@7191
  1778
#endif /* SDL_HAPTIC_DINPUT */
slouken@2713
  1779
slouken@7191
  1780
/* vi: set ts=4 sw=4 expandtab: */