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 extern awesome_t globalconf
;
32 DO_LUA_NEW(static, button_t
, button
, "button", button_ref
)
33 DO_LUA_GC(button_t
, button
, "button", button_unref
)
34 DO_LUA_EQ(button_t
, button
, "button")
37 * \param button The button to destroy.
40 button_delete(button_t
**button
)
42 luaL_unref(globalconf
.L
, LUA_REGISTRYINDEX
, (*button
)->press
);
43 luaL_unref(globalconf
.L
, LUA_REGISTRYINDEX
, (*button
)->release
);
47 /** Get the pointer position.
48 * \param window The window to get position on.
49 * \param x will be set to the Pointer-x-coordinate relative to window
50 * \param y will be set to the Pointer-y-coordinate relative to window
51 * \param child Will be set to the window under the pointer.
52 * \param mask will be set to the current buttons state
53 * \return true on success, false if an error occured
56 mouse_query_pointer(xcb_window_t window
, int16_t *x
, int16_t *y
, xcb_window_t
*child
, uint16_t *mask
)
58 xcb_query_pointer_cookie_t query_ptr_c
;
59 xcb_query_pointer_reply_t
*query_ptr_r
;
61 query_ptr_c
= xcb_query_pointer_unchecked(globalconf
.connection
, window
);
62 query_ptr_r
= xcb_query_pointer_reply(globalconf
.connection
, query_ptr_c
, NULL
);
64 if(!query_ptr_r
|| !query_ptr_r
->same_screen
)
67 *x
= query_ptr_r
->win_x
;
68 *y
= query_ptr_r
->win_y
;
70 *mask
= query_ptr_r
->mask
;
72 *child
= query_ptr_r
->child
;
74 p_delete(&query_ptr_r
);
79 /** Get the pointer position on the screen.
80 * \param screen This will be set to the screen number the mouse is on.
81 * \param x This will be set to the Pointer-x-coordinate relative to window.
82 * \param y This will be set to the Pointer-y-coordinate relative to window.
83 * \param child This will be set to the window under the pointer.
84 * \param mask This will be set to the current buttons state.
85 * \return True on success, false if an error occured.
88 mouse_query_pointer_root(int *s
, int16_t *x
, int16_t *y
, xcb_window_t
*child
, uint16_t *mask
)
91 screen
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
94 xcb_window_t root
= xutil_screen_get(globalconf
.connection
, screen
)->root
;
96 if(mouse_query_pointer(root
, x
, y
, child
, mask
))
105 /** Set the pointer position.
106 * \param window The destination window.
107 * \param x X-coordinate inside window.
108 * \param y Y-coordinate inside window.
111 mouse_warp_pointer(xcb_window_t window
, int x
, int y
)
113 xcb_warp_pointer(globalconf
.connection
, XCB_NONE
, window
,
117 /** Create a new mouse button bindings.
118 * \param L The Lua VM state.
119 * \return The number of elements pushed on stack.
121 * \lparam A table with modifiers keys, or a button to clone.
122 * \lparam A mouse button number.
123 * \lparam A function to execute on click events.
124 * \lparam A function to execute on release events.
125 * \lreturn A mouse button binding.
128 luaA_button_new(lua_State
*L
)
131 button_t
*button
, **orig
;
133 if((orig
= luaA_toudata(L
, 2, "button")))
135 button_t
*copy
= p_new(button_t
, 1);
136 copy
->mod
= (*orig
)->mod
;
137 copy
->button
= (*orig
)->button
;
138 if((*orig
)->press
!= LUA_REFNIL
)
140 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*orig
)->press
);
141 luaA_registerfct(L
, -1, ©
->press
);
144 copy
->press
= LUA_REFNIL
;
145 if((*orig
)->release
!= LUA_REFNIL
)
147 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*orig
)->release
);
148 luaA_registerfct(L
, -1, ©
->release
);
151 copy
->release
= LUA_REFNIL
;
152 return luaA_button_userdata_new(L
, copy
);
155 luaA_checktable(L
, 2);
156 /* arg 3 is mouse button */
157 i
= luaL_checknumber(L
, 3);
158 /* arg 4 and 5 are callback functions */
160 luaA_checkfunction(L
, 4);
161 if(lua_gettop(L
) == 5 && !lua_isnil(L
, 5))
162 luaA_checkfunction(L
, 5);
164 button
= p_new(button_t
, 1);
165 button
->button
= xutil_button_fromint(i
);
168 button
->press
= LUA_REFNIL
;
170 luaA_registerfct(L
, 4, &button
->press
);
172 if(lua_gettop(L
) == 5 && !lua_isnil(L
, 5))
173 luaA_registerfct(L
, 5, &button
->release
);
175 button
->release
= LUA_REFNIL
;
177 len
= lua_objlen(L
, 2);
178 for(i
= 1; i
<= len
; i
++)
182 lua_rawgeti(L
, 2, i
);
183 buf
= luaL_checklstring(L
, -1, &blen
);
184 button
->mod
|= xutil_key_mask_fromstr(buf
, blen
);
187 return luaA_button_userdata_new(L
, button
);
190 /** Set a button array with a Lua table.
191 * \param L The Lua VM state.
192 * \param idx The index of the Lua table.
193 * \param buttons The array button to fill.
196 luaA_button_array_set(lua_State
*L
, int idx
, button_array_t
*buttons
)
200 luaA_checktable(L
, idx
);
201 button_array_wipe(buttons
);
202 button_array_init(buttons
);
204 while(lua_next(L
, idx
))
206 b
= luaA_checkudata(L
, -1, "button");
207 button_array_append(buttons
, *b
);
213 /** Push an array of button as an Lua table onto the stack.
214 * \param L The Lua VM state.
215 * \param buttons The button array to push.
216 * \return The number of elements pushed on stack.
219 luaA_button_array_get(lua_State
*L
, button_array_t
*buttons
)
222 for(int i
= 0; i
< buttons
->len
; i
++)
224 luaA_button_userdata_new(L
, buttons
->tab
[i
]);
225 lua_rawseti(L
, -2, i
+ 1);
231 * \param L The Lua VM state.
232 * \return The number of elements pushed on stack.
234 * \lfield press The function called when button press event is received.
235 * \lfield release The function called when button release event is received.
238 luaA_button_index(lua_State
*L
)
240 if(luaA_usemetatable(L
, 1, 2))
244 button_t
**button
= luaA_checkudata(L
, 1, "button");
245 const char *attr
= luaL_checklstring(L
, 2, &len
);
247 switch(a_tokenize(attr
, len
))
250 if((*button
)->press
!= LUA_REFNIL
)
251 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*button
)->press
);
256 if((*button
)->release
!= LUA_REFNIL
)
257 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*button
)->release
);
262 /* works fine, but not *really* neat */
263 lua_pushnumber(L
, (*button
)->button
);
273 * \param L The Lua VM state.
274 * \return The number of elements pushed on stack.
278 luaA_button_newindex(lua_State
*L
)
280 if(luaA_usemetatable(L
, 1, 2))
284 button_t
**button
= luaA_checkudata(L
, 1, "button");
285 const char *attr
= luaL_checklstring(L
, 2, &len
);
287 switch(a_tokenize(attr
, len
))
290 luaA_registerfct(L
, 3, &(*button
)->press
);
293 luaA_registerfct(L
, 3, &(*button
)->release
);
296 (*button
)->button
= xutil_button_fromint(luaL_checknumber(L
, 3));
305 const struct luaL_reg awesome_button_methods
[] =
307 { "__call", luaA_button_new
},
310 const struct luaL_reg awesome_button_meta
[] =
312 { "__index", luaA_button_index
},
313 { "__newindex", luaA_button_newindex
},
314 { "__gc", luaA_button_gc
},
315 { "__eq", luaA_button_eq
},
316 { "__tostring", luaA_button_tostring
},
321 * \param L The Lua VM state.
322 * \return The number of elements pushed on stack.
324 * \lfield coords Mouse coordinates.
325 * \lfield screen Mouse screen number.
328 luaA_mouse_index(lua_State
*L
)
331 const char *attr
= luaL_checklstring(L
, 2, &len
);
332 int16_t mouse_x
, mouse_y
;
335 switch(a_tokenize(attr
, len
))
338 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, NULL
, NULL
))
341 i
= screen_getbycoord(screen
, mouse_x
, mouse_y
);
343 lua_pushnumber(L
, i
+ 1);
352 /** Newindex for mouse.
353 * \param L The Lua VM state.
354 * \return The number of elements pushed on stack.
357 luaA_mouse_newindex(lua_State
*L
)
360 const char *attr
= luaL_checklstring(L
, 2, &len
);
363 int screen
, phys_screen
;
365 switch(a_tokenize(attr
, len
))
368 screen
= luaL_checknumber(L
, 3) - 1;
369 luaA_checkscreen(screen
);
371 /* we need the physical one to get the root window */
372 phys_screen
= screen_virttophys(screen
);
373 root
= xutil_screen_get(globalconf
.connection
, phys_screen
)->root
;
375 x
= globalconf
.screens
[screen
].geometry
.x
;
376 y
= globalconf
.screens
[screen
].geometry
.y
;
378 mouse_warp_pointer(root
, x
, y
);
387 /** Push a table with mouse status.
388 * \param L The Lua VM state.
389 * \param x The x coordinate.
390 * \param y The y coordinate.
391 * \param mask The button mask.
394 luaA_mouse_pushstatus(lua_State
*L
, int x
, int y
, uint16_t mask
)
397 lua_pushnumber(L
, x
);
398 lua_setfield(L
, -2, "x");
399 lua_pushnumber(L
, y
);
400 lua_setfield(L
, -2, "y");
406 for(uint16_t maski
= XCB_BUTTON_MASK_1
; maski
<= XCB_BUTTON_MASK_5
; maski
<<= 1)
409 lua_pushboolean(L
, true);
411 lua_pushboolean(L
, false);
412 lua_rawseti(L
, -2, i
++);
414 lua_setfield(L
, -2, "buttons");
418 /** Get or set the mouse coords.
419 * \param L The Lua VM state.
420 * \return The number of elements pushed on stack.
422 * \lparam None or a table with x and y keys as mouse coordinates.
423 * \lreturn A table with mouse coordinates.
426 luaA_mouse_coords(lua_State
*L
)
430 int16_t mouse_x
, mouse_y
;
432 if(lua_gettop(L
) == 1)
436 luaA_checktable(L
, 1);
438 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, NULL
, &mask
))
441 x
= luaA_getopt_number(L
, 1, "x", mouse_x
);
442 y
= luaA_getopt_number(L
, 1, "y", mouse_y
);
444 root
= xutil_screen_get(globalconf
.connection
, screen
)->root
;
445 mouse_warp_pointer(root
, x
, y
);
449 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, NULL
, &mask
))
452 return luaA_mouse_pushstatus(L
, mouse_x
, mouse_y
, mask
);
455 /** Get the client which is under the pointer.
456 * \param L The Lua VM state.
457 * \return The number of elements pushed on stack.
459 * \lreturn A client or nil.
462 luaA_mouse_object_under_pointer(lua_State
*L
)
465 int16_t mouse_x
, mouse_y
;
468 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, &child
, NULL
))
473 if((wibox
= wibox_getbywin(child
)))
475 luaA_wibox_userdata_new(L
, wibox
);
477 int16_t x
= mouse_x
- wibox
->sw
.geometry
.x
;
478 int16_t y
= mouse_y
- wibox
->sw
.geometry
.y
;
480 widget_t
*widget
= widget_getbycoords(wibox
->position
, &wibox
->widgets
,
481 wibox
->sw
.geometry
.width
,
482 wibox
->sw
.geometry
.height
,
487 luaA_widget_userdata_new(L
, widget
);
492 else if((client
= client_getbywin(child
)))
493 return luaA_client_userdata_new(L
, client
);
498 const struct luaL_reg awesome_mouse_methods
[] =
500 { "__index", luaA_mouse_index
},
501 { "__newindex", luaA_mouse_newindex
},
502 { "coords", luaA_mouse_coords
},
503 { "object_under_pointer", luaA_mouse_object_under_pointer
},
506 const struct luaL_reg awesome_mouse_meta
[] =
511 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80