🛠️🐜 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.

148 lines
5.0 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. /* Simple program to test the SDL joystick hotplugging */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "SDL.h"
  15. #if !defined SDL_JOYSTICK_DISABLED && !defined SDL_HAPTIC_DISABLED
  16. int main(int argc, char *argv[])
  17. {
  18. SDL_Joystick *joystick = NULL;
  19. SDL_Haptic *haptic = NULL;
  20. SDL_JoystickID instance = -1;
  21. SDL_bool keepGoing = SDL_TRUE;
  22. int i;
  23. SDL_bool enable_haptic = SDL_TRUE;
  24. Uint32 init_subsystems = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK;
  25. for (i = 1; i < argc; ++i) {
  26. if (SDL_strcasecmp(argv[i], "--nohaptic") == 0) {
  27. enable_haptic = SDL_FALSE;
  28. }
  29. }
  30. if (enable_haptic) {
  31. init_subsystems |= SDL_INIT_HAPTIC;
  32. }
  33. /* Enable standard application logging */
  34. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  35. SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
  36. /* Initialize SDL (Note: video is required to start event loop) */
  37. if (SDL_Init(init_subsystems) < 0) {
  38. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  39. exit(1);
  40. }
  41. /*
  42. //SDL_CreateWindow("Dummy", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 128, 128, 0);
  43. */
  44. SDL_Log("There are %d joysticks at startup\n", SDL_NumJoysticks());
  45. if (enable_haptic) {
  46. SDL_Log("There are %d haptic devices at startup\n", SDL_NumHaptics());
  47. }
  48. while (keepGoing) {
  49. SDL_Event event;
  50. while (SDL_PollEvent(&event)) {
  51. switch (event.type) {
  52. case SDL_QUIT:
  53. keepGoing = SDL_FALSE;
  54. break;
  55. case SDL_JOYDEVICEADDED:
  56. if (joystick != NULL) {
  57. SDL_Log("Only one joystick supported by this test\n");
  58. } else {
  59. joystick = SDL_JoystickOpen(event.jdevice.which);
  60. instance = SDL_JoystickInstanceID(joystick);
  61. SDL_Log("Joy Added : %" SDL_PRIs32 " : %s\n", event.jdevice.which, SDL_JoystickName(joystick));
  62. if (enable_haptic) {
  63. if (SDL_JoystickIsHaptic(joystick)) {
  64. haptic = SDL_HapticOpenFromJoystick(joystick);
  65. if (haptic) {
  66. SDL_Log("Joy Haptic Opened\n");
  67. if (SDL_HapticRumbleInit(haptic) != 0) {
  68. SDL_Log("Could not init Rumble!: %s\n", SDL_GetError());
  69. SDL_HapticClose(haptic);
  70. haptic = NULL;
  71. }
  72. } else {
  73. SDL_Log("Joy haptic open FAILED!: %s\n", SDL_GetError());
  74. }
  75. } else {
  76. SDL_Log("No haptic found\n");
  77. }
  78. }
  79. }
  80. break;
  81. case SDL_JOYDEVICEREMOVED:
  82. if (instance == event.jdevice.which) {
  83. SDL_Log("Joy Removed: %" SDL_PRIs32 "\n", event.jdevice.which);
  84. instance = -1;
  85. if (enable_haptic && haptic) {
  86. SDL_HapticClose(haptic);
  87. haptic = NULL;
  88. }
  89. SDL_JoystickClose(joystick);
  90. joystick = NULL;
  91. } else {
  92. SDL_Log("Unknown joystick diconnected\n");
  93. }
  94. break;
  95. case SDL_JOYAXISMOTION:
  96. /*
  97. // SDL_Log("Axis Move: %d\n", event.jaxis.axis);
  98. */
  99. if (enable_haptic) {
  100. SDL_HapticRumblePlay(haptic, 0.25, 250);
  101. }
  102. break;
  103. case SDL_JOYBUTTONDOWN:
  104. SDL_Log("Button Press: %d\n", event.jbutton.button);
  105. if (enable_haptic && haptic) {
  106. SDL_HapticRumblePlay(haptic, 0.25, 250);
  107. }
  108. if (event.jbutton.button == 0) {
  109. SDL_Log("Exiting due to button press of button 0\n");
  110. keepGoing = SDL_FALSE;
  111. }
  112. break;
  113. case SDL_JOYBUTTONUP:
  114. SDL_Log("Button Release: %d\n", event.jbutton.button);
  115. break;
  116. }
  117. }
  118. }
  119. SDL_Quit();
  120. return 0;
  121. }
  122. #else
  123. int
  124. main(int argc, char *argv[])
  125. {
  126. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick and haptic support.\n");
  127. return 1;
  128. }
  129. #endif