luaa: add XDG_CONFIG_DIR as include path
[awesome.git] / luaa.c
blob92b32059f0492d6b3d8265d9faac166020c7fe59
1 /*
2 * lua.c - Lua configuration management
4 * Copyright © 2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <errno.h>
23 #include <stdio.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #include <fcntl.h>
32 #include <ev.h>
34 #include <lua.h>
35 #include <lauxlib.h>
36 #include <lualib.h>
38 #include <xcb/xcb.h>
40 #include "awesome.h"
41 #include "awesome-version-internal.h"
42 #include "ewmh.h"
43 #include "config.h"
44 #include "luaa.h"
45 #include "tag.h"
46 #include "client.h"
47 #include "screen.h"
48 #include "event.h"
49 #include "titlebar.h"
50 #include "mouse.h"
51 #include "layouts/tile.h"
52 #include "common/socket.h"
53 #include "common/buffer.h"
55 extern awesome_t globalconf;
57 extern const struct luaL_reg awesome_hooks_lib[];
58 extern const struct luaL_reg awesome_keygrabber_lib[];
59 extern const struct luaL_reg awesome_button_methods[];
60 extern const struct luaL_reg awesome_button_meta[];
61 extern const struct luaL_reg awesome_image_methods[];
62 extern const struct luaL_reg awesome_image_meta[];
63 extern const struct luaL_reg awesome_mouse_methods[];
64 extern const struct luaL_reg awesome_mouse_meta[];
65 extern const struct luaL_reg awesome_screen_methods[];
66 extern const struct luaL_reg awesome_screen_meta[];
67 extern const struct luaL_reg awesome_client_methods[];
68 extern const struct luaL_reg awesome_client_meta[];
69 extern const struct luaL_reg awesome_titlebar_methods[];
70 extern const struct luaL_reg awesome_titlebar_meta[];
71 extern const struct luaL_reg awesome_tag_methods[];
72 extern const struct luaL_reg awesome_tag_meta[];
73 extern const struct luaL_reg awesome_widget_methods[];
74 extern const struct luaL_reg awesome_widget_meta[];
75 extern const struct luaL_reg awesome_statusbar_methods[];
76 extern const struct luaL_reg awesome_statusbar_meta[];
77 extern const struct luaL_reg awesome_wibox_methods[];
78 extern const struct luaL_reg awesome_wibox_meta[];
79 extern const struct luaL_reg awesome_keybinding_methods[];
80 extern const struct luaL_reg awesome_keybinding_meta[];
82 static struct sockaddr_un *addr;
83 static ev_io csio = { .fd = -1 };
85 /** Get or set global mouse bindings.
86 * This binding will be available when you'll click on root window.
87 * \param L The Lua VM state.
88 * \return The number of element pushed on stack.
89 * \luastack
90 * \lvalue A client.
91 * \lparam An array of mouse button bindings objects, or nothing.
92 * \return The array of mouse button bindings objects of this client.
94 static int
95 luaA_buttons(lua_State *L)
97 button_array_t *buttons = &globalconf.buttons;
99 if(lua_gettop(L) == 1)
100 luaA_button_array_set(L, 1, buttons);
102 return luaA_button_array_get(L, buttons);
105 /** Quit awesome.
106 * \param L The Lua VM state.
107 * \return The number of elements pushed on stack.
109 static int
110 luaA_quit(lua_State *L __attribute__ ((unused)))
112 ev_unloop(globalconf.loop, 1);
113 return 0;
116 /** Execute another application, probably a window manager, to replace
117 * awesome.
118 * \param L The Lua VM state.
119 * \return The number of elements pushed on stack.
120 * \luastack
121 * \lparam The command line to execute.
123 static int
124 luaA_exec(lua_State *L)
126 const char *cmd = luaL_checkstring(L, 1);
128 awesome_atexit();
130 a_exec(cmd);
131 return 0;
134 /** Restart awesome.
136 static int
137 luaA_restart(lua_State *L __attribute__ ((unused)))
139 awesome_restart();
140 return 0;
143 /** Set or get default font. (DEPRECATED)
144 * \param L The Lua VM state.
145 * \return The number of elements pushed on stack.
146 * \luastack
147 * \lparam An optional string with a font name in Pango format.
148 * \lreturn The font used, in Pango format.
150 static int
151 luaA_font(lua_State *L)
153 char *font;
155 if(lua_gettop(L) == 1)
157 const char *newfont = luaL_checkstring(L, 1);
158 draw_font_delete(&globalconf.font);
159 globalconf.font = draw_font_new(newfont);
162 font = pango_font_description_to_string(globalconf.font->desc);
163 lua_pushstring(L, font);
164 g_free(font);
166 return 1;
169 /** Set default font. (DEPRECATED)
170 * \param L The Lua VM state.
171 * \return The number of elements pushed on stack.
172 * \luastack
173 * \lparam A string with a font name in Pango format.
175 static int
176 luaA_font_set(lua_State *L)
178 deprecate(L);
179 return luaA_font(L);
182 /** Get configuration file path used by awesome.
183 * \param L The Lua VM state.
184 * \return The number of elements pushed on stack.
185 * \luastack
186 * \lreturn The awesome configuration file path.
188 static int
189 luaA_conffile(lua_State *L)
191 lua_pushstring(L, globalconf.conffile);
192 return 1;
195 /** Set default colors.
196 * \param L The Lua VM state.
197 * \return The number of elements pushed on stack.
198 * \luastack
199 * \lparam A table with `fg' and `bg' elements, containing colors.
200 * \lreturn A table with `fg' and `bg' elements, containing colors.
202 static int
203 luaA_colors(lua_State *L)
205 if(lua_gettop(L) == 1)
207 const char *buf;
208 size_t len;
209 int8_t colors_nbr = -1, i;
210 xcolor_init_request_t reqs[2];
212 luaA_checktable(L, 1);
214 if((buf = luaA_getopt_lstring(L, 1, "fg", NULL, &len)))
215 reqs[++colors_nbr] = xcolor_init_unchecked(&globalconf.colors.fg, buf, len);
217 if((buf = luaA_getopt_lstring(L, 1, "bg", NULL, &len)))
218 reqs[++colors_nbr] = xcolor_init_unchecked(&globalconf.colors.bg, buf, len);
220 for(i = 0; i <= colors_nbr; i++)
221 xcolor_init_reply(reqs[i]);
224 lua_newtable(L);
225 luaA_pushcolor(L, &globalconf.colors.fg);
226 lua_setfield(L, -2, "fg");
227 luaA_pushcolor(L, &globalconf.colors.bg);
228 lua_setfield(L, -2, "bg");
230 return 1;
233 /** Set default colors. (DEPRECATED)
234 * \param L The Lua VM state.
235 * \return The number of elements pushed on stack.
237 * \luastack
238 * \lparam A table with `fg' and `bg' elements, containing colors.
240 static int
241 luaA_colors_set(lua_State *L)
243 deprecate(L);
244 return luaA_colors(L);
247 static void
248 luaA_openlib(lua_State *L, const char *name,
249 const struct luaL_reg methods[],
250 const struct luaL_reg meta[])
252 luaL_newmetatable(L, name); /* 1 */
253 lua_pushvalue(L, -1); /* dup metatable 2 */
254 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
256 luaL_register(L, NULL, meta); /* 1 */
257 luaL_register(L, name, methods); /* 2 */
258 lua_pushvalue(L, -1); /* dup self as metatable 3 */
259 lua_setmetatable(L, -2); /* set self as metatable 2 */
260 lua_pop(L, 2);
263 /** UTF-8 aware string length computing.
264 * \param L The Lua VM state.
265 * \return The number of elements pushed on stack.
267 static int
268 luaA_mbstrlen(lua_State *L)
270 const char *cmd = luaL_checkstring(L, 1);
271 lua_pushnumber(L, mbstowcs(NULL, NONULL(cmd), 0));
272 return 1;
275 /** Overload standard Lua next function to use __next key on metatable.
276 * \param L The Lua VM state.
277 * \param The number of elements pushed on stack.
279 static int
280 luaAe_next(lua_State *L)
282 if(luaL_getmetafield(L, 1, "__next"))
284 lua_insert(L, 1);
285 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
286 return lua_gettop(L);
289 luaL_checktype(L, 1, LUA_TTABLE);
290 lua_settop(L, 2);
291 if(lua_next(L, 1))
292 return 2;
293 lua_pushnil(L);
294 return 1;
297 /** Overload lua_next() function by using __next metatable field
298 * to get next elements.
299 * \param L The Lua VM stack.
300 * \param idx The index number of elements in stack.
301 * \return 1 if more elements to come, 0 otherwise.
304 luaA_next(lua_State *L, int idx)
306 if(luaL_getmetafield(L, idx, "__next"))
308 /* if idx is relative, reduce it since we got __next */
309 if(idx < 0) idx--;
310 /* copy table and then move key */
311 lua_pushvalue(L, idx);
312 lua_pushvalue(L, -3);
313 lua_remove(L, -4);
314 lua_pcall(L, 2, 2, 0);
315 /* next returned nil, it's the end */
316 if(lua_isnil(L, -1))
318 /* remove nil */
319 lua_pop(L, 2);
320 return 0;
322 return 1;
325 return lua_next(L, idx);
328 /** Generic pairs function.
329 * \param L The Lua VM state.
330 * \return The number of elements pushed on stack.
332 static int
333 luaA_generic_pairs(lua_State *L)
335 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
336 lua_pushvalue(L, 1); /* state, */
337 lua_pushnil(L); /* and initial value */
338 return 3;
341 /** Overload standard pairs function to use __pairs field of metatables.
342 * \param L The Lua VM state.
343 * \return The number of elements pushed on stack.
345 static int
346 luaAe_pairs(lua_State *L)
348 if(luaL_getmetafield(L, 1, "__pairs"))
350 lua_insert(L, 1);
351 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
352 return lua_gettop(L);
355 luaL_checktype(L, 1, LUA_TTABLE);
356 return luaA_generic_pairs(L);
359 static int
360 luaA_ipairs_aux(lua_State *L)
362 int i = luaL_checkint(L, 2) + 1;
363 luaL_checktype(L, 1, LUA_TTABLE);
364 lua_pushinteger(L, i);
365 lua_rawgeti(L, 1, i);
366 return (lua_isnil(L, -1)) ? 0 : 2;
369 /** Overload standard ipairs function to use __ipairs field of metatables.
370 * \param L The Lua VM state.
371 * \return The number of elements pushed on stack.
373 static int
374 luaAe_ipairs(lua_State *L)
376 if(luaL_getmetafield(L, 1, "__ipairs"))
378 lua_insert(L, 1);
379 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
380 return lua_gettop(L);
383 luaL_checktype(L, 1, LUA_TTABLE);
384 lua_pushvalue(L, lua_upvalueindex(1));
385 lua_pushvalue(L, 1);
386 lua_pushinteger(L, 0); /* and initial value */
387 return 3;
390 /** Replace various standards Lua functions with our own.
391 * \param L The Lua VM state.
393 static void
394 luaA_fixups(lua_State *L)
396 /* replace string.len */
397 lua_getglobal(L, "string");
398 lua_pushcfunction(L, luaA_mbstrlen);
399 lua_setfield(L, -2, "len");
400 lua_pop(L, 1);
401 /* replace next */
402 lua_pushliteral(L, "next");
403 lua_pushcfunction(L, luaAe_next);
404 lua_settable(L, LUA_GLOBALSINDEX);
405 /* replace pairs */
406 lua_pushliteral(L, "pairs");
407 lua_pushcfunction(L, luaAe_next);
408 lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
409 lua_settable(L, LUA_GLOBALSINDEX);
410 /* replace ipairs */
411 lua_pushliteral(L, "ipairs");
412 lua_pushcfunction(L, luaA_ipairs_aux);
413 lua_pushcclosure(L, luaAe_ipairs, 1);
414 lua_settable(L, LUA_GLOBALSINDEX);
417 /** __next function for wtable objects.
418 * \param L The Lua VM state.
419 * \return The number of elements pushed on stack.
421 static int
422 luaA_wtable_next(lua_State *L)
424 /* upvalue 1 is content table */
425 if(lua_next(L, lua_upvalueindex(1)))
426 return 2;
427 lua_pushnil(L);
428 return 1;
431 /** __ipairs function for wtable objects.
432 * \param L The Lua VM state.
433 * \return The number of elements pushed on stack.
435 static int
436 luaA_wtable_ipairs(lua_State *L)
438 /* push ipairs_aux */
439 lua_pushvalue(L, lua_upvalueindex(2));
440 /* push content table */
441 lua_pushvalue(L, lua_upvalueindex(1));
442 lua_pushinteger(L, 0); /* and initial value */
443 return 3;
446 /** Index function of wtable objects.
447 * \param L The Lua VM state.
448 * \return The number of elements pushed on stack.
450 static int
451 luaA_wtable_index(lua_State *L)
453 size_t len;
454 const char *buf;
456 lua_pushvalue(L, 2);
457 /* check for size, waiting lua 5.2 and __len on tables */
458 if((buf = lua_tolstring(L, -1, &len)))
459 if(a_tokenize(buf, len) == A_TK_LEN)
461 lua_pushnumber(L, lua_objlen(L, lua_upvalueindex(1)));
462 return 1;
464 lua_pop(L, 1);
466 /* upvalue 1 is content table */
467 lua_rawget(L, lua_upvalueindex(1));
468 return 1;
471 /** Newndex function of wtable objects.
472 * \param L The Lua VM state.
473 * \return The number of elements pushed on stack.
475 static int
476 luaA_wtable_newindex(lua_State *L)
478 bool invalid = false;
480 /* push key on top */
481 lua_pushvalue(L, 2);
482 /* get current key value in content table */
483 lua_rawget(L, lua_upvalueindex(1));
484 /* if value is a widget, notify change */
485 if(lua_istable(L, -1) || luaA_toudata(L, -1, "widget"))
486 invalid = true;
488 lua_pop(L, 1); /* remove value */
490 /* if new value is a widget or a table */
491 if(lua_istable(L, 3))
493 luaA_table2wtable(L);
494 invalid = true;
496 else if(!invalid && luaA_toudata(L, 3, "widget"))
497 invalid = true;
499 /* upvalue 1 is content table */
500 lua_rawset(L, lua_upvalueindex(1));
502 if(invalid)
503 luaA_wibox_invalidate_byitem(L, lua_topointer(L, 1));
505 return 0;
508 /** Convert the top element of the stack to a proxied wtable.
509 * \param L The Lua VM state.
511 void
512 luaA_table2wtable(lua_State *L)
514 if(!lua_istable(L, -1))
515 return;
517 lua_newtable(L); /* create *real* content table */
518 lua_newtable(L); /* metatable */
519 lua_pushvalue(L, -2); /* copy content table */
520 lua_pushcfunction(L, luaA_ipairs_aux); /* push ipairs aux */
521 lua_pushcclosure(L, luaA_wtable_ipairs, 2);
522 lua_pushvalue(L, -3); /* copy content table */
523 lua_pushcclosure(L, luaA_wtable_next, 1); /* __next has the content table as upvalue */
524 lua_pushvalue(L, -4); /* copy content table */
525 lua_pushcclosure(L, luaA_wtable_index, 1); /* __index has the content table as upvalue */
526 lua_pushvalue(L, -5); /* copy content table */
527 lua_pushcclosure(L, luaA_wtable_newindex, 1); /* __newindex has the content table as upvalue */
528 /* set metatable field with just pushed closure */
529 lua_setfield(L, -5, "__newindex");
530 lua_setfield(L, -4, "__index");
531 lua_setfield(L, -3, "__next");
532 lua_setfield(L, -2, "__ipairs");
533 /* set metatable impossible to touch */
534 lua_pushliteral(L, "wtable");
535 lua_setfield(L, -2, "__metatable");
536 /* set new metatable on original table */
537 lua_setmetatable(L, -3);
539 /* initial key */
540 lua_pushnil(L);
541 /* go through original table */
542 while(lua_next(L, -3))
544 /* if convert value to wtable */
545 luaA_table2wtable(L);
546 /* copy key */
547 lua_pushvalue(L, -2);
548 /* move key before value */
549 lua_insert(L, -2);
550 /* set same value in content table */
551 lua_rawset(L, -4);
552 /* copy key */
553 lua_pushvalue(L, -1);
554 /* push the new value :-> */
555 lua_pushnil(L);
556 /* set orig[k] = nil */
557 lua_rawset(L, -5);
559 /* remove content table */
560 lua_pop(L, 1);
563 /** Look for an item: table, function, etc.
564 * \param L The Lua VM state.
565 * \param item The pointer item.
567 bool
568 luaA_hasitem(lua_State *L, const void *item)
570 lua_pushnil(L);
571 while(luaA_next(L, -2))
573 if(lua_topointer(L, -1) == item)
575 /* remove value and key */
576 lua_pop(L, 2);
577 return true;
579 if(lua_istable(L, -1))
580 if(luaA_hasitem(L, item))
582 /* remove key and value */
583 lua_pop(L, 2);
584 return true;
586 /* remove value */
587 lua_pop(L, 1);
589 return false;
592 /** Check if a table is a loop. When using table as direct acyclic digram,
593 * this is useful.
594 * \param L The Lua VM state.
595 * \param idx The index of the table in the stack
596 * \return True if the table loops.
598 bool
599 luaA_isloop(lua_State *L, int idx)
601 if(lua_istable(L, idx))
603 lua_pushvalue(L, idx); /* push table on top */
604 if(luaA_hasitem(L, lua_topointer(L, -1)))
606 lua_pop(L, 1); /* remove pushed table */
607 return true;
609 lua_pushnil(L);
610 while(luaA_next(L, -2))
612 /* check for recursivity */
613 if(luaA_isloop(L, -1))
615 lua_pop(L, 2); /* remove key and value */
616 return true;
618 lua_pop(L, 1); /* remove value */
620 lua_pop(L, 1); /* remove pushed table */
622 return false;
625 /** Object table.
626 * This table can use safely object as key.
627 * \param L The Lua VM state.
628 * \return The number of elements pushed on stack.
631 luaA_otable_index(lua_State *L)
633 void **obj, **v;
635 if((obj = lua_touserdata(L, 2)))
637 /* begins at nil */
638 lua_pushnil(L);
639 while(lua_next(L, 1))
641 /* check the key against the key if the key is a userdata,
642 * otherwise check it again the value. */
643 if((v = lua_touserdata(L, -2))
644 && *v == *obj)
645 /* return value */
646 return 1;
647 else if((v = lua_touserdata(L, -1))
648 && *v == *obj)
650 /* return key */
651 lua_pop(L, 1);
652 return 1;
654 /* removes 'value'; keeps 'key' for next iteration */
655 lua_pop(L, 1);
657 return 0;
660 lua_rawget(L, 1);
661 return 1;
664 /** Object table.
665 * This table can use safely object as key.
666 * \param L The Lua VM state.
667 * \return The number of elements pushed on stack.
669 static int
670 luaA_otable_newindex(lua_State *L)
672 void **obj, **v;
674 if((obj = lua_touserdata(L, 2)))
676 /* begins at nil */
677 lua_pushnil(L);
678 while(lua_next(L, 1))
680 if((v = lua_touserdata(L, -2))
681 && *v == *obj)
683 /* remove value */
684 lua_pop(L, 1);
685 /* push new value on top */
686 lua_pushvalue(L, 3);
687 /* set in table key = value */
688 lua_rawset(L, 1);
689 return 0;
691 /* removes 'value'; keeps 'key' for next iteration */
692 lua_pop(L, 1);
696 lua_rawset(L, 1);
698 return 0;
701 /** Spawn a program.
702 * This function is multi-head (Zaphod) aware and will set display to
703 * the right screen according to mouse position.
704 * \param L The Lua VM state.
705 * \return The number of elements pushed on stack
706 * \luastack
707 * \lparam The command to launch.
708 * \lparam The optional screen number to spawn the command on.
710 static int
711 luaA_spawn(lua_State *L)
713 char *host, newdisplay[128];
714 const char *cmd;
715 int screen = 0, screenp, displayp;
717 if(lua_gettop(L) == 2)
719 screen = luaL_checknumber(L, 2) - 1;
720 luaA_checkscreen(screen);
723 cmd = luaL_checkstring(L, 1);
725 if(!globalconf.xinerama_is_active)
727 xcb_parse_display(NULL, &host, &displayp, &screenp);
728 snprintf(newdisplay, sizeof(newdisplay), "%s:%d.%d", host, displayp, screen);
729 setenv("DISPLAY", newdisplay, 1);
730 p_delete(&host);
733 /* The double-fork construct avoids zombie processes and keeps the code
734 * clean from stupid signal handlers. */
735 if(fork() == 0)
737 if(fork() == 0)
739 if(globalconf.connection)
740 xcb_disconnect(globalconf.connection);
741 setsid();
742 a_exec(cmd);
743 warn("execl '%s' failed: %s\n", cmd, strerror(errno));
745 exit(EXIT_SUCCESS);
747 wait(0);
749 return 0;
752 /** Deprecated function, does nothing.
754 static int
755 luaA_mouse_add(lua_State *L)
757 deprecate(L);
758 return 0;
761 /** Initialize the Lua VM
763 void
764 luaA_init(void)
766 lua_State *L;
768 static const struct luaL_reg otable_methods[] =
770 { "__call", luaA_otable_new },
771 { NULL, NULL }
773 static const struct luaL_reg otable_meta[] =
775 { "__index", luaA_otable_index },
776 { "__newindex", luaA_otable_newindex },
777 { NULL, NULL }
779 static const struct luaL_reg awesome_lib[] =
781 { "quit", luaA_quit },
782 { "exec", luaA_exec },
783 { "spawn", luaA_spawn },
784 { "restart", luaA_restart },
785 { "buttons", luaA_buttons },
786 { "font_set", luaA_font_set },
787 { "colors_set", luaA_colors_set },
788 { "font", luaA_font },
789 { "colors", luaA_colors },
790 { "conffile", luaA_conffile },
791 /* deprecated */
792 { "mouse_add", luaA_mouse_add },
793 { NULL, NULL }
796 L = globalconf.L = luaL_newstate();
798 luaL_openlibs(L);
800 luaA_fixups(L);
802 /* Export awesome lib */
803 luaL_register(L, "awesome", awesome_lib);
805 /* Export hooks lib */
806 luaL_register(L, "hooks", awesome_hooks_lib);
808 /* Export keygrabber lib */
809 luaL_register(L, "keygrabber", awesome_keygrabber_lib);
811 /* Export otable lib */
812 luaA_openlib(L, "otable", otable_methods, otable_meta);
814 /* Export screen */
815 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
817 /* Export mouse */
818 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
820 /* Export button */
821 luaA_openlib(L, "button", awesome_button_methods, awesome_button_meta);
823 /* Export image */
824 luaA_openlib(L, "image", awesome_image_methods, awesome_image_meta);
826 /* Export tag */
827 luaA_openlib(L, "tag", awesome_tag_methods, awesome_tag_meta);
829 /* Export statusbar */
830 luaA_openlib(L, "statusbar", awesome_statusbar_methods, awesome_statusbar_meta);
832 /* Export wibox */
833 luaA_openlib(L, "wibox", awesome_wibox_methods, awesome_wibox_meta);
835 /* Export widget */
836 luaA_openlib(L, "widget", awesome_widget_methods, awesome_widget_meta);
838 /* Export client */
839 luaA_openlib(L, "client", awesome_client_methods, awesome_client_meta);
841 /* Export titlebar */
842 luaA_openlib(L, "titlebar", awesome_titlebar_methods, awesome_titlebar_meta);
844 /* Export keys */
845 luaA_openlib(L, "keybinding", awesome_keybinding_methods, awesome_keybinding_meta);
847 lua_pushliteral(L, "AWESOME_VERSION");
848 lua_pushstring(L, AWESOME_VERSION);
849 lua_settable(L, LUA_GLOBALSINDEX);
851 lua_pushliteral(L, "AWESOME_RELEASE");
852 lua_pushstring(L, AWESOME_RELEASE);
853 lua_settable(L, LUA_GLOBALSINDEX);
855 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?.lua\"");
856 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?/init.lua\"");
858 /* init hooks */
859 globalconf.hooks.manage = LUA_REFNIL;
860 globalconf.hooks.unmanage = LUA_REFNIL;
861 globalconf.hooks.focus = LUA_REFNIL;
862 globalconf.hooks.unfocus = LUA_REFNIL;
863 globalconf.hooks.mouse_enter = LUA_REFNIL;
864 globalconf.hooks.arrange = LUA_REFNIL;
865 globalconf.hooks.clients = LUA_REFNIL;
866 globalconf.hooks.tags = LUA_REFNIL;
867 globalconf.hooks.tagged = LUA_REFNIL;
868 globalconf.hooks.property = LUA_REFNIL;
869 globalconf.hooks.timer = LUA_REFNIL;
872 #define XDG_CONFIG_HOME_DEFAULT "/.config"
874 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
876 static bool
877 luaA_loadrc(const char *confpath, bool run)
879 if(confpath)
881 if(!luaL_loadfile(globalconf.L, confpath))
883 if(run)
885 if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
886 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
887 else
889 globalconf.conffile = a_strdup(confpath);
890 return true;
893 else
894 lua_pop(globalconf.L, 1);
895 return true;
897 else
898 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
900 return false;
903 /** Load a configuration file.
904 * \param confpatharg The configuration file to load.
905 * \param run Run the configuration file.
907 bool
908 luaA_parserc(const char *confpatharg, bool run)
910 int screen;
911 const char *confdir, *xdg_config_dirs;
912 char *confpath = NULL, **xdg_files, **buf, path[1024];
913 ssize_t len;
914 bool ret;
916 ret = luaA_loadrc(confpatharg, run);
918 confdir = getenv("XDG_CONFIG_HOME");
920 if((len = a_strlen(confdir)))
922 len += sizeof(AWESOME_CONFIG_FILE);
923 confpath = p_new(char, len);
924 a_strcpy(confpath, len, confdir);
925 /* update package.path */
926 snprintf(path, sizeof(path), "package.path = package.path .. \";%s/awesome/?.lua\"", confdir);
927 luaA_dostring(globalconf.L, path);
929 else
931 confdir = getenv("HOME");
932 len = a_strlen(confdir) + sizeof(AWESOME_CONFIG_FILE)-1 + sizeof(XDG_CONFIG_HOME_DEFAULT);
933 confpath = p_new(char, len);
934 a_strcpy(confpath, len, confdir);
935 a_strcat(confpath, len, XDG_CONFIG_HOME_DEFAULT);
936 /* update package.path */
937 snprintf(path, sizeof(path), "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?.lua\"", confdir);
938 luaA_dostring(globalconf.L, path);
940 a_strcat(confpath, len, AWESOME_CONFIG_FILE);
942 if(!ret)
943 ret = luaA_loadrc(confpath, run);
945 xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
947 if(!(len = a_strlen(xdg_config_dirs)))
949 xdg_config_dirs = XDG_CONFIG_DIR;
950 len = sizeof(XDG_CONFIG_DIR) - 1;
952 else
954 /* add XDG_CONFIG_DIR (/etc/xdg/awesome by default) as include path */
955 snprintf(path, sizeof(path),
956 "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"",
957 XDG_CONFIG_DIR, XDG_CONFIG_DIR);
958 luaA_dostring(globalconf.L, path);
961 xdg_files = a_strsplit(xdg_config_dirs, len, ':');
963 for(buf = xdg_files; *buf; buf++)
965 p_delete(&confpath);
966 len = a_strlen(*buf) + sizeof("AWESOME_CONFIG_FILE");
967 confpath = p_new(char, len);
968 a_strcpy(confpath, len, *buf);
969 a_strcat(confpath, len, AWESOME_CONFIG_FILE);
970 snprintf(path, sizeof(path),
971 "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"",
972 *buf, *buf);
973 luaA_dostring(globalconf.L, path);
974 if(!ret)
975 ret = luaA_loadrc(confpath, run);
978 for(buf = xdg_files; *buf; buf++)
979 p_delete(buf);
980 p_delete(&xdg_files);
982 /* Assure there's at least one tag */
983 for(screen = 0; screen < globalconf.nscreen; screen++)
984 if(!globalconf.screens[screen].tags.len)
985 tag_append_to_screen(tag_new("default", sizeof("default")-1, layout_tile, 0.5, 1, 0),
986 &globalconf.screens[screen]);
988 p_delete(&confpath);
990 return ret;
993 /** Parse a command.
994 * \param cmd The buffer to parse.
995 * \return the number of elements pushed on the stack by the last statement in cmd.
996 * If there's an error, the message is pushed onto the stack and this returns 1.
998 static int
999 luaA_docmd(const char *cmd)
1001 char *p;
1002 int newtop, oldtop = lua_gettop(globalconf.L);
1004 while((p = strchr(cmd, '\n')))
1006 newtop = lua_gettop(globalconf.L);
1007 lua_pop(globalconf.L, newtop - oldtop);
1008 oldtop = newtop;
1010 *p = '\0';
1011 if (luaL_dostring(globalconf.L, cmd))
1013 warn("error executing Lua code: %s", lua_tostring(globalconf.L, -1));
1014 return 1;
1016 cmd = p + 1;
1018 return lua_gettop(globalconf.L) - oldtop;
1021 /** Pushes a Lua array containing the top n elements of the stack.
1022 * \param n The number of elements to put in the array.
1024 static void
1025 luaA_array(int n)
1027 int i;
1028 lua_createtable(globalconf.L, n, 0);
1029 lua_insert(globalconf.L, -n - 1);
1031 for (i = n; i > 0; i--)
1032 lua_rawseti(globalconf.L, -i - 1, i);
1035 /** Maps the top n elements of the stack to the result of
1036 * applying a function to that element.
1037 * \param n The number of elements to map.
1038 * \luastack
1039 * \lparam The function to map the elements by. This should be
1040 * at position -(n + 1).
1042 static void
1043 luaA_map(int n)
1045 int i;
1046 for (i = 0; i < n; i++)
1048 lua_pushvalue(globalconf.L, -n - 1); /* copy of the function */
1049 lua_pushvalue(globalconf.L, -n - 1); /* value to map */
1050 lua_pcall(globalconf.L, 1, 1, 0); /* call function */
1051 lua_remove(globalconf.L, -n - 1); /* remove old value */
1053 lua_remove(globalconf.L, -n - 1); /* remove function */
1056 static void luaA_conn_cleanup(EV_P_ ev_io *w)
1058 ev_ref(EV_DEFAULT_UC);
1059 ev_io_stop(EV_DEFAULT_UC_ w);
1060 if (close(w->fd))
1061 warn("error closing UNIX domain socket: %s", strerror(errno));
1062 p_delete(&w);
1065 static void
1066 luaA_cb(EV_P_ ev_io *w, int revents)
1068 char buf[1024];
1069 int r, els;
1070 const char *s;
1071 size_t len;
1073 switch(r = recv(w->fd, buf, sizeof(buf)-1, MSG_TRUNC))
1075 case -1:
1076 warn("error reading UNIX domain socket: %s", strerror(errno));
1077 case 0: /* 0 bytes are only transferred when the connection is closed */
1078 luaA_conn_cleanup(EV_DEFAULT_UC_ w);
1079 break;
1080 default:
1081 if(r >= ssizeof(buf))
1082 break;
1083 buf[r] = '\0';
1084 lua_getglobal(globalconf.L, "table");
1085 lua_getfield(globalconf.L, -1, "concat");
1086 lua_remove(globalconf.L, -2); /* remove table */
1088 lua_getglobal(globalconf.L, "tostring");
1089 els = luaA_docmd(buf);
1090 luaA_map(els); /* map results to strings */
1091 luaA_array(els); /* put strings in an array */
1093 lua_pushstring(globalconf.L, "\t");
1094 lua_pcall(globalconf.L, 2, 1, 0); /* concatenate results with tabs */
1096 s = lua_tolstring(globalconf.L, -1, &len);
1098 /* ignore ENOENT because the client may not read */
1099 if (send(w->fd, s, len, MSG_DONTWAIT) == -1)
1100 switch(errno)
1102 case ENOENT:
1103 case EAGAIN:
1104 break;
1105 default:
1106 warn("can't send back to client via domain socket: %s", strerror(errno));
1107 break;
1110 lua_pop(globalconf.L, 1); /* pop the string */
1112 awesome_refresh(globalconf.connection);
1116 static void
1117 luaA_conn_cb(EV_P_ ev_io *w, int revents)
1119 ev_io *csio_conn = p_new(ev_io, 1);
1120 int csfd = accept(w->fd, NULL, NULL);
1122 ev_io_init(csio_conn, &luaA_cb, csfd, EV_READ);
1123 ev_io_start(EV_DEFAULT_UC_ csio_conn);
1124 ev_unref(EV_DEFAULT_UC);
1127 void
1128 luaA_cs_init(void)
1130 int csfd = socket_getclient();
1132 if (csfd < 0 || fcntl(csfd, F_SETFD, FD_CLOEXEC) == -1)
1133 return;
1135 addr = socket_getaddr(getenv("DISPLAY"));
1137 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1139 if(errno == EADDRINUSE)
1141 if(unlink(addr->sun_path))
1142 warn("error unlinking existing file: %s", strerror(errno));
1143 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1144 return warn("error binding UNIX domain socket: %s", strerror(errno));
1146 else
1147 return warn("error binding UNIX domain socket: %s", strerror(errno));
1149 listen(csfd, 10);
1151 ev_io_init(&csio, &luaA_conn_cb, csfd, EV_READ);
1152 ev_io_start(EV_DEFAULT_UC_ &csio);
1153 ev_unref(EV_DEFAULT_UC);
1156 void
1157 luaA_cs_cleanup(void)
1159 if(csio.fd < 0)
1160 return;
1161 ev_ref(EV_DEFAULT_UC);
1162 ev_io_stop(EV_DEFAULT_UC_ &csio);
1163 if (close(csio.fd))
1164 warn("error closing UNIX domain socket: %s", strerror(errno));
1165 if(unlink(addr->sun_path))
1166 warn("error unlinking UNIX domain socket: %s", strerror(errno));
1167 p_delete(&addr);
1168 csio.fd = -1;
1171 void
1172 luaA_on_timer(EV_P_ ev_timer *w, int revents)
1174 luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0, 0);
1175 awesome_refresh(globalconf.connection);
1178 /** Push a color as a string onto the stack
1179 * \param L The Lua VM state.
1180 * \param c The color to push.
1181 * \return The number of elements pushed on stack.
1184 luaA_pushcolor(lua_State *L, const xcolor_t *c)
1186 uint8_t r = (unsigned)c->red * 0xff / 0xffff;
1187 uint8_t g = (unsigned)c->green * 0xff / 0xffff;
1188 uint8_t b = (unsigned)c->blue * 0xff / 0xffff;
1189 uint8_t a = (unsigned)c->alpha * 0xff / 0xffff;
1190 char s[10];
1191 int len;
1192 /* do not print alpha if it's full */
1193 if(a == 0xff)
1194 len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
1195 else
1196 len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
1197 lua_pushlstring(L, s, len);
1198 return 1;