naughty: add opacity option
[awesome.git] / mouse.c
blob56a7fd18fcc6e369094780e03c4402350f51cda2
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 DO_LUA_NEW(static, button_t, button, "button", button_ref)
31 DO_LUA_GC(button_t, button, "button", button_unref)
32 DO_LUA_EQ(button_t, button, "button")
34 /** Delete a button.
35 * \param button The button to destroy.
37 void
38 button_delete(button_t **button)
40 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*button)->press);
41 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*button)->release);
42 p_delete(button);
45 /** Get the pointer position.
46 * \param window The window to get position on.
47 * \param x will be set to the Pointer-x-coordinate relative to window
48 * \param y will be set to the Pointer-y-coordinate relative to window
49 * \param child Will be set to the window under the pointer.
50 * \param mask will be set to the current buttons state
51 * \return true on success, false if an error occured
52 **/
53 bool
54 mouse_query_pointer(xcb_window_t window, int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
56 xcb_query_pointer_cookie_t query_ptr_c;
57 xcb_query_pointer_reply_t *query_ptr_r;
59 query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, window);
60 query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
62 if(!query_ptr_r || !query_ptr_r->same_screen)
63 return false;
65 *x = query_ptr_r->win_x;
66 *y = query_ptr_r->win_y;
67 if(mask)
68 *mask = query_ptr_r->mask;
69 if(child)
70 *child = query_ptr_r->child;
72 p_delete(&query_ptr_r);
74 return true;
77 /** Get the pointer position on the screen.
78 * \param screen This will be set to the screen number the mouse is on.
79 * \param x This will be set to the Pointer-x-coordinate relative to window.
80 * \param y This will be set to the Pointer-y-coordinate relative to window.
81 * \param child This will be set to the window under the pointer.
82 * \param mask This will be set to the current buttons state.
83 * \return True on success, false if an error occured.
85 static bool
86 mouse_query_pointer_root(int *s, int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
88 for(int screen = 0;
89 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
90 screen++)
92 xcb_window_t root = xutil_screen_get(globalconf.connection, screen)->root;
94 if(mouse_query_pointer(root, x, y, child, mask))
96 *s = screen;
97 return true;
100 return false;
103 /** Set the pointer position.
104 * \param window The destination window.
105 * \param x X-coordinate inside window.
106 * \param y Y-coordinate inside window.
108 static inline void
109 mouse_warp_pointer(xcb_window_t window, int x, int y)
111 xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
112 0, 0, 0, 0, x, y );
115 /** Create a new mouse button bindings.
116 * \param L The Lua VM state.
117 * \return The number of elements pushed on stack.
118 * \luastack
119 * \lparam A table with modifiers keys, or a button to clone.
120 * \lparam A mouse button number.
121 * \lparam A function to execute on click events.
122 * \lparam A function to execute on release events.
123 * \lreturn A mouse button binding.
125 static int
126 luaA_button_new(lua_State *L)
128 int i, len;
129 button_t *button, **orig;
131 if((orig = luaA_toudata(L, 2, "button")))
133 button_t *copy = p_new(button_t, 1);
134 copy->mod = (*orig)->mod;
135 copy->button = (*orig)->button;
136 if((*orig)->press != LUA_REFNIL)
138 lua_rawgeti(L, LUA_REGISTRYINDEX, (*orig)->press);
139 luaA_registerfct(L, -1, &copy->press);
141 else
142 copy->press = LUA_REFNIL;
143 if((*orig)->release != LUA_REFNIL)
145 lua_rawgeti(L, LUA_REGISTRYINDEX, (*orig)->release);
146 luaA_registerfct(L, -1, &copy->release);
148 else
149 copy->release = LUA_REFNIL;
150 return luaA_button_userdata_new(L, copy);
153 luaA_checktable(L, 2);
154 /* arg 3 is mouse button */
155 i = luaL_checknumber(L, 3);
156 /* arg 4 and 5 are callback functions */
157 if(!lua_isnil(L, 4))
158 luaA_checkfunction(L, 4);
159 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
160 luaA_checkfunction(L, 5);
162 button = p_new(button_t, 1);
163 button->button = xutil_button_fromint(i);
165 if(lua_isnil(L, 4))
166 button->press = LUA_REFNIL;
167 else
168 luaA_registerfct(L, 4, &button->press);
170 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
171 luaA_registerfct(L, 5, &button->release);
172 else
173 button->release = LUA_REFNIL;
175 len = lua_objlen(L, 2);
176 for(i = 1; i <= len; i++)
178 size_t blen;
179 const char *buf;
180 lua_rawgeti(L, 2, i);
181 buf = luaL_checklstring(L, -1, &blen);
182 button->mod |= xutil_key_mask_fromstr(buf, blen);
185 return luaA_button_userdata_new(L, button);
188 /** Set a button array with a Lua table.
189 * \param L The Lua VM state.
190 * \param idx The index of the Lua table.
191 * \param buttons The array button to fill.
193 void
194 luaA_button_array_set(lua_State *L, int idx, button_array_t *buttons)
196 button_t **b;
198 luaA_checktable(L, idx);
199 button_array_wipe(buttons);
200 button_array_init(buttons);
201 lua_pushnil(L);
202 while(lua_next(L, idx))
204 b = luaA_checkudata(L, -1, "button");
205 button_array_append(buttons, *b);
206 button_ref(b);
207 lua_pop(L, 1);
211 /** Push an array of button as an Lua table onto the stack.
212 * \param L The Lua VM state.
213 * \param buttons The button array to push.
214 * \return The number of elements pushed on stack.
217 luaA_button_array_get(lua_State *L, button_array_t *buttons)
219 lua_createtable(L, buttons->len, 0);
220 for(int i = 0; i < buttons->len; i++)
222 luaA_button_userdata_new(L, buttons->tab[i]);
223 lua_rawseti(L, -2, i + 1);
225 return 1;
228 /** Button object.
229 * \param L The Lua VM state.
230 * \return The number of elements pushed on stack.
231 * \luastack
232 * \lfield press The function called when button press event is received.
233 * \lfield release The function called when button release event is received.
235 static int
236 luaA_button_index(lua_State *L)
238 if(luaA_usemetatable(L, 1, 2))
239 return 1;
241 size_t len;
242 button_t **button = luaA_checkudata(L, 1, "button");
243 const char *attr = luaL_checklstring(L, 2, &len);
245 switch(a_tokenize(attr, len))
247 case A_TK_PRESS:
248 if((*button)->press != LUA_REFNIL)
249 lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->press);
250 else
251 lua_pushnil(L);
252 break;
253 case A_TK_RELEASE:
254 if((*button)->release != LUA_REFNIL)
255 lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->release);
256 else
257 lua_pushnil(L);
258 break;
259 case A_TK_BUTTON:
260 /* works fine, but not *really* neat */
261 lua_pushnumber(L, (*button)->button);
262 break;
263 default:
264 break;
267 return 1;
270 /** Button object.
271 * \param L The Lua VM state.
272 * \return The number of elements pushed on stack.
273 * \luastack
275 static int
276 luaA_button_newindex(lua_State *L)
278 if(luaA_usemetatable(L, 1, 2))
279 return 1;
281 size_t len;
282 button_t **button = luaA_checkudata(L, 1, "button");
283 const char *attr = luaL_checklstring(L, 2, &len);
285 switch(a_tokenize(attr, len))
287 case A_TK_PRESS:
288 luaA_registerfct(L, 3, &(*button)->press);
289 break;
290 case A_TK_RELEASE:
291 luaA_registerfct(L, 3, &(*button)->release);
292 break;
293 case A_TK_BUTTON:
294 (*button)->button = xutil_button_fromint(luaL_checknumber(L, 3));
295 break;
296 default:
297 break;
300 return 0;
303 const struct luaL_reg awesome_button_methods[] =
305 { "__call", luaA_button_new },
306 { NULL, NULL }
308 const struct luaL_reg awesome_button_meta[] =
310 { "__index", luaA_button_index },
311 { "__newindex", luaA_button_newindex },
312 { "__gc", luaA_button_gc },
313 { "__eq", luaA_button_eq },
314 { "__tostring", luaA_button_tostring },
315 { NULL, NULL }
318 /** Mouse library.
319 * \param L The Lua VM state.
320 * \return The number of elements pushed on stack.
321 * \luastack
322 * \lfield coords Mouse coordinates.
323 * \lfield screen Mouse screen number.
325 static int
326 luaA_mouse_index(lua_State *L)
328 size_t len;
329 const char *attr = luaL_checklstring(L, 2, &len);
330 int16_t mouse_x, mouse_y;
331 int screen, i;
333 switch(a_tokenize(attr, len))
335 case A_TK_SCREEN:
336 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, NULL))
337 return 0;
339 i = screen_getbycoord(screen, mouse_x, mouse_y);
341 lua_pushnumber(L, i + 1);
342 break;
343 default:
344 return 0;
347 return 1;
350 /** Newindex for mouse.
351 * \param L The Lua VM state.
352 * \return The number of elements pushed on stack.
354 static int
355 luaA_mouse_newindex(lua_State *L)
357 size_t len;
358 const char *attr = luaL_checklstring(L, 2, &len);
359 int x, y = 0;
360 xcb_window_t root;
361 int screen, phys_screen;
363 switch(a_tokenize(attr, len))
365 case A_TK_SCREEN:
366 screen = luaL_checknumber(L, 3) - 1;
367 luaA_checkscreen(screen);
369 /* we need the physical one to get the root window */
370 phys_screen = screen_virttophys(screen);
371 root = xutil_screen_get(globalconf.connection, phys_screen)->root;
373 x = globalconf.screens[screen].geometry.x;
374 y = globalconf.screens[screen].geometry.y;
376 mouse_warp_pointer(root, x, y);
377 break;
378 default:
379 return 0;
382 return 0;
385 /** Push a table with mouse status.
386 * \param L The Lua VM state.
387 * \param x The x coordinate.
388 * \param y The y coordinate.
389 * \param mask The button mask.
392 luaA_mouse_pushstatus(lua_State *L, int x, int y, uint16_t mask)
394 lua_newtable(L);
395 lua_pushnumber(L, x);
396 lua_setfield(L, -2, "x");
397 lua_pushnumber(L, y);
398 lua_setfield(L, -2, "y");
400 lua_newtable(L);
402 int i = 1;
404 for(uint16_t maski = XCB_BUTTON_MASK_1; maski <= XCB_BUTTON_MASK_5; maski <<= 1)
406 if(mask & maski)
407 lua_pushboolean(L, true);
408 else
409 lua_pushboolean(L, false);
410 lua_rawseti(L, -2, i++);
412 lua_setfield(L, -2, "buttons");
413 return 1;
416 /** Get or set the mouse coords.
417 * \param L The Lua VM state.
418 * \return The number of elements pushed on stack.
419 * \luastack
420 * \lparam None or a table with x and y keys as mouse coordinates.
421 * \lreturn A table with mouse coordinates.
423 static int
424 luaA_mouse_coords(lua_State *L)
426 uint16_t mask;
427 int screen, x, y;
428 int16_t mouse_x, mouse_y;
430 if(lua_gettop(L) == 1)
432 xcb_window_t root;
434 luaA_checktable(L, 1);
436 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, &mask))
437 return 0;
439 x = luaA_getopt_number(L, 1, "x", mouse_x);
440 y = luaA_getopt_number(L, 1, "y", mouse_y);
442 root = xutil_screen_get(globalconf.connection, screen)->root;
443 mouse_warp_pointer(root, x, y);
444 lua_pop(L, 1);
447 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, &mask))
448 return 0;
450 return luaA_mouse_pushstatus(L, mouse_x, mouse_y, mask);
453 /** Get the client which is under the pointer.
454 * \param L The Lua VM state.
455 * \return The number of elements pushed on stack.
456 * \luastack
457 * \lreturn A client or nil.
459 static int
460 luaA_mouse_object_under_pointer(lua_State *L)
462 int screen;
463 int16_t mouse_x, mouse_y;
464 xcb_window_t child;
466 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &child, NULL))
467 return 0;
469 wibox_t *wibox;
470 client_t *client;
471 if((wibox = wibox_getbywin(child)))
473 luaA_wibox_userdata_new(L, wibox);
475 int16_t x = mouse_x - wibox->sw.geometry.x;
476 int16_t y = mouse_y - wibox->sw.geometry.y;
478 widget_t *widget = widget_getbycoords(wibox->position, &wibox->widgets,
479 wibox->sw.geometry.width,
480 wibox->sw.geometry.height,
481 &x, &y);
483 if(widget)
485 luaA_widget_userdata_new(L, widget);
486 return 2;
488 return 1;
490 else if((client = client_getbywin(child)))
491 return luaA_client_userdata_new(L, client);
493 return 0;
496 const struct luaL_reg awesome_mouse_methods[] =
498 { "__index", luaA_mouse_index },
499 { "__newindex", luaA_mouse_newindex },
500 { "coords", luaA_mouse_coords },
501 { "object_under_pointer", luaA_mouse_object_under_pointer },
502 { NULL, NULL }
504 const struct luaL_reg awesome_mouse_meta[] =
506 { NULL, NULL }
509 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80