Fix wibox.layout.rotate
[awesome.git] / objects / tag.c
blobcff1981b25014cc10aa180c16542768ce2166aba
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 /** View only a tag, selected by its index.
165 * \param dindex The index.
167 void
168 tag_view_only_byindex(int dindex)
170 if(dindex < 0 || dindex >= globalconf.tags.len)
171 return;
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.
183 * \luastack
184 * \lparam A name.
185 * \lreturn A new tag object.
187 static int
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.
196 * \luastack
197 * \lparam None or a table of clients to set.
198 * \lreturn A table with the clients attached to this tags.
200 static int
201 luaA_tag_clients(lua_State *L)
203 tag_t *tag = luaA_checkudata(L, 1, &tag_class);
204 client_array_t *clients = &tag->clients;
205 int i;
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 */
213 bool found = false;
214 lua_pushnil(L);
215 while(lua_next(L, 2))
217 client_t *tc = luaA_checkudata(L, -1, &client_class);
218 /* Pop the value from lua_next */
219 lua_pop(L, 1);
220 if (tc != *c)
221 continue;
223 /* Pop the key from lua_next */
224 lua_pop(L, 1);
225 found = true;
226 break;
228 if(!found)
229 untag_client(*c, tag);
231 lua_pushnil(L);
232 while(lua_next(L, 2))
234 client_t *c = luaA_checkudata(L, -1, &client_class);
235 /* push tag on top of the stack */
236 lua_pushvalue(L, 1);
237 tag_client(c);
238 lua_pop(L, 1);
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);
249 return 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.
261 static int
262 luaA_tag_set_name(lua_State *L, tag_t *tag)
264 size_t len;
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();
270 return 0;
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.
278 static int
279 luaA_tag_set_selected(lua_State *L, tag_t *tag)
281 tag_view(L, -3, luaA_checkboolean(L, -1));
282 return 0;
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.
290 static int
291 luaA_tag_set_activated(lua_State *L, tag_t *tag)
293 bool activated = luaA_checkboolean(L, -1);
294 if(activated == tag->activated)
295 return 0;
297 tag->activated = activated;
298 if(activated)
300 lua_pushvalue(L, -3);
301 tag_array_append(&globalconf.tags, luaA_object_ref_class(L, -1, &tag_class));
303 else
305 for (int i = 0; i < globalconf.tags.len; i++)
306 if(globalconf.tags.tab[i] == tag)
308 tag_array_take(&globalconf.tags, i);
309 break;
312 if (tag->selected)
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);
325 return 0;
328 void
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 },
335 { NULL, NULL }
338 static const struct luaL_Reg tag_meta[] =
340 LUA_OBJECT_META(tag)
341 LUA_CLASS_META
342 { "clients", luaA_tag_clients },
343 { NULL, NULL },
346 luaA_class_setup(L, &tag_class, "tag", NULL,
347 (lua_class_allocator_t) tag_new,
348 (lua_class_collector_t) tag_wipe,
349 NULL,
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