awesomerc: use getmaster()
[awesome.git] / luaa.c
blob66ee966d514679d60d49479e8ca8c50919470ac3
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 or get default font. (DEPRECATED)
147 * \param L The Lua VM state.
148 * \return The number of elements pushed on stack.
149 * \luastack
150 * \lparam An optional string with a font name in Pango format.
151 * \lreturn The font used, in Pango format.
153 static int
154 luaA_font(lua_State *L)
156 char *font;
158 if(lua_gettop(L) == 1)
160 const char *newfont = luaL_checkstring(L, 1);
161 draw_font_delete(&globalconf.font);
162 globalconf.font = draw_font_new(newfont);
165 font = pango_font_description_to_string(globalconf.font->desc);
166 lua_pushstring(L, font);
167 g_free(font);
169 return 1;
172 /** Set default font. (DEPRECATED)
173 * \param L The Lua VM state.
174 * \return The number of elements pushed on stack.
175 * \luastack
176 * \lparam A string with a font name in Pango format.
178 static int
179 luaA_font_set(lua_State *L)
181 deprecate(L);
182 return luaA_font(L);
185 /** Get configuration file path used by awesome.
186 * \param L The Lua VM state.
187 * \return The number of elements pushed on stack.
188 * \luastack
189 * \lreturn The awesome configuration file path.
191 static int
192 luaA_conffile(lua_State *L)
194 lua_pushstring(L, globalconf.conffile);
195 return 1;
198 /** Set default colors.
199 * \param L The Lua VM state.
200 * \return The number of elements pushed on stack.
201 * \luastack
202 * \lparam A table with `fg' and `bg' elements, containing colors.
203 * \lreturn A table with `fg' and `bg' elements, containing colors.
205 static int
206 luaA_colors(lua_State *L)
208 if(lua_gettop(L) == 1)
210 const char *buf;
211 size_t len;
212 int8_t colors_nbr = -1, i;
213 xcolor_init_request_t reqs[2];
215 luaA_checktable(L, 1);
217 if((buf = luaA_getopt_lstring(L, 1, "fg", NULL, &len)))
218 reqs[++colors_nbr] = xcolor_init_unchecked(&globalconf.colors.fg, buf, len);
220 if((buf = luaA_getopt_lstring(L, 1, "bg", NULL, &len)))
221 reqs[++colors_nbr] = xcolor_init_unchecked(&globalconf.colors.bg, buf, len);
223 for(i = 0; i <= colors_nbr; i++)
224 xcolor_init_reply(reqs[i]);
227 lua_newtable(L);
228 luaA_pushcolor(L, &globalconf.colors.fg);
229 lua_setfield(L, -2, "fg");
230 luaA_pushcolor(L, &globalconf.colors.bg);
231 lua_setfield(L, -2, "bg");
233 return 1;
236 /** Set default colors. (DEPRECATED)
237 * \param L The Lua VM state.
238 * \return The number of elements pushed on stack.
240 * \luastack
241 * \lparam A table with `fg' and `bg' elements, containing colors.
243 static int
244 luaA_colors_set(lua_State *L)
246 deprecate(L);
247 return luaA_colors(L);
250 static void
251 luaA_openlib(lua_State *L, const char *name,
252 const struct luaL_reg methods[],
253 const struct luaL_reg meta[])
255 luaL_newmetatable(L, name); /* 1 */
256 lua_pushvalue(L, -1); /* dup metatable 2 */
257 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
259 luaL_register(L, NULL, meta); /* 1 */
260 luaL_register(L, name, methods); /* 2 */
261 lua_pushvalue(L, -1); /* dup self as metatable 3 */
262 lua_setmetatable(L, -2); /* set self as metatable 2 */
263 lua_pop(L, 2);
266 /** UTF-8 aware string length computing.
267 * \param L The Lua VM state.
268 * \return The number of elements pushed on stack.
270 static int
271 luaA_mbstrlen(lua_State *L)
273 const char *cmd = luaL_checkstring(L, 1);
274 lua_pushnumber(L, mbstowcs(NULL, NONULL(cmd), 0));
275 return 1;
278 /** Overload standard Lua next function to use __next key on metatable.
279 * \param L The Lua VM state.
280 * \param The number of elements pushed on stack.
282 static int
283 luaAe_next(lua_State *L)
285 if(luaL_getmetafield(L, 1, "__next"))
287 lua_insert(L, 1);
288 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
289 return lua_gettop(L);
292 luaL_checktype(L, 1, LUA_TTABLE);
293 lua_settop(L, 2);
294 if(lua_next(L, 1))
295 return 2;
296 lua_pushnil(L);
297 return 1;
300 /** Overload lua_next() function by using __next metatable field
301 * to get next elements.
302 * \param L The Lua VM stack.
303 * \param idx The index number of elements in stack.
304 * \return 1 if more elements to come, 0 otherwise.
307 luaA_next(lua_State *L, int idx)
309 if(luaL_getmetafield(L, idx, "__next"))
311 /* if idx is relative, reduce it since we got __next */
312 if(idx < 0) idx--;
313 /* copy table and then move key */
314 lua_pushvalue(L, idx);
315 lua_pushvalue(L, -3);
316 lua_remove(L, -4);
317 lua_pcall(L, 2, 2, 0);
318 /* next returned nil, it's the end */
319 if(lua_isnil(L, -1))
321 /* remove nil */
322 lua_pop(L, 2);
323 return 0;
325 return 1;
328 return lua_next(L, idx);
331 /** Generic pairs function.
332 * \param L The Lua VM state.
333 * \return The number of elements pushed on stack.
335 static int
336 luaA_generic_pairs(lua_State *L)
338 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
339 lua_pushvalue(L, 1); /* state, */
340 lua_pushnil(L); /* and initial value */
341 return 3;
344 /** Overload standard pairs function to use __pairs field of metatables.
345 * \param L The Lua VM state.
346 * \return The number of elements pushed on stack.
348 static int
349 luaAe_pairs(lua_State *L)
351 if(luaL_getmetafield(L, 1, "__pairs"))
353 lua_insert(L, 1);
354 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
355 return lua_gettop(L);
358 luaL_checktype(L, 1, LUA_TTABLE);
359 return luaA_generic_pairs(L);
362 static int
363 luaA_ipairs_aux(lua_State *L)
365 int i = luaL_checkint(L, 2) + 1;
366 luaL_checktype(L, 1, LUA_TTABLE);
367 lua_pushinteger(L, i);
368 lua_rawgeti(L, 1, i);
369 return (lua_isnil(L, -1)) ? 0 : 2;
372 /** Overload standard ipairs function to use __ipairs field of metatables.
373 * \param L The Lua VM state.
374 * \return The number of elements pushed on stack.
376 static int
377 luaAe_ipairs(lua_State *L)
379 if(luaL_getmetafield(L, 1, "__ipairs"))
381 lua_insert(L, 1);
382 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
383 return lua_gettop(L);
386 luaL_checktype(L, 1, LUA_TTABLE);
387 lua_pushvalue(L, lua_upvalueindex(1));
388 lua_pushvalue(L, 1);
389 lua_pushinteger(L, 0); /* and initial value */
390 return 3;
393 /** Replace various standards Lua functions with our own.
394 * \param L The Lua VM state.
396 static void
397 luaA_fixups(lua_State *L)
399 /* replace string.len */
400 lua_getglobal(L, "string");
401 lua_pushcfunction(L, luaA_mbstrlen);
402 lua_setfield(L, -2, "len");
403 lua_pop(L, 1);
404 /* replace next */
405 lua_pushliteral(L, "next");
406 lua_pushcfunction(L, luaAe_next);
407 lua_settable(L, LUA_GLOBALSINDEX);
408 /* replace pairs */
409 lua_pushliteral(L, "pairs");
410 lua_pushcfunction(L, luaAe_next);
411 lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
412 lua_settable(L, LUA_GLOBALSINDEX);
413 /* replace ipairs */
414 lua_pushliteral(L, "ipairs");
415 lua_pushcfunction(L, luaA_ipairs_aux);
416 lua_pushcclosure(L, luaAe_ipairs, 1);
417 lua_settable(L, LUA_GLOBALSINDEX);
420 /** __next function for wtable objects.
421 * \param L The Lua VM state.
422 * \return The number of elements pushed on stack.
424 static int
425 luaA_wtable_next(lua_State *L)
427 /* upvalue 1 is content table */
428 if(lua_next(L, lua_upvalueindex(1)))
429 return 2;
430 lua_pushnil(L);
431 return 1;
434 /** __ipairs function for wtable objects.
435 * \param L The Lua VM state.
436 * \return The number of elements pushed on stack.
438 static int
439 luaA_wtable_ipairs(lua_State *L)
441 /* push ipairs_aux */
442 lua_pushvalue(L, lua_upvalueindex(2));
443 /* push content table */
444 lua_pushvalue(L, lua_upvalueindex(1));
445 lua_pushinteger(L, 0); /* and initial value */
446 return 3;
449 /** Index function of wtable objects.
450 * \param L The Lua VM state.
451 * \return The number of elements pushed on stack.
453 static int
454 luaA_wtable_index(lua_State *L)
456 size_t len;
457 const char *buf;
459 lua_pushvalue(L, 2);
460 /* check for size, waiting lua 5.2 and __len on tables */
461 if((buf = lua_tolstring(L, -1, &len)))
462 if(a_tokenize(buf, len) == A_TK_LEN)
464 lua_pushnumber(L, lua_objlen(L, lua_upvalueindex(1)));
465 return 1;
467 lua_pop(L, 1);
469 /* upvalue 1 is content table */
470 lua_rawget(L, lua_upvalueindex(1));
471 return 1;
474 /** Newndex function of wtable objects.
475 * \param L The Lua VM state.
476 * \return The number of elements pushed on stack.
478 static int
479 luaA_wtable_newindex(lua_State *L)
481 bool invalid = false;
483 /* push key on top */
484 lua_pushvalue(L, 2);
485 /* get current key value in content table */
486 lua_rawget(L, lua_upvalueindex(1));
487 /* if value is a widget, notify change */
488 if(lua_istable(L, -1) || luaA_toudata(L, -1, "widget"))
489 invalid = true;
491 lua_pop(L, 1); /* remove value */
493 /* if new value is a widget or a table */
494 if(lua_istable(L, 3))
496 luaA_table2wtable(L);
497 invalid = true;
499 else if(!invalid && luaA_toudata(L, 3, "widget"))
500 invalid = true;
502 /* upvalue 1 is content table */
503 lua_rawset(L, lua_upvalueindex(1));
505 if(invalid)
506 luaA_wibox_invalidate_byitem(L, lua_topointer(L, 1));
508 return 0;
511 /** Convert the top element of the stack to a proxied wtable.
512 * \param L The Lua VM state.
514 void
515 luaA_table2wtable(lua_State *L)
517 if(!lua_istable(L, -1))
518 return;
520 lua_newtable(L); /* create *real* content table */
521 lua_newtable(L); /* metatable */
522 lua_pushvalue(L, -2); /* copy content table */
523 lua_pushcfunction(L, luaA_ipairs_aux); /* push ipairs aux */
524 lua_pushcclosure(L, luaA_wtable_ipairs, 2);
525 lua_pushvalue(L, -3); /* copy content table */
526 lua_pushcclosure(L, luaA_wtable_next, 1); /* __next has the content table as upvalue */
527 lua_pushvalue(L, -4); /* copy content table */
528 lua_pushcclosure(L, luaA_wtable_index, 1); /* __index has the content table as upvalue */
529 lua_pushvalue(L, -5); /* copy content table */
530 lua_pushcclosure(L, luaA_wtable_newindex, 1); /* __newindex has the content table as upvalue */
531 /* set metatable field with just pushed closure */
532 lua_setfield(L, -5, "__newindex");
533 lua_setfield(L, -4, "__index");
534 lua_setfield(L, -3, "__next");
535 lua_setfield(L, -2, "__ipairs");
536 /* set metatable impossible to touch */
537 lua_pushliteral(L, "wtable");
538 lua_setfield(L, -2, "__metatable");
539 /* set new metatable on original table */
540 lua_setmetatable(L, -3);
542 /* initial key */
543 lua_pushnil(L);
544 /* go through original table */
545 while(lua_next(L, -3))
547 /* if convert value to wtable */
548 luaA_table2wtable(L);
549 /* copy key */
550 lua_pushvalue(L, -2);
551 /* move key before value */
552 lua_insert(L, -2);
553 /* set same value in content table */
554 lua_rawset(L, -4);
555 /* copy key */
556 lua_pushvalue(L, -1);
557 /* push the new value :-> */
558 lua_pushnil(L);
559 /* set orig[k] = nil */
560 lua_rawset(L, -5);
562 /* remove content table */
563 lua_pop(L, 1);
566 /** Look for an item: table, function, etc.
567 * \param L The Lua VM state.
568 * \param item The pointer item.
570 bool
571 luaA_hasitem(lua_State *L, const void *item)
573 lua_pushnil(L);
574 while(luaA_next(L, -2))
576 if(lua_topointer(L, -1) == item)
578 /* remove value and key */
579 lua_pop(L, 2);
580 return true;
582 if(lua_istable(L, -1))
583 if(luaA_hasitem(L, item))
585 /* remove key and value */
586 lua_pop(L, 2);
587 return true;
589 /* remove value */
590 lua_pop(L, 1);
592 return false;
595 /** Check if a table is a loop. When using table as direct acyclic digram,
596 * this is useful.
597 * \param L The Lua VM state.
598 * \param idx The index of the table in the stack
599 * \return True if the table loops.
601 bool
602 luaA_isloop(lua_State *L, int idx)
604 if(lua_istable(L, idx))
606 lua_pushvalue(L, idx); /* push table on top */
607 if(luaA_hasitem(L, lua_topointer(L, -1)))
609 lua_pop(L, 1); /* remove pushed table */
610 return true;
612 lua_pushnil(L);
613 while(luaA_next(L, -2))
615 /* check for recursivity */
616 if(luaA_isloop(L, -1))
618 lua_pop(L, 2); /* remove key and value */
619 return true;
621 lua_pop(L, 1); /* remove value */
623 lua_pop(L, 1); /* remove pushed table */
625 return false;
628 /** Object table.
629 * This table can use safely object as key.
630 * \param L The Lua VM state.
631 * \return The number of elements pushed on stack.
634 luaA_otable_index(lua_State *L)
636 void **obj, **v;
638 if((obj = lua_touserdata(L, 2)))
640 /* begins at nil */
641 lua_pushnil(L);
642 while(lua_next(L, 1))
644 /* check the key against the key if the key is a userdata,
645 * otherwise check it again the value. */
646 if((v = lua_touserdata(L, -2))
647 && *v == *obj)
648 /* return value */
649 return 1;
650 else if((v = lua_touserdata(L, -1))
651 && *v == *obj)
653 /* return key */
654 lua_pop(L, 1);
655 return 1;
657 /* removes 'value'; keeps 'key' for next iteration */
658 lua_pop(L, 1);
660 return 0;
663 lua_rawget(L, 1);
664 return 1;
667 /** Object table.
668 * This table can use safely object as key.
669 * \param L The Lua VM state.
670 * \return The number of elements pushed on stack.
672 static int
673 luaA_otable_newindex(lua_State *L)
675 void **obj, **v;
677 if((obj = lua_touserdata(L, 2)))
679 /* begins at nil */
680 lua_pushnil(L);
681 while(lua_next(L, 1))
683 if((v = lua_touserdata(L, -2))
684 && *v == *obj)
686 /* remove value */
687 lua_pop(L, 1);
688 /* push new value on top */
689 lua_pushvalue(L, 3);
690 /* set in table key = value */
691 lua_rawset(L, 1);
692 return 0;
694 /* removes 'value'; keeps 'key' for next iteration */
695 lua_pop(L, 1);
699 lua_rawset(L, 1);
701 return 0;
704 /** Spawn a program.
705 * This function is multi-head (Zaphod) aware and will set display to
706 * the right screen according to mouse position.
707 * \param L The Lua VM state.
708 * \return The number of elements pushed on stack
709 * \luastack
710 * \lparam The command to launch.
711 * \lparam The optional screen number to spawn the command on.
713 static int
714 luaA_spawn(lua_State *L)
716 char *host, newdisplay[128];
717 const char *cmd;
718 int screen = 0, screenp, displayp;
720 if(lua_gettop(L) == 2)
722 screen = luaL_checknumber(L, 2) - 1;
723 luaA_checkscreen(screen);
726 cmd = luaL_checkstring(L, 1);
728 if(!globalconf.xinerama_is_active)
730 xcb_parse_display(NULL, &host, &displayp, &screenp);
731 snprintf(newdisplay, sizeof(newdisplay), "%s:%d.%d", host, displayp, screen);
732 setenv("DISPLAY", newdisplay, 1);
733 p_delete(&host);
736 /* The double-fork construct avoids zombie processes and keeps the code
737 * clean from stupid signal handlers. */
738 if(fork() == 0)
740 if(fork() == 0)
742 if(globalconf.connection)
743 xcb_disconnect(globalconf.connection);
744 setsid();
745 a_exec(cmd);
746 warn("execl '%s' failed: %s\n", cmd, strerror(errno));
748 exit(EXIT_SUCCESS);
750 wait(0);
752 return 0;
755 /** Deprecated function, does nothing.
757 static int
758 luaA_mouse_add(lua_State *L)
760 deprecate(L);
761 return 0;
764 /** Initialize the Lua VM
766 void
767 luaA_init(void)
769 lua_State *L;
771 static const struct luaL_reg otable_methods[] =
773 { "__call", luaA_otable_new },
774 { NULL, NULL }
776 static const struct luaL_reg otable_meta[] =
778 { "__index", luaA_otable_index },
779 { "__newindex", luaA_otable_newindex },
780 { NULL, NULL }
782 static const struct luaL_reg awesome_lib[] =
784 { "quit", luaA_quit },
785 { "exec", luaA_exec },
786 { "spawn", luaA_spawn },
787 { "restart", luaA_restart },
788 { "buttons", luaA_buttons },
789 { "font_set", luaA_font_set },
790 { "colors_set", luaA_colors_set },
791 { "font", luaA_font },
792 { "colors", luaA_colors },
793 { "conffile", luaA_conffile },
794 /* deprecated */
795 { "mouse_add", luaA_mouse_add },
796 { NULL, NULL }
799 L = globalconf.L = luaL_newstate();
801 luaL_openlibs(L);
803 luaA_fixups(L);
805 /* Export awesome lib */
806 luaL_register(L, "awesome", awesome_lib);
808 /* Export hooks lib */
809 luaL_register(L, "hooks", awesome_hooks_lib);
811 /* Export keygrabber lib */
812 luaL_register(L, "keygrabber", awesome_keygrabber_lib);
814 /* Export otable lib */
815 luaA_openlib(L, "otable", otable_methods, otable_meta);
817 /* Export screen */
818 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
820 /* Export mouse */
821 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
823 /* Export button */
824 luaA_openlib(L, "button", awesome_button_methods, awesome_button_meta);
826 /* Export image */
827 luaA_openlib(L, "image", awesome_image_methods, awesome_image_meta);
829 /* Export tag */
830 luaA_openlib(L, "tag", awesome_tag_methods, awesome_tag_meta);
832 /* Export statusbar */
833 luaA_openlib(L, "statusbar", awesome_statusbar_methods, awesome_statusbar_meta);
835 /* Export wibox */
836 luaA_openlib(L, "wibox", awesome_wibox_methods, awesome_wibox_meta);
838 /* Export widget */
839 luaA_openlib(L, "widget", awesome_widget_methods, awesome_widget_meta);
841 /* Export client */
842 luaA_openlib(L, "client", awesome_client_methods, awesome_client_meta);
844 /* Export titlebar */
845 luaA_openlib(L, "titlebar", awesome_titlebar_methods, awesome_titlebar_meta);
847 /* Export keys */
848 luaA_openlib(L, "keybinding", awesome_keybinding_methods, awesome_keybinding_meta);
850 lua_pushliteral(L, "AWESOME_VERSION");
851 lua_pushstring(L, AWESOME_VERSION);
852 lua_settable(L, LUA_GLOBALSINDEX);
854 lua_pushliteral(L, "AWESOME_RELEASE");
855 lua_pushstring(L, AWESOME_RELEASE);
856 lua_settable(L, LUA_GLOBALSINDEX);
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;
871 /* add Lua lib path (/usr/share/awesome/lib by default) */
872 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?.lua\"");
873 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?/init.lua\"");
875 /* add XDG_CONFIG_DIR (/etc/xdg/awesome by default) as include path */
876 luaA_dostring(L, "package.path = package.path .. \";" XDG_CONFIG_DIR "/awesome/?.lua\"");
877 luaA_dostring(L, "package.path = package.path .. \";" XDG_CONFIG_DIR "/awesome/?/init.lua\"");
879 /* add either XDG_CONFIG_HOME/awesome or HOME/.config/awesome to path */
880 char *dir;
881 if((dir = getenv("XDG_CONFIG_HOME")))
883 char *path;
884 a_asprintf(&path, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"", dir, dir);
885 luaA_dostring(globalconf.L, path);
886 p_delete(&path);
888 else
890 char *path, *homedir = getenv("HOME");
891 a_asprintf(&path,
892 "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?.lua;%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?/init.lua\"",
893 homedir, homedir);
894 luaA_dostring(globalconf.L, path);
895 p_delete(&path);
898 /* add XDG_CONFIG_DIRS to include paths */
899 char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
900 ssize_t len;
902 if((len = a_strlen(xdg_config_dirs)))
904 char **buf, **xdg_files = a_strsplit(xdg_config_dirs, len, ':');
906 for(buf = xdg_files; *buf; buf++)
908 char *confpath;
909 a_asprintf(&confpath, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"",
910 *buf, *buf);
911 luaA_dostring(globalconf.L, confpath);
912 p_delete(&confpath);
915 for(buf = xdg_files; *buf; buf++)
916 p_delete(buf);
917 p_delete(&xdg_files);
921 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
923 static bool
924 luaA_loadrc(const char *confpath, bool run)
926 if(confpath)
928 if(!luaL_loadfile(globalconf.L, confpath))
930 if(run)
932 if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
933 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
934 else
936 globalconf.conffile = a_strdup(confpath);
937 return true;
940 else
941 lua_pop(globalconf.L, 1);
942 return true;
944 else
945 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
947 return false;
950 /** Load a configuration file.
951 * \param confpatharg The configuration file to load.
952 * \param run Run the configuration file.
954 bool
955 luaA_parserc(const char *confpatharg, bool run)
957 int screen;
958 const char *confdir, *xdg_config_dirs;
959 char *confpath = NULL, **xdg_files = NULL, **buf;
960 ssize_t len;
961 bool ret = false;
963 /* try to load, return if it's ok */
964 if(luaA_loadrc(confpatharg, run))
966 ret = true;
967 goto bailout;
970 if((confdir = getenv("XDG_CONFIG_HOME")))
971 a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, confdir);
972 else
973 a_asprintf(&confpath, "%s" XDG_CONFIG_HOME_DEFAULT AWESOME_CONFIG_FILE, getenv("HOME"));
975 /* try to run XDG_CONFIG_HOME/awesome/rc.lua */
976 if(luaA_loadrc(confpath, run))
978 ret = true;
979 goto bailout;
982 p_delete(&confpath);
984 xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
986 if(!(len = a_strlen(xdg_config_dirs)))
988 xdg_config_dirs = XDG_CONFIG_DIR;
989 len = sizeof(XDG_CONFIG_DIR) - 1;
992 xdg_files = a_strsplit(xdg_config_dirs, len, ':');
994 for(buf = xdg_files; *buf && !ret; buf++)
996 a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, *buf);
997 if(luaA_loadrc(confpath, run))
999 ret = true;
1000 goto bailout;
1002 p_delete(&confpath);
1005 bailout:
1007 p_delete(&confpath);
1009 if(xdg_files)
1011 for(buf = xdg_files; *buf; buf++)
1012 p_delete(buf);
1013 p_delete(&xdg_files);
1016 /* Assure there's at least one tag */
1017 for(screen = 0; screen < globalconf.nscreen; screen++)
1018 if(!globalconf.screens[screen].tags.len)
1019 tag_append_to_screen(tag_new("default", sizeof("default") - 1, layout_tile, 0.5, 1, 0),
1020 &globalconf.screens[screen]);
1021 return ret;
1024 /** Parse a command.
1025 * \param cmd The buffer to parse.
1026 * \return the number of elements pushed on the stack by the last statement in cmd.
1027 * If there's an error, the message is pushed onto the stack and this returns 1.
1029 static int
1030 luaA_docmd(const char *cmd)
1032 char *p;
1033 int newtop, oldtop = lua_gettop(globalconf.L);
1035 while((p = strchr(cmd, '\n')))
1037 newtop = lua_gettop(globalconf.L);
1038 lua_pop(globalconf.L, newtop - oldtop);
1039 oldtop = newtop;
1041 *p = '\0';
1042 if (luaL_dostring(globalconf.L, cmd))
1044 warn("error executing Lua code: %s", lua_tostring(globalconf.L, -1));
1045 return 1;
1047 cmd = p + 1;
1049 return lua_gettop(globalconf.L) - oldtop;
1052 /** Pushes a Lua array containing the top n elements of the stack.
1053 * \param n The number of elements to put in the array.
1055 static void
1056 luaA_array(int n)
1058 int i;
1059 lua_createtable(globalconf.L, n, 0);
1060 lua_insert(globalconf.L, -n - 1);
1062 for (i = n; i > 0; i--)
1063 lua_rawseti(globalconf.L, -i - 1, i);
1066 /** Maps the top n elements of the stack to the result of
1067 * applying a function to that element.
1068 * \param n The number of elements to map.
1069 * \luastack
1070 * \lparam The function to map the elements by. This should be
1071 * at position -(n + 1).
1073 static void
1074 luaA_map(int n)
1076 int i;
1077 for (i = 0; i < n; i++)
1079 lua_pushvalue(globalconf.L, -n - 1); /* copy of the function */
1080 lua_pushvalue(globalconf.L, -n - 1); /* value to map */
1081 lua_pcall(globalconf.L, 1, 1, 0); /* call function */
1082 lua_remove(globalconf.L, -n - 1); /* remove old value */
1084 lua_remove(globalconf.L, -n - 1); /* remove function */
1087 static void luaA_conn_cleanup(EV_P_ ev_io *w)
1089 ev_ref(EV_DEFAULT_UC);
1090 ev_io_stop(EV_DEFAULT_UC_ w);
1091 if (close(w->fd))
1092 warn("error closing UNIX domain socket: %s", strerror(errno));
1093 p_delete(&w);
1096 static void
1097 luaA_cb(EV_P_ ev_io *w, int revents)
1099 char buf[1024];
1100 int r, els;
1101 const char *s;
1102 size_t len;
1104 switch(r = recv(w->fd, buf, sizeof(buf)-1, MSG_TRUNC))
1106 case -1:
1107 warn("error reading UNIX domain socket: %s", strerror(errno));
1108 case 0: /* 0 bytes are only transferred when the connection is closed */
1109 luaA_conn_cleanup(EV_DEFAULT_UC_ w);
1110 break;
1111 default:
1112 if(r >= ssizeof(buf))
1113 break;
1114 buf[r] = '\0';
1115 lua_getglobal(globalconf.L, "table");
1116 lua_getfield(globalconf.L, -1, "concat");
1117 lua_remove(globalconf.L, -2); /* remove table */
1119 lua_getglobal(globalconf.L, "tostring");
1120 els = luaA_docmd(buf);
1121 luaA_map(els); /* map results to strings */
1122 luaA_array(els); /* put strings in an array */
1124 lua_pushstring(globalconf.L, "\t");
1125 lua_pcall(globalconf.L, 2, 1, 0); /* concatenate results with tabs */
1127 s = lua_tolstring(globalconf.L, -1, &len);
1129 /* ignore ENOENT because the client may not read */
1130 if (send(w->fd, s, len, MSG_DONTWAIT) == -1)
1131 switch(errno)
1133 case ENOENT:
1134 case EAGAIN:
1135 break;
1136 default:
1137 warn("can't send back to client via domain socket: %s", strerror(errno));
1138 break;
1141 lua_pop(globalconf.L, 1); /* pop the string */
1143 awesome_refresh(globalconf.connection);
1147 static void
1148 luaA_conn_cb(EV_P_ ev_io *w, int revents)
1150 ev_io *csio_conn = p_new(ev_io, 1);
1151 int csfd = accept(w->fd, NULL, NULL);
1153 ev_io_init(csio_conn, &luaA_cb, csfd, EV_READ);
1154 ev_io_start(EV_DEFAULT_UC_ csio_conn);
1155 ev_unref(EV_DEFAULT_UC);
1158 void
1159 luaA_cs_init(void)
1161 int csfd = socket_getclient();
1163 if (csfd < 0 || fcntl(csfd, F_SETFD, FD_CLOEXEC) == -1)
1164 return;
1166 addr = socket_getaddr(getenv("DISPLAY"));
1168 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1170 if(errno == EADDRINUSE)
1172 if(unlink(addr->sun_path))
1173 warn("error unlinking existing file: %s", strerror(errno));
1174 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1175 return warn("error binding UNIX domain socket: %s", strerror(errno));
1177 else
1178 return warn("error binding UNIX domain socket: %s", strerror(errno));
1180 listen(csfd, 10);
1182 ev_io_init(&csio, &luaA_conn_cb, csfd, EV_READ);
1183 ev_io_start(EV_DEFAULT_UC_ &csio);
1184 ev_unref(EV_DEFAULT_UC);
1187 void
1188 luaA_cs_cleanup(void)
1190 if(csio.fd < 0)
1191 return;
1192 ev_ref(EV_DEFAULT_UC);
1193 ev_io_stop(EV_DEFAULT_UC_ &csio);
1194 if (close(csio.fd))
1195 warn("error closing UNIX domain socket: %s", strerror(errno));
1196 if(unlink(addr->sun_path))
1197 warn("error unlinking UNIX domain socket: %s", strerror(errno));
1198 p_delete(&addr);
1199 csio.fd = -1;
1202 void
1203 luaA_on_timer(EV_P_ ev_timer *w, int revents)
1205 luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0, 0);
1206 awesome_refresh(globalconf.connection);
1209 /** Push a color as a string onto the stack
1210 * \param L The Lua VM state.
1211 * \param c The color to push.
1212 * \return The number of elements pushed on stack.
1215 luaA_pushcolor(lua_State *L, const xcolor_t *c)
1217 uint8_t r = (unsigned)c->red * 0xff / 0xffff;
1218 uint8_t g = (unsigned)c->green * 0xff / 0xffff;
1219 uint8_t b = (unsigned)c->blue * 0xff / 0xffff;
1220 uint8_t a = (unsigned)c->alpha * 0xff / 0xffff;
1221 char s[10];
1222 int len;
1223 /* do not print alpha if it's full */
1224 if(a == 0xff)
1225 len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
1226 else
1227 len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
1228 lua_pushlstring(L, s, len);
1229 return 1;