tag: use new Lua object system

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2009-04-11 11:45:55 +02:00
parent 5931bf752d
commit 4531a3a17c
6 changed files with 96 additions and 102 deletions

View File

@ -1449,33 +1449,26 @@ luaA_client_swap(lua_State *L)
static int static int
luaA_client_tags(lua_State *L) luaA_client_tags(lua_State *L)
{ {
tag_array_t *tags;
tag_t **tag;
client_t *c = luaA_client_checkudata(L, 1); client_t *c = luaA_client_checkudata(L, 1);
tag_array_t *tags = &globalconf.screens[c->screen].tags;
int j = 0; int j = 0;
if(lua_gettop(L) == 2) if(lua_gettop(L) == 2)
{ {
luaA_checktable(L, 2); luaA_checktable(L, 2);
tags = &globalconf.screens[c->screen].tags;
for(int i = 0; i < tags->len; i++) for(int i = 0; i < tags->len; i++)
untag_client(c, tags->tab[i]); untag_client(c, tags->tab[i]);
lua_pushnil(L); lua_pushnil(L);
while(lua_next(L, 2)) while(lua_next(L, 2))
{ tag_client(c);
tag = luaA_checkudata(L, -1, "tag");
tag_client(c, *tag);
lua_pop(L, 1);
}
lua_pop(L, 1); lua_pop(L, 1);
} }
tags = &globalconf.screens[c->screen].tags;
lua_newtable(L); lua_newtable(L);
for(int i = 0; i < tags->len; i++) foreach(tag, *tags)
if(is_client_tagged(c, tags->tab[i])) if(is_client_tagged(c, *tag))
{ {
luaA_tag_userdata_new(L, tags->tab[i]); tag_push(L, *tag);
lua_rawseti(L, -2, ++j); lua_rawseti(L, -2, ++j);
} }

10
ewmh.c
View File

@ -406,7 +406,10 @@ ewmh_process_client_message(xcb_client_message_event_t *ev)
else else
for(int i = 0; i < tags->len; i++) for(int i = 0; i < tags->len; i++)
if((int)ev->data.data32[0] == i) if((int)ev->data.data32[0] == i)
tag_client(c, tags->tab[i]); {
tag_push(globalconf.L, tags->tab[i]);
tag_client(c);
}
else else
untag_client(c, tags->tab[i]); untag_client(c, tags->tab[i]);
} }
@ -540,7 +543,10 @@ ewmh_client_check_hints(client_t *c)
else else
for(int i = 0; i < tags->len; i++) for(int i = 0; i < tags->len; i++)
if(desktop == i) if(desktop == i)
tag_client(c, tags->tab[i]); {
tag_push(globalconf.L, tags->tab[i]);
tag_client(c);
}
else else
untag_client(c, tags->tab[i]); untag_client(c, tags->tab[i]);
} }

View File

@ -325,9 +325,12 @@ screen_client_moveto(client_t *c, int new_screen, bool dotag, bool doresize)
untag_client(c, old_tags->tab[i]); untag_client(c, old_tags->tab[i]);
/* add new tags */ /* add new tags */
for(i = 0; i < new_tags->len; i++) foreach(new_tag, *new_tags)
if(new_tags->tab[i]->selected) if((*new_tag)->selected)
tag_client(c, new_tags->tab[i]); {
tag_push(globalconf.L, *new_tag);
tag_client(c);
}
} }
if(wasvisible) if(wasvisible)
@ -420,8 +423,6 @@ luaA_screen_tags(lua_State *L)
if(lua_gettop(L) == 2) if(lua_gettop(L) == 2)
{ {
tag_t **tag;
luaA_checktable(L, 2); luaA_checktable(L, 2);
/* remove current tags */ /* remove current tags */
@ -436,18 +437,14 @@ luaA_screen_tags(lua_State *L)
/* push new tags */ /* push new tags */
lua_pushnil(L); lua_pushnil(L);
while(lua_next(L, 2)) while(lua_next(L, 2))
{ tag_append_to_screen(s);
tag = luaA_checkudata(L, -1, "tag");
tag_append_to_screen(*tag, s);
lua_pop(L, 1);
}
} }
else else
{ {
lua_newtable(L); lua_newtable(L);
for(i = 0; i < s->tags.len; i++) for(i = 0; i < s->tags.len; i++)
{ {
luaA_tag_userdata_new(L, s->tags.tab[i]); tag_push(L, s->tags.tab[i]);
lua_rawseti(L, -2, i + 1); lua_rawseti(L, -2, i + 1);
} }
} }

