draw: allow text to be on multiple lines
[awesome.git] / tag.c
blobae45cebb732dcedd4b47b4a0e90506b043395a08
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 widget_invalidate_cache(tag->screen, WIDGET_CACHE_TAGS);
46 globalconf.screens[tag->screen].need_arrange = true;
49 /** Create a new tag. Parameters values are checked.
50 * \param name Tag name.
51 * \param len Tag name length.
52 * \param layout Layout to use.
53 * \param mwfact Master width factor.
54 * \param nmaster Number of master windows.
55 * \param ncol Number of columns for slaves windows.
56 * \return A new tag with all these parameters.
58 tag_t *
59 tag_new(const char *name, ssize_t len, layout_t *layout, double mwfact, int nmaster, int ncol)
61 tag_t *tag;
63 tag = p_new(tag_t, 1);
64 a_iso2utf8(&tag->name, name, len);
65 tag->layout = layout;
67 /* to avoid error */
68 tag->screen = SCREEN_UNDEF;
70 tag->mwfact = mwfact;
71 if(tag->mwfact <= 0 || tag->mwfact >= 1)
72 tag->mwfact = 0.5;
74 if((tag->nmaster = nmaster) < 0)
75 tag->nmaster = 1;
77 if((tag->ncol = ncol) < 1)
78 tag->ncol = 1;
80 return tag;
83 /** Append a tag to a screen.
84 * \param tag the tag to append
85 * \param screen the screen id
87 void
88 tag_append_to_screen(tag_t *tag, screen_t *s)
90 int phys_screen = screen_virttophys(s->index);
92 tag->screen = s->index;
93 tag_array_append(&s->tags, tag_ref(&tag));
94 ewmh_update_net_numbers_of_desktop(phys_screen);
95 ewmh_update_net_desktop_names(phys_screen);
96 ewmh_update_workarea(phys_screen);
97 widget_invalidate_cache(s->index, WIDGET_CACHE_TAGS);
100 /** Remove a tag from screen. Tag must be on a screen and have no clients.
101 * \param tag The tag to remove.
103 static void
104 tag_remove_from_screen(tag_t *tag)
106 int phys_screen = screen_virttophys(tag->screen);
107 tag_array_t *tags = &globalconf.screens[tag->screen].tags;
109 for(int i = 0; i < tags->len; i++)
110 if(tags->tab[i] == tag)
112 tag_array_take(tags, i);
113 break;
115 ewmh_update_net_numbers_of_desktop(phys_screen);
116 ewmh_update_net_desktop_names(phys_screen);
117 ewmh_update_workarea(phys_screen);
118 widget_invalidate_cache(tag->screen, WIDGET_CACHE_TAGS);
119 tag->screen = SCREEN_UNDEF;
120 tag_unref(&tag);
123 /** Tag a client with specified tag.
124 * \param c the client to tag
125 * \param t the tag to tag the client with
127 void
128 tag_client(client_t *c, tag_t *t)
130 /* don't tag twice */
131 if(is_client_tagged(c, t))
132 return;
134 tag_ref(&t);
135 client_array_append(&t->clients, c);
136 client_saveprops_tags(c);
137 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
138 client_need_arrange(c);
141 /** Untag a client with specified tag.
142 * \param c the client to tag
143 * \param t the tag to tag the client with
145 void
146 untag_client(client_t *c, tag_t *t)
148 for(int i = 0; i < t->clients.len; i++)
149 if(t->clients.tab[i] == c)
151 client_need_arrange(c);
152 client_array_take(&t->clients, i);
153 tag_unref(&t);
154 client_saveprops_tags(c);
155 widget_invalidate_cache(c->screen, WIDGET_CACHE_CLIENTS);
156 return;
160 /** Check if a client is tagged with the specified tag.
161 * \param c the client
162 * \param t the tag
163 * \return true if the client is tagged with the tag, false otherwise.
165 bool
166 is_client_tagged(client_t *c, tag_t *t)
168 for(int i = 0; i < t->clients.len; i++)
169 if (t->clients.tab[i] == c)
170 return true;
172 return false;
175 /** Get the current tags for the specified screen.
176 * Returned pointer must be p_delete'd after.
177 * \param screen screen id
178 * \return a double pointer of tag list finished with a NULL element
180 tag_t **
181 tags_get_current(int screen)
183 tag_array_t *tags = &globalconf.screens[screen].tags;
184 tag_t **out = p_new(tag_t *, tags->len + 1);
185 int n = 0;
187 for(int i = 0; i < tags->len; i++)
188 if(tags->tab[i]->selected)
189 out[n++] = tags->tab[i];
191 return out;
195 /** Set a tag to be the only one viewed.
196 * \param target the tag to see
198 static void
199 tag_view_only(tag_t *target)
201 if (target)
203 tag_array_t *tags = &globalconf.screens[target->screen].tags;
205 for(int i = 0; i < tags->len; i++)
206 tag_view(tags->tab[i], tags->tab[i] == target);
210 /** View only a tag, selected by its index.
211 * \param screen screen id
212 * \param dindex the index
214 void
215 tag_view_only_byindex(int screen, int dindex)
217 tag_array_t *tags = &globalconf.screens[screen].tags;
219 if(dindex < 0 || dindex >= tags->len)
220 return;
221 tag_view_only(tags->tab[dindex]);
224 /** Convert a tag to a printable string.
225 * \param L The Lua VM state.
227 * \luastack
228 * \lvalue A tag.
229 * \lreturn A string.
231 static int
232 luaA_tag_tostring(lua_State *L)
234 tag_t **p = luaA_checkudata(L, 1, "tag");
235 lua_pushfstring(L, "[tag udata(%p) name(%s)]", *p, (*p)->name);
236 return 1;
239 /** Create a new tag.
240 * \param L The Lua VM state.
242 * \luastack
243 * \lparam A table with at least a name attribute.
244 * Optional attributes are: mwfact, ncol, nmaster and layout.
245 * \lreturn A new tag object.
247 static int
248 luaA_tag_new(lua_State *L)
250 size_t len;
251 tag_t *tag;
252 int ncol, nmaster;
253 const char *name, *lay;
254 double mwfact;
255 layout_t *layout;
257 luaA_checktable(L, 2);
259 if(!(name = luaA_getopt_string(L, 2, "name", NULL)))
260 luaL_error(L, "object tag must have a name");
262 mwfact = luaA_getopt_number(L, 2, "mwfact", 0.5);
263 ncol = luaA_getopt_number(L, 2, "ncol", 1);
264 nmaster = luaA_getopt_number(L, 2, "nmaster", 1);
265 lay = luaA_getopt_lstring(L, 2, "layout", "tile", &len);
267 layout = name_func_lookup(lay, LayoutList);
269 tag = tag_new(name, len,
270 layout,
271 mwfact, nmaster, ncol);
273 return luaA_tag_userdata_new(L, tag);
276 /** Get or set the clients attached to this tag.
277 * \param L The Lua VM state.
278 * \return The number of elements pushed on stack.
279 * \luastack
280 * \lparam None or a table of clients to set.
281 * \lreturn A table with the clients attached to this tags.
283 static int
284 luaA_tag_clients(lua_State *L)
286 tag_t **tag = luaA_checkudata(L, 1, "tag");
287 client_array_t *clients = &(*tag)->clients;
288 int i;
290 if(lua_gettop(L) == 2)
292 client_t **c;
294 luaA_checktable(L, 2);
295 for(i = 0; i < (*tag)->clients.len; i++)
296 untag_client((*tag)->clients.tab[i], *tag);
297 lua_pushnil(L);
298 while(lua_next(L, 2))
300 c = luaA_checkudata(L, -1, "client");
301 tag_client(*c, *tag);
302 lua_pop(L, 1);
306 luaA_otable_new(L);
307 for(i = 0; i < clients->len; i++)
309 luaA_client_userdata_new(L, clients->tab[i]);
310 lua_rawseti(L, -2, i + 1);
313 return 1;
316 /** Tag object.
317 * \param L The Lua VM state.
318 * \return The number of elements pushed on stack.
319 * \luastack
320 * \lfield name Tag name.
321 * \lfield screen Screen number of the tag.
322 * \lfield layout Tag layout.
323 * \lfield selected True if the client is selected to be viewed.
324 * \lfield mwfact Master width factor.
325 * \lfield nmaster Number of master windows.
326 * \lfield ncol Number of column for slave windows.
328 static int
329 luaA_tag_index(lua_State *L)
331 size_t len;
332 tag_t **tag = luaA_checkudata(L, 1, "tag");
333 const char *attr;
335 if(luaA_usemetatable(L, 1, 2))
336 return 1;
338 attr = luaL_checklstring(L, 2, &len);
340 switch(a_tokenize(attr, len))
342 case A_TK_NAME:
343 lua_pushstring(L, (*tag)->name);
344 break;
345 case A_TK_SCREEN:
346 if((*tag)->screen == SCREEN_UNDEF)
347 return 0;
348 lua_pushnumber(L, (*tag)->screen + 1);
349 break;
350 case A_TK_LAYOUT:
351 lua_pushstring(L, name_func_rlookup((*tag)->layout, LayoutList));
352 break;
353 case A_TK_SELECTED:
354 lua_pushboolean(L, (*tag)->selected);
355 break;
356 case A_TK_MWFACT:
357 lua_pushnumber(L, (*tag)->mwfact);
358 break;
359 case A_TK_NMASTER:
360 lua_pushnumber(L, (*tag)->nmaster);
361 break;
362 case A_TK_NCOL:
363 lua_pushnumber(L, (*tag)->ncol);
364 break;
365 default:
366 return 0;
369 return 1;
372 /** Get a screen by its name.
373 * \param screen The screen to look into.
374 * \param name The name.
376 static tag_t *
377 tag_getbyname(int screen, const char *name)
379 int i;
380 tag_array_t *tags = &globalconf.screens[screen].tags;
382 for(i = 0; i < tags->len; i++)
383 if(!a_strcmp(name, tags->tab[i]->name))
384 return tags->tab[i];
385 return NULL;
388 /** Tag newindex.
389 * \param L The Lua VM state.
390 * \return The number of elements pushed on stack.
392 static int
393 luaA_tag_newindex(lua_State *L)
395 size_t len;
396 tag_t **tag = luaA_checkudata(L, 1, "tag");
397 const char *buf, *attr = luaL_checklstring(L, 2, &len);
398 double d;
399 int i, screen;
400 layout_t *l;
402 switch(a_tokenize(attr, len))
404 case A_TK_NAME:
405 buf = luaL_checklstring(L, 3, &len);
406 if((*tag)->screen != SCREEN_UNDEF)
408 if(tag_getbyname((*tag)->screen, (*tag)->name) != *tag)
409 luaL_error(L, "a tag with the name `%s' is already on screen %d",
410 buf, (*tag)->screen);
411 widget_invalidate_cache((*tag)->screen, WIDGET_CACHE_TAGS);
413 p_delete(&(*tag)->name);
414 a_iso2utf8(&(*tag)->name, buf, len);
415 break;
416 case A_TK_SCREEN:
417 if(!lua_isnil(L, 3))
419 screen = luaL_checknumber(L, 3) - 1;
420 luaA_checkscreen(screen);
422 else
423 screen = SCREEN_UNDEF;
425 if((*tag)->screen != SCREEN_UNDEF)
426 tag_remove_from_screen(*tag);
428 if(screen != SCREEN_UNDEF)
430 if(tag_getbyname(screen, (*tag)->name))
431 luaL_error(L, "a tag with the name `%s' is already on screen %d",
432 (*tag)->name, screen);
434 tag_append_to_screen(*tag, &globalconf.screens[screen]);
436 break;
437 case A_TK_LAYOUT:
438 buf = luaL_checkstring(L, 3);
439 l = name_func_lookup(buf, LayoutList);
440 if(l)
441 (*tag)->layout = l;
442 else
443 luaL_error(L, "unknown layout: %s", buf);
444 break;
445 case A_TK_SELECTED:
446 if((*tag)->screen != SCREEN_UNDEF)
447 tag_view(*tag, luaA_checkboolean(L, 3));
448 return 0;
449 case A_TK_MWFACT:
450 d = luaL_checknumber(L, 3);
451 if(d > 0 && d < 1)
452 (*tag)->mwfact = d;
453 else
454 luaL_error(L, "bad value, must be between 0 and 1");
455 break;
456 case A_TK_NMASTER:
457 i = luaL_checknumber(L, 3);
458 if(i >= 0)
459 (*tag)->nmaster = i;
460 else
461 luaL_error(L, "bad value, must be greater than 0");
462 break;
463 case A_TK_NCOL:
464 i = luaL_checknumber(L, 3);
465 if(i >= 1)
466 (*tag)->ncol = i;
467 else
468 luaL_error(L, "bad value, must be greater than 1");
469 break;
470 default:
471 return 0;
474 if((*tag)->screen != SCREEN_UNDEF && (*tag)->selected)
475 globalconf.screens[(*tag)->screen].need_arrange = true;
477 return 0;
480 const struct luaL_reg awesome_tag_methods[] =
482 { "__call", luaA_tag_new },
483 { NULL, NULL }
485 const struct luaL_reg awesome_tag_meta[] =
487 { "clients", luaA_tag_clients },
488 { "__index", luaA_tag_index },
489 { "__newindex", luaA_tag_newindex },
490 { "__eq", luaA_tag_eq },
491 { "__gc", luaA_tag_gc },
492 { "__tostring", luaA_tag_tostring },
493 { NULL, NULL },
496 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80