src/haptic/windows/SDL_syshaptic.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 29 Nov 2012 15:24:56 -0500
changeset 6716 1616f6b3738c
parent 6690 9548c8a58103
child 6885 700f1b25f77f
permissions -rw-r--r--
Move all DirectInput code from DI2 to DI8.

Fixes failing Haptic subsystem initialization on Windows.
slouken@2713
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 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
*/
slouken@2713
    21
#include "SDL_config.h"
slouken@2713
    22
slouken@2713
    23
#ifdef SDL_HAPTIC_DINPUT
slouken@2713
    24
slouken@2713
    25
#include "SDL_haptic.h"
slouken@2713
    26
#include "../SDL_syshaptic.h"
slouken@2713
    27
#include "SDL_joystick.h"
slouken@2713
    28
#include "../../joystick/SDL_sysjoystick.h"     /* For the real SDL_Joystick */
slouken@5062
    29
#include "../../joystick/windows/SDL_dxjoystick_c.h"      /* For joystick hwdata */
slouken@2713
    30
slouken@2713
    31
slouken@2713
    32
#define MAX_HAPTICS  32
slouken@2713
    33
slouken@2713
    34
slouken@2713
    35
/*
slouken@2713
    36
 * List of available haptic devices.
slouken@2713
    37
 */
slouken@2713
    38
static struct
slouken@2713
    39
{
slouken@2713
    40
    DIDEVICEINSTANCE instance;
slouken@5090
    41
    char *name;
slouken@2713
    42
    SDL_Haptic *haptic;
slouken@2713
    43
    DIDEVCAPS capabilities;
slouken@2713
    44
} SDL_hapticlist[MAX_HAPTICS];
slouken@2713
    45
slouken@2713
    46
slouken@2713
    47
/*
slouken@2713
    48
 * Haptic system hardware data.
slouken@2713
    49
 */
slouken@2713
    50
struct haptic_hwdata
slouken@2713
    51
{
icculus@6716
    52
    LPDIRECTINPUTDEVICE8 device;
slouken@2713
    53
    DWORD axes[3];              /* Axes to use. */
slouken@2713
    54
    int is_joystick;            /* Device is loaded as joystick. */
slouken@2713
    55
};
slouken@2713
    56
slouken@2713
    57
slouken@2713
    58
/*
slouken@2713
    59
 * Haptic system effect data.
slouken@2713
    60
 */
slouken@2713
    61
struct haptic_hweffect
slouken@2713
    62
{
slouken@2713
    63
    DIEFFECT effect;
slouken@2713
    64
    LPDIRECTINPUTEFFECT ref;
slouken@2713
    65
};
slouken@2713
    66
slouken@2713
    67
slouken@2713
    68
/*
slouken@2713
    69
 * Internal stuff.
slouken@2713
    70
 */
icculus@5591
    71
static SDL_bool coinitialized = SDL_FALSE;
icculus@6716
    72
static LPDIRECTINPUT8 dinput = NULL;
slouken@2713
    73
slouken@2713
    74
slouken@2713
    75
/*
slouken@2713
    76
 * External stuff.
slouken@2713
    77
 */
slouken@2713
    78
extern HWND SDL_HelperWindow;
slouken@2713
    79
slouken@2713
    80
slouken@2713
    81
/*
slouken@2713
    82
 * Prototypes.
slouken@2713
    83
 */
slouken@2713
    84
static void DI_SetError(const char *str, HRESULT err);
slouken@2713
    85
static int DI_GUIDIsSame(const GUID * a, const GUID * b);
slouken@2713
    86
static int SDL_SYS_HapticOpenFromInstance(SDL_Haptic * haptic,
slouken@2713
    87
                                          DIDEVICEINSTANCE instance);
icculus@6716
    88
static int SDL_SYS_HapticOpenFromDevice8(SDL_Haptic * haptic,
icculus@6716
    89
                                         LPDIRECTINPUTDEVICE8 device8);
slouken@2713
    90
static DWORD DIGetTriggerButton(Uint16 button);
slouken@2713
    91
static int SDL_SYS_SetDirection(DIEFFECT * effect, SDL_HapticDirection * dir,
slouken@2713
    92
                                int naxes);
slouken@2713
    93
static int SDL_SYS_ToDIEFFECT(SDL_Haptic * haptic, DIEFFECT * dest,
slouken@2713
    94
                              SDL_HapticEffect * src);
slouken@2713
    95
static void SDL_SYS_HapticFreeDIEFFECT(DIEFFECT * effect, int type);
slouken@2713
    96
static REFGUID SDL_SYS_HapticEffectType(SDL_HapticEffect * effect);
slouken@2713
    97
/* Callbacks. */
slouken@2713
    98
static BOOL CALLBACK EnumHapticsCallback(const DIDEVICEINSTANCE *
slouken@2713
    99
                                         pdidInstance, VOID * pContext);
slouken@2713
   100
static BOOL CALLBACK DI_EffectCallback(LPCDIEFFECTINFO pei, LPVOID pv);
slouken@2713
   101
slouken@2713
   102
slouken@2713
   103
/* 
slouken@2713
   104
 * Like SDL_SetError but for DX error codes.
slouken@2713
   105
 */
slouken@2713
   106
static void
slouken@2713
   107
DI_SetError(const char *str, HRESULT err)
slouken@2713
   108
{
slouken@2786
   109
    /*
slouken@2786
   110
       SDL_SetError("Haptic: %s - %s: %s", str,
slouken@2786
   111
       DXGetErrorString8A(err), DXGetErrorDescription8A(err));
slouken@2786
   112
     */
slouken@2786
   113
    SDL_SetError("Haptic error %s", str);
slouken@2713
   114
}
slouken@2713
   115
slouken@2713
   116
slouken@2713
   117
/*
slouken@2713
   118
 * Checks to see if two GUID are the same.
slouken@2713
   119
 */
slouken@2713
   120
static int
slouken@2713
   121
DI_GUIDIsSame(const GUID * a, const GUID * b)
slouken@2713
   122
{
icculus@6403
   123
    return (SDL_memcmp(a, b, sizeof (GUID)) == 0);
slouken@2713
   124
}
slouken@2713
   125
slouken@2713
   126
slouken@2713
   127
/*
slouken@2713
   128
 * Initializes the haptic subsystem.
slouken@2713
   129
 */
slouken@2713
   130
int
slouken@2713
   131
SDL_SYS_HapticInit(void)
slouken@2713
   132
{
slouken@2713
   133
    HRESULT ret;
slouken@2713
   134
    HINSTANCE instance;
slouken@2713
   135
slouken@2713
   136
    if (dinput != NULL) {       /* Already open. */
slouken@2713
   137
        SDL_SetError("Haptic: SubSystem already open.");
slouken@2713
   138
        return -1;
slouken@2713
   139
    }
slouken@2713
   140
slouken@2713
   141
    /* Clear all the memory. */
slouken@2713
   142
    SDL_memset(SDL_hapticlist, 0, sizeof(SDL_hapticlist));
slouken@2713
   143
slouken@2713
   144
    SDL_numhaptics = 0;
slouken@2713
   145
icculus@5591
   146
    ret = WIN_CoInitialize();
slouken@2713
   147
    if (FAILED(ret)) {
slouken@2713
   148
        DI_SetError("Coinitialize", ret);
slouken@2713
   149
        return -1;
slouken@2713
   150
    }
slouken@2713
   151
icculus@5591
   152
    coinitialized = SDL_TRUE;
icculus@5591
   153
icculus@6716
   154
    ret = CoCreateInstance(&CLSID_DirectInput8, NULL, CLSCTX_INPROC_SERVER,
icculus@6716
   155
                           &IID_IDirectInput8, (LPVOID) & dinput);
slouken@2713
   156
    if (FAILED(ret)) {
icculus@5591
   157
        SDL_SYS_HapticQuit();
slouken@2713
   158
        DI_SetError("CoCreateInstance", ret);
slouken@2713
   159
        return -1;
slouken@2713
   160
    }
slouken@2713
   161
slouken@2713
   162
    /* Because we used CoCreateInstance, we need to Initialize it, first. */
slouken@2713
   163
    instance = GetModuleHandle(NULL);
slouken@2713
   164
    if (instance == NULL) {
icculus@5591
   165
        SDL_SYS_HapticQuit();
slouken@2713
   166
        SDL_SetError("GetModuleHandle() failed with error code %d.",
slouken@2713
   167
                     GetLastError());
slouken@2713
   168
        return -1;
slouken@2713
   169
    }
icculus@6716
   170
    ret = IDirectInput8_Initialize(dinput, instance, DIRECTINPUT_VERSION);
slouken@2713
   171
    if (FAILED(ret)) {
icculus@5591
   172
        SDL_SYS_HapticQuit();
slouken@2713
   173
        DI_SetError("Initializing DirectInput device", ret);
slouken@2713
   174
        return -1;
slouken@2713
   175
    }
slouken@2713
   176
slouken@2713
   177
    /* Look for haptic devices. */
icculus@6716
   178
    ret = IDirectInput8_EnumDevices(dinput,
slouken@2714
   179
                                   0,
slouken@2713
   180
                                   EnumHapticsCallback,
slouken@2713
   181
                                   NULL,
slouken@2713
   182
                                   DIEDFL_FORCEFEEDBACK |
slouken@2713
   183
                                   DIEDFL_ATTACHEDONLY);
slouken@2713
   184
    if (FAILED(ret)) {
icculus@5591
   185
        SDL_SYS_HapticQuit();
slouken@2713
   186
        DI_SetError("Enumerating DirectInput devices", ret);
slouken@2713
   187
        return -1;
slouken@2713
   188
    }
slouken@2713
   189
slouken@2713
   190
    return SDL_numhaptics;
slouken@2713
   191
}
slouken@2713
   192
slouken@2713
   193
/*
slouken@2713
   194
 * Callback to find the haptic devices.
slouken@2713
   195
 */
slouken@2713
   196
