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.
26 #include "common/xcursor.h"
27 #include "common/xutil.h"
29 DO_LUA_TOSTRING(button_t
, button
, "button")
30 LUA_OBJECT_FUNCS(button_t
, button
, "button")
33 button_unref_simplified(button_t
**b
)
35 button_unref(globalconf
.L
, *b
);
39 * \param L The Lua VM state.
43 luaA_button_gc(lua_State
*L
)
45 button_t
*button
= luaL_checkudata(L
, 1, "button");
46 luaL_unref(globalconf
.L
, LUA_REGISTRYINDEX
, button
->press
);
47 luaL_unref(globalconf
.L
, LUA_REGISTRYINDEX
, button
->release
);
51 /** Get the pointer position.
52 * \param window The window to get position on.
53 * \param x will be set to the Pointer-x-coordinate relative to window
54 * \param y will be set to the Pointer-y-coordinate relative to window
55 * \param child Will be set to the window under the pointer.
56 * \param mask will be set to the current buttons state
57 * \return true on success, false if an error occured
60 mouse_query_pointer(xcb_window_t window
, int16_t *x
, int16_t *y
, xcb_window_t
*child
, uint16_t *mask
)
62 xcb_query_pointer_cookie_t query_ptr_c
;
63 xcb_query_pointer_reply_t
*query_ptr_r
;
65 query_ptr_c
= xcb_query_pointer_unchecked(globalconf
.connection
, window
);
66 query_ptr_r
= xcb_query_pointer_reply(globalconf
.connection
, query_ptr_c
, NULL
);
68 if(!query_ptr_r
|| !query_ptr_r
->same_screen
)
71 *x
= query_ptr_r
->win_x
;
72 *y
= query_ptr_r
->win_y
;
74 *mask
= query_ptr_r
->mask
;
76 *child
= query_ptr_r
->child
;
78 p_delete(&query_ptr_r
);
83 /** Get the pointer position on the screen.
84 * \param screen This will be set to the screen the mouse is on.
85 * \param x This will be set to the Pointer-x-coordinate relative to window.
86 * \param y This will be set to the Pointer-y-coordinate relative to window.
87 * \param child This will be set to the window under the pointer.
88 * \param mask This will be set to the current buttons state.
89 * \return True on success, false if an error occured.
92 mouse_query_pointer_root(screen_t
**s
, int16_t *x
, int16_t *y
, xcb_window_t
*child
, uint16_t *mask
)
95 screen
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
98 xcb_window_t root
= xutil_screen_get(globalconf
.connection
, screen
)->root
;
100 if(mouse_query_pointer(root
, x
, y
, child
, mask
))
102 *s
= &globalconf
.screens
.tab
[screen
];
109 /** Set the pointer position.
110 * \param window The destination window.
111 * \param x X-coordinate inside window.
112 * \param y Y-coordinate inside window.
115 mouse_warp_pointer(xcb_window_t window
, int x
, int y
)
117 xcb_warp_pointer(globalconf
.connection
, XCB_NONE
, window
,
121 /** Create a new mouse button bindings.
122 * \param L The Lua VM state.
123 * \return The number of elements pushed on stack.
125 * \lparam A table with modifiers keys, or a button to clone.
126 * \lparam A mouse button number.
127 * \lparam A function to execute on click events.
128 * \lparam A function to execute on release events.
129 * \lreturn A mouse button binding.
132 luaA_button_new(lua_State
*L
)
135 button_t
*button
, *orig
;
136 luaA_ref press
= LUA_REFNIL
, release
= LUA_REFNIL
;
138 if((orig
= luaA_toudata(L
, 2, "button")))
140 button_t
*copy
= button_new(L
);
141 copy
->mod
= orig
->mod
;
142 copy
->button
= orig
->button
;
143 if(orig
->press
!= LUA_REFNIL
)
145 lua_rawgeti(L
, LUA_REGISTRYINDEX
, orig
->press
);
146 luaA_registerfct(L
, -1, ©
->press
);
150 copy
->press
= LUA_REFNIL
;
151 if(orig
->release
!= LUA_REFNIL
)
153 lua_rawgeti(L
, LUA_REGISTRYINDEX
, orig
->release
);
154 luaA_registerfct(L
, -1, ©
->release
);
158 copy
->release
= LUA_REFNIL
;
162 luaA_checktable(L
, 2);
163 /* arg 3 is mouse button */
164 i
= luaL_checknumber(L
, 3);
166 /* arg 4 and 5 are callback functions, check they are functions... */
168 luaA_checkfunction(L
, 4);
169 if(lua_gettop(L
) == 5 && !lua_isnil(L
, 5))
170 luaA_checkfunction(L
, 5);
172 /* ... then register (can't register before since 5 maybe not nil but not a
175 luaA_registerfct(L
, 4, &press
);
176 if(lua_gettop(L
) == 5 && !lua_isnil(L
, 5))
177 luaA_registerfct(L
, 5, &release
);
179 button
= button_new(L
);
180 button
->press
= press
;
181 button
->release
= release
;
182 button
->button
= xutil_button_fromint(i
);
184 len
= lua_objlen(L
, 2);
185 for(i
= 1; i
<= len
; i
++)
189 lua_rawgeti(L
, 2, i
);
190 buf
= luaL_checklstring(L
, -1, &blen
);
191 button
->mod
|= xutil_key_mask_fromstr(buf
, blen
);
198 /** Set a button array with a Lua table.
199 * \param L The Lua VM state.
200 * \param idx The index of the Lua table.
201 * \param buttons The array button to fill.
204 luaA_button_array_set(lua_State
*L
, int idx
, button_array_t
*buttons
)
206 luaA_checktable(L
, idx
);
207 button_array_wipe(buttons
);
208 button_array_init(buttons
);
210 while(lua_next(L
, idx
))
211 button_array_append(buttons
, button_ref(L
));
214 /** Push an array of button as an Lua table onto the stack.
215 * \param L The Lua VM state.
216 * \param buttons The button array to push.
217 * \return The number of elements pushed on stack.
220 luaA_button_array_get(lua_State
*L
, button_array_t
*buttons
)
222 lua_createtable(L
, buttons
->len
, 0);
223 for(int i
= 0; i
< buttons
->len
; i
++)
225 button_push(L
, buttons
->tab
[i
]);
226 lua_rawseti(L
, -2, i
+ 1);
232 * \param L The Lua VM state.
233 * \return The number of elements pushed on stack.
235 * \lfield press The function called when button press event is received.
236 * \lfield release The function called when button release event is received.
239 luaA_button_index(lua_State
*L
)
241 if(luaA_usemetatable(L
, 1, 2))
245 button_t
*button
= luaL_checkudata(L
, 1, "button");
246 const char *attr
= luaL_checklstring(L
, 2, &len
);
248 switch(a_tokenize(attr
, len
))
251 if(button
->press
!= LUA_REFNIL
)
252 lua_rawgeti(L
, LUA_REGISTRYINDEX
, button
->press
);
257 if(button
->release
!= LUA_REFNIL
)
258 lua_rawgeti(L
, LUA_REGISTRYINDEX
, button
->release
);
263 /* works fine, but not *really* neat */
264 lua_pushnumber(L
, button
->button
);
274 * \param L The Lua VM state.
275 * \return The number of elements pushed on stack.
279 luaA_button_newindex(lua_State
*L
)
281 if(luaA_usemetatable(L
, 1, 2))
285 button_t
*button
= luaL_checkudata(L
, 1, "button");
286 const char *attr
= luaL_checklstring(L
, 2, &len
);
288 switch(a_tokenize(attr
, len
))
291 luaA_registerfct(L
, 3, &button
->press
);
294 luaA_registerfct(L
, 3, &button
->release
);
297 button
->button
= xutil_button_fromint(luaL_checknumber(L
, 3));
306 const struct luaL_reg awesome_button_methods
[] =
308 { "__call", luaA_button_new
},
311 const struct luaL_reg awesome_button_meta
[] =
313 { "__index", luaA_button_index
},
314 { "__newindex", luaA_button_newindex
},
315 { "__gc", luaA_button_gc
},
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 screen
= screen_getbycoord(screen
, mouse_x
, mouse_y
);
343 lua_pushnumber(L
, screen
->index
+ 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
.tab
[screen
].geometry
.x
;
376 y
= globalconf
.screens
.tab
[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
;
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
->index
)->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