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.
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.
44 * \param view set visible or not
47 tag_view(tag_t
*tag
, bool 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.
65 tag_new(const char *name
, ssize_t len
, layout_t
*layout
, double mwfact
, int nmaster
, int ncol
)
69 tag
= p_new(tag_t
, 1);
70 a_iso2utf8(&tag
->name
, name
, len
);
74 tag
->screen
= SCREEN_UNDEF
;
77 if(tag
->mwfact
<= 0 || tag
->mwfact
>= 1)
80 if((tag
->nmaster
= nmaster
) < 0)
83 if((tag
->ncol
= ncol
) < 1)
89 /** Append a tag to a screen.
90 * \param tag the tag to append
91 * \param screen the screen id
94 tag_append_to_screen(tag_t
*tag
, int screen
)
96 int phys_screen
= screen_virttophys(screen
);
99 tag_array_append(&globalconf
.screens
[screen
].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(screen
, 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.
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
);
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
;
129 /** Tag a client with specified tag.
130 * \param c the client to tag
131 * \param t the tag to tag the client with
134 tag_client(client_t
*c
, tag_t
*t
)
136 /* don't tag twice */
137 if(is_client_tagged(c
, t
))
140 client_array_append(&t
->clients
, c
);
142 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
143 globalconf
.screens
[c
->screen
].need_arrange
= true;
146 /** Untag a client with specified tag.
147 * \param c the client to tag
148 * \param t the tag to tag the client with
151 untag_client(client_t
*c
, tag_t
*t
)
153 for(int i
= 0; i
< t
->clients
.len
; i
++)
154 if(t
->clients
.tab
[i
] == c
)
156 client_array_take(&t
->clients
, i
);
158 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
159 globalconf
.screens
[c
->screen
].need_arrange
= true;
164 /** Check if a client is tagged with the specified tag.
165 * \param c the client
167 * \return true if the client is tagged with the tag, false otherwise.
170 is_client_tagged(client_t
*c
, tag_t
*t
)
172 for(int i
= 0; i
< t
->clients
.len
; i
++)
173 if (t
->clients
.tab
[i
] == c
)
179 /** Get the current tags for the specified screen.
180 * Returned pointer must be p_delete'd after.
181 * \param screen screen id
182 * \return a double pointer of tag list finished with a NULL element
185 tags_get_current(int screen
)
187 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
188 tag_t
**out
= p_new(tag_t
*, tags
->len
+ 1);
191 for(int i
= 0; i
< tags
->len
; i
++)
192 if(tags
->tab
[i
]->selected
)
193 out
[n
++] = tags
->tab
[i
];
199 /** Set a tag to be the only one viewed.
200 * \param target the tag to see
203 tag_view_only(tag_t
*target
)
207 tag_array_t
*tags
= &globalconf
.screens
[target
->screen
].tags
;
209 for(int i
= 0; i
< tags
->len
; i
++)
210 tag_view(tags
->tab
[i
], tags
->tab
[i
] == target
);
214 /** View only a tag, selected by its index.
215 * \param screen screen id
216 * \param dindex the index
219 tag_view_only_byindex(int screen
, int dindex
)
221 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
223 if(dindex
< 0 || dindex
>= tags
->len
)
225 tag_view_only(tags
->tab
[dindex
]);
228 /** Convert a tag to a printable string.
229 * \param L The Lua VM state.
236 luaA_tag_tostring(lua_State
*L
)
238 tag_t
**p
= luaA_checkudata(L
, 1, "tag");
239 lua_pushfstring(L
, "[tag udata(%p) name(%s)]", *p
, (*p
)->name
);
243 /** Get all tags from a screen.
244 * \param L The Lua VM state.
247 * \lparam A screen number.
248 * \lreturn A table with all tags from the screen specified, indexed by tag name.
251 luaA_tag_get(lua_State
*L
)
253 int screen
= luaL_checknumber(L
, 1) - 1;
254 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
256 luaA_checkscreen(screen
);
260 for(int i
= 0; i
< tags
->len
; i
++)
262 luaA_tag_userdata_new(L
, tags
->tab
[i
]);
263 lua_setfield(L
, -2, tags
->tab
[i
]->name
);
269 /** Get all tags from a screen.
270 * \param L The Lua VM state.
273 * \lparam A screen number.
274 * \lreturn A table with all tags from the screen specified, ordered and indexed
278 luaA_tag_geti(lua_State
*L
)
280 int screen
= luaL_checknumber(L
, 1) - 1;
281 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
283 luaA_checkscreen(screen
);
287 for(int i
= 0; i
< tags
->len
; i
++)
289 luaA_tag_userdata_new(L
, tags
->tab
[i
]);
290 lua_rawseti(L
, -2, i
+ 1);
296 /** Create a new tag.
297 * \param L The Lua VM state.
300 * \lparam A table with at least a name attribute.
301 * Optional attributes are: mwfact, ncol, nmaster and layout.
302 * \lreturn A new tag object.
305 luaA_tag_new(lua_State
*L
)
310 const char *name
, *lay
;
314 luaA_checktable(L
, 2);
316 if(!(name
= luaA_getopt_string(L
, 2, "name", NULL
)))
317 luaL_error(L
, "object tag must have a name");
319 mwfact
= luaA_getopt_number(L
, 2, "mwfact", 0.5);
320 ncol
= luaA_getopt_number(L
, 2, "ncol", 1);
321 nmaster
= luaA_getopt_number(L
, 2, "nmaster", 1);
322 lay
= luaA_getopt_lstring(L
, 2, "layout", "tile", &len
);
324 layout
= name_func_lookup(lay
, LayoutList
);
326 tag
= tag_new(name
, len
,
328 mwfact
, nmaster
, ncol
);
330 return luaA_tag_userdata_new(L
, tag
);
334 * \param L The Lua VM state.
335 * \return The number of elements pushed on stack.
337 * \lfield name Tag name.
338 * \lfield screen Screen number of the tag.
339 * \lfield layout Tag layout.
340 * \lfield selected True if the client is selected to be viewed.
341 * \lfield mwfact Master width factor.
342 * \lfield nmaster Number of master windows.
343 * \lfield ncol Number of column for slave windows.
344 * \lfield clients The clients that has this tag set.
347 luaA_tag_index(lua_State
*L
)
350 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
352 client_array_t
*clients
;
355 if(luaA_usemetatable(L
, 1, 2))
358 attr
= luaL_checklstring(L
, 2, &len
);
360 switch(a_tokenize(attr
, len
))
363 lua_pushstring(L
, (*tag
)->name
);
366 if((*tag
)->screen
== SCREEN_UNDEF
)
368 lua_pushnumber(L
, (*tag
)->screen
+ 1);
371 lua_pushstring(L
, name_func_rlookup((*tag
)->layout
, LayoutList
));
374 lua_pushboolean(L
, (*tag
)->selected
);
377 lua_pushnumber(L
, (*tag
)->mwfact
);
380 lua_pushnumber(L
, (*tag
)->nmaster
);
383 lua_pushnumber(L
, (*tag
)->ncol
);
386 clients
= &(*tag
)->clients
;
388 for(i
= 0; i
< clients
->len
; i
++)
390 luaA_client_userdata_new(L
, clients
->tab
[i
]);
391 luaA_client_userdata_new(L
, clients
->tab
[i
]);
402 /** Get a screen by its name.
403 * \param screen The screen to look into.
404 * \param name The name.
407 tag_getbyname(int screen
, const char *name
)
410 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
412 for(i
= 0; i
< tags
->len
; i
++)
413 if(!a_strcmp(name
, tags
->tab
[i
]->name
))
419 * \param L The Lua VM state.
420 * \return The number of elements pushed on stack.
423 luaA_tag_newindex(lua_State
*L
)
426 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
427 const char *buf
, *attr
= luaL_checklstring(L
, 2, &len
);
433 switch(a_tokenize(attr
, len
))
436 buf
= luaL_checklstring(L
, 3, &len
);
437 if((*tag
)->screen
!= SCREEN_UNDEF
)
439 if(tag_getbyname((*tag
)->screen
, (*tag
)->name
) != *tag
)
440 luaL_error(L
, "a tag with the name `%s' is already on screen %d",
441 buf
, (*tag
)->screen
);
442 widget_invalidate_cache((*tag
)->screen
, WIDGET_CACHE_TAGS
);
444 p_delete(&(*tag
)->name
);
445 a_iso2utf8(&(*tag
)->name
, buf
, len
);
450 screen
= luaL_checknumber(L
, 3) - 1;
451 luaA_checkscreen(screen
);
454 screen
= SCREEN_UNDEF
;
456 if((*tag
)->screen
!= SCREEN_UNDEF
)
458 if((*tag
)->clients
.len
)
459 luaL_error(L
, "unable to remove tag %s from screen %d, it still has clients",
460 (*tag
)->name
, (*tag
)->screen
);
462 tag_remove_from_screen(*tag
);
465 if(screen
!= SCREEN_UNDEF
)
467 if(tag_getbyname(screen
, (*tag
)->name
))
468 luaL_error(L
, "a tag with the name `%s' is already on screen %d",
469 (*tag
)->name
, screen
);
471 tag_append_to_screen(*tag
, screen
);
475 buf
= luaL_checkstring(L
, 3);
476 l
= name_func_lookup(buf
, LayoutList
);
480 luaL_error(L
, "unknown layout: %s", buf
);
483 tag_view(*tag
, luaA_checkboolean(L
, 3));
486 d
= luaL_checknumber(L
, 3);
490 luaL_error(L
, "bad value, must be between 0 and 1");
493 i
= luaL_checknumber(L
, 3);
497 luaL_error(L
, "bad value, must be greater than 0");
500 i
= luaL_checknumber(L
, 3);
504 luaL_error(L
, "bad value, must be greater than 1");
507 luaA_checktable(L
, 3);
508 for(i
= 0; i
< (*tag
)->clients
.len
; i
++)
509 untag_client((*tag
)->clients
.tab
[i
], *tag
);
511 while(lua_next(L
, 3))
513 c
= luaA_checkudata(L
, -1, "client");
514 tag_client(*c
, *tag
);
522 if((*tag
)->screen
!= SCREEN_UNDEF
)
523 globalconf
.screens
[(*tag
)->screen
].need_arrange
= true;
528 const struct luaL_reg awesome_tag_methods
[] =
530 { "__call", luaA_tag_new
},
531 { "get", luaA_tag_get
},
532 { "geti", luaA_tag_geti
},
535 const struct luaL_reg awesome_tag_meta
[] =
537 { "__index", luaA_tag_index
},
538 { "__newindex", luaA_tag_newindex
},
539 { "__eq", luaA_tag_eq
},
540 { "__gc", luaA_tag_gc
},
541 { "__tostring", luaA_tag_tostring
},
545 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80