static BOOL CALLBACK
slouken@2713
   197
EnumHapticsCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
slouken@2713
   198
{
slouken@2713
   199
    HRESULT ret;
icculus@6716
   200
    LPDIRECTINPUTDEVICE8 device;
slouken@2713
   201
slouken@2713
   202
    /* Copy the instance over, useful for creating devices. */
slouken@2713
   203
    SDL_memcpy(&SDL_hapticlist[SDL_numhaptics].instance, pdidInstance,
slouken@2713
   204
               sizeof(DIDEVICEINSTANCE));
slouken@2713
   205
slouken@2713
   206
    /* Open the device */
icculus@6716
   207
    ret = IDirectInput8_CreateDevice(dinput, &pdidInstance->guidInstance,
slouken@2713
   208
                                    &device, NULL);
slouken@2713
   209
    if (FAILED(ret)) {
slouken@2713
   210
        /* DI_SetError("Creating DirectInput device",ret); */
slouken@2713
   211
        return DIENUM_CONTINUE;
slouken@2713
   212
    }
slouken@2713
   213
slouken@2713
   214
    /* Get capabilities. */
slouken@2713
   215
    SDL_hapticlist[SDL_numhaptics].capabilities.dwSize = sizeof(DIDEVCAPS);
icculus@6716
   216
    ret = IDirectInputDevice8_GetCapabilities(device,
slouken@3013
   217
                                             &SDL_hapticlist[SDL_numhaptics].
slouken@3013
   218
                                             capabilities);
slouken@2713
   219
    if (FAILED(ret)) {
slouken@2713
   220
        /* DI_SetError("Getting device capabilities",ret); */
icculus@6716
   221
        IDirectInputDevice8_Release(device);
slouken@2713
   222
        return DIENUM_CONTINUE;
slouken@2713
   223
    }
slouken@2713
   224
slouken@5090
   225
    /* Copy the name */
slouken@5090
   226
    SDL_hapticlist[SDL_numhaptics].name = WIN_StringToUTF8(SDL_hapticlist[SDL_numhaptics].instance.tszProductName);
slouken@5090
   227
slouken@2713
   228
    /* Close up device and count it. */
icculus@6716
   229
    IDirectInputDevice8_Release(device);
slouken@2713
   230
    SDL_numhaptics++;
slouken@2713
   231
slouken@2713
   232
    /* Watch out for hard limit. */
slouken@2713
   233
    if (SDL_numhaptics >= MAX_HAPTICS)
slouken@2713
   234
        return DIENUM_STOP;
slouken@2713
   235
slouken@2713
   236
    return DIENUM_CONTINUE;
slouken@2713
   237
}
slouken@2713
   238
slouken@2713
   239
slouken@2713
   240
/*
slouken@2713
   241
 * Return the name of a haptic device, does not need to be opened.
slouken@2713
   242
 */
slouken@2713
   243
const char *
slouken@2713
   244
SDL_SYS_HapticName(int index)
slouken@2713
   245
{
slouken@5090
   246
    return SDL_hapticlist[index].name;
slouken@2713
   247
}
slouken@2713
   248
slouken@2713
   249
slouken@2713
   250
/*
slouken@2713
   251
 * Callback to get all supported effects.
slouken@2713
   252
 */
slouken@2713
   253
#define EFFECT_TEST(e,s)               \
slouken@2713
   254
if (DI_GUIDIsSame(&pei->guid, &(e)))   \
slouken@2713
   255
   haptic->supported |= (s)
slouken@2713
   256
static BOOL CALLBACK
slouken@2713
   257
DI_EffectCallback(LPCDIEFFECTINFO pei, LPVOID pv)
slouken@2713
   258
{
slouken@2713
   259
    /* Prepare the haptic device. */
slouken@2713
   260
    SDL_Haptic *haptic = (SDL_Haptic *) pv;
slouken@2713
   261
slouken@2713
   262
    /* Get supported. */
slouken@2713
   263
    EFFECT_TEST(GUID_Spring, SDL_HAPTIC_SPRING);
slouken@2713
   264
    EFFECT_TEST(GUID_Damper, SDL_HAPTIC_DAMPER);
slouken@2713
   265
    EFFECT_TEST(GUID_Inertia, SDL_HAPTIC_INERTIA);
slouken@2713
   266
    EFFECT_TEST(GUID_Friction, SDL_HAPTIC_FRICTION);
slouken@2713
   267
    EFFECT_TEST(GUID_ConstantForce, SDL_HAPTIC_CONSTANT);
slouken@2713
   268
    EFFECT_TEST(GUID_CustomForce, SDL_HAPTIC_CUSTOM);
slouken@2713
   269
    EFFECT_TEST(GUID_Sine, SDL_HAPTIC_SINE);
slouken@2713
   270
    EFFECT_TEST(GUID_Square, SDL_HAPTIC_SQUARE);
slouken@2713
   271
    EFFECT_TEST(GUID_Triangle, SDL_HAPTIC_TRIANGLE);
slouken@2713
   272
    EFFECT_TEST(GUID_SawtoothUp, SDL_HAPTIC_SAWTOOTHUP);
slouken@2713
   273
    EFFECT_TEST(GUID_SawtoothDown, SDL_HAPTIC_SAWTOOTHDOWN);
slouken@2713
   274
    EFFECT_TEST(GUID_RampForce, SDL_HAPTIC_RAMP);
slouken@2713
   275
slouken@2713
   276
    /* Check for more. */
slouken@2713
   277
    return DIENUM_CONTINUE;
slouken@2713
   278
}
slouken@2713
   279
slouken@2713
   280
slouken@2713
   281
/*
slouken@2713
   282
 * Callback to get supported axes.
slouken@2713
   283
 */
slouken@2713
   284
static BOOL CALLBACK
slouken@2713
   285
DI_DeviceObjectCallback(LPCDIDEVICEOBJECTINSTANCE dev, LPVOID pvRef)
slouken@2713
   286
{
slouken@2713
   287
    SDL_Haptic *haptic = (SDL_Haptic *) pvRef;
slouken@2713
   288
slouken@2713
   289
    if ((dev->dwType & DIDFT_AXIS) && (dev->dwFlags & DIDOI_FFACTUATOR)) {
slouken@2713
   290
slouken@2713
   291
        haptic->hwdata->axes[haptic->naxes] = dev->dwOfs;
slouken@2713
   292
        haptic->naxes++;
slouken@2713
   293
slouken@2713
   294
        /* Currently using the artificial limit of 3 axes. */
slouken@2713
   295
        if (haptic->naxes >= 3) {
slouken@2713
   296
            return DIENUM_STOP;
slouken@2713
   297
        }
slouken@2713
   298
    }
slouken@2713
   299
slouken@2713
   300
    return DIENUM_CONTINUE;
slouken@2713
   301
}
slouken@2713
   302
slouken@2713
   303
slouken@2713
   304
/*
slouken@2713
   305
 * Opens the haptic device from the file descriptor.
slouken@2713
   306
 *
slouken@2713
   307
 *    Steps:
slouken@2713
   308
 *       - Open temporary DirectInputDevice interface.
icculus@6716
   309
 *       - Create DirectInputDevice8 interface.
slouken@2713
   310
 *       - Release DirectInputDevice interface.
icculus@6716
   311
 *       - Call SDL_SYS_HapticOpenFromDevice8
slouken@2713
   312
 */
slouken@2713
   313
static int
slouken@2713
   314
SDL_SYS_HapticOpenFromInstance(SDL_Haptic * haptic, DIDEVICEINSTANCE instance)
slouken@2713
   315
{
slouken@2713
   316
    HRESULT ret;
slouken@2713
   317
    int ret2;
icculus@6716
   318
    LPDIRECTINPUTDEVICE8 device;
slouken@2713
   319
slouken@2713
   320
    /* Allocate the hwdata */
slouken@2713
   321
    haptic->hwdata = (struct haptic_hwdata *)
slouken@2713
   322
        SDL_malloc(sizeof(*haptic->hwdata));
slouken@2713
   323
    if (haptic->hwdata == NULL) {
slouken@2713
   324
        SDL_OutOfMemory();
slouken@2713
   325
        goto creat_err;
slouken@2713
   326
    }
slouken@2713
   327
    SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
slouken@2713
   328
slouken@2713
   329
    /* Open the device */
icculus@6716
   330
    ret = IDirectInput8_CreateDevice(dinput, &instance.guidInstance,
slouken@2713
   331
                                    &device, NULL);
slouken@2713
   332
    if (FAILED(ret)) {
slouken@2713
   333
        DI_SetError("Creating DirectInput device", ret);
slouken@2713
   334
        goto creat_err;
slouken@2713
   335
    }
slouken@2713
   336
icculus@6716
   337
    /* Now get the IDirectInputDevice8 interface, instead. */
icculus@6716
   338
    ret = IDirectInputDevice8_QueryInterface(device,
icculus@6716
   339
                                            &IID_IDirectInputDevice8,
slouken@3013
   340
                                            (LPVOID *) & haptic->hwdata->
slouken@3013
   341
                                            device);
slouken@2713
   342
    /* Done with the temporary one now. */
icculus@6716
   343
    IDirectInputDevice8_Release(device);
slouken@2713
   344
    if (FAILED(ret)) {
slouken@2713
   345
        DI_SetError("Querying DirectInput interface", ret);
slouken@2713
   346
        goto creat_err;
slouken@2713
   347
    }
slouken@2713
   348
icculus@6716
   349
    ret2 = SDL_SYS_HapticOpenFromDevice8(haptic, haptic->hwdata->device);
slouken@2713
   350
    if (ret2 < 0) {
slouken@2713
   351
        goto query_err;
slouken@2713
   352
    }
slouken@2713
   353
slouken@2713
   354
    return 0;
slouken@2713
   355
slouken@2713
   356
  query_err:
icculus@6716
   357
    IDirectInputDevice8_Release(haptic->hwdata->device);
slouken@2713
   358
  creat_err:
slouken@2713
   359
    if (haptic->hwdata != NULL) {
slouken@2713
   360
        SDL_free(haptic->hwdata);
slouken@2713
   361
        haptic->hwdata = NULL;
slouken@2713
   362
    }
slouken@2713
   363
    return -1;
slouken@2713
   364
}
slouken@2713
   365
