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.
23 #include "common/util.h"
24 #include "globalconf.h"
25 #include "objects/client.h"
26 #include "objects/drawin.h"
27 #include "objects/screen.h"
29 /** Get the pointer position.
30 * \param window The window to get position on.
31 * \param x will be set to the Pointer-x-coordinate relative to window
32 * \param y will be set to the Pointer-y-coordinate relative to window
33 * \param child Will be set to the window under the pointer.
34 * \param mask will be set to the current buttons state
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
, uint16_t *mask
)
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
)
48 p_delete(&query_ptr_r
);
52 *x
= query_ptr_r
->win_x
;
53 *y
= query_ptr_r
->win_y
;
56 *mask
= query_ptr_r
->mask
;
58 *child
= query_ptr_r
->child
;
60 p_delete(&query_ptr_r
);
65 /** Get the pointer position on the screen.
66 * \param x This will be set to the Pointer-x-coordinate relative to window.
67 * \param y This will be set to the Pointer-y-coordinate relative to window.
68 * \param child This will be set to the window under the pointer.
69 * \param mask This will be set to the current buttons state.
70 * \return True on success, false if an error occurred.
73 mouse_query_pointer_root(int16_t *x
, int16_t *y
, xcb_window_t
*child
, uint16_t *mask
)
75 xcb_window_t root
= globalconf
.screen
->root
;
77 return mouse_query_pointer(root
, x
, y
, child
, mask
);
80 /** Set the pointer position.
81 * \param window The destination window.
82 * \param x X-coordinate inside window.
83 * \param y Y-coordinate inside window.
86 mouse_warp_pointer(xcb_window_t window
, int x
, int y
)
88 xcb_warp_pointer(globalconf
.connection
, XCB_NONE
, window
,
93 * \param L The Lua VM state.
94 * \return The number of elements pushed on stack.
96 * \lfield coords Mouse coordinates.
97 * \lfield screen Mouse screen number.
100 luaA_mouse_index(lua_State
*L
)
102 const char *attr
= luaL_checkstring(L
, 2);
103 int16_t mouse_x
, mouse_y
;
106 /* attr is not "screen"?! */
107 if (A_STRNEQ(attr
, "screen"))
108 return luaA_default_index(L
);
110 if (!mouse_query_pointer_root(&mouse_x
, &mouse_y
, NULL
, NULL
))
112 /* Nothing ever handles mouse.screen being nil. Lying is better than
113 * having lots of lua errors in this case.
115 if (globalconf
.focus
.client
)
116 lua_pushnumber(L
, screen_get_index(globalconf
.focus
.client
->screen
));
118 lua_pushnumber(L
, 1);
122 screen
= screen_getbycoord(mouse_x
, mouse_y
);
123 lua_pushnumber(L
, screen_get_index(screen
));
127 /** Newindex for mouse.
128 * \param L The Lua VM state.
129 * \return The number of elements pushed on stack.
132 luaA_mouse_newindex(lua_State
*L
)
134 const char *attr
= luaL_checkstring(L
, 2);
137 if (A_STRNEQ(attr
, "screen"))
138 return luaA_default_newindex(L
);
140 screen
= luaA_checkscreen(L
, 3);
141 mouse_warp_pointer(globalconf
.screen
->root
, screen
->geometry
.x
, screen
->geometry
.y
);
145 /** Push a table with mouse status.
146 * \param L The Lua VM state.
147 * \param x The x coordinate.
148 * \param y The y coordinate.
149 * \param mask The button mask.
152 luaA_mouse_pushstatus(lua_State
*L
, int x
, int y
, uint16_t mask
)
154 lua_createtable(L
, 0, 2);
155 lua_pushnumber(L
, x
);
156 lua_setfield(L
, -2, "x");
157 lua_pushnumber(L
, y
);
158 lua_setfield(L
, -2, "y");
160 lua_createtable(L
, 5, 0);
164 for(uint16_t maski
= XCB_BUTTON_MASK_1
; maski
<= XCB_BUTTON_MASK_5
; maski
<<= 1)
167 lua_pushboolean(L
, true);
169 lua_pushboolean(L
, false);
170 lua_rawseti(L
, -2, i
++);
172 lua_setfield(L
, -2, "buttons");
176 /** Get or set the mouse coords.
177 * \param L The Lua VM state.
178 * \return The number of elements pushed on stack.
181 luaA_mouse_coords(lua_State
*L
)
185 int16_t mouse_x
, mouse_y
;
187 if(lua_gettop(L
) >= 1)
189 luaA_checktable(L
, 1);
190 bool ignore_enter_notify
= (lua_gettop(L
) == 2 && luaA_checkboolean(L
, 2));
192 if(!mouse_query_pointer_root(&mouse_x
, &mouse_y
, NULL
, &mask
))
195 x
= luaA_getopt_number(L
, 1, "x", mouse_x
);
196 y
= luaA_getopt_number(L
, 1, "y", mouse_y
);
198 if(ignore_enter_notify
)
199 client_ignore_enterleave_events();
201 mouse_warp_pointer(globalconf
.screen
->root
, x
, y
);
203 if(ignore_enter_notify
)
204 client_restore_enterleave_events();
209 if(!mouse_query_pointer_root(&mouse_x
, &mouse_y
, NULL
, &mask
))
212 return luaA_mouse_pushstatus(L
, mouse_x
, mouse_y
, mask
);
215 /** Get the client which is under the pointer.
216 * \param L The Lua VM state.
217 * \return The number of elements pushed on stack.
219 * \lreturn A client or nil.
222 luaA_mouse_object_under_pointer(lua_State
*L
)
224 int16_t mouse_x
, mouse_y
;
227 if(!mouse_query_pointer_root(&mouse_x
, &mouse_y
, &child
, NULL
))
233 if((drawin
= drawin_getbywin(child
)))
234 return luaA_object_push(L
, drawin
);
236 if((client
= client_getbyframewin(child
)))
237 return luaA_object_push(globalconf
.L
, client
);
242 const struct luaL_Reg awesome_mouse_methods
[] =
244 { "__index", luaA_mouse_index
},
245 { "__newindex", luaA_mouse_newindex
},
246 { "coords", luaA_mouse_coords
},
247 { "object_under_pointer", luaA_mouse_object_under_pointer
},
250 const struct luaL_Reg awesome_mouse_meta
[] =
255 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80