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_TOSTRING(button_t
, button
, "button")
31 LUA_OBJECT_FUNCS(button_t
, button
, "button")
34 button_unref_simplified(button_t
**b
)
36 button_unref(globalconf
.L
, *b
);
40 * \param L The Lua VM state.
44 luaA_button_gc(lua_State
*L
)
46 button_t
*button
= luaL_checkudata(L
, 1, "button");
47 luaL_unref(globalconf
.L
, LUA_REGISTRYINDEX
, button
->press
);
48 luaL_unref(globalconf
.L
, LUA_REGISTRYINDEX
, button
->release
);
52 /** Get the pointer position.
53 * \param window The window to get position on.
54 * \param x will be set to the Pointer-x-coordinate relative to window
55 * \param y will be set to the Pointer-y-coordinate relative to window
56 * \param child Will be set to the window under the pointer.
57 * \param mask will be set to the current buttons state
58 * \return true on success, false if an error occured
61 mouse_query_pointer(xcb_window_t window
, int16_t *x
, int16_t *y
, xcb_window_t
*child
, uint16_t *mask
)
63 xcb_query_pointer_cookie_t query_ptr_c
;
64 xcb_query_pointer_reply_t
*query_ptr_r
;
66 query_ptr_c
= xcb_query_pointer_unchecked(globalconf
.connection
, window
);
67 query_ptr_r
= xcb_query_pointer_reply(globalconf
.connection
, query_ptr_c
, NULL
);
69 if(!query_ptr_r
|| !query_ptr_r
->same_screen
)
72 *x
= query_ptr_r
->win_x
;
73 *y
= query_ptr_r
->win_y
;
75 *mask
= query_ptr_r
->mask
;
77 *child
= query_ptr_r
->child
;
79 p_delete(&query_ptr_r
);
84 /** Get the pointer position on the screen.
85 * \param screen This will be set to the screen number the mouse is on.
86 * \param x This will be set to the Pointer-x-coordinate relative to window.
87 * \param y This will be set to the Pointer-y-coordinate relative to window.
88 * \param child This will be set to the window under the pointer.
89 * \param mask This will be set to the current buttons state.
90 * \return True on success, false if an error occured.
93 mouse_query_pointer_root(int *s
, int16_t *x
, int16_t *y
, xcb_window_t
*child
, uint16_t *mask
)
96 screen
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
99 xcb_window_t root
= xutil_screen_get(globalconf
.connection
, screen
)->root
;
101 if(mouse_query_pointer(root
, x
, y
, child
, mask
))
110 /** Set the pointer position.
111 * \param window The destination window.
112 * \param x X-coordinate inside window.
113 * \param y Y-coordinate inside window.
116 mouse_warp_pointer(xcb_window_t window
, int x
, int y
)
118 xcb_warp_pointer(globalconf
.connection
, XCB_NONE
, window
,
122 /** Create a new mouse button bindings.
123 * \param L The Lua VM state.
124 * \return The number of elements pushed on stack.
126 * \lparam A table with modifiers keys, or a button to clone.
127 * \lparam A mouse button number.
128 * \lparam A function to execute on click events.
129 * \lparam A function to execute on release events.
130 * \lreturn A mouse button binding.
133 luaA_button_new(lua_State
*L
)
136 button_t
*button
, *orig
;
137 luaA_ref press
= LUA_REFNIL
, release
= LUA_REFNIL
;
139 if((orig
= luaA_toudata(L
, 2, "button")))
141 button_t
*copy
= button_new(L
);
142 copy
->mod
= orig
->mod
;
143 copy
->button
= orig
->button
;
144 if(orig
->press
!= LUA_REFNIL
)
146 lua_rawgeti(L
, LUA_REGISTRYINDEX
, orig
->press
);
147 luaA_registerfct(L
, -1, ©
->press
);
151 copy
->press
= LUA_REFNIL
;
152 if(orig
->release
!= LUA_REFNIL
)
154 lua_rawgeti(L
, LUA_REGISTRYINDEX
, orig
->release
);
155 luaA_registerfct(L
, -1, ©
->release
);
159 copy
->release
= LUA_REFNIL
;
163 luaA_checktable(L
, 2);
164 /* arg 3 is mouse button */
165 i
= luaL_checknumber(L
, 3);
167 /* arg 4 and 5 are callback functions, check they are functions... */
169 luaA_checkfunction(L
, 4);
170 if(lua_gettop(L
) == 5 && !lua_isnil(L
, 5))
171 luaA_checkfunction(L
, 5);
173 /* ... then register (can't register before since 5 maybe not nil but not a
176 luaA_registerfct(L
, 4, &press
);
177 if(lua_gettop(L
) == 5 && !lua_isnil(L
, 5))
178 luaA_registerfct(L
, 5, &release
);
180 button
= button_new(L
);
181 button
->press
= press
;
182 button
->release
= release
;
183 button
->button
= xutil_button_fromint(i
);
185 len
= lua_objlen(L
, 2);
186 for(i
= 1; i
<= len
; i
++)
190 lua_rawgeti(L
, 2, i
);
191 buf
= luaL_checklstring(L
, -1, &blen
);
192 button
->mod
|= xutil_key_mask_fromstr(buf
, blen
);
199 /** Set a button array with a Lua table.
200 * \param L The Lua VM state.
201 * \param idx The index of the Lua table.
202 * \param buttons The array button to fill.
205 luaA_button_array_set(lua_State
*L
, int idx
, button_array_t
*buttons
)
207 luaA_checktable(L
, idx
);
208 button_array_wipe(buttons
);
209 button_array_init(buttons
);
211 while(lua_next(L
, idx
))
212 button_array_append(buttons
, button_ref(L
));
215 /** Push an array of button as an Lua table onto the stack.
216 * \param L The Lua VM state.
217 * \param buttons The button array to push.
218 * \return The number of elements pushed on stack.
221 luaA_button_array_get(lua_State
*L
, button_array_t
*buttons
)
223 lua_createtable(L
, buttons
->len
, 0);
224 for(int i
= 0; i
< buttons
->len
; i
++)
226 button_push(L
, buttons
->tab
[i
]);
227 lua_rawseti(L
, -2, i
+ 1);
233 * \param L The Lua VM state.
234 * \return The number of elements pushed on stack.
236 * \lfield press The function called when button press event is received.
237 * \lfield release The function called when button release event is received.
240 luaA_button_index(lua_State
*L
)
242 if(luaA_usemetatable(L
, 1, 2))
246 button_t
*button
= luaL_checkudata(L
, 1, "button");
247 const char *attr
= luaL_checklstring(L
, 2, &len
);
249 switch(a_tokenize(attr
, len
))
252 if(button
->press
!= LUA_REFNIL
)
253 lua_rawgeti(L
, LUA_REGISTRYINDEX
, button
->press
);
258 if(button
->release
!= LUA_REFNIL
)
259 lua_rawgeti(L
, LUA_REGISTRYINDEX
, button
->release
);
264 /* works fine, but not *really* neat */
265 lua_pushnumber(L
, button
->button
);
275 * \param L The Lua VM state.
276 * \return The number of elements pushed on stack.
280 luaA_button_newindex(lua_State
*L
)
282 if(luaA_usemetatable(L
, 1, 2))
286 button_t
*button
= luaL_checkudata(L
, 1, "button");
287 const char *attr
= luaL_checklstring(L
, 2, &len
);
289 switch(a_tokenize(attr
, len
))
292 luaA_registerfct(L
, 3, &button
->press
);
295 luaA_registerfct(L
, 3, &button
->release
);
298 button
->button
= xutil_button_fromint(luaL_checknumber(L
, 3));
307 const struct luaL_reg awesome_button_methods
[] =
309 { "__call", luaA_button_new
},
312 const struct luaL_reg awesome_button_meta
[] =
314 { "__index", luaA_button_index
},
315 { "__newindex", luaA_button_newindex
},
316 { "__gc", luaA_button_gc
},
317 { "__tostring", luaA_button_tostring
},
322 * \param L The Lua VM state.
323 * \return The number of elements pushed on stack.
325 * \lfield coords Mouse coordinates.
326 * \lfield screen Mouse screen number.
329 luaA_mouse_index(lua_State
*L
)
332 const char *attr
= luaL_checklstring(L
, 2, &len
);
333 int16_t mouse_x
, mouse_y
;
336 switch(a_tokenize(attr
, len
))
339 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, NULL
, NULL
))
342 i
= screen_getbycoord(screen
, mouse_x
, mouse_y
);
344 lua_pushnumber(L
, i
+ 1);
353 /** Newindex for mouse.
354 * \param L The Lua VM state.
355 * \return The number of elements pushed on stack.
358 luaA_mouse_newindex(lua_State
*L
)
361 const char *attr
= luaL_checklstring(L
, 2, &len
);
364 int screen
, phys_screen
;
366 switch(a_tokenize(attr
, len
))
369 screen
= luaL_checknumber(L
, 3) - 1;
370 luaA_checkscreen(screen
);
372 /* we need the physical one to get the root window */
373 phys_screen
= screen_virttophys(screen
);
374 root
= xutil_screen_get(globalconf
.connection
, phys_screen
)->root
;
376 x
= globalconf
.screens
[screen
].geometry
.x
;
377 y
= globalconf
.screens
[screen
].geometry
.y
;
379 mouse_warp_pointer(root
, x
, y
);
388 /** Push a table with mouse status.
389 * \param L The Lua VM state.
390 * \param x The x coordinate.
391 * \param y The y coordinate.
392 * \param mask The button mask.
395 luaA_mouse_pushstatus(lua_State
*L
, int x
, int y
, uint16_t mask
)
398 lua_pushnumber(L
, x
);
399 lua_setfield(L
, -2, "x");
400 lua_pushnumber(L
, y
);
401 lua_setfield(L
, -2, "y");
407 for(uint16_t maski
= XCB_BUTTON_MASK_1
; maski
<= XCB_BUTTON_MASK_5
; maski
<<= 1)
410 lua_pushboolean(L
, true);
412 lua_pushboolean(L
, false);
413 lua_rawseti(L
, -2, i
++);
415 lua_setfield(L
, -2, "buttons");
419 /** Get or set the mouse coords.
420 * \param L The Lua VM state.
421 * \return The number of elements pushed on stack.
423 * \lparam None or a table with x and y keys as mouse coordinates.
424 * \lreturn A table with mouse coordinates.
427 luaA_mouse_coords(lua_State
*L
)
431 int16_t mouse_x
, mouse_y
;
433 if(lua_gettop(L
) == 1)
437 luaA_checktable(L
, 1);
439 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, NULL
, &mask
))
442 x
= luaA_getopt_number(L
, 1, "x", mouse_x
);
443 y
= luaA_getopt_number(L
, 1, "y", mouse_y
);
445 root
= xutil_screen_get(globalconf
.connection
, screen
)->root
;
446 mouse_warp_pointer(root
, x
, y
);
450 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, NULL
, &mask
))
453 return luaA_mouse_pushstatus(L
, mouse_x
, mouse_y
, mask
);
456 /** Get the client which is under the pointer.
457 * \param L The Lua VM state.
458 * \return The number of elements pushed on stack.
460 * \lreturn A client or nil.
463 luaA_mouse_object_under_pointer(lua_State
*L
)
466 int16_t mouse_x
, mouse_y
;
469 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, &child
, NULL
))
474 if((wibox
= wibox_getbywin(child
)))
476 wibox_push(L
, wibox
);
478 int16_t x
= mouse_x
- wibox
->sw
.geometry
.x
;
479 int16_t y
= mouse_y
- wibox
->sw
.geometry
.y
;
481 widget_t
*widget
= widget_getbycoords(wibox
->position
, &wibox
->widgets
,
482 wibox
->sw
.geometry
.width
,
483 wibox
->sw
.geometry
.height
,
488 widget_push(L
, widget
);
493 else if((client
= client_getbywin(child
)))
494 return client_push(globalconf
.L
, client
);
499 const struct luaL_reg awesome_mouse_methods
[] =
501 { "__index", luaA_mouse_index
},
502 { "__newindex", luaA_mouse_newindex
},
503 { "coords", luaA_mouse_coords
},
504 { "object_under_pointer", luaA_mouse_object_under_pointer
},
507 const struct luaL_reg awesome_mouse_meta
[] =
512 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80