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