tag: port to new object system

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2009-07-01 16:01:44 +02:00
parent 9c40168f02
commit 99b75bf486
5 changed files with 145 additions and 121 deletions

View File

@ -78,7 +78,7 @@ for s = 1, screen.count() do
tags[s] = {} tags[s] = {}
-- Create 9 tags per screen. -- Create 9 tags per screen.
for tagnumber = 1, 9 do for tagnumber = 1, 9 do
tags[s][tagnumber] = tag(tagnumber) tags[s][tagnumber] = tag { name = tagnumber }
-- Add tags to screen one by one -- Add tags to screen one by one
tags[s][tagnumber].screen = s tags[s][tagnumber].screen = s
awful.layout.set(layouts[1], tags[s][tagnumber]) awful.layout.set(layouts[1], tags[s][tagnumber])

5
luaa.c
View File

@ -59,8 +59,6 @@ extern const struct luaL_reg awesome_screen_methods[];
extern const struct luaL_reg awesome_screen_meta[]; extern const struct luaL_reg awesome_screen_meta[];
extern const struct luaL_reg awesome_client_methods[]; extern const struct luaL_reg awesome_client_methods[];
extern const struct luaL_reg awesome_client_meta[]; extern const struct luaL_reg awesome_client_meta[];
extern const struct luaL_reg awesome_tag_methods[];
extern const struct luaL_reg awesome_tag_meta[];
extern const struct luaL_reg awesome_wibox_methods[]; extern const struct luaL_reg awesome_wibox_methods[];
extern const struct luaL_reg awesome_wibox_meta[]; extern const struct luaL_reg awesome_wibox_meta[];
@ -743,8 +741,7 @@ luaA_init(xdgHandle* xdg)
awesome_image_methods, awesome_image_meta); awesome_image_methods, awesome_image_meta);
/* Export tag */ /* Export tag */
luaA_class_setup(L, &tag_class, "tag", (lua_class_allocator_t) tag_new, tag_class_setup(L);
awesome_tag_methods, awesome_tag_meta);
/* Export wibox */ /* Export wibox */
luaA_class_setup(L, &wibox_class, "wibox", (lua_class_allocator_t) wibox_new, luaA_class_setup(L, &wibox_class, "wibox", (lua_class_allocator_t) wibox_new,

View File

@ -394,7 +394,7 @@ 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_append_to_screen(L, -1, s);
} }
else else
{ {

197
tag.c
View File

@ -25,6 +25,9 @@
#include "ewmh.h" #include "ewmh.h"
#include "widget.h" #include "widget.h"
static lua_class_t tag_class;
LUA_OBJECT_FUNCS(tag_class, tag_t, tag, "tag")
void void
tag_unref_simplified(tag_t **tag) tag_unref_simplified(tag_t **tag)
{ {
@ -45,18 +48,26 @@ luaA_tag_gc(lua_State *L)
} }
/** View or unview a tag. /** View or unview a tag.
* \param tag the tag * \param L The Lua VM state.
* \param view set visible or not * \param udx The index of the tag on the stack.
* \param view Set visible or not.
*/ */
static void static void
tag_view(tag_t *tag, bool view) tag_view(lua_State *L, int udx, bool view)
{ {
tag_t *tag = luaL_checkudata(L, udx, "tag");
if(tag->selected != view) if(tag->selected != view)
{
tag->selected = view;
luaA_object_emit_signal(L, udx, "property::selected", 0);
if(tag->screen)
{ {
int screen_index = screen_array_indexof(&globalconf.screens, tag->screen); int screen_index = screen_array_indexof(&globalconf.screens, tag->screen);
tag->selected = view;
tag->screen->need_reban = true; tag->screen->need_reban = true;
ewmh_update_net_current_desktop(screen_virttophys(screen_index)); ewmh_update_net_current_desktop(screen_virttophys(screen_index));
if(globalconf.hooks.tags != LUA_REFNIL) if(globalconf.hooks.tags != LUA_REFNIL)
@ -71,14 +82,17 @@ tag_view(tag_t *tag, bool view)
} }
} }
} }
}
/** Append a tag which on top of the stack to a screen. /** Append a tag to a screen.
* \param s screen the screen id * \param L The Lua VM state.
* \param udx The tag index on the stack.
* \param s The screen.
*/ */
void void
tag_append_to_screen(screen_t *s) tag_append_to_screen(lua_State *L, int udx, screen_t *s)
{ {
tag_t *tag = luaL_checkudata(globalconf.L, -1, "tag"); tag_t *tag = luaL_checkudata(globalconf.L, udx, "tag");
/* can't attach a tag twice */ /* can't attach a tag twice */
if(tag->screen) if(tag->screen)
@ -88,11 +102,15 @@ tag_append_to_screen(screen_t *s)
int phys_screen = screen_virttophys(screen_index); int phys_screen = screen_virttophys(screen_index);
tag->screen = s; tag->screen = s;
tag_array_append(&s->tags, luaA_object_ref(globalconf.L, -1)); tag_array_append(&s->tags, luaA_object_ref(globalconf.L, udx));
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);
luaA_object_push(globalconf.L, tag);
luaA_object_emit_signal(L, -1, "property::screen", 0);
lua_pop(L, 1);
/* call hook */ /* call hook */
if(globalconf.hooks.tags != LUA_REFNIL) if(globalconf.hooks.tags != LUA_REFNIL)
{ {
@ -134,6 +152,10 @@ tag_remove_from_screen(tag_t *tag)
tag->screen = NULL; tag->screen = NULL;
luaA_object_push(globalconf.L, tag);
luaA_object_emit_signal(globalconf.L, -1, "property::screen", 0);
lua_pop(globalconf.L, 1);
luaA_object_unref(globalconf.L, tag); luaA_object_unref(globalconf.L, tag);
} }
@ -227,7 +249,11 @@ tag_view_only(tag_t *target)
{ {
if(target) if(target)
foreach(tag, target->screen->tags) foreach(tag, target->screen->tags)
tag_view(*tag, *tag == target); {
luaA_object_push(globalconf.L, *tag);
tag_view(globalconf.L, -1, *tag == target);
lua_pop(globalconf.L, 1);
}
} }
/** View only a tag, selected by its index. /** View only a tag, selected by its index.
@ -253,6 +279,11 @@ tag_view_only_byindex(screen_t *screen, int dindex)
static int static int
luaA_tag_new(lua_State *L) luaA_tag_new(lua_State *L)
{ {
if(lua_isstring(L, 2))
{
/* compat code */
luaA_deprecate(L, "new syntax");
size_t len; size_t len;
const char *name = luaL_checklstring(L, 2, &len); const char *name = luaL_checklstring(L, 2, &len);
tag_t *tag = tag_new(globalconf.L); tag_t *tag = tag_new(globalconf.L);
@ -262,6 +293,9 @@ luaA_tag_new(lua_State *L)
return 1; return 1;
} }
return luaA_class_new(L, &tag_class);
}
/** Get or set the clients attached to this tag. /** Get or set the clients attached to this tag.
* \param L The Lua VM state. * \param L The Lua VM state.
* \return The number of elements pushed on stack. * \return The number of elements pushed on stack.
@ -302,113 +336,106 @@ luaA_tag_clients(lua_State *L)
return 1; return 1;
} }
/** Tag object. LUA_OBJECT_EXPORT_PROPERTY(tag, tag_t, name, lua_pushstring)
* \param L The Lua VM state. LUA_OBJECT_EXPORT_PROPERTY(tag, tag_t, selected, lua_pushboolean)
* \return The number of elements pushed on stack.
* \luastack
* \lfield name Tag name.
* \lfield screen Screen number of the tag.
* \lfield layout Tag layout.
* \lfield selected True if the client is selected to be viewed.
*/
static int
luaA_tag_index(lua_State *L)
{
size_t len;
tag_t *tag = luaL_checkudata(L, 1, "tag");
const char *attr;
if(luaA_usemetatable(L, 1, 2)) /** Set the tag name.
return 1;
attr = luaL_checklstring(L, 2, &len);
switch(a_tokenize(attr, len))
{
case A_TK_NAME:
lua_pushstring(L, tag->name);
break;
case A_TK_SCREEN:
if(!tag->screen)
return 0;
lua_pushnumber(L, screen_array_indexof(&globalconf.screens, tag->screen) + 1);
break;
case A_TK_SELECTED:
lua_pushboolean(L, tag->selected);
break;
default:
return 0;
}
return 1;
}
/** Tag newindex.
* \param L The Lua VM state. * \param L The Lua VM state.
* \return The number of elements pushed on stack. * \return The number of elements pushed on stack.
*/ */
static int static int
luaA_tag_newindex(lua_State *L) luaA_tag_set_name(lua_State *L, tag_t *tag)
{ {
size_t len; size_t len;
tag_t *tag = luaL_checkudata(L, 1, "tag"); const char *buf = luaL_checklstring(L, -1, &len);
const char *attr = luaL_checklstring(L, 2, &len);
switch(a_tokenize(attr, len))
{
int screen;
case A_TK_NAME:
{
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);
luaA_object_emit_signal(L, -3, "property::name", 0);
return 0;
} }
break;
case A_TK_SCREEN: /** Set the tag selection status.
if(!lua_isnil(L, 3)) * \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_tag_set_selected(lua_State *L, tag_t *tag)
{ {
screen = luaL_checknumber(L, 3) - 1; tag_view(L, -3, luaA_checkboolean(L, -1));
return 0;
}
/** Set the tag screen.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_tag_set_screen(lua_State *L, tag_t *tag)
{
int screen;
if(lua_isnil(L, -1))
screen = -1;
else
{
screen = luaL_checknumber(L, -1) - 1;
luaA_checkscreen(screen); luaA_checkscreen(screen);
} }
else
screen = -1;
if(tag->screen) if(tag->screen)
tag_remove_from_screen(tag); tag_remove_from_screen(tag);
if(screen != -1) if(screen != -1)
tag_append_to_screen(L, -3, &globalconf.screens.tab[screen]);
return 0;
}
/** Get the tag screen.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_tag_get_screen(lua_State *L, tag_t *tag)
{ {
/* push tag on top of the stack */ if(!tag->screen)
lua_pushvalue(L, 1);
tag_append_to_screen(&globalconf.screens.tab[screen]);
}
break;
case A_TK_SELECTED:
if(tag->screen)
tag_view(tag, luaA_checkboolean(L, 3));
return 0;
default:
return 0; return 0;
lua_pushnumber(L, screen_array_indexof(&globalconf.screens, tag->screen) + 1);
return 1;
} }
return 0; void
} tag_class_setup(lua_State *L)
{
const struct luaL_reg awesome_tag_methods[] = static const struct luaL_reg tag_methods[] =
{ {
LUA_CLASS_METHODS(tag) LUA_CLASS_METHODS(tag)
{ "__call", luaA_tag_new }, { "__call", luaA_tag_new },
{ NULL, NULL } { NULL, NULL }
}; };
const struct luaL_reg awesome_tag_meta[] =
static const struct luaL_reg tag_meta[] =
{ {
LUA_OBJECT_META(tag) LUA_OBJECT_META(tag)
LUA_CLASS_META
{ "clients", luaA_tag_clients }, { "clients", luaA_tag_clients },
{ "__index", luaA_tag_index },
{ "__newindex", luaA_tag_newindex },
{ "__gc", luaA_tag_gc }, { "__gc", luaA_tag_gc },
{ NULL, NULL }, { NULL, NULL },
}; };
luaA_class_setup(L, &tag_class, "tag", (lua_class_allocator_t) tag_new,
tag_methods, tag_meta);
luaA_class_add_property(&tag_class, A_TK_NAME,
(lua_class_propfunc_t) luaA_tag_set_name,
(lua_class_propfunc_t) luaA_tag_get_name,
(lua_class_propfunc_t) luaA_tag_set_name);
luaA_class_add_property(&tag_class, A_TK_SCREEN,
(lua_class_propfunc_t) NULL,
(lua_class_propfunc_t) luaA_tag_get_screen,
(lua_class_propfunc_t) luaA_tag_set_screen);
luaA_class_add_property(&tag_class, A_TK_SELECTED,
(lua_class_propfunc_t) luaA_tag_set_selected,
(lua_class_propfunc_t) luaA_tag_get_selected,
(lua_class_propfunc_t) luaA_tag_set_selected);
}
// 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

6
tag.h
View File

@ -43,12 +43,12 @@ 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(screen_t *, int); void tag_view_only_byindex(screen_t *, int);
void tag_append_to_screen(screen_t *); void tag_append_to_screen(lua_State *, int, screen_t *);
void tag_unref_simplified(tag_t **); void tag_unref_simplified(tag_t **);
ARRAY_FUNCS(tag_t *, tag, tag_unref_simplified) ARRAY_FUNCS(tag_t *, tag, tag_unref_simplified)
lua_class_t tag_class;
LUA_OBJECT_FUNCS(tag_class, tag_t, tag, "tag") void tag_class_setup(lua_State *);
#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