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
= XCB_NONE
;
41 screen
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
47 root
= xutil_screen_get(globalconf
.connection
, screen
)->root
;
48 if(mouse_query_pointer(root
, &x
, &y
, NULL
, &mask
))
52 for(int i
= 1000; i
; i
--)
54 xcb_grab_pointer_reply_t
*grab_ptr_r
;
55 xcb_grab_pointer_cookie_t grab_ptr_c
=
56 xcb_grab_pointer_unchecked(globalconf
.connection
, false, root
,
57 XCB_EVENT_MASK_BUTTON_PRESS
58 | XCB_EVENT_MASK_BUTTON_RELEASE
59 | XCB_EVENT_MASK_POINTER_MOTION
,
62 root
, cursor
, XCB_CURRENT_TIME
);
64 if((grab_ptr_r
= xcb_grab_pointer_reply(globalconf
.connection
, grab_ptr_c
, NULL
)))
66 p_delete(&grab_ptr_r
);
74 /** Handle mouse motion events.
75 * \param L Lua stack to push the pointer motion.
76 * \param x The received mouse event x component.
77 * \param y The received mouse event y component.
78 * \param mask The received mouse event bit mask.
81 mousegrabber_handleevent(lua_State
*L
, int x
, int y
, uint16_t mask
)
83 luaA_mouse_pushstatus(L
, x
, y
, mask
);
86 /** Grab the mouse pointer and list motions, calling callback function at each
87 * motion. The callback function must return a boolean value: true to
88 * continue grabbing, false to stop.
89 * The function is called with one argument:
90 * a table containing modifiers pointer coordinates.
92 * \param L The Lua VM state.
93 * \return The number of elements pushed on stack.
97 * \lparam A callback function as described above.
100 luaA_mousegrabber_run(lua_State
*L
)
102 if(globalconf
.mousegrabber
!= LUA_REFNIL
)
103 luaL_error(L
, "mousegrabber already running");
105 uint16_t cfont
= xcursor_font_fromstr(luaL_checkstring(L
, 2));
109 xcb_cursor_t cursor
= xcursor_new(globalconf
.connection
, cfont
);
111 luaA_registerfct(L
, 1, &globalconf
.mousegrabber
);
113 if(!mousegrabber_grab(cursor
))
115 luaA_unregister(L
, &globalconf
.mousegrabber
);
116 luaL_error(L
, "unable to grab mouse pointer");
120 luaA_warn(L
, "invalid cursor");
125 /** Stop grabbing the mouse pointer.
126 * \param L The Lua VM state.
127 * \return The number of elements pushed on stack.
130 luaA_mousegrabber_stop(lua_State
*L
)
132 xcb_ungrab_pointer(globalconf
.connection
, XCB_CURRENT_TIME
);
133 luaA_unregister(L
, &globalconf
.mousegrabber
);
137 const struct luaL_reg awesome_mousegrabber_lib
[] =
139 { "run", luaA_mousegrabber_run
},
140 { "stop", luaA_mousegrabber_stop
},
144 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80