Added a utility function to simplify the hint handling logic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
diff --git a/src/SDL_hints.c b/src/SDL_hints.c
index 277d96a..0337def 100644
--- a/src/SDL_hints.c
+++ b/src/SDL_hints.c
@@ -119,18 +119,24 @@ SDL_GetHint(const char *name)
}
SDL_bool
-SDL_GetHintBoolean(const char *name, SDL_bool default_value)
+SDL_GetStringBoolean(const char *value, SDL_bool default_value)
{
- const char *hint = SDL_GetHint(name);
- if (!hint || !*hint) {
+ if (!value || !*value) {
return default_value;
}
- if (*hint == '0' || SDL_strcasecmp(hint, "false") == 0) {
+ if (*value == '0' || SDL_strcasecmp(value, "false") == 0) {
return SDL_FALSE;
}
return SDL_TRUE;
}
+SDL_bool
+SDL_GetHintBoolean(const char *name, SDL_bool default_value)
+{
+ const char *hint = SDL_GetHint(name);
+ return SDL_GetStringBoolean(hint, default_value);
+}
+
void
SDL_AddHintCallback(const char *name, SDL_HintCallback callback, void *userdata)
{
diff --git a/src/SDL_hints_c.h b/src/SDL_hints_c.h
new file mode 100644
index 0000000..54ebf0c
--- /dev/null
+++ b/src/SDL_hints_c.h
@@ -0,0 +1,32 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
+
+ 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"
+
+/* This file defines useful function for working with SDL hints */
+
+#ifndef SDL_hints_c_h_
+#define SDL_hints_c_h_
+
+extern SDL_bool SDL_GetStringBoolean(const char *value, SDL_bool default_value);
+
+#endif /* SDL_hints_c_h_ */
+
+/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/events/SDL_mouse.c b/src/events/SDL_mouse.c
index 29c445f..4eee719 100644
--- a/src/events/SDL_mouse.c
+++ b/src/events/SDL_mouse.c
@@ -27,6 +27,7 @@
#include "SDL_timer.h"
#include "SDL_events.h"
#include "SDL_events_c.h"
+#include "../SDL_hints_c.h"
#include "../video/SDL_sysvideo.h"
#ifdef __WIN32__
#include "../core/windows/SDL_windows.h" // For GetDoubleClickTime()
@@ -100,30 +101,21 @@ SDL_TouchMouseEventsChanged(void *userdata, const char *name, const char *oldVal
{
SDL_Mouse *mouse = (SDL_Mouse *)userdata;
- if (hint && (*hint == '0' || SDL_strcasecmp(hint, "false") == 0)) {
- mouse->touch_mouse_events = SDL_FALSE;
- } else {
- mouse->touch_mouse_events = SDL_TRUE;
- }
+ mouse->touch_mouse_events = SDL_GetStringBoolean(hint, SDL_TRUE);
}
static void SDLCALL
SDL_MouseTouchEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
SDL_Mouse *mouse = (SDL_Mouse *)userdata;
+ SDL_bool default_value;
- if (hint == NULL || *hint == '\0') {
- /* Default */
#if defined(__ANDROID__) || (defined(__IPHONEOS__) && !defined(__TVOS__))
- mouse->mouse_touch_events = SDL_TRUE;
+ default_value = SDL_TRUE;
#else
- mouse->mouse_touch_events = SDL_FALSE;
+ default_value = SDL_FALSE;
#endif
- } else if (*hint == '1' || SDL_strcasecmp(hint, "true") == 0) {
- mouse->mouse_touch_events = SDL_TRUE;
- } else {
- mouse->mouse_touch_events = SDL_FALSE;
- }
+ mouse->mouse_touch_events = SDL_GetStringBoolean(hint, default_value);
if (mouse->mouse_touch_events) {
SDL_AddTouch(SDL_MOUSE_TOUCHID, SDL_TOUCH_DEVICE_DIRECT, "mouse_input");
diff --git a/src/joystick/hidapi/SDL_hidapi_switch.c b/src/joystick/hidapi/SDL_hidapi_switch.c
index 3d084bf..7c2648a 100644
--- a/src/joystick/hidapi/SDL_hidapi_switch.c
+++ b/src/joystick/hidapi/SDL_hidapi_switch.c
@@ -33,6 +33,7 @@
#include "SDL_gamecontroller.h"
#include "../SDL_sysjoystick.h"
#include "SDL_hidapijoystick_c.h"
+#include "../../SDL_hints_c.h"
#ifdef SDL_JOYSTICK_HIDAPI_SWITCH
@@ -591,7 +592,7 @@ static Sint16 ApplyStickCalibration(SDL_DriverSwitch_Context *ctx, int nStick, i
static void SDLCALL SDL_GameControllerButtonReportingHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)userdata;
- ctx->m_bUseButtonLabels = (!hint || !*hint || ((*hint != '0') && (SDL_strcasecmp(hint, "false") != 0)));
+ ctx->m_bUseButtonLabels = SDL_GetStringBoolean(hint, SDL_TRUE);
}
static Uint8 RemapButton(SDL_DriverSwitch_Context *ctx, Uint8 button)
diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c
index 212383d..cc032e9 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick.c
+++ b/src/joystick/hidapi/SDL_hidapijoystick.c
@@ -31,6 +31,7 @@
#include "SDL_joystick.h"
#include "../SDL_sysjoystick.h"
#include "SDL_hidapijoystick_c.h"
+#include "../../SDL_hints_c.h"
#if defined(__WIN32__)
#include "../../core/windows/SDL_windows.h"
@@ -641,7 +642,7 @@ SDL_HIDAPIDriverHintChanged(void *userdata, const char *name, const char *oldVal
{
int i;
SDL_HIDAPI_Device *device = SDL_HIDAPI_devices;
- SDL_bool enabled = (!hint || !*hint || ((*hint != '0') && (SDL_strcasecmp(hint, "false") != 0)));
+ SDL_bool enabled = SDL_GetStringBoolean(hint, SDL_TRUE);
if (SDL_strcmp(name, SDL_HINT_JOYSTICK_HIDAPI) == 0) {
for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {