change codename
[awesome.git] / mouse.c
blobdb802d9840db1a2d9b5e2443e6609e7d1bec4beb
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 * \return true on success, false if an error occurred
36 **/
37 static bool
38 mouse_query_pointer(xcb_window_t window, int16_t *x, int16_t *y, xcb_window_t *child)
40 xcb_query_pointer_cookie_t query_ptr_c;
41 xcb_query_pointer_reply_t *query_ptr_r;
43 query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, window);
44 query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
46 if(!query_ptr_r || !query_ptr_r->same_screen)
47 return false;
49 *x = query_ptr_r->win_x;
50 *y = query_ptr_r->win_y;
52 if(child)
53 *child = query_ptr_r->child;
55 p_delete(&query_ptr_r);
57 return true;
60 /** Get the pointer position on the screen.
61 * \param x This will be set to the Pointer-x-coordinate relative to window.
62 * \param y This will be set to the Pointer-y-coordinate relative to window.
63 * \param child This will be set to the window under the pointer.
64 * \param mask This will be set to the current buttons state.
65 * \return True on success, false if an error occurred.
67 static inline bool
68 mouse_query_pointer_root(int16_t *x, int16_t *y, xcb_window_t *child)
70 return mouse_query_pointer(globalconf.screen->root, x, y, child);
73 /** Set the pointer position.
74 * \param window The destination window.
75 * \param x X-coordinate inside window.
76 * \param y Y-coordinate inside window.
78 static inline void
79 mouse_warp_pointer(xcb_window_t window, int x, int y)
81 xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
82 0, 0, 0, 0, x, y);
85 /** Mouse library.
86 * \param L The Lua VM state.
87 * \return The number of elements pushed on stack.
88 * \luastack
89 * \lfield coords Mouse coordinates.
90 * \lfield screen Mouse screen number.
92 static int
93 luaA_mouse_index(lua_State *L)
95 const char *attr = luaL_checkstring(L, 2);
96 int16_t mouse_x, mouse_y;
97 screen_t *screen;
99 /* attr is not "screen"?! */
100 if (A_STRNEQ(attr, "screen"))
101 return 0;
103 if (!mouse_query_pointer_root(&mouse_x, &mouse_y, NULL))
104 return 0;
106 screen = screen_getbycoord(mouse_x, mouse_y);
107 lua_pushnumber(L, screen_array_indexof(&globalconf.screens, screen) + 1);
108 return 1;
111 /** Newindex for mouse.
112 * \param L The Lua VM state.
113 * \return The number of elements pushed on stack.
115 static int
116 luaA_mouse_newindex(lua_State *L)
118 const char *attr = luaL_checkstring(L, 2);
119 int x, y = 0;
120 int screen;
122 if (A_STRNEQ(attr, "screen"))
123 return 0;
125 screen = luaL_checknumber(L, 3) - 1;
126 luaA_checkscreen(screen);
128 x = globalconf.screens.tab[screen].geometry.x;
129 y = globalconf.screens.tab[screen].geometry.y;
131 mouse_warp_pointer(globalconf.screen->root, x, y);
132 return 0;
135 /** Push a table with mouse status.
136 * \param L The Lua VM state.
137 * \param x The x coordinate.
138 * \param y The y coordinate.
139 * \param mask The button mask.
142 luaA_mouse_pushstatus(lua_State *L, int x, int y)
144 lua_createtable(L, 0, 2);
145 lua_pushnumber(L, x);
146 lua_setfield(L, -2, "x");
147 lua_pushnumber(L, y);
148 lua_setfield(L, -2, "y");
150 lua_createtable(L, 5, 0);
152 const unsigned int max_button = sizeof(globalconf.buttons_pressed) * 8;
153 unsigned int mask = 1;
154 for (unsigned int i = 1; i <= max_button; i++, mask <<= 1)
156 lua_pushboolean(L, globalconf.buttons_pressed & mask);
157 lua_rawseti(L, -2, i);
159 lua_setfield(L, -2, "buttons");
160 return 1;
163 /** Get or set the mouse coords.
164 * \param L The Lua VM state.
165 * \return The number of elements pushed on stack.
167 static int
168 luaA_mouse_coords(lua_State *L)
170 int x, y;
171 int16_t mouse_x, mouse_y;
173 if(lua_gettop(L) >= 1)
175 luaA_checktable(L, 1);
176 bool ignore_enter_notify = (lua_gettop(L) == 2 && luaA_checkboolean(L, 2));
178 if(!mouse_query_pointer_root(&mouse_x, &mouse_y, NULL))
179 return 0;
181 x = luaA_getopt_number(L, 1, "x", mouse_x);
182 y = luaA_getopt_number(L, 1, "y", mouse_y);
184 if(ignore_enter_notify)
185 client_ignore_enterleave_events();
187 mouse_warp_pointer(globalconf.screen->root, x, y);
189 if(ignore_enter_notify)
190 client_restore_enterleave_events();
192 lua_pop(L, 1);
195 if(!mouse_query_pointer_root(&mouse_x, &mouse_y, NULL))
196 return 0;
198 return luaA_mouse_pushstatus(L, mouse_x, mouse_y);
201 /** Get the client which is under the pointer.
202 * \param L The Lua VM state.
203 * \return The number of elements pushed on stack.
204 * \luastack
205 * \lreturn A client or nil.
207 static int
208 luaA_mouse_object_under_pointer(lua_State *L)
210 int16_t mouse_x, mouse_y;
211 xcb_window_t child;
213 if(!mouse_query_pointer_root(&mouse_x, &mouse_y, &child))
214 return 0;
216 drawin_t *drawin;
217 client_t *client;
219 if((drawin = drawin_getbywin(child)))
220 luaA_object_push(L, drawin);
222 if((client = client_getbyframewin(child)))
223 return luaA_object_push(globalconf.L, client);
225 return 0;
228 const struct luaL_Reg awesome_mouse_methods[] =
230 { "__index", luaA_mouse_index },
231 { "__newindex", luaA_mouse_newindex },
232 { "coords", luaA_mouse_coords },
233 { "object_under_pointer", luaA_mouse_object_under_pointer },
234 { NULL, NULL }
236 const struct luaL_Reg awesome_mouse_meta[] =
238 { NULL, NULL }
241 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80