awesomerc.5: update
[awesome.git] / tag.c
blob35904841987e096ab677a46a28ec853a5643659a
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 lua_pushnumber(globalconf.L, s->index + 1);
104 luaA_dofunction(globalconf.L, globalconf.hooks.tags, 1, 0);
107 /** Remove a tag from screen. Tag must be on a screen and have no clients.
108 * \param tag The tag to remove.
110 static void
111 tag_remove_from_screen(tag_t *tag)
113 int screen = tag->screen;
114 int phys_screen = screen_virttophys(tag->screen);
115 tag_array_t *tags = &globalconf.screens[tag->screen].tags;
117 for(int i = 0; i < tags->len; i++)
118 if(tags->tab[i] == tag)
120 tag_array_take(tags, i);
121 break;
123 ewmh_update_net_numbers_of_desktop(phys_screen);
124 ewmh_update_net_desktop_names(phys_screen);
125 ewmh_update_workarea(phys_screen);
126 tag->screen = SCREEN_UNDEF;
127 tag_unref(&tag);
129 /* resave tag prop of all clients so the number of tag will be the
130 * same */
131 for(client_t *c = globalconf.clients; c; c = c->next)
132 client_saveprops_tags(c);
134 /* call hook */
135 lua_pushnumber(globalconf.L, screen + 1);
136 luaA_dofunction(globalconf.L, globalconf.hooks.tags, 1, 0);
139 /** Tag a client with specified tag.
140 * \param c the client to tag
141 * \param t the tag to tag the client with
143 void
144 tag_client(client_t *c, tag_t *t)
146 /* don't tag twice */
147 if(is_client_tagged(c, t))
148 return;
150 tag_ref(&t);
151 client_array_append(&t->clients, c);
152 client_saveprops_tags(c);
153 client_need_arrange(c);
154 /* call hook */
155 luaA_client_userdata_new(globalconf.L, c);
156 luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 1, 0);
159 /** Untag a client with specified tag.
160 * \param c the client to tag
161 * \param t the tag to tag the client with
163 void
164 untag_client(client_t *c, tag_t *t)
166 for(int i = 0; i < t->clients.len; i++)
167 if(t->clients.tab[i] == c)
169 client_need_arrange(c);
170 client_array_take(&t->clients, i);
171 tag_unref(&t);
172 client_saveprops_tags(c);
173 /* call hook */
174 luaA_client_userdata_new(globalconf.L, c);
175 luaA_tag_userdata_new(globalconf.L, t);
176 luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
177 return;
181 /** Check if a client is tagged with the specified tag.
182 * \param c the client
183 * \param t the tag
184 * \return true if the client is tagged with the tag, false otherwise.
186 bool
187 is_client_tagged(client_t *c, tag_t *t)
189 for(int i = 0; i < t->clients.len; i++)
190 if (t->clients.tab[i] == c)
191 return true;
193 return false;
196 /** Get the current tags for the specified screen.
197 * Returned pointer must be p_delete'd after.
198 * \param screen screen id
199 * \return a double pointer of tag list finished with a NULL element
201 tag_t **
202 tags_get_current(int screen)
204 tag_array_t *tags = &globalconf.screens[screen].tags;
205 tag_t **out = p_new(tag_t *, tags->len + 1);
206 int n = 0;
208 for(int i = 0; i < tags->len; i++)
209 if(tags->tab[i]->selected)
210 out[n++] = tags->tab[i];
212 return out;
216 /** Set a tag to be the only one viewed.
217 * \param target the tag to see
219 static void
220 tag_view_only(tag_t *target)
222 if (target)
224 tag_array_t *tags = &globalconf.screens[target->screen].tags;
226 for(int i = 0; i < tags->len; i++)
227 tag_view(tags->tab[i], tags->tab[i] == target);
231 /** View only a tag, selected by its index.
232 * \param screen screen id
233 * \param dindex the index
235 void
236 tag_view_only_byindex(int screen, int dindex)
238 tag_array_t *tags = &globalconf.screens[screen].tags;
240 if(dindex < 0 || dindex >= tags->len)
241 return;
242 tag_view_only(tags->tab[dindex]);
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_lstring(L, 2, "name", NULL, &len)))
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_string(L, 2, "layout", "tile");
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 * \lparam None or a table of clients to set.
287 * \lreturn 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 /** Tag newindex.
379 * \param L The Lua VM state.
380 * \return The number of elements pushed on stack.
382 static int
383 luaA_tag_newindex(lua_State *L)
385 size_t len;
386 tag_t **tag = luaA_checkudata(L, 1, "tag");
387 const char *buf, *attr = luaL_checklstring(L, 2, &len);
388 double d;
389 int i, screen;
390 layout_t *l;
392 switch(a_tokenize(attr, len))
394 case A_TK_NAME:
395 buf = luaL_checklstring(L, 3, &len);
396 p_delete(&(*tag)->name);
397 a_iso2utf8(&(*tag)->name, buf, len);
398 break;
399 case A_TK_SCREEN:
400 if(!lua_isnil(L, 3))
402 screen = luaL_checknumber(L, 3) - 1;
403 luaA_checkscreen(screen);
405 else
406 screen = SCREEN_UNDEF;
408 if((*tag)->screen != SCREEN_UNDEF)
409 tag_remove_from_screen(*tag);
411 if(screen != SCREEN_UNDEF)
412 tag_append_to_screen(*tag, &globalconf.screens[screen]);
413 break;
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
421 luaA_warn(L, "unknown layout: %s", buf);
422 return 0;
424 break;
425 case A_TK_SELECTED:
426 if((*tag)->screen != SCREEN_UNDEF)
427 tag_view(*tag, luaA_checkboolean(L, 3));
428 return 0;
429 case A_TK_MWFACT:
430 d = luaL_checknumber(L, 3);
431 if(d > 0 && d < 1)
432 (*tag)->mwfact = d;
433 else
435 luaA_warn(L, "bad value, must be between 0 and 1");
436 return 0;
438 break;
439 case A_TK_NMASTER:
440 i = luaL_checknumber(L, 3);
441 if(i >= 0)
442 (*tag)->nmaster = i;
443 else
445 luaA_warn(L, "bad value, must be greater than 0");
446 return 0;
448 break;
449 case A_TK_NCOL:
450 i = luaL_checknumber(L, 3);
451 if(i >= 1)
452 (*tag)->ncol = i;
453 else
455 luaA_warn(L, "bad value, must be greater than 1");
456 return 0;
458 break;
459 default:
460 return 0;
463 if((*tag)->screen != SCREEN_UNDEF && (*tag)->selected)
464 globalconf.screens[(*tag)->screen].need_arrange = true;
466 return 0;
469 const struct luaL_reg awesome_tag_methods[] =
471 { "__call", luaA_tag_new },
472 { NULL, NULL }
474 const struct luaL_reg awesome_tag_meta[] =
476 { "clients", luaA_tag_clients },
477 { "__index", luaA_tag_index },
478 { "__newindex", luaA_tag_newindex },
479 { "__eq", luaA_tag_eq },
480 { "__gc", luaA_tag_gc },
481 { "__tostring", luaA_tag_tostring },
482 { NULL, NULL },
485 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80