This repository has been archived by the owner on Jan 11, 2020. It is now read-only.
forked from awesomeWM/awesome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.c
462 lines (412 loc) · 13.3 KB
/
root.c
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
/*
* root.c - root window management
*
* Copyright © 2008-2009 Julien Danjou <[email protected]>
*
* 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.
*
*/
/** awesome root window API
* @author Julien Danjou <[email protected]>
* @copyright 2008-2009 Julien Danjou
* @coreclassmod root
*/
#include <stdbool.h>
#include "root.h"
#include "globalconf.h"
#include "common/atoms.h"
#include "common/xcursor.h"
#include "common/xutil.h"
#include "objects/button.h"
#include "keygrabber.h"
#include "math.h"
#include <xcb/xtest.h>
struct root_impl root_impl;
static xcb_keycode_t
_string_to_key_code(const char *s)
{
xcb_keysym_t keysym;
xcb_keycode_t *keycodes;
keysym = XStringToKeysym(s);
keycodes = xcb_key_symbols_get_keycode(globalconf.keysyms, keysym);
if(keycodes) {
return keycodes[0]; /* XXX only returning the first is probably not
* the best */
} else {
return 0;
}
}
/** Send fake keyboard or mouse events.
*
* Usually the currently focused client or the keybindings will receive those
* events. If a `keygrabber` or `mousegrabber` is running, then it will get them.
*
* Some keys have different names compared to the ones generally used in
* Awesome. For example, Awesome uses "modifier keys" for keybindings using
* their X11 names such as "Control" or "Mod1" (for "Alt"). These are not the
* name of the key but is only the name of the modifier they represent. Some
* modifiers are even present twice on some keyboard like the left and right
* "Shift". Here is a list of the "real" key names matching the modifiers in
* `fake_input`:
*
* <table class='widget_list' border=1>
* <tr style='font-weight: bold;'>
* <th align='center'>Modifier name </th>
* <th align='center'>Key name</th>
* <th align='center'>Other key name</th>
* </tr>
* <tr><td> Mod4</td><td align='center'> Super_L </td><td align='center'> Super_R </td></tr>
* <tr><td> Control </td><td align='center'> Control_L </td><td align='center'> Control_R </td></tr>
* <tr><td> Shift </td><td align='center'> Shift_L </td><td align='center'> Shift_R </td></tr>
* <tr><td> Mod1</td><td align='center'> Alt_L </td><td align='center'> Alt_R </td></tr>
* </table>
*
* Note that this is valid for most of the modern "western" keyboard layouts.
* Some older, custom or foreign layouts may break this convention.
*
* This function is very low level, to be more useful, it can be wrapped into
* higher level constructs such as:
*
* **Sending strings:**
*
* @DOC_text_root_fake_string_EXAMPLE@
*
* Note that this example works for most ASCII inputs but may fail depending on
* how the string is encoded. Some multi-byte characters may not represent
* keys and some UTF-8 encoding format create characters by combining multiple
* elements such as accent + base character or various escape sequences. If you
* wish to use this example for "real world" i18n use cases, learning about
* XKB event and UTF-8 encoding is a prerequisites.
*
* **Clicking:**
*
* ![Client geometry](../images/mouse.svg)
*
* @DOC_text_root_fake_click_EXAMPLE@
*
* @param event_type The event type: key\_press, key\_release, button\_press,
* button\_release or motion\_notify.
* @param detail The detail: in case of a key event, this is the keycode
* to send, in case of a button event this is the number of the button. In
* case of a motion event, this is a boolean value which if true makes the
* coordinates relatives.
* @param x In case of a motion event, this is the X coordinate.
* @param y In case of a motion event, this is the Y coordinate.
* @staticfct fake_input
*/
static int
luaA_root_fake_input(lua_State *L)
{
if(!globalconf.have_xtest)
{
luaA_warn(L, "XTest extension is not available, cannot fake input.");
return 0;
}
const char *stype = luaL_checkstring(L, 1);
uint8_t type, detail;
int x = 0, y = 0;
if (A_STREQ(stype, "key_press"))
{
type = XCB_KEY_PRESS;
if(lua_type(L, 2) == LUA_TSTRING) {
detail = _string_to_key_code(lua_tostring(L, 2)); /* keysym */
} else {
detail = luaL_checkinteger(L, 2); /* keycode */
}
}
else if(A_STREQ(stype, "key_release"))
{
type = XCB_KEY_RELEASE;
if(lua_type(L, 2) == LUA_TSTRING) {
detail = _string_to_key_code(lua_tostring(L, 2)); /* keysym */
} else {
detail = luaL_checkinteger(L, 2); /* keycode */
}
}
else if(A_STREQ(stype, "button_press"))
{
type = XCB_BUTTON_PRESS;
detail = luaL_checkinteger(L, 2); /* button number */
}
else if(A_STREQ(stype, "button_release"))
{
type = XCB_BUTTON_RELEASE;
detail = luaL_checkinteger(L, 2); /* button number */
}
else if(A_STREQ(stype, "motion_notify"))
{
type = XCB_MOTION_NOTIFY;
detail = luaA_checkboolean(L, 2); /* relative to the current position or not */
x = round(luaA_checknumber_range(L, 3, MIN_X11_COORDINATE, MAX_X11_COORDINATE));
y = round(luaA_checknumber_range(L, 4, MIN_X11_COORDINATE, MAX_X11_COORDINATE));
}
else
return 0;
xcb_test_fake_input(globalconf.connection,
type,
detail,
0, /* This is a delay, not a timestamp! */
XCB_NONE,
x, y,
0);
return 0;
}
/** Get or set global key bindings.
* These bindings will be available when you press keys on the root window.
*
* @tparam table|nil keys_array An array of key binding objects, or nothing.
* @return The array of key bindings objects of this client.
* @staticfct keys
*/
static int
luaA_root_keys(lua_State *L)
{
if(lua_gettop(L) == 1)
{
luaA_checktable(L, 1);
foreach(key, globalconf.keys)
luaA_object_unref(L, *key);
key_array_wipe(&globalconf.keys);
key_array_init(&globalconf.keys);
lua_pushnil(L);
while(lua_next(L, 1))
key_array_append(&globalconf.keys, luaA_object_ref_class(L, -1, &key_class));
root_impl.grab_keys();
return 1;
}
lua_createtable(L, globalconf.keys.len, 0);
for(int i = 0; i < globalconf.keys.len; i++)
{
luaA_object_push(L, globalconf.keys.tab[i]);
lua_rawseti(L, -2, i + 1);
}
return 1;
}
/** Get or set global mouse bindings.
* This binding will be available when you click on the root window.
*
* @param button_table An array of mouse button bindings objects, or nothing.
* @return The array of mouse button bindings objects.
* @staticfct buttons
*/
static int
luaA_root_buttons(lua_State *L)
{
if(lua_gettop(L) == 1)
{
luaA_checktable(L, 1);
foreach(button, globalconf.buttons)
luaA_object_unref(L, *button);
button_array_wipe(&globalconf.buttons);
button_array_init(&globalconf.buttons);
lua_pushnil(L);
while(lua_next(L, 1))
button_array_append(&globalconf.buttons, luaA_object_ref(L, -1));
return 1;
}
lua_createtable(L, globalconf.buttons.len, 0);
for(int i = 0; i < globalconf.buttons.len; i++)
{
luaA_object_push(L, globalconf.buttons.tab[i]);
lua_rawseti(L, -2, i + 1);
}
return 1;
}
/** Set the root cursor
*
* The possible values are:
*
*@DOC_cursor_c_COMMON@
*
* @param cursor_name A X cursor name.
* @staticfct cursor
*/
static int
luaA_root_cursor(lua_State *L)
{
const char *cursor_name = luaL_checkstring(L, 1);
uint16_t cursor_font = xcursor_font_fromstr(cursor_name);
if(cursor_font)
{
uint32_t change_win_vals[] = { xcursor_new(globalconf.cursor_ctx, cursor_font) };
xcb_change_window_attributes(globalconf.connection,
globalconf.screen->root,
XCB_CW_CURSOR,
change_win_vals);
}
else
luaA_warn(L, "invalid cursor %s", cursor_name);
return 0;
}
/** Get the drawins attached to a screen.
*
* @return A table with all drawins.
* @staticfct drawins
*/
static int
luaA_root_drawins(lua_State *L)
{
lua_createtable(L, globalconf.drawins.len, 0);
for(int i = 0; i < globalconf.drawins.len; i++)
{
luaA_object_push(L, globalconf.drawins.tab[i]);
lua_rawseti(L, -2, i + 1);
}
return 1;
}
/** Get the wallpaper as a cairo surface or set it as a cairo pattern.
*
* @param pattern A cairo pattern as light userdata
* @return A cairo surface or nothing.
* @staticfct wallpaper
*/
static int
luaA_root_wallpaper(lua_State *L)
{
if(lua_gettop(L) == 1)
{
cairo_pattern_t *pattern = (cairo_pattern_t *)lua_touserdata(L, -1);
lua_pushboolean(L, root_impl.set_wallpaper(pattern));
/* Don't return the wallpaper, it's too easy to get memleaks */
return 1;
}
if(globalconf.wallpaper == NULL)
return 0;
/* lua has to make sure this surface gets destroyed */
lua_pushlightuserdata(L, cairo_surface_reference(globalconf.wallpaper));
return 1;
}
/** Get the size of the root window.
*
* @return Width of the root window.
* @return height of the root window.
* @staticfct size
*/
static int
luaA_root_size(lua_State *L)
{
lua_pushinteger(L, globalconf.screen->width_in_pixels);
lua_pushinteger(L, globalconf.screen->height_in_pixels);
return 2;
}
/** Get the physical size of the root window, in millimeter.
*
* @return Width of the root window, in millimeters.
* @return height of the root window, in millimeters.
* @staticfct size_mm
*/
static int
luaA_root_size_mm(lua_State *L)
{
lua_pushinteger(L, globalconf.screen->width_in_millimeters);
lua_pushinteger(L, globalconf.screen->height_in_millimeters);
return 2;
}
/** Get the attached tags.
* @return A table with all tags.
* @staticfct tags
*/
static int
luaA_root_tags(lua_State *L)
{
lua_createtable(L, globalconf.tags.len, 0);
for(int i = 0; i < globalconf.tags.len; i++)
{
luaA_object_push(L, globalconf.tags.tab[i]);
lua_rawseti(L, -2, i + 1);
}
return 1;
}
static bool
event_key_match(xcb_keycode_t keycode, uint16_t mods, keyb_t *k, xcb_keysym_t keysym)
{
return (((k->keycode && keycode == k->keycode)
|| (k->keysym && keysym == k->keysym))
&& (k->modifiers == XCB_BUTTON_MASK_ANY || k->modifiers == mods));
}
static void emit_key_signals(key_array_t *arr, bool pressed,
xcb_keycode_t keycode, uint16_t mods,
int oud, int nargs, xcb_keysym_t keysym)
{
lua_State *L = globalconf_get_lua_State();
int abs_oud = oud < 0 ? ((lua_gettop(L) + 1) + oud) : oud;
int item_matching = 0;
foreach(item, *arr)
{
if (event_key_match(keycode, mods, *item, keysym))
{
if (oud) luaA_object_push_item(L, abs_oud, *item);
else luaA_object_push(L, *item);
item_matching++;
}
}
for(; item_matching > 0; item_matching--)
{
const char *event;
if (pressed) event = "press";
else event = "release";
for(int i = 0; i < nargs; i++)
{
lua_pushvalue(L, - nargs - item_matching);
}
luaA_object_emit_signal(L, - nargs - 1, event, nargs);
lua_pop(L, 1);
}
lua_pop(L, nargs);
}
void root_handle_key(key_array_t *keys, bool pushed_to_stack,
uint32_t timestamp, uint32_t keycode, uint16_t state,
bool pressed, xcb_keysym_t keysym, struct root_impl *root)
{
lua_State *L = globalconf_get_lua_State();
globalconf.timestamp = timestamp;
if(globalconf.keygrabber != LUA_REFNIL)
{
if(keygrabber_handlekpress(keycode, pressed, state))
{
lua_rawgeti(L, LUA_REGISTRYINDEX, globalconf.keygrabber);
if(!luaA_dofunction(L, 3, 0))
{
warn("Stopping keygrabber.");
luaA_keygrabber_stop(L);
}
}
if (pushed_to_stack)
{
lua_pop(L, pushed_to_stack);
}
}
else
{
emit_key_signals(keys, pressed, keycode, state,
-pushed_to_stack, pushed_to_stack, keysym);
}
}
const struct luaL_Reg awesome_root_lib[] =
{
{ "buttons", luaA_root_buttons },
{ "keys", luaA_root_keys },
{ "cursor", luaA_root_cursor },
{ "fake_input", luaA_root_fake_input },
{ "drawins", luaA_root_drawins },
{ "wallpaper", luaA_root_wallpaper },
{ "size", luaA_root_size },
{ "size_mm", luaA_root_size_mm },
{ "tags", luaA_root_tags },
{ "__index", luaA_default_index },
{ "__newindex", luaA_default_newindex },
{ NULL, NULL }
};
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80