2 * mousegrabber.c - mouse pointer grabbing
4 * Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "globalconf.h"
26 #include "mousegrabber.h"
28 #include "common/xcursor.h"
29 #include "common/xutil.h"
32 * \param cursor The cursor to use while grabbing.
33 * \return True if mouse was grabbed.
36 mousegrabber_grab(xcb_cursor_t cursor
)
38 xcb_window_t root
= globalconf
.screen
->root
;
40 for(int i
= 1000; i
; i
--)
42 xcb_grab_pointer_reply_t
*grab_ptr_r
;
43 xcb_grab_pointer_cookie_t grab_ptr_c
=
44 xcb_grab_pointer_unchecked(globalconf
.connection
, false, root
,
45 XCB_EVENT_MASK_BUTTON_PRESS
46 | XCB_EVENT_MASK_BUTTON_RELEASE
47 | XCB_EVENT_MASK_POINTER_MOTION
,
50 root
, cursor
, XCB_CURRENT_TIME
);
52 if((grab_ptr_r
= xcb_grab_pointer_reply(globalconf
.connection
, grab_ptr_c
, NULL
)))
54 p_delete(&grab_ptr_r
);
62 /** Handle mouse motion events.
63 * \param L Lua stack to push the pointer motion.
64 * \param x The received mouse event x component.
65 * \param y The received mouse event y component.
66 * \param mask The received mouse event bit mask.
69 mousegrabber_handleevent(lua_State
*L
, int x
, int y
, uint16_t mask
)
71 luaA_mouse_pushstatus(L
, x
, y
, mask
);
74 /** Grab the mouse pointer and list motions, calling callback function at each
75 * motion. The callback function must return a boolean value: true to
76 * continue grabbing, false to stop.
77 * The function is called with one argument:
78 * a table containing modifiers pointer coordinates.
80 * \param L The Lua VM state.
81 * \return The number of elements pushed on stack.
85 * \lparam A callback function as described above.
88 luaA_mousegrabber_run(lua_State
*L
)
90 if(globalconf
.mousegrabber
!= LUA_REFNIL
)
91 luaL_error(L
, "mousegrabber already running");
93 uint16_t cfont
= xcursor_font_fromstr(luaL_checkstring(L
, 2));
97 xcb_cursor_t cursor
= xcursor_new(globalconf
.cursor_ctx
, cfont
);
99 luaA_registerfct(L
, 1, &globalconf
.mousegrabber
);
101 if(!mousegrabber_grab(cursor
))
103 luaA_unregister(L
, &globalconf
.mousegrabber
);
104 luaL_error(L
, "unable to grab mouse pointer");
108 luaA_warn(L
, "invalid cursor");
113 /** Stop grabbing the mouse pointer.
114 * \param L The Lua VM state.
115 * \return The number of elements pushed on stack.
118 luaA_mousegrabber_stop(lua_State
*L
)
120 xcb_ungrab_pointer(globalconf
.connection
, XCB_CURRENT_TIME
);
121 luaA_unregister(L
, &globalconf
.mousegrabber
);
125 /** Check if mousegrabber is running.
126 * \param L The Lua VM state.
127 * \return The number of elements pushed on stack.
129 * \lreturn A boolean value, true if mousegrabber is running, false otherwise.
132 luaA_mousegrabber_isrunning(lua_State
*L
)
134 lua_pushboolean(L
, globalconf
.mousegrabber
!= LUA_REFNIL
);
138 const struct luaL_Reg awesome_mousegrabber_lib
[] =
140 { "run", luaA_mousegrabber_run
},
141 { "stop", luaA_mousegrabber_stop
},
142 { "isrunning", luaA_mousegrabber_isrunning
},
146 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80