slouken@2713
   366
slouken@2713
   367
/*
slouken@2713
   368
 * Opens the haptic device from the file descriptor.
slouken@2713
   369
 *
slouken@2713
   370
 *    Steps:
slouken@2713
   371
 *       - Set cooperative level.
slouken@2713
   372
 *       - Set data format.
slouken@2713
   373
 *       - Acquire exclusiveness.
slouken@2713
   374
 *       - Reset actuators.
slouken@2713
   375
 *       - Get supported featuers.
slouken@2713
   376
 */
slouken@2713
   377
static int
icculus@6716
   378
SDL_SYS_HapticOpenFromDevice8(SDL_Haptic * haptic,
icculus@6716
   379
                              LPDIRECTINPUTDEVICE8 device8)
slouken@2713
   380
{
slouken@2713
   381
    HRESULT ret;
slouken@2713
   382
    DIPROPDWORD dipdw;
slouken@2713
   383
icculus@6716
   384
    /* We'll use the device8 from now on. */
icculus@6716
   385
    haptic->hwdata->device = device8;
slouken@2713
   386
slouken@2713
   387
    /* Grab it exclusively to use force feedback stuff. */
icculus@6716
   388
    ret = IDirectInputDevice8_SetCooperativeLevel(haptic->hwdata->device,
slouken@2713
   389
                                                  SDL_HelperWindow,
slouken@2713
   390
                                                  DISCL_EXCLUSIVE |
slouken@2713
   391
                                                  DISCL_BACKGROUND);
slouken@2713
   392
    if (FAILED(ret)) {
slouken@2713
   393
        DI_SetError("Setting cooperative level to exclusive", ret);
slouken@2713
   394
        goto acquire_err;
slouken@2713
   395
    }
slouken@2713
   396
slouken@2713
   397
    /* Set data format. */
icculus@6716
   398
    ret = IDirectInputDevice8_SetDataFormat(haptic->hwdata->device,
slouken@2713
   399
                                            &c_dfDIJoystick2);
slouken@2713
   400
    if (FAILED(ret)) {
slouken@2713
   401
        DI_SetError("Setting data format", ret);
slouken@2713
   402
        goto acquire_err;
slouken@2713
   403
    }
slouken@2713
   404
slouken@2713
   405
    /* Get number of axes. */
icculus@6716
   406
    ret = IDirectInputDevice8_EnumObjects(haptic->hwdata->device,
slouken@2713
   407
                                          DI_DeviceObjectCallback,
slouken@2713
   408
                                          haptic, DIDFT_AXIS);
slouken@2713
   409
    if (FAILED(ret)) {
slouken@2713
   410
        DI_SetError("Getting device axes", ret);
slouken@2713
   411
        goto acquire_err;
slouken@2713
   412
    }
slouken@2713
   413
slouken@2713
   414
    /* Acquire the device. */
icculus@6716
   415
    ret = IDirectInputDevice8_Acquire(haptic->hwdata->device);
slouken@2713
   416
    if (FAILED(ret)) {
slouken@2713
   417
        DI_SetError("Acquiring DirectInput device", ret);
slouken@2713
   418
        goto acquire_err;
slouken@2713
   419
    }
slouken@2713
   420
slouken@2713
   421
    /* Reset all actuators - just in case. */
icculus@6716
   422
    ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
slouken@2713
   423
                                                       DISFFC_RESET);
slouken@2713
   424
    if (FAILED(ret)) {
slouken@2713
   425
        DI_SetError("Resetting device", ret);
slouken@2713
   426
        goto acquire_err;
slouken@2713
   427
    }
slouken@2713
   428
slouken@2713
   429
    /* Enabling actuators. */
icculus@6716
   430
    ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
slouken@2713
   431
                                                       DISFFC_SETACTUATORSON);
slouken@2713
   432
    if (FAILED(ret)) {
slouken@2713
   433
        DI_SetError("Enabling actuators", ret);
slouken@2713
   434
        goto acquire_err;
slouken@2713
   435
    }
slouken@2713
   436
slouken@2713
   437
    /* Get supported effects. */
icculus@6716
   438
    ret = IDirectInputDevice8_EnumEffects(haptic->hwdata->device,
slouken@2713
   439
                                          DI_EffectCallback, haptic,
slouken@2713
   440
                                          DIEFT_ALL);
slouken@2713
   441
    if (FAILED(ret)) {
slouken@2713
   442
        DI_SetError("Enumerating supported effects", ret);
slouken@2713
   443
        goto acquire_err;
slouken@2713
   444
    }
slouken@2713
   445
    if (haptic->supported == 0) {       /* Error since device supports nothing. */
slouken@2713
   446
        SDL_SetError("Haptic: Internal error on finding supported effects.");
slouken@2713
   447
        goto acquire_err;
slouken@2713
   448
    }
slouken@2713
   449
slouken@2713
   450
    /* Check autogain and autocenter. */
slouken@2713
   451
    dipdw.diph.dwSize = sizeof(DIPROPDWORD);
slouken@2713
   452
    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
slouken@2713
   453
    dipdw.diph.dwObj = 0;
slouken@2713
   454
    dipdw.diph.dwHow = DIPH_DEVICE;
slouken@2713
   455
    dipdw.dwData = 10000;
icculus@6716
   456
    ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device,
slouken@2713
   457
                                          DIPROP_FFGAIN, &dipdw.diph);
slouken@2713
   458
    if (!FAILED(ret)) {         /* Gain is supported. */
slouken@2713
   459
        haptic->supported |= SDL_HAPTIC_GAIN;
slouken@2713
   460
    }
slouken@2713
   461
    dipdw.diph.dwObj = 0;
slouken@2713
   462
    dipdw.diph.dwHow = DIPH_DEVICE;
slouken@2713
   463
    dipdw.dwData = DIPROPAUTOCENTER_OFF;
icculus@6716
   464
    ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device,
slouken@2713
   465
                                          DIPROP_AUTOCENTER, &dipdw.diph);
slouken@2713
   466
    if (!FAILED(ret)) {         /* Autocenter is supported. */
slouken@2713
   467
        haptic->supported |= SDL_HAPTIC_AUTOCENTER;
slouken@2713
   468
    }
slouken@2713
   469
slouken@2713
   470
    /* Status is always supported. */
slouken@2713
   471
    haptic->supported |= SDL_HAPTIC_STATUS | SDL_HAPTIC_PAUSE;
slouken@2713
   472
slouken@2713
   473
    /* Check maximum effects. */
slouken@2713
   474
    haptic->neffects = 128;     /* This is not actually supported as thus under windows,
slouken@2713
   475
                                   there is no way to tell the number of EFFECTS that a
slouken@2713
   476
                                   device can hold, so we'll just use a "random" number
slouken@2713
   477
                                   instead and put warnings in SDL_haptic.h */
slouken@2713
   478
    haptic->nplaying = 128;     /* Even more impossible to get this then neffects. */
slouken@2713
   479
slouken@2713
   480
    /* Prepare effects memory. */
slouken@2713
   481
    haptic->effects = (struct haptic_effect *)
slouken@2713
   482
        SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
slouken@2713
   483
    if (haptic->effects == NULL) {
slouken@2713
   484
        SDL_OutOfMemory();
slouken@2713
   485
        goto acquire_err;
slouken@2713
   486
    }
slouken@2713
   487
    /* Clear the memory */
slouken@2713
   488
    SDL_memset(haptic->effects, 0,
slouken@2713
   489
               sizeof(struct haptic_effect) * haptic->neffects);
slouken@2713
   490
slouken@2713
   491
    return 0;
slouken@2713
   492
slouken@2713
   493
    /* Error handling */
slouken@2713
   494
  acquire_err:
icculus@6716
   495
    IDirectInputDevice8_Unacquire(haptic->hwdata->device);
slouken@2713
   496
    return -1;
slouken@2713
   497
slouken@2713
   498
}
slouken@2713
   499
slouken@2713
   500
slouken@2713
   501
/*
slouken@2713
   502
 * Opens a haptic device for usage.
slouken@2713
   503
 */
slouken@2713
   504
int
slouken@2713
   505
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
slouken@2713
   506
{
slouken@2713
   507
    return SDL_SYS_HapticOpenFromInstance(haptic,
slouken@3013
   508
                                          SDL_hapticlist[haptic->index].
slouken@3013
   509
                                          instance);
slouken@2713
   510
}
slouken@2713
   511
slouken@2713
   512
slouken@2713
   513
/*
slouken@2713
   514
 * Opens a haptic device from first mouse it finds for usage.
slouken@2713
   515
 */
slouken@2713
   516
int
slouken@2713
   517
SDL_SYS_HapticMouse(void)
slouken@2713
   518
{
slouken@2713
   519
    int i;
slouken@2713
   520
slouken@2713
   521
    /* Grab the first mouse haptic device we find. */
slouken@2713
   522
    for (i = 0; i < SDL_numhaptics; i++) {
slouken@6690
   523
        if (SDL_hapticlist[i].capabilities.dwDevType == DI8DEVCLASS_POINTER ) {
slouken@2713
   524
            return i;
slouken@2713
   525
        }
slouken@2713
   526
    }
slouken@2713
   527
slouken@2713
   528
    return -1;
slouken@2713
   529
}
slouken@2713
   530
slouken@2713
   531
slouken@2713
   532
/*
slouken@2713
   533
 * Checks to see if a joystick has haptic features.
slouken@2713
   534
 */
slouken@2713
   535
int
slouken@2713
   536
SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
slouken@2713
   537
{
slouken@2713
   538
    if (joystick->hwdata->Capabilities.dwFlags & DIDC_FORCEFEEDBACK) {
slouken@2713
   539
        return SDL_TRUE;
slouken@2713
   540
    }
slouken@2713
   541
slouken@2713
   542
    return SDL_FALSE;
slouken@2713
   543
}
slouken@2713
   544
