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

375 lines
14 KiB

  1. /*
  2. Copyright (c) 2008, Edgar Simo Serra
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  6. * 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.
  7. * 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.
  8. 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.
  9. */
  10. /*
  11. * includes
  12. */
  13. #include "SDL.h"
  14. #ifndef SDL_HAPTIC_DISABLED
  15. static SDL_Haptic *haptic;
  16. /*
  17. * prototypes
  18. */
  19. static void abort_execution(void);
  20. static void HapticPrintSupported(SDL_Haptic *);
  21. /**
  22. * @brief The entry point of this force feedback demo.
  23. * @param[in] argc Number of arguments.
  24. * @param[in] argv Array of argc arguments.
  25. */
  26. int main(int argc, char **argv)
  27. {
  28. int i;
  29. char *name;
  30. int index;
  31. SDL_HapticEffect efx[9];
  32. int id[9];
  33. int nefx;
  34. unsigned int supported;
  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. name = argv[1];
  41. if ((SDL_strcmp(name, "--help") == 0) || (SDL_strcmp(name, "-h") == 0)) {
  42. SDL_Log("USAGE: %s [device]\n"
  43. "If device is a two-digit number it'll use it as an index, otherwise\n"
  44. "it'll use it as if it were part of the device's name.\n",
  45. argv[0]);
  46. return 0;
  47. }
  48. i = (int)SDL_strlen(name);
  49. if ((i < 3) && SDL_isdigit(name[0]) && ((i == 1) || SDL_isdigit(name[1]))) {
  50. index = SDL_atoi(name);
  51. name = NULL;
  52. }
  53. }
  54. /* Initialize the force feedbackness */
  55. SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK |
  56. SDL_INIT_HAPTIC);
  57. SDL_Log("%d Haptic devices detected.\n", SDL_NumHaptics());
  58. if (SDL_NumHaptics() > 0) {
  59. /* We'll just use index or the first force feedback device found */
  60. if (name == NULL) {
  61. i = (index != -1) ? index : 0;
  62. }
  63. /* Try to find matching device */
  64. else {
  65. for (i = 0; i < SDL_NumHaptics(); i++) {
  66. if (SDL_strstr(SDL_HapticName(i), name) != NULL) {
  67. break;
  68. }
  69. }
  70. if (i >= SDL_NumHaptics()) {
  71. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n",
  72. name);
  73. return 1;
  74. }
  75. }
  76. haptic = SDL_HapticOpen(i);
  77. if (haptic == NULL) {
  78. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n",
  79. SDL_GetError());
  80. return 1;
  81. }
  82. SDL_Log("Device: %s\n", SDL_HapticName(i));
  83. HapticPrintSupported(haptic);
  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. /* Create effects. */
  91. SDL_memset(efx, 0, sizeof(efx));
  92. nefx = 0;
  93. supported = SDL_HapticQuery(haptic);
  94. SDL_Log("\nUploading effects\n");
  95. /* First we'll try a SINE effect. */
  96. if (supported & SDL_HAPTIC_SINE) {
  97. SDL_Log(" effect %d: Sine Wave\n", nefx);
  98. efx[nefx].type = SDL_HAPTIC_SINE;
  99. efx[nefx].periodic.period = 1000;
  100. efx[nefx].periodic.magnitude = -0x2000; /* Negative magnitude and ... */
  101. efx[nefx].periodic.phase = 18000; /* ... 180 degrees phase shift => cancel eachother */
  102. efx[nefx].periodic.length = 5000;
  103. efx[nefx].periodic.attack_length = 1000;
  104. efx[nefx].periodic.fade_length = 1000;
  105. id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
  106. if (id[nefx] < 0) {
  107. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
  108. abort_execution();
  109. }
  110. nefx++;
  111. }
  112. /* Now we'll try a SAWTOOTHUP */
  113. if (supported & SDL_HAPTIC_SAWTOOTHUP) {
  114. SDL_Log(" effect %d: Sawtooth Up\n", nefx);
  115. efx[nefx].type = SDL_HAPTIC_SAWTOOTHUP;
  116. efx[nefx].periodic.period = 500;
  117. efx[nefx].periodic.magnitude = 0x5000;
  118. efx[nefx].periodic.length = 5000;
  119. efx[nefx].periodic.attack_length = 1000;
  120. efx[nefx].periodic.fade_length = 1000;
  121. id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
  122. if (id[nefx] < 0) {
  123. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
  124. abort_execution();
  125. }
  126. nefx++;
  127. }
  128. /* Now the classical constant effect. */
  129. if (supported & SDL_HAPTIC_CONSTANT) {
  130. SDL_Log(" effect %d: Constant Force\n", nefx);
  131. efx[nefx].type = SDL_HAPTIC_CONSTANT;
  132. efx[nefx].constant.direction.type = SDL_HAPTIC_POLAR;
  133. efx[nefx].constant.direction.dir[0] = 20000; /* Force comes from the south-west. */
  134. efx[nefx].constant.length = 5000;
  135. efx[nefx].constant.level = 0x6000;
  136. efx[nefx].constant.attack_length = 1000;
  137. efx[nefx].constant.fade_length = 1000;
  138. id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
  139. if (id[nefx] < 0) {
  140. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
  141. abort_execution();
  142. }
  143. nefx++;
  144. }
  145. /* The cute spring effect. */
  146. if (supported & SDL_HAPTIC_SPRING) {
  147. SDL_Log(" effect %d: Condition Spring\n", nefx);
  148. efx[nefx].type = SDL_HAPTIC_SPRING;
  149. efx[nefx].condition.length = 5000;
  150. for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
  151. efx[nefx].condition.right_sat[i] = 0xFFFF;
  152. efx[nefx].condition.left_sat[i] = 0xFFFF;
  153. efx[nefx].condition.right_coeff[i] = 0x2000;
  154. efx[nefx].condition.left_coeff[i] = 0x2000;
  155. efx[nefx].condition.center[i] = 0x1000; /* Displace the center for it to move. */
  156. }
  157. id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
  158. if (id[nefx] < 0) {
  159. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
  160. abort_execution();
  161. }
  162. nefx++;
  163. }
  164. /* The interesting damper effect. */
  165. if (supported & SDL_HAPTIC_DAMPER) {
  166. SDL_Log(" effect %d: Condition Damper\n", nefx);
  167. efx[nefx].type = SDL_HAPTIC_DAMPER;
  168. efx[nefx].condition.length = 5000;
  169. for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
  170. efx[nefx].condition.right_sat[i] = 0xFFFF;
  171. efx[nefx].condition.left_sat[i] = 0xFFFF;
  172. efx[nefx].condition.right_coeff[i] = 0x2000;
  173. efx[nefx].condition.left_coeff[i] = 0x2000;
  174. }
  175. id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
  176. if (id[nefx] < 0) {
  177. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
  178. abort_execution();
  179. }
  180. nefx++;
  181. }
  182. /* The pretty awesome inertia effect. */
  183. if (supported & SDL_HAPTIC_INERTIA) {
  184. SDL_Log(" effect %d: Condition Inertia\n", nefx);
  185. efx[nefx].type = SDL_HAPTIC_INERTIA;
  186. efx[nefx].condition.length = 5000;
  187. for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
  188. efx[nefx].condition.right_sat[i] = 0xFFFF;
  189. efx[nefx].condition.left_sat[i] = 0xFFFF;
  190. efx[nefx].condition.right_coeff[i] = 0x2000;
  191. efx[nefx].condition.left_coeff[i] = 0x2000;
  192. efx[nefx].condition.deadband[i] = 0x1000; /* 1/16th of axis-range around the center is 'dead'. */
  193. }
  194. id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
  195. if (id[nefx] < 0) {
  196. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
  197. abort_execution();
  198. }
  199. nefx++;
  200. }
  201. /* The hot friction effect. */
  202. if (supported & SDL_HAPTIC_FRICTION) {
  203. SDL_Log(" effect %d: Condition Friction\n", nefx);
  204. efx[nefx].type = SDL_HAPTIC_FRICTION;
  205. efx[nefx].condition.length = 5000;
  206. for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
  207. efx[nefx].condition.right_sat[i] = 0xFFFF;
  208. efx[nefx].condition.left_sat[i] = 0xFFFF;
  209. efx[nefx].condition.right_coeff[i] = 0x2000;
  210. efx[nefx].condition.left_coeff[i] = 0x2000;
  211. }
  212. id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
  213. if (id[nefx] < 0) {
  214. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
  215. abort_execution();
  216. }
  217. nefx++;
  218. }
  219. /* Now we'll try a ramp effect */
  220. if (supported & SDL_HAPTIC_RAMP) {
  221. SDL_Log(" effect %d: Ramp\n", nefx);
  222. efx[nefx].type = SDL_HAPTIC_RAMP;
  223. efx[nefx].ramp.direction.type = SDL_HAPTIC_CARTESIAN;
  224. efx[nefx].ramp.direction.dir[0] = 1; /* Force comes from */
  225. efx[nefx].ramp.direction.dir[1] = -1; /* the north-east. */
  226. efx[nefx].ramp.length = 5000;
  227. efx[nefx].ramp.start = 0x4000;
  228. efx[nefx].ramp.end = -0x4000;
  229. efx[nefx].ramp.attack_length = 1000;
  230. efx[nefx].ramp.fade_length = 1000;
  231. id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
  232. if (id[nefx] < 0) {
  233. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
  234. abort_execution();
  235. }
  236. nefx++;
  237. }
  238. /* Finally we'll try a left/right effect. */
  239. if (supported & SDL_HAPTIC_LEFTRIGHT) {
  240. SDL_Log(" effect %d: Left/Right\n", nefx);
  241. efx[nefx].type = SDL_HAPTIC_LEFTRIGHT;
  242. efx[nefx].leftright.length = 5000;
  243. efx[nefx].leftright.large_magnitude = 0x3000;
  244. efx[nefx].leftright.small_magnitude = 0xFFFF;
  245. id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
  246. if (id[nefx] < 0) {
  247. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
  248. abort_execution();
  249. }
  250. nefx++;
  251. }
  252. SDL_Log("\nNow playing effects for 5 seconds each with 1 second delay between\n");
  253. for (i = 0; i < nefx; i++) {
  254. SDL_Log(" Playing effect %d\n", i);
  255. SDL_HapticRunEffect(haptic, id[i], 1);
  256. SDL_Delay(6000); /* Effects only have length 5000 */
  257. }
  258. /* Quit */
  259. if (haptic != NULL) {
  260. SDL_HapticClose(haptic);
  261. }
  262. SDL_Quit();
  263. return 0;
  264. }
  265. /*
  266. * Cleans up a bit.
  267. */
  268. static void
  269. abort_execution(void)
  270. {
  271. SDL_Log("\nAborting program execution.\n");
  272. SDL_HapticClose(haptic);
  273. SDL_Quit();
  274. exit(1);
  275. }
  276. /*
  277. * Displays information about the haptic device.
  278. */
  279. static void
  280. HapticPrintSupported(SDL_Haptic *ptr)
  281. {
  282. unsigned int supported;
  283. supported = SDL_HapticQuery(ptr);
  284. SDL_Log(" Supported effects [%d effects, %d playing]:\n",
  285. SDL_HapticNumEffects(ptr), SDL_HapticNumEffectsPlaying(ptr));
  286. if (supported & SDL_HAPTIC_CONSTANT) {
  287. SDL_Log(" constant\n");
  288. }
  289. if (supported & SDL_HAPTIC_SINE) {
  290. SDL_Log(" sine\n");
  291. }
  292. /* !!! FIXME: put this back when we have more bits in 2.1 */
  293. /* if (supported & SDL_HAPTIC_SQUARE)
  294. SDL_Log(" square\n"); */
  295. if (supported & SDL_HAPTIC_TRIANGLE) {
  296. SDL_Log(" triangle\n");
  297. }
  298. if (supported & SDL_HAPTIC_SAWTOOTHUP) {
  299. SDL_Log(" sawtoothup\n");
  300. }
  301. if (supported & SDL_HAPTIC_SAWTOOTHDOWN) {
  302. SDL_Log(" sawtoothdown\n");
  303. }
  304. if (supported & SDL_HAPTIC_RAMP) {
  305. SDL_Log(" ramp\n");
  306. }
  307. if (supported & SDL_HAPTIC_FRICTION) {
  308. SDL_Log(" friction\n");
  309. }
  310. if (supported & SDL_HAPTIC_SPRING) {
  311. SDL_Log(" spring\n");
  312. }
  313. if (supported & SDL_HAPTIC_DAMPER) {
  314. SDL_Log(" damper\n");
  315. }
  316. if (supported & SDL_HAPTIC_INERTIA) {
  317. SDL_Log(" inertia\n");
  318. }
  319. if (supported & SDL_HAPTIC_CUSTOM) {
  320. SDL_Log(" custom\n");
  321. }
  322. if (supported & SDL_HAPTIC_LEFTRIGHT) {
  323. SDL_Log(" left/right\n");
  324. }
  325. SDL_Log(" Supported capabilities:\n");
  326. if (supported & SDL_HAPTIC_GAIN) {
  327. SDL_Log(" gain\n");
  328. }
  329. if (supported & SDL_HAPTIC_AUTOCENTER) {
  330. SDL_Log(" autocenter\n");
  331. }
  332. if (supported & SDL_HAPTIC_STATUS) {
  333. SDL_Log(" status\n");
  334. }
  335. }
  336. #else
  337. int
  338. main(int argc, char *argv[])
  339. {
  340. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Haptic support.\n");
  341. return 1;
  342. }
  343. #endif