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 "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.
38 * \param view set visible or not
41 tag_view(tag_t
*tag
, bool 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.
59 tag_new(const char *name
, ssize_t len
, layout_t
*layout
, double mwfact
, int nmaster
, int ncol
)
63 tag
= p_new(tag_t
, 1);
64 a_iso2utf8(&tag
->name
, name
, len
);
68 tag
->screen
= SCREEN_UNDEF
;
71 if(tag
->mwfact
<= 0 || tag
->mwfact
>= 1)
74 if((tag
->nmaster
= nmaster
) < 0)
77 if((tag
->ncol
= ncol
) < 1)
83 /** Append a tag to a screen.
84 * \param tag the tag to append
85 * \param screen the screen id
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.
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
);
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
;
123 /** Tag a client with specified tag.
124 * \param c the client to tag
125 * \param t the tag to tag the client with
128 tag_client(client_t
*c
, tag_t
*t
)
130 /* don't tag twice */
131 if(is_client_tagged(c
, 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
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
);
154 client_saveprops_tags(c
);
155 widget_invalidate_cache(c
->screen
, WIDGET_CACHE_CLIENTS
);
160 /** Check if a client is tagged with the specified tag.
161 * \param c the client
163 * \return true if the client is tagged with the tag, false otherwise.
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
)
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
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);
187 for(int i
= 0; i
< tags
->len
; i
++)
188 if(tags
->tab
[i
]->selected
)
189 out
[n
++] = tags
->tab
[i
];
195 /** Set a tag to be the only one viewed.
196 * \param target the tag to see
199 tag_view_only(tag_t
*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
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
)
221 tag_view_only(tags
->tab
[dindex
]);
224 /** Convert a tag to a printable string.
225 * \param L The Lua VM state.
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
);
239 /** Create a new tag.
240 * \param L The Lua VM state.
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.
248 luaA_tag_new(lua_State
*L
)
253 const char *name
, *lay
;
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
,
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.
280 * \lparam None or a table of clients to set.
281 * \lreturn A table with the clients attached to this tags.
284 luaA_tag_clients(lua_State
*L
)
286 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
287 client_array_t
*clients
= &(*tag
)->clients
;
290 if(lua_gettop(L
) == 2)
294 luaA_checktable(L
, 2);
295 for(i
= 0; i
< (*tag
)->clients
.len
; i
++)
296 untag_client((*tag
)->clients
.tab
[i
], *tag
);
298 while(lua_next(L
, 2))
300 c
= luaA_checkudata(L
, -1, "client");
301 tag_client(*c
, *tag
);
307 for(i
= 0; i
< clients
->len
; i
++)
309 luaA_client_userdata_new(L
, clients
->tab
[i
]);
310 lua_rawseti(L
, -2, i
+ 1);
317 * \param L The Lua VM state.
318 * \return The number of elements pushed on stack.
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.
329 luaA_tag_index(lua_State
*L
)
332 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
335 if(luaA_usemetatable(L
, 1, 2))
338 attr
= luaL_checklstring(L
, 2, &len
);
340 switch(a_tokenize(attr
, len
))
343 lua_pushstring(L
, (*tag
)->name
);
346 if((*tag
)->screen
== SCREEN_UNDEF
)
348 lua_pushnumber(L
, (*tag
)->screen
+ 1);
351 lua_pushstring(L
, name_func_rlookup((*tag
)->layout
, LayoutList
));
354 lua_pushboolean(L
, (*tag
)->selected
);
357 lua_pushnumber(L
, (*tag
)->mwfact
);
360 lua_pushnumber(L
, (*tag
)->nmaster
);
363 lua_pushnumber(L
, (*tag
)->ncol
);
372 /** Get a screen by its name.
373 * \param screen The screen to look into.
374 * \param name The name.
377 tag_getbyname(int screen
, const char *name
)
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
))
389 * \param L The Lua VM state.
390 * \return The number of elements pushed on stack.
393 luaA_tag_newindex(lua_State
*L
)
396 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
397 const char *buf
, *attr
= luaL_checklstring(L
, 2, &len
);
402 switch(a_tokenize(attr
, len
))
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
);
419 screen
= luaL_checknumber(L
, 3) - 1;
420 luaA_checkscreen(screen
);
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
]);
438 buf
= luaL_checkstring(L
, 3);
439 l
= name_func_lookup(buf
, LayoutList
);
443 luaL_error(L
, "unknown layout: %s", buf
);
446 if((*tag
)->screen
!= SCREEN_UNDEF
)
447 tag_view(*tag
, luaA_checkboolean(L
, 3));
450 d
= luaL_checknumber(L
, 3);
454 luaL_error(L
, "bad value, must be between 0 and 1");
457 i
= luaL_checknumber(L
, 3);
461 luaL_error(L
, "bad value, must be greater than 0");
464 i
= luaL_checknumber(L
, 3);
468 luaL_error(L
, "bad value, must be greater than 1");
474 if((*tag
)->screen
!= SCREEN_UNDEF
&& (*tag
)->selected
)
475 globalconf
.screens
[(*tag
)->screen
].need_arrange
= true;
480 const struct luaL_reg awesome_tag_methods
[] =
482 { "__call", luaA_tag_new
},
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
},
496 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80