slouken@2713
   545
slouken@2713
   546
/*
slouken@2713
   547
 * Checks to see if the haptic device and joystick and in reality the same.
slouken@2713
   548
 */
slouken@2713
   549
int
slouken@2713
   550
SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
slouken@2713
   551
{
slouken@2713
   552
    HRESULT ret;
slouken@2713
   553
    DIDEVICEINSTANCE hap_instance, joy_instance;
slouken@5539
   554
    hap_instance.dwSize = sizeof(DIDEVICEINSTANCE);
slouken@5539
   555
    joy_instance.dwSize = sizeof(DIDEVICEINSTANCE);
slouken@2713
   556
slouken@2713
   557
    /* Get the device instances. */
icculus@6716
   558
    ret = IDirectInputDevice8_GetDeviceInfo(haptic->hwdata->device,
slouken@2713
   559
                                            &hap_instance);
slouken@2713
   560
    if (FAILED(ret)) {
slouken@2713
   561
        return 0;
slouken@2713
   562
    }
icculus@6716
   563
    ret = IDirectInputDevice8_GetDeviceInfo(joystick->hwdata->InputDevice,
slouken@2713
   564
                                            &joy_instance);
slouken@2713
   565
    if (FAILED(ret)) {
slouken@2713
   566
        return 0;
slouken@2713
   567
    }
slouken@2713
   568
slouken@2713
   569
    if (DI_GUIDIsSame(&hap_instance.guidInstance, &joy_instance.guidInstance))
slouken@2713
   570
        return 1;
slouken@2713
   571
slouken@2713
   572
    return 0;
slouken@2713
   573
}
slouken@2713
   574
slouken@2713
   575
slouken@2713
   576
/*
slouken@2713
   577
 * Opens a SDL_Haptic from a SDL_Joystick.
slouken@2713
   578
 */
slouken@2713
   579
int
slouken@2713
   580
SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
slouken@2713
   581
{
slouken@5358
   582
    int i, ret;
slouken@5358
   583
    HRESULT idret;
slouken@5358
   584
    DIDEVICEINSTANCE joy_instance;
slouken@5539
   585
    joy_instance.dwSize = sizeof(DIDEVICEINSTANCE);
slouken@5358
   586
slouken@5358
   587
    /* Since it comes from a joystick we have to try to match it with a haptic device on our haptic list. */
slouken@5358
   588
    for (i=0; i<SDL_numhaptics; i++) {
icculus@6716
   589
        idret = IDirectInputDevice8_GetDeviceInfo(joystick->hwdata->InputDevice,
slouken@5358
   590
              &joy_instance);
slouken@5358
   591
        if (FAILED(idret)) {
slouken@5358
   592
            return -1;
slouken@5358
   593
        }
slouken@5358
   594
        if (DI_GUIDIsSame(&SDL_hapticlist[i].instance.guidInstance,
slouken@5358
   595
                          &joy_instance.guidInstance)) {
slouken@5358
   596
            haptic->index = i;
slouken@5358
   597
            break;
slouken@5358
   598
        }
slouken@5358
   599
    }
slouken@5358
   600
    if (i >= SDL_numhaptics) {
slouken@5358
   601
        return -1;
slouken@5358
   602
    }
slouken@2713
   603
slouken@2713
   604
    /* Allocate the hwdata */
slouken@2713
   605
    haptic->hwdata = (struct haptic_hwdata *)
slouken@2713
   606
        SDL_malloc(sizeof(*haptic->hwdata));
slouken@2713
   607
    if (haptic->hwdata == NULL) {
slouken@2713
   608
        SDL_OutOfMemory();
slouken@2713
   609
        return -1;
slouken@2713
   610
    }
slouken@2713
   611
    SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
slouken@2713
   612
slouken@2713
   613
    /* Now open the device. */
slouken@2713
   614
    ret =
icculus@6716
   615
        SDL_SYS_HapticOpenFromDevice8(haptic, joystick->hwdata->InputDevice);
slouken@2713
   616
    if (ret < 0) {
slouken@2713
   617
        return -1;
slouken@2713
   618
    }
slouken@2713
   619
slouken@2713
   620
    /* It's using the joystick device. */
slouken@2713
   621
    haptic->hwdata->is_joystick = 1;
slouken@2713
   622
slouken@2713
   623
    return 0;
slouken@2713
   624
}
slouken@2713
   625
slouken@2713
   626
slouken@2713
   627
/*
slouken@2713
   628
 * Closes the haptic device.
slouken@2713
   629
 */
slouken@2713
   630
void
slouken@2713
   631
SDL_SYS_HapticClose(SDL_Haptic * haptic)
slouken@2713
   632
{
slouken@2713
   633
    if (haptic->hwdata) {
slouken@2713
   634
slouken@2713
   635
        /* Free effects. */
slouken@2713
   636
        SDL_free(haptic->effects);
slouken@2713
   637
        haptic->effects = NULL;
slouken@2713
   638
        haptic->neffects = 0;
slouken@2713
   639
slouken@2713
   640
        /* Clean up */
icculus@6716
   641
        IDirectInputDevice8_Unacquire(haptic->hwdata->device);
slouken@2713
   642
        /* Only release if isn't grabbed by a joystick. */
slouken@2713
   643
        if (haptic->hwdata->is_joystick == 0) {
icculus@6716
   644
            IDirectInputDevice8_Release(haptic->hwdata->device);
slouken@2713
   645
        }
slouken@2713
   646
slouken@2713
   647
        /* Free */
slouken@2713
   648
        SDL_free(haptic->hwdata);
slouken@2713
   649
        haptic->hwdata = NULL;
slouken@2713
   650
    }
slouken@2713
   651
}
slouken@2713
   652
slouken@2713
   653
slouken@2713
   654
/* 
slouken@2713
   655
 * Clean up after system specific haptic stuff
slouken@2713
   656
 */
slouken@2713
   657
void
slouken@2713
   658
SDL_SYS_HapticQuit(void)
slouken@2713
   659
{
slouken@5090
   660
    int i;
slouken@5090
   661
slouken@5090
   662
    for (i = 0; i < SDL_arraysize(SDL_hapticlist); ++i) {
slouken@5090
   663
        if (SDL_hapticlist[i].name) {
slouken@5090
   664
            SDL_free(SDL_hapticlist[i].name);
slouken@5090
   665
            SDL_hapticlist[i].name = NULL;
slouken@5090
   666
        }
slouken@5090
   667
    }
slouken@5090
   668
icculus@5591
   669
    if (dinput != NULL) {
icculus@6716
   670
        IDirectInput8_Release(dinput);
icculus@5591
   671
        dinput = NULL;
icculus@5591
   672
    }
icculus@5591
   673
icculus@5591
   674
    if (coinitialized) {
icculus@5591
   675
        WIN_CoUninitialize();
icculus@5591
   676
        coinitialized = SDL_FALSE;
icculus@5591
   677
    }
slouken@2713
   678
}
slouken@2713
   679
slouken@2713
   680
slouken@2713
   681
/*
slouken@2713
   682
 * Converts an SDL trigger button to an DIEFFECT trigger button.
slouken@2713
   683
 */
slouken@2713
   684
static DWORD
slouken@2713
   685
DIGetTriggerButton(Uint16 button)
slouken@2713
   686
{
slouken@2713
   687
    DWORD dwTriggerButton;
slouken@2713
   688
slouken@2713
   689
    dwTriggerButton = DIEB_NOTRIGGER;
slouken@2713
   690
slouken@2713
   691
    if (button != 0) {
slouken@2713
   692
        dwTriggerButton = DIJOFS_BUTTON(button - 1);
slouken@2713
   693
    }
slouken@2713
   694
slouken@2713
   695
    return dwTriggerButton;
slouken@2713
   696
}
slouken@2713
   697
slouken@2713
   698
slouken@2713
   699
/*
slouken@2713
   700
 * Sets the direction.
slouken@2713
   701
 */
slouken@2713
   702
static int
slouken@2713
   703
SDL_SYS_SetDirection(DIEFFECT * effect, SDL_HapticDirection * dir, int naxes)
slouken@2713
   704
{
slouken@2713
   705
    LONG *rglDir;
slouken@2713
   706
slouken@2713
   707
    /* Handle no axes a part. */
slouken@2713
   708
    if (naxes == 0) {
slouken@2713
   709
        effect->dwFlags |= DIEFF_SPHERICAL;     /* Set as default. */
slouken@2713
   710
        effect->rglDirection = NULL;
slouken@2713
   711
        return 0;
slouken@2713
   712
    }
slouken@2713
   713
slouken@2713
   714
    /* Has axes. */
slouken@2713
   715
    rglDir = SDL_malloc(sizeof(LONG) * naxes);
slouken@2713
   716
    if (rglDir == NULL) {
slouken@2713
   717
        SDL_OutOfMemory();
slouken@2713
   718
        return -1;
slouken@2713
   719
    }
slouken@2713
   720
    SDL_memset(rglDir, 0, sizeof(LONG) * naxes);
slouken@2713
   721
    effect->rglDirection = rglDir;
slouken@2713
   722
slouken@2713
   723
    switch (dir->type) {
slouken@2713
   724
    case SDL_HAPTIC_POLAR:
slouken@2713
   725
        effect->dwFlags |= DIEFF_POLAR;
slouken@2713
   726
        rglDir[0] = dir->dir[0];
slouken@2713
   727
        return 0;
slouken@2713
   728
    case SDL_HAPTIC_CARTESIAN:
slouken@2713
   729
        effect->dwFlags |= DIEFF_CARTESIAN;
slouken@2713
   730
        rglDir[0] = dir->dir[0];
slouken@2713
   731
        if (naxes > 1)
slouken@2713
   732
            rglDir[1] = dir->dir[1];
slouken@2713
   733
        if (naxes > 2)
slouken@2713
   734
            rglDir[2] = dir->dir[2];
slouken@2713
   735
        return 0;
slouken@2713
   736
    case SDL_HAPTIC_SPHERICAL:
slouken@2713
   737
        effect->dwFlags |= DIEFF_SPHERICAL;
slouken@2713
   738
        rglDir[0] = dir->dir[0];
slouken@2713
   739
        if (naxes > 1)
slouken@2713
   740
            rglDir[1] = dir->dir[1];
slouken@2713
   741
        if (naxes > 2)
slouken@2713
   742
            rglDir[2] = dir->dir[2];
slouken@2713
   743
        return 0;
slouken@2713
   744
slouken@2713
   745
    default:
slouken@2713
   746
        SDL_SetError("Haptic: Unknown direction type.");
slouken@2713
   747
        return -1;
slouken@2713
   748
    }
slouken@2713
   749
}
slouken@2713
   750
