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.
29 tag_unref_simplified(tag_t
**tag
)
31 tag_unref(globalconf
.L
, *tag
);
34 /** Garbage collect a tag.
35 * \param L The Lua VM state.
39 luaA_tag_gc(lua_State
*L
)
41 tag_t
*tag
= luaL_checkudata(L
, 1, "tag");
42 client_array_wipe(&tag
->clients
);
44 return luaA_object_gc(L
);
47 /** View or unview a tag.
49 * \param view set visible or not
52 tag_view(tag_t
*tag
, bool view
)
54 if(tag
->selected
!= view
)
56 int screen_index
= screen_array_indexof(&globalconf
.screens
, tag
->screen
);
59 tag
->screen
->need_reban
= true;
60 ewmh_update_net_current_desktop(screen_virttophys(screen_index
));
62 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
64 lua_pushnumber(globalconf
.L
, screen_index
+ 1);
65 tag_push(globalconf
.L
, tag
);
67 lua_pushliteral(globalconf
.L
, "select");
69 lua_pushliteral(globalconf
.L
, "unselect");
70 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
75 /** Append a tag which on top of the stack to a screen.
76 * \param s screen the screen id
79 tag_append_to_screen(screen_t
*s
)
81 tag_t
*tag
= luaL_checkudata(globalconf
.L
, -1, "tag");
83 /* can't attach a tag twice */
87 int screen_index
= screen_array_indexof(&globalconf
.screens
, s
);
88 int phys_screen
= screen_virttophys(screen_index
);
91 tag_array_append(&s
->tags
, tag_ref(globalconf
.L
, -1));
92 ewmh_update_net_numbers_of_desktop(phys_screen
);
93 ewmh_update_net_desktop_names(phys_screen
);
94 ewmh_update_workarea(phys_screen
);
97 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
99 lua_pushnumber(globalconf
.L
, screen_index
+ 1);
100 tag_push(globalconf
.L
, tag
);
101 lua_pushliteral(globalconf
.L
, "add");
102 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
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 screen_index
= screen_array_indexof(&globalconf
.screens
, tag
->screen
);
113 int phys_screen
= screen_virttophys(screen_index
);
114 tag_array_t
*tags
= &tag
->screen
->tags
;
116 for(int i
= 0; i
< tags
->len
; i
++)
117 if(tags
->tab
[i
] == tag
)
119 tag_array_take(tags
, i
);
122 ewmh_update_net_numbers_of_desktop(phys_screen
);
123 ewmh_update_net_desktop_names(phys_screen
);
124 ewmh_update_workarea(phys_screen
);
127 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
129 lua_pushnumber(globalconf
.L
, screen_index
+ 1);
130 tag_push(globalconf
.L
, tag
);
131 lua_pushliteral(globalconf
.L
, "remove");
132 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
137 tag_unref(globalconf
.L
, tag
);
140 /** Tag a client with the tag on top of the stack.
141 * \param c the client to tag
144 tag_client(client_t
*c
)
146 tag_t
*t
= tag_ref(globalconf
.L
, -1);
148 /* don't tag twice */
149 if(is_client_tagged(c
, t
))
151 tag_unref(globalconf
.L
, t
);
155 client_array_append(&t
->clients
, c
);
156 ewmh_client_update_desktop(c
);
157 client_need_reban(c
);
160 if(globalconf
.hooks
.tagged
!= LUA_REFNIL
)
162 client_push(globalconf
.L
, c
);
163 tag_push(globalconf
.L
, t
);
164 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
168 /** Untag a client with specified tag.
169 * \param c the client to tag
170 * \param t the tag to tag the client with
173 untag_client(client_t
*c
, tag_t
*t
)
175 for(int i
= 0; i
< t
->clients
.len
; i
++)
176 if(t
->clients
.tab
[i
] == c
)
178 client_need_reban(c
);
179 client_array_take(&t
->clients
, i
);
180 client_need_reban(c
);
181 ewmh_client_update_desktop(c
);
183 if(globalconf
.hooks
.tagged
!= LUA_REFNIL
)
185 client_push(globalconf
.L
, c
);
186 tag_push(globalconf
.L
, t
);
187 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
189 tag_unref(globalconf
.L
, t
);
194 /** Check if a client is tagged with the specified tag.
195 * \param c the client
197 * \return true if the client is tagged with the tag, false otherwise.
200 is_client_tagged(client_t
*c
, tag_t
*t
)
202 for(int i
= 0; i
< t
->clients
.len
; i
++)
203 if(t
->clients
.tab
[i
] == c
)
209 /** Get the index of the first selected tag.
210 * \param screen Screen.
214 tags_get_first_selected_index(screen_t
*screen
)
216 foreach(tag
, screen
->tags
)
218 return tag_array_indexof(&screen
->tags
, tag
);
222 /** Set a tag to be the only one viewed.
223 * \param target the tag to see
226 tag_view_only(tag_t
*target
)
229 foreach(tag
, target
->screen
->tags
)
230 tag_view(*tag
, *tag
== target
);
233 /** View only a tag, selected by its index.
234 * \param screen Screen.
235 * \param dindex The index.
238 tag_view_only_byindex(screen_t
*screen
, int dindex
)
240 tag_array_t
*tags
= &screen
->tags
;
242 if(dindex
< 0 || dindex
>= tags
->len
)
244 tag_view_only(tags
->tab
[dindex
]);
247 /** Create a new tag.
248 * \param L The Lua VM state.
251 * \lreturn A new tag object.
254 luaA_tag_new(lua_State
*L
)
257 const char *name
= luaL_checklstring(L
, 2, &len
);
258 tag_t
*tag
= tag_new(globalconf
.L
);
260 a_iso2utf8(name
, len
, &tag
->name
, NULL
);
265 /** Get or set the clients attached to this tag.
266 * \param L The Lua VM state.
267 * \return The number of elements pushed on stack.
269 * \lparam None or a table of clients to set.
270 * \lreturn A table with the clients attached to this tags.
273 luaA_tag_clients(lua_State
*L
)
275 tag_t
*tag
= luaL_checkudata(L
, 1, "tag");
276 client_array_t
*clients
= &tag
->clients
;
279 if(lua_gettop(L
) == 2)
281 luaA_checktable(L
, 2);
282 foreach(c
, tag
->clients
)
283 untag_client(*c
, tag
);
285 while(lua_next(L
, 2))
287 client_t
*c
= luaA_client_checkudata(L
, -1);
288 /* push tag on top of the stack */
295 lua_createtable(L
, clients
->len
, 0);
296 for(i
= 0; i
< clients
->len
; i
++)
298 client_push(L
, clients
->tab
[i
]);
299 lua_rawseti(L
, -2, i
+ 1);
306 * \param L The Lua VM state.
307 * \return The number of elements pushed on stack.
309 * \lfield name Tag name.
310 * \lfield screen Screen number of the tag.
311 * \lfield layout Tag layout.
312 * \lfield selected True if the client is selected to be viewed.
315 luaA_tag_index(lua_State
*L
)
318 tag_t
*tag
= luaL_checkudata(L
, 1, "tag");
321 if(luaA_usemetatable(L
, 1, 2))
324 attr
= luaL_checklstring(L
, 2, &len
);
326 switch(a_tokenize(attr
, len
))
329 lua_pushstring(L
, tag
->name
);
334 lua_pushnumber(L
, screen_array_indexof(&globalconf
.screens
, tag
->screen
) + 1);
337 lua_pushboolean(L
, tag
->selected
);
347 * \param L The Lua VM state.
348 * \return The number of elements pushed on stack.
351 luaA_tag_newindex(lua_State
*L
)
354 tag_t
*tag
= luaL_checkudata(L
, 1, "tag");
355 const char *attr
= luaL_checklstring(L
, 2, &len
);
357 switch(a_tokenize(attr
, len
))
363 const char *buf
= luaL_checklstring(L
, 3, &len
);
364 p_delete(&tag
->name
);
365 a_iso2utf8(buf
, len
, &tag
->name
, NULL
);
371 screen
= luaL_checknumber(L
, 3) - 1;
372 luaA_checkscreen(screen
);
378 tag_remove_from_screen(tag
);
382 /* push tag on top of the stack */
384 tag_append_to_screen(&globalconf
.screens
.tab
[screen
]);
389 tag_view(tag
, luaA_checkboolean(L
, 3));
398 const struct luaL_reg awesome_tag_methods
[] =
400 LUA_CLASS_METHODS(tag
)
401 { "__call", luaA_tag_new
},
404 const struct luaL_reg awesome_tag_meta
[] =
407 { "clients", luaA_tag_clients
},
408 { "__index", luaA_tag_index
},
409 { "__newindex", luaA_tag_newindex
},
410 { "__gc", luaA_tag_gc
},
414 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80