awful.widget: beautiful font for {tag,task}list
[awesome.git] / luaa.c
blob2c736c6628cd4a62faf23364b5e77ba7776ab5c9
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 "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_dbus_lib[];
59 extern const struct luaL_reg awesome_keygrabber_lib[];
60 extern const struct luaL_reg awesome_mousegrabber_lib[];
61 extern const struct luaL_reg awesome_button_methods[];
62 extern const struct luaL_reg awesome_button_meta[];
63 extern const struct luaL_reg awesome_image_methods[];
64 extern const struct luaL_reg awesome_image_meta[];
65 extern const struct luaL_reg awesome_mouse_methods[];
66 extern const struct luaL_reg awesome_mouse_meta[];
67 extern const struct luaL_reg awesome_screen_methods[];
68 extern const struct luaL_reg awesome_screen_meta[];
69 extern const struct luaL_reg awesome_client_methods[];
70 extern const struct luaL_reg awesome_client_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_wibox_methods[];
76 extern const struct luaL_reg awesome_wibox_meta[];
77 extern const struct luaL_reg awesome_keybinding_methods[];
78 extern const struct luaL_reg awesome_keybinding_meta[];
80 static struct sockaddr_un *addr;
81 static ev_io csio = { .fd = -1 };
83 #define XDG_CONFIG_HOME_DEFAULT "/.config"
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 static void
144 luaA_openlib(lua_State *L, const char *name,
145 const struct luaL_reg methods[],
146 const struct luaL_reg meta[])
148 luaL_newmetatable(L, name); /* 1 */
149 lua_pushvalue(L, -1); /* dup metatable 2 */
150 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
152 luaL_register(L, NULL, meta); /* 1 */
153 luaL_register(L, name, methods); /* 2 */
154 lua_pushvalue(L, -1); /* dup self as metatable 3 */
155 lua_setmetatable(L, -2); /* set self as metatable 2 */
156 lua_pop(L, 2);
159 /** UTF-8 aware string length computing.
160 * \param L The Lua VM state.
161 * \return The number of elements pushed on stack.
163 static int
164 luaA_mbstrlen(lua_State *L)
166 const char *cmd = luaL_checkstring(L, 1);
167 lua_pushnumber(L, mbstowcs(NULL, NONULL(cmd), 0));
168 return 1;
171 /** Overload standard Lua next function to use __next key on metatable.
172 * \param L The Lua VM state.
173 * \param The number of elements pushed on stack.
175 static int
176 luaAe_next(lua_State *L)
178 if(luaL_getmetafield(L, 1, "__next"))
180 lua_insert(L, 1);
181 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
182 return lua_gettop(L);
185 luaL_checktype(L, 1, LUA_TTABLE);
186 lua_settop(L, 2);
187 if(lua_next(L, 1))
188 return 2;
189 lua_pushnil(L);
190 return 1;
193 /** Overload lua_next() function by using __next metatable field
194 * to get next elements.
195 * \param L The Lua VM stack.
196 * \param idx The index number of elements in stack.
197 * \return 1 if more elements to come, 0 otherwise.
200 luaA_next(lua_State *L, int idx)
202 if(luaL_getmetafield(L, idx, "__next"))
204 /* if idx is relative, reduce it since we got __next */
205 if(idx < 0) idx--;
206 /* copy table and then move key */
207 lua_pushvalue(L, idx);
208 lua_pushvalue(L, -3);
209 lua_remove(L, -4);
210 lua_pcall(L, 2, 2, 0);
211 /* next returned nil, it's the end */
212 if(lua_isnil(L, -1))
214 /* remove nil */
215 lua_pop(L, 2);
216 return 0;
218 return 1;
220 else if(lua_istable(L, idx))
221 return lua_next(L, idx);
222 /* remove the key */
223 lua_pop(L, 1);
224 return 0;
227 /** Generic pairs function.
228 * \param L The Lua VM state.
229 * \return The number of elements pushed on stack.
231 static int
232 luaA_generic_pairs(lua_State *L)
234 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
235 lua_pushvalue(L, 1); /* state, */
236 lua_pushnil(L); /* and initial value */
237 return 3;
240 /** Overload standard pairs function to use __pairs field of metatables.
241 * \param L The Lua VM state.
242 * \return The number of elements pushed on stack.
244 static int
245 luaAe_pairs(lua_State *L)
247 if(luaL_getmetafield(L, 1, "__pairs"))
249 lua_insert(L, 1);
250 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
251 return lua_gettop(L);
254 luaL_checktype(L, 1, LUA_TTABLE);
255 return luaA_generic_pairs(L);
258 static int
259 luaA_ipairs_aux(lua_State *L)
261 int i = luaL_checkint(L, 2) + 1;
262 luaL_checktype(L, 1, LUA_TTABLE);
263 lua_pushinteger(L, i);
264 lua_rawgeti(L, 1, i);
265 return (lua_isnil(L, -1)) ? 0 : 2;
268 /** Overload standard ipairs function to use __ipairs field of metatables.
269 * \param L The Lua VM state.
270 * \return The number of elements pushed on stack.
272 static int
273 luaAe_ipairs(lua_State *L)
275 if(luaL_getmetafield(L, 1, "__ipairs"))
277 lua_insert(L, 1);
278 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
279 return lua_gettop(L);
282 luaL_checktype(L, 1, LUA_TTABLE);
283 lua_pushvalue(L, lua_upvalueindex(1));
284 lua_pushvalue(L, 1);
285 lua_pushinteger(L, 0); /* and initial value */
286 return 3;
289 /** Replace various standards Lua functions with our own.
290 * \param L The Lua VM state.
292 static void
293 luaA_fixups(lua_State *L)
295 /* replace string.len */
296 lua_getglobal(L, "string");
297 lua_pushcfunction(L, luaA_mbstrlen);
298 lua_setfield(L, -2, "len");
299 lua_pop(L, 1);
300 /* replace next */
301 lua_pushliteral(L, "next");
302 lua_pushcfunction(L, luaAe_next);
303 lua_settable(L, LUA_GLOBALSINDEX);
304 /* replace pairs */
305 lua_pushliteral(L, "pairs");
306 lua_pushcfunction(L, luaAe_next);
307 lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
308 lua_settable(L, LUA_GLOBALSINDEX);
309 /* replace ipairs */
310 lua_pushliteral(L, "ipairs");
311 lua_pushcfunction(L, luaA_ipairs_aux);
312 lua_pushcclosure(L, luaAe_ipairs, 1);
313 lua_settable(L, LUA_GLOBALSINDEX);
316 /** __next function for wtable objects.
317 * \param L The Lua VM state.
318 * \return The number of elements pushed on stack.
320 static int
321 luaA_wtable_next(lua_State *L)
323 /* upvalue 1 is content table */
324 if(lua_next(L, lua_upvalueindex(1)))
325 return 2;
326 lua_pushnil(L);
327 return 1;
330 /** __ipairs function for wtable objects.
331 * \param L The Lua VM state.
332 * \return The number of elements pushed on stack.
334 static int
335 luaA_wtable_ipairs(lua_State *L)
337 /* push ipairs_aux */
338 lua_pushvalue(L, lua_upvalueindex(2));
339 /* push content table */
340 lua_pushvalue(L, lua_upvalueindex(1));
341 lua_pushinteger(L, 0); /* and initial value */
342 return 3;
345 /** Index function of wtable objects.
346 * \param L The Lua VM state.
347 * \return The number of elements pushed on stack.
349 static int
350 luaA_wtable_index(lua_State *L)
352 size_t len;
353 const char *buf;
355 lua_pushvalue(L, 2);
356 /* check for size, waiting lua 5.2 and __len on tables */
357 if((buf = lua_tolstring(L, -1, &len)))
358 if(a_tokenize(buf, len) == A_TK_LEN)
360 lua_pushnumber(L, lua_objlen(L, lua_upvalueindex(1)));
361 return 1;
363 lua_pop(L, 1);
365 /* upvalue 1 is content table */
366 lua_rawget(L, lua_upvalueindex(1));
367 return 1;
370 /** Newndex function of wtable objects.
371 * \param L The Lua VM state.
372 * \return The number of elements pushed on stack.
374 static int
375 luaA_wtable_newindex(lua_State *L)
377 bool invalid = false;
379 /* push key on top */
380 lua_pushvalue(L, 2);
381 /* get current key value in content table */
382 lua_rawget(L, lua_upvalueindex(1));
383 /* if value is a widget, notify change */
384 if(lua_istable(L, -1) || luaA_toudata(L, -1, "widget"))
385 invalid = true;
387 lua_pop(L, 1); /* remove value */
389 /* if new value is a widget or a table */
390 if(lua_istable(L, 3))
392 luaA_table2wtable(L);
393 invalid = true;
395 else if(!invalid && luaA_toudata(L, 3, "widget"))
396 invalid = true;
398 /* upvalue 1 is content table */
399 lua_rawset(L, lua_upvalueindex(1));
401 if(invalid)
402 luaA_wibox_invalidate_byitem(L, lua_topointer(L, 1));
404 return 0;
407 /** Convert the top element of the stack to a proxied wtable.
408 * \param L The Lua VM state.
410 void
411 luaA_table2wtable(lua_State *L)
413 if(!lua_istable(L, -1))
414 return;
416 lua_newtable(L); /* create *real* content table */
417 lua_newtable(L); /* metatable */
418 lua_pushvalue(L, -2); /* copy content table */
419 lua_pushcfunction(L, luaA_ipairs_aux); /* push ipairs aux */
420 lua_pushcclosure(L, luaA_wtable_ipairs, 2);
421 lua_pushvalue(L, -3); /* copy content table */
422 lua_pushcclosure(L, luaA_wtable_next, 1); /* __next has the content table as upvalue */
423 lua_pushvalue(L, -4); /* copy content table */
424 lua_pushcclosure(L, luaA_wtable_index, 1); /* __index has the content table as upvalue */
425 lua_pushvalue(L, -5); /* copy content table */
426 lua_pushcclosure(L, luaA_wtable_newindex, 1); /* __newindex has the content table as upvalue */
427 /* set metatable field with just pushed closure */
428 lua_setfield(L, -5, "__newindex");
429 lua_setfield(L, -4, "__index");
430 lua_setfield(L, -3, "__next");
431 lua_setfield(L, -2, "__ipairs");
432 /* set metatable impossible to touch */
433 lua_pushliteral(L, "wtable");
434 lua_setfield(L, -2, "__metatable");
435 /* set new metatable on original table */
436 lua_setmetatable(L, -3);
438 /* initial key */
439 lua_pushnil(L);
440 /* go through original table */
441 while(lua_next(L, -3))
443 /* if convert value to wtable */
444 luaA_table2wtable(L);
445 /* copy key */
446 lua_pushvalue(L, -2);
447 /* move key before value */
448 lua_insert(L, -2);
449 /* set same value in content table */
450 lua_rawset(L, -4);
451 /* copy key */
452 lua_pushvalue(L, -1);
453 /* push the new value :-> */
454 lua_pushnil(L);
455 /* set orig[k] = nil */
456 lua_rawset(L, -5);
458 /* remove content table */
459 lua_pop(L, 1);
462 /** Look for an item: table, function, etc.
463 * \param L The Lua VM state.
464 * \param item The pointer item.
466 bool
467 luaA_hasitem(lua_State *L, const void *item)
469 lua_pushnil(L);
470 while(luaA_next(L, -2))
472 if(lua_topointer(L, -1) == item)
474 /* remove value and key */
475 lua_pop(L, 2);
476 return true;
478 if(lua_istable(L, -1))
479 if(luaA_hasitem(L, item))
481 /* remove key and value */
482 lua_pop(L, 2);
483 return true;
485 /* remove value */
486 lua_pop(L, 1);
488 return false;
491 /** Check if a table is a loop. When using table as direct acyclic digram,
492 * this is useful.
493 * \param L The Lua VM state.
494 * \param idx The index of the table in the stack
495 * \return True if the table loops.
497 bool
498 luaA_isloop(lua_State *L, int idx)
500 if(lua_istable(L, idx))
502 lua_pushvalue(L, idx); /* push table on top */
503 if(luaA_hasitem(L, lua_topointer(L, -1)))
505 lua_pop(L, 1); /* remove pushed table */
506 return true;
508 lua_pushnil(L);
509 while(luaA_next(L, -2))
511 /* check for recursivity */
512 if(luaA_isloop(L, -1))
514 lua_pop(L, 2); /* remove key and value */
515 return true;
517 lua_pop(L, 1); /* remove value */
519 lua_pop(L, 1); /* remove pushed table */
521 return false;
524 /** Object table.
525 * This table can use safely object as key.
526 * \param L The Lua VM state.
527 * \return The number of elements pushed on stack.
530 luaA_otable_index(lua_State *L)
532 void **obj, **v;
534 if((obj = lua_touserdata(L, 2)))
536 /* begins at nil */
537 lua_pushnil(L);
538 while(lua_next(L, 1))
540 /* check the key against the key if the key is a userdata,
541 * otherwise check it again the value. */
542 if((v = lua_touserdata(L, -2))
543 && *v == *obj)
544 /* return value */
545 return 1;
546 else if((v = lua_touserdata(L, -1))
547 && *v == *obj)
549 /* return key */
550 lua_pop(L, 1);
551 return 1;
553 /* removes 'value'; keeps 'key' for next iteration */
554 lua_pop(L, 1);
556 return 0;
559 lua_rawget(L, 1);
560 return 1;
563 /** Object table.
564 * This table can use safely object as key.
565 * \param L The Lua VM state.
566 * \return The number of elements pushed on stack.
568 static int
569 luaA_otable_newindex(lua_State *L)
571 void **obj, **v;
573 if((obj = lua_touserdata(L, 2)))
575 /* begins at nil */
576 lua_pushnil(L);
577 while(lua_next(L, 1))
579 if((v = lua_touserdata(L, -2))
580 && *v == *obj)
582 /* remove value */
583 lua_pop(L, 1);
584 /* push new value on top */
585 lua_pushvalue(L, 3);
586 /* set in table key = value */
587 lua_rawset(L, 1);
588 return 0;
590 /* removes 'value'; keeps 'key' for next iteration */
591 lua_pop(L, 1);
595 lua_rawset(L, 1);
597 return 0;
600 /** Spawn a program.
601 * This function is multi-head (Zaphod) aware and will set display to
602 * the right screen according to mouse position.
603 * \param L The Lua VM state.
604 * \return The number of elements pushed on stack
605 * \luastack
606 * \lparam The command to launch.
607 * \lparam The optional screen number to spawn the command on.
609 static int
610 luaA_spawn(lua_State *L)
612 char *host, newdisplay[128];
613 const char *cmd;
614 int screen = 0, screenp, displayp;
616 if(lua_gettop(L) == 2)
618 screen = luaL_checknumber(L, 2) - 1;
619 luaA_checkscreen(screen);
622 cmd = luaL_checkstring(L, 1);
624 if(!globalconf.xinerama_is_active)
626 xcb_parse_display(NULL, &host, &displayp, &screenp);
627 snprintf(newdisplay, sizeof(newdisplay), "%s:%d.%d", host, displayp, screen);
628 setenv("DISPLAY", newdisplay, 1);
629 p_delete(&host);
632 /* The double-fork construct avoids zombie processes and keeps the code
633 * clean from stupid signal handlers. */
634 if(fork() == 0)
636 if(fork() == 0)
638 if(globalconf.connection)
639 xcb_disconnect(globalconf.connection);
640 setsid();
641 a_exec(cmd);
642 warn("execl '%s' failed: %s\n", cmd, strerror(errno));
644 exit(EXIT_SUCCESS);
646 wait(0);
648 return 0;
651 /** awesome global table.
652 * \param L The Lua VM state.
653 * \return The number of elements pushed on stack.
654 * \luastack
655 * \lfield font The default font.
656 * \lfield conffile The configuration file which has been loaded.
658 static int
659 luaA_awesome_index(lua_State *L)
661 if(luaA_usemetatable(L, 1, 2))
662 return 1;
664 size_t len;
665 const char *buf = luaL_checklstring(L, 2, &len);
667 switch(a_tokenize(buf, len))
669 case A_TK_FONT:
671 char *font = pango_font_description_to_string(globalconf.font->desc);
672 lua_pushstring(L, font);
673 g_free(font);
675 break;
676 case A_TK_CONFFILE:
677 lua_pushstring(L, globalconf.conffile);
678 break;
679 case A_TK_FG:
680 luaA_pushcolor(L, &globalconf.colors.fg);
681 break;
682 case A_TK_BG:
683 luaA_pushcolor(L, &globalconf.colors.bg);
684 break;
685 default:
686 return 0;
689 return 1;
692 /** Newindex function for the awesome global table.
693 * \param L The Lua VM state.
694 * \return The number of elements pushed on stack.
696 static int
697 luaA_awesome_newindex(lua_State *L)
699 if(luaA_usemetatable(L, 1, 2))
700 return 1;
702 size_t len;
703 const char *buf = luaL_checklstring(L, 2, &len);
705 switch(a_tokenize(buf, len))
707 case A_TK_FONT:
709 const char *newfont = luaL_checkstring(L, 3);
710 draw_font_delete(&globalconf.font);
711 globalconf.font = draw_font_new(newfont);
713 break;
714 case A_TK_FG:
715 if((buf = luaL_checklstring(L, 3, &len)))
716 xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.fg, buf, len));
717 break;
718 case A_TK_BG:
719 if((buf = luaL_checklstring(L, 3, &len)))
720 xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.bg, buf, len));
721 break;
722 default:
723 return 0;
726 return 0;
729 /** Initialize the Lua VM
731 void
732 luaA_init(void)
734 lua_State *L;
736 static const struct luaL_reg otable_methods[] =
738 { "__call", luaA_otable_new },
739 { NULL, NULL }
741 static const struct luaL_reg otable_meta[] =
743 { "__index", luaA_otable_index },
744 { "__newindex", luaA_otable_newindex },
745 { NULL, NULL }
747 static const struct luaL_reg awesome_lib[] =
749 { "quit", luaA_quit },
750 { "exec", luaA_exec },
751 { "spawn", luaA_spawn },
752 { "restart", luaA_restart },
753 { "buttons", luaA_buttons },
754 { "__index", luaA_awesome_index },
755 { "__newindex", luaA_awesome_newindex },
756 { NULL, NULL }
759 L = globalconf.L = luaL_newstate();
761 luaL_openlibs(L);
763 luaA_fixups(L);
765 /* Export awesome lib */
766 luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
768 /* Export hooks lib */
769 luaL_register(L, "hooks", awesome_hooks_lib);
771 /* Export D-Bus lib */
772 luaL_register(L, "dbus", awesome_dbus_lib);
774 /* Export keygrabber lib */
775 luaL_register(L, "keygrabber", awesome_keygrabber_lib);
777 /* Export mousegrabber lib */
778 luaL_register(L, "mousegrabber", awesome_mousegrabber_lib);
780 /* Export otable lib */
781 luaA_openlib(L, "otable", otable_methods, otable_meta);
783 /* Export screen */
784 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
786 /* Export mouse */
787 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
789 /* Export button */
790 luaA_openlib(L, "button", awesome_button_methods, awesome_button_meta);
792 /* Export image */
793 luaA_openlib(L, "image", awesome_image_methods, awesome_image_meta);
795 /* Export tag */
796 luaA_openlib(L, "tag", awesome_tag_methods, awesome_tag_meta);
798 /* Export wibox */
799 luaA_openlib(L, "wibox", awesome_wibox_methods, awesome_wibox_meta);
801 /* Export widget */
802 luaA_openlib(L, "widget", awesome_widget_methods, awesome_widget_meta);
804 /* Export client */
805 luaA_openlib(L, "client", awesome_client_methods, awesome_client_meta);
807 /* Export keys */
808 luaA_openlib(L, "keybinding", awesome_keybinding_methods, awesome_keybinding_meta);
810 lua_pushliteral(L, "AWESOME_VERSION");
811 lua_pushstring(L, AWESOME_VERSION);
812 lua_settable(L, LUA_GLOBALSINDEX);
814 lua_pushliteral(L, "AWESOME_RELEASE");
815 lua_pushstring(L, AWESOME_RELEASE);
816 lua_settable(L, LUA_GLOBALSINDEX);
818 /* init hooks */
819 globalconf.hooks.manage = LUA_REFNIL;
820 globalconf.hooks.unmanage = LUA_REFNIL;
821 globalconf.hooks.focus = LUA_REFNIL;
822 globalconf.hooks.unfocus = LUA_REFNIL;
823 globalconf.hooks.mouse_enter = LUA_REFNIL;
824 globalconf.hooks.arrange = LUA_REFNIL;
825 globalconf.hooks.clients = LUA_REFNIL;
826 globalconf.hooks.tags = LUA_REFNIL;
827 globalconf.hooks.tagged = LUA_REFNIL;
828 globalconf.hooks.property = LUA_REFNIL;
829 globalconf.hooks.timer = LUA_REFNIL;
830 #ifdef WITH_DBUS
831 globalconf.hooks.dbus = LUA_REFNIL;
832 #endif
834 /* add Lua lib path (/usr/share/awesome/lib by default) */
835 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?.lua\"");
836 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?/init.lua\"");
838 /* add XDG_CONFIG_DIR (/etc/xdg/awesome by default) as include path */
839 luaA_dostring(L, "package.path = package.path .. \";" XDG_CONFIG_DIR "/awesome/?.lua\"");
840 luaA_dostring(L, "package.path = package.path .. \";" XDG_CONFIG_DIR "/awesome/?/init.lua\"");
842 /* add either XDG_CONFIG_HOME/awesome or HOME/.config/awesome to path */
843 char *dir;
844 if((dir = getenv("XDG_CONFIG_HOME")))
846 char *path;
847 a_asprintf(&path, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"", dir, dir);
848 luaA_dostring(globalconf.L, path);
849 p_delete(&path);
851 else
853 char *path, *homedir = getenv("HOME");
854 a_asprintf(&path,
855 "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?.lua;%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?/init.lua\"",
856 homedir, homedir);
857 luaA_dostring(globalconf.L, path);
858 p_delete(&path);
861 /* add XDG_CONFIG_DIRS to include paths */
862 char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
863 ssize_t len;
865 if((len = a_strlen(xdg_config_dirs)))
867 char **buf, **xdg_files = a_strsplit(xdg_config_dirs, len, ':');
869 for(buf = xdg_files; *buf; buf++)
871 char *confpath;
872 a_asprintf(&confpath, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"",
873 *buf, *buf);
874 luaA_dostring(globalconf.L, confpath);
875 p_delete(&confpath);
878 for(buf = xdg_files; *buf; buf++)
879 p_delete(buf);
880 p_delete(&xdg_files);
884 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
886 static bool
887 luaA_loadrc(const char *confpath, bool run)
889 if(confpath)
891 if(!luaL_loadfile(globalconf.L, confpath))
893 if(run)
895 if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
896 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
897 else
899 globalconf.conffile = a_strdup(confpath);
900 return true;
903 else
904 lua_pop(globalconf.L, 1);
905 return true;
907 else
908 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
910 return false;
913 /** Load a configuration file.
914 * \param confpatharg The configuration file to load.
915 * \param run Run the configuration file.
917 bool
918 luaA_parserc(const char *confpatharg, bool run)
920 int screen;
921 const char *confdir, *xdg_config_dirs;
922 char *confpath = NULL, **xdg_files = NULL, **buf;
923 ssize_t len;
924 bool ret = false;
926 /* try to load, return if it's ok */
927 if(luaA_loadrc(confpatharg, run))
929 ret = true;
930 goto bailout;
933 if((confdir = getenv("XDG_CONFIG_HOME")))
934 a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, confdir);
935 else
936 a_asprintf(&confpath, "%s" XDG_CONFIG_HOME_DEFAULT AWESOME_CONFIG_FILE, getenv("HOME"));
938 /* try to run XDG_CONFIG_HOME/awesome/rc.lua */
939 if(luaA_loadrc(confpath, run))
941 ret = true;
942 goto bailout;
945 p_delete(&confpath);
947 xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
949 if(!(len = a_strlen(xdg_config_dirs)))
951 xdg_config_dirs = XDG_CONFIG_DIR;
952 len = sizeof(XDG_CONFIG_DIR) - 1;
955 xdg_files = a_strsplit(xdg_config_dirs, len, ':');
957 for(buf = xdg_files; *buf && !ret; buf++)
959 a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, *buf);
960 if(luaA_loadrc(confpath, run))
962 ret = true;
963 goto bailout;
965 p_delete(&confpath);
968 bailout:
970 p_delete(&confpath);
972 if(xdg_files)
974 for(buf = xdg_files; *buf; buf++)
975 p_delete(buf);
976 p_delete(&xdg_files);
979 /* Assure there's at least one tag */
980 for(screen = 0; screen < globalconf.nscreen; screen++)
981 if(!globalconf.screens[screen].tags.len)
982 tag_append_to_screen(tag_new("default", sizeof("default") - 1, layout_tile, 0.5, 1, 0),
983 &globalconf.screens[screen]);
984 return ret;
987 /** Parse a command.
988 * \param cmd The buffer to parse.
989 * \return the number of elements pushed on the stack by the last statement in cmd.
990 * If there's an error, the message is pushed onto the stack and this returns 1.
992 static int
993 luaA_docmd(const char *cmd)
995 char *p;
996 int newtop, oldtop = lua_gettop(globalconf.L);
998 while((p = strchr(cmd, '\n')))
1000 newtop = lua_gettop(globalconf.L);
1001 lua_pop(globalconf.L, newtop - oldtop);
1002 oldtop = newtop;
1004 *p = '\0';
1005 if (luaL_dostring(globalconf.L, cmd))
1007 warn("error executing Lua code: %s", lua_tostring(globalconf.L, -1));
1008 return 1;
1010 cmd = p + 1;
1012 return lua_gettop(globalconf.L) - oldtop;
1015 /** Pushes a Lua array containing the top n elements of the stack.
1016 * \param n The number of elements to put in the array.
1018 static void
1019 luaA_array(int n)
1021 int i;
1022 lua_createtable(globalconf.L, n, 0);
1023 lua_insert(globalconf.L, -n - 1);
1025 for (i = n; i > 0; i--)
1026 lua_rawseti(globalconf.L, -i - 1, i);
1029 /** Maps the top n elements of the stack to the result of
1030 * applying a function to that element.
1031 * \param n The number of elements to map.
1032 * \luastack
1033 * \lparam The function to map the elements by. This should be
1034 * at position -(n + 1).
1036 static void
1037 luaA_map(int n)
1039 int i;
1040 for (i = 0; i < n; i++)
1042 lua_pushvalue(globalconf.L, -n - 1); /* copy of the function */
1043 lua_pushvalue(globalconf.L, -n - 1); /* value to map */
1044 lua_pcall(globalconf.L, 1, 1, 0); /* call function */
1045 lua_remove(globalconf.L, -n - 1); /* remove old value */
1047 lua_remove(globalconf.L, -n - 1); /* remove function */
1050 static void luaA_conn_cleanup(EV_P_ ev_io *w)
1052 ev_ref(EV_DEFAULT_UC);
1053 ev_io_stop(EV_DEFAULT_UC_ w);
1054 if (close(w->fd))
1055 warn("error closing UNIX domain socket: %s", strerror(errno));
1056 p_delete(&w);
1059 static void
1060 luaA_cb(EV_P_ ev_io *w, int revents)
1062 char buf[1024];
1063 int r, els;
1064 const char *s;
1065 size_t len;
1067 switch(r = recv(w->fd, buf, sizeof(buf)-1, MSG_TRUNC))
1069 case -1:
1070 warn("error reading UNIX domain socket: %s", strerror(errno));
1071 case 0: /* 0 bytes are only transferred when the connection is closed */
1072 luaA_conn_cleanup(EV_DEFAULT_UC_ w);
1073 break;
1074 default:
1075 if(r >= ssizeof(buf))
1076 break;
1077 buf[r] = '\0';
1078 lua_getglobal(globalconf.L, "table");
1079 lua_getfield(globalconf.L, -1, "concat");
1080 lua_remove(globalconf.L, -2); /* remove table */
1082 lua_getglobal(globalconf.L, "tostring");
1083 els = luaA_docmd(buf);
1084 luaA_map(els); /* map results to strings */
1085 luaA_array(els); /* put strings in an array */
1087 lua_pushstring(globalconf.L, "\t");
1088 lua_pcall(globalconf.L, 2, 1, 0); /* concatenate results with tabs */
1090 s = lua_tolstring(globalconf.L, -1, &len);
1092 /* ignore ENOENT because the client may not read */
1093 if (send(w->fd, s, len, MSG_DONTWAIT) == -1)
1094 switch(errno)
1096 case ENOENT:
1097 case EAGAIN:
1098 break;
1099 default:
1100 warn("can't send back to client via domain socket: %s", strerror(errno));
1101 break;
1104 lua_pop(globalconf.L, 1); /* pop the string */
1106 awesome_refresh(globalconf.connection);
1110 static void
1111 luaA_conn_cb(EV_P_ ev_io *w, int revents)
1113 ev_io *csio_conn = p_new(ev_io, 1);
1114 int csfd = accept(w->fd, NULL, NULL);
1116 ev_io_init(csio_conn, &luaA_cb, csfd, EV_READ);
1117 ev_io_start(EV_DEFAULT_UC_ csio_conn);
1118 ev_unref(EV_DEFAULT_UC);
1121 void
1122 luaA_cs_init(void)
1124 int csfd = socket_getclient();
1126 if (csfd < 0 || fcntl(csfd, F_SETFD, FD_CLOEXEC) == -1)
1127 return;
1129 addr = socket_getaddr(getenv("DISPLAY"));
1131 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1133 if(errno == EADDRINUSE)
1135 if(unlink(addr->sun_path))
1136 warn("error unlinking existing file: %s", strerror(errno));
1137 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1138 return warn("error binding UNIX domain socket: %s", strerror(errno));
1140 else
1141 return warn("error binding UNIX domain socket: %s", strerror(errno));
1143 listen(csfd, 10);
1145 ev_io_init(&csio, &luaA_conn_cb, csfd, EV_READ);
1146 ev_io_start(EV_DEFAULT_UC_ &csio);
1147 ev_unref(EV_DEFAULT_UC);
1150 void
1151 luaA_cs_cleanup(void)
1153 if(csio.fd < 0)
1154 return;
1155 ev_ref(EV_DEFAULT_UC);
1156 ev_io_stop(EV_DEFAULT_UC_ &csio);
1157 if (close(csio.fd))
1158 warn("error closing UNIX domain socket: %s", strerror(errno));
1159 if(unlink(addr->sun_path))
1160 warn("error unlinking UNIX domain socket: %s", strerror(errno));
1161 p_delete(&addr);
1162 csio.fd = -1;
1165 void
1166 luaA_on_timer(EV_P_ ev_timer *w, int revents)
1168 if(globalconf.hooks.timer != LUA_REFNIL)
1169 luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0, 0);
1170 awesome_refresh(globalconf.connection);
1173 /** Push a color as a string onto the stack
1174 * \param L The Lua VM state.
1175 * \param c The color to push.
1176 * \return The number of elements pushed on stack.
1179 luaA_pushcolor(lua_State *L, const xcolor_t *c)
1181 uint8_t r = (unsigned)c->red * 0xff / 0xffff;
1182 uint8_t g = (unsigned)c->green * 0xff / 0xffff;
1183 uint8_t b = (unsigned)c->blue * 0xff / 0xffff;
1184 uint8_t a = (unsigned)c->alpha * 0xff / 0xffff;
1185 char s[10];
1186 int len;
1187 /* do not print alpha if it's full */
1188 if(a == 0xff)
1189 len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
1190 else
1191 len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
1192 lua_pushlstring(L, s, len);
1193 return 1;