screen: Fix screen equality comparison (FS#1151)

We did some black magic which broke and was replaced with more black magic. This
now broke using screen objects as table indexes:

  $ echo 'local l, s = {}, screen[1] ; l[s] = 42 ; return l[s]' | awesome-client
  double 42
  $ echo 'local l, s = {}, screen[1] ; l[s] = 42 ; return l[screen[1]]' | awesome-client
  <no output>

Fix this by using just a single lua userdata for representing a screen object.

It would be even better if screens were allocated with lua, but that doesn't
really provide any benefits right now and would be more complicated...

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2013-10-04 15:24:09 +02:00
parent ed66fda1f1
commit 0bdaed2704
2 changed files with 12 additions and 22 deletions

View file

@ -71,8 +71,17 @@ screen_add(screen_t new_screen)
MAX(new_screen.geometry.height, screen_to_test->geometry.height);
return;
}
signal_add(&new_screen.signals, "property::workarea");
screen_array_append(&globalconf.screens, new_screen);
/* Allocate the lua userdata object representing this screen */
screen_t *s = &globalconf.screens.tab[globalconf.screens.len-1];
screen_t **ps = lua_newuserdata(globalconf.L, sizeof(*ps));
*ps = s;
luaL_getmetatable(globalconf.L, "screen");
lua_setmetatable(globalconf.L, -2);
s->userdata = luaA_object_ref(globalconf.L, -1);
}
static bool
@ -398,10 +407,7 @@ screen_client_moveto(client_t *c, screen_t *new_screen, bool doresize)
static int
luaA_pushscreen(lua_State *L, screen_t *s)
{
screen_t **ps = lua_newuserdata(L, sizeof(*ps));
*ps = s;
luaL_getmetatable(L, "screen");
lua_setmetatable(L, -2);
luaA_object_push(L, s->userdata);
return 1;
}
@ -499,23 +505,6 @@ luaA_screen_index(lua_State *L)
return 0;
}
/** A screen.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_screen_equal(lua_State *L)
{
screen_t **ps1;
screen_t **ps2;
ps1 = luaL_checkudata(L, 1, "screen");
ps2 = luaL_checkudata(L, 2, "screen");
lua_pushboolean(L, *ps1 == *ps2);
return 1;
}
/** Add a signal to a screen.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
@ -631,7 +620,6 @@ const struct luaL_Reg awesome_screen_meta[] =
{ "disconnect_signal", luaA_screen_disconnect_signal },
{ "emit_signal", luaA_screen_emit_signal },
{ "__index", luaA_screen_index },
{ "__eq", luaA_screen_equal },
{ NULL, NULL }
};

View file

@ -36,6 +36,8 @@ struct a_screen
signal_array_t signals;
/** The screen outputs informations */
screen_output_array_t outputs;
/** The lua userdata representing this screen */
void *userdata;
};
ARRAY_FUNCS(screen_t, screen, DO_NOTHING)