From d261f9a3cc6f5a983f2abadc026c91ee549c34d9 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Tue, 19 Aug 2008 19:13:46 +0200 Subject: [PATCH] widgets: generic set/get for list Signed-off-by: Julien Danjou --- common/tokenize.gperf | 1 - lib/awful.lua.in | 2 +- statusbar.c | 41 +++--------------------------- titlebar.c | 53 +++++++++++--------------------------- widget.c | 59 +++++++++++++++++++++++++++++++++++++++++++ widget.h | 2 ++ 6 files changed, 80 insertions(+), 78 deletions(-) diff --git a/common/tokenize.gperf b/common/tokenize.gperf index a06ab1c26..f18e76489 100644 --- a/common/tokenize.gperf +++ b/common/tokenize.gperf @@ -70,7 +70,6 @@ true urgent visible vertical -widgets width workarea yes diff --git a/lib/awful.lua.in b/lib/awful.lua.in index 748f26b74..3cb119054 100644 --- a/lib/awful.lua.in +++ b/lib/awful.lua.in @@ -1419,7 +1419,7 @@ end -- @param c The client to update. function titlebar.update(c) if c.titlebar and titlebar.data[c] then - local widgets = c.titlebar.widgets + local widgets = c.titlebar:widgets() local title, close for k, v in ipairs(widgets) do if v.name == "title" then title = v end diff --git a/statusbar.c b/statusbar.c index bb7535756..80be44018 100644 --- a/statusbar.c +++ b/statusbar.c @@ -541,49 +541,14 @@ static int luaA_statusbar_widgets(lua_State *L) { statusbar_t **statusbar = luaA_checkudata(L, 1, "statusbar"); - widget_node_t *witer; if(lua_gettop(L) == 2) { - luaA_checktable(L, 2); - - /* remove all widgets */ - for(witer = (*statusbar)->widgets; witer; witer = (*statusbar)->widgets) - { - if(witer->widget->detach) - witer->widget->detach(witer->widget, *statusbar); - widget_unref(&witer->widget); - widget_node_list_detach(&(*statusbar)->widgets, witer); - p_delete(&witer); - } - + luaA_widget_set(L, 2, *statusbar, &(*statusbar)->widgets); (*statusbar)->need_update = true; - - /* now read all widgets and add them */ - lua_pushnil(L); - while(lua_next(L, 2)) - { - widget_t **widget = luaA_checkudata(L, -1, "widget"); - widget_node_t *w = p_new(widget_node_t, 1); - w->widget = *widget; - widget_node_list_append(&(*statusbar)->widgets, w); - widget_ref(widget); - lua_pop(L, 1); - } - lua_pop(L, 1); + return 1; } - else - { - int i = 0; - lua_newtable(L); - for(witer = (*statusbar)->widgets; witer; witer = witer->next) - { - luaA_widget_userdata_new(L, witer->widget); - lua_rawseti(L, -2, ++i); - } - } - - return 1; + return luaA_widget_get(L, (*statusbar)->widgets); } /** Remove a statubar from a screen. diff --git a/titlebar.c b/titlebar.c index eaa4f1ee2..27c41975b 100644 --- a/titlebar.c +++ b/titlebar.c @@ -372,7 +372,6 @@ luaA_titlebar_newindex(lua_State *L) const char *buf, *attr = luaL_checklstring(L, 2, &len); client_t *c = NULL, **newc; int i; - widget_node_t *witer; position_t position; switch(a_tokenize(attr, len)) @@ -449,33 +448,6 @@ luaA_titlebar_newindex(lua_State *L) globalconf.default_screen, buf, len))) (*titlebar)->need_update = true; - return 0; - case A_TK_WIDGETS: - luaA_checktable(L, 3); - - /* remove all widgets */ - for(witer = (*titlebar)->widgets; witer; witer = (*titlebar)->widgets) - { - if(witer->widget->detach) - witer->widget->detach(witer->widget, *titlebar); - widget_unref(&witer->widget); - widget_node_list_detach(&(*titlebar)->widgets, witer); - p_delete(&witer); - } - - (*titlebar)->need_update = true; - - /* now read all widgets and add them */ - lua_pushnil(L); - while(lua_next(L, 3)) - { - widget_t **widget = luaA_checkudata(L, -1, "widget"); - widget_node_t *w = p_new(widget_node_t, 1); - w->widget = *widget; - widget_node_list_append(&(*titlebar)->widgets, w); - widget_ref(widget); - lua_pop(L, 1); - } break; case A_TK_POSITION: buf = luaL_checklstring(L, 3, &len); @@ -518,8 +490,6 @@ luaA_titlebar_index(lua_State *L) titlebar_t **titlebar = luaA_checkudata(L, 1, "titlebar"); const char *attr = luaL_checklstring(L, 2, &len); client_t *c; - widget_node_t *witer; - int i = 0; if(luaA_usemetatable(L, 1, 2)) return 1; @@ -546,14 +516,6 @@ luaA_titlebar_index(lua_State *L) case A_TK_BG: luaA_pushcolor(L, &(*titlebar)->colors.bg); break; - case A_TK_WIDGETS: - lua_newtable(L); - for(witer = (*titlebar)->widgets; witer; witer = witer->next) - { - luaA_widget_userdata_new(L, witer->widget); - lua_rawseti(L, -2, ++i); - } - break; case A_TK_POSITION: lua_pushstring(L, position_tostr((*titlebar)->position)); break; @@ -564,6 +526,20 @@ luaA_titlebar_index(lua_State *L) return 1; } +static int +luaA_titlebar_widgets(lua_State *L) +{ + titlebar_t **titlebar = luaA_checkudata(L, 1, "titlebar"); + + if(lua_gettop(L) == 2) + { + luaA_widget_set(L, 2, *titlebar, &(*titlebar)->widgets); + (*titlebar)->need_update = true; + return 1; + } + return luaA_widget_get(L, (*titlebar)->widgets); +} + /** Convert a titlebar to a printable string. * \param L The Lua VM state. * \return The number of value pushed. @@ -587,6 +563,7 @@ const struct luaL_reg awesome_titlebar_methods[] = }; const struct luaL_reg awesome_titlebar_meta[] = { + { "widgets", luaA_titlebar_widgets }, { "__index", luaA_titlebar_index }, { "__newindex", luaA_titlebar_newindex }, { "__eq", luaA_titlebar_eq }, diff --git a/widget.c b/widget.c index b30eaa960..cb4ea4701 100644 --- a/widget.c +++ b/widget.c @@ -401,6 +401,65 @@ luaA_widget_newindex(lua_State *L) return (*widget)->newindex ? (*widget)->newindex(L, token) : 0; } +/** Generic widget set. + * \param L The Lua VM state. + * \param idx The table of widgets index. + * \param object The object the widget will be attached to. + * \param widgets The widgets to fill. + * \return The number of elements pushed on stack. + */ +void +luaA_widget_set(lua_State *L, int idx, void *object, widget_node_t **widgets) +{ + widget_node_t *witer; + + luaA_checktable(L, idx); + + /* remove all widgets */ + for(witer = *widgets; witer; witer = *widgets) + { + if(witer->widget->detach) + witer->widget->detach(witer->widget, object); + widget_unref(&witer->widget); + widget_node_list_detach(widgets, witer); + p_delete(&witer); + } + + /* now read all widgets and add them */ + lua_pushnil(L); + while(lua_next(L, idx)) + { + widget_t **widget = luaA_checkudata(L, -1, "widget"); + widget_node_t *w = p_new(widget_node_t, 1); + w->widget = *widget; + widget_node_list_append(widgets, w); + widget_ref(widget); + lua_pop(L, 1); + } +} + +/** Generic widget get. + * \param L The Lua VM state. + * \param widget The widget list. + * \return The number of elements pushed on stack. + */ +int +luaA_widget_get(lua_State *L, widget_node_t *widgets) +{ + widget_node_t *witer; + int i = 0; + + lua_newtable(L); + + for(witer = widgets; witer; witer = witer->next) + { + luaA_widget_userdata_new(L, witer->widget); + lua_rawseti(L, -2, ++i); + } + + return 1; +} + const struct luaL_reg awesome_widget_methods[] = { { "__call", luaA_widget_new }, diff --git a/widget.h b/widget.h index 8c9a0a225..83127bab7 100644 --- a/widget.h +++ b/widget.h @@ -32,6 +32,8 @@ void widget_invalidate_cache(int, int); int widget_calculate_offset(int, int, int, int); void widget_common_new(widget_t *); void widget_render(widget_node_t *, draw_context_t *, xcb_gcontext_t, xcb_drawable_t, int, position_t, int, int, void *, awesome_type_t); +void luaA_widget_set(lua_State *, int, void *, widget_node_t **); +int luaA_widget_get(lua_State *, widget_node_t *); int luaA_widget_userdata_new(lua_State *, widget_t *);