Skip to content

Commit

Permalink
Implemented SDL_GetHintBoolean() to make it easier to check boolean h…
Browse files Browse the repository at this point in the history
…ints
  • Loading branch information
slouken committed Oct 8, 2016
1 parent 92d700f commit 27d4f09
Show file tree
Hide file tree
Showing 26 changed files with 68 additions and 136 deletions.
7 changes: 7 additions & 0 deletions include/SDL_hints.h
Expand Up @@ -738,6 +738,13 @@ extern DECLSPEC SDL_bool SDLCALL SDL_SetHint(const char *name,
*/
extern DECLSPEC const char * SDLCALL SDL_GetHint(const char *name);

/**
* \brief Get a hint
*
* \return The boolean value of a hint variable.
*/
extern DECLSPEC SDL_bool SDLCALL SDL_GetHintBoolean(const char *name, SDL_bool default_value);

/**
* \brief Add a function to watch a particular hint
*
Expand Down
13 changes: 13 additions & 0 deletions src/SDL_hints.c
Expand Up @@ -118,6 +118,19 @@ SDL_GetHint(const char *name)
return env;
}

SDL_bool
SDL_GetHintBoolean(const char *name, SDL_bool default_value)
{
const char *hint = SDL_GetHint(name);
if (!hint) {
return default_value;
}
if (*hint == '0' || SDL_strcasecmp(hint, "false") == 0) {
return SDL_FALSE;
}
return SDL_TRUE;
}

void
SDL_AddHintCallback(const char *name, SDL_HintCallback callback, void *userdata)
{
Expand Down
9 changes: 4 additions & 5 deletions src/core/winrt/SDL_winrtapp_direct3d.cpp
Expand Up @@ -822,11 +822,8 @@ static void WINRT_OnBackButtonPressed(BackButtonEventArgs ^ args)
SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_AC_BACK);
SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_AC_BACK);

const char *hint = SDL_GetHint(SDL_HINT_WINRT_HANDLE_BACK_BUTTON);
if (hint) {
if (*hint == '1') {
args->Handled = true;
}
if (SDL_GetHintBoolean(SDL_HINT_WINRT_HANDLE_BACK_BUTTON, SDL_FALSE)) {
args->Handled = true;
}
}

Expand Down Expand Up @@ -854,3 +851,5 @@ void SDL_WinRTApp::OnGamepadAdded(Platform::Object ^sender, Windows::Gaming::Inp
*/
}
#endif

