change codename
[awesome.git] / tag.c
blob1d03e8adf6892c4e5f0738c60ddfb1d091cd3dee
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 #include "layoutgen.h"
30 extern awesome_t globalconf;
32 DO_LUA_NEW(extern, tag_t, tag, "tag", tag_ref)
33 DO_LUA_GC(tag_t, tag, "tag", tag_unref)
34 DO_LUA_EQ(tag_t, tag, "tag")
36 /** View or unview a tag.
37 * \param tag the tag
38 * \param view set visible or not
40 static void
41 tag_view(tag_t *tag, bool view)
43 tag->selected = view;
44 ewmh_update_net_current_desktop(screen_virttophys(tag->screen));
45 globalconf.screens[tag->screen].need_arrange = true;
48 /** Create a new tag. Parameters values are checked.
49 * \param name Tag name.
50 * \param len Tag name length.
51 * \param layout Layout to use.
52 * \param mwfact Master width factor.
53 * \param nmaster Number of master windows.
54 * \param ncol Number of columns for slaves windows.
55 * \return A new tag with all these parameters.
57 tag_t *
58 tag_new(const char *name, ssize_t len, layout_t *layout, double mwfact, int nmaster, int ncol)
60 tag_t *tag;
62 tag = p_new(tag_t, 1);
63 a_iso2utf8(&tag->name, name, len);
64 tag->layout = layout;
66 /* to avoid error */
67 tag->screen = SCREEN_UNDEF;
69 tag->mwfact = mwfact;
70 if(tag->mwfact <= 0 || tag->mwfact >= 1)
71 tag->mwfact = 0.5;
73 if((tag->nmaster = nmaster) < 0)
74 tag->nmaster = 1;
76 if((tag->ncol = ncol) < 1)
77 tag->ncol = 1;
79 return tag;
82 /** Append a tag to a screen.
83 * \param tag the tag to append
84 * \param screen the screen id
86 void
87 tag_append_to_screen(tag_t *tag, screen_t *s)
89 int phys_screen = screen_virttophys(s->index);
91 tag->screen = s->index;
92 tag_array_append(&s->tags, tag_ref(&tag));
93 ewmh_update_net_numbers_of_desktop(phys_screen);
94 ewmh_update_net_desktop_names(phys_screen);
95 ewmh_update_workarea(phys_screen);
97 /* resave tag prop of all clients so the number of tag will be the
98 * same */
99 for(client_t *c = globalconf.clients; c; c = c->next)
100 client_saveprops_tags(c);
102 /* call hook */
103 if(globalconf.hooks.tags != LUA_REFNIL)
105 lua_pushnumber(globalconf.L, s->index + 1);
106 luaA_tag_userdata_new(globalconf.L, tag);
107 lua_pushliteral(globalconf.L, "add");
108 luaA_dofunction(globalconf.L, globalconf.hooks.tags, 3, 0);
112 /** Remove a tag from screen. Tag must be on a screen and have no clients.
113 * \param tag The tag to remove.
115 static void
116 tag_remove_from_screen(tag_t *tag)
118 int screen = tag->screen;
119 int phys_screen = screen_virttophys(tag->screen);
120 tag_array_t *tags = &globalconf.screens[tag->screen].tags;
122 for(int i = 0; i < tags->len; i++)
123 if(tags->tab[i] == tag)
125 tag_array_take(tags, i);
126 break;
128 ewmh_update_net_numbers_of_desktop(phys_screen);
129 ewmh_update_net_desktop_names(phys_screen);
130 ewmh_update_workarea(phys_screen);
131 tag->screen = SCREEN_UNDEF;
133 /* resave tag prop of all clients so the number of tag will be the
134 * same */
135 for(client_t *c = globalconf.clients; c; c = c->next)
136 client_saveprops_tags(c);
138 /* call hook */
139 if(globalconf.hooks.tags != LUA_REFNIL)
141 lua_pushnumber(globalconf.L, screen + 1);
142 luaA_tag_userdata_new(globalconf.L, tag);
143 lua_pushliteral(globalconf.L, "remove");
144 luaA_dofunction(globalconf.L, globalconf.hooks.tags, 3, 0);
147 tag_unref(&tag);
150 /** Tag a client with specified tag.
151 * \param c the client to tag
152 * \param t the tag to tag the client with
154 void
155 tag_client(client_t *c, tag_t *t)
157 /* don't tag twice */
158 if(is_client_tagged(c, t))
159 return;
161 tag_ref(&t);
162 client_array_append(&t->clients, c);
163 client_saveprops_tags(c);
164 client_need_arrange(c);
165 /* call hook */
166 if(globalconf.hooks.tagged != LUA_REFNIL)
168 luaA_client_userdata_new(globalconf.L, c);
169 luaA_tag_userdata_new(globalconf.L, t);
170 luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
174 /** Untag a client with specified tag.
175 * \param c the client to tag
176 * \param t the tag to tag the client with
178 void
179 untag_client(client_t *c, tag_t *t)
181 for(int i = 0; i < t->clients.len; i++)
182 if(t->clients.tab[i] == c)
184 client_need_arrange(c);
185 client_array_take(&t->clients, i);
186 client_saveprops_tags(c);
187 /* call hook */
188 if(globalconf.hooks.tagged != LUA_REFNIL)
190 luaA_client_userdata_new(globalconf.L, c);
191 luaA_tag_userdata_new(globalconf.L, t);
192 luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
194 tag_unref(&t);
195 return;
199 /** Check if a client is tagged with the specified tag.
200 * \param c the client
201 * \param t the tag
202 * \return true if the client is tagged with the tag, false otherwise.
204 bool
205 is_client_tagged(client_t *c, tag_t *t)
207 for(int i = 0; i < t->clients.len; i++)
208 if (t->clients.tab[i] == c)
209 return true;
211 return false;
214 /** Get the current tags for the specified screen.
215 * Returned pointer must be p_delete'd after.
216 * \param screen screen id
217 * \return a double pointer of tag list finished with a NULL element
219 tag_t **
220 tags_get_current(int screen)
222 tag_array_t *tags = &globalconf.screens[screen].tags;
223 tag_t **out = p_new(tag_t *, tags->len + 1);
224 int n = 0;
226 for(int i = 0; i < tags->len; i++)
227 if(tags->tab[i]->selected)
228 out[n++] = tags->tab[i];
230 return out;
234 /** Set a tag to be the only one viewed.
235 * \param target the tag to see
237 static void
238 tag_view_only(tag_t *target)
240 if (target)
242 tag_array_t *tags = &globalconf.screens[target->screen].tags;
244 for(int i = 0; i < tags->len; i++)
245 tag_view(tags->tab[i], tags->tab[i] == target);
249 /** View only a tag, selected by its index.
250 * \param screen screen id
251 * \param dindex the index
253 void
254 tag_view_only_byindex(int screen, int dindex)
256 tag_array_t *tags = &globalconf.screens[screen].tags;
258 if(dindex < 0 || dindex >= tags->len)
259 return;
260 tag_view_only(tags->tab[dindex]);
263 /** Create a new tag.
264 * \param L The Lua VM state.
266 * \luastack
267 * \lparam A table with at least a name attribute.
268 * Optional attributes are: mwfact, ncol, nmaster and layout.
269 * \lreturn A new tag object.
271 static int
272 luaA_tag_new(lua_State *L)
274 size_t len;
275 tag_t *tag;
276 int ncol, nmaster;
277 const char *name, *lay;
278 double mwfact;
279 layout_t *layout;
281 luaA_checktable(L, 2);
283 if(!(name = luaA_getopt_lstring(L, 2, "name", NULL, &len)))
284 luaL_error(L, "object tag must have a name");
286 mwfact = luaA_getopt_number(L, 2, "mwfact", 0.5);
287 ncol = luaA_getopt_number(L, 2, "ncol", 1);
288 nmaster = luaA_getopt_number(L, 2, "nmaster", 1);
289 lay = luaA_getopt_string(L, 2, "layout", "tile");
291 layout = name_func_lookup(lay, LayoutList);
293 tag = tag_new(name, len,
294 layout,
295 mwfact, nmaster, ncol);
297 return luaA_tag_userdata_new(L, tag);
300 /** Get or set the clients attached to this tag.
301 * \param L The Lua VM state.
302 * \return The number of elements pushed on stack.
303 * \luastack
304 * \lparam None or a table of clients to set.
305 * \lreturn A table with the clients attached to this tags.
307 static int
308 luaA_tag_clients(lua_State *L)
310 tag_t **tag = luaA_checkudata(L, 1, "tag");
311 client_array_t *clients = &(*tag)->clients;
312 int i;
314 if(lua_gettop(L) == 2)
316 client_t **c;
318 luaA_checktable(L, 2);
319 for(i = 0; i < (*tag)->clients.len; i++)
320 untag_client((*tag)->clients.tab[i], *tag);
321 lua_pushnil(L);
322 while(lua_next(L, 2))
324 c = luaA_checkudata(L, -1, "client");
325 tag_client(*c, *tag);
326 lua_pop(L, 1);
330 luaA_otable_new(L);
331 for(i = 0; i < clients->len; i++)
333 luaA_client_userdata_new(L, clients->tab[i]);
334 lua_rawseti(L, -2, i + 1);
337 return 1;
340 /** Tag object.
341 * \param L The Lua VM state.
342 * \return The number of elements pushed on stack.
343 * \luastack
344 * \lfield name Tag name.
345 * \lfield screen Screen number of the tag.
346 * \lfield layout Tag layout.
347 * \lfield selected True if the client is selected to be viewed.
348 * \lfield mwfact Master width factor.
349 * \lfield nmaster Number of master windows.
350 * \lfield ncol Number of column for slave windows.
352 static int
353 luaA_tag_index(lua_State *L)
355 size_t len;
356 tag_t **tag = luaA_checkudata(L, 1, "tag");
357 const char *attr;
359 if(luaA_usemetatable(L, 1, 2))
360 return 1;
362 attr = luaL_checklstring(L, 2, &len);
364 switch(a_tokenize(attr, len))
366 case A_TK_NAME:
367 lua_pushstring(L, (*tag)->name);
368 break;
369 case A_TK_SCREEN:
370 if((*tag)->screen == SCREEN_UNDEF)
371 return 0;
372 lua_pushnumber(L, (*tag)->screen + 1);
373 break;
374 case A_TK_LAYOUT:
375 lua_pushstring(L, name_func_rlookup((*tag)->layout, LayoutList));
376 break;
377 case A_TK_SELECTED:
378 lua_pushboolean(L, (*tag)->selected);
379 break;
380 case A_TK_MWFACT:
381 lua_pushnumber(L, (*tag)->mwfact);
382 break;
383 case A_TK_NMASTER:
384 lua_pushnumber(L, (*tag)->nmaster);
385 break;
386 case A_TK_NCOL:
387 lua_pushnumber(L, (*tag)->ncol);
388 break;
389 default:
390 return 0;
393 return 1;
396 /** Tag newindex.
397 * \param L The Lua VM state.
398 * \return The number of elements pushed on stack.
400 static int
401 luaA_tag_newindex(lua_State *L)
403 size_t len;
404 tag_t **tag = luaA_checkudata(L, 1, "tag");
405 const char *buf, *attr = luaL_checklstring(L, 2, &len);
406 double d;
407 int i, screen;
408 layout_t *l;
410 switch(a_tokenize(attr, len))
412 case A_TK_NAME:
413 buf = luaL_checklstring(L, 3, &len);
414 p_delete(&(*tag)->name);
415 a_iso2utf8(&(*tag)->name, buf, len);
416 break;
417 case A_TK_SCREEN:
418 if(!lua_isnil(L, 3))
420 screen = luaL_checknumber(L, 3) - 1;
421 luaA_checkscreen(screen);
423 else
424 screen = SCREEN_UNDEF;
426 if((*tag)->screen != SCREEN_UNDEF)
427 tag_remove_from_screen(*tag);
429 if(screen != SCREEN_UNDEF)
430 tag_append_to_screen(*tag, &globalconf.screens[screen]);
431 break;
432 case A_TK_LAYOUT:
433 buf = luaL_checkstring(L, 3);
434 l = name_func_lookup(buf, LayoutList);
435 if(l)
436 (*tag)->layout = l;
437 else
439 luaA_warn(L, "unknown layout: %s", buf);
440 return 0;
442 break;
443 case A_TK_SELECTED:
444 if((*tag)->screen != SCREEN_UNDEF)
445 tag_view(*tag, luaA_checkboolean(L, 3));
446 return 0;
447 case A_TK_MWFACT:
448 d = luaL_checknumber(L, 3);
449 if(d > 0 && d < 1)
450 (*tag)->mwfact = d;
451 else
453 luaA_warn(L, "bad value, must be between 0 and 1");
454 return 0;
456 break;
457 case A_TK_NMASTER:
458 i = luaL_checknumber(L, 3);
459 if(i >= 0)
460 (*tag)->nmaster = i;
461 else
463 luaA_warn(L, "bad value, must be greater than 0");
464 return 0;
466 break;
467 case A_TK_NCOL:
468 i = luaL_checknumber(L, 3);
469 if(i >= 1)
470 (*tag)->ncol = i;
471 else
473 luaA_warn(L, "bad value, must be greater than 1");
474 return 0;
476 break;
477 default:
478 return 0;
481 if((*tag)->screen != SCREEN_UNDEF && (*tag)->selected)
482 globalconf.screens[(*tag)->screen].need_arrange = true;
484 return 0;
487 const struct luaL_reg awesome_tag_methods[] =
489 { "__call", luaA_tag_new },
490 { NULL, NULL }
492 const struct luaL_reg awesome_tag_meta[] =
494 { "clients", luaA_tag_clients },
495 { "__index", luaA_tag_index },
496 { "__newindex", luaA_tag_newindex },
497 { "__eq", luaA_tag_eq },
498 { "__gc", luaA_tag_gc },
499 { "__tostring", luaA_tag_tostring },
500 { NULL, NULL },
503 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80