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"
28 #include "common/xcursor.h"
30 DO_LUA_NEW(static, button_t
, button
, "button", button_ref
)
31 DO_LUA_GC(button_t
, button
, "button", button_unref
)
32 DO_LUA_EQ(button_t
, button
, "button")
35 * \param button The button to destroy.
38 button_delete(button_t
**button
)
40 luaL_unref(globalconf
.L
, LUA_REGISTRYINDEX
, (*button
)->press
);
41 luaL_unref(globalconf
.L
, LUA_REGISTRYINDEX
, (*button
)->release
);
45 /** Get the pointer position.
46 * \param window The window to get position on.
47 * \param x will be set to the Pointer-x-coordinate relative to window
48 * \param y will be set to the Pointer-y-coordinate relative to window
49 * \param child Will be set to the window under the pointer.
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
, int16_t *x
, int16_t *y
, xcb_window_t
*child
, 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 *child
= query_ptr_r
->child
;
72 p_delete(&query_ptr_r
);
77 /** Get the pointer position on the screen.
78 * \param screen This will be set to the screen number the mouse is on.
79 * \param x This will be set to the Pointer-x-coordinate relative to window.
80 * \param y This will be set to the Pointer-y-coordinate relative to window.
81 * \param child This will be set to the window under the pointer.
82 * \param mask This will be set to the current buttons state.
83 * \return True on success, false if an error occured.
86 mouse_query_pointer_root(int *s
, int16_t *x
, int16_t *y
, xcb_window_t
*child
, uint16_t *mask
)
89 screen
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
92 xcb_window_t root
= xutil_screen_get(globalconf
.connection
, screen
)->root
;
94 if(mouse_query_pointer(root
, x
, y
, child
, mask
))
103 /** Set the pointer position.
104 * \param window The destination window.
105 * \param x X-coordinate inside window.
106 * \param y Y-coordinate inside window.
109 mouse_warp_pointer(xcb_window_t window
, int x
, int y
)
111 xcb_warp_pointer(globalconf
.connection
, XCB_NONE
, window
,
115 /** Create a new mouse button bindings.
116 * \param L The Lua VM state.
117 * \return The number of elements pushed on stack.
119 * \lparam A table with modifiers keys, or a button to clone.
120 * \lparam A mouse button number.
121 * \lparam A function to execute on click events.
122 * \lparam A function to execute on release events.
123 * \lreturn A mouse button binding.
126 luaA_button_new(lua_State
*L
)
129 button_t
*button
, **orig
;
131 if((orig
= luaA_toudata(L
, 2, "button")))
133 button_t
*copy
= p_new(button_t
, 1);
134 copy
->mod
= (*orig
)->mod
;
135 copy
->button
= (*orig
)->button
;
136 if((*orig
)->press
!= LUA_REFNIL
)
138 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*orig
)->press
);
139 luaA_registerfct(L
, -1, ©
->press
);
142 copy
->press
= LUA_REFNIL
;
143 if((*orig
)->release
!= LUA_REFNIL
)
145 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*orig
)->release
);
146 luaA_registerfct(L
, -1, ©
->release
);
149 copy
->release
= LUA_REFNIL
;
150 return luaA_button_userdata_new(L
, copy
);
153 luaA_checktable(L
, 2);
154 /* arg 3 is mouse button */
155 i
= luaL_checknumber(L
, 3);
156 /* arg 4 and 5 are callback functions */
158 luaA_checkfunction(L
, 4);
159 if(lua_gettop(L
) == 5 && !lua_isnil(L
, 5))
160 luaA_checkfunction(L
, 5);
162 button
= p_new(button_t
, 1);
163 button
->button
= xutil_button_fromint(i
);
166 button
->press
= LUA_REFNIL
;
168 luaA_registerfct(L
, 4, &button
->press
);
170 if(lua_gettop(L
) == 5 && !lua_isnil(L
, 5))
171 luaA_registerfct(L
, 5, &button
->release
);
173 button
->release
= LUA_REFNIL
;
175 len
= lua_objlen(L
, 2);
176 for(i
= 1; i
<= len
; i
++)
180 lua_rawgeti(L
, 2, i
);
181 buf
= luaL_checklstring(L
, -1, &blen
);
182 button
->mod
|= xutil_key_mask_fromstr(buf
, blen
);
185 return luaA_button_userdata_new(L
, button
);
188 /** Set a button array with a Lua table.
189 * \param L The Lua VM state.
190 * \param idx The index of the Lua table.
191 * \param buttons The array button to fill.
194 luaA_button_array_set(lua_State
*L
, int idx
, button_array_t
*buttons
)
198 luaA_checktable(L
, idx
);
199 button_array_wipe(buttons
);
200 button_array_init(buttons
);
202 while(lua_next(L
, idx
))
204 b
= luaA_checkudata(L
, -1, "button");
205 button_array_append(buttons
, *b
);
211 /** Push an array of button as an Lua table onto the stack.
212 * \param L The Lua VM state.
213 * \param buttons The button array to push.
214 * \return The number of elements pushed on stack.
217 luaA_button_array_get(lua_State
*L
, button_array_t
*buttons
)
219 lua_createtable(L
, buttons
->len
, 0);
220 for(int i
= 0; i
< buttons
->len
; i
++)
222 luaA_button_userdata_new(L
, buttons
->tab
[i
]);
223 lua_rawseti(L
, -2, i
+ 1);
229 * \param L The Lua VM state.
230 * \return The number of elements pushed on stack.
232 * \lfield press The function called when button press event is received.
233 * \lfield release The function called when button release event is received.
236 luaA_button_index(lua_State
*L
)
238 if(luaA_usemetatable(L
, 1, 2))
242 button_t
**button
= luaA_checkudata(L
, 1, "button");
243 const char *attr
= luaL_checklstring(L
, 2, &len
);
245 switch(a_tokenize(attr
, len
))
248 if((*button
)->press
!= LUA_REFNIL
)
249 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*button
)->press
);
254 if((*button
)->release
!= LUA_REFNIL
)
255 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*button
)->release
);
260 /* works fine, but not *really* neat */
261 lua_pushnumber(L
, (*button
)->button
);
271 * \param L The Lua VM state.
272 * \return The number of elements pushed on stack.
276 luaA_button_newindex(lua_State
*L
)
278 if(luaA_usemetatable(L
, 1, 2))
282 button_t
**button
= luaA_checkudata(L
, 1, "button");
283 const char *attr
= luaL_checklstring(L
, 2, &len
);
285 switch(a_tokenize(attr
, len
))
288 luaA_registerfct(L
, 3, &(*button
)->press
);
291 luaA_registerfct(L
, 3, &(*button
)->release
);
294 (*button
)->button
= xutil_button_fromint(luaL_checknumber(L
, 3));
303 const struct luaL_reg awesome_button_methods
[] =
305 { "__call", luaA_button_new
},
308 const struct luaL_reg awesome_button_meta
[] =
310 { "__index", luaA_button_index
},
311 { "__newindex", luaA_button_newindex
},
312 { "__gc", luaA_button_gc
},
313 { "__eq", luaA_button_eq
},
314 { "__tostring", luaA_button_tostring
},
319 * \param L The Lua VM state.
320 * \return The number of elements pushed on stack.
322 * \lfield coords Mouse coordinates.
323 * \lfield screen Mouse screen number.
326 luaA_mouse_index(lua_State
*L
)
329 const char *attr
= luaL_checklstring(L
, 2, &len
);
330 int16_t mouse_x
, mouse_y
;
333 switch(a_tokenize(attr
, len
))
336 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, NULL
, NULL
))
339 i
= screen_getbycoord(screen
, mouse_x
, mouse_y
);
341 lua_pushnumber(L
, i
+ 1);
350 /** Newindex for mouse.
351 * \param L The Lua VM state.
352 * \return The number of elements pushed on stack.
355 luaA_mouse_newindex(lua_State
*L
)
358 const char *attr
= luaL_checklstring(L
, 2, &len
);
361 int screen
, phys_screen
;
363 switch(a_tokenize(attr
, len
))
366 screen
= luaL_checknumber(L
, 3) - 1;
367 luaA_checkscreen(screen
);
369 /* we need the physical one to get the root window */
370 phys_screen
= screen_virttophys(screen
);
371 root
= xutil_screen_get(globalconf
.connection
, phys_screen
)->root
;
373 x
= globalconf
.screens
[screen
].geometry
.x
;
374 y
= globalconf
.screens
[screen
].geometry
.y
;
376 mouse_warp_pointer(root
, x
, y
);
385 /** Push a table with mouse status.
386 * \param L The Lua VM state.
387 * \param x The x coordinate.
388 * \param y The y coordinate.
389 * \param mask The button mask.
392 luaA_mouse_pushstatus(lua_State
*L
, int x
, int y
, uint16_t mask
)
395 lua_pushnumber(L
, x
);
396 lua_setfield(L
, -2, "x");
397 lua_pushnumber(L
, y
);
398 lua_setfield(L
, -2, "y");
404 for(uint16_t maski
= XCB_BUTTON_MASK_1
; maski
<= XCB_BUTTON_MASK_5
; maski
<<= 1)
407 lua_pushboolean(L
, true);
409 lua_pushboolean(L
, false);
410 lua_rawseti(L
, -2, i
++);
412 lua_setfield(L
, -2, "buttons");
416 /** Get or set the mouse coords.
417 * \param L The Lua VM state.
418 * \return The number of elements pushed on stack.
420 * \lparam None or a table with x and y keys as mouse coordinates.
421 * \lreturn A table with mouse coordinates.
424 luaA_mouse_coords(lua_State
*L
)
428 int16_t mouse_x
, mouse_y
;
430 if(lua_gettop(L
) == 1)
434 luaA_checktable(L
, 1);
436 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, NULL
, &mask
))
439 x
= luaA_getopt_number(L
, 1, "x", mouse_x
);
440 y
= luaA_getopt_number(L
, 1, "y", mouse_y
);
442 root
= xutil_screen_get(globalconf
.connection
, screen
)->root
;
443 mouse_warp_pointer(root
, x
, y
);
447 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, NULL
, &mask
))
450 return luaA_mouse_pushstatus(L
, mouse_x
, mouse_y
, mask
);
453 /** Get the client which is under the pointer.
454 * \param L The Lua VM state.
455 * \return The number of elements pushed on stack.
457 * \lreturn A client or nil.
460 luaA_mouse_object_under_pointer(lua_State
*L
)
463 int16_t mouse_x
, mouse_y
;
466 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, &child
, NULL
))
471 if((wibox
= wibox_getbywin(child
)))
473 luaA_wibox_userdata_new(L
, wibox
);
475 int16_t x
= mouse_x
- wibox
->sw
.geometry
.x
;
476 int16_t y
= mouse_y
- wibox
->sw
.geometry
.y
;
478 widget_t
*widget
= widget_getbycoords(wibox
->position
, &wibox
->widgets
,
479 wibox
->sw
.geometry
.width
,
480 wibox
->sw
.geometry
.height
,
485 luaA_widget_userdata_new(L
, widget
);
490 else if((client
= client_getbywin(child
)))
491 return luaA_client_userdata_new(L
, client
);
496 const struct luaL_reg awesome_mouse_methods
[] =
498 { "__index", luaA_mouse_index
},
499 { "__newindex", luaA_mouse_newindex
},
500 { "coords", luaA_mouse_coords
},
501 { "object_under_pointer", luaA_mouse_object_under_pointer
},
504 const struct luaL_reg awesome_mouse_meta
[] =
509 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80