tag: Improve tag property::index support (FS#1229)
[awesome.git] / mousegrabber.c
blob1df805de647f81158419d1fb23054583506f682e
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 "mousegrabber.h"
23 #include "common/xcursor.h"
24 #include "mouse.h"
25 #include "globalconf.h"
27 #include <unistd.h>
28 #include <stdbool.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 = globalconf.screen->root;
39 for(int i = 1000; i; i--)
41 xcb_grab_pointer_reply_t *grab_ptr_r;
42 xcb_grab_pointer_cookie_t grab_ptr_c =
43 xcb_grab_pointer_unchecked(globalconf.connection, false, root,
44 XCB_EVENT_MASK_BUTTON_PRESS
45 | XCB_EVENT_MASK_BUTTON_RELEASE
46 | XCB_EVENT_MASK_POINTER_MOTION,
47 XCB_GRAB_MODE_ASYNC,
48 XCB_GRAB_MODE_ASYNC,
49 root, cursor, XCB_CURRENT_TIME);
51 if((grab_ptr_r = xcb_grab_pointer_reply(globalconf.connection, grab_ptr_c, NULL)))
53 p_delete(&grab_ptr_r);
54 return true;
56 usleep(1000);
58 return false;
61 /** Handle mouse motion events.
62 * \param L Lua stack to push the pointer motion.
63 * \param x The received mouse event x component.
64 * \param y The received mouse event y component.
65 * \param mask The received mouse event bit mask.
67 void
68 mousegrabber_handleevent(lua_State *L, int x, int y, uint16_t mask)
70 luaA_mouse_pushstatus(L, x, y, mask);
73 /** Grab the mouse pointer and list motions, calling callback function at each
74 * motion. The callback function must return a boolean value: true to
75 * continue grabbing, false to stop.
76 * The function is called with one argument:
77 * a table containing modifiers pointer coordinates.
79 * \param L The Lua VM state.
80 * \return The number of elements pushed on stack.
82 * \luastack
84 * \lparam A callback function as described above.
86 static int
87 luaA_mousegrabber_run(lua_State *L)
89 if(globalconf.mousegrabber != LUA_REFNIL)
90 luaL_error(L, "mousegrabber already running");
92 uint16_t cfont = xcursor_font_fromstr(luaL_checkstring(L, 2));
94 if(cfont)
96 xcb_cursor_t cursor = xcursor_new(globalconf.cursor_ctx, cfont);
98 luaA_registerfct(L, 1, &globalconf.mousegrabber);
100 if(!mousegrabber_grab(cursor))
102 luaA_unregister(L, &globalconf.mousegrabber);
103 luaL_error(L, "unable to grab mouse pointer");
106 else
107 luaA_warn(L, "invalid cursor");
109 return 0;
112 /** Stop grabbing the mouse pointer.
113 * \param L The Lua VM state.
114 * \return The number of elements pushed on stack.
117 luaA_mousegrabber_stop(lua_State *L)
119 xcb_ungrab_pointer(globalconf.connection, XCB_CURRENT_TIME);
120 luaA_unregister(L, &globalconf.mousegrabber);
121 return 0;
124 /** Check if mousegrabber is running.
125 * \param L The Lua VM state.
126 * \return The number of elements pushed on stack.
127 * \luastack
128 * \lreturn A boolean value, true if mousegrabber is running, false otherwise.
130 static int
131 luaA_mousegrabber_isrunning(lua_State *L)
133 lua_pushboolean(L, globalconf.mousegrabber != LUA_REFNIL);
134 return 1;
137 const struct luaL_Reg awesome_mousegrabber_lib[] =
139 { "run", luaA_mousegrabber_run },
140 { "stop", luaA_mousegrabber_stop },
141 { "isrunning", luaA_mousegrabber_isrunning },
142 { "__index", luaA_default_index },
143 { "__newindex", luaA_default_newindex },
144 { NULL, NULL }
147 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80