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.
28 #include "common/tokenize.h"
29 #include "common/xutil.h"
31 /** Get the pointer position.
32 * \param window The window to get position on.
33 * \param x will be set to the Pointer-x-coordinate relative to window
34 * \param y will be set to the Pointer-y-coordinate relative to window
35 * \param child Will be set to the window under the pointer.
36 * \param mask will be set to the current buttons state
37 * \return true on success, false if an error occured
40 mouse_query_pointer(xcb_window_t window
, int16_t *x
, int16_t *y
, xcb_window_t
*child
, uint16_t *mask
)
42 xcb_query_pointer_cookie_t query_ptr_c
;
43 xcb_query_pointer_reply_t
*query_ptr_r
;
45 query_ptr_c
= xcb_query_pointer_unchecked(globalconf
.connection
, window
);
46 query_ptr_r
= xcb_query_pointer_reply(globalconf
.connection
, query_ptr_c
, NULL
);
48 if(!query_ptr_r
|| !query_ptr_r
->same_screen
)
51 *x
= query_ptr_r
->win_x
;
52 *y
= query_ptr_r
->win_y
;
54 *mask
= query_ptr_r
->mask
;
56 *child
= query_ptr_r
->child
;
58 p_delete(&query_ptr_r
);
63 /** Get the pointer position on the screen.
64 * \param s This will be set to the screen the mouse is on.
65 * \param x This will be set to the Pointer-x-coordinate relative to window.
66 * \param y This will be set to the Pointer-y-coordinate relative to window.
67 * \param child This will be set to the window under the pointer.
68 * \param mask This will be set to the current buttons state.
69 * \return True on success, false if an error occured.
72 mouse_query_pointer_root(screen_t
**s
, int16_t *x
, int16_t *y
, xcb_window_t
*child
, uint16_t *mask
)
75 screen
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
78 xcb_window_t root
= xutil_screen_get(globalconf
.connection
, screen
)->root
;
80 if(mouse_query_pointer(root
, x
, y
, child
, mask
))
82 *s
= &globalconf
.screens
.tab
[screen
];
89 /** Set the pointer position.
90 * \param window The destination window.
91 * \param x X-coordinate inside window.
92 * \param y Y-coordinate inside window.
95 mouse_warp_pointer(xcb_window_t window
, int x
, int y
)
97 xcb_warp_pointer(globalconf
.connection
, XCB_NONE
, window
,
102 * \param L The Lua VM state.
103 * \return The number of elements pushed on stack.
105 * \lfield coords Mouse coordinates.
106 * \lfield screen Mouse screen number.
109 luaA_mouse_index(lua_State
*L
)
112 const char *attr
= luaL_checklstring(L
, 2, &len
);
113 int16_t mouse_x
, mouse_y
;
116 switch(a_tokenize(attr
, len
))
119 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, NULL
, NULL
))
122 screen
= screen_getbycoord(screen
, mouse_x
, mouse_y
);
124 lua_pushnumber(L
, screen_array_indexof(&globalconf
.screens
, screen
) + 1);
133 /** Newindex for mouse.
134 * \param L The Lua VM state.
135 * \return The number of elements pushed on stack.
138 luaA_mouse_newindex(lua_State
*L
)
141 const char *attr
= luaL_checklstring(L
, 2, &len
);
144 int screen
, phys_screen
;
146 switch(a_tokenize(attr
, len
))
149 screen
= luaL_checknumber(L
, 3) - 1;
150 luaA_checkscreen(screen
);
152 /* we need the physical one to get the root window */
153 phys_screen
= screen_virttophys(screen
);
154 root
= xutil_screen_get(globalconf
.connection
, phys_screen
)->root
;
156 x
= globalconf
.screens
.tab
[screen
].geometry
.x
;
157 y
= globalconf
.screens
.tab
[screen
].geometry
.y
;
159 mouse_warp_pointer(root
, x
, y
);
168 /** Push a table with mouse status.
169 * \param L The Lua VM state.
170 * \param x The x coordinate.
171 * \param y The y coordinate.
172 * \param mask The button mask.
175 luaA_mouse_pushstatus(lua_State
*L
, int x
, int y
, uint16_t mask
)
177 lua_createtable(L
, 0, 2);
178 lua_pushnumber(L
, x
);
179 lua_setfield(L
, -2, "x");
180 lua_pushnumber(L
, y
);
181 lua_setfield(L
, -2, "y");
183 lua_createtable(L
, 5, 0);
187 for(uint16_t maski
= XCB_BUTTON_MASK_1
; maski
<= XCB_BUTTON_MASK_5
; maski
<<= 1)
190 lua_pushboolean(L
, true);
192 lua_pushboolean(L
, false);
193 lua_rawseti(L
, -2, i
++);
195 lua_setfield(L
, -2, "buttons");
199 /** Get or set the mouse coords.
200 * \param L The Lua VM state.
201 * \return The number of elements pushed on stack.
203 * \lparam None or a table with x and y keys as mouse coordinates.
204 * \lreturn A table with mouse coordinates.
207 luaA_mouse_coords(lua_State
*L
)
211 int16_t mouse_x
, mouse_y
;
214 if(lua_gettop(L
) == 1)
218 luaA_checktable(L
, 1);
220 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, NULL
, &mask
))
223 x
= luaA_getopt_number(L
, 1, "x", mouse_x
);
224 y
= luaA_getopt_number(L
, 1, "y", mouse_y
);
226 root
= xutil_screen_get(globalconf
.connection
,
227 screen_array_indexof(&globalconf
.screens
, screen
))->root
;
228 mouse_warp_pointer(root
, x
, y
);
232 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, NULL
, &mask
))
235 return luaA_mouse_pushstatus(L
, mouse_x
, mouse_y
, mask
);
238 /** Get the client which is under the pointer.
239 * \param L The Lua VM state.
240 * \return The number of elements pushed on stack.
242 * \lreturn A client or nil.
245 luaA_mouse_object_under_pointer(lua_State
*L
)
248 int16_t mouse_x
, mouse_y
;
251 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, &child
, NULL
))
256 if((wibox
= wibox_getbywin(child
)))
258 luaA_object_push(L
, wibox
);
260 int16_t x
= mouse_x
- wibox
->geometry
.x
;
261 int16_t y
= mouse_y
- wibox
->geometry
.y
;
263 widget_t
*widget
= widget_getbycoords(wibox
->orientation
, &wibox
->widgets
,
264 wibox
->geometry
.width
,
265 wibox
->geometry
.height
,
270 luaA_object_push(L
, widget
);
275 else if((client
= client_getbywin(child
)))
276 return luaA_object_push(globalconf
.L
, client
);
281 const struct luaL_reg awesome_mouse_methods
[] =
283 { "__index", luaA_mouse_index
},
284 { "__newindex", luaA_mouse_newindex
},
285 { "coords", luaA_mouse_coords
},
286 { "object_under_pointer", luaA_mouse_object_under_pointer
},
289 const struct luaL_reg awesome_mouse_meta
[] =
294 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80