tag: Improve tag property::index support (FS#1229)
[awesome.git] / objects / tag.c
blob4d7f3cb06fa6b8217068642097baa4ade576fc3f
1 /*
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.
22 #include "tag.h"
23 #include "banning.h"
24 #include "client.h"
25 #include "ewmh.h"
26 #include "luaa.h"
28 /** Tag type */
29 struct tag
31 LUA_OBJECT_HEADER
32 /** Tag name */
33 char *name;
34 /** true if activated */
35 bool activated;
36 /** true if selected */
37 bool selected;
38 /** clients in this tag */
39 client_array_t clients;
42 static lua_class_t tag_class;
43 LUA_OBJECT_FUNCS(tag_class, tag_t, tag)
46 void
47 tag_unref_simplified(tag_t **tag)
49 luaA_object_unref(globalconf.L, *tag);
52 static void
53 tag_wipe(tag_t *tag)
55 client_array_wipe(&tag->clients);
56 p_delete(&tag->name);
59 OBJECT_EXPORT_PROPERTY(tag, tag_t, selected)
60 OBJECT_EXPORT_PROPERTY(tag, tag_t, name)
62 /** View or unview a tag.
63 * \param L The Lua VM state.
64 * \param udx The index of the tag on the stack.
65 * \param view Set selected or not.
67 static void
68 tag_view(lua_State *L, int udx, bool view)
70 tag_t *tag = luaA_checkudata(L, udx, &tag_class);
71 if(tag->selected != view)
73 tag->selected = view;
74 banning_need_update();
75 ewmh_update_net_current_desktop();
77 luaA_object_emit_signal(L, udx, "property::selected", 0);
81 static void
82 tag_client_emit_signal(lua_State *L, tag_t *t, client_t *c, const char *signame)
84 luaA_object_push(L, c);
85 luaA_object_push(L, t);
86 /* emit signal on client, with new tag as argument */
87 luaA_object_emit_signal(L, -2, signame, 1);
88 /* re push tag */
89 luaA_object_push(L, t);
90 /* move tag before client */
91 lua_insert(L, -2);
92 luaA_object_emit_signal(L, -2, signame, 1);
93 /* Remove tag */
94 lua_pop(L, 1);
97 /** Tag a client with the tag on top of the stack.
98 * \param c the client to tag
100 void
101 tag_client(client_t *c)
103 tag_t *t = luaA_object_ref_class(globalconf.L, -1, &tag_class);
105 /* don't tag twice */
106 if(is_client_tagged(c, t))
108 luaA_object_unref(globalconf.L, t);
109 return;
112 client_array_append(&t->clients, c);
113 ewmh_client_update_desktop(c);
114 banning_need_update();
116 tag_client_emit_signal(globalconf.L, t, c, "tagged");
119 /** Untag a client with specified tag.
120 * \param c the client to tag
121 * \param t the tag to tag the client with
123 void
124 untag_client(client_t *c, tag_t *t)
126 for(int i = 0; i < t->clients.len; i++)
127 if(t->clients.tab[i] == c)
129 client_array_take(&t->clients, i);
130 banning_need_update();
131 ewmh_client_update_desktop(c);
132 tag_client_emit_signal(globalconf.L, t, c, "untagged");
133 luaA_object_unref(globalconf.L, t);
134 return;
138 /** Check if a client is tagged with the specified tag.
139 * \param c the client
140 * \param t the tag
141 * \return true if the client is tagged with the tag, false otherwise.
143 bool
144 is_client_tagged(client_t *c, tag_t *t)
146 for(int i = 0; i < t->clients.len; i++)
147 if(t->clients.tab[i] == c)
148 return true;
150 return false;
153 /** Get the index of the first selected tag.
154 * \return Its index.
157 tags_get_first_selected_index(void)
159 foreach(tag, globalconf.tags)
160 if((*tag)->selected)
161 return tag_array_indexof(&globalconf.tags, tag);
162 return 0;
165 /** Create a new tag.
166 * \param L The Lua VM state.
167 * \luastack
168 * \lparam A name.
169 * \lreturn A new tag object.
171 static int
172 luaA_tag_new(lua_State *L)
174 return luaA_class_new(L, &tag_class);
177 /** Get or set the clients attached to this tag.
178 * \param L The Lua VM state.
179 * \return The number of elements pushed on stack.
180 * \luastack
181 * \lparam None or a table of clients to set.
182 * \lreturn A table with the clients attached to this tags.
184 static int
185 luaA_tag_clients(lua_State *L)
187 tag_t *tag = luaA_checkudata(L, 1, &tag_class);
188 client_array_t *clients = &tag->clients;
189 int i;
191 if(lua_gettop(L) == 2)
193 luaA_checktable(L, 2);
194 foreach(c, tag->clients)
196 /* Only untag if we aren't going to add this tag again */
197 bool found = false;
198 lua_pushnil(L);
199 while(lua_next(L, 2))
201 client_t *tc = luaA_checkudata(L, -1, &client_class);
202 /* Pop the value from lua_next */
203 lua_pop(L, 1);
204 if (tc != *c)
205 continue;
207 /* Pop the key from lua_next */
208 lua_pop(L, 1);
209 found = true;
210 break;
212 if(!found)
213 untag_client(*c, tag);
215 lua_pushnil(L);
216 while(lua_next(L, 2))
218 client_t *c = luaA_checkudata(L, -1, &client_class);
219 /* push tag on top of the stack */
220 lua_pushvalue(L, 1);
221 tag_client(c);
222 lua_pop(L, 1);
226 lua_createtable(L, clients->len, 0);
227 for(i = 0; i < clients->len; i++)
229 luaA_object_push(L, clients->tab[i]);
230 lua_rawseti(L, -2, i + 1);
233 return 1;
236 LUA_OBJECT_EXPORT_PROPERTY(tag, tag_t, name, lua_pushstring)
237 LUA_OBJECT_EXPORT_PROPERTY(tag, tag_t, selected, lua_pushboolean)
238 LUA_OBJECT_EXPORT_PROPERTY(tag, tag_t, activated, lua_pushboolean)
240 /** Set the tag name.
241 * \param L The Lua VM state.
242 * \param tag The tag to name.
243 * \return The number of elements pushed on stack.
245 static int
246 luaA_tag_set_name(lua_State *L, tag_t *tag)
248 size_t len;
249 const char *buf = luaL_checklstring(L, -1, &len);
250 p_delete(&tag->name);
251 a_iso2utf8(buf, len, &tag->name, NULL);
252 luaA_object_emit_signal(L, -3, "property::name", 0);
253 ewmh_update_net_desktop_names();
254 return 0;
257 /** Set the tag selection status.
258 * \param L The Lua VM state.
259 * \param tag The tag to set the selection status for.
260 * \return The number of elements pushed on stack.
262 static int
263 luaA_tag_set_selected(lua_State *L, tag_t *tag)
265 tag_view(L, -3, luaA_checkboolean(L, -1));
266 return 0;
269 /** Set the tag activated status.
270 * \param L The Lua VM state.
271 * \param tag The tag to set the activated status for.
272 * \return The number of elements pushed on stack.
274 static int
275 luaA_tag_set_activated(lua_State *L, tag_t *tag)
277 bool activated = luaA_checkboolean(L, -1);
278 if(activated == tag->activated)
279 return 0;
281 tag->activated = activated;
282 if(activated)
284 lua_pushvalue(L, -3);
285 tag_array_append(&globalconf.tags, luaA_object_ref_class(L, -1, &tag_class));
287 else
289 for (int i = 0; i < globalconf.tags.len; i++)
290 if(globalconf.tags.tab[i] == tag)
292 tag_array_take(&globalconf.tags, i);
293 break;
296 if (tag->selected)
298 tag->selected = false;
299 luaA_object_emit_signal(L, -3, "property::selected", 0);
300 banning_need_update();
302 luaA_object_unref(L, tag);
304 ewmh_update_net_numbers_of_desktop();
305 ewmh_update_net_desktop_names();
307 luaA_object_emit_signal(L, -3, "property::activated", 0);
309 return 0;
312 void
313 tag_class_setup(lua_State *L)
315 static const struct luaL_Reg tag_methods[] =
317 LUA_CLASS_METHODS(tag)
318 { "__call", luaA_tag_new },
319 { NULL, NULL }
322 static const struct luaL_Reg tag_meta[] =
324 LUA_OBJECT_META(tag)
325 LUA_CLASS_META
326 { "clients", luaA_tag_clients },
327 { NULL, NULL },
330 luaA_class_setup(L, &tag_class, "tag", NULL,
331 (lua_class_allocator_t) tag_new,
332 (lua_class_collector_t) tag_wipe,
333 NULL,
334 luaA_class_index_miss_property, luaA_class_newindex_miss_property,
335 tag_methods, tag_meta);
336 luaA_class_add_property(&tag_class, "name",
337 (lua_class_propfunc_t) luaA_tag_set_name,
338 (lua_class_propfunc_t) luaA_tag_get_name,
339 (lua_class_propfunc_t) luaA_tag_set_name);
340 luaA_class_add_property(&tag_class, "selected",
341 (lua_class_propfunc_t) luaA_tag_set_selected,
342 (lua_class_propfunc_t) luaA_tag_get_selected,
343 (lua_class_propfunc_t) luaA_tag_set_selected);
344 luaA_class_add_property(&tag_class, "activated",
345 (lua_class_propfunc_t) luaA_tag_set_activated,
346 (lua_class_propfunc_t) luaA_tag_get_activated,
347 (lua_class_propfunc_t) luaA_tag_set_activated);
349 signal_add(&tag_class.signals, "property::name");
350 signal_add(&tag_class.signals, "property::selected");
351 signal_add(&tag_class.signals, "property::activated");
352 signal_add(&tag_class.signals, "request::select");
353 signal_add(&tag_class.signals, "tagged");
354 signal_add(&tag_class.signals, "untagged");
357 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80