awful.prompt: add support for Shift+Insert to paste
[awesome.git] / tag.c
blob10a611beb3e1e4b539bbc2bb97c15a17f800a512
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 extern awesome_t globalconf;
30 DO_LUA_NEW(extern, tag_t, tag, "tag", tag_ref)
31 DO_LUA_GC(tag_t, tag, "tag", tag_unref)
32 DO_LUA_EQ(tag_t, tag, "tag")
34 /** View or unview a tag.
35 * \param tag the tag
36 * \param view set visible or not
38 static void
39 tag_view(tag_t *tag, bool view)
41 tag->selected = view;
42 ewmh_update_net_current_desktop(screen_virttophys(tag->screen));
43 globalconf.screens[tag->screen].need_arrange = true;
46 /** Create a new tag. Parameters values are checked.
47 * \param name Tag name.
48 * \param len Tag name length.
49 * \return A new tag with all these parameters.
51 tag_t *
52 tag_new(const char *name, ssize_t len)
54 tag_t *tag;
56 tag = p_new(tag_t, 1);
57 a_iso2utf8(name, len, &tag->name, NULL);
59 /* to avoid error */
60 tag->screen = SCREEN_UNDEF;
62 return tag;
65 /** Append a tag to a screen.
66 * \param tag the tag to append
67 * \param screen the screen id
69 void
70 tag_append_to_screen(tag_t *tag, screen_t *s)
72 int phys_screen = screen_virttophys(s->index);
74 tag->screen = s->index;
75 tag_array_append(&s->tags, tag_ref(&tag));
76 ewmh_update_net_numbers_of_desktop(phys_screen);
77 ewmh_update_net_desktop_names(phys_screen);
78 ewmh_update_workarea(phys_screen);
80 /* resave tag prop of all clients so the number of tag will be the
81 * same */
82 for(client_t *c = globalconf.clients; c; c = c->next)
83 client_saveprops_tags(c);
85 /* call hook */
86 if(globalconf.hooks.tags != LUA_REFNIL)
88 lua_pushnumber(globalconf.L, s->index + 1);
89 luaA_tag_userdata_new(globalconf.L, tag);
90 lua_pushliteral(globalconf.L, "add");
91 luaA_dofunction(globalconf.L, globalconf.hooks.tags, 3, 0);
95 /** Remove a tag from screen. Tag must be on a screen and have no clients.
96 * \param tag The tag to remove.
98 static void
99 tag_remove_from_screen(tag_t *tag)
101 int screen = tag->screen;
102 int phys_screen = screen_virttophys(tag->screen);
103 tag_array_t *tags = &globalconf.screens[tag->screen].tags;
105 for(int i = 0; i < tags->len; i++)
106 if(tags->tab[i] == tag)
108 tag_array_take(tags, i);
109 break;
111 ewmh_update_net_numbers_of_desktop(phys_screen);
112 ewmh_update_net_desktop_names(phys_screen);
113 ewmh_update_workarea(phys_screen);
114 tag->screen = SCREEN_UNDEF;
116 /* resave tag prop of all clients so the number of tag will be the
117 * same */
118 for(client_t *c = globalconf.clients; c; c = c->next)
119 client_saveprops_tags(c);
121 /* call hook */
122 if(globalconf.hooks.tags != LUA_REFNIL)
124 lua_pushnumber(globalconf.L, screen + 1);
125 luaA_tag_userdata_new(globalconf.L, tag);
126 lua_pushliteral(globalconf.L, "remove");
127 luaA_dofunction(globalconf.L, globalconf.hooks.tags, 3, 0);
130 tag_unref(&tag);
133 /** Tag a client with specified tag.
134 * \param c the client to tag
135 * \param t the tag to tag the client with
137 void
138 tag_client(client_t *c, tag_t *t)
140 /* don't tag twice */
141 if(is_client_tagged(c, t))
142 return;
144 tag_ref(&t);
145 client_array_append(&t->clients, c);
146 client_saveprops_tags(c);
147 ewmh_client_update_desktop(c);
148 client_need_arrange(c);
149 /* call hook */
150 if(globalconf.hooks.tagged != LUA_REFNIL)
152 luaA_client_userdata_new(globalconf.L, c);
153 luaA_tag_userdata_new(globalconf.L, t);
154 luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
158 /** Untag a client with specified tag.
159 * \param c the client to tag
160 * \param t the tag to tag the client with
162 void
163 untag_client(client_t *c, tag_t *t)
165 for(int i = 0; i < t->clients.len; i++)
166 if(t->clients.tab[i] == c)
168 client_need_arrange(c);
169 client_array_take(&t->clients, i);
170 client_saveprops_tags(c);
171 ewmh_client_update_desktop(c);
172 /* call hook */
173 if(globalconf.hooks.tagged != LUA_REFNIL)
175 luaA_client_userdata_new(globalconf.L, c);
176 luaA_tag_userdata_new(globalconf.L, t);
177 luaA_dofunction(globalconf.L, globalconf.hooks.tagged, 2, 0);
179 tag_unref(&t);
180 return;
184 /** Check if a client is tagged with the specified tag.
185 * \param c the client
186 * \param t the tag
187 * \return true if the client is tagged with the tag, false otherwise.
189 bool
190 is_client_tagged(client_t *c, tag_t *t)
192 for(int i = 0; i < t->clients.len; i++)
193 if (t->clients.tab[i] == c)
194 return true;
196 return false;
199 /** Get the current tags for the specified screen.
200 * Returned pointer must be p_delete'd after.
201 * \param screen screen id
202 * \return a double pointer of tag list finished with a NULL element
204 tag_t **
205 tags_get_current(int screen)
207 tag_array_t *tags = &globalconf.screens[screen].tags;
208 tag_t **out = p_new(tag_t *, tags->len + 1);
209 int n = 0;
211 for(int i = 0; i < tags->len; i++)
212 if(tags->tab[i]->selected)
213 out[n++] = tags->tab[i];
215 return out;
219 /** Set a tag to be the only one viewed.
220 * \param target the tag to see
222 static void
223 tag_view_only(tag_t *target)
225 if (target)
227 tag_array_t *tags = &globalconf.screens[target->screen].tags;
229 for(int i = 0; i < tags->len; i++)
230 tag_view(tags->tab[i], tags->tab[i] == target);
234 /** View only a tag, selected by its index.
235 * \param screen screen id
236 * \param dindex the index
238 void
239 tag_view_only_byindex(int screen, int dindex)
241 tag_array_t *tags = &globalconf.screens[screen].tags;
243 if(dindex < 0 || dindex >= tags->len)
244 return;
245 tag_view_only(tags->tab[dindex]);
248 /** Create a new tag.
249 * \param L The Lua VM state.
250 * \luastack
251 * \lparam A name.
252 * \lreturn A new tag object.
254 static int
255 luaA_tag_new(lua_State *L)
257 size_t len;
258 const char *name = luaL_checklstring(L, 2, &len);
259 return luaA_tag_userdata_new(L, tag_new(name, len));
262 /** Get or set the clients attached to this tag.
263 * \param L The Lua VM state.
264 * \return The number of elements pushed on stack.
265 * \luastack
266 * \lparam None or a table of clients to set.
267 * \lreturn A table with the clients attached to this tags.
269 static int
270 luaA_tag_clients(lua_State *L)
272 tag_t **tag = luaA_checkudata(L, 1, "tag");
273 client_array_t *clients = &(*tag)->clients;
274 int i;
276 if(lua_gettop(L) == 2)
278 client_t **c;
280 luaA_checktable(L, 2);
281 for(i = 0; i < (*tag)->clients.len; i++)
282 untag_client((*tag)->clients.tab[i], *tag);
283 lua_pushnil(L);
284 while(lua_next(L, 2))
286 c = luaA_checkudata(L, -1, "client");
287 tag_client(*c, *tag);
288 lua_pop(L, 1);
292 luaA_otable_new(L);
293 for(i = 0; i < clients->len; i++)
295 luaA_client_userdata_new(L, clients->tab[i]);
296 lua_rawseti(L, -2, i + 1);
299 return 1;
302 /** Tag object.
303 * \param L The Lua VM state.
304 * \return The number of elements pushed on stack.
305 * \luastack
306 * \lfield name Tag name.
307 * \lfield screen Screen number of the tag.
308 * \lfield layout Tag layout.
309 * \lfield selected True if the client is selected to be viewed.
311 static int
312 luaA_tag_index(lua_State *L)
314 size_t len;
315 tag_t **tag = luaA_checkudata(L, 1, "tag");
316 const char *attr;
318 if(luaA_usemetatable(L, 1, 2))
319 return 1;
321 attr = luaL_checklstring(L, 2, &len);
323 switch(a_tokenize(attr, len))
325 case A_TK_NAME:
326 lua_pushstring(L, (*tag)->name);
327 break;
328 case A_TK_SCREEN:
329 if((*tag)->screen == SCREEN_UNDEF)
330 return 0;
331 lua_pushnumber(L, (*tag)->screen + 1);
332 break;
333 case A_TK_SELECTED:
334 lua_pushboolean(L, (*tag)->selected);
335 break;
336 default:
337 return 0;
340 return 1;
343 /** Tag newindex.
344 * \param L The Lua VM state.
345 * \return The number of elements pushed on stack.
347 static int
348 luaA_tag_newindex(lua_State *L)
350 size_t len;
351 tag_t **tag = luaA_checkudata(L, 1, "tag");
352 const char *attr = luaL_checklstring(L, 2, &len);
354 switch(a_tokenize(attr, len))
356 int screen;
358 case A_TK_NAME:
360 const char *buf = luaL_checklstring(L, 3, &len);
361 p_delete(&(*tag)->name);
362 a_iso2utf8(buf, len, &(*tag)->name, NULL);
364 break;
365 case A_TK_SCREEN:
366 if(!lua_isnil(L, 3))
368 screen = luaL_checknumber(L, 3) - 1;
369 luaA_checkscreen(screen);
371 else
372 screen = SCREEN_UNDEF;
374 if((*tag)->screen != SCREEN_UNDEF)
375 tag_remove_from_screen(*tag);
377 if(screen != SCREEN_UNDEF)
378 tag_append_to_screen(*tag, &globalconf.screens[screen]);
379 break;
380 case A_TK_SELECTED:
381 if((*tag)->screen != SCREEN_UNDEF)
382 tag_view(*tag, luaA_checkboolean(L, 3));
383 return 0;
384 default:
385 return 0;
388 if((*tag)->screen != SCREEN_UNDEF && (*tag)->selected)
389 globalconf.screens[(*tag)->screen].need_arrange = true;
391 return 0;
394 const struct luaL_reg awesome_tag_methods[] =
396 { "__call", luaA_tag_new },
397 { NULL, NULL }
399 const struct luaL_reg awesome_tag_meta[] =
401 { "clients", luaA_tag_clients },
402 { "__index", luaA_tag_index },
403 { "__newindex", luaA_tag_newindex },
404 { "__eq", luaA_tag_eq },
405 { "__gc", luaA_tag_gc },
406 { "__tostring", luaA_tag_tostring },
407 { NULL, NULL },
410 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80