build remove -f to hostname since some OS don't support it
[awesome.git] / tag.c
blob417257e104f21ffa45400fdb9611b017bb325d0c
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, screen_t *s)
96 int phys_screen = screen_virttophys(s->index);
98 tag->screen = s->index;
99 tag_array_append(&s->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(s->index, 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 tag_ref(&t);
141 client_array_append(&t->clients, c);
142 client_saveprops(c);
143 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
144 globalconf.screens[c->screen].need_arrange = true;
147 /** Untag a client with specified tag.
148 * \param c the client to tag
149 * \param t the tag to tag the client with
151 void
152 untag_client(client_t *c, tag_t *t)
154 for(int i = 0; i < t->clients.len; i++)
155 if(t->clients.tab[i] == c)
157 client_array_take(&t->clients, i);
158 tag_unref(&t);
159 client_saveprops(c);
160 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
161 globalconf.screens[c->screen].need_arrange = true;
162 return;
166 /** Check if a client is tagged with the specified tag.
167 * \param c the client
168 * \param t the tag
169 * \return true if the client is tagged with the tag, false otherwise.
171 bool
172 is_client_tagged(client_t *c, tag_t *t)
174 for(int i = 0; i < t->clients.len; i++)
175 if (t->clients.tab[i] == c)
176 return true;
178 return false;
181 /** Get the current tags for the specified screen.
182 * Returned pointer must be p_delete'd after.
183 * \param screen screen id
184 * \return a double pointer of tag list finished with a NULL element
186 tag_t **
187 tags_get_current(int screen)
189 tag_array_t *tags = &globalconf.screens[screen].tags;
190 tag_t **out = p_new(tag_t *, tags->len + 1);
191 int n = 0;
193 for(int i = 0; i < tags->len; i++)
194 if(tags->tab[i]->selected)
195 out[n++] = tags->tab[i];
197 return out;
201 /** Set a tag to be the only one viewed.
202 * \param target the tag to see
204 static void
205 tag_view_only(tag_t *target)
207 if (target)
209 tag_array_t *tags = &globalconf.screens[target->screen].tags;
211 for(int i = 0; i < tags->len; i++)
212 tag_view(tags->tab[i], tags->tab[i] == target);
216 /** View only a tag, selected by its index.
217 * \param screen screen id
218 * \param dindex the index
220 void
221 tag_view_only_byindex(int screen, int dindex)
223 tag_array_t *tags = &globalconf.screens[screen].tags;
225 if(dindex < 0 || dindex >= tags->len)
226 return;
227 tag_view_only(tags->tab[dindex]);
230 /** Convert a tag to a printable string.
231 * \param L The Lua VM state.
233 * \luastack
234 * \lvalue A tag.
235 * \lreturn A string.
237 static int
238 luaA_tag_tostring(lua_State *L)
240 tag_t **p = luaA_checkudata(L, 1, "tag");
241 lua_pushfstring(L, "[tag udata(%p) name(%s)]", *p, (*p)->name);
242 return 1;
245 /** Create a new tag.
246 * \param L The Lua VM state.
248 * \luastack
249 * \lparam A table with at least a name attribute.
250 * Optional attributes are: mwfact, ncol, nmaster and layout.
251 * \lreturn A new tag object.
253 static int
254 luaA_tag_new(lua_State *L)
256 size_t len;
257 tag_t *tag;
258 int ncol, nmaster;
259 const char *name, *lay;
260 double mwfact;
261 layout_t *layout;
263 luaA_checktable(L, 2);
265 if(!(name = luaA_getopt_string(L, 2, "name", NULL)))
266 luaL_error(L, "object tag must have a name");
268 mwfact = luaA_getopt_number(L, 2, "mwfact", 0.5);
269 ncol = luaA_getopt_number(L, 2, "ncol", 1);
270 nmaster = luaA_getopt_number(L, 2, "nmaster", 1);
271 lay = luaA_getopt_lstring(L, 2, "layout", "tile", &len);
273 layout = name_func_lookup(lay, LayoutList);
275 tag = tag_new(name, len,
276 layout,
277 mwfact, nmaster, ncol);
279 return luaA_tag_userdata_new(L, tag);
282 /** Get or set the clients attached to this tag.
283 * \param L The Lua VM state.
284 * \return The number of elements pushed on stack.
285 * \luastack
286 * \param None or a table of clients to set.
287 * \return A table with the clients attached to this tags.
289 static int
290 luaA_tag_clients(lua_State *L)
292 tag_t **tag = luaA_checkudata(L, 1, "tag");
293 client_array_t *clients = &(*tag)->clients;
294 int i;
296 if(lua_gettop(L) == 2)
298 client_t **c;
300 luaA_checktable(L, 2);
301 for(i = 0; i < (*tag)->clients.len; i++)
302 untag_client((*tag)->clients.tab[i], *tag);
303 lua_pushnil(L);
304 while(lua_next(L, 2))
306 c = luaA_checkudata(L, -1, "client");
307 tag_client(*c, *tag);
308 lua_pop(L, 1);
312 luaA_otable_new(L);
313 for(i = 0; i < clients->len; i++)
315 luaA_client_userdata_new(L, clients->tab[i]);
316 lua_rawseti(L, -2, i + 1);
319 return 1;
322 /** Tag object.
323 * \param L The Lua VM state.
324 * \return The number of elements pushed on stack.
325 * \luastack
326 * \lfield name Tag name.
327 * \lfield screen Screen number of the tag.
328 * \lfield layout Tag layout.
329 * \lfield selected True if the client is selected to be viewed.
330 * \lfield mwfact Master width factor.
331 * \lfield nmaster Number of master windows.
332 * \lfield ncol Number of column for slave windows.
334 static int
335 luaA_tag_index(lua_State *L)
337 size_t len;
338 tag_t **tag = luaA_checkudata(L, 1, "tag");
339 const char *attr;
341 if(luaA_usemetatable(L, 1, 2))
342 return 1;
344 attr = luaL_checklstring(L, 2, &len);
346 switch(a_tokenize(attr, len))
348 case A_TK_NAME:
349 lua_pushstring(L, (*tag)->name);
350 break;
351 case A_TK_SCREEN:
352 if((*tag)->screen == SCREEN_UNDEF)
353 return 0;
354 lua_pushnumber(L, (*tag)->screen + 1);
355 break;
356 case A_TK_LAYOUT:
357 lua_pushstring(L, name_func_rlookup((*tag)->layout, LayoutList));
358 break;
359 case A_TK_SELECTED:
360 lua_pushboolean(L, (*tag)->selected);
361 break;
362 case A_TK_MWFACT:
363 lua_pushnumber(L, (*tag)->mwfact);
364 break;
365 case A_TK_NMASTER:
366 lua_pushnumber(L, (*tag)->nmaster);
367 break;
368 case A_TK_NCOL:
369 lua_pushnumber(L, (*tag)->ncol);
370 break;
371 default:
372 return 0;
375 return 1;
378 /** Get a screen by its name.
379 * \param screen The screen to look into.
380 * \param name The name.
382 static tag_t *
383 tag_getbyname(int screen, const char *name)
385 int i;
386 tag_array_t *tags = &globalconf.screens[screen].tags;
388 for(i = 0; i < tags->len; i++)
389 if(!a_strcmp(name, tags->tab[i]->name))
390 return tags->tab[i];
391 return NULL;
394 /** Tag newindex.
395 * \param L The Lua VM state.
396 * \return The number of elements pushed on stack.
398 static int
399 luaA_tag_newindex(lua_State *L)
401 size_t len;
402 tag_t **tag = luaA_checkudata(L, 1, "tag");
403 const char *buf, *attr = luaL_checklstring(L, 2, &len);
404 double d;
405 int i, screen;
406 layout_t *l;
408 switch(a_tokenize(attr, len))
410 case A_TK_NAME:
411 buf = luaL_checklstring(L, 3, &len);
412 if((*tag)->screen != SCREEN_UNDEF)
414 if(tag_getbyname((*tag)->screen, (*tag)->name) != *tag)
415 luaL_error(L, "a tag with the name `%s' is already on screen %d",
416 buf, (*tag)->screen);
417 widget_invalidate_cache((*tag)->screen, WIDGET_CACHE_TAGS);
419 p_delete(&(*tag)->name);
420 a_iso2utf8(&(*tag)->name, buf, len);
421 break;
422 case A_TK_SCREEN:
423 if(!lua_isnil(L, 3))
425 screen = luaL_checknumber(L, 3) - 1;
426 luaA_checkscreen(screen);
428 else
429 screen = SCREEN_UNDEF;
431 if((*tag)->screen != SCREEN_UNDEF)
432 tag_remove_from_screen(*tag);
434 if(screen != SCREEN_UNDEF)
436 if(tag_getbyname(screen, (*tag)->name))
437 luaL_error(L, "a tag with the name `%s' is already on screen %d",
438 (*tag)->name, screen);
440 tag_append_to_screen(*tag, &globalconf.screens[screen]);
442 break;
443 case A_TK_LAYOUT:
444 buf = luaL_checkstring(L, 3);
445 l = name_func_lookup(buf, LayoutList);
446 if(l)
447 (*tag)->layout = l;
448 else
449 luaL_error(L, "unknown layout: %s", buf);
450 break;
451 case A_TK_SELECTED:
452 if((*tag)->screen != SCREEN_UNDEF)
453 tag_view(*tag, luaA_checkboolean(L, 3));
454 return 0;
455 case A_TK_MWFACT:
456 d = luaL_checknumber(L, 3);
457 if(d > 0 && d < 1)
458 (*tag)->mwfact = d;
459 else
460 luaL_error(L, "bad value, must be between 0 and 1");
461 break;
462 case A_TK_NMASTER:
463 i = luaL_checknumber(L, 3);
464 if(i >= 0)
465 (*tag)->nmaster = i;
466 else
467 luaL_error(L, "bad value, must be greater than 0");
468 break;
469 case A_TK_NCOL:
470 i = luaL_checknumber(L, 3);
471 if(i >= 1)
472 (*tag)->ncol = i;
473 else
474 luaL_error(L, "bad value, must be greater than 1");
475 break;
476 default:
477 return 0;
480 if((*tag)->screen != SCREEN_UNDEF)
481 globalconf.screens[(*tag)->screen].need_arrange = true;
483 return 0;
486 const struct luaL_reg awesome_tag_methods[] =
488 { "__call", luaA_tag_new },
489 { NULL, NULL }
491 const struct luaL_reg awesome_tag_meta[] =
493 { "clients", luaA_tag_clients },
494 { "__index", luaA_tag_index },
495 { "__newindex", luaA_tag_newindex },
496 { "__eq", luaA_tag_eq },
497 { "__gc", luaA_tag_gc },
498 { "__tostring", luaA_tag_tostring },
499 { NULL, NULL },
502 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80