Use a libev prepare watcher for calling awesome_refresh()
[awesome.git] / tag.c
blob8400b5309a3fc8404f4a911f16cd71cca5ab1866
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"
28 DO_LUA_TOSTRING(tag_t, tag, "tag")
30 void
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.
38 * \return 0.
40 static int
41 luaA_tag_gc(lua_State *L)
43 tag_t *tag = luaL_checkudata(L, 1, "tag");
44 client_array_wipe(&tag->clients);
45 p_delete(&tag->name);
46 return luaA_object_gc(L);
49 /** View or unview a tag.
50 * \param tag the tag
51 * \param view set visible or not
53 static void
54 tag_view(tag_t *tag, bool view)
56 if(tag->selected != view)
58 int screen_index = screen_array_indexof(&globalconf.screens, tag->screen);
60 tag->selected = view;
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);
68 if(view)
69 lua_pushliteral(globalconf.L, "select");
70 else
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
80 void
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 */
86 if(tag->screen)
87 return;
89 int screen_index = screen_array_indexof(&globalconf.screens, s);
90 int phys_screen = screen_virttophys(screen_index);
92 tag->screen = s;
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);
98 /* call hook */
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.
111 static void
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);
122 break;
124 ewmh_update_net_numbers_of_desktop(phys_screen);
125 ewmh_update_net_desktop_names(phys_screen);
126 ewmh_update_workarea(phys_screen);
128 /* call hook */
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);
137 tag->screen = NULL;
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
145 void
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);
154 return;
157 client_array_append(&t->clients, c);
158 ewmh_client_update_desktop(c);
159 client_need_reban(c);
161 /* call hook */
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
174 void
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);
184 /* call hook */
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);
192 return;
196 /** Check if a client is tagged with the specified tag.
197 * \param c the client
198 * \param t the tag
199 * \return true if the client is tagged with the tag, false otherwise.
201 bool
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)
206 return true;
208 return false;
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.
216 tag_t **
217 tags_get_current(screen_t *screen)
219 tag_t **out = p_new(tag_t *, screen->tags.len + 1);
220 int n = 0;
222 foreach(tag, screen->tags)
223 if((*tag)->selected)
224 out[n++] = *tag;
226 return out;
230 /** Set a tag to be the only one viewed.
231 * \param target the tag to see
233 static void
234 tag_view_only(tag_t *target)
236 if(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.
245 void
246 tag_view_only_byindex(screen_t *screen, int dindex)
248 tag_array_t *tags = &screen->tags;
250 if(dindex < 0 || dindex >= tags->len)
251 return;
252 tag_view_only(tags->tab[dindex]);
255 /** Create a new tag.
256 * \param L The Lua VM state.
257 * \luastack
258 * \lparam A name.
259 * \lreturn A new tag object.
261 static int
262 luaA_tag_new(lua_State *L)
264 size_t len;
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);
270 return 1;
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.
276 * \luastack
277 * \lparam None or a table of clients to set.
278 * \lreturn A table with the clients attached to this tags.
280 static int
281 luaA_tag_clients(lua_State *L)
283 tag_t *tag = luaL_checkudata(L, 1, "tag");
284 client_array_t *clients = &tag->clients;
285 int i;
287 if(lua_gettop(L) == 2)
289 luaA_checktable(L, 2);
290 foreach(c, tag->clients)
291 untag_client(*c, tag);
292 lua_pushnil(L);
293 while(lua_next(L, 2))
295 client_t *c = luaA_client_checkudata(L, -1);
296 /* push tag on top of the stack */
297 lua_pushvalue(L, 1);
298 tag_client(c);
299 lua_pop(L, 1);
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);
310 return 1;
313 /** Tag object.
314 * \param L The Lua VM state.
315 * \return The number of elements pushed on stack.
316 * \luastack
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.
322 static int
323 luaA_tag_index(lua_State *L)
325 size_t len;
326 tag_t *tag = luaL_checkudata(L, 1, "tag");
327 const char *attr;
329 if(luaA_usemetatable(L, 1, 2))
330 return 1;
332 attr = luaL_checklstring(L, 2, &len);
334 switch(a_tokenize(attr, len))
336 case A_TK_NAME:
337 lua_pushstring(L, tag->name);
338 break;
339 case A_TK_SCREEN:
340 if(!tag->screen)
341 return 0;
342 lua_pushnumber(L, screen_array_indexof(&globalconf.screens, tag->screen) + 1);
343 break;
344 case A_TK_SELECTED:
345 lua_pushboolean(L, tag->selected);
346 break;
347 default:
348 return 0;
351 return 1;
354 /** Tag newindex.
355 * \param L The Lua VM state.
356 * \return The number of elements pushed on stack.
358 static int
359 luaA_tag_newindex(lua_State *L)
361 size_t len;
362 tag_t *tag = luaL_checkudata(L, 1, "tag");
363 const char *attr = luaL_checklstring(L, 2, &len);
365 switch(a_tokenize(attr, len))
367 int screen;
369 case A_TK_NAME:
371 const char *buf = luaL_checklstring(L, 3, &len);
372 p_delete(&tag->name);
373 a_iso2utf8(buf, len, &tag->name, NULL);
375 break;
376 case A_TK_SCREEN:
377 if(!lua_isnil(L, 3))
379 screen = luaL_checknumber(L, 3) - 1;
380 luaA_checkscreen(screen);
382 else
383 screen = -1;
385 if(tag->screen)
386 tag_remove_from_screen(tag);
388 if(screen != -1)
390 /* push tag on top of the stack */
391 lua_pushvalue(L, 1);
392 tag_append_to_screen(&globalconf.screens.tab[screen]);
394 break;
395 case A_TK_SELECTED:
396 if(tag->screen)
397 tag_view(tag, luaA_checkboolean(L, 3));
398 return 0;
399 default:
400 return 0;
403 return 0;
406 const struct luaL_reg awesome_tag_methods[] =
408 { "__call", luaA_tag_new },
409 { NULL, NULL }
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 },
418 { NULL, NULL },
421 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80