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.
28 DO_LUA_TOSTRING(tag_t
, tag
, "tag")
31 tag_unref_simplified(tag_t
**tag
)
33 tag_unref(globalconf
.L
, *tag
);
36 /** Garbage collect a tag.
37 * \param L The Lua VM state.
41 luaA_tag_gc(lua_State
*L
)
43 tag_t
*tag
= luaL_checkudata(L
, 1, "tag");
44 luaA_ref_array_wipe(&tag
->refs
);
45 client_array_wipe(&tag
->clients
);
50 /** View or unview a tag.
52 * \param view set visible or not
55 tag_view(tag_t
*tag
, bool view
)
58 ewmh_update_net_current_desktop(screen_virttophys(screen_array_indexof(&globalconf
.screens
,
60 tag
->screen
->need_arrange
= true;
63 /** Append a tag which on top of the stack to a screen.
64 * \param screen the screen id
67 tag_append_to_screen(screen_t
*s
)
69 tag_t
*tag
= luaL_checkudata(globalconf
.L
, -1, "tag");
71 /* can't attach a tag twice */
75 int screen_index
= screen_array_indexof(&globalconf
.screens
, s
);
76 int phys_screen
= screen_virttophys(screen_index
);
79 tag_array_append(&s
->tags
, tag_ref(globalconf
.L
));
80 ewmh_update_net_numbers_of_desktop(phys_screen
);
81 ewmh_update_net_desktop_names(phys_screen
);
82 ewmh_update_workarea(phys_screen
);
85 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
87 lua_pushnumber(globalconf
.L
, screen_index
+ 1);
88 tag_push(globalconf
.L
, tag
);
89 lua_pushliteral(globalconf
.L
, "add");
90 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
94 /** Remove a tag from screen. Tag must be on a screen and have no clients.
95 * \param tag The tag to remove.
98 tag_remove_from_screen(tag_t
*tag
)
100 int screen_index
= screen_array_indexof(&globalconf
.screens
, tag
->screen
);
101 int phys_screen
= screen_virttophys(screen_index
);
102 tag_array_t
*tags
= &tag
->screen
->tags
;
104 for(int i
= 0; i
< tags
->len
; i
++)
105 if(tags
->tab
[i
] == tag
)
107 tag_array_take(tags
, i
);
110 ewmh_update_net_numbers_of_desktop(phys_screen
);
111 ewmh_update_net_desktop_names(phys_screen
);
112 ewmh_update_workarea(phys_screen
);
115 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
117 lua_pushnumber(globalconf
.L
, screen_index
+ 1);
118 tag_push(globalconf
.L
, tag
);
119 lua_pushliteral(globalconf
.L
, "remove");
120 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
125 tag_unref(globalconf
.L
, tag
);
128 /** Tag a client with the tag on top of the stack.
129 * \param c the client to tag
132 tag_client(client_t
*c
)
134 tag_t
*t
= tag_ref(globalconf
.L
);
136 /* don't tag twice */
137 if(is_client_tagged(c
, t
))
140 client_array_append(&t
->clients
, c
);
141 ewmh_client_update_desktop(c
);
142 client_need_arrange(c
);
144 if(globalconf
.hooks
.tagged
!= LUA_REFNIL
)
146 client_push(globalconf
.L
, c
);
147 tag_push(globalconf
.L
, t
);
148 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
152 /** Untag a client with specified tag.
153 * \param c the client to tag
154 * \param t the tag to tag the client with
157 untag_client(client_t
*c
, tag_t
*t
)
159 for(int i
= 0; i
< t
->clients
.len
; i
++)
160 if(t
->clients
.tab
[i
] == c
)
162 client_need_arrange(c
);
163 client_array_take(&t
->clients
, i
);
164 ewmh_client_update_desktop(c
);
166 if(globalconf
.hooks
.tagged
!= LUA_REFNIL
)
168 client_push(globalconf
.L
, c
);
169 tag_push(globalconf
.L
, t
);
170 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
172 tag_unref(globalconf
.L
, t
);
177 /** Check if a client is tagged with the specified tag.
178 * \param c the client
180 * \return true if the client is tagged with the tag, false otherwise.
183 is_client_tagged(client_t
*c
, tag_t
*t
)
185 for(int i
= 0; i
< t
->clients
.len
; i
++)
186 if(t
->clients
.tab
[i
] == c
)
192 /** Get the current tags for the specified screen.
193 * Returned pointer must be p_delete'd after.
194 * \param screen Screen.
195 * \return A double pointer of tag list finished with a NULL element.
198 tags_get_current(screen_t
*screen
)
200 tag_t
**out
= p_new(tag_t
*, screen
->tags
.len
+ 1);
203 foreach(tag
, screen
->tags
)
211 /** Set a tag to be the only one viewed.
212 * \param target the tag to see
215 tag_view_only(tag_t
*target
)
218 foreach(tag
, target
->screen
->tags
)
219 tag_view(*tag
, *tag
== target
);
222 /** View only a tag, selected by its index.
223 * \param screen Screen.
224 * \param dindex The index.
227 tag_view_only_byindex(screen_t
*screen
, int dindex
)
229 tag_array_t
*tags
= &screen
->tags
;
231 if(dindex
< 0 || dindex
>= tags
->len
)
233 tag_view_only(tags
->tab
[dindex
]);
236 /** Create a new tag.
237 * \param L The Lua VM state.
240 * \lreturn A new tag object.
243 luaA_tag_new(lua_State
*L
)
246 const char *name
= luaL_checklstring(L
, 2, &len
);
247 tag_t
*tag
= tag_new(globalconf
.L
);
249 a_iso2utf8(name
, len
, &tag
->name
, NULL
);
254 /** Get or set the clients attached to this tag.
255 * \param L The Lua VM state.
256 * \return The number of elements pushed on stack.
258 * \lparam None or a table of clients to set.
259 * \lreturn A table with the clients attached to this tags.
262 luaA_tag_clients(lua_State
*L
)
264 tag_t
*tag
= luaL_checkudata(L
, 1, "tag");
265 client_array_t
*clients
= &tag
->clients
;
268 if(lua_gettop(L
) == 2)
270 luaA_checktable(L
, 2);
271 foreach(c
, tag
->clients
)
272 untag_client(*c
, tag
);
274 while(lua_next(L
, 2))
276 client_t
*c
= luaA_client_checkudata(L
, -1);
277 /* push tag on top of the stack */
284 lua_createtable(L
, clients
->len
, 0);
285 for(i
= 0; i
< clients
->len
; i
++)
287 client_push(L
, clients
->tab
[i
]);
288 lua_rawseti(L
, -2, i
+ 1);
295 * \param L The Lua VM state.
296 * \return The number of elements pushed on stack.
298 * \lfield name Tag name.
299 * \lfield screen Screen number of the tag.
300 * \lfield layout Tag layout.
301 * \lfield selected True if the client is selected to be viewed.
304 luaA_tag_index(lua_State
*L
)
307 tag_t
*tag
= luaL_checkudata(L
, 1, "tag");
310 if(luaA_usemetatable(L
, 1, 2))
313 attr
= luaL_checklstring(L
, 2, &len
);
315 switch(a_tokenize(attr
, len
))
318 lua_pushstring(L
, tag
->name
);
323 lua_pushnumber(L
, screen_array_indexof(&globalconf
.screens
, tag
->screen
) + 1);
326 lua_pushboolean(L
, tag
->selected
);
336 * \param L The Lua VM state.
337 * \return The number of elements pushed on stack.
340 luaA_tag_newindex(lua_State
*L
)
343 tag_t
*tag
= luaL_checkudata(L
, 1, "tag");
344 const char *attr
= luaL_checklstring(L
, 2, &len
);
346 switch(a_tokenize(attr
, len
))
352 const char *buf
= luaL_checklstring(L
, 3, &len
);
353 p_delete(&tag
->name
);
354 a_iso2utf8(buf
, len
, &tag
->name
, NULL
);
360 screen
= luaL_checknumber(L
, 3) - 1;
361 luaA_checkscreen(screen
);
367 tag_remove_from_screen(tag
);
371 /* push tag on top of the stack */
373 tag_append_to_screen(&globalconf
.screens
.tab
[screen
]);
378 tag_view(tag
, luaA_checkboolean(L
, 3));
384 if(tag
->screen
&& tag
->selected
)
385 tag
->screen
->need_arrange
= true;
390 const struct luaL_reg awesome_tag_methods
[] =
392 { "__call", luaA_tag_new
},
395 const struct luaL_reg awesome_tag_meta
[] =
397 { "clients", luaA_tag_clients
},
398 { "__index", luaA_tag_index
},
399 { "__newindex", luaA_tag_newindex
},
400 { "__gc", luaA_tag_gc
},
401 { "__tostring", luaA_tag_tostring
},
405 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80