change codename
[awesome.git] / mousegrabber.c
blobc38132264f8c1b775233fe23741e28763969c78e
1 /*
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.
22 #include <unistd.h>
24 #include "globalconf.h"
25 #include "mouse.h"
26 #include "mousegrabber.h"
27 #include "luaa.h"
28 #include "common/xcursor.h"
29 #include "common/xutil.h"
31 /** Grab the mouse.
32 * \param cursor The cursor to use while grabbing.
33 * \return True if mouse was grabbed.
35 static bool
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,
48 XCB_GRAB_MODE_ASYNC,
49 XCB_GRAB_MODE_ASYNC,
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);
55 return true;
57 usleep(1000);
59 return false;
62 /** Grab the mouse pointer and list motions, calling callback function at each
63 * motion. The callback function must return a boolean value: true to
64 * continue grabbing, false to stop.
65 * The function is called with one argument:
66 * a table containing modifiers pointer coordinates.
68 * \param L The Lua VM state.
69 * \return The number of elements pushed on stack.
71 * \luastack
73 * \lparam A callback function as described above.
75 static int
76 luaA_mousegrabber_run(lua_State *L)
78 if(globalconf.mousegrabber != LUA_REFNIL)
79 luaL_error(L, "mousegrabber already running");
81 uint16_t cfont = xcursor_font_fromstr(luaL_checkstring(L, 2));
83 if(cfont)
85 xcb_cursor_t cursor = xcursor_new(globalconf.display, cfont);
87 luaA_registerfct(L, 1, &globalconf.mousegrabber);
89 if(!mousegrabber_grab(cursor))
91 luaA_unregister(L, &globalconf.mousegrabber);
92 luaL_error(L, "unable to grab mouse pointer");
95 else
96 luaA_warn(L, "invalid cursor");
98 return 0;
101 /** Stop grabbing the mouse pointer.
102 * \param L The Lua VM state.
103 * \return The number of elements pushed on stack.
106 luaA_mousegrabber_stop(lua_State *L)
108 xcb_ungrab_pointer(globalconf.connection, XCB_CURRENT_TIME);
109 luaA_unregister(L, &globalconf.mousegrabber);
110 return 0;
113 /** Check if mousegrabber is running.
114 * \param L The Lua VM state.
115 * \return The number of elements pushed on stack.
116 * \luastack
117 * \lreturn A boolean value, true if mousegrabber is running, false otherwise.
119 static int
120 luaA_mousegrabber_isrunning(lua_State *L)
122 lua_pushboolean(L, globalconf.mousegrabber != LUA_REFNIL);
123 return 1;
126 const struct luaL_Reg awesome_mousegrabber_lib[] =
128 { "run", luaA_mousegrabber_run },
129 { "stop", luaA_mousegrabber_stop },
130 { "isrunning", luaA_mousegrabber_isrunning },
131 { NULL, NULL }
134 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80