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 * \param mask will be set to the current buttons state
36 * \return true on success, false if an error occurred
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
)
49 p_delete(&query_ptr_r
);
53 *x
= query_ptr_r
->win_x
;
54 *y
= query_ptr_r
->win_y
;
57 *mask
= query_ptr_r
->mask
;
59 *child
= query_ptr_r
->child
;
61 p_delete(&query_ptr_r
);
66 /** Get the pointer position on the screen.
67 * \param x This will be set to the Pointer-x-coordinate relative to window.
68 * \param y This will be set to the Pointer-y-coordinate relative to window.
69 * \param child This will be set to the window under the pointer.
70 * \param mask This will be set to the current buttons state.
71 * \return True on success, false if an error occurred.
74 mouse_query_pointer_root(int16_t *x
, int16_t *y
, xcb_window_t
*child
, uint16_t *mask
)
76 xcb_window_t root
= globalconf
.screen
->root
;
78 return mouse_query_pointer(root
, x
, y
, child
, mask
);
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.
87 mouse_warp_pointer(xcb_window_t window
, int x
, int y
)
89 xcb_warp_pointer(globalconf
.connection
, XCB_NONE
, window
,
94 * \param L The Lua VM state.
95 * \return The number of elements pushed on stack.
97 * \lfield coords Mouse coordinates.
98 * \lfield screen Mouse screen number.
101 luaA_mouse_index(lua_State
*L
)
103 const char *attr
= luaL_checkstring(L
, 2);
104 int16_t mouse_x
, mouse_y
;
107 /* attr is not "screen"?! */
108 if (A_STRNEQ(attr
, "screen"))
111 if (!mouse_query_pointer_root(&mouse_x
, &mouse_y
, NULL
, NULL
))
113 /* Nothing ever handles mouse.screen being nil. Lying is better than
114 * having lots of lua errors in this case.
116 if (globalconf
.focus
.client
)
117 lua_pushnumber(L
, screen_array_indexof(&globalconf
.screens
, globalconf
.focus
.client
->screen
) + 1);
119 lua_pushnumber(L
, 1);
123 screen
= screen_getbycoord(mouse_x
, mouse_y
);
124 lua_pushnumber(L
, screen_array_indexof(&globalconf
.screens
, screen
) + 1);
128 /** Newindex for mouse.
129 * \param L The Lua VM state.
130 * \return The number of elements pushed on stack.
133 luaA_mouse_newindex(lua_State
*L
)
135 const char *attr
= luaL_checkstring(L
, 2);
139 if (A_STRNEQ(attr
, "screen"))
142 screen
= luaL_checknumber(L
, 3) - 1;
143 luaA_checkscreen(screen
);
145 x
= globalconf
.screens
.tab
[screen
].geometry
.x
;
146 y
= globalconf
.screens
.tab
[screen
].geometry
.y
;
148 mouse_warp_pointer(globalconf
.screen
->root
, x
, y
);
152 /** Push a table with mouse status.
153 * \param L The Lua VM state.
154 * \param x The x coordinate.
155 * \param y The y coordinate.
156 * \param mask The button mask.
159 luaA_mouse_pushstatus(lua_State
*L
, int x
, int y
, uint16_t mask
)
161 lua_createtable(L
, 0, 2);
162 lua_pushnumber(L
, x
);
163 lua_setfield(L
, -2, "x");
164 lua_pushnumber(L
, y
);
165 lua_setfield(L
, -2, "y");
167 lua_createtable(L
, 5, 0);
171 for(uint16_t maski
= XCB_BUTTON_MASK_1
; maski
<= XCB_BUTTON_MASK_5
; maski
<<= 1)
174 lua_pushboolean(L
, true);
176 lua_pushboolean(L
, false);
177 lua_rawseti(L
, -2, i
++);
179 lua_setfield(L
, -2, "buttons");
183 /** Get or set the mouse coords.
184 * \param L The Lua VM state.
185 * \return The number of elements pushed on stack.
188 luaA_mouse_coords(lua_State
*L
)
192 int16_t mouse_x
, mouse_y
;
194 if(lua_gettop(L
) >= 1)
196 luaA_checktable(L
, 1);
197 bool ignore_enter_notify
= (lua_gettop(L
) == 2 && luaA_checkboolean(L
, 2));
199 if(!mouse_query_pointer_root(&mouse_x
, &mouse_y
, NULL
, &mask
))
202 x
= luaA_getopt_number(L
, 1, "x", mouse_x
);
203 y
= luaA_getopt_number(L
, 1, "y", mouse_y
);
205 if(ignore_enter_notify
)
206 client_ignore_enterleave_events();
208 mouse_warp_pointer(globalconf
.screen
->root
, x
, y
);
210 if(ignore_enter_notify
)
211 client_restore_enterleave_events();
216 if(!mouse_query_pointer_root(&mouse_x
, &mouse_y
, NULL
, &mask
))
219 return luaA_mouse_pushstatus(L
, mouse_x
, mouse_y
, mask
);
222 /** Get the client which is under the pointer.
223 * \param L The Lua VM state.
224 * \return The number of elements pushed on stack.
226 * \lreturn A client or nil.
229 luaA_mouse_object_under_pointer(lua_State
*L
)
231 int16_t mouse_x
, mouse_y
;
234 if(!mouse_query_pointer_root(&mouse_x
, &mouse_y
, &child
, NULL
))
240 if((drawin
= drawin_getbywin(child
)))
241 return luaA_object_push(L
, drawin
);
243 if((client
= client_getbyframewin(child
)))
244 return luaA_object_push(globalconf
.L
, client
);
249 const struct luaL_Reg awesome_mouse_methods
[] =
251 { "__index", luaA_mouse_index
},
252 { "__newindex", luaA_mouse_newindex
},
253 { "coords", luaA_mouse_coords
},
254 { "object_under_pointer", luaA_mouse_object_under_pointer
},
257 const struct luaL_Reg awesome_mouse_meta
[] =
262 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80