SDL_hints.c 8.6 KB

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