awesomerc: do not change windows screen if filtered out.
[awesome.git] / tag.c
blob55f2b139a4b4b7027c6789fcb2c31a9e62071ee3
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 extern awesome_t globalconf;
30 DO_LUA_NEW(extern, tag_t, tag, "tag", tag_ref)
31 DO_LUA_GC(tag_t, tag, "tag", tag_unref)
32 DO_LUA_EQ(tag_t, tag, "tag")
34 /** View or unview a tag.
35 * \param tag the tag
36 * \param view set visible or not
38 static void
39 tag_view(tag_t *tag, bool view)
41 tag->selected = view;
42 ewmh_update_net_current_desktop(screen_virttophys(tag->screen));
43 globalconf.screens[tag->screen].need_arrange = true;
46 /** Create a new tag. Parameters values are checked.
47 * \param name Tag name.
48 * \param len Tag name length.
49 * \return A new tag with all these parameters.
51 tag_t *
52 tag_new(const char *name, ssize_t len)
54 tag_t *tag;
56 tag = p_new(tag_t, 1);
57 a_iso2utf8(name, len, &tag->name, NULL);
59 /* to avoid error */
60 tag->screen = SCREEN_UNDEF;
62 return tag;
65 /** Append a tag to a screen.
66 * \param tag the tag to append
67 * \param screen the screen id
69 void
70 tag_append_to_screen(tag_t *tag, screen_t *s)
72 int phys_screen = screen_virttophys(s->index);
74 tag->screen = s->index;
75 tag_array_append(&s->tags, tag_ref(&tag));
76 ewmh_update_net_numbers_of_desktop(phys_screen);
77 ewmh_update_net_desktop_names(phys_screen);
78 ewmh_update_workarea(phys_screen);
80 /* resave tag prop of all clients so the number of tag will be the
81 * same */
82 for(client_t *c = globalconf.clients; c; c = c->next)
83 client_saveprops_tags(c);
85 /* call hook */
86 if(globalconf.hooks.tags != LUA_REFNIL)
88 lua_pushnumber(globalconf.L, s->index + 1);
89 luaA_tag_userdata_new(globalconf.L, tag);
90 lua_pushliteral(globalconf.L, "add");
91 luaA_dofunction(globalconf.L, globalconf.hooks.tags, 3, 0);
95 /** Remove a tag from screen. Tag must be on a screen and have no clients.
96 * \param tag The tag to remove.
98 static void
99 tag_remove_from_screen(tag_t *tag)
101 int screen = tag->screen;
102 int phys_screen = screen_virttophys(tag->screen);
103 tag_array_t *tags = &globalconf.screens[tag->screen].tags;
105 for(int i = 0; i < tags->len; i++)
106 if(tags->tab[i] == tag)
108 tag_array_take(tags, i);
109 break;
111 ewmh_update_net_numbers_of_desktop(phys_screen);
112 ewmh_update_net_desktop_names(phys_screen);
113 ewmh_update_workarea(phys_screen);
114 tag->screen = SCREEN_UNDEF;
116 /* resave tag prop of all clients so the number of tag will be the
117 * same */
118 for(client_t *c = globalconf.clients; c; c = c->next)
119 client_saveprops_tags(c);
121 /* call hook */
122 if(globalconf.hooks.tags != LUA_REFNIL)
124 lua_pushnumber(globalconf.L, screen + 1);
125 luaA_tag_userdata_new(globalconf.L, tag);
126 lua_pushliteral(globalconf.L, "remove");
127 luaA_dofunction(globalconf.L, globalconf.hooks.tags, 3, 0);
130 tag_unref(&tag);
133 /** Tag a client with specified tag.
134 * \param c the client to tag
135 * \param t the tag to tag the client with
137 void
138 tag_client(client_t *c, tag_t *t)
140 /* don't tag twice */
141 if(is_client_tagged(c, t))
142 return;
144 tag_ref(&t);
145 client_array_append(&t->clients, c);
146 client_saveprops_tags(c);
147 client_need_arrange(c);
148 /* call hook */
149 if(globalconf.hooks.tagged != LUA_REFNIL)
151 luaA_client_userdata_new(globalconf.L, c);
152 luaA_tag_userdata_new(globalconf.L, t);
153 luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
157 /** Untag a client with specified tag.
158 * \param c the client to tag
159 * \param t the tag to tag the client with
161 void
162 untag_client(client_t *c, tag_t *t)
164 for(int i = 0; i < t->clients.len; i++)
165 if(t->clients.tab[i] == c)
167 client_need_arrange(c);
168 client_array_take(&t->clients, i);
169 client_saveprops_tags(c);
170 /* call hook */
171 if(globalconf.hooks.tagged != LUA_REFNIL)
173 luaA_client_userdata_new(globalconf.L, c);
174 luaA_tag_userdata_new(globalconf.L, t);
175 luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
177 tag_unref(&t);
178 return;
182 /** Check if a client is tagged with the specified tag.
183 * \param c the client
184 * \param t the tag
185 * \return true if the client is tagged with the tag, false otherwise.
187 bool
188 is_client_tagged(client_t *c, tag_t *t)
190 for(int i = 0; i < t->clients.len; i++)
191 if (t->clients.tab[i] == c)
192 return true;
194 return false;
197 /** Get the current tags for the specified screen.
198 * Returned pointer must be p_delete'd after.
199 * \param screen screen id
200 * \return a double pointer of tag list finished with a NULL element
202 tag_t **
203 tags_get_current(int screen)
205 tag_array_t *tags = &globalconf.screens[screen].tags;
206 tag_t **out = p_new(tag_t *, tags->len + 1);
207 int n = 0;
209 for(int i = 0; i < tags->len; i++)
210 if(tags->tab[i]->selected)
211 out[n++] = tags->tab[i];
213 return out;
217 /** Set a tag to be the only one viewed.
218 * \param target the tag to see
220 static void
221 tag_view_only(tag_t *target)
223 if (target)
225 tag_array_t *tags = &globalconf.screens[target->screen].tags;
227 for(int i = 0; i < tags->len; i++)
228 tag_view(tags->tab[i], tags->tab[i] == target);
232 /** View only a tag, selected by its index.
233 * \param screen screen id
234 * \param dindex the index
236 void
237 tag_view_only_byindex(int screen, int dindex)
239 tag_array_t *tags = &globalconf.screens[screen].tags;
241 if(dindex < 0 || dindex >= tags->len)
242 return;
243 tag_view_only(tags->tab[dindex]);
246 /** Create a new tag.
247 * \param L The Lua VM state.
248 * \luastack
249 * \lparam A name.
250 * \lreturn A new tag object.
252 static int
253 luaA_tag_new(lua_State *L)
255 size_t len;
256 const char *name = luaL_checklstring(L, 2, &len);
257 return luaA_tag_userdata_new(L, tag_new(name, len));
260 /** Get or set the clients attached to this tag.
261 * \param L The Lua VM state.
262 * \return The number of elements pushed on stack.
263 * \luastack
264 * \lparam None or a table of clients to set.
265 * \lreturn A table with the clients attached to this tags.
267 static int
268 luaA_tag_clients(lua_State *L)
270 tag_t **tag = luaA_checkudata(L, 1, "tag");
271 client_array_t *clients = &(*tag)->clients;
272 int i;
274 if(lua_gettop(L) == 2)
276 client_t **c;
278 luaA_checktable(L, 2);
279 for(i = 0; i < (*tag)->clients.len; i++)
280 untag_client((*tag)->clients.tab[i], *tag);
281 lua_pushnil(L);
282 while(lua_next(L, 2))
284 c = luaA_checkudata(L, -1, "client");
285 tag_client(*c, *tag);
286 lua_pop(L, 1);
290 luaA_otable_new(L);
291 for(i = 0; i < clients->len; i++)
293 luaA_client_userdata_new(L, clients->tab[i]);
294 lua_rawseti(L, -2, i + 1);
297 return 1;
300 /** Tag object.
301 * \param L The Lua VM state.
302 * \return The number of elements pushed on stack.
303 * \luastack
304 * \lfield name Tag name.
305 * \lfield screen Screen number of the tag.
306 * \lfield layout Tag layout.
307 * \lfield selected True if the client is selected to be viewed.
309 static int
310 luaA_tag_index(lua_State *L)
312 size_t len;
313 tag_t **tag = luaA_checkudata(L, 1, "tag");
314 const char *attr;
316 if(luaA_usemetatable(L, 1, 2))
317 return 1;
319 attr = luaL_checklstring(L, 2, &len);
321 switch(a_tokenize(attr, len))
323 case A_TK_NAME:
324 lua_pushstring(L, (*tag)->name);
325 break;
326 case A_TK_SCREEN:
327 if((*tag)->screen == SCREEN_UNDEF)
328 return 0;
329 lua_pushnumber(L, (*tag)->screen + 1);
330 break;
331 case A_TK_SELECTED:
332 lua_pushboolean(L, (*tag)->selected);
333 break;
334 default:
335 return 0;
338 return 1;
341 /** Tag newindex.
342 * \param L The Lua VM state.
343 * \return The number of elements pushed on stack.
345 static int
346 luaA_tag_newindex(lua_State *L)
348 size_t len;
349 tag_t **tag = luaA_checkudata(L, 1, "tag");
350 const char *attr = luaL_checklstring(L, 2, &len);
352 switch(a_tokenize(attr, len))
354 int screen;
356 case A_TK_NAME:
358 const char *buf = luaL_checklstring(L, 3, &len);
359 p_delete(&(*tag)->name);
360 a_iso2utf8(buf, len, &(*tag)->name, NULL);
362 break;
363 case A_TK_SCREEN:
364 if(!lua_isnil(L, 3))
366 screen = luaL_checknumber(L, 3) - 1;
367 luaA_checkscreen(screen);
369 else
370 screen = SCREEN_UNDEF;
372 if((*tag)->screen != SCREEN_UNDEF)
373 tag_remove_from_screen(*tag);
375 if(screen != SCREEN_UNDEF)
376 tag_append_to_screen(*tag, &globalconf.screens[screen]);
377 break;
378 case A_TK_SELECTED:
379 if((*tag)->screen != SCREEN_UNDEF)
380 tag_view(*tag, luaA_checkboolean(L, 3));
381 return 0;
382 default:
383 return 0;
386 if((*tag)->screen != SCREEN_UNDEF && (*tag)->selected)
387 globalconf.screens[(*tag)->screen].need_arrange = true;
389 return 0;
392 const struct luaL_reg awesome_tag_methods[] =
394 { "__call", luaA_tag_new },
395 { NULL, NULL }
397 const struct luaL_reg awesome_tag_meta[] =
399 { "clients", luaA_tag_clients },
400 { "__index", luaA_tag_index },
401 { "__newindex", luaA_tag_newindex },
402 { "__eq", luaA_tag_eq },
403 { "__gc", luaA_tag_gc },
404 { "__tostring", luaA_tag_tostring },
405 { NULL, NULL },
408 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80