1
0
mirror of https://github.com/gryf/wmaker.git synced 2026-03-22 19:43:31 +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; 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) static void eatExpose(void)
{ {
XEvent event, foo; XEvent event, foo;

View File

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

View File

@@ -123,35 +123,6 @@ static void make_keys(void)
sNo = WMCreatePLString("No"); 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) static unsigned getInt(WMPropList * value)
{ {
char *val; char *val;
@@ -423,11 +394,11 @@ static WSavedState *getWindowState(WScreen * scr, WMPropList * win_state)
value = WMGetFromPLDictionary(win_state, sShaded); value = WMGetFromPLDictionary(win_state, sShaded);
if (value != NULL) if (value != NULL)
state->shaded = getBool(value); state->shaded = WMPLGetBool(value);
value = WMGetFromPLDictionary(win_state, sMiniaturized); value = WMGetFromPLDictionary(win_state, sMiniaturized);
if (value != NULL) if (value != NULL)
state->miniaturized = getBool(value); state->miniaturized = WMPLGetBool(value);
value = WMGetFromPLDictionary(win_state, sMaximized); value = WMGetFromPLDictionary(win_state, sMaximized);
if (value != NULL) { if (value != NULL) {
@@ -436,7 +407,7 @@ static WSavedState *getWindowState(WScreen * scr, WMPropList * win_state)
value = WMGetFromPLDictionary(win_state, sHidden); value = WMGetFromPLDictionary(win_state, sHidden);
if (value != NULL) if (value != NULL)
state->hidden = getBool(value); state->hidden = WMPLGetBool(value);
value = WMGetFromPLDictionary(win_state, sShortcutMask); value = WMGetFromPLDictionary(win_state, sShortcutMask);
if (value != NULL) { if (value != NULL) {
@@ -456,20 +427,6 @@ static WSavedState *getWindowState(WScreen * scr, WMPropList * win_state)
return 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) void wSessionRestoreState(WScreen *scr)
{ {
WSavedState *state; WSavedState *state;
@@ -556,9 +513,9 @@ void wSessionRestoreState(WScreen *scr)
if (dock != NULL) { if (dock != NULL) {
for (j = 0; j < dock->max_icons; j++) { for (j = 0; j < dock->max_icons; j++) {
btn = dock->icon_array[j]; btn = dock->icon_array[j];
if (btn && is_same(instance, btn->wm_instance) && if (btn && WMStrEqual(instance, btn->wm_instance) &&
is_same(class, btn->wm_class) && WMStrEqual(class, btn->wm_class) &&
is_same(command, btn->command) && WMStrEqual(command, btn->command) &&
!btn->launching) { !btn->launching) {
found = 1; found = 1;
break; break;

View File

@@ -44,13 +44,12 @@
#include "misc.h" #include "misc.h"
#define APPLY_VAL(value, flag, attrib) \ #define APPLY_VAL(value, flag, attrib) \
if (value) {attr->flag = getBool(attrib, value); \ if (value) {attr->flag = WMPLGetBool(value); \
if (mask) mask->flag = 1;} if (mask) mask->flag = 1;}
/* Local stuff */ /* Local stuff */
/* type converters */ /* type converters */
static int getBool(WMPropList *, WMPropList *);
static char *getString(WMPropList *, WMPropList *); static char *getString(WMPropList *, WMPropList *);
static WMPropList *ANoTitlebar = NULL; static WMPropList *ANoTitlebar = NULL;
static WMPropList *ANoResizebar; static WMPropList *ANoResizebar;
@@ -634,38 +633,6 @@ void wDefaultPurgeInfo(const char *instance, const char *class)
WMPLSetCaseSensitive(False); 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!! */ /* WARNING: Do not free the value returned by this function!! */
static char *getString(WMPropList * key, WMPropList * value) static char *getString(WMPropList * key, WMPropList * value)
{ {

View File

@@ -2726,20 +2726,6 @@ WMagicNumber wWindowAddSavedState(const char *instance, const char *class,
return wstate; 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) WMagicNumber wWindowGetSavedState(Window win)
{ {
char *instance, *class, *command = NULL; char *instance, *class, *command = NULL;
@@ -2754,9 +2740,9 @@ WMagicNumber wWindowGetSavedState(Window win)
if (PropGetWMClass(win, &class, &instance)) { if (PropGetWMClass(win, &class, &instance)) {
while (wstate) { while (wstate) {
if (is_same(instance, wstate->instance) && if (WMStrEqual(instance, wstate->instance) &&
is_same(class, wstate->class) && WMStrEqual(class, wstate->class) &&
is_same(command, wstate->command)) { WMStrEqual(command, wstate->command)) {
break; break;
} }
wstate = wstate->next; wstate = wstate->next;

View File

@@ -504,33 +504,6 @@ static int showIconFor(WMScreen *scrPtr, InspectorPanel *panel, const char *wm_i
return 0; 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, /* Will insert the attribute = value; pair in window's list,
* if it's different from the defaults. * if it's different from the defaults.
* Defaults means either defaults database, or attributes saved * 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; def_value = ((flags & IS_BOOLEAN) != 0) ? No : EmptyString;
if (flags & IS_BOOLEAN) if (flags & IS_BOOLEAN)
update = (getBool(value) != getBool(def_value)); update = (WMPLGetBool(value) != WMPLGetBool(def_value));
else else
update = !WMIsPropListEqualTo(value, def_value); update = !WMIsPropListEqualTo(value, def_value);