forked from awesomeWM/awesome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
luaa.c
1354 lines (1182 loc) · 36.5 KB
/
luaa.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
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* luaa.c - Lua configuration 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.
*
*/
/** AwesomeWM lifecycle and low-level APIs.
*
* This module contains the functions and signal to manage the lifecycle of the
* AwesomeWM process. It allows to execute code at specific point from the early
* initialization all the way to the last events before exiting or restarting.
*
* Additionally it handles signals for spawn and keyboard related events.
*
* @author Julien Danjou <[email protected]>
* @copyright 2008-2009 Julien Danjou
* @coreclassmod awesome
*/
/** Register a new xproperty.
*
* @tparam string name The name of the X11 property.
* @tparam string type One of "string", "number" or "boolean".
* @noreturn
* @staticfct register_xproperty
*/
#define _GNU_SOURCE
#include "luaa.h"
#include "globalconf.h"
#include "awesome.h"
#include "common/backtrace.h"
#include "common/version.h"
#include "config.h"
#include "event.h"
#include "objects/client.h"
#include "objects/drawable.h"
#include "objects/drawin.h"
#include "objects/selection_getter.h"
#include "objects/screen.h"
#include "objects/selection_acquire.h"
#include "objects/selection_transfer.h"
#include "objects/selection_watcher.h"
#include "objects/tag.h"
#include "property.h"
#include "selection.h"
#include "spawn.h"
#include "systray.h"
#include "xkb.h"
#include "xrdb.h"
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
/* for strings and Unicode handling */
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <basedir_fs.h>
#include <xcb/xcb_atom.h>
#include <xcb/xcb_aux.h>
#include "xkb_utf32_to_keysym_compat.c"
#include <unistd.h> /* for gethostname() */
#ifdef WITH_DBUS
extern const struct luaL_Reg awesome_dbus_lib[];
#endif
extern const struct luaL_Reg awesome_keygrabber_lib[];
extern const struct luaL_Reg awesome_mousegrabber_lib[];
extern const struct luaL_Reg awesome_mouse_methods[];
extern const struct luaL_Reg awesome_mouse_meta[];
extern const struct luaL_Reg awesome_root_methods[];
extern const struct luaL_Reg awesome_root_meta[];
signal_array_t global_signals;
/** A call into the Lua code aborted with an error.
*
* This signal is used in the example configuration, @{05-awesomerc.md},
* to let a notification box pop up.
* @tparam table err Table with the error object, can be converted to a string with
* `tostring(err)`.
* @signal debug::error
*/
/** A deprecated Lua function was called.
*
* @tparam string hint String with a hint on what to use instead of the
* deprecated functionality.
* @tparam[opt=nil] string|nil see The name of the newer API
* @tparam[opt=nil] table|nil args The name of the newer API
* @signal debug::deprecation
*/
/** An invalid key was read from an object.
*
* This can happen if `foo` in an `c.foo` access does not exist.
* @param unknown1 Class?
* @param unknown2 Key?
* @signal debug::index::miss
*/
/** An invalid key was written to an object.
*
* This can happen if `foo` in an `c.foo = "bar"` assignment doesn't exist.
* @param unknown1 Class?
* @param unknown2 Key?
* @param unknown3 Value?
* @signal debug::newindex::miss
*/
/** The systray should be updated.
*
* This signal is used in `wibox.widget.systray`.
* @signal systray::update
*/
/** The wallpaper has changed.
*
* This signal is used for pseudo-transparency in `wibox.drawable` if no
* composite manager is running.
* @signal wallpaper_changed
*/
/** Keyboard map has changed.
*
* This signal is sent after the new keymap has been loaded. It is used in
* `awful.widget.keyboardlayout` to redraw the layout.
* @signal xkb::map_changed
*/
/** Keyboard group has changed.
*
* It's used in `awful.widget.keyboardlayout` to redraw the layout.
* @tparam number group Integer containing the changed group
* @signal xkb::group_changed.
*/
/** Refresh.
*
* This signal is emitted as a kind of idle signal in the event loop.
* One example usage is in `gears.timer` to executed delayed calls.
* @signal refresh
*/
/** AwesomeWM is about to enter the event loop.
*
* This means all initialization has been done.
* @signal startup
*/
/** AwesomeWM is exiting / about to restart.
*
* This signal is emitted in the `atexit` handler as well when awesome
* restarts.
* @tparam boolean reason_restart Boolean value is true if the signal was sent
* because of a restart.
* @signal exit
*/
/** The output status of a screen has changed.
*
* @tparam string output String containing which output has changed.
* @tparam string connection_state String containing the connection status of
* the output: It will be either "Connected", "Disconnected" or
* "Unknown".
* @signal screen::change
*/
/** Path to config file */
static char *conffile;
/** Check whether a composite manager is running.
* \return True if such a manager is running.
*/
static bool
composite_manager_running(void)
{
xcb_intern_atom_reply_t *atom_r;
xcb_get_selection_owner_reply_t *selection_r;
char *atom_name;
bool result;
if(!(atom_name = xcb_atom_name_by_screen("_NET_WM_CM", globalconf.default_screen)))
{
warn("error getting composite manager atom");
return false;
}
atom_r = xcb_intern_atom_reply(globalconf.connection,
xcb_intern_atom_unchecked(globalconf.connection, false,
a_strlen(atom_name), atom_name),
NULL);
p_delete(&atom_name);
if(!atom_r)
return false;
selection_r = xcb_get_selection_owner_reply(globalconf.connection,
xcb_get_selection_owner_unchecked(globalconf.connection,
atom_r->atom),
NULL);
p_delete(&atom_r);
result = selection_r != NULL && selection_r->owner != XCB_NONE;
p_delete(&selection_r);
return result;
}
/** Quit awesome.
* @tparam[opt=0] integer code The exit code to use when exiting.
* @staticfct quit
* @noreturn
*/
static int
luaA_quit(lua_State *L)
{
if (!lua_isnoneornil(L, 1))
globalconf.exit_code = luaL_checkinteger(L, 1);
if (globalconf.loop == NULL)
globalconf.loop = g_main_loop_new(NULL, FALSE);
g_main_loop_quit(globalconf.loop);
return 0;
}
/** Execute another application, probably a window manager, to replace
* awesome.
*
* @tparam string cmd The command line to execute.
* @staticfct exec
* @noreturn
*/
static int
luaA_exec(lua_State *L)
{
const char *cmd = luaL_checkstring(L, 1);
awesome_atexit(false);
a_exec(cmd);
return 0;
}
/** Restart awesome.
* @staticfct restart
* @noreturn
*/
static int
luaA_restart(lua_State *L)
{
awesome_restart();
return 0;
}
/** Send a signal to a process.
* @tparam integer pid Process identifier. 0 and negative values have special
* meaning. See `man 3 kill`.
* @tparam integer sig Signal number.
* See `awesome.unix_signal` for a list of signals.
* @treturn boolean true if the signal was successfully sent, else false
* @staticfct kill
*/
static int
luaA_kill(lua_State *L)
{
int pid = luaL_checknumber(L, 1);
int sig = luaA_checknumber_range(L, 2, 0, INT_MAX);
int result = kill(pid, sig);
lua_pushboolean(L, result == 0);
return 1;
}
/** Synchronize with the X11 server. This is needed in the test suite to avoid
* some race conditions. You should never need to use this function.
* @staticfct sync
* @noreturn
*/
static int
luaA_sync(lua_State *L)
{
xcb_aux_sync(globalconf.connection);
return 0;
}
/** Translate a GdkPixbuf to a cairo image surface..
*
* @param pixbuf The pixbuf as a light user datum.
* @param path The pixbuf origin path
* @treturn gears.surface A cairo surface as light user datum.
* @staticfct pixbuf_to_surface
*/
static int
luaA_pixbuf_to_surface(lua_State *L)
{
GdkPixbuf *pixbuf = (GdkPixbuf *) lua_touserdata(L, 1);
cairo_surface_t *surface = draw_surface_from_pixbuf(pixbuf);
/* lua has to make sure to free the ref or we have a leak */
lua_pushlightuserdata(L, surface);
return 1;
}
/** Load an image from a given path.
*
* @tparam string name The file name.
* @treturn gears.surface A cairo surface as light user datum.
* @treturn nil|string The error message, if any.
* @staticfct load_image
*/
static int
luaA_load_image(lua_State *L)
{
/* TODO: Deprecate this function, Lua can use GdkPixbuf directly plus
* awesome.pixbuf_to_surface
*/
GError *error = NULL;
const char *filename = luaL_checkstring(L, 1);
cairo_surface_t *surface = draw_load_image(L, filename, &error);
if (!surface) {
lua_pushnil(L);
lua_pushstring(L, error->message);
g_error_free(error);
return 2;
}
/* lua has to make sure to free the ref or we have a leak */
lua_pushlightuserdata(L, surface);
return 1;
}
/** Set the preferred size for client icons.
*
* The closest equal or bigger size is picked if present, otherwise the closest
* smaller size is picked. The default is 0 pixels, ie. the smallest icon.
*
* @tparam integer size The size of the icons in pixels.
* @staticfct set_preferred_icon_size
* @noreturn
*/
static int
luaA_set_preferred_icon_size(lua_State *L)
{
globalconf.preferred_icon_size = luaA_checkinteger_range(L, 1, 0, UINT32_MAX);
return 0;
}
/** UTF-8 aware string length computing.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_mbstrlen(lua_State *L)
{
const char *cmd = luaL_checkstring(L, 1);
lua_pushinteger(L, (ssize_t) mbstowcs(NULL, NONULL(cmd), 0));
return 1;
}
/** Enhanced type() function which recognize awesome objects.
* \param L The Lua VM state.
* \return The number of arguments pushed on the stack.
*/
static int
luaAe_type(lua_State *L)
{
luaL_checkany(L, 1);
lua_pushstring(L, luaA_typename(L, 1));
return 1;
}
/** Replace various standards Lua functions with our own.
* \param L The Lua VM state.
*/
static void
luaA_fixups(lua_State *L)
{
/* export string.wlen */
lua_getglobal(L, "string");
lua_pushcfunction(L, luaA_mbstrlen);
lua_setfield(L, -2, "wlen");
lua_pop(L, 1);
/* replace type */
lua_pushcfunction(L, luaAe_type);
lua_setglobal(L, "type");
}
static const char *
get_modifier_name(int map_index)
{
switch (map_index) {
case XCB_MAP_INDEX_SHIFT: return "Shift";
case XCB_MAP_INDEX_LOCK: return "Lock";
case XCB_MAP_INDEX_CONTROL: return "Control";
case XCB_MAP_INDEX_1: return "Mod1"; /* Alt */
case XCB_MAP_INDEX_2: return "Mod2";
case XCB_MAP_INDEX_3: return "Mod3";
case XCB_MAP_INDEX_4: return "Mod4";
case XCB_MAP_INDEX_5: return "Mod5";
}
return 0; /* \0 */
}
/* Helper function for luaA_get_key_name() below.
* Will return the UTF-32 codepoint IF AND ONLY IF the input is exactly one
* valid UTF-8 character. Otherwise, it will return zero.
*/
static uint32_t
one_utf8_to_utf32(const char* input, const size_t length) {
gunichar character = g_utf8_get_char_validated(input, length);
// Return 0 if there is more than one UTF-8 character:
if (g_unichar_to_utf8(character, NULL) != (gint)length)
return 0;
// Return 0 if the character is invalid.
if (character == (gunichar)-1 || character == (gunichar)-2)
return 0;
return character;
}
/* Get X11 keysym and a one-character representation from an Awesome keycode.
*
* A "one-character representation" is a single UTF-8 representing the typical
* output from that keysym in a text editor (e.g. " " for space, "ñ" for
* n_tilde, "Ā" for A_macron). It usually matches the main engraving of the key
* for level-0 symbols (but lowercase).
*
* Keycodes may be given in a string in any valid format for `awful.key`:
* "#" + keycode, the symkey name and the UTF-8 representation will all work.
*
* If no suitable keysym is found, or a malformed keycode is given as an
* argument, this function will return (nil, nil)
*
* @treturn[1] string keysym The keysym name
* @treturn[1] nil keysym If no valid keysym is found
* @treturn[2] string printsymbol The xkb_keysym_to_utf8 result
* @treturn[2] nil printsymbol If the keysym has no printable representation.
* @staticfct awful.keyboard.get_key_name
*/
static int
luaA_get_key_name(lua_State *L)
{
// check if argument is valid
if (lua_gettop(L) > 1 || lua_type(L, 1) != LUA_TSTRING)
{
return 0;
}
const char* input = luaL_checkstring(L, 1);
const xkb_keysym_t *keysyms;
xkb_keysym_t keysym = XKB_KEY_NoSymbol;
const size_t length = strlen(input);
uint32_t ucs;
/* Checking for the three possible syntaxes awful.key uses:
* 1: #keycode (#8 to #255, any other is invalid)
* 2: the symbol itself (the result of xkb_keysym_to_utf8, e.g. @ for at).
* 3: the keysym
*/
if (input[0] == '#' && input[1] != '\0' && length < 5) // syntax #1
{
int keycode_from_hash = atoi(input+1);
// We discard keycodes with invalid values:
const xcb_setup_t *setup = xcb_get_setup(globalconf.connection);
if (keycode_from_hash < setup->min_keycode ||
keycode_from_hash > setup->max_keycode)
return 0;
xkb_keycode_t keycode = (xkb_keycode_t) keycode_from_hash;
struct xkb_keymap *keymap = xkb_state_get_keymap(globalconf.xkb_state);
xkb_keymap_key_get_syms_by_level(keymap, keycode, 0, 0, &keysyms);
keysym = keysyms[0];
}
else if ((ucs = one_utf8_to_utf32(input, length)) > 0) //syntax #2
keysym = xkb_utf32_to_keysym_compat(ucs);
else //syntax #3
keysym = xkb_keysym_from_name(input, XKB_KEYSYM_NO_FLAGS);
if (keysym == XKB_KEY_NoSymbol)
return 0;
else
{
char *name = key_get_keysym_name(keysym);
lua_pushstring(L, name);
char utfname[8];
if (xkb_keysym_to_utf8(keysym, utfname, 8) > 0)
lua_pushstring(L, utfname);
else
return 1; // this will make the second returned value a nil
}
return 2;
}
/* Undocumented */
/*
* The table of keybindings modifiers.
*
* Each modifier has zero to many entries depending on the keyboard layout.
* For example, `Shift` usually both has a left and right variant. Each
* modifier entry has a `keysym` and `keycode` entry. For the US PC 105
* keyboard, it looks like:
*
* awesome.modifiers = {
* Shift = {
* {keycode = 50 , keysym = 'Shift_L' },
* {keycode = 62 , keysym = 'Shift_R' },
* },
* Lock = {},
* Control = {
* {keycode = 37 , keysym = 'Control_L' },
* {keycode = 105, keysym = 'Control_R' },
* },
* Mod1 = {
* {keycode = 64 , keysym = 'Alt_L' },
* {keycode = 108, keysym = 'Alt_R' },
* },
* Mod2 = {
* {keycode = 77 , keysym = 'Num_Lock' },
* },
* Mod3 = {},
* Mod4 = {
* {keycode = 133, keysym = 'Super_L' },
* {keycode = 134, keysym = 'Super_R' },
* },
* Mod5 = {
* {keycode = 203, keysym = 'Mode_switch'},
* },
* };
*
* @tfield table modifiers
* @tparam table modifiers.Shift The Shift modifiers.
* @tparam table modifiers.Lock The Lock modifiers.
* @tparam table modifiers.Control The Control modifiers.
* @tparam table modifiers.Mod1 The Mod1 (Alt) modifiers.
* @tparam table modifiers.Mod2 The Mod2 modifiers.
* @tparam table modifiers.Mod3 The Mod3 modifiers.
* @tparam table modifiers.Mod4 The Mod4 modifiers.
* @tparam table modifiers.Mod5 The Mod5 modifiers.
*/
/*
* Modifiers can change over time, given they are currently not tracked, just
* query them each time. Use with moderation.
*/
static int luaA_get_modifiers(lua_State *L)
{
xcb_get_modifier_mapping_reply_t *mods = xcb_get_modifier_mapping_reply(globalconf.connection,
xcb_get_modifier_mapping(globalconf.connection), NULL);
if (!mods)
return 0;
xcb_keycode_t *mappings = xcb_get_modifier_mapping_keycodes(mods);
struct xkb_keymap *keymap = xkb_state_get_keymap(globalconf.xkb_state);
lua_newtable(L);
/* This get the MAPPED modifiers, not all of them are */
for (int i = XCB_MAP_INDEX_SHIFT; i <= XCB_MAP_INDEX_5; i++) {
lua_pushstring(L, get_modifier_name(i));
lua_newtable(L);
for (int j = 0; j < mods->keycodes_per_modifier; j++) {
const xkb_keysym_t *keysyms;
const xcb_keycode_t key_code = mappings[i * mods->keycodes_per_modifier + j];
xkb_keymap_key_get_syms_by_level(keymap, key_code, 0, 0, &keysyms);
if (keysyms != NULL) {
/* The +1 because j starts at zero and Lua at 1 */
lua_pushinteger(L, j+1);
lua_newtable(L);
lua_pushstring(L, "keycode");
lua_pushinteger(L, key_code);
lua_settable(L, -3);
/* Technically it is possible to get multiple keysyms here,
* but... we just use the first one.
*/
lua_pushstring(L, "keysym");
char *string = key_get_keysym_name(keysyms[0]);
lua_pushstring(L, string);
p_delete(&string);
lua_settable(L, -3);
lua_settable(L, -3);
}
}
lua_settable(L, -3);
}
free(mods);
return 0;
}
/* Undocumented */
/*
* A table with the currently active modifier names.
*
* @tfield table _active_modifiers
*/
static int luaA_get_active_modifiers(lua_State *L)
{
int count = 1;
lua_newtable(L);
for (int i = XCB_MAP_INDEX_SHIFT; i <= XCB_MAP_INDEX_5; i++) {
const int active = xkb_state_mod_index_is_active (globalconf.xkb_state, i,
XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_EFFECTIVE
);
if (active) {
lua_pushstring(L, get_modifier_name(i));
lua_rawseti(L,-2, count++);
}
}
return 0;
}
/**
* The AwesomeWM version.
* @tfield string version
*/
/**
* The AwesomeWM release name.
* @tfield string release
*/
/**
* The AwesomeWM API level.
*
* By default, this matches the major version (first component of the version).
*
* API levels are used to allow newer version of AwesomeWM to alter the behavior
* and subset deprecated APIs. Using an older API level than the current major
* version allows to use legacy `rc.lua` with little porting. However, they won't
* be able to use all the new features. Attempting to use a newer feature along
* with an older API level is not and will not be supported, even if it almost
* works. Keeping up to date with the newer API levels is highly recommended.
*
* Going the other direction, setting an higher API level allows to take
* advantage of experimental feature. It will also be much harsher when it comes
* to deprecation. Setting the API level value beyond `current+3` will treat
* using APIs currently pending deprecation as fatal errors. All new code
* submitted to the upstream AwesomeWM codebase is forbidden to use deprecated
* APIs. Testing your patches with mode and the default config is recommended
* before submitting a patch.
*
* You can use the `-l` command line option or `api-level` modeline key to set
* the API level for your `rc.lua`. This setting is global and read only,
* individual modules cannot set their own API level.
*
* @tfield string api_level
*/
/**
* The configuration file which has been loaded.
* @tfield string conffile
*/
/**
* True if we are still in startup, false otherwise.
* @tfield boolean startup
*/
/**
* Error message for errors that occurred during
* startup.
* @tfield string startup_errors
*/
/**
* True if a composite manager is running.
* @tfield boolean composite_manager_running
*/
/**
* Table mapping between signal numbers and signal identifiers.
* @tfield table unix_signal
*/
/**
* The hostname of the computer on which we are running.
* @tfield string hostname
*/
/**
* The path where themes were installed to.
* @tfield string themes_path
*/
/**
* The path where icons were installed to.
* @tfield string icon_path
*/
static int
luaA_awesome_index(lua_State *L)
{
if(luaA_usemetatable(L, 1, 2))
return 1;
const char *buf = luaL_checkstring(L, 2);
if(A_STREQ(buf, "conffile"))
{
lua_pushstring(L, conffile);
return 1;
}
if(A_STREQ(buf, "version"))
{
lua_pushstring(L, awesome_version_string());
return 1;
}
if(A_STREQ(buf, "release"))
{
lua_pushstring(L, awesome_release_string());
return 1;
}
if(A_STREQ(buf, "api_level"))
{
lua_pushinteger(L, globalconf.api_level);
return 1;
}
if(A_STREQ(buf, "startup"))
{
lua_pushboolean(L, globalconf.loop == NULL);
return 1;
}
if(A_STREQ(buf, "_modifiers"))
{
luaA_get_modifiers(L);
return 1;
}
if(A_STREQ(buf, "_active_modifiers"))
{
luaA_get_active_modifiers(L);
return 1;
}
if(A_STREQ(buf, "startup_errors"))
{
if (globalconf.startup_errors.len == 0)
return 0;
lua_pushstring(L, globalconf.startup_errors.s);
return 1;
}
if(A_STREQ(buf, "composite_manager_running"))
{
lua_pushboolean(L, composite_manager_running());
return 1;
}
if(A_STREQ(buf, "hostname"))
{
/* No good way to handle failures... */
char hostname[256] = "";
gethostname(&hostname[0], countof(hostname));
hostname[countof(hostname) - 1] = '\0';
lua_pushstring(L, hostname);
return 1;
}
if(A_STREQ(buf, "themes_path"))
{
lua_pushliteral(L, AWESOME_THEMES_PATH);
return 1;
}
if(A_STREQ(buf, "icon_path"))
{
lua_pushliteral(L, AWESOME_ICON_PATH);
return 1;
}
return luaA_default_index(L);
}
/** Add a global signal.
*
* @tparam string name A string with the event name.
* @tparam function func The function to call.
* @staticfct connect_signal
* @noreturn
*/
static int
luaA_awesome_connect_signal(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
luaA_checkfunction(L, 2);
signal_connect(&global_signals, name, luaA_object_ref(L, 2));
return 0;
}
/** Remove a global signal.
*
* @tparam string name A string with the event name.
* @tparam function func The function to call.
* @staticfct disconnect_signal
* @noreturn
*/
static int
luaA_awesome_disconnect_signal(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
luaA_checkfunction(L, 2);
const void *func = lua_topointer(L, 2);
if (signal_disconnect(&global_signals, name, func))
luaA_object_unref(L, (void *) func);
return 0;
}
/** Emit a global signal.
*
* @tparam function name A string with the event name.
* @param ... The signal arguments.
* @staticfct emit_signal
* @noreturn
*/
static int
luaA_awesome_emit_signal(lua_State *L)
{
signal_object_emit(L, &global_signals, luaL_checkstring(L, 1), lua_gettop(L) - 1);
return 0;
}
static int
luaA_panic(lua_State *L)
{
warn("unprotected error in call to Lua API (%s)",
lua_tostring(L, -1));
buffer_t buf;
backtrace_get(&buf);
warn("dumping backtrace\n%s", buf.s);
warn("restarting awesome");
awesome_restart();
return 0;
}
#if LUA_VERSION_NUM >= 502
static const char *
luaA_tolstring(lua_State *L, int idx, size_t *len)
{
return luaL_tolstring(L, idx, len);
}
#else
static const char *
luaA_tolstring(lua_State *L, int idx, size_t *len)
{
/* Try using the metatable. If that fails, push the value itself onto
* the stack.
*/
if (!luaL_callmeta(L, idx, "__tostring"))
lua_pushvalue(L, idx);
switch (lua_type(L, -1)) {
case LUA_TSTRING:
lua_pushvalue(L, -1);
break;
case LUA_TBOOLEAN:
if (lua_toboolean(L, -1))
lua_pushliteral(L, "true");
else
lua_pushliteral(L, "false");
break;
case LUA_TNUMBER:
lua_pushfstring(L, "%f", lua_tonumber(L, -1));
break;
case LUA_TNIL:
lua_pushstring(L, "nil");
break;
default:
lua_pushfstring(L, "%s: %p",
lua_typename(L, lua_type(L, -1)),
lua_topointer(L, -1));
break;
}
lua_remove(L, -2);
return lua_tolstring(L, -1, len);
}
#endif
static int
luaA_dofunction_on_error(lua_State *L)
{
/* Convert error to string, to prevent a follow-up error with lua_concat. */
luaA_tolstring(L, -1, NULL);
/* duplicate string error */
lua_pushvalue(L, -1);
/* emit error signal */
signal_object_emit(L, &global_signals, "debug::error", 1);
if(!luaL_dostring(L, "return debug.traceback(\"error while running function!\", 3)"))
{
/* Move traceback before error */
lua_insert(L, -2);
/* Insert sentence */
lua_pushliteral(L, "\nerror: ");
/* Move it before error */
lua_insert(L, -2);
lua_concat(L, 3);
}
return 1;
}
static void
setup_awesome_signals(lua_State *L)
{
lua_getglobal(L, "awesome");
lua_pushstring(L, "unix_signal");
lua_newtable(L);
#define SETUP_SIGNAL(sig) \
do { \
/* Set awesome.unix_signal["SIGSTOP"] = 42 */ \
lua_pushinteger(L, sig); \
lua_setfield(L, -2, #sig); \
/* Set awesome.unix_signal[42] = "SIGSTOP" */ \
lua_pushinteger(L, sig); \
lua_pushstring(L, #sig); \
lua_settable(L, -3); \
} while (0)
/* Non-standard signals. These are first so that e.g. (on my system)
* signals[29] is SIGPOLL and not SIGIO (the value gets overwritten).
*/
#ifdef SIGIOT
SETUP_SIGNAL(SIGIOT);
#endif
#ifdef SIGEMT
SETUP_SIGNAL(SIGEMT);
#endif
#ifdef SIGSTKFLT
SETUP_SIGNAL(SIGSTKFLT);
#endif
#ifdef SIGIO
SETUP_SIGNAL(SIGIO);
#endif
#ifdef SIGCLD
SETUP_SIGNAL(SIGCLD);
#endif
#ifdef SIGPWR
SETUP_SIGNAL(SIGPWR);
#endif
#ifdef SIGINFO
SETUP_SIGNAL(SIGINFO);
#endif
#ifdef SIGLOST
SETUP_SIGNAL(SIGLOST);
#endif
#ifdef SIGWINCH
SETUP_SIGNAL(SIGWINCH);
#endif
#ifdef SIGUNUSED
SETUP_SIGNAL(SIGUNUSED);
#endif
/* POSIX.1-1990, according to man 7 signal */
SETUP_SIGNAL(SIGHUP);
SETUP_SIGNAL(SIGINT);
SETUP_SIGNAL(SIGQUIT);
SETUP_SIGNAL(SIGILL);
SETUP_SIGNAL(SIGABRT);
SETUP_SIGNAL(SIGFPE);
SETUP_SIGNAL(SIGKILL);
SETUP_SIGNAL(SIGSEGV);