cnode: move struct to cnode header
[awesome.git] / tag.c
blob3d09f6eba297f917499177ddf7f8f3237a3f64ac
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);
96 /* call hook */
97 lua_pushnumber(globalconf.L, s->index+ 1);
98 luaA_dofunction(globalconf.L, globalconf.hooks.tags, 1, 0);
101 /** Remove a tag from screen. Tag must be on a screen and have no clients.
102 * \param tag The tag to remove.
104 static void
105 tag_remove_from_screen(tag_t *tag)
107 int screen = tag->screen;
108 int phys_screen = screen_virttophys(tag->screen);
109 tag_array_t *tags = &globalconf.screens[tag->screen].tags;
111 for(int i = 0; i < tags->len; i++)
112 if(tags->tab[i] == tag)
114 tag_array_take(tags, i);
115 break;
117 ewmh_update_net_numbers_of_desktop(phys_screen);
118 ewmh_update_net_desktop_names(phys_screen);
119 ewmh_update_workarea(phys_screen);
120 tag->screen = SCREEN_UNDEF;
121 tag_unref(&tag);
122 /* call hook */
123 lua_pushnumber(globalconf.L, screen + 1);
124 luaA_dofunction(globalconf.L, globalconf.hooks.tags, 1, 0);
127 /** Tag a client with specified tag.
128 * \param c the client to tag
129 * \param t the tag to tag the client with
131 void
132 tag_client(client_t *c, tag_t *t)
134 /* don't tag twice */
135 if(is_client_tagged(c, t))
136 return;
138 tag_ref(&t);
139 client_array_append(&t->clients, c);
140 client_saveprops_tags(c);
141 client_need_arrange(c);
142 /* call hook */
143 luaA_client_userdata_new(globalconf.L, c);
144 luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 1, 0);
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_need_arrange(c);
158 client_array_take(&t->clients, i);
159 tag_unref(&t);
160 client_saveprops_tags(c);
161 /* call hook */
162 luaA_client_userdata_new(globalconf.L, c);
163 luaA_tag_userdata_new(globalconf.L, t);
164 luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
165 return;
169 /** Check if a client is tagged with the specified tag.
170 * \param c the client
171 * \param t the tag
172 * \return true if the client is tagged with the tag, false otherwise.
174 bool
175 is_client_tagged(client_t *c, tag_t *t)
177 for(int i = 0; i < t->clients.len; i++)
178 if (t->clients.tab[i] == c)
179 return true;
181 return false;
184 /** Get the current tags for the specified screen.
185 * Returned pointer must be p_delete'd after.
186 * \param screen screen id
187 * \return a double pointer of tag list finished with a NULL element
189 tag_t **
190 tags_get_current(int screen)
192 tag_array_t *tags = &globalconf.screens[screen].tags;
193 tag_t **out = p_new(tag_t *, tags->len + 1);
194 int n = 0;
196 for(int i = 0; i < tags->len; i++)
197 if(tags->tab[i]->selected)
198 out[n++] = tags->tab[i];
200 return out;
204 /** Set a tag to be the only one viewed.
205 * \param target the tag to see
207 static void
208 tag_view_only(tag_t *target)
210 if (target)
212 tag_array_t *tags = &globalconf.screens[target->screen].tags;
214 for(int i = 0; i < tags->len; i++)
215 tag_view(tags->tab[i], tags->tab[i] == target);
219 /** View only a tag, selected by its index.
220 * \param screen screen id
221 * \param dindex the index
223 void
224 tag_view_only_byindex(int screen, int dindex)
226 tag_array_t *tags = &globalconf.screens[screen].tags;
228 if(dindex < 0 || dindex >= tags->len)
229 return;
230 tag_view_only(tags->tab[dindex]);
233 /** Convert a tag to a printable string.
234 * \param L The Lua VM state.
236 * \luastack
237 * \lvalue A tag.
238 * \lreturn A string.
240 static int
241 luaA_tag_tostring(lua_State *L)
243 tag_t **p = luaA_checkudata(L, 1, "tag");
244 lua_pushfstring(L, "[tag udata(%p) name(%s)]", *p, (*p)->name);
245 return 1;
248 /** Create a new tag.
249 * \param L The Lua VM state.
251 * \luastack
252 * \lparam A table with at least a name attribute.
253 * Optional attributes are: mwfact, ncol, nmaster and layout.
254 * \lreturn A new tag object.
256 static int
257 luaA_tag_new(lua_State *L)
259 size_t len;
260 tag_t *tag;
261 int ncol, nmaster;
262 const char *name, *lay;
263 double mwfact;
264 layout_t *layout;
266 luaA_checktable(L, 2);
268 if(!(name = luaA_getopt_string(L, 2, "name", NULL)))
269 luaL_error(L, "object tag must have a name");
271 mwfact = luaA_getopt_number(L, 2, "mwfact", 0.5);
272 ncol = luaA_getopt_number(L, 2, "ncol", 1);
273 nmaster = luaA_getopt_number(L, 2, "nmaster", 1);
274 lay = luaA_getopt_lstring(L, 2, "layout", "tile", &len);
276 layout = name_func_lookup(lay, LayoutList);
278 tag = tag_new(name, len,
279 layout,
280 mwfact, nmaster, ncol);
282 return luaA_tag_userdata_new(L, tag);
285 /** Get or set the clients attached to this tag.
286 * \param L The Lua VM state.
287 * \return The number of elements pushed on stack.
288 * \luastack
289 * \lparam None or a table of clients to set.
290 * \lreturn A table with the clients attached to this tags.
292 static int
293 luaA_tag_clients(lua_State *L)
295 tag_t **tag = luaA_checkudata(L, 1, "tag");
296 client_array_t *clients = &(*tag)->clients;
297 int i;
299 if(lua_gettop(L) == 2)
301 client_t **c;
303 luaA_checktable(L, 2);
304 for(i = 0; i < (*tag)->clients.len; i++)
305 untag_client((*tag)->clients.tab[i], *tag);
306 lua_pushnil(L);
307 while(lua_next(L, 2))
309 c = luaA_checkudata(L, -1, "client");
310 tag_client(*c, *tag);
311 lua_pop(L, 1);
315 luaA_otable_new(L);
316 for(i = 0; i < clients->len; i++)
318 luaA_client_userdata_new(L, clients->tab[i]);
319 lua_rawseti(L, -2, i + 1);
322 return 1;
325 /** Tag object.
326 * \param L The Lua VM state.
327 * \return The number of elements pushed on stack.
328 * \luastack
329 * \lfield name Tag name.
330 * \lfield screen Screen number of the tag.
331 * \lfield layout Tag layout.
332 * \lfield selected True if the client is selected to be viewed.
333 * \lfield mwfact Master width factor.
334 * \lfield nmaster Number of master windows.
335 * \lfield ncol Number of column for slave windows.
337 static int
338 luaA_tag_index(lua_State *L)
340 size_t len;
341 tag_t **tag = luaA_checkudata(L, 1, "tag");
342 const char *attr;
344 if(luaA_usemetatable(L, 1, 2))
345 return 1;
347 attr = luaL_checklstring(L, 2, &len);
349 switch(a_tokenize(attr, len))
351 case A_TK_NAME:
352 lua_pushstring(L, (*tag)->name);
353 break;
354 case A_TK_SCREEN:
355 if((*tag)->screen == SCREEN_UNDEF)
356 return 0;
357 lua_pushnumber(L, (*tag)->screen + 1);
358 break;
359 case A_TK_LAYOUT:
360 lua_pushstring(L, name_func_rlookup((*tag)->layout, LayoutList));
361 break;
362 case A_TK_SELECTED:
363 lua_pushboolean(L, (*tag)->selected);
364 break;
365 case A_TK_MWFACT:
366 lua_pushnumber(L, (*tag)->mwfact);
367 break;
368 case A_TK_NMASTER:
369 lua_pushnumber(L, (*tag)->nmaster);
370 break;
371 case A_TK_NCOL:
372 lua_pushnumber(L, (*tag)->ncol);
373 break;
374 default:
375 return 0;
378 return 1;
381 /** Get a screen by its name.
382 * \param screen The screen to look into.
383 * \param name The name.
385 static tag_t *
386 tag_getbyname(int screen, const char *name)
388 int i;
389 tag_array_t *tags = &globalconf.screens[screen].tags;
391 for(i = 0; i < tags->len; i++)
392 if(!a_strcmp(name, tags->tab[i]->name))
393 return tags->tab[i];
394 return NULL;
397 /** Tag newindex.
398 * \param L The Lua VM state.
399 * \return The number of elements pushed on stack.
401 static int
402 luaA_tag_newindex(lua_State *L)
404 size_t len;
405 tag_t **tag = luaA_checkudata(L, 1, "tag");
406 const char *buf, *attr = luaL_checklstring(L, 2, &len);
407 double d;
408 int i, screen;
409 layout_t *l;
411 switch(a_tokenize(attr, len))
413 case A_TK_NAME:
414 buf = luaL_checklstring(L, 3, &len);
415 if((*tag)->screen != SCREEN_UNDEF)
416 if(tag_getbyname((*tag)->screen, (*tag)->name) != *tag)
417 luaL_error(L, "a tag with the name `%s' is already on screen %d",
418 buf, (*tag)->screen);
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 && (*tag)->selected)
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