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 /* resave tag prop of all clients so the number of tag will be the
99 for(client_t
*c
= globalconf
.clients
; c
; c
= c
->next
)
100 client_saveprops_tags(c
);
103 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
105 lua_pushnumber(globalconf
.L
, s
->index
+ 1);
106 luaA_tag_userdata_new(globalconf
.L
, tag
);
107 lua_pushliteral(globalconf
.L
, "add");
108 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
112 /** Remove a tag from screen. Tag must be on a screen and have no clients.
113 * \param tag The tag to remove.
116 tag_remove_from_screen(tag_t
*tag
)
118 int screen
= tag
->screen
;
119 int phys_screen
= screen_virttophys(tag
->screen
);
120 tag_array_t
*tags
= &globalconf
.screens
[tag
->screen
].tags
;
122 for(int i
= 0; i
< tags
->len
; i
++)
123 if(tags
->tab
[i
] == tag
)
125 tag_array_take(tags
, i
);
128 ewmh_update_net_numbers_of_desktop(phys_screen
);
129 ewmh_update_net_desktop_names(phys_screen
);
130 ewmh_update_workarea(phys_screen
);
131 tag
->screen
= SCREEN_UNDEF
;
133 /* resave tag prop of all clients so the number of tag will be the
135 for(client_t
*c
= globalconf
.clients
; c
; c
= c
->next
)
136 client_saveprops_tags(c
);
139 if(globalconf
.hooks
.tags
!= LUA_REFNIL
)
141 lua_pushnumber(globalconf
.L
, screen
+ 1);
142 luaA_tag_userdata_new(globalconf
.L
, tag
);
143 lua_pushliteral(globalconf
.L
, "remove");
144 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tags
, 3, 0);
150 /** Tag a client with specified tag.
151 * \param c the client to tag
152 * \param t the tag to tag the client with
155 tag_client(client_t
*c
, tag_t
*t
)
157 /* don't tag twice */
158 if(is_client_tagged(c
, t
))
162 client_array_append(&t
->clients
, c
);
163 client_saveprops_tags(c
);
164 client_need_arrange(c
);
166 if(globalconf
.hooks
.tagged
!= LUA_REFNIL
)
168 luaA_client_userdata_new(globalconf
.L
, c
);
169 luaA_tag_userdata_new(globalconf
.L
, t
);
170 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
174 /** Untag a client with specified tag.
175 * \param c the client to tag
176 * \param t the tag to tag the client with
179 untag_client(client_t
*c
, tag_t
*t
)
181 for(int i
= 0; i
< t
->clients
.len
; i
++)
182 if(t
->clients
.tab
[i
] == c
)
184 client_need_arrange(c
);
185 client_array_take(&t
->clients
, i
);
186 client_saveprops_tags(c
);
188 if(globalconf
.hooks
.tagged
!= LUA_REFNIL
)
190 luaA_client_userdata_new(globalconf
.L
, c
);
191 luaA_tag_userdata_new(globalconf
.L
, t
);
192 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.tagged
, 2, 0);
199 /** Check if a client is tagged with the specified tag.
200 * \param c the client
202 * \return true if the client is tagged with the tag, false otherwise.
205 is_client_tagged(client_t
*c
, tag_t
*t
)
207 for(int i
= 0; i
< t
->clients
.len
; i
++)
208 if (t
->clients
.tab
[i
] == c
)
214 /** Get the current tags for the specified screen.
215 * Returned pointer must be p_delete'd after.
216 * \param screen screen id
217 * \return a double pointer of tag list finished with a NULL element
220 tags_get_current(int screen
)
222 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
223 tag_t
**out
= p_new(tag_t
*, tags
->len
+ 1);
226 for(int i
= 0; i
< tags
->len
; i
++)
227 if(tags
->tab
[i
]->selected
)
228 out
[n
++] = tags
->tab
[i
];
234 /** Set a tag to be the only one viewed.
235 * \param target the tag to see
238 tag_view_only(tag_t
*target
)
242 tag_array_t
*tags
= &globalconf
.screens
[target
->screen
].tags
;
244 for(int i
= 0; i
< tags
->len
; i
++)
245 tag_view(tags
->tab
[i
], tags
->tab
[i
] == target
);
249 /** View only a tag, selected by its index.
250 * \param screen screen id
251 * \param dindex the index
254 tag_view_only_byindex(int screen
, int dindex
)
256 tag_array_t
*tags
= &globalconf
.screens
[screen
].tags
;
258 if(dindex
< 0 || dindex
>= tags
->len
)
260 tag_view_only(tags
->tab
[dindex
]);
263 /** Create a new tag.
264 * \param L The Lua VM state.
267 * \lparam A table with at least a name attribute.
268 * Optional attributes are: mwfact, ncol, nmaster and layout.
269 * \lreturn A new tag object.
272 luaA_tag_new(lua_State
*L
)
277 const char *name
, *lay
;
281 luaA_checktable(L
, 2);
283 if(!(name
= luaA_getopt_lstring(L
, 2, "name", NULL
, &len
)))
284 luaL_error(L
, "object tag must have a name");
286 mwfact
= luaA_getopt_number(L
, 2, "mwfact", 0.5);
287 ncol
= luaA_getopt_number(L
, 2, "ncol", 1);
288 nmaster
= luaA_getopt_number(L
, 2, "nmaster", 1);
289 lay
= luaA_getopt_string(L
, 2, "layout", "tile");
291 layout
= name_func_lookup(lay
, LayoutList
);
293 tag
= tag_new(name
, len
,
295 mwfact
, nmaster
, ncol
);
297 return luaA_tag_userdata_new(L
, tag
);
300 /** Get or set the clients attached to this tag.
301 * \param L The Lua VM state.
302 * \return The number of elements pushed on stack.
304 * \lparam None or a table of clients to set.
305 * \lreturn A table with the clients attached to this tags.
308 luaA_tag_clients(lua_State
*L
)
310 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
311 client_array_t
*clients
= &(*tag
)->clients
;
314 if(lua_gettop(L
) == 2)
318 luaA_checktable(L
, 2);
319 for(i
= 0; i
< (*tag
)->clients
.len
; i
++)
320 untag_client((*tag
)->clients
.tab
[i
], *tag
);
322 while(lua_next(L
, 2))
324 c
= luaA_checkudata(L
, -1, "client");
325 tag_client(*c
, *tag
);
331 for(i
= 0; i
< clients
->len
; i
++)
333 luaA_client_userdata_new(L
, clients
->tab
[i
]);
334 lua_rawseti(L
, -2, i
+ 1);
341 * \param L The Lua VM state.
342 * \return The number of elements pushed on stack.
344 * \lfield name Tag name.
345 * \lfield screen Screen number of the tag.
346 * \lfield layout Tag layout.
347 * \lfield selected True if the client is selected to be viewed.
348 * \lfield mwfact Master width factor.
349 * \lfield nmaster Number of master windows.
350 * \lfield ncol Number of column for slave windows.
353 luaA_tag_index(lua_State
*L
)
356 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
359 if(luaA_usemetatable(L
, 1, 2))
362 attr
= luaL_checklstring(L
, 2, &len
);
364 switch(a_tokenize(attr
, len
))
367 lua_pushstring(L
, (*tag
)->name
);
370 if((*tag
)->screen
== SCREEN_UNDEF
)
372 lua_pushnumber(L
, (*tag
)->screen
+ 1);
375 lua_pushstring(L
, name_func_rlookup((*tag
)->layout
, LayoutList
));
378 lua_pushboolean(L
, (*tag
)->selected
);
381 lua_pushnumber(L
, (*tag
)->mwfact
);
384 lua_pushnumber(L
, (*tag
)->nmaster
);
387 lua_pushnumber(L
, (*tag
)->ncol
);
397 * \param L The Lua VM state.
398 * \return The number of elements pushed on stack.
401 luaA_tag_newindex(lua_State
*L
)
404 tag_t
**tag
= luaA_checkudata(L
, 1, "tag");
405 const char *buf
, *attr
= luaL_checklstring(L
, 2, &len
);
410 switch(a_tokenize(attr
, len
))
413 buf
= luaL_checklstring(L
, 3, &len
);
414 p_delete(&(*tag
)->name
);
415 a_iso2utf8(&(*tag
)->name
, buf
, len
);
420 screen
= luaL_checknumber(L
, 3) - 1;
421 luaA_checkscreen(screen
);
424 screen
= SCREEN_UNDEF
;
426 if((*tag
)->screen
!= SCREEN_UNDEF
)
427 tag_remove_from_screen(*tag
);
429 if(screen
!= SCREEN_UNDEF
)
430 tag_append_to_screen(*tag
, &globalconf
.screens
[screen
]);
433 buf
= luaL_checkstring(L
, 3);
434 l
= name_func_lookup(buf
, LayoutList
);
439 luaA_warn(L
, "unknown layout: %s", buf
);
444 if((*tag
)->screen
!= SCREEN_UNDEF
)
445 tag_view(*tag
, luaA_checkboolean(L
, 3));
448 d
= luaL_checknumber(L
, 3);
453 luaA_warn(L
, "bad value, must be between 0 and 1");
458 i
= luaL_checknumber(L
, 3);
463 luaA_warn(L
, "bad value, must be greater than 0");
468 i
= luaL_checknumber(L
, 3);
473 luaA_warn(L
, "bad value, must be greater than 1");
481 if((*tag
)->screen
!= SCREEN_UNDEF
&& (*tag
)->selected
)
482 globalconf
.screens
[(*tag
)->screen
].need_arrange
= true;
487 const struct luaL_reg awesome_tag_methods
[] =
489 { "__call", luaA_tag_new
},
492 const struct luaL_reg awesome_tag_meta
[] =
494 { "clients", luaA_tag_clients
},
495 { "__index", luaA_tag_index
},
496 { "__newindex", luaA_tag_newindex
},
497 { "__eq", luaA_tag_eq
},
498 { "__gc", luaA_tag_gc
},
499 { "__tostring", luaA_tag_tostring
},
503 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80