awful.layout: emit arrange signal
[awesome.git] / mouse.c
blobb743ad3a411ffa9b95aca92e4115b03000c09d8d
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 "globalconf.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 occurred
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 occurred.
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.
203 static int
204 luaA_mouse_coords(lua_State *L)
206 uint16_t mask;
207 int x, y;
208 int16_t mouse_x, mouse_y;
209 screen_t *screen;
211 if(lua_gettop(L) >= 1)
213 xcb_window_t root;
215 luaA_checktable(L, 1);
216 bool ignore_enter_notify = (lua_gettop(L) == 2 && luaA_checkboolean(L, 2));
218 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, &mask))
219 return 0;
221 x = luaA_getopt_number(L, 1, "x", mouse_x);
222 y = luaA_getopt_number(L, 1, "y", mouse_y);
224 root = xutil_screen_get(globalconf.connection,
225 screen_array_indexof(&globalconf.screens, screen))->root;
227 if(ignore_enter_notify)
228 client_ignore_enterleave_events();
230 mouse_warp_pointer(root, x, y);
232 if(ignore_enter_notify)
233 client_restore_enterleave_events();
235 lua_pop(L, 1);
238 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, &mask))
239 return 0;
241 return luaA_mouse_pushstatus(L, mouse_x, mouse_y, mask);
244 /** Get the client which is under the pointer.
245 * \param L The Lua VM state.
246 * \return The number of elements pushed on stack.
247 * \luastack
248 * \lreturn A client or nil.
250 static int
251 luaA_mouse_object_under_pointer(lua_State *L)
253 screen_t *screen;
254 int16_t mouse_x, mouse_y;
255 xcb_window_t child;
257 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &child, NULL))
258 return 0;
260 wibox_t *wibox;
261 client_t *client;
262 if((wibox = wibox_getbywin(child)))
264 luaA_object_push(L, wibox);
266 int16_t x = mouse_x - wibox->geometry.x;
267 int16_t y = mouse_y - wibox->geometry.y;
269 widget_t *widget = widget_getbycoords(wibox->orientation, &wibox->widgets,
270 wibox->geometry.width,
271 wibox->geometry.height,
272 &x, &y);
274 if(widget)
276 luaA_object_push(L, widget);
277 return 2;
279 return 1;
281 else if((client = client_getbywin(child)))
282 return luaA_object_push(globalconf.L, client);
284 return 0;
287 const struct luaL_reg awesome_mouse_methods[] =
289 { "__index", luaA_mouse_index },
290 { "__newindex", luaA_mouse_newindex },
291 { "coords", luaA_mouse_coords },
292 { "object_under_pointer", luaA_mouse_object_under_pointer },
293 { NULL, NULL }
295 const struct luaL_reg awesome_mouse_meta[] =
297 { NULL, NULL }
300 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80