slouken@2713
   751
#define CONVERT(x)   (((x) > 0x7FFF) ? 10000 : ((x)*10000) / 0x7FFF)
slouken@2713
   752
/*
slouken@2713
   753
 * Creates the DIEFFECT from a SDL_HapticEffect.
slouken@2713
   754
 */
slouken@2713
   755
static int
slouken@2713
   756
SDL_SYS_ToDIEFFECT(SDL_Haptic * haptic, DIEFFECT * dest,
slouken@2713
   757
                   SDL_HapticEffect * src)
slouken@2713
   758
{
slouken@2713
   759
    int i;
slouken@2713
   760
    DICONSTANTFORCE *constant;
slouken@2713
   761
    DIPERIODIC *periodic;
slouken@2713
   762
    DICONDITION *condition;     /* Actually an array of conditions - one per axis. */
slouken@2713
   763
    DIRAMPFORCE *ramp;
slouken@2713
   764
    DICUSTOMFORCE *custom;
slouken@2713
   765
    DIENVELOPE *envelope;
slouken@2713
   766
    SDL_HapticConstant *hap_constant;
slouken@2713
   767
    SDL_HapticPeriodic *hap_periodic;
slouken@2713
   768
    SDL_HapticCondition *hap_condition;
slouken@2713
   769
    SDL_HapticRamp *hap_ramp;
slouken@2713
   770
    SDL_HapticCustom *hap_custom;
slouken@2713
   771
    DWORD *axes;
slouken@2713
   772
slouken@2713
   773
    /* Set global stuff. */
slouken@2713
   774
    SDL_memset(dest, 0, sizeof(DIEFFECT));
slouken@2713
   775
    dest->dwSize = sizeof(DIEFFECT);    /* Set the structure size. */
slouken@2713
   776
    dest->dwSamplePeriod = 0;   /* Not used by us. */
slouken@2713
   777
    dest->dwGain = 10000;       /* Gain is set globally, not locally. */
slouken@2713
   778
    dest->dwFlags = DIEFF_OBJECTOFFSETS;        /* Seems obligatory. */
slouken@2713
   779
slouken@2713
   780
    /* Envelope. */
slouken@2713
   781
    envelope = SDL_malloc(sizeof(DIENVELOPE));
slouken@2713
   782
    if (envelope == NULL) {
slouken@2713
   783
        SDL_OutOfMemory();
slouken@2713
   784
        return -1;
slouken@2713
   785
    }
slouken@2713
   786
    SDL_memset(envelope, 0, sizeof(DIENVELOPE));
slouken@2713
   787
    dest->lpEnvelope = envelope;
slouken@2713
   788
    envelope->dwSize = sizeof(DIENVELOPE);      /* Always should be this. */
slouken@2713
   789
slouken@2713
   790
    /* Axes. */
slouken@2713
   791
    dest->cAxes = haptic->naxes;
slouken@2713
   792
    if (dest->cAxes > 0) {
slouken@2713
   793
        axes = SDL_malloc(sizeof(DWORD) * dest->cAxes);
slouken@2713
   794
        if (axes == NULL) {
slouken@2713
   795
            SDL_OutOfMemory();
slouken@2713
   796
            return -1;
slouken@2713
   797
        }
slouken@2713
   798
        axes[0] = haptic->hwdata->axes[0];      /* Always at least one axis. */
slouken@2713
   799
        if (dest->cAxes > 1) {
slouken@2713
   800
            axes[1] = haptic->hwdata->axes[1];
slouken@2713
   801
        }
slouken@2713
   802
        if (dest->cAxes > 2) {
slouken@2713
   803
            axes[2] = haptic->hwdata->axes[2];
slouken@2713
   804
        }
slouken@2713
   805
        dest->rgdwAxes = axes;
slouken@2713
   806
    }
slouken@2713
   807
slouken@2713
   808
slouken@2713
   809
    /* The big type handling switch, even bigger then linux's version. */
slouken@2713
   810
    switch (src->type) {
slouken@2713
   811
    case SDL_HAPTIC_CONSTANT:
slouken@2713
   812
        hap_constant = &src->constant;
slouken@2713
   813
        constant = SDL_malloc(sizeof(DICONSTANTFORCE));
slouken@2713
   814
        if (constant == NULL) {
slouken@2713
   815
            SDL_OutOfMemory();
slouken@2713
   816
            return -1;
slouken@2713
   817
        }
slouken@2713
   818
        SDL_memset(constant, 0, sizeof(DICONSTANTFORCE));
slouken@2713
   819
slouken@2713
   820
        /* Specifics */
slouken@2713
   821
        constant->lMagnitude = CONVERT(hap_constant->level);
slouken@2713
   822
        dest->cbTypeSpecificParams = sizeof(DICONSTANTFORCE);
slouken@2713
   823
        dest->lpvTypeSpecificParams = constant;
slouken@2713
   824
slouken@2713
   825
        /* Generics */
slouken@2713
   826
        dest->dwDuration = hap_constant->length * 1000; /* In microseconds. */
slouken@2713
   827
        dest->dwTriggerButton = DIGetTriggerButton(hap_constant->button);
slouken@2713
   828
        dest->dwTriggerRepeatInterval = hap_constant->interval;
slouken@2713
   829
        dest->dwStartDelay = hap_constant->delay * 1000;        /* In microseconds. */
slouken@2713
   830
slouken@2713
   831
        /* Direction. */
slouken@2713
   832
        if (SDL_SYS_SetDirection(dest, &hap_constant->direction, dest->cAxes)
slouken@2713
   833
            < 0) {
slouken@2713
   834
            return -1;
slouken@2713
   835
        }
slouken@2713
   836
slouken@2713
   837
        /* Envelope */
slouken@2713
   838
        if ((hap_constant->attack_length == 0)
slouken@2713
   839
            && (hap_constant->fade_length == 0)) {
slouken@2713
   840
            SDL_free(dest->lpEnvelope);
slouken@2713
   841
            dest->lpEnvelope = NULL;
slouken@2713
   842
        } else {
slouken@2713
   843
            envelope->dwAttackLevel = CONVERT(hap_constant->attack_level);
slouken@2713
   844
            envelope->dwAttackTime = hap_constant->attack_length * 1000;
slouken@2713
   845
            envelope->dwFadeLevel = CONVERT(hap_constant->fade_level);
slouken@2713
   846
            envelope->dwFadeTime = hap_constant->fade_length * 1000;
slouken@2713
   847
        }
slouken@2713
   848
slouken@2713
   849
        break;
slouken@2713
   850
slouken@2713
   851
    case SDL_HAPTIC_SINE:
slouken@2713
   852
    case SDL_HAPTIC_SQUARE:
slouken@2713
   853
    case SDL_HAPTIC_TRIANGLE:
slouken@2713
   854
    case SDL_HAPTIC_SAWTOOTHUP:
slouken@2713
   855
    case SDL_HAPTIC_SAWTOOTHDOWN:
slouken@2713
   856
        hap_periodic = &src->periodic;
slouken@2713
   857
        periodic = SDL_malloc(sizeof(DIPERIODIC));
slouken@2713
   858
        if (periodic == NULL) {
slouken@2713
   859
            SDL_OutOfMemory();
slouken@2713
   860
            return -1;
slouken@2713
   861
        }
slouken@2713
   862
        SDL_memset(periodic, 0, sizeof(DIPERIODIC));
slouken@2713
   863
slouken@2713
   864
        /* Specifics */
slouken@2713
   865
        periodic->dwMagnitude = CONVERT(hap_periodic->magnitude);
slouken@2713
   866
        periodic->lOffset = CONVERT(hap_periodic->offset);
slouken@2713
   867
        periodic->dwPhase = hap_periodic->phase;
slouken@2713
   868
        periodic->dwPeriod = hap_periodic->period * 1000;
slouken@2713
   869
        dest->cbTypeSpecificParams = sizeof(DIPERIODIC);
slouken@2713
   870
        dest->lpvTypeSpecificParams = periodic;
slouken@2713
   871
slouken@2713
   872
        /* Generics */
slouken@2713
   873
        dest->dwDuration = hap_periodic->length * 1000; /* In microseconds. */
slouken@2713
   874
        dest->dwTriggerButton = DIGetTriggerButton(hap_periodic->button);
slouken@2713
   875
        dest->dwTriggerRepeatInterval = hap_periodic->interval;
slouken@2713
   876
        dest->dwStartDelay = hap_periodic->delay * 1000;        /* In microseconds. */
slouken@2713
   877
slouken@2713
   878
        /* Direction. */
slouken@2713
   879
        if (SDL_SYS_SetDirection(dest, &hap_periodic->direction, dest->cAxes)
slouken@2713
   880
            < 0) {
slouken@2713
   881
            return -1;
slouken@2713
   882
        }
slouken@2713
   883
slouken@2713
   884
        /* Envelope */
slouken@2713
   885
        if ((hap_periodic->attack_length == 0)
slouken@2713
   886
            && (hap_periodic->fade_length == 0)) {
slouken@2713
   887
            SDL_free(dest->lpEnvelope);
slouken@2713
   888
            dest->lpEnvelope = NULL;
slouken@2713
   889
        } else {
slouken@2713
   890
            envelope->dwAttackLevel = CONVERT(hap_periodic->attack_level);
slouken@2713
   891
            envelope->dwAttackTime = hap_periodic->attack_length * 1000;
slouken@2713
   892
            envelope->dwFadeLevel = CONVERT(hap_periodic->fade_level);
slouken@2713
   893
            envelope->dwFadeTime = hap_periodic->fade_length * 1000;
slouken@2713
   894
        }
slouken@2713
   895
slouken@2713
   896
        break;
slouken@2713
   897
slouken@2713
   898
    case SDL_HAPTIC_SPRING:
slouken@2713
   899
    case SDL_HAPTIC_DAMPER:
slouken@2713
   900
    case SDL_HAPTIC_INERTIA:
slouken@2713
   901
    case SDL_HAPTIC_FRICTION:
slouken@2713
   902
        hap_condition = &src->condition;
slouken@2713
   903
        condition = SDL_malloc(sizeof(DICONDITION) * dest->cAxes);
slouken@2713
   904
        if (condition == NULL) {
slouken@2713
   905
            SDL_OutOfMemory();
slouken@2713
   906
            return -1;
slouken@2713
   907
        }
slouken@2713
   908
        SDL_memset(condition, 0, sizeof(DICONDITION));
slouken@2713
   909
slouken@2713
   910
        /* Specifics */
slouken@2713
   911
        for (i = 0; i < (int) dest->cAxes; i++) {
slouken@2713
   912
            condition[i].lOffset = CONVERT(hap_condition->center[i]);
slouken@2713
   913
            condition[i].lPositiveCoefficient =
slouken@2713
   914
                CONVERT(hap_condition->right_coeff[i]);
slouken@2713
   915
            condition[i].lNegativeCoefficient =
slouken@2713
   916
                CONVERT(hap_condition->left_coeff[i]);
slouken@2713
   917
            condition[i].dwPositiveSaturation =
slouken@2713
   918
                CONVERT(hap_condition->right_sat[i]);
slouken@2713
   919
            condition[i].dwNegativeSaturation =
slouken@2713
   920
                CONVERT(hap_condition->left_sat[i]);
slouken@2713
   921
            condition[i].lDeadBand = CONVERT(hap_condition->deadband[i]);
slouken@2713
   922
        }
slouken@2713
   923
        dest->cbTypeSpecificParams = sizeof(DICONDITION) * dest->cAxes;
slouken@2713
   924
        dest->lpvTypeSpecificParams = condition;
slouken@2713
   925
slouken@2713
   926
        /* Generics */
slouken@2713
   927
        dest->dwDuration = hap_condition->length * 1000;        /* In microseconds. */
slouken@2713
   928
        dest->dwTriggerButton = DIGetTriggerButton(hap_condition->button);
slouken@2713
   929
        dest->dwTriggerRepeatInterval = hap_condition->interval;
slouken@2713
   930
        dest->dwStartDelay = hap_condition->delay * 1000;       /* In microseconds. */
slouken@2713
   931
slouken@2713
   932
        /* Direction. */
slouken@2713
   933
        if (SDL_SYS_SetDirection(dest, &hap_condition->direction, dest->cAxes)
slouken@2713
   934
            < 0) {
slouken@2713
   935
            return -1;
slouken@2713
   936
        }
slouken@2713
   937
slouken@2713
   938
        /* Envelope - Not actually supported by most CONDITION implementations. */
slouken@2713
   939
        SDL_free(dest->lpEnvelope);
slouken@2713
   940
        dest->lpEnvelope = NULL;
slouken@2713
   941
slouken@2713
   942
        break;
slouken@2713
   943
slouken@2713
   944
    case SDL_HAPTIC_RAMP:
slouken@2713
   945
        hap_ramp = &src->ramp;
slouken@2713
   946
        ramp = SDL_malloc(sizeof(DIRAMPFORCE));
slouken@2713
   947
        if (ramp == NULL) {
slouken@2713
   948
            SDL_OutOfMemory();
slouken@2713
   949
            return -1;
slouken@2713
   950
        }
slouken@2713
   951
        SDL_memset(ramp, 0, sizeof(DIRAMPFORCE));
slouken@2713
   952
slouken@2713
   953
        /* Specifics */
slouken@2713
   954
        ramp->lStart = CONVERT(hap_ramp->start);
slouken@2713
   955
        ramp->lEnd = CONVERT(hap_ramp->end);
slouken@2713
   956
        dest->cbTypeSpecificParams = sizeof(DIRAMPFORCE);
slouken@2713
   957
        dest->lpvTypeSpecificParams = ramp;
slouken@2713
   958
slouken@2713
   959
        /* Generics */
slouken@2713
   960
        dest->dwDuration = hap_ramp->length * 1000;     /* In microseconds. */
slouken@2713
   961
        dest->dwTriggerButton = DIGetTriggerButton(hap_ramp->button);
slouken@2713
   962
        dest->dwTriggerRepeatInterval = hap_ramp->interval;
slouken@2713
   963
        dest->dwStartDelay = hap_ramp->delay * 1000;    /* In microseconds. */
slouken@2713
   964
slouken@2713
   965
        /* Direction. */
slouken@2713
   966
        if (SDL_SYS_SetDirection(dest, &hap_ramp->direction, dest->cAxes) < 0) {
slouken@2713
   967
            return -1;
slouken@2713
   968
        }
slouken@2713
   969
slouken@2713
   970
        /* Envelope */
slouken@2713
   971
        if ((hap_ramp->attack_length == 0) && (hap_ramp->fade_length == 0)) {
slouken@2713
   972
            SDL_free(dest->lpEnvelope);
slouken@2713
   973
            dest->lpEnvelope = NULL;
slouken@2713
   974
        } else {
slouken@2713
   975
            envelope->dwAttackLevel = CONVERT(hap_ramp->attack_level);
slouken@2713
   976
            envelope->dwAttackTime = hap_ramp->attack_length * 1000;
slouken@2713
   977
            envelope->dwFadeLevel = CONVERT(hap_ramp->fade_level);
slouken@2713
   978
            envelope->dwFadeTime = hap_ramp->fade_length * 1000;
slouken@2713
   979
        }
slouken@2713
   980
slouken@2713
   981
        break;
slouken@2713
   982
slouken@2713
   983
    case SDL_HAPTIC_CUSTOM:
slouken@2713
   984
        hap_custom = &src->custom;
slouken@2713
   985
        custom = SDL_malloc(sizeof(DICUSTOMFORCE));
slouken@2713
   986
        if (custom == NULL) {
slouken@2713
   987
            SDL_OutOfMemory();
slouken@2713
   988
            return -1;
slouken@2713
   989
        }
slouken@2713
   990
        SDL_memset(custom, 0, sizeof(DICUSTOMFORCE));
slouken@2713
   991
slouken@2713
   992
        /* Specifics */
slouken@2713
   993
        custom->cChannels = hap_custom->channels;
slouken@2713
   994
        custom->dwSamplePeriod = hap_custom->period * 1000;
slouken@2713
   995
        custom->cSamples = hap_custom->samples;
slouken@2713
   996
        custom->rglForceData =
slouken@2713
   997
            SDL_malloc(sizeof(LONG) * custom->cSamples * custom->cChannels);
slouken@2713
   998
        for (i = 0; i < hap_custom->samples * hap_custom->channels; i++) {      /* Copy data. */
slouken@2713
   999
            custom->rglForceData[i] = CONVERT(hap_custom->data[i]);
slouken@2713
  1000
        }
slouken@2713
  1001
        dest->cbTypeSpecificParams = sizeof(DICUSTOMFORCE);
slouken@2713
  1002
        dest->lpvTypeSpecificParams = custom;
slouken@2713
  1003
slouken@2713
  1004
        /* Generics */
slouken@2713
  1005
        dest->dwDuration = hap_custom->length * 1000;   /* In microseconds. */
slouken@2713
  1006
        dest->dwTriggerButton = DIGetTriggerButton(hap_custom->button);
slouken@2713
  1007
        dest->dwTriggerRepeatInterval = hap_custom->interval;
slouken@2713
  1008
        dest->dwStartDelay = hap_custom->delay * 1000;  /* In microseconds. */
slouken@2713
  1009
slouken@2713
  1010
        /* Direction. */
slouken@2713
  1011
        if (SDL_SYS_SetDirection(dest, &hap_custom->direction, dest->cAxes) <
slouken@2713
  1012
            0) {
slouken@2713
  1013
            return -1;
slouken@2713
  1014
        }
slouken@2713
  1015
slouken@2713
  1016
        /* Envelope */
slouken@2713
  1017
        if ((hap_custom->attack_length == 0)
slouken@2713
  1018
            && (hap_custom->fade_length == 0)) {
slouken@2713
  1019
            SDL_free(dest->lpEnvelope);
slouken@2713
  1020
            dest->lpEnvelope = NULL;
slouken@2713
  1021
        } else {
slouken@2713
  1022
            envelope->dwAttackLevel = CONVERT(hap_custom->attack_level);
slouken@2713
  1023
            envelope->dwAttackTime = hap_custom->attack_length * 1000;
slouken@2713
  1024
            envelope->dwFadeLevel = CONVERT(hap_custom->fade_level);
slouken@2713
  1025
            envelope->dwFadeTime = hap_custom->fade_length * 1000;
slouken@2713
  1026
        }
slouken@2713
  1027
slouken@2713
  1028
        break;
slouken@2713
  1029
slouken@2713
  1030
slouken@2713
  1031
    default:
slouken@2713
  1032
        SDL_SetError("Haptic: Unknown effect type.");
slouken@2713
  1033
        return -1;
slouken@2713
  1034
    }
slouken@2713
  1035
slouken@2713
  1036
    return 0;
slouken@2713
  1037
}
slouken@2713
  1038
