forked from awesomeWM/awesome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluaa.c
More file actions
1283 lines (1133 loc) · 34.4 KB
/
Copy pathluaa.c
File metadata and controls
1283 lines (1133 loc) · 34.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
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
/*
* lua.c - Lua configuration management
*
* Copyright © 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 "common/util.h"
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <ev.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <xcb/xcb.h>
#include "awesome.h"
#include "awesome-version-internal.h"
#include "ewmh.h"
#include "config.h"
#include "luaa.h"
#include "tag.h"
#include "client.h"
#include "screen.h"
#include "event.h"
#include "titlebar.h"
#include "mouse.h"
#include "layouts/tile.h"
#include "common/socket.h"
#include "common/buffer.h"
extern awesome_t globalconf;
extern const struct luaL_reg awesome_hooks_lib[];
extern const struct luaL_reg awesome_keygrabber_lib[];
extern const struct luaL_reg awesome_button_methods[];
extern const struct luaL_reg awesome_button_meta[];
extern const struct luaL_reg awesome_image_methods[];
extern const struct luaL_reg awesome_image_meta[];
extern const struct luaL_reg awesome_mouse_methods[];
extern const struct luaL_reg awesome_mouse_meta[];
extern const struct luaL_reg awesome_screen_methods[];
extern const struct luaL_reg awesome_screen_meta[];
extern const struct luaL_reg awesome_client_methods[];
extern const struct luaL_reg awesome_client_meta[];
extern const struct luaL_reg awesome_titlebar_methods[];
extern const struct luaL_reg awesome_titlebar_meta[];
extern const struct luaL_reg awesome_tag_methods[];
extern const struct luaL_reg awesome_tag_meta[];
extern const struct luaL_reg awesome_widget_methods[];
extern const struct luaL_reg awesome_widget_meta[];
extern const struct luaL_reg awesome_statusbar_methods[];
extern const struct luaL_reg awesome_statusbar_meta[];
extern const struct luaL_reg awesome_wibox_methods[];
extern const struct luaL_reg awesome_wibox_meta[];
extern const struct luaL_reg awesome_keybinding_methods[];
extern const struct luaL_reg awesome_keybinding_meta[];
static struct sockaddr_un *addr;
static ev_io csio = { .fd = -1 };
#define XDG_CONFIG_HOME_DEFAULT "/.config"
/** Get or set global mouse bindings.
* This binding will be available when you'll click on root window.
* \param L The Lua VM state.
* \return The number of element pushed on stack.
* \luastack
* \lvalue A client.
* \lparam An array of mouse button bindings objects, or nothing.
* \return The array of mouse button bindings objects of this client.
*/
static int
luaA_buttons(lua_State *L)
{
button_array_t *buttons = &globalconf.buttons;
if(lua_gettop(L) == 1)
luaA_button_array_set(L, 1, buttons);
return luaA_button_array_get(L, buttons);
}
/** Quit awesome.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_quit(lua_State *L __attribute__ ((unused)))
{
ev_unloop(globalconf.loop, 1);
return 0;
}
/** Execute another application, probably a window manager, to replace
* awesome.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lparam The command line to execute.
*/
static int
luaA_exec(lua_State *L)
{
const char *cmd = luaL_checkstring(L, 1);
awesome_atexit();
a_exec(cmd);
return 0;
}
/** Restart awesome.
*/
static int
luaA_restart(lua_State *L __attribute__ ((unused)))
{
awesome_restart();
return 0;
}
/** Set default font. (DEPRECATED)
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lparam A string with a font name in Pango format.
*/
static int
luaA_font_set(lua_State *L)
{
deprecate(L, "awesome.font attribute");
if(lua_gettop(L) == 1)
{
const char *newfont = luaL_checkstring(L, 1);
draw_font_delete(&globalconf.font);
globalconf.font = draw_font_new(newfont);
}
char *font = pango_font_description_to_string(globalconf.font->desc);
lua_pushstring(L, font);
g_free(font);
return 0;
}
/** Set default colors (DEPRECATED).
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lparam A table with `fg' and `bg' elements, containing colors.
* \lreturn A table with `fg' and `bg' elements, containing colors.
*/
static int
luaA_colors(lua_State *L)
{
deprecate(L, "awesome.fg and awesome.bg attributes");
if(lua_gettop(L) == 1)
{
const char *buf;
size_t len;
int8_t colors_nbr = -1, i;
xcolor_init_request_t reqs[2];
luaA_checktable(L, 1);
if((buf = luaA_getopt_lstring(L, 1, "fg", NULL, &len)))
reqs[++colors_nbr] = xcolor_init_unchecked(&globalconf.colors.fg, buf, len);
if((buf = luaA_getopt_lstring(L, 1, "bg", NULL, &len)))
reqs[++colors_nbr] = xcolor_init_unchecked(&globalconf.colors.bg, buf, len);
for(i = 0; i <= colors_nbr; i++)
xcolor_init_reply(reqs[i]);
}
lua_newtable(L);
luaA_pushcolor(L, &globalconf.colors.fg);
lua_setfield(L, -2, "fg");
luaA_pushcolor(L, &globalconf.colors.bg);
lua_setfield(L, -2, "bg");
return 1;
}
/** Set default colors. (DEPRECATED)
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*
* \luastack
* \lparam A table with `fg' and `bg' elements, containing colors.
*/
static int
luaA_colors_set(lua_State *L)
{
return luaA_colors(L);
}
static void
luaA_openlib(lua_State *L, const char *name,
const struct luaL_reg methods[],
const struct luaL_reg meta[])
{
luaL_newmetatable(L, name); /* 1 */
lua_pushvalue(L, -1); /* dup metatable 2 */
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
luaL_register(L, NULL, meta); /* 1 */
luaL_register(L, name, methods); /* 2 */
lua_pushvalue(L, -1); /* dup self as metatable 3 */
lua_setmetatable(L, -2); /* set self as metatable 2 */
lua_pop(L, 2);
}
/** 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_pushnumber(L, mbstowcs(NULL, NONULL(cmd), 0));
return 1;
}
/** Overload standard Lua next function to use __next key on metatable.
* \param L The Lua VM state.
* \param The number of elements pushed on stack.
*/
static int
luaAe_next(lua_State *L)
{
if(luaL_getmetafield(L, 1, "__next"))
{
lua_insert(L, 1);
lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
return lua_gettop(L);
}
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 2);
if(lua_next(L, 1))
return 2;
lua_pushnil(L);
return 1;
}
/** Overload lua_next() function by using __next metatable field
* to get next elements.
* \param L The Lua VM stack.
* \param idx The index number of elements in stack.
* \return 1 if more elements to come, 0 otherwise.
*/
int
luaA_next(lua_State *L, int idx)
{
if(luaL_getmetafield(L, idx, "__next"))
{
/* if idx is relative, reduce it since we got __next */
if(idx < 0) idx--;
/* copy table and then move key */
lua_pushvalue(L, idx);
lua_pushvalue(L, -3);
lua_remove(L, -4);
lua_pcall(L, 2, 2, 0);
/* next returned nil, it's the end */
if(lua_isnil(L, -1))
{
/* remove nil */
lua_pop(L, 2);
return 0;
}
return 1;
}
else if(lua_istable(L, idx))
return lua_next(L, idx);
/* remove the key */
lua_pop(L, 1);
return 0;
}
/** Generic pairs function.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_generic_pairs(lua_State *L)
{
lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
lua_pushvalue(L, 1); /* state, */
lua_pushnil(L); /* and initial value */
return 3;
}
/** Overload standard pairs function to use __pairs field of metatables.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaAe_pairs(lua_State *L)
{
if(luaL_getmetafield(L, 1, "__pairs"))
{
lua_insert(L, 1);
lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
return lua_gettop(L);
}
luaL_checktype(L, 1, LUA_TTABLE);
return luaA_generic_pairs(L);
}
static int
luaA_ipairs_aux(lua_State *L)
{
int i = luaL_checkint(L, 2) + 1;
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushinteger(L, i);
lua_rawgeti(L, 1, i);
return (lua_isnil(L, -1)) ? 0 : 2;
}
/** Overload standard ipairs function to use __ipairs field of metatables.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaAe_ipairs(lua_State *L)
{
if(luaL_getmetafield(L, 1, "__ipairs"))
{
lua_insert(L, 1);
lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
return lua_gettop(L);
}
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushvalue(L, lua_upvalueindex(1));
lua_pushvalue(L, 1);
lua_pushinteger(L, 0); /* and initial value */
return 3;
}
/** Replace various standards Lua functions with our own.
* \param L The Lua VM state.
*/
static void
luaA_fixups(lua_State *L)
{
/* replace string.len */
lua_getglobal(L, "string");
lua_pushcfunction(L, luaA_mbstrlen);
lua_setfield(L, -2, "len");
lua_pop(L, 1);
/* replace next */
lua_pushliteral(L, "next");
lua_pushcfunction(L, luaAe_next);
lua_settable(L, LUA_GLOBALSINDEX);
/* replace pairs */
lua_pushliteral(L, "pairs");
lua_pushcfunction(L, luaAe_next);
lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
lua_settable(L, LUA_GLOBALSINDEX);
/* replace ipairs */
lua_pushliteral(L, "ipairs");
lua_pushcfunction(L, luaA_ipairs_aux);
lua_pushcclosure(L, luaAe_ipairs, 1);
lua_settable(L, LUA_GLOBALSINDEX);
}
/** __next function for wtable objects.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_wtable_next(lua_State *L)
{
/* upvalue 1 is content table */
if(lua_next(L, lua_upvalueindex(1)))
return 2;
lua_pushnil(L);
return 1;
}
/** __ipairs function for wtable objects.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_wtable_ipairs(lua_State *L)
{
/* push ipairs_aux */
lua_pushvalue(L, lua_upvalueindex(2));
/* push content table */
lua_pushvalue(L, lua_upvalueindex(1));
lua_pushinteger(L, 0); /* and initial value */
return 3;
}
/** Index function of wtable objects.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_wtable_index(lua_State *L)
{
size_t len;
const char *buf;
lua_pushvalue(L, 2);
/* check for size, waiting lua 5.2 and __len on tables */
if((buf = lua_tolstring(L, -1, &len)))
if(a_tokenize(buf, len) == A_TK_LEN)
{
lua_pushnumber(L, lua_objlen(L, lua_upvalueindex(1)));
return 1;
}
lua_pop(L, 1);
/* upvalue 1 is content table */
lua_rawget(L, lua_upvalueindex(1));
return 1;
}
/** Newndex function of wtable objects.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_wtable_newindex(lua_State *L)
{
bool invalid = false;
/* push key on top */
lua_pushvalue(L, 2);
/* get current key value in content table */
lua_rawget(L, lua_upvalueindex(1));
/* if value is a widget, notify change */
if(lua_istable(L, -1) || luaA_toudata(L, -1, "widget"))
invalid = true;
lua_pop(L, 1); /* remove value */
/* if new value is a widget or a table */
if(lua_istable(L, 3))
{
luaA_table2wtable(L);
invalid = true;
}
else if(!invalid && luaA_toudata(L, 3, "widget"))
invalid = true;
/* upvalue 1 is content table */
lua_rawset(L, lua_upvalueindex(1));
if(invalid)
luaA_wibox_invalidate_byitem(L, lua_topointer(L, 1));
return 0;
}
/** Convert the top element of the stack to a proxied wtable.
* \param L The Lua VM state.
*/
void
luaA_table2wtable(lua_State *L)
{
if(!lua_istable(L, -1))
return;
lua_newtable(L); /* create *real* content table */
lua_newtable(L); /* metatable */
lua_pushvalue(L, -2); /* copy content table */
lua_pushcfunction(L, luaA_ipairs_aux); /* push ipairs aux */
lua_pushcclosure(L, luaA_wtable_ipairs, 2);
lua_pushvalue(L, -3); /* copy content table */
lua_pushcclosure(L, luaA_wtable_next, 1); /* __next has the content table as upvalue */
lua_pushvalue(L, -4); /* copy content table */
lua_pushcclosure(L, luaA_wtable_index, 1); /* __index has the content table as upvalue */
lua_pushvalue(L, -5); /* copy content table */
lua_pushcclosure(L, luaA_wtable_newindex, 1); /* __newindex has the content table as upvalue */
/* set metatable field with just pushed closure */
lua_setfield(L, -5, "__newindex");
lua_setfield(L, -4, "__index");
lua_setfield(L, -3, "__next");
lua_setfield(L, -2, "__ipairs");
/* set metatable impossible to touch */
lua_pushliteral(L, "wtable");
lua_setfield(L, -2, "__metatable");
/* set new metatable on original table */
lua_setmetatable(L, -3);
/* initial key */
lua_pushnil(L);
/* go through original table */
while(lua_next(L, -3))
{
/* if convert value to wtable */
luaA_table2wtable(L);
/* copy key */
lua_pushvalue(L, -2);
/* move key before value */
lua_insert(L, -2);
/* set same value in content table */
lua_rawset(L, -4);
/* copy key */
lua_pushvalue(L, -1);
/* push the new value :-> */
lua_pushnil(L);
/* set orig[k] = nil */
lua_rawset(L, -5);
}
/* remove content table */
lua_pop(L, 1);
}
/** Look for an item: table, function, etc.
* \param L The Lua VM state.
* \param item The pointer item.
*/
bool
luaA_hasitem(lua_State *L, const void *item)
{
lua_pushnil(L);
while(luaA_next(L, -2))
{
if(lua_topointer(L, -1) == item)
{
/* remove value and key */
lua_pop(L, 2);
return true;
}
if(lua_istable(L, -1))
if(luaA_hasitem(L, item))
{
/* remove key and value */
lua_pop(L, 2);
return true;
}
/* remove value */
lua_pop(L, 1);
}
return false;
}
/** Check if a table is a loop. When using table as direct acyclic digram,
* this is useful.
* \param L The Lua VM state.
* \param idx The index of the table in the stack
* \return True if the table loops.
*/
bool
luaA_isloop(lua_State *L, int idx)
{
if(lua_istable(L, idx))
{
lua_pushvalue(L, idx); /* push table on top */
if(luaA_hasitem(L, lua_topointer(L, -1)))
{
lua_pop(L, 1); /* remove pushed table */
return true;
}
lua_pushnil(L);
while(luaA_next(L, -2))
{
/* check for recursivity */
if(luaA_isloop(L, -1))
{
lua_pop(L, 2); /* remove key and value */
return true;
}
lua_pop(L, 1); /* remove value */
}
lua_pop(L, 1); /* remove pushed table */
}
return false;
}
/** Object table.
* This table can use safely object as key.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
int
luaA_otable_index(lua_State *L)
{
void **obj, **v;
if((obj = lua_touserdata(L, 2)))
{
/* begins at nil */
lua_pushnil(L);
while(lua_next(L, 1))
{
/* check the key against the key if the key is a userdata,
* otherwise check it again the value. */
if((v = lua_touserdata(L, -2))
&& *v == *obj)
/* return value */
return 1;
else if((v = lua_touserdata(L, -1))
&& *v == *obj)
{
/* return key */
lua_pop(L, 1);
return 1;
}
/* removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 1);
}
return 0;
}
lua_rawget(L, 1);
return 1;
}
/** Object table.
* This table can use safely object as key.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_otable_newindex(lua_State *L)
{
void **obj, **v;
if((obj = lua_touserdata(L, 2)))
{
/* begins at nil */
lua_pushnil(L);
while(lua_next(L, 1))
{
if((v = lua_touserdata(L, -2))
&& *v == *obj)
{
/* remove value */
lua_pop(L, 1);
/* push new value on top */
lua_pushvalue(L, 3);
/* set in table key = value */
lua_rawset(L, 1);
return 0;
}
/* removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 1);
}
}
lua_rawset(L, 1);
return 0;
}
/** Spawn a program.
* This function is multi-head (Zaphod) aware and will set display to
* the right screen according to mouse position.
* \param L The Lua VM state.
* \return The number of elements pushed on stack
* \luastack
* \lparam The command to launch.
* \lparam The optional screen number to spawn the command on.
*/
static int
luaA_spawn(lua_State *L)
{
char *host, newdisplay[128];
const char *cmd;
int screen = 0, screenp, displayp;
if(lua_gettop(L) == 2)
{
screen = luaL_checknumber(L, 2) - 1;
luaA_checkscreen(screen);
}
cmd = luaL_checkstring(L, 1);
if(!globalconf.xinerama_is_active)
{
xcb_parse_display(NULL, &host, &displayp, &screenp);
snprintf(newdisplay, sizeof(newdisplay), "%s:%d.%d", host, displayp, screen);
setenv("DISPLAY", newdisplay, 1);
p_delete(&host);
}
/* The double-fork construct avoids zombie processes and keeps the code
* clean from stupid signal handlers. */
if(fork() == 0)
{
if(fork() == 0)
{
if(globalconf.connection)
xcb_disconnect(globalconf.connection);
setsid();
a_exec(cmd);
warn("execl '%s' failed: %s\n", cmd, strerror(errno));
}
exit(EXIT_SUCCESS);
}
wait(0);
return 0;
}
/** Deprecated function, does nothing.
*/
static int
luaA_mouse_add(lua_State *L)
{
deprecate(L, "awesome.buttons()");
return 0;
}
/** awesome global table.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lfield font The default font.
* \lfield conffile The configuration file which has been loaded.
*/
static int
luaA_awesome_index(lua_State *L)
{
if(luaA_usemetatable(L, 1, 2))
return 1;
size_t len;
const char *buf = luaL_checklstring(L, 2, &len);
switch(a_tokenize(buf, len))
{
case A_TK_FONT:
{
char *font = pango_font_description_to_string(globalconf.font->desc);
lua_pushstring(L, font);
g_free(font);
}
break;
case A_TK_CONFFILE:
lua_pushstring(L, globalconf.conffile);
break;
case A_TK_FG:
luaA_pushcolor(L, &globalconf.colors.fg);
break;
case A_TK_BG:
luaA_pushcolor(L, &globalconf.colors.bg);
break;
default:
return 0;
}
return 1;
}
/** Newindex function for the awesome global table.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_awesome_newindex(lua_State *L)
{
if(luaA_usemetatable(L, 1, 2))
return 1;
size_t len;
const char *buf = luaL_checklstring(L, 2, &len);
switch(a_tokenize(buf, len))
{
case A_TK_FONT:
{
const char *newfont = luaL_checkstring(L, 3);
draw_font_delete(&globalconf.font);
globalconf.font = draw_font_new(newfont);
}
break;
case A_TK_FG:
if((buf = luaL_checklstring(L, 3, &len)))
xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.fg, buf, len));
break;
case A_TK_BG:
if((buf = luaL_checklstring(L, 3, &len)))
xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.bg, buf, len));
break;
default:
return 0;
}
return 0;
}
/** Initialize the Lua VM
*/
void
luaA_init(void)
{
lua_State *L;
static const struct luaL_reg otable_methods[] =
{
{ "__call", luaA_otable_new },
{ NULL, NULL }
};
static const struct luaL_reg otable_meta[] =
{
{ "__index", luaA_otable_index },
{ "__newindex", luaA_otable_newindex },
{ NULL, NULL }
};
static const struct luaL_reg awesome_lib[] =
{
{ "quit", luaA_quit },
{ "exec", luaA_exec },
{ "spawn", luaA_spawn },
{ "restart", luaA_restart },
{ "buttons", luaA_buttons },
{ "font_set", luaA_font_set },
{ "colors_set", luaA_colors_set },
{ "colors", luaA_colors },
{ "__index", luaA_awesome_index },
{ "__newindex", luaA_awesome_newindex },
/* deprecated */
{ "mouse_add", luaA_mouse_add },
{ NULL, NULL }
};
L = globalconf.L = luaL_newstate();
luaL_openlibs(L);
luaA_fixups(L);
/* Export awesome lib */
luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
/* Export hooks lib */
luaL_register(L, "hooks", awesome_hooks_lib);
/* Export keygrabber lib */
luaL_register(L, "keygrabber", awesome_keygrabber_lib);
/* Export otable lib */
luaA_openlib(L, "otable", otable_methods, otable_meta);
/* Export screen */
luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
/* Export mouse */
luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
/* Export button */
luaA_openlib(L, "button", awesome_button_methods, awesome_button_meta);
/* Export image */
luaA_openlib(L, "image", awesome_image_methods, awesome_image_meta);
/* Export tag */
luaA_openlib(L, "tag", awesome_tag_methods, awesome_tag_meta);
/* Export statusbar */
luaA_openlib(L, "statusbar", awesome_statusbar_methods, awesome_statusbar_meta);
/* Export wibox */
luaA_openlib(L, "wibox", awesome_wibox_methods, awesome_wibox_meta);
/* Export widget */
luaA_openlib(L, "widget", awesome_widget_methods, awesome_widget_meta);
/* Export client */
luaA_openlib(L, "client", awesome_client_methods, awesome_client_meta);
/* Export titlebar */
luaA_openlib(L, "titlebar", awesome_titlebar_methods, awesome_titlebar_meta);
/* Export keys */
luaA_openlib(L, "keybinding", awesome_keybinding_methods, awesome_keybinding_meta);
lua_pushliteral(L, "AWESOME_VERSION");
lua_pushstring(L, AWESOME_VERSION);
lua_settable(L, LUA_GLOBALSINDEX);
lua_pushliteral(L, "AWESOME_RELEASE");
lua_pushstring(L, AWESOME_RELEASE);
lua_settable(L, LUA_GLOBALSINDEX);
/* init hooks */
globalconf.hooks.manage = LUA_REFNIL;
globalconf.hooks.unmanage = LUA_REFNIL;
globalconf.hooks.focus = LUA_REFNIL;
globalconf.hooks.unfocus = LUA_REFNIL;
globalconf.hooks.mouse_enter = LUA_REFNIL;
globalconf.hooks.arrange = LUA_REFNIL;
globalconf.hooks.clients = LUA_REFNIL;
globalconf.hooks.tags = LUA_REFNIL;
globalconf.hooks.tagged = LUA_REFNIL;
globalconf.hooks.property = LUA_REFNIL;
globalconf.hooks.timer = LUA_REFNIL;
/* add Lua lib path (/usr/share/awesome/lib by default) */
luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?.lua\"");
luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?/init.lua\"");
/* add XDG_CONFIG_DIR (/etc/xdg/awesome by default) as include path */
luaA_dostring(L, "package.path = package.path .. \";" XDG_CONFIG_DIR "/awesome/?.lua\"");
luaA_dostring(L, "package.path = package.path .. \";" XDG_CONFIG_DIR "/awesome/?/init.lua\"");
/* add either XDG_CONFIG_HOME/awesome or HOME/.config/awesome to path */
char *dir;
if((dir = getenv("XDG_CONFIG_HOME")))
{
char *path;
a_asprintf(&path, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"", dir, dir);
luaA_dostring(globalconf.L, path);
p_delete(&path);
}
else
{
char *path, *homedir = getenv("HOME");
a_asprintf(&path,
"package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?.lua;%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?/init.lua\"",
homedir, homedir);
luaA_dostring(globalconf.L, path);
p_delete(&path);
}
/* add XDG_CONFIG_DIRS to include paths */
char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
ssize_t len;
if((len = a_strlen(xdg_config_dirs)))
{
char **buf, **xdg_files = a_strsplit(xdg_config_dirs, len, ':');
for(buf = xdg_files; *buf; buf++)
{
char *confpath;
a_asprintf(&confpath, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"",
*buf, *buf);
luaA_dostring(globalconf.L, confpath);
p_delete(&confpath);
}
for(buf = xdg_files; *buf; buf++)
p_delete(buf);
p_delete(&xdg_files);
}
}
#define AWESOME_CONFIG_FILE "/awesome/rc.lua"
static bool
luaA_loadrc(const char *confpath, bool run)
{
if(confpath)
{
if(!luaL_loadfile(globalconf.L, confpath))
{
if(run)
{
if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
else
{
globalconf.conffile = a_strdup(confpath);
return true;
}
}
else
lua_pop(globalconf.L, 1);
return true;
}
else
fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
}
return false;
}