textbox: allow nil as value for text
[awesome.git] / tag.c
blob2f20c6ac833977c6d1bc9bd0182b853a4ae11df7
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 /** Create a new tag.
244 * \param L The Lua VM state.
246 * \luastack
247 * \lparam A table with at least a name attribute.
248 * Optional attributes are: mwfact, ncol, nmaster and layout.
249 * \lreturn A new tag object.
251 static int
252 luaA_tag_new(lua_State *L)
254 size_t len;
255 tag_t *tag;
256 int ncol, nmaster;
257 const char *name, *lay;
258 double mwfact;
259 layout_t *layout;
261 luaA_checktable(L, 2);
263 if(!(name = luaA_getopt_string(L, 2, "name", NULL)))
264 luaL_error(L, "object tag must have a name");
266 mwfact = luaA_getopt_number(L, 2, "mwfact", 0.5);
267 ncol = luaA_getopt_number(L, 2, "ncol", 1);
268 nmaster = luaA_getopt_number(L, 2, "nmaster", 1);
269 lay = luaA_getopt_lstring(L, 2, "layout", "tile", &len);
271 layout = name_func_lookup(lay, LayoutList);
273 tag = tag_new(name, len,
274 layout,
275 mwfact, nmaster, ncol);
277 return luaA_tag_userdata_new(L, tag);
280 /** Tag object.
281 * \param L The Lua VM state.
282 * \return The number of elements pushed on stack.
283 * \luastack
284 * \lfield name Tag name.
285 * \lfield screen Screen number of the tag.
286 * \lfield layout Tag layout.
287 * \lfield selected True if the client is selected to be viewed.
288 * \lfield mwfact Master width factor.
289 * \lfield nmaster Number of master windows.
290 * \lfield ncol Number of column for slave windows.
291 * \lfield clients The clients that has this tag set.
293 static int
294 luaA_tag_index(lua_State *L)
296 size_t len;
297 tag_t **tag = luaA_checkudata(L, 1, "tag");
298 const char *attr;
299 client_array_t *clients;
300 int i;
302 if(luaA_usemetatable(L, 1, 2))
303 return 1;
305 attr = luaL_checklstring(L, 2, &len);
307 switch(a_tokenize(attr, len))
309 case A_TK_NAME:
310 lua_pushstring(L, (*tag)->name);
311 break;
312 case A_TK_SCREEN:
313 if((*tag)->screen == SCREEN_UNDEF)
314 return 0;
315 lua_pushnumber(L, (*tag)->screen + 1);
316 break;
317 case A_TK_LAYOUT:
318 lua_pushstring(L, name_func_rlookup((*tag)->layout, LayoutList));
319 break;
320 case A_TK_SELECTED:
321 lua_pushboolean(L, (*tag)->selected);
322 break;
323 case A_TK_MWFACT:
324 lua_pushnumber(L, (*tag)->mwfact);
325 break;
326 case A_TK_NMASTER:
327 lua_pushnumber(L, (*tag)->nmaster);
328 break;
329 case A_TK_NCOL:
330 lua_pushnumber(L, (*tag)->ncol);
331 break;
332 case A_TK_CLIENTS:
333 clients = &(*tag)->clients;
334 luaA_otable_new(L);
335 for(i = 0; i < clients->len; i++)
337 luaA_client_userdata_new(L, clients->tab[i]);
338 luaA_client_userdata_new(L, clients->tab[i]);
339 lua_rawset(L, -3);
341 break;
342 default:
343 return 0;
346 return 1;
349 /** Get a screen by its name.
350 * \param screen The screen to look into.
351 * \param name The name.
353 static tag_t *
354 tag_getbyname(int screen, const char *name)
356 int i;
357 tag_array_t *tags = &globalconf.screens[screen].tags;
359 for(i = 0; i < tags->len; i++)
360 if(!a_strcmp(name, tags->tab[i]->name))
361 return tags->tab[i];
362 return NULL;
365 /** Tag newindex.
366 * \param L The Lua VM state.
367 * \return The number of elements pushed on stack.
369 static int
370 luaA_tag_newindex(lua_State *L)
372 size_t len;
373 tag_t **tag = luaA_checkudata(L, 1, "tag");
374 const char *buf, *attr = luaL_checklstring(L, 2, &len);
375 double d;
376 int i, screen;
377 layout_t *l;
378 client_t **c;
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 if(tag_getbyname((*tag)->screen, (*tag)->name) != *tag)
387 luaL_error(L, "a tag with the name `%s' is already on screen %d",
388 buf, (*tag)->screen);
389 widget_invalidate_cache((*tag)->screen, WIDGET_CACHE_TAGS);
391 p_delete(&(*tag)->name);
392 a_iso2utf8(&(*tag)->name, buf, len);
393 break;
394 case A_TK_SCREEN:
395 if(!lua_isnil(L, 3))
397 screen = luaL_checknumber(L, 3) - 1;
398 luaA_checkscreen(screen);
400 else
401 screen = SCREEN_UNDEF;
403 if((*tag)->screen != SCREEN_UNDEF)
405 if((*tag)->clients.len)
406 luaL_error(L, "unable to remove tag %s from screen %d, it still has clients",
407 (*tag)->name, (*tag)->screen);
408 else
409 tag_remove_from_screen(*tag);
412 if(screen != SCREEN_UNDEF)
414 if(tag_getbyname(screen, (*tag)->name))
415 luaL_error(L, "a tag with the name `%s' is already on screen %d",
416 (*tag)->name, screen);
418 tag_append_to_screen(*tag, screen);
420 break;
421 case A_TK_LAYOUT:
422 buf = luaL_checkstring(L, 3);
423 l = name_func_lookup(buf, LayoutList);
424 if(l)
425 (*tag)->layout = l;
426 else
427 luaL_error(L, "unknown layout: %s", buf);
428 break;
429 case A_TK_SELECTED:
430 if((*tag)->screen != SCREEN_UNDEF)
431 tag_view(*tag, luaA_checkboolean(L, 3));
432 return 0;
433 case A_TK_MWFACT:
434 d = luaL_checknumber(L, 3);
435 if(d > 0 && d < 1)
436 (*tag)->mwfact = d;
437 else
438 luaL_error(L, "bad value, must be between 0 and 1");
439 break;
440 case A_TK_NMASTER:
441 i = luaL_checknumber(L, 3);
442 if(i >= 0)
443 (*tag)->nmaster = i;
444 else
445 luaL_error(L, "bad value, must be greater than 0");
446 break;
447 case A_TK_NCOL:
448 i = luaL_checknumber(L, 3);
449 if(i >= 1)
450 (*tag)->ncol = i;
451 else
452 luaL_error(L, "bad value, must be greater than 1");
453 break;
454 case A_TK_CLIENTS:
455 luaA_checktable(L, 3);
456 for(i = 0; i < (*tag)->clients.len; i++)
457 untag_client((*tag)->clients.tab[i], *tag);
458 lua_pushnil(L);
459 while(lua_next(L, 3))
461 c = luaA_checkudata(L, -1, "client");
462 tag_client(*c, *tag);
463 lua_pop(L, 1);
465 break;
466 default:
467 return 0;
470 if((*tag)->screen != SCREEN_UNDEF)
471 globalconf.screens[(*tag)->screen].need_arrange = true;
473 return 0;
476 const struct luaL_reg awesome_tag_methods[] =
478 { "__call", luaA_tag_new },
479 { NULL, NULL }
481 const struct luaL_reg awesome_tag_meta[] =
483 { "__index", luaA_tag_index },
484 { "__newindex", luaA_tag_newindex },
485 { "__eq", luaA_tag_eq },
486 { "__gc", luaA_tag_gc },
487 { "__tostring", luaA_tag_tostring },
488 { NULL, NULL },
491 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80