awful: fix titlebar creation
[awesome.git] / tag.c
blob60238d331835b9ab073f4eeaf79c0c223ebf5de1
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 /** Tag a client with specified tag.
107 * \param c the client to tag
108 * \param t the tag to tag the client with
110 void
111 tag_client(client_t *c, tag_t *t)
113 /* don't tag twice */
114 if(is_client_tagged(c, t))
115 return;
117 client_array_append(&t->clients, c);
118 client_saveprops(c);
119 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
120 globalconf.screens[c->screen].need_arrange = true;
123 /** Untag a client with specified tag.
124 * \param c the client to tag
125 * \param t the tag to tag the client with
127 void
128 untag_client(client_t *c, tag_t *t)
130 for(int i = 0; i < t->clients.len; i++)
131 if(t->clients.tab[i] == c)
133 client_array_take(&t->clients, i);
134 client_saveprops(c);
135 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
136 globalconf.screens[c->screen].need_arrange = true;
137 return;
141 /** Check if a client is tagged with the specified tag.
142 * \param c the client
143 * \param t the tag
144 * \return true if the client is tagged with the tag, false otherwise.
146 bool
147 is_client_tagged(client_t *c, tag_t *t)
149 for(int i = 0; i < t->clients.len; i++)
150 if (t->clients.tab[i] == c)
151 return true;
153 return false;
156 /** Get the current tags for the specified screen.
157 * Returned pointer must be p_delete'd after.
158 * \param screen screen id
159 * \return a double pointer of tag list finished with a NULL element
161 tag_t **
162 tags_get_current(int screen)
164 tag_array_t *tags = &globalconf.screens[screen].tags;
165 tag_t **out = p_new(tag_t *, tags->len + 1);
166 int n = 0;
168 for(int i = 0; i < tags->len; i++)
169 if(tags->tab[i]->selected)
170 out[n++] = tags->tab[i];
172 return out;
176 /** Set a tag to be the only one viewed.
177 * \param target the tag to see
179 static void
180 tag_view_only(tag_t *target)
182 if (target)
184 tag_array_t *tags = &globalconf.screens[target->screen].tags;
186 for(int i = 0; i < tags->len; i++)
187 tag_view(tags->tab[i], tags->tab[i] == target);
191 /** View only a tag, selected by its index.
192 * \param screen screen id
193 * \param dindex the index
195 void
196 tag_view_only_byindex(int screen, int dindex)
198 tag_array_t *tags = &globalconf.screens[screen].tags;
200 if(dindex < 0 || dindex >= tags->len)
201 return;
202 tag_view_only(tags->tab[dindex]);
205 /** Convert a tag to a printable string.
206 * \param L The Lua VM state.
208 * \luastack
209 * \lvalue A tag.
210 * \lreturn A string.
212 static int
213 luaA_tag_tostring(lua_State *L)
215 tag_t **p = luaA_checkudata(L, 1, "tag");
216 lua_pushfstring(L, "[tag udata(%p) name(%s)]", *p, (*p)->name);
217 return 1;
220 /** Get all tags from a screen.
221 * \param L The Lua VM state.
223 * \luastack
224 * \lparam A screen number.
225 * \lreturn A table with all tags from the screen specified, indexed by tag name.
227 static int
228 luaA_tag_get(lua_State *L)
230 int screen = luaL_checknumber(L, 1) - 1;
231 tag_array_t *tags = &globalconf.screens[screen].tags;
233 luaA_checkscreen(screen);
235 lua_newtable(L);
237 for(int i = 0; i < tags->len; i++)
239 luaA_tag_userdata_new(L, tags->tab[i]);
240 lua_setfield(L, -2, tags->tab[i]->name);
243 return 1;
246 /** Get all tags from a screen.
247 * \param L The Lua VM state.
249 * \luastack
250 * \lparam A screen number.
251 * \lreturn A table with all tags from the screen specified, ordered and indexed
252 * by number.
254 static int
255 luaA_tag_geti(lua_State *L)
257 int screen = luaL_checknumber(L, 1) - 1;
258 tag_array_t *tags = &globalconf.screens[screen].tags;
260 luaA_checkscreen(screen);
262 lua_newtable(L);
264 for(int i = 0; i < tags->len; i++)
266 luaA_tag_userdata_new(L, tags->tab[i]);
267 lua_rawseti(L, -2, i + 1);
270 return 1;
273 /** Create a new tag.
274 * \param L The Lua VM state.
276 * \luastack
277 * \lparam A table with at least a name attribute.
278 * Optional attributes are: mwfact, ncol, nmaster and layout.
279 * \lreturn A new tag object.
281 static int
282 luaA_tag_new(lua_State *L)
284 size_t len;
285 tag_t *tag;
286 int ncol, nmaster;
287 const char *name, *lay;
288 double mwfact;
289 layout_t *layout;
291 luaA_checktable(L, 2);
293 if(!(name = luaA_getopt_string(L, 2, "name", NULL)))
294 luaL_error(L, "object tag must have a name");
296 mwfact = luaA_getopt_number(L, 2, "mwfact", 0.5);
297 ncol = luaA_getopt_number(L, 2, "ncol", 1);
298 nmaster = luaA_getopt_number(L, 2, "nmaster", 1);
299 lay = luaA_getopt_lstring(L, 2, "layout", "tile", &len);
301 layout = name_func_lookup(lay, LayoutList);
303 tag = tag_new(name, len,
304 layout,
305 mwfact, nmaster, ncol);
307 return luaA_tag_userdata_new(L, tag);
310 /** Tag object.
311 * \param L The Lua VM state.
312 * \return The number of elements pushed on stack.
313 * \luastack
314 * \lfield name Tag name.
315 * \lfield screen Screen number of the tag.
316 * \lfield layout Tag layout.
317 * \lfield selected True if the client is selected to be viewed.
318 * \lfield mwfact Master width factor.
319 * \lfield nmaster Number of master windows.
320 * \lfield ncol Number of column for slave windows.
322 static int
323 luaA_tag_index(lua_State *L)
325 size_t len;
326 tag_t **tag = luaA_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 == SCREEN_UNDEF)
341 return 0;
342 lua_pushnumber(L, (*tag)->screen + 1);
343 break;
344 case A_TK_LAYOUT:
345 lua_pushstring(L, name_func_rlookup((*tag)->layout, LayoutList));
346 break;
347 case A_TK_SELECTED:
348 lua_pushboolean(L, (*tag)->selected);
349 break;
350 case A_TK_MWFACT:
351 lua_pushnumber(L, (*tag)->mwfact);
352 break;
353 case A_TK_NMASTER:
354 lua_pushnumber(L, (*tag)->nmaster);
355 break;
356 case A_TK_NCOL:
357 lua_pushnumber(L, (*tag)->ncol);
358 break;
359 default:
360 return 0;
363 return 1;
366 /** Tag newindex.
367 * \param L The Lua VM state.
368 * \return The number of elements pushed on stack.
370 static int
371 luaA_tag_newindex(lua_State *L)
373 size_t len;
374 tag_t **tag = luaA_checkudata(L, 1, "tag");
375 const char *buf, *attr = luaL_checklstring(L, 2, &len);
376 double d;
377 int i;
378 layout_t *l;
380 switch(a_tokenize(attr, len))
382 case A_TK_NAME:
383 buf = luaL_checklstring(L, 3, &len);
384 if((*tag)->screen != SCREEN_UNDEF)
386 tag_array_t *tags = &globalconf.screens[(*tag)->screen].tags;
387 for(i = 0; i < tags->len; i++)
388 if(tags->tab[i] != *tag && !a_strcmp(buf, tags->tab[i]->name))
389 luaL_error(L, "a tag with the name `%s' is already on screen %d",
390 buf, (*tag)->screen);
392 p_delete(&(*tag)->name);
393 a_iso2utf8(&(*tag)->name, buf, len);
394 widget_invalidate_cache((*tag)->screen, WIDGET_CACHE_TAGS);
395 break;
396 case A_TK_SCREEN:
397 if((*tag)->screen == SCREEN_UNDEF)
399 int screen = luaL_checknumber(L, 3) - 1;
401 luaA_checkscreen(screen);
403 tag_array_t *tags = &globalconf.screens[screen].tags;
404 for(i = 0; i < tags->len; i++)
405 if(tags->tab[i] != *tag && !a_strcmp((*tag)->name, tags->tab[i]->name))
406 luaL_error(L, "a tag with the name `%s' is already on screen %d",
407 (*tag)->name, screen);
409 tag_append_to_screen(*tag, screen);
410 return 0;
412 else
413 luaL_error(L, "tag already on screen %d", (*tag)->screen + 1);
414 case A_TK_LAYOUT:
415 buf = luaL_checkstring(L, 3);
416 l = name_func_lookup(buf, LayoutList);
417 if(l)
418 (*tag)->layout = l;
419 else
420 luaL_error(L, "unknown layout: %s", buf);
421 break;
422 case A_TK_SELECTED:
423 tag_view(*tag, luaA_checkboolean(L, 3));
424 return 0;
425 case A_TK_MWFACT:
426 d = luaL_checknumber(L, 3);
427 if(d > 0 && d < 1)
428 (*tag)->mwfact = d;
429 else
430 luaL_error(L, "bad value, must be between 0 and 1");
431 break;
432 case A_TK_NMASTER:
433 i = luaL_checknumber(L, 3);
434 if(i >= 0)
435 (*tag)->nmaster = i;
436 else
437 luaL_error(L, "bad value, must be greater than 0");
438 break;
439 case A_TK_NCOL:
440 i = luaL_checknumber(L, 3);
441 if(i >= 1)
442 (*tag)->ncol = i;
443 else
444 luaL_error(L, "bad value, must be greater than 1");
445 break;
446 default:
447 return 0;
450 if((*tag)->screen != SCREEN_UNDEF)
451 globalconf.screens[(*tag)->screen].need_arrange = true;
453 return 0;
456 const struct luaL_reg awesome_tag_methods[] =
458 { "__call", luaA_tag_new },
459 { "get", luaA_tag_get },
460 { "geti", luaA_tag_geti },
461 { NULL, NULL }
463 const struct luaL_reg awesome_tag_meta[] =
465 { "__index", luaA_tag_index },
466 { "__newindex", luaA_tag_newindex },
467 { "__eq", luaA_tag_eq },
468 { "__gc", luaA_tag_gc },
469 { "__tostring", luaA_tag_tostring },
470 { NULL, NULL },
473 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80