1
0
mirror of https://github.com/gryf/wmaker.git synced 2026-03-19 09:13:33 +01:00

wmaker: factorize duplicated code

This patch is factorizing is_same and getBool functions in misc.
is_same is renamed to WMStrEqual
getBool is renamed to WMPLGetBool
to prevent name collision issues.
This commit is contained in:
David Maciejak
2026-02-03 18:36:55 -05:00
committed by Carlos R. Mafra
parent 92e1e9fb0b
commit 1e63c590b6
6 changed files with 53 additions and 130 deletions

View File

@@ -124,6 +124,44 @@ Bool wGetIconName(Display *dpy, Window win, char **iconname)
return False;
}
int WMStrEqual(const char *x, const char *y)
{
if ((x == NULL) && (y == NULL))
return 1;
if ((x == NULL) || (y == NULL))
return 0;
return (strcmp(x, y) == 0);
}
int WMPLGetBool(WMPropList *value)
{
char *val;
if (!WMIsPLString(value))
return 0;
val = WMGetFromPLString(value);
if (val == NULL)
return 0;
if ((val[1] == '\0' &&
(val[0] == 'y' || val[0] == 'Y' || val[0] == 'T' ||
val[0] == 't' || val[0] == '1')) ||
(strcasecmp(val, "YES") == 0 || strcasecmp(val, "TRUE") == 0)) {
return 1;
} else if ((val[1] == '\0' &&
(val[0] == 'n' || val[0] == 'N' || val[0] == 'F' ||
val[0] == 'f' || val[0] == '0')) ||
(strcasecmp(val, "NO") == 0 || strcasecmp(val, "FALSE") == 0)) {
return 0;
} else {
wwarning(_("can't convert \"%s\" to boolean"), val);
return 0;
}
}
static void eatExpose(void)
{
XEvent event, foo;

View File

@@ -54,4 +54,6 @@ char *GetShortcutKey(WShortKey key);
char *EscapeWM_CLASS(const char *name, const char *class);
char *StrConcatDot(const char *a, const char *b);
char *GetCommandForWindow(Window win);
int WMStrEqual(const char *x, const char *y);
int WMPLGetBool(WMPropList *value);
#endif

View File

@@ -123,35 +123,6 @@ static void make_keys(void)
sNo = WMCreatePLString("No");
}
static int getBool(WMPropList * value)
{
char *val;
if (!WMIsPLString(value)) {
return 0;
}
val = WMGetFromPLString(value);
if (val == NULL)
return 0;
if ((val[1] == '\0' && (val[0] == 'y' || val[0] == 'Y'))
|| strcasecmp(val, "YES") == 0) {
return 1;
} else if ((val[1] == '\0' && (val[0] == 'n' || val[0] == 'N'))
|| strcasecmp(val, "NO") == 0) {
return 0;
} else {
int i;
if (sscanf(val, "%i", &i) == 1) {
return (i != 0);
} else {
wwarning(_("can't convert \"%s\" to boolean"), val);
return 0;
}
}
}
static unsigned getInt(WMPropList * value)
{
char *val;
@@ -423,11 +394,11 @@ static WSavedState *getWindowState(WScreen * scr, WMPropList * win_state)
value = WMGetFromPLDictionary(win_state, sShaded);
if (value != NULL)
state->shaded = getBool(value);
state->shaded = WMPLGetBool(value);
value = WMGetFromPLDictionary(win_state, sMiniaturized);
if (value != NULL)
state->miniaturized = getBool(value);
state->miniaturized = WMPLGetBool(value);
value = WMGetFromPLDictionary(win_state, sMaximized);
if (value != NULL) {
@@ -436,7 +407,7 @@ static WSavedState *getWindowState(WScreen * scr, WMPropList * win_state)
value = WMGetFromPLDictionary(win_state, sHidden);
if (value != NULL)
state->hidden = getBool(value);
state->hidden = WMPLGetBool(value);
value = WMGetFromPLDictionary(win_state, sShortcutMask);
if (value != NULL) {
@@ -456,20 +427,6 @@ static WSavedState *getWindowState(WScreen * scr, WMPropList * win_state)
return state;
}
static inline int is_same(const char *x, const char *y)
{
if ((x == NULL) && (y == NULL))
return 1;
if ((x == NULL) || (y == NULL))
return 0;
if (strcmp(x, y) == 0)
return 1;
else
return 0;
}
void wSessionRestoreState(WScreen *scr)
{
WSavedState *state;
@@ -556,9 +513,9 @@ void wSessionRestoreState(WScreen *scr)
if (dock != NULL) {
for (j = 0; j < dock->max_icons; j++) {
btn = dock->icon_array[j];
if (btn && is_same(instance, btn->wm_instance) &&
is_same(class, btn->wm_class) &&
is_same(command, btn->command) &&
if (btn && WMStrEqual(instance, btn->wm_instance) &&
WMStrEqual(class, btn->wm_class) &&
WMStrEqual(command, btn->command) &&
!btn->launching) {
found = 1;
break;

View File

@@ -43,14 +43,13 @@
#include "icon.h"
#include "misc.h"
#define APPLY_VAL(value, flag, attrib) \
if (value) {attr->flag = getBool(attrib, value); \
if (mask) mask->flag = 1;}
#define APPLY_VAL(value, flag, attrib) \
if (value) {attr->flag = WMPLGetBool(value); \
if (mask) mask->flag = 1;}
/* Local stuff */
/* type converters */
static int getBool(WMPropList *, WMPropList *);
static char *getString(WMPropList *, WMPropList *);
static WMPropList *ANoTitlebar = NULL;
static WMPropList *ANoResizebar;
@@ -634,38 +633,6 @@ void wDefaultPurgeInfo(const char *instance, const char *class)
WMPLSetCaseSensitive(False);
}
/* --------------------------- Local ----------------------- */
static int getBool(WMPropList * key, WMPropList * value)
{
char *val;
if (!WMIsPLString(value)) {
wwarning(_("Wrong option format for key \"%s\". Should be %s."),
WMGetFromPLString(key), "Boolean");
return 0;
}
val = WMGetFromPLString(value);
if ((val[1] == '\0' && (val[0] == 'y' || val[0] == 'Y' || val[0] == 'T' || val[0] == 't' || val[0] == '1'))
|| (strcasecmp(val, "YES") == 0 || strcasecmp(val, "TRUE") == 0)) {
return 1;
} else if ((val[1] == '\0'
&& (val[0] == 'n' || val[0] == 'N' || val[0] == 'F' || val[0] == 'f' || val[0] == '0'))
|| (strcasecmp(val, "NO") == 0 || strcasecmp(val, "FALSE") == 0)) {
return 0;
} else {
wwarning(_("can't convert \"%s\" to boolean"), val);
/* We return False if we can't convert to BOOLEAN.
* This is because all options defaults to False.
* -1 is not checked and thus is interpreted as True,
* which is not good.*/
return 0;
}
}
/* WARNING: Do not free the value returned by this function!! */
static char *getString(WMPropList * key, WMPropList * value)
{

View File

@@ -2726,20 +2726,6 @@ WMagicNumber wWindowAddSavedState(const char *instance, const char *class,
return wstate;
}
static inline int is_same(const char *x, const char *y)
{
if ((x == NULL) && (y == NULL))
return 1;
if ((x == NULL) || (y == NULL))
return 0;
if (strcmp(x, y) == 0)
return 1;
else
return 0;
}
WMagicNumber wWindowGetSavedState(Window win)
{
char *instance, *class, *command = NULL;
@@ -2754,9 +2740,9 @@ WMagicNumber wWindowGetSavedState(Window win)
if (PropGetWMClass(win, &class, &instance)) {
while (wstate) {
if (is_same(instance, wstate->instance) &&
is_same(class, wstate->class) &&
is_same(command, wstate->command)) {
if (WMStrEqual(instance, wstate->instance) &&
WMStrEqual(class, wstate->class) &&
WMStrEqual(command, wstate->command)) {
break;
}
wstate = wstate->next;

View File

@@ -504,33 +504,6 @@ static int showIconFor(WMScreen *scrPtr, InspectorPanel *panel, const char *wm_i
return 0;
}
static int getBool(WMPropList *value)
{
char *val;
if (!WMIsPLString(value))
return 0;
val = WMGetFromPLString(value);
if (val == NULL)
return 0;
if ((val[1] == '\0' &&
(val[0] == 'y' || val[0] == 'Y' || val[0] == 'T' ||
val[0] == 't' || val[0] == '1')) ||
(strcasecmp(val, "YES") == 0 || strcasecmp(val, "TRUE") == 0)) {
return 1;
} else if ((val[1] == '\0' &&
(val[0] == 'n' || val[0] == 'N' || val[0] == 'F' ||
val[0] == 'f' || val[0] == '0')) ||
(strcasecmp(val, "NO") == 0 || strcasecmp(val, "FALSE") == 0)) {
return 0;
} else {
wwarning(_("can't convert \"%s\" to boolean"), val);
return 0;
}
}
/* Will insert the attribute = value; pair in window's list,
* if it's different from the defaults.
* Defaults means either defaults database, or attributes saved
@@ -554,7 +527,7 @@ insertAttribute(WMPropList *dict, WMPropList *window, WMPropList *attr, WMPropLi
def_value = ((flags & IS_BOOLEAN) != 0) ? No : EmptyString;
if (flags & IS_BOOLEAN)
update = (getBool(value) != getBool(def_value));
update = (WMPLGetBool(value) != WMPLGetBool(def_value));
else
update = !WMIsPropListEqualTo(value, def_value);