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
);
46 return luaA_object_gc(L
);
49 /** View or unview a tag.
51 * \param view set visible or not
54 tag_view(tag_t
*tag
, bool view
)
56 if(tag
->selected
!= view
)
58 int screen_index
= screen_array_indexof(&globalconf
.screens
, tag
->screen
);
61 tag
->screen
->need_reban
= true;
62 ewmh_update_net_current_desktop(screen_virttophys(screen_index
));
64 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
66 lua_pushnumber(globalconf
.L
, screen_index
+ 1);
67 tag_push(globalconf
.L
, tag
);
69 lua_pushliteral(globalconf
.L
, "select");
71 lua_pushliteral(globalconf
.L
, "unselect");
72 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
77 /** Append a tag which on top of the stack to a screen.
78 * \param screen the screen id
81 tag_append_to_screen(screen_t
*s
)
83 tag_t
*tag
= luaL_checkudata(globalconf
.L
, -1, "tag");
85 /* can't attach a tag twice */
89 int screen_index
= screen_array_indexof(&globalconf
.screens
, s
);
90 int phys_screen
= screen_virttophys(screen_index
);
93 tag_array_append(&s
->tags
, tag_ref(globalconf
.L
, -1));
94 ewmh_update_net_numbers_of_desktop(phys_screen
);
95 ewmh_update_net_desktop_names(phys_screen
);
96 ewmh_update_workarea(phys_screen
);
99 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
101 lua_pushnumber(globalconf
.L
, screen_index
+ 1);
102 tag_push(globalconf
.L
, tag
);
103 lua_pushliteral(globalconf
.L
, "add");
104 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
108 /** Remove a tag from screen. Tag must be on a screen and have no clients.
109 * \param tag The tag to remove.
112 tag_remove_from_screen(tag_t
*tag
)
114 int screen_index
= screen_array_indexof(&globalconf
.screens
, tag
->screen
);
115 int phys_screen
= screen_virttophys(screen_index
);
116 tag_array_t
*tags
= &tag
->screen
->tags
;
118 for(int i
= 0; i
< tags
->len
; i
++)
119 if(tags
->tab
[i
] == tag
)
121 tag_array_take(tags
, i
);
124 ewmh_update_net_numbers_of_desktop(phys_screen
);
125 ewmh_update_net_desktop_names(phys_screen
);
126 ewmh_update_workarea(phys_screen
);
129 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
131 lua_pushnumber(globalconf
.L
, screen_index
+ 1);
132 tag_push(globalconf
.L
, tag
);
133 lua_pushliteral(globalconf
.L
, "remove");
134 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
139 tag_unref(globalconf
.L
, tag
);
142 /** Tag a client with the tag on top of the stack.
143 * \param c the client to tag
146 tag_client(client_t
*c
)
148 tag_t
*t
= tag_ref(globalconf
.L
, -1);
150 /* don't tag twice */
151 if(is_client_tagged(c
, t
))
153 tag_unref(globalconf
.L
, t
);
157 client_array_append(&t
->clients
, c
);
158 ewmh_client_update_desktop(c
);
159 client_need_reban(c
);
162 if(globalconf
.hooks
.tagged
!= LUA_REFNIL
)
164 client_push(globalconf
.L
, c
);
165 tag_push(globalconf
.L
, t
);
166 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
170 /** Untag a client with specified tag.
171 * \param c the client to tag
172 * \param t the tag to tag the client with
175 untag_client(client_t
*c
, tag_t
*t
)
177 for(int i
= 0; i
< t
->clients
.len
; i
++)
178 if(t
->clients
.tab
[i
] == c
)
180 client_need_reban(c
);
181 client_array_take(&t
->clients
, i
);
182 client_need_reban(c
);
183 ewmh_client_update_desktop(c
);
185 if(globalconf
.hooks
.tagged
!= LUA_REFNIL
)
187 client_push(globalconf
.L
, c
);
188 tag_push(globalconf
.L
, t
);
189 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
191 tag_unref(globalconf
.L
, t
);
196 /** Check if a client is tagged with the specified tag.
197 * \param c the client
199 * \return true if the client is tagged with the tag, false otherwise.
202 is_client_tagged(client_t
*c
, tag_t
*t
)
204 for(int i
= 0; i
< t
->clients
.len
; i
++)
205 if(t
->clients
.tab
[i
] == c
)
211 /** Get the current tags for the specified screen.
212 * Returned pointer must be p_delete'd after.
213 * \param screen Screen.
214 * \return A double pointer of tag list finished with a NULL element.
217 tags_get_current(screen_t
*screen
)
219 tag_t
**out
= p_new(tag_t
*, screen
->tags
.len
+ 1);
222 foreach(tag
, screen
->tags
)
230 /** Set a tag to be the only one viewed.
231 * \param target the tag to see
234 tag_view_only(tag_t
*target
)
237 foreach(tag
, target
->screen
->tags
)
238 tag_view(*tag
, *tag
== target
);
241 /** View only a tag, selected by its index.
242 * \param screen Screen.
243 * \param dindex The index.
246 tag_view_only_byindex(screen_t
*screen
, int dindex
)
248 tag_array_t
*tags
= &screen
->tags
;
250 if(dindex
< 0 || dindex
>= tags
->len
)
252 tag_view_only(tags
->tab
[dindex
]);
255 /** Create a new tag.
256 * \param L The Lua VM state.
259 * \lreturn A new tag object.
262 luaA_tag_new(lua_State
*L
)
265 const char *name
= luaL_checklstring(L
, 2, &len
);
266 tag_t
*tag
= tag_new(globalconf
.L
);
268 a_iso2utf8(name
, len
, &tag
->name
, NULL
);
273 /** Get or set the clients attached to this tag.
274 * \param L The Lua VM state.
275 * \return The number of elements pushed on stack.
277 * \lparam None or a table of clients to set.
278 * \lreturn A table with the clients attached to this tags.
281 luaA_tag_clients(lua_State
*L
)
283 tag_t
*tag
= luaL_checkudata(L
, 1, "tag");
284 client_array_t
*clients
= &tag
->clients
;
287 if(lua_gettop(L
) == 2)
289 luaA_checktable(L
, 2);
290 foreach(c
, tag
->clients
)
291 untag_client(*c
, tag
);
293 while(lua_next(L
, 2))
295 client_t
*c
= luaA_client_checkudata(L
, -1);
296 /* push tag on top of the stack */
303 lua_createtable(L
, clients
->len
, 0);
304 for(i
= 0; i
< clients
->len
; i
++)
306 client_push(L
, clients
->tab
[i
]);
307 lua_rawseti(L
, -2, i
+ 1);
314 * \param L The Lua VM state.
315 * \return The number of elements pushed on stack.
317 * \lfield name Tag name.
318 * \lfield screen Screen number of the tag.
319 * \lfield layout Tag layout.
320 * \lfield selected True if the client is selected to be viewed.
323 luaA_tag_index(lua_State
*L
)
326 tag_t
*tag
= luaL_checkudata(L
, 1, "tag");
329 if(luaA_usemetatable(L
, 1, 2))
332 attr
= luaL_checklstring(L
, 2, &len
);
334 switch(a_tokenize(attr
, len
))
337 lua_pushstring(L
, tag
->name
);
342 lua_pushnumber(L
, screen_array_indexof(&globalconf
.screens
, tag
->screen
) + 1);
345 lua_pushboolean(L
, tag
->selected
);
355 * \param L The Lua VM state.
356 * \return The number of elements pushed on stack.
359 luaA_tag_newindex(lua_State
*L
)
362 tag_t
*tag
= luaL_checkudata(L
, 1, "tag");
363 const char *attr
= luaL_checklstring(L
, 2, &len
);
365 switch(a_tokenize(attr
, len
))
371 const char *buf
= luaL_checklstring(L
, 3, &len
);
372 p_delete(&tag
->name
);
373 a_iso2utf8(buf
, len
, &tag
->name
, NULL
);
379 screen
= luaL_checknumber(L
, 3) - 1;
380 luaA_checkscreen(screen
);
386 tag_remove_from_screen(tag
);
390 /* push tag on top of the stack */
392 tag_append_to_screen(&globalconf
.screens
.tab
[screen
]);
397 tag_view(tag
, luaA_checkboolean(L
, 3));
406 const struct luaL_reg awesome_tag_methods
[] =
408 { "__call", luaA_tag_new
},
411 const struct luaL_reg awesome_tag_meta
[] =
413 { "clients", luaA_tag_clients
},
414 { "__index", luaA_tag_index
},
415 { "__newindex", luaA_tag_newindex
},
416 { "__gc", luaA_tag_gc
},
417 { "__tostring", luaA_tag_tostring
},
421 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80