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

162 lines
5.4 KiB

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