diff --git a/include/common/get-bool.h b/include/common/get-bool.h deleted file mode 100644 index 9c480fde..00000000 --- a/include/common/get-bool.h +++ /dev/null @@ -1,15 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -#ifndef __LABWC_GET_BOOL_H -#define __LABWC_GET_BOOL_H -#include - -/** - * get_bool - interpret string and return boolean - * @s: string to interpret - * - * Note: This merely performs a case-insensitive check for 'yes' and 'true'. - * Returns false by default. - */ -bool get_bool(const char *s); - -#endif /* __LABWC_GET_BOOL_H */ diff --git a/include/common/parse-bool.h b/include/common/parse-bool.h new file mode 100644 index 00000000..b2d4d6bb --- /dev/null +++ b/include/common/parse-bool.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __LABWC_PARSE_BOOL_H +#define __LABWC_PARSE_BOOL_H +#include + +/** + * parse_bool() - Parse boolean value of string. + * @string: String to interpret. This check is case-insensitive. + * @default_value: Default value to use if string is not a recognised boolean. + * Use -1 to avoid setting a default value. + * + * Return: 0 for false; 1 for true; -1 for non-boolean + */ +int parse_bool(const char *str, int default_value); + +#endif /* __LABWC_PARSE_BOOL_H */ diff --git a/src/action.c b/src/action.c index 92098194..9106059c 100644 --- a/src/action.c +++ b/src/action.c @@ -7,9 +7,9 @@ #include #include #include "action.h" -#include "common/get-bool.h" #include "common/list.h" #include "common/mem.h" +#include "common/parse-bool.h" #include "common/spawn.h" #include "debug.h" #include "labwc.h" @@ -154,7 +154,7 @@ action_arg_from_xml_node(struct action *action, char *nodename, char *content) action_arg_add_str(action, "to", content); } else if (!strcmp(nodename, "follow.action")) { /* SendToDesktop */ - action_arg_add_bool(action, "follow", get_bool(content)); + action_arg_add_bool(action, "follow", parse_bool(content, true)); } else if (!strcmp(nodename, "region.action")) { /* SnapToRegion */ action_arg_add_str(action, NULL, content); diff --git a/src/common/get-bool.c b/src/common/get-bool.c deleted file mode 100644 index afddbb4c..00000000 --- a/src/common/get-bool.c +++ /dev/null @@ -1,19 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-only -#include -#include -#include "common/get-bool.h" - -bool -get_bool(const char *s) -{ - if (!s) { - return false; - } - if (!strcasecmp(s, "yes")) { - return true; - } - if (!strcasecmp(s, "true")) { - return true; - } - return false; -} diff --git a/src/common/meson.build b/src/common/meson.build index 5f21c258..68326d31 100644 --- a/src/common/meson.build +++ b/src/common/meson.build @@ -3,11 +3,11 @@ labwc_sources += files( 'dir.c', 'fd_util.c', 'font.c', - 'get-bool.c', 'grab-file.c', 'graphic-helpers.c', 'mem.c', 'nodename.c', + 'parse-bool.c', 'scaled_font_buffer.c', 'scaled_scene_buffer.c', 'scene-helpers.c', diff --git a/src/common/parse-bool.c b/src/common/parse-bool.c new file mode 100644 index 00000000..0e1d4b13 --- /dev/null +++ b/src/common/parse-bool.c @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include "common/parse-bool.h" + +int +parse_bool(const char *str, int default_value) +{ + if (!str) { + goto error_not_a_boolean; + } else if (!strcasecmp(str, "yes")) { + return true; + } else if (!strcasecmp(str, "true")) { + return true; + } else if (!strcasecmp(str, "no")) { + return false; + } else if (!strcasecmp(str, "false")) { + return false; + } +error_not_a_boolean: + wlr_log(WLR_ERROR, "(%s) is not a boolean value", str); + return default_value; +} + diff --git a/src/config/rcxml.c b/src/config/rcxml.c index d64807d3..910a95c5 100644 --- a/src/config/rcxml.c +++ b/src/config/rcxml.c @@ -15,10 +15,10 @@ #include #include #include "action.h" -#include "common/get-bool.h" #include "common/list.h" #include "common/mem.h" #include "common/nodename.h" +#include "common/parse-bool.h" #include "common/string-helpers.h" #include "config/keybind.h" #include "config/libinput.h" @@ -265,10 +265,17 @@ fill_libinput_category(char *nodename, char *content) current_libinput_category->name = xstrdup(content); } } else if (!strcasecmp(nodename, "naturalScroll")) { - current_libinput_category->natural_scroll = - get_bool(content) ? 1 : 0; + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + current_libinput_category->natural_scroll = ret; } else if (!strcasecmp(nodename, "leftHanded")) { - current_libinput_category->left_handed = get_bool(content) ? 1 : 0; + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + current_libinput_category->left_handed = ret; } else if (!strcasecmp(nodename, "pointerSpeed")) { current_libinput_category->pointer_speed = atof(content); if (current_libinput_category->pointer_speed < -1) { @@ -277,9 +284,12 @@ fill_libinput_category(char *nodename, char *content) current_libinput_category->pointer_speed = 1; } } else if (!strcasecmp(nodename, "tap")) { - current_libinput_category->tap = get_bool(content) ? - LIBINPUT_CONFIG_TAP_ENABLED : - LIBINPUT_CONFIG_TAP_DISABLED; + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + current_libinput_category->tap = ret ? LIBINPUT_CONFIG_TAP_ENABLED + : LIBINPUT_CONFIG_TAP_DISABLED; } else if (!strcasecmp(nodename, "tapButtonMap")) { if (!strcmp(content, "lrm")) { current_libinput_category->tap_button_map = @@ -294,13 +304,20 @@ fill_libinput_category(char *nodename, char *content) current_libinput_category->accel_profile = get_accel_profile(content); } else if (!strcasecmp(nodename, "middleEmulation")) { - current_libinput_category->middle_emu = get_bool(content) ? - LIBINPUT_CONFIG_MIDDLE_EMULATION_ENABLED : - LIBINPUT_CONFIG_MIDDLE_EMULATION_DISABLED; + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + current_libinput_category->middle_emu = ret + ? LIBINPUT_CONFIG_MIDDLE_EMULATION_ENABLED + : LIBINPUT_CONFIG_MIDDLE_EMULATION_DISABLED; } else if (!strcasecmp(nodename, "disableWhileTyping")) { - current_libinput_category->dwt = get_bool(content) ? - LIBINPUT_CONFIG_DWT_ENABLED : - LIBINPUT_CONFIG_DWT_DISABLED; + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + current_libinput_category->dwt = ret ? LIBINPUT_CONFIG_DWT_ENABLED + : LIBINPUT_CONFIG_DWT_DISABLED; } } @@ -438,9 +455,17 @@ entry(xmlNode *node, char *nodename, char *content) } else if (!strcmp(nodename, "gap.core")) { rc.gap = atoi(content); } else if (!strcasecmp(nodename, "adaptiveSync.core")) { - rc.adaptive_sync = get_bool(content); + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + rc.adaptive_sync = ret; } else if (!strcasecmp(nodename, "reuseOutputMode.core")) { - rc.reuse_output_mode = get_bool(content); + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + rc.reuse_output_mode = ret; } else if (!strcmp(nodename, "name.theme")) { rc.theme_name = xstrdup(content); } else if (!strcmp(nodename, "cornerradius.theme")) { @@ -454,9 +479,17 @@ entry(xmlNode *node, char *nodename, char *content) } else if (!strcmp(nodename, "weight.font.theme")) { fill_font(nodename, content, font_place); } else if (!strcasecmp(nodename, "followMouse.focus")) { - rc.focus_follow_mouse = get_bool(content); + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + rc.focus_follow_mouse = ret; } else if (!strcasecmp(nodename, "raiseOnFocus.focus")) { - rc.raise_on_focus = get_bool(content); + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + rc.raise_on_focus = ret; } else if (!strcasecmp(nodename, "doubleClickTime.mouse")) { long doubleclick_time_parsed = strtol(content, NULL, 10); if (doubleclick_time_parsed > 0) { @@ -478,15 +511,31 @@ entry(xmlNode *node, char *nodename, char *content) } else if (!strcasecmp(nodename, "range.snapping")) { rc.snap_edge_range = atoi(content); } else if (!strcasecmp(nodename, "topMaximize.snapping")) { - rc.snap_top_maximize = get_bool(content); + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + rc.snap_top_maximize = ret; /* */ } else if (!strcasecmp(nodename, "show.windowSwitcher")) { - rc.window_switcher.show = get_bool(content); + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + rc.window_switcher.show = ret; } else if (!strcasecmp(nodename, "preview.windowSwitcher")) { - rc.window_switcher.preview = get_bool(content); + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + rc.window_switcher.preview = ret; } else if (!strcasecmp(nodename, "outlines.windowSwitcher")) { - rc.window_switcher.outlines = get_bool(content); + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + rc.window_switcher.outlines = ret; /* Remove this long term - just a friendly warning for now */ } else if (strstr(nodename, "windowswitcher.core")) { @@ -494,23 +543,47 @@ entry(xmlNode *node, char *nodename, char *content) /* The following three are for backward compatibility only */ } else if (!strcasecmp(nodename, "show.windowSwitcher.core")) { - rc.window_switcher.show = get_bool(content); + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + rc.window_switcher.show = ret; } else if (!strcasecmp(nodename, "preview.windowSwitcher.core")) { - rc.window_switcher.preview = get_bool(content); + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + rc.window_switcher.preview = ret; } else if (!strcasecmp(nodename, "outlines.windowSwitcher.core")) { - rc.window_switcher.outlines = get_bool(content); + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + rc.window_switcher.outlines = ret; /* The following three are for backward compatibility only */ } else if (!strcasecmp(nodename, "cycleViewOSD.core")) { - rc.window_switcher.show = get_bool(content); + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + rc.window_switcher.show = ret; wlr_log(WLR_ERROR, " is deprecated." " Use "); } else if (!strcasecmp(nodename, "cycleViewPreview.core")) { - rc.window_switcher.preview = get_bool(content); + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + rc.window_switcher.preview = ret; wlr_log(WLR_ERROR, " is deprecated." " Use "); } else if (!strcasecmp(nodename, "cycleViewOutlines.core")) { - rc.window_switcher.outlines = get_bool(content); + int ret = parse_bool(content, -1); + if (ret < 0) { + return; + } + rc.window_switcher.outlines = ret; wlr_log(WLR_ERROR, " is deprecated." " Use ");