1
0

SDL_steam_virtual_gamepad.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. #include "SDL_joystick_c.h"
  20. #include "SDL_steam_virtual_gamepad.h"
  21. #ifdef SDL_PLATFORM_WIN32
  22. #include "../core/windows/SDL_windows.h"
  23. #else
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #endif
  27. #define SDL_HINT_STEAM_VIRTUAL_GAMEPAD_INFO_FILE "SteamVirtualGamepadInfo"
  28. static char *SDL_steam_virtual_gamepad_info_file SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
  29. static Uint64 SDL_steam_virtual_gamepad_info_file_mtime SDL_GUARDED_BY(SDL_joystick_lock) = 0;
  30. static Uint64 SDL_steam_virtual_gamepad_info_check_time SDL_GUARDED_BY(SDL_joystick_lock) = 0;
  31. static SDL_SteamVirtualGamepadInfo **SDL_steam_virtual_gamepad_info SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
  32. static int SDL_steam_virtual_gamepad_info_count SDL_GUARDED_BY(SDL_joystick_lock) = 0;
  33. static Uint64 GetFileModificationTime(const char *file)
  34. {
  35. Uint64 modification_time = 0;
  36. #ifdef SDL_PLATFORM_WIN32
  37. WCHAR *wFile = WIN_UTF8ToStringW(file);
  38. if (wFile) {
  39. HANDLE hFile = CreateFileW(wFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
  40. if (hFile != INVALID_HANDLE_VALUE) {
  41. FILETIME last_write_time;
  42. if (GetFileTime(hFile, NULL, NULL, &last_write_time)) {
  43. modification_time = last_write_time.dwHighDateTime;
  44. modification_time <<= 32;
  45. modification_time |= last_write_time.dwLowDateTime;
  46. }
  47. CloseHandle(hFile);
  48. }
  49. SDL_free(wFile);
  50. }
  51. #else
  52. struct stat sb;
  53. if (stat(file, &sb) == 0) {
  54. modification_time = (Uint64)sb.st_mtime;
  55. }
  56. #endif
  57. return modification_time;
  58. }
  59. static void SDL_FreeSteamVirtualGamepadInfo(void)
  60. {
  61. int i;
  62. SDL_AssertJoysticksLocked();
  63. for (i = 0; i < SDL_steam_virtual_gamepad_info_count; ++i) {
  64. SDL_SteamVirtualGamepadInfo *entry = SDL_steam_virtual_gamepad_info[i];
  65. if (entry) {
  66. SDL_free(entry->name);
  67. SDL_free(entry);
  68. }
  69. }
  70. SDL_free(SDL_steam_virtual_gamepad_info);
  71. SDL_steam_virtual_gamepad_info = NULL;
  72. SDL_steam_virtual_gamepad_info_count = 0;
  73. }
  74. static void AddVirtualGamepadInfo(int slot, SDL_SteamVirtualGamepadInfo *info)
  75. {
  76. SDL_SteamVirtualGamepadInfo *new_info;
  77. SDL_AssertJoysticksLocked();
  78. if (slot < 0) {
  79. return;
  80. }
  81. if (slot >= SDL_steam_virtual_gamepad_info_count) {
  82. SDL_SteamVirtualGamepadInfo **slots = (SDL_SteamVirtualGamepadInfo **)SDL_realloc(SDL_steam_virtual_gamepad_info, (slot + 1)*sizeof(*SDL_steam_virtual_gamepad_info));
  83. if (!slots) {
  84. return;
  85. }
  86. while (SDL_steam_virtual_gamepad_info_count <= slot) {
  87. slots[SDL_steam_virtual_gamepad_info_count++] = NULL;
  88. }
  89. SDL_steam_virtual_gamepad_info = slots;
  90. }
  91. if (SDL_steam_virtual_gamepad_info[slot]) {
  92. /* We already have this slot info */
  93. return;
  94. }
  95. new_info = (SDL_SteamVirtualGamepadInfo *)SDL_malloc(sizeof(*new_info));
  96. if (!new_info) {
  97. return;
  98. }
  99. SDL_copyp(new_info, info);
  100. SDL_steam_virtual_gamepad_info[slot] = new_info;
  101. SDL_zerop(info);
  102. }
  103. void SDL_InitSteamVirtualGamepadInfo(void)
  104. {
  105. const char *file;
  106. SDL_AssertJoysticksLocked();
  107. file = SDL_GetHint(SDL_HINT_STEAM_VIRTUAL_GAMEPAD_INFO_FILE);
  108. if (file && *file) {
  109. SDL_steam_virtual_gamepad_info_file = SDL_strdup(file);
  110. }
  111. SDL_UpdateSteamVirtualGamepadInfo();
  112. }
  113. SDL_bool SDL_SteamVirtualGamepadEnabled(void)
  114. {
  115. SDL_AssertJoysticksLocked();
  116. return (SDL_steam_virtual_gamepad_info != NULL);
  117. }
  118. SDL_bool SDL_UpdateSteamVirtualGamepadInfo(void)
  119. {
  120. const int UPDATE_CHECK_INTERVAL_MS = 3000;
  121. Uint64 now;
  122. Uint64 mtime;
  123. char *data, *end, *next, *line, *value;
  124. size_t size;
  125. int slot, new_slot;
  126. SDL_SteamVirtualGamepadInfo info;
  127. SDL_AssertJoysticksLocked();
  128. if (!SDL_steam_virtual_gamepad_info_file) {
  129. return SDL_FALSE;
  130. }
  131. now = SDL_GetTicks();
  132. if (SDL_steam_virtual_gamepad_info_check_time &&
  133. now < (SDL_steam_virtual_gamepad_info_check_time + UPDATE_CHECK_INTERVAL_MS)) {
  134. return SDL_FALSE;
  135. }
  136. SDL_steam_virtual_gamepad_info_check_time = now;
  137. mtime = GetFileModificationTime(SDL_steam_virtual_gamepad_info_file);
  138. if (mtime == 0 || mtime == SDL_steam_virtual_gamepad_info_file_mtime) {
  139. return SDL_FALSE;
  140. }
  141. data = (char *)SDL_LoadFile(SDL_steam_virtual_gamepad_info_file, &size);
  142. if (!data) {
  143. return SDL_FALSE;
  144. }
  145. SDL_FreeSteamVirtualGamepadInfo();
  146. slot = -1;
  147. SDL_zero(info);
  148. for (next = data, end = data + size; next < end; ) {
  149. while (next < end && (*next == '\0' || *next == '\r' || *next == '\n')) {
  150. ++next;
  151. }
  152. line = next;
  153. while (next < end && (*next != '\r' && *next != '\n')) {
  154. ++next;
  155. }
  156. *next = '\0';
  157. if (SDL_sscanf(line, "[slot %d]", &new_slot) == 1) {
  158. if (slot >= 0) {
  159. AddVirtualGamepadInfo(slot, &info);
  160. }
  161. slot = new_slot;
  162. } else {
  163. value = SDL_strchr(line, '=');
  164. if (value) {
  165. *value++ = '\0';
  166. if (SDL_strcmp(line, "name") == 0) {
  167. SDL_free(info.name);
  168. info.name = SDL_strdup(value);
  169. } else if (SDL_strcmp(line, "VID") == 0) {
  170. info.vendor_id = (Uint16)SDL_strtoul(value, NULL, 0);
  171. } else if (SDL_strcmp(line, "PID") == 0) {
  172. info.product_id = (Uint16)SDL_strtoul(value, NULL, 0);
  173. } else if (SDL_strcmp(line, "type") == 0) {
  174. info.type = SDL_GetGamepadTypeFromString(value);
  175. } else if (SDL_strcmp(line, "handle") == 0) {
  176. info.handle = SDL_strtoull(value, NULL, 0);
  177. }
  178. }
  179. }
  180. }
  181. if (slot >= 0) {
  182. AddVirtualGamepadInfo(slot, &info);
  183. }
  184. SDL_free(info.name);
  185. SDL_free(data);
  186. SDL_steam_virtual_gamepad_info_file_mtime = mtime;
  187. return SDL_TRUE;
  188. }
  189. const SDL_SteamVirtualGamepadInfo *SDL_GetSteamVirtualGamepadInfo(int slot)
  190. {
  191. SDL_AssertJoysticksLocked();
  192. if (slot < 0 || slot >= SDL_steam_virtual_gamepad_info_count) {
  193. return NULL;
  194. }
  195. return SDL_steam_virtual_gamepad_info[slot];
  196. }
  197. void SDL_QuitSteamVirtualGamepadInfo(void)
  198. {
  199. SDL_AssertJoysticksLocked();
  200. if (SDL_steam_virtual_gamepad_info_file) {
  201. SDL_FreeSteamVirtualGamepadInfo();
  202. SDL_free(SDL_steam_virtual_gamepad_info_file);
  203. SDL_steam_virtual_gamepad_info_file = NULL;
  204. }
  205. }