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.
33 /** true if activated */
35 /** true if selected */
37 /** clients in this tag */
38 client_array_t clients
;
41 static lua_class_t tag_class
;
42 LUA_OBJECT_FUNCS(tag_class
, tag_t
, tag
)
46 tag_unref_simplified(tag_t
**tag
)
48 luaA_object_unref(globalconf
.L
, *tag
);
54 client_array_wipe(&tag
->clients
);
58 OBJECT_EXPORT_PROPERTY(tag
, tag_t
, selected
)
59 OBJECT_EXPORT_PROPERTY(tag
, tag_t
, name
)
61 /** View or unview a tag.
62 * \param L The Lua VM state.
63 * \param udx The index of the tag on the stack.
64 * \param view Set selected or not.
67 tag_view(lua_State
*L
, int udx
, bool view
)
69 tag_t
*tag
= luaA_checkudata(L
, udx
, &tag_class
);
70 if(tag
->selected
!= view
)
73 banning_need_update();
74 ewmh_update_net_current_desktop();
76 luaA_object_emit_signal(L
, udx
, "property::selected", 0);
81 tag_client_emit_signal(lua_State
*L
, tag_t
*t
, client_t
*c
, const char *signame
)
83 luaA_object_push(L
, c
);
84 luaA_object_push(L
, t
);
85 /* emit signal on client, with new tag as argument */
86 luaA_object_emit_signal(L
, -2, signame
, 1);
88 luaA_object_push(L
, t
);
89 /* move tag before client */
91 luaA_object_emit_signal(L
, -2, signame
, 1);
96 /** Tag a client with the tag on top of the stack.
97 * \param c the client to tag
100 tag_client(client_t
*c
)
102 tag_t
*t
= luaA_object_ref_class(globalconf
.L
, -1, &tag_class
);
104 /* don't tag twice */
105 if(is_client_tagged(c
, t
))
107 luaA_object_unref(globalconf
.L
, t
);
111 client_array_append(&t
->clients
, c
);
112 ewmh_client_update_desktop(c
);
113 banning_need_update();
115 tag_client_emit_signal(globalconf
.L
, t
, c
, "tagged");
118 /** Untag a client with specified tag.
119 * \param c the client to tag
120 * \param t the tag to tag the client with
123 untag_client(client_t
*c
, tag_t
*t
)
125 for(int i
= 0; i
< t
->clients
.len
; i
++)
126 if(t
->clients
.tab
[i
] == c
)
128 client_array_take(&t
->clients
, i
);
129 banning_need_update();
130 ewmh_client_update_desktop(c
);
131 tag_client_emit_signal(globalconf
.L
, t
, c
, "untagged");
132 luaA_object_unref(globalconf
.L
, t
);
137 /** Check if a client is tagged with the specified tag.
138 * \param c the client
140 * \return true if the client is tagged with the tag, false otherwise.
143 is_client_tagged(client_t
*c
, tag_t
*t
)
145 for(int i
= 0; i
< t
->clients
.len
; i
++)
146 if(t
->clients
.tab
[i
] == c
)
152 /** Get the index of the first selected tag.
156 tags_get_first_selected_index(void)
158 foreach(tag
, globalconf
.tags
)
160 return tag_array_indexof(&globalconf
.tags
, tag
);
164 /** View only a tag, selected by its index.
165 * \param dindex The index.
168 tag_view_only_byindex(int dindex
)
170 if(dindex
< 0 || dindex
>= globalconf
.tags
.len
)
173 foreach(tag
, globalconf
.tags
)
175 luaA_object_push(globalconf
.L
, *tag
);
176 tag_view(globalconf
.L
, -1, *tag
== globalconf
.tags
.tab
[dindex
]);
177 lua_pop(globalconf
.L
, 1);
181 /** Create a new tag.
182 * \param L The Lua VM state.
185 * \lreturn A new tag object.
188 luaA_tag_new(lua_State
*L
)
190 return luaA_class_new(L
, &tag_class
);
193 /** Get or set the clients attached to this tag.
194 * \param L The Lua VM state.
195 * \return The number of elements pushed on stack.
197 * \lparam None or a table of clients to set.
198 * \lreturn A table with the clients attached to this tags.
201 luaA_tag_clients(lua_State
*L
)
203 tag_t
*tag
= luaA_checkudata(L
, 1, &tag_class
);
204 client_array_t
*clients
= &tag
->clients
;
207 if(lua_gettop(L
) == 2)
209 luaA_checktable(L
, 2);
210 foreach(c
, tag
->clients
)
212 /* Only untag if we aren't going to add this tag again */
215 while(lua_next(L
, 2))
217 client_t
*tc
= luaA_checkudata(L
, -1, &client_class
);
218 /* Pop the value from lua_next */
223 /* Pop the key from lua_next */
229 untag_client(*c
, tag
);
232 while(lua_next(L
, 2))
234 client_t
*c
= luaA_checkudata(L
, -1, &client_class
);
235 /* push tag on top of the stack */
242 lua_createtable(L
, clients
->len
, 0);
243 for(i
= 0; i
< clients
->len
; i
++)
245 luaA_object_push(L
, clients
->tab
[i
]);
246 lua_rawseti(L
, -2, i
+ 1);
252 LUA_OBJECT_EXPORT_PROPERTY(tag
, tag_t
, name
, lua_pushstring
)
253 LUA_OBJECT_EXPORT_PROPERTY(tag
, tag_t
, selected
, lua_pushboolean
)
254 LUA_OBJECT_EXPORT_PROPERTY(tag
, tag_t
, activated
, lua_pushboolean
)
256 /** Set the tag name.
257 * \param L The Lua VM state.
258 * \param tag The tag to name.
259 * \return The number of elements pushed on stack.
262 luaA_tag_set_name(lua_State
*L
, tag_t
*tag
)
265 const char *buf
= luaL_checklstring(L
, -1, &len
);
266 p_delete(&tag
->name
);
267 a_iso2utf8(buf
, len
, &tag
->name
, NULL
);
268 luaA_object_emit_signal(L
, -3, "property::name", 0);
269 ewmh_update_net_desktop_names();
273 /** Set the tag selection status.
274 * \param L The Lua VM state.
275 * \param tag The tag to set the selection status for.
276 * \return The number of elements pushed on stack.
279 luaA_tag_set_selected(lua_State
*L
, tag_t
*tag
)
281 tag_view(L
, -3, luaA_checkboolean(L
, -1));
285 /** Set the tag activated status.
286 * \param L The Lua VM state.
287 * \param tag The tag to set the activated status for.
288 * \return The number of elements pushed on stack.
291 luaA_tag_set_activated(lua_State
*L
, tag_t
*tag
)
293 bool activated
= luaA_checkboolean(L
, -1);
294 if(activated
== tag
->activated
)
297 tag
->activated
= activated
;
300 lua_pushvalue(L
, -3);
301 tag_array_append(&globalconf
.tags
, luaA_object_ref_class(L
, -1, &tag_class
));
305 for (int i
= 0; i
< globalconf
.tags
.len
; i
++)
306 if(globalconf
.tags
.tab
[i
] == tag
)
308 tag_array_take(&globalconf
.tags
, i
);
314 tag
->selected
= false;
315 luaA_object_emit_signal(L
, -3, "property::selected", 0);
316 banning_need_update();
318 luaA_object_unref(L
, tag
);
320 ewmh_update_net_numbers_of_desktop();
321 ewmh_update_net_desktop_names();
323 luaA_object_emit_signal(L
, -3, "property::activated", 0);
329 tag_class_setup(lua_State
*L
)
331 static const struct luaL_Reg tag_methods
[] =
333 LUA_CLASS_METHODS(tag
)
334 { "__call", luaA_tag_new
},
338 static const struct luaL_Reg tag_meta
[] =
342 { "clients", luaA_tag_clients
},
346 luaA_class_setup(L
, &tag_class
, "tag", NULL
,
347 (lua_class_allocator_t
) tag_new
,
348 (lua_class_collector_t
) tag_wipe
,
350 luaA_class_index_miss_property
, luaA_class_newindex_miss_property
,
351 tag_methods
, tag_meta
);
352 luaA_class_add_property(&tag_class
, "name",
353 (lua_class_propfunc_t
) luaA_tag_set_name
,
354 (lua_class_propfunc_t
) luaA_tag_get_name
,
355 (lua_class_propfunc_t
) luaA_tag_set_name
);
356 luaA_class_add_property(&tag_class
, "selected",
357 (lua_class_propfunc_t
) luaA_tag_set_selected
,
358 (lua_class_propfunc_t
) luaA_tag_get_selected
,
359 (lua_class_propfunc_t
) luaA_tag_set_selected
);
360 luaA_class_add_property(&tag_class
, "activated",
361 (lua_class_propfunc_t
) luaA_tag_set_activated
,
362 (lua_class_propfunc_t
) luaA_tag_get_activated
,
363 (lua_class_propfunc_t
) luaA_tag_set_activated
);
365 signal_add(&tag_class
.signals
, "property::name");
366 signal_add(&tag_class
.signals
, "property::selected");
367 signal_add(&tag_class
.signals
, "property::activated");
368 signal_add(&tag_class
.signals
, "tagged");
369 signal_add(&tag_class
.signals
, "untagged");
372 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80