2 SDL - Simple DirectMedia Layer
3 Copyright (C) 2008 Edgar Simo
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 #include "SDL_syshaptic.h"
25 #include "SDL_haptic_c.h"
26 #include "../joystick/SDL_joystick_c.h" /* For SDL_PrivateJoystickValid */
29 Uint8 SDL_numhaptics = 0;
30 SDL_Haptic **SDL_haptics = NULL;
34 * Initializes the Haptic devices.
43 status = SDL_SYS_HapticInit();
45 arraylen = (status + 1) * sizeof(*SDL_haptics);
46 SDL_haptics = (SDL_Haptic **) SDL_malloc(arraylen);
47 if (SDL_haptics == NULL) { /* Out of memory. */
50 SDL_memset(SDL_haptics, 0, arraylen);
51 SDL_numhaptics = status;
61 * Checks to see if the haptic device is valid
64 ValidHaptic(SDL_Haptic * haptic)
71 for (i = 0; i < SDL_numhaptics; i++) {
72 if (SDL_haptics[i] == haptic) {
79 /* Create the error here. */
81 SDL_SetError("Haptic: Invalid haptic device identifier");
89 * Returns the number of available devices.
94 return SDL_numhaptics;
99 * Gets the name of a Haptic device by index.
102 SDL_HapticName(int device_index)
104 if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
105 SDL_SetError("Haptic: There are %d haptic devices available",
109 return SDL_SYS_HapticName(device_index);
114 * Opens a Haptic device.
117 SDL_HapticOpen(int device_index)
122 if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
123 SDL_SetError("Haptic: There are %d haptic devices available",
128 /* If the haptic is already open, return it */
129 for (i = 0; SDL_haptics[i]; i++) {
130 if (device_index == SDL_haptics[i]->index) {
131 haptic = SDL_haptics[i];
137 /* Create the haptic device */
138 haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
139 if (haptic == NULL) {
144 /* Initialize the haptic device */
145 SDL_memset(haptic, 0, (sizeof *haptic));
146 haptic->index = device_index;
147 if (SDL_SYS_HapticOpen(haptic) < 0) {
152 /* Disable autocenter and set gain to max. */
153 if (haptic->supported & SDL_HAPTIC_GAIN)
154 SDL_HapticSetGain(haptic, 100);
155 if (haptic->supported & SDL_HAPTIC_AUTOCENTER)
156 SDL_HapticSetAutocenter(haptic, 0);
158 /* Add haptic to list */
160 for (i = 0; SDL_haptics[i]; i++)
161 /* Skip to next haptic */ ;
162 SDL_haptics[i] = haptic;
169 * Returns 1 if the device has been opened.
172 SDL_HapticOpened(int device_index)
177 for (i = 0; SDL_haptics[i]; i++) {
178 if (SDL_haptics[i]->index == (Uint8) device_index) {
188 * Returns the index to a haptic device.
191 SDL_HapticIndex(SDL_Haptic * haptic)
193 if (!ValidHaptic(haptic)) {
197 return haptic->index;
202 * Returns SDL_TRUE if mouse is haptic, SDL_FALSE if it isn't.
205 SDL_MouseIsHaptic(void)
207 if (SDL_SYS_HapticMouse() < 0)
214 * Returns the haptic device if mouse is haptic or NULL elsewise.
217 SDL_HapticOpenFromMouse(void)
221 device_index = SDL_SYS_HapticMouse();
223 if (device_index < 0) {
224 SDL_SetError("Haptic: Mouse isn't a haptic device.");
228 return SDL_HapticOpen(device_index);
233 * Returns SDL_TRUE if joystick has haptic features.
236 SDL_JoystickIsHaptic(SDL_Joystick * joystick)
240 /* Must be a valid joystick */
241 if (!SDL_PrivateJoystickValid(&joystick)) {
245 ret = SDL_SYS_JoystickIsHaptic(joystick);
257 * Opens a haptic device from a joystick.
260 SDL_HapticOpenFromJoystick(SDL_Joystick * joystick)
265 /* Must be a valid joystick */
266 if (!SDL_PrivateJoystickValid(&joystick)) {
267 SDL_SetError("Haptic: Joystick isn't valid.");
271 /* Joystick must be haptic */
272 if (SDL_SYS_JoystickIsHaptic(joystick) <= 0) {
273 SDL_SetError("Haptic: Joystick isn't a haptic device.");
277 /* Check to see if joystick's haptic is already open */
278 for (i = 0; SDL_haptics[i]; i++) {
279 if (SDL_SYS_JoystickSameHaptic(SDL_haptics[i], joystick)) {
280 haptic = SDL_haptics[i];
286 /* Create the haptic device */
287 haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
288 if (haptic == NULL) {
293 /* Initialize the haptic device */
294 SDL_memset(haptic, 0, sizeof(SDL_Haptic));
295 if (SDL_SYS_HapticOpenFromJoystick(haptic, joystick) < 0) {
300 /* Add haptic to list */
302 for (i = 0; SDL_haptics[i]; i++)
303 /* Skip to next haptic */ ;
304 SDL_haptics[i] = haptic;
311 * Closes a SDL_Haptic device.
314 SDL_HapticClose(SDL_Haptic * haptic)
319 if (!ValidHaptic(haptic)) {
323 /* Check if it's still in use */
324 if (--haptic->ref_count < 0) {
328 /* Close it, properly removing effects if needed */
329 for (i = 0; i < haptic->neffects; i++) {
330 if (haptic->effects[i].hweffect != NULL) {
331 SDL_HapticDestroyEffect(haptic, i);
334 SDL_SYS_HapticClose(haptic);
336 /* Remove from the list */
337 for (i = 0; SDL_haptics[i]; ++i) {
338 if (haptic == SDL_haptics[i]) {
339 SDL_haptics[i] = NULL;
340 SDL_memcpy(&SDL_haptics[i], &SDL_haptics[i + 1],
341 (SDL_numhaptics - i) * sizeof(haptic));
351 * Cleans up after the subsystem.
356 SDL_SYS_HapticQuit();
357 if (SDL_haptics != NULL) {
358 SDL_free(SDL_haptics);
365 * Returns the number of effects a haptic device has.
368 SDL_HapticNumEffects(SDL_Haptic * haptic)
370 if (!ValidHaptic(haptic)) {
374 return haptic->neffects;
379 * Returns the number of effects a haptic device can play.
382 SDL_HapticNumEffectsPlaying(SDL_Haptic * haptic)
384 if (!ValidHaptic(haptic)) {
388 return haptic->nplaying;
393 * Returns supported effects by the device.
396 SDL_HapticQuery(SDL_Haptic * haptic)
398 if (!ValidHaptic(haptic)) {
402 return haptic->supported;
407 * Returns the number of axis on the device.
410 SDL_HapticNumAxes(SDL_Haptic * haptic)
412 if (!ValidHaptic(haptic)) {
416 return haptic->naxes;
420 * Checks to see if the device can support the effect.
423 SDL_HapticEffectSupported(SDL_Haptic * haptic, SDL_HapticEffect * effect)
425 if (!ValidHaptic(haptic)) {
429 if ((haptic->supported & effect->type) != 0)
435 * Creates a new haptic effect.
438 SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect)
442 /* Check for device validity. */
443 if (!ValidHaptic(haptic)) {
447 /* Check to see if effect is supported */
448 if (SDL_HapticEffectSupported(haptic, effect) == SDL_FALSE) {
449 SDL_SetError("Haptic: Effect not supported by haptic device.");
453 /* See if there's a free slot */
454 for (i = 0; i < haptic->neffects; i++) {
455 if (haptic->effects[i].hweffect == NULL) {
457 /* Now let the backend create the real effect */
458 if (SDL_SYS_HapticNewEffect(haptic, &haptic->effects[i], effect)
460 return -1; /* Backend failed to create effect */
463 SDL_memcpy(&haptic->effects[i].effect, effect,
464 sizeof(SDL_HapticEffect));
469 SDL_SetError("Haptic: Device has no free space left.");
474 * Checks to see if an effect is valid.
477 ValidEffect(SDL_Haptic * haptic, int effect)
479 if ((effect < 0) || (effect >= haptic->neffects)) {
480 SDL_SetError("Haptic: Invalid effect identifier.");
490 SDL_HapticUpdateEffect(SDL_Haptic * haptic, int effect,
491 SDL_HapticEffect * data)
493 if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
497 /* Can't change type dynamically. */
498 if (data->type != haptic->effects[effect].effect.type) {
499 SDL_SetError("Haptic: Updating effect type is illegal.");
503 /* Updates the effect */
504 if (SDL_SYS_HapticUpdateEffect(haptic, &haptic->effects[effect], data) <
509 SDL_memcpy(&haptic->effects[effect].effect, data,
510 sizeof(SDL_HapticEffect));
516 * Runs the haptic effect on the device.
519 SDL_HapticRunEffect(SDL_Haptic * haptic, int effect, Uint32 iterations)
521 if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
526 if (SDL_SYS_HapticRunEffect(haptic, &haptic->effects[effect], iterations)
535 * Stops the haptic effect on the device.
538 SDL_HapticStopEffect(SDL_Haptic * haptic, int effect)
540 if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
544 /* Stop the effect */
545 if (SDL_SYS_HapticStopEffect(haptic, &haptic->effects[effect]) < 0) {
553 * Gets rid of a haptic effect.
556 SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect)
558 if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
563 if (haptic->effects[effect].hweffect == NULL) {
567 SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]);
571 * Gets the status of a haptic effect.
574 SDL_HapticGetEffectStatus(SDL_Haptic * haptic, int effect)
576 if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
580 if ((haptic->supported & SDL_HAPTIC_STATUS) == 0) {
581 SDL_SetError("Haptic: Device does not support status queries.");
585 return SDL_SYS_HapticGetEffectStatus(haptic, &haptic->effects[effect]);
589 * Sets the global gain of the device.
592 SDL_HapticSetGain(SDL_Haptic * haptic, int gain)
595 int real_gain, max_gain;
597 if (!ValidHaptic(haptic)) {
601 if ((haptic->supported & SDL_HAPTIC_GAIN) == 0) {
602 SDL_SetError("Haptic: Device does not support setting gain.");
606 if ((gain < 0) || (gain > 100)) {
607 SDL_SetError("Haptic: Gain must be between 0 and 100.");
611 /* We use the envvar to get the maximum gain. */
612 env = SDL_getenv("SDL_HAPTIC_GAIN_MAX");
614 max_gain = SDL_atoi(env);
616 /* Check for sanity. */
619 else if (max_gain > 100)
622 /* We'll scale it linearly with SDL_HAPTIC_GAIN_MAX */
623 real_gain = (gain * max_gain) / 100;
628 if (SDL_SYS_HapticSetGain(haptic, real_gain) < 0) {
636 * Makes the device autocenter, 0 disables.
639 SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
641 if (!ValidHaptic(haptic)) {
645 if ((haptic->supported & SDL_HAPTIC_AUTOCENTER) == 0) {
646 SDL_SetError("Haptic: Device does not support setting autocenter.");
650 if ((autocenter < 0) || (autocenter > 100)) {
651 SDL_SetError("Haptic: Autocenter must be between 0 and 100.");
655 if (SDL_SYS_HapticSetAutocenter(haptic, autocenter) < 0) {
663 * Pauses the haptic device.
666 SDL_HapticPause(SDL_Haptic * haptic)
668 if (!ValidHaptic(haptic)) {
672 if ((haptic->supported & SDL_HAPTIC_PAUSE) == 0) {
673 SDL_SetError("Haptic: Device does not support setting pausing.");
677 return SDL_SYS_HapticPause(haptic);
681 * Unpauses the haptic device.
684 SDL_HapticUnpause(SDL_Haptic * haptic)
686 if (!ValidHaptic(haptic)) {
690 if ((haptic->supported & SDL_HAPTIC_PAUSE) == 0) {
691 return 0; /* Not going to be paused, so we pretend it's unpaused. */
694 return SDL_SYS_HapticUnpause(haptic);
698 * Stops all the currently playing effects.
701 SDL_HapticStopAll(SDL_Haptic * haptic)
703 if (!ValidHaptic(haptic)) {
707 return SDL_SYS_HapticStopAll(haptic);