From fed199eee5f8eb84cbee2e01117bf181fa4c0494 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sun, 30 Mar 2014 17:55:42 +0200 Subject: [PATCH] Also allow screen objects were screen indices are expected Signed-off-by: Uli Schlachter --- luaa.h | 6 ------ mouse.c | 12 +++--------- objects/client.c | 26 +++++++------------------- objects/screen.c | 18 +++++++++++++++--- objects/screen.h | 2 ++ 5 files changed, 27 insertions(+), 37 deletions(-) diff --git a/luaa.h b/luaa.h index 799279759..9576908bd 100644 --- a/luaa.h +++ b/luaa.h @@ -39,12 +39,6 @@ signal_object_emit(L, &global_signals, "debug::deprecation", 1); \ } while(0) -#define luaA_checkscreen(screen) \ - do { \ - if(screen < 0 || screen >= globalconf.screens.len) \ - luaL_error(L, "invalid screen number: %d", screen + 1); \ - } while(0) - /** Print a warning about some Lua code. * This is less mean than luaL_error() which setjmp via lua_error() and kills * everything. This only warn, it's up to you to then do what's should be done. diff --git a/mouse.c b/mouse.c index 1045f90ee..6fb4fc7bd 100644 --- a/mouse.c +++ b/mouse.c @@ -133,19 +133,13 @@ static int luaA_mouse_newindex(lua_State *L) { const char *attr = luaL_checkstring(L, 2); - int x, y = 0; - int screen; + screen_t *screen; if (A_STRNEQ(attr, "screen")) return luaA_default_newindex(L); - screen = luaL_checknumber(L, 3) - 1; - luaA_checkscreen(screen); - - x = globalconf.screens.tab[screen]->geometry.x; - y = globalconf.screens.tab[screen]->geometry.y; - - mouse_warp_pointer(globalconf.screen->root, x, y); + screen = luaA_checkscreen(L, 3); + mouse_warp_pointer(globalconf.screen->root, screen->geometry.x, screen->geometry.y); return 0; } diff --git a/objects/client.c b/objects/client.c index bee6ef8e6..aa23cbe8d 100644 --- a/objects/client.c +++ b/objects/client.c @@ -1245,28 +1245,19 @@ client_kill(client_t *c) static int luaA_client_get(lua_State *L) { - int i = 1, screen; + int i = 1; + screen_t *screen = NULL; - screen = luaL_optnumber(L, 1, 0) - 1; + if(!lua_isnoneornil(L, 1)) + screen = luaA_checkscreen(L, 1); lua_newtable(L); - - if(screen == -1) - foreach(c, globalconf.clients) + foreach(c, globalconf.clients) + if(screen == NULL || (*c)->screen == screen) { luaA_object_push(L, *c); lua_rawseti(L, -2, i++); } - else - { - luaA_checkscreen(screen); - foreach(c, globalconf.clients) - if((*c)->screen == globalconf.screens.tab[screen]) - { - luaA_object_push(L, *c); - lua_rawseti(L, -2, i++); - } - } return 1; } @@ -1682,10 +1673,7 @@ luaA_client_geometry(lua_State *L) static int luaA_client_set_screen(lua_State *L, client_t *c) { - int screen = luaL_checknumber(L, -1) - 1; - luaA_checkscreen(screen); - screen_client_moveto(c, globalconf.screens.tab[screen], true); - + screen_client_moveto(c, luaA_checkscreen(L, -1), true); return 0; } diff --git a/objects/screen.c b/objects/screen.c index d002c3547..4cddaa102 100644 --- a/objects/screen.c +++ b/objects/screen.c @@ -59,6 +59,20 @@ screen_wipe(screen_t *s) screen_output_array_wipe(&s->outputs); } +/** Get a screen argument from the lua stack */ +screen_t * +luaA_checkscreen(lua_State *L, int sidx) +{ + if (lua_isnumber(L, sidx)) + { + int screen = lua_tointeger(L, sidx); + if(screen < 1 || screen > globalconf.screens.len) + luaL_error(L, "invalid screen number: %d", screen); + return globalconf.screens.tab[screen - 1]; + } else + return luaA_checkudata(L, sidx, &screen_class); +} + static inline area_t screen_xsitoarea(xcb_xinerama_screen_info_t si) { @@ -439,9 +453,7 @@ luaA_screen_module_index(lua_State *L) if(A_STREQ(output->name, name)) return luaA_object_push(L, screen); - int screen = luaL_checknumber(L, 2) - 1; - luaA_checkscreen(screen); - return luaA_object_push(L, globalconf.screens.tab[screen]); + return luaA_object_push(L, luaA_checkscreen(L, 2)); } LUA_OBJECT_EXPORT_PROPERTY(screen, screen_t, geometry, luaA_pusharea) diff --git a/objects/screen.h b/objects/screen.h index a8db33705..fb8335391 100644 --- a/objects/screen.h +++ b/objects/screen.h @@ -45,5 +45,7 @@ int screen_get_index(screen_t *); area_t display_area_get(void); void screen_client_moveto(client_t *, screen_t *, bool); +screen_t *luaA_checkscreen(lua_State *, int); + #endif // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80