completion: append / to directories
[awesome.git] / mouse.c
blob636577947dee827491919a2e08c72a8b703df064
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 "common/tokenize.h"
25 #include "screen.h"
26 #include "tag.h"
27 #include "wibox.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")
36 /** Delete a button.
37 * \param button The button to destroy.
39 void
40 button_delete(button_t **button)
42 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*button)->press);
43 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*button)->release);
44 p_delete(button);
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
54 **/
55 bool
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)
65 return false;
67 *x = query_ptr_r->win_x;
68 *y = query_ptr_r->win_y;
69 if (mask)
70 *mask = query_ptr_r->mask;
71 if(child)
72 *child = query_ptr_r->child;
74 p_delete(&query_ptr_r);
76 return true;
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.
87 static bool
88 mouse_query_pointer_root(int *s, int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
90 for(int screen = 0;
91 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
92 screen++)
94 xcb_window_t root = xutil_screen_get(globalconf.connection, screen)->root;
96 if(mouse_query_pointer(root, x, y, child, mask))
98 *s = screen;
99 return true;
102 return false;
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.
110 static inline void
111 mouse_warp_pointer(xcb_window_t window, int x, int y)
113 xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
114 0, 0, 0, 0, x, y );
117 /** Create a new mouse button bindings.
118 * \param L The Lua VM state.
119 * \return The number of elements pushed on stack.
120 * \luastack
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.
127 static int
128 luaA_button_new(lua_State *L)
130 int i, len;
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, &copy->press);
143 else
144 copy->press = LUA_REFNIL;
145 if((*orig)->release != LUA_REFNIL)
147 lua_rawgeti(L, LUA_REGISTRYINDEX, (*orig)->release);
148 luaA_registerfct(L, -1, &copy->release);
150 else
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 */
159 if(!lua_isnil(L, 4))
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);
167 if(lua_isnil(L, 4))
168 button->press = LUA_REFNIL;
169 else
170 luaA_registerfct(L, 4, &button->press);
172 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
173 luaA_registerfct(L, 5, &button->release);
174 else
175 button->release = LUA_REFNIL;
177 len = lua_objlen(L, 2);
178 for(i = 1; i <= len; i++)
180 size_t blen;
181 const char *buf;
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.
195 void
196 luaA_button_array_set(lua_State *L, int idx, button_array_t *buttons)
198 button_t **b;
200 luaA_checktable(L, idx);
201 button_array_wipe(buttons);
202 button_array_init(buttons);
203 lua_pushnil(L);
204 while(lua_next(L, idx))
206 b = luaA_checkudata(L, -1, "button");
207 button_array_append(buttons, *b);
208 button_ref(b);
209 lua_pop(L, 1);
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)
221 luaA_otable_new(L);
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);
227 return 1;
230 /** Button object.
231 * \param L The Lua VM state.
232 * \return The number of elements pushed on stack.
233 * \luastack
234 * \lfield press The function called when button press event is received.
235 * \lfield release The function called when button release event is received.
237 static int
238 luaA_button_index(lua_State *L)
240 if(luaA_usemetatable(L, 1, 2))
241 return 1;
243 size_t len;
244 button_t **button = luaA_checkudata(L, 1, "button");
245 const char *attr = luaL_checklstring(L, 2, &len);
247 switch(a_tokenize(attr, len))
249 case A_TK_PRESS:
250 if((*button)->press != LUA_REFNIL)
251 lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->press);
252 else
253 lua_pushnil(L);
254 break;
255 case A_TK_RELEASE:
256 if((*button)->release != LUA_REFNIL)
257 lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->release);
258 else
259 lua_pushnil(L);
260 break;
261 case A_TK_BUTTON:
262 /* works fine, but not *really* neat */
263 lua_pushnumber(L, (*button)->button);
264 break;
265 default:
266 break;
269 return 1;
272 /** Button object.
273 * \param L The Lua VM state.
274 * \return The number of elements pushed on stack.
275 * \luastack
277 static int
278 luaA_button_newindex(lua_State *L)
280 if(luaA_usemetatable(L, 1, 2))
281 return 1;
283 size_t len;
284 button_t **button = luaA_checkudata(L, 1, "button");
285 const char *attr = luaL_checklstring(L, 2, &len);
287 switch(a_tokenize(attr, len))
289 case A_TK_PRESS:
290 luaA_registerfct(L, 3, &(*button)->press);
291 break;
292 case A_TK_RELEASE:
293 luaA_registerfct(L, 3, &(*button)->release);
294 break;
295 case A_TK_BUTTON:
296 (*button)->button = xutil_button_fromint(luaL_checknumber(L, 3));
297 break;
298 default:
299 break;
302 return 0;
305 const struct luaL_reg awesome_button_methods[] =
307 { "__call", luaA_button_new },
308 { NULL, NULL }
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 },
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 int screen, i;
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 i = screen_getbycoord(screen, mouse_x, mouse_y);
343 lua_pushnumber(L, i + 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[screen].geometry.x;
376 y = globalconf.screens[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 screen, x, y;
430 int16_t mouse_x, mouse_y;
432 if(lua_gettop(L) == 1)
434 xcb_window_t root;
436 luaA_checktable(L, 1);
438 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, &mask))
439 return 0;
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);
446 lua_pop(L, 1);
449 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, &mask))
450 return 0;
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.
458 * \luastack
459 * \lreturn A client or nil.
461 static int
462 luaA_mouse_object_under_pointer(lua_State *L)
464 int screen;
465 int16_t mouse_x, mouse_y;
466 xcb_window_t child;
468 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &child, NULL))
469 return 0;
471 wibox_t *wibox;
472 client_t *client;
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,
483 &x, &y);
485 if(widget)
487 luaA_widget_userdata_new(L, widget);
488 return 2;
490 return 1;
492 else if((client = client_getbywin(child)))
493 return luaA_client_userdata_new(L, client);
495 return 0;
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 },
504 { NULL, NULL }
506 const struct luaL_reg awesome_mouse_meta[] =
508 { NULL, NULL }
511 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80