2 * tag.c - tag management
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 DO_LUA_TOSTRING(tag_t
, tag
, "tag")
31 tag_unref_simplified(tag_t
**tag
)
33 tag_unref(globalconf
.L
, *tag
);
36 /** Garbage collect a tag.
37 * \param L The Lua VM state.
41 luaA_tag_gc(lua_State
*L
)
43 tag_t
*tag
= luaL_checkudata(L
, 1, "tag");
44 client_array_wipe(&tag
->clients
);
49 /** View or unview a tag.
51 * \param view set visible or not
54 tag_view(tag_t
*tag
, bool view
)
57 ewmh_update_net_current_desktop(screen_virttophys(screen_array_indexof(&globalconf
.screens
,
59 tag
->screen
->need_arrange
= true;
62 /** Append a tag which on top of the stack to a screen.
63 * \param screen the screen id
66 tag_append_to_screen(screen_t
*s
)
68 tag_t
*tag
= luaL_checkudata(globalconf
.L
, -1, "tag");
70 /* can't attach a tag twice */
74 int screen_index
= screen_array_indexof(&globalconf
.screens
, s
);
75 int phys_screen
= screen_virttophys(screen_index
);
78 tag_array_append(&s
->tags
, tag_ref(globalconf
.L
));
79 ewmh_update_net_numbers_of_desktop(phys_screen
);
80 ewmh_update_net_desktop_names(phys_screen
);
81 ewmh_update_workarea(phys_screen
);
84 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
86 lua_pushnumber(globalconf
.L
, screen_index
+ 1);
87 tag_push(globalconf
.L
, tag
);
88 lua_pushliteral(globalconf
.L
, "add");
89 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
93 /** Remove a tag from screen. Tag must be on a screen and have no clients.
94 * \param tag The tag to remove.
97 tag_remove_from_screen(tag_t
*tag
)
99 int screen_index
= screen_array_indexof(&globalconf
.screens
, tag
->screen
);
100 int phys_screen
= screen_virttophys(screen_index
);
101 tag_array_t
*tags
= &tag
->screen
->tags
;
103 for(int i
= 0; i
< tags
->len
; i
++)
104 if(tags
->tab
[i
] == tag
)
106 tag_array_take(tags
, i
);
109 ewmh_update_net_numbers_of_desktop(phys_screen
);
110 ewmh_update_net_desktop_names(phys_screen
);
111 ewmh_update_workarea(phys_screen
);
114 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
116 lua_pushnumber(globalconf
.L
, screen_index
+ 1);
117 tag_push(globalconf
.L
, tag
);
118 lua_pushliteral(globalconf
.L
, "remove");
119 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
124 tag_unref(globalconf
.L
, tag
);
127 /** Tag a client with the tag on top of the stack.
128 * \param c the client to tag
131 tag_client(client_t
*c
)
133 tag_t
*t
= tag_ref(globalconf
.L
);
135 /* don't tag twice */
136 if(is_client_tagged(c
, t
))
139 client_array_append(&t
->clients
, c
);
140 ewmh_client_update_desktop(c
);
141 client_need_arrange(c
);
143 if(globalconf
.hooks
.tagged
!= LUA_REFNIL
)
145 client_push(globalconf
.L
, c
);
146 tag_push(globalconf
.L
, t
);
147 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
151 /** Untag a client with specified tag.
152 * \param c the client to tag
153 * \param t the tag to tag the client with
156 untag_client(client_t
*c
, tag_t
*t
)
158 for(int i
= 0; i
< t
->clients
.len
; i
++)
159 if(t
->clients
.tab
[i
] == c
)
161 client_need_arrange(c
);
162 client_array_take(&t
->clients
, i
);
163 ewmh_client_update_desktop(c
);
165 if(globalconf
.hooks
.tagged
!= LUA_REFNIL
)
167 client_push(globalconf
.L
, c
);
168 tag_push(globalconf
.L
, t
);
169 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
171 tag_unref(globalconf
.L
, t
);
176 /** Check if a client is tagged with the specified tag.
177 * \param c the client
179 * \return true if the client is tagged with the tag, false otherwise.
182 is_client_tagged(client_t
*c
, tag_t
*t
)
184 for(int i
= 0; i
< t
->clients
.len
; i
++)
185 if(t
->clients
.tab
[i
] == c
)
191 /** Get the current tags for the specified screen.
192 * Returned pointer must be p_delete'd after.
193 * \param screen Screen.
194 * \return A double pointer of tag list finished with a NULL element.
197 tags_get_current(screen_t
*screen
)
199 tag_t
**out
= p_new(tag_t
*, screen
->tags
.len
+ 1);
202 foreach(tag
, screen
->tags
)
210 /** Set a tag to be the only one viewed.
211 * \param target the tag to see
214 tag_view_only(tag_t
*target
)
217 foreach(tag
, target
->screen
->tags
)
218 tag_view(*tag
, *tag
== target
);
221 /** View only a tag, selected by its index.
222 * \param screen Screen.
223 * \param dindex The index.
226 tag_view_only_byindex(screen_t
*screen
, int dindex
)
228 tag_array_t
*tags
= &screen
->tags
;
230 if(dindex
< 0 || dindex
>= tags
->len
)
232 tag_view_only(tags
->tab
[dindex
]);
235 /** Create a new tag.
236 * \param L The Lua VM state.
239 * \lreturn A new tag object.
242 luaA_tag_new(lua_State
*L
)
245 const char *name
= luaL_checklstring(L
, 2, &len
);
246 tag_t
*tag
= tag_new(globalconf
.L
);
248 a_iso2utf8(name
, len
, &tag
->name
, NULL
);
253 /** Get or set the clients attached to this tag.
254 * \param L The Lua VM state.
255 * \return The number of elements pushed on stack.
257 * \lparam None or a table of clients to set.
258 * \lreturn A table with the clients attached to this tags.
261 luaA_tag_clients(lua_State
*L
)
263 tag_t
*tag
= luaL_checkudata(L
, 1, "tag");
264 client_array_t
*clients
= &tag
->clients
;
267 if(lua_gettop(L
) == 2)
269 luaA_checktable(L
, 2);
270 foreach(c
, tag
->clients
)
271 untag_client(*c
, tag
);
273 while(lua_next(L
, 2))
275 client_t
*c
= luaA_client_checkudata(L
, -1);
276 /* push tag on top of the stack */
283 lua_createtable(L
, clients
->len
, 0);
284 for(i
= 0; i
< clients
->len
; i
++)
286 client_push(L
, clients
->tab
[i
]);
287 lua_rawseti(L
, -2, i
+ 1);
294 * \param L The Lua VM state.
295 * \return The number of elements pushed on stack.
297 * \lfield name Tag name.
298 * \lfield screen Screen number of the tag.
299 * \lfield layout Tag layout.
300 * \lfield selected True if the client is selected to be viewed.
303 luaA_tag_index(lua_State
*L
)
306 tag_t
*tag
= luaL_checkudata(L
, 1, "tag");
309 if(luaA_usemetatable(L
, 1, 2))
312 attr
= luaL_checklstring(L
, 2, &len
);
314 switch(a_tokenize(attr
, len
))
317 lua_pushstring(L
, tag
->name
);
322 lua_pushnumber(L
, screen_array_indexof(&globalconf
.screens
, tag
->screen
) + 1);
325 lua_pushboolean(L
, tag
->selected
);
335 * \param L The Lua VM state.
336 * \return The number of elements pushed on stack.
339 luaA_tag_newindex(lua_State
*L
)
342 tag_t
*tag
= luaL_checkudata(L
, 1, "tag");
343 const char *attr
= luaL_checklstring(L
, 2, &len
);
345 switch(a_tokenize(attr
, len
))
351 const char *buf
= luaL_checklstring(L
, 3, &len
);
352 p_delete(&tag
->name
);
353 a_iso2utf8(buf
, len
, &tag
->name
, NULL
);
359 screen
= luaL_checknumber(L
, 3) - 1;
360 luaA_checkscreen(screen
);
366 tag_remove_from_screen(tag
);
370 /* push tag on top of the stack */
372 tag_append_to_screen(&globalconf
.screens
.tab
[screen
]);
377 tag_view(tag
, luaA_checkboolean(L
, 3));
383 if(tag
->screen
&& tag
->selected
)
384 tag
->screen
->need_arrange
= true;
389 const struct luaL_reg awesome_tag_methods
[] =
391 { "__call", luaA_tag_new
},
394 const struct luaL_reg awesome_tag_meta
[] =
396 { "clients", luaA_tag_clients
},
397 { "__index", luaA_tag_index
},
398 { "__newindex", luaA_tag_newindex
},
399 { "__gc", luaA_tag_gc
},
400 { "__tostring", luaA_tag_tostring
},
404 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80