From 2b0e70dc5b1b4811186589dc724060b55c02ce1a Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Sun, 25 May 2008 14:13:50 +0200 Subject: [PATCH] [statusbar] Check for names and uniqness before adding, add remove function Signed-off-by: Julien Danjou --- statusbar.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/statusbar.c b/statusbar.c index 8949ccb8..af2dbe77 100644 --- a/statusbar.c +++ b/statusbar.c @@ -369,13 +369,24 @@ static int luaA_statusbar_add(lua_State *L) { statusbar_t *s, **sb = luaL_checkudata(L, 1, "statusbar"); - int screen = luaL_checknumber(L, 2) - 1; + int i, screen = luaL_checknumber(L, 2) - 1; luaA_checkscreen(screen); + /* Check for uniq name and id. */ + for(i = 0; i < globalconf.screens_info->nscreen; i++) + for(s = globalconf.screens[i].statusbar; s; s = s->next) + { + if(s == *sb) + luaL_error(L, "this statusbar is already on screen %d", + s->screen + 1); + if(!a_strcmp(s->name, (*sb)->name)) + luaL_error(L, "a statusbar with that name is already on screen %d\n", + s->screen + 1); + } + (*sb)->screen = screen; - /* \todo check for uniq name */ statusbar_list_append(&globalconf.screens[screen].statusbar, *sb); for(s = globalconf.screens[(*sb)->screen].statusbar; s; s = s->next) statusbar_position_update(s, s->position); @@ -386,6 +397,28 @@ luaA_statusbar_add(lua_State *L) return 0; } +static int +luaA_statusbar_remove(lua_State *L) +{ + statusbar_t *s, **sb = luaL_checkudata(L, 1, "statusbar"); + int i; + + for(i = 0; i < globalconf.screens_info->nscreen; i++) + for(s = globalconf.screens[i].statusbar; s; s = s->next) + if(s == *sb) + { + statusbar_position_update(*sb, Off); + statusbar_list_detach(&globalconf.screens[i].statusbar, *sb); + statusbar_unref(sb); + globalconf.screens[i].need_arrange = true; + return 0; + } + + luaL_error(L, "unable to remove statusbar: not on any screen"); + + return 0; +} + static int luaA_statusbar_new(lua_State *L) { @@ -449,6 +482,7 @@ const struct luaL_reg awesome_statusbar_meta[] = { "position_set", luaA_statusbar_position_set }, { "align_set", luaA_statusbar_align_set }, { "add", luaA_statusbar_add }, + { "remove", luaA_statusbar_remove }, { "__gc", luaA_statusbar_gc }, { "__eq", luaA_statusbar_eq }, { "__tostring", luaA_statusbar_tostring },