luaa: restart awesome on Lua PANIC
[awesome.git] / mouse.c
blob24acf56982dc6dee3c19288b527b09f1991c68a9
1 /*
2 * mouse.c - mouse managing
4 * Copyright © 2007-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 "mouse.h"
23 #include "screen.h"
24 #include "client.h"
25 #include "structs.h"
26 #include "wibox.h"
27 #include "luaa.h"
28 #include "common/tokenize.h"
29 #include "common/xutil.h"
31 /** Get the pointer position.
32 * \param window The window to get position on.
33 * \param x will be set to the Pointer-x-coordinate relative to window
34 * \param y will be set to the Pointer-y-coordinate relative to window
35 * \param child Will be set to the window under the pointer.
36 * \param mask will be set to the current buttons state
37 * \return true on success, false if an error occured
38 **/
39 bool
40 mouse_query_pointer(xcb_window_t window, int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
42 xcb_query_pointer_cookie_t query_ptr_c;
43 xcb_query_pointer_reply_t *query_ptr_r;
45 query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, window);
46 query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
48 if(!query_ptr_r || !query_ptr_r->same_screen)
49 return false;
51 *x = query_ptr_r->win_x;
52 *y = query_ptr_r->win_y;
53 if(mask)
54 *mask = query_ptr_r->mask;
55 if(child)
56 *child = query_ptr_r->child;
58 p_delete(&query_ptr_r);
60 return true;
63 /** Get the pointer position on the screen.
64 * \param s This will be set to the screen the mouse is on.
65 * \param x This will be set to the Pointer-x-coordinate relative to window.
66 * \param y This will be set to the Pointer-y-coordinate relative to window.
67 * \param child This will be set to the window under the pointer.
68 * \param mask This will be set to the current buttons state.
69 * \return True on success, false if an error occured.
71 static bool
72 mouse_query_pointer_root(screen_t **s, int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
74 for(int screen = 0;
75 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
76 screen++)
78 xcb_window_t root = xutil_screen_get(globalconf.connection, screen)->root;
80 if(mouse_query_pointer(root, x, y, child, mask))
82 *s = &globalconf.screens.tab[screen];
83 return true;
86 return false;
89 /** Set the pointer position.
90 * \param window The destination window.
91 * \param x X-coordinate inside window.
92 * \param y Y-coordinate inside window.
94 static inline void
95 mouse_warp_pointer(xcb_window_t window, int x, int y)
97 xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
98 0, 0, 0, 0, x, y );
101 /** Mouse library.
102 * \param L The Lua VM state.
103 * \return The number of elements pushed on stack.
104 * \luastack
105 * \lfield coords Mouse coordinates.
106 * \lfield screen Mouse screen number.
108 static int
109 luaA_mouse_index(lua_State *L)
111 size_t len;
112 const char *attr = luaL_checklstring(L, 2, &len);
113 int16_t mouse_x, mouse_y;
114 screen_t *screen;
116 switch(a_tokenize(attr, len))
118 case A_TK_SCREEN:
119 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, NULL))
120 return 0;
122 screen = screen_getbycoord(screen, mouse_x, mouse_y);
124 lua_pushnumber(L, screen_array_indexof(&globalconf.screens, screen) + 1);
125 break;
126 default:
127 return 0;
130 return 1;
133 /** Newindex for mouse.
134 * \param L The Lua VM state.
135 * \return The number of elements pushed on stack.
137 static int
138 luaA_mouse_newindex(lua_State *L)
140 size_t len;
141 const char *attr = luaL_checklstring(L, 2, &len);
142 int x, y = 0;
143 xcb_window_t root;
144 int screen, phys_screen;
146 switch(a_tokenize(attr, len))
148 case A_TK_SCREEN:
149 screen = luaL_checknumber(L, 3) - 1;
150 luaA_checkscreen(screen);
152 /* we need the physical one to get the root window */
153 phys_screen = screen_virttophys(screen);
154 root = xutil_screen_get(globalconf.connection, phys_screen)->root;
156 x = globalconf.screens.tab[screen].geometry.x;
157 y = globalconf.screens.tab[screen].geometry.y;
159 mouse_warp_pointer(root, x, y);
160 break;
161 default:
162 return 0;
165 return 0;
168 /** Push a table with mouse status.
169 * \param L The Lua VM state.
170 * \param x The x coordinate.
171 * \param y The y coordinate.
172 * \param mask The button mask.
175 luaA_mouse_pushstatus(lua_State *L, int x, int y, uint16_t mask)
177 lua_createtable(L, 0, 2);
178 lua_pushnumber(L, x);
179 lua_setfield(L, -2, "x");
180 lua_pushnumber(L, y);
181 lua_setfield(L, -2, "y");
183 lua_createtable(L, 5, 0);
185 int i = 1;
187 for(uint16_t maski = XCB_BUTTON_MASK_1; maski <= XCB_BUTTON_MASK_5; maski <<= 1)
189 if(mask & maski)
190 lua_pushboolean(L, true);
191 else
192 lua_pushboolean(L, false);
193 lua_rawseti(L, -2, i++);
195 lua_setfield(L, -2, "buttons");
196 return 1;
199 /** Get or set the mouse coords.
200 * \param L The Lua VM state.
201 * \return The number of elements pushed on stack.
202 * \luastack
203 * \lparam None or a table with x and y keys as mouse coordinates.
204 * \lreturn A table with mouse coordinates.
206 static int
207 luaA_mouse_coords(lua_State *L)
209 uint16_t mask;
210 int x, y;
211 int16_t mouse_x, mouse_y;
212 screen_t *screen;
214 if(lua_gettop(L) == 1)
216 xcb_window_t root;
218 luaA_checktable(L, 1);
220 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, &mask))
221 return 0;
223 x = luaA_getopt_number(L, 1, "x", mouse_x);
224 y = luaA_getopt_number(L, 1, "y", mouse_y);
226 root = xutil_screen_get(globalconf.connection,
227 screen_array_indexof(&globalconf.screens, screen))->root;
228 mouse_warp_pointer(root, x, y);
229 lua_pop(L, 1);
232 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, &mask))
233 return 0;
235 return luaA_mouse_pushstatus(L, mouse_x, mouse_y, mask);
238 /** Get the client which is under the pointer.
239 * \param L The Lua VM state.
240 * \return The number of elements pushed on stack.
241 * \luastack
242 * \lreturn A client or nil.
244 static int
245 luaA_mouse_object_under_pointer(lua_State *L)
247 screen_t *screen;
248 int16_t mouse_x, mouse_y;
249 xcb_window_t child;
251 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &child, NULL))
252 return 0;
254 wibox_t *wibox;
255 client_t *client;
256 if((wibox = wibox_getbywin(child)))
258 luaA_object_push(L, wibox);
260 int16_t x = mouse_x - wibox->geometry.x;
261 int16_t y = mouse_y - wibox->geometry.y;
263 widget_t *widget = widget_getbycoords(wibox->orientation, &wibox->widgets,
264 wibox->geometry.width,
265 wibox->geometry.height,
266 &x, &y);
268 if(widget)
270 luaA_object_push(L, widget);
271 return 2;
273 return 1;
275 else if((client = client_getbywin(child)))
276 return luaA_object_push(globalconf.L, client);
278 return 0;
281 const struct luaL_reg awesome_mouse_methods[] =
283 { "__index", luaA_mouse_index },
284 { "__newindex", luaA_mouse_newindex },
285 { "coords", luaA_mouse_coords },
286 { "object_under_pointer", luaA_mouse_object_under_pointer },
287 { NULL, NULL }
289 const struct luaL_reg awesome_mouse_meta[] =
291 { NULL, NULL }
294 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80