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.
24 #include "objects/client.h"
25 #include "globalconf.h"
26 #include "objects/drawin.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
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
)
49 *x
= query_ptr_r
->win_x
;
50 *y
= query_ptr_r
->win_y
;
53 *child
= query_ptr_r
->child
;
55 p_delete(&query_ptr_r
);
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.
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.
79 mouse_warp_pointer(xcb_window_t window
, int x
, int y
)
81 xcb_warp_pointer(globalconf
.connection
, XCB_NONE
, window
,
86 * \param L The Lua VM state.
87 * \return The number of elements pushed on stack.
89 * \lfield coords Mouse coordinates.
90 * \lfield screen Mouse screen number.
93 luaA_mouse_index(lua_State
*L
)
95 const char *attr
= luaL_checkstring(L
, 2);
96 int16_t mouse_x
, mouse_y
;
99 /* attr is not "screen"?! */
100 if (A_STRNEQ(attr
, "screen"))
103 if (!mouse_query_pointer_root(&mouse_x
, &mouse_y
, NULL
))
106 screen
= screen_getbycoord(mouse_x
, mouse_y
);
107 lua_pushnumber(L
, screen_array_indexof(&globalconf
.screens
, screen
) + 1);
111 /** Newindex for mouse.
112 * \param L The Lua VM state.
113 * \return The number of elements pushed on stack.
116 luaA_mouse_newindex(lua_State
*L
)
118 const char *attr
= luaL_checkstring(L
, 2);
122 if (A_STRNEQ(attr
, "screen"))
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
);
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");
163 /** Get or set the mouse coords.
164 * \param L The Lua VM state.
165 * \return The number of elements pushed on stack.
168 luaA_mouse_coords(lua_State
*L
)
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
))
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();
195 if(!mouse_query_pointer_root(&mouse_x
, &mouse_y
, NULL
))
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.
205 * \lreturn A client or nil.
208 luaA_mouse_object_under_pointer(lua_State
*L
)
210 int16_t mouse_x
, mouse_y
;
213 if(!mouse_query_pointer_root(&mouse_x
, &mouse_y
, &child
))
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
);
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
},
236 const struct luaL_Reg awesome_mouse_meta
[] =
241 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80