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 client_need_arrange(c
);
149 if(globalconf
.hooks
.tagged
!= LUA_REFNIL
)
151 luaA_client_userdata_new(globalconf
.L
, c
);
152 luaA_tag_userdata_new(globalconf
.L
, t
);
153 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
157 /** Untag a client with specified tag.
158 * \param c the client to tag
159 * \param t the tag to tag the client with
162 untag_client(client_t
*c
, tag_t
*t
)
164 for(int i
= 0; i
< t
->clients
.len
; i
++)
165 if(t
->clients
.tab
[i
] == c
)
167 client_need_arrange(c
);
168 client_array_take(&t
->clients
, i
);
169 client_saveprops_tags(c
);
171 if(globalconf
.hooks
.tagged
!= LUA_REFNIL
)
173 luaA_client_userdata_new(globalconf
.L
, c
);
174 luaA_tag_userdata_new(globalconf
.L
, t
);
175 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
182 /** Check if a client is tagged with the specified tag.
183 * \param c the client
185 * \return true if the client is tagged with the tag, false otherwise.
188 is_client_tagged(client_t
*c
, tag_t
*t
)
190 for(int i
= 0; i
< t
->clients
.len
; i
++)
191 if (t
->clients
.tab
[i
] == c
)
197 /** Get the current tags for the specified screen.
198 * Returned pointer must be p_delete'd after.
199 * \param screen screen id
200 * \return a double pointer of tag list finished with a NULL element
203 tags_get_current(int screen
)
205 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
206 tag_t
**out
= p_new(tag_t
*, tags
->len
+ 1);
209 for(int i
= 0; i
< tags
->len
; i
++)
210 if(tags
->tab
[i
]->selected
)
211 out
[n
++] = tags
->tab
[i
];
217 /** Set a tag to be the only one viewed.
218 * \param target the tag to see
221 tag_view_only(tag_t
*target
)
225 tag_array_t
*tags
= &globalconf
.screens
[target
->screen
].tags
;
227 for(int i
= 0; i
< tags
->len
; i
++)
228 tag_view(tags
->tab
[i
], tags
->tab
[i
] == target
);
232 /** View only a tag, selected by its index.
233 * \param screen screen id
234 * \param dindex the index
237 tag_view_only_byindex(int screen
, int dindex
)
239 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
241 if(dindex
< 0 || dindex
>= tags
->len
)
243 tag_view_only(tags
->tab
[dindex
]);
246 /** Create a new tag.
247 * \param L The Lua VM state.
250 * \lreturn A new tag object.
253 luaA_tag_new(lua_State
*L
)
256 const char *name
= luaL_checklstring(L
, 2, &len
);
257 return luaA_tag_userdata_new(L
, tag_new(name
, len
));
260 /** Get or set the clients attached to this tag.
261 * \param L The Lua VM state.
262 * \return The number of elements pushed on stack.
264 * \lparam None or a table of clients to set.
265 * \lreturn A table with the clients attached to this tags.
268 luaA_tag_clients(lua_State
*L
)
270 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
271 client_array_t
*clients
= &(*tag
)->clients
;
274 if(lua_gettop(L
) == 2)
278 luaA_checktable(L
, 2);
279 for(i
= 0; i
< (*tag
)->clients
.len
; i
++)
280 untag_client((*tag
)->clients
.tab
[i
], *tag
);
282 while(lua_next(L
, 2))
284 c
= luaA_checkudata(L
, -1, "client");
285 tag_client(*c
, *tag
);
291 for(i
= 0; i
< clients
->len
; i
++)
293 luaA_client_userdata_new(L
, clients
->tab
[i
]);
294 lua_rawseti(L
, -2, i
+ 1);
301 * \param L The Lua VM state.
302 * \return The number of elements pushed on stack.
304 * \lfield name Tag name.
305 * \lfield screen Screen number of the tag.
306 * \lfield layout Tag layout.
307 * \lfield selected True if the client is selected to be viewed.
310 luaA_tag_index(lua_State
*L
)
313 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
316 if(luaA_usemetatable(L
, 1, 2))
319 attr
= luaL_checklstring(L
, 2, &len
);
321 switch(a_tokenize(attr
, len
))
324 lua_pushstring(L
, (*tag
)->name
);
327 if((*tag
)->screen
== SCREEN_UNDEF
)
329 lua_pushnumber(L
, (*tag
)->screen
+ 1);
332 lua_pushboolean(L
, (*tag
)->selected
);
342 * \param L The Lua VM state.
343 * \return The number of elements pushed on stack.
346 luaA_tag_newindex(lua_State
*L
)
349 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
350 const char *attr
= luaL_checklstring(L
, 2, &len
);
352 switch(a_tokenize(attr
, len
))
358 const char *buf
= luaL_checklstring(L
, 3, &len
);
359 p_delete(&(*tag
)->name
);
360 a_iso2utf8(buf
, len
, &(*tag
)->name
, NULL
);
366 screen
= luaL_checknumber(L
, 3) - 1;
367 luaA_checkscreen(screen
);
370 screen
= SCREEN_UNDEF
;
372 if((*tag
)->screen
!= SCREEN_UNDEF
)
373 tag_remove_from_screen(*tag
);
375 if(screen
!= SCREEN_UNDEF
)
376 tag_append_to_screen(*tag
, &globalconf
.screens
[screen
]);
379 if((*tag
)->screen
!= SCREEN_UNDEF
)
380 tag_view(*tag
, luaA_checkboolean(L
, 3));
386 if((*tag
)->screen
!= SCREEN_UNDEF
&& (*tag
)->selected
)
387 globalconf
.screens
[(*tag
)->screen
].need_arrange
= true;
392 const struct luaL_reg awesome_tag_methods
[] =
394 { "__call", luaA_tag_new
},
397 const struct luaL_reg awesome_tag_meta
[] =
399 { "clients", luaA_tag_clients
},
400 { "__index", luaA_tag_index
},
401 { "__newindex", luaA_tag_newindex
},
402 { "__eq", luaA_tag_eq
},
403 { "__gc", luaA_tag_gc
},
404 { "__tostring", luaA_tag_tostring
},
408 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80