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 globalconf
.screens
[tag
->screen
].need_arrange
= true;
48 /** Create a new tag. Parameters values are checked.
49 * \param name Tag name.
50 * \param len Tag name length.
51 * \param layout Layout to use.
52 * \param mwfact Master width factor.
53 * \param nmaster Number of master windows.
54 * \param ncol Number of columns for slaves windows.
55 * \return A new tag with all these parameters.
58 tag_new(const char *name
, ssize_t len
, layout_t
*layout
, double mwfact
, int nmaster
, int ncol
)
62 tag
= p_new(tag_t
, 1);
63 a_iso2utf8(&tag
->name
, name
, len
);
67 tag
->screen
= SCREEN_UNDEF
;
70 if(tag
->mwfact
<= 0 || tag
->mwfact
>= 1)
73 if((tag
->nmaster
= nmaster
) < 0)
76 if((tag
->ncol
= ncol
) < 1)
82 /** Append a tag to a screen.
83 * \param tag the tag to append
84 * \param screen the screen id
87 tag_append_to_screen(tag_t
*tag
, screen_t
*s
)
89 int phys_screen
= screen_virttophys(s
->index
);
91 tag
->screen
= s
->index
;
92 tag_array_append(&s
->tags
, tag_ref(&tag
));
93 ewmh_update_net_numbers_of_desktop(phys_screen
);
94 ewmh_update_net_desktop_names(phys_screen
);
95 ewmh_update_workarea(phys_screen
);
97 /* resave tag prop of all clients so the number of tag will be the
99 for(client_t
*c
= globalconf
.clients
; c
; c
= c
->next
)
100 client_saveprops_tags(c
);
103 lua_pushnumber(globalconf
.L
, s
->index
+ 1);
104 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tags
, 1, 0);
107 /** Remove a tag from screen. Tag must be on a screen and have no clients.
108 * \param tag The tag to remove.
111 tag_remove_from_screen(tag_t
*tag
)
113 int screen
= tag
->screen
;
114 int phys_screen
= screen_virttophys(tag
->screen
);
115 tag_array_t
*tags
= &globalconf
.screens
[tag
->screen
].tags
;
117 for(int i
= 0; i
< tags
->len
; i
++)
118 if(tags
->tab
[i
] == tag
)
120 tag_array_take(tags
, i
);
123 ewmh_update_net_numbers_of_desktop(phys_screen
);
124 ewmh_update_net_desktop_names(phys_screen
);
125 ewmh_update_workarea(phys_screen
);
126 tag
->screen
= SCREEN_UNDEF
;
129 /* resave tag prop of all clients so the number of tag will be the
131 for(client_t
*c
= globalconf
.clients
; c
; c
= c
->next
)
132 client_saveprops_tags(c
);
135 lua_pushnumber(globalconf
.L
, screen
+ 1);
136 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tags
, 1, 0);
139 /** Tag a client with specified tag.
140 * \param c the client to tag
141 * \param t the tag to tag the client with
144 tag_client(client_t
*c
, tag_t
*t
)
146 /* don't tag twice */
147 if(is_client_tagged(c
, t
))
151 client_array_append(&t
->clients
, c
);
152 client_saveprops_tags(c
);
153 client_need_arrange(c
);
155 luaA_client_userdata_new(globalconf
.L
, c
);
156 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tagged
, 1, 0);
159 /** Untag a client with specified tag.
160 * \param c the client to tag
161 * \param t the tag to tag the client with
164 untag_client(client_t
*c
, tag_t
*t
)
166 for(int i
= 0; i
< t
->clients
.len
; i
++)
167 if(t
->clients
.tab
[i
] == c
)
169 client_need_arrange(c
);
170 client_array_take(&t
->clients
, i
);
172 client_saveprops_tags(c
);
174 luaA_client_userdata_new(globalconf
.L
, c
);
175 luaA_tag_userdata_new(globalconf
.L
, t
);
176 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
181 /** Check if a client is tagged with the specified tag.
182 * \param c the client
184 * \return true if the client is tagged with the tag, false otherwise.
187 is_client_tagged(client_t
*c
, tag_t
*t
)
189 for(int i
= 0; i
< t
->clients
.len
; i
++)
190 if (t
->clients
.tab
[i
] == c
)
196 /** Get the current tags for the specified screen.
197 * Returned pointer must be p_delete'd after.
198 * \param screen screen id
199 * \return a double pointer of tag list finished with a NULL element
202 tags_get_current(int screen
)
204 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
205 tag_t
**out
= p_new(tag_t
*, tags
->len
+ 1);
208 for(int i
= 0; i
< tags
->len
; i
++)
209 if(tags
->tab
[i
]->selected
)
210 out
[n
++] = tags
->tab
[i
];
216 /** Set a tag to be the only one viewed.
217 * \param target the tag to see
220 tag_view_only(tag_t
*target
)
224 tag_array_t
*tags
= &globalconf
.screens
[target
->screen
].tags
;
226 for(int i
= 0; i
< tags
->len
; i
++)
227 tag_view(tags
->tab
[i
], tags
->tab
[i
] == target
);
231 /** View only a tag, selected by its index.
232 * \param screen screen id
233 * \param dindex the index
236 tag_view_only_byindex(int screen
, int dindex
)
238 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
240 if(dindex
< 0 || dindex
>= tags
->len
)
242 tag_view_only(tags
->tab
[dindex
]);
245 /** Create a new tag.
246 * \param L The Lua VM state.
249 * \lparam A table with at least a name attribute.
250 * Optional attributes are: mwfact, ncol, nmaster and layout.
251 * \lreturn A new tag object.
254 luaA_tag_new(lua_State
*L
)
259 const char *name
, *lay
;
263 luaA_checktable(L
, 2);
265 if(!(name
= luaA_getopt_lstring(L
, 2, "name", NULL
, &len
)))
266 luaL_error(L
, "object tag must have a name");
268 mwfact
= luaA_getopt_number(L
, 2, "mwfact", 0.5);
269 ncol
= luaA_getopt_number(L
, 2, "ncol", 1);
270 nmaster
= luaA_getopt_number(L
, 2, "nmaster", 1);
271 lay
= luaA_getopt_string(L
, 2, "layout", "tile");
273 layout
= name_func_lookup(lay
, LayoutList
);
275 tag
= tag_new(name
, len
,
277 mwfact
, nmaster
, ncol
);
279 return luaA_tag_userdata_new(L
, tag
);
282 /** Get or set the clients attached to this tag.
283 * \param L The Lua VM state.
284 * \return The number of elements pushed on stack.
286 * \lparam None or a table of clients to set.
287 * \lreturn A table with the clients attached to this tags.
290 luaA_tag_clients(lua_State
*L
)
292 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
293 client_array_t
*clients
= &(*tag
)->clients
;
296 if(lua_gettop(L
) == 2)
300 luaA_checktable(L
, 2);
301 for(i
= 0; i
< (*tag
)->clients
.len
; i
++)
302 untag_client((*tag
)->clients
.tab
[i
], *tag
);
304 while(lua_next(L
, 2))
306 c
= luaA_checkudata(L
, -1, "client");
307 tag_client(*c
, *tag
);
313 for(i
= 0; i
< clients
->len
; i
++)
315 luaA_client_userdata_new(L
, clients
->tab
[i
]);
316 lua_rawseti(L
, -2, i
+ 1);
323 * \param L The Lua VM state.
324 * \return The number of elements pushed on stack.
326 * \lfield name Tag name.
327 * \lfield screen Screen number of the tag.
328 * \lfield layout Tag layout.
329 * \lfield selected True if the client is selected to be viewed.
330 * \lfield mwfact Master width factor.
331 * \lfield nmaster Number of master windows.
332 * \lfield ncol Number of column for slave windows.
335 luaA_tag_index(lua_State
*L
)
338 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
341 if(luaA_usemetatable(L
, 1, 2))
344 attr
= luaL_checklstring(L
, 2, &len
);
346 switch(a_tokenize(attr
, len
))
349 lua_pushstring(L
, (*tag
)->name
);
352 if((*tag
)->screen
== SCREEN_UNDEF
)
354 lua_pushnumber(L
, (*tag
)->screen
+ 1);
357 lua_pushstring(L
, name_func_rlookup((*tag
)->layout
, LayoutList
));
360 lua_pushboolean(L
, (*tag
)->selected
);
363 lua_pushnumber(L
, (*tag
)->mwfact
);
366 lua_pushnumber(L
, (*tag
)->nmaster
);
369 lua_pushnumber(L
, (*tag
)->ncol
);
379 * \param L The Lua VM state.
380 * \return The number of elements pushed on stack.
383 luaA_tag_newindex(lua_State
*L
)
386 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
387 const char *buf
, *attr
= luaL_checklstring(L
, 2, &len
);
392 switch(a_tokenize(attr
, len
))
395 buf
= luaL_checklstring(L
, 3, &len
);
396 p_delete(&(*tag
)->name
);
397 a_iso2utf8(&(*tag
)->name
, buf
, len
);
402 screen
= luaL_checknumber(L
, 3) - 1;
403 luaA_checkscreen(screen
);
406 screen
= SCREEN_UNDEF
;
408 if((*tag
)->screen
!= SCREEN_UNDEF
)
409 tag_remove_from_screen(*tag
);
411 if(screen
!= SCREEN_UNDEF
)
412 tag_append_to_screen(*tag
, &globalconf
.screens
[screen
]);
415 buf
= luaL_checkstring(L
, 3);
416 l
= name_func_lookup(buf
, LayoutList
);
421 luaA_warn(L
, "unknown layout: %s", buf
);
426 if((*tag
)->screen
!= SCREEN_UNDEF
)
427 tag_view(*tag
, luaA_checkboolean(L
, 3));
430 d
= luaL_checknumber(L
, 3);
435 luaA_warn(L
, "bad value, must be between 0 and 1");
440 i
= luaL_checknumber(L
, 3);
445 luaA_warn(L
, "bad value, must be greater than 0");
450 i
= luaL_checknumber(L
, 3);
455 luaA_warn(L
, "bad value, must be greater than 1");
463 if((*tag
)->screen
!= SCREEN_UNDEF
&& (*tag
)->selected
)
464 globalconf
.screens
[(*tag
)->screen
].need_arrange
= true;
469 const struct luaL_reg awesome_tag_methods
[] =
471 { "__call", luaA_tag_new
},
474 const struct luaL_reg awesome_tag_meta
[] =
476 { "clients", luaA_tag_clients
},
477 { "__index", luaA_tag_index
},
478 { "__newindex", luaA_tag_newindex
},
479 { "__eq", luaA_tag_eq
},
480 { "__gc", luaA_tag_gc
},
481 { "__tostring", luaA_tag_tostring
},
485 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80