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.
24 #include "common/tokenize.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")
36 * \param button The button to destroy.
39 button_delete(button_t
**button
)
41 luaL_unref(globalconf
.L
, LUA_REGISTRYINDEX
, (*button
)->press
);
42 luaL_unref(globalconf
.L
, LUA_REGISTRYINDEX
, (*button
)->release
);
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
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
)
65 *x
= query_ptr_r
->win_x
;
66 *y
= query_ptr_r
->win_y
;
68 *mask
= query_ptr_r
->mask
;
70 p_delete(&query_ptr_r
);
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.
83 mouse_query_pointer_root(int *s
, int *x
, int *y
, uint16_t *mask
)
86 screen
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
89 xcb_window_t root
= xutil_screen_get(globalconf
.connection
, screen
)->root
;
91 if(mouse_query_pointer(root
, x
, y
, mask
))
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.
106 mouse_warp_pointer(xcb_window_t window
, int x
, int y
)
108 xcb_warp_pointer(globalconf
.connection
, XCB_NONE
, window
,
112 /** Create a new mouse button bindings.
113 * \param L The Lua VM state.
114 * \return The number of elements pushed on stack.
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.
123 luaA_button_new(lua_State
*L
)
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, ©
->press
);
139 copy
->press
= LUA_REFNIL
;
140 if((*orig
)->release
!= LUA_REFNIL
)
142 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*orig
)->release
);
143 luaA_registerfct(L
, -1, ©
->release
);
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 */
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
);
163 button
->press
= LUA_REFNIL
;
165 luaA_registerfct(L
, 4, &button
->press
);
167 if(lua_gettop(L
) == 5 && !lua_isnil(L
, 5))
168 luaA_registerfct(L
, 5, &button
->release
);
170 button
->release
= LUA_REFNIL
;
172 len
= lua_objlen(L
, 2);
173 for(i
= 1; i
<= len
; i
++)
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.
191 luaA_button_array_set(lua_State
*L
, int idx
, button_array_t
*buttons
)
195 luaA_checktable(L
, idx
);
196 button_array_wipe(buttons
);
197 button_array_init(buttons
);
199 while(lua_next(L
, idx
))
201 b
= luaA_checkudata(L
, -1, "button");
202 button_array_append(buttons
, *b
);
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
)
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);
226 * \param L The Lua VM state.
227 * \return The number of elements pushed on stack.
229 * \lfield press The function called when button press event is received.
230 * \lfield release The function called when button release event is received.
233 luaA_button_index(lua_State
*L
)
235 if(luaA_usemetatable(L
, 1, 2))
239 button_t
**button
= luaA_checkudata(L
, 1, "button");
240 const char *attr
= luaL_checklstring(L
, 2, &len
);
242 switch(a_tokenize(attr
, len
))
245 if((*button
)->press
!= LUA_REFNIL
)
246 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*button
)->press
);
251 if((*button
)->release
!= LUA_REFNIL
)
252 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*button
)->release
);
257 /* works fine, but not *really* neat */
258 lua_pushnumber(L
, (*button
)->button
);
268 * \param L The Lua VM state.
269 * \return The number of elements pushed on stack.
273 luaA_button_newindex(lua_State
*L
)
275 if(luaA_usemetatable(L
, 1, 2))
279 button_t
**button
= luaA_checkudata(L
, 1, "button");
280 const char *attr
= luaL_checklstring(L
, 2, &len
);
282 switch(a_tokenize(attr
, len
))
285 luaA_registerfct(L
, 3, &(*button
)->press
);
288 luaA_registerfct(L
, 3, &(*button
)->release
);
291 (*button
)->button
= xutil_button_fromint(luaL_checknumber(L
, 3));
300 const struct luaL_reg awesome_button_methods
[] =
302 { "__call", luaA_button_new
},
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
},
316 * \param L The Lua VM state.
317 * \return The number of elements pushed on stack.
319 * \lfield coords Mouse coordinates.
320 * \lfield screen Mouse screen number.
323 luaA_mouse_index(lua_State
*L
)
326 const char *attr
= luaL_checklstring(L
, 2, &len
);
327 int mouse_x
, mouse_y
, i
;
330 switch(a_tokenize(attr
, len
))
333 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, NULL
))
336 i
= screen_getbycoord(screen
, mouse_x
, mouse_y
);
338 lua_pushnumber(L
, i
+ 1);
347 /** Newindex for mouse.
348 * \param L The Lua VM state.
349 * \return The number of elements pushed on stack.
352 luaA_mouse_newindex(lua_State
*L
)
355 const char *attr
= luaL_checklstring(L
, 2, &len
);
358 int screen
, phys_screen
;
360 switch(a_tokenize(attr
, len
))
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
);
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
)
392 lua_pushnumber(L
, x
);
393 lua_setfield(L
, -2, "x");
394 lua_pushnumber(L
, y
);
395 lua_setfield(L
, -2, "y");
401 for(uint16_t maski
= XCB_BUTTON_MASK_1
; maski
<= XCB_BUTTON_MASK_5
; maski
<<= 1)
404 lua_pushboolean(L
, true);
406 lua_pushboolean(L
, false);
407 lua_rawseti(L
, -2, i
++);
409 lua_setfield(L
, -2, "buttons");
413 /** Get or set the mouse coords.
414 * \param L The Lua VM state.
415 * \return The number of elements pushed on stack.
417 * \lparam None or a table with x and y keys as mouse coordinates.
418 * \lreturn A table with mouse coordinates.
421 luaA_mouse_coords(lua_State
*L
)
424 int screen
, x
, y
, mouse_x
, mouse_y
;
426 if(lua_gettop(L
) == 1)
430 luaA_checktable(L
, 1);
432 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, &mask
))
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
);
443 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, &mask
))
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.
453 * \lreturn A client or nil.
456 luaA_mouse_client_under_pointer(lua_State
*L
)
459 screen
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
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
);
469 client_t
*c
= client_getbywin(query_ptr_r
->child
);
470 p_delete(&query_ptr_r
);
472 return luaA_client_userdata_new(L
, c
);
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
},
491 const struct luaL_reg awesome_mouse_meta
[] =
496 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80