2007-11-13 22:57:57 +01:00
|
|
|
/*
|
|
|
|
* mouse.c - mouse managing
|
|
|
|
*
|
2009-04-26 10:46:09 +02:00
|
|
|
* Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
|
2007-11-13 22:57:57 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-04-26 10:46:09 +02:00
|
|
|
#include "mouse.h"
|
2007-11-13 22:57:57 +01:00
|
|
|
#include "screen.h"
|
2009-04-26 10:46:09 +02:00
|
|
|
#include "client.h"
|
|
|
|
#include "structs.h"
|
2009-05-10 14:55:13 +02:00
|
|
|
#include "wibox.h"
|
|
|
|
#include "common/tokenize.h"
|
2009-04-17 16:52:25 +02:00
|
|
|
#include "common/xutil.h"
|
2007-11-13 22:57:57 +01:00
|
|
|
|
2008-08-14 22:16:13 +02:00
|
|
|
/** Get the pointer position.
|
|
|
|
* \param window The window to get position on.
|
2008-06-04 19:05:46 +02:00
|
|
|
* \param x will be set to the Pointer-x-coordinate relative to window
|
|
|
|
* \param y will be set to the Pointer-y-coordinate relative to window
|
2008-12-03 23:03:44 +01:00
|
|
|
* \param child Will be set to the window under the pointer.
|
2008-06-19 16:51:33 +02:00
|
|
|
* \param mask will be set to the current buttons state
|
2008-06-04 19:05:46 +02:00
|
|
|
* \return true on success, false if an error occured
|
|
|
|
**/
|
2008-11-13 16:08:14 +01:00
|
|
|
bool
|
2008-12-03 23:03:44 +01:00
|
|
|
mouse_query_pointer(xcb_window_t window, int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
|
2008-06-04 19:05:46 +02:00
|
|
|
{
|
|
|
|
xcb_query_pointer_cookie_t query_ptr_c;
|
|
|
|
xcb_query_pointer_reply_t *query_ptr_r;
|
|
|
|
|
2008-06-13 21:33:38 +02:00
|
|
|
query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, window);
|
2008-06-04 19:05:46 +02:00
|
|
|
query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
|
|
|
|
|
2008-08-14 22:16:13 +02:00
|
|
|
if(!query_ptr_r || !query_ptr_r->same_screen)
|
2008-06-04 19:05:46 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
*x = query_ptr_r->win_x;
|
|
|
|
*y = query_ptr_r->win_y;
|
2009-03-29 20:26:39 +02:00
|
|
|
if(mask)
|
2008-06-19 16:51:33 +02:00
|
|
|
*mask = query_ptr_r->mask;
|
2008-12-03 23:03:44 +01:00
|
|
|
if(child)
|
|
|
|
*child = query_ptr_r->child;
|
2008-06-04 19:05:46 +02:00
|
|
|
|
|
|
|
p_delete(&query_ptr_r);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-08-14 22:16:13 +02:00
|
|
|
/** Get the pointer position on the screen.
|
2009-07-27 06:20:00 +02:00
|
|
|
* \param s This will be set to the screen the mouse is on.
|
2008-08-14 22:16:13 +02:00
|
|
|
* \param x This will be set to the Pointer-x-coordinate relative to window.
|
|
|
|
* \param y This will be set to the Pointer-y-coordinate relative to window.
|
2008-12-03 23:03:44 +01:00
|
|
|
* \param child This will be set to the window under the pointer.
|
2008-08-14 22:16:13 +02:00
|
|
|
* \param mask This will be set to the current buttons state.
|
|
|
|
* \return True on success, false if an error occured.
|
|
|
|
*/
|
|
|
|
static bool
|
2009-04-17 16:14:09 +02:00
|
|
|
mouse_query_pointer_root(screen_t **s, int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
|
2008-08-14 22:16:13 +02:00
|
|
|
{
|
|
|
|
for(int screen = 0;
|
|
|
|
screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
|
|
|
|
screen++)
|
|
|
|
{
|
|
|
|
xcb_window_t root = xutil_screen_get(globalconf.connection, screen)->root;
|
|
|
|
|
2008-12-03 23:03:44 +01:00
|
|
|
if(mouse_query_pointer(root, x, y, child, mask))
|
2008-08-14 22:16:13 +02:00
|
|
|
{
|
2009-04-17 16:14:09 +02:00
|
|
|
*s = &globalconf.screens.tab[screen];
|
2008-08-14 22:16:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Set the pointer position.
|
|
|
|
* \param window The destination window.
|
|
|
|
* \param x X-coordinate inside window.
|
|
|
|
* \param y Y-coordinate inside window.
|
2008-06-04 19:05:46 +02:00
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
mouse_warp_pointer(xcb_window_t window, int x, int y)
|
|
|
|
{
|
|
|
|
xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
|
|
|
|
0, 0, 0, 0, x, y );
|
|
|
|
}
|
|
|
|
|
2008-08-12 13:23:10 +02:00
|
|
|
/** Mouse library.
|
2008-07-02 09:00:59 +02:00
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
2008-07-10 09:22:23 +02:00
|
|
|
* \luastack
|
|
|
|
* \lfield coords Mouse coordinates.
|
|
|
|
* \lfield screen Mouse screen number.
|
2008-07-02 09:00:59 +02:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_mouse_index(lua_State *L)
|
|
|
|
{
|
|
|
|
size_t len;
|
2008-07-02 09:05:21 +02:00
|
|
|
const char *attr = luaL_checklstring(L, 2, &len);
|
2008-12-03 23:03:44 +01:00
|
|
|
int16_t mouse_x, mouse_y;
|
2009-04-17 16:14:09 +02:00
|
|
|
screen_t *screen;
|
2008-07-02 09:00:59 +02:00
|
|
|
|
|
|
|
switch(a_tokenize(attr, len))
|
|
|
|
{
|
2008-07-02 09:14:17 +02:00
|
|
|
case A_TK_SCREEN:
|
2008-12-03 23:03:44 +01:00
|
|
|
if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, NULL))
|
2008-07-02 09:14:17 +02:00
|
|
|
return 0;
|
|
|
|
|
2009-04-17 16:14:09 +02:00
|
|
|
screen = screen_getbycoord(screen, mouse_x, mouse_y);
|
2008-07-02 09:14:17 +02:00
|
|
|
|
2009-04-18 17:51:31 +02:00
|
|
|
lua_pushnumber(L, screen_array_indexof(&globalconf.screens, screen) + 1);
|
2008-07-02 09:14:17 +02:00
|
|
|
break;
|
2008-07-02 09:00:59 +02:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-07-02 09:05:21 +02:00
|
|
|
/** 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);
|
2008-08-20 12:10:22 +02:00
|
|
|
int x, y = 0;
|
2008-07-02 09:05:21 +02:00
|
|
|
xcb_window_t root;
|
2008-08-14 22:16:13 +02:00
|
|
|
int screen, phys_screen;
|
2008-07-02 09:05:21 +02:00
|
|
|
|
|
|
|
switch(a_tokenize(attr, len))
|
|
|
|
{
|
2008-08-14 22:16:13 +02:00
|
|
|
case A_TK_SCREEN:
|
|
|
|
screen = luaL_checknumber(L, 3) - 1;
|
|
|
|
luaA_checkscreen(screen);
|
|
|
|
|
|
|
|
/* we need the physical one to get the root window */
|
|
|
|
phys_screen = screen_virttophys(screen);
|
|
|
|
root = xutil_screen_get(globalconf.connection, phys_screen)->root;
|
|
|
|
|
2009-04-17 16:14:09 +02:00
|
|
|
x = globalconf.screens.tab[screen].geometry.x;
|
|
|
|
y = globalconf.screens.tab[screen].geometry.y;
|
2008-08-14 22:16:13 +02:00
|
|
|
|
2008-07-02 09:05:21 +02:00
|
|
|
mouse_warp_pointer(root, x, y);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-13 16:08:34 +01:00
|
|
|
/** Push a table with mouse status.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param x The x coordinate.
|
|
|
|
* \param y The y coordinate.
|
|
|
|
* \param mask The button mask.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
luaA_mouse_pushstatus(lua_State *L, int x, int y, uint16_t mask)
|
|
|
|
{
|
2009-04-25 15:04:27 +02:00
|
|
|
lua_createtable(L, 0, 2);
|
2008-11-13 16:08:34 +01:00
|
|
|
lua_pushnumber(L, x);
|
|
|
|
lua_setfield(L, -2, "x");
|
|
|
|
lua_pushnumber(L, y);
|
|
|
|
lua_setfield(L, -2, "y");
|
|
|
|
|
2009-04-25 15:04:27 +02:00
|
|
|
lua_createtable(L, 5, 0);
|
2008-11-13 16:08:34 +01:00
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
for(uint16_t maski = XCB_BUTTON_MASK_1; maski <= XCB_BUTTON_MASK_5; maski <<= 1)
|
|
|
|
{
|
|
|
|
if(mask & maski)
|
|
|
|
lua_pushboolean(L, true);
|
|
|
|
else
|
|
|
|
lua_pushboolean(L, false);
|
|
|
|
lua_rawseti(L, -2, i++);
|
|
|
|
}
|
|
|
|
lua_setfield(L, -2, "buttons");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-08-20 12:10:22 +02:00
|
|
|
/** Get or set the mouse coords.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lparam None or a table with x and y keys as mouse coordinates.
|
|
|
|
* \lreturn A table with mouse coordinates.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_mouse_coords(lua_State *L)
|
|
|
|
{
|
2008-11-13 16:08:34 +01:00
|
|
|
uint16_t mask;
|
2009-04-17 16:14:09 +02:00
|
|
|
int x, y;
|
2008-12-03 23:03:44 +01:00
|
|
|
int16_t mouse_x, mouse_y;
|
2009-04-17 16:14:09 +02:00
|
|
|
screen_t *screen;
|
2008-08-20 12:10:22 +02:00
|
|
|
|
2008-08-20 16:25:45 +02:00
|
|
|
if(lua_gettop(L) == 1)
|
2008-08-20 12:10:22 +02:00
|
|
|
{
|
|
|
|
xcb_window_t root;
|
|
|
|
|
2008-08-20 16:25:45 +02:00
|
|
|
luaA_checktable(L, 1);
|
2008-08-20 12:10:22 +02:00
|
|
|
|
2008-12-03 23:03:44 +01:00
|
|
|
if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, &mask))
|
2008-08-20 12:10:22 +02:00
|
|
|
return 0;
|
|
|
|
|
2008-08-20 16:25:45 +02:00
|
|
|
x = luaA_getopt_number(L, 1, "x", mouse_x);
|
|
|
|
y = luaA_getopt_number(L, 1, "y", mouse_y);
|
2008-08-20 12:10:22 +02:00
|
|
|
|
2009-04-18 17:51:31 +02:00
|
|
|
root = xutil_screen_get(globalconf.connection,
|
|
|
|
screen_array_indexof(&globalconf.screens, screen))->root;
|
2008-08-20 12:10:22 +02:00
|
|
|
mouse_warp_pointer(root, x, y);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
|
2008-12-03 23:03:44 +01:00
|
|
|
if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, &mask))
|
2008-08-20 12:10:22 +02:00
|
|
|
return 0;
|
|
|
|
|
2008-11-13 16:08:34 +01:00
|
|
|
return luaA_mouse_pushstatus(L, mouse_x, mouse_y, mask);
|
2008-08-20 12:10:22 +02:00
|
|
|
}
|
|
|
|
|
2008-11-13 17:44:09 +01:00
|
|
|
/** Get the client which is under the pointer.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lreturn A client or nil.
|
|
|
|
*/
|
|
|
|
static int
|
2008-12-03 23:03:44 +01:00
|
|
|
luaA_mouse_object_under_pointer(lua_State *L)
|
2008-11-13 17:44:09 +01:00
|
|
|
{
|
2009-04-17 16:14:09 +02:00
|
|
|
screen_t *screen;
|
2008-12-03 23:03:44 +01:00
|
|
|
int16_t mouse_x, mouse_y;
|
|
|
|
xcb_window_t child;
|
|
|
|
|
|
|
|
if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &child, NULL))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
wibox_t *wibox;
|
|
|
|
client_t *client;
|
|
|
|
if((wibox = wibox_getbywin(child)))
|
2008-11-13 17:44:09 +01:00
|
|
|
{
|
2009-04-11 00:27:06 +02:00
|
|
|
wibox_push(L, wibox);
|
2008-12-03 23:03:44 +01:00
|
|
|
|
|
|
|
int16_t x = mouse_x - wibox->sw.geometry.x;
|
|
|
|
int16_t y = mouse_y - wibox->sw.geometry.y;
|
2008-11-13 17:44:09 +01:00
|
|
|
|
2009-05-10 16:17:39 +02:00
|
|
|
widget_t *widget = widget_getbycoords(wibox->sw.orientation, &wibox->widgets,
|
2008-12-03 23:03:44 +01:00
|
|
|
wibox->sw.geometry.width,
|
|
|
|
wibox->sw.geometry.height,
|
|
|
|
&x, &y);
|
|
|
|
|
|
|
|
if(widget)
|
2008-11-13 17:44:09 +01:00
|
|
|
{
|
2009-04-09 15:12:17 +02:00
|
|
|
widget_push(L, widget);
|
2008-12-03 23:03:44 +01:00
|
|
|
return 2;
|
2008-11-13 17:44:09 +01:00
|
|
|
}
|
2008-12-03 23:03:44 +01:00
|
|
|
return 1;
|
2008-11-13 17:44:09 +01:00
|
|
|
}
|
2008-12-03 23:03:44 +01:00
|
|
|
else if((client = client_getbywin(child)))
|
2009-04-10 15:23:55 +02:00
|
|
|
return client_push(globalconf.L, client);
|
2008-12-03 23:03:44 +01:00
|
|
|
|
2008-11-13 17:44:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-13 15:35:47 +02:00
|
|
|
const struct luaL_reg awesome_mouse_methods[] =
|
|
|
|
{
|
2008-07-02 09:00:59 +02:00
|
|
|
{ "__index", luaA_mouse_index },
|
2008-07-02 09:05:21 +02:00
|
|
|
{ "__newindex", luaA_mouse_newindex },
|
2008-08-20 12:10:22 +02:00
|
|
|
{ "coords", luaA_mouse_coords },
|
2008-12-03 23:03:44 +01:00
|
|
|
{ "object_under_pointer", luaA_mouse_object_under_pointer },
|
2008-05-20 15:39:47 +02:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
2008-06-13 15:35:47 +02:00
|
|
|
const struct luaL_reg awesome_mouse_meta[] =
|
|
|
|
{
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2007-12-18 09:24:15 +01:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|