change codename
[awesome.git] / tag.c
blob96589d5e7fb4c2857f4c690acd54639fe0f342b0
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 luaA_ref_array_wipe(&tag->refs);
45 client_array_wipe(&tag->clients);
46 p_delete(&tag->name);
47 return 0;
50 /** View or unview a tag.
51 * \param tag the tag
52 * \param view set visible or not
54 static void
55 tag_view(tag_t *tag, bool view)
57 tag->selected = view;
58 ewmh_update_net_current_desktop(screen_virttophys(screen_array_indexof(&globalconf.screens,
59 tag->screen)));
60 tag->screen->need_arrange = true;
63 /** Append a tag which on top of the stack to a screen.
64 * \param screen the screen id
66 void
67 tag_append_to_screen(screen_t *s)
69 tag_t *tag = luaL_checkudata(globalconf.L, -1, "tag");
71 /* can't attach a tag twice */
72 if(tag->screen)
73 return;
75 int screen_index = screen_array_indexof(&globalconf.screens, s);
76 int phys_screen = screen_virttophys(screen_index);
78 tag->screen = s;
79 tag_array_append(&s->tags, tag_ref(globalconf.L));
80 ewmh_update_net_numbers_of_desktop(phys_screen);
81 ewmh_update_net_desktop_names(phys_screen);
82 ewmh_update_workarea(phys_screen);
84 /* call hook */
85 if(globalconf.hooks.tags != LUA_REFNIL)
87 lua_pushnumber(globalconf.L, screen_index + 1);
88 tag_push(globalconf.L, tag);
89 lua_pushliteral(globalconf.L, "add");
90 luaA_dofunction(globalconf.L, globalconf.hooks.tags, 3, 0);
94 /** Remove a tag from screen. Tag must be on a screen and have no clients.
95 * \param tag The tag to remove.
97 static void
98 tag_remove_from_screen(tag_t *tag)
100 int screen_index = screen_array_indexof(&globalconf.screens, tag->screen);
101 int phys_screen = screen_virttophys(screen_index);
102 tag_array_t *tags = &tag->screen->tags;
104 for(int i = 0; i < tags->len; i++)
105 if(tags->tab[i] == tag)
107 tag_array_take(tags, i);
108 break;
110 ewmh_update_net_numbers_of_desktop(phys_screen);
111 ewmh_update_net_desktop_names(phys_screen);
112 ewmh_update_workarea(phys_screen);
114 /* call hook */
115 if(globalconf.hooks.tags != LUA_REFNIL)
117 lua_pushnumber(globalconf.L, screen_index + 1);
118 tag_push(globalconf.L, tag);
119 lua_pushliteral(globalconf.L, "remove");
120 luaA_dofunction(globalconf.L, globalconf.hooks.tags, 3, 0);
123 tag->screen = NULL;
125 tag_unref(globalconf.L, tag);
128 /** Tag a client with the tag on top of the stack.
129 * \param c the client to tag
131 void
132 tag_client(client_t *c)
134 tag_t *t = tag_ref(globalconf.L);
136 /* don't tag twice */
137 if(is_client_tagged(c, t))
138 return;
140 client_array_append(&t->clients, c);
141 ewmh_client_update_desktop(c);
142 client_need_arrange(c);
143 /* call hook */
144 if(globalconf.hooks.tagged != LUA_REFNIL)
146 client_push(globalconf.L, c);
147 tag_push(globalconf.L, t);
148 luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
152 /** Untag a client with specified tag.
153 * \param c the client to tag
154 * \param t the tag to tag the client with
156 void
157 untag_client(client_t *c, tag_t *t)
159 for(int i = 0; i < t->clients.len; i++)
160 if(t->clients.tab[i] == c)
162 client_need_arrange(c);
163 client_array_take(&t->clients, i);
164 ewmh_client_update_desktop(c);
165 /* call hook */
166 if(globalconf.hooks.tagged != LUA_REFNIL)
168 client_push(globalconf.L, c);
169 tag_push(globalconf.L, t);
170 luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
172 tag_unref(globalconf.L, t);
173 return;
177 /** Check if a client is tagged with the specified tag.
178 * \param c the client
179 * \param t the tag
180 * \return true if the client is tagged with the tag, false otherwise.
182 bool
183 is_client_tagged(client_t *c, tag_t *t)
185 for(int i = 0; i < t->clients.len; i++)
186 if(t->clients.tab[i] == c)
187 return true;
189 return false;
192 /** Get the current tags for the specified screen.
193 * Returned pointer must be p_delete'd after.
194 * \param screen Screen.
195 * \return A double pointer of tag list finished with a NULL element.
197 tag_t **
198 tags_get_current(screen_t *screen)
200 tag_t **out = p_new(tag_t *, screen->tags.len + 1);
201 int n = 0;
203 foreach(tag, screen->tags)
204 if((*tag)->selected)
205 out[n++] = *tag;
207 return out;
211 /** Set a tag to be the only one viewed.
212 * \param target the tag to see
214 static void
215 tag_view_only(tag_t *target)
217 if(target)
218 foreach(tag, target->screen->tags)
219 tag_view(*tag, *tag == target);
222 /** View only a tag, selected by its index.
223 * \param screen Screen.
224 * \param dindex The index.
226 void
227 tag_view_only_byindex(screen_t *screen, int dindex)
229 tag_array_t *tags = &screen->tags;
231 if(dindex < 0 || dindex >= tags->len)
232 return;
233 tag_view_only(tags->tab[dindex]);
236 /** Create a new tag.
237 * \param L The Lua VM state.
238 * \luastack
239 * \lparam A name.
240 * \lreturn A new tag object.
242 static int
243 luaA_tag_new(lua_State *L)
245 size_t len;
246 const char *name = luaL_checklstring(L, 2, &len);
247 tag_t *tag = tag_new(globalconf.L);
249 a_iso2utf8(name, len, &tag->name, NULL);
251 return 1;
254 /** Get or set the clients attached to this tag.
255 * \param L The Lua VM state.
256 * \return The number of elements pushed on stack.
257 * \luastack
258 * \lparam None or a table of clients to set.
259 * \lreturn A table with the clients attached to this tags.
261 static int
262 luaA_tag_clients(lua_State *L)
264 tag_t *tag = luaL_checkudata(L, 1, "tag");
265 client_array_t *clients = &tag->clients;
266 int i;
268 if(lua_gettop(L) == 2)
270 luaA_checktable(L, 2);
271 foreach(c, tag->clients)
272 untag_client(*c, tag);
273 lua_pushnil(L);
274 while(lua_next(L, 2))
276 client_t *c = luaA_client_checkudata(L, -1);
277 /* push tag on top of the stack */
278 lua_pushvalue(L, 1);
279 tag_client(c);
280 lua_pop(L, 1);
284 lua_createtable(L, clients->len, 0);
285 for(i = 0; i < clients->len; i++)
287 client_push(L, clients->tab[i]);
288 lua_rawseti(L, -2, i + 1);
291 return 1;
294 /** Tag object.
295 * \param L The Lua VM state.
296 * \return The number of elements pushed on stack.
297 * \luastack
298 * \lfield name Tag name.
299 * \lfield screen Screen number of the tag.
300 * \lfield layout Tag layout.
301 * \lfield selected True if the client is selected to be viewed.
303 static int
304 luaA_tag_index(lua_State *L)
306 size_t len;
307 tag_t *tag = luaL_checkudata(L, 1, "tag");
308 const char *attr;
310 if(luaA_usemetatable(L, 1, 2))
311 return 1;
313 attr = luaL_checklstring(L, 2, &len);
315 switch(a_tokenize(attr, len))
317 case A_TK_NAME:
318 lua_pushstring(L, tag->name);
319 break;
320 case A_TK_SCREEN:
321 if(!tag->screen)
322 return 0;
323 lua_pushnumber(L, screen_array_indexof(&globalconf.screens, tag->screen) + 1);
324 break;
325 case A_TK_SELECTED:
326 lua_pushboolean(L, tag->selected);
327 break;
328 default:
329 return 0;
332 return 1;
335 /** Tag newindex.
336 * \param L The Lua VM state.
337 * \return The number of elements pushed on stack.
339 static int
340 luaA_tag_newindex(lua_State *L)
342 size_t len;
343 tag_t *tag = luaL_checkudata(L, 1, "tag");
344 const char *attr = luaL_checklstring(L, 2, &len);
346 switch(a_tokenize(attr, len))
348 int screen;
350 case A_TK_NAME:
352 const char *buf = luaL_checklstring(L, 3, &len);
353 p_delete(&tag->name);
354 a_iso2utf8(buf, len, &tag->name, NULL);
356 break;
357 case A_TK_SCREEN:
358 if(!lua_isnil(L, 3))
360 screen = luaL_checknumber(L, 3) - 1;
361 luaA_checkscreen(screen);
363 else
364 screen = -1;
366 if(tag->screen)
367 tag_remove_from_screen(tag);
369 if(screen != -1)
371 /* push tag on top of the stack */
372 lua_pushvalue(L, 1);
373 tag_append_to_screen(&globalconf.screens.tab[screen]);
375 break;
376 case A_TK_SELECTED:
377 if(tag->screen)
378 tag_view(tag, luaA_checkboolean(L, 3));
379 return 0;
380 default:
381 return 0;
384 if(tag->screen && tag->selected)
385 tag->screen->need_arrange = true;
387 return 0;
390 const struct luaL_reg awesome_tag_methods[] =
392 { "__call", luaA_tag_new },
393 { NULL, NULL }
395 const struct luaL_reg awesome_tag_meta[] =
397 { "clients", luaA_tag_clients },
398 { "__index", luaA_tag_index },
399 { "__newindex", luaA_tag_newindex },
400 { "__gc", luaA_tag_gc },
401 { "__tostring", luaA_tag_tostring },
402 { NULL, NULL },
405 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80