View File

@ -248,8 +248,8 @@ ARRAY_TYPE(client_t *, client)
/** Tag type */ /** Tag type */
struct tag struct tag
{ {
/** Ref count */ /** Lua references count */
int refcount; luaA_ref_array_t refs;
/** Tag name */ /** Tag name */
char *name; char *name;
/** Screen */ /** Screen */

126
tag.c
View File

@ -25,9 +25,26 @@
#include "ewmh.h" #include "ewmh.h"
#include "widget.h" #include "widget.h"
DO_LUA_NEW(extern, tag_t, tag, "tag", tag_ref) DO_LUA_TOSTRING(tag_t, tag, "tag")
DO_LUA_GC(tag_t, tag, "tag", tag_unref)
DO_LUA_EQ(tag_t, tag, "tag") void
tag_unref_simplified(tag_t **tag)
{
tag_unref(globalconf.L, *tag);
}
/** Garbage collect a tag.
* \param L The Lua VM state.
* \return 0.
*/
static int
luaA_tag_gc(lua_State *L)
{
tag_t *tag = luaL_checkudata(L, 1, "tag");
client_array_wipe(&tag->clients);
p_delete(&tag->name);
return 0;
}
/** View or unview a tag. /** View or unview a tag.
* \param tag the tag * \param tag the tag
@ -41,36 +58,17 @@ tag_view(tag_t *tag, bool view)
globalconf.screens[tag->screen].need_arrange = true; globalconf.screens[tag->screen].need_arrange = true;
} }
/** Create a new tag. Parameters values are checked. /** Append a tag which on top of the stack to a screen.
* \param name Tag name.
* \param len Tag name length.
* \return A new tag with all these parameters.
*/
tag_t *
tag_new(const char *name, ssize_t len)
{
tag_t *tag;
tag = p_new(tag_t, 1);
a_iso2utf8(name, len, &tag->name, NULL);
/* to avoid error */
tag->screen = SCREEN_UNDEF;
return tag;
}
/** Append a tag to a screen.
* \param tag the tag to append
* \param screen the screen id * \param screen the screen id
*/ */
void void
tag_append_to_screen(tag_t *tag, screen_t *s) tag_append_to_screen(screen_t *s)
{ {
int phys_screen = screen_virttophys(s->index); int phys_screen = screen_virttophys(s->index);
tag_t *tag = tag_ref(globalconf.L);
tag->screen = s->index; tag->screen = s->index;
tag_array_append(&s->tags, tag_ref(&tag)); tag_array_append(&s->tags, tag);
ewmh_update_net_numbers_of_desktop(phys_screen); ewmh_update_net_numbers_of_desktop(phys_screen);
ewmh_update_net_desktop_names(phys_screen); ewmh_update_net_desktop_names(phys_screen);
ewmh_update_workarea(phys_screen); ewmh_update_workarea(phys_screen);
@ -79,7 +77,7 @@ tag_append_to_screen(tag_t *tag, screen_t *s)
if(globalconf.hooks.tags != LUA_REFNIL) if(globalconf.hooks.tags != LUA_REFNIL)
{ {
lua_pushnumber(globalconf.L, s->index + 1); lua_pushnumber(globalconf.L, s->index + 1);
luaA_tag_userdata_new(globalconf.L, tag); tag_push(globalconf.L, tag);
lua_pushliteral(globalconf.L, "add"); lua_pushliteral(globalconf.L, "add");
luaA_dofunction(globalconf.L, globalconf.hooks.tags, 3, 0); luaA_dofunction(globalconf.L, globalconf.hooks.tags, 3, 0);
} }
@ -110,26 +108,26 @@ tag_remove_from_screen(tag_t *tag)
if(globalconf.hooks.tags != LUA_REFNIL) if(globalconf.hooks.tags != LUA_REFNIL)
{ {
lua_pushnumber(globalconf.L, screen + 1); lua_pushnumber(globalconf.L, screen + 1);
luaA_tag_userdata_new(globalconf.L, tag); tag_push(globalconf.L, tag);
lua_pushliteral(globalconf.L, "remove"); lua_pushliteral(globalconf.L, "remove");
luaA_dofunction(globalconf.L, globalconf.hooks.tags, 3, 0); luaA_dofunction(globalconf.L, globalconf.hooks.tags, 3, 0);
} }
tag_unref(&tag); tag_unref(globalconf.L, tag);
} }
/** Tag a client with specified tag. /** Tag a client with the tag on top of the stack.
* \param c the client to tag * \param c the client to tag
* \param t the tag to tag the client with
*/ */
void void
tag_client(client_t *c, tag_t *t) tag_client(client_t *c)
{ {
tag_t *t = tag_ref(globalconf.L);
/* don't tag twice */ /* don't tag twice */
if(is_client_tagged(c, t)) if(is_client_tagged(c, t))
return; return;
tag_ref(&t);
client_array_append(&t->clients, c); client_array_append(&t->clients, c);
ewmh_client_update_desktop(c); ewmh_client_update_desktop(c);
client_need_arrange(c); client_need_arrange(c);
@ -137,7 +135,7 @@ tag_client(client_t *c, tag_t *t)
if(globalconf.hooks.tagged != LUA_REFNIL) if(globalconf.hooks.tagged != LUA_REFNIL)
{ {
client_push(globalconf.L, c); client_push(globalconf.L, c);
luaA_tag_userdata_new(globalconf.L, t); tag_push(globalconf.L, t);
luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0); luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
} }
} }
@ -159,10 +157,10 @@ untag_client(client_t *c, tag_t *t)
if(globalconf.hooks.tagged != LUA_REFNIL) if(globalconf.hooks.tagged != LUA_REFNIL)
{ {
client_push(globalconf.L, c); client_push(globalconf.L, c);
luaA_tag_userdata_new(globalconf.L, t); tag_push(globalconf.L, t);
luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0); luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
} }
tag_unref(&t); tag_unref(globalconf.L, t);
return; return;
} }
} }
@ -242,7 +240,14 @@ luaA_tag_new(lua_State *L)
{ {
size_t len; size_t len;
const char *name = luaL_checklstring(L, 2, &len); const char *name = luaL_checklstring(L, 2, &len);
return luaA_tag_userdata_new(L, tag_new(name, len)); tag_t *tag = tag_new(globalconf.L);
a_iso2utf8(name, len, &tag->name, NULL);
/* to avoid error */
tag->screen = SCREEN_UNDEF;
return 1;
} }
/** Get or set the clients attached to this tag. /** Get or set the clients attached to this tag.
@ -255,20 +260,22 @@ luaA_tag_new(lua_State *L)
static int static int
luaA_tag_clients(lua_State *L) luaA_tag_clients(lua_State *L)
{ {
tag_t **tag = luaA_checkudata(L, 1, "tag"); tag_t *tag = luaL_checkudata(L, 1, "tag");
client_array_t *clients = &(*tag)->clients; client_array_t *clients = &tag->clients;
int i; int i;
if(lua_gettop(L) == 2) if(lua_gettop(L) == 2)
{ {
luaA_checktable(L, 2); luaA_checktable(L, 2);
for(i = 0; i < (*tag)->clients.len; i++) foreach(c, tag->clients)
untag_client((*tag)->clients.tab[i], *tag); untag_client(*c, tag);
lua_pushnil(L); lua_pushnil(L);
while(lua_next(L, 2)) while(lua_next(L, 2))
{ {
client_t *c = luaA_client_checkudata(L, -1); client_t *c = luaA_client_checkudata(L, -1);
tag_client(c, *tag); /* push tag on top of the stack */
lua_pushvalue(L, 1);
tag_client(c);
lua_pop(L, 1); lua_pop(L, 1);
} }
} }
@ -296,7 +303,7 @@ static int
luaA_tag_index(lua_State *L) luaA_tag_index(lua_State *L)
{ {
size_t len; size_t len;
tag_t **tag = luaA_checkudata(L, 1, "tag"); tag_t *tag = luaL_checkudata(L, 1, "tag");
const char *attr; const char *attr;
if(luaA_usemetatable(L, 1, 2)) if(luaA_usemetatable(L, 1, 2))
@ -307,15 +314,15 @@ luaA_tag_index(lua_State *L)
switch(a_tokenize(attr, len)) switch(a_tokenize(attr, len))
{ {
case A_TK_NAME: case A_TK_NAME:
lua_pushstring(L, (*tag)->name); lua_pushstring(L, tag->name);
break; break;
case A_TK_SCREEN: case A_TK_SCREEN:
if((*tag)->screen == SCREEN_UNDEF) if(tag->screen == SCREEN_UNDEF)
return 0; return 0;
lua_pushnumber(L, (*tag)->screen + 1); lua_pushnumber(L, tag->screen + 1);
break; break;
case A_TK_SELECTED: case A_TK_SELECTED:
lua_pushboolean(L, (*tag)->selected); lua_pushboolean(L, tag->selected);
break; break;
default: default:
return 0; return 0;
@ -332,7 +339,7 @@ static int
luaA_tag_newindex(lua_State *L) luaA_tag_newindex(lua_State *L)
{ {
size_t len; size_t len;
tag_t **tag = luaA_checkudata(L, 1, "tag"); tag_t *tag = luaL_checkudata(L, 1, "tag");
const char *attr = luaL_checklstring(L, 2, &len); const char *attr = luaL_checklstring(L, 2, &len);
switch(a_tokenize(attr, len)) switch(a_tokenize(attr, len))
@ -342,8 +349,8 @@ luaA_tag_newindex(lua_State *L)
case A_TK_NAME: case A_TK_NAME:
{ {
const char *buf = luaL_checklstring(L, 3, &len); const char *buf = luaL_checklstring(L, 3, &len);
p_delete(&(*tag)->name); p_delete(&tag->name);
a_iso2utf8(buf, len, &(*tag)->name, NULL); a_iso2utf8(buf, len, &tag->name, NULL);
} }
break; break;
case A_TK_SCREEN: case A_TK_SCREEN:
@ -355,22 +362,26 @@ luaA_tag_newindex(lua_State *L)
else else
screen = SCREEN_UNDEF; screen = SCREEN_UNDEF;
if((*tag)->screen != SCREEN_UNDEF) if(tag->screen != SCREEN_UNDEF)
tag_remove_from_screen(*tag); tag_remove_from_screen(tag);
if(screen != SCREEN_UNDEF) if(screen != SCREEN_UNDEF)
tag_append_to_screen(*tag, &globalconf.screens[screen]); {
/* push tag on top of the stack */
lua_pushvalue(L, 1);
tag_append_to_screen(&globalconf.screens[screen]);
}
break; break;
case A_TK_SELECTED: case A_TK_SELECTED:
if((*tag)->screen != SCREEN_UNDEF) if(tag->screen != SCREEN_UNDEF)
tag_view(*tag, luaA_checkboolean(L, 3)); tag_view(tag, luaA_checkboolean(L, 3));
return 0; return 0;
default: default:
return 0; return 0;
} }
if((*tag)->screen != SCREEN_UNDEF && (*tag)->selected) if(tag->screen != SCREEN_UNDEF && tag->selected)
globalconf.screens[(*tag)->screen].need_arrange = true; globalconf.screens[tag->screen].need_arrange = true;
return 0; return 0;
} }
@ -385,7 +396,6 @@ const struct luaL_reg awesome_tag_meta[] =
{ "clients", luaA_tag_clients }, { "clients", luaA_tag_clients },
{ "__index", luaA_tag_index }, { "__index", luaA_tag_index },
{ "__newindex", luaA_tag_newindex }, { "__newindex", luaA_tag_newindex },
{ "__eq", luaA_tag_eq },
{ "__gc", luaA_tag_gc }, { "__gc", luaA_tag_gc },
{ "__tostring", luaA_tag_tostring }, { "__tostring", luaA_tag_tostring },
{ NULL, NULL }, { NULL, NULL },

22
tag.h
View File

@ -24,29 +24,17 @@
#include "structs.h" #include "structs.h"
#include "client.h" #include "client.h"
#include "common/refcount.h"
/* Contructor, destructor and referencors */
tag_t *tag_new(const char *, ssize_t);
static inline void
tag_delete(tag_t **tag)
{
client_array_wipe(&(*tag)->clients);
p_delete(&(*tag)->name);
p_delete(tag);
}
tag_t **tags_get_current(int); tag_t **tags_get_current(int);
void tag_client(client_t *, tag_t *); void tag_client(client_t *);
void untag_client(client_t *, tag_t *); void untag_client(client_t *, tag_t *);
bool is_client_tagged(client_t *, tag_t *); bool is_client_tagged(client_t *, tag_t *);
void tag_view_only_byindex(int, int); void tag_view_only_byindex(int, int);
void tag_append_to_screen(tag_t *, screen_t *); void tag_append_to_screen(screen_t *);
int luaA_tag_userdata_new(lua_State *, tag_t *); void tag_unref_simplified(tag_t **);
DO_RCNT(tag_t, tag, tag_delete) ARRAY_FUNCS(tag_t *, tag, tag_unref_simplified)
ARRAY_FUNCS(tag_t *, tag, tag_unref) LUA_OBJECT_FUNCS(tag_t, tag, "tag")
#endif #endif
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80