SDL_hints.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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_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_PropertiesID SDL_hint_props = 0;
  33. static SDL_PropertiesID GetHintProperties(SDL_bool create)
  34. {
  35. if (!SDL_hint_props && create) {
  36. SDL_hint_props = SDL_CreateProperties();
  37. }
  38. return SDL_hint_props;
  39. }
  40. void SDL_InitHints(void)
  41. {
  42. // Just make sure the hint properties are created on the main thread
  43. (void)GetHintProperties(SDL_TRUE);
  44. }
  45. static void SDLCALL CleanupHintProperty(void *userdata, void *value)
  46. {
  47. SDL_Hint *hint = (SDL_Hint *) value;
  48. SDL_free(hint->value);
  49. SDL_HintWatch *entry = hint->callbacks;
  50. while (entry) {
  51. SDL_HintWatch *freeable = entry;
  52. entry = entry->next;
  53. SDL_free(freeable);
  54. }
  55. SDL_free(hint);
  56. }
  57. int SDL_SetHintWithPriority(const char *name, const char *value, SDL_HintPriority priority)
  58. {
  59. if (!name || !*name) {
  60. return SDL_InvalidParamError("name");
  61. }
  62. const char *env = SDL_getenv(name);
  63. if (env && (priority < SDL_HINT_OVERRIDE)) {
  64. return SDL_SetError("An environment variable is taking priority");
  65. }
  66. const SDL_PropertiesID hints = GetHintProperties(SDL_TRUE);
  67. if (!hints) {
  68. return -1;
  69. }
  70. int retval = -1;
  71. SDL_LockProperties(hints);
  72. SDL_Hint *hint = SDL_GetPointerProperty(hints, name, NULL);
  73. if (hint) {
  74. if (priority >= hint->priority) {
  75. if (hint->value != value && (!value || !hint->value || SDL_strcmp(hint->value, value) != 0)) {
  76. char *old_value = hint->value;
  77. hint->value = value ? SDL_strdup(value) : NULL;
  78. SDL_HintWatch *entry = hint->callbacks;
  79. while (entry) {
  80. // Save the next entry in case this one is deleted
  81. SDL_HintWatch *next = entry->next;
  82. entry->callback(entry->userdata, name, old_value, value);
  83. entry = next;
  84. }
  85. SDL_free(old_value);
  86. }
  87. hint->priority = priority;
  88. retval = 0;
  89. }
  90. } else { // Couldn't find the hint? Add a new one.
  91. hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
  92. if (hint) {
  93. hint->value = value ? SDL_strdup(value) : NULL;
  94. hint->priority = priority;
  95. hint->callbacks = NULL;
  96. retval = (SDL_SetPointerPropertyWithCleanup(hints, name, hint, CleanupHintProperty, NULL) != -1);
  97. }
  98. }
  99. SDL_UnlockProperties(hints);
  100. return retval;
  101. }
  102. int SDL_ResetHint(const char *name)
  103. {
  104. if (!name || !*name) {
  105. return SDL_InvalidParamError("name");
  106. }
  107. const char *env = SDL_getenv(name);
  108. const SDL_PropertiesID hints = GetHintProperties(SDL_FALSE);
  109. if (!hints) {
  110. return -1;
  111. }
  112. int retval = -1;
  113. SDL_LockProperties(hints);
  114. SDL_Hint *hint = SDL_GetPointerProperty(hints, name, NULL);
  115. if (hint) {
  116. if ((!env && hint->value) || (env && !hint->value) || (env && SDL_strcmp(env, hint->value) != 0)) {
  117. for (SDL_HintWatch *entry = hint->callbacks; entry;) {
  118. // Save the next entry in case this one is deleted
  119. SDL_HintWatch *next = entry->next;
  120. entry->callback(entry->userdata, name, hint->value, env);
  121. entry = next;
  122. }
  123. }
  124. SDL_free(hint->value);
  125. hint->value = NULL;
  126. hint->priority = SDL_HINT_DEFAULT;
  127. retval = 0;
  128. }
  129. SDL_UnlockProperties(hints);
  130. return retval;
  131. }
  132. static void SDLCALL ResetHintsCallback(void *userdata, SDL_PropertiesID hints, const char *name)
  133. {
  134. SDL_Hint *hint = SDL_GetPointerProperty(hints, name, NULL);
  135. if (!hint) {
  136. return; // uh...okay.
  137. }
  138. const char *env = SDL_getenv(name);
  139. if ((!env && hint->value) || (env && !hint->value) || (env && SDL_strcmp(env, hint->value) != 0)) {
  140. SDL_HintWatch *entry = hint->callbacks;
  141. while (entry) {
  142. // Save the next entry in case this one is deleted
  143. SDL_HintWatch *next = entry->next;
  144. entry->callback(entry->userdata, name, hint->value, env);
  145. entry = next;
  146. }
  147. }
  148. SDL_free(hint->value);
  149. hint->value = NULL;
  150. hint->priority = SDL_HINT_DEFAULT;
  151. }
  152. void SDL_ResetHints(void)
  153. {
  154. SDL_EnumerateProperties(GetHintProperties(SDL_FALSE), ResetHintsCallback, NULL);
  155. }
  156. int SDL_SetHint(const char *name, const char *value)
  157. {
  158. return SDL_SetHintWithPriority(name, value, SDL_HINT_NORMAL);
  159. }
  160. const char *SDL_GetHint(const char *name)
  161. {
  162. if (!name) {
  163. return NULL;
  164. }
  165. const char *retval = SDL_getenv(name);
  166. const SDL_PropertiesID hints = GetHintProperties(SDL_FALSE);
  167. if (hints) {
  168. SDL_LockProperties(hints);
  169. SDL_Hint *hint = SDL_GetPointerProperty(hints, name, NULL);
  170. if (hint) {
  171. if (!retval || hint->priority == SDL_HINT_OVERRIDE) {
  172. retval = SDL_GetPersistentString(hint->value);
  173. }
  174. }
  175. SDL_UnlockProperties(hints);
  176. }
  177. return retval;
  178. }
  179. int SDL_GetStringInteger(const char *value, int default_value)
  180. {
  181. if (!value || !*value) {
  182. return default_value;
  183. }
  184. if (*value == '0' || SDL_strcasecmp(value, "false") == 0) {
  185. return 0;
  186. }
  187. if (*value == '1' || SDL_strcasecmp(value, "true") == 0) {
  188. return 1;
  189. }
  190. if (*value == '-' || SDL_isdigit(*value)) {
  191. return SDL_atoi(value);
  192. }
  193. return default_value;
  194. }
  195. SDL_bool SDL_GetStringBoolean(const char *value, SDL_bool default_value)
  196. {
  197. if (!value || !*value) {
  198. return default_value;
  199. }
  200. if (*value == '0' || SDL_strcasecmp(value, "false") == 0) {
  201. return SDL_FALSE;
  202. }
  203. return SDL_TRUE;
  204. }
  205. SDL_bool SDL_GetHintBoolean(const char *name, SDL_bool default_value)
  206. {
  207. const char *hint = SDL_GetHint(name);
  208. return SDL_GetStringBoolean(hint, default_value);
  209. }
  210. int SDL_AddHintCallback(const char *name, SDL_HintCallback callback, void *userdata)
  211. {
  212. if (!name || !*name) {
  213. return SDL_InvalidParamError("name");
  214. } else if (!callback) {
  215. return SDL_InvalidParamError("callback");
  216. }
  217. const SDL_PropertiesID hints = GetHintProperties(SDL_TRUE);
  218. if (!hints) {
  219. return -1;
  220. }
  221. SDL_HintWatch *entry = (SDL_HintWatch *)SDL_malloc(sizeof(*entry));
  222. if (!entry) {
  223. return -1;
  224. }
  225. entry->callback = callback;
  226. entry->userdata = userdata;
  227. int retval = -1;
  228. SDL_LockProperties(hints);
  229. SDL_DelHintCallback(name, callback, userdata);
  230. SDL_Hint *hint = SDL_GetPointerProperty(hints, name, NULL);
  231. if (hint) {
  232. retval = 0;
  233. } else { // Need to add a hint entry for this watcher
  234. hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
  235. if (!hint) {
  236. SDL_free(entry);
  237. SDL_UnlockProperties(hints);
  238. return -1;
  239. } else {
  240. hint->value = NULL;
  241. hint->priority = SDL_HINT_DEFAULT;
  242. hint->callbacks = NULL;
  243. retval = SDL_SetPointerPropertyWithCleanup(hints, name, hint, CleanupHintProperty, NULL);
  244. }
  245. }
  246. // Add it to the callbacks for this hint
  247. entry->next = hint->callbacks;
  248. hint->callbacks = entry;
  249. // Now call it with the current value
  250. const char *value = SDL_GetHint(name);
  251. callback(userdata, name, value, value);
  252. SDL_UnlockProperties(hints);
  253. return retval;
  254. }
  255. void SDL_DelHintCallback(const char *name, SDL_HintCallback callback, void *userdata)
  256. {
  257. if (!name || !*name) {
  258. return;
  259. }
  260. const SDL_PropertiesID hints = GetHintProperties(SDL_FALSE);
  261. if (!hints) {
  262. return;
  263. }
  264. SDL_LockProperties(hints);
  265. SDL_Hint *hint = SDL_GetPointerProperty(hints, name, NULL);
  266. if (hint) {
  267. SDL_HintWatch *prev = NULL;
  268. for (SDL_HintWatch *entry = hint->callbacks; entry; entry = entry->next) {
  269. if ((callback == entry->callback) && (userdata == entry->userdata)) {
  270. if (prev) {
  271. prev->next = entry->next;
  272. } else {
  273. hint->callbacks = entry->next;
  274. }
  275. SDL_free(entry);
  276. break;
  277. }
  278. prev = entry;
  279. }
  280. }
  281. SDL_UnlockProperties(hints);
  282. }
  283. void SDL_QuitHints(void)
  284. {
  285. SDL_DestroyProperties(SDL_hint_props);
  286. SDL_hint_props = 0;
  287. }