Skip to content

Commit

Permalink
Added a utility function to simplify the hint handling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Nov 14, 2019
1 parent a63e93a commit cf33f1f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 20 deletions.
14 changes: 10 additions & 4 deletions src/SDL_hints.c
Expand Up @@ -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)
{
Expand Down
32 changes: 32 additions & 0 deletions 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: */
20 changes: 6 additions & 14 deletions src/events/SDL_mouse.c
Expand Up @@ -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()
Expand Down Expand Up @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion src/joystick/hidapi/SDL_hidapi_switch.c
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/joystick/hidapi/SDL_hidapijoystick.c
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit cf33f1f

Please sign in to comment.