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 */