diff --git a/button.c b/button.c index 805a51e9..2a5d5cca 100644 --- a/button.c +++ b/button.c @@ -21,7 +21,6 @@ #include "button.h" -#include "common/xutil.h" #include "common/tokenize.h" DO_LUA_TOSTRING(button_t, button, "button") @@ -59,7 +58,6 @@ luaA_button_gc(lua_State *L) static int luaA_button_new(lua_State *L) { - int len; xcb_button_t xbutton; button_t *button, *orig; luaA_ref press = LUA_REFNIL, release = LUA_REFNIL; @@ -110,16 +108,7 @@ luaA_button_new(lua_State *L) button->release = release; button->button = xbutton; - len = lua_objlen(L, 2); - for(int i = 1; i <= len; i++) - { - size_t blen; - const char *buf; - lua_rawgeti(L, 2, i); - buf = luaL_checklstring(L, -1, &blen); - button->mod |= xutil_key_mask_fromstr(buf, blen); - lua_pop(L, 1); - } + luaA_setmodifiers(L, 2, &button->mod); return 1; } diff --git a/key.c b/key.c index d267adcd..2047e67f 100644 --- a/key.c +++ b/key.c @@ -301,7 +301,7 @@ luaA_keystore(keyb_t *key, const char *str, ssize_t len) static int luaA_key_new(lua_State *L) { - size_t i, len; + size_t len; keyb_t *k; const char *key; luaA_ref press = LUA_REFNIL, release = LUA_REFNIL; @@ -323,15 +323,7 @@ luaA_key_new(lua_State *L) k->press = press; k->release = release; - len = lua_objlen(L, 2); - for(i = 1; i <= len; i++) - { - size_t blen; - lua_rawgeti(L, 2, i); - key = luaL_checklstring(L, -1, &blen); - lua_pop(L, 1); - k->mod |= xutil_key_mask_fromstr(key, blen); - } + luaA_setmodifiers(L, 2, &k->mod); return 1; } @@ -400,6 +392,25 @@ luaA_pushmodifiers(lua_State *L, uint16_t modifiers) return 1; } +/** Take a modifier table from the stack and set modifiers in mod. + * \param L The Lua VM state. + * \param ud The index of the table. + * \param mod Where to set the modifiers. + */ +void +luaA_setmodifiers(lua_State *L, int ud, uint16_t *mod) +{ + ssize_t len = lua_objlen(L, ud); + for(int i = 1; i <= len; i++) + { + lua_rawgeti(L, ud, i); + size_t blen; + const char *key = luaL_checklstring(L, -1, &blen); + *mod |= xutil_key_mask_fromstr(key, blen); + lua_pop(L, 1); + } +} + /** Key object. * \param L The Lua VM state. * \return The number of elements pushed on stack. diff --git a/key.h b/key.h index 48ebecb6..9f4cf2de 100644 --- a/key.h +++ b/key.h @@ -57,5 +57,6 @@ int luaA_key_array_get(lua_State *, keybindings_t *); void window_grabkeys(xcb_window_t, keybindings_t *); int luaA_pushmodifiers(lua_State *, uint16_t); +void luaA_setmodifiers(lua_State *, int, uint16_t *); #endif