systray: fix screen number retrieval
[awesome.git] / tag.c
blobe271d97bdece9e1014309bba549cefb1b6312e43
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 /** View or unview a tag.
70 * \param L The Lua VM state.
71 * \param udx The index of the tag on the stack.
72 * \param view Set visible or not.
74 static void
75 tag_view(lua_State *L, int udx, bool view)
77 tag_t *tag = luaA_checkudata(L, udx, &tag_class);
78 if(tag->selected != view)
80 tag->selected = view;
82 if(tag->screen)
84 int screen_index = screen_array_indexof(&globalconf.screens, tag->screen);
86 banning_need_update(tag->screen);
88 ewmh_update_net_current_desktop(screen_virttophys(screen_index));
90 if(globalconf.hooks.tags != LUA_REFNIL)
92 lua_pushnumber(globalconf.L, screen_index + 1);
93 luaA_object_push(globalconf.L, tag);
94 if(view)
95 lua_pushliteral(globalconf.L, "select");
96 else
97 lua_pushliteral(globalconf.L, "unselect");
98 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.tags, 3, 0);
102 luaA_object_emit_signal(L, udx, "property::selected", 0);
106 /** Append a tag to a screen.
107 * \param L The Lua VM state.
108 * \param udx The tag index on the stack.
109 * \param s The screen.
111 void
112 tag_append_to_screen(lua_State *L, int udx, screen_t *s)
114 tag_t *tag = luaA_checkudata(globalconf.L, udx, &tag_class);
116 /* can't attach a tag twice */
117 if(tag->screen)
119 lua_remove(L, udx);
120 return;
123 int screen_index = screen_array_indexof(&globalconf.screens, s);
124 int phys_screen = screen_virttophys(screen_index);
126 tag->screen = s;
127 tag_array_append(&s->tags, luaA_object_ref_class(globalconf.L, udx, &tag_class));
128 ewmh_update_net_numbers_of_desktop(phys_screen);
129 ewmh_update_net_desktop_names(phys_screen);
130 ewmh_update_workarea(phys_screen);
132 luaA_object_push(globalconf.L, tag);
133 luaA_object_emit_signal(L, -1, "property::screen", 0);
134 lua_pop(L, 1);
136 /* call hook */
137 if(globalconf.hooks.tags != LUA_REFNIL)
139 lua_pushnumber(globalconf.L, screen_index + 1);
140 luaA_object_push(globalconf.L, tag);
141 lua_pushliteral(globalconf.L, "add");
142 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.tags, 3, 0);
145 luaA_object_push(globalconf.L, tag);
146 screen_emit_signal(globalconf.L, s, "tag::attach", 1);
149 /** Remove a tag from screen. Tag must be on a screen and have no clients.
150 * \param tag The tag to remove.
152 void
153 tag_remove_from_screen(tag_t *tag)
155 if(!tag->screen)
156 return;
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;
169 /* tag was selected? If so, reban */
170 if(tag->selected)
171 banning_need_update(tag->screen);
173 ewmh_update_net_numbers_of_desktop(phys_screen);
174 ewmh_update_net_desktop_names(phys_screen);
175 ewmh_update_workarea(phys_screen);
177 /* call hook */
178 if(globalconf.hooks.tags != LUA_REFNIL)
180 lua_pushnumber(globalconf.L, screen_index + 1);
181 luaA_object_push(globalconf.L, tag);
182 lua_pushliteral(globalconf.L, "remove");
183 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.tags, 3, 0);
186 screen_t *s = tag->screen;
187 tag->screen = NULL;
189 luaA_object_push(globalconf.L, tag);
190 luaA_object_emit_signal(globalconf.L, -1, "property::screen", 0);
191 screen_emit_signal(globalconf.L, s, "tag::detach", 1);
193 luaA_object_unref(globalconf.L, tag);
196 static void
197 tag_client_emit_signal(lua_State *L, tag_t *t, client_t *c, const char *signame)
199 luaA_object_push(L, c);
200 luaA_object_push(L, t);
201 /* emit signal on client, with new tag as argument */
202 luaA_object_emit_signal(L, -2, signame, 1);
203 /* re push tag */
204 luaA_object_push(L, t);
205 /* move tag before client */
206 lua_insert(L, -2);
207 luaA_object_emit_signal(L, -2, signame, 1);
208 /* Remove tag */
209 lua_pop(L, 1);
212 /** Tag a client with the tag on top of the stack.
213 * \param c the client to tag
215 void
216 tag_client(client_t *c)
218 tag_t *t = luaA_object_ref_class(globalconf.L, -1, &tag_class);
220 /* don't tag twice */
221 if(is_client_tagged(c, t))
223 luaA_object_unref(globalconf.L, t);
224 return;
227 client_array_append(&t->clients, c);
228 ewmh_client_update_desktop(c);
229 banning_need_update((c)->screen);
231 /* call hook */
232 if(globalconf.hooks.tagged != LUA_REFNIL)
234 luaA_object_push(globalconf.L, c);
235 luaA_object_push(globalconf.L, t);
236 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.tagged, 2, 0);
238 tag_client_emit_signal(globalconf.L, t, c, "tagged");
241 /** Untag a client with specified tag.
242 * \param c the client to tag
243 * \param t the tag to tag the client with
245 void
246 untag_client(client_t *c, tag_t *t)
248 for(int i = 0; i < t->clients.len; i++)
249 if(t->clients.tab[i] == c)
251 client_array_take(&t->clients, i);
252 banning_need_update((c)->screen);
253 ewmh_client_update_desktop(c);
254 /* call hook */
255 if(globalconf.hooks.tagged != LUA_REFNIL)
257 luaA_object_push(globalconf.L, c);
258 luaA_object_push(globalconf.L, t);
259 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.tagged, 2, 0);
261 tag_client_emit_signal(globalconf.L, t, c, "untagged");
262 luaA_object_unref(globalconf.L, t);
263 return;
267 /** Check if a client is tagged with the specified tag.
268 * \param c the client
269 * \param t the tag
270 * \return true if the client is tagged with the tag, false otherwise.
272 bool
273 is_client_tagged(client_t *c, tag_t *t)
275 for(int i = 0; i < t->clients.len; i++)
276 if(t->clients.tab[i] == c)
277 return true;
279 return false;
282 /** Get the index of the first selected tag.
283 * \param screen Screen.
284 * \return Its index.
287 tags_get_first_selected_index(screen_t *screen)
289 foreach(tag, screen->tags)
290 if((*tag)->selected)
291 return tag_array_indexof(&screen->tags, tag);
292 return 0;
295 /** Set a tag to be the only one viewed.
296 * \param target the tag to see
298 static void
299 tag_view_only(tag_t *target)
301 if(target)
302 foreach(tag, target->screen->tags)
304 luaA_object_push(globalconf.L, *tag);
305 tag_view(globalconf.L, -1, *tag == target);
306 lua_pop(globalconf.L, 1);
310 /** View only a tag, selected by its index.
311 * \param screen Screen.
312 * \param dindex The index.
314 void
315 tag_view_only_byindex(screen_t *screen, int dindex)
317 tag_array_t *tags = &screen->tags;
319 if(dindex < 0 || dindex >= tags->len)
320 return;
321 tag_view_only(tags->tab[dindex]);
324 /** Create a new tag.
325 * \param L The Lua VM state.
326 * \luastack
327 * \lparam A name.
328 * \lreturn A new tag object.
330 static int
331 luaA_tag_new(lua_State *L)
333 if(lua_isstring(L, 2))
335 /* compat code */
336 luaA_deprecate(L, "new syntax");
338 size_t len;
339 const char *name = luaL_checklstring(L, 2, &len);
340 tag_t *tag = tag_new(globalconf.L);
342 a_iso2utf8(name, len, &tag->name, NULL);
344 return 1;
347 return luaA_class_new(L, &tag_class);
350 /** Get or set the clients attached to this tag.
351 * \param L The Lua VM state.
352 * \return The number of elements pushed on stack.
353 * \luastack
354 * \lparam None or a table of clients to set.
355 * \lreturn A table with the clients attached to this tags.
357 static int
358 luaA_tag_clients(lua_State *L)
360 tag_t *tag = luaA_checkudata(L, 1, &tag_class);
361 client_array_t *clients = &tag->clients;
362 int i;
364 if(lua_gettop(L) == 2)
366 luaA_checktable(L, 2);
367 foreach(c, tag->clients)
368 untag_client(*c, tag);
369 lua_pushnil(L);
370 while(lua_next(L, 2))
372 client_t *c = luaA_client_checkudata(L, -1);
373 /* push tag on top of the stack */
374 lua_pushvalue(L, 1);
375 tag_client(c);
376 lua_pop(L, 1);
380 lua_createtable(L, clients->len, 0);
381 for(i = 0; i < clients->len; i++)
383 luaA_object_push(L, clients->tab[i]);
384 lua_rawseti(L, -2, i + 1);
387 return 1;
390 LUA_OBJECT_EXPORT_PROPERTY(tag, tag_t, name, lua_pushstring)
391 LUA_OBJECT_EXPORT_PROPERTY(tag, tag_t, selected, lua_pushboolean)
393 /** Set the tag name.
394 * \param L The Lua VM state.
395 * \param tag The tag to name.
396 * \return The number of elements pushed on stack.
398 static int
399 luaA_tag_set_name(lua_State *L, tag_t *tag)
401 size_t len;
402 const char *buf = luaL_checklstring(L, -1, &len);
403 p_delete(&tag->name);
404 a_iso2utf8(buf, len, &tag->name, NULL);
405 luaA_object_emit_signal(L, -3, "property::name", 0);
406 return 0;
409 /** Set the tag selection status.
410 * \param L The Lua VM state.
411 * \param tag The tag to set the selection status for.
412 * \return The number of elements pushed on stack.
414 static int
415 luaA_tag_set_selected(lua_State *L, tag_t *tag)
417 tag_view(L, -3, luaA_checkboolean(L, -1));
418 return 0;
421 /** Set the tag screen.
422 * \param L The Lua VM state.
423 * \param tag The tag to set the screen for.
424 * \return The number of elements pushed on stack.
426 static int
427 luaA_tag_set_screen(lua_State *L, tag_t *tag)
429 int screen;
430 if(lua_isnil(L, -1))
431 screen = -1;
432 else
434 screen = luaL_checknumber(L, -1) - 1;
435 luaA_checkscreen(screen);
438 tag_remove_from_screen(tag);
440 if(screen != -1)
441 tag_append_to_screen(L, -3, &globalconf.screens.tab[screen]);
443 return 0;
446 /** Get the tag screen.
447 * \param L The Lua VM state.
448 * \param tag The tag to get the screen for.
449 * \return The number of elements pushed on stack.
451 static int
452 luaA_tag_get_screen(lua_State *L, tag_t *tag)
454 if(!tag->screen)
455 return 0;
456 lua_pushnumber(L, screen_array_indexof(&globalconf.screens, tag->screen) + 1);
457 return 1;
460 void
461 tag_class_setup(lua_State *L)
463 static const struct luaL_reg tag_methods[] =
465 LUA_CLASS_METHODS(tag)
466 { "__call", luaA_tag_new },
467 { NULL, NULL }
470 static const struct luaL_reg tag_meta[] =
472 LUA_OBJECT_META(tag)
473 LUA_CLASS_META
474 { "clients", luaA_tag_clients },
475 { "__gc", luaA_tag_gc },
476 { NULL, NULL },
479 luaA_class_setup(L, &tag_class, "tag", (lua_class_allocator_t) tag_new,
480 luaA_class_index_miss_property, luaA_class_newindex_miss_property,
481 tag_methods, tag_meta);
482 luaA_class_add_property(&tag_class, A_TK_NAME,
483 (lua_class_propfunc_t) luaA_tag_set_name,
484 (lua_class_propfunc_t) luaA_tag_get_name,
485 (lua_class_propfunc_t) luaA_tag_set_name);
486 luaA_class_add_property(&tag_class, A_TK_SCREEN,
487 (lua_class_propfunc_t) NULL,
488 (lua_class_propfunc_t) luaA_tag_get_screen,
489 (lua_class_propfunc_t) luaA_tag_set_screen);
490 luaA_class_add_property(&tag_class, A_TK_SELECTED,
491 (lua_class_propfunc_t) luaA_tag_set_selected,
492 (lua_class_propfunc_t) luaA_tag_get_selected,
493 (lua_class_propfunc_t) luaA_tag_set_selected);
496 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80