Revert "client: fix windows managed on startup"
[awesome.git] / tag.c
blob23167eb65fc1f862aee0520e93eabb169a809cb8
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 "layouts/magnifier.h"
29 #include "layouts/tile.h"
30 #include "layouts/max.h"
31 #include "layouts/floating.h"
32 #include "layouts/fibonacci.h"
34 #include "layoutgen.h"
36 extern awesome_t globalconf;
38 DO_LUA_NEW(extern, tag_t, tag, "tag", tag_ref)
39 DO_LUA_GC(tag_t, tag, "tag", tag_unref)
40 DO_LUA_EQ(tag_t, tag, "tag")
42 /** View or unview a tag.
43 * \param tag the tag
44 * \param view set visible or not
46 static void
47 tag_view(tag_t *tag, bool view)
49 tag->selected = view;
50 ewmh_update_net_current_desktop(screen_virttophys(tag->screen));
51 widget_invalidate_cache(tag->screen, WIDGET_CACHE_TAGS);
52 globalconf.screens[tag->screen].need_arrange = true;
55 /** Create a new tag. Parameters values are checked.
56 * \param name Tag name.
57 * \param len Tag name length.
58 * \param layout Layout to use.
59 * \param mwfact Master width factor.
60 * \param nmaster Number of master windows.
61 * \param ncol Number of columns for slaves windows.
62 * \return A new tag with all these parameters.
64 tag_t *
65 tag_new(const char *name, ssize_t len, layout_t *layout, double mwfact, int nmaster, int ncol)
67 tag_t *tag;
69 tag = p_new(tag_t, 1);
70 a_iso2utf8(&tag->name, name, len);
71 tag->layout = layout;
73 /* to avoid error */
74 tag->screen = SCREEN_UNDEF;
76 tag->mwfact = mwfact;
77 if(tag->mwfact <= 0 || tag->mwfact >= 1)
78 tag->mwfact = 0.5;
80 if((tag->nmaster = nmaster) < 0)
81 tag->nmaster = 1;
83 if((tag->ncol = ncol) < 1)
84 tag->ncol = 1;
86 return tag;
89 /** Append a tag to a screen.
90 * \param tag the tag to append
91 * \param screen the screen id
93 void
94 tag_append_to_screen(tag_t *tag, int screen)
96 int phys_screen = screen_virttophys(screen);
98 tag->screen = screen;
99 tag_array_append(&globalconf.screens[screen].tags, tag_ref(&tag));
100 ewmh_update_net_numbers_of_desktop(phys_screen);
101 ewmh_update_net_desktop_names(phys_screen);
102 ewmh_update_workarea(phys_screen);
103 widget_invalidate_cache(screen, WIDGET_CACHE_TAGS);
106 /** Remove a tag from screen. Tag must be on a screen and have no clients.
107 * \param tag The tag to remove.
109 static void
110 tag_remove_from_screen(tag_t *tag)
112 int phys_screen = screen_virttophys(tag->screen);
113 tag_array_t *tags = &globalconf.screens[tag->screen].tags;
115 for(int i = 0; i < tags->len; i++)
116 if(tags->tab[i] == tag)
118 tag_array_take(tags, i);
119 break;
121 ewmh_update_net_numbers_of_desktop(phys_screen);
122 ewmh_update_net_desktop_names(phys_screen);
123 ewmh_update_workarea(phys_screen);
124 widget_invalidate_cache(tag->screen, WIDGET_CACHE_TAGS);
125 tag->screen = SCREEN_UNDEF;
126 tag_unref(&tag);
129 /** Tag a client with specified tag.
130 * \param c the client to tag
131 * \param t the tag to tag the client with
133 void
134 tag_client(client_t *c, tag_t *t)
136 /* don't tag twice */
137 if(is_client_tagged(c, t))
138 return;
140 client_array_append(&t->clients, c);
141 client_saveprops(c);
142 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
143 globalconf.screens[c->screen].need_arrange = true;
146 /** Untag a client with specified tag.
147 * \param c the client to tag
148 * \param t the tag to tag the client with
150 void
151 untag_client(client_t *c, tag_t *t)
153 for(int i = 0; i < t->clients.len; i++)
154 if(t->clients.tab[i] == c)
156 client_array_take(&t->clients, i);
157 client_saveprops(c);
158 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
159 globalconf.screens[c->screen].need_arrange = true;
160 return;
164 /** Check if a client is tagged with the specified tag.
165 * \param c the client
166 * \param t the tag
167 * \return true if the client is tagged with the tag, false otherwise.
169 bool
170 is_client_tagged(client_t *c, tag_t *t)
172 for(int i = 0; i < t->clients.len; i++)
173 if (t->clients.tab[i] == c)
174 return true;
176 return false;
179 /** Get the current tags for the specified screen.
180 * Returned pointer must be p_delete'd after.
181 * \param screen screen id
182 * \return a double pointer of tag list finished with a NULL element
184 tag_t **
185 tags_get_current(int screen)
187 tag_array_t *tags = &globalconf.screens[screen].tags;
188 tag_t **out = p_new(tag_t *, tags->len + 1);
189 int n = 0;
191 for(int i = 0; i < tags->len; i++)
192 if(tags->tab[i]->selected)
193 out[n++] = tags->tab[i];
195 return out;
199 /** Set a tag to be the only one viewed.
200 * \param target the tag to see
202 static void
203 tag_view_only(tag_t *target)
205 if (target)
207 tag_array_t *tags = &globalconf.screens[target->screen].tags;
209 for(int i = 0; i < tags->len; i++)
210 tag_view(tags->tab[i], tags->tab[i] == target);
214 /** View only a tag, selected by its index.
215 * \param screen screen id
216 * \param dindex the index
218 void
219 tag_view_only_byindex(int screen, int dindex)
221 tag_array_t *tags = &globalconf.screens[screen].tags;
223 if(dindex < 0 || dindex >= tags->len)
224 return;
225 tag_view_only(tags->tab[dindex]);
228 /** Convert a tag to a printable string.
229 * \param L The Lua VM state.
231 * \luastack
232 * \lvalue A tag.
233 * \lreturn A string.
235 static int
236 luaA_tag_tostring(lua_State *L)
238 tag_t **p = luaA_checkudata(L, 1, "tag");
239 lua_pushfstring(L, "[tag udata(%p) name(%s)]", *p, (*p)->name);
240 return 1;
243 /** Get all tags from a screen.
244 * \param L The Lua VM state.
246 * \luastack
247 * \lparam A screen number.
248 * \lreturn A table with all tags from the screen specified, indexed by tag name.
250 static int
251 luaA_tag_get(lua_State *L)
253 int screen = luaL_checknumber(L, 1) - 1;
254 tag_array_t *tags = &globalconf.screens[screen].tags;
256 luaA_checkscreen(screen);
258 lua_newtable(L);
260 for(int i = 0; i < tags->len; i++)
262 luaA_tag_userdata_new(L, tags->tab[i]);
263 lua_setfield(L, -2, tags->tab[i]->name);
266 return 1;
269 /** Get all tags from a screen.
270 * \param L The Lua VM state.
272 * \luastack
273 * \lparam A screen number.
274 * \lreturn A table with all tags from the screen specified, ordered and indexed
275 * by number.
277 static int
278 luaA_tag_geti(lua_State *L)
280 int screen = luaL_checknumber(L, 1) - 1;
281 tag_array_t *tags = &globalconf.screens[screen].tags;
283 luaA_checkscreen(screen);
285 lua_newtable(L);
287 for(int i = 0; i < tags->len; i++)
289 luaA_tag_userdata_new(L, tags->tab[i]);
290 lua_rawseti(L, -2, i + 1);
293 return 1;
296 /** Create a new tag.
297 * \param L The Lua VM state.
299 * \luastack
300 * \lparam A table with at least a name attribute.
301 * Optional attributes are: mwfact, ncol, nmaster and layout.
302 * \lreturn A new tag object.
304 static int
305 luaA_tag_new(lua_State *L)
307 size_t len;
308 tag_t *tag;
309 int ncol, nmaster;
310 const char *name, *lay;
311 double mwfact;
312 layout_t *layout;
314 luaA_checktable(L, 2);
316 if(!(name = luaA_getopt_string(L, 2, "name", NULL)))
317 luaL_error(L, "object tag must have a name");
319 mwfact = luaA_getopt_number(L, 2, "mwfact", 0.5);
320 ncol = luaA_getopt_number(L, 2, "ncol", 1);
321 nmaster = luaA_getopt_number(L, 2, "nmaster", 1);
322 lay = luaA_getopt_lstring(L, 2, "layout", "tile", &len);
324 layout = name_func_lookup(lay, LayoutList);
326 tag = tag_new(name, len,
327 layout,
328 mwfact, nmaster, ncol);
330 return luaA_tag_userdata_new(L, tag);
333 /** Tag object.
334 * \param L The Lua VM state.
335 * \return The number of elements pushed on stack.
336 * \luastack
337 * \lfield name Tag name.
338 * \lfield screen Screen number of the tag.
339 * \lfield layout Tag layout.
340 * \lfield selected True if the client is selected to be viewed.
341 * \lfield mwfact Master width factor.
342 * \lfield nmaster Number of master windows.
343 * \lfield ncol Number of column for slave windows.
344 * \lfield clients The clients that has this tag set.
346 static int
347 luaA_tag_index(lua_State *L)
349 size_t len;
350 tag_t **tag = luaA_checkudata(L, 1, "tag");
351 const char *attr;
352 client_array_t *clients;
353 int i;
355 if(luaA_usemetatable(L, 1, 2))
356 return 1;
358 attr = luaL_checklstring(L, 2, &len);
360 switch(a_tokenize(attr, len))
362 case A_TK_NAME:
363 lua_pushstring(L, (*tag)->name);
364 break;
365 case A_TK_SCREEN:
366 if((*tag)->screen == SCREEN_UNDEF)
367 return 0;
368 lua_pushnumber(L, (*tag)->screen + 1);
369 break;
370 case A_TK_LAYOUT:
371 lua_pushstring(L, name_func_rlookup((*tag)->layout, LayoutList));
372 break;
373 case A_TK_SELECTED:
374 lua_pushboolean(L, (*tag)->selected);
375 break;
376 case A_TK_MWFACT:
377 lua_pushnumber(L, (*tag)->mwfact);
378 break;
379 case A_TK_NMASTER:
380 lua_pushnumber(L, (*tag)->nmaster);
381 break;
382 case A_TK_NCOL:
383 lua_pushnumber(L, (*tag)->ncol);
384 break;
385 case A_TK_CLIENTS:
386 clients = &(*tag)->clients;
387 luaA_otable_new(L);
388 for(i = 0; i < clients->len; i++)
390 luaA_client_userdata_new(L, clients->tab[i]);
391 luaA_client_userdata_new(L, clients->tab[i]);
392 lua_rawset(L, -3);
394 break;
395 default:
396 return 0;
399 return 1;
402 /** Get a screen by its name.
403 * \param screen The screen to look into.
404 * \param name The name.
406 static tag_t *
407 tag_getbyname(int screen, const char *name)
409 int i;
410 tag_array_t *tags = &globalconf.screens[screen].tags;
412 for(i = 0; i < tags->len; i++)
413 if(!a_strcmp(name, tags->tab[i]->name))
414 return tags->tab[i];
415 return NULL;
418 /** Tag newindex.
419 * \param L The Lua VM state.
420 * \return The number of elements pushed on stack.
422 static int
423 luaA_tag_newindex(lua_State *L)
425 size_t len;
426 tag_t **tag = luaA_checkudata(L, 1, "tag");
427 const char *buf, *attr = luaL_checklstring(L, 2, &len);
428 double d;
429 int i, screen;
430 layout_t *l;
431 client_t **c;
433 switch(a_tokenize(attr, len))
435 case A_TK_NAME:
436 buf = luaL_checklstring(L, 3, &len);
437 if((*tag)->screen != SCREEN_UNDEF)
439 if(tag_getbyname((*tag)->screen, (*tag)->name) != *tag)
440 luaL_error(L, "a tag with the name `%s' is already on screen %d",
441 buf, (*tag)->screen);
442 widget_invalidate_cache((*tag)->screen, WIDGET_CACHE_TAGS);
444 p_delete(&(*tag)->name);
445 a_iso2utf8(&(*tag)->name, buf, len);
446 break;
447 case A_TK_SCREEN:
448 if(!lua_isnil(L, 3))
450 screen = luaL_checknumber(L, 3) - 1;
451 luaA_checkscreen(screen);
453 else
454 screen = SCREEN_UNDEF;
456 if((*tag)->screen != SCREEN_UNDEF)
458 if((*tag)->clients.len)
459 luaL_error(L, "unable to remove tag %s from screen %d, it still has clients",
460 (*tag)->name, (*tag)->screen);
461 else
462 tag_remove_from_screen(*tag);
465 if(screen != SCREEN_UNDEF)
467 if(tag_getbyname(screen, (*tag)->name))
468 luaL_error(L, "a tag with the name `%s' is already on screen %d",
469 (*tag)->name, screen);
471 tag_append_to_screen(*tag, screen);
473 break;
474 case A_TK_LAYOUT:
475 buf = luaL_checkstring(L, 3);
476 l = name_func_lookup(buf, LayoutList);
477 if(l)
478 (*tag)->layout = l;
479 else
480 luaL_error(L, "unknown layout: %s", buf);
481 break;
482 case A_TK_SELECTED:
483 tag_view(*tag, luaA_checkboolean(L, 3));
484 return 0;
485 case A_TK_MWFACT:
486 d = luaL_checknumber(L, 3);
487 if(d > 0 && d < 1)
488 (*tag)->mwfact = d;
489 else
490 luaL_error(L, "bad value, must be between 0 and 1");
491 break;
492 case A_TK_NMASTER:
493 i = luaL_checknumber(L, 3);
494 if(i >= 0)
495 (*tag)->nmaster = i;
496 else
497 luaL_error(L, "bad value, must be greater than 0");
498 break;
499 case A_TK_NCOL:
500 i = luaL_checknumber(L, 3);
501 if(i >= 1)
502 (*tag)->ncol = i;
503 else
504 luaL_error(L, "bad value, must be greater than 1");
505 break;
506 case A_TK_CLIENTS:
507 luaA_checktable(L, 3);
508 for(i = 0; i < (*tag)->clients.len; i++)
509 untag_client((*tag)->clients.tab[i], *tag);
510 lua_pushnil(L);
511 while(lua_next(L, 3))
513 c = luaA_checkudata(L, -1, "client");
514 tag_client(*c, *tag);
515 lua_pop(L, 1);
517 break;
518 default:
519 return 0;
522 if((*tag)->screen != SCREEN_UNDEF)
523 globalconf.screens[(*tag)->screen].need_arrange = true;
525 return 0;
528 const struct luaL_reg awesome_tag_methods[] =
530 { "__call", luaA_tag_new },
531 { "get", luaA_tag_get },
532 { "geti", luaA_tag_geti },
533 { NULL, NULL }
535 const struct luaL_reg awesome_tag_meta[] =
537 { "__index", luaA_tag_index },
538 { "__newindex", luaA_tag_newindex },
539 { "__eq", luaA_tag_eq },
540 { "__gc", luaA_tag_gc },
541 { "__tostring", luaA_tag_tostring },
542 { NULL, NULL },
545 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80