src/haptic/windows/SDL_windowshaptic.c
changeset 8972 dfc759d7486f
parent 8688 1543a7183f88
child 9619 b94b6d0bff0f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/haptic/windows/SDL_windowshaptic.c	Thu Jul 03 15:39:55 2014 -0700
     1.3 @@ -0,0 +1,445 @@
     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 +#if SDL_HAPTIC_DINPUT || SDL_HAPTIC_XINPUT
    1.27 +
    1.28 +#include "SDL_assert.h"
    1.29 +#include "SDL_thread.h"
    1.30 +#include "SDL_mutex.h"
    1.31 +#include "SDL_timer.h"
    1.32 +#include "SDL_hints.h"
    1.33 +#include "SDL_haptic.h"
    1.34 +#include "../SDL_syshaptic.h"
    1.35 +#include "SDL_joystick.h"
    1.36 +#include "../../joystick/SDL_sysjoystick.h"     /* For the real SDL_Joystick */
    1.37 +#include "../../joystick/windows/SDL_windowsjoystick_c.h"      /* For joystick hwdata */
    1.38 +#include "../../joystick/windows/SDL_xinputjoystick_c.h"      /* For xinput rumble */
    1.39 +
    1.40 +#include "SDL_windowshaptic_c.h"
    1.41 +#include "SDL_dinputhaptic_c.h"
    1.42 +#include "SDL_xinputhaptic_c.h"
    1.43 +
    1.44 +
    1.45 +/*
    1.46 + * Internal stuff.
    1.47 + */
    1.48 +SDL_hapticlist_item *SDL_hapticlist = NULL;
    1.49 +static SDL_hapticlist_item *SDL_hapticlist_tail = NULL;
    1.50 +static int numhaptics = 0;
    1.51 +
    1.52 +
    1.53 +/*
    1.54 + * Initializes the haptic subsystem.
    1.55 + */
    1.56 +int
    1.57 +SDL_SYS_HapticInit(void)
    1.58 +{
    1.59 +    if (SDL_DINPUT_HapticInit() < 0) {
    1.60 +        return -1;
    1.61 +    }
    1.62 +    if (SDL_XINPUT_HapticInit() < 0) {
    1.63 +        return -1;
    1.64 +    }
    1.65 +    return numhaptics;
    1.66 +}
    1.67 +
    1.68 +int
    1.69 +SDL_SYS_AddHapticDevice(SDL_hapticlist_item *item)
    1.70 +{
    1.71 +    if (SDL_hapticlist_tail == NULL) {
    1.72 +        SDL_hapticlist = SDL_hapticlist_tail = item;
    1.73 +    } else {
    1.74 +        SDL_hapticlist_tail->next = item;
    1.75 +        SDL_hapticlist_tail = item;
    1.76 +    }
    1.77 +
    1.78 +    /* Device has been added. */
    1.79 +    ++numhaptics;
    1.80 +
    1.81 +    return numhaptics;
    1.82 +}
    1.83 +
    1.84 +int
    1.85 +SDL_SYS_RemoveHapticDevice(SDL_hapticlist_item *prev, SDL_hapticlist_item *item)
    1.86 +{
    1.87 +    const int retval = item->haptic ? item->haptic->index : -1;
    1.88 +    if (prev != NULL) {
    1.89 +        prev->next = item->next;
    1.90 +    } else {
    1.91 +        SDL_assert(SDL_hapticlist == item);
    1.92 +        SDL_hapticlist = item->next;
    1.93 +    }
    1.94 +    if (item == SDL_hapticlist_tail) {
    1.95 +        SDL_hapticlist_tail = prev;
    1.96 +    }
    1.97 +    --numhaptics;
    1.98 +    /* !!! TODO: Send a haptic remove event? */
    1.99 +    SDL_free(item);
   1.100 +    return retval;
   1.101 +}
   1.102 +
   1.103 +int
   1.104 +SDL_SYS_NumHaptics()
   1.105 +{
   1.106 +    return numhaptics;
   1.107 +}
   1.108 +
   1.109 +static SDL_hapticlist_item *
   1.110 +HapticByDevIndex(int device_index)
   1.111 +{
   1.112 +    SDL_hapticlist_item *item = SDL_hapticlist;
   1.113 +
   1.114 +    if ((device_index < 0) || (device_index >= numhaptics)) {
   1.115 +        return NULL;
   1.116 +    }
   1.117 +
   1.118 +    while (device_index > 0) {
   1.119 +        SDL_assert(item != NULL);
   1.120 +        --device_index;
   1.121 +        item = item->next;
   1.122 +    }
   1.123 +    return item;
   1.124 +}
   1.125 +
   1.126 +/*
   1.127 + * Return the name of a haptic device, does not need to be opened.
   1.128 + */
   1.129 +const char *
   1.130 +SDL_SYS_HapticName(int index)
   1.131 +{
   1.132 +    SDL_hapticlist_item *item = HapticByDevIndex(index);
   1.133 +    return item->name;
   1.134 +}
   1.135 +
   1.136 +/*
   1.137 + * Opens a haptic device for usage.
   1.138 + */
   1.139 +int
   1.140 +SDL_SYS_HapticOpen(SDL_Haptic * haptic)
   1.141 +{
   1.142 +    SDL_hapticlist_item *item = HapticByDevIndex(haptic->index);
   1.143 +    if (item->bXInputHaptic) {
   1.144 +        return SDL_XINPUT_HapticOpen(haptic, item);
   1.145 +    } else {
   1.146 +        return SDL_DINPUT_HapticOpen(haptic, item);
   1.147 +    }
   1.148 +}
   1.149 +
   1.150 +
   1.151 +/*
   1.152 + * Opens a haptic device from first mouse it finds for usage.
   1.153 + */
   1.154 +int
   1.155 +SDL_SYS_HapticMouse(void)
   1.156 +{
   1.157 +#if SDL_HAPTIC_DINPUT
   1.158 +    SDL_hapticlist_item *item;
   1.159 +    int index = 0;
   1.160 +
   1.161 +    /* Grab the first mouse haptic device we find. */
   1.162 +    for (item = SDL_hapticlist; item != NULL; item = item->next) {
   1.163 +        if (item->capabilities.dwDevType == DI8DEVCLASS_POINTER ) {
   1.164 +            return index;
   1.165 +        }
   1.166 +        ++index;
   1.167 +    }
   1.168 +#endif /* SDL_HAPTIC_DINPUT */
   1.169 +    return -1;
   1.170 +}
   1.171 +
   1.172 +
   1.173 +/*
   1.174 + * Checks to see if a joystick has haptic features.
   1.175 + */
   1.176 +int
   1.177 +SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
   1.178 +{
   1.179 +    const struct joystick_hwdata *hwdata = joystick->hwdata;
   1.180 +#if SDL_HAPTIC_XINPUT
   1.181 +    if (hwdata->bXInputHaptic) {
   1.182 +        return 1;
   1.183 +    }
   1.184 +#endif
   1.185 +#if SDL_HAPTIC_DINPUT
   1.186 +    if (hwdata->Capabilities.dwFlags & DIDC_FORCEFEEDBACK) {
   1.187 +        return 1;
   1.188 +    }
   1.189 +#endif
   1.190 +    return 0;
   1.191 +}
   1.192 +
   1.193 +/*
   1.194 + * Checks to see if the haptic device and joystick are in reality the same.
   1.195 + */
   1.196 +int
   1.197 +SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
   1.198 +{
   1.199 +    if (joystick->hwdata->bXInputHaptic != haptic->hwdata->bXInputHaptic) {
   1.200 +        return 0;  /* one is XInput, one is not; not the same device. */
   1.201 +    } else if (joystick->hwdata->bXInputHaptic) {
   1.202 +        return SDL_XINPUT_JoystickSameHaptic(haptic, joystick);
   1.203 +    } else {
   1.204 +        return SDL_DINPUT_JoystickSameHaptic(haptic, joystick);
   1.205 +    }
   1.206 +}
   1.207 +
   1.208 +/*
   1.209 + * Opens a SDL_Haptic from a SDL_Joystick.
   1.210 + */
   1.211 +int
   1.212 +SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
   1.213 +{
   1.214 +    if (joystick->hwdata->bXInputDevice) {
   1.215 +        return SDL_XINPUT_HapticOpenFromJoystick(haptic, joystick);
   1.216 +    } else {
   1.217 +        return SDL_DINPUT_HapticOpenFromJoystick(haptic, joystick);
   1.218 +    }
   1.219 +}
   1.220 +
   1.221 +/*
   1.222 + * Closes the haptic device.
   1.223 + */
   1.224 +void
   1.225 +SDL_SYS_HapticClose(SDL_Haptic * haptic)
   1.226 +{
   1.227 +    if (haptic->hwdata) {
   1.228 +
   1.229 +        /* Free effects. */
   1.230 +        SDL_free(haptic->effects);
   1.231 +        haptic->effects = NULL;
   1.232 +        haptic->neffects = 0;
   1.233 +
   1.234 +        /* Clean up */
   1.235 +        if (haptic->hwdata->bXInputHaptic) {
   1.236 +            SDL_XINPUT_HapticClose(haptic);
   1.237 +        } else {
   1.238 +            SDL_DINPUT_HapticClose(haptic);
   1.239 +        }
   1.240 +
   1.241 +        /* Free */
   1.242 +        SDL_free(haptic->hwdata);
   1.243 +        haptic->hwdata = NULL;
   1.244 +    }
   1.245 +}
   1.246 +
   1.247 +/*
   1.248 + * Clean up after system specific haptic stuff
   1.249 + */
   1.250 +void
   1.251 +SDL_SYS_HapticQuit(void)
   1.252 +{
   1.253 +    SDL_hapticlist_item *item;
   1.254 +    SDL_hapticlist_item *next = NULL;
   1.255 +    SDL_Haptic *hapticitem = NULL;
   1.256 +
   1.257 +    extern SDL_Haptic *SDL_haptics;
   1.258 +    for (hapticitem = SDL_haptics; hapticitem; hapticitem = hapticitem->next) {
   1.259 +        if ((hapticitem->hwdata->bXInputHaptic) && (hapticitem->hwdata->thread)) {
   1.260 +            /* we _have_ to stop the thread before we free the XInput DLL! */
   1.261 +            hapticitem->hwdata->stopThread = 1;
   1.262 +            SDL_WaitThread(hapticitem->hwdata->thread, NULL);
   1.263 +            hapticitem->hwdata->thread = NULL;
   1.264 +        }
   1.265 +    }
   1.266 +
   1.267 +    for (item = SDL_hapticlist; item; item = next) {
   1.268 +        /* Opened and not closed haptics are leaked, this is on purpose.
   1.269 +         * Close your haptic devices after usage. */
   1.270 +        /* !!! FIXME: (...is leaking on purpose a good idea?) - No, of course not. */
   1.271 +        next = item->next;
   1.272 +        SDL_free(item->name);
   1.273 +        SDL_free(item);
   1.274 +    }
   1.275 +
   1.276 +    SDL_XINPUT_HapticQuit();
   1.277 +    SDL_DINPUT_HapticQuit();
   1.278 +}
   1.279 +
   1.280 +/*
   1.281 + * Creates a new haptic effect.
   1.282 + */
   1.283 +int
   1.284 +SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
   1.285 +                        SDL_HapticEffect * base)
   1.286 +{
   1.287 +    int result;
   1.288 +
   1.289 +    /* Alloc the effect. */
   1.290 +    effect->hweffect = (struct haptic_hweffect *)
   1.291 +        SDL_malloc(sizeof(struct haptic_hweffect));
   1.292 +    if (effect->hweffect == NULL) {
   1.293 +        SDL_OutOfMemory();
   1.294 +        return -1;
   1.295 +    }
   1.296 +    SDL_zerop(effect->hweffect);
   1.297 +
   1.298 +    if (haptic->hwdata->bXInputHaptic) {
   1.299 +        result = SDL_XINPUT_HapticNewEffect(haptic, effect, base);
   1.300 +    } else {
   1.301 +        result = SDL_DINPUT_HapticNewEffect(haptic, effect, base);
   1.302 +    }
   1.303 +    if (result < 0) {
   1.304 +        SDL_free(effect->hweffect);
   1.305 +        effect->hweffect = NULL;
   1.306 +    }
   1.307 +    return result;
   1.308 +}
   1.309 +
   1.310 +/*
   1.311 + * Updates an effect.
   1.312 + */
   1.313 +int
   1.314 +SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
   1.315 +                           struct haptic_effect *effect,
   1.316 +                           SDL_HapticEffect * data)
   1.317 +{
   1.318 +    if (haptic->hwdata->bXInputHaptic) {
   1.319 +        return SDL_XINPUT_HapticUpdateEffect(haptic, effect, data);
   1.320 +    } else {
   1.321 +        return SDL_DINPUT_HapticUpdateEffect(haptic, effect, data);
   1.322 +    }
   1.323 +}
   1.324 +
   1.325 +/*
   1.326 + * Runs an effect.
   1.327 + */
   1.328 +int
   1.329 +SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
   1.330 +                        Uint32 iterations)
   1.331 +{
   1.332 +    if (haptic->hwdata->bXInputHaptic) {
   1.333 +        return SDL_XINPUT_HapticRunEffect(haptic, effect, iterations);
   1.334 +    } else {
   1.335 +        return SDL_DINPUT_HapticRunEffect(haptic, effect, iterations);
   1.336 +    }
   1.337 +}
   1.338 +
   1.339 +/*
   1.340 + * Stops an effect.
   1.341 + */
   1.342 +int
   1.343 +SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
   1.344 +{
   1.345 +    if (haptic->hwdata->bXInputHaptic) {
   1.346 +        return SDL_XINPUT_HapticStopEffect(haptic, effect);
   1.347 +    } else {
   1.348 +        return SDL_DINPUT_HapticStopEffect(haptic, effect);
   1.349 +    }
   1.350 +}
   1.351 +
   1.352 +/*
   1.353 + * Frees the effect.
   1.354 + */
   1.355 +void
   1.356 +SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
   1.357 +{
   1.358 +    if (haptic->hwdata->bXInputHaptic) {
   1.359 +        SDL_XINPUT_HapticDestroyEffect(haptic, effect);
   1.360 +    } else {
   1.361 +        SDL_DINPUT_HapticDestroyEffect(haptic, effect);
   1.362 +    }
   1.363 +    SDL_free(effect->hweffect);
   1.364 +    effect->hweffect = NULL;
   1.365 +}
   1.366 +
   1.367 +/*
   1.368 + * Gets the status of a haptic effect.
   1.369 + */
   1.370 +int
   1.371 +SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic,
   1.372 +                              struct haptic_effect *effect)
   1.373 +{
   1.374 +    if (haptic->hwdata->bXInputHaptic) {
   1.375 +        return SDL_XINPUT_HapticGetEffectStatus(haptic, effect);
   1.376 +    } else {
   1.377 +        return SDL_DINPUT_HapticGetEffectStatus(haptic, effect);
   1.378 +    }
   1.379 +}
   1.380 +
   1.381 +/*
   1.382 + * Sets the gain.
   1.383 + */
   1.384 +int
   1.385 +SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
   1.386 +{
   1.387 +    if (haptic->hwdata->bXInputHaptic) {
   1.388 +        return SDL_XINPUT_HapticSetGain(haptic, gain);
   1.389 +    } else {
   1.390 +        return SDL_DINPUT_HapticSetGain(haptic, gain);
   1.391 +    }
   1.392 +}
   1.393 +
   1.394 +/*
   1.395 + * Sets the autocentering.
   1.396 + */
   1.397 +int
   1.398 +SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
   1.399 +{
   1.400 +    if (haptic->hwdata->bXInputHaptic) {
   1.401 +        return SDL_XINPUT_HapticSetAutocenter(haptic, autocenter);
   1.402 +    } else {
   1.403 +        return SDL_DINPUT_HapticSetAutocenter(haptic, autocenter);
   1.404 +    }
   1.405 +}
   1.406 +
   1.407 +/*
   1.408 + * Pauses the device.
   1.409 + */
   1.410 +int
   1.411 +SDL_SYS_HapticPause(SDL_Haptic * haptic)
   1.412 +{
   1.413 +    if (haptic->hwdata->bXInputHaptic) {
   1.414 +        return SDL_XINPUT_HapticPause(haptic);
   1.415 +    } else {
   1.416 +        return SDL_DINPUT_HapticPause(haptic);
   1.417 +    }
   1.418 +}
   1.419 +
   1.420 +/*
   1.421 + * Pauses the device.
   1.422 + */
   1.423 +int
   1.424 +SDL_SYS_HapticUnpause(SDL_Haptic * haptic)
   1.425 +{
   1.426 +    if (haptic->hwdata->bXInputHaptic) {
   1.427 +        return SDL_XINPUT_HapticUnpause(haptic);
   1.428 +    } else {
   1.429 +        return SDL_DINPUT_HapticUnpause(haptic);
   1.430 +    }
   1.431 +}
   1.432 +
   1.433 +/*
   1.434 + * Stops all the playing effects on the device.
   1.435 + */
   1.436 +int
   1.437 +SDL_SYS_HapticStopAll(SDL_Haptic * haptic)
   1.438 +{
   1.439 +    if (haptic->hwdata->bXInputHaptic) {
   1.440 +        return SDL_XINPUT_HapticStopAll(haptic);
   1.441 +    } else {
   1.442 +        return SDL_DINPUT_HapticStopAll(haptic);
   1.443 +    }
   1.444 +}
   1.445 +
   1.446 +#endif /* SDL_HAPTIC_DINPUT || SDL_HAPTIC_XINPUT */
   1.447 +
   1.448 +/* vi: set ts=4 sw=4 expandtab: */