naughty: add vertical margin
[awesome.git] / mousegrabber.c
blob3544feaf65991d7ca294f0bc75e09bb209452c80
1 /*
2 * mousegrabber.c - mouse pointer grabbing
4 * Copyright © 2008 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.
22 #include <unistd.h>
24 #include "structs.h"
25 #include "mouse.h"
26 #include "mousegrabber.h"
27 #include "common/xcursor.h"
28 #include "common/xutil.h"
30 /** Grab the mouse.
31 * \param cursor The cursor to use while grabbing.
32 * \return True if mouse was grabbed.
34 static bool
35 mousegrabber_grab(xcb_cursor_t cursor)
37 xcb_window_t root = XCB_NONE;
39 for(int screen = 0;
40 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
41 screen++)
43 int16_t x, y;
44 uint16_t mask;
46 root = xutil_screen_get(globalconf.connection, screen)->root;
47 if(mouse_query_pointer(root, &x, &y, NULL, &mask))
48 break;
51 for(int i = 1000; i; i--)
53 xcb_grab_pointer_reply_t *grab_ptr_r;
54 xcb_grab_pointer_cookie_t grab_ptr_c =
55 xcb_grab_pointer_unchecked(globalconf.connection, false, root,
56 XCB_EVENT_MASK_BUTTON_PRESS
57 | XCB_EVENT_MASK_BUTTON_RELEASE
58 | XCB_EVENT_MASK_POINTER_MOTION,
59 XCB_GRAB_MODE_ASYNC,
60 XCB_GRAB_MODE_ASYNC,
61 root, cursor, XCB_CURRENT_TIME);
63 if((grab_ptr_r = xcb_grab_pointer_reply(globalconf.connection, grab_ptr_c, NULL)))
65 p_delete(&grab_ptr_r);
66 return true;
68 usleep(1000);
70 return false;
73 /** Handle mouse motion events.
74 * \param L Lua stack to push the pointer motion.
75 * \param ev Received mouse motion event.
77 void
78 mousegrabber_handleevent(lua_State *L, int x, int y, uint16_t mask)
80 luaA_mouse_pushstatus(L, x, y, mask);
83 /** Grab the mouse pointer and list motions, calling callback function at each
84 * motion. The callback function must return a boolean value: true to
85 * continue grabbing, false to stop.
86 * The function is called with one argument:
87 * a table containing modifiers pointer coordinates.
89 * \param L The Lua VM state.
90 * \return The number of elements pushed on stack.
92 * \luastack
94 * \lparam A callback function as described above.
96 static int
97 luaA_mousegrabber_run(lua_State *L)
99 if(globalconf.mousegrabber != LUA_REFNIL)
100 luaL_error(L, "mousegrabber already running");
102 uint16_t cfont = xcursor_font_fromstr(luaL_checkstring(L, 2));
104 if(cfont)
106 xcb_cursor_t cursor = xcursor_new(globalconf.connection, cfont);
108 luaA_registerfct(L, 1, &globalconf.mousegrabber);
110 if(!mousegrabber_grab(cursor))
112 luaA_unregister(L, &globalconf.mousegrabber);
113 luaL_error(L, "unable to grab mouse pointer");
116 else
117 luaA_warn(L, "invalid cursor");
119 return 0;
122 /** Stop grabbing the mouse pointer.
123 * \param L The Lua VM state.
124 * \return The number of elements pushed on stack.
127 luaA_mousegrabber_stop(lua_State *L)
129 xcb_ungrab_pointer(globalconf.connection, XCB_CURRENT_TIME);
130 luaA_unregister(L, &globalconf.mousegrabber);
131 return 0;
134 const struct luaL_reg awesome_mousegrabber_lib[] =
136 { "run", luaA_mousegrabber_run },
137 { "stop", luaA_mousegrabber_stop },
138 { NULL, NULL }