🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
5.4 KiB

  1. /*
  2. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /*
  11. Copyright (c) 2011, Edgar Simo Serra
  12. All rights reserved.
  13. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  14. * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  15. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  16. * Neither the name of the Simple Directmedia Layer (SDL) nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18. */
  19. /*
  20. * includes
  21. */
  22. #include "SDL.h"
  23. #ifndef SDL_HAPTIC_DISABLED
  24. static SDL_Haptic *haptic;
  25. /**
  26. * @brief The entry point of this force feedback demo.
  27. * @param[in] argc Number of arguments.
  28. * @param[in] argv Array of argc arguments.
  29. */
  30. int main(int argc, char **argv)
  31. {
  32. int i;
  33. char *name;
  34. int index;
  35. /* Enable standard application logging */
  36. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  37. name = NULL;
  38. index = -1;
  39. if (argc > 1) {
  40. size_t l;
  41. name = argv[1];
  42. if ((SDL_strcmp(name, "--help") == 0) || (SDL_strcmp(name, "-h") == 0)) {
  43. SDL_Log("USAGE: %s [device]\n"
  44. "If device is a two-digit number it'll use it as an index, otherwise\n"
  45. "it'll use it as if it were part of the device's name.\n",
  46. argv[0]);
  47. return 0;
  48. }
  49. l = SDL_strlen(name);
  50. if ((l < 3) && SDL_isdigit(name[0]) && ((l == 1) || SDL_isdigit(name[1]))) {
  51. index = SDL_atoi(name);
  52. name = NULL;
  53. }
  54. }
  55. /* Initialize the force feedbackness */
  56. SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK |
  57. SDL_INIT_HAPTIC);
  58. SDL_Log("%d Haptic devices detected.\n", SDL_NumHaptics());
  59. if (SDL_NumHaptics() > 0) {
  60. /* We'll just use index or the first force feedback device found */
  61. if (name == NULL) {
  62. i = (index != -1) ? index : 0;
  63. }
  64. /* Try to find matching device */
  65. else {
  66. for (i = 0; i < SDL_NumHaptics(); i++) {
  67. if (SDL_strstr(SDL_HapticName(i), name) != NULL) {
  68. break;
  69. }
  70. }
  71. if (i >= SDL_NumHaptics()) {
  72. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n",
  73. name);
  74. return 1;
  75. }
  76. }
  77. haptic = SDL_HapticOpen(i);
  78. if (haptic == NULL) {
  79. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n",
  80. SDL_GetError());
  81. return 1;
  82. }
  83. SDL_Log("Device: %s\n", SDL_HapticName(i));
  84. } else {
  85. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!\n");
  86. return 1;
  87. }
  88. /* We only want force feedback errors. */
  89. SDL_ClearError();
  90. if (SDL_HapticRumbleSupported(haptic) == SDL_FALSE) {
  91. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Rumble not supported!\n");
  92. return 1;
  93. }
  94. if (SDL_HapticRumbleInit(haptic) != 0) {
  95. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize rumble: %s\n", SDL_GetError());
  96. return 1;
  97. }
  98. SDL_Log("Playing 2 second rumble at 0.5 magnitude.\n");
  99. if (SDL_HapticRumblePlay(haptic, 0.5, 5000) != 0) {
  100. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to play rumble: %s\n", SDL_GetError());
  101. return 1;
  102. }
  103. SDL_Delay(2000);
  104. SDL_Log("Stopping rumble.\n");
  105. SDL_HapticRumbleStop(haptic);
  106. SDL_Delay(2000);
  107. SDL_Log("Playing 2 second rumble at 0.3 magnitude.\n");
  108. if (SDL_HapticRumblePlay(haptic, 0.3f, 5000) != 0) {
  109. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to play rumble: %s\n", SDL_GetError());
  110. return 1;
  111. }
  112. SDL_Delay(2000);
  113. /* Quit */
  114. if (haptic != NULL) {
  115. SDL_HapticClose(haptic);
  116. }
  117. SDL_Quit();
  118. return 0;
  119. }
  120. #else
  121. int
  122. main(int argc, char *argv[])
  123. {
  124. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Haptic support.\n");
  125. return 1;
  126. }
  127. #endif