forked from awesomeWM/awesome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse.c
More file actions
131 lines (111 loc) · 3.51 KB
/
Copy pathmouse.c
File metadata and controls
131 lines (111 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* mouse.c - mouse managing
*
* Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "mouse.h"
#include "awesome.h"
#include "screen.h"
#include "objects/client.h"
/** Get the pointer position.
* \param window The window to get position on.
* \param x will be set to the Pointer-x-coordinate relative to window
* \param y will be set to the Pointer-y-coordinate relative to window
* \param child Will be set to the window under the pointer.
* \param mask will be set to the current buttons state
* \return true on success, false if an error occurred
**/
static bool
mouse_query_pointer(xcb_window_t window, int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
{
xcb_query_pointer_cookie_t query_ptr_c = xcb_query_pointer_unchecked(_G_connection, window);
xcb_query_pointer_reply_t *query_ptr_r = xcb_query_pointer_reply(_G_connection, query_ptr_c, NULL);
if(!query_ptr_r || !query_ptr_r->same_screen)
return false;
*x = query_ptr_r->win_x;
*y = query_ptr_r->win_y;
*mask = query_ptr_r->mask;
*child = query_ptr_r->child;
p_delete(&query_ptr_r);
return true;
}
static int
luaA_mouse_pushmask(lua_State *L, uint16_t mask)
{
lua_createtable(L, 5, 0);
int i = 1;
for(uint16_t maski = XCB_BUTTON_MASK_1; maski <= XCB_BUTTON_MASK_5; maski <<= 1)
{
if(mask & maski)
lua_pushboolean(L, true);
else
lua_pushboolean(L, false);
lua_rawseti(L, -2, i++);
}
return 1;
}
/** Push a table with mouse status.
* \param L The Lua VM state.
* \param x The x coordinate.
* \param y The y coordinate.
* \param mask The button mask.
*/
int
luaA_mouse_pushstatus(lua_State *L, int x, int y, uint16_t mask)
{
lua_createtable(L, 0, 2);
lua_pushnumber(L, x);
lua_setfield(L, -2, "x");
lua_pushnumber(L, y);
lua_setfield(L, -2, "y");
luaA_mouse_pushmask(L, mask);
lua_setfield(L, -2, "buttons");
return 1;
}
/** Query mouse information.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_mouse_query(lua_State *L)
{
uint16_t mask;
int16_t x, y;
xcb_window_t child;
if(!mouse_query_pointer(_G_screen->root, &x, &y, &child, &mask))
return 0;
luaA_object_push(L, ewindow_getbywin(child));
lua_pushinteger(L, x);
lua_pushinteger(L, y);
luaA_mouse_pushmask(L, mask);
return 4;
}
static int
luaA_mouse_warp(lua_State *L)
{
window_t *window = luaA_checkudata(L, 1, &window_class);
int x = luaL_checknumber(L, 2), y = luaL_checknumber(L, 3);
xcb_warp_pointer(_G_connection, XCB_NONE, window->window, 0, 0, 0, 0, x, y);
return 0;
}
const struct luaL_reg awesome_mouse_lib[] =
{
{ "query", luaA_mouse_query },
{ "warp", luaA_mouse_warp },
{ NULL, NULL }
};
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80