From cf33f1f0ef4d43f604388a3169a51044fbe6ec27 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 13 Nov 2019 21:53:01 -0800 Subject: [PATCH] Added a utility function to simplify the hint handling logic --- src/SDL_hints.c | 14 ++++++++--- src/SDL_hints_c.h | 32 ++++++++++++++++++++++++ src/events/SDL_mouse.c | 20 +++++---------- src/joystick/hidapi/SDL_hidapi_switch.c | 3 ++- src/joystick/hidapi/SDL_hidapijoystick.c | 3 ++- 5 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 src/SDL_hints_c.h diff --git a/src/SDL_hints.c b/src/SDL_hints.c index 277d96a00c4c8..0337def601011 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 0000000000000..54ebf0c59cc0a --- /dev/null +++ b/src/SDL_hints_c.h @@ -0,0 +1,32 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2019 Sam Lantinga + + 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 29c445fd8af55..4eee719323afd 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 3d084bf3d0907..7c2648a69cb89 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 212383db760c5..cc032e945d973 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) {