Revert "Support more than 5 mouse buttons" (FS#1082)
[awesome.git] / mouse.c
blob8d3785345ee2734f13dcbc67110914a52543f2af
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;
53 if(mask)
54 *mask = query_ptr_r->mask;
55 if(child)
56 *child = query_ptr_r->child;
58 p_delete(&query_ptr_r);
60 return true;
63 /** Get the pointer position on the screen.
64 * \param x This will be set to the Pointer-x-coordinate relative to window.
65 * \param y This will be set to the Pointer-y-coordinate relative to window.
66 * \param child This will be set to the window under the pointer.
67 * \param mask This will be set to the current buttons state.
68 * \return True on success, false if an error occurred.
70 static bool
71 mouse_query_pointer_root(int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
73 xcb_window_t root = globalconf.screen->root;
75 return mouse_query_pointer(root, x, y, child, mask);
78 /** Set the pointer position.
79 * \param window The destination window.
80 * \param x X-coordinate inside window.
81 * \param y Y-coordinate inside window.
83 static inline void
84 mouse_warp_pointer(xcb_window_t window, int x, int y)
86 xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
87 0, 0, 0, 0, x, y);
90 /** Mouse library.
91 * \param L The Lua VM state.
92 * \return The number of elements pushed on stack.
93 * \luastack
94 * \lfield coords Mouse coordinates.
95 * \lfield screen Mouse screen number.
97 static int
98 luaA_mouse_index(lua_State *L)
100 const char *attr = luaL_checkstring(L, 2);
101 int16_t mouse_x, mouse_y;
102 screen_t *screen;
104 /* attr is not "screen"?! */
105 if (A_STRNEQ(attr, "screen"))
106 return 0;
108 if (!mouse_query_pointer_root(&mouse_x, &mouse_y, NULL, NULL))
109 return 0;
111 screen = screen_getbycoord(mouse_x, mouse_y);
112 lua_pushnumber(L, screen_array_indexof(&globalconf.screens, screen) + 1);
113 return 1;
116 /** Newindex for mouse.
117 * \param L The Lua VM state.
118 * \return The number of elements pushed on stack.
120 static int
121 luaA_mouse_newindex(lua_State *L)
123 const char *attr = luaL_checkstring(L, 2);
124 int x, y = 0;
125 int screen;
127 if (A_STRNEQ(attr, "screen"))
128 return 0;
130 screen = luaL_checknumber(L, 3) - 1;
131 luaA_checkscreen(screen);
133 x = globalconf.screens.tab[screen].geometry.x;
134 y = globalconf.screens.tab[screen].geometry.y;
136 mouse_warp_pointer(globalconf.screen->root, x, y);
137 return 0;
140 /** Push a table with mouse status.
141 * \param L The Lua VM state.
142 * \param x The x coordinate.
143 * \param y The y coordinate.
144 * \param mask The button mask.
147 luaA_mouse_pushstatus(lua_State *L, int x, int y, uint16_t mask)
149 lua_createtable(L, 0, 2);
150 lua_pushnumber(L, x);
151 lua_setfield(L, -2, "x");
152 lua_pushnumber(L, y);
153 lua_setfield(L, -2, "y");
155 lua_createtable(L, 5, 0);
157 int i = 1;
159 for(uint16_t maski = XCB_BUTTON_MASK_1; maski <= XCB_BUTTON_MASK_5; maski <<= 1)
161 if(mask & maski)
162 lua_pushboolean(L, true);
163 else
164 lua_pushboolean(L, false);
165 lua_rawseti(L, -2, i++);
167 lua_setfield(L, -2, "buttons");
168 return 1;
171 /** Get or set the mouse coords.
172 * \param L The Lua VM state.
173 * \return The number of elements pushed on stack.
175 static int
176 luaA_mouse_coords(lua_State *L)
178 uint16_t mask;
179 int x, y;
180 int16_t mouse_x, mouse_y;
182 if(lua_gettop(L) >= 1)
184 luaA_checktable(L, 1);
185 bool ignore_enter_notify = (lua_gettop(L) == 2 && luaA_checkboolean(L, 2));
187 if(!mouse_query_pointer_root(&mouse_x, &mouse_y, NULL, &mask))
188 return 0;
190 x = luaA_getopt_number(L, 1, "x", mouse_x);
191 y = luaA_getopt_number(L, 1, "y", mouse_y);
193 if(ignore_enter_notify)
194 client_ignore_enterleave_events();
196 mouse_warp_pointer(globalconf.screen->root, x, y);
198 if(ignore_enter_notify)
199 client_restore_enterleave_events();
201 lua_pop(L, 1);
204 if(!mouse_query_pointer_root(&mouse_x, &mouse_y, NULL, &mask))
205 return 0;
207 return luaA_mouse_pushstatus(L, mouse_x, mouse_y, mask);
210 /** Get the client which is under the pointer.
211 * \param L The Lua VM state.
212 * \return The number of elements pushed on stack.
213 * \luastack
214 * \lreturn A client or nil.
216 static int
217 luaA_mouse_object_under_pointer(lua_State *L)
219 int16_t mouse_x, mouse_y;
220 xcb_window_t child;
222 if(!mouse_query_pointer_root(&mouse_x, &mouse_y, &child, NULL))
223 return 0;
225 drawin_t *drawin;
226 client_t *client;
228 if((drawin = drawin_getbywin(child)))
229 return luaA_object_push(L, drawin);
231 if((client = client_getbyframewin(child)))
232 return luaA_object_push(globalconf.L, client);
234 return 0;
237 const struct luaL_Reg awesome_mouse_methods[] =
239 { "__index", luaA_mouse_index },
240 { "__newindex", luaA_mouse_newindex },
241 { "coords", luaA_mouse_coords },
242 { "object_under_pointer", luaA_mouse_object_under_pointer },
243 { NULL, NULL }
245 const struct luaL_Reg awesome_mouse_meta[] =
247 { NULL, NULL }
250 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80