From 0e8fc995bbbfd1f5fee98f6d98e401fe2837ed8a Mon Sep 17 00:00:00 2001 From: Majic Date: Thu, 17 Nov 2011 07:38:39 -0800 Subject: [PATCH] Minor readability fixes, STREQ()-like macros added Signed-off-by: Julien Danjou --- common/util.h | 9 +++++++++ common/xcursor.c | 2 +- common/xutil.c | 18 ++++++++--------- dbus.c | 4 ++-- draw.c | 2 +- luaa.c | 32 +++++++++++++++++++------------ mouse.c | 50 +++++++++++++++++++----------------------------- objects/client.c | 4 ++-- objects/window.c | 31 +++++++++++++++--------------- root.c | 10 +++++----- screen.c | 30 +++++++++++++++++++++-------- spawn.c | 6 +++--- 12 files changed, 110 insertions(+), 88 deletions(-) diff --git a/common/util.h b/common/util.h index 8310ed5b9..920b85f56 100644 --- a/common/util.h +++ b/common/util.h @@ -234,6 +234,9 @@ static inline int a_strcmp(const char *a, const char *b) return strcmp(NONULL(a), NONULL(b)); } +#define A_STREQ(a, b) (((a) == (b)) || a_strcmp(a, b) == 0) +#define A_STRNEQ(a, b) (!A_STREQ(a, b)) + /** \brief \c NULL resistant strcasecmp. * \param[in] a the first string. * \param[in] b the second string. @@ -245,6 +248,9 @@ static inline int a_strcasecmp(const char *a, const char *b) return strcasecmp(NONULL(a), NONULL(b)); } +#define A_STREQ_CASE(a, b) (((a) == (b)) || a_strcasecmp(a, b) == 0) +#define A_STRNEQ_CASE(a, b) (!A_STRCASEEQ(a, b)) + /** \brief \c NULL resistant strncmp. * \param[in] a the first string. * \param[in] b the second string. @@ -257,6 +263,9 @@ static inline int a_strncmp(const char *a, const char *b, ssize_t n) return strncmp(NONULL(a), NONULL(b), n); } +#define A_STREQ_N(a, b, n) (((a) == (b)) || (n) == ((ssize_t) 0) || a_strncmp(a, b, n) == 0) +#define A_STRNEQ_N(a, b) (!A_STREQN(a, b)) + ssize_t a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l) __attribute__((nonnull(1))); ssize_t a_strcpy(char *dst, ssize_t n, const char *src) __attribute__((nonnull(1))); diff --git a/common/xcursor.c b/common/xcursor.c index 2f8824e85..1849626a0 100644 --- a/common/xcursor.c +++ b/common/xcursor.c @@ -114,7 +114,7 @@ xcursor_font_fromstr(const char *s) { if(s) for(int i = 0; i < countof(xcursor_font); i++) - if(xcursor_font[i] && !a_strcmp(s, xcursor_font[i])) + if(xcursor_font[i] && A_STREQ(s, xcursor_font[i])) return i; return 0; } diff --git a/common/xutil.c b/common/xutil.c index a34e48864..f8ecfc9b8 100644 --- a/common/xutil.c +++ b/common/xutil.c @@ -91,23 +91,23 @@ xutil_lock_mask_get(xcb_connection_t *connection, uint16_t xutil_key_mask_fromstr(const char *keyname) { - if(a_strcmp(keyname, "Shift") == 0) + if (A_STREQ(keyname, "Shift")) return XCB_MOD_MASK_SHIFT; - if(a_strcmp(keyname, "Lock") == 0) + if (A_STREQ(keyname, "Lock")) return XCB_MOD_MASK_LOCK; - if(a_strcmp(keyname, "Ctrl") == 0 || a_strcmp(keyname, "Control") == 0) + if (A_STREQ(keyname, "Ctrl") || A_STREQ(keyname, "Control")) return XCB_MOD_MASK_CONTROL; - if(a_strcmp(keyname, "Mod1") == 0) + if (A_STREQ(keyname, "Mod1")) return XCB_MOD_MASK_1; - if(a_strcmp(keyname, "Mod2") == 0) + if(A_STREQ(keyname, "Mod2")) return XCB_MOD_MASK_2; - if(a_strcmp(keyname, "Mod3") == 0) + if(A_STREQ(keyname, "Mod3")) return XCB_MOD_MASK_3; - if(a_strcmp(keyname, "Mod4") == 0) + if(A_STREQ(keyname, "Mod4")) return XCB_MOD_MASK_4; - if(a_strcmp(keyname, "Mod5") == 0) + if(A_STREQ(keyname, "Mod5")) return XCB_MOD_MASK_5; - if(a_strcmp(keyname, "Any") == 0) + if(A_STREQ(keyname, "Any")) /* this is misnamed but correct */ return XCB_BUTTON_MASK_ANY; return XCB_NO_SYMBOL; diff --git a/dbus.c b/dbus.c index 9380165a9..48676f5ec 100644 --- a/dbus.c +++ b/dbus.c @@ -657,9 +657,9 @@ a_dbus_cleanup(void) static DBusConnection * a_dbus_bus_getbyname(const char *name) { - if(a_strcmp(name, "system") == 0) + if(A_STREQ(name, "system")) return dbus_connection_system; - if(a_strcmp(name, "session") == 0) + if(A_STREQ(name, "session")) return dbus_connection_session; return NULL; } diff --git a/draw.c b/draw.c index 580aecc5c..16e63c466 100644 --- a/draw.c +++ b/draw.c @@ -49,7 +49,7 @@ draw_iso2utf8(const char *iso, size_t len, char **dest, ssize_t *dlen) static int8_t dont_need_convert = -1; if(dont_need_convert == -1) - dont_need_convert = !a_strcmp(nl_langinfo(CODESET), "UTF-8"); + dont_need_convert = A_STREQ(nl_langinfo(CODESET), "UTF-8"); if(!len || dont_need_convert) return false; diff --git a/luaa.c b/luaa.c index 256ed3bb7..3c33814e5 100644 --- a/luaa.c +++ b/luaa.c @@ -390,23 +390,31 @@ luaA_awesome_index(lua_State *L) const char *buf = luaL_checkstring(L, 2); - if(a_strcmp(buf, "conffile") == 0) - lua_pushstring(L, conffile); - else if(a_strcmp(buf, "version") == 0) - lua_pushliteral(L, AWESOME_VERSION); - else if(a_strcmp(buf, "release") == 0) - lua_pushliteral(L, AWESOME_RELEASE); - else if(a_strcmp(buf, "startup_errors") == 0) + if(A_STREQ(buf, "conffile")) + { + lua_pushstring(L, conffile); + return 1; + } + + if(A_STREQ(buf, "version")) + { + lua_pushliteral(L, AWESOME_VERSION); + return 1; + } + + if(A_STREQ(buf, "release")) + { + lua_pushliteral(L, AWESOME_RELEASE); + return 1; + } + + if(A_STREQ(buf, "startup_errors") && globalconf.startup_errors.len != 0) { - if (globalconf.startup_errors.len == 0) - return 0; lua_pushstring(L, globalconf.startup_errors.s); return 1; } - else - return 0; - return 1; + return 0; } /** Add a global signal. diff --git a/mouse.c b/mouse.c index 889755a52..732baac5b 100644 --- a/mouse.c +++ b/mouse.c @@ -48,6 +48,7 @@ mouse_query_pointer(xcb_window_t window, int16_t *x, int16_t *y, xcb_window_t *c *x = query_ptr_r->win_x; *y = query_ptr_r->win_y; + if(child) *child = query_ptr_r->child; @@ -63,16 +64,10 @@ mouse_query_pointer(xcb_window_t window, int16_t *x, int16_t *y, xcb_window_t *c * \param mask This will be set to the current buttons state. * \return True on success, false if an error occurred. */ -static bool +static inline bool mouse_query_pointer_root(int16_t *x, int16_t *y, xcb_window_t *child) { - xcb_window_t root = globalconf.screen->root; - - if(mouse_query_pointer(root, x, y, child)) - { - return true; - } - return false; + return mouse_query_pointer(globalconf.screen->root, x, y, child); } /** Set the pointer position. @@ -84,7 +79,7 @@ static inline void mouse_warp_pointer(xcb_window_t window, int x, int y) { xcb_warp_pointer(globalconf.connection, XCB_NONE, window, - 0, 0, 0, 0, x, y ); + 0, 0, 0, 0, x, y); } /** Mouse library. @@ -101,17 +96,15 @@ luaA_mouse_index(lua_State *L) int16_t mouse_x, mouse_y; screen_t *screen; - if(a_strcmp(attr, "screen") == 0) - { - if(!mouse_query_pointer_root(&mouse_x, &mouse_y, NULL)) - return 0; - - screen = screen_getbycoord(mouse_x, mouse_y); - - lua_pushnumber(L, screen_array_indexof(&globalconf.screens, screen) + 1); - } else + /* attr is not "screen"?! */ + if (A_STRNEQ(attr, "screen")) return 0; + if (!mouse_query_pointer_root(&mouse_x, &mouse_y, NULL)) + return 0; + + screen = screen_getbycoord(mouse_x, mouse_y); + lua_pushnumber(L, screen_array_indexof(&globalconf.screens, screen) + 1); return 1; } @@ -126,17 +119,16 @@ luaA_mouse_newindex(lua_State *L) int x, y = 0; int screen; - if(a_strcmp(attr, "screen") == 0) - { - screen = luaL_checknumber(L, 3) - 1; - luaA_checkscreen(screen); + if (A_STRNEQ(attr, "screen")) + return 0; - x = globalconf.screens.tab[screen].geometry.x; - y = globalconf.screens.tab[screen].geometry.y; + screen = luaL_checknumber(L, 3) - 1; + luaA_checkscreen(screen); - mouse_warp_pointer(globalconf.screen->root, x, y); - } + x = globalconf.screens.tab[screen].geometry.x; + y = globalconf.screens.tab[screen].geometry.y; + mouse_warp_pointer(globalconf.screen->root, x, y); return 0; } @@ -223,13 +215,11 @@ luaA_mouse_object_under_pointer(lua_State *L) drawin_t *drawin; client_t *client; + if((drawin = drawin_getbywin(child))) - { luaA_object_push(L, drawin); - return 1; - } - else if((client = client_getbyframewin(child))) + if((client = client_getbyframewin(child))) return luaA_object_push(globalconf.L, client); return 0; diff --git a/objects/client.c b/objects/client.c index e7c26ea5d..452f801f3 100644 --- a/objects/client.c +++ b/objects/client.c @@ -1587,7 +1587,7 @@ luaA_client_module_index(lua_State *L) { const char *buf = luaL_checkstring(L, 2); - if(a_strcmp(buf, "focus") == 0) + if (A_STREQ(buf, "focus")) return luaA_object_push(globalconf.L, globalconf.focus.client); return 0; } @@ -1602,7 +1602,7 @@ luaA_client_module_newindex(lua_State *L) const char *buf = luaL_checkstring(L, 2); client_t *c; - if(a_strcmp(buf, "focus") == 0) + if (A_STREQ(buf, "focus")) { c = luaA_checkudata(L, 3, &client_class); client_focus(c); diff --git a/objects/window.c b/objects/window.c index 0b9c6e699..02e5ace50 100644 --- a/objects/window.c +++ b/objects/window.c @@ -251,33 +251,33 @@ luaA_window_set_type(lua_State *L, window_t *w) window_type_t type; const char *buf = luaL_checkstring(L, -1); - if(a_strcmp(buf, "desktop") == 0) + if (A_STREQ(buf, "desktop")) type = WINDOW_TYPE_DESKTOP; - else if(a_strcmp(buf, "dock") == 0) + else if(A_STREQ(buf, "dock")) type = WINDOW_TYPE_DOCK; - else if(a_strcmp(buf, "splash") == 0) + else if(A_STREQ(buf, "splash")) type = WINDOW_TYPE_SPLASH; - else if(a_strcmp(buf, "dialog") == 0) + else if(A_STREQ(buf, "dialog")) type = WINDOW_TYPE_DIALOG; - else if(a_strcmp(buf, "menu") == 0) + else if(A_STREQ(buf, "menu")) type = WINDOW_TYPE_MENU; - else if(a_strcmp(buf, "toolbar") == 0) + else if(A_STREQ(buf, "toolbar")) type = WINDOW_TYPE_TOOLBAR; - else if(a_strcmp(buf, "utility") == 0) + else if(A_STREQ(buf, "utility")) type = WINDOW_TYPE_UTILITY; - else if(a_strcmp(buf, "dropdown_menu") == 0) + else if(A_STREQ(buf, "dropdown_menu")) type = WINDOW_TYPE_DROPDOWN_MENU; - else if(a_strcmp(buf, "popup_menu") == 0) + else if(A_STREQ(buf, "popup_menu")) type = WINDOW_TYPE_POPUP_MENU; - else if(a_strcmp(buf, "tooltip") == 0) + else if(A_STREQ(buf, "tooltip")) type = WINDOW_TYPE_TOOLTIP; - else if(a_strcmp(buf, "notification") == 0) + else if(A_STREQ(buf, "notification")) type = WINDOW_TYPE_NOTIFICATION; - else if(a_strcmp(buf, "combo") == 0) + else if(A_STREQ(buf, "combo")) type = WINDOW_TYPE_COMBO; - else if(a_strcmp(buf, "dnd") == 0) + else if(A_STREQ(buf, "dnd")) type = WINDOW_TYPE_DND; - else if(a_strcmp(buf, "normal") == 0) + else if(A_STREQ(buf, "normal")) type = WINDOW_TYPE_NORMAL; else { @@ -289,9 +289,10 @@ luaA_window_set_type(lua_State *L, window_t *w) { w->type = type; if(w->window != XCB_WINDOW_NONE) - ewmh_update_window_type(w->window, window_translate_type(type)); + ewmh_update_window_type(w->window, window_translate_type(w->type)); luaA_object_emit_signal(globalconf.L, -3, "property::type", 0); } + return 0; } diff --git a/root.c b/root.c index 47ded9f6b..e2733521f 100644 --- a/root.c +++ b/root.c @@ -55,27 +55,27 @@ luaA_root_fake_input(lua_State *L) uint8_t type, detail; int x = 0, y = 0; - if(a_strcmp(stype, "key_press") == 0) + if (A_STREQ(stype, "key_press")) { type = XCB_KEY_PRESS; detail = luaL_checknumber(L, 2); /* keycode */ } - else if(a_strcmp(stype, "key_release") == 0) + else if(A_STREQ(stype, "key_release")) { type = XCB_KEY_RELEASE; detail = luaL_checknumber(L, 2); /* keycode */ } - else if(a_strcmp(stype, "button_press") == 0) + else if(A_STREQ(stype, "button_press")) { type = XCB_BUTTON_PRESS; detail = luaL_checknumber(L, 2); /* button number */ } - else if(a_strcmp(stype, "button_release") == 0) + else if(A_STREQ(stype, "button_release")) { type = XCB_BUTTON_RELEASE; detail = luaL_checknumber(L, 2); /* button number */ } - else if(a_strcmp(stype, "motion_notify") == 0) + else if(A_STREQ(stype, "motion_notify")) { type = XCB_MOTION_NOTIFY; detail = luaA_checkboolean(L, 2); /* relative to the current position or not */ diff --git a/screen.c b/screen.c index 511f73117..3dc70d49c 100644 --- a/screen.c +++ b/screen.c @@ -425,7 +425,7 @@ luaA_screen_module_index(lua_State *L) if((name = lua_tostring(L, 2))) foreach(screen, globalconf.screens) foreach(output, screen->outputs) - if(!a_strcmp(output->name, name)) + if(A_STREQ(output->name, name)) return luaA_pushscreen(L, screen); int screen = luaL_checknumber(L, 2) - 1; @@ -505,29 +505,43 @@ luaA_screen_index(lua_State *L) ps = luaL_checkudata(L, 1, "screen"); s = *ps; - if(a_strcmp(buf, "index") == 0) + if(A_STREQ(buf, "index")) + { lua_pushinteger(L, screen_array_indexof(&globalconf.screens, s) + 1); - else if(a_strcmp(buf, "geometry") == 0) + return 1; + } + + if(A_STREQ(buf, "geometry")) + { luaA_pusharea(L, s->geometry); - else if(a_strcmp(buf, "workarea") == 0) + return 1; + } + + if(A_STREQ(buf, "workarea")) + { luaA_pusharea(L, screen_area_get(s, true)); - else if(a_strcmp(buf, "outputs") == 0) + return 1; + } + + if(A_STREQ(buf, "outputs")) { lua_createtable(L, 0, s->outputs.len); foreach(output, s->outputs) { lua_createtable(L, 2, 0); + lua_pushinteger(L, output->mm_width); lua_setfield(L, -2, "mm_width"); lua_pushinteger(L, output->mm_height); lua_setfield(L, -2, "mm_height"); + lua_setfield(L, -2, output->name); } + /* The table of tables we created. */ + return 1; } - else - return 0; - return 1; + return 0; } /** A screen. diff --git a/spawn.c b/spawn.c index 468977a02..f1edaa91c 100644 --- a/spawn.c +++ b/spawn.c @@ -210,17 +210,17 @@ spawn_start_notify(client_t *c, const char * startup_id) bool found = false; const char *seqid = sn_startup_sequence_get_id(seq); - if(!a_strcmp(seqid, startup_id)) + if (A_STRNEQ(seqid, startup_id)) found = true; else { const char *seqclass = sn_startup_sequence_get_wmclass(seq); - if(!a_strcmp(seqclass, c->class) || !a_strcmp(seqclass, c->instance)) + if (A_STREQ(seqclass, c->class) || A_STREQ(seqclass, c->instance)) found = true; else { const char *seqbin = sn_startup_sequence_get_binary_name(seq); - if(!a_strcasecmp(seqbin, c->class) || !a_strcasecmp(seqbin, c->instance)) + if (A_STREQ_CASE(seqbin, c->class) || A_STREQ_CASE(seqbin, c->instance)) found = true; } }