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
511 lines (445 loc) · 13.4 KB
/
Copy pathmouse.c
File metadata and controls
511 lines (445 loc) · 13.4 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
* mouse.c - mouse managing
*
* Copyright © 2007-2008 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <math.h>
#include "common/tokenize.h"
#include "screen.h"
#include "tag.h"
#include "wibox.h"
#include "common/xcursor.h"
extern awesome_t globalconf;
DO_LUA_NEW(static, button_t, button, "button", button_ref)
DO_LUA_GC(button_t, button, "button", button_unref)
DO_LUA_EQ(button_t, button, "button")
/** Delete a button.
* \param button The button to destroy.
*/
void
button_delete(button_t **button)
{
luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*button)->press);
luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*button)->release);
p_delete(button);
}
/** 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 occured
**/
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_reply_t *query_ptr_r;
query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, window);
query_ptr_r = xcb_query_pointer_reply(globalconf.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;
if (mask)
*mask = query_ptr_r->mask;
if(child)
*child = query_ptr_r->child;
p_delete(&query_ptr_r);
return true;
}
/** Get the pointer position on the screen.
* \param screen This will be set to the screen number the mouse is on.
* \param x This will be set to the Pointer-x-coordinate relative to window.
* \param y This will be set to the Pointer-y-coordinate relative to window.
* \param child This will be set to the window under the pointer.
* \param mask This will be set to the current buttons state.
* \return True on success, false if an error occured.
*/
static bool
mouse_query_pointer_root(int *s, int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
{
for(int screen = 0;
screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
screen++)
{
xcb_window_t root = xutil_screen_get(globalconf.connection, screen)->root;
if(mouse_query_pointer(root, x, y, child, mask))
{
*s = screen;
return true;
}
}
return false;
}
/** Set the pointer position.
* \param window The destination window.
* \param x X-coordinate inside window.
* \param y Y-coordinate inside window.
*/
static inline void
mouse_warp_pointer(xcb_window_t window, int x, int y)
{
xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
0, 0, 0, 0, x, y );
}
/** Create a new mouse button bindings.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lparam A table with modifiers keys, or a button to clone.
* \lparam A mouse button number.
* \lparam A function to execute on click events.
* \lparam A function to execute on release events.
* \lreturn A mouse button binding.
*/
static int
luaA_button_new(lua_State *L)
{
int i, len;
button_t *button, **orig;
if((orig = luaA_toudata(L, 2, "button")))
{
button_t *copy = p_new(button_t, 1);
copy->mod = (*orig)->mod;
copy->button = (*orig)->button;
if((*orig)->press != LUA_REFNIL)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, (*orig)->press);
luaA_registerfct(L, -1, ©->press);
}
else
copy->press = LUA_REFNIL;
if((*orig)->release != LUA_REFNIL)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, (*orig)->release);
luaA_registerfct(L, -1, ©->release);
}
else
copy->release = LUA_REFNIL;
return luaA_button_userdata_new(L, copy);
}
luaA_checktable(L, 2);
/* arg 3 is mouse button */
i = luaL_checknumber(L, 3);
/* arg 4 and 5 are callback functions */
if(!lua_isnil(L, 4))
luaA_checkfunction(L, 4);
if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
luaA_checkfunction(L, 5);
button = p_new(button_t, 1);
button->button = xutil_button_fromint(i);
if(lua_isnil(L, 4))
button->press = LUA_REFNIL;
else
luaA_registerfct(L, 4, &button->press);
if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
luaA_registerfct(L, 5, &button->release);
else
button->release = LUA_REFNIL;
len = lua_objlen(L, 2);
for(i = 1; i <= len; i++)
{
size_t blen;
const char *buf;
lua_rawgeti(L, 2, i);
buf = luaL_checklstring(L, -1, &blen);
button->mod |= xutil_key_mask_fromstr(buf, blen);
}
return luaA_button_userdata_new(L, button);
}
/** Set a button array with a Lua table.
* \param L The Lua VM state.
* \param idx The index of the Lua table.
* \param buttons The array button to fill.
*/
void
luaA_button_array_set(lua_State *L, int idx, button_array_t *buttons)
{
button_t **b;
luaA_checktable(L, idx);
button_array_wipe(buttons);
button_array_init(buttons);
lua_pushnil(L);
while(lua_next(L, idx))
{
b = luaA_checkudata(L, -1, "button");
button_array_append(buttons, *b);
button_ref(b);
lua_pop(L, 1);
}
}
/** Push an array of button as an Lua table onto the stack.
* \param L The Lua VM state.
* \param buttons The button array to push.
* \return The number of elements pushed on stack.
*/
int
luaA_button_array_get(lua_State *L, button_array_t *buttons)
{
luaA_otable_new(L);
for(int i = 0; i < buttons->len; i++)
{
luaA_button_userdata_new(L, buttons->tab[i]);
lua_rawseti(L, -2, i + 1);
}
return 1;
}
/** Button object.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lfield press The function called when button press event is received.
* \lfield release The function called when button release event is received.
*/
static int
luaA_button_index(lua_State *L)
{
if(luaA_usemetatable(L, 1, 2))
return 1;
size_t len;
button_t **button = luaA_checkudata(L, 1, "button");
const char *attr = luaL_checklstring(L, 2, &len);
switch(a_tokenize(attr, len))
{
case A_TK_PRESS:
if((*button)->press != LUA_REFNIL)
lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->press);
else
lua_pushnil(L);
break;
case A_TK_RELEASE:
if((*button)->release != LUA_REFNIL)
lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->release);
else
lua_pushnil(L);
break;
case A_TK_BUTTON:
/* works fine, but not *really* neat */
lua_pushnumber(L, (*button)->button);
break;
default:
break;
}
return 1;
}
/** Button object.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
*/
static int
luaA_button_newindex(lua_State *L)
{
if(luaA_usemetatable(L, 1, 2))
return 1;
size_t len;
button_t **button = luaA_checkudata(L, 1, "button");
const char *attr = luaL_checklstring(L, 2, &len);
switch(a_tokenize(attr, len))
{
case A_TK_PRESS:
luaA_registerfct(L, 3, &(*button)->press);
break;
case A_TK_RELEASE:
luaA_registerfct(L, 3, &(*button)->release);
break;
case A_TK_BUTTON:
(*button)->button = xutil_button_fromint(luaL_checknumber(L, 3));
break;
default:
break;
}
return 0;
}
const struct luaL_reg awesome_button_methods[] =
{
{ "__call", luaA_button_new },
{ NULL, NULL }
};
const struct luaL_reg awesome_button_meta[] =
{
{ "__index", luaA_button_index },
{ "__newindex", luaA_button_newindex },
{ "__gc", luaA_button_gc },
{ "__eq", luaA_button_eq },
{ "__tostring", luaA_button_tostring },
{ NULL, NULL }
};
/** Mouse library.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lfield coords Mouse coordinates.
* \lfield screen Mouse screen number.
*/
static int
luaA_mouse_index(lua_State *L)
{
size_t len;
const char *attr = luaL_checklstring(L, 2, &len);
int16_t mouse_x, mouse_y;
int screen, i;
switch(a_tokenize(attr, len))
{
case A_TK_SCREEN:
if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, NULL))
return 0;
i = screen_getbycoord(screen, mouse_x, mouse_y);
lua_pushnumber(L, i + 1);
break;
default:
return 0;
}
return 1;
}
/** Newindex for mouse.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_mouse_newindex(lua_State *L)
{
size_t len;
const char *attr = luaL_checklstring(L, 2, &len);
int x, y = 0;
xcb_window_t root;
int screen, phys_screen;
switch(a_tokenize(attr, len))
{
case A_TK_SCREEN:
screen = luaL_checknumber(L, 3) - 1;
luaA_checkscreen(screen);
/* we need the physical one to get the root window */
phys_screen = screen_virttophys(screen);
root = xutil_screen_get(globalconf.connection, phys_screen)->root;
x = globalconf.screens[screen].geometry.x;
y = globalconf.screens[screen].geometry.y;
mouse_warp_pointer(root, x, y);
break;
default:
return 0;
}
return 0;
}
/** 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_newtable(L);
lua_pushnumber(L, x);
lua_setfield(L, -2, "x");
lua_pushnumber(L, y);
lua_setfield(L, -2, "y");
lua_newtable(L);
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++);
}
lua_setfield(L, -2, "buttons");
return 1;
}
/** Get or set the mouse coords.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lparam None or a table with x and y keys as mouse coordinates.
* \lreturn A table with mouse coordinates.
*/
static int
luaA_mouse_coords(lua_State *L)
{
uint16_t mask;
int screen, x, y;
int16_t mouse_x, mouse_y;
if(lua_gettop(L) == 1)
{
xcb_window_t root;
luaA_checktable(L, 1);
if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, &mask))
return 0;
x = luaA_getopt_number(L, 1, "x", mouse_x);
y = luaA_getopt_number(L, 1, "y", mouse_y);
root = xutil_screen_get(globalconf.connection, screen)->root;
mouse_warp_pointer(root, x, y);
lua_pop(L, 1);
}
if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL, &mask))
return 0;
return luaA_mouse_pushstatus(L, mouse_x, mouse_y, mask);
}
/** Get the client which is under the pointer.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lreturn A client or nil.
*/
static int
luaA_mouse_object_under_pointer(lua_State *L)
{
int screen;
int16_t mouse_x, mouse_y;
xcb_window_t child;
if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &child, NULL))
return 0;
wibox_t *wibox;
client_t *client;
if((wibox = wibox_getbywin(child)))
{
luaA_wibox_userdata_new(L, wibox);
int16_t x = mouse_x - wibox->sw.geometry.x;
int16_t y = mouse_y - wibox->sw.geometry.y;
widget_t *widget = widget_getbycoords(wibox->position, &wibox->widgets,
wibox->sw.geometry.width,
wibox->sw.geometry.height,
&x, &y);
if(widget)
{
luaA_widget_userdata_new(L, widget);
return 2;
}
return 1;
}
else if((client = client_getbywin(child)))
return luaA_client_userdata_new(L, client);
return 0;
}
const struct luaL_reg awesome_mouse_methods[] =
{
{ "__index", luaA_mouse_index },
{ "__newindex", luaA_mouse_newindex },
{ "coords", luaA_mouse_coords },
{ "object_under_pointer", luaA_mouse_object_under_pointer },
{ NULL, NULL }
};
const struct luaL_reg awesome_mouse_meta[] =
{
{ NULL, NULL }
};
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80