common/parse-bool.c: make parse_bool() generic

...to avoid multiple versions of a boolean-parser.

- Optionally take a default value
- Return -1 on error
- Rename get-bool.c to parse-bool.c
This commit is contained in:
Johan Malm 2023-03-26 22:34:44 +01:00 committed by Johan Malm
parent 473f0aacbd
commit fa50149525
7 changed files with 144 additions and 64 deletions

View file

@ -1,15 +0,0 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef __LABWC_GET_BOOL_H
#define __LABWC_GET_BOOL_H
#include <stdbool.h>
/**
* 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 */

View file

@ -0,0 +1,16 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef __LABWC_PARSE_BOOL_H
#define __LABWC_PARSE_BOOL_H
#include <stdbool.h>
/**
* 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 */

View file

@ -7,9 +7,9 @@
#include <unistd.h>
#include <wlr/util/log.h>
#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);

View file

@ -1,19 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <string.h>
#include <strings.h>
#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;
}

View file

@ -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',

25
src/common/parse-bool.c Normal file
View file

@ -0,0 +1,25 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <string.h>
#include <strings.h>
#include <wlr/util/log.h>
#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;
}

View file

@ -15,10 +15,10 @@
#include <wlr/util/box.h>
#include <wlr/util/log.h>
#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;
/* <windowSwitcher show="" preview="" outlines="" /> */
} 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, "<cycleViewOSD> is deprecated."
" Use <windowSwitcher show=\"\" />");
} 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, "<cycleViewPreview> is deprecated."
" Use <windowSwitcher preview=\"\" />");
} 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, "<cycleViewOutlines> is deprecated."
" Use <windowSwitcher outlines=\"\" />");