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 /** Create a new tag.
165 * \param L The Lua VM state.
168 * \lreturn A new tag object.
171 luaA_tag_new(lua_State
*L
)
173 return luaA_class_new(L
, &tag_class
);
176 /** Get or set the clients attached to this tag.
177 * \param L The Lua VM state.
178 * \return The number of elements pushed on stack.
180 * \lparam None or a table of clients to set.
181 * \lreturn A table with the clients attached to this tags.
184 luaA_tag_clients(lua_State
*L
)
186 tag_t
*tag
= luaA_checkudata(L
, 1, &tag_class
);
187 client_array_t
*clients
= &tag
->clients
;
190 if(lua_gettop(L
) == 2)
192 luaA_checktable(L
, 2);
193 foreach(c
, tag
->clients
)
195 /* Only untag if we aren't going to add this tag again */
198 while(lua_next(L
, 2))
200 client_t
*tc
= luaA_checkudata(L
, -1, &client_class
);
201 /* Pop the value from lua_next */
206 /* Pop the key from lua_next */
212 untag_client(*c
, tag
);
215 while(lua_next(L
, 2))
217 client_t
*c
= luaA_checkudata(L
, -1, &client_class
);
218 /* push tag on top of the stack */
225 lua_createtable(L
, clients
->len
, 0);
226 for(i
= 0; i
< clients
->len
; i
++)
228 luaA_object_push(L
, clients
->tab
[i
]);
229 lua_rawseti(L
, -2, i
+ 1);
235 LUA_OBJECT_EXPORT_PROPERTY(tag
, tag_t
, name
, lua_pushstring
)
236 LUA_OBJECT_EXPORT_PROPERTY(tag
, tag_t
, selected
, lua_pushboolean
)
237 LUA_OBJECT_EXPORT_PROPERTY(tag
, tag_t
, activated
, lua_pushboolean
)
239 /** Set the tag name.
240 * \param L The Lua VM state.
241 * \param tag The tag to name.
242 * \return The number of elements pushed on stack.
245 luaA_tag_set_name(lua_State
*L
, tag_t
*tag
)
248 const char *buf
= luaL_checklstring(L
, -1, &len
);
249 p_delete(&tag
->name
);
250 a_iso2utf8(buf
, len
, &tag
->name
, NULL
);
251 luaA_object_emit_signal(L
, -3, "property::name", 0);
252 ewmh_update_net_desktop_names();
256 /** Set the tag selection status.
257 * \param L The Lua VM state.
258 * \param tag The tag to set the selection status for.
259 * \return The number of elements pushed on stack.
262 luaA_tag_set_selected(lua_State
*L
, tag_t
*tag
)
264 tag_view(L
, -3, luaA_checkboolean(L
, -1));
268 /** Set the tag activated status.
269 * \param L The Lua VM state.
270 * \param tag The tag to set the activated status for.
271 * \return The number of elements pushed on stack.
274 luaA_tag_set_activated(lua_State
*L
, tag_t
*tag
)
276 bool activated
= luaA_checkboolean(L
, -1);
277 if(activated
== tag
->activated
)
280 tag
->activated
= activated
;
283 lua_pushvalue(L
, -3);
284 tag_array_append(&globalconf
.tags
, luaA_object_ref_class(L
, -1, &tag_class
));
288 for (int i
= 0; i
< globalconf
.tags
.len
; i
++)
289 if(globalconf
.tags
.tab
[i
] == tag
)
291 tag_array_take(&globalconf
.tags
, i
);
297 tag
->selected
= false;
298 luaA_object_emit_signal(L
, -3, "property::selected", 0);
299 banning_need_update();
301 luaA_object_unref(L
, tag
);
303 ewmh_update_net_numbers_of_desktop();
304 ewmh_update_net_desktop_names();
306 luaA_object_emit_signal(L
, -3, "property::activated", 0);
312 tag_class_setup(lua_State
*L
)
314 static const struct luaL_Reg tag_methods
[] =
316 LUA_CLASS_METHODS(tag
)
317 { "__call", luaA_tag_new
},
321 static const struct luaL_Reg tag_meta
[] =
325 { "clients", luaA_tag_clients
},
329 luaA_class_setup(L
, &tag_class
, "tag", NULL
,
330 (lua_class_allocator_t
) tag_new
,
331 (lua_class_collector_t
) tag_wipe
,
333 luaA_class_index_miss_property
, luaA_class_newindex_miss_property
,
334 tag_methods
, tag_meta
);
335 luaA_class_add_property(&tag_class
, "name",
336 (lua_class_propfunc_t
) luaA_tag_set_name
,
337 (lua_class_propfunc_t
) luaA_tag_get_name
,
338 (lua_class_propfunc_t
) luaA_tag_set_name
);
339 luaA_class_add_property(&tag_class
, "selected",
340 (lua_class_propfunc_t
) luaA_tag_set_selected
,
341 (lua_class_propfunc_t
) luaA_tag_get_selected
,
342 (lua_class_propfunc_t
) luaA_tag_set_selected
);
343 luaA_class_add_property(&tag_class
, "activated",
344 (lua_class_propfunc_t
) luaA_tag_set_activated
,
345 (lua_class_propfunc_t
) luaA_tag_get_activated
,
346 (lua_class_propfunc_t
) luaA_tag_set_activated
);
348 signal_add(&tag_class
.signals
, "property::name");
349 signal_add(&tag_class
.signals
, "property::selected");
350 signal_add(&tag_class
.signals
, "property::activated");
351 signal_add(&tag_class
.signals
, "request::select");
352 signal_add(&tag_class
.signals
, "tagged");
353 signal_add(&tag_class
.signals
, "untagged");
356 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80