titlebar: fix removal of too much elements (FS#603)
[awesome.git] / tag.c
blobdfb5365ed8fe2943ed5ef38b546499281aa981e5
1 /*
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.
22 #include "screen.h"
23 #include "tag.h"
24 #include "client.h"
25 #include "ewmh.h"
26 #include "widget.h"
27 #include "luaa.h"
29 /** Tag type */
30 struct tag
32 LUA_OBJECT_HEADER
33 /** Tag name */
34 char *name;
35 /** Screen */
36 screen_t *screen;
37 /** true if selected */
38 bool selected;
39 /** clients in this tag */
40 client_array_t clients;
43 static lua_class_t tag_class;
44 LUA_OBJECT_FUNCS(tag_class, tag_t, tag)
47 void
48 tag_unref_simplified(tag_t **tag)
50 luaA_object_unref(globalconf.L, *tag);
53 /** Garbage collect a tag.
54 * \param L The Lua VM state.
55 * \return 0.
57 static int
58 luaA_tag_gc(lua_State *L)
60 tag_t *tag = luaA_checkudata(L, 1, &tag_class);
61 client_array_wipe(&tag->clients);
62 p_delete(&tag->name);
63 return luaA_object_gc(L);
66 OBJECT_EXPORT_PROPERTY(tag, tag_t, selected)
67 OBJECT_EXPORT_PROPERTY(tag, tag_t, name)
69 void
70 tag_set_screen(tag_t *tag, screen_t *s)
72 tag->screen = s;
75 /** View or unview a tag.
76 * \param L The Lua VM state.
77 * \param udx The index of the tag on the stack.
78 * \param view Set visible or not.
80 static void
81 tag_view(lua_State *L, int udx, bool view)
83 tag_t *tag = luaA_checkudata(L, udx, &tag_class);
84 if(tag->selected != view)
86 tag->selected = view;
88 luaA_object_emit_signal(L, udx, "property::selected", 0);
90 if(tag->screen)
92 int screen_index = screen_array_indexof(&globalconf.screens, tag->screen);
94 tag->screen->need_reban = true;
96 ewmh_update_net_current_desktop(screen_virttophys(screen_index));
98 if(globalconf.hooks.tags != LUA_REFNIL)
100 lua_pushnumber(globalconf.L, screen_index + 1);
101 luaA_object_push(globalconf.L, tag);
102 if(view)
103 lua_pushliteral(globalconf.L, "select");
104 else
105 lua_pushliteral(globalconf.L, "unselect");
106 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.tags, 3, 0);
112 /** Append a tag to a screen.
113 * \param L The Lua VM state.
114 * \param udx The tag index on the stack.
115 * \param s The screen.
117 void
118 tag_append_to_screen(lua_State *L, int udx, screen_t *s)
120 tag_t *tag = luaA_checkudata(globalconf.L, udx, &tag_class);
122 /* can't attach a tag twice */
123 if(tag->screen)
124 return;
126 int screen_index = screen_array_indexof(&globalconf.screens, s);
127 int phys_screen = screen_virttophys(screen_index);
129 tag->screen = s;
130 tag_array_append(&s->tags, luaA_object_ref(globalconf.L, udx));
131 ewmh_update_net_numbers_of_desktop(phys_screen);
132 ewmh_update_net_desktop_names(phys_screen);
133 ewmh_update_workarea(phys_screen);
135 luaA_object_push(globalconf.L, tag);
136 luaA_object_emit_signal(L, -1, "property::screen", 0);
137 lua_pop(L, 1);
139 /* call hook */
140 if(globalconf.hooks.tags != LUA_REFNIL)
142 lua_pushnumber(globalconf.L, screen_index + 1);
143 luaA_object_push(globalconf.L, tag);
144 lua_pushliteral(globalconf.L, "add");
145 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.tags, 3, 0);
148 luaA_object_push(globalconf.L, tag);
149 screen_emit_signal(globalconf.L, s, "tag::attach", 1);
152 /** Remove a tag from screen. Tag must be on a screen and have no clients.
153 * \param tag The tag to remove.
155 static void
156 tag_remove_from_screen(tag_t *tag)
158 int screen_index = screen_array_indexof(&globalconf.screens, tag->screen);
159 int phys_screen = screen_virttophys(screen_index);
160 tag_array_t *tags = &tag->screen->tags;
162 for(int i = 0; i < tags->len; i++)
163 if(tags->tab[i] == tag)
165 tag_array_take(tags, i);
166 break;
168 ewmh_update_net_numbers_of_desktop(phys_screen);
169 ewmh_update_net_desktop_names(phys_screen);
170 ewmh_update_workarea(phys_screen);
172 /* call hook */
173 if(globalconf.hooks.tags != LUA_REFNIL)
175 lua_pushnumber(globalconf.L, screen_index + 1);
176 luaA_object_push(globalconf.L, tag);
177 lua_pushliteral(globalconf.L, "remove");
178 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.tags, 3, 0);
181 screen_t *s = tag->screen;
182 tag->screen = NULL;
184 luaA_object_push(globalconf.L, tag);
185 luaA_object_emit_signal(globalconf.L, -1, "property::screen", 0);
186 screen_emit_signal(globalconf.L, s, "tag::detach", 1);
188 luaA_object_unref(globalconf.L, tag);
191 static void
192 tag_client_emit_signal(lua_State *L, tag_t *t, client_t *c, const char *signame)
194 luaA_object_push(L, c);
195 luaA_object_push(L, t);
196 /* emit signal on client, with new tag as argument */
197 luaA_object_emit_signal(L, -2, signame, 1);
198 /* re push tag */
199 luaA_object_push(L, t);
200 /* move tag before client */
201 lua_insert(L, -2);
202 luaA_object_emit_signal(L, -2, signame, 1);
203 /* Remove tag */
204 lua_pop(L, 1);
207 /** Tag a client with the tag on top of the stack.
208 * \param c the client to tag
210 void
211 tag_client(client_t *c)
213 tag_t *t = luaA_object_ref(globalconf.L, -1);
215 /* don't tag twice */
216 if(is_client_tagged(c, t))
218 luaA_object_unref(globalconf.L, t);
219 return;
222 client_array_append(&t->clients, c);
223 ewmh_client_update_desktop(c);
224 client_need_reban(c);
226 /* call hook */
227 if(globalconf.hooks.tagged != LUA_REFNIL)
229 luaA_object_push(globalconf.L, c);
230 luaA_object_push(globalconf.L, t);
231 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.tagged, 2, 0);
233 tag_client_emit_signal(globalconf.L, t, c, "tagged");
236 /** Untag a client with specified tag.
237 * \param c the client to tag
238 * \param t the tag to tag the client with
240 void
241 untag_client(client_t *c, tag_t *t)
243 for(int i = 0; i < t->clients.len; i++)
244 if(t->clients.tab[i] == c)
246 client_need_reban(c);
247 client_array_take(&t->clients, i);
248 client_need_reban(c);
249 ewmh_client_update_desktop(c);
250 /* call hook */
251 if(globalconf.hooks.tagged != LUA_REFNIL)
253 luaA_object_push(globalconf.L, c);
254 luaA_object_push(globalconf.L, t);
255 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.tagged, 2, 0);
257 tag_client_emit_signal(globalconf.L, t, c, "untagged");
258 luaA_object_unref(globalconf.L, t);
259 return;
263 /** Check if a client is tagged with the specified tag.
264 * \param c the client
265 * \param t the tag
266 * \return true if the client is tagged with the tag, false otherwise.
268 bool
269 is_client_tagged(client_t *c, tag_t *t)
271 for(int i = 0; i < t->clients.len; i++)
272 if(t->clients.tab[i] == c)
273 return true;
275 return false;
278 /** Get the index of the first selected tag.
279 * \param screen Screen.
280 * \return Its index.
283 tags_get_first_selected_index(screen_t *screen)
285 foreach(tag, screen->tags)
286 if((*tag)->selected)
287 return tag_array_indexof(&screen->tags, tag);
288 return 0;
291 /** Set a tag to be the only one viewed.
292 * \param target the tag to see
294 static void
295 tag_view_only(tag_t *target)
297 if(target)
298 foreach(tag, target->screen->tags)
300 luaA_object_push(globalconf.L, *tag);
301 tag_view(globalconf.L, -1, *tag == target);
302 lua_pop(globalconf.L, 1);
306 /** View only a tag, selected by its index.
307 * \param screen Screen.
308 * \param dindex The index.
310 void
311 tag_view_only_byindex(screen_t *screen, int dindex)
313 tag_array_t *tags = &screen->tags;
315 if(dindex < 0 || dindex >= tags->len)
316 return;
317 tag_view_only(tags->tab[dindex]);
320 /** Create a new tag.
321 * \param L The Lua VM state.
322 * \luastack
323 * \lparam A name.
324 * \lreturn A new tag object.
326 static int
327 luaA_tag_new(lua_State *L)
329 if(lua_isstring(L, 2))
331 /* compat code */
332 luaA_deprecate(L, "new syntax");
334 size_t len;
335 const char *name = luaL_checklstring(L, 2, &len);
336 tag_t *tag = tag_new(globalconf.L);
338 a_iso2utf8(name, len, &tag->name, NULL);
340 return 1;
343 return luaA_class_new(L, &tag_class);
346 /** Get or set the clients attached to this tag.
347 * \param L The Lua VM state.
348 * \return The number of elements pushed on stack.
349 * \luastack
350 * \lparam None or a table of clients to set.
351 * \lreturn A table with the clients attached to this tags.
353 static int
354 luaA_tag_clients(lua_State *L)
356 tag_t *tag = luaA_checkudata(L, 1, &tag_class);
357 client_array_t *clients = &tag->clients;
358 int i;
360 if(lua_gettop(L) == 2)
362 luaA_checktable(L, 2);
363 foreach(c, tag->clients)
364 untag_client(*c, tag);
365 lua_pushnil(L);
366 while(lua_next(L, 2))
368 client_t *c = luaA_client_checkudata(L, -1);
369 /* push tag on top of the stack */
370 lua_pushvalue(L, 1);
371 tag_client(c);
372 lua_pop(L, 1);
376 lua_createtable(L, clients->len, 0);
377 for(i = 0; i < clients->len; i++)
379 luaA_object_push(L, clients->tab[i]);
380 lua_rawseti(L, -2, i + 1);
383 return 1;
386 LUA_OBJECT_EXPORT_PROPERTY(tag, tag_t, name, lua_pushstring)
387 LUA_OBJECT_EXPORT_PROPERTY(tag, tag_t, selected, lua_pushboolean)
389 /** Set the tag name.
390 * \param L The Lua VM state.
391 * \param tag The tag to name.
392 * \return The number of elements pushed on stack.
394 static int
395 luaA_tag_set_name(lua_State *L, tag_t *tag)
397 size_t len;
398 const char *buf = luaL_checklstring(L, -1, &len);
399 p_delete(&tag->name);
400 a_iso2utf8(buf, len, &tag->name, NULL);
401 luaA_object_emit_signal(L, -3, "property::name", 0);
402 return 0;
405 /** Set the tag selection status.
406 * \param L The Lua VM state.
407 * \param tag The tag to set the selection status for.
408 * \return The number of elements pushed on stack.
410 static int
411 luaA_tag_set_selected(lua_State *L, tag_t *tag)
413 tag_view(L, -3, luaA_checkboolean(L, -1));
414 return 0;
417 /** Set the tag screen.
418 * \param L The Lua VM state.
419 * \param tag The tag to set the screen for.
420 * \return The number of elements pushed on stack.
422 static int
423 luaA_tag_set_screen(lua_State *L, tag_t *tag)
425 int screen;
426 if(lua_isnil(L, -1))
427 screen = -1;
428 else
430 screen = luaL_checknumber(L, -1) - 1;
431 luaA_checkscreen(screen);
434 if(tag->screen)
435 tag_remove_from_screen(tag);
437 if(screen != -1)
438 tag_append_to_screen(L, -3, &globalconf.screens.tab[screen]);
440 return 0;
443 /** Get the tag screen.
444 * \param L The Lua VM state.
445 * \param tag The tag to get the screen for.
446 * \return The number of elements pushed on stack.
448 static int
449 luaA_tag_get_screen(lua_State *L, tag_t *tag)
451 if(!tag->screen)
452 return 0;
453 lua_pushnumber(L, screen_array_indexof(&globalconf.screens, tag->screen) + 1);
454 return 1;
457 void
458 tag_class_setup(lua_State *L)
460 static const struct luaL_reg tag_methods[] =
462 LUA_CLASS_METHODS(tag)
463 { "__call", luaA_tag_new },
464 { NULL, NULL }
467 static const struct luaL_reg tag_meta[] =
469 LUA_OBJECT_META(tag)
470 LUA_CLASS_META
471 { "clients", luaA_tag_clients },
472 { "__gc", luaA_tag_gc },
473 { NULL, NULL },
476 luaA_class_setup(L, &tag_class, "tag", (lua_class_allocator_t) tag_new,
477 luaA_class_index_miss_property, luaA_class_newindex_miss_property,
478 tag_methods, tag_meta);
479 luaA_class_add_property(&tag_class, A_TK_NAME,
480 (lua_class_propfunc_t) luaA_tag_set_name,
481 (lua_class_propfunc_t) luaA_tag_get_name,
482 (lua_class_propfunc_t) luaA_tag_set_name);
483 luaA_class_add_property(&tag_class, A_TK_SCREEN,
484 (lua_class_propfunc_t) NULL,
485 (lua_class_propfunc_t) luaA_tag_get_screen,
486 (lua_class_propfunc_t) luaA_tag_set_screen);
487 luaA_class_add_property(&tag_class, A_TK_SELECTED,
488 (lua_class_propfunc_t) luaA_tag_set_selected,
489 (lua_class_propfunc_t) luaA_tag_get_selected,
490 (lua_class_propfunc_t) luaA_tag_set_selected);
493 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80