2015-06-21 15:33:46 +00:00
|
|
|
/*
|
|
|
|
Simple DirectMedia Layer
|
2023-01-09 17:48:21 +00:00
|
|
|
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
#include "./SDL_internal.h"
|
|
|
|
|
|
|
|
#include "SDL_hints.h"
|
|
|
|
#include "SDL_error.h"
|
2019-11-16 21:59:56 +00:00
|
|
|
#include "SDL_hints_c.h"
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
/* Assuming there aren't many hints set and they aren't being queried in
|
|
|
|
critical performance paths, we'll just use linked lists here.
|
|
|
|
*/
|
2022-11-30 20:51:59 +00:00
|
|
|
typedef struct SDL_HintWatch
|
|
|
|
{
|
2015-06-21 15:33:46 +00:00
|
|
|
SDL_HintCallback callback;
|
|
|
|
void *userdata;
|
|
|
|
struct SDL_HintWatch *next;
|
|
|
|
} SDL_HintWatch;
|
|
|
|
|
2022-11-30 20:51:59 +00:00
|
|
|
typedef struct SDL_Hint
|
|
|
|
{
|
2015-06-21 15:33:46 +00:00
|
|
|
char *name;
|
|
|
|
char *value;
|
|
|
|
SDL_HintPriority priority;
|
|
|
|
SDL_HintWatch *callbacks;
|
|
|
|
struct SDL_Hint *next;
|
|
|
|
} SDL_Hint;
|
|
|
|
|
|
|
|
static SDL_Hint *SDL_hints;
|
|
|
|
|
|
|
|
SDL_bool
|
|
|
|
SDL_SetHintWithPriority(const char *name, const char *value,
|
|
|
|
SDL_HintPriority priority)
|
|
|
|
{
|
|
|
|
const char *env;
|
|
|
|
SDL_Hint *hint;
|
|
|
|
SDL_HintWatch *entry;
|
|
|
|
|
2022-11-27 16:38:43 +00:00
|
|
|
if (name == NULL) {
|
2015-06-21 15:33:46 +00:00
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
env = SDL_getenv(name);
|
|
|
|
if (env && priority < SDL_HINT_OVERRIDE) {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (hint = SDL_hints; hint; hint = hint->next) {
|
|
|
|
if (SDL_strcmp(name, hint->name) == 0) {
|
|
|
|
if (priority < hint->priority) {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
2022-08-10 00:27:25 +00:00
|
|
|
if (hint->value != value &&
|
2022-11-27 16:38:43 +00:00
|
|
|
(value == NULL || !hint->value || SDL_strcmp(hint->value, value) != 0)) {
|
2022-11-30 20:51:59 +00:00
|
|
|
for (entry = hint->callbacks; entry;) {
|
2015-06-21 15:33:46 +00:00
|
|
|
/* Save the next entry in case this one is deleted */
|
|
|
|
SDL_HintWatch *next = entry->next;
|
|
|
|
entry->callback(entry->userdata, name, hint->value, value);
|
|
|
|
entry = next;
|
|
|
|
}
|
|
|
|
SDL_free(hint->value);
|
|
|
|
hint->value = value ? SDL_strdup(value) : NULL;
|
|
|
|
}
|
|
|
|
hint->priority = priority;
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Couldn't find the hint, add a new one */
|
|
|
|
hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
|
2022-11-27 16:38:43 +00:00
|
|
|
if (hint == NULL) {
|
2015-06-21 15:33:46 +00:00
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
hint->name = SDL_strdup(name);
|
|
|
|
hint->value = value ? SDL_strdup(value) : NULL;
|
|
|
|
hint->priority = priority;
|
|
|
|
hint->callbacks = NULL;
|
|
|
|
hint->next = SDL_hints;
|
|
|
|
SDL_hints = hint;
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
2022-08-10 14:59:12 +00:00
|
|
|
SDL_bool
|
|
|
|
SDL_ResetHint(const char *name)
|
|
|
|
{
|
|
|
|
const char *env;
|
2022-10-03 00:25:36 +00:00
|
|
|
SDL_Hint *hint;
|
2022-08-10 14:59:12 +00:00
|
|
|
SDL_HintWatch *entry;
|
|
|
|
|
2022-11-27 16:38:43 +00:00
|
|
|
if (name == NULL) {
|
2022-08-10 14:59:12 +00:00
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
env = SDL_getenv(name);
|
2022-10-03 00:17:31 +00:00
|
|
|
for (hint = SDL_hints; hint; hint = hint->next) {
|
2022-08-10 14:59:12 +00:00
|
|
|
if (SDL_strcmp(name, hint->name) == 0) {
|
|
|
|
if ((env == NULL && hint->value != NULL) ||
|
|
|
|
(env != NULL && hint->value == NULL) ||
|
Fixes made in response to running a static code analyzer under MS Windows.
Most of these are probably harmless, but the changes to SDL_immdevice.c and SDL_pixels.c appear to have fixed genuine bugs.
SDL_audiocvt.c: By separating the calculation of the divisor, I got rid of the suspicion that dividing a double by an integer led to loss of precision.
SDL_immdevice.c: Added a missing test, one that could have otherwise led to dereferencing a null pointer.
SDL_events.c, SDL_gamecontroller.c, SDL_joystick.c, SDL_malloc.c, SDL_video.c: Made it clear the return values weren't used.
SDL_hidapi_shield.c: The size is zero, so nothing bad would have happened, but the SDL_memset() was still being given an address outside of the array's range.
SDL_dinputjoystick.c: Initialize local data, just in case IDirectInputDevice8_GetProperty() isn't guaranteed to write to it.
SDL_render_sw.c: drawstate.viewport could be null (as seen on line 691).
SDL.c: SDL_MostSignificantBitIndex32() could return -1, though I don't know if you want to cope with that (what I did) or SDL_assert() that it can't happen.
SDL_hints.c: Replaced boolean tests on pointer values with comparisons to NULL.
SDL_pixels.c: Looks like the switch is genuinely missing a break!
SDL_rect_impl.h: The MacOS static checker pointed out issues with the X comparisons that were handled by assertions; I added assertions for the Y comparisons.
SDL_yuv.c, SDL_windowskeyboard.c, SDL_windowswindow.c: Checked error-result returns.
2022-10-06 02:26:09 +00:00
|
|
|
(env != NULL && SDL_strcmp(env, hint->value) != 0)) {
|
2022-11-30 20:51:59 +00:00
|
|
|
for (entry = hint->callbacks; entry;) {
|
2022-08-10 14:59:12 +00:00
|
|
|
/* Save the next entry in case this one is deleted */
|
|
|
|
SDL_HintWatch *next = entry->next;
|
|
|
|
entry->callback(entry->userdata, name, hint->value, env);
|
|
|
|
entry = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SDL_free(hint->value);
|
2022-10-03 00:17:31 +00:00
|
|
|
hint->value = NULL;
|
|
|
|
hint->priority = SDL_HINT_DEFAULT;
|
2022-08-10 14:59:12 +00:00
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
2022-11-30 20:51:59 +00:00
|
|
|
void SDL_ResetHints(void)
|
2022-10-03 00:17:31 +00:00
|
|
|
{
|
|
|
|
const char *env;
|
2022-10-03 00:25:36 +00:00
|
|
|
SDL_Hint *hint;
|
2022-10-03 00:17:31 +00:00
|
|
|
SDL_HintWatch *entry;
|
|
|
|
|
|
|
|
for (hint = SDL_hints; hint; hint = hint->next) {
|
|
|
|
env = SDL_getenv(hint->name);
|
|
|
|
if ((env == NULL && hint->value != NULL) ||
|
|
|
|
(env != NULL && hint->value == NULL) ||
|
Fixes made in response to running a static code analyzer under MS Windows.
Most of these are probably harmless, but the changes to SDL_immdevice.c and SDL_pixels.c appear to have fixed genuine bugs.
SDL_audiocvt.c: By separating the calculation of the divisor, I got rid of the suspicion that dividing a double by an integer led to loss of precision.
SDL_immdevice.c: Added a missing test, one that could have otherwise led to dereferencing a null pointer.
SDL_events.c, SDL_gamecontroller.c, SDL_joystick.c, SDL_malloc.c, SDL_video.c: Made it clear the return values weren't used.
SDL_hidapi_shield.c: The size is zero, so nothing bad would have happened, but the SDL_memset() was still being given an address outside of the array's range.
SDL_dinputjoystick.c: Initialize local data, just in case IDirectInputDevice8_GetProperty() isn't guaranteed to write to it.
SDL_render_sw.c: drawstate.viewport could be null (as seen on line 691).
SDL.c: SDL_MostSignificantBitIndex32() could return -1, though I don't know if you want to cope with that (what I did) or SDL_assert() that it can't happen.
SDL_hints.c: Replaced boolean tests on pointer values with comparisons to NULL.
SDL_pixels.c: Looks like the switch is genuinely missing a break!
SDL_rect_impl.h: The MacOS static checker pointed out issues with the X comparisons that were handled by assertions; I added assertions for the Y comparisons.
SDL_yuv.c, SDL_windowskeyboard.c, SDL_windowswindow.c: Checked error-result returns.
2022-10-06 02:26:09 +00:00
|
|
|
(env != NULL && SDL_strcmp(env, hint->value) != 0)) {
|
2022-11-30 20:51:59 +00:00
|
|
|
for (entry = hint->callbacks; entry;) {
|
2022-10-03 00:17:31 +00:00
|
|
|
/* Save the next entry in case this one is deleted */
|
|
|
|
SDL_HintWatch *next = entry->next;
|
2022-10-03 00:25:36 +00:00
|
|
|
entry->callback(entry->userdata, hint->name, hint->value, env);
|
2022-10-03 00:17:31 +00:00
|
|
|
entry = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SDL_free(hint->value);
|
|
|
|
hint->value = NULL;
|
|
|
|
hint->priority = SDL_HINT_DEFAULT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
SDL_bool
|
|
|
|
SDL_SetHint(const char *name, const char *value)
|
|
|
|
{
|
|
|
|
return SDL_SetHintWithPriority(name, value, SDL_HINT_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
SDL_GetHint(const char *name)
|
|
|
|
{
|
|
|
|
const char *env;
|
|
|
|
SDL_Hint *hint;
|
|
|
|
|
|
|
|
env = SDL_getenv(name);
|
|
|
|
for (hint = SDL_hints; hint; hint = hint->next) {
|
|
|
|
if (SDL_strcmp(name, hint->name) == 0) {
|
Fixes made in response to running a static code analyzer under MS Windows.
Most of these are probably harmless, but the changes to SDL_immdevice.c and SDL_pixels.c appear to have fixed genuine bugs.
SDL_audiocvt.c: By separating the calculation of the divisor, I got rid of the suspicion that dividing a double by an integer led to loss of precision.
SDL_immdevice.c: Added a missing test, one that could have otherwise led to dereferencing a null pointer.
SDL_events.c, SDL_gamecontroller.c, SDL_joystick.c, SDL_malloc.c, SDL_video.c: Made it clear the return values weren't used.
SDL_hidapi_shield.c: The size is zero, so nothing bad would have happened, but the SDL_memset() was still being given an address outside of the array's range.
SDL_dinputjoystick.c: Initialize local data, just in case IDirectInputDevice8_GetProperty() isn't guaranteed to write to it.
SDL_render_sw.c: drawstate.viewport could be null (as seen on line 691).
SDL.c: SDL_MostSignificantBitIndex32() could return -1, though I don't know if you want to cope with that (what I did) or SDL_assert() that it can't happen.
SDL_hints.c: Replaced boolean tests on pointer values with comparisons to NULL.
SDL_pixels.c: Looks like the switch is genuinely missing a break!
SDL_rect_impl.h: The MacOS static checker pointed out issues with the X comparisons that were handled by assertions; I added assertions for the Y comparisons.
SDL_yuv.c, SDL_windowskeyboard.c, SDL_windowswindow.c: Checked error-result returns.
2022-10-06 02:26:09 +00:00
|
|
|
if (env == NULL || hint->priority == SDL_HINT_OVERRIDE) {
|
2015-06-21 15:33:46 +00:00
|
|
|
return hint->value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
2016-10-08 06:40:44 +00:00
|
|
|
SDL_bool
|
2019-11-14 05:53:01 +00:00
|
|
|
SDL_GetStringBoolean(const char *value, SDL_bool default_value)
|
2016-10-08 06:40:44 +00:00
|
|
|
{
|
2022-11-27 16:38:43 +00:00
|
|
|
if (value == NULL || !*value) {
|
2016-10-08 06:40:44 +00:00
|
|
|
return default_value;
|
|
|
|
}
|
2019-11-14 05:53:01 +00:00
|
|
|
if (*value == '0' || SDL_strcasecmp(value, "false") == 0) {
|
2016-10-08 06:40:44 +00:00
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
2019-11-14 05:53:01 +00:00
|
|
|
SDL_bool
|
|
|
|
SDL_GetHintBoolean(const char *name, SDL_bool default_value)
|
|
|
|
{
|
|
|
|
const char *hint = SDL_GetHint(name);
|
|
|
|
return SDL_GetStringBoolean(hint, default_value);
|
|
|
|
}
|
|
|
|
|
2022-11-30 20:51:59 +00:00
|
|
|
void SDL_AddHintCallback(const char *name, SDL_HintCallback callback, void *userdata)
|
2015-06-21 15:33:46 +00:00
|
|
|
{
|
|
|
|
SDL_Hint *hint;
|
|
|
|
SDL_HintWatch *entry;
|
|
|
|
const char *value;
|
|
|
|
|
2022-11-27 16:38:43 +00:00
|
|
|
if (name == NULL || !*name) {
|
2015-06-21 15:33:46 +00:00
|
|
|
SDL_InvalidParamError("name");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!callback) {
|
|
|
|
SDL_InvalidParamError("callback");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_DelHintCallback(name, callback, userdata);
|
|
|
|
|
|
|
|
entry = (SDL_HintWatch *)SDL_malloc(sizeof(*entry));
|
2022-11-27 16:38:43 +00:00
|
|
|
if (entry == NULL) {
|
2015-06-21 15:33:46 +00:00
|
|
|
SDL_OutOfMemory();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
entry->callback = callback;
|
|
|
|
entry->userdata = userdata;
|
|
|
|
|
|
|
|
for (hint = SDL_hints; hint; hint = hint->next) {
|
|
|
|
if (SDL_strcmp(name, hint->name) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-11-27 16:38:43 +00:00
|
|
|
if (hint == NULL) {
|
2015-06-21 15:33:46 +00:00
|
|
|
/* Need to add a hint entry for this watcher */
|
|
|
|
hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
|
2022-11-27 16:38:43 +00:00
|
|
|
if (hint == NULL) {
|
2015-06-21 15:33:46 +00:00
|
|
|
SDL_OutOfMemory();
|
|
|
|
SDL_free(entry);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
hint->name = SDL_strdup(name);
|
2022-04-18 20:45:41 +00:00
|
|
|
if (!hint->name) {
|
2022-05-21 08:44:50 +00:00
|
|
|
SDL_free(entry);
|
2022-04-18 20:45:41 +00:00
|
|
|
SDL_free(hint);
|
|
|
|
SDL_OutOfMemory();
|
|
|
|
return;
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
hint->value = NULL;
|
|
|
|
hint->priority = SDL_HINT_DEFAULT;
|
|
|
|
hint->callbacks = NULL;
|
|
|
|
hint->next = SDL_hints;
|
|
|
|
SDL_hints = hint;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add it to the callbacks for this hint */
|
|
|
|
entry->next = hint->callbacks;
|
|
|
|
hint->callbacks = entry;
|
|
|
|
|
|
|
|
/* Now call it with the current value */
|
|
|
|
value = SDL_GetHint(name);
|
|
|
|
callback(userdata, name, value, value);
|
|
|
|
}
|
|
|
|
|
2022-11-30 20:51:59 +00:00
|
|
|
void SDL_DelHintCallback(const char *name, SDL_HintCallback callback, void *userdata)
|
2015-06-21 15:33:46 +00:00
|
|
|
{
|
|
|
|
SDL_Hint *hint;
|
|
|
|
SDL_HintWatch *entry, *prev;
|
|
|
|
|
|
|
|
for (hint = SDL_hints; hint; hint = hint->next) {
|
|
|
|
if (SDL_strcmp(name, hint->name) == 0) {
|
|
|
|
prev = NULL;
|
|
|
|
for (entry = hint->callbacks; entry; entry = entry->next) {
|
|
|
|
if (callback == entry->callback && userdata == entry->userdata) {
|
|
|
|
if (prev) {
|
|
|
|
prev->next = entry->next;
|
|
|
|
} else {
|
|
|
|
hint->callbacks = entry->next;
|
|
|
|
}
|
|
|
|
SDL_free(entry);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev = entry;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDL_ClearHints(void)
|
|
|
|
{
|
|
|
|
SDL_Hint *hint;
|
|
|
|
SDL_HintWatch *entry;
|
|
|
|
|
|
|
|
while (SDL_hints) {
|
|
|
|
hint = SDL_hints;
|
|
|
|
SDL_hints = hint->next;
|
|
|
|
|
|
|
|
SDL_free(hint->name);
|
|
|
|
SDL_free(hint->value);
|
2022-11-30 20:51:59 +00:00
|
|
|
for (entry = hint->callbacks; entry;) {
|
2015-06-21 15:33:46 +00:00
|
|
|
SDL_HintWatch *freeable = entry;
|
|
|
|
entry = entry->next;
|
|
|
|
SDL_free(freeable);
|
|
|
|
}
|
|
|
|
SDL_free(hint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|