From: Johan Malm Date: Wed, 26 Apr 2023 19:34:48 +0000 (+0100) Subject: parse-bool.c: add helpers to set bool/int iff valid boolean X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=ac1899ada5c2a8b0aac8dee2620def0db812d0af;p=proto%2Flabwc.git parse-bool.c: add helpers to set bool/int iff valid boolean --- diff --git a/include/common/parse-bool.h b/include/common/parse-bool.h index b2d4d6bb..900a83f3 100644 --- a/include/common/parse-bool.h +++ b/include/common/parse-bool.h @@ -13,4 +13,12 @@ */ int parse_bool(const char *str, int default_value); +/** + * set_bool() - Parse boolean text and set variable iff text is valid boolean + * @string: Boolean text to interpret. + * @variable: Variable to set. + */ +void set_bool(const char *str, bool *variable); +void set_bool_as_int(const char *str, int *variable); + #endif /* __LABWC_PARSE_BOOL_H */ diff --git a/src/common/parse-bool.c b/src/common/parse-bool.c index 0e1d4b13..0c10e9de 100644 --- a/src/common/parse-bool.c +++ b/src/common/parse-bool.c @@ -23,3 +23,22 @@ error_not_a_boolean: return default_value; } +void +set_bool(const char *str, bool *variable) +{ + int ret = parse_bool(str, -1); + if (ret < 0) { + return; + } + *variable = ret; +} + +void +set_bool_as_int(const char *str, int *variable) +{ + int ret = parse_bool(str, -1); + if (ret < 0) { + return; + } + *variable = ret; +} diff --git a/src/config/rcxml.c b/src/config/rcxml.c index 910a95c5..283affc0 100644 --- a/src/config/rcxml.c +++ b/src/config/rcxml.c @@ -265,17 +265,9 @@ fill_libinput_category(char *nodename, char *content) current_libinput_category->name = xstrdup(content); } } else if (!strcasecmp(nodename, "naturalScroll")) { - int ret = parse_bool(content, -1); - if (ret < 0) { - return; - } - current_libinput_category->natural_scroll = ret; + set_bool_as_int(content, ¤t_libinput_category->natural_scroll); } else if (!strcasecmp(nodename, "leftHanded")) { - int ret = parse_bool(content, -1); - if (ret < 0) { - return; - } - current_libinput_category->left_handed = ret; + set_bool_as_int(content, ¤t_libinput_category->left_handed); } else if (!strcasecmp(nodename, "pointerSpeed")) { current_libinput_category->pointer_speed = atof(content); if (current_libinput_category->pointer_speed < -1) { @@ -288,7 +280,8 @@ fill_libinput_category(char *nodename, char *content) if (ret < 0) { return; } - current_libinput_category->tap = ret ? LIBINPUT_CONFIG_TAP_ENABLED + current_libinput_category->tap = ret + ? LIBINPUT_CONFIG_TAP_ENABLED : LIBINPUT_CONFIG_TAP_DISABLED; } else if (!strcasecmp(nodename, "tapButtonMap")) { if (!strcmp(content, "lrm")) { @@ -316,7 +309,8 @@ fill_libinput_category(char *nodename, char *content) if (ret < 0) { return; } - current_libinput_category->dwt = ret ? LIBINPUT_CONFIG_DWT_ENABLED + current_libinput_category->dwt = ret + ? LIBINPUT_CONFIG_DWT_ENABLED : LIBINPUT_CONFIG_DWT_DISABLED; } } @@ -455,17 +449,9 @@ entry(xmlNode *node, char *nodename, char *content) } else if (!strcmp(nodename, "gap.core")) { rc.gap = atoi(content); } else if (!strcasecmp(nodename, "adaptiveSync.core")) { - int ret = parse_bool(content, -1); - if (ret < 0) { - return; - } - rc.adaptive_sync = ret; + set_bool(content, &rc.adaptive_sync); } else if (!strcasecmp(nodename, "reuseOutputMode.core")) { - int ret = parse_bool(content, -1); - if (ret < 0) { - return; - } - rc.reuse_output_mode = ret; + set_bool(content, &rc.reuse_output_mode); } else if (!strcmp(nodename, "name.theme")) { rc.theme_name = xstrdup(content); } else if (!strcmp(nodename, "cornerradius.theme")) { @@ -479,17 +465,9 @@ 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")) { - int ret = parse_bool(content, -1); - if (ret < 0) { - return; - } - rc.focus_follow_mouse = ret; + set_bool(content, &rc.focus_follow_mouse); } else if (!strcasecmp(nodename, "raiseOnFocus.focus")) { - int ret = parse_bool(content, -1); - if (ret < 0) { - return; - } - rc.raise_on_focus = ret; + set_bool(content, &rc.raise_on_focus); } else if (!strcasecmp(nodename, "doubleClickTime.mouse")) { long doubleclick_time_parsed = strtol(content, NULL, 10); if (doubleclick_time_parsed > 0) { @@ -511,31 +489,15 @@ 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")) { - int ret = parse_bool(content, -1); - if (ret < 0) { - return; - } - rc.snap_top_maximize = ret; + set_bool(content, &rc.snap_top_maximize); /* */ } else if (!strcasecmp(nodename, "show.windowSwitcher")) { - int ret = parse_bool(content, -1); - if (ret < 0) { - return; - } - rc.window_switcher.show = ret; + set_bool(content, &rc.window_switcher.show); } else if (!strcasecmp(nodename, "preview.windowSwitcher")) { - int ret = parse_bool(content, -1); - if (ret < 0) { - return; - } - rc.window_switcher.preview = ret; + set_bool(content, &rc.window_switcher.preview); } else if (!strcasecmp(nodename, "outlines.windowSwitcher")) { - int ret = parse_bool(content, -1); - if (ret < 0) { - return; - } - rc.window_switcher.outlines = ret; + set_bool(content, &rc.window_switcher.outlines); /* Remove this long term - just a friendly warning for now */ } else if (strstr(nodename, "windowswitcher.core")) { @@ -543,47 +505,23 @@ entry(xmlNode *node, char *nodename, char *content) /* The following three are for backward compatibility only */ } else if (!strcasecmp(nodename, "show.windowSwitcher.core")) { - int ret = parse_bool(content, -1); - if (ret < 0) { - return; - } - rc.window_switcher.show = ret; + set_bool(content, &rc.window_switcher.show); } else if (!strcasecmp(nodename, "preview.windowSwitcher.core")) { - int ret = parse_bool(content, -1); - if (ret < 0) { - return; - } - rc.window_switcher.preview = ret; + set_bool(content, &rc.window_switcher.preview); } else if (!strcasecmp(nodename, "outlines.windowSwitcher.core")) { - int ret = parse_bool(content, -1); - if (ret < 0) { - return; - } - rc.window_switcher.outlines = ret; + set_bool(content, &rc.window_switcher.outlines); /* The following three are for backward compatibility only */ } else if (!strcasecmp(nodename, "cycleViewOSD.core")) { - int ret = parse_bool(content, -1); - if (ret < 0) { - return; - } - rc.window_switcher.show = ret; + set_bool(content, &rc.window_switcher.show); wlr_log(WLR_ERROR, " is deprecated." " Use "); } else if (!strcasecmp(nodename, "cycleViewPreview.core")) { - int ret = parse_bool(content, -1); - if (ret < 0) { - return; - } - rc.window_switcher.preview = ret; + set_bool(content, &rc.window_switcher.preview); wlr_log(WLR_ERROR, " is deprecated." " Use "); } else if (!strcasecmp(nodename, "cycleViewOutlines.core")) { - int ret = parse_bool(content, -1); - if (ret < 0) { - return; - } - rc.window_switcher.outlines = ret; + set_bool(content, &rc.window_switcher.outlines); wlr_log(WLR_ERROR, " is deprecated." " Use ");