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 /** Tag a client with specified tag.
107 * \param c the client to tag
108 * \param t the tag to tag the client with
111 tag_client(client_t
*c
, tag_t
*t
)
113 /* don't tag twice */
114 if(is_client_tagged(c
, t
))
117 client_array_append(&t
->clients
, c
);
119 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
120 globalconf
.screens
[c
->screen
].need_arrange
= true;
123 /** Untag a client with specified tag.
124 * \param c the client to tag
125 * \param t the tag to tag the client with
128 untag_client(client_t
*c
, tag_t
*t
)
130 for(int i
= 0; i
< t
->clients
.len
; i
++)
131 if(t
->clients
.tab
[i
] == c
)
133 client_array_take(&t
->clients
, i
);
135 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
136 globalconf
.screens
[c
->screen
].need_arrange
= true;
141 /** Check if a client is tagged with the specified tag.
142 * \param c the client
144 * \return true if the client is tagged with the tag, false otherwise.
147 is_client_tagged(client_t
*c
, tag_t
*t
)
149 for(int i
= 0; i
< t
->clients
.len
; i
++)
150 if (t
->clients
.tab
[i
] == c
)
156 /** Get the current tags for the specified screen.
157 * Returned pointer must be p_delete'd after.
158 * \param screen screen id
159 * \return a double pointer of tag list finished with a NULL element
162 tags_get_current(int screen
)
164 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
165 tag_t
**out
= p_new(tag_t
*, tags
->len
+ 1);
168 for(int i
= 0; i
< tags
->len
; i
++)
169 if(tags
->tab
[i
]->selected
)
170 out
[n
++] = tags
->tab
[i
];
176 /** Set a tag to be the only one viewed.
177 * \param target the tag to see
180 tag_view_only(tag_t
*target
)
184 tag_array_t
*tags
= &globalconf
.screens
[target
->screen
].tags
;
186 for(int i
= 0; i
< tags
->len
; i
++)
187 tag_view(tags
->tab
[i
], tags
->tab
[i
] == target
);
191 /** View only a tag, selected by its index.
192 * \param screen screen id
193 * \param dindex the index
196 tag_view_only_byindex(int screen
, int dindex
)
198 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
200 if(dindex
< 0 || dindex
>= tags
->len
)
202 tag_view_only(tags
->tab
[dindex
]);
205 /** Convert a tag to a printable string.
206 * \param L The Lua VM state.
213 luaA_tag_tostring(lua_State
*L
)
215 tag_t
**p
= luaA_checkudata(L
, 1, "tag");
216 lua_pushfstring(L
, "[tag udata(%p) name(%s)]", *p
, (*p
)->name
);
220 /** Get all tags from a screen.
221 * \param L The Lua VM state.
224 * \lparam A screen number.
225 * \lreturn A table with all tags from the screen specified, indexed by tag name.
228 luaA_tag_get(lua_State
*L
)
230 int screen
= luaL_checknumber(L
, 1) - 1;
231 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
233 luaA_checkscreen(screen
);
237 for(int i
= 0; i
< tags
->len
; i
++)
239 luaA_tag_userdata_new(L
, tags
->tab
[i
]);
240 lua_setfield(L
, -2, tags
->tab
[i
]->name
);
246 /** Get all tags from a screen.
247 * \param L The Lua VM state.
250 * \lparam A screen number.
251 * \lreturn A table with all tags from the screen specified, ordered and indexed
255 luaA_tag_geti(lua_State
*L
)
257 int screen
= luaL_checknumber(L
, 1) - 1;
258 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
260 luaA_checkscreen(screen
);
264 for(int i
= 0; i
< tags
->len
; i
++)
266 luaA_tag_userdata_new(L
, tags
->tab
[i
]);
267 lua_rawseti(L
, -2, i
+ 1);
273 /** Create a new tag.
274 * \param L The Lua VM state.
277 * \lparam A table with at least a name attribute.
278 * Optional attributes are: mwfact, ncol, nmaster and layout.
279 * \lreturn A new tag object.
282 luaA_tag_new(lua_State
*L
)
287 const char *name
, *lay
;
291 luaA_checktable(L
, 2);
293 if(!(name
= luaA_getopt_string(L
, 2, "name", NULL
)))
294 luaL_error(L
, "object tag must have a name");
296 mwfact
= luaA_getopt_number(L
, 2, "mwfact", 0.5);
297 ncol
= luaA_getopt_number(L
, 2, "ncol", 1);
298 nmaster
= luaA_getopt_number(L
, 2, "nmaster", 1);
299 lay
= luaA_getopt_lstring(L
, 2, "layout", "tile", &len
);
301 layout
= name_func_lookup(lay
, LayoutList
);
303 tag
= tag_new(name
, len
,
305 mwfact
, nmaster
, ncol
);
307 return luaA_tag_userdata_new(L
, tag
);
311 * \param L The Lua VM state.
312 * \return The number of elements pushed on stack.
314 * \lfield name Tag name.
315 * \lfield screen Screen number of the tag.
316 * \lfield layout Tag layout.
317 * \lfield selected True if the client is selected to be viewed.
318 * \lfield mwfact Master width factor.
319 * \lfield nmaster Number of master windows.
320 * \lfield ncol Number of column for slave windows.
323 luaA_tag_index(lua_State
*L
)
326 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
329 if(luaA_usemetatable(L
, 1, 2))
332 attr
= luaL_checklstring(L
, 2, &len
);
334 switch(a_tokenize(attr
, len
))
337 lua_pushstring(L
, (*tag
)->name
);
340 if((*tag
)->screen
== SCREEN_UNDEF
)
342 lua_pushnumber(L
, (*tag
)->screen
+ 1);
345 lua_pushstring(L
, name_func_rlookup((*tag
)->layout
, LayoutList
));
348 lua_pushboolean(L
, (*tag
)->selected
);
351 lua_pushnumber(L
, (*tag
)->mwfact
);
354 lua_pushnumber(L
, (*tag
)->nmaster
);
357 lua_pushnumber(L
, (*tag
)->ncol
);
367 * \param L The Lua VM state.
368 * \return The number of elements pushed on stack.
371 luaA_tag_newindex(lua_State
*L
)
374 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
375 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 tag_array_t
*tags
= &globalconf
.screens
[(*tag
)->screen
].tags
;
387 for(i
= 0; i
< tags
->len
; i
++)
388 if(tags
->tab
[i
] != *tag
&& !a_strcmp(buf
, tags
->tab
[i
]->name
))
389 luaL_error(L
, "a tag with the name `%s' is already on screen %d",
390 buf
, (*tag
)->screen
);
392 p_delete(&(*tag
)->name
);
393 a_iso2utf8(&(*tag
)->name
, buf
, len
);
394 widget_invalidate_cache((*tag
)->screen
, WIDGET_CACHE_TAGS
);
397 if((*tag
)->screen
== SCREEN_UNDEF
)
399 int screen
= luaL_checknumber(L
, 3) - 1;
401 luaA_checkscreen(screen
);
403 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
404 for(i
= 0; i
< tags
->len
; i
++)
405 if(tags
->tab
[i
] != *tag
&& !a_strcmp((*tag
)->name
, tags
->tab
[i
]->name
))
406 luaL_error(L
, "a tag with the name `%s' is already on screen %d",
407 (*tag
)->name
, screen
);
409 tag_append_to_screen(*tag
, screen
);
413 luaL_error(L
, "tag already on screen %d", (*tag
)->screen
+ 1);
415 buf
= luaL_checkstring(L
, 3);
416 l
= name_func_lookup(buf
, LayoutList
);
420 luaL_error(L
, "unknown layout: %s", buf
);
423 tag_view(*tag
, luaA_checkboolean(L
, 3));
426 d
= luaL_checknumber(L
, 3);
430 luaL_error(L
, "bad value, must be between 0 and 1");
433 i
= luaL_checknumber(L
, 3);
437 luaL_error(L
, "bad value, must be greater than 0");
440 i
= luaL_checknumber(L
, 3);
444 luaL_error(L
, "bad value, must be greater than 1");
450 if((*tag
)->screen
!= SCREEN_UNDEF
)
451 globalconf
.screens
[(*tag
)->screen
].need_arrange
= true;
456 const struct luaL_reg awesome_tag_methods
[] =
458 { "__call", luaA_tag_new
},
459 { "get", luaA_tag_get
},
460 { "geti", luaA_tag_geti
},
463 const struct luaL_reg awesome_tag_meta
[] =
465 { "__index", luaA_tag_index
},
466 { "__newindex", luaA_tag_newindex
},
467 { "__eq", luaA_tag_eq
},
468 { "__gc", luaA_tag_gc
},
469 { "__tostring", luaA_tag_tostring
},
473 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80