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.
34 /** true if activated */
36 /** true if 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
)
47 tag_unref_simplified(tag_t
**tag
)
49 luaA_object_unref(globalconf
.L
, *tag
);
55 client_array_wipe(&tag
->clients
);
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.
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
)
74 banning_need_update();
75 ewmh_update_net_current_desktop();
77 luaA_object_emit_signal(L
, udx
, "property::selected", 0);
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);
89 luaA_object_push(L
, t
);
90 /* move tag before client */
92 luaA_object_emit_signal(L
, -2, signame
, 1);
97 /** Tag a client with the tag on top of the stack.
98 * \param c the client to tag
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
);
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
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
);
138 /** Check if a client is tagged with the specified tag.
139 * \param c the client
141 * \return true if the client is tagged with the tag, false otherwise.
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
)
153 /** Get the index of the first selected tag.
157 tags_get_first_selected_index(void)
159 foreach(tag
, globalconf
.tags
)
161 return tag_array_indexof(&globalconf
.tags
, tag
);
165 /** Create a new tag.
166 * \param L The Lua VM state.
169 * \lreturn A new tag object.
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.
181 * \lparam None or a table of clients to set.
182 * \lreturn A table with the clients attached to this tags.
185 luaA_tag_clients(lua_State
*L
)
187 tag_t
*tag
= luaA_checkudata(L
, 1, &tag_class
);
188 client_array_t
*clients
= &tag
->clients
;
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 */
199 while(lua_next(L
, 2))
201 client_t
*tc
= luaA_checkudata(L
, -1, &client_class
);
202 /* Pop the value from lua_next */
207 /* Pop the key from lua_next */
213 untag_client(*c
, tag
);
216 while(lua_next(L
, 2))
218 client_t
*c
= luaA_checkudata(L
, -1, &client_class
);
219 /* push tag on top of the stack */
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);
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.
246 luaA_tag_set_name(lua_State
*L
, tag_t
*tag
)
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();
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.
263 luaA_tag_set_selected(lua_State
*L
, tag_t
*tag
)
265 tag_view(L
, -3, luaA_checkboolean(L
, -1));
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.
275 luaA_tag_set_activated(lua_State
*L
, tag_t
*tag
)
277 bool activated
= luaA_checkboolean(L
, -1);
278 if(activated
== tag
->activated
)
281 tag
->activated
= activated
;
284 lua_pushvalue(L
, -3);
285 tag_array_append(&globalconf
.tags
, luaA_object_ref_class(L
, -1, &tag_class
));
289 for (int i
= 0; i
< globalconf
.tags
.len
; i
++)
290 if(globalconf
.tags
.tab
[i
] == tag
)
292 tag_array_take(&globalconf
.tags
, i
);
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);
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
},
322 static const struct luaL_Reg tag_meta
[] =
326 { "clients", luaA_tag_clients
},
330 luaA_class_setup(L
, &tag_class
, "tag", NULL
,
331 (lua_class_allocator_t
) tag_new
,
332 (lua_class_collector_t
) tag_wipe
,
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