slouken@2713
  1039
slouken@2713
  1040
/*
slouken@2713
  1041
 * Frees an DIEFFECT allocated by SDL_SYS_ToDIEFFECT.
slouken@2713
  1042
 */
slouken@2713
  1043
static void
slouken@2713
  1044
SDL_SYS_HapticFreeDIEFFECT(DIEFFECT * effect, int type)
slouken@2713
  1045
{
slouken@2713
  1046
    DICUSTOMFORCE *custom;
slouken@2713
  1047
slouken@2713
  1048
    if (effect->lpEnvelope != NULL) {
slouken@2713
  1049
        SDL_free(effect->lpEnvelope);
slouken@2713
  1050
        effect->lpEnvelope = NULL;
slouken@2713
  1051
    }
slouken@2713
  1052
    if (effect->rgdwAxes != NULL) {
slouken@2713
  1053
        SDL_free(effect->rgdwAxes);
slouken@2713
  1054
        effect->rgdwAxes = NULL;
slouken@2713
  1055
    }
slouken@2713
  1056
    if (effect->lpvTypeSpecificParams != NULL) {
slouken@2713
  1057
        if (type == SDL_HAPTIC_CUSTOM) {        /* Must free the custom data. */
slouken@2713
  1058
            custom = (DICUSTOMFORCE *) effect->lpvTypeSpecificParams;
slouken@2713
  1059
            SDL_free(custom->rglForceData);
slouken@2713
  1060
            custom->rglForceData = NULL;
slouken@2713
  1061
        }
slouken@2713
  1062
        SDL_free(effect->lpvTypeSpecificParams);
slouken@2713
  1063
        effect->lpvTypeSpecificParams = NULL;
slouken@2713
  1064
    }
slouken@2713
  1065
    if (effect->rglDirection != NULL) {
slouken@2713
  1066
        SDL_free(effect->rglDirection);
slouken@2713
  1067
        effect->rglDirection = NULL;
slouken@2713
  1068
    }
slouken@2713
  1069
}
slouken@2713
  1070
