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