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 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.
36 * \param view set visible or not
39 tag_view(tag_t
*tag
, bool 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.
52 tag_new(const char *name
, ssize_t len
)
56 tag
= p_new(tag_t
, 1);
57 a_iso2utf8(name
, len
, &tag
->name
, NULL
);
60 tag
->screen
= SCREEN_UNDEF
;
65 /** Append a tag to a screen.
66 * \param tag the tag to append
67 * \param screen the screen id
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
82 for(client_t
*c
= globalconf
.clients
; c
; c
= c
->next
)
83 client_saveprops_tags(c
);
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.
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
);
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
118 for(client_t
*c
= globalconf
.clients
; c
; c
= c
->next
)
119 client_saveprops_tags(c
);
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);
133 /** Tag a client with specified tag.
134 * \param c the client to tag
135 * \param t the tag to tag the client with
138 tag_client(client_t
*c
, tag_t
*t
)
140 /* don't tag twice */
141 if(is_client_tagged(c
, t
))
145 client_array_append(&t
->clients
, c
);
146 client_saveprops_tags(c
);
147 ewmh_client_update_desktop(c
);
148 client_need_arrange(c
);
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
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
);
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);
184 /** Check if a client is tagged with the specified tag.
185 * \param c the client
187 * \return true if the client is tagged with the tag, false otherwise.
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
)
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
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);
211 for(int i
= 0; i
< tags
->len
; i
++)
212 if(tags
->tab
[i
]->selected
)
213 out
[n
++] = tags
->tab
[i
];
219 /** Set a tag to be the only one viewed.
220 * \param target the tag to see
223 tag_view_only(tag_t
*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
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
)
245 tag_view_only(tags
->tab
[dindex
]);
248 /** Create a new tag.
249 * \param L The Lua VM state.
252 * \lreturn A new tag object.
255 luaA_tag_new(lua_State
*L
)
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.
266 * \lparam None or a table of clients to set.
267 * \lreturn A table with the clients attached to this tags.
270 luaA_tag_clients(lua_State
*L
)
272 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
273 client_array_t
*clients
= &(*tag
)->clients
;
276 if(lua_gettop(L
) == 2)
280 luaA_checktable(L
, 2);
281 for(i
= 0; i
< (*tag
)->clients
.len
; i
++)
282 untag_client((*tag
)->clients
.tab
[i
], *tag
);
284 while(lua_next(L
, 2))
286 c
= luaA_checkudata(L
, -1, "client");
287 tag_client(*c
, *tag
);
293 for(i
= 0; i
< clients
->len
; i
++)
295 luaA_client_userdata_new(L
, clients
->tab
[i
]);
296 lua_rawseti(L
, -2, i
+ 1);
303 * \param L The Lua VM state.
304 * \return The number of elements pushed on stack.
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.
312 luaA_tag_index(lua_State
*L
)
315 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
318 if(luaA_usemetatable(L
, 1, 2))
321 attr
= luaL_checklstring(L
, 2, &len
);
323 switch(a_tokenize(attr
, len
))
326 lua_pushstring(L
, (*tag
)->name
);
329 if((*tag
)->screen
== SCREEN_UNDEF
)
331 lua_pushnumber(L
, (*tag
)->screen
+ 1);
334 lua_pushboolean(L
, (*tag
)->selected
);
344 * \param L The Lua VM state.
345 * \return The number of elements pushed on stack.
348 luaA_tag_newindex(lua_State
*L
)
351 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
352 const char *attr
= luaL_checklstring(L
, 2, &len
);
354 switch(a_tokenize(attr
, len
))
360 const char *buf
= luaL_checklstring(L
, 3, &len
);
361 p_delete(&(*tag
)->name
);
362 a_iso2utf8(buf
, len
, &(*tag
)->name
, NULL
);
368 screen
= luaL_checknumber(L
, 3) - 1;
369 luaA_checkscreen(screen
);
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
]);
381 if((*tag
)->screen
!= SCREEN_UNDEF
)
382 tag_view(*tag
, luaA_checkboolean(L
, 3));
388 if((*tag
)->screen
!= SCREEN_UNDEF
&& (*tag
)->selected
)
389 globalconf
.screens
[(*tag
)->screen
].need_arrange
= true;
394 const struct luaL_reg awesome_tag_methods
[] =
396 { "__call", luaA_tag_new
},
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
},
410 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80