From 0a394596054d862ba22f01d04872bcb4e27f8172 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sun, 6 Mar 2016 13:25:10 +0100 Subject: [PATCH] Add a non-index way to iterate over screens This is a preparation for getting rid of screen indices. Signed-off-by: Uli Schlachter --- objects/screen.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/objects/screen.c b/objects/screen.c index 924b30596..a328b122d 100644 --- a/objects/screen.c +++ b/objects/screen.c @@ -624,6 +624,30 @@ luaA_screen_module_index(lua_State *L) return luaA_object_push(L, luaA_checkscreen(L, 2)); } +/** Iterate over screens. + * @usage + * for s in screen do + * print("Oh, wow, we have screen " .. tostring(s)) + * end + * @function screen + */ +static int +luaA_screen_module_call(lua_State *L) +{ + int idx; + + if (lua_isnoneornil(L, 3)) + idx = 0; + else + idx = screen_get_index(luaA_checkscreen(L, 3)); + if (idx >= 0 && idx < globalconf.screens.len) + /* No +1 needed, index starts at 1, C array at 0 */ + luaA_pushscreen(L, globalconf.screens.tab[idx]); + else + lua_pushnil(L); + return 1; +} + LUA_OBJECT_EXPORT_PROPERTY(screen, screen_t, geometry, luaA_pusharea) static int @@ -680,6 +704,7 @@ screen_class_setup(lua_State *L) { "count", luaA_screen_count }, { "__index", luaA_screen_module_index }, { "__newindex", luaA_default_newindex }, + { "__call", luaA_screen_module_call }, { NULL, NULL } };