awful.tag: fix typo
[awesome.git] / tag.c
blob1fb1cfe5a99ec2651f32916798150c04a17b48e4
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 "screen.h"
23 #include "tag.h"
24 #include "client.h"
25 #include "ewmh.h"
26 #include "widget.h"
28 DO_LUA_TOSTRING(tag_t, tag, "tag")
30 void
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.
38 * \return 0.
40 static int
41 luaA_tag_gc(lua_State *L)
43 tag_t *tag = luaL_checkudata(L, 1, "tag");
44 client_array_wipe(&tag->clients);
45 p_delete(&tag->name);
46 return 0;
49 /** View or unview a tag.
50 * \param tag the tag
51 * \param view set visible or not
53 static void
54 tag_view(tag_t *tag, bool view)
56 tag->selected = view;
57 ewmh_update_net_current_desktop(screen_virttophys(screen_array_indexof(&globalconf.screens,
58 tag->screen)));
59 tag->screen->need_arrange = true;
62 /** Append a tag which on top of the stack to a screen.
63 * \param screen the screen id
65 void
66 tag_append_to_screen(screen_t *s)
68 tag_t *tag = luaL_checkudata(globalconf.L, -1, "tag");
70 /* can't attach a tag twice */
71 if(tag->screen)
72 return;
74 int screen_index = screen_array_indexof(&globalconf.screens, s);
75 int phys_screen = screen_virttophys(screen_index);
77 tag->screen = s;
78 tag_array_append(&s->tags, tag_ref(globalconf.L));
79 ewmh_update_net_numbers_of_desktop(phys_screen);
80 ewmh_update_net_desktop_names(phys_screen);
81 ewmh_update_workarea(phys_screen);
83 /* call hook */
84 if(globalconf.hooks.tags != LUA_REFNIL)
86 lua_pushnumber(globalconf.L, screen_index + 1);
87 tag_push(globalconf.L, tag);
88 lua_pushliteral(globalconf.L, "add");
89 luaA_dofunction(globalconf.L, globalconf.hooks.tags, 3, 0);
93 /** Remove a tag from screen. Tag must be on a screen and have no clients.
94 * \param tag The tag to remove.
96 static void
97 tag_remove_from_screen(tag_t *tag)
99 int screen_index = screen_array_indexof(&globalconf.screens, tag->screen);
100 int phys_screen = screen_virttophys(screen_index);
101 tag_array_t *tags = &tag->screen->tags;
103 for(int i = 0; i < tags->len; i++)
104 if(tags->tab[i] == tag)
106 tag_array_take(tags, i);
107 break;
109 ewmh_update_net_numbers_of_desktop(phys_screen);
110 ewmh_update_net_desktop_names(phys_screen);
111 ewmh_update_workarea(phys_screen);
113 /* call hook */
114 if(globalconf.hooks.tags != LUA_REFNIL)
116 lua_pushnumber(globalconf.L, screen_index + 1);
117 tag_push(globalconf.L, tag);
118 lua_pushliteral(globalconf.L, "remove");
119 luaA_dofunction(globalconf.L, globalconf.hooks.tags, 3, 0);
122 tag->screen = NULL;
124 tag_unref(globalconf.L, tag);
127 /** Tag a client with the tag on top of the stack.
128 * \param c the client to tag
130 void
131 tag_client(client_t *c)
133 tag_t *t = tag_ref(globalconf.L);
135 /* don't tag twice */
136 if(is_client_tagged(c, t))
137 return;
139 client_array_append(&t->clients, c);
140 ewmh_client_update_desktop(c);
141 client_need_arrange(c);
142 /* call hook */
143 if(globalconf.hooks.tagged != LUA_REFNIL)
145 client_push(globalconf.L, c);
146 tag_push(globalconf.L, t);
147 luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
151 /** Untag a client with specified tag.
152 * \param c the client to tag
153 * \param t the tag to tag the client with
155 void
156 untag_client(client_t *c, tag_t *t)
158 for(int i = 0; i < t->clients.len; i++)
159 if(t->clients.tab[i] == c)
161 client_need_arrange(c);
162 client_array_take(&t->clients, i);
163 ewmh_client_update_desktop(c);
164 /* call hook */
165 if(globalconf.hooks.tagged != LUA_REFNIL)
167 client_push(globalconf.L, c);
168 tag_push(globalconf.L, t);
169 luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
171 tag_unref(globalconf.L, t);
172 return;
176 /** Check if a client is tagged with the specified tag.
177 * \param c the client
178 * \param t the tag
179 * \return true if the client is tagged with the tag, false otherwise.
181 bool
182 is_client_tagged(client_t *c, tag_t *t)
184 for(int i = 0; i < t->clients.len; i++)
185 if(t->clients.tab[i] == c)
186 return true;
188 return false;
191 /** Get the current tags for the specified screen.
192 * Returned pointer must be p_delete'd after.
193 * \param screen Screen.
194 * \return A double pointer of tag list finished with a NULL element.
196 tag_t **
197 tags_get_current(screen_t *screen)
199 tag_t **out = p_new(tag_t *, screen->tags.len + 1);
200 int n = 0;
202 foreach(tag, screen->tags)
203 if((*tag)->selected)
204 out[n++] = *tag;
206 return out;
210 /** Set a tag to be the only one viewed.
211 * \param target the tag to see
213 static void
214 tag_view_only(tag_t *target)
216 if(target)
217 foreach(tag, target->screen->tags)
218 tag_view(*tag, *tag == target);
221 /** View only a tag, selected by its index.
222 * \param screen Screen.
223 * \param dindex The index.
225 void
226 tag_view_only_byindex(screen_t *screen, int dindex)
228 tag_array_t *tags = &screen->tags;
230 if(dindex < 0 || dindex >= tags->len)
231 return;
232 tag_view_only(tags->tab[dindex]);
235 /** Create a new tag.
236 * \param L The Lua VM state.
237 * \luastack
238 * \lparam A name.
239 * \lreturn A new tag object.
241 static int
242 luaA_tag_new(lua_State *L)
244 size_t len;
245 const char *name = luaL_checklstring(L, 2, &len);
246 tag_t *tag = tag_new(globalconf.L);
248 a_iso2utf8(name, len, &tag->name, NULL);
250 return 1;
253 /** Get or set the clients attached to this tag.
254 * \param L The Lua VM state.
255 * \return The number of elements pushed on stack.
256 * \luastack
257 * \lparam None or a table of clients to set.
258 * \lreturn A table with the clients attached to this tags.
260 static int
261 luaA_tag_clients(lua_State *L)
263 tag_t *tag = luaL_checkudata(L, 1, "tag");
264 client_array_t *clients = &tag->clients;
265 int i;
267 if(lua_gettop(L) == 2)
269 luaA_checktable(L, 2);
270 foreach(c, tag->clients)
271 untag_client(*c, tag);
272 lua_pushnil(L);
273 while(lua_next(L, 2))
275 client_t *c = luaA_client_checkudata(L, -1);
276 /* push tag on top of the stack */
277 lua_pushvalue(L, 1);
278 tag_client(c);
279 lua_pop(L, 1);
283 lua_createtable(L, clients->len, 0);
284 for(i = 0; i < clients->len; i++)
286 client_push(L, clients->tab[i]);
287 lua_rawseti(L, -2, i + 1);
290 return 1;
293 /** Tag object.
294 * \param L The Lua VM state.
295 * \return The number of elements pushed on stack.
296 * \luastack
297 * \lfield name Tag name.
298 * \lfield screen Screen number of the tag.
299 * \lfield layout Tag layout.
300 * \lfield selected True if the client is selected to be viewed.
302 static int
303 luaA_tag_index(lua_State *L)
305 size_t len;
306 tag_t *tag = luaL_checkudata(L, 1, "tag");
307 const char *attr;
309 if(luaA_usemetatable(L, 1, 2))
310 return 1;
312 attr = luaL_checklstring(L, 2, &len);
314 switch(a_tokenize(attr, len))
316 case A_TK_NAME:
317 lua_pushstring(L, tag->name);
318 break;
319 case A_TK_SCREEN:
320 if(!tag->screen)
321 return 0;
322 lua_pushnumber(L, screen_array_indexof(&globalconf.screens, tag->screen) + 1);
323 break;
324 case A_TK_SELECTED:
325 lua_pushboolean(L, tag->selected);
326 break;
327 default:
328 return 0;
331 return 1;
334 /** Tag newindex.
335 * \param L The Lua VM state.
336 * \return The number of elements pushed on stack.
338 static int
339 luaA_tag_newindex(lua_State *L)
341 size_t len;
342 tag_t *tag = luaL_checkudata(L, 1, "tag");
343 const char *attr = luaL_checklstring(L, 2, &len);
345 switch(a_tokenize(attr, len))
347 int screen;
349 case A_TK_NAME:
351 const char *buf = luaL_checklstring(L, 3, &len);
352 p_delete(&tag->name);
353 a_iso2utf8(buf, len, &tag->name, NULL);
355 break;
356 case A_TK_SCREEN:
357 if(!lua_isnil(L, 3))
359 screen = luaL_checknumber(L, 3) - 1;
360 luaA_checkscreen(screen);
362 else
363 screen = -1;
365 if(tag->screen)
366 tag_remove_from_screen(tag);
368 if(screen != -1)
370 /* push tag on top of the stack */
371 lua_pushvalue(L, 1);
372 tag_append_to_screen(&globalconf.screens.tab[screen]);
374 break;
375 case A_TK_SELECTED:
376 if(tag->screen)
377 tag_view(tag, luaA_checkboolean(L, 3));
378 return 0;
379 default:
380 return 0;
383 if(tag->screen && tag->selected)
384 tag->screen->need_arrange = true;
386 return 0;
389 const struct luaL_reg awesome_tag_methods[] =
391 { "__call", luaA_tag_new },
392 { NULL, NULL }
394 const struct luaL_reg awesome_tag_meta[] =
396 { "clients", luaA_tag_clients },
397 { "__index", luaA_tag_index },
398 { "__newindex", luaA_tag_newindex },
399 { "__gc", luaA_tag_gc },
400 { "__tostring", luaA_tag_tostring },
401 { NULL, NULL },
404 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80