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
)
70 tag_set_screen(tag_t
*tag
, screen_t
*s
)
75 /** View or unview a tag.
76 * \param L The Lua VM state.
77 * \param udx The index of the tag on the stack.
78 * \param view Set visible or not.
81 tag_view(lua_State
*L
, int udx
, bool view
)
83 tag_t
*tag
= luaA_checkudata(L
, udx
, &tag_class
);
84 if(tag
->selected
!= view
)
88 luaA_object_emit_signal(L
, udx
, "property::selected", 0);
92 int screen_index
= screen_array_indexof(&globalconf
.screens
, tag
->screen
);
94 tag
->screen
->need_reban
= true;
96 ewmh_update_net_current_desktop(screen_virttophys(screen_index
));
98 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
100 lua_pushnumber(globalconf
.L
, screen_index
+ 1);
101 luaA_object_push(globalconf
.L
, tag
);
103 lua_pushliteral(globalconf
.L
, "select");
105 lua_pushliteral(globalconf
.L
, "unselect");
106 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
112 /** Append a tag to a screen.
113 * \param L The Lua VM state.
114 * \param udx The tag index on the stack.
115 * \param s The screen.
118 tag_append_to_screen(lua_State
*L
, int udx
, screen_t
*s
)
120 tag_t
*tag
= luaA_checkudata(globalconf
.L
, udx
, &tag_class
);
122 /* can't attach a tag twice */
126 int screen_index
= screen_array_indexof(&globalconf
.screens
, s
);
127 int phys_screen
= screen_virttophys(screen_index
);
130 tag_array_append(&s
->tags
, luaA_object_ref(globalconf
.L
, udx
));
131 ewmh_update_net_numbers_of_desktop(phys_screen
);
132 ewmh_update_net_desktop_names(phys_screen
);
133 ewmh_update_workarea(phys_screen
);
135 luaA_object_push(globalconf
.L
, tag
);
136 luaA_object_emit_signal(L
, -1, "property::screen", 0);
140 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
142 lua_pushnumber(globalconf
.L
, screen_index
+ 1);
143 luaA_object_push(globalconf
.L
, tag
);
144 lua_pushliteral(globalconf
.L
, "add");
145 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
148 luaA_object_push(globalconf
.L
, tag
);
149 screen_emit_signal(globalconf
.L
, s
, "tag::attach", 1);
152 /** Remove a tag from screen. Tag must be on a screen and have no clients.
153 * \param tag The tag to remove.
156 tag_remove_from_screen(tag_t
*tag
)
158 int screen_index
= screen_array_indexof(&globalconf
.screens
, tag
->screen
);
159 int phys_screen
= screen_virttophys(screen_index
);
160 tag_array_t
*tags
= &tag
->screen
->tags
;
162 for(int i
= 0; i
< tags
->len
; i
++)
163 if(tags
->tab
[i
] == tag
)
165 tag_array_take(tags
, i
);
168 ewmh_update_net_numbers_of_desktop(phys_screen
);
169 ewmh_update_net_desktop_names(phys_screen
);
170 ewmh_update_workarea(phys_screen
);
173 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
175 lua_pushnumber(globalconf
.L
, screen_index
+ 1);
176 luaA_object_push(globalconf
.L
, tag
);
177 lua_pushliteral(globalconf
.L
, "remove");
178 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
181 screen_t
*s
= tag
->screen
;
184 luaA_object_push(globalconf
.L
, tag
);
185 luaA_object_emit_signal(globalconf
.L
, -1, "property::screen", 0);
186 screen_emit_signal(globalconf
.L
, s
, "tag::detach", 1);
188 luaA_object_unref(globalconf
.L
, tag
);
192 tag_client_emit_signal(lua_State
*L
, tag_t
*t
, client_t
*c
, const char *signame
)
194 luaA_object_push(L
, c
);
195 luaA_object_push(L
, t
);
196 /* emit signal on client, with new tag as argument */
197 luaA_object_emit_signal(L
, -2, signame
, 1);
199 luaA_object_push(L
, t
);
200 /* move tag before client */
202 luaA_object_emit_signal(L
, -2, signame
, 1);
207 /** Tag a client with the tag on top of the stack.
208 * \param c the client to tag
211 tag_client(client_t
*c
)
213 tag_t
*t
= luaA_object_ref(globalconf
.L
, -1);
215 /* don't tag twice */
216 if(is_client_tagged(c
, t
))
218 luaA_object_unref(globalconf
.L
, t
);
222 client_array_append(&t
->clients
, c
);
223 ewmh_client_update_desktop(c
);
224 client_need_reban(c
);
227 if(globalconf
.hooks
.tagged
!= LUA_REFNIL
)
229 luaA_object_push(globalconf
.L
, c
);
230 luaA_object_push(globalconf
.L
, t
);
231 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
233 tag_client_emit_signal(globalconf
.L
, t
, c
, "tagged");
236 /** Untag a client with specified tag.
237 * \param c the client to tag
238 * \param t the tag to tag the client with
241 untag_client(client_t
*c
, tag_t
*t
)
243 for(int i
= 0; i
< t
->clients
.len
; i
++)
244 if(t
->clients
.tab
[i
] == c
)
246 client_need_reban(c
);
247 client_array_take(&t
->clients
, i
);
248 client_need_reban(c
);
249 ewmh_client_update_desktop(c
);
251 if(globalconf
.hooks
.tagged
!= LUA_REFNIL
)
253 luaA_object_push(globalconf
.L
, c
);
254 luaA_object_push(globalconf
.L
, t
);
255 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
257 tag_client_emit_signal(globalconf
.L
, t
, c
, "untagged");
258 luaA_object_unref(globalconf
.L
, t
);
263 /** Check if a client is tagged with the specified tag.
264 * \param c the client
266 * \return true if the client is tagged with the tag, false otherwise.
269 is_client_tagged(client_t
*c
, tag_t
*t
)
271 for(int i
= 0; i
< t
->clients
.len
; i
++)
272 if(t
->clients
.tab
[i
] == c
)
278 /** Get the index of the first selected tag.
279 * \param screen Screen.
283 tags_get_first_selected_index(screen_t
*screen
)
285 foreach(tag
, screen
->tags
)
287 return tag_array_indexof(&screen
->tags
, tag
);
291 /** Set a tag to be the only one viewed.
292 * \param target the tag to see
295 tag_view_only(tag_t
*target
)
298 foreach(tag
, target
->screen
->tags
)
300 luaA_object_push(globalconf
.L
, *tag
);
301 tag_view(globalconf
.L
, -1, *tag
== target
);
302 lua_pop(globalconf
.L
, 1);
306 /** View only a tag, selected by its index.
307 * \param screen Screen.
308 * \param dindex The index.
311 tag_view_only_byindex(screen_t
*screen
, int dindex
)
313 tag_array_t
*tags
= &screen
->tags
;
315 if(dindex
< 0 || dindex
>= tags
->len
)
317 tag_view_only(tags
->tab
[dindex
]);
320 /** Create a new tag.
321 * \param L The Lua VM state.
324 * \lreturn A new tag object.
327 luaA_tag_new(lua_State
*L
)
329 if(lua_isstring(L
, 2))
332 luaA_deprecate(L
, "new syntax");
335 const char *name
= luaL_checklstring(L
, 2, &len
);
336 tag_t
*tag
= tag_new(globalconf
.L
);
338 a_iso2utf8(name
, len
, &tag
->name
, NULL
);
343 return luaA_class_new(L
, &tag_class
);
346 /** Get or set the clients attached to this tag.
347 * \param L The Lua VM state.
348 * \return The number of elements pushed on stack.
350 * \lparam None or a table of clients to set.
351 * \lreturn A table with the clients attached to this tags.
354 luaA_tag_clients(lua_State
*L
)
356 tag_t
*tag
= luaA_checkudata(L
, 1, &tag_class
);
357 client_array_t
*clients
= &tag
->clients
;
360 if(lua_gettop(L
) == 2)
362 luaA_checktable(L
, 2);
363 foreach(c
, tag
->clients
)
364 untag_client(*c
, tag
);
366 while(lua_next(L
, 2))
368 client_t
*c
= luaA_client_checkudata(L
, -1);
369 /* push tag on top of the stack */
376 lua_createtable(L
, clients
->len
, 0);
377 for(i
= 0; i
< clients
->len
; i
++)
379 luaA_object_push(L
, clients
->tab
[i
]);
380 lua_rawseti(L
, -2, i
+ 1);
386 LUA_OBJECT_EXPORT_PROPERTY(tag
, tag_t
, name
, lua_pushstring
)
387 LUA_OBJECT_EXPORT_PROPERTY(tag
, tag_t
, selected
, lua_pushboolean
)
389 /** Set the tag name.
390 * \param L The Lua VM state.
391 * \param tag The tag to name.
392 * \return The number of elements pushed on stack.
395 luaA_tag_set_name(lua_State
*L
, tag_t
*tag
)
398 const char *buf
= luaL_checklstring(L
, -1, &len
);
399 p_delete(&tag
->name
);
400 a_iso2utf8(buf
, len
, &tag
->name
, NULL
);
401 luaA_object_emit_signal(L
, -3, "property::name", 0);
405 /** Set the tag selection status.
406 * \param L The Lua VM state.
407 * \param tag The tag to set the selection status for.
408 * \return The number of elements pushed on stack.
411 luaA_tag_set_selected(lua_State
*L
, tag_t
*tag
)
413 tag_view(L
, -3, luaA_checkboolean(L
, -1));
417 /** Set the tag screen.
418 * \param L The Lua VM state.
419 * \param tag The tag to set the screen for.
420 * \return The number of elements pushed on stack.
423 luaA_tag_set_screen(lua_State
*L
, tag_t
*tag
)
430 screen
= luaL_checknumber(L
, -1) - 1;
431 luaA_checkscreen(screen
);
435 tag_remove_from_screen(tag
);
438 tag_append_to_screen(L
, -3, &globalconf
.screens
.tab
[screen
]);
443 /** Get the tag screen.
444 * \param L The Lua VM state.
445 * \param tag The tag to get the screen for.
446 * \return The number of elements pushed on stack.
449 luaA_tag_get_screen(lua_State
*L
, tag_t
*tag
)
453 lua_pushnumber(L
, screen_array_indexof(&globalconf
.screens
, tag
->screen
) + 1);
458 tag_class_setup(lua_State
*L
)
460 static const struct luaL_reg tag_methods
[] =
462 LUA_CLASS_METHODS(tag
)
463 { "__call", luaA_tag_new
},
467 static const struct luaL_reg tag_meta
[] =
471 { "clients", luaA_tag_clients
},
472 { "__gc", luaA_tag_gc
},
476 luaA_class_setup(L
, &tag_class
, "tag", (lua_class_allocator_t
) tag_new
,
477 luaA_class_index_miss_property
, luaA_class_newindex_miss_property
,
478 tag_methods
, tag_meta
);
479 luaA_class_add_property(&tag_class
, A_TK_NAME
,
480 (lua_class_propfunc_t
) luaA_tag_set_name
,
481 (lua_class_propfunc_t
) luaA_tag_get_name
,
482 (lua_class_propfunc_t
) luaA_tag_set_name
);
483 luaA_class_add_property(&tag_class
, A_TK_SCREEN
,
484 (lua_class_propfunc_t
) NULL
,
485 (lua_class_propfunc_t
) luaA_tag_get_screen
,
486 (lua_class_propfunc_t
) luaA_tag_set_screen
);
487 luaA_class_add_property(&tag_class
, A_TK_SELECTED
,
488 (lua_class_propfunc_t
) luaA_tag_set_selected
,
489 (lua_class_propfunc_t
) luaA_tag_get_selected
,
490 (lua_class_propfunc_t
) luaA_tag_set_selected
);
493 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80