SDL_virtualjoystick.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if defined(SDL_JOYSTICK_VIRTUAL)
  20. /* This is the virtual implementation of the SDL joystick API */
  21. #include "SDL_virtualjoystick_c.h"
  22. #include "../SDL_sysjoystick.h"
  23. #include "../SDL_joystick_c.h"
  24. extern SDL_JoystickDriver SDL_VIRTUAL_JoystickDriver;
  25. static joystick_hwdata * g_VJoys = NULL;
  26. static joystick_hwdata *
  27. VIRTUAL_HWDataForIndex(int device_index)
  28. {
  29. joystick_hwdata *vjoy = g_VJoys;
  30. while (vjoy) {
  31. if (device_index == 0)
  32. break;
  33. --device_index;
  34. vjoy = vjoy->next;
  35. }
  36. return vjoy;
  37. }
  38. static void
  39. VIRTUAL_FreeHWData(joystick_hwdata *hwdata)
  40. {
  41. joystick_hwdata * cur = g_VJoys;
  42. joystick_hwdata * prev = NULL;
  43. if (!hwdata) {
  44. return;
  45. }
  46. if (hwdata->axes) {
  47. SDL_free((void *)hwdata->axes);
  48. hwdata->axes = NULL;
  49. }
  50. if (hwdata->buttons) {
  51. SDL_free((void *)hwdata->buttons);
  52. hwdata->buttons = NULL;
  53. }
  54. if (hwdata->hats) {
  55. SDL_free(hwdata->hats);
  56. hwdata->hats = NULL;
  57. }
  58. /* Remove hwdata from SDL-global list */
  59. while (cur) {
  60. if (hwdata == cur) {
  61. if (prev) {
  62. prev->next = cur->next;
  63. } else {
  64. g_VJoys = cur->next;
  65. }
  66. break;
  67. }
  68. prev = cur;
  69. cur = cur->next;
  70. }
  71. SDL_free(hwdata);
  72. }
  73. int
  74. SDL_JoystickAttachVirtualInner(SDL_JoystickType type,
  75. int naxes,
  76. int nbuttons,
  77. int nhats)
  78. {
  79. joystick_hwdata *hwdata = NULL;
  80. int device_index = -1;
  81. hwdata = SDL_calloc(1, sizeof(joystick_hwdata));
  82. if (!hwdata) {
  83. VIRTUAL_FreeHWData(hwdata);
  84. return SDL_OutOfMemory();
  85. }
  86. hwdata->naxes = naxes;
  87. hwdata->nbuttons = nbuttons;
  88. hwdata->nhats = nhats;
  89. hwdata->name = "Virtual Joystick";
  90. /* Note that this is a Virtual device and what subtype it is */
  91. hwdata->guid.data[14] = 'v';
  92. hwdata->guid.data[15] = (Uint8)type;
  93. /* Allocate fields for different control-types */
  94. if (naxes > 0) {
  95. hwdata->axes = SDL_calloc(naxes, sizeof(Sint16));
  96. if (!hwdata->axes) {
  97. VIRTUAL_FreeHWData(hwdata);
  98. return SDL_OutOfMemory();
  99. }
  100. }
  101. if (nbuttons > 0) {
  102. hwdata->buttons = SDL_calloc(nbuttons, sizeof(Uint8));
  103. if (!hwdata->buttons) {
  104. VIRTUAL_FreeHWData(hwdata);
  105. return SDL_OutOfMemory();
  106. }
  107. }
  108. if (nhats > 0) {
  109. hwdata->hats = SDL_calloc(nhats, sizeof(Uint8));
  110. if (!hwdata->hats) {
  111. VIRTUAL_FreeHWData(hwdata);
  112. return SDL_OutOfMemory();
  113. }
  114. }
  115. /* Allocate an instance ID for this device */
  116. hwdata->instance_id = SDL_GetNextJoystickInstanceID();
  117. /* Add virtual joystick to SDL-global lists */
  118. hwdata->next = g_VJoys;
  119. g_VJoys = hwdata;
  120. SDL_PrivateJoystickAdded(hwdata->instance_id);
  121. /* Return the new virtual-device's index */
  122. device_index = SDL_JoystickGetDeviceIndexFromInstanceID(hwdata->instance_id);
  123. return device_index;
  124. }
  125. int
  126. SDL_JoystickDetachVirtualInner(int device_index)
  127. {
  128. SDL_JoystickID instance_id;
  129. joystick_hwdata *hwdata = VIRTUAL_HWDataForIndex(device_index);
  130. if (!hwdata) {
  131. return SDL_SetError("Virtual joystick data not found");
  132. }
  133. instance_id = hwdata->instance_id;
  134. VIRTUAL_FreeHWData(hwdata);
  135. SDL_PrivateJoystickRemoved(instance_id);
  136. return 0;
  137. }
  138. int
  139. SDL_JoystickSetVirtualAxisInner(SDL_Joystick * joystick, int axis, Sint16 value)
  140. {
  141. joystick_hwdata *hwdata;
  142. SDL_LockJoysticks();
  143. if (!joystick || !joystick->hwdata) {
  144. SDL_UnlockJoysticks();
  145. return SDL_SetError("Invalid joystick");
  146. }
  147. hwdata = (joystick_hwdata *)joystick->hwdata;
  148. if (axis < 0 || axis >= hwdata->nbuttons) {
  149. SDL_UnlockJoysticks();
  150. return SDL_SetError("Invalid axis index");
  151. }
  152. hwdata->axes[axis] = value;
  153. SDL_UnlockJoysticks();
  154. return 0;
  155. }
  156. int
  157. SDL_JoystickSetVirtualButtonInner(SDL_Joystick * joystick, int button, Uint8 value)
  158. {
  159. joystick_hwdata *hwdata;
  160. SDL_LockJoysticks();
  161. if (!joystick || !joystick->hwdata) {
  162. SDL_UnlockJoysticks();
  163. return SDL_SetError("Invalid joystick");
  164. }
  165. hwdata = (joystick_hwdata *)joystick->hwdata;
  166. if (button < 0 || button >= hwdata->nbuttons) {
  167. SDL_UnlockJoysticks();
  168. return SDL_SetError("Invalid button index");
  169. }
  170. hwdata->buttons[button] = value;
  171. SDL_UnlockJoysticks();
  172. return 0;
  173. }
  174. int
  175. SDL_JoystickSetVirtualHatInner(SDL_Joystick * joystick, int hat, Uint8 value)
  176. {
  177. joystick_hwdata *hwdata;
  178. SDL_LockJoysticks();
  179. if (!joystick || !joystick->hwdata) {
  180. SDL_UnlockJoysticks();
  181. return SDL_SetError("Invalid joystick");
  182. }
  183. hwdata = (joystick_hwdata *)joystick->hwdata;
  184. if (hat < 0 || hat >= hwdata->nbuttons) {
  185. SDL_UnlockJoysticks();
  186. return SDL_SetError("Invalid hat index");
  187. }
  188. hwdata->hats[hat] = value;
  189. SDL_UnlockJoysticks();
  190. return 0;
  191. }
  192. static int
  193. VIRTUAL_JoystickInit(void)
  194. {
  195. return 0;
  196. }
  197. static int
  198. VIRTUAL_JoystickGetCount(void)
  199. {
  200. int count = 0;
  201. joystick_hwdata *cur = g_VJoys;
  202. while (cur) {
  203. ++count;
  204. cur = cur->next;
  205. }
  206. return count;
  207. }
  208. static void
  209. VIRTUAL_JoystickDetect(void)
  210. {
  211. }
  212. static const char *
  213. VIRTUAL_JoystickGetDeviceName(int device_index)
  214. {
  215. joystick_hwdata *hwdata = VIRTUAL_HWDataForIndex(device_index);
  216. if (!hwdata) {
  217. return NULL;
  218. }
  219. return hwdata->name ? hwdata->name : "";
  220. }
  221. static int
  222. VIRTUAL_JoystickGetDevicePlayerIndex(int device_index)
  223. {
  224. return -1;
  225. }
  226. static void
  227. VIRTUAL_JoystickSetDevicePlayerIndex(int device_index, int player_index)
  228. {
  229. }
  230. static SDL_JoystickGUID
  231. VIRTUAL_JoystickGetDeviceGUID(int device_index)
  232. {
  233. joystick_hwdata *hwdata = VIRTUAL_HWDataForIndex(device_index);
  234. if (!hwdata) {
  235. SDL_JoystickGUID guid;
  236. SDL_zero(guid);
  237. return guid;
  238. }
  239. return hwdata->guid;
  240. }
  241. static SDL_JoystickID
  242. VIRTUAL_JoystickGetDeviceInstanceID(int device_index)
  243. {
  244. joystick_hwdata *hwdata = VIRTUAL_HWDataForIndex(device_index);
  245. if (!hwdata) {
  246. return -1;
  247. }
  248. return hwdata->instance_id;
  249. }
  250. static int
  251. VIRTUAL_JoystickOpen(SDL_Joystick * joystick, int device_index)
  252. {
  253. joystick_hwdata *hwdata = VIRTUAL_HWDataForIndex(device_index);
  254. if (!hwdata) {
  255. return SDL_SetError("No such device");
  256. }
  257. if (hwdata->opened) {
  258. return SDL_SetError("Joystick already opened");
  259. }
  260. joystick->instance_id = hwdata->instance_id;
  261. joystick->hwdata = hwdata;
  262. joystick->naxes = hwdata->naxes;
  263. joystick->nbuttons = hwdata->nbuttons;
  264. joystick->nhats = hwdata->nhats;
  265. hwdata->opened = SDL_TRUE;
  266. return 0;
  267. }
  268. static int
  269. VIRTUAL_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  270. {
  271. return SDL_Unsupported();
  272. }
  273. static int
  274. VIRTUAL_JoystickRumbleTriggers(SDL_Joystick * joystick, Uint16 left_rumble, Uint16 right_rumble)
  275. {
  276. return SDL_Unsupported();
  277. }
  278. static SDL_bool
  279. VIRTUAL_JoystickHasLED(SDL_Joystick * joystick)
  280. {
  281. return SDL_FALSE;
  282. }
  283. static int
  284. VIRTUAL_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 blue)
  285. {
  286. return SDL_Unsupported();
  287. }
  288. static int
  289. VIRTUAL_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
  290. {
  291. return SDL_Unsupported();
  292. }
  293. static void
  294. VIRTUAL_JoystickUpdate(SDL_Joystick * joystick)
  295. {
  296. joystick_hwdata *hwdata;
  297. int i;
  298. if (!joystick) {
  299. return;
  300. }
  301. if (!joystick->hwdata) {
  302. return;
  303. }
  304. hwdata = (joystick_hwdata *)joystick->hwdata;
  305. for (i = 0; i < hwdata->naxes; ++i) {
  306. SDL_PrivateJoystickAxis(joystick, i, hwdata->axes[i]);
  307. }
  308. for (i = 0; i < hwdata->nbuttons; ++i) {
  309. SDL_PrivateJoystickButton(joystick, i, hwdata->buttons[i]);
  310. }
  311. for (i = 0; i < hwdata->nhats; ++i) {
  312. SDL_PrivateJoystickHat(joystick, i, hwdata->hats[i]);
  313. }
  314. }
  315. static void
  316. VIRTUAL_JoystickClose(SDL_Joystick * joystick)
  317. {
  318. joystick_hwdata *hwdata;
  319. if (!joystick) {
  320. return;
  321. }
  322. if (!joystick->hwdata) {
  323. return;
  324. }
  325. hwdata = (joystick_hwdata *)joystick->hwdata;
  326. hwdata->opened = SDL_FALSE;
  327. }
  328. static void
  329. VIRTUAL_JoystickQuit(void)
  330. {
  331. while (g_VJoys) {
  332. VIRTUAL_FreeHWData(g_VJoys);
  333. }
  334. }
  335. static SDL_bool
  336. VIRTUAL_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
  337. {
  338. return SDL_FALSE;
  339. }
  340. SDL_JoystickDriver SDL_VIRTUAL_JoystickDriver =
  341. {
  342. VIRTUAL_JoystickInit,
  343. VIRTUAL_JoystickGetCount,
  344. VIRTUAL_JoystickDetect,
  345. VIRTUAL_JoystickGetDeviceName,
  346. VIRTUAL_JoystickGetDevicePlayerIndex,
  347. VIRTUAL_JoystickSetDevicePlayerIndex,
  348. VIRTUAL_JoystickGetDeviceGUID,
  349. VIRTUAL_JoystickGetDeviceInstanceID,
  350. VIRTUAL_JoystickOpen,
  351. VIRTUAL_JoystickRumble,
  352. VIRTUAL_JoystickRumbleTriggers,
  353. VIRTUAL_JoystickHasLED,
  354. VIRTUAL_JoystickSetLED,
  355. VIRTUAL_JoystickSetSensorsEnabled,
  356. VIRTUAL_JoystickUpdate,
  357. VIRTUAL_JoystickClose,
  358. VIRTUAL_JoystickQuit,
  359. VIRTUAL_JoystickGetGamepadMapping
  360. };
  361. #endif /* SDL_JOYSTICK_VIRTUAL */
  362. /* vi: set ts=4 sw=4 expandtab: */