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.
26 #include "common/xutil.h"
28 /** Get the pointer position.
29 * \param window The window to get position on.
30 * \param x will be set to the Pointer-x-coordinate relative to window
31 * \param y will be set to the Pointer-y-coordinate relative to window
32 * \param child Will be set to the window under the pointer.
33 * \param mask will be set to the current buttons state
34 * \return true on success, false if an error occured
37 mouse_query_pointer(xcb_window_t window
, int16_t *x
, int16_t *y
, xcb_window_t
*child
, uint16_t *mask
)
39 xcb_query_pointer_cookie_t query_ptr_c
;
40 xcb_query_pointer_reply_t
*query_ptr_r
;
42 query_ptr_c
= xcb_query_pointer_unchecked(globalconf
.connection
, window
);
43 query_ptr_r
= xcb_query_pointer_reply(globalconf
.connection
, query_ptr_c
, NULL
);
45 if(!query_ptr_r
|| !query_ptr_r
->same_screen
)
48 *x
= query_ptr_r
->win_x
;
49 *y
= query_ptr_r
->win_y
;
51 *mask
= query_ptr_r
->mask
;
53 *child
= query_ptr_r
->child
;
55 p_delete(&query_ptr_r
);
60 /** Get the pointer position on the screen.
61 * \param screen This will be set to the screen the mouse is on.
62 * \param x This will be set to the Pointer-x-coordinate relative to window.
63 * \param y This will be set to the Pointer-y-coordinate relative to window.
64 * \param child This will be set to the window under the pointer.
65 * \param mask This will be set to the current buttons state.
66 * \return True on success, false if an error occured.
69 mouse_query_pointer_root(screen_t
**s
, int16_t *x
, int16_t *y
, xcb_window_t
*child
, uint16_t *mask
)
72 screen
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
75 xcb_window_t root
= xutil_screen_get(globalconf
.connection
, screen
)->root
;
77 if(mouse_query_pointer(root
, x
, y
, child
, mask
))
79 *s
= &globalconf
.screens
.tab
[screen
];
86 /** Set the pointer position.
87 * \param window The destination window.
88 * \param x X-coordinate inside window.
89 * \param y Y-coordinate inside window.
92 mouse_warp_pointer(xcb_window_t window
, int x
, int y
)
94 xcb_warp_pointer(globalconf
.connection
, XCB_NONE
, window
,
99 * \param L The Lua VM state.
100 * \return The number of elements pushed on stack.
102 * \lfield coords Mouse coordinates.
103 * \lfield screen Mouse screen number.
106 luaA_mouse_index(lua_State
*L
)
109 const char *attr
= luaL_checklstring(L
, 2, &len
);
110 int16_t mouse_x
, mouse_y
;
113 switch(a_tokenize(attr
, len
))
116 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, NULL
, NULL
))
119 screen
= screen_getbycoord(screen
, mouse_x
, mouse_y
);
121 lua_pushnumber(L
, screen_array_indexof(&globalconf
.screens
, screen
) + 1);
130 /** Newindex for mouse.
131 * \param L The Lua VM state.
132 * \return The number of elements pushed on stack.
135 luaA_mouse_newindex(lua_State
*L
)
138 const char *attr
= luaL_checklstring(L
, 2, &len
);
141 int screen
, phys_screen
;
143 switch(a_tokenize(attr
, len
))
146 screen
= luaL_checknumber(L
, 3) - 1;
147 luaA_checkscreen(screen
);
149 /* we need the physical one to get the root window */
150 phys_screen
= screen_virttophys(screen
);
151 root
= xutil_screen_get(globalconf
.connection
, phys_screen
)->root
;
153 x
= globalconf
.screens
.tab
[screen
].geometry
.x
;
154 y
= globalconf
.screens
.tab
[screen
].geometry
.y
;
156 mouse_warp_pointer(root
, x
, y
);
165 /** Push a table with mouse status.
166 * \param L The Lua VM state.
167 * \param x The x coordinate.
168 * \param y The y coordinate.
169 * \param mask The button mask.
172 luaA_mouse_pushstatus(lua_State
*L
, int x
, int y
, uint16_t mask
)
174 lua_createtable(L
, 0, 2);
175 lua_pushnumber(L
, x
);
176 lua_setfield(L
, -2, "x");
177 lua_pushnumber(L
, y
);
178 lua_setfield(L
, -2, "y");
180 lua_createtable(L
, 5, 0);
184 for(uint16_t maski
= XCB_BUTTON_MASK_1
; maski
<= XCB_BUTTON_MASK_5
; maski
<<= 1)
187 lua_pushboolean(L
, true);
189 lua_pushboolean(L
, false);
190 lua_rawseti(L
, -2, i
++);
192 lua_setfield(L
, -2, "buttons");
196 /** Get or set the mouse coords.
197 * \param L The Lua VM state.
198 * \return The number of elements pushed on stack.
200 * \lparam None or a table with x and y keys as mouse coordinates.
201 * \lreturn A table with mouse coordinates.
204 luaA_mouse_coords(lua_State
*L
)
208 int16_t mouse_x
, mouse_y
;
211 if(lua_gettop(L
) == 1)
215 luaA_checktable(L
, 1);
217 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, NULL
, &mask
))
220 x
= luaA_getopt_number(L
, 1, "x", mouse_x
);
221 y
= luaA_getopt_number(L
, 1, "y", mouse_y
);
223 root
= xutil_screen_get(globalconf
.connection
,
224 screen_array_indexof(&globalconf
.screens
, screen
))->root
;
225 mouse_warp_pointer(root
, x
, y
);
229 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, NULL
, &mask
))
232 return luaA_mouse_pushstatus(L
, mouse_x
, mouse_y
, mask
);
235 /** Get the client which is under the pointer.
236 * \param L The Lua VM state.
237 * \return The number of elements pushed on stack.
239 * \lreturn A client or nil.
242 luaA_mouse_object_under_pointer(lua_State
*L
)
245 int16_t mouse_x
, mouse_y
;
248 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, &child
, NULL
))
253 if((wibox
= wibox_getbywin(child
)))
255 wibox_push(L
, wibox
);
257 int16_t x
= mouse_x
- wibox
->sw
.geometry
.x
;
258 int16_t y
= mouse_y
- wibox
->sw
.geometry
.y
;
260 widget_t
*widget
= widget_getbycoords(wibox
->sw
.orientation
, &wibox
->widgets
,
261 wibox
->sw
.geometry
.width
,
262 wibox
->sw
.geometry
.height
,
267 widget_push(L
, widget
);
272 else if((client
= client_getbywin(child
)))
273 return client_push(globalconf
.L
, client
);
278 const struct luaL_reg awesome_mouse_methods
[] =
280 { "__index", luaA_mouse_index
},
281 { "__newindex", luaA_mouse_newindex
},
282 { "coords", luaA_mouse_coords
},
283 { "object_under_pointer", luaA_mouse_object_under_pointer
},
286 const struct luaL_reg awesome_mouse_meta
[] =
291 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80