awful.widget.taglist: remove needless taglist_squares conditions
[awesome.git] / mouse.c
blob262edcffc0928943f91f56238c1d47ea4e391c94
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_TOSTRING(button_t, button, "button")
31 LUA_OBJECT_FUNCS(button_t, button, "button")
33 void
34 button_unref_simplified(button_t **b)
36 button_unref(globalconf.L, *b);
39 /** Collect a button.
40 * \param L The Lua VM state.
41 * \return 0.
43 static int
44 luaA_button_gc(lua_State *L)
46 button_t *button = luaL_checkudata(L, 1, "button");
47 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, button->press);
48 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, button->release);
49 return 0;
52 /** Get the pointer position.
53 * \param window The window to get position on.
54 * \param x will be set to the Pointer-x-coordinate relative to window
55 * \param y will be set to the Pointer-y-coordinate relative to window
56 * \param child Will be set to the window under the pointer.
57 * \param mask will be set to the current buttons state
58 * \return true on success, false if an error occured
59 **/
60 bool
61 mouse_query_pointer(xcb_window_t window, int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
63 xcb_query_pointer_cookie_t query_ptr_c;
64 xcb_query_pointer_reply_t *query_ptr_r;
66 query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, window);
67 query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
69 if(!query_ptr_r || !query_ptr_r->same_screen)
70 return false;
72 *x = query_ptr_r->win_x;
73 *y = query_ptr_r->win_y;
74 if(mask)
75 *mask = query_ptr_r->mask;
76 if(child)
77 *child = query_ptr_r->child;
79 p_delete(&query_ptr_r);
81 return true;
84 /** Get the pointer position on the screen.
85 * \param screen This will be set to the screen number the mouse is on.
86 * \param x This will be set to the Pointer-x-coordinate relative to window.
87 * \param y This will be set to the Pointer-y-coordinate relative to window.
88 * \param child This will be set to the window under the pointer.
89 * \param mask This will be set to the current buttons state.
90 * \return True on success, false if an error occured.
92 static bool
93 mouse_query_pointer_root(int *s, int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
95 for(int screen = 0;
96 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
97 screen++)
99 xcb_window_t root = xutil_screen_get(globalconf.connection, screen)->root;
101 if(mouse_query_pointer(root, x, y, child, mask))
103 *s = screen;
104 return true;
107 return false;
110 /** Set the pointer position.
111 * \param window The destination window.
112 * \param x X-coordinate inside window.
113 * \param y Y-coordinate inside window.
115 static inline void
116 mouse_warp_pointer(xcb_window_t window, int x, int y)
118 xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
119 0, 0, 0, 0, x, y );
122 /** Create a new mouse button bindings.
123 * \param L The Lua VM state.
124 * \return The number of elements pushed on stack.
125 * \luastack
126 * \lparam A table with modifiers keys, or a button to clone.
127 * \lparam A mouse button number.
128 * \lparam A function to execute on click events.
129 * \lparam A function to execute on release events.
130 * \lreturn A mouse button binding.
132 static int
133 luaA_button_new(lua_State *L)
135 int i, len;
136 button_t *button, *orig;
137 luaA_ref press = LUA_REFNIL, release = LUA_REFNIL;
139 if((orig = luaA_toudata(L, 2, "button")))
141 button_t *copy = button_new(L);
142 copy->mod = orig->mod;
143 copy->button = orig->button;
144 if(orig->press != LUA_REFNIL)
146 lua_rawgeti(L, LUA_REGISTRYINDEX, orig->press);
147 luaA_registerfct(L, -1, &copy->press);
148 lua_pop(L, 1);
150 else
151 copy->press = LUA_REFNIL;
152 if(orig->release != LUA_REFNIL)
154 lua_rawgeti(L, LUA_REGISTRYINDEX, orig->release);
155 luaA_registerfct(L, -1, &copy->release);
156 lua_pop(L, 1);
158 else
159 copy->release = LUA_REFNIL;
160 return 1;
163 luaA_checktable(L, 2);
164 /* arg 3 is mouse button */
165 i = luaL_checknumber(L, 3);
167 /* arg 4 and 5 are callback functions, check they are functions... */
168 if(!lua_isnil(L, 4))
169 luaA_checkfunction(L, 4);
170 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
171 luaA_checkfunction(L, 5);
173 /* ... then register (can't register before since 5 maybe not nil but not a
174 * function */
175 if(!lua_isnil(L, 4))
176 luaA_registerfct(L, 4, &press);
177 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
178 luaA_registerfct(L, 5, &release);
180 button = button_new(L);
181 button->press = press;
182 button->release = release;
183 button->button = xutil_button_fromint(i);
185 len = lua_objlen(L, 2);
186 for(i = 1; i <= len; i++)
188 size_t blen;
189 const char *buf;
190 lua_rawgeti(L, 2, i);
191 buf = luaL_checklstring(L, -1, &blen);
192 button->mod |= xutil_key_mask_fromstr(buf, blen);
193 lua_pop(L, 1);
196 return 1;
199 /** Set a button array with a Lua table.
200 * \param L The Lua VM state.
201 * \param idx The index of the Lua table.
202 * \param buttons The array button to fill.
204 void
205 luaA_button_array_set(lua_State *L, int idx, button_array_t *buttons)
207 luaA_checktable(L, idx);
208 button_array_wipe(buttons);
209 button_array_init(buttons);
210 lua_pushnil(L);
211 while(lua_next(L, idx))
212 button_array_append(buttons, button_ref(L));
215 /** Push an array of button as an Lua table onto the stack.
216 * \param L The Lua VM state.
217 * \param buttons The button array to push.
218 * \return The number of elements pushed on stack.
221 luaA_button_array_get(lua_State *L, button_array_t *buttons)
223 lua_createtable(L, buttons->len, 0);
224 for(int i = 0; i < buttons->len; i++)
226 button_push(L, buttons->tab[i]);
227 lua_rawseti(L, -2, i + 1);
229 return 1;
232 /** Button object.
233 * \param L The Lua VM state.
234 * \return The number of elements pushed on stack.
235 * \luastack
236 * \lfield press The function called when button press event is received.
237 * \lfield release The function called when button release event is received.
239 static int
240 luaA_button_index(lua_State *L)
242 if(luaA_usemetatable(L, 1, 2))
243 return 1;
245 size_t len;
246 button_t *button = luaL_checkudata(L, 1, "button");
247 const char *attr = luaL_checklstring(L, 2, &len);
249 switch(a_tokenize(attr, len))
251 case A_TK_PRESS:
252 if(button->press != LUA_REFNIL)
253 lua_rawgeti(L, LUA_REGISTRYINDEX, button->press);
254 else
255 lua_pushnil(L);
256 break;
257 case A_TK_RELEASE:
258 if(button->release != LUA_REFNIL)
259 lua_rawgeti(L, LUA_REGISTRYINDEX, button->release);
260 else
261 lua_pushnil(L);
262 break;
263 case A_TK_BUTTON:
264 /* works fine, but not *really* neat */
265 lua_pushnumber(L, button->button);
266 break;
267 default:
268 break;
271 return 1;
274 /** Button object.
275 * \param L The Lua VM state.
276 * \return The number of elements pushed on stack.
277 * \luastack
279 static int
280 luaA_button_newindex(lua_State *L)
282 if(luaA_usemetatable(L, 1, 2))
283 return 1;
285 size_t len;
286 button_t *button = luaL_checkudata(L, 1, "button");
287 const char *attr = luaL_checklstring(L, 2, &len);
289 switch(a_tokenize(attr, len))
291 case A_TK_PRESS:
292 luaA_registerfct(L, 3, &button->press);
293 break;
294 case A_TK_RELEASE:
295 luaA_registerfct(L, 3, &button->release);
296 break;
297 case A_TK_BUTTON:
298 button->button = xutil_button_fromint(luaL_checknumber(L, 3));
299 break;
300 default:
301 break;
304 return 0;
307 const struct luaL_reg awesome_button_methods[] =
309 { "__call", luaA_button_new },
310 { NULL, NULL }
312 const struct luaL_reg awesome_button_meta[] =
314 { "__index", luaA_button_index },
315 { "__newindex", luaA_button_newindex },
316 { "__gc", luaA_button_gc },
317 { "__tostring", luaA_button_tostring },
318 { NULL, NULL }
321 /** Mouse library.
322 * \param L The Lua VM state.
323 * \return The number of elements pushed on stack.
324 * \luastack
325 * \lfield coords Mouse coordinates.
326 * \lfield screen Mouse screen number.
328 static int
329 luaA_mouse_index(lua_State *L)
331 size_t len;
332 const char *attr = luaL_checklstring(L, 2, &len);
333 int16_t mouse_x, mouse_y;
334 int screen, i;
336 switch(a_tokenize(attr, len))
338 case A_TK_SCREEN:
339 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, NULL))
340 return 0;
342 i = screen_getbycoord(screen, mouse_x, mouse_y);
344 lua_pushnumber(L, i + 1);
345 break;
346 default:
347 return 0;
350 return 1;
353 /** Newindex for mouse.
354 * \param L The Lua VM state.
355 * \return The number of elements pushed on stack.
357 static int
358 luaA_mouse_newindex(lua_State *L)
360 size_t len;
361 const char *attr = luaL_checklstring(L, 2, &len);
362 int x, y = 0;
363 xcb_window_t root;
364 int screen, phys_screen;
366 switch(a_tokenize(attr, len))
368 case A_TK_SCREEN:
369 screen = luaL_checknumber(L, 3) - 1;
370 luaA_checkscreen(screen);
372 /* we need the physical one to get the root window */
373 phys_screen = screen_virttophys(screen);
374 root = xutil_screen_get(globalconf.connection, phys_screen)->root;
376 x = globalconf.screens[screen].geometry.x;
377 y = globalconf.screens[screen].geometry.y;
379 mouse_warp_pointer(root, x, y);
380 break;
381 default:
382 return 0;
385 return 0;
388 /** Push a table with mouse status.
389 * \param L The Lua VM state.
390 * \param x The x coordinate.
391 * \param y The y coordinate.
392 * \param mask The button mask.
395 luaA_mouse_pushstatus(lua_State *L, int x, int y, uint16_t mask)
397 lua_newtable(L);
398 lua_pushnumber(L, x);
399 lua_setfield(L, -2, "x");
400 lua_pushnumber(L, y);
401 lua_setfield(L, -2, "y");
403 lua_newtable(L);
405 int i = 1;
407 for(uint16_t maski = XCB_BUTTON_MASK_1; maski <= XCB_BUTTON_MASK_5; maski <<= 1)
409 if(mask & maski)
410 lua_pushboolean(L, true);
411 else
412 lua_pushboolean(L, false);
413 lua_rawseti(L, -2, i++);
415 lua_setfield(L, -2, "buttons");
416 return 1;
419 /** Get or set the mouse coords.
420 * \param L The Lua VM state.
421 * \return The number of elements pushed on stack.
422 * \luastack
423 * \lparam None or a table with x and y keys as mouse coordinates.
424 * \lreturn A table with mouse coordinates.
426 static int
427 luaA_mouse_coords(lua_State *L)
429 uint16_t mask;
430 int screen, x, y;
431 int16_t mouse_x, mouse_y;
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)->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 int 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