draw: stop using font as argument, use global
[awesome.git] / mouse.c
blob9afb027e97fe6f27ec341a738df2c6e21064cc23
1 /*
2 * mouse.c - mouse managing
4 * Copyright © 2007-2008 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 <math.h>
24 #include "common/tokenize.h"
25 #include "screen.h"
26 #include "tag.h"
27 #include "common/xcursor.h"
29 extern awesome_t globalconf;
31 DO_LUA_NEW(static, button_t, button, "button", button_ref)
32 DO_LUA_GC(button_t, button, "button", button_unref)
33 DO_LUA_EQ(button_t, button, "button")
35 /** Delete a button.
36 * \param button The button to destroy.
38 void
39 button_delete(button_t **button)
41 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*button)->press);
42 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*button)->release);
43 p_delete(button);
46 /** Get the pointer position.
47 * \param window The window to get position on.
48 * \param x will be set to the Pointer-x-coordinate relative to window
49 * \param y will be set to the Pointer-y-coordinate relative to window
50 * \param mask will be set to the current buttons state
51 * \return true on success, false if an error occured
52 **/
53 bool
54 mouse_query_pointer(xcb_window_t window, int *x, int *y, uint16_t *mask)
56 xcb_query_pointer_cookie_t query_ptr_c;
57 xcb_query_pointer_reply_t *query_ptr_r;
59 query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, window);
60 query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
62 if(!query_ptr_r || !query_ptr_r->same_screen)
63 return false;
65 *x = query_ptr_r->win_x;
66 *y = query_ptr_r->win_y;
67 if (mask)
68 *mask = query_ptr_r->mask;
70 p_delete(&query_ptr_r);
72 return true;
75 /** Get the pointer position on the screen.
76 * \param screen This will be set to the screen number the mouse is on.
77 * \param x This will be set to the Pointer-x-coordinate relative to window.
78 * \param y This will be set to the Pointer-y-coordinate relative to window.
79 * \param mask This will be set to the current buttons state.
80 * \return True on success, false if an error occured.
82 static bool
83 mouse_query_pointer_root(int *s, int *x, int *y, uint16_t *mask)
85 for(int screen = 0;
86 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
87 screen++)
89 xcb_window_t root = xutil_screen_get(globalconf.connection, screen)->root;
91 if(mouse_query_pointer(root, x, y, mask))
93 *s = screen;
94 return true;
97 return false;
100 /** Set the pointer position.
101 * \param window The destination window.
102 * \param x X-coordinate inside window.
103 * \param y Y-coordinate inside window.
105 static inline void
106 mouse_warp_pointer(xcb_window_t window, int x, int y)
108 xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
109 0, 0, 0, 0, x, y );
112 /** Create a new mouse button bindings.
113 * \param L The Lua VM state.
114 * \return The number of elements pushed on stack.
115 * \luastack
116 * \lparam A table with modifiers keys, or a button to clone.
117 * \lparam A mouse button number.
118 * \lparam A function to execute on click events.
119 * \lparam A function to execute on release events.
120 * \lreturn A mouse button binding.
122 static int
123 luaA_button_new(lua_State *L)
125 int i, len;
126 button_t *button, **orig;
128 if((orig = luaA_toudata(L, 2, "button")))
130 button_t *copy = p_new(button_t, 1);
131 copy->mod = (*orig)->mod;
132 copy->button = (*orig)->button;
133 if((*orig)->press != LUA_REFNIL)
135 lua_rawgeti(L, LUA_REGISTRYINDEX, (*orig)->press);
136 luaA_registerfct(L, -1, &copy->press);
138 else
139 copy->press = LUA_REFNIL;
140 if((*orig)->release != LUA_REFNIL)
142 lua_rawgeti(L, LUA_REGISTRYINDEX, (*orig)->release);
143 luaA_registerfct(L, -1, &copy->release);
145 else
146 copy->release = LUA_REFNIL;
147 return luaA_button_userdata_new(L, copy);
150 luaA_checktable(L, 2);
151 /* arg 3 is mouse button */
152 i = luaL_checknumber(L, 3);
153 /* arg 4 and 5 are callback functions */
154 if(!lua_isnil(L, 4))
155 luaA_checkfunction(L, 4);
156 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
157 luaA_checkfunction(L, 5);
159 button = p_new(button_t, 1);
160 button->button = xutil_button_fromint(i);
162 if(lua_isnil(L, 4))
163 button->press = LUA_REFNIL;
164 else
165 luaA_registerfct(L, 4, &button->press);
167 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
168 luaA_registerfct(L, 5, &button->release);
169 else
170 button->release = LUA_REFNIL;
172 len = lua_objlen(L, 2);
173 for(i = 1; i <= len; i++)
175 size_t blen;
176 const char *buf;
177 lua_rawgeti(L, 2, i);
178 buf = luaL_checklstring(L, -1, &blen);
179 button->mod |= xutil_key_mask_fromstr(buf, blen);
182 return luaA_button_userdata_new(L, button);
185 /** Set a button array with a Lua table.
186 * \param L The Lua VM state.
187 * \param idx The index of the Lua table.
188 * \param buttons The array button to fill.
190 void
191 luaA_button_array_set(lua_State *L, int idx, button_array_t *buttons)
193 button_t **b;
195 luaA_checktable(L, idx);
196 button_array_wipe(buttons);
197 button_array_init(buttons);
198 lua_pushnil(L);
199 while(lua_next(L, idx))
201 b = luaA_checkudata(L, -1, "button");
202 button_array_append(buttons, *b);
203 button_ref(b);
204 lua_pop(L, 1);
208 /** Push an array of button as an Lua table onto the stack.
209 * \param L The Lua VM state.
210 * \param buttons The button array to push.
211 * \return The number of elements pushed on stack.
214 luaA_button_array_get(lua_State *L, button_array_t *buttons)
216 luaA_otable_new(L);
217 for(int i = 0; i < buttons->len; i++)
219 luaA_button_userdata_new(L, buttons->tab[i]);
220 lua_rawseti(L, -2, i + 1);
222 return 1;
225 /** Button object.
226 * \param L The Lua VM state.
227 * \return The number of elements pushed on stack.
228 * \luastack
229 * \lfield press The function called when button press event is received.
230 * \lfield release The function called when button release event is received.
232 static int
233 luaA_button_index(lua_State *L)
235 if(luaA_usemetatable(L, 1, 2))
236 return 1;
238 size_t len;
239 button_t **button = luaA_checkudata(L, 1, "button");
240 const char *attr = luaL_checklstring(L, 2, &len);
242 switch(a_tokenize(attr, len))
244 case A_TK_PRESS:
245 if((*button)->press != LUA_REFNIL)
246 lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->press);
247 else
248 lua_pushnil(L);
249 break;
250 case A_TK_RELEASE:
251 if((*button)->release != LUA_REFNIL)
252 lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->release);
253 else
254 lua_pushnil(L);
255 break;
256 case A_TK_BUTTON:
257 /* works fine, but not *really* neat */
258 lua_pushnumber(L, (*button)->button);
259 break;
260 default:
261 break;
264 return 1;
267 /** Button object.
268 * \param L The Lua VM state.
269 * \return The number of elements pushed on stack.
270 * \luastack
272 static int
273 luaA_button_newindex(lua_State *L)
275 if(luaA_usemetatable(L, 1, 2))
276 return 1;
278 size_t len;
279 button_t **button = luaA_checkudata(L, 1, "button");
280 const char *attr = luaL_checklstring(L, 2, &len);
282 switch(a_tokenize(attr, len))
284 case A_TK_PRESS:
285 luaA_registerfct(L, 3, &(*button)->press);
286 break;
287 case A_TK_RELEASE:
288 luaA_registerfct(L, 3, &(*button)->release);
289 break;
290 case A_TK_BUTTON:
291 (*button)->button = xutil_button_fromint(luaL_checknumber(L, 3));
292 break;
293 default:
294 break;
297 return 0;
300 const struct luaL_reg awesome_button_methods[] =
302 { "__call", luaA_button_new },
303 { NULL, NULL }
305 const struct luaL_reg awesome_button_meta[] =
307 { "__index", luaA_button_index },
308 { "__newindex", luaA_button_newindex },
309 { "__gc", luaA_button_gc },
310 { "__eq", luaA_button_eq },
311 { "__tostring", luaA_button_tostring },
312 { NULL, NULL }
315 /** Mouse library.
316 * \param L The Lua VM state.
317 * \return The number of elements pushed on stack.
318 * \luastack
319 * \lfield coords Mouse coordinates.
320 * \lfield screen Mouse screen number.
322 static int
323 luaA_mouse_index(lua_State *L)
325 size_t len;
326 const char *attr = luaL_checklstring(L, 2, &len);
327 int mouse_x, mouse_y, i;
328 int screen;
330 switch(a_tokenize(attr, len))
332 case A_TK_SCREEN:
333 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL))
334 return 0;
336 i = screen_getbycoord(screen, mouse_x, mouse_y);
338 lua_pushnumber(L, i + 1);
339 break;
340 default:
341 return 0;
344 return 1;
347 /** Newindex for mouse.
348 * \param L The Lua VM state.
349 * \return The number of elements pushed on stack.
351 static int
352 luaA_mouse_newindex(lua_State *L)
354 size_t len;
355 const char *attr = luaL_checklstring(L, 2, &len);
356 int x, y = 0;
357 xcb_window_t root;
358 int screen, phys_screen;
360 switch(a_tokenize(attr, len))
362 case A_TK_SCREEN:
363 screen = luaL_checknumber(L, 3) - 1;
364 luaA_checkscreen(screen);
366 /* we need the physical one to get the root window */
367 phys_screen = screen_virttophys(screen);
368 root = xutil_screen_get(globalconf.connection, phys_screen)->root;
370 x = globalconf.screens[screen].geometry.x;
371 y = globalconf.screens[screen].geometry.y;
373 mouse_warp_pointer(root, x, y);
374 break;
375 default:
376 return 0;
379 return 0;
382 /** Push a table with mouse status.
383 * \param L The Lua VM state.
384 * \param x The x coordinate.
385 * \param y The y coordinate.
386 * \param mask The button mask.
389 luaA_mouse_pushstatus(lua_State *L, int x, int y, uint16_t mask)
391 lua_newtable(L);
392 lua_pushnumber(L, x);
393 lua_setfield(L, -2, "x");
394 lua_pushnumber(L, y);
395 lua_setfield(L, -2, "y");
397 lua_newtable(L);
399 int i = 1;
401 for(uint16_t maski = XCB_BUTTON_MASK_1; maski <= XCB_BUTTON_MASK_5; maski <<= 1)
403 if(mask & maski)
404 lua_pushboolean(L, true);
405 else
406 lua_pushboolean(L, false);
407 lua_rawseti(L, -2, i++);
409 lua_setfield(L, -2, "buttons");
410 return 1;
413 /** Get or set the mouse coords.
414 * \param L The Lua VM state.
415 * \return The number of elements pushed on stack.
416 * \luastack
417 * \lparam None or a table with x and y keys as mouse coordinates.
418 * \lreturn A table with mouse coordinates.
420 static int
421 luaA_mouse_coords(lua_State *L)
423 uint16_t mask;
424 int screen, x, y, mouse_x, mouse_y;
426 if(lua_gettop(L) == 1)
428 xcb_window_t root;
430 luaA_checktable(L, 1);
432 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &mask))
433 return 0;
435 x = luaA_getopt_number(L, 1, "x", mouse_x);
436 y = luaA_getopt_number(L, 1, "y", mouse_y);
438 root = xutil_screen_get(globalconf.connection, screen)->root;
439 mouse_warp_pointer(root, x, y);
440 lua_pop(L, 1);
443 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &mask))
444 return 0;
446 return luaA_mouse_pushstatus(L, mouse_x, mouse_y, mask);
449 /** Get the client which is under the pointer.
450 * \param L The Lua VM state.
451 * \return The number of elements pushed on stack.
452 * \luastack
453 * \lreturn A client or nil.
455 static int
456 luaA_mouse_client_under_pointer(lua_State *L)
458 for(int screen = 0;
459 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
460 screen++)
462 xcb_window_t root = xutil_screen_get(globalconf.connection, screen)->root;
463 xcb_query_pointer_reply_t *query_ptr_r;
464 xcb_query_pointer_cookie_t query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, root);
465 query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
467 if(query_ptr_r)
469 client_t *c = client_getbywin(query_ptr_r->child);
470 p_delete(&query_ptr_r);
471 if(c)
472 return luaA_client_userdata_new(L, c);
473 else
475 lua_pushnil(L);
476 return 1;
480 return 0;
483 const struct luaL_reg awesome_mouse_methods[] =
485 { "__index", luaA_mouse_index },
486 { "__newindex", luaA_mouse_newindex },
487 { "coords", luaA_mouse_coords },
488 { "client_under_pointer", luaA_mouse_client_under_pointer },
489 { NULL, NULL }
491 const struct luaL_reg awesome_mouse_meta[] =
493 { NULL, NULL }
496 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80