src/haptic/windows/SDL_dinputhaptic.c
changeset 8972 dfc759d7486f
child 8976 1a5d959d7b32
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/haptic/windows/SDL_dinputhaptic.c	Thu Jul 03 15:39:55 2014 -0700
     1.3 @@ -0,0 +1,1293 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +#include "../../SDL_internal.h"
    1.25 +
    1.26 +#include "SDL_error.h"
    1.27 +#include "SDL_haptic.h"
    1.28 +#include "SDL_timer.h"
    1.29 +#include "SDL_windowshaptic_c.h"
    1.30 +#include "SDL_dinputhaptic_c.h"
    1.31 +#include "../SDL_syshaptic.h"
    1.32 +#include "../../joystick/windows/SDL_windowsjoystick_c.h"
    1.33 +
    1.34 +
    1.35 +#if SDL_HAPTIC_DINPUT
    1.36 +
    1.37 +/*
    1.38 + * External stuff.
    1.39 + */
    1.40 +extern HWND SDL_HelperWindow;
    1.41 +
    1.42 +
    1.43 +/*
    1.44 + * Internal stuff.
    1.45 + */
    1.46 +static SDL_bool coinitialized = SDL_FALSE;
    1.47 +static LPDIRECTINPUT8 dinput = NULL;
    1.48 +
    1.49 +
    1.50 +/*
    1.51 + * Like SDL_SetError but for DX error codes.
    1.52 + */
    1.53 +static int
    1.54 +DI_SetError(const char *str, HRESULT err)
    1.55 +{
    1.56 +    /*
    1.57 +       SDL_SetError("Haptic: %s - %s: %s", str,
    1.58 +       DXGetErrorString8A(err), DXGetErrorDescription8A(err));
    1.59 +     */
    1.60 +    return SDL_SetError("Haptic error %s", str);
    1.61 +}
    1.62 +
    1.63 +/*
    1.64 + * Checks to see if two GUID are the same.
    1.65 + */
    1.66 +static int
    1.67 +DI_GUIDIsSame(const GUID * a, const GUID * b)
    1.68 +{
    1.69 +    return (SDL_memcmp(a, b, sizeof (GUID)) == 0);
    1.70 +}
    1.71 +
    1.72 +/*
    1.73 + * Callback to find the haptic devices.
    1.74 + */
    1.75 +static BOOL CALLBACK
    1.76 +EnumHapticsCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
    1.77 +{
    1.78 +    (void) pContext;
    1.79 +    SDL_DINPUT_MaybeAddDevice(pdidInstance);
    1.80 +    return DIENUM_CONTINUE;  /* continue enumerating */
    1.81 +}
    1.82 +
    1.83 +int
    1.84 +SDL_DINPUT_HapticInit(void)
    1.85 +{
    1.86 +    HRESULT ret;
    1.87 +    HINSTANCE instance;
    1.88 +
    1.89 +    if (dinput != NULL) {       /* Already open. */
    1.90 +        return SDL_SetError("Haptic: SubSystem already open.");
    1.91 +    }
    1.92 +
    1.93 +    ret = WIN_CoInitialize();
    1.94 +    if (FAILED(ret)) {
    1.95 +        return DI_SetError("Coinitialize", ret);
    1.96 +    }
    1.97 +
    1.98 +    coinitialized = SDL_TRUE;
    1.99 +
   1.100 +    ret = CoCreateInstance(&CLSID_DirectInput8, NULL, CLSCTX_INPROC_SERVER,
   1.101 +        &IID_IDirectInput8, (LPVOID)& dinput);
   1.102 +    if (FAILED(ret)) {
   1.103 +        SDL_SYS_HapticQuit();
   1.104 +        return DI_SetError("CoCreateInstance", ret);
   1.105 +    }
   1.106 +
   1.107 +    /* Because we used CoCreateInstance, we need to Initialize it, first. */
   1.108 +    instance = GetModuleHandle(NULL);
   1.109 +    if (instance == NULL) {
   1.110 +        SDL_SYS_HapticQuit();
   1.111 +        return SDL_SetError("GetModuleHandle() failed with error code %d.",
   1.112 +            GetLastError());
   1.113 +    }
   1.114 +    ret = IDirectInput8_Initialize(dinput, instance, DIRECTINPUT_VERSION);
   1.115 +    if (FAILED(ret)) {
   1.116 +        SDL_SYS_HapticQuit();
   1.117 +        return DI_SetError("Initializing DirectInput device", ret);
   1.118 +    }
   1.119 +
   1.120 +    /* Look for haptic devices. */
   1.121 +    ret = IDirectInput8_EnumDevices(dinput,
   1.122 +        0,
   1.123 +        EnumHapticsCallback,
   1.124 +        NULL,
   1.125 +        DIEDFL_FORCEFEEDBACK |
   1.126 +        DIEDFL_ATTACHEDONLY);
   1.127 +    if (FAILED(ret)) {
   1.128 +        SDL_SYS_HapticQuit();
   1.129 +        return DI_SetError("Enumerating DirectInput devices", ret);
   1.130 +    }
   1.131 +    return 0;
   1.132 +}
   1.133 +
   1.134 +int
   1.135 +SDL_DINPUT_MaybeAddDevice(const DIDEVICEINSTANCE * pdidInstance)
   1.136 +{
   1.137 +    HRESULT ret;
   1.138 +    LPDIRECTINPUTDEVICE8 device;
   1.139 +    const DWORD needflags = DIDC_ATTACHED | DIDC_FORCEFEEDBACK;
   1.140 +    DIDEVCAPS capabilities;
   1.141 +    SDL_hapticlist_item *item = NULL;
   1.142 +
   1.143 +    if (dinput == NULL) {
   1.144 +        return -1;  /* not initialized. We'll pick these up on enumeration if we init later. */
   1.145 +    }
   1.146 +
   1.147 +    /* Make sure we don't already have it */
   1.148 +    for (item = SDL_hapticlist; item; item = item->next) {
   1.149 +        if ((!item->bXInputHaptic) && (SDL_memcmp(&item->instance, pdidInstance, sizeof(*pdidInstance)) == 0)) {
   1.150 +            return -1;  /* Already added */
   1.151 +        }
   1.152 +    }
   1.153 +
   1.154 +    /* Open the device */
   1.155 +    ret = IDirectInput8_CreateDevice(dinput, &pdidInstance->guidInstance, &device, NULL);
   1.156 +    if (FAILED(ret)) {
   1.157 +        /* DI_SetError("Creating DirectInput device",ret); */
   1.158 +        return -1;
   1.159 +    }
   1.160 +
   1.161 +    /* Get capabilities. */
   1.162 +    SDL_zero(capabilities);
   1.163 +    capabilities.dwSize = sizeof(DIDEVCAPS);
   1.164 +    ret = IDirectInputDevice8_GetCapabilities(device, &capabilities);
   1.165 +    IDirectInputDevice8_Release(device);
   1.166 +    if (FAILED(ret)) {
   1.167 +        /* DI_SetError("Getting device capabilities",ret); */
   1.168 +        return -1;
   1.169 +    }
   1.170 +
   1.171 +    if ((capabilities.dwFlags & needflags) != needflags) {
   1.172 +        return -1;  /* not a device we can use. */
   1.173 +    }
   1.174 +
   1.175 +    item = (SDL_hapticlist_item *)SDL_calloc(1, sizeof(SDL_hapticlist_item));
   1.176 +    if (item == NULL) {
   1.177 +        return SDL_OutOfMemory();
   1.178 +    }
   1.179 +
   1.180 +    item->name = WIN_StringToUTF8(pdidInstance->tszProductName);
   1.181 +    if (!item->name) {
   1.182 +        SDL_free(item);
   1.183 +        return -1;
   1.184 +    }
   1.185 +
   1.186 +    /* Copy the instance over, useful for creating devices. */
   1.187 +    SDL_memcpy(&item->instance, pdidInstance, sizeof(DIDEVICEINSTANCE));
   1.188 +    SDL_memcpy(&item->capabilities, &capabilities, sizeof(capabilities));
   1.189 +
   1.190 +    return SDL_SYS_AddHapticDevice(item);
   1.191 +}
   1.192 +
   1.193 +int
   1.194 +SDL_DINPUT_MaybeRemoveDevice(const DIDEVICEINSTANCE * pdidInstance)
   1.195 +{
   1.196 +    SDL_hapticlist_item *item;
   1.197 +    SDL_hapticlist_item *prev = NULL;
   1.198 +
   1.199 +    if (dinput == NULL) {
   1.200 +        return -1;  /* not initialized, ignore this. */
   1.201 +    }
   1.202 +
   1.203 +    for (item = SDL_hapticlist; item != NULL; item = item->next) {
   1.204 +        if (!item->bXInputHaptic && SDL_memcmp(&item->instance, pdidInstance, sizeof(*pdidInstance)) == 0) {
   1.205 +            /* found it, remove it. */
   1.206 +            return SDL_SYS_RemoveHapticDevice(prev, item);
   1.207 +        }
   1.208 +        prev = item;
   1.209 +    }
   1.210 +    return -1;
   1.211 +}
   1.212 +
   1.213 +/*
   1.214 + * Callback to get supported axes.
   1.215 + */
   1.216 +static BOOL CALLBACK
   1.217 +DI_DeviceObjectCallback(LPCDIDEVICEOBJECTINSTANCE dev, LPVOID pvRef)
   1.218 +{
   1.219 +    SDL_Haptic *haptic = (SDL_Haptic *) pvRef;
   1.220 +
   1.221 +    if ((dev->dwType & DIDFT_AXIS) && (dev->dwFlags & DIDOI_FFACTUATOR)) {
   1.222 +        const GUID *guid = &dev->guidType;
   1.223 +        DWORD offset = 0;
   1.224 +        if (DI_GUIDIsSame(guid, &GUID_XAxis)) {
   1.225 +            offset = DIJOFS_X;
   1.226 +        } else if (DI_GUIDIsSame(guid, &GUID_YAxis)) {
   1.227 +            offset = DIJOFS_Y;
   1.228 +        } else if (DI_GUIDIsSame(guid, &GUID_ZAxis)) {
   1.229 +            offset = DIJOFS_Z;
   1.230 +        } else if (DI_GUIDIsSame(guid, &GUID_RxAxis)) {
   1.231 +            offset = DIJOFS_RX;
   1.232 +        } else if (DI_GUIDIsSame(guid, &GUID_RyAxis)) {
   1.233 +            offset = DIJOFS_RY;
   1.234 +        } else if (DI_GUIDIsSame(guid, &GUID_RzAxis)) {
   1.235 +            offset = DIJOFS_RZ;
   1.236 +        } else {
   1.237 +            return DIENUM_CONTINUE;   /* can't use this, go on. */
   1.238 +        }
   1.239 +
   1.240 +        haptic->hwdata->axes[haptic->naxes] = offset;
   1.241 +        haptic->naxes++;
   1.242 +
   1.243 +        /* Currently using the artificial limit of 3 axes. */
   1.244 +        if (haptic->naxes >= 3) {
   1.245 +            return DIENUM_STOP;
   1.246 +        }
   1.247 +    }
   1.248 +
   1.249 +    return DIENUM_CONTINUE;
   1.250 +}
   1.251 +
   1.252 +/*
   1.253 + * Callback to get all supported effects.
   1.254 + */
   1.255 +#define EFFECT_TEST(e,s)               \
   1.256 +if (DI_GUIDIsSame(&pei->guid, &(e)))   \
   1.257 +   haptic->supported |= (s)
   1.258 +static BOOL CALLBACK
   1.259 +DI_EffectCallback(LPCDIEFFECTINFO pei, LPVOID pv)
   1.260 +{
   1.261 +    /* Prepare the haptic device. */
   1.262 +    SDL_Haptic *haptic = (SDL_Haptic *) pv;
   1.263 +
   1.264 +    /* Get supported. */
   1.265 +    EFFECT_TEST(GUID_Spring, SDL_HAPTIC_SPRING);
   1.266 +    EFFECT_TEST(GUID_Damper, SDL_HAPTIC_DAMPER);
   1.267 +    EFFECT_TEST(GUID_Inertia, SDL_HAPTIC_INERTIA);
   1.268 +    EFFECT_TEST(GUID_Friction, SDL_HAPTIC_FRICTION);
   1.269 +    EFFECT_TEST(GUID_ConstantForce, SDL_HAPTIC_CONSTANT);
   1.270 +    EFFECT_TEST(GUID_CustomForce, SDL_HAPTIC_CUSTOM);
   1.271 +    EFFECT_TEST(GUID_Sine, SDL_HAPTIC_SINE);
   1.272 +    /* !!! FIXME: put this back when we have more bits in 2.1 */
   1.273 +    /* EFFECT_TEST(GUID_Square, SDL_HAPTIC_SQUARE); */
   1.274 +    EFFECT_TEST(GUID_Triangle, SDL_HAPTIC_TRIANGLE);
   1.275 +    EFFECT_TEST(GUID_SawtoothUp, SDL_HAPTIC_SAWTOOTHUP);
   1.276 +    EFFECT_TEST(GUID_SawtoothDown, SDL_HAPTIC_SAWTOOTHDOWN);
   1.277 +    EFFECT_TEST(GUID_RampForce, SDL_HAPTIC_RAMP);
   1.278 +
   1.279 +    /* Check for more. */
   1.280 +    return DIENUM_CONTINUE;
   1.281 +}
   1.282 +
   1.283 +/*
   1.284 + * Opens the haptic device.
   1.285 + *
   1.286 + *    Steps:
   1.287 + *       - Set cooperative level.
   1.288 + *       - Set data format.
   1.289 + *       - Acquire exclusiveness.
   1.290 + *       - Reset actuators.
   1.291 + *       - Get supported features.
   1.292 + */
   1.293 +static int
   1.294 +SDL_DINPUT_HapticOpenFromDevice(SDL_Haptic * haptic, LPDIRECTINPUTDEVICE8 device8, SDL_bool is_joystick)
   1.295 +{
   1.296 +    HRESULT ret;
   1.297 +    DIPROPDWORD dipdw;
   1.298 +
   1.299 +    /* Allocate the hwdata */
   1.300 +    haptic->hwdata = (struct haptic_hwdata *)SDL_malloc(sizeof(*haptic->hwdata));
   1.301 +    if (haptic->hwdata == NULL) {
   1.302 +        return SDL_OutOfMemory();
   1.303 +    }
   1.304 +    SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
   1.305 +
   1.306 +    /* We'll use the device8 from now on. */
   1.307 +    haptic->hwdata->device = device8;
   1.308 +    haptic->hwdata->is_joystick = is_joystick;
   1.309 +
   1.310 +    /* !!! FIXME: opening a haptic device here first will make an attempt to
   1.311 +       !!! FIXME:  SDL_JoystickOpen() that same device fail later, since we
   1.312 +       !!! FIXME:  have it open in exclusive mode. But this will allow
   1.313 +       !!! FIXME:  SDL_JoystickOpen() followed by SDL_HapticOpenFromJoystick()
   1.314 +       !!! FIXME:  to work, and that's probably the common case. Still,
   1.315 +       !!! FIXME:  ideally, We need to unify the opening code. */
   1.316 +
   1.317 +    if (!is_joystick) {  /* if is_joystick, we already set this up elsewhere. */
   1.318 +        /* Grab it exclusively to use force feedback stuff. */
   1.319 +        ret = IDirectInputDevice8_SetCooperativeLevel(haptic->hwdata->device,
   1.320 +                                                      SDL_HelperWindow,
   1.321 +                                                      DISCL_EXCLUSIVE |
   1.322 +                                                      DISCL_BACKGROUND);
   1.323 +        if (FAILED(ret)) {
   1.324 +            DI_SetError("Setting cooperative level to exclusive", ret);
   1.325 +            goto acquire_err;
   1.326 +        }
   1.327 +
   1.328 +        /* Set data format. */
   1.329 +        ret = IDirectInputDevice8_SetDataFormat(haptic->hwdata->device,
   1.330 +                                                &c_dfDIJoystick2);
   1.331 +        if (FAILED(ret)) {
   1.332 +            DI_SetError("Setting data format", ret);
   1.333 +            goto acquire_err;
   1.334 +        }
   1.335 +
   1.336 +        /* Get number of axes. */
   1.337 +        ret = IDirectInputDevice8_EnumObjects(haptic->hwdata->device,
   1.338 +                                              DI_DeviceObjectCallback,
   1.339 +                                              haptic, DIDFT_AXIS);
   1.340 +        if (FAILED(ret)) {
   1.341 +            DI_SetError("Getting device axes", ret);
   1.342 +            goto acquire_err;
   1.343 +        }
   1.344 +
   1.345 +        /* Acquire the device. */
   1.346 +        ret = IDirectInputDevice8_Acquire(haptic->hwdata->device);
   1.347 +        if (FAILED(ret)) {
   1.348 +            DI_SetError("Acquiring DirectInput device", ret);
   1.349 +            goto acquire_err;
   1.350 +        }
   1.351 +    }
   1.352 +
   1.353 +    /* Reset all actuators - just in case. */
   1.354 +    ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
   1.355 +                                                       DISFFC_RESET);
   1.356 +    if (FAILED(ret)) {
   1.357 +        DI_SetError("Resetting device", ret);
   1.358 +        goto acquire_err;
   1.359 +    }
   1.360 +
   1.361 +    /* Enabling actuators. */
   1.362 +    ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
   1.363 +                                                       DISFFC_SETACTUATORSON);
   1.364 +    if (FAILED(ret)) {
   1.365 +        DI_SetError("Enabling actuators", ret);
   1.366 +        goto acquire_err;
   1.367 +    }
   1.368 +
   1.369 +    /* Get supported effects. */
   1.370 +    ret = IDirectInputDevice8_EnumEffects(haptic->hwdata->device,
   1.371 +                                          DI_EffectCallback, haptic,
   1.372 +                                          DIEFT_ALL);
   1.373 +    if (FAILED(ret)) {
   1.374 +        DI_SetError("Enumerating supported effects", ret);
   1.375 +        goto acquire_err;
   1.376 +    }
   1.377 +    if (haptic->supported == 0) {       /* Error since device supports nothing. */
   1.378 +        SDL_SetError("Haptic: Internal error on finding supported effects.");
   1.379 +        goto acquire_err;
   1.380 +    }
   1.381 +
   1.382 +    /* Check autogain and autocenter. */
   1.383 +    dipdw.diph.dwSize = sizeof(DIPROPDWORD);
   1.384 +    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
   1.385 +    dipdw.diph.dwObj = 0;
   1.386 +    dipdw.diph.dwHow = DIPH_DEVICE;
   1.387 +    dipdw.dwData = 10000;
   1.388 +    ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device,
   1.389 +                                          DIPROP_FFGAIN, &dipdw.diph);
   1.390 +    if (!FAILED(ret)) {         /* Gain is supported. */
   1.391 +        haptic->supported |= SDL_HAPTIC_GAIN;
   1.392 +    }
   1.393 +    dipdw.diph.dwObj = 0;
   1.394 +    dipdw.diph.dwHow = DIPH_DEVICE;
   1.395 +    dipdw.dwData = DIPROPAUTOCENTER_OFF;
   1.396 +    ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device,
   1.397 +                                          DIPROP_AUTOCENTER, &dipdw.diph);
   1.398 +    if (!FAILED(ret)) {         /* Autocenter is supported. */
   1.399 +        haptic->supported |= SDL_HAPTIC_AUTOCENTER;
   1.400 +    }
   1.401 +
   1.402 +    /* Status is always supported. */
   1.403 +    haptic->supported |= SDL_HAPTIC_STATUS | SDL_HAPTIC_PAUSE;
   1.404 +
   1.405 +    /* Check maximum effects. */
   1.406 +    haptic->neffects = 128;     /* This is not actually supported as thus under windows,
   1.407 +                                   there is no way to tell the number of EFFECTS that a
   1.408 +                                   device can hold, so we'll just use a "random" number
   1.409 +                                   instead and put warnings in SDL_haptic.h */
   1.410 +    haptic->nplaying = 128;     /* Even more impossible to get this then neffects. */
   1.411 +
   1.412 +    /* Prepare effects memory. */
   1.413 +    haptic->effects = (struct haptic_effect *)
   1.414 +        SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
   1.415 +    if (haptic->effects == NULL) {
   1.416 +        SDL_OutOfMemory();
   1.417 +        goto acquire_err;
   1.418 +    }
   1.419 +    /* Clear the memory */
   1.420 +    SDL_memset(haptic->effects, 0,
   1.421 +               sizeof(struct haptic_effect) * haptic->neffects);
   1.422 +
   1.423 +    return 0;
   1.424 +
   1.425 +    /* Error handling */
   1.426 +  acquire_err:
   1.427 +    IDirectInputDevice8_Unacquire(haptic->hwdata->device);
   1.428 +    return -1;
   1.429 +}
   1.430 +
   1.431 +int
   1.432 +SDL_DINPUT_HapticOpen(SDL_Haptic * haptic, SDL_hapticlist_item *item)
   1.433 +{
   1.434 +    HRESULT ret;
   1.435 +    LPDIRECTINPUTDEVICE8 device;
   1.436 +    LPDIRECTINPUTDEVICE8 device8;
   1.437 +
   1.438 +    /* Open the device */
   1.439 +    ret = IDirectInput8_CreateDevice(dinput, &item->instance.guidInstance,
   1.440 +        &device, NULL);
   1.441 +    if (FAILED(ret)) {
   1.442 +        DI_SetError("Creating DirectInput device", ret);
   1.443 +        return -1;
   1.444 +    }
   1.445 +
   1.446 +    /* Now get the IDirectInputDevice8 interface, instead. */
   1.447 +    ret = IDirectInputDevice8_QueryInterface(device,
   1.448 +        &IID_IDirectInputDevice8,
   1.449 +        (LPVOID *)&device8);
   1.450 +    /* Done with the temporary one now. */
   1.451 +    IDirectInputDevice8_Release(device);
   1.452 +    if (FAILED(ret)) {
   1.453 +        DI_SetError("Querying DirectInput interface", ret);
   1.454 +        return -1;
   1.455 +    }
   1.456 +
   1.457 +    if (SDL_DINPUT_HapticOpenFromDevice(haptic, device8, SDL_FALSE) < 0) {
   1.458 +        IDirectInputDevice8_Release(device8);
   1.459 +        return -1;
   1.460 +    }
   1.461 +    return 0;
   1.462 +}
   1.463 +
   1.464 +int
   1.465 +SDL_DINPUT_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
   1.466 +{
   1.467 +    HRESULT ret;
   1.468 +    DIDEVICEINSTANCE hap_instance, joy_instance;
   1.469 +
   1.470 +    hap_instance.dwSize = sizeof(DIDEVICEINSTANCE);
   1.471 +    joy_instance.dwSize = sizeof(DIDEVICEINSTANCE);
   1.472 +
   1.473 +    /* Get the device instances. */
   1.474 +    ret = IDirectInputDevice8_GetDeviceInfo(haptic->hwdata->device,
   1.475 +        &hap_instance);
   1.476 +    if (FAILED(ret)) {
   1.477 +        return 0;
   1.478 +    }
   1.479 +    ret = IDirectInputDevice8_GetDeviceInfo(joystick->hwdata->InputDevice,
   1.480 +        &joy_instance);
   1.481 +    if (FAILED(ret)) {
   1.482 +        return 0;
   1.483 +    }
   1.484 +
   1.485 +    return DI_GUIDIsSame(&hap_instance.guidInstance, &joy_instance.guidInstance);
   1.486 +}
   1.487 +
   1.488 +int
   1.489 +SDL_DINPUT_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
   1.490 +{
   1.491 +    SDL_hapticlist_item *item;
   1.492 +    int index = 0;
   1.493 +    HRESULT ret;
   1.494 +    DIDEVICEINSTANCE joy_instance;
   1.495 +
   1.496 +    joy_instance.dwSize = sizeof(DIDEVICEINSTANCE);
   1.497 +    ret = IDirectInputDevice8_GetDeviceInfo(joystick->hwdata->InputDevice, &joy_instance);
   1.498 +    if (FAILED(ret)) {
   1.499 +        return -1;
   1.500 +    }
   1.501 +
   1.502 +    /* Since it comes from a joystick we have to try to match it with a haptic device on our haptic list. */
   1.503 +    for (item = SDL_hapticlist; item != NULL; item = item->next) {
   1.504 +        if (!item->bXInputHaptic && DI_GUIDIsSame(&item->instance.guidInstance, &joy_instance.guidInstance)) {
   1.505 +            haptic->index = index;
   1.506 +            return SDL_DINPUT_HapticOpenFromDevice(haptic, joystick->hwdata->InputDevice, SDL_TRUE);
   1.507 +        }
   1.508 +        ++index;
   1.509 +    }
   1.510 +
   1.511 +    SDL_SetError("Couldn't find joystick in haptic device list");
   1.512 +    return -1;
   1.513 +}
   1.514 +
   1.515 +void
   1.516 +SDL_DINPUT_HapticClose(SDL_Haptic * haptic)
   1.517 +{
   1.518 +    IDirectInputDevice8_Unacquire(haptic->hwdata->device);
   1.519 +
   1.520 +    /* Only release if isn't grabbed by a joystick. */
   1.521 +    if (haptic->hwdata->is_joystick == 0) {
   1.522 +        IDirectInputDevice8_Release(haptic->hwdata->device);
   1.523 +    }
   1.524 +}
   1.525 +
   1.526 +void
   1.527 +SDL_DINPUT_HapticQuit(void)
   1.528 +{
   1.529 +    if (dinput != NULL) {
   1.530 +        IDirectInput8_Release(dinput);
   1.531 +        dinput = NULL;
   1.532 +    }
   1.533 +
   1.534 +    if (coinitialized) {
   1.535 +        WIN_CoUninitialize();
   1.536 +        coinitialized = SDL_FALSE;
   1.537 +    }
   1.538 +}
   1.539 +
   1.540 +/*
   1.541 + * Converts an SDL trigger button to an DIEFFECT trigger button.
   1.542 + */
   1.543 +static DWORD
   1.544 +DIGetTriggerButton(Uint16 button)
   1.545 +{
   1.546 +    DWORD dwTriggerButton;
   1.547 +
   1.548 +    dwTriggerButton = DIEB_NOTRIGGER;
   1.549 +
   1.550 +    if (button != 0) {
   1.551 +        dwTriggerButton = DIJOFS_BUTTON(button - 1);
   1.552 +    }
   1.553 +
   1.554 +    return dwTriggerButton;
   1.555 +}
   1.556 +
   1.557 +
   1.558 +/*
   1.559 + * Sets the direction.
   1.560 + */
   1.561 +static int
   1.562 +SDL_SYS_SetDirection(DIEFFECT * effect, SDL_HapticDirection * dir, int naxes)
   1.563 +{
   1.564 +    LONG *rglDir;
   1.565 +
   1.566 +    /* Handle no axes a part. */
   1.567 +    if (naxes == 0) {
   1.568 +        effect->dwFlags |= DIEFF_SPHERICAL;     /* Set as default. */
   1.569 +        effect->rglDirection = NULL;
   1.570 +        return 0;
   1.571 +    }
   1.572 +
   1.573 +    /* Has axes. */
   1.574 +    rglDir = SDL_malloc(sizeof(LONG) * naxes);
   1.575 +    if (rglDir == NULL) {
   1.576 +        return SDL_OutOfMemory();
   1.577 +    }
   1.578 +    SDL_memset(rglDir, 0, sizeof(LONG) * naxes);
   1.579 +    effect->rglDirection = rglDir;
   1.580 +
   1.581 +    switch (dir->type) {
   1.582 +    case SDL_HAPTIC_POLAR:
   1.583 +        effect->dwFlags |= DIEFF_POLAR;
   1.584 +        rglDir[0] = dir->dir[0];
   1.585 +        return 0;
   1.586 +    case SDL_HAPTIC_CARTESIAN:
   1.587 +        effect->dwFlags |= DIEFF_CARTESIAN;
   1.588 +        rglDir[0] = dir->dir[0];
   1.589 +        if (naxes > 1)
   1.590 +            rglDir[1] = dir->dir[1];
   1.591 +        if (naxes > 2)
   1.592 +            rglDir[2] = dir->dir[2];
   1.593 +        return 0;
   1.594 +    case SDL_HAPTIC_SPHERICAL:
   1.595 +        effect->dwFlags |= DIEFF_SPHERICAL;
   1.596 +        rglDir[0] = dir->dir[0];
   1.597 +        if (naxes > 1)
   1.598 +            rglDir[1] = dir->dir[1];
   1.599 +        if (naxes > 2)
   1.600 +            rglDir[2] = dir->dir[2];
   1.601 +        return 0;
   1.602 +
   1.603 +    default:
   1.604 +        return SDL_SetError("Haptic: Unknown direction type.");
   1.605 +    }
   1.606 +}
   1.607 +
   1.608 +#define CONVERT(x)   (((x) > 0x7FFF) ? 10000 : ((x)*10000) / 0x7FFF)
   1.609 +/*
   1.610 + * Creates the DIEFFECT from a SDL_HapticEffect.
   1.611 + */
   1.612 +static int
   1.613 +SDL_SYS_ToDIEFFECT(SDL_Haptic * haptic, DIEFFECT * dest,
   1.614 +                   SDL_HapticEffect * src)
   1.615 +{
   1.616 +    int i;
   1.617 +    DICONSTANTFORCE *constant;
   1.618 +    DIPERIODIC *periodic;
   1.619 +    DICONDITION *condition;     /* Actually an array of conditions - one per axis. */
   1.620 +    DIRAMPFORCE *ramp;
   1.621 +    DICUSTOMFORCE *custom;
   1.622 +    DIENVELOPE *envelope;
   1.623 +    SDL_HapticConstant *hap_constant;
   1.624 +    SDL_HapticPeriodic *hap_periodic;
   1.625 +    SDL_HapticCondition *hap_condition;
   1.626 +    SDL_HapticRamp *hap_ramp;
   1.627 +    SDL_HapticCustom *hap_custom;
   1.628 +    DWORD *axes;
   1.629 +
   1.630 +    /* Set global stuff. */
   1.631 +    SDL_memset(dest, 0, sizeof(DIEFFECT));
   1.632 +    dest->dwSize = sizeof(DIEFFECT);    /* Set the structure size. */
   1.633 +    dest->dwSamplePeriod = 0;   /* Not used by us. */
   1.634 +    dest->dwGain = 10000;       /* Gain is set globally, not locally. */
   1.635 +    dest->dwFlags = DIEFF_OBJECTOFFSETS;        /* Seems obligatory. */
   1.636 +
   1.637 +    /* Envelope. */
   1.638 +    envelope = SDL_malloc(sizeof(DIENVELOPE));
   1.639 +    if (envelope == NULL) {
   1.640 +        return SDL_OutOfMemory();
   1.641 +    }
   1.642 +    SDL_memset(envelope, 0, sizeof(DIENVELOPE));
   1.643 +    dest->lpEnvelope = envelope;
   1.644 +    envelope->dwSize = sizeof(DIENVELOPE);      /* Always should be this. */
   1.645 +
   1.646 +    /* Axes. */
   1.647 +    dest->cAxes = haptic->naxes;
   1.648 +    if (dest->cAxes > 0) {
   1.649 +        axes = SDL_malloc(sizeof(DWORD) * dest->cAxes);
   1.650 +        if (axes == NULL) {
   1.651 +            return SDL_OutOfMemory();
   1.652 +        }
   1.653 +        axes[0] = haptic->hwdata->axes[0];      /* Always at least one axis. */
   1.654 +        if (dest->cAxes > 1) {
   1.655 +            axes[1] = haptic->hwdata->axes[1];
   1.656 +        }
   1.657 +        if (dest->cAxes > 2) {
   1.658 +            axes[2] = haptic->hwdata->axes[2];
   1.659 +        }
   1.660 +        dest->rgdwAxes = axes;
   1.661 +    }
   1.662 +
   1.663 +    /* The big type handling switch, even bigger than Linux's version. */
   1.664 +    switch (src->type) {
   1.665 +    case SDL_HAPTIC_CONSTANT:
   1.666 +        hap_constant = &src->constant;
   1.667 +        constant = SDL_malloc(sizeof(DICONSTANTFORCE));
   1.668 +        if (constant == NULL) {
   1.669 +            return SDL_OutOfMemory();
   1.670 +        }
   1.671 +        SDL_memset(constant, 0, sizeof(DICONSTANTFORCE));
   1.672 +
   1.673 +        /* Specifics */
   1.674 +        constant->lMagnitude = CONVERT(hap_constant->level);
   1.675 +        dest->cbTypeSpecificParams = sizeof(DICONSTANTFORCE);
   1.676 +        dest->lpvTypeSpecificParams = constant;
   1.677 +
   1.678 +        /* Generics */
   1.679 +        dest->dwDuration = hap_constant->length * 1000; /* In microseconds. */
   1.680 +        dest->dwTriggerButton = DIGetTriggerButton(hap_constant->button);
   1.681 +        dest->dwTriggerRepeatInterval = hap_constant->interval;
   1.682 +        dest->dwStartDelay = hap_constant->delay * 1000;        /* In microseconds. */
   1.683 +
   1.684 +        /* Direction. */
   1.685 +        if (SDL_SYS_SetDirection(dest, &hap_constant->direction, dest->cAxes) < 0) {
   1.686 +            return -1;
   1.687 +        }
   1.688 +
   1.689 +        /* Envelope */
   1.690 +        if ((hap_constant->attack_length == 0)
   1.691 +            && (hap_constant->fade_length == 0)) {
   1.692 +            SDL_free(dest->lpEnvelope);
   1.693 +            dest->lpEnvelope = NULL;
   1.694 +        } else {
   1.695 +            envelope->dwAttackLevel = CONVERT(hap_constant->attack_level);
   1.696 +            envelope->dwAttackTime = hap_constant->attack_length * 1000;
   1.697 +            envelope->dwFadeLevel = CONVERT(hap_constant->fade_level);
   1.698 +            envelope->dwFadeTime = hap_constant->fade_length * 1000;
   1.699 +        }
   1.700 +
   1.701 +        break;
   1.702 +
   1.703 +    case SDL_HAPTIC_SINE:
   1.704 +    /* !!! FIXME: put this back when we have more bits in 2.1 */
   1.705 +    /* case SDL_HAPTIC_SQUARE: */
   1.706 +    case SDL_HAPTIC_TRIANGLE:
   1.707 +    case SDL_HAPTIC_SAWTOOTHUP:
   1.708 +    case SDL_HAPTIC_SAWTOOTHDOWN:
   1.709 +        hap_periodic = &src->periodic;
   1.710 +        periodic = SDL_malloc(sizeof(DIPERIODIC));
   1.711 +        if (periodic == NULL) {
   1.712 +            return SDL_OutOfMemory();
   1.713 +        }
   1.714 +        SDL_memset(periodic, 0, sizeof(DIPERIODIC));
   1.715 +
   1.716 +        /* Specifics */
   1.717 +        periodic->dwMagnitude = CONVERT(hap_periodic->magnitude);
   1.718 +        periodic->lOffset = CONVERT(hap_periodic->offset);
   1.719 +        periodic->dwPhase = hap_periodic->phase;
   1.720 +        periodic->dwPeriod = hap_periodic->period * 1000;
   1.721 +        dest->cbTypeSpecificParams = sizeof(DIPERIODIC);
   1.722 +        dest->lpvTypeSpecificParams = periodic;
   1.723 +
   1.724 +        /* Generics */
   1.725 +        dest->dwDuration = hap_periodic->length * 1000; /* In microseconds. */
   1.726 +        dest->dwTriggerButton = DIGetTriggerButton(hap_periodic->button);
   1.727 +        dest->dwTriggerRepeatInterval = hap_periodic->interval;
   1.728 +        dest->dwStartDelay = hap_periodic->delay * 1000;        /* In microseconds. */
   1.729 +
   1.730 +        /* Direction. */
   1.731 +        if (SDL_SYS_SetDirection(dest, &hap_periodic->direction, dest->cAxes)
   1.732 +            < 0) {
   1.733 +            return -1;
   1.734 +        }
   1.735 +
   1.736 +        /* Envelope */
   1.737 +        if ((hap_periodic->attack_length == 0)
   1.738 +            && (hap_periodic->fade_length == 0)) {
   1.739 +            SDL_free(dest->lpEnvelope);
   1.740 +            dest->lpEnvelope = NULL;
   1.741 +        } else {
   1.742 +            envelope->dwAttackLevel = CONVERT(hap_periodic->attack_level);
   1.743 +            envelope->dwAttackTime = hap_periodic->attack_length * 1000;
   1.744 +            envelope->dwFadeLevel = CONVERT(hap_periodic->fade_level);
   1.745 +            envelope->dwFadeTime = hap_periodic->fade_length * 1000;
   1.746 +        }
   1.747 +
   1.748 +        break;
   1.749 +
   1.750 +    case SDL_HAPTIC_SPRING:
   1.751 +    case SDL_HAPTIC_DAMPER:
   1.752 +    case SDL_HAPTIC_INERTIA:
   1.753 +    case SDL_HAPTIC_FRICTION:
   1.754 +        hap_condition = &src->condition;
   1.755 +        condition = SDL_malloc(sizeof(DICONDITION) * dest->cAxes);
   1.756 +        if (condition == NULL) {
   1.757 +            return SDL_OutOfMemory();
   1.758 +        }
   1.759 +        SDL_memset(condition, 0, sizeof(DICONDITION));
   1.760 +
   1.761 +        /* Specifics */
   1.762 +        for (i = 0; i < (int) dest->cAxes; i++) {
   1.763 +            condition[i].lOffset = CONVERT(hap_condition->center[i]);
   1.764 +            condition[i].lPositiveCoefficient =
   1.765 +                CONVERT(hap_condition->right_coeff[i]);
   1.766 +            condition[i].lNegativeCoefficient =
   1.767 +                CONVERT(hap_condition->left_coeff[i]);
   1.768 +            condition[i].dwPositiveSaturation =
   1.769 +                CONVERT(hap_condition->right_sat[i]);
   1.770 +            condition[i].dwNegativeSaturation =
   1.771 +                CONVERT(hap_condition->left_sat[i]);
   1.772 +            condition[i].lDeadBand = CONVERT(hap_condition->deadband[i]);
   1.773 +        }
   1.774 +        dest->cbTypeSpecificParams = sizeof(DICONDITION) * dest->cAxes;
   1.775 +        dest->lpvTypeSpecificParams = condition;
   1.776 +
   1.777 +        /* Generics */
   1.778 +        dest->dwDuration = hap_condition->length * 1000;        /* In microseconds. */
   1.779 +        dest->dwTriggerButton = DIGetTriggerButton(hap_condition->button);
   1.780 +        dest->dwTriggerRepeatInterval = hap_condition->interval;
   1.781 +        dest->dwStartDelay = hap_condition->delay * 1000;       /* In microseconds. */
   1.782 +
   1.783 +        /* Direction. */
   1.784 +        if (SDL_SYS_SetDirection(dest, &hap_condition->direction, dest->cAxes)
   1.785 +            < 0) {
   1.786 +            return -1;
   1.787 +        }
   1.788 +
   1.789 +        /* Envelope - Not actually supported by most CONDITION implementations. */
   1.790 +        SDL_free(dest->lpEnvelope);
   1.791 +        dest->lpEnvelope = NULL;
   1.792 +
   1.793 +        break;
   1.794 +
   1.795 +    case SDL_HAPTIC_RAMP:
   1.796 +        hap_ramp = &src->ramp;
   1.797 +        ramp = SDL_malloc(sizeof(DIRAMPFORCE));
   1.798 +        if (ramp == NULL) {
   1.799 +            return SDL_OutOfMemory();
   1.800 +        }
   1.801 +        SDL_memset(ramp, 0, sizeof(DIRAMPFORCE));
   1.802 +
   1.803 +        /* Specifics */
   1.804 +        ramp->lStart = CONVERT(hap_ramp->start);
   1.805 +        ramp->lEnd = CONVERT(hap_ramp->end);
   1.806 +        dest->cbTypeSpecificParams = sizeof(DIRAMPFORCE);
   1.807 +        dest->lpvTypeSpecificParams = ramp;
   1.808 +
   1.809 +        /* Generics */
   1.810 +        dest->dwDuration = hap_ramp->length * 1000;     /* In microseconds. */
   1.811 +        dest->dwTriggerButton = DIGetTriggerButton(hap_ramp->button);
   1.812 +        dest->dwTriggerRepeatInterval = hap_ramp->interval;
   1.813 +        dest->dwStartDelay = hap_ramp->delay * 1000;    /* In microseconds. */
   1.814 +
   1.815 +        /* Direction. */
   1.816 +        if (SDL_SYS_SetDirection(dest, &hap_ramp->direction, dest->cAxes) < 0) {
   1.817 +            return -1;
   1.818 +        }
   1.819 +
   1.820 +        /* Envelope */
   1.821 +        if ((hap_ramp->attack_length == 0) && (hap_ramp->fade_length == 0)) {
   1.822 +            SDL_free(dest->lpEnvelope);
   1.823 +            dest->lpEnvelope = NULL;
   1.824 +        } else {
   1.825 +            envelope->dwAttackLevel = CONVERT(hap_ramp->attack_level);
   1.826 +            envelope->dwAttackTime = hap_ramp->attack_length * 1000;
   1.827 +            envelope->dwFadeLevel = CONVERT(hap_ramp->fade_level);
   1.828 +            envelope->dwFadeTime = hap_ramp->fade_length * 1000;
   1.829 +        }
   1.830 +
   1.831 +        break;
   1.832 +
   1.833 +    case SDL_HAPTIC_CUSTOM:
   1.834 +        hap_custom = &src->custom;
   1.835 +        custom = SDL_malloc(sizeof(DICUSTOMFORCE));
   1.836 +        if (custom == NULL) {
   1.837 +            return SDL_OutOfMemory();
   1.838 +        }
   1.839 +        SDL_memset(custom, 0, sizeof(DICUSTOMFORCE));
   1.840 +
   1.841 +        /* Specifics */
   1.842 +        custom->cChannels = hap_custom->channels;
   1.843 +        custom->dwSamplePeriod = hap_custom->period * 1000;
   1.844 +        custom->cSamples = hap_custom->samples;
   1.845 +        custom->rglForceData =
   1.846 +            SDL_malloc(sizeof(LONG) * custom->cSamples * custom->cChannels);
   1.847 +        for (i = 0; i < hap_custom->samples * hap_custom->channels; i++) {      /* Copy data. */
   1.848 +            custom->rglForceData[i] = CONVERT(hap_custom->data[i]);
   1.849 +        }
   1.850 +        dest->cbTypeSpecificParams = sizeof(DICUSTOMFORCE);
   1.851 +        dest->lpvTypeSpecificParams = custom;
   1.852 +
   1.853 +        /* Generics */
   1.854 +        dest->dwDuration = hap_custom->length * 1000;   /* In microseconds. */
   1.855 +        dest->dwTriggerButton = DIGetTriggerButton(hap_custom->button);
   1.856 +        dest->dwTriggerRepeatInterval = hap_custom->interval;
   1.857 +        dest->dwStartDelay = hap_custom->delay * 1000;  /* In microseconds. */
   1.858 +
   1.859 +        /* Direction. */
   1.860 +        if (SDL_SYS_SetDirection(dest, &hap_custom->direction, dest->cAxes) < 0) {
   1.861 +            return -1;
   1.862 +        }
   1.863 +
   1.864 +        /* Envelope */
   1.865 +        if ((hap_custom->attack_length == 0)
   1.866 +            && (hap_custom->fade_length == 0)) {
   1.867 +            SDL_free(dest->lpEnvelope);
   1.868 +            dest->lpEnvelope = NULL;
   1.869 +        } else {
   1.870 +            envelope->dwAttackLevel = CONVERT(hap_custom->attack_level);
   1.871 +            envelope->dwAttackTime = hap_custom->attack_length * 1000;
   1.872 +            envelope->dwFadeLevel = CONVERT(hap_custom->fade_level);
   1.873 +            envelope->dwFadeTime = hap_custom->fade_length * 1000;
   1.874 +        }
   1.875 +
   1.876 +        break;
   1.877 +
   1.878 +    default:
   1.879 +        return SDL_SetError("Haptic: Unknown effect type.");
   1.880 +    }
   1.881 +
   1.882 +    return 0;
   1.883 +}
   1.884 +
   1.885 +
   1.886 +/*
   1.887 + * Frees an DIEFFECT allocated by SDL_SYS_ToDIEFFECT.
   1.888 + */
   1.889 +static void
   1.890 +SDL_SYS_HapticFreeDIEFFECT(DIEFFECT * effect, int type)
   1.891 +{
   1.892 +    DICUSTOMFORCE *custom;
   1.893 +
   1.894 +    SDL_free(effect->lpEnvelope);
   1.895 +    effect->lpEnvelope = NULL;
   1.896 +    SDL_free(effect->rgdwAxes);
   1.897 +    effect->rgdwAxes = NULL;
   1.898 +    if (effect->lpvTypeSpecificParams != NULL) {
   1.899 +        if (type == SDL_HAPTIC_CUSTOM) {        /* Must free the custom data. */
   1.900 +            custom = (DICUSTOMFORCE *) effect->lpvTypeSpecificParams;
   1.901 +            SDL_free(custom->rglForceData);
   1.902 +            custom->rglForceData = NULL;
   1.903 +        }
   1.904 +        SDL_free(effect->lpvTypeSpecificParams);
   1.905 +        effect->lpvTypeSpecificParams = NULL;
   1.906 +    }
   1.907 +    SDL_free(effect->rglDirection);
   1.908 +    effect->rglDirection = NULL;
   1.909 +}
   1.910 +
   1.911 +/*
   1.912 + * Gets the effect type from the generic SDL haptic effect wrapper.
   1.913 + */
   1.914 +static REFGUID
   1.915 +SDL_SYS_HapticEffectType(SDL_HapticEffect * effect)
   1.916 +{
   1.917 +    switch (effect->type) {
   1.918 +    case SDL_HAPTIC_CONSTANT:
   1.919 +        return &GUID_ConstantForce;
   1.920 +
   1.921 +    case SDL_HAPTIC_RAMP:
   1.922 +        return &GUID_RampForce;
   1.923 +
   1.924 +    /* !!! FIXME: put this back when we have more bits in 2.1 */
   1.925 +    /* case SDL_HAPTIC_SQUARE:
   1.926 +        return &GUID_Square; */
   1.927 +
   1.928 +    case SDL_HAPTIC_SINE:
   1.929 +        return &GUID_Sine;
   1.930 +
   1.931 +    case SDL_HAPTIC_TRIANGLE:
   1.932 +        return &GUID_Triangle;
   1.933 +
   1.934 +    case SDL_HAPTIC_SAWTOOTHUP:
   1.935 +        return &GUID_SawtoothUp;
   1.936 +
   1.937 +    case SDL_HAPTIC_SAWTOOTHDOWN:
   1.938 +        return &GUID_SawtoothDown;
   1.939 +
   1.940 +    case SDL_HAPTIC_SPRING:
   1.941 +        return &GUID_Spring;
   1.942 +
   1.943 +    case SDL_HAPTIC_DAMPER:
   1.944 +        return &GUID_Damper;
   1.945 +
   1.946 +    case SDL_HAPTIC_INERTIA:
   1.947 +        return &GUID_Inertia;
   1.948 +
   1.949 +    case SDL_HAPTIC_FRICTION:
   1.950 +        return &GUID_Friction;
   1.951 +
   1.952 +    case SDL_HAPTIC_CUSTOM:
   1.953 +        return &GUID_CustomForce;
   1.954 +
   1.955 +    default:
   1.956 +        return NULL;
   1.957 +    }
   1.958 +}
   1.959 +int
   1.960 +SDL_DINPUT_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * base)
   1.961 +{
   1.962 +    HRESULT ret;
   1.963 +    REFGUID type = SDL_SYS_HapticEffectType(base);
   1.964 +
   1.965 +    if (type == NULL) {
   1.966 +        SDL_SetError("Haptic: Unknown effect type.");
   1.967 +        return -1;
   1.968 +    }
   1.969 +
   1.970 +    /* Get the effect. */
   1.971 +    if (SDL_SYS_ToDIEFFECT(haptic, &effect->hweffect->effect, base) < 0) {
   1.972 +        goto err_effectdone;
   1.973 +    }
   1.974 +
   1.975 +    /* Create the actual effect. */
   1.976 +    ret = IDirectInputDevice8_CreateEffect(haptic->hwdata->device, type,
   1.977 +        &effect->hweffect->effect,
   1.978 +        &effect->hweffect->ref, NULL);
   1.979 +    if (FAILED(ret)) {
   1.980 +        DI_SetError("Unable to create effect", ret);
   1.981 +        goto err_effectdone;
   1.982 +    }
   1.983 +
   1.984 +    return 0;
   1.985 +
   1.986 +err_effectdone:
   1.987 +    SDL_SYS_HapticFreeDIEFFECT(&effect->hweffect->effect, base->type);
   1.988 +    return -1;
   1.989 +}
   1.990 +
   1.991 +int
   1.992 +SDL_DINPUT_HapticUpdateEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * data)
   1.993 +{
   1.994 +    HRESULT ret;
   1.995 +    DWORD flags;
   1.996 +    DIEFFECT temp;
   1.997 +
   1.998 +    /* Get the effect. */
   1.999 +    SDL_memset(&temp, 0, sizeof(DIEFFECT));
  1.1000 +    if (SDL_SYS_ToDIEFFECT(haptic, &temp, data) < 0) {
  1.1001 +        goto err_update;
  1.1002 +    }
  1.1003 +
  1.1004 +    /* Set the flags.  Might be worthwhile to diff temp with loaded effect and
  1.1005 +    *  only change those parameters. */
  1.1006 +    flags = DIEP_DIRECTION |
  1.1007 +        DIEP_DURATION |
  1.1008 +        DIEP_ENVELOPE |
  1.1009 +        DIEP_STARTDELAY |
  1.1010 +        DIEP_TRIGGERBUTTON |
  1.1011 +        DIEP_TRIGGERREPEATINTERVAL | DIEP_TYPESPECIFICPARAMS;
  1.1012 +
  1.1013 +    /* Create the actual effect. */
  1.1014 +    ret =
  1.1015 +        IDirectInputEffect_SetParameters(effect->hweffect->ref, &temp, flags);
  1.1016 +    if (FAILED(ret)) {
  1.1017 +        DI_SetError("Unable to update effect", ret);
  1.1018 +        goto err_update;
  1.1019 +    }
  1.1020 +
  1.1021 +    /* Copy it over. */
  1.1022 +    SDL_SYS_HapticFreeDIEFFECT(&effect->hweffect->effect, data->type);
  1.1023 +    SDL_memcpy(&effect->hweffect->effect, &temp, sizeof(DIEFFECT));
  1.1024 +
  1.1025 +    return 0;
  1.1026 +
  1.1027 +err_update:
  1.1028 +    SDL_SYS_HapticFreeDIEFFECT(&temp, data->type);
  1.1029 +    return -1;
  1.1030 +}
  1.1031 +
  1.1032 +int
  1.1033 +SDL_DINPUT_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect, Uint32 iterations)
  1.1034 +{
  1.1035 +    HRESULT ret;
  1.1036 +    DWORD iter;
  1.1037 +
  1.1038 +    /* Check if it's infinite. */
  1.1039 +    if (iterations == SDL_HAPTIC_INFINITY) {
  1.1040 +        iter = INFINITE;
  1.1041 +    } else {
  1.1042 +        iter = iterations;
  1.1043 +    }
  1.1044 +
  1.1045 +    /* Run the effect. */
  1.1046 +    ret = IDirectInputEffect_Start(effect->hweffect->ref, iter, 0);
  1.1047 +    if (FAILED(ret)) {
  1.1048 +        return DI_SetError("Running the effect", ret);
  1.1049 +    }
  1.1050 +    return 0;
  1.1051 +}
  1.1052 +
  1.1053 +int
  1.1054 +SDL_DINPUT_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
  1.1055 +{
  1.1056 +    HRESULT ret;
  1.1057 +
  1.1058 +    ret = IDirectInputEffect_Stop(effect->hweffect->ref);
  1.1059 +    if (FAILED(ret)) {
  1.1060 +        return DI_SetError("Unable to stop effect", ret);
  1.1061 +    }
  1.1062 +    return 0;
  1.1063 +}
  1.1064 +
  1.1065 +void
  1.1066 +SDL_DINPUT_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
  1.1067 +{
  1.1068 +    HRESULT ret;
  1.1069 +
  1.1070 +    ret = IDirectInputEffect_Unload(effect->hweffect->ref);
  1.1071 +    if (FAILED(ret)) {
  1.1072 +        DI_SetError("Removing effect from the device", ret);
  1.1073 +    }
  1.1074 +    SDL_SYS_HapticFreeDIEFFECT(&effect->hweffect->effect, effect->effect.type);
  1.1075 +}
  1.1076 +
  1.1077 +int
  1.1078 +SDL_DINPUT_HapticGetEffectStatus(SDL_Haptic * haptic, struct haptic_effect *effect)
  1.1079 +{
  1.1080 +    HRESULT ret;
  1.1081 +    DWORD status;
  1.1082 +
  1.1083 +    ret = IDirectInputEffect_GetEffectStatus(effect->hweffect->ref, &status);
  1.1084 +    if (FAILED(ret)) {
  1.1085 +        return DI_SetError("Getting effect status", ret);
  1.1086 +    }
  1.1087 +
  1.1088 +    if (status == 0)
  1.1089 +        return SDL_FALSE;
  1.1090 +    return SDL_TRUE;
  1.1091 +}
  1.1092 +
  1.1093 +int
  1.1094 +SDL_DINPUT_HapticSetGain(SDL_Haptic * haptic, int gain)
  1.1095 +{
  1.1096 +    HRESULT ret;
  1.1097 +    DIPROPDWORD dipdw;
  1.1098 +
  1.1099 +    /* Create the weird structure thingy. */
  1.1100 +    dipdw.diph.dwSize = sizeof(DIPROPDWORD);
  1.1101 +    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  1.1102 +    dipdw.diph.dwObj = 0;
  1.1103 +    dipdw.diph.dwHow = DIPH_DEVICE;
  1.1104 +    dipdw.dwData = gain * 100;  /* 0 to 10,000 */
  1.1105 +
  1.1106 +    /* Try to set the autocenter. */
  1.1107 +    ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device,
  1.1108 +        DIPROP_FFGAIN, &dipdw.diph);
  1.1109 +    if (FAILED(ret)) {
  1.1110 +        return DI_SetError("Setting gain", ret);
  1.1111 +    }
  1.1112 +    return 0;
  1.1113 +}
  1.1114 +
  1.1115 +int
  1.1116 +SDL_DINPUT_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
  1.1117 +{
  1.1118 +    HRESULT ret;
  1.1119 +    DIPROPDWORD dipdw;
  1.1120 +
  1.1121 +    /* Create the weird structure thingy. */
  1.1122 +    dipdw.diph.dwSize = sizeof(DIPROPDWORD);
  1.1123 +    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  1.1124 +    dipdw.diph.dwObj = 0;
  1.1125 +    dipdw.diph.dwHow = DIPH_DEVICE;
  1.1126 +    dipdw.dwData = (autocenter == 0) ? DIPROPAUTOCENTER_OFF :
  1.1127 +        DIPROPAUTOCENTER_ON;
  1.1128 +
  1.1129 +    /* Try to set the autocenter. */
  1.1130 +    ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device,
  1.1131 +        DIPROP_AUTOCENTER, &dipdw.diph);
  1.1132 +    if (FAILED(ret)) {
  1.1133 +        return DI_SetError("Setting autocenter", ret);
  1.1134 +    }
  1.1135 +    return 0;
  1.1136 +}
  1.1137 +
  1.1138 +int
  1.1139 +SDL_DINPUT_HapticPause(SDL_Haptic * haptic)
  1.1140 +{
  1.1141 +    HRESULT ret;
  1.1142 +
  1.1143 +    /* Pause the device. */
  1.1144 +    ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
  1.1145 +        DISFFC_PAUSE);
  1.1146 +    if (FAILED(ret)) {
  1.1147 +        return DI_SetError("Pausing the device", ret);
  1.1148 +    }
  1.1149 +    return 0;
  1.1150 +}
  1.1151 +
  1.1152 +int
  1.1153 +SDL_DINPUT_HapticUnpause(SDL_Haptic * haptic)
  1.1154 +{
  1.1155 +    HRESULT ret;
  1.1156 +
  1.1157 +    /* Unpause the device. */
  1.1158 +    ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
  1.1159 +        DISFFC_CONTINUE);
  1.1160 +    if (FAILED(ret)) {
  1.1161 +        return DI_SetError("Pausing the device", ret);
  1.1162 +    }
  1.1163 +    return 0;
  1.1164 +}
  1.1165 +
  1.1166 +int
  1.1167 +SDL_DINPUT_HapticStopAll(SDL_Haptic * haptic)
  1.1168 +{
  1.1169 +    HRESULT ret;
  1.1170 +
  1.1171 +    /* Try to stop the effects. */
  1.1172 +    ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
  1.1173 +        DISFFC_STOPALL);
  1.1174 +    if (FAILED(ret)) {
  1.1175 +        return DI_SetError("Stopping the device", ret);
  1.1176 +    }
  1.1177 +    return 0;
  1.1178 +}
  1.1179 +
  1.1180 +#else /* !SDL_HAPTIC_DINPUT */
  1.1181 +
  1.1182 +
  1.1183 +int
  1.1184 +SDL_DINPUT_HapticInit(void)
  1.1185 +{
  1.1186 +    return 0;
  1.1187 +}
  1.1188 +
  1.1189 +int
  1.1190 +SDL_DINPUT_MaybeAddDevice(const DIDEVICEINSTANCE * pdidInstance)
  1.1191 +{
  1.1192 +    return SDL_Unsupported();
  1.1193 +}
  1.1194 +
  1.1195 +int
  1.1196 +SDL_DINPUT_MaybeRemoveDevice(const DIDEVICEINSTANCE * pdidInstance)
  1.1197 +{
  1.1198 +    return SDL_Unsupported();
  1.1199 +}
  1.1200 +
  1.1201 +int
  1.1202 +SDL_DINPUT_HapticOpen(SDL_Haptic * haptic, SDL_hapticlist_item *item)
  1.1203 +{
  1.1204 +    return SDL_Unsupported();
  1.1205 +}
  1.1206 +
  1.1207 +int
  1.1208 +SDL_DINPUT_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
  1.1209 +{
  1.1210 +    return SDL_Unsupported();
  1.1211 +}
  1.1212 +
  1.1213 +int
  1.1214 +SDL_DINPUT_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
  1.1215 +{
  1.1216 +    return SDL_Unsupported();
  1.1217 +}
  1.1218 +
  1.1219 +void
  1.1220 +SDL_DINPUT_HapticClose(SDL_Haptic * haptic)
  1.1221 +{
  1.1222 +}
  1.1223 +
  1.1224 +void
  1.1225 +SDL_DINPUT_HapticQuit(void)
  1.1226 +{
  1.1227 +}
  1.1228 +
  1.1229 +int
  1.1230 +SDL_DINPUT_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * base)
  1.1231 +{
  1.1232 +    return SDL_Unsupported();
  1.1233 +}
  1.1234 +
  1.1235 +int
  1.1236 +SDL_DINPUT_HapticUpdateEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * data)
  1.1237 +{
  1.1238 +    return SDL_Unsupported();
  1.1239 +}
  1.1240 +
  1.1241 +int
  1.1242 +SDL_DINPUT_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect, Uint32 iterations)
  1.1243 +{
  1.1244 +    return SDL_Unsupported();
  1.1245 +}
  1.1246 +
  1.1247 +int
  1.1248 +SDL_DINPUT_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
  1.1249 +{
  1.1250 +    return SDL_Unsupported();
  1.1251 +}
  1.1252 +
  1.1253 +void
  1.1254 +SDL_DINPUT_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
  1.1255 +{
  1.1256 +}
  1.1257 +
  1.1258 +int
  1.1259 +SDL_DINPUT_HapticGetEffectStatus(SDL_Haptic * haptic, struct haptic_effect *effect)
  1.1260 +{
  1.1261 +    return SDL_Unsupported();
  1.1262 +}
  1.1263 +
  1.1264 +int
  1.1265 +SDL_DINPUT_HapticSetGain(SDL_Haptic * haptic, int gain)
  1.1266 +{
  1.1267 +    return SDL_Unsupported();
  1.1268 +}
  1.1269 +
  1.1270 +int
  1.1271 +SDL_DINPUT_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
  1.1272 +{
  1.1273 +    return SDL_Unsupported();
  1.1274 +}
  1.1275 +
  1.1276 +int
  1.1277 +SDL_DINPUT_HapticPause(SDL_Haptic * haptic)
  1.1278 +{
  1.1279 +    return SDL_Unsupported();
  1.1280 +}
  1.1281 +
  1.1282 +int
  1.1283 +SDL_DINPUT_HapticUnpause(SDL_Haptic * haptic)
  1.1284 +{
  1.1285 +    return SDL_Unsupported();
  1.1286 +}
  1.1287 +
  1.1288 +int
  1.1289 +SDL_DINPUT_HapticStopAll(SDL_Haptic * haptic)
  1.1290 +{
  1.1291 +    return SDL_Unsupported();
  1.1292 +}
  1.1293 +
  1.1294 +#endif /* SDL_HAPTIC_DINPUT */
  1.1295 +
  1.1296 +/* vi: set ts=4 sw=4 expandtab: */