naughty: notify{run} gets notification object argument (FS#398)
[awesome.git] / luaa.c
blobf63e339a137aad0942f2d1a03453f68a73eb0e9a
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 "common/socket.h"
52 #include "common/buffer.h"
54 extern awesome_t globalconf;
56 extern const struct luaL_reg awesome_hooks_lib[];
57 extern const struct luaL_reg awesome_dbus_lib[];
58 extern const struct luaL_reg awesome_keygrabber_lib[];
59 extern const struct luaL_reg awesome_mousegrabber_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_tag_methods[];
71 extern const struct luaL_reg awesome_tag_meta[];
72 extern const struct luaL_reg awesome_widget_methods[];
73 extern const struct luaL_reg awesome_widget_meta[];
74 extern const struct luaL_reg awesome_wibox_methods[];
75 extern const struct luaL_reg awesome_wibox_meta[];
76 extern const struct luaL_reg awesome_keybinding_methods[];
77 extern const struct luaL_reg awesome_keybinding_meta[];
79 static struct sockaddr_un *addr;
80 static ev_io csio = { .fd = -1 };
82 #define XDG_CONFIG_HOME_DEFAULT "/.config"
84 /** Get or set global mouse bindings.
85 * This binding will be available when you'll click on root window.
86 * \param L The Lua VM state.
87 * \return The number of element pushed on stack.
88 * \luastack
89 * \lvalue A client.
90 * \lparam An array of mouse button bindings objects, or nothing.
91 * \return The array of mouse button bindings objects of this client.
93 static int
94 luaA_buttons(lua_State *L)
96 button_array_t *buttons = &globalconf.buttons;
98 if(lua_gettop(L) == 1)
99 luaA_button_array_set(L, 1, buttons);
101 return luaA_button_array_get(L, buttons);
104 /** Quit awesome.
105 * \param L The Lua VM state.
106 * \return The number of elements pushed on stack.
108 static int
109 luaA_quit(lua_State *L __attribute__ ((unused)))
111 ev_unloop(globalconf.loop, 1);
112 return 0;
115 /** Execute another application, probably a window manager, to replace
116 * awesome.
117 * \param L The Lua VM state.
118 * \return The number of elements pushed on stack.
119 * \luastack
120 * \lparam The command line to execute.
122 static int
123 luaA_exec(lua_State *L)
125 const char *cmd = luaL_checkstring(L, 1);
127 awesome_atexit();
129 a_exec(cmd);
130 return 0;
133 /** Restart awesome.
135 static int
136 luaA_restart(lua_State *L __attribute__ ((unused)))
138 awesome_restart();
139 return 0;
142 static void
143 luaA_openlib(lua_State *L, const char *name,
144 const struct luaL_reg methods[],
145 const struct luaL_reg meta[])
147 luaL_newmetatable(L, name); /* 1 */
148 lua_pushvalue(L, -1); /* dup metatable 2 */
149 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
151 luaL_register(L, NULL, meta); /* 1 */
152 luaL_register(L, name, methods); /* 2 */
153 lua_pushvalue(L, -1); /* dup self as metatable 3 */
154 lua_setmetatable(L, -2); /* set self as metatable 2 */
155 lua_pop(L, 2);
158 /** UTF-8 aware string length computing.
159 * \param L The Lua VM state.
160 * \return The number of elements pushed on stack.
162 static int
163 luaA_mbstrlen(lua_State *L)
165 const char *cmd = luaL_checkstring(L, 1);
166 lua_pushnumber(L, mbstowcs(NULL, NONULL(cmd), 0));
167 return 1;
170 /** Overload standard Lua next function to use __next key on metatable.
171 * \param L The Lua VM state.
172 * \param The number of elements pushed on stack.
174 static int
175 luaAe_next(lua_State *L)
177 if(luaL_getmetafield(L, 1, "__next"))
179 lua_insert(L, 1);
180 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
181 return lua_gettop(L);
184 luaL_checktype(L, 1, LUA_TTABLE);
185 lua_settop(L, 2);
186 if(lua_next(L, 1))
187 return 2;
188 lua_pushnil(L);
189 return 1;
192 /** Overload lua_next() function by using __next metatable field
193 * to get next elements.
194 * \param L The Lua VM stack.
195 * \param idx The index number of elements in stack.
196 * \return 1 if more elements to come, 0 otherwise.
199 luaA_next(lua_State *L, int idx)
201 if(luaL_getmetafield(L, idx, "__next"))
203 /* if idx is relative, reduce it since we got __next */
204 if(idx < 0) idx--;
205 /* copy table and then move key */
206 lua_pushvalue(L, idx);
207 lua_pushvalue(L, -3);
208 lua_remove(L, -4);
209 lua_pcall(L, 2, 2, 0);
210 /* next returned nil, it's the end */
211 if(lua_isnil(L, -1))
213 /* remove nil */
214 lua_pop(L, 2);
215 return 0;
217 return 1;
219 else if(lua_istable(L, idx))
220 return lua_next(L, idx);
221 /* remove the key */
222 lua_pop(L, 1);
223 return 0;
226 /** Generic pairs function.
227 * \param L The Lua VM state.
228 * \return The number of elements pushed on stack.
230 static int
231 luaA_generic_pairs(lua_State *L)
233 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
234 lua_pushvalue(L, 1); /* state, */
235 lua_pushnil(L); /* and initial value */
236 return 3;
239 /** Overload standard pairs function to use __pairs field of metatables.
240 * \param L The Lua VM state.
241 * \return The number of elements pushed on stack.
243 static int
244 luaAe_pairs(lua_State *L)
246 if(luaL_getmetafield(L, 1, "__pairs"))
248 lua_insert(L, 1);
249 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
250 return lua_gettop(L);
253 luaL_checktype(L, 1, LUA_TTABLE);
254 return luaA_generic_pairs(L);
257 static int
258 luaA_ipairs_aux(lua_State *L)
260 int i = luaL_checkint(L, 2) + 1;
261 luaL_checktype(L, 1, LUA_TTABLE);
262 lua_pushinteger(L, i);
263 lua_rawgeti(L, 1, i);
264 return (lua_isnil(L, -1)) ? 0 : 2;
267 /** Overload standard ipairs function to use __ipairs field of metatables.
268 * \param L The Lua VM state.
269 * \return The number of elements pushed on stack.
271 static int
272 luaAe_ipairs(lua_State *L)
274 if(luaL_getmetafield(L, 1, "__ipairs"))
276 lua_insert(L, 1);
277 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
278 return lua_gettop(L);
281 luaL_checktype(L, 1, LUA_TTABLE);
282 lua_pushvalue(L, lua_upvalueindex(1));
283 lua_pushvalue(L, 1);
284 lua_pushinteger(L, 0); /* and initial value */
285 return 3;
288 /** Enhanced type() function which recognize awesome objects.
289 * \param L The Lua VM state.
290 * \return The number of arguments pushed on the stack.
292 static int
293 luaAe_type(lua_State *L)
295 luaL_checkany(L, 1);
296 #define CHECK_TYPE(type) \
297 do { \
298 if(luaA_toudata(L, 1, #type)) \
300 lua_pushliteral(L, #type); \
301 return 1; \
303 } while(0)
304 CHECK_TYPE(wibox);
305 CHECK_TYPE(client);
306 CHECK_TYPE(image);
307 CHECK_TYPE(keybinding);
308 CHECK_TYPE(button);
309 CHECK_TYPE(tag);
310 CHECK_TYPE(widget);
311 #undef CHECK_TYPE
312 lua_pushstring(L, luaL_typename(L, 1));
313 return 1;
316 /** Replace various standards Lua functions with our own.
317 * \param L The Lua VM state.
319 static void
320 luaA_fixups(lua_State *L)
322 /* replace string.len */
323 lua_getglobal(L, "string");
324 lua_pushcfunction(L, luaA_mbstrlen);
325 lua_setfield(L, -2, "len");
326 lua_pop(L, 1);
327 /* replace next */
328 lua_pushliteral(L, "next");
329 lua_pushcfunction(L, luaAe_next);
330 lua_settable(L, LUA_GLOBALSINDEX);
331 /* replace pairs */
332 lua_pushliteral(L, "pairs");
333 lua_pushcfunction(L, luaAe_next);
334 lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
335 lua_settable(L, LUA_GLOBALSINDEX);
336 /* replace ipairs */
337 lua_pushliteral(L, "ipairs");
338 lua_pushcfunction(L, luaA_ipairs_aux);
339 lua_pushcclosure(L, luaAe_ipairs, 1);
340 lua_settable(L, LUA_GLOBALSINDEX);
341 /* replace type */
342 lua_pushliteral(L, "type");
343 lua_pushcfunction(L, luaAe_type);
344 lua_settable(L, LUA_GLOBALSINDEX);
347 /** __next function for wtable objects.
348 * \param L The Lua VM state.
349 * \return The number of elements pushed on stack.
351 static int
352 luaA_wtable_next(lua_State *L)
354 /* upvalue 1 is content table */
355 if(lua_next(L, lua_upvalueindex(1)))
356 return 2;
357 lua_pushnil(L);
358 return 1;
361 /** __ipairs function for wtable objects.
362 * \param L The Lua VM state.
363 * \return The number of elements pushed on stack.
365 static int
366 luaA_wtable_ipairs(lua_State *L)
368 /* push ipairs_aux */
369 lua_pushvalue(L, lua_upvalueindex(2));
370 /* push content table */
371 lua_pushvalue(L, lua_upvalueindex(1));
372 lua_pushinteger(L, 0); /* and initial value */
373 return 3;
376 /** Index function of wtable objects.
377 * \param L The Lua VM state.
378 * \return The number of elements pushed on stack.
380 static int
381 luaA_wtable_index(lua_State *L)
383 size_t len;
384 const char *buf;
386 lua_pushvalue(L, 2);
387 /* check for size, waiting lua 5.2 and __len on tables */
388 if((buf = lua_tolstring(L, -1, &len)))
389 if(a_tokenize(buf, len) == A_TK_LEN)
391 lua_pushnumber(L, lua_objlen(L, lua_upvalueindex(1)));
392 return 1;
394 lua_pop(L, 1);
396 /* upvalue 1 is content table */
397 lua_rawget(L, lua_upvalueindex(1));
398 return 1;
401 /** Newndex function of wtable objects.
402 * \param L The Lua VM state.
403 * \return The number of elements pushed on stack.
405 static int
406 luaA_wtable_newindex(lua_State *L)
408 bool invalid = false;
410 /* push key on top */
411 lua_pushvalue(L, 2);
412 /* get current key value in content table */
413 lua_rawget(L, lua_upvalueindex(1));
414 /* if value is a widget, notify change */
415 if(lua_istable(L, -1) || luaA_toudata(L, -1, "widget"))
416 invalid = true;
418 lua_pop(L, 1); /* remove value */
420 /* if new value is a widget or a table */
421 if(lua_istable(L, 3))
423 luaA_table2wtable(L);
424 invalid = true;
426 else if(!invalid && luaA_toudata(L, 3, "widget"))
427 invalid = true;
429 /* upvalue 1 is content table */
430 lua_rawset(L, lua_upvalueindex(1));
432 if(invalid)
433 luaA_wibox_invalidate_byitem(L, lua_topointer(L, 1));
435 return 0;
438 /** Convert the top element of the stack to a proxied wtable.
439 * \param L The Lua VM state.
441 void
442 luaA_table2wtable(lua_State *L)
444 if(!lua_istable(L, -1))
445 return;
447 lua_newtable(L); /* create *real* content table */
448 lua_newtable(L); /* metatable */
449 lua_pushvalue(L, -2); /* copy content table */
450 lua_pushcfunction(L, luaA_ipairs_aux); /* push ipairs aux */
451 lua_pushcclosure(L, luaA_wtable_ipairs, 2);
452 lua_pushvalue(L, -3); /* copy content table */
453 lua_pushcclosure(L, luaA_wtable_next, 1); /* __next has the content table as upvalue */
454 lua_pushvalue(L, -4); /* copy content table */
455 lua_pushcclosure(L, luaA_wtable_index, 1); /* __index has the content table as upvalue */
456 lua_pushvalue(L, -5); /* copy content table */
457 lua_pushcclosure(L, luaA_wtable_newindex, 1); /* __newindex has the content table as upvalue */
458 /* set metatable field with just pushed closure */
459 lua_setfield(L, -5, "__newindex");
460 lua_setfield(L, -4, "__index");
461 lua_setfield(L, -3, "__next");
462 lua_setfield(L, -2, "__ipairs");
463 /* set metatable impossible to touch */
464 lua_pushliteral(L, "wtable");
465 lua_setfield(L, -2, "__metatable");
466 /* set new metatable on original table */
467 lua_setmetatable(L, -3);
469 /* initial key */
470 lua_pushnil(L);
471 /* go through original table */
472 while(lua_next(L, -3))
474 /* if convert value to wtable */
475 luaA_table2wtable(L);
476 /* copy key */
477 lua_pushvalue(L, -2);
478 /* move key before value */
479 lua_insert(L, -2);
480 /* set same value in content table */
481 lua_rawset(L, -4);
482 /* copy key */
483 lua_pushvalue(L, -1);
484 /* push the new value :-> */
485 lua_pushnil(L);
486 /* set orig[k] = nil */
487 lua_rawset(L, -5);
489 /* remove content table */
490 lua_pop(L, 1);
493 /** Look for an item: table, function, etc.
494 * \param L The Lua VM state.
495 * \param item The pointer item.
497 bool
498 luaA_hasitem(lua_State *L, const void *item)
500 lua_pushnil(L);
501 while(luaA_next(L, -2))
503 if(lua_topointer(L, -1) == item)
505 /* remove value and key */
506 lua_pop(L, 2);
507 return true;
509 if(lua_istable(L, -1))
510 if(luaA_hasitem(L, item))
512 /* remove key and value */
513 lua_pop(L, 2);
514 return true;
516 /* remove value */
517 lua_pop(L, 1);
519 return false;
522 /** Check if a table is a loop. When using table as direct acyclic digram,
523 * this is useful.
524 * \param L The Lua VM state.
525 * \param idx The index of the table in the stack
526 * \return True if the table loops.
528 bool
529 luaA_isloop(lua_State *L, int idx)
531 if(lua_istable(L, idx))
533 lua_pushvalue(L, idx); /* push table on top */
534 if(luaA_hasitem(L, lua_topointer(L, -1)))
536 lua_pop(L, 1); /* remove pushed table */
537 return true;
539 lua_pushnil(L);
540 while(luaA_next(L, -2))
542 /* check for recursivity */
543 if(luaA_isloop(L, -1))
545 lua_pop(L, 2); /* remove key and value */
546 return true;
548 lua_pop(L, 1); /* remove value */
550 lua_pop(L, 1); /* remove pushed table */
552 return false;
555 /** Object table.
556 * This table can use safely object as key.
557 * \param L The Lua VM state.
558 * \return The number of elements pushed on stack.
561 luaA_otable_index(lua_State *L)
563 void **obj, **v;
565 if((obj = lua_touserdata(L, 2)))
567 /* begins at nil */
568 lua_pushnil(L);
569 while(lua_next(L, 1))
571 /* check the key against the key if the key is a userdata,
572 * otherwise check it again the value. */
573 if((v = lua_touserdata(L, -2))
574 && *v == *obj)
575 /* return value */
576 return 1;
577 else if((v = lua_touserdata(L, -1))
578 && *v == *obj)
580 /* return key */
581 lua_pop(L, 1);
582 return 1;
584 /* removes 'value'; keeps 'key' for next iteration */
585 lua_pop(L, 1);
587 return 0;
590 lua_rawget(L, 1);
591 return 1;
594 /** Object table.
595 * This table can use safely object as key.
596 * \param L The Lua VM state.
597 * \return The number of elements pushed on stack.
599 static int
600 luaA_otable_newindex(lua_State *L)
602 void **obj, **v;
604 if((obj = lua_touserdata(L, 2)))
606 /* begins at nil */
607 lua_pushnil(L);
608 while(lua_next(L, 1))
610 if((v = lua_touserdata(L, -2))
611 && *v == *obj)
613 /* remove value */
614 lua_pop(L, 1);
615 /* push new value on top */
616 lua_pushvalue(L, 3);
617 /* set in table key = value */
618 lua_rawset(L, 1);
619 return 0;
621 /* removes 'value'; keeps 'key' for next iteration */
622 lua_pop(L, 1);
626 lua_rawset(L, 1);
628 return 0;
631 /** Spawn a program.
632 * This function is multi-head (Zaphod) aware and will set display to
633 * the right screen according to mouse position.
634 * \param L The Lua VM state.
635 * \return The number of elements pushed on stack
636 * \luastack
637 * \lparam The command to launch.
638 * \lparam The optional screen number to spawn the command on.
640 static int
641 luaA_spawn(lua_State *L)
643 char *host, newdisplay[128];
644 const char *cmd;
645 int screen = 0, screenp, displayp;
647 if(lua_gettop(L) == 2)
649 screen = luaL_checknumber(L, 2) - 1;
650 luaA_checkscreen(screen);
653 cmd = luaL_checkstring(L, 1);
655 if(!globalconf.xinerama_is_active)
657 xcb_parse_display(NULL, &host, &displayp, &screenp);
658 snprintf(newdisplay, sizeof(newdisplay), "%s:%d.%d", host, displayp, screen);
659 setenv("DISPLAY", newdisplay, 1);
660 p_delete(&host);
663 /* The double-fork construct avoids zombie processes and keeps the code
664 * clean from stupid signal handlers. */
665 if(fork() == 0)
667 if(fork() == 0)
669 if(globalconf.connection)
670 xcb_disconnect(globalconf.connection);
671 setsid();
672 a_exec(cmd);
673 warn("execl '%s' failed: %s\n", cmd, strerror(errno));
675 exit(EXIT_SUCCESS);
677 wait(0);
679 return 0;
682 /** awesome global table.
683 * \param L The Lua VM state.
684 * \return The number of elements pushed on stack.
685 * \luastack
686 * \lfield font The default font.
687 * \lfield conffile The configuration file which has been loaded.
689 static int
690 luaA_awesome_index(lua_State *L)
692 if(luaA_usemetatable(L, 1, 2))
693 return 1;
695 size_t len;
696 const char *buf = luaL_checklstring(L, 2, &len);
698 switch(a_tokenize(buf, len))
700 case A_TK_FONT:
702 char *font = pango_font_description_to_string(globalconf.font->desc);
703 lua_pushstring(L, font);
704 g_free(font);
706 break;
707 case A_TK_CONFFILE:
708 lua_pushstring(L, globalconf.conffile);
709 break;
710 case A_TK_FG:
711 luaA_pushcolor(L, &globalconf.colors.fg);
712 break;
713 case A_TK_BG:
714 luaA_pushcolor(L, &globalconf.colors.bg);
715 break;
716 default:
717 return 0;
720 return 1;
723 /** Newindex function for the awesome global table.
724 * \param L The Lua VM state.
725 * \return The number of elements pushed on stack.
727 static int
728 luaA_awesome_newindex(lua_State *L)
730 if(luaA_usemetatable(L, 1, 2))
731 return 1;
733 size_t len;
734 const char *buf = luaL_checklstring(L, 2, &len);
736 switch(a_tokenize(buf, len))
738 case A_TK_FONT:
740 const char *newfont = luaL_checkstring(L, 3);
741 draw_font_delete(&globalconf.font);
742 globalconf.font = draw_font_new(newfont);
744 break;
745 case A_TK_FG:
746 if((buf = luaL_checklstring(L, 3, &len)))
747 xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.fg, buf, len));
748 break;
749 case A_TK_BG:
750 if((buf = luaL_checklstring(L, 3, &len)))
751 xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.bg, buf, len));
752 break;
753 default:
754 return 0;
757 return 0;
760 /** Initialize the Lua VM
762 void
763 luaA_init(void)
765 lua_State *L;
767 static const struct luaL_reg otable_methods[] =
769 { "__call", luaA_otable_new },
770 { NULL, NULL }
772 static const struct luaL_reg otable_meta[] =
774 { "__index", luaA_otable_index },
775 { "__newindex", luaA_otable_newindex },
776 { NULL, NULL }
778 static const struct luaL_reg awesome_lib[] =
780 { "quit", luaA_quit },
781 { "exec", luaA_exec },
782 { "spawn", luaA_spawn },
783 { "restart", luaA_restart },
784 { "buttons", luaA_buttons },
785 { "__index", luaA_awesome_index },
786 { "__newindex", luaA_awesome_newindex },
787 { NULL, NULL }
790 L = globalconf.L = luaL_newstate();
792 luaL_openlibs(L);
794 luaA_fixups(L);
796 /* Export awesome lib */
797 luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
799 /* Export hooks lib */
800 luaL_register(L, "hooks", awesome_hooks_lib);
802 /* Export D-Bus lib */
803 luaL_register(L, "dbus", awesome_dbus_lib);
805 /* Export keygrabber lib */
806 luaL_register(L, "keygrabber", awesome_keygrabber_lib);
808 /* Export mousegrabber lib */
809 luaL_register(L, "mousegrabber", awesome_mousegrabber_lib);
811 /* Export otable lib */
812 luaA_openlib(L, "otable", otable_methods, otable_meta);
814 /* Export screen */
815 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
817 /* Export mouse */
818 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
820 /* Export button */
821 luaA_openlib(L, "button", awesome_button_methods, awesome_button_meta);
823 /* Export image */
824 luaA_openlib(L, "image", awesome_image_methods, awesome_image_meta);
826 /* Export tag */
827 luaA_openlib(L, "tag", awesome_tag_methods, awesome_tag_meta);
829 /* Export wibox */
830 luaA_openlib(L, "wibox", awesome_wibox_methods, awesome_wibox_meta);
832 /* Export widget */
833 luaA_openlib(L, "widget", awesome_widget_methods, awesome_widget_meta);
835 /* Export client */
836 luaA_openlib(L, "client", awesome_client_methods, awesome_client_meta);
838 /* Export keys */
839 luaA_openlib(L, "keybinding", awesome_keybinding_methods, awesome_keybinding_meta);
841 lua_pushliteral(L, "AWESOME_VERSION");
842 lua_pushstring(L, AWESOME_VERSION);
843 lua_settable(L, LUA_GLOBALSINDEX);
845 lua_pushliteral(L, "AWESOME_RELEASE");
846 lua_pushstring(L, AWESOME_RELEASE);
847 lua_settable(L, LUA_GLOBALSINDEX);
849 /* init hooks */
850 globalconf.hooks.manage = LUA_REFNIL;
851 globalconf.hooks.unmanage = LUA_REFNIL;
852 globalconf.hooks.focus = LUA_REFNIL;
853 globalconf.hooks.unfocus = LUA_REFNIL;
854 globalconf.hooks.mouse_enter = LUA_REFNIL;
855 globalconf.hooks.mouse_leave = LUA_REFNIL;
856 globalconf.hooks.arrange = LUA_REFNIL;
857 globalconf.hooks.clients = LUA_REFNIL;
858 globalconf.hooks.tags = LUA_REFNIL;
859 globalconf.hooks.tagged = LUA_REFNIL;
860 globalconf.hooks.property = LUA_REFNIL;
861 globalconf.hooks.timer = LUA_REFNIL;
862 #ifdef WITH_DBUS
863 globalconf.hooks.dbus = LUA_REFNIL;
864 #endif
866 /* add Lua lib path (/usr/share/awesome/lib by default) */
867 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?.lua\"");
868 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?/init.lua\"");
870 /* add XDG_CONFIG_DIR (/etc/xdg/awesome by default) as include path */
871 luaA_dostring(L, "package.path = package.path .. \";" XDG_CONFIG_DIR "/awesome/?.lua\"");
872 luaA_dostring(L, "package.path = package.path .. \";" XDG_CONFIG_DIR "/awesome/?/init.lua\"");
874 /* add either XDG_CONFIG_HOME/awesome or HOME/.config/awesome to path */
875 char *dir;
876 if((dir = getenv("XDG_CONFIG_HOME")))
878 char *path;
879 a_asprintf(&path, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"", dir, dir);
880 luaA_dostring(globalconf.L, path);
881 p_delete(&path);
883 else
885 char *path, *homedir = getenv("HOME");
886 a_asprintf(&path,
887 "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?.lua;%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?/init.lua\"",
888 homedir, homedir);
889 luaA_dostring(globalconf.L, path);
890 p_delete(&path);
893 /* add XDG_CONFIG_DIRS to include paths */
894 char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
895 ssize_t len;
897 if((len = a_strlen(xdg_config_dirs)))
899 char **buf, **xdg_files = a_strsplit(xdg_config_dirs, len, ':');
901 for(buf = xdg_files; *buf; buf++)
903 char *confpath;
904 a_asprintf(&confpath, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"",
905 *buf, *buf);
906 luaA_dostring(globalconf.L, confpath);
907 p_delete(&confpath);
910 for(buf = xdg_files; *buf; buf++)
911 p_delete(buf);
912 p_delete(&xdg_files);
916 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
918 static bool
919 luaA_loadrc(const char *confpath, bool run)
921 if(confpath)
923 if(!luaL_loadfile(globalconf.L, confpath))
925 if(run)
927 if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
928 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
929 else
931 globalconf.conffile = a_strdup(confpath);
932 return true;
935 else
936 lua_pop(globalconf.L, 1);
937 return true;
939 else
940 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
942 return false;
945 /** Load a configuration file.
946 * \param confpatharg The configuration file to load.
947 * \param run Run the configuration file.
949 bool
950 luaA_parserc(const char *confpatharg, bool run)
952 int screen;
953 const char *confdir, *xdg_config_dirs;
954 char *confpath = NULL, **xdg_files = NULL, **buf;
955 ssize_t len;
956 bool ret = false;
958 /* try to load, return if it's ok */
959 if(luaA_loadrc(confpatharg, run))
961 ret = true;
962 goto bailout;
965 if((confdir = getenv("XDG_CONFIG_HOME")))
966 a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, confdir);
967 else
968 a_asprintf(&confpath, "%s" XDG_CONFIG_HOME_DEFAULT AWESOME_CONFIG_FILE, getenv("HOME"));
970 /* try to run XDG_CONFIG_HOME/awesome/rc.lua */
971 if(luaA_loadrc(confpath, run))
973 ret = true;
974 goto bailout;
977 p_delete(&confpath);
979 xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
981 if(!(len = a_strlen(xdg_config_dirs)))
983 xdg_config_dirs = XDG_CONFIG_DIR;
984 len = sizeof(XDG_CONFIG_DIR) - 1;
987 xdg_files = a_strsplit(xdg_config_dirs, len, ':');
989 for(buf = xdg_files; *buf && !ret; buf++)
991 a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, *buf);
992 if(luaA_loadrc(confpath, run))
994 ret = true;
995 goto bailout;
997 p_delete(&confpath);
1000 bailout:
1002 p_delete(&confpath);
1004 if(xdg_files)
1006 for(buf = xdg_files; *buf; buf++)
1007 p_delete(buf);
1008 p_delete(&xdg_files);
1011 /* Assure there's at least one tag */
1012 for(screen = 0; screen < globalconf.nscreen; screen++)
1013 if(!globalconf.screens[screen].tags.len)
1014 tag_append_to_screen(tag_new("default", sizeof("default") - 1),
1015 &globalconf.screens[screen]);
1016 return ret;
1019 /** Parse a command.
1020 * \param cmd The buffer to parse.
1021 * \return the number of elements pushed on the stack by the last statement in cmd.
1022 * If there's an error, the message is pushed onto the stack and this returns 1.
1024 static int
1025 luaA_docmd(const char *cmd)
1027 char *p;
1028 int newtop, oldtop = lua_gettop(globalconf.L);
1030 while((p = strchr(cmd, '\n')))
1032 newtop = lua_gettop(globalconf.L);
1033 lua_pop(globalconf.L, newtop - oldtop);
1034 oldtop = newtop;
1036 *p = '\0';
1037 if (luaL_dostring(globalconf.L, cmd))
1039 warn("error executing Lua code: %s", lua_tostring(globalconf.L, -1));
1040 return 1;
1042 cmd = p + 1;
1044 return lua_gettop(globalconf.L) - oldtop;
1047 /** Pushes a Lua array containing the top n elements of the stack.
1048 * \param n The number of elements to put in the array.
1050 static void
1051 luaA_array(int n)
1053 int i;
1054 lua_createtable(globalconf.L, n, 0);
1055 lua_insert(globalconf.L, -n - 1);
1057 for (i = n; i > 0; i--)
1058 lua_rawseti(globalconf.L, -i - 1, i);
1061 /** Maps the top n elements of the stack to the result of
1062 * applying a function to that element.
1063 * \param n The number of elements to map.
1064 * \luastack
1065 * \lparam The function to map the elements by. This should be
1066 * at position -(n + 1).
1068 static void
1069 luaA_map(int n)
1071 int i;
1072 for (i = 0; i < n; i++)
1074 lua_pushvalue(globalconf.L, -n - 1); /* copy of the function */
1075 lua_pushvalue(globalconf.L, -n - 1); /* value to map */
1076 lua_pcall(globalconf.L, 1, 1, 0); /* call function */
1077 lua_remove(globalconf.L, -n - 1); /* remove old value */
1079 lua_remove(globalconf.L, -n - 1); /* remove function */
1082 static void luaA_conn_cleanup(EV_P_ ev_io *w)
1084 ev_ref(EV_DEFAULT_UC);
1085 ev_io_stop(EV_DEFAULT_UC_ w);
1086 if (close(w->fd))
1087 warn("error closing UNIX domain socket: %s", strerror(errno));
1088 p_delete(&w);
1091 static void
1092 luaA_cb(EV_P_ ev_io *w, int revents)
1094 char buf[1024];
1095 int r, els;
1096 const char *s;
1097 size_t len;
1099 switch(r = recv(w->fd, buf, sizeof(buf)-1, MSG_TRUNC))
1101 case -1:
1102 warn("error reading UNIX domain socket: %s", strerror(errno));
1103 case 0: /* 0 bytes are only transferred when the connection is closed */
1104 luaA_conn_cleanup(EV_DEFAULT_UC_ w);
1105 break;
1106 default:
1107 if(r >= ssizeof(buf))
1108 break;
1109 buf[r] = '\0';
1110 lua_getglobal(globalconf.L, "table");
1111 lua_getfield(globalconf.L, -1, "concat");
1112 lua_remove(globalconf.L, -2); /* remove table */
1114 lua_getglobal(globalconf.L, "tostring");
1115 els = luaA_docmd(buf);
1116 luaA_map(els); /* map results to strings */
1117 luaA_array(els); /* put strings in an array */
1119 lua_pushstring(globalconf.L, "\t");
1120 lua_pcall(globalconf.L, 2, 1, 0); /* concatenate results with tabs */
1122 s = lua_tolstring(globalconf.L, -1, &len);
1124 /* ignore ENOENT because the client may not read */
1125 if (send(w->fd, s, len, MSG_DONTWAIT) == -1)
1126 switch(errno)
1128 case ENOENT:
1129 case EAGAIN:
1130 break;
1131 default:
1132 warn("can't send back to client via domain socket: %s", strerror(errno));
1133 break;
1136 lua_pop(globalconf.L, 1); /* pop the string */
1138 awesome_refresh(globalconf.connection);
1142 static void
1143 luaA_conn_cb(EV_P_ ev_io *w, int revents)
1145 ev_io *csio_conn = p_new(ev_io, 1);
1146 int csfd = accept(w->fd, NULL, NULL);
1148 ev_io_init(csio_conn, &luaA_cb, csfd, EV_READ);
1149 ev_io_start(EV_DEFAULT_UC_ csio_conn);
1150 ev_unref(EV_DEFAULT_UC);
1153 void
1154 luaA_cs_init(void)
1156 int csfd = socket_getclient();
1158 if (csfd < 0 || fcntl(csfd, F_SETFD, FD_CLOEXEC) == -1)
1159 return;
1161 addr = socket_getaddr(getenv("DISPLAY"));
1163 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1165 if(errno == EADDRINUSE)
1167 if(unlink(addr->sun_path))
1168 warn("error unlinking existing file: %s", strerror(errno));
1169 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1170 return warn("error binding UNIX domain socket: %s", strerror(errno));
1172 else
1173 return warn("error binding UNIX domain socket: %s", strerror(errno));
1175 listen(csfd, 10);
1177 ev_io_init(&csio, &luaA_conn_cb, csfd, EV_READ);
1178 ev_io_start(EV_DEFAULT_UC_ &csio);
1179 ev_unref(EV_DEFAULT_UC);
1182 void
1183 luaA_cs_cleanup(void)
1185 if(csio.fd < 0)
1186 return;
1187 ev_ref(EV_DEFAULT_UC);
1188 ev_io_stop(EV_DEFAULT_UC_ &csio);
1189 if (close(csio.fd))
1190 warn("error closing UNIX domain socket: %s", strerror(errno));
1191 if(unlink(addr->sun_path))
1192 warn("error unlinking UNIX domain socket: %s", strerror(errno));
1193 p_delete(&addr);
1194 csio.fd = -1;
1197 void
1198 luaA_on_timer(EV_P_ ev_timer *w, int revents)
1200 if(globalconf.hooks.timer != LUA_REFNIL)
1201 luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0, 0);
1202 awesome_refresh(globalconf.connection);
1205 /** Push a color as a string onto the stack
1206 * \param L The Lua VM state.
1207 * \param c The color to push.
1208 * \return The number of elements pushed on stack.
1211 luaA_pushcolor(lua_State *L, const xcolor_t *c)
1213 uint8_t r = (unsigned)c->red * 0xff / 0xffff;
1214 uint8_t g = (unsigned)c->green * 0xff / 0xffff;
1215 uint8_t b = (unsigned)c->blue * 0xff / 0xffff;
1216 uint8_t a = (unsigned)c->alpha * 0xff / 0xffff;
1217 char s[10];
1218 int len;
1219 /* do not print alpha if it's full */
1220 if(a == 0xff)
1221 len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
1222 else
1223 len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
1224 lua_pushlstring(L, s, len);
1225 return 1;