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 #include "layoutgen.h"
30 extern awesome_t globalconf
;
32 DO_LUA_NEW(extern, tag_t
, tag
, "tag", tag_ref
)
33 DO_LUA_GC(tag_t
, tag
, "tag", tag_unref
)
34 DO_LUA_EQ(tag_t
, tag
, "tag")
36 /** View or unview a tag.
38 * \param view set visible or not
41 tag_view(tag_t
*tag
, bool view
)
44 ewmh_update_net_current_desktop(screen_virttophys(tag
->screen
));
45 globalconf
.screens
[tag
->screen
].need_arrange
= true;
48 /** Create a new tag. Parameters values are checked.
49 * \param name Tag name.
50 * \param len Tag name length.
51 * \param layout Layout to use.
52 * \param mwfact Master width factor.
53 * \param nmaster Number of master windows.
54 * \param ncol Number of columns for slaves windows.
55 * \return A new tag with all these parameters.
58 tag_new(const char *name
, ssize_t len
, layout_t
*layout
, double mwfact
, int nmaster
, int ncol
)
62 tag
= p_new(tag_t
, 1);
63 a_iso2utf8(&tag
->name
, name
, len
);
67 tag
->screen
= SCREEN_UNDEF
;
70 if(tag
->mwfact
<= 0 || tag
->mwfact
>= 1)
73 if((tag
->nmaster
= nmaster
) < 0)
76 if((tag
->ncol
= ncol
) < 1)
82 /** Append a tag to a screen.
83 * \param tag the tag to append
84 * \param screen the screen id
87 tag_append_to_screen(tag_t
*tag
, screen_t
*s
)
89 int phys_screen
= screen_virttophys(s
->index
);
91 tag
->screen
= s
->index
;
92 tag_array_append(&s
->tags
, tag_ref(&tag
));
93 ewmh_update_net_numbers_of_desktop(phys_screen
);
94 ewmh_update_net_desktop_names(phys_screen
);
95 ewmh_update_workarea(phys_screen
);
97 lua_pushnumber(globalconf
.L
, s
->index
+ 1);
98 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tags
, 1, 0);
101 /** Remove a tag from screen. Tag must be on a screen and have no clients.
102 * \param tag The tag to remove.
105 tag_remove_from_screen(tag_t
*tag
)
107 int screen
= tag
->screen
;
108 int phys_screen
= screen_virttophys(tag
->screen
);
109 tag_array_t
*tags
= &globalconf
.screens
[tag
->screen
].tags
;
111 for(int i
= 0; i
< tags
->len
; i
++)
112 if(tags
->tab
[i
] == tag
)
114 tag_array_take(tags
, i
);
117 ewmh_update_net_numbers_of_desktop(phys_screen
);
118 ewmh_update_net_desktop_names(phys_screen
);
119 ewmh_update_workarea(phys_screen
);
120 tag
->screen
= SCREEN_UNDEF
;
123 lua_pushnumber(globalconf
.L
, screen
+ 1);
124 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tags
, 1, 0);
127 /** Tag a client with specified tag.
128 * \param c the client to tag
129 * \param t the tag to tag the client with
132 tag_client(client_t
*c
, tag_t
*t
)
134 /* don't tag twice */
135 if(is_client_tagged(c
, t
))
139 client_array_append(&t
->clients
, c
);
140 client_saveprops_tags(c
);
141 client_need_arrange(c
);
143 luaA_client_userdata_new(globalconf
.L
, c
);
144 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tagged
, 1, 0);
147 /** Untag a client with specified tag.
148 * \param c the client to tag
149 * \param t the tag to tag the client with
152 untag_client(client_t
*c
, tag_t
*t
)
154 for(int i
= 0; i
< t
->clients
.len
; i
++)
155 if(t
->clients
.tab
[i
] == c
)
157 client_need_arrange(c
);
158 client_array_take(&t
->clients
, i
);
160 client_saveprops_tags(c
);
162 luaA_client_userdata_new(globalconf
.L
, c
);
163 luaA_tag_userdata_new(globalconf
.L
, t
);
164 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
169 /** Check if a client is tagged with the specified tag.
170 * \param c the client
172 * \return true if the client is tagged with the tag, false otherwise.
175 is_client_tagged(client_t
*c
, tag_t
*t
)
177 for(int i
= 0; i
< t
->clients
.len
; i
++)
178 if (t
->clients
.tab
[i
] == c
)
184 /** Get the current tags for the specified screen.
185 * Returned pointer must be p_delete'd after.
186 * \param screen screen id
187 * \return a double pointer of tag list finished with a NULL element
190 tags_get_current(int screen
)
192 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
193 tag_t
**out
= p_new(tag_t
*, tags
->len
+ 1);
196 for(int i
= 0; i
< tags
->len
; i
++)
197 if(tags
->tab
[i
]->selected
)
198 out
[n
++] = tags
->tab
[i
];
204 /** Set a tag to be the only one viewed.
205 * \param target the tag to see
208 tag_view_only(tag_t
*target
)
212 tag_array_t
*tags
= &globalconf
.screens
[target
->screen
].tags
;
214 for(int i
= 0; i
< tags
->len
; i
++)
215 tag_view(tags
->tab
[i
], tags
->tab
[i
] == target
);
219 /** View only a tag, selected by its index.
220 * \param screen screen id
221 * \param dindex the index
224 tag_view_only_byindex(int screen
, int dindex
)
226 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
228 if(dindex
< 0 || dindex
>= tags
->len
)
230 tag_view_only(tags
->tab
[dindex
]);
233 /** Create a new tag.
234 * \param L The Lua VM state.
237 * \lparam A table with at least a name attribute.
238 * Optional attributes are: mwfact, ncol, nmaster and layout.
239 * \lreturn A new tag object.
242 luaA_tag_new(lua_State
*L
)
247 const char *name
, *lay
;
251 luaA_checktable(L
, 2);
253 if(!(name
= luaA_getopt_string(L
, 2, "name", NULL
)))
254 luaL_error(L
, "object tag must have a name");
256 mwfact
= luaA_getopt_number(L
, 2, "mwfact", 0.5);
257 ncol
= luaA_getopt_number(L
, 2, "ncol", 1);
258 nmaster
= luaA_getopt_number(L
, 2, "nmaster", 1);
259 lay
= luaA_getopt_lstring(L
, 2, "layout", "tile", &len
);
261 layout
= name_func_lookup(lay
, LayoutList
);
263 tag
= tag_new(name
, len
,
265 mwfact
, nmaster
, ncol
);
267 return luaA_tag_userdata_new(L
, tag
);
270 /** Get or set the clients attached to this tag.
271 * \param L The Lua VM state.
272 * \return The number of elements pushed on stack.
274 * \lparam None or a table of clients to set.
275 * \lreturn A table with the clients attached to this tags.
278 luaA_tag_clients(lua_State
*L
)
280 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
281 client_array_t
*clients
= &(*tag
)->clients
;
284 if(lua_gettop(L
) == 2)
288 luaA_checktable(L
, 2);
289 for(i
= 0; i
< (*tag
)->clients
.len
; i
++)
290 untag_client((*tag
)->clients
.tab
[i
], *tag
);
292 while(lua_next(L
, 2))
294 c
= luaA_checkudata(L
, -1, "client");
295 tag_client(*c
, *tag
);
301 for(i
= 0; i
< clients
->len
; i
++)
303 luaA_client_userdata_new(L
, clients
->tab
[i
]);
304 lua_rawseti(L
, -2, i
+ 1);
311 * \param L The Lua VM state.
312 * \return The number of elements pushed on stack.
314 * \lfield name Tag name.
315 * \lfield screen Screen number of the tag.
316 * \lfield layout Tag layout.
317 * \lfield selected True if the client is selected to be viewed.
318 * \lfield mwfact Master width factor.
319 * \lfield nmaster Number of master windows.
320 * \lfield ncol Number of column for slave windows.
323 luaA_tag_index(lua_State
*L
)
326 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
329 if(luaA_usemetatable(L
, 1, 2))
332 attr
= luaL_checklstring(L
, 2, &len
);
334 switch(a_tokenize(attr
, len
))
337 lua_pushstring(L
, (*tag
)->name
);
340 if((*tag
)->screen
== SCREEN_UNDEF
)
342 lua_pushnumber(L
, (*tag
)->screen
+ 1);
345 lua_pushstring(L
, name_func_rlookup((*tag
)->layout
, LayoutList
));
348 lua_pushboolean(L
, (*tag
)->selected
);
351 lua_pushnumber(L
, (*tag
)->mwfact
);
354 lua_pushnumber(L
, (*tag
)->nmaster
);
357 lua_pushnumber(L
, (*tag
)->ncol
);
366 /** Get a screen by its name.
367 * \param screen The screen to look into.
368 * \param name The name.
371 tag_getbyname(int screen
, const char *name
)
374 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
376 for(i
= 0; i
< tags
->len
; i
++)
377 if(!a_strcmp(name
, tags
->tab
[i
]->name
))
383 * \param L The Lua VM state.
384 * \return The number of elements pushed on stack.
387 luaA_tag_newindex(lua_State
*L
)
390 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
391 const char *buf
, *attr
= luaL_checklstring(L
, 2, &len
);
396 switch(a_tokenize(attr
, len
))
399 buf
= luaL_checklstring(L
, 3, &len
);
400 if((*tag
)->screen
!= SCREEN_UNDEF
)
401 if(tag_getbyname((*tag
)->screen
, (*tag
)->name
) != *tag
)
402 luaL_error(L
, "a tag with the name `%s' is already on screen %d",
403 buf
, (*tag
)->screen
);
404 p_delete(&(*tag
)->name
);
405 a_iso2utf8(&(*tag
)->name
, buf
, len
);
410 screen
= luaL_checknumber(L
, 3) - 1;
411 luaA_checkscreen(screen
);
414 screen
= SCREEN_UNDEF
;
416 if((*tag
)->screen
!= SCREEN_UNDEF
)
417 tag_remove_from_screen(*tag
);
419 if(screen
!= SCREEN_UNDEF
)
421 if(tag_getbyname(screen
, (*tag
)->name
))
422 luaL_error(L
, "a tag with the name `%s' is already on screen %d",
423 (*tag
)->name
, screen
);
425 tag_append_to_screen(*tag
, &globalconf
.screens
[screen
]);
429 buf
= luaL_checkstring(L
, 3);
430 l
= name_func_lookup(buf
, LayoutList
);
434 luaL_error(L
, "unknown layout: %s", buf
);
437 if((*tag
)->screen
!= SCREEN_UNDEF
)
438 tag_view(*tag
, luaA_checkboolean(L
, 3));
441 d
= luaL_checknumber(L
, 3);
445 luaL_error(L
, "bad value, must be between 0 and 1");
448 i
= luaL_checknumber(L
, 3);
452 luaL_error(L
, "bad value, must be greater than 0");
455 i
= luaL_checknumber(L
, 3);
459 luaL_error(L
, "bad value, must be greater than 1");
465 if((*tag
)->screen
!= SCREEN_UNDEF
&& (*tag
)->selected
)
466 globalconf
.screens
[(*tag
)->screen
].need_arrange
= true;
471 const struct luaL_reg awesome_tag_methods
[] =
473 { "__call", luaA_tag_new
},
476 const struct luaL_reg awesome_tag_meta
[] =
478 { "clients", luaA_tag_clients
},
479 { "__index", luaA_tag_index
},
480 { "__newindex", luaA_tag_newindex
},
481 { "__eq", luaA_tag_eq
},
482 { "__gc", luaA_tag_gc
},
483 { "__tostring", luaA_tag_tostring
},
487 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80