awful.prompt: return true since we unregistered (FS#383)
[awesome.git] / tag.c
blob0399a78a4e5f54511fdc10d824dd63aad2f4b46f
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 if(globalconf.hooks.tags != LUA_REFNIL)
105 lua_pushnumber(globalconf.L, s->index + 1);
106 luaA_dofunction(globalconf.L, globalconf.hooks.tags, 1, 0);
110 /** Remove a tag from screen. Tag must be on a screen and have no clients.
111 * \param tag The tag to remove.
113 static void
114 tag_remove_from_screen(tag_t *tag)
116 int screen = tag->screen;
117 int phys_screen = screen_virttophys(tag->screen);
118 tag_array_t *tags = &globalconf.screens[tag->screen].tags;
120 for(int i = 0; i < tags->len; i++)
121 if(tags->tab[i] == tag)
123 tag_array_take(tags, i);
124 break;
126 ewmh_update_net_numbers_of_desktop(phys_screen);
127 ewmh_update_net_desktop_names(phys_screen);
128 ewmh_update_workarea(phys_screen);
129 tag->screen = SCREEN_UNDEF;
130 tag_unref(&tag);
132 /* resave tag prop of all clients so the number of tag will be the
133 * same */
134 for(client_t *c = globalconf.clients; c; c = c->next)
135 client_saveprops_tags(c);
137 /* call hook */
138 if(globalconf.hooks.tags != LUA_REFNIL)
140 lua_pushnumber(globalconf.L, screen + 1);
141 luaA_dofunction(globalconf.L, globalconf.hooks.tags, 1, 0);
145 /** Tag a client with specified tag.
146 * \param c the client to tag
147 * \param t the tag to tag the client with
149 void
150 tag_client(client_t *c, tag_t *t)
152 /* don't tag twice */
153 if(is_client_tagged(c, t))
154 return;
156 tag_ref(&t);
157 client_array_append(&t->clients, c);
158 client_saveprops_tags(c);
159 client_need_arrange(c);
160 /* call hook */
161 if(globalconf.hooks.tagged != LUA_REFNIL)
163 luaA_client_userdata_new(globalconf.L, c);
164 luaA_tag_userdata_new(globalconf.L, t);
165 luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
169 /** Untag a client with specified tag.
170 * \param c the client to tag
171 * \param t the tag to tag the client with
173 void
174 untag_client(client_t *c, tag_t *t)
176 for(int i = 0; i < t->clients.len; i++)
177 if(t->clients.tab[i] == c)
179 client_need_arrange(c);
180 client_array_take(&t->clients, i);
181 client_saveprops_tags(c);
182 /* call hook */
183 if(globalconf.hooks.tagged != LUA_REFNIL)
185 luaA_client_userdata_new(globalconf.L, c);
186 luaA_tag_userdata_new(globalconf.L, t);
187 luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
189 tag_unref(&t);
190 return;
194 /** Check if a client is tagged with the specified tag.
195 * \param c the client
196 * \param t the tag
197 * \return true if the client is tagged with the tag, false otherwise.
199 bool
200 is_client_tagged(client_t *c, tag_t *t)
202 for(int i = 0; i < t->clients.len; i++)
203 if (t->clients.tab[i] == c)
204 return true;
206 return false;
209 /** Get the current tags for the specified screen.
210 * Returned pointer must be p_delete'd after.
211 * \param screen screen id
212 * \return a double pointer of tag list finished with a NULL element
214 tag_t **
215 tags_get_current(int screen)
217 tag_array_t *tags = &globalconf.screens[screen].tags;
218 tag_t **out = p_new(tag_t *, tags->len + 1);
219 int n = 0;
221 for(int i = 0; i < tags->len; i++)
222 if(tags->tab[i]->selected)
223 out[n++] = tags->tab[i];
225 return out;
229 /** Set a tag to be the only one viewed.
230 * \param target the tag to see
232 static void
233 tag_view_only(tag_t *target)
235 if (target)
237 tag_array_t *tags = &globalconf.screens[target->screen].tags;
239 for(int i = 0; i < tags->len; i++)
240 tag_view(tags->tab[i], tags->tab[i] == target);
244 /** View only a tag, selected by its index.
245 * \param screen screen id
246 * \param dindex the index
248 void
249 tag_view_only_byindex(int screen, int dindex)
251 tag_array_t *tags = &globalconf.screens[screen].tags;
253 if(dindex < 0 || dindex >= tags->len)
254 return;
255 tag_view_only(tags->tab[dindex]);
258 /** Create a new tag.
259 * \param L The Lua VM state.
261 * \luastack
262 * \lparam A table with at least a name attribute.
263 * Optional attributes are: mwfact, ncol, nmaster and layout.
264 * \lreturn A new tag object.
266 static int
267 luaA_tag_new(lua_State *L)
269 size_t len;
270 tag_t *tag;
271 int ncol, nmaster;
272 const char *name, *lay;
273 double mwfact;
274 layout_t *layout;
276 luaA_checktable(L, 2);
278 if(!(name = luaA_getopt_lstring(L, 2, "name", NULL, &len)))
279 luaL_error(L, "object tag must have a name");
281 mwfact = luaA_getopt_number(L, 2, "mwfact", 0.5);
282 ncol = luaA_getopt_number(L, 2, "ncol", 1);
283 nmaster = luaA_getopt_number(L, 2, "nmaster", 1);
284 lay = luaA_getopt_string(L, 2, "layout", "tile");
286 layout = name_func_lookup(lay, LayoutList);
288 tag = tag_new(name, len,
289 layout,
290 mwfact, nmaster, ncol);
292 return luaA_tag_userdata_new(L, tag);
295 /** Get or set the clients attached to this tag.
296 * \param L The Lua VM state.
297 * \return The number of elements pushed on stack.
298 * \luastack
299 * \lparam None or a table of clients to set.
300 * \lreturn A table with the clients attached to this tags.
302 static int
303 luaA_tag_clients(lua_State *L)
305 tag_t **tag = luaA_checkudata(L, 1, "tag");
306 client_array_t *clients = &(*tag)->clients;
307 int i;
309 if(lua_gettop(L) == 2)
311 client_t **c;
313 luaA_checktable(L, 2);
314 for(i = 0; i < (*tag)->clients.len; i++)
315 untag_client((*tag)->clients.tab[i], *tag);
316 lua_pushnil(L);
317 while(lua_next(L, 2))
319 c = luaA_checkudata(L, -1, "client");
320 tag_client(*c, *tag);
321 lua_pop(L, 1);
325 luaA_otable_new(L);
326 for(i = 0; i < clients->len; i++)
328 luaA_client_userdata_new(L, clients->tab[i]);
329 lua_rawseti(L, -2, i + 1);
332 return 1;
335 /** Tag object.
336 * \param L The Lua VM state.
337 * \return The number of elements pushed on stack.
338 * \luastack
339 * \lfield name Tag name.
340 * \lfield screen Screen number of the tag.
341 * \lfield layout Tag layout.
342 * \lfield selected True if the client is selected to be viewed.
343 * \lfield mwfact Master width factor.
344 * \lfield nmaster Number of master windows.
345 * \lfield ncol Number of column for slave windows.
347 static int
348 luaA_tag_index(lua_State *L)
350 size_t len;
351 tag_t **tag = luaA_checkudata(L, 1, "tag");
352 const char *attr;
354 if(luaA_usemetatable(L, 1, 2))
355 return 1;
357 attr = luaL_checklstring(L, 2, &len);
359 switch(a_tokenize(attr, len))
361 case A_TK_NAME:
362 lua_pushstring(L, (*tag)->name);
363 break;
364 case A_TK_SCREEN:
365 if((*tag)->screen == SCREEN_UNDEF)
366 return 0;
367 lua_pushnumber(L, (*tag)->screen + 1);
368 break;
369 case A_TK_LAYOUT:
370 lua_pushstring(L, name_func_rlookup((*tag)->layout, LayoutList));
371 break;
372 case A_TK_SELECTED:
373 lua_pushboolean(L, (*tag)->selected);
374 break;
375 case A_TK_MWFACT:
376 lua_pushnumber(L, (*tag)->mwfact);
377 break;
378 case A_TK_NMASTER:
379 lua_pushnumber(L, (*tag)->nmaster);
380 break;
381 case A_TK_NCOL:
382 lua_pushnumber(L, (*tag)->ncol);
383 break;
384 default:
385 return 0;
388 return 1;
391 /** Tag newindex.
392 * \param L The Lua VM state.
393 * \return The number of elements pushed on stack.
395 static int
396 luaA_tag_newindex(lua_State *L)
398 size_t len;
399 tag_t **tag = luaA_checkudata(L, 1, "tag");
400 const char *buf, *attr = luaL_checklstring(L, 2, &len);
401 double d;
402 int i, screen;
403 layout_t *l;
405 switch(a_tokenize(attr, len))
407 case A_TK_NAME:
408 buf = luaL_checklstring(L, 3, &len);
409 p_delete(&(*tag)->name);
410 a_iso2utf8(&(*tag)->name, buf, len);
411 break;
412 case A_TK_SCREEN:
413 if(!lua_isnil(L, 3))
415 screen = luaL_checknumber(L, 3) - 1;
416 luaA_checkscreen(screen);
418 else
419 screen = SCREEN_UNDEF;
421 if((*tag)->screen != SCREEN_UNDEF)
422 tag_remove_from_screen(*tag);
424 if(screen != SCREEN_UNDEF)
425 tag_append_to_screen(*tag, &globalconf.screens[screen]);
426 break;
427 case A_TK_LAYOUT:
428 buf = luaL_checkstring(L, 3);
429 l = name_func_lookup(buf, LayoutList);
430 if(l)
431 (*tag)->layout = l;
432 else
434 luaA_warn(L, "unknown layout: %s", buf);
435 return 0;
437 break;
438 case A_TK_SELECTED:
439 if((*tag)->screen != SCREEN_UNDEF)
440 tag_view(*tag, luaA_checkboolean(L, 3));
441 return 0;
442 case A_TK_MWFACT:
443 d = luaL_checknumber(L, 3);
444 if(d > 0 && d < 1)
445 (*tag)->mwfact = d;
446 else
448 luaA_warn(L, "bad value, must be between 0 and 1");
449 return 0;
451 break;
452 case A_TK_NMASTER:
453 i = luaL_checknumber(L, 3);
454 if(i >= 0)
455 (*tag)->nmaster = i;
456 else
458 luaA_warn(L, "bad value, must be greater than 0");
459 return 0;
461 break;
462 case A_TK_NCOL:
463 i = luaL_checknumber(L, 3);
464 if(i >= 1)
465 (*tag)->ncol = i;
466 else
468 luaA_warn(L, "bad value, must be greater than 1");
469 return 0;
471 break;
472 default:
473 return 0;
476 if((*tag)->screen != SCREEN_UNDEF && (*tag)->selected)
477 globalconf.screens[(*tag)->screen].need_arrange = true;
479 return 0;
482 const struct luaL_reg awesome_tag_methods[] =
484 { "__call", luaA_tag_new },
485 { NULL, NULL }
487 const struct luaL_reg awesome_tag_meta[] =
489 { "clients", luaA_tag_clients },
490 { "__index", luaA_tag_index },
491 { "__newindex", luaA_tag_newindex },
492 { "__eq", luaA_tag_eq },
493 { "__gc", luaA_tag_gc },
494 { "__tostring", luaA_tag_tostring },
495 { NULL, NULL },
498 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80