Wibox: Rename to drawin
[awesome.git] / mouse.c
bloba1779aa15ad4b43ec4b26fe49750174d93754b75
1 /*
2 * mouse.c - mouse managing
4 * Copyright © 2007-2009 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 "mouse.h"
23 #include "screen.h"
24 #include "objects/client.h"
25 #include "globalconf.h"
26 #include "objects/drawin.h"
27 #include "luaa.h"
28 #include "common/xutil.h"
30 /** Get the pointer position.
31 * \param window The window to get position on.
32 * \param x will be set to the Pointer-x-coordinate relative to window
33 * \param y will be set to the Pointer-y-coordinate relative to window
34 * \param child Will be set to the window under the pointer.
35 * \param mask will be set to the current buttons state
36 * \return true on success, false if an error occurred
37 **/
38 bool
39 mouse_query_pointer(xcb_window_t window, int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
41 xcb_query_pointer_cookie_t query_ptr_c;
42 xcb_query_pointer_reply_t *query_ptr_r;
44 query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, window);
45 query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
47 if(!query_ptr_r || !query_ptr_r->same_screen)
48 return false;
50 *x = query_ptr_r->win_x;
51 *y = query_ptr_r->win_y;
52 if(mask)
53 *mask = query_ptr_r->mask;
54 if(child)
55 *child = query_ptr_r->child;
57 p_delete(&query_ptr_r);
59 return true;
62 /** Get the pointer position on the screen.
63 * \param x This will be set to the Pointer-x-coordinate relative to window.
64 * \param y This will be set to the Pointer-y-coordinate relative to window.
65 * \param child This will be set to the window under the pointer.
66 * \param mask This will be set to the current buttons state.
67 * \return True on success, false if an error occurred.
69 static bool
70 mouse_query_pointer_root(int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
72 xcb_window_t root = globalconf.screen->root;
74 if(mouse_query_pointer(root, x, y, child, mask))
76 return true;
78 return false;
81 /** Set the pointer position.
82 * \param window The destination window.
83 * \param x X-coordinate inside window.
84 * \param y Y-coordinate inside window.
86 static inline void
87 mouse_warp_pointer(xcb_window_t window, int x, int y)
89 xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
90 0, 0, 0, 0, x, y );
93 /** Mouse library.
94 * \param L The Lua VM state.
95 * \return The number of elements pushed on stack.
96 * \luastack
97 * \lfield coords Mouse coordinates.
98 * \lfield screen Mouse screen number.
100 static int
101 luaA_mouse_index(lua_State *L)
103 const char *attr = luaL_checkstring(L, 2);
104 int16_t mouse_x, mouse_y;
105 screen_t *screen;
107 if(a_strcmp(attr, "screen") == 0)
109 if(!mouse_query_pointer_root(&mouse_x, &mouse_y, NULL, NULL))
110 return 0;
112 screen = screen_getbycoord(mouse_x, mouse_y);
114 lua_pushnumber(L, screen_array_indexof(&globalconf.screens, screen) + 1);
115 } else
116 return 0;
118 return 1;
121 /** Newindex for mouse.
122 * \param L The Lua VM state.
123 * \return The number of elements pushed on stack.
125 static int
126 luaA_mouse_newindex(lua_State *L)
128 const char *attr = luaL_checkstring(L, 2);
129 int x, y = 0;
130 int screen;
132 if(a_strcmp(attr, "screen") == 0)
134 screen = luaL_checknumber(L, 3) - 1;
135 luaA_checkscreen(screen);
137 x = globalconf.screens.tab[screen].geometry.x;
138 y = globalconf.screens.tab[screen].geometry.y;
140 mouse_warp_pointer(globalconf.screen->root, x, y);
143 return 0;
146 /** Push a table with mouse status.
147 * \param L The Lua VM state.
148 * \param x The x coordinate.
149 * \param y The y coordinate.
150 * \param mask The button mask.
153 luaA_mouse_pushstatus(lua_State *L, int x, int y, uint16_t mask)
155 lua_createtable(L, 0, 2);
156 lua_pushnumber(L, x);
157 lua_setfield(L, -2, "x");
158 lua_pushnumber(L, y);
159 lua_setfield(L, -2, "y");
161 lua_createtable(L, 5, 0);
163 int i = 1;
165 for(uint16_t maski = XCB_BUTTON_MASK_1; maski <= XCB_BUTTON_MASK_5; maski <<= 1)
167 if(mask & maski)
168 lua_pushboolean(L, true);
169 else
170 lua_pushboolean(L, false);
171 lua_rawseti(L, -2, i++);
173 lua_setfield(L, -2, "buttons");
174 return 1;
177 /** Get or set the mouse coords.
178 * \param L The Lua VM state.
179 * \return The number of elements pushed on stack.
181 static int
182 luaA_mouse_coords(lua_State *L)
184 uint16_t mask;
185 int x, y;
186 int16_t mouse_x, mouse_y;
188 if(lua_gettop(L) >= 1)
190 luaA_checktable(L, 1);
191 bool ignore_enter_notify = (lua_gettop(L) == 2 && luaA_checkboolean(L, 2));
193 if(!mouse_query_pointer_root(&mouse_x, &mouse_y, NULL, &mask))
194 return 0;
196 x = luaA_getopt_number(L, 1, "x", mouse_x);
197 y = luaA_getopt_number(L, 1, "y", mouse_y);
199 if(ignore_enter_notify)
200 client_ignore_enterleave_events();
202 mouse_warp_pointer(globalconf.screen->root, x, y);
204 if(ignore_enter_notify)
205 client_restore_enterleave_events();
207 lua_pop(L, 1);
210 if(!mouse_query_pointer_root(&mouse_x, &mouse_y, NULL, &mask))
211 return 0;
213 return luaA_mouse_pushstatus(L, mouse_x, mouse_y, mask);
216 /** Get the client which is under the pointer.
217 * \param L The Lua VM state.
218 * \return The number of elements pushed on stack.
219 * \luastack
220 * \lreturn A client or nil.
222 static int
223 luaA_mouse_object_under_pointer(lua_State *L)
225 int16_t mouse_x, mouse_y;
226 xcb_window_t child;
228 if(!mouse_query_pointer_root(&mouse_x, &mouse_y, &child, NULL))
229 return 0;
231 drawin_t *drawin;
232 client_t *client;
233 if((drawin = drawin_getbywin(child)))
235 luaA_object_push(L, drawin);
237 return 1;
239 else if((client = client_getbyframewin(child)))
240 return luaA_object_push(globalconf.L, client);
242 return 0;
245 const struct luaL_reg awesome_mouse_methods[] =
247 { "__index", luaA_mouse_index },
248 { "__newindex", luaA_mouse_newindex },
249 { "coords", luaA_mouse_coords },
250 { "object_under_pointer", luaA_mouse_object_under_pointer },
251 { NULL, NULL }
253 const struct luaL_reg awesome_mouse_meta[] =
255 { NULL, NULL }
258 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80