/* vi: set ts=4 sw=4 expandtab: */
1 change: 1 addition & 0 deletions src/dynapi/SDL_dynapi_overrides.h
Expand Up @@ -611,3 +611,4 @@
#define SDL_SetWindowResizable SDL_SetWindowResizable_REAL
#define SDL_CreateRGBSurfaceWithFormat SDL_CreateRGBSurfaceWithFormat_REAL
#define SDL_CreateRGBSurfaceWithFormatFrom SDL_CreateRGBSurfaceWithFormatFrom_REAL
#define SDL_GetHintBoolean SDL_GetHintBoolean_REAL
1 change: 1 addition & 0 deletions src/dynapi/SDL_dynapi_procs.h
Expand Up @@ -643,3 +643,4 @@ SDL_DYNAPI_PROC(Uint32,SDL_DequeueAudio,(SDL_AudioDeviceID a, void *b, Uint32 c)
SDL_DYNAPI_PROC(void,SDL_SetWindowResizable,(SDL_Window *a, SDL_bool b),(a,b),)
SDL_DYNAPI_PROC(SDL_Surface*,SDL_CreateRGBSurfaceWithFormat,(Uint32 a, int b, int c, int d, Uint32 e),(a,b,c,d,e),return)
SDL_DYNAPI_PROC(SDL_Surface*,SDL_CreateRGBSurfaceWithFormatFrom,(void *a, int b, int c, int d, int e, Uint32 f),(a,b,c,d,e,f),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_GetHintBoolean,(const char *a, SDL_bool b),(a,b),return)
12 changes: 1 addition & 11 deletions src/events/SDL_mouse.c
Expand Up @@ -564,21 +564,11 @@ SDL_WarpMouseGlobal(int x, int y)
static SDL_bool
ShouldUseRelativeModeWarp(SDL_Mouse *mouse)
{
const char *hint;

if (!mouse->SetRelativeMouseMode) {
return SDL_TRUE;
}

hint = SDL_GetHint(SDL_HINT_MOUSE_RELATIVE_MODE_WARP);
if (hint) {
if (*hint == '0') {
return SDL_FALSE;
} else {
return SDL_TRUE;
}
}
return SDL_FALSE;
return SDL_GetHintBoolean(SDL_HINT_MOUSE_RELATIVE_MODE_WARP, SDL_FALSE);
}

int
Expand Down
4 changes: 1 addition & 3 deletions src/events/SDL_quit.c
Expand Up @@ -91,9 +91,7 @@ SDL_QuitInit_Internal(void)
int
SDL_QuitInit(void)
{
const char *hint = SDL_GetHint(SDL_HINT_NO_SIGNAL_HANDLERS);
disable_signals = hint && (SDL_atoi(hint) == 1);
if (!disable_signals) {
if (!SDL_GetHintBoolean(SDL_HINT_NO_SIGNAL_HANDLERS, SDL_FALSE)) {
return SDL_QuitInit_Internal();
}
return 0;
Expand Down
3 changes: 1 addition & 2 deletions src/haptic/windows/SDL_xinputhaptic.c
Expand Up @@ -44,8 +44,7 @@ static SDL_bool loaded_xinput = SDL_FALSE;
int
SDL_XINPUT_HapticInit(void)
{
const char *env = SDL_GetHint(SDL_HINT_XINPUT_ENABLED);
if (!env || SDL_atoi(env)) {
if (SDL_GetHintBoolean(SDL_HINT_XINPUT_ENABLED, SDL_TRUE)) {
loaded_xinput = (WIN_LoadXInputDLL() == 0);
}

Expand Down
4 changes: 1 addition & 3 deletions src/joystick/android/SDL_sysjoystick.c
Expand Up @@ -352,11 +352,9 @@ Android_RemoveJoystick(int device_id)
int
SDL_SYS_JoystickInit(void)
{
const char *hint;
SDL_SYS_JoystickDetect();

hint = SDL_GetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK);
if (!hint || SDL_atoi(hint)) {
if (SDL_GetHintBoolean(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_TRUE)) {
/* Default behavior, accelerometer as joystick */
Android_AddJoystick(ANDROID_ACCELEROMETER_DEVICE_ID, ANDROID_ACCELEROMETER_NAME, SDL_TRUE, 0, 3, 0, 0);
}
Expand Down
7 changes: 2 additions & 5 deletions src/joystick/iphoneos/SDL_sysjoystick.m
Expand Up @@ -127,13 +127,11 @@
}
#if TARGET_OS_TV
else if (controller.microGamepad) {
const char *hint = SDL_GetHint(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION);

device->naxes = 2; /* treat the touch surface as two axes */
device->nhats = 0; /* apparently the touch surface-as-dpad is buggy */
device->nbuttons = 3; /* AX, pause button */

controller.microGamepad.allowsRotation = (hint != NULL && *hint != '0');
controller.microGamepad.allowsRotation = SDL_GetHintBoolean(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION, SDL_FALSE);
}
#endif /* TARGET_OS_TV */

Expand Down Expand Up @@ -279,8 +277,7 @@
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

#if !TARGET_OS_TV
const char *hint = SDL_GetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK);
if (!hint || SDL_atoi(hint)) {
if (SDL_GetHintBoolean(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_TRUE)) {
/* Default behavior, accelerometer as joystick */
SDL_SYS_AddJoystickDevice(nil, SDL_TRUE);
}
Expand Down
8 changes: 2 additions & 6 deletions src/joystick/windows/SDL_xinputjoystick.c
Expand Up @@ -40,8 +40,7 @@ SDL_XInputUseOldJoystickMapping()
{
static int s_XInputUseOldJoystickMapping = -1;
if (s_XInputUseOldJoystickMapping < 0) {
const char *hint = SDL_GetHint(SDL_HINT_XINPUT_USE_OLD_JOYSTICK_MAPPING);
s_XInputUseOldJoystickMapping = (hint && *hint == '1') ? 1 : 0;
s_XInputUseOldJoystickMapping = SDL_GetHintBoolean(SDL_HINT_XINPUT_USE_OLD_JOYSTICK_MAPPING, SDL_FALSE);
}
return (s_XInputUseOldJoystickMapping > 0);
}
Expand All @@ -54,10 +53,7 @@ SDL_bool SDL_XINPUT_Enabled(void)
int
SDL_XINPUT_JoystickInit(void)
{
const char *env = SDL_GetHint(SDL_HINT_XINPUT_ENABLED);
if (env && !SDL_atoi(env)) {
s_bXInputEnabled = SDL_FALSE;
}
s_bXInputEnabled = SDL_GetHintBoolean(SDL_HINT_XINPUT_ENABLED, SDL_TRUE);

if (s_bXInputEnabled && WIN_LoadXInputDLL() < 0) {
s_bXInputEnabled = SDL_FALSE; /* oh well. */
Expand Down
9 changes: 4 additions & 5 deletions src/render/SDL_render.c
Expand Up @@ -234,12 +234,11 @@ SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
return NULL;
}

hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
if (hint) {
if (*hint == '0') {
flags &= ~SDL_RENDERER_PRESENTVSYNC;
} else {
if (SDL_GetHint(SDL_HINT_RENDER_VSYNC)) {
if (SDL_GetHintBoolean(SDL_HINT_RENDER_VSYNC, SDL_TRUE)) {
flags |= SDL_RENDERER_PRESENTVSYNC;
} else {
flags &= ~SDL_RENDERER_PRESENTVSYNC;
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/render/direct3d/SDL_render_d3d.c
Expand Up @@ -512,7 +512,6 @@ D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
D3D_RenderData *data;
SDL_SysWMinfo windowinfo;
HRESULT result;
const char *hint;
D3DPRESENT_PARAMETERS pparams;
IDirect3DSwapChain9 *chain;
D3DCAPS9 caps;
Expand Down Expand Up @@ -607,8 +606,7 @@ D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
device_flags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
}

hint = SDL_GetHint(SDL_HINT_RENDER_DIRECT3D_THREADSAFE);
if (hint && SDL_atoi(hint)) {
if (SDL_GetHintBoolean(SDL_HINT_RENDER_DIRECT3D_THREADSAFE, SDL_FALSE)) {
device_flags |= D3DCREATE_MULTITHREADED;
}

Expand Down
4 changes: 1 addition & 3 deletions src/render/direct3d11/SDL_render_d3d11.c
Expand Up @@ -1000,7 +1000,6 @@ D3D11_CreateDeviceResources(SDL_Renderer * renderer)
IDXGIDevice1 *dxgiDevice = NULL;
HRESULT result = S_OK;
UINT creationFlags;
const char *hint;

/* This array defines the set of DirectX hardware feature levels this app will support.
* Note the ordering should be preserved.
Expand Down Expand Up @@ -1078,8 +1077,7 @@ D3D11_CreateDeviceResources(SDL_Renderer * renderer)
creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;

/* Make sure Direct3D's debugging feature gets used, if the app requests it. */
hint = SDL_GetHint(SDL_HINT_RENDER_DIRECT3D11_DEBUG);
if (hint && SDL_atoi(hint) > 0) {
if (SDL_GetHintBoolean(SDL_HINT_RENDER_DIRECT3D11_DEBUG, SDL_FALSE)) {
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
}

Expand Down
4 changes: 1 addition & 3 deletions src/render/opengl/SDL_render_gl.c
Expand Up @@ -390,7 +390,6 @@ GL_CreateRenderer(SDL_Window * window, Uint32 flags)
{
SDL_Renderer *renderer;
GL_RenderData *data;
const char *hint;
GLint value;
Uint32 window_flags;
int profile_mask = 0, major = 0, minor = 0;
Expand Down Expand Up @@ -528,8 +527,7 @@ GL_CreateRenderer(SDL_Window * window, Uint32 flags)
}

/* Check for shader support */
hint = SDL_GetHint(SDL_HINT_RENDER_OPENGL_SHADERS);
if (!hint || *hint != '0') {
if (SDL_GetHintBoolean(SDL_HINT_RENDER_OPENGL_SHADERS, SDL_TRUE)) {
data->shaders = GL_CreateShaderContext();
}
SDL_LogInfo(SDL_LOG_CATEGORY_RENDER, "OpenGL shaders: %s",
Expand Down
3 changes: 1 addition & 2 deletions src/thread/windows/SDL_systhread.c
Expand Up @@ -171,8 +171,7 @@ SDL_SYS_SetupThread(const char *name)
THREADNAME_INFO inf;

/* C# and friends will try to catch this Exception, let's avoid it. */
const char *hint = SDL_GetHint(SDL_HINT_WINDOWS_DISABLE_THREAD_NAMING);
if (hint && *hint == '1') {
if (SDL_GetHintBoolean(SDL_HINT_WINDOWS_DISABLE_THREAD_NAMING, SDL_FALSE)) {
return;
}

Expand Down
5 changes: 1 addition & 4 deletions src/video/SDL_bmp.c
Expand Up @@ -556,10 +556,7 @@ SDL_SaveBMP_RW(SDL_Surface * saveme, SDL_RWops * dst, int freedst)
}

if (save32bit) {
const char *hint = SDL_GetHint(SDL_HINT_BMP_SAVE_LEGACY_FORMAT);
if (hint != NULL && (hint[0] == '1' && hint[1] == 0)) {
saveLegacyBMP = SDL_TRUE;
}
saveLegacyBMP = SDL_GetHintBoolean(SDL_HINT_BMP_SAVE_LEGACY_FORMAT, SDL_FALSE);
}

if (surface && (SDL_LockSurface(surface) == 0)) {
Expand Down
41 changes: 7 additions & 34 deletions src/video/SDL_video.c
Expand Up @@ -181,7 +181,7 @@ ShouldUseTextureFramebuffer()
/* See if the user or application wants a specific behavior */
hint = SDL_GetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION);
if (hint) {
if (*hint == '0') {
if (*hint == '0' || SDL_strcasecmp(hint, "false") == 0) {
return SDL_FALSE;
} else {
return SDL_TRUE;
Expand Down Expand Up @@ -258,6 +258,8 @@ SDL_CreateWindowTexture(SDL_VideoDevice *unused, SDL_Window * window, Uint32 * f

/* Check to see if there's a specific driver requested */
if (hint && *hint != '0' && *hint != '1' &&
SDL_strcasecmp(hint, "true") != 0 &&
SDL_strcasecmp(hint, "false") != 0 &&
SDL_strcasecmp(hint, "software") != 0) {
for (i = 0; i < SDL_GetNumRenderDrivers(); ++i) {
SDL_RendererInfo info;
Expand Down Expand Up @@ -447,10 +449,8 @@ int
SDL_VideoInit(const char *driver_name)
{
SDL_VideoDevice *video;
const char *hint;
int index;
int i;
SDL_bool allow_screensaver;

/* Check to make sure we don't overwrite '_this' */
if (_this != NULL) {
Expand Down Expand Up @@ -538,13 +538,7 @@ SDL_VideoInit(const char *driver_name)
joystick, or passively watching a movie. Things that use SDL but
function more like a normal desktop app should explicitly reenable the
screensaver. */
hint = SDL_GetHint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER);
if (hint) {
allow_screensaver = SDL_atoi(hint) ? SDL_TRUE : SDL_FALSE;
} else {
allow_screensaver = SDL_FALSE;
}
if (!allow_screensaver) {
if (!SDL_GetHintBoolean(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, SDL_FALSE)) {
SDL_DisableScreenSaver();
}

Expand Down Expand Up @@ -1336,7 +1330,6 @@ SDL_Window *
SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
{
SDL_Window *window;
const char *hint;

if (!_this) {
/* Initialize the video system if needed */
Expand Down Expand Up @@ -1384,8 +1377,7 @@ SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
* SDL_WINDOW_ALLOW_HIGHDPI flag.
*/
if (flags & SDL_WINDOW_ALLOW_HIGHDPI) {
hint = SDL_GetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED);
if (hint && SDL_atoi(hint) > 0) {
if (SDL_GetHintBoolean(SDL_HINT_VIDEO_HIGHDPI_DISABLED, SDL_FALSE)) {
flags &= ~SDL_WINDOW_ALLOW_HIGHDPI;
}
}
Expand Down Expand Up @@ -2509,8 +2501,6 @@ SDL_OnWindowFocusGained(SDL_Window * window)
static SDL_bool
ShouldMinimizeOnFocusLoss(SDL_Window * window)
{
const char *hint;

if (!(window->flags & SDL_WINDOW_FULLSCREEN) || window->is_destroying) {
return SDL_FALSE;
}
Expand All @@ -2521,16 +2511,7 @@ ShouldMinimizeOnFocusLoss(SDL_Window * window)
}
#endif

hint = SDL_GetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS);
if (hint) {
if (*hint == '0') {
return SDL_FALSE;
} else {
return SDL_TRUE;
}
}

return SDL_TRUE;
return SDL_GetHintBoolean(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, SDL_TRUE);
}

void
Expand Down Expand Up @@ -3779,15 +3760,7 @@ SDL_ShowSimpleMessageBox(Uint32 flags, const char *title, const char *message, S
SDL_bool
SDL_ShouldAllowTopmost(void)
{
const char *hint = SDL_GetHint(SDL_HINT_ALLOW_TOPMOST);
if (hint) {
if (*hint == '0') {
return SDL_FALSE;
} else {
return SDL_TRUE;
}
}
return SDL_TRUE;
return SDL_GetHintBoolean(SDL_HINT_ALLOW_TOPMOST, SDL_TRUE);
}

int
Expand Down
3 changes: 1 addition & 2 deletions src/video/cocoa/SDL_cocoaevents.m
Expand Up @@ -348,8 +348,7 @@ - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filenam
[SDLApplication sharedApplication];
SDL_assert(NSApp != nil);

const char *hint = SDL_GetHint(SDL_HINT_MAC_BACKGROUND_APP);
if (!hint || *hint == '0') {
if (!SDL_GetHintBoolean(SDL_HINT_MAC_BACKGROUND_APP, SDL_FALSE)) {
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp activateIgnoringOtherApps:YES];
}
Expand Down
3 changes: 1 addition & 2 deletions src/video/cocoa/SDL_cocoavideo.m
Expand Up @@ -150,8 +150,7 @@
Cocoa_InitKeyboard(_this);
Cocoa_InitMouse(_this);

const char *hint = SDL_GetHint(SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES);
data->allow_spaces = ( (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6) && (!hint || (*hint != '0')) );
data->allow_spaces = ((floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6) && SDL_GetHintBoolean(SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES, SDL_TRUE));

/* The IOPM assertion API can disable the screensaver as of 10.7. */
data->screensaver_use_iopm = floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6;
Expand Down

0 comments on commit 27d4f09

Please sign in to comment.