slouken@2713
  1071
slouken@2713
  1072
/*
slouken@2713
  1073
 * Gets the effect type from the generic SDL haptic effect wrapper.
slouken@2713
  1074
 */
slouken@2713
  1075
static REFGUID
slouken@2713
  1076
SDL_SYS_HapticEffectType(SDL_HapticEffect * effect)
slouken@2713
  1077
{
slouken@2713
  1078
    switch (effect->type) {
slouken@2713
  1079
    case SDL_HAPTIC_CONSTANT:
slouken@2713
  1080
        return &GUID_ConstantForce;
slouken@2713
  1081
slouken@2713
  1082
    case SDL_HAPTIC_RAMP:
slouken@2713
  1083
        return &GUID_RampForce;
slouken@2713
  1084
slouken@2713
  1085
    case SDL_HAPTIC_SQUARE:
slouken@2713
  1086
        return &GUID_Square;
slouken@2713
  1087
slouken@2713
  1088
    case SDL_HAPTIC_SINE:
slouken@2713
  1089
        return &GUID_Sine;
slouken@2713
  1090
slouken@2713
  1091
    case SDL_HAPTIC_TRIANGLE:
slouken@2713
  1092
        return &GUID_Triangle;
slouken@2713
  1093
slouken@2713
  1094
    case SDL_HAPTIC_SAWTOOTHUP:
slouken@2713
  1095
        return &GUID_SawtoothUp;
slouken@2713
  1096
slouken@2713
  1097
    case SDL_HAPTIC_SAWTOOTHDOWN:
slouken@2713
  1098
        return &GUID_SawtoothDown;
slouken@2713
  1099
slouken@2713
  1100
    case SDL_HAPTIC_SPRING:
slouken@2713
  1101
        return &GUID_Spring;
slouken@2713
  1102
slouken@2713
  1103
    case SDL_HAPTIC_DAMPER:
slouken@2713
  1104
        return &GUID_Damper;
slouken@2713
  1105
slouken@2713
  1106
    case SDL_HAPTIC_INERTIA:
slouken@2713
  1107
        return &GUID_Inertia;
slouken@2713
  1108
slouken@2713
  1109
    case SDL_HAPTIC_FRICTION:
slouken@2713
  1110
        return &GUID_Friction;
slouken@2713
  1111
slouken@2713
  1112
    case SDL_HAPTIC_CUSTOM:
slouken@2713
  1113
        return &GUID_CustomForce;
slouken@2713
  1114
slouken@2713
  1115
    default:
slouken@2713
  1116
        SDL_SetError("Haptic: Unknown effect type.");
slouken@2713
  1117
        return NULL;
slouken@2713
  1118
    }
slouken@2713
  1119
}
slouken@2713
  1120
slouken@2713
  1121
slouken@2713
  1122
/*
slouken@2713
  1123
 * Creates a new haptic effect.
slouken@2713
  1124
 */
slouken@2713
  1125
int
slouken@2713
  1126
SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
slouken@2713
  1127
                        SDL_HapticEffect * base)
slouken@2713
  1128
{
slouken@2713
  1129
    HRESULT ret;
slouken@2713
  1130
slouken@2713
  1131
    /* Get the type. */
slouken@2713
  1132
    REFGUID type = SDL_SYS_HapticEffectType(base);
slouken@2713
  1133
    if (type == NULL) {
slouken@2713
  1134
        goto err_hweffect;
slouken@2713
  1135
    }
slouken@2713
  1136
slouken@2713
  1137
    /* Alloc the effect. */
slouken@2713
  1138
    effect->hweffect = (struct haptic_hweffect *)
slouken@2713
  1139
        SDL_malloc(sizeof(struct haptic_hweffect));
slouken@2713
  1140
    if (effect->hweffect == NULL) {
slouken@2713
  1141
        SDL_OutOfMemory();
slouken@2713
  1142
        goto err_hweffect;
slouken@2713
  1143
    }
slouken@2713
  1144
slouken@2713
  1145
    /* Get the effect. */
slouken@2713
  1146
    if (SDL_SYS_ToDIEFFECT(haptic, &effect->hweffect->effect, base) < 0) {
slouken@2713
  1147
        goto err_effectdone;
slouken@2713
  1148
    }
slouken@2713
  1149
slouken@2713
  1150
    /* Create the actual effect. */
icculus@6716
  1151
    ret = IDirectInputDevice8_CreateEffect(haptic->hwdata->device, type,
slouken@2713
  1152
                                           &effect->hweffect->effect,
slouken@2713
  1153
                                           &effect->hweffect->ref, NULL);
slouken@2713
  1154
    if (FAILED(ret)) {
slouken@2713
  1155
        DI_SetError("Unable to create effect", ret);
slouken@2713
  1156
        goto err_effectdone;
slouken@2713
  1157
    }
slouken@2713
  1158
slouken@2713
  1159
    return 0;
slouken@2713
  1160
slouken@2713
  1161
  err_effectdone:
slouken@2713
  1162
    SDL_SYS_HapticFreeDIEFFECT(&effect->hweffect->effect, base->type);
slouken@2713
  1163
  err_hweffect:
slouken@2713
  1164
    if (effect->hweffect != NULL) {
slouken@2713
  1165
        SDL_free(effect->hweffect);
slouken@2713
  1166
        effect->hweffect = NULL;
slouken@2713
  1167
    }
slouken@2713
  1168
    return -1;
slouken@2713
  1169
}
slouken@2713
  1170
slouken@2713
  1171
slouken@2713
  1172
/*
slouken@2713
  1173
 * Updates an effect.
slouken@2713
  1174
 */
slouken@2713
  1175
int
slouken@2713
  1176
SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
slouken@2713
  1177
                           struct haptic_effect *effect,
slouken@2713
  1178
                           SDL_HapticEffect * data)
slouken@2713
  1179
{
slouken@2713
  1180
    HRESULT ret;
slouken@2713
  1181
    DWORD flags;
slouken@2713
  1182
    DIEFFECT temp;
slouken@2713
  1183
slouken@2713
  1184
    /* Get the effect. */
slouken@2713
  1185
    SDL_memset(&temp, 0, sizeof(DIEFFECT));
slouken@2713
  1186
    if (SDL_SYS_ToDIEFFECT(haptic, &temp, data) < 0) {
slouken@2713
  1187
        goto err_update;
slouken@2713
  1188
    }
slouken@2713
  1189
slouken@2713
  1190
    /* Set the flags.  Might be worthwhile to diff temp with loaded effect and
slouken@2713
  1191
     *  only change those parameters. */
slouken@2713
  1192
    flags = DIEP_DIRECTION |
slouken@2713
  1193
        DIEP_DURATION |
slouken@2713
  1194
        DIEP_ENVELOPE |
slouken@2713
  1195
        DIEP_STARTDELAY |
slouken@2713
  1196
        DIEP_TRIGGERBUTTON |
slouken@2713
  1197
        DIEP_TRIGGERREPEATINTERVAL | DIEP_TYPESPECIFICPARAMS;
slouken@2713
  1198
slouken@2713
  1199
    /* Create the actual effect. */
slouken@2713
  1200
    ret =
slouken@2713
  1201
        IDirectInputEffect_SetParameters(effect->hweffect->ref, &temp, flags);
slouken@2713
  1202
    if (FAILED(ret)) {
slouken@2713
  1203
        DI_SetError("Unable to update effect", ret);
slouken@2713
  1204
        goto err_update;
slouken@2713
  1205
    }
slouken@2713
  1206
slouken@2713
  1207
    /* Copy it over. */
slouken@2713
  1208
    SDL_SYS_HapticFreeDIEFFECT(&effect->hweffect->effect, data->type);
slouken@2713
  1209
    SDL_memcpy(&effect->hweffect->effect, &temp, sizeof(DIEFFECT));
slouken@2713
  1210
slouken@2713
  1211
    return 0;
slouken@2713
  1212
slouken@2713
  1213
  err_update:
slouken@2713
  1214
    SDL_SYS_HapticFreeDIEFFECT(&temp, data->type);
slouken@2713
  1215
    return -1;
slouken@2713
  1216
}
slouken@2713
  1217
