testhotplug.c 5.4 KB

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