2008-11-13 16:08:34 +01:00
|
|
|
/*
|
|
|
|
* mousegrabber.c - mouse pointer grabbing
|
|
|
|
*
|
2009-08-17 16:38:56 +02:00
|
|
|
* Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
|
2008-11-13 16:08:34 +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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-12-21 06:54:15 +01:00
|
|
|
/** Set a callback to process all mouse events.
|
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 mousegrabber
|
2015-02-27 00:24:23 +01:00
|
|
|
*/
|
|
|
|
|
2008-11-13 16:08:34 +01:00
|
|
|
#include "mousegrabber.h"
|
|
|
|
#include "common/xcursor.h"
|
2014-03-30 20:07:48 +02:00
|
|
|
#include "mouse.h"
|
|
|
|
#include "globalconf.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdbool.h>
|
2008-11-13 16:08:34 +01:00
|
|
|
|
|
|
|
/** Grab the mouse.
|
|
|
|
* \param cursor The cursor to use while grabbing.
|
|
|
|
* \return True if mouse was grabbed.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
mousegrabber_grab(xcb_cursor_t cursor)
|
|
|
|
{
|
2010-08-16 14:10:58 +02:00
|
|
|
xcb_window_t root = globalconf.screen->root;
|
2008-11-13 16:08:34 +01:00
|
|
|
|
|
|
|
for(int i = 1000; i; i--)
|
|
|
|
{
|
|
|
|
xcb_grab_pointer_reply_t *grab_ptr_r;
|
|
|
|
xcb_grab_pointer_cookie_t grab_ptr_c =
|
|
|
|
xcb_grab_pointer_unchecked(globalconf.connection, false, root,
|
|
|
|
XCB_EVENT_MASK_BUTTON_PRESS
|
|
|
|
| XCB_EVENT_MASK_BUTTON_RELEASE
|
|
|
|
| XCB_EVENT_MASK_POINTER_MOTION,
|
|
|
|
XCB_GRAB_MODE_ASYNC,
|
|
|
|
XCB_GRAB_MODE_ASYNC,
|
|
|
|
root, cursor, XCB_CURRENT_TIME);
|
|
|
|
|
|
|
|
if((grab_ptr_r = xcb_grab_pointer_reply(globalconf.connection, grab_ptr_c, NULL)))
|
|
|
|
{
|
|
|
|
p_delete(&grab_ptr_r);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
usleep(1000);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-29 16:07:13 +01:00
|
|
|
/** Handle mouse motion events.
|
|
|
|
* \param L Lua stack to push the pointer motion.
|
|
|
|
* \param x The received mouse event x component.
|
|
|
|
* \param y The received mouse event y component.
|
|
|
|
* \param mask The received mouse event bit mask.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
mousegrabber_handleevent(lua_State *L, int x, int y, uint16_t mask)
|
|
|
|
{
|
|
|
|
luaA_mouse_pushstatus(L, x, y, mask);
|
|
|
|
}
|
|
|
|
|
2008-11-13 16:08:34 +01:00
|
|
|
/** Grab the mouse pointer and list motions, calling callback function at each
|
|
|
|
* motion. The callback function must return a boolean value: true to
|
|
|
|
* continue grabbing, false to stop.
|
|
|
|
* The function is called with one argument:
|
|
|
|
* a table containing modifiers pointer coordinates.
|
|
|
|
*
|
2021-07-31 13:26:26 +02:00
|
|
|
* The list of valid cursors is:
|
2016-09-09 08:03:07 +02:00
|
|
|
*
|
|
|
|
*@DOC_cursor_c_COMMON@
|
|
|
|
*
|
|
|
|
*
|
2022-07-05 10:37:14 +02:00
|
|
|
* @tparam function func A callback function as described above.
|
2022-11-14 16:37:42 +01:00
|
|
|
* @tparam string|nil cursor The name of an X cursor to use while grabbing or `nil`
|
|
|
|
* to not change the cursor.
|
2022-07-05 10:37:14 +02:00
|
|
|
* @noreturn
|
2019-06-08 01:08:05 +02:00
|
|
|
* @staticfct run
|
2008-11-13 16:08:34 +01:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_mousegrabber_run(lua_State *L)
|
|
|
|
{
|
|
|
|
if(globalconf.mousegrabber != LUA_REFNIL)
|
|
|
|
luaL_error(L, "mousegrabber already running");
|
|
|
|
|
2022-11-14 16:37:42 +01:00
|
|
|
xcb_cursor_t cursor = XCB_NONE;
|
2008-11-13 16:08:34 +01:00
|
|
|
|
2022-11-14 16:37:42 +01:00
|
|
|
if(!lua_isnil(L, 2))
|
2008-11-13 16:08:34 +01:00
|
|
|
{
|
2022-12-04 08:55:46 +01:00
|
|
|
cursor = xcursor_new(&globalconf.cursor_cache, globalconf.cursor_ctx, luaL_checkstring(L, 2));
|
|
|
|
if (!cursor)
|
2008-11-13 16:08:34 +01:00
|
|
|
{
|
2022-11-14 16:37:42 +01:00
|
|
|
luaA_warn(L, "invalid cursor");
|
|
|
|
return 0;
|
2008-11-13 16:08:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 16:37:42 +01:00
|
|
|
luaA_registerfct(L, 1, &globalconf.mousegrabber);
|
|
|
|
|
|
|
|
if(!mousegrabber_grab(cursor))
|
|
|
|
{
|
|
|
|
luaA_unregister(L, &globalconf.mousegrabber);
|
|
|
|
luaL_error(L, "unable to grab mouse pointer");
|
|
|
|
}
|
|
|
|
|
2008-11-13 16:08:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Stop grabbing the mouse pointer.
|
2015-02-27 00:24:23 +01:00
|
|
|
*
|
2019-06-08 01:08:05 +02:00
|
|
|
* @staticfct stop
|
2022-07-05 10:37:14 +02:00
|
|
|
* @noreturn
|
2008-11-13 16:08:34 +01:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
luaA_mousegrabber_stop(lua_State *L)
|
|
|
|
{
|
|
|
|
xcb_ungrab_pointer(globalconf.connection, XCB_CURRENT_TIME);
|
|
|
|
luaA_unregister(L, &globalconf.mousegrabber);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-06 00:35:11 +02:00
|
|
|
/** Check if mousegrabber is running.
|
2015-02-27 00:24:23 +01:00
|
|
|
*
|
2016-04-24 08:44:19 +02:00
|
|
|
* @treturn boolean True if running, false otherwise.
|
2019-06-08 01:08:05 +02:00
|
|
|
* @staticfct isrunning
|
2012-06-06 00:35:11 +02:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_mousegrabber_isrunning(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_pushboolean(L, globalconf.mousegrabber != LUA_REFNIL);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-06-06 23:07:07 +02:00
|
|
|
const struct luaL_Reg awesome_mousegrabber_lib[] =
|
2008-11-13 16:08:34 +01:00
|
|
|
{
|
|
|
|
{ "run", luaA_mousegrabber_run },
|
|
|
|
{ "stop", luaA_mousegrabber_stop },
|
2012-06-06 00:35:11 +02:00
|
|
|
{ "isrunning", luaA_mousegrabber_isrunning },
|
2014-03-23 19:09:11 +01:00
|
|
|
{ "__index", luaA_default_index },
|
|
|
|
{ "__newindex", luaA_default_newindex },
|
2008-11-13 16:08:34 +01:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
2009-08-21 16:44:30 +02:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|