screen: implement tags set/get as function

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-08-13 18:12:26 +02:00
parent d69b602e52
commit 7e15509733
3 changed files with 76 additions and 40 deletions

View File

@ -352,7 +352,7 @@ end
--- Update the tag history. --- Update the tag history.
-- @param screen The screen number. -- @param screen The screen number.
function tag.history.update(screen) function tag.history.update(screen)
local curtags = capi.screen[screen].tags local curtags = capi.screen[screen]:tags()
if not tag_compare_select(curtags, tag.history.data.current[screen]) then if not tag_compare_select(curtags, tag.history.data.current[screen]) then
tag.history.data.past[screen] = tag.history.data.current[screen] tag.history.data.past[screen] = tag.history.data.current[screen]
tag.history.data.current[screen] = {} tag.history.data.current[screen] = {}
@ -366,7 +366,7 @@ end
-- @param screen The screen number. -- @param screen The screen number.
function tag.history.restore(screen) function tag.history.restore(screen)
local s = screen or capi.mouse.screen local s = screen or capi.mouse.screen
local tags = capi.screen[s].tags local tags = capi.screen[s]:tags()
for k, t in pairs(tags) do for k, t in pairs(tags) do
t.selected = tag.history.data.past[s][k] t.selected = tag.history.data.past[s][k]
end end
@ -377,7 +377,7 @@ end
-- @return A table with all selected tags. -- @return A table with all selected tags.
function tag.selectedlist(s) function tag.selectedlist(s)
local screen = s or capi.mouse.screen local screen = s or capi.mouse.screen
local tags = capi.screen[screen].tags local tags = capi.screen[screen]:tags()
local vtags = {} local vtags = {}
for i, t in pairs(tags) do for i, t in pairs(tags) do
if t.selected then if t.selected then
@ -450,7 +450,7 @@ end
--- View no tag. --- View no tag.
-- @param Optional screen number. -- @param Optional screen number.
function tag.viewnone(screen) function tag.viewnone(screen)
local tags = capi.screen[screen or capi.mouse.screen].tags local tags = capi.screen[screen or capi.mouse.screen]:tags()
for i, t in pairs(tags) do for i, t in pairs(tags) do
t.selected = false t.selected = false
end end
@ -460,7 +460,7 @@ end
-- @param i The relative index to see. -- @param i The relative index to see.
-- @param screen Optional screen number. -- @param screen Optional screen number.
function tag.viewidx(i, screen) function tag.viewidx(i, screen)
local tags = capi.screen[screen or capi.mouse.screen].tags local tags = capi.screen[screen or capi.mouse.screen]:tags()
local sel = tag.selected() local sel = tag.selected()
tag.viewnone() tag.viewnone()
for k, t in ipairs(tags) do for k, t in ipairs(tags) do
@ -1288,7 +1288,7 @@ end
function widget.tasklist.label.currenttags(c, screen, args) function widget.tasklist.label.currenttags(c, screen, args)
-- Only print client on the same screen as this widget -- Only print client on the same screen as this widget
if c.screen ~= screen then return end if c.screen ~= screen then return end
for k, t in ipairs(capi.screen[screen].tags) do for k, t in ipairs(capi.screen[screen]:tags()) do
if t.selected and c:tags()[t] then if t.selected and c:tags()[t] then
return widget_tasklist_label_common(c, args) return widget_tasklist_label_common(c, args)
end end

2
lua.h
View File

