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
);
130 ewmh_update_workarea(phys_screen
);
132 luaA_object_push(globalconf
.L
, tag
);
133 luaA_object_emit_signal(L
, -1, "property::screen", 0);
137 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
139 lua_pushnumber(globalconf
.L
, screen_index
+ 1);
140 luaA_object_push(globalconf
.L
, tag
);
141 lua_pushliteral(globalconf
.L
, "add");
142 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
145 luaA_object_push(globalconf
.L
, tag
);
146 screen_emit_signal(globalconf
.L
, s
, "tag::attach", 1);
149 /** Remove a tag from screen. Tag must be on a screen and have no clients.
150 * \param tag The tag to remove.
153 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
);
169 /* tag was selected? If so, reban */
171 banning_need_update(tag
->screen
);
173 ewmh_update_net_numbers_of_desktop(phys_screen
);
174 ewmh_update_net_desktop_names(phys_screen
);
175 ewmh_update_workarea(phys_screen
);
178 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
180 lua_pushnumber(globalconf
.L
, screen_index
+ 1);
181 luaA_object_push(globalconf
.L
, tag
);
182 lua_pushliteral(globalconf
.L
, "remove");
183 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
186 screen_t
*s
= tag
->screen
;
189 luaA_object_push(globalconf
.L
, tag
);
190 luaA_object_emit_signal(globalconf
.L
, -1, "property::screen", 0);
191 screen_emit_signal(globalconf
.L
, s
, "tag::detach", 1);
193 luaA_object_unref(globalconf
.L
, tag
);
197 tag_client_emit_signal(lua_State
*L
, tag_t
*t
, client_t
*c
, const char *signame
)
199 luaA_object_push(L
, c
);
200 luaA_object_push(L
, t
);
201 /* emit signal on client, with new tag as argument */
202 luaA_object_emit_signal(L
, -2, signame
, 1);
204 luaA_object_push(L
, t
);
205 /* move tag before client */
207 luaA_object_emit_signal(L
, -2, signame
, 1);
212 /** Tag a client with the tag on top of the stack.
213 * \param c the client to tag
216 tag_client(client_t
*c
)
218 tag_t
*t
= luaA_object_ref_class(globalconf
.L
, -1, &tag_class
);
220 /* don't tag twice */
221 if(is_client_tagged(c
, t
))
223 luaA_object_unref(globalconf
.L
, t
);
227 client_array_append(&t
->clients
, c
);
228 ewmh_client_update_desktop(c
);
229 banning_need_update((c
)->screen
);
232 if(globalconf
.hooks
.tagged
!= LUA_REFNIL
)
234 luaA_object_push(globalconf
.L
, c
);
235 luaA_object_push(globalconf
.L
, t
);
236 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
238 tag_client_emit_signal(globalconf
.L
, t
, c
, "tagged");
241 /** Untag a client with specified tag.
242 * \param c the client to tag
243 * \param t the tag to tag the client with
246 untag_client(client_t
*c
, tag_t
*t
)
248 for(int i
= 0; i
< t
->clients
.len
; i
++)
249 if(t
->clients
.tab
[i
] == c
)
251 client_array_take(&t
->clients
, i
);
252 banning_need_update((c
)->screen
);
253 ewmh_client_update_desktop(c
);
255 if(globalconf
.hooks
.tagged
!= LUA_REFNIL
)
257 luaA_object_push(globalconf
.L
, c
);
258 luaA_object_push(globalconf
.L
, t
);
259 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
261 tag_client_emit_signal(globalconf
.L
, t
, c
, "untagged");
262 luaA_object_unref(globalconf
.L
, t
);
267 /** Check if a client is tagged with the specified tag.
268 * \param c the client
270 * \return true if the client is tagged with the tag, false otherwise.
273 is_client_tagged(client_t
*c
, tag_t
*t
)
275 for(int i
= 0; i
< t
->clients
.len
; i
++)
276 if(t
->clients
.tab
[i
] == c
)
282 /** Get the index of the first selected tag.
283 * \param screen Screen.
287 tags_get_first_selected_index(screen_t
*screen
)
289 foreach(tag
, screen
->tags
)
291 return tag_array_indexof(&screen
->tags
, tag
);
295 /** Set a tag to be the only one viewed.
296 * \param target the tag to see
299 tag_view_only(tag_t
*target
)
302 foreach(tag
, target
->screen
->tags
)
304 luaA_object_push(globalconf
.L
, *tag
);
305 tag_view(globalconf
.L
, -1, *tag
== target
);
306 lua_pop(globalconf
.L
, 1);
310 /** View only a tag, selected by its index.
311 * \param screen Screen.
312 * \param dindex The index.
315 tag_view_only_byindex(screen_t
*screen
, int dindex
)
317 tag_array_t
*tags
= &screen
->tags
;
319 if(dindex
< 0 || dindex
>= tags
->len
)
321 tag_view_only(tags
->tab
[dindex
]);
324 /** Create a new tag.
325 * \param L The Lua VM state.
328 * \lreturn A new tag object.
331 luaA_tag_new(lua_State
*L
)
333 if(lua_isstring(L
, 2))
336 luaA_deprecate(L
, "new syntax");
339 const char *name
= luaL_checklstring(L
, 2, &len
);
340 tag_t
*tag
= tag_new(globalconf
.L
);
342 a_iso2utf8(name
, len
, &tag
->name
, NULL
);
347 return luaA_class_new(L
, &tag_class
);
350 /** Get or set the clients attached to this tag.
351 * \param L The Lua VM state.
352 * \return The number of elements pushed on stack.
354 * \lparam None or a table of clients to set.
355 * \lreturn A table with the clients attached to this tags.
358 luaA_tag_clients(lua_State
*L
)
360 tag_t
*tag
= luaA_checkudata(L
, 1, &tag_class
);
361 client_array_t
*clients
= &tag
->clients
;
364 if(lua_gettop(L
) == 2)
366 luaA_checktable(L
, 2);
367 foreach(c
, tag
->clients
)
368 untag_client(*c
, tag
);
370 while(lua_next(L
, 2))
372 client_t
*c
= luaA_client_checkudata(L
, -1);
373 /* push tag on top of the stack */
380 lua_createtable(L
, clients
->len
, 0);
381 for(i
= 0; i
< clients
->len
; i
++)
383 luaA_object_push(L
, clients
->tab
[i
]);
384 lua_rawseti(L
, -2, i
+ 1);
390 LUA_OBJECT_EXPORT_PROPERTY(tag
, tag_t
, name
, lua_pushstring
)
391 LUA_OBJECT_EXPORT_PROPERTY(tag
, tag_t
, selected
, lua_pushboolean
)
393 /** Set the tag name.
394 * \param L The Lua VM state.
395 * \param tag The tag to name.
396 * \return The number of elements pushed on stack.
399 luaA_tag_set_name(lua_State
*L
, tag_t
*tag
)
402 const char *buf
= luaL_checklstring(L
, -1, &len
);
403 p_delete(&tag
->name
);
404 a_iso2utf8(buf
, len
, &tag
->name
, NULL
);
405 luaA_object_emit_signal(L
, -3, "property::name", 0);
409 /** Set the tag selection status.
410 * \param L The Lua VM state.
411 * \param tag The tag to set the selection status for.
412 * \return The number of elements pushed on stack.
415 luaA_tag_set_selected(lua_State
*L
, tag_t
*tag
)
417 tag_view(L
, -3, luaA_checkboolean(L
, -1));
421 /** Set the tag screen.
422 * \param L The Lua VM state.
423 * \param tag The tag to set the screen for.
424 * \return The number of elements pushed on stack.
427 luaA_tag_set_screen(lua_State
*L
, tag_t
*tag
)
434 screen
= luaL_checknumber(L
, -1) - 1;
435 luaA_checkscreen(screen
);
438 tag_remove_from_screen(tag
);
441 tag_append_to_screen(L
, -3, &globalconf
.screens
.tab
[screen
]);
446 /** Get the tag screen.
447 * \param L The Lua VM state.
448 * \param tag The tag to get the screen for.
449 * \return The number of elements pushed on stack.
452 luaA_tag_get_screen(lua_State
*L
, tag_t
*tag
)
456 lua_pushnumber(L
, screen_array_indexof(&globalconf
.screens
, tag
->screen
) + 1);
461 tag_class_setup(lua_State
*L
)
463 static const struct luaL_reg tag_methods
[] =
465 LUA_CLASS_METHODS(tag
)
466 { "__call", luaA_tag_new
},
470 static const struct luaL_reg tag_meta
[] =
474 { "clients", luaA_tag_clients
},
475 { "__gc", luaA_tag_gc
},
479 luaA_class_setup(L
, &tag_class
, "tag", (lua_class_allocator_t
) tag_new
,
480 luaA_class_index_miss_property
, luaA_class_newindex_miss_property
,
481 tag_methods
, tag_meta
);
482 luaA_class_add_property(&tag_class
, A_TK_NAME
,
483 (lua_class_propfunc_t
) luaA_tag_set_name
,
484 (lua_class_propfunc_t
) luaA_tag_get_name
,
485 (lua_class_propfunc_t
) luaA_tag_set_name
);
486 luaA_class_add_property(&tag_class
, A_TK_SCREEN
,
487 (lua_class_propfunc_t
) NULL
,
488 (lua_class_propfunc_t
) luaA_tag_get_screen
,
489 (lua_class_propfunc_t
) luaA_tag_set_screen
);
490 luaA_class_add_property(&tag_class
, A_TK_SELECTED
,
491 (lua_class_propfunc_t
) luaA_tag_set_selected
,
492 (lua_class_propfunc_t
) luaA_tag_get_selected
,
493 (lua_class_propfunc_t
) luaA_tag_set_selected
);
496 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80