From 9992fd6b1a91d95a8ebedd9d3f0fe9b30611c828 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Mon, 6 Mar 2017 16:18:15 +0100 Subject: [PATCH] client: Add API to query all icons This adds c.icon_sizes which is a table containing the width and height of each available icon. With c:get_icon(i), Lua can query the i-th icon as a lightuserdata. Signed-off-by: Uli Schlachter --- objects/client.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/objects/client.c b/objects/client.c index cb201a6c8..3adfe3ac8 100644 --- a/objects/client.c +++ b/objects/client.c @@ -381,6 +381,19 @@ * @param surface */ +/** + * The available sizes of client icons. This is a table where each entry + * contains the width and height of an icon. + * + * **Signal:** + * + * * *property::icon* + * + * @property icon_sizes + * @tparam table sizes + * @see `get_icon` + */ + /** * Client screen. * @@ -3425,6 +3438,50 @@ luaA_client_keys(lua_State *L) return luaA_key_array_get(L, 1, keys); } +static int +luaA_client_get_icon_sizes(lua_State *L) +{ + int index = 1; + client_t *c = luaA_checkudata(L, 1, &client_class); + + lua_newtable(L); + foreach (s, c->icons) { + /* Create a table { width, height } and append it to the table */ + lua_createtable(L, 2, 0); + + lua_pushinteger(L, cairo_image_surface_get_width(*s)); + lua_rawseti(L, -2, 1); + + lua_pushinteger(L, cairo_image_surface_get_height(*s)); + lua_rawseti(L, -2, 2); + + lua_rawseti(L, -2, index++); + } + return 1; +} + +/** Get the client's n-th icon. + * + * **Signal:** + * + * * *property::icon* + * + * @tparam interger index The index in the list of icons to get. + * @treturn surface A lightuserdata for a cairo surface. This reference must be + * destroyed! + * @function get_icon + */ +static int +luaA_client_get_some_icon(lua_State *L) +{ + client_t *c = luaA_checkudata(L, 1, &client_class); + int index = luaL_checkinteger(L, 2); + luaL_argcheck(L, (index >= 1 && index <= c->icons.len), 2, + "invalid icon index"); + lua_pushlightuserdata(L, cairo_surface_reference(c->icons.tab[index-1])); + return 1; +} + static int client_tostring(lua_State *L, client_t *c) { @@ -3510,6 +3567,7 @@ client_class_setup(lua_State *L) { "titlebar_right", luaA_client_titlebar_right }, { "titlebar_bottom", luaA_client_titlebar_bottom }, { "titlebar_left", luaA_client_titlebar_left }, + { "get_icon", luaA_client_get_some_icon }, { NULL, NULL } }; @@ -3608,6 +3666,10 @@ client_class_setup(lua_State *L) (lua_class_propfunc_t) luaA_client_set_icon, (lua_class_propfunc_t) luaA_client_get_icon, (lua_class_propfunc_t) luaA_client_set_icon); + luaA_class_add_property(&client_class, "icon_sizes", + NULL, + (lua_class_propfunc_t) luaA_client_get_icon_sizes, + NULL); luaA_class_add_property(&client_class, "ontop", (lua_class_propfunc_t) luaA_client_set_ontop, (lua_class_propfunc_t) luaA_client_get_ontop,