luaa: add support for conffile in index
[awesome.git] / luaa.c
blob95863300da6e932493660d328e6f113b1e7ef1b2
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 "common/util.h"
24 #include <errno.h>
25 #include <sys/socket.h>
26 #include <sys/un.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31 #include <fcntl.h>
33 #include <ev.h>
35 #include <lua.h>
36 #include <lauxlib.h>
37 #include <lualib.h>
39 #include <xcb/xcb.h>
41 #include "awesome.h"
42 #include "awesome-version-internal.h"
43 #include "ewmh.h"
44 #include "config.h"
45 #include "luaa.h"
46 #include "tag.h"
47 #include "client.h"
48 #include "screen.h"
49 #include "event.h"
50 #include "titlebar.h"
51 #include "mouse.h"
52 #include "layouts/tile.h"
53 #include "common/socket.h"
54 #include "common/buffer.h"
56 extern awesome_t globalconf;
58 extern const struct luaL_reg awesome_hooks_lib[];
59 extern const struct luaL_reg awesome_keygrabber_lib[];
60 extern const struct luaL_reg awesome_button_methods[];
61 extern const struct luaL_reg awesome_button_meta[];
62 extern const struct luaL_reg awesome_image_methods[];
63 extern const struct luaL_reg awesome_image_meta[];
64 extern const struct luaL_reg awesome_mouse_methods[];
65 extern const struct luaL_reg awesome_mouse_meta[];
66 extern const struct luaL_reg awesome_screen_methods[];
67 extern const struct luaL_reg awesome_screen_meta[];
68 extern const struct luaL_reg awesome_client_methods[];
69 extern const struct luaL_reg awesome_client_meta[];
70 extern const struct luaL_reg awesome_titlebar_methods[];
71 extern const struct luaL_reg awesome_titlebar_meta[];
72 extern const struct luaL_reg awesome_tag_methods[];
73 extern const struct luaL_reg awesome_tag_meta[];
74 extern const struct luaL_reg awesome_widget_methods[];
75 extern const struct luaL_reg awesome_widget_meta[];
76 extern const struct luaL_reg awesome_statusbar_methods[];
77 extern const struct luaL_reg awesome_statusbar_meta[];
78 extern const struct luaL_reg awesome_wibox_methods[];
79 extern const struct luaL_reg awesome_wibox_meta[];
80 extern const struct luaL_reg awesome_keybinding_methods[];
81 extern const struct luaL_reg awesome_keybinding_meta[];
83 static struct sockaddr_un *addr;
84 static ev_io csio = { .fd = -1 };
86 #define XDG_CONFIG_HOME_DEFAULT "/.config"
88 /** Get or set global mouse bindings.
89 * This binding will be available when you'll click on root window.
90 * \param L The Lua VM state.
91 * \return The number of element pushed on stack.
92 * \luastack
93 * \lvalue A client.
94 * \lparam An array of mouse button bindings objects, or nothing.
95 * \return The array of mouse button bindings objects of this client.
97 static int
98 luaA_buttons(lua_State *L)
100 button_array_t *buttons = &globalconf.buttons;
102 if(lua_gettop(L) == 1)
103 luaA_button_array_set(L, 1, buttons);
105 return luaA_button_array_get(L, buttons);
108 /** Quit awesome.
109 * \param L The Lua VM state.
110 * \return The number of elements pushed on stack.
112 static int
113 luaA_quit(lua_State *L __attribute__ ((unused)))
115 ev_unloop(globalconf.loop, 1);
116 return 0;
119 /** Execute another application, probably a window manager, to replace
120 * awesome.
121 * \param L The Lua VM state.
122 * \return The number of elements pushed on stack.
123 * \luastack
124 * \lparam The command line to execute.
126 static int
127 luaA_exec(lua_State *L)
129 const char *cmd = luaL_checkstring(L, 1);
131 awesome_atexit();
133 a_exec(cmd);
134 return 0;
137 /** Restart awesome.
139 static int
140 luaA_restart(lua_State *L __attribute__ ((unused)))
142 awesome_restart();
143 return 0;
146 /** Set default font. (DEPRECATED)
147 * \param L The Lua VM state.
148 * \return The number of elements pushed on stack.
149 * \luastack
150 * \lparam A string with a font name in Pango format.
152 static int
153 luaA_font_set(lua_State *L)
155 deprecate(L);
156 if(lua_gettop(L) == 1)
158 const char *newfont = luaL_checkstring(L, 1);
159 draw_font_delete(&globalconf.font);
160 globalconf.font = draw_font_new(newfont);
163 char *font = pango_font_description_to_string(globalconf.font->desc);
164 lua_pushstring(L, font);
165 g_free(font);
166 return 0;
169 /** Set default colors.
170 * \param L The Lua VM state.
171 * \return The number of elements pushed on stack.
172 * \luastack
173 * \lparam A table with `fg' and `bg' elements, containing colors.
174 * \lreturn A table with `fg' and `bg' elements, containing colors.
176 static int
177 luaA_colors(lua_State *L)
179 if(lua_gettop(L) == 1)
181 const char *buf;
182 size_t len;
183 int8_t colors_nbr = -1, i;
184 xcolor_init_request_t reqs[2];
186 luaA_checktable(L, 1);
188 if((buf = luaA_getopt_lstring(L, 1, "fg", NULL, &len)))
189 reqs[++colors_nbr] = xcolor_init_unchecked(&globalconf.colors.fg, buf, len);
191 if((buf = luaA_getopt_lstring(L, 1, "bg", NULL, &len)))
192 reqs[++colors_nbr] = xcolor_init_unchecked(&globalconf.colors.bg, buf, len);
194 for(i = 0; i <= colors_nbr; i++)
195 xcolor_init_reply(reqs[i]);
198 lua_newtable(L);
199 luaA_pushcolor(L, &globalconf.colors.fg);
200 lua_setfield(L, -2, "fg");
201 luaA_pushcolor(L, &globalconf.colors.bg);
202 lua_setfield(L, -2, "bg");
204 return 1;
207 /** Set default colors. (DEPRECATED)
208 * \param L The Lua VM state.
209 * \return The number of elements pushed on stack.
211 * \luastack
212 * \lparam A table with `fg' and `bg' elements, containing colors.
214 static int
215 luaA_colors_set(lua_State *L)
217 deprecate(L);
218 return luaA_colors(L);
221 static void
222 luaA_openlib(lua_State *L, const char *name,
223 const struct luaL_reg methods[],
224 const struct luaL_reg meta[])
226 luaL_newmetatable(L, name); /* 1 */
227 lua_pushvalue(L, -1); /* dup metatable 2 */
228 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
230 luaL_register(L, NULL, meta); /* 1 */
231 luaL_register(L, name, methods); /* 2 */
232 lua_pushvalue(L, -1); /* dup self as metatable 3 */
233 lua_setmetatable(L, -2); /* set self as metatable 2 */
234 lua_pop(L, 2);
237 /** UTF-8 aware string length computing.
238 * \param L The Lua VM state.
239 * \return The number of elements pushed on stack.
241 static int
242 luaA_mbstrlen(lua_State *L)
244 const char *cmd = luaL_checkstring(L, 1);
245 lua_pushnumber(L, mbstowcs(NULL, NONULL(cmd), 0));
246 return 1;
249 /** Overload standard Lua next function to use __next key on metatable.
250 * \param L The Lua VM state.
251 * \param The number of elements pushed on stack.
253 static int
254 luaAe_next(lua_State *L)
256 if(luaL_getmetafield(L, 1, "__next"))
258 lua_insert(L, 1);
259 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
260 return lua_gettop(L);
263 luaL_checktype(L, 1, LUA_TTABLE);
264 lua_settop(L, 2);
265 if(lua_next(L, 1))
266 return 2;
267 lua_pushnil(L);
268 return 1;
271 /** Overload lua_next() function by using __next metatable field
272 * to get next elements.
273 * \param L The Lua VM stack.
274 * \param idx The index number of elements in stack.
275 * \return 1 if more elements to come, 0 otherwise.
278 luaA_next(lua_State *L, int idx)
280 if(luaL_getmetafield(L, idx, "__next"))
282 /* if idx is relative, reduce it since we got __next */
283 if(idx < 0) idx--;
284 /* copy table and then move key */
285 lua_pushvalue(L, idx);
286 lua_pushvalue(L, -3);
287 lua_remove(L, -4);
288 lua_pcall(L, 2, 2, 0);
289 /* next returned nil, it's the end */
290 if(lua_isnil(L, -1))
292 /* remove nil */
293 lua_pop(L, 2);
294 return 0;
296 return 1;
299 return lua_next(L, idx);
302 /** Generic pairs function.
303 * \param L The Lua VM state.
304 * \return The number of elements pushed on stack.
306 static int
307 luaA_generic_pairs(lua_State *L)
309 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
310 lua_pushvalue(L, 1); /* state, */
311 lua_pushnil(L); /* and initial value */
312 return 3;
315 /** Overload standard pairs function to use __pairs field of metatables.
316 * \param L The Lua VM state.
317 * \return The number of elements pushed on stack.
319 static int
320 luaAe_pairs(lua_State *L)
322 if(luaL_getmetafield(L, 1, "__pairs"))
324 lua_insert(L, 1);
325 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
326 return lua_gettop(L);
329 luaL_checktype(L, 1, LUA_TTABLE);
330 return luaA_generic_pairs(L);
333 static int
334 luaA_ipairs_aux(lua_State *L)
336 int i = luaL_checkint(L, 2) + 1;
337 luaL_checktype(L, 1, LUA_TTABLE);
338 lua_pushinteger(L, i);
339 lua_rawgeti(L, 1, i);
340 return (lua_isnil(L, -1)) ? 0 : 2;
343 /** Overload standard ipairs function to use __ipairs field of metatables.
344 * \param L The Lua VM state.
345 * \return The number of elements pushed on stack.
347 static int
348 luaAe_ipairs(lua_State *L)
350 if(luaL_getmetafield(L, 1, "__ipairs"))
352 lua_insert(L, 1);
353 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
354 return lua_gettop(L);
357 luaL_checktype(L, 1, LUA_TTABLE);
358 lua_pushvalue(L, lua_upvalueindex(1));
359 lua_pushvalue(L, 1);
360 lua_pushinteger(L, 0); /* and initial value */
361 return 3;
364 /** Replace various standards Lua functions with our own.
365 * \param L The Lua VM state.
367 static void
368 luaA_fixups(lua_State *L)
370 /* replace string.len */
371 lua_getglobal(L, "string");
372 lua_pushcfunction(L, luaA_mbstrlen);
373 lua_setfield(L, -2, "len");
374 lua_pop(L, 1);
375 /* replace next */
376 lua_pushliteral(L, "next");
377 lua_pushcfunction(L, luaAe_next);
378 lua_settable(L, LUA_GLOBALSINDEX);
379 /* replace pairs */
380 lua_pushliteral(L, "pairs");
381 lua_pushcfunction(L, luaAe_next);
382 lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
383 lua_settable(L, LUA_GLOBALSINDEX);
384 /* replace ipairs */
385 lua_pushliteral(L, "ipairs");
386 lua_pushcfunction(L, luaA_ipairs_aux);
387 lua_pushcclosure(L, luaAe_ipairs, 1);
388 lua_settable(L, LUA_GLOBALSINDEX);
391 /** __next function for wtable objects.
392 * \param L The Lua VM state.
393 * \return The number of elements pushed on stack.
395 static int
396 luaA_wtable_next(lua_State *L)
398 /* upvalue 1 is content table */
399 if(lua_next(L, lua_upvalueindex(1)))
400 return 2;
401 lua_pushnil(L);
402 return 1;
405 /** __ipairs function for wtable objects.
406 * \param L The Lua VM state.
407 * \return The number of elements pushed on stack.
409 static int
410 luaA_wtable_ipairs(lua_State *L)
412 /* push ipairs_aux */
413 lua_pushvalue(L, lua_upvalueindex(2));
414 /* push content table */
415 lua_pushvalue(L, lua_upvalueindex(1));
416 lua_pushinteger(L, 0); /* and initial value */
417 return 3;
420 /** Index function of wtable objects.
421 * \param L The Lua VM state.
422 * \return The number of elements pushed on stack.
424 static int
425 luaA_wtable_index(lua_State *L)
427 size_t len;
428 const char *buf;
430 lua_pushvalue(L, 2);
431 /* check for size, waiting lua 5.2 and __len on tables */
432 if((buf = lua_tolstring(L, -1, &len)))
433 if(a_tokenize(buf, len) == A_TK_LEN)
435 lua_pushnumber(L, lua_objlen(L, lua_upvalueindex(1)));
436 return 1;
438 lua_pop(L, 1);
440 /* upvalue 1 is content table */
441 lua_rawget(L, lua_upvalueindex(1));
442 return 1;
445 /** Newndex function of wtable objects.
446 * \param L The Lua VM state.
447 * \return The number of elements pushed on stack.
449 static int
450 luaA_wtable_newindex(lua_State *L)
452 bool invalid = false;
454 /* push key on top */
455 lua_pushvalue(L, 2);
456 /* get current key value in content table */
457 lua_rawget(L, lua_upvalueindex(1));
458 /* if value is a widget, notify change */
459 if(lua_istable(L, -1) || luaA_toudata(L, -1, "widget"))
460 invalid = true;
462 lua_pop(L, 1); /* remove value */
464 /* if new value is a widget or a table */
465 if(lua_istable(L, 3))
467 luaA_table2wtable(L);
468 invalid = true;
470 else if(!invalid && luaA_toudata(L, 3, "widget"))
471 invalid = true;
473 /* upvalue 1 is content table */
474 lua_rawset(L, lua_upvalueindex(1));
476 if(invalid)
477 luaA_wibox_invalidate_byitem(L, lua_topointer(L, 1));
479 return 0;
482 /** Convert the top element of the stack to a proxied wtable.
483 * \param L The Lua VM state.
485 void
486 luaA_table2wtable(lua_State *L)
488 if(!lua_istable(L, -1))
489 return;
491 lua_newtable(L); /* create *real* content table */
492 lua_newtable(L); /* metatable */
493 lua_pushvalue(L, -2); /* copy content table */
494 lua_pushcfunction(L, luaA_ipairs_aux); /* push ipairs aux */
495 lua_pushcclosure(L, luaA_wtable_ipairs, 2);
496 lua_pushvalue(L, -3); /* copy content table */
497 lua_pushcclosure(L, luaA_wtable_next, 1); /* __next has the content table as upvalue */
498 lua_pushvalue(L, -4); /* copy content table */
499 lua_pushcclosure(L, luaA_wtable_index, 1); /* __index has the content table as upvalue */
500 lua_pushvalue(L, -5); /* copy content table */
501 lua_pushcclosure(L, luaA_wtable_newindex, 1); /* __newindex has the content table as upvalue */
502 /* set metatable field with just pushed closure */
503 lua_setfield(L, -5, "__newindex");
504 lua_setfield(L, -4, "__index");
505 lua_setfield(L, -3, "__next");
506 lua_setfield(L, -2, "__ipairs");
507 /* set metatable impossible to touch */
508 lua_pushliteral(L, "wtable");
509 lua_setfield(L, -2, "__metatable");
510 /* set new metatable on original table */
511 lua_setmetatable(L, -3);
513 /* initial key */
514 lua_pushnil(L);
515 /* go through original table */
516 while(lua_next(L, -3))
518 /* if convert value to wtable */
519 luaA_table2wtable(L);
520 /* copy key */
521 lua_pushvalue(L, -2);
522 /* move key before value */
523 lua_insert(L, -2);
524 /* set same value in content table */
525 lua_rawset(L, -4);
526 /* copy key */
527 lua_pushvalue(L, -1);
528 /* push the new value :-> */
529 lua_pushnil(L);
530 /* set orig[k] = nil */
531 lua_rawset(L, -5);
533 /* remove content table */
534 lua_pop(L, 1);
537 /** Look for an item: table, function, etc.
538 * \param L The Lua VM state.
539 * \param item The pointer item.
541 bool
542 luaA_hasitem(lua_State *L, const void *item)
544 lua_pushnil(L);
545 while(luaA_next(L, -2))
547 if(lua_topointer(L, -1) == item)
549 /* remove value and key */
550 lua_pop(L, 2);
551 return true;
553 if(lua_istable(L, -1))
554 if(luaA_hasitem(L, item))
556 /* remove key and value */
557 lua_pop(L, 2);
558 return true;
560 /* remove value */
561 lua_pop(L, 1);
563 return false;
566 /** Check if a table is a loop. When using table as direct acyclic digram,
567 * this is useful.
568 * \param L The Lua VM state.
569 * \param idx The index of the table in the stack
570 * \return True if the table loops.
572 bool
573 luaA_isloop(lua_State *L, int idx)
575 if(lua_istable(L, idx))
577 lua_pushvalue(L, idx); /* push table on top */
578 if(luaA_hasitem(L, lua_topointer(L, -1)))
580 lua_pop(L, 1); /* remove pushed table */
581 return true;
583 lua_pushnil(L);
584 while(luaA_next(L, -2))
586 /* check for recursivity */
587 if(luaA_isloop(L, -1))
589 lua_pop(L, 2); /* remove key and value */
590 return true;
592 lua_pop(L, 1); /* remove value */
594 lua_pop(L, 1); /* remove pushed table */
596 return false;
599 /** Object table.
600 * This table can use safely object as key.
601 * \param L The Lua VM state.
602 * \return The number of elements pushed on stack.
605 luaA_otable_index(lua_State *L)
607 void **obj, **v;
609 if((obj = lua_touserdata(L, 2)))
611 /* begins at nil */
612 lua_pushnil(L);
613 while(lua_next(L, 1))
615 /* check the key against the key if the key is a userdata,
616 * otherwise check it again the value. */
617 if((v = lua_touserdata(L, -2))
618 && *v == *obj)
619 /* return value */
620 return 1;
621 else if((v = lua_touserdata(L, -1))
622 && *v == *obj)
624 /* return key */
625 lua_pop(L, 1);
626 return 1;
628 /* removes 'value'; keeps 'key' for next iteration */
629 lua_pop(L, 1);
631 return 0;
634 lua_rawget(L, 1);
635 return 1;
638 /** Object table.
639 * This table can use safely object as key.
640 * \param L The Lua VM state.
641 * \return The number of elements pushed on stack.
643 static int
644 luaA_otable_newindex(lua_State *L)
646 void **obj, **v;
648 if((obj = lua_touserdata(L, 2)))
650 /* begins at nil */
651 lua_pushnil(L);
652 while(lua_next(L, 1))
654 if((v = lua_touserdata(L, -2))
655 && *v == *obj)
657 /* remove value */
658 lua_pop(L, 1);
659 /* push new value on top */
660 lua_pushvalue(L, 3);
661 /* set in table key = value */
662 lua_rawset(L, 1);
663 return 0;
665 /* removes 'value'; keeps 'key' for next iteration */
666 lua_pop(L, 1);
670 lua_rawset(L, 1);
672 return 0;
675 /** Spawn a program.
676 * This function is multi-head (Zaphod) aware and will set display to
677 * the right screen according to mouse position.
678 * \param L The Lua VM state.
679 * \return The number of elements pushed on stack
680 * \luastack
681 * \lparam The command to launch.
682 * \lparam The optional screen number to spawn the command on.
684 static int
685 luaA_spawn(lua_State *L)
687 char *host, newdisplay[128];
688 const char *cmd;
689 int screen = 0, screenp, displayp;
691 if(lua_gettop(L) == 2)
693 screen = luaL_checknumber(L, 2) - 1;
694 luaA_checkscreen(screen);
697 cmd = luaL_checkstring(L, 1);
699 if(!globalconf.xinerama_is_active)
701 xcb_parse_display(NULL, &host, &displayp, &screenp);
702 snprintf(newdisplay, sizeof(newdisplay), "%s:%d.%d", host, displayp, screen);
703 setenv("DISPLAY", newdisplay, 1);
704 p_delete(&host);
707 /* The double-fork construct avoids zombie processes and keeps the code
708 * clean from stupid signal handlers. */
709 if(fork() == 0)
711 if(fork() == 0)
713 if(globalconf.connection)
714 xcb_disconnect(globalconf.connection);
715 setsid();
716 a_exec(cmd);
717 warn("execl '%s' failed: %s\n", cmd, strerror(errno));
719 exit(EXIT_SUCCESS);
721 wait(0);
723 return 0;
726 /** Deprecated function, does nothing.
728 static int
729 luaA_mouse_add(lua_State *L)
731 deprecate(L);
732 return 0;
735 /** awesome global table.
736 * \param L The Lua VM state.
737 * \return The number of elements pushed on stack.
738 * \luastack
739 * \lfield font The default font.
740 * \lfield conffile The configuration file which has been loaded.
742 static int
743 luaA_awesome_index(lua_State *L)
745 if(luaA_usemetatable(L, 1, 2))
746 return 1;
748 size_t len;
749 const char *buf = luaL_checklstring(L, 2, &len);
751 switch(a_tokenize(buf, len))
753 case A_TK_FONT:
755 char *font = pango_font_description_to_string(globalconf.font->desc);
756 lua_pushstring(L, font);
757 g_free(font);
759 break;
760 case A_TK_CONFFILE:
761 lua_pushstring(L, globalconf.conffile);
762 break;
763 default:
764 return 0;
767 return 1;
770 /** Newindex function for the awesome global table.
771 * \param L The Lua VM state.
772 * \return The number of elements pushed on stack.
774 static int
775 luaA_awesome_newindex(lua_State *L)
777 if(luaA_usemetatable(L, 1, 2))
778 return 1;
780 size_t len;
781 const char *buf = luaL_checklstring(L, 2, &len);
783 switch(a_tokenize(buf, len))
785 case A_TK_FONT:
787 const char *newfont = luaL_checkstring(L, 3);
788 draw_font_delete(&globalconf.font);
789 globalconf.font = draw_font_new(newfont);
791 break;
792 default:
793 return 0;
796 return 0;
799 /** Initialize the Lua VM
801 void
802 luaA_init(void)
804 lua_State *L;
806 static const struct luaL_reg otable_methods[] =
808 { "__call", luaA_otable_new },
809 { NULL, NULL }
811 static const struct luaL_reg otable_meta[] =
813 { "__index", luaA_otable_index },
814 { "__newindex", luaA_otable_newindex },
815 { NULL, NULL }
817 static const struct luaL_reg awesome_lib[] =
819 { "quit", luaA_quit },
820 { "exec", luaA_exec },
821 { "spawn", luaA_spawn },
822 { "restart", luaA_restart },
823 { "buttons", luaA_buttons },
824 { "font_set", luaA_font_set },
825 { "colors_set", luaA_colors_set },
826 { "colors", luaA_colors },
827 { "__index", luaA_awesome_index },
828 { "__newindex", luaA_awesome_newindex },
829 /* deprecated */
830 { "mouse_add", luaA_mouse_add },
831 { NULL, NULL }
834 L = globalconf.L = luaL_newstate();
836 luaL_openlibs(L);
838 luaA_fixups(L);
840 /* Export awesome lib */
841 luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
843 /* Export hooks lib */
844 luaL_register(L, "hooks", awesome_hooks_lib);
846 /* Export keygrabber lib */
847 luaL_register(L, "keygrabber", awesome_keygrabber_lib);
849 /* Export otable lib */
850 luaA_openlib(L, "otable", otable_methods, otable_meta);
852 /* Export screen */
853 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
855 /* Export mouse */
856 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
858 /* Export button */
859 luaA_openlib(L, "button", awesome_button_methods, awesome_button_meta);
861 /* Export image */
862 luaA_openlib(L, "image", awesome_image_methods, awesome_image_meta);
864 /* Export tag */
865 luaA_openlib(L, "tag", awesome_tag_methods, awesome_tag_meta);
867 /* Export statusbar */
868 luaA_openlib(L, "statusbar", awesome_statusbar_methods, awesome_statusbar_meta);
870 /* Export wibox */
871 luaA_openlib(L, "wibox", awesome_wibox_methods, awesome_wibox_meta);
873 /* Export widget */
874 luaA_openlib(L, "widget", awesome_widget_methods, awesome_widget_meta);
876 /* Export client */
877 luaA_openlib(L, "client", awesome_client_methods, awesome_client_meta);
879 /* Export titlebar */
880 luaA_openlib(L, "titlebar", awesome_titlebar_methods, awesome_titlebar_meta);
882 /* Export keys */
883 luaA_openlib(L, "keybinding", awesome_keybinding_methods, awesome_keybinding_meta);
885 lua_pushliteral(L, "AWESOME_VERSION");
886 lua_pushstring(L, AWESOME_VERSION);
887 lua_settable(L, LUA_GLOBALSINDEX);
889 lua_pushliteral(L, "AWESOME_RELEASE");
890 lua_pushstring(L, AWESOME_RELEASE);
891 lua_settable(L, LUA_GLOBALSINDEX);
893 /* init hooks */
894 globalconf.hooks.manage = LUA_REFNIL;
895 globalconf.hooks.unmanage = LUA_REFNIL;
896 globalconf.hooks.focus = LUA_REFNIL;
897 globalconf.hooks.unfocus = LUA_REFNIL;
898 globalconf.hooks.mouse_enter = LUA_REFNIL;
899 globalconf.hooks.arrange = LUA_REFNIL;
900 globalconf.hooks.clients = LUA_REFNIL;
901 globalconf.hooks.tags = LUA_REFNIL;
902 globalconf.hooks.tagged = LUA_REFNIL;
903 globalconf.hooks.property = LUA_REFNIL;
904 globalconf.hooks.timer = LUA_REFNIL;
906 /* add Lua lib path (/usr/share/awesome/lib by default) */
907 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?.lua\"");
908 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?/init.lua\"");
910 /* add XDG_CONFIG_DIR (/etc/xdg/awesome by default) as include path */
911 luaA_dostring(L, "package.path = package.path .. \";" XDG_CONFIG_DIR "/awesome/?.lua\"");
912 luaA_dostring(L, "package.path = package.path .. \";" XDG_CONFIG_DIR "/awesome/?/init.lua\"");
914 /* add either XDG_CONFIG_HOME/awesome or HOME/.config/awesome to path */
915 char *dir;
916 if((dir = getenv("XDG_CONFIG_HOME")))
918 char *path;
919 a_asprintf(&path, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"", dir, dir);
920 luaA_dostring(globalconf.L, path);
921 p_delete(&path);
923 else
925 char *path, *homedir = getenv("HOME");
926 a_asprintf(&path,
927 "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?.lua;%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?/init.lua\"",
928 homedir, homedir);
929 luaA_dostring(globalconf.L, path);
930 p_delete(&path);
933 /* add XDG_CONFIG_DIRS to include paths */
934 char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
935 ssize_t len;
937 if((len = a_strlen(xdg_config_dirs)))
939 char **buf, **xdg_files = a_strsplit(xdg_config_dirs, len, ':');
941 for(buf = xdg_files; *buf; buf++)
943 char *confpath;
944 a_asprintf(&confpath, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"",
945 *buf, *buf);
946 luaA_dostring(globalconf.L, confpath);
947 p_delete(&confpath);
950 for(buf = xdg_files; *buf; buf++)
951 p_delete(buf);
952 p_delete(&xdg_files);
956 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
958 static bool
959 luaA_loadrc(const char *confpath, bool run)
961 if(confpath)
963 if(!luaL_loadfile(globalconf.L, confpath))
965 if(run)
967 if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
968 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
969 else
971 globalconf.conffile = a_strdup(confpath);
972 return true;
975 else
976 lua_pop(globalconf.L, 1);
977 return true;
979 else
980 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
982 return false;
985 /** Load a configuration file.
986 * \param confpatharg The configuration file to load.
987 * \param run Run the configuration file.
989 bool
990 luaA_parserc(const char *confpatharg, bool run)
992 int screen;
993 const char *confdir, *xdg_config_dirs;
994 char *confpath = NULL, **xdg_files = NULL, **buf;
995 ssize_t len;
996 bool ret = false;
998 /* try to load, return if it's ok */
999 if(luaA_loadrc(confpatharg, run))
1001 ret = true;
1002 goto bailout;
1005 if((confdir = getenv("XDG_CONFIG_HOME")))
1006 a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, confdir);
1007 else
1008 a_asprintf(&confpath, "%s" XDG_CONFIG_HOME_DEFAULT AWESOME_CONFIG_FILE, getenv("HOME"));
1010 /* try to run XDG_CONFIG_HOME/awesome/rc.lua */
1011 if(luaA_loadrc(confpath, run))
1013 ret = true;
1014 goto bailout;
1017 p_delete(&confpath);
1019 xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
1021 if(!(len = a_strlen(xdg_config_dirs)))
1023 xdg_config_dirs = XDG_CONFIG_DIR;
1024 len = sizeof(XDG_CONFIG_DIR) - 1;
1027 xdg_files = a_strsplit(xdg_config_dirs, len, ':');
1029 for(buf = xdg_files; *buf && !ret; buf++)
1031 a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, *buf);
1032 if(luaA_loadrc(confpath, run))
1034 ret = true;
1035 goto bailout;
1037 p_delete(&confpath);
1040 bailout:
1042 p_delete(&confpath);
1044 if(xdg_files)
1046 for(buf = xdg_files; *buf; buf++)
1047 p_delete(buf);
1048 p_delete(&xdg_files);
1051 /* Assure there's at least one tag */
1052 for(screen = 0; screen < globalconf.nscreen; screen++)
1053 if(!globalconf.screens[screen].tags.len)
1054 tag_append_to_screen(tag_new("default", sizeof("default") - 1, layout_tile, 0.5, 1, 0),
1055 &globalconf.screens[screen]);
1056 return ret;
1059 /** Parse a command.
1060 * \param cmd The buffer to parse.
1061 * \return the number of elements pushed on the stack by the last statement in cmd.
1062 * If there's an error, the message is pushed onto the stack and this returns 1.
1064 static int
1065 luaA_docmd(const char *cmd)
1067 char *p;
1068 int newtop, oldtop = lua_gettop(globalconf.L);
1070 while((p = strchr(cmd, '\n')))
1072 newtop = lua_gettop(globalconf.L);
1073 lua_pop(globalconf.L, newtop - oldtop);
1074 oldtop = newtop;
1076 *p = '\0';
1077 if (luaL_dostring(globalconf.L, cmd))
1079 warn("error executing Lua code: %s", lua_tostring(globalconf.L, -1));
1080 return 1;
1082 cmd = p + 1;
1084 return lua_gettop(globalconf.L) - oldtop;
1087 /** Pushes a Lua array containing the top n elements of the stack.
1088 * \param n The number of elements to put in the array.
1090 static void
1091 luaA_array(int n)
1093 int i;
1094 lua_createtable(globalconf.L, n, 0);
1095 lua_insert(globalconf.L, -n - 1);
1097 for (i = n; i > 0; i--)
1098 lua_rawseti(globalconf.L, -i - 1, i);
1101 /** Maps the top n elements of the stack to the result of
1102 * applying a function to that element.
1103 * \param n The number of elements to map.
1104 * \luastack
1105 * \lparam The function to map the elements by. This should be
1106 * at position -(n + 1).
1108 static void
1109 luaA_map(int n)
1111 int i;
1112 for (i = 0; i < n; i++)
1114 lua_pushvalue(globalconf.L, -n - 1); /* copy of the function */
1115 lua_pushvalue(globalconf.L, -n - 1); /* value to map */
1116 lua_pcall(globalconf.L, 1, 1, 0); /* call function */
1117 lua_remove(globalconf.L, -n - 1); /* remove old value */
1119 lua_remove(globalconf.L, -n - 1); /* remove function */
1122 static void luaA_conn_cleanup(EV_P_ ev_io *w)
1124 ev_ref(EV_DEFAULT_UC);
1125 ev_io_stop(EV_DEFAULT_UC_ w);
1126 if (close(w->fd))
1127 warn("error closing UNIX domain socket: %s", strerror(errno));
1128 p_delete(&w);
1131 static void
1132 luaA_cb(EV_P_ ev_io *w, int revents)
1134 char buf[1024];
1135 int r, els;
1136 const char *s;
1137 size_t len;
1139 switch(r = recv(w->fd, buf, sizeof(buf)-1, MSG_TRUNC))
1141 case -1:
1142 warn("error reading UNIX domain socket: %s", strerror(errno));
1143 case 0: /* 0 bytes are only transferred when the connection is closed */
1144 luaA_conn_cleanup(EV_DEFAULT_UC_ w);
1145 break;
1146 default:
1147 if(r >= ssizeof(buf))
1148 break;
1149 buf[r] = '\0';
1150 lua_getglobal(globalconf.L, "table");
1151 lua_getfield(globalconf.L, -1, "concat");
1152 lua_remove(globalconf.L, -2); /* remove table */
1154 lua_getglobal(globalconf.L, "tostring");
1155 els = luaA_docmd(buf);
1156 luaA_map(els); /* map results to strings */
1157 luaA_array(els); /* put strings in an array */
1159 lua_pushstring(globalconf.L, "\t");
1160 lua_pcall(globalconf.L, 2, 1, 0); /* concatenate results with tabs */
1162 s = lua_tolstring(globalconf.L, -1, &len);
1164 /* ignore ENOENT because the client may not read */
1165 if (send(w->fd, s, len, MSG_DONTWAIT) == -1)
1166 switch(errno)
1168 case ENOENT:
1169 case EAGAIN:
1170 break;
1171 default:
1172 warn("can't send back to client via domain socket: %s", strerror(errno));
1173 break;
1176 lua_pop(globalconf.L, 1); /* pop the string */
1178 awesome_refresh(globalconf.connection);
1182 static void
1183 luaA_conn_cb(EV_P_ ev_io *w, int revents)
1185 ev_io *csio_conn = p_new(ev_io, 1);
1186 int csfd = accept(w->fd, NULL, NULL);
1188 ev_io_init(csio_conn, &luaA_cb, csfd, EV_READ);
1189 ev_io_start(EV_DEFAULT_UC_ csio_conn);
1190 ev_unref(EV_DEFAULT_UC);
1193 void
1194 luaA_cs_init(void)
1196 int csfd = socket_getclient();
1198 if (csfd < 0 || fcntl(csfd, F_SETFD, FD_CLOEXEC) == -1)
1199 return;
1201 addr = socket_getaddr(getenv("DISPLAY"));
1203 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1205 if(errno == EADDRINUSE)
1207 if(unlink(addr->sun_path))
1208 warn("error unlinking existing file: %s", strerror(errno));
1209 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1210 return warn("error binding UNIX domain socket: %s", strerror(errno));
1212 else
1213 return warn("error binding UNIX domain socket: %s", strerror(errno));
1215 listen(csfd, 10);
1217 ev_io_init(&csio, &luaA_conn_cb, csfd, EV_READ);
1218 ev_io_start(EV_DEFAULT_UC_ &csio);
1219 ev_unref(EV_DEFAULT_UC);
1222 void
1223 luaA_cs_cleanup(void)
1225 if(csio.fd < 0)
1226 return;
1227 ev_ref(EV_DEFAULT_UC);
1228 ev_io_stop(EV_DEFAULT_UC_ &csio);
1229 if (close(csio.fd))
1230 warn("error closing UNIX domain socket: %s", strerror(errno));
1231 if(unlink(addr->sun_path))
1232 warn("error unlinking UNIX domain socket: %s", strerror(errno));
1233 p_delete(&addr);
1234 csio.fd = -1;
1237 void
1238 luaA_on_timer(EV_P_ ev_timer *w, int revents)
1240 luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0, 0);
1241 awesome_refresh(globalconf.connection);
1244 /** Push a color as a string onto the stack
1245 * \param L The Lua VM state.
1246 * \param c The color to push.
1247 * \return The number of elements pushed on stack.
1250 luaA_pushcolor(lua_State *L, const xcolor_t *c)
1252 uint8_t r = (unsigned)c->red * 0xff / 0xffff;
1253 uint8_t g = (unsigned)c->green * 0xff / 0xffff;
1254 uint8_t b = (unsigned)c->blue * 0xff / 0xffff;
1255 uint8_t a = (unsigned)c->alpha * 0xff / 0xffff;
1256 char s[10];
1257 int len;
1258 /* do not print alpha if it's full */
1259 if(a == 0xff)
1260 len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
1261 else
1262 len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
1263 lua_pushlstring(L, s, len);
1264 return 1;