change codename
[awesome.git] / mouse.c
blob065053916e121da800966d537b2f11db723972de
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 "common/xutil.h"
28 /** Get the pointer position.
29 * \param window The window to get position on.
30 * \param x will be set to the Pointer-x-coordinate relative to window
31 * \param y will be set to the Pointer-y-coordinate relative to window
32 * \param child Will be set to the window under the pointer.
33 * \param mask will be set to the current buttons state
34 * \return true on success, false if an error occured
35 **/
36 bool
37 mouse_query_pointer(xcb_window_t window, int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
39 xcb_query_pointer_cookie_t query_ptr_c;
40 xcb_query_pointer_reply_t *query_ptr_r;
42 query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, window);
43 query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
45 if(!query_ptr_r || !query_ptr_r->same_screen)
46 return false;
48 *x = query_ptr_r->win_x;
49 *y = query_ptr_r->win_y;
50 if(mask)
51 *mask = query_ptr_r->mask;
52 if(child)
53 *child = query_ptr_r->child;
55 p_delete(&query_ptr_r);
57 return true;
60 /** Get the pointer position on the screen.
61 * \param screen This will be set to the screen the mouse is on.
62 * \param x This will be set to the Pointer-x-coordinate relative to window.
63 * \param y This will be set to the Pointer-y-coordinate relative to window.
64 * \param child This will be set to the window under the pointer.
65 * \param mask This will be set to the current buttons state.
66 * \return True on success, false if an error occured.
68 static bool
69 mouse_query_pointer_root(screen_t **s, int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
71 for(int screen = 0;
72 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
73 screen++)
75 xcb_window_t root = xutil_screen_get(globalconf.connection, screen)->root;
77 if(mouse_query_pointer(root, x, y, child, mask))
79 *s = &globalconf.screens.tab[screen];
80 return true;
83 return false;
86 /** Set the pointer position.
87 * \param window The destination window.
88 * \param x X-coordinate inside window.
89 * \param y Y-coordinate inside window.
91 static inline void
92 mouse_warp_pointer(xcb_window_t window, int x, int y)
94 xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
95 0, 0, 0, 0, x, y );
98 /** Mouse library.
99 * \param L The Lua VM state.
100 * \return The number of elements pushed on stack.
101 * \luastack
102 * \lfield coords Mouse coordinates.
103 * \lfield screen Mouse screen number.
105 static int
106 luaA_mouse_index(lua_State *L)
108 size_t len;
109 const char *attr = luaL_checklstring(L, 2, &len);
110 int16_t mouse_x, mouse_y;
111 screen_t *screen;
113 switch(a_tokenize(attr, len))
115 case A_TK_SCREEN:
116 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, NULL))
117 return 0;
119 screen = screen_getbycoord(screen, mouse_x, mouse_y);
121 lua_pushnumber(L, screen_array_indexof(&globalconf.screens, screen) + 1);
122 break;
123 default:
124 return 0;
127 return 1;
130 /** Newindex for mouse.
131 * \param L The Lua VM state.
132 * \return The number of elements pushed on stack.
134 static int
135 luaA_mouse_newindex(lua_State *L)
137 size_t len;
138 const char *attr = luaL_checklstring(L, 2, &len);
139 int x, y = 0;
140 xcb_window_t root;
141 int screen, phys_screen;
143 switch(a_tokenize(attr, len))
145 case A_TK_SCREEN:
146 screen = luaL_checknumber(L, 3) - 1;
147 luaA_checkscreen(screen);
149 /* we need the physical one to get the root window */
150 phys_screen = screen_virttophys(screen);
151 root = xutil_screen_get(globalconf.connection, phys_screen)->root;
153 x = globalconf.screens.tab[screen].geometry.x;
154 y = globalconf.screens.tab[screen].geometry.y;
156 mouse_warp_pointer(root, x, y);
157 break;
158 default:
159 return 0;
162 return 0;
165 /** Push a table with mouse status.
166 * \param L The Lua VM state.
167 * \param x The x coordinate.
168 * \param y The y coordinate.
169 * \param mask The button mask.
172 luaA_mouse_pushstatus(lua_State *L, int x, int y, uint16_t mask)
174 lua_createtable(L, 0, 2);
175 lua_pushnumber(L, x);
176 lua_setfield(L, -2, "x");
177 lua_pushnumber(L, y);
178 lua_setfield(L, -2, "y");
180 lua_createtable(L, 5, 0);
182 int i = 1;
184 for(uint16_t maski = XCB_BUTTON_MASK_1; maski <= XCB_BUTTON_MASK_5; maski <<= 1)
186 if(mask & maski)
187 lua_pushboolean(L, true);
188 else
189 lua_pushboolean(L, false);
190 lua_rawseti(L, -2, i++);
192 lua_setfield(L, -2, "buttons");
193 return 1;
196 /** Get or set the mouse coords.
197 * \param L The Lua VM state.
198 * \return The number of elements pushed on stack.
199 * \luastack
200 * \lparam None or a table with x and y keys as mouse coordinates.
201 * \lreturn A table with mouse coordinates.
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);
217 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, &mask))
218 return 0;
220 x = luaA_getopt_number(L, 1, "x", mouse_x);
221 y = luaA_getopt_number(L, 1, "y", mouse_y);
223 root = xutil_screen_get(globalconf.connection,
224 screen_array_indexof(&globalconf.screens, screen))->root;
225 mouse_warp_pointer(root, x, y);
226 lua_pop(L, 1);
229 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, &mask))
230 return 0;
232 return luaA_mouse_pushstatus(L, mouse_x, mouse_y, mask);
235 /** Get the client which is under the pointer.
236 * \param L The Lua VM state.
237 * \return The number of elements pushed on stack.
238 * \luastack
239 * \lreturn A client or nil.
241 static int
242 luaA_mouse_object_under_pointer(lua_State *L)
244 screen_t *screen;
245 int16_t mouse_x, mouse_y;
246 xcb_window_t child;
248 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &child, NULL))
249 return 0;
251 wibox_t *wibox;
252 client_t *client;
253 if((wibox = wibox_getbywin(child)))
255 wibox_push(L, wibox);
257 int16_t x = mouse_x - wibox->sw.geometry.x;
258 int16_t y = mouse_y - wibox->sw.geometry.y;
260 widget_t *widget = widget_getbycoords(wibox->sw.orientation, &wibox->widgets,
261 wibox->sw.geometry.width,
262 wibox->sw.geometry.height,
263 &x, &y);
265 if(widget)
267 widget_push(L, widget);
268 return 2;
270 return 1;
272 else if((client = client_getbywin(child)))
273 return client_push(globalconf.L, client);
275 return 0;
278 const struct luaL_reg awesome_mouse_methods[] =
280 { "__index", luaA_mouse_index },
281 { "__newindex", luaA_mouse_newindex },
282 { "coords", luaA_mouse_coords },
283 { "object_under_pointer", luaA_mouse_object_under_pointer },
284 { NULL, NULL }
286 const struct luaL_reg awesome_mouse_meta[] =
288 { NULL, NULL }
291 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80