slouken@2713
  1218
slouken@2713
  1219
/*
slouken@2713
  1220
 * Runs an effect.
slouken@2713
  1221
 */
slouken@2713
  1222
int
slouken@2713
  1223
SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
slouken@2713
  1224
                        Uint32 iterations)
slouken@2713
  1225
{
slouken@2713
  1226
    HRESULT ret;
slouken@2713
  1227
    DWORD iter;
slouken@2713
  1228
slouken@2713
  1229
    /* Check if it's infinite. */
slouken@2713
  1230
    if (iterations == SDL_HAPTIC_INFINITY) {
slouken@2713
  1231
        iter = INFINITE;
slouken@2713
  1232
    } else
slouken@2713
  1233
        iter = iterations;
slouken@2713
  1234
slouken@2713
  1235
    /* Run the effect. */
slouken@2713
  1236
    ret = IDirectInputEffect_Start(effect->hweffect->ref, iter, 0);
slouken@2713
  1237
    if (FAILED(ret)) {
slouken@2713
  1238
        DI_SetError("Running the effect", ret);
slouken@2713
  1239
        return -1;
slouken@2713
  1240
    }
slouken@2713
  1241
slouken@2713
  1242
    return 0;
slouken@2713
  1243
}
slouken@2713
  1244
slouken@2713
  1245
slouken@2713
  1246
/*
slouken@2713
  1247
 * Stops an effect.
slouken@2713
  1248
 */
slouken@2713
  1249
int
slouken@2713
  1250
SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
slouken@2713
  1251
{
slouken@2713
  1252
    HRESULT ret;
slouken@2713
  1253
slouken@2713
  1254
    ret = IDirectInputEffect_Stop(effect->hweffect->ref);
slouken@2713
  1255
    if (FAILED(ret)) {
slouken@2713
  1256
        DI_SetError("Unable to stop effect", ret);
slouken@2713
  1257
        return -1;
slouken@2713
  1258
    }
slouken@2713
  1259
slouken@2713
  1260
    return 0;
slouken@2713
  1261
}
slouken@2713
  1262
slouken@2713
  1263
slouken@2713
  1264
/*
slouken@2713
  1265
 * Frees the effect.
slouken@2713
  1266
 */
slouken@2713
  1267
void
slouken@2713
  1268
SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
slouken@2713
  1269
{
slouken@2713
  1270
    HRESULT ret;
slouken@2713
  1271
slouken@2713
  1272
    ret = IDirectInputEffect_Unload(effect->hweffect->ref);
slouken@2713
  1273
    if (FAILED(ret)) {
slouken@2713
  1274
        DI_SetError("Removing effect from the device", ret);
slouken@2713
  1275
    }
slouken@2713
  1276
    SDL_SYS_HapticFreeDIEFFECT(&effect->hweffect->effect,
slouken@2713
  1277
                               effect->effect.type);
slouken@2713
  1278
    SDL_free(effect->hweffect);
slouken@2713
  1279
    effect->hweffect = NULL;
slouken@2713
  1280
}
slouken@2713
  1281
slouken@2713
  1282
slouken@2713
  1283
/*
slouken@2713
  1284
 * Gets the status of a haptic effect.
slouken@2713
  1285
 */
slouken@2713
  1286
int
slouken@2713
  1287
SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic,
slouken@2713
  1288
                              struct haptic_effect *effect)
slouken@2713
  1289
{
slouken@2713
  1290
    HRESULT ret;
slouken@2713
  1291
    DWORD status;
slouken@2713
  1292
slouken@2713
  1293
    ret = IDirectInputEffect_GetEffectStatus(effect->hweffect->ref, &status);
slouken@2713
  1294
    if (FAILED(ret)) {
slouken@2713
  1295
        DI_SetError("Getting effect status", ret);
slouken@2713
  1296
        return -1;
slouken@2713
  1297
    }
slouken@2713
  1298
slouken@2713
  1299
    if (status == 0)
slouken@2713
  1300
        return SDL_FALSE;
slouken@2713
  1301
    return SDL_TRUE;
slouken@2713
  1302
}
slouken@2713
  1303
slouken@2713
  1304
slouken@2713
  1305
/*
slouken@2713
  1306
 * Sets the gain.
slouken@2713
  1307
 */
slouken@2713
  1308
int
slouken@2713
  1309
SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
slouken@2713
  1310
{
slouken@2713
  1311
    HRESULT ret;
slouken@2713
  1312
    DIPROPDWORD dipdw;
slouken@2713
  1313
slouken@2713
  1314
    /* Create the weird structure thingy. */
slouken@2713
  1315
    dipdw.diph.dwSize = sizeof(DIPROPDWORD);
slouken@2713
  1316
    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
slouken@2713
  1317
    dipdw.diph.dwObj = 0;
slouken@2713
  1318
    dipdw.diph.dwHow = DIPH_DEVICE;
slouken@2713
  1319
    dipdw.dwData = gain * 100;  /* 0 to 10,000 */
slouken@2713
  1320
slouken@2713
  1321
    /* Try to set the autocenter. */
icculus@6716
  1322
    ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device,
slouken@2713
  1323
                                          DIPROP_FFGAIN, &dipdw.diph);
slouken@2713
  1324
    if (FAILED(ret)) {
slouken@2713
  1325
        DI_SetError("Setting gain", ret);
slouken@2713
  1326
        return -1;
slouken@2713
  1327
    }
slouken@2713
  1328
slouken@2713
  1329
    return 0;
slouken@2713
  1330
}
slouken@2713
  1331
slouken@2713
  1332
slouken@2713
  1333
/*
slouken@2713
  1334
 * Sets the autocentering.
slouken@2713
  1335
 */
slouken@2713
  1336
int
slouken@2713
  1337
SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
slouken@2713
  1338
{
slouken@2713
  1339
    HRESULT ret;
slouken@2713
  1340
    DIPROPDWORD dipdw;
slouken@2713
  1341
slouken@2713
  1342
    /* Create the weird structure thingy. */
slouken@2713
  1343
    dipdw.diph.dwSize = sizeof(DIPROPDWORD);
slouken@2713
  1344
    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
slouken@2713
  1345
    dipdw.diph.dwObj = 0;
slouken@2713
  1346
    dipdw.diph.dwHow = DIPH_DEVICE;
slouken@2713
  1347
    dipdw.dwData = (autocenter == 0) ? DIPROPAUTOCENTER_OFF :
slouken@2713
  1348
        DIPROPAUTOCENTER_ON;
slouken@2713
  1349
slouken@2713
  1350
    /* Try to set the autocenter. */
icculus@6716
  1351
    ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device,
slouken@2713
  1352
                                          DIPROP_AUTOCENTER, &dipdw.diph);
slouken@2713
  1353
    if (FAILED(ret)) {
slouken@2713
  1354
        DI_SetError("Setting autocenter", ret);
slouken@2713
  1355
        return -1;
slouken@2713
  1356
    }
slouken@2713
  1357
slouken@2713
  1358
    return 0;
slouken@2713
  1359
}
slouken@2713
  1360
slouken@2713
  1361
slouken@2713
  1362
/*
slouken@2713
  1363
 * Pauses the device.
slouken@2713
  1364
 */
slouken@2713
  1365
int
slouken@2713
  1366
SDL_SYS_HapticPause(SDL_Haptic * haptic)
slouken@2713
  1367
{
slouken@2713
  1368
    HRESULT ret;
slouken@2713
  1369
slouken@2713
  1370
    /* Pause the device. */
icculus@6716
  1371
    ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
slouken@2713
  1372
                                                       DISFFC_PAUSE);
slouken@2713
  1373
    if (FAILED(ret)) {
slouken@2713
  1374
        DI_SetError("Pausing the device", ret);
slouken@2713
  1375
        return -1;
slouken@2713
  1376
    }
slouken@2713
  1377
slouken@2713
  1378
    return 0;
slouken@2713
  1379
}
slouken@2713
  1380
slouken@2713
  1381
slouken@2713
  1382
/*
slouken@2713
  1383
 * Pauses the device.
slouken@2713
  1384
 */
slouken@2713
  1385
int
slouken@2713
  1386
SDL_SYS_HapticUnpause(SDL_Haptic * haptic)
slouken@2713
  1387
{
slouken@2713
  1388
    HRESULT ret;
slouken@2713
  1389
slouken@2713
  1390
    /* Unpause the device. */
icculus@6716
  1391
    ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
slouken@2713
  1392
                                                       DISFFC_CONTINUE);
slouken@2713
  1393
    if (FAILED(ret)) {
slouken@2713
  1394
        DI_SetError("Pausing the device", ret);
slouken@2713
  1395
        return -1;
slouken@2713
  1396
    }
slouken@2713
  1397
slouken@2713
  1398
    return 0;
slouken@2713
  1399
}
slouken@2713
  1400
slouken@2713
  1401
slouken@2713
  1402
/*
slouken@2713
  1403
 * Stops all the playing effects on the device.
slouken@2713
  1404
 */
slouken@2713
  1405
int
bobbens@2715
  1406
SDL_SYS_HapticStopAll(SDL_Haptic * haptic)
slouken@2713
  1407
{
slouken@2713
  1408
    HRESULT ret;
slouken@2713
  1409
slouken@2713
  1410
    /* Try to stop the effects. */
icculus@6716
  1411
    ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
slouken@2713
  1412
                                                       DISFFC_STOPALL);
slouken@2713
  1413
    if (FAILED(ret)) {
slouken@2713
  1414
        DI_SetError("Stopping the device", ret);
slouken@2713
  1415
        return -1;
slouken@2713
  1416
    }
slouken@2713
  1417
slouken@2713
  1418
    return 0;
slouken@2713
  1419
}
slouken@2713
  1420
slouken@2713
  1421
slouken@2713
  1422
#endif /* SDL_HAPTIC_DINPUT */