awesome-client: Use rlwrap if it is available
[awesome.git] / mouse.c
blob6e53ae925d68f0b9f523eac5e11ce47d483bb26c
1 /*
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.
22 #include <math.h>
24 #include "screen.h"
25 #include "tag.h"
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")
32 void
33 button_unref_simplified(button_t **b)
35 button_unref(globalconf.L, *b);
38 /** Collect a button.
39 * \param L The Lua VM state.
40 * \return 0.
42 static int
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);
48 return 0;
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
58 **/
59 bool
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)
69 return false;
71 *x = query_ptr_r->win_x;
72 *y = query_ptr_r->win_y;
73 if(mask)
74 *mask = query_ptr_r->mask;
75 if(child)
76 *child = query_ptr_r->child;
78 p_delete(&query_ptr_r);
80 return true;
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.
91 static bool
92 mouse_query_pointer_root(screen_t **s, int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
94 for(int screen = 0;
95 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
96 screen++)
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];
103 return true;
106 return false;
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.
114 static inline void
115 mouse_warp_pointer(xcb_window_t window, int x, int y)
117 xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
118 0, 0, 0, 0, x, y );
121 /** Create a new mouse button bindings.
122 * \param L The Lua VM state.
123 * \return The number of elements pushed on stack.
124 * \luastack
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.
131 static int
132 luaA_button_new(lua_State *L)
134 int i, len;
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, &copy->press);
147 lua_pop(L, 1);
149 else
150 copy->press = LUA_REFNIL;
151 if(orig->release != LUA_REFNIL)
153 lua_rawgeti(L, LUA_REGISTRYINDEX, orig->release);
154 luaA_registerfct(L, -1, &copy->release);
155 lua_pop(L, 1);
157 else
158 copy->release = LUA_REFNIL;
159 return 1;
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... */
167 if(!lua_isnil(L, 4))
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
173 * function */
174 if(!lua_isnil(L, 4))
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++)
187 size_t blen;
188 const char *buf;
189 lua_rawgeti(L, 2, i);
190 buf = luaL_checklstring(L, -1, &blen);
191 button->mod |= xutil_key_mask_fromstr(buf, blen);
192 lua_pop(L, 1);
195 return 1;
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.
203 void
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);
209 lua_pushnil(L);
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);
228 return 1;
231 /** Button object.
232 * \param L The Lua VM state.
233 * \return The number of elements pushed on stack.
234 * \luastack
235 * \lfield press The function called when button press event is received.
236 * \lfield release The function called when button release event is received.
238 static int
239 luaA_button_index(lua_State *L)
241 if(luaA_usemetatable(L, 1, 2))
242 return 1;
244 size_t len;
245 button_t *button = luaL_checkudata(L, 1, "button");
246 const char *attr = luaL_checklstring(L, 2, &len);
248 switch(a_tokenize(attr, len))
250 case A_TK_PRESS:
251 if(button->press != LUA_REFNIL)
252 lua_rawgeti(L, LUA_REGISTRYINDEX, button->press);
253 else
254 lua_pushnil(L);
255 break;
256 case A_TK_RELEASE:
257 if(button->release != LUA_REFNIL)
258 lua_rawgeti(L, LUA_REGISTRYINDEX, button->release);
259 else
260 lua_pushnil(L);
261 break;
262 case A_TK_BUTTON:
263 /* works fine, but not *really* neat */
264 lua_pushnumber(L, button->button);
265 break;
266 default:
267 break;
270 return 1;
273 /** Button object.
274 * \param L The Lua VM state.
275 * \return The number of elements pushed on stack.
276 * \luastack
278 static int
279 luaA_button_newindex(lua_State *L)
281 if(luaA_usemetatable(L, 1, 2))
282 return 1;
284 size_t len;
285 button_t *button = luaL_checkudata(L, 1, "button");
286 const char *attr = luaL_checklstring(L, 2, &len);
288 switch(a_tokenize(attr, len))
290 case A_TK_PRESS:
291 luaA_registerfct(L, 3, &button->press);
292 break;
293 case A_TK_RELEASE:
294 luaA_registerfct(L, 3, &button->release);
295 break;
296 case A_TK_BUTTON:
297 button->button = xutil_button_fromint(luaL_checknumber(L, 3));
298 break;
299 default:
300 break;
303 return 0;
306 const struct luaL_reg awesome_button_methods[] =
308 { "__call", luaA_button_new },
309 { NULL, NULL }
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 },
317 { NULL, NULL }
320 /** Mouse library.
321 * \param L The Lua VM state.
322 * \return The number of elements pushed on stack.
323 * \luastack
324 * \lfield coords Mouse coordinates.
325 * \lfield screen Mouse screen number.
327 static int
328 luaA_mouse_index(lua_State *L)
330 size_t len;
331 const char *attr = luaL_checklstring(L, 2, &len);
332 int16_t mouse_x, mouse_y;
333 screen_t *screen;
335 switch(a_tokenize(attr, len))
337 case A_TK_SCREEN:
338 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, NULL))
339 return 0;
341 screen = screen_getbycoord(screen, mouse_x, mouse_y);
343 lua_pushnumber(L, screen->index + 1);
344 break;
345 default:
346 return 0;
349 return 1;
352 /** Newindex for mouse.
353 * \param L The Lua VM state.
354 * \return The number of elements pushed on stack.
356 static int
357 luaA_mouse_newindex(lua_State *L)
359 size_t len;
360 const char *attr = luaL_checklstring(L, 2, &len);
361 int x, y = 0;
362 xcb_window_t root;
363 int screen, phys_screen;
365 switch(a_tokenize(attr, len))
367 case A_TK_SCREEN:
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);
379 break;
380 default:
381 return 0;
384 return 0;
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)
396 lua_newtable(L);
397 lua_pushnumber(L, x);
398 lua_setfield(L, -2, "x");
399 lua_pushnumber(L, y);
400 lua_setfield(L, -2, "y");
402 lua_newtable(L);
404 int i = 1;
406 for(uint16_t maski = XCB_BUTTON_MASK_1; maski <= XCB_BUTTON_MASK_5; maski <<= 1)
408 if(mask & maski)
409 lua_pushboolean(L, true);
410 else
411 lua_pushboolean(L, false);
412 lua_rawseti(L, -2, i++);
414 lua_setfield(L, -2, "buttons");
415 return 1;
418 /** Get or set the mouse coords.
419 * \param L The Lua VM state.
420 * \return The number of elements pushed on stack.
421 * \luastack
422 * \lparam None or a table with x and y keys as mouse coordinates.
423 * \lreturn A table with mouse coordinates.
425 static int
426 luaA_mouse_coords(lua_State *L)
428 uint16_t mask;
429 int x, y;
430 int16_t mouse_x, mouse_y;
431 screen_t *screen;
433 if(lua_gettop(L) == 1)
435 xcb_window_t root;
437 luaA_checktable(L, 1);
439 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, &mask))
440 return 0;
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);
447 lua_pop(L, 1);
450 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, &mask))
451 return 0;
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.
459 * \luastack
460 * \lreturn A client or nil.
462 static int
463 luaA_mouse_object_under_pointer(lua_State *L)
465 screen_t *screen;
466 int16_t mouse_x, mouse_y;
467 xcb_window_t child;
469 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &child, NULL))
470 return 0;
472 wibox_t *wibox;
473 client_t *client;
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,
484 &x, &y);
486 if(widget)
488 widget_push(L, widget);
489 return 2;
491 return 1;
493 else if((client = client_getbywin(child)))
494 return client_push(globalconf.L, client);
496 return 0;
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 },
505 { NULL, NULL }
507 const struct luaL_reg awesome_mouse_meta[] =
509 { NULL, NULL }
512 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80