Hash :
3f1fd5ab
Author :
Date :
2023-05-23T10:59:03
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../SDL_internal.h"
#include "SDL_syshaptic.h"
#include "SDL_haptic_c.h"
#include "../joystick/SDL_joystick_c.h" /* For SDL_PrivateJoystickValid */
/* Global for SDL_windowshaptic.c */
#if (defined(SDL_HAPTIC_DINPUT) && SDL_HAPTIC_DINPUT) || (defined(SDL_HAPTIC_XINPUT) && SDL_HAPTIC_XINPUT)
SDL_Haptic *SDL_haptics = NULL;
#else
static SDL_Haptic *SDL_haptics = NULL;
#endif
/*
* Initializes the Haptic devices.
*/
int SDL_HapticInit(void)
{
int status;
status = SDL_SYS_HapticInit();
if (status >= 0) {
status = 0;
}
return status;
}
/*
* Checks to see if the haptic device is valid
*/
static int ValidHaptic(SDL_Haptic *haptic)
{
int valid;
SDL_Haptic *hapticlist;
valid = 0;
if (haptic != NULL) {
hapticlist = SDL_haptics;
while (hapticlist) {
if (hapticlist == haptic) {
valid = 1;
break;
}
hapticlist = hapticlist->next;
}
}
/* Create the error here. */
if (valid == 0) {
SDL_SetError("Haptic: Invalid haptic device identifier");
}
return valid;
}
/*
* Returns the number of available devices.
*/
int SDL_NumHaptics(void)
{
return SDL_SYS_NumHaptics();
}
/*
* Gets the name of a Haptic device by index.
*/
const char *SDL_HapticName(int device_index)
{
if ((device_index < 0) || (device_index >= SDL_NumHaptics())) {
SDL_SetError("Haptic: There are %d haptic devices available",
SDL_NumHaptics());
return NULL;
}
return SDL_SYS_HapticName(device_index);
}
/*
* Opens a Haptic device.
*/
SDL_Haptic *SDL_HapticOpen(int device_index)
{
SDL_Haptic *haptic;
SDL_Haptic *hapticlist;
if ((device_index < 0) || (device_index >= SDL_NumHaptics())) {
SDL_SetError("Haptic: There are %d haptic devices available",
SDL_NumHaptics());
return NULL;
}
hapticlist = SDL_haptics;
/* If the haptic is already open, return it
* TODO: Should we create haptic instance IDs like the Joystick API?
*/
while (hapticlist) {
if (device_index == hapticlist->index) {
haptic = hapticlist;
++haptic->ref_count;
return haptic;
}
hapticlist = hapticlist->next;
}
/* Create the haptic device */
haptic = (SDL_Haptic *)SDL_malloc(sizeof(*haptic));
if (haptic == NULL) {
SDL_OutOfMemory();
return NULL;
}
/* Initialize the haptic device */
SDL_memset(haptic, 0, sizeof(*haptic));
haptic->rumble_id = -1;
haptic->index = device_index;
if (SDL_SYS_HapticOpen(haptic) < 0) {
SDL_free(haptic);
return NULL;
}
/* Add haptic to list */
++haptic->ref_count;
/* Link the haptic in the list */
haptic->next = SDL_haptics;
SDL_haptics = haptic;
/* Disable autocenter and set gain to max. */
if (haptic->supported & SDL_HAPTIC_GAIN) {
SDL_HapticSetGain(haptic, 100);
}
if (haptic->supported & SDL_HAPTIC_AUTOCENTER) {
SDL_HapticSetAutocenter(haptic, 0);
}
return haptic;
}
/*
* Returns 1 if the device has been opened.
*/
int SDL_HapticOpened(int device_index)
{
int opened;
SDL_Haptic *hapticlist;
/* Make sure it's valid. */
if ((device_index < 0) || (device_index >= SDL_NumHaptics())) {
SDL_SetError("Haptic: There are %d haptic devices available",
SDL_NumHaptics());
return 0;
}
opened = 0;
hapticlist = SDL_haptics;
/* TODO Should this use an instance ID? */
while (hapticlist) {
if (hapticlist->index == (Uint8)device_index) {
opened = 1;
break;
}
hapticlist = hapticlist->next;
}
return opened;
}
/*
* Returns the index to a haptic device.
*/
int SDL_HapticIndex(SDL_Haptic *haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}
return haptic->index;
}
/*
* Returns SDL_TRUE if mouse is haptic, SDL_FALSE if it isn't.
*/
int SDL_MouseIsHaptic(void)
{
if (SDL_SYS_HapticMouse() < 0) {
return SDL_FALSE;
}
return SDL_TRUE;
}
/*
* Returns the haptic device if mouse is haptic or NULL elsewise.
*/
SDL_Haptic *SDL_HapticOpenFromMouse(void)
{
int device_index;
device_index = SDL_SYS_HapticMouse();
if (device_index < 0) {
SDL_SetError("Haptic: Mouse isn't a haptic device.");
return NULL;
}
return SDL_HapticOpen(device_index);
}
/*
* Returns SDL_TRUE if joystick has haptic features.
*/
int SDL_JoystickIsHaptic(SDL_Joystick *joystick)
{
int ret;
SDL_LockJoysticks();
{
/* Must be a valid joystick */
if (!SDL_PrivateJoystickValid(joystick)) {
SDL_UnlockJoysticks();
return -1;
}
ret = SDL_SYS_JoystickIsHaptic(joystick);
}
SDL_UnlockJoysticks();
if (ret > 0) {
return SDL_TRUE;
} else if (ret == 0) {
return SDL_FALSE;
}
return -1;
}
/*
* Opens a haptic device from a joystick.
*/
SDL_Haptic *SDL_HapticOpenFromJoystick(SDL_Joystick *joystick)
{
SDL_Haptic *haptic;
SDL_Haptic *hapticlist;
/* Make sure there is room. */
if (SDL_NumHaptics() <= 0) {
SDL_SetError("Haptic: There are %d haptic devices available",
SDL_NumHaptics());
return NULL;
}
SDL_LockJoysticks();
{
/* Must be a valid joystick */
if (!SDL_PrivateJoystickValid(joystick)) {
SDL_SetError("Haptic: Joystick isn't valid.");
SDL_UnlockJoysticks();
return NULL;
}
/* Joystick must be haptic */
if (SDL_SYS_JoystickIsHaptic(joystick) <= 0) {
SDL_SetError("Haptic: Joystick isn't a haptic device.");
SDL_UnlockJoysticks();
return NULL;
}
hapticlist = SDL_haptics;
/* Check to see if joystick's haptic is already open */
while (hapticlist) {
if (SDL_SYS_JoystickSameHaptic(hapticlist, joystick)) {
haptic = hapticlist;
++haptic->ref_count;
SDL_UnlockJoysticks();
return haptic;
}
hapticlist = hapticlist->next;
}
/* Create the haptic device */
haptic = (SDL_Haptic *)SDL_malloc(sizeof(*haptic));
if (haptic == NULL) {
SDL_OutOfMemory();
SDL_UnlockJoysticks();
return NULL;
}
/* Initialize the haptic device */
SDL_memset(haptic, 0, sizeof(SDL_Haptic));
haptic->rumble_id = -1;
if (SDL_SYS_HapticOpenFromJoystick(haptic, joystick) < 0) {
SDL_SetError("Haptic: SDL_SYS_HapticOpenFromJoystick failed.");
SDL_free(haptic);
SDL_UnlockJoysticks();
return NULL;
}
}
SDL_UnlockJoysticks();
/* Add haptic to list */
++haptic->ref_count;
/* Link the haptic in the list */
haptic->next = SDL_haptics;
SDL_haptics = haptic;
return haptic;
}
/*
* Closes a SDL_Haptic device.
*/
void SDL_HapticClose(SDL_Haptic *haptic)
{
int i;
SDL_Haptic *hapticlist;
SDL_Haptic *hapticlistprev;
/* Must be valid */
if (!ValidHaptic(haptic)) {
return;
}
/* Check if it's still in use */
if (--haptic->ref_count > 0) {
return;
}
/* Close it, properly removing effects if needed */
for (i = 0; i < haptic->neffects; i++) {
if (haptic->effects[i].hweffect != NULL) {
SDL_HapticDestroyEffect(haptic, i);
}
}
SDL_SYS_HapticClose(haptic);
/* Remove from the list */
hapticlist = SDL_haptics;
hapticlistprev = NULL;
while (hapticlist) {
if (haptic == hapticlist) {
if (hapticlistprev) {
/* unlink this entry */
hapticlistprev->next = hapticlist->next;
} else {
SDL_haptics = haptic->next;
}
break;
}
hapticlistprev = hapticlist;
hapticlist = hapticlist->next;
}
/* Free */
SDL_free(haptic);
}
/*
* Cleans up after the subsystem.
*/
void SDL_HapticQuit(void)
{
while (SDL_haptics) {
SDL_HapticClose(SDL_haptics);
}
SDL_SYS_HapticQuit();
}
/*
* Returns the number of effects a haptic device has.
*/
int SDL_HapticNumEffects(SDL_Haptic *haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}
return haptic->neffects;
}
/*
* Returns the number of effects a haptic device can play.
*/
int SDL_HapticNumEffectsPlaying(SDL_Haptic *haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}
return haptic->nplaying;
}
/*
* Returns supported effects by the device.
*/
unsigned int SDL_HapticQuery(SDL_Haptic *haptic)
{
if (!ValidHaptic(haptic)) {
return 0; /* same as if no effects were supported */
}
return haptic->supported;
}
/*
* Returns the number of axis on the device.
*/
int SDL_HapticNumAxes(SDL_Haptic *haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}
return haptic->naxes;
}
/*
* Checks to see if the device can support the effect.
*/
int SDL_HapticEffectSupported(SDL_Haptic *haptic, SDL_HapticEffect *effect)
{
if (!ValidHaptic(haptic)) {
return -1;
}
if ((haptic->supported & effect->type) != 0) {
return SDL_TRUE;
}
return SDL_FALSE;
}
/*
* Creates a new haptic effect.
*/
int SDL_HapticNewEffect(SDL_Haptic *haptic, SDL_HapticEffect *effect)
{
int i;
/* Check for device validity. */
if (!ValidHaptic(haptic)) {
return -1;
}
/* Check to see if effect is supported */
if (SDL_HapticEffectSupported(haptic, effect) == SDL_FALSE) {
return SDL_SetError("Haptic: Effect not supported by haptic device.");
}
/* See if there's a free slot */
for (i = 0; i < haptic->neffects; i++) {
if (haptic->effects[i].hweffect == NULL) {
/* Now let the backend create the real effect */
if (SDL_SYS_HapticNewEffect(haptic, &haptic->effects[i], effect) != 0) {
return -1; /* Backend failed to create effect */
}
SDL_memcpy(&haptic->effects[i].effect, effect,
sizeof(SDL_HapticEffect));
return i;
}
}
return SDL_SetError("Haptic: Device has no free space left.");
}
/*
* Checks to see if an effect is valid.
*/
static int ValidEffect(SDL_Haptic *haptic, int effect)
{
if ((effect < 0) || (effect >= haptic->neffects)) {
SDL_SetError("Haptic: Invalid effect identifier.");
return 0;
}
return 1;
}
/*
* Updates an effect.
*/
int SDL_HapticUpdateEffect(SDL_Haptic *haptic, int effect,
SDL_HapticEffect *data)
{
if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
return -1;
}
/* Can't change type dynamically. */
if (data->type != haptic->effects[effect].effect.type) {
return SDL_SetError("Haptic: Updating effect type is illegal.");
}
/* Updates the effect */
if (SDL_SYS_HapticUpdateEffect(haptic, &haptic->effects[effect], data) <
0) {
return -1;
}
SDL_memcpy(&haptic->effects[effect].effect, data,
sizeof(SDL_HapticEffect));
return 0;
}
/*
* Runs the haptic effect on the device.
*/
int SDL_HapticRunEffect(SDL_Haptic *haptic, int effect, Uint32 iterations)
{
if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
return -1;
}
/* Run the effect */
if (SDL_SYS_HapticRunEffect(haptic, &haptic->effects[effect], iterations) < 0) {
return -1;
}
return 0;
}
/*
* Stops the haptic effect on the device.
*/
int SDL_HapticStopEffect(SDL_Haptic *haptic, int effect)
{
if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
return -1;
}
/* Stop the effect */
if (SDL_SYS_HapticStopEffect(haptic, &haptic->effects[effect]) < 0) {
return -1;
}
return 0;
}
/*
* Gets rid of a haptic effect.
*/
void SDL_HapticDestroyEffect(SDL_Haptic *haptic, int effect)
{
if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
return;
}
/* Not allocated */
if (haptic->effects[effect].hweffect == NULL) {
return;
}
SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]);
}
/*
* Gets the status of a haptic effect.
*/
int SDL_HapticGetEffectStatus(SDL_Haptic *haptic, int effect)
{
if (!ValidHaptic(haptic) || !ValidEffect(haptic, effect)) {
return -1;
}
if (!(haptic->supported & SDL_HAPTIC_STATUS)) {
return SDL_SetError("Haptic: Device does not support status queries.");
}
return SDL_SYS_HapticGetEffectStatus(haptic, &haptic->effects[effect]);
}
/*
* Sets the global gain of the device.
*/
int SDL_HapticSetGain(SDL_Haptic *haptic, int gain)
{
const char *env;
int real_gain, max_gain;
if (!ValidHaptic(haptic)) {
return -1;
}
if (!(haptic->supported & SDL_HAPTIC_GAIN)) {
return SDL_SetError("Haptic: Device does not support setting gain.");
}
if ((gain < 0) || (gain > 100)) {
return SDL_SetError("Haptic: Gain must be between 0 and 100.");
}
/* We use the envvar to get the maximum gain. */
env = SDL_getenv("SDL_HAPTIC_GAIN_MAX");
if (env != NULL) {
max_gain = SDL_atoi(env);
/* Check for sanity. */
if (max_gain < 0) {
max_gain = 0;
} else if (max_gain > 100) {
max_gain = 100;
}
/* We'll scale it linearly with SDL_HAPTIC_GAIN_MAX */
real_gain = (gain * max_gain) / 100;
} else {
real_gain = gain;
}
if (SDL_SYS_HapticSetGain(haptic, real_gain) < 0) {
return -1;
}
return 0;
}
/*
* Makes the device autocenter, 0 disables.
*/
int SDL_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter)
{
if (!ValidHaptic(haptic)) {
return -1;
}
if (!(haptic->supported & SDL_HAPTIC_AUTOCENTER)) {
return SDL_SetError("Haptic: Device does not support setting autocenter.");
}
if ((autocenter < 0) || (autocenter > 100)) {
return SDL_SetError("Haptic: Autocenter must be between 0 and 100.");
}
if (SDL_SYS_HapticSetAutocenter(haptic, autocenter) < 0) {
return -1;
}
return 0;
}
/*
* Pauses the haptic device.
*/
int SDL_HapticPause(SDL_Haptic *haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}
if (!(haptic->supported & SDL_HAPTIC_PAUSE)) {
return SDL_SetError("Haptic: Device does not support setting pausing.");
}
return SDL_SYS_HapticPause(haptic);
}
/*
* Unpauses the haptic device.
*/
int SDL_HapticUnpause(SDL_Haptic *haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}
if (!(haptic->supported & SDL_HAPTIC_PAUSE)) {
return 0; /* Not going to be paused, so we pretend it's unpaused. */
}
return SDL_SYS_HapticUnpause(haptic);
}
/*
* Stops all the currently playing effects.
*/
int SDL_HapticStopAll(SDL_Haptic *haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}
return SDL_SYS_HapticStopAll(haptic);
}
/*
* Checks to see if rumble is supported.
*/
int SDL_HapticRumbleSupported(SDL_Haptic *haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}
/* Most things can use SINE, but XInput only has LEFTRIGHT. */
return (haptic->supported & (SDL_HAPTIC_SINE | SDL_HAPTIC_LEFTRIGHT)) != 0;
}
/*
* Initializes the haptic device for simple rumble playback.
*/
int SDL_HapticRumbleInit(SDL_Haptic *haptic)
{
SDL_HapticEffect *efx = &haptic->rumble_effect;
if (!ValidHaptic(haptic)) {
return -1;
}
/* Already allocated. */
if (haptic->rumble_id >= 0) {
return 0;
}
SDL_zerop(efx);
if (haptic->supported & SDL_HAPTIC_SINE) {
efx->type = SDL_HAPTIC_SINE;
efx->periodic.direction.type = SDL_HAPTIC_CARTESIAN;
efx->periodic.period = 1000;
efx->periodic.magnitude = 0x4000;
efx->periodic.length = 5000;
efx->periodic.attack_length = 0;
efx->periodic.fade_length = 0;
} else if (haptic->supported & SDL_HAPTIC_LEFTRIGHT) { /* XInput? */
efx->type = SDL_HAPTIC_LEFTRIGHT;
efx->leftright.length = 5000;
efx->leftright.large_magnitude = 0x4000;
efx->leftright.small_magnitude = 0x4000;
} else {
return SDL_SetError("Device doesn't support rumble");
}
haptic->rumble_id = SDL_HapticNewEffect(haptic, &haptic->rumble_effect);
if (haptic->rumble_id >= 0) {
return 0;
}
return -1;
}
/*
* Runs simple rumble on a haptic device
*/
int SDL_HapticRumblePlay(SDL_Haptic *haptic, float strength, Uint32 length)
{
SDL_HapticEffect *efx;
Sint16 magnitude;
if (!ValidHaptic(haptic)) {
return -1;
}
if (haptic->rumble_id < 0) {
return SDL_SetError("Haptic: Rumble effect not initialized on haptic device");
}
/* Clamp strength. */
if (strength > 1.0f) {
strength = 1.0f;
} else if (strength < 0.0f) {
strength = 0.0f;
}
magnitude = (Sint16)(32767.0f * strength);
efx = &haptic->rumble_effect;
if (efx->type == SDL_HAPTIC_SINE) {
efx->periodic.magnitude = magnitude;
efx->periodic.length = length;
} else if (efx->type == SDL_HAPTIC_LEFTRIGHT) {
efx->leftright.small_magnitude = efx->leftright.large_magnitude = magnitude;
efx->leftright.length = length;
} else {
SDL_assert(0 && "This should have been caught elsewhere");
}
if (SDL_HapticUpdateEffect(haptic, haptic->rumble_id, &haptic->rumble_effect) < 0) {
return -1;
}
return SDL_HapticRunEffect(haptic, haptic->rumble_id, 1);
}
/*
* Stops the simple rumble on a haptic device.
*/
int SDL_HapticRumbleStop(SDL_Haptic *haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}
if (haptic->rumble_id < 0) {
return SDL_SetError("Haptic: Rumble effect not initialized on haptic device");
}
return SDL_HapticStopEffect(haptic, haptic->rumble_id);
}
/* vi: set ts=4 sw=4 expandtab: */