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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-04-25 05:29:22 +02:00
|
|
|
/** awesome mouse API.
|
|
|
|
*
|
|
|
|
* The mouse buttons are represented as index. The common ones are:
|
|
|
|
*
|
|
|
|
* ![Client geometry](../images/mouse.svg)
|
|
|
|
*
|
|
|
|
* It is possible to be notified of mouse events by connecting to various
|
|
|
|
* `client`, `widget`s and `wibox` signals:
|
|
|
|
*
|
|
|
|
* * `mouse::enter`
|
|
|
|
* * `mouse::leave`
|
|
|
|
* * `mouse::press`
|
|
|
|
* * `mouse::release`
|
|
|
|
* * `mouse::move`
|
|
|
|
*
|
|
|
|
* It is also possible to add generic mouse button callbacks for `client`s,
|
|
|
|
* `wiboxe`s and the `root` window. Those are set in the default `rc.lua` as such:
|
|
|
|
*
|
|
|
|
* **root**:
|
|
|
|
*
|
|
|
|
* root.buttons(awful.util.table.join(
|
|
|
|
* awful.button({ }, 3, function () mymainmenu:toggle() end),
|
|
|
|
* awful.button({ }, 4, awful.tag.viewnext),
|
|
|
|
* awful.button({ }, 5, awful.tag.viewprev)
|
|
|
|
* ))
|
|
|
|
*
|
|
|
|
* **client**:
|
|
|
|
*
|
|
|
|
* clientbuttons = awful.util.table.join(
|
|
|
|
* awful.button({ }, 1, function (c) client.focus = c; c:raise() end),
|
|
|
|
* awful.button({ modkey }, 1, awful.mouse.client.move),
|
|
|
|
* awful.button({ modkey }, 3, awful.mouse.client.resize)
|
|
|
|
* )
|
2016-04-04 10:14:18 +02:00
|
|
|
*
|
|
|
|
* See also `mousegrabber`
|
|
|
|
*
|
2015-02-27 00:24:23 +01:00
|
|
|
* @author Julien Danjou <julien@danjou.info>
|
|
|
|
* @copyright 2008-2009 Julien Danjou
|
2019-06-06 08:40:00 +02:00
|
|
|
* @coreclassmod mouse
|
2015-02-27 00:24:23 +01:00
|
|
|
*/
|
|
|
|
|
2009-04-26 10:46:09 +02:00
|
|
|
#include "mouse.h"
|
2016-04-19 05:26:39 +02:00
|
|
|
#include "math.h"
|
2014-03-30 20:07:48 +02:00
|
|
|
#include "common/util.h"
|
2016-04-17 13:44:05 +02:00
|
|
|
#include "common/xutil.h"
|
2016-04-25 02:55:29 +02:00
|
|
|
#include "common/luaclass.h"
|
2009-09-07 17:22:32 +02:00
|
|
|
#include "globalconf.h"
|
2014-03-30 20:07:48 +02:00
|
|
|
#include "objects/client.h"
|
2010-10-06 13:35:14 +02:00
|
|
|
#include "objects/drawin.h"
|
2014-03-30 20:07:48 +02:00
|
|
|
#include "objects/screen.h"
|
2007-11-13 22:57:57 +01:00
|
|
|
|
2016-04-25 02:55:29 +02:00
|
|
|
static int miss_index_handler = LUA_REFNIL;
|
|
|
|
static int miss_newindex_handler = LUA_REFNIL;
|
|
|
|
|
2016-04-04 10:14:18 +02:00
|
|
|
/**
|
|
|
|
* The `screen` under the cursor
|
2016-04-25 04:09:14 +02:00
|
|
|
* @property screen
|
|
|
|
* @param screen
|
2015-02-27 00:24:23 +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.
|
2013-03-29 16:07:13 +01:00
|
|
|
* \param mask will be set to the current buttons state
|
2009-08-30 07:26:19 +02:00
|
|
|
* \return true on success, false if an error occurred
|
2008-06-04 19:05:46 +02:00
|
|
|
**/
|
2013-03-29 16:07:13 +01:00
|
|
|
bool
|
|
|
|
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)
|
2013-03-31 18:15:25 +02:00
|
|
|
{
|
|
|
|
p_delete(&query_ptr_r);
|
2008-06-04 19:05:46 +02:00
|
|
|
return false;
|
2013-03-31 18:15:25 +02:00
|
|
|
}
|
2008-06-04 19:05:46 +02:00
|
|
|
|
|
|
|
*x = query_ptr_r->win_x;
|
|
|
|
*y = query_ptr_r->win_y;
|
2011-11-17 16:38:39 +01:00
|
|
|
|
2013-03-29 16:07:13 +01:00
|
|
|
if(mask)
|
|
|
|
*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.
|
|
|
|
* \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.
|
2009-08-30 07:26:19 +02:00
|
|
|
* \return True on success, false if an error occurred.
|
2008-08-14 22:16:13 +02:00
|
|
|
*/
|
2013-03-29 16:07:13 +01:00
|
|
|
static bool
|
|
|
|
mouse_query_pointer_root(int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
|
2008-08-14 22:16:13 +02:00
|
|
|
{
|
2013-03-29 16:07:13 +01:00
|
|
|
xcb_window_t root = globalconf.screen->root;
|
|
|
|
|
|
|
|
return mouse_query_pointer(root, x, y, child, mask);
|
2008-08-14 22:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** 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
|
2016-04-17 13:44:05 +02:00
|
|
|
mouse_warp_pointer(xcb_window_t window, int16_t x, int16_t y)
|
2008-06-04 19:05:46 +02:00
|
|
|
{
|
|
|
|
xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
|
2011-11-17 16:38:39 +01:00
|
|
|
0, 0, 0, 0, x, y);
|
2008-06-04 19:05:46 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2016-03-27 10:22:14 +02:00
|
|
|
* \lfield screen Mouse screen.
|
2008-07-02 09:00:59 +02:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_mouse_index(lua_State *L)
|
|
|
|
{
|
2010-09-02 19:09:34 +02:00
|
|
|
const char *attr = luaL_checkstring(L, 2);
|
2008-12-03 23:03:44 +01:00
|
|
|
int16_t mouse_x, mouse_y;
|
2008-07-02 09:00:59 +02:00
|
|
|
|
2011-11-17 16:38:39 +01:00
|
|
|
/* attr is not "screen"?! */
|
2016-04-25 02:55:29 +02:00
|
|
|
if (A_STRNEQ(attr, "screen")) {
|
|
|
|
if (miss_index_handler != LUA_REFNIL) {
|
2018-07-26 00:31:02 +02:00
|
|
|
return luaA_call_handler(L, miss_index_handler);
|
2016-04-25 02:55:29 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return luaA_default_index(L);
|
|
|
|
}
|
2008-07-02 09:14:17 +02:00
|
|
|
|
2013-03-29 16:07:13 +01:00
|
|
|
if (!mouse_query_pointer_root(&mouse_x, &mouse_y, NULL, NULL))
|
2014-03-06 22:18:35 +01:00
|
|
|
{
|
|
|
|
/* Nothing ever handles mouse.screen being nil. Lying is better than
|
|
|
|
* having lots of lua errors in this case.
|
|
|
|
*/
|
|
|
|
if (globalconf.focus.client)
|
2016-03-26 18:13:55 +01:00
|
|
|
luaA_object_push(L, globalconf.focus.client->screen);
|
2014-03-06 22:18:35 +01:00
|
|
|
else
|
2016-03-26 18:13:55 +01:00
|
|
|
luaA_object_push(L, screen_get_primary());
|
2014-03-06 22:18:35 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2008-07-02 09:00:59 +02:00
|
|
|
|
2016-03-26 18:13:55 +01:00
|
|
|
luaA_object_push(L, screen_getbycoord(mouse_x, mouse_y));
|
2008-07-02 09:00:59 +02:00
|
|
|
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)
|
|
|
|
{
|
2010-09-02 19:09:34 +02:00
|
|
|
const char *attr = luaL_checkstring(L, 2);
|
2014-03-30 17:55:42 +02:00
|
|
|
screen_t *screen;
|
2008-07-02 09:05:21 +02:00
|
|
|
|
2016-04-25 02:55:29 +02:00
|
|
|
if (A_STRNEQ(attr, "screen")) {
|
|
|
|
/* Call the lua mouse property handler */
|
|
|
|
if (miss_newindex_handler != LUA_REFNIL) {
|
2018-07-26 00:31:02 +02:00
|
|
|
return luaA_call_handler(L, miss_newindex_handler);
|
2016-04-25 02:55:29 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return luaA_default_newindex(L);
|
|
|
|
}
|
2008-08-14 22:16:13 +02:00
|
|
|
|
2014-03-30 17:55:42 +02:00
|
|
|
screen = luaA_checkscreen(L, 3);
|
|
|
|
mouse_warp_pointer(globalconf.screen->root, screen->geometry.x, screen->geometry.y);
|
2008-07-02 09:05:21 +02:00
|
|
|
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
|
2013-03-29 16:07:13 +01:00
|
|
|
luaA_mouse_pushstatus(lua_State *L, int x, int y, uint16_t mask)
|
2008-11-13 16:08:34 +01:00
|
|
|
{
|
2009-04-25 15:04:27 +02:00
|
|
|
lua_createtable(L, 0, 2);
|
2015-05-26 07:09:12 +02:00
|
|
|
lua_pushinteger(L, x);
|
2008-11-13 16:08:34 +01:00
|
|
|
lua_setfield(L, -2, "x");
|
2015-05-26 07:09:12 +02:00
|
|
|
lua_pushinteger(L, y);
|
2008-11-13 16:08:34 +01:00
|
|
|
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
|
|
|
|
2013-03-29 16:07:13 +01:00
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
for(uint16_t maski = XCB_BUTTON_MASK_1; maski <= XCB_BUTTON_MASK_5; maski <<= 1)
|
2008-11-13 16:08:34 +01:00
|
|
|
{
|
2013-03-29 16:07:13 +01:00
|
|
|
if(mask & maski)
|
|
|
|
lua_pushboolean(L, true);
|
|
|
|
else
|
|
|
|
lua_pushboolean(L, false);
|
|
|
|
lua_rawseti(L, -2, i++);
|
2008-11-13 16:08:34 +01:00
|
|
|
}
|
|
|
|
lua_setfield(L, -2, "buttons");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-04-25 04:21:35 +02:00
|
|
|
/* documented in lib/awful/mouse/init.lua */
|
2008-08-20 12:10:22 +02:00
|
|
|
static int
|
|
|
|
luaA_mouse_coords(lua_State *L)
|
|
|
|
{
|
2013-03-29 16:07:13 +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;
|
2008-08-20 12:10:22 +02:00
|
|
|
|
2009-10-27 11:18:16 +01:00
|
|
|
if(lua_gettop(L) >= 1)
|
2008-08-20 12:10:22 +02:00
|
|
|
{
|
2008-08-20 16:25:45 +02:00
|
|
|
luaA_checktable(L, 1);
|
2009-10-27 11:18:16 +01:00
|
|
|
bool ignore_enter_notify = (lua_gettop(L) == 2 && luaA_checkboolean(L, 2));
|
2008-08-20 12:10:22 +02:00
|
|
|
|
2013-03-29 16:07:13 +01:00
|
|
|
if(!mouse_query_pointer_root(&mouse_x, &mouse_y, NULL, &mask))
|
2008-08-20 12:10:22 +02:00
|
|
|
return 0;
|
|
|
|
|
2016-04-19 05:26:39 +02:00
|
|
|
x = round(luaA_getopt_number_range(L, 1, "x", mouse_x, MIN_X11_COORDINATE, MAX_X11_COORDINATE));
|
|
|
|
y = round(luaA_getopt_number_range(L, 1, "y", mouse_y, MIN_X11_COORDINATE, MAX_X11_COORDINATE));
|
2008-08-20 12:10:22 +02:00
|
|
|
|
2009-10-27 11:18:16 +01:00
|
|
|
if(ignore_enter_notify)
|
|
|
|
client_ignore_enterleave_events();
|
|
|
|
|
2010-08-16 14:10:58 +02:00
|
|
|
mouse_warp_pointer(globalconf.screen->root, x, y);
|
2009-10-27 11:18:16 +01:00
|
|
|
|
|
|
|
if(ignore_enter_notify)
|
|
|
|
client_restore_enterleave_events();
|
|
|
|
|
2008-08-20 12:10:22 +02:00
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
|
2013-03-29 16:07:13 +01:00
|
|
|
if(!mouse_query_pointer_root(&mouse_x, &mouse_y, NULL, &mask))
|
2008-08-20 12:10:22 +02:00
|
|
|
return 0;
|
|
|
|
|
2013-03-29 16:07:13 +01:00
|
|
|
return luaA_mouse_pushstatus(L, mouse_x, mouse_y, mask);
|
2008-08-20 12:10:22 +02:00
|
|
|
}
|
|
|
|
|
2015-02-27 00:24:23 +01:00
|
|
|
/** Get the client or any object which is under the pointer.
|
|
|
|
*
|
2015-07-25 00:00:07 +02:00
|
|
|
* @treturn client.object|nil A client or nil.
|
2015-02-27 00:24:23 +01:00
|
|
|
* @function object_under_pointer
|
2008-11-13 17:44:09 +01:00
|
|
|
*/
|
|
|
|
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
|
|
|
{
|
2008-12-03 23:03:44 +01:00
|
|
|
int16_t mouse_x, mouse_y;
|
|
|
|
xcb_window_t child;
|
|
|
|
|
2013-03-29 16:07:13 +01:00
|
|
|
if(!mouse_query_pointer_root(&mouse_x, &mouse_y, &child, NULL))
|
2008-12-03 23:03:44 +01:00
|
|
|
return 0;
|
|
|
|
|
2010-10-06 13:35:14 +02:00
|
|
|
drawin_t *drawin;
|
2008-12-03 23:03:44 +01:00
|
|
|
client_t *client;
|
2011-11-17 16:38:39 +01:00
|
|
|
|
2010-10-06 13:35:14 +02:00
|
|
|
if((drawin = drawin_getbywin(child)))
|
2013-01-05 20:53:13 +01:00
|
|
|
return luaA_object_push(L, drawin);
|
2008-12-03 23:03:44 +01:00
|
|
|
|
2011-11-17 16:38:39 +01:00
|
|
|
if((client = client_getbyframewin(child)))
|
2014-12-06 11:07:20 +01:00
|
|
|
return luaA_object_push(L, client);
|
2008-12-03 23:03:44 +01:00
|
|
|
|
2008-11-13 17:44:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-25 02:55:29 +02:00
|
|
|
/**
|
|
|
|
* Add a custom property handler (getter).
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_mouse_set_index_miss_handler(lua_State *L)
|
|
|
|
{
|
|
|
|
return luaA_registerfct(L, 1, &miss_index_handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a custom property handler (setter).
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_mouse_set_newindex_miss_handler(lua_State *L)
|
|
|
|
{
|
|
|
|
return luaA_registerfct(L, 1, &miss_newindex_handler);
|
|
|
|
}
|
|
|
|
|
2012-06-06 23:07:07 +02:00
|
|
|
const struct luaL_Reg awesome_mouse_methods[] =
|
2008-06-13 15:35:47 +02:00
|
|
|
{
|
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 },
|
2016-04-25 02:55:29 +02:00
|
|
|
{ "set_index_miss_handler", luaA_mouse_set_index_miss_handler},
|
|
|
|
{ "set_newindex_miss_handler", luaA_mouse_set_newindex_miss_handler},
|
2008-05-20 15:39:47 +02:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
2012-06-06 23:07:07 +02:00
|
|
|
const struct luaL_Reg awesome_mouse_meta[] =
|
2008-06-13 15:35:47 +02:00
|
|
|
{
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|