SDL_hints.c 10 KB

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