manage signal handler: use awesome.startup
[awesome.git] / objects / tag.c
blob23007a408f3cba43065034ba5f3ef2e80c63e852
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 "client.h"
24 #include "ewmh.h"
25 #include "luaa.h"
27 /** Tag type */
28 struct tag
30 LUA_OBJECT_HEADER
31 /** Tag name */
32 char *name;
33 /** true if activated */
34 bool activated;
35 /** true if selected */
36 bool 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)
45 void
46 tag_unref_simplified(tag_t **tag)
48 luaA_object_unref(globalconf.L, *tag);
51 static void
52 tag_wipe(tag_t *tag)
54 client_array_wipe(&tag->clients);
55 p_delete(&tag->name);
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.
66 static void
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)
72 tag->selected = view;
73 banning_need_update();
74 ewmh_update_net_current_desktop();
76 luaA_object_emit_signal(L, udx, "property::selected", 0);
80 static void
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);
87 /* re push tag */
88 luaA_object_push(L, t);
89 /* move tag before client */
90 lua_insert(L, -2);
91 luaA_object_emit_signal(L, -2, signame, 1);
92 /* Remove tag */
93 lua_pop(L, 1);
96 /** Tag a client with the tag on top of the stack.
97 * \param c the client to tag
99 void
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);
108 return;
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
122 void
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);
133 return;
137 /** Check if a client is tagged with the specified tag.
138 * \param c the client
139 * \param t the tag
140 * \return true if the client is tagged with the tag, false otherwise.
142 bool
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)
147 return true;
149 return false;
152 /** Get the index of the first selected tag.
153 * \return Its index.
156 tags_get_first_selected_index(void)
158 foreach(tag, globalconf.tags)
159 if((*tag)->selected)
160 return tag_array_indexof(&globalconf.tags, tag);
161 return 0;
164 /** Create a new tag.
165 * \param L The Lua VM state.
166 * \luastack
167 * \lparam A name.
168 * \lreturn A new tag object.
170 static int
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.
179 * \luastack
180 * \lparam None or a table of clients to set.
181 * \lreturn A table with the clients attached to this tags.
183 static int
184 luaA_tag_clients(lua_State *L)
186 tag_t *tag = luaA_checkudata(L, 1, &tag_class);
187 client_array_t *clients = &tag->clients;
188 int i;
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 */
196 bool found = false;
197 lua_pushnil(L);
198 while(lua_next(L, 2))
200 client_t *tc = luaA_checkudata(L, -1, &client_class);
201 /* Pop the value from lua_next */
202 lua_pop(L, 1);
203 if (tc != *c)
204 continue;
206 /* Pop the key from lua_next */
207 lua_pop(L, 1);
208 found = true;
209 break;
211 if(!found)
212 untag_client(*c, tag);
214 lua_pushnil(L);
215 while(lua_next(L, 2))
217 client_t *c = luaA_checkudata(L, -1, &client_class);
218 /* push tag on top of the stack */
219 lua_pushvalue(L, 1);
220 tag_client(c);
221 lua_pop(L, 1);
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);
232 return 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.
244 static int
245 luaA_tag_set_name(lua_State *L, tag_t *tag)
247 size_t len;
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();
253 return 0;
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.
261 static int
262 luaA_tag_set_selected(lua_State *L, tag_t *tag)
264 tag_view(L, -3, luaA_checkboolean(L, -1));
265 return 0;
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.
273 static int
274 luaA_tag_set_activated(lua_State *L, tag_t *tag)
276 bool activated = luaA_checkboolean(L, -1);
277 if(activated == tag->activated)
278 return 0;
280 tag->activated = activated;
281 if(activated)
283 lua_pushvalue(L, -3);
284 tag_array_append(&globalconf.tags, luaA_object_ref_class(L, -1, &tag_class));
286 else
288 for (int i = 0; i < globalconf.tags.len; i++)
289 if(globalconf.tags.tab[i] == tag)
291 tag_array_take(&globalconf.tags, i);
292 break;
295 if (tag->selected)
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);
308 return 0;
311 void
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 },
318 { NULL, NULL }
321 static const struct luaL_Reg tag_meta[] =
323 LUA_OBJECT_META(tag)
324 LUA_CLASS_META
325 { "clients", luaA_tag_clients },
326 { NULL, NULL },
329 luaA_class_setup(L, &tag_class, "tag", NULL,
330 (lua_class_allocator_t) tag_new,
331 (lua_class_collector_t) tag_wipe,
332 NULL,
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