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.
37 /** true if selected */
39 /** clients in this tag */
40 client_array_t clients
;
43 static lua_class_t tag_class
;
44 LUA_OBJECT_FUNCS(tag_class
, tag_t
, tag
)
48 tag_unref_simplified(tag_t
**tag
)
50 luaA_object_unref(globalconf
.L
, *tag
);
53 /** Garbage collect a tag.
54 * \param L The Lua VM state.
58 luaA_tag_gc(lua_State
*L
)
60 tag_t
*tag
= luaA_checkudata(L
, 1, &tag_class
);
61 client_array_wipe(&tag
->clients
);
63 return luaA_object_gc(L
);
66 OBJECT_EXPORT_PROPERTY(tag
, tag_t
, selected
)
67 OBJECT_EXPORT_PROPERTY(tag
, tag_t
, name
)
69 /** View or unview a tag.
70 * \param L The Lua VM state.
71 * \param udx The index of the tag on the stack.
72 * \param view Set visible or not.
75 tag_view(lua_State
*L
, int udx
, bool view
)
77 tag_t
*tag
= luaA_checkudata(L
, udx
, &tag_class
);
78 if(tag
->selected
!= view
)
84 int screen_index
= screen_array_indexof(&globalconf
.screens
, tag
->screen
);
86 banning_need_update(tag
->screen
);
88 ewmh_update_net_current_desktop(screen_virttophys(screen_index
));
90 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
92 lua_pushnumber(globalconf
.L
, screen_index
+ 1);
93 luaA_object_push(globalconf
.L
, tag
);
95 lua_pushliteral(globalconf
.L
, "select");
97 lua_pushliteral(globalconf
.L
, "unselect");
98 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
102 luaA_object_emit_signal(L
, udx
, "property::selected", 0);
106 /** Append a tag to a screen.
107 * \param L The Lua VM state.
108 * \param udx The tag index on the stack.
109 * \param s The screen.
112 tag_append_to_screen(lua_State
*L
, int udx
, screen_t
*s
)
114 tag_t
*tag
= luaA_checkudata(globalconf
.L
, udx
, &tag_class
);
116 /* can't attach a tag twice */
123 int screen_index
= screen_array_indexof(&globalconf
.screens
, s
);
124 int phys_screen
= screen_virttophys(screen_index
);
127 tag_array_append(&s
->tags
, luaA_object_ref_class(globalconf
.L
, udx
, &tag_class
));
128 ewmh_update_net_numbers_of_desktop(phys_screen
);
129 ewmh_update_net_desktop_names(phys_screen
);
131 luaA_object_push(globalconf
.L
, tag
);
132 luaA_object_emit_signal(L
, -1, "property::screen", 0);
136 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
138 lua_pushnumber(globalconf
.L
, screen_index
+ 1);
139 luaA_object_push(globalconf
.L
, tag
);
140 lua_pushliteral(globalconf
.L
, "add");
141 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
144 luaA_object_push(globalconf
.L
, tag
);
145 screen_emit_signal(globalconf
.L
, s
, "tag::attach", 1);
148 /** Remove a tag from screen. Tag must be on a screen and have no clients.
149 * \param tag The tag to remove.
152 tag_remove_from_screen(tag_t
*tag
)
157 int screen_index
= screen_array_indexof(&globalconf
.screens
, tag
->screen
);
158 int phys_screen
= screen_virttophys(screen_index
);
159 tag_array_t
*tags
= &tag
->screen
->tags
;
161 for(int i
= 0; i
< tags
->len
; i
++)
162 if(tags
->tab
[i
] == tag
)
164 tag_array_take(tags
, i
);
168 /* tag was selected? If so, reban */
170 banning_need_update(tag
->screen
);
172 ewmh_update_net_numbers_of_desktop(phys_screen
);
173 ewmh_update_net_desktop_names(phys_screen
);
176 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
178 lua_pushnumber(globalconf
.L
, screen_index
+ 1);
179 luaA_object_push(globalconf
.L
, tag
);
180 lua_pushliteral(globalconf
.L
, "remove");
181 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
184 screen_t
*s
= tag
->screen
;
187 luaA_object_push(globalconf
.L
, tag
);
188 luaA_object_emit_signal(globalconf
.L
, -1, "property::screen", 0);
189 screen_emit_signal(globalconf
.L
, s
, "tag::detach", 1);
191 luaA_object_unref(globalconf
.L
, tag
);
195 tag_client_emit_signal(lua_State
*L
, tag_t
*t
, client_t
*c
, const char *signame
)
197 luaA_object_push(L
, c
);
198 luaA_object_push(L
, t
);
199 /* emit signal on client, with new tag as argument */
200 luaA_object_emit_signal(L
, -2, signame
, 1);
202 luaA_object_push(L
, t
);
203 /* move tag before client */
205 luaA_object_emit_signal(L
, -2, signame
, 1);
210 /** Tag a client with the tag on top of the stack.
211 * \param c the client to tag
214 tag_client(client_t
*c
)
216 tag_t
*t
= luaA_object_ref_class(globalconf
.L
, -1, &tag_class
);
218 /* don't tag twice */
219 if(is_client_tagged(c
, t
))
221 luaA_object_unref(globalconf
.L
, t
);
225 client_array_append(&t
->clients
, c
);
226 ewmh_client_update_desktop(c
);
227 banning_need_update((c
)->screen
);
230 if(globalconf
.hooks
.tagged
!= LUA_REFNIL
)
232 luaA_object_push(globalconf
.L
, c
);
233 luaA_object_push(globalconf
.L
, t
);
234 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
236 tag_client_emit_signal(globalconf
.L
, t
, c
, "tagged");
239 /** Untag a client with specified tag.
240 * \param c the client to tag
241 * \param t the tag to tag the client with
244 untag_client(client_t
*c
, tag_t
*t
)
246 for(int i
= 0; i
< t
->clients
.len
; i
++)
247 if(t
->clients
.tab
[i
] == c
)
249 client_array_take(&t
->clients
, i
);
250 banning_need_update((c
)->screen
);
251 ewmh_client_update_desktop(c
);
253 if(globalconf
.hooks
.tagged
!= LUA_REFNIL
)
255 luaA_object_push(globalconf
.L
, c
);
256 luaA_object_push(globalconf
.L
, t
);
257 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
259 tag_client_emit_signal(globalconf
.L
, t
, c
, "untagged");
260 luaA_object_unref(globalconf
.L
, t
);
265 /** Check if a client is tagged with the specified tag.
266 * \param c the client
268 * \return true if the client is tagged with the tag, false otherwise.
271 is_client_tagged(client_t
*c
, tag_t
*t
)
273 for(int i
= 0; i
< t
->clients
.len
; i
++)
274 if(t
->clients
.tab
[i
] == c
)
280 /** Get the index of the first selected tag.
281 * \param screen Screen.
285 tags_get_first_selected_index(screen_t
*screen
)
287 foreach(tag
, screen
->tags
)
289 return tag_array_indexof(&screen
->tags
, tag
);
293 /** Set a tag to be the only one viewed.
294 * \param target the tag to see
297 tag_view_only(tag_t
*target
)
300 foreach(tag
, target
->screen
->tags
)
302 luaA_object_push(globalconf
.L
, *tag
);
303 tag_view(globalconf
.L
, -1, *tag
== target
);
304 lua_pop(globalconf
.L
, 1);
308 /** View only a tag, selected by its index.
309 * \param screen Screen.
310 * \param dindex The index.
313 tag_view_only_byindex(screen_t
*screen
, int dindex
)
315 tag_array_t
*tags
= &screen
->tags
;
317 if(dindex
< 0 || dindex
>= tags
->len
)
319 tag_view_only(tags
->tab
[dindex
]);
322 /** Create a new tag.
323 * \param L The Lua VM state.
326 * \lreturn A new tag object.
329 luaA_tag_new(lua_State
*L
)
331 if(lua_isstring(L
, 2))
334 luaA_deprecate(L
, "new syntax");
337 const char *name
= luaL_checklstring(L
, 2, &len
);
338 tag_t
*tag
= tag_new(globalconf
.L
);
340 a_iso2utf8(name
, len
, &tag
->name
, NULL
);
345 return luaA_class_new(L
, &tag_class
);
348 /** Get or set the clients attached to this tag.
349 * \param L The Lua VM state.
350 * \return The number of elements pushed on stack.
352 * \lparam None or a table of clients to set.
353 * \lreturn A table with the clients attached to this tags.
356 luaA_tag_clients(lua_State
*L
)
358 tag_t
*tag
= luaA_checkudata(L
, 1, &tag_class
);
359 client_array_t
*clients
= &tag
->clients
;
362 if(lua_gettop(L
) == 2)
364 luaA_checktable(L
, 2);
365 foreach(c
, tag
->clients
)
366 untag_client(*c
, tag
);
368 while(lua_next(L
, 2))
370 client_t
*c
= luaA_client_checkudata(L
, -1);
371 /* push tag on top of the stack */
378 lua_createtable(L
, clients
->len
, 0);
379 for(i
= 0; i
< clients
->len
; i
++)
381 luaA_object_push(L
, clients
->tab
[i
]);
382 lua_rawseti(L
, -2, i
+ 1);
388 LUA_OBJECT_EXPORT_PROPERTY(tag
, tag_t
, name
, lua_pushstring
)
389 LUA_OBJECT_EXPORT_PROPERTY(tag
, tag_t
, selected
, lua_pushboolean
)
391 /** Set the tag name.
392 * \param L The Lua VM state.
393 * \param tag The tag to name.
394 * \return The number of elements pushed on stack.
397 luaA_tag_set_name(lua_State
*L
, tag_t
*tag
)
400 const char *buf
= luaL_checklstring(L
, -1, &len
);
401 p_delete(&tag
->name
);
402 a_iso2utf8(buf
, len
, &tag
->name
, NULL
);
403 luaA_object_emit_signal(L
, -3, "property::name", 0);
407 /** Set the tag selection status.
408 * \param L The Lua VM state.
409 * \param tag The tag to set the selection status for.
410 * \return The number of elements pushed on stack.
413 luaA_tag_set_selected(lua_State
*L
, tag_t
*tag
)
415 tag_view(L
, -3, luaA_checkboolean(L
, -1));
419 /** Set the tag screen.
420 * \param L The Lua VM state.
421 * \param tag The tag to set the screen for.
422 * \return The number of elements pushed on stack.
425 luaA_tag_set_screen(lua_State
*L
, tag_t
*tag
)
432 screen
= luaL_checknumber(L
, -1) - 1;
433 luaA_checkscreen(screen
);
436 tag_remove_from_screen(tag
);
439 tag_append_to_screen(L
, -3, &globalconf
.screens
.tab
[screen
]);
444 /** Get the tag screen.
445 * \param L The Lua VM state.
446 * \param tag The tag to get the screen for.
447 * \return The number of elements pushed on stack.
450 luaA_tag_get_screen(lua_State
*L
, tag_t
*tag
)
454 lua_pushnumber(L
, screen_array_indexof(&globalconf
.screens
, tag
->screen
) + 1);
459 tag_class_setup(lua_State
*L
)
461 static const struct luaL_reg tag_methods
[] =
463 LUA_CLASS_METHODS(tag
)
464 { "__call", luaA_tag_new
},
468 static const struct luaL_reg tag_meta
[] =
472 { "clients", luaA_tag_clients
},
473 { "__gc", luaA_tag_gc
},
477 luaA_class_setup(L
, &tag_class
, "tag", (lua_class_allocator_t
) tag_new
,
478 luaA_class_index_miss_property
, luaA_class_newindex_miss_property
,
479 tag_methods
, tag_meta
);
480 luaA_class_add_property(&tag_class
, A_TK_NAME
,
481 (lua_class_propfunc_t
) luaA_tag_set_name
,
482 (lua_class_propfunc_t
) luaA_tag_get_name
,
483 (lua_class_propfunc_t
) luaA_tag_set_name
);
484 luaA_class_add_property(&tag_class
, A_TK_SCREEN
,
485 (lua_class_propfunc_t
) NULL
,
486 (lua_class_propfunc_t
) luaA_tag_get_screen
,
487 (lua_class_propfunc_t
) luaA_tag_set_screen
);
488 luaA_class_add_property(&tag_class
, A_TK_SELECTED
,
489 (lua_class_propfunc_t
) luaA_tag_set_selected
,
490 (lua_class_propfunc_t
) luaA_tag_get_selected
,
491 (lua_class_propfunc_t
) luaA_tag_set_selected
);
494 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80