SDL_hints.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  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_hints_c.h"
  20. #ifdef SDL_PLATFORM_ANDROID
  21. #include "core/android/SDL_android.h"
  22. #endif
  23. typedef struct SDL_HintWatch
  24. {
  25. SDL_HintCallback callback;
  26. void *userdata;
  27. struct SDL_HintWatch *next;
  28. } SDL_HintWatch;
  29. typedef struct SDL_Hint
  30. {
  31. char *value;
  32. SDL_HintPriority priority;
  33. SDL_HintWatch *callbacks;
  34. } SDL_Hint;
  35. static SDL_AtomicU32 SDL_hint_props;
  36. void SDL_InitHints(void)
  37. {
  38. }
  39. void SDL_QuitHints(void)
  40. {
  41. SDL_PropertiesID props;
  42. do {
  43. props = SDL_GetAtomicU32(&SDL_hint_props);
  44. } while (!SDL_CompareAndSwapAtomicU32(&SDL_hint_props, props, 0));
  45. if (props) {
  46. SDL_DestroyProperties(props);
  47. }
  48. }
  49. static SDL_PropertiesID GetHintProperties(bool create)
  50. {
  51. SDL_PropertiesID props = SDL_GetAtomicU32(&SDL_hint_props);
  52. if (!props && create) {
  53. props = SDL_CreateProperties();
  54. if (!SDL_CompareAndSwapAtomicU32(&SDL_hint_props, 0, props)) {
  55. // Somebody else created hint properties before us, just use those
  56. SDL_DestroyProperties(props);
  57. props = SDL_GetAtomicU32(&SDL_hint_props);
  58. }
  59. }
  60. return props;
  61. }
  62. static void SDLCALL CleanupHintProperty(void *userdata, void *value)
  63. {
  64. SDL_Hint *hint = (SDL_Hint *) value;
  65. SDL_free(hint->value);
  66. SDL_HintWatch *entry = hint->callbacks;
  67. while (entry) {
  68. SDL_HintWatch *freeable = entry;
  69. entry = entry->next;
  70. SDL_free(freeable);
  71. }
  72. SDL_free(hint);
  73. }
  74. static const char* GetHintEnvironmentVariable(const char *name)
  75. {
  76. const char *result = SDL_getenv(name);
  77. if (!result && name && *name) {
  78. // fall back to old (SDL2) names of environment variables that
  79. // are important to users (e.g. many use SDL_VIDEODRIVER=wayland)
  80. if (SDL_strcmp(name, SDL_HINT_VIDEO_DRIVER) == 0) {
  81. result = SDL_getenv("SDL_VIDEODRIVER");
  82. } else if (SDL_strcmp(name, SDL_HINT_AUDIO_DRIVER) == 0) {
  83. result = SDL_getenv("SDL_AUDIODRIVER");
  84. }
  85. }
  86. return result;
  87. }
  88. bool SDL_SetHintWithPriority(const char *name, const char *value, SDL_HintPriority priority)
  89. {
  90. if (!name || !*name) {
  91. return SDL_InvalidParamError("name");
  92. }
  93. const char *env = GetHintEnvironmentVariable(name);
  94. if (env && (priority < SDL_HINT_OVERRIDE)) {
  95. return SDL_SetError("An environment variable is taking priority");
  96. }
  97. const SDL_PropertiesID hints = GetHintProperties(true);
  98. if (!hints) {
  99. return false;
  100. }
  101. bool result = false;
  102. SDL_LockProperties(hints);
  103. SDL_Hint *hint = (SDL_Hint *)SDL_GetPointerProperty(hints, name, NULL);
  104. if (hint) {
  105. if (priority >= hint->priority) {
  106. if (hint->value != value && (!value || !hint->value || SDL_strcmp(hint->value, value) != 0)) {
  107. char *old_value = hint->value;
  108. hint->value = value ? SDL_strdup(value) : NULL;
  109. SDL_HintWatch *entry = hint->callbacks;
  110. while (entry) {
  111. // Save the next entry in case this one is deleted
  112. SDL_HintWatch *next = entry->next;
  113. entry->callback(entry->userdata, name, old_value, value);
  114. entry = next;
  115. }
  116. SDL_free(old_value);
  117. }
  118. hint->priority = priority;
  119. result = true;
  120. }
  121. } else { // Couldn't find the hint? Add a new one.
  122. hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
  123. if (hint) {
  124. hint->value = value ? SDL_strdup(value) : NULL;
  125. hint->priority = priority;
  126. hint->callbacks = NULL;
  127. result = SDL_SetPointerPropertyWithCleanup(hints, name, hint, CleanupHintProperty, NULL);
  128. }
  129. }
  130. #ifdef SDL_PLATFORM_ANDROID
  131. if (SDL_strcmp(name, SDL_HINT_ANDROID_ALLOW_RECREATE_ACTIVITY) == 0) {
  132. // Special handling for this hint, which needs to persist outside the normal application flow
  133. Android_SetAllowRecreateActivity(SDL_GetStringBoolean(value, false));
  134. }
  135. #endif // SDL_PLATFORM_ANDROID
  136. SDL_UnlockProperties(hints);
  137. return result;
  138. }
  139. bool SDL_ResetHint(const char *name)
  140. {
  141. if (!name || !*name) {
  142. return SDL_InvalidParamError("name");
  143. }
  144. const char *env = GetHintEnvironmentVariable(name);
  145. const SDL_PropertiesID hints = GetHintProperties(false);
  146. if (!hints) {
  147. return false;
  148. }
  149. bool result = false;
  150. SDL_LockProperties(hints);
  151. SDL_Hint *hint = (SDL_Hint *)SDL_GetPointerProperty(hints, name, NULL);
  152. if (hint) {
  153. if ((!env && hint->value) || (env && !hint->value) || (env && SDL_strcmp(env, hint->value) != 0)) {
  154. for (SDL_HintWatch *entry = hint->callbacks; entry;) {
  155. // Save the next entry in case this one is deleted
  156. SDL_HintWatch *next = entry->next;
  157. entry->callback(entry->userdata, name, hint->value, env);
  158. entry = next;
  159. }
  160. }
  161. SDL_free(hint->value);
  162. hint->value = NULL;
  163. hint->priority = SDL_HINT_DEFAULT;
  164. result = true;
  165. }
  166. #ifdef SDL_PLATFORM_ANDROID
  167. if (SDL_strcmp(name, SDL_HINT_ANDROID_ALLOW_RECREATE_ACTIVITY) == 0) {
  168. // Special handling for this hint, which needs to persist outside the normal application flow
  169. if (env) {
  170. Android_SetAllowRecreateActivity(SDL_GetStringBoolean(env, false));
  171. } else {
  172. Android_SetAllowRecreateActivity(false);
  173. }
  174. }
  175. #endif // SDL_PLATFORM_ANDROID
  176. SDL_UnlockProperties(hints);
  177. return result;
  178. }
  179. static void SDLCALL ResetHintsCallback(void *userdata, SDL_PropertiesID hints, const char *name)
  180. {
  181. SDL_Hint *hint = (SDL_Hint *)SDL_GetPointerProperty(hints, name, NULL);
  182. if (!hint) {
  183. return; // uh...okay.
  184. }
  185. const char *env = GetHintEnvironmentVariable(name);
  186. if ((!env && hint->value) || (env && !hint->value) || (env && SDL_strcmp(env, hint->value) != 0)) {
  187. SDL_HintWatch *entry = hint->callbacks;
  188. while (entry) {
  189. // Save the next entry in case this one is deleted
  190. SDL_HintWatch *next = entry->next;
  191. entry->callback(entry->userdata, name, hint->value, env);
  192. entry = next;
  193. }
  194. }
  195. SDL_free(hint->value);
  196. hint->value = NULL;
  197. hint->priority = SDL_HINT_DEFAULT;
  198. #ifdef SDL_PLATFORM_ANDROID
  199. if (SDL_strcmp(name, SDL_HINT_ANDROID_ALLOW_RECREATE_ACTIVITY) == 0) {
  200. // Special handling for this hint, which needs to persist outside the normal application flow
  201. if (env) {
  202. Android_SetAllowRecreateActivity(SDL_GetStringBoolean(env, false));
  203. } else {
  204. Android_SetAllowRecreateActivity(false);
  205. }
  206. }
  207. #endif // SDL_PLATFORM_ANDROID
  208. }
  209. void SDL_ResetHints(void)
  210. {
  211. SDL_EnumerateProperties(GetHintProperties(false), ResetHintsCallback, NULL);
  212. }
  213. bool SDL_SetHint(const char *name, const char *value)
  214. {
  215. return SDL_SetHintWithPriority(name, value, SDL_HINT_NORMAL);
  216. }
  217. const char *SDL_GetHint(const char *name)
  218. {
  219. if (!name) {
  220. return NULL;
  221. }
  222. const char *result = GetHintEnvironmentVariable(name);
  223. const SDL_PropertiesID hints = GetHintProperties(false);
  224. if (hints) {
  225. SDL_LockProperties(hints);
  226. SDL_Hint *hint = (SDL_Hint *)SDL_GetPointerProperty(hints, name, NULL);
  227. if (hint) {
  228. if (!result || hint->priority == SDL_HINT_OVERRIDE) {
  229. result = SDL_GetPersistentString(hint->value);
  230. }
  231. }
  232. SDL_UnlockProperties(hints);
  233. }
  234. return result;
  235. }
  236. int SDL_GetStringInteger(const char *value, int default_value)
  237. {
  238. if (!value || !*value) {
  239. return default_value;
  240. }
  241. if (SDL_strcasecmp(value, "false") == 0) {
  242. return 0;
  243. }
  244. if (SDL_strcasecmp(value, "true") == 0) {
  245. return 1;
  246. }
  247. if (*value == '-' || SDL_isdigit(*value)) {
  248. return SDL_atoi(value);
  249. }
  250. return default_value;
  251. }
  252. bool SDL_GetStringBoolean(const char *value, bool default_value)
  253. {
  254. if (!value || !*value) {
  255. return default_value;
  256. }
  257. if (*value == '0' || SDL_strcasecmp(value, "false") == 0) {
  258. return false;
  259. }
  260. return true;
  261. }
  262. bool SDL_GetHintBoolean(const char *name, bool default_value)
  263. {
  264. const char *hint = SDL_GetHint(name);
  265. return SDL_GetStringBoolean(hint, default_value);
  266. }
  267. bool SDL_AddHintCallback(const char *name, SDL_HintCallback callback, void *userdata)
  268. {
  269. if (!name || !*name) {
  270. return SDL_InvalidParamError("name");
  271. } else if (!callback) {
  272. return SDL_InvalidParamError("callback");
  273. }
  274. const SDL_PropertiesID hints = GetHintProperties(true);
  275. if (!hints) {
  276. return false;
  277. }
  278. SDL_HintWatch *entry = (SDL_HintWatch *)SDL_malloc(sizeof(*entry));
  279. if (!entry) {
  280. return false;
  281. }
  282. entry->callback = callback;
  283. entry->userdata = userdata;
  284. bool result = false;
  285. SDL_LockProperties(hints);
  286. SDL_RemoveHintCallback(name, callback, userdata);
  287. SDL_Hint *hint = (SDL_Hint *)SDL_GetPointerProperty(hints, name, NULL);
  288. if (hint) {
  289. result = true;
  290. } else { // Need to add a hint entry for this watcher
  291. hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
  292. if (!hint) {
  293. SDL_free(entry);
  294. SDL_UnlockProperties(hints);
  295. return false;
  296. } else {
  297. hint->value = NULL;
  298. hint->priority = SDL_HINT_DEFAULT;
  299. hint->callbacks = NULL;
  300. result = SDL_SetPointerPropertyWithCleanup(hints, name, hint, CleanupHintProperty, NULL);
  301. }
  302. }
  303. // Add it to the callbacks for this hint
  304. entry->next = hint->callbacks;
  305. hint->callbacks = entry;
  306. // Now call it with the current value
  307. const char *value = SDL_GetHint(name);
  308. callback(userdata, name, value, value);
  309. SDL_UnlockProperties(hints);
  310. return result;
  311. }
  312. void SDL_RemoveHintCallback(const char *name, SDL_HintCallback callback, void *userdata)
  313. {
  314. if (!name || !*name) {
  315. return;
  316. }
  317. const SDL_PropertiesID hints = GetHintProperties(false);
  318. if (!hints) {
  319. return;
  320. }
  321. SDL_LockProperties(hints);
  322. SDL_Hint *hint = (SDL_Hint *)SDL_GetPointerProperty(hints, name, NULL);
  323. if (hint) {
  324. SDL_HintWatch *prev = NULL;
  325. for (SDL_HintWatch *entry = hint->callbacks; entry; entry = entry->next) {
  326. if ((callback == entry->callback) && (userdata == entry->userdata)) {
  327. if (prev) {
  328. prev->next = entry->next;
  329. } else {
  330. hint->callbacks = entry->next;
  331. }
  332. SDL_free(entry);
  333. break;
  334. }
  335. prev = entry;
  336. }
  337. }
  338. SDL_UnlockProperties(hints);
  339. }