From 464e103a4ed5f621444167b2e6000ed273ac21a0 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Thu, 25 Jul 2019 21:53:21 -0400 Subject: [PATCH] screen: Add a `name` property to the C API. This will be useful to address the screens by roles/names in the client and tag rules. Since the sceen rules will make setups where sceens are attached and removed much easier to work with, using indexes or output names in the rules becomes a limitation. --- objects/screen.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-- objects/screen.h | 2 ++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/objects/screen.c b/objects/screen.c index 572e097bf..f4510215c 100644 --- a/objects/screen.c +++ b/objects/screen.c @@ -594,6 +594,15 @@ screen_add(lua_State *L, screen_array_t *screens) return new_screen; } +static void +screen_wipe(screen_t *c) +{ + if (c->name) { + free(c->name); + c->name = NULL; + } +} + /* Monitors were introduced in RandR 1.5 */ #ifdef XCB_RANDR_GET_MONITORS @@ -1516,7 +1525,9 @@ luaA_screen_module_index(lua_State *L) } foreach(screen, globalconf.screens) - if ((*screen)->viewport) + if ((*screen)->name && A_STREQ(name, (*screen)->name)) + return luaA_object_push(L, *screen); + else if ((*screen)->viewport) foreach(output, (*screen)->viewport->outputs) if(A_STREQ(output->name, name)) return luaA_object_push(L, *screen); @@ -1613,6 +1624,33 @@ luaA_screen_get_workarea(lua_State *L, screen_t *s) return 1; } +static int +luaA_screen_set_name(lua_State *L, screen_t *s) +{ + const char *buf = luaL_checkstring(L, -1); + + if (s->name) + free(s->name); + + s->name = a_strdup(buf); + + return 0; +} + +static int +luaA_screen_get_name(lua_State *L, screen_t *s) +{ + lua_pushstring(L, s->name ? s->name : "screen"); + + /* Fallback to "screen1", "screen2", etc if no name is set */ + if (!s->name) { + lua_pushinteger(L, screen_get_index(s)); + lua_concat(L, 2); + } + + return 1; +} + /** Get the number of screens. * * @return The screen count, at least 1. @@ -1820,7 +1858,7 @@ screen_class_setup(lua_State *L) luaA_class_setup(L, &screen_class, "screen", NULL, (lua_class_allocator_t) screen_new, - (lua_class_collector_t) NULL, + (lua_class_collector_t) screen_wipe, (lua_class_checker_t) screen_checker, luaA_class_index_miss_property, luaA_class_newindex_miss_property, screen_methods, screen_meta); @@ -1844,6 +1882,10 @@ screen_class_setup(lua_State *L) NULL, (lua_class_propfunc_t) luaA_screen_get_workarea, NULL); + luaA_class_add_property(&screen_class, "name", + (lua_class_propfunc_t) luaA_screen_set_name, + (lua_class_propfunc_t) luaA_screen_get_name, + (lua_class_propfunc_t) luaA_screen_set_name); } /* @DOC_cobject_COMMON@ */ diff --git a/objects/screen.h b/objects/screen.h index 54f8104a8..5971719ad 100644 --- a/objects/screen.h +++ b/objects/screen.h @@ -47,6 +47,8 @@ struct a_screen area_t geometry; /** Screen workarea */ area_t workarea; + /** The name of the screen */ + char *name; /** Opaque pointer to the viewport */ struct viewport_t *viewport; /** Some XID identifying this screen */