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 /** Create a new tag.
244 * \param L The Lua VM state.
247 * \lparam A table with at least a name attribute.
248 * Optional attributes are: mwfact, ncol, nmaster and layout.
249 * \lreturn A new tag object.
252 luaA_tag_new(lua_State
*L
)
257 const char *name
, *lay
;
261 luaA_checktable(L
, 2);
263 if(!(name
= luaA_getopt_string(L
, 2, "name", NULL
)))
264 luaL_error(L
, "object tag must have a name");
266 mwfact
= luaA_getopt_number(L
, 2, "mwfact", 0.5);
267 ncol
= luaA_getopt_number(L
, 2, "ncol", 1);
268 nmaster
= luaA_getopt_number(L
, 2, "nmaster", 1);
269 lay
= luaA_getopt_lstring(L
, 2, "layout", "tile", &len
);
271 layout
= name_func_lookup(lay
, LayoutList
);
273 tag
= tag_new(name
, len
,
275 mwfact
, nmaster
, ncol
);
277 return luaA_tag_userdata_new(L
, tag
);
281 * \param L The Lua VM state.
282 * \return The number of elements pushed on stack.
284 * \lfield name Tag name.
285 * \lfield screen Screen number of the tag.
286 * \lfield layout Tag layout.
287 * \lfield selected True if the client is selected to be viewed.
288 * \lfield mwfact Master width factor.
289 * \lfield nmaster Number of master windows.
290 * \lfield ncol Number of column for slave windows.
291 * \lfield clients The clients that has this tag set.
294 luaA_tag_index(lua_State
*L
)
297 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
299 client_array_t
*clients
;
302 if(luaA_usemetatable(L
, 1, 2))
305 attr
= luaL_checklstring(L
, 2, &len
);
307 switch(a_tokenize(attr
, len
))
310 lua_pushstring(L
, (*tag
)->name
);
313 if((*tag
)->screen
== SCREEN_UNDEF
)
315 lua_pushnumber(L
, (*tag
)->screen
+ 1);
318 lua_pushstring(L
, name_func_rlookup((*tag
)->layout
, LayoutList
));
321 lua_pushboolean(L
, (*tag
)->selected
);
324 lua_pushnumber(L
, (*tag
)->mwfact
);
327 lua_pushnumber(L
, (*tag
)->nmaster
);
330 lua_pushnumber(L
, (*tag
)->ncol
);
333 clients
= &(*tag
)->clients
;
335 for(i
= 0; i
< clients
->len
; i
++)
337 luaA_client_userdata_new(L
, clients
->tab
[i
]);
338 luaA_client_userdata_new(L
, clients
->tab
[i
]);
349 /** Get a screen by its name.
350 * \param screen The screen to look into.
351 * \param name The name.
354 tag_getbyname(int screen
, const char *name
)
357 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
359 for(i
= 0; i
< tags
->len
; i
++)
360 if(!a_strcmp(name
, tags
->tab
[i
]->name
))
366 * \param L The Lua VM state.
367 * \return The number of elements pushed on stack.
370 luaA_tag_newindex(lua_State
*L
)
373 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
374 const char *buf
, *attr
= luaL_checklstring(L
, 2, &len
);
380 switch(a_tokenize(attr
, len
))
383 buf
= luaL_checklstring(L
, 3, &len
);
384 if((*tag
)->screen
!= SCREEN_UNDEF
)
386 if(tag_getbyname((*tag
)->screen
, (*tag
)->name
) != *tag
)
387 luaL_error(L
, "a tag with the name `%s' is already on screen %d",
388 buf
, (*tag
)->screen
);
389 widget_invalidate_cache((*tag
)->screen
, WIDGET_CACHE_TAGS
);
391 p_delete(&(*tag
)->name
);
392 a_iso2utf8(&(*tag
)->name
, buf
, len
);
397 screen
= luaL_checknumber(L
, 3) - 1;
398 luaA_checkscreen(screen
);
401 screen
= SCREEN_UNDEF
;
403 if((*tag
)->screen
!= SCREEN_UNDEF
)
405 if((*tag
)->clients
.len
)
406 luaL_error(L
, "unable to remove tag %s from screen %d, it still has clients",
407 (*tag
)->name
, (*tag
)->screen
);
409 tag_remove_from_screen(*tag
);
412 if(screen
!= SCREEN_UNDEF
)
414 if(tag_getbyname(screen
, (*tag
)->name
))
415 luaL_error(L
, "a tag with the name `%s' is already on screen %d",
416 (*tag
)->name
, screen
);
418 tag_append_to_screen(*tag
, screen
);
422 buf
= luaL_checkstring(L
, 3);
423 l
= name_func_lookup(buf
, LayoutList
);
427 luaL_error(L
, "unknown layout: %s", buf
);
430 if((*tag
)->screen
!= SCREEN_UNDEF
)
431 tag_view(*tag
, luaA_checkboolean(L
, 3));
434 d
= luaL_checknumber(L
, 3);
438 luaL_error(L
, "bad value, must be between 0 and 1");
441 i
= luaL_checknumber(L
, 3);
445 luaL_error(L
, "bad value, must be greater than 0");
448 i
= luaL_checknumber(L
, 3);
452 luaL_error(L
, "bad value, must be greater than 1");
455 luaA_checktable(L
, 3);
456 for(i
= 0; i
< (*tag
)->clients
.len
; i
++)
457 untag_client((*tag
)->clients
.tab
[i
], *tag
);
459 while(lua_next(L
, 3))
461 c
= luaA_checkudata(L
, -1, "client");
462 tag_client(*c
, *tag
);
470 if((*tag
)->screen
!= SCREEN_UNDEF
)
471 globalconf
.screens
[(*tag
)->screen
].need_arrange
= true;
476 const struct luaL_reg awesome_tag_methods
[] =
478 { "__call", luaA_tag_new
},
481 const struct luaL_reg awesome_tag_meta
[] =
483 { "__index", luaA_tag_index
},
484 { "__newindex", luaA_tag_newindex
},
485 { "__eq", luaA_tag_eq
},
486 { "__gc", luaA_tag_gc
},
487 { "__tostring", luaA_tag_tostring
},
491 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80