@ -170,7 +170,7 @@ luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
lua_getmetatable(L, idxobj); lua_getmetatable(L, idxobj);
lua_pushvalue(L, idxfield); lua_pushvalue(L, idxfield);
lua_rawget(L, -2); lua_rawget(L, -2);
if (!lua_isnil(L, -1)) if(!lua_isnil(L, -1))
{ {
lua_remove(L, -2); lua_remove(L, -2);
return 1; return 1;

102
screen.c
View File

@ -28,6 +28,7 @@
#include "ewmh.h" #include "ewmh.h"
#include "tag.h" #include "tag.h"
#include "client.h" #include "client.h"
#include "layouts/tile.h"
extern awesome_t globalconf; extern awesome_t globalconf;
@ -231,8 +232,70 @@ luaA_screen_module_index(lua_State *L)
return luaA_settype(L, "screen"); return luaA_settype(L, "screen");
} }
/** Get or set screen tags.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lparam None or a table of tags to set to the screen.
* The table must contains at least one tag.
* \return A table with all screen tags.
*/
static int
luaA_screen_tags(lua_State *L)
{
int i;
screen_t *s = lua_touserdata(L, 1);
if(!s)
luaL_typerror(L, 1, "screen");
if(lua_gettop(L) == 2)
{
tag_t **tag;
luaA_checktable(L, 2);
/* remove current tags */
for(i = 0; i < s->tags.len; i++)
s->tags.tab[i]->screen = SCREEN_UNDEF;
tag_array_wipe(&s->tags);
tag_array_init(&s->tags);
s->need_arrange = true;
/* push new tags */
lua_pushnil(L);
while(lua_next(L, 2))
{
tag = luaA_checkudata(L, -1, "tag");
tag_append_to_screen(*tag, s);
lua_pop(L, 1);
}
/* check there's at least one tag! */
if(!s->tags.len)
{
tag_append_to_screen(tag_new("default", sizeof("default") - 1, layout_tile, 0.5, 1, 0), s);
luaL_error(L, "no tag were added on screen %d, taking last resort action and adding default tag\n", s->index);
}
}
else
{
lua_newtable(L);
for(i = 0; i < s->tags.len; i++)
{
luaA_tag_userdata_new(L, s->tags.tab[i]);
lua_rawseti(L, -2, i + 1);
}
}
return 1;
}
/** A screen. /** A screen.
* \param L The Lua VM state. * \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack * \luastack
* \lfield padding The screen padding. A table with top, right, left and bottom * \lfield padding The screen padding. A table with top, right, left and bottom
* keys and values in pixel. * keys and values in pixel.
@ -243,11 +306,14 @@ static int
luaA_screen_index(lua_State *L) luaA_screen_index(lua_State *L)
{ {
size_t len; size_t len;
const char *buf = luaL_checklstring(L, 2, &len); const char *buf;
screen_t *s; screen_t *s;
area_t g; area_t g;
int i;
if(luaA_usemetatable(L, 1, 2))
return 1;
buf = luaL_checklstring(L, 2, &len);
s = lua_touserdata(L, 1); s = lua_touserdata(L, 1);
switch(a_tokenize(buf, len)) switch(a_tokenize(buf, len))
@ -287,14 +353,6 @@ luaA_screen_index(lua_State *L)
lua_pushnumber(L, g.height); lua_pushnumber(L, g.height);
lua_setfield(L, -2, "height"); lua_setfield(L, -2, "height");
break; break;
case A_TK_TAGS:
lua_newtable(L);
for(i = 0; i < s->tags.len; i++)
{
luaA_tag_userdata_new(L, s->tags.tab[i]);
lua_rawseti(L, -2, i + 1);
}
break;
default: default:
return 0; return 0;
} }
@ -308,8 +366,6 @@ luaA_screen_newindex(lua_State *L)
size_t len; size_t len;
const char *buf = luaL_checklstring(L, 2, &len); const char *buf = luaL_checklstring(L, 2, &len);
screen_t *s; screen_t *s;
tag_t **tag;
int i;
s = lua_touserdata(L, 1); s = lua_touserdata(L, 1);
@ -324,27 +380,6 @@ luaA_screen_newindex(lua_State *L)
ewmh_update_workarea(screen_virttophys(s->index)); ewmh_update_workarea(screen_virttophys(s->index));
break;
case A_TK_TAGS:
luaA_checktable(L, 3);
/* remove current tags */
for(i = 0; i < s->tags.len; i++)
s->tags.tab[i]->screen = SCREEN_UNDEF;
tag_array_wipe(&s->tags);
tag_array_init(&s->tags);
s->need_arrange = true;
/* push new tags */
lua_pushnil(L);
while(lua_next(L, 3))
{
tag = luaA_checkudata(L, -1, "tag");
tag_append_to_screen(*tag, s);
lua_pop(L, 1);
}
break; break;
default: default:
return 0; return 0;
@ -376,6 +411,7 @@ const struct luaL_reg awesome_screen_methods[] =
const struct luaL_reg awesome_screen_meta[] = const struct luaL_reg awesome_screen_meta[] =
{ {
{ "tags", luaA_screen_tags },
{ "__index", luaA_screen_index }, { "__index", luaA_screen_index },
{ "__newindex", luaA_screen_newindex }, { "__newindex", luaA_screen_newindex },
{ NULL, NULL } { NULL, NULL }