Added a utility function to simplify the hint handling logic
authorSam Lantinga <slouken@libsdl.org>
Wed, 13 Nov 2019 21:53:01 -0800
changeset 1324450987b0b4533
parent 13243 2ec6a2476551
child 13245 326295ab64d7
Added a utility function to simplify the hint handling logic
src/SDL_hints.c
src/SDL_hints_c.h
src/events/SDL_mouse.c
src/joystick/hidapi/SDL_hidapi_switch.c
src/joystick/hidapi/SDL_hidapijoystick.c
     1.1 --- a/src/SDL_hints.c	Wed Nov 13 15:46:58 2019 -0800
     1.2 +++ b/src/SDL_hints.c	Wed Nov 13 21:53:01 2019 -0800
     1.3 @@ -119,16 +119,22 @@
     1.4  }
     1.5  
     1.6  SDL_bool
     1.7 +SDL_GetStringBoolean(const char *value, SDL_bool default_value)
     1.8 +{
     1.9 +    if (!value || !*value) {
    1.10 +        return default_value;
    1.11 +    }
    1.12 +    if (*value == '0' || SDL_strcasecmp(value, "false") == 0) {
    1.13 +        return SDL_FALSE;
    1.14 +    }
    1.15 +    return SDL_TRUE;
    1.16 +}
    1.17 +
    1.18 +SDL_bool
    1.19  SDL_GetHintBoolean(const char *name, SDL_bool default_value)
    1.20  {
    1.21      const char *hint = SDL_GetHint(name);
    1.22 -    if (!hint || !*hint) {
    1.23 -        return default_value;
    1.24 -    }
    1.25 -    if (*hint == '0' || SDL_strcasecmp(hint, "false") == 0) {
    1.26 -        return SDL_FALSE;
    1.27 -    }
    1.28 -    return SDL_TRUE;
    1.29 +    return SDL_GetStringBoolean(hint, default_value);
    1.30  }
    1.31  
    1.32  void
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/SDL_hints_c.h	Wed Nov 13 21:53:01 2019 -0800
     2.3 @@ -0,0 +1,32 @@
     2.4 +/*
     2.5 +  Simple DirectMedia Layer
     2.6 +  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
     2.7 +
     2.8 +  This software is provided 'as-is', without any express or implied
     2.9 +  warranty.  In no event will the authors be held liable for any damages
    2.10 +  arising from the use of this software.
    2.11 +
    2.12 +  Permission is granted to anyone to use this software for any purpose,
    2.13 +  including commercial applications, and to alter it and redistribute it
    2.14 +  freely, subject to the following restrictions:
    2.15 +
    2.16 +  1. The origin of this software must not be misrepresented; you must not
    2.17 +     claim that you wrote the original software. If you use this software
    2.18 +     in a product, an acknowledgment in the product documentation would be
    2.19 +     appreciated but is not required.
    2.20 +  2. Altered source versions must be plainly marked as such, and must not be
    2.21 +     misrepresented as being the original software.
    2.22 +  3. This notice may not be removed or altered from any source distribution.
    2.23 +*/
    2.24 +#include "./SDL_internal.h"
    2.25 +
    2.26 +/* This file defines useful function for working with SDL hints */
    2.27 +
    2.28 +#ifndef SDL_hints_c_h_
    2.29 +#define SDL_hints_c_h_
    2.30 +
    2.31 +extern SDL_bool SDL_GetStringBoolean(const char *value, SDL_bool default_value);
    2.32 +
    2.33 +#endif /* SDL_hints_c_h_ */
    2.34 +
    2.35 +/* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/events/SDL_mouse.c	Wed Nov 13 15:46:58 2019 -0800
     3.2 +++ b/src/events/SDL_mouse.c	Wed Nov 13 21:53:01 2019 -0800
     3.3 @@ -27,6 +27,7 @@
     3.4  #include "SDL_timer.h"
     3.5  #include "SDL_events.h"
     3.6  #include "SDL_events_c.h"
     3.7 +#include "../SDL_hints_c.h"
     3.8  #include "../video/SDL_sysvideo.h"
     3.9  #ifdef __WIN32__
    3.10  #include "../core/windows/SDL_windows.h"    // For GetDoubleClickTime()
    3.11 @@ -100,30 +101,21 @@
    3.12  {
    3.13      SDL_Mouse *mouse = (SDL_Mouse *)userdata;
    3.14  
    3.15 -    if (hint && (*hint == '0' || SDL_strcasecmp(hint, "false") == 0)) {
    3.16 -        mouse->touch_mouse_events = SDL_FALSE;
    3.17 -    } else {
    3.18 -        mouse->touch_mouse_events = SDL_TRUE;
    3.19 -    }
    3.20 +    mouse->touch_mouse_events = SDL_GetStringBoolean(hint, SDL_TRUE);
    3.21  }
    3.22  
    3.23  static void SDLCALL
    3.24  SDL_MouseTouchEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
    3.25  {
    3.26      SDL_Mouse *mouse = (SDL_Mouse *)userdata;
    3.27 +    SDL_bool default_value;
    3.28  
    3.29 -    if (hint == NULL || *hint == '\0') {
    3.30 -        /* Default */
    3.31  #if defined(__ANDROID__) || (defined(__IPHONEOS__) && !defined(__TVOS__))
    3.32 -        mouse->mouse_touch_events = SDL_TRUE;
    3.33 +    default_value = SDL_TRUE;
    3.34  #else
    3.35 -        mouse->mouse_touch_events = SDL_FALSE;
    3.36 +    default_value = SDL_FALSE;
    3.37  #endif
    3.38 -    } else if (*hint == '1' || SDL_strcasecmp(hint, "true") == 0) {
    3.39 -        mouse->mouse_touch_events = SDL_TRUE;
    3.40 -    } else {
    3.41 -        mouse->mouse_touch_events = SDL_FALSE;
    3.42 -    }
    3.43 +    mouse->mouse_touch_events = SDL_GetStringBoolean(hint, default_value);
    3.44  
    3.45      if (mouse->mouse_touch_events) {
    3.46          SDL_AddTouch(SDL_MOUSE_TOUCHID, SDL_TOUCH_DEVICE_DIRECT, "mouse_input");
     4.1 --- a/src/joystick/hidapi/SDL_hidapi_switch.c	Wed Nov 13 15:46:58 2019 -0800
     4.2 +++ b/src/joystick/hidapi/SDL_hidapi_switch.c	Wed Nov 13 21:53:01 2019 -0800
     4.3 @@ -33,6 +33,7 @@
     4.4  #include "SDL_gamecontroller.h"
     4.5  #include "../SDL_sysjoystick.h"
     4.6  #include "SDL_hidapijoystick_c.h"
     4.7 +#include "../../SDL_hints_c.h"
     4.8  
     4.9  
    4.10  #ifdef SDL_JOYSTICK_HIDAPI_SWITCH
    4.11 @@ -591,7 +592,7 @@
    4.12  static void SDLCALL SDL_GameControllerButtonReportingHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
    4.13  {
    4.14      SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)userdata;
    4.15 -    ctx->m_bUseButtonLabels = (!hint || !*hint || ((*hint != '0') && (SDL_strcasecmp(hint, "false") != 0)));
    4.16 +    ctx->m_bUseButtonLabels = SDL_GetStringBoolean(hint, SDL_TRUE);
    4.17  }
    4.18  
    4.19  static Uint8 RemapButton(SDL_DriverSwitch_Context *ctx, Uint8 button)
     5.1 --- a/src/joystick/hidapi/SDL_hidapijoystick.c	Wed Nov 13 15:46:58 2019 -0800
     5.2 +++ b/src/joystick/hidapi/SDL_hidapijoystick.c	Wed Nov 13 21:53:01 2019 -0800
     5.3 @@ -31,6 +31,7 @@
     5.4  #include "SDL_joystick.h"
     5.5  #include "../SDL_sysjoystick.h"
     5.6  #include "SDL_hidapijoystick_c.h"
     5.7 +#include "../../SDL_hints_c.h"
     5.8  
     5.9  #if defined(__WIN32__)
    5.10  #include "../../core/windows/SDL_windows.h"
    5.11 @@ -641,7 +642,7 @@
    5.12  {
    5.13      int i;
    5.14      SDL_HIDAPI_Device *device = SDL_HIDAPI_devices;
    5.15 -    SDL_bool enabled = (!hint || !*hint || ((*hint != '0') && (SDL_strcasecmp(hint, "false") != 0)));
    5.16 +    SDL_bool enabled = SDL_GetStringBoolean(hint, SDL_TRUE);
    5.17  
    5.18      if (SDL_strcmp(name, SDL_HINT_JOYSTICK_HIDAPI) == 0) {
    5.19          for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {