mouse: add newindex, merge coords

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-07-02 09:05:21 +02:00
parent 0cbf293e13
commit ab72f6f02b
3 changed files with 82 additions and 90 deletions

View File

@ -342,12 +342,12 @@ function hook_arrange(screen)
local sel = client.focus_get()
if sel then
local c_c = sel.coords
local m_c = mouse.coords_get()
local m_c = mouse.coords
if m_c.x < c_c.x or m_c.x >= c_c.x + c_c.width or
m_c.y < c_c.y or m_c.y >= c_c.y + c_c.height then
if table.maxn(m_c.buttons) == 0 then
mouse.coords_set({ x = c_c.x + 5, y = c_c.y + 5})
mouse.coords = { x = c_c.x + 5, y = c_c.y + 5}
end
end
end

View File

@ -150,7 +150,7 @@ function P.screen.focus(i)
s = cycle(screen.count(), s + i)
screen.focus(s)
-- Move the mouse on the screen
mouse.coords_set(screen.coords_get(s))
mouse.coords = screen.coords_get(s)
end
--- Return a table with all visible tags
@ -332,7 +332,7 @@ function P.client.movetoscreen(c, s)
end
if s > sc then s = 1 elseif s < 1 then s = sc end
sel.screen = s
mouse.coords_set(screen.coords_get(s))
mouse.coords = screen.coords_get(s)
sel:focus_set()
end
end

164
mouse.c
View File

@ -965,89 +965,6 @@ mouse_client_resize(client_t *c, corner_t corner, bool infobox)
}
}
/** Get mouse coordinates.
* \param L The Lua VM state.
*
* \luastack
* \lreturn A table with `x' and `y' keys set to mouse coordinates, and
* 'buttons' the list of the currently pressed buttons as numbers.
*/
static int
luaA_mouse_coords_get(lua_State *L)
{
int mouse_x, mouse_y, i = 0;
uint16_t mask;
xcb_window_t root;
root = xutil_screen_get(globalconf.connection, globalconf.default_screen)->root;
if(!mouse_query_pointer(root, &mouse_x, &mouse_y, &mask))
return 0;
lua_newtable(L);
lua_pushnumber(L, mouse_x);
lua_setfield(L, -2, "x");
lua_pushnumber(L, mouse_y);
lua_setfield(L, -2, "y");
lua_newtable(L);
if (mask & XCB_BUTTON_MASK_1)
{
lua_pushnumber(L, 1);
lua_rawseti(L, -2, ++i);
}
if (mask & XCB_BUTTON_MASK_2)
{
lua_pushnumber(L, 2);
lua_rawseti(L, -2, ++i);
}
if (mask & XCB_BUTTON_MASK_3)
{
lua_pushnumber(L, 3);
lua_rawseti(L, -2, ++i);
}
if (mask & XCB_BUTTON_MASK_4)
{
lua_pushnumber(L, 4);
lua_rawseti(L, -2, ++i);
}
if (mask & XCB_BUTTON_MASK_5)
{
lua_pushnumber(L, 5);
lua_rawseti(L, -2, ++i);
}
lua_setfield(L, -2, "buttons");
return 1;
}
/** Set mouse coordinates.
* \param L The Lua VM state.
*
* \luastack
* \lparam The x and y coordinate in a table.
*/
static int
luaA_mouse_coords_set(lua_State *L)
{
int x, y, mouse_x, mouse_y;
xcb_window_t root;
uint16_t mask;
luaA_checktable(L, 1);
root = xutil_screen_get(globalconf.connection, globalconf.default_screen)->root;
if(!mouse_query_pointer(root, &mouse_x, &mouse_y, &mask))
return 0;
x = luaA_getopt_number(L, 1, "x", mouse_x);
y = luaA_getopt_number(L, 1, "y", mouse_y);
root = xutil_screen_get(globalconf.connection, globalconf.default_screen)->root;
mouse_warp_pointer(root, x, y);
return 0;
}
/** Resize a client with mouse.
* \param L The Lua VM state.
*
@ -1177,10 +1094,52 @@ static int
luaA_mouse_index(lua_State *L)
{
size_t len;
const char *attr = luaL_checklstring(L, 1, &len);
const char *attr = luaL_checklstring(L, 2, &len);
int mouse_x, mouse_y, i = 0;
uint16_t mask;
xcb_window_t root;
switch(a_tokenize(attr, len))
{
case A_TK_COORDS:
root = xutil_screen_get(globalconf.connection, globalconf.default_screen)->root;
if(!mouse_query_pointer(root, &mouse_x, &mouse_y, &mask))
return 0;
lua_newtable(L);
lua_pushnumber(L, mouse_x);
lua_setfield(L, -2, "x");
lua_pushnumber(L, mouse_y);
lua_setfield(L, -2, "y");
lua_newtable(L);
if (mask & XCB_BUTTON_MASK_1)
{
lua_pushnumber(L, 1);
lua_rawseti(L, -2, ++i);
}
if (mask & XCB_BUTTON_MASK_2)
{
lua_pushnumber(L, 2);
lua_rawseti(L, -2, ++i);
}
if (mask & XCB_BUTTON_MASK_3)
{
lua_pushnumber(L, 3);
lua_rawseti(L, -2, ++i);
}
if (mask & XCB_BUTTON_MASK_4)
{
lua_pushnumber(L, 4);
lua_rawseti(L, -2, ++i);
}
if (mask & XCB_BUTTON_MASK_5)
{
lua_pushnumber(L, 5);
lua_rawseti(L, -2, ++i);
}
lua_setfield(L, -2, "buttons");
break;
default:
return 0;
}
@ -1188,13 +1147,46 @@ luaA_mouse_index(lua_State *L)
return 1;
}
/** Newindex for mouse.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_mouse_newindex(lua_State *L)
{
size_t len;
const char *attr = luaL_checklstring(L, 2, &len);
int mouse_x, mouse_y, x, y = 0;
uint16_t mask;
xcb_window_t root;
switch(a_tokenize(attr, len))
{
case A_TK_COORDS:
luaA_checktable(L, 3);
root = xutil_screen_get(globalconf.connection, globalconf.default_screen)->root;
if(!mouse_query_pointer(root, &mouse_x, &mouse_y, &mask))
return 0;
x = luaA_getopt_number(L, 3, "x", mouse_x);
y = luaA_getopt_number(L, 3, "y", mouse_y);
mouse_warp_pointer(root, x, y);
break;
default:
return 0;
}
return 0;
}
const struct luaL_reg awesome_mouse_methods[] =
{
{ "__call", luaA_mouse_new },
{ "__index", luaA_mouse_index },
{ "__newindex", luaA_mouse_newindex },
{ "screen_get", luaA_mouse_screen_get },
{ "coords_set", luaA_mouse_coords_set },
{ "coords_get", luaA_mouse_coords_get },
{ NULL, NULL }
};
const struct luaL_reg awesome_mouse_meta[] =