awful.completion: sort matches
[awesome.git] / mouse.c
blobc9edd17b7234710336ec5cdbfc063bebfd477a3c
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 "common/tokenize.h"
28 #include "common/xutil.h"
30 /** Get the pointer position.
31 * \param window The window to get position on.
32 * \param x will be set to the Pointer-x-coordinate relative to window
33 * \param y will be set to the Pointer-y-coordinate relative to window
34 * \param child Will be set to the window under the pointer.
35 * \param mask will be set to the current buttons state
36 * \return true on success, false if an error occured
37 **/
38 bool
39 mouse_query_pointer(xcb_window_t window, int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
41 xcb_query_pointer_cookie_t query_ptr_c;
42 xcb_query_pointer_reply_t *query_ptr_r;
44 query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, window);
45 query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
47 if(!query_ptr_r || !query_ptr_r->same_screen)
48 return false;
50 *x = query_ptr_r->win_x;
51 *y = query_ptr_r->win_y;
52 if(mask)
53 *mask = query_ptr_r->mask;
54 if(child)
55 *child = query_ptr_r->child;
57 p_delete(&query_ptr_r);
59 return true;
62 /** Get the pointer position on the screen.
63 * \param s This will be set to the screen the mouse is on.
64 * \param x This will be set to the Pointer-x-coordinate relative to window.
65 * \param y This will be set to the Pointer-y-coordinate relative to window.
66 * \param child This will be set to the window under the pointer.
67 * \param mask This will be set to the current buttons state.
68 * \return True on success, false if an error occured.
70 static bool
71 mouse_query_pointer_root(screen_t **s, int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
73 for(int screen = 0;
74 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
75 screen++)
77 xcb_window_t root = xutil_screen_get(globalconf.connection, screen)->root;
79 if(mouse_query_pointer(root, x, y, child, mask))
81 *s = &globalconf.screens.tab[screen];
82 return true;
85 return false;
88 /** Set the pointer position.
89 * \param window The destination window.
90 * \param x X-coordinate inside window.
91 * \param y Y-coordinate inside window.
93 static inline void
94 mouse_warp_pointer(xcb_window_t window, int x, int y)
96 xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
97 0, 0, 0, 0, x, y );
100 /** Mouse library.
101 * \param L The Lua VM state.
102 * \return The number of elements pushed on stack.
103 * \luastack
104 * \lfield coords Mouse coordinates.
105 * \lfield screen Mouse screen number.
107 static int
108 luaA_mouse_index(lua_State *L)
110 size_t len;
111 const char *attr = luaL_checklstring(L, 2, &len);
112 int16_t mouse_x, mouse_y;
113 screen_t *screen;
115 switch(a_tokenize(attr, len))
117 case A_TK_SCREEN:
118 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, NULL))
119 return 0;
121 screen = screen_getbycoord(screen, mouse_x, mouse_y);
123 lua_pushnumber(L, screen_array_indexof(&globalconf.screens, screen) + 1);
124 break;
125 default:
126 return 0;
129 return 1;
132 /** Newindex for mouse.
133 * \param L The Lua VM state.
134 * \return The number of elements pushed on stack.
136 static int
137 luaA_mouse_newindex(lua_State *L)
139 size_t len;
140 const char *attr = luaL_checklstring(L, 2, &len);
141 int x, y = 0;
142 xcb_window_t root;
143 int screen, phys_screen;
145 switch(a_tokenize(attr, len))
147 case A_TK_SCREEN:
148 screen = luaL_checknumber(L, 3) - 1;
149 luaA_checkscreen(screen);
151 /* we need the physical one to get the root window */
152 phys_screen = screen_virttophys(screen);
153 root = xutil_screen_get(globalconf.connection, phys_screen)->root;
155 x = globalconf.screens.tab[screen].geometry.x;
156 y = globalconf.screens.tab[screen].geometry.y;
158 mouse_warp_pointer(root, x, y);
159 break;
160 default:
161 return 0;
164 return 0;
167 /** Push a table with mouse status.
168 * \param L The Lua VM state.
169 * \param x The x coordinate.
170 * \param y The y coordinate.
171 * \param mask The button mask.
174 luaA_mouse_pushstatus(lua_State *L, int x, int y, uint16_t mask)
176 lua_createtable(L, 0, 2);
177 lua_pushnumber(L, x);
178 lua_setfield(L, -2, "x");
179 lua_pushnumber(L, y);
180 lua_setfield(L, -2, "y");
182 lua_createtable(L, 5, 0);
184 int i = 1;
186 for(uint16_t maski = XCB_BUTTON_MASK_1; maski <= XCB_BUTTON_MASK_5; maski <<= 1)
188 if(mask & maski)
189 lua_pushboolean(L, true);
190 else
191 lua_pushboolean(L, false);
192 lua_rawseti(L, -2, i++);
194 lua_setfield(L, -2, "buttons");
195 return 1;
198 /** Get or set the mouse coords.
199 * \param L The Lua VM state.
200 * \return The number of elements pushed on stack.
201 * \luastack
202 * \lparam None or a table with x and y keys as mouse coordinates.
203 * \lreturn A table with mouse coordinates.
205 static int
206 luaA_mouse_coords(lua_State *L)
208 uint16_t mask;
209 int x, y;
210 int16_t mouse_x, mouse_y;
211 screen_t *screen;
213 if(lua_gettop(L) == 1)
215 xcb_window_t root;
217 luaA_checktable(L, 1);
219 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, &mask))
220 return 0;
222 x = luaA_getopt_number(L, 1, "x", mouse_x);
223 y = luaA_getopt_number(L, 1, "y", mouse_y);
225 root = xutil_screen_get(globalconf.connection,
226 screen_array_indexof(&globalconf.screens, screen))->root;
227 mouse_warp_pointer(root, x, y);
228 lua_pop(L, 1);
231 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, &mask))
232 return 0;
234 return luaA_mouse_pushstatus(L, mouse_x, mouse_y, mask);
237 /** Get the client which is under the pointer.
238 * \param L The Lua VM state.
239 * \return The number of elements pushed on stack.
240 * \luastack
241 * \lreturn A client or nil.
243 static int
244 luaA_mouse_object_under_pointer(lua_State *L)
246 screen_t *screen;
247 int16_t mouse_x, mouse_y;
248 xcb_window_t child;
250 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &child, NULL))
251 return 0;
253 wibox_t *wibox;
254 client_t *client;
255 if((wibox = wibox_getbywin(child)))
257 wibox_push(L, wibox);
259 int16_t x = mouse_x - wibox->geometry.x;
260 int16_t y = mouse_y - wibox->geometry.y;
262 widget_t *widget = widget_getbycoords(wibox->orientation, &wibox->widgets,
263 wibox->geometry.width,
264 wibox->geometry.height,
265 &x, &y);
267 if(widget)
269 widget_push(L, widget);
270 return 2;
272 return 1;
274 else if((client = client_getbywin(child)))
275 return client_push(globalconf.L, client);
277 return 0;
280 const struct luaL_reg awesome_mouse_methods[] =
282 { "__index", luaA_mouse_index },
283 { "__newindex", luaA_mouse_newindex },
284 { "coords", luaA_mouse_coords },
285 { "object_under_pointer", luaA_mouse_object_under_pointer },
286 { NULL, NULL }
288 const struct luaL_reg awesome_mouse_meta[] =
290 { NULL, NULL }
293 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80