Added a utility function to simplify the hint handling logic

This commit is contained in:
Sam Lantinga 2019-11-13 21:53:01 -08:00
parent a63e93a193
commit cf33f1f0ef
5 changed files with 52 additions and 20 deletions

View File

@ -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)
{

32
src/SDL_hints_c.h Normal file
View File

@ -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: */

View File

@ -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");

View File

@ -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)

View File

@ -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) {