awesomerc: do not change windows screen if filtered out.
[awesome.git] / luaa.c
blob6e6a379c26497ecb6838ac936471825a1a643c86
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_key_methods[];
77 extern const struct luaL_reg awesome_key_meta[];
78 extern const struct luaL_reg awesome_keybinding_methods[];
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 /** Enhanced type() function which recognize awesome objects.
290 * \param L The Lua VM state.
291 * \return The number of arguments pushed on the stack.
293 static int
294 luaAe_type(lua_State *L)
296 luaL_checkany(L, 1);
297 #define CHECK_TYPE(type) \
298 do { \
299 if(luaA_toudata(L, 1, #type)) \
301 lua_pushliteral(L, #type); \
302 return 1; \
304 } while(0)
305 CHECK_TYPE(wibox);
306 CHECK_TYPE(client);
307 CHECK_TYPE(image);
308 CHECK_TYPE(key);
309 CHECK_TYPE(button);
310 CHECK_TYPE(tag);
311 CHECK_TYPE(widget);
312 #undef CHECK_TYPE
313 lua_pushstring(L, luaL_typename(L, 1));
314 return 1;
317 /** Replace various standards Lua functions with our own.
318 * \param L The Lua VM state.
320 static void
321 luaA_fixups(lua_State *L)
323 /* export string.wlen */
324 lua_getglobal(L, "string");
325 lua_pushcfunction(L, luaA_mbstrlen);
326 lua_setfield(L, -2, "wlen");
327 lua_pop(L, 1);
328 /* replace next */
329 lua_pushliteral(L, "next");
330 lua_pushcfunction(L, luaAe_next);
331 lua_settable(L, LUA_GLOBALSINDEX);
332 /* replace pairs */
333 lua_pushliteral(L, "pairs");
334 lua_pushcfunction(L, luaAe_next);
335 lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
336 lua_settable(L, LUA_GLOBALSINDEX);
337 /* replace ipairs */
338 lua_pushliteral(L, "ipairs");
339 lua_pushcfunction(L, luaA_ipairs_aux);
340 lua_pushcclosure(L, luaAe_ipairs, 1);
341 lua_settable(L, LUA_GLOBALSINDEX);
342 /* replace type */
343 lua_pushliteral(L, "type");
344 lua_pushcfunction(L, luaAe_type);
345 lua_settable(L, LUA_GLOBALSINDEX);
348 /** __next function for wtable objects.
349 * \param L The Lua VM state.
350 * \return The number of elements pushed on stack.
352 static int
353 luaA_wtable_next(lua_State *L)
355 /* upvalue 1 is content table */
356 if(lua_next(L, lua_upvalueindex(1)))
357 return 2;
358 lua_pushnil(L);
359 return 1;
362 /** __ipairs function for wtable objects.
363 * \param L The Lua VM state.
364 * \return The number of elements pushed on stack.
366 static int
367 luaA_wtable_ipairs(lua_State *L)
369 /* push ipairs_aux */
370 lua_pushvalue(L, lua_upvalueindex(2));
371 /* push content table */
372 lua_pushvalue(L, lua_upvalueindex(1));
373 lua_pushinteger(L, 0); /* and initial value */
374 return 3;
377 /** Index function of wtable objects.
378 * \param L The Lua VM state.
379 * \return The number of elements pushed on stack.
381 static int
382 luaA_wtable_index(lua_State *L)
384 size_t len;
385 const char *buf;
387 lua_pushvalue(L, 2);
388 /* check for size, waiting lua 5.2 and __len on tables */
389 if((buf = lua_tolstring(L, -1, &len)))
390 if(a_tokenize(buf, len) == A_TK_LEN)
392 lua_pushnumber(L, lua_objlen(L, lua_upvalueindex(1)));
393 return 1;
395 lua_pop(L, 1);
397 /* upvalue 1 is content table */
398 lua_rawget(L, lua_upvalueindex(1));
399 return 1;
402 /** Newndex function of wtable objects.
403 * \param L The Lua VM state.
404 * \return The number of elements pushed on stack.
406 static int
407 luaA_wtable_newindex(lua_State *L)
409 bool invalid = false;
411 /* push key on top */
412 lua_pushvalue(L, 2);
413 /* get current key value in content table */
414 lua_rawget(L, lua_upvalueindex(1));
415 /* if value is a widget, notify change */
416 if(lua_istable(L, -1) || luaA_toudata(L, -1, "widget"))
417 invalid = true;
419 lua_pop(L, 1); /* remove value */
421 /* if new value is a widget or a table */
422 if(lua_istable(L, 3))
424 luaA_table2wtable(L);
425 invalid = true;
427 else if(!invalid && luaA_toudata(L, 3, "widget"))
428 invalid = true;
430 /* upvalue 1 is content table */
431 lua_rawset(L, lua_upvalueindex(1));
433 if(invalid)
434 luaA_wibox_invalidate_byitem(L, lua_topointer(L, 1));
436 return 0;
439 /** Convert the top element of the stack to a proxied wtable.
440 * \param L The Lua VM state.
442 void
443 luaA_table2wtable(lua_State *L)
445 if(!lua_istable(L, -1))
446 return;
448 lua_newtable(L); /* create *real* content table */
449 lua_newtable(L); /* metatable */
450 lua_pushvalue(L, -2); /* copy content table */
451 lua_pushcfunction(L, luaA_ipairs_aux); /* push ipairs aux */
452 lua_pushcclosure(L, luaA_wtable_ipairs, 2);
453 lua_pushvalue(L, -3); /* copy content table */
454 lua_pushcclosure(L, luaA_wtable_next, 1); /* __next has the content table as upvalue */
455 lua_pushvalue(L, -4); /* copy content table */
456 lua_pushcclosure(L, luaA_wtable_index, 1); /* __index has the content table as upvalue */
457 lua_pushvalue(L, -5); /* copy content table */
458 lua_pushcclosure(L, luaA_wtable_newindex, 1); /* __newindex has the content table as upvalue */
459 /* set metatable field with just pushed closure */
460 lua_setfield(L, -5, "__newindex");
461 lua_setfield(L, -4, "__index");
462 lua_setfield(L, -3, "__next");
463 lua_setfield(L, -2, "__ipairs");
464 /* set metatable impossible to touch */
465 lua_pushliteral(L, "wtable");
466 lua_setfield(L, -2, "__metatable");
467 /* set new metatable on original table */
468 lua_setmetatable(L, -3);
470 /* initial key */
471 lua_pushnil(L);
472 /* go through original table */
473 while(lua_next(L, -3))
475 /* if convert value to wtable */
476 luaA_table2wtable(L);
477 /* copy key */
478 lua_pushvalue(L, -2);
479 /* move key before value */
480 lua_insert(L, -2);
481 /* set same value in content table */
482 lua_rawset(L, -4);
483 /* copy key */
484 lua_pushvalue(L, -1);
485 /* push the new value :-> */
486 lua_pushnil(L);
487 /* set orig[k] = nil */
488 lua_rawset(L, -5);
490 /* remove content table */
491 lua_pop(L, 1);
494 /** Look for an item: table, function, etc.
495 * \param L The Lua VM state.
496 * \param item The pointer item.
498 bool
499 luaA_hasitem(lua_State *L, const void *item)
501 lua_pushnil(L);
502 while(luaA_next(L, -2))
504 if(lua_topointer(L, -1) == item)
506 /* remove value and key */
507 lua_pop(L, 2);
508 return true;
510 if(lua_istable(L, -1))
511 if(luaA_hasitem(L, item))
513 /* remove key and value */
514 lua_pop(L, 2);
515 return true;
517 /* remove value */
518 lua_pop(L, 1);
520 return false;
523 /** Check if a table is a loop. When using table as direct acyclic digram,
524 * this is useful.
525 * \param L The Lua VM state.
526 * \param idx The index of the table in the stack
527 * \return True if the table loops.
529 bool
530 luaA_isloop(lua_State *L, int idx)
532 if(lua_istable(L, idx))
534 lua_pushvalue(L, idx); /* push table on top */
535 if(luaA_hasitem(L, lua_topointer(L, -1)))
537 lua_pop(L, 1); /* remove pushed table */
538 return true;
540 lua_pushnil(L);
541 while(luaA_next(L, -2))
543 /* check for recursivity */
544 if(luaA_isloop(L, -1))
546 lua_pop(L, 2); /* remove key and value */
547 return true;
549 lua_pop(L, 1); /* remove value */
551 lua_pop(L, 1); /* remove pushed table */
553 return false;
556 /** Object table.
557 * This table can use safely object as key.
558 * \param L The Lua VM state.
559 * \return The number of elements pushed on stack.
562 luaA_otable_index(lua_State *L)
564 void **obj, **v;
566 if((obj = lua_touserdata(L, 2)))
568 /* begins at nil */
569 lua_pushnil(L);
570 while(lua_next(L, 1))
572 /* check the key against the key if the key is a userdata,
573 * otherwise check it again the value. */
574 if((v = lua_touserdata(L, -2))
575 && *v == *obj)
576 /* return value */
577 return 1;
578 else if((v = lua_touserdata(L, -1))
579 && *v == *obj)
581 /* return key */
582 lua_pop(L, 1);
583 return 1;
585 /* removes 'value'; keeps 'key' for next iteration */
586 lua_pop(L, 1);
588 return 0;
591 lua_rawget(L, 1);
592 return 1;
595 /** Object table.
596 * This table can use safely object as key.
597 * \param L The Lua VM state.
598 * \return The number of elements pushed on stack.
600 static int
601 luaA_otable_newindex(lua_State *L)
603 void **obj, **v;
605 if((obj = lua_touserdata(L, 2)))
607 /* begins at nil */
608 lua_pushnil(L);
609 while(lua_next(L, 1))
611 if((v = lua_touserdata(L, -2))
612 && *v == *obj)
614 /* remove value */
615 lua_pop(L, 1);
616 /* push new value on top */
617 lua_pushvalue(L, 3);
618 /* set in table key = value */
619 lua_rawset(L, 1);
620 return 0;
622 /* removes 'value'; keeps 'key' for next iteration */
623 lua_pop(L, 1);
627 lua_rawset(L, 1);
629 return 0;
632 /** Spawn a program.
633 * This function is multi-head (Zaphod) aware and will set display to
634 * the right screen according to mouse position.
635 * \param L The Lua VM state.
636 * \return The number of elements pushed on stack
637 * \luastack
638 * \lparam The command to launch.
639 * \lparam The optional screen number to spawn the command on.
641 static int
642 luaA_spawn(lua_State *L)
644 char *host, newdisplay[128];
645 const char *cmd;
646 int screen = 0, screenp, displayp;
648 if(lua_gettop(L) == 2)
650 screen = luaL_checknumber(L, 2) - 1;
651 luaA_checkscreen(screen);
654 cmd = luaL_checkstring(L, 1);
656 if(!globalconf.xinerama_is_active)
658 xcb_parse_display(NULL, &host, &displayp, &screenp);
659 snprintf(newdisplay, sizeof(newdisplay), "%s:%d.%d", host, displayp, screen);
660 setenv("DISPLAY", newdisplay, 1);
661 p_delete(&host);
664 /* The double-fork construct avoids zombie processes and keeps the code
665 * clean from stupid signal handlers. */
666 if(fork() == 0)
668 if(fork() == 0)
670 if(globalconf.connection)
671 xcb_disconnect(globalconf.connection);
672 setsid();
673 a_exec(cmd);
674 warn("execl '%s' failed: %s\n", cmd, strerror(errno));
676 exit(EXIT_SUCCESS);
678 wait(0);
680 return 0;
683 /** awesome global table.
684 * \param L The Lua VM state.
685 * \return The number of elements pushed on stack.
686 * \luastack
687 * \lfield font The default font.
688 * \lfield conffile The configuration file which has been loaded.
690 static int
691 luaA_awesome_index(lua_State *L)
693 if(luaA_usemetatable(L, 1, 2))
694 return 1;
696 size_t len;
697 const char *buf = luaL_checklstring(L, 2, &len);
699 switch(a_tokenize(buf, len))
701 case A_TK_FONT:
703 char *font = pango_font_description_to_string(globalconf.font->desc);
704 lua_pushstring(L, font);
705 g_free(font);
707 break;
708 case A_TK_CONFFILE:
709 lua_pushstring(L, globalconf.conffile);
710 break;
711 case A_TK_FG:
712 luaA_pushcolor(L, &globalconf.colors.fg);
713 break;
714 case A_TK_BG:
715 luaA_pushcolor(L, &globalconf.colors.bg);
716 break;
717 default:
718 return 0;
721 return 1;
724 /** Newindex function for the awesome global table.
725 * \param L The Lua VM state.
726 * \return The number of elements pushed on stack.
728 static int
729 luaA_awesome_newindex(lua_State *L)
731 if(luaA_usemetatable(L, 1, 2))
732 return 1;
734 size_t len;
735 const char *buf = luaL_checklstring(L, 2, &len);
737 switch(a_tokenize(buf, len))
739 case A_TK_FONT:
741 const char *newfont = luaL_checkstring(L, 3);
742 draw_font_delete(&globalconf.font);
743 globalconf.font = draw_font_new(newfont);
745 break;
746 case A_TK_FG:
747 if((buf = luaL_checklstring(L, 3, &len)))
748 xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.fg, buf, len));
749 break;
750 case A_TK_BG:
751 if((buf = luaL_checklstring(L, 3, &len)))
752 xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.bg, buf, len));
753 break;
754 default:
755 return 0;
758 return 0;
761 /** Initialize the Lua VM
763 void
764 luaA_init(void)
766 lua_State *L;
768 static const struct luaL_reg otable_methods[] =
770 { "__call", luaA_otable_new },
771 { NULL, NULL }
773 static const struct luaL_reg otable_meta[] =
775 { "__index", luaA_otable_index },
776 { "__newindex", luaA_otable_newindex },
777 { NULL, NULL }
779 static const struct luaL_reg awesome_lib[] =
781 { "quit", luaA_quit },
782 { "exec", luaA_exec },
783 { "spawn", luaA_spawn },
784 { "restart", luaA_restart },
785 { "buttons", luaA_buttons },
786 { "__index", luaA_awesome_index },
787 { "__newindex", luaA_awesome_newindex },
788 { NULL, NULL }
791 L = globalconf.L = luaL_newstate();
793 luaL_openlibs(L);
795 luaA_fixups(L);
797 /* Export awesome lib */
798 luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
800 /* Export hooks lib */
801 luaL_register(L, "hooks", awesome_hooks_lib);
803 /* Export D-Bus lib */
804 luaL_register(L, "dbus", awesome_dbus_lib);
806 /* Export keygrabber lib */
807 luaL_register(L, "keygrabber", awesome_keygrabber_lib);
809 /* Export mousegrabber lib */
810 luaL_register(L, "mousegrabber", awesome_mousegrabber_lib);
812 /* Export otable lib */
813 luaA_openlib(L, "otable", otable_methods, otable_meta);
815 /* Export screen */
816 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
818 /* Export mouse */
819 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
821 /* Export button */
822 luaA_openlib(L, "button", awesome_button_methods, awesome_button_meta);
824 /* Export image */
825 luaA_openlib(L, "image", awesome_image_methods, awesome_image_meta);
827 /* Export tag */
828 luaA_openlib(L, "tag", awesome_tag_methods, awesome_tag_meta);
830 /* Export wibox */
831 luaA_openlib(L, "wibox", awesome_wibox_methods, awesome_wibox_meta);
833 /* Export widget */
834 luaA_openlib(L, "widget", awesome_widget_methods, awesome_widget_meta);
836 /* Export client */
837 luaA_openlib(L, "client", awesome_client_methods, awesome_client_meta);
839 /* Export keys */
840 luaA_openlib(L, "key", awesome_key_methods, awesome_key_meta);
841 luaA_openlib(L, "keybinding", awesome_keybinding_methods, awesome_key_meta);
843 lua_pushliteral(L, "AWESOME_VERSION");
844 lua_pushstring(L, AWESOME_VERSION);
845 lua_settable(L, LUA_GLOBALSINDEX);
847 lua_pushliteral(L, "AWESOME_RELEASE");
848 lua_pushstring(L, AWESOME_RELEASE);
849 lua_settable(L, LUA_GLOBALSINDEX);
851 /* init hooks */
852 globalconf.hooks.manage = LUA_REFNIL;
853 globalconf.hooks.unmanage = LUA_REFNIL;
854 globalconf.hooks.focus = LUA_REFNIL;
855 globalconf.hooks.unfocus = LUA_REFNIL;
856 globalconf.hooks.mouse_enter = LUA_REFNIL;
857 globalconf.hooks.mouse_leave = LUA_REFNIL;
858 globalconf.hooks.arrange = LUA_REFNIL;
859 globalconf.hooks.clients = LUA_REFNIL;
860 globalconf.hooks.tags = LUA_REFNIL;
861 globalconf.hooks.tagged = LUA_REFNIL;
862 globalconf.hooks.property = LUA_REFNIL;
863 globalconf.hooks.timer = LUA_REFNIL;
864 #ifdef WITH_DBUS
865 globalconf.hooks.dbus = LUA_REFNIL;
866 #endif
868 /* add Lua lib path (/usr/share/awesome/lib by default) */
869 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?.lua\"");
870 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?/init.lua\"");
872 /* add XDG_CONFIG_DIR (/etc/xdg/awesome by default) as include path */
873 luaA_dostring(L, "package.path = package.path .. \";" XDG_CONFIG_DIR "/awesome/?.lua\"");
874 luaA_dostring(L, "package.path = package.path .. \";" XDG_CONFIG_DIR "/awesome/?/init.lua\"");
876 /* add either XDG_CONFIG_HOME/awesome or HOME/.config/awesome to path */
877 char *dir;
878 if((dir = getenv("XDG_CONFIG_HOME")))
880 char *path;
881 a_asprintf(&path, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"", dir, dir);
882 luaA_dostring(globalconf.L, path);
883 p_delete(&path);
885 else
887 char *path, *homedir = getenv("HOME");
888 a_asprintf(&path,
889 "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?.lua;%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?/init.lua\"",
890 homedir, homedir);
891 luaA_dostring(globalconf.L, path);
892 p_delete(&path);
895 /* add XDG_CONFIG_DIRS to include paths */
896 char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
897 ssize_t len;
899 if((len = a_strlen(xdg_config_dirs)))
901 char **buf, **xdg_files = a_strsplit(xdg_config_dirs, len, ':');
903 for(buf = xdg_files; *buf; buf++)
905 char *confpath;
906 a_asprintf(&confpath, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"",
907 *buf, *buf);
908 luaA_dostring(globalconf.L, confpath);
909 p_delete(&confpath);
912 for(buf = xdg_files; *buf; buf++)
913 p_delete(buf);
914 p_delete(&xdg_files);
918 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
920 static bool
921 luaA_loadrc(const char *confpath, bool run)
923 if(confpath)
925 if(!luaL_loadfile(globalconf.L, confpath))
927 if(run)
929 if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
930 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
931 else
933 globalconf.conffile = a_strdup(confpath);
934 return true;
937 else
938 lua_pop(globalconf.L, 1);
939 return true;
941 else
942 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
944 return false;
947 /** Load a configuration file.
948 * \param confpatharg The configuration file to load.
949 * \param run Run the configuration file.
951 bool
952 luaA_parserc(const char *confpatharg, bool run)
954 int screen;
955 const char *confdir, *xdg_config_dirs;
956 char *confpath = NULL, **xdg_files = NULL, **buf;
957 ssize_t len;
958 bool ret = false;
960 /* try to load, return if it's ok */
961 if(luaA_loadrc(confpatharg, run))
963 ret = true;
964 goto bailout;
967 if((confdir = getenv("XDG_CONFIG_HOME")))
968 a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, confdir);
969 else
970 a_asprintf(&confpath, "%s" XDG_CONFIG_HOME_DEFAULT AWESOME_CONFIG_FILE, getenv("HOME"));
972 /* try to run XDG_CONFIG_HOME/awesome/rc.lua */
973 if(luaA_loadrc(confpath, run))
975 ret = true;
976 goto bailout;
979 p_delete(&confpath);
981 xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
983 if(!(len = a_strlen(xdg_config_dirs)))
985 xdg_config_dirs = XDG_CONFIG_DIR;
986 len = sizeof(XDG_CONFIG_DIR) - 1;
989 xdg_files = a_strsplit(xdg_config_dirs, len, ':');
991 for(buf = xdg_files; *buf && !ret; buf++)
993 a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, *buf);
994 if(luaA_loadrc(confpath, run))
996 ret = true;
997 goto bailout;
999 p_delete(&confpath);
1002 bailout:
1004 p_delete(&confpath);
1006 if(xdg_files)
1008 for(buf = xdg_files; *buf; buf++)
1009 p_delete(buf);
1010 p_delete(&xdg_files);
1013 /* Assure there's at least one tag */
1014 for(screen = 0; screen < globalconf.nscreen; screen++)
1015 if(!globalconf.screens[screen].tags.len)
1016 tag_append_to_screen(tag_new("default", sizeof("default") - 1),
1017 &globalconf.screens[screen]);
1018 return ret;
1021 /** Parse a command.
1022 * \param cmd The buffer to parse.
1023 * \return the number of elements pushed on the stack by the last statement in cmd.
1024 * If there's an error, the message is pushed onto the stack and this returns 1.
1026 static int
1027 luaA_docmd(const char *cmd)
1029 char *p;
1030 int newtop, oldtop = lua_gettop(globalconf.L);
1032 while((p = strchr(cmd, '\n')))
1034 newtop = lua_gettop(globalconf.L);
1035 lua_pop(globalconf.L, newtop - oldtop);
1036 oldtop = newtop;
1038 *p = '\0';
1039 if (luaL_dostring(globalconf.L, cmd))
1041 warn("error executing Lua code: %s", lua_tostring(globalconf.L, -1));
1042 return 1;
1044 cmd = p + 1;
1046 return lua_gettop(globalconf.L) - oldtop;
1049 /** Pushes a Lua array containing the top n elements of the stack.
1050 * \param n The number of elements to put in the array.
1052 static void
1053 luaA_array(int n)
1055 int i;
1056 lua_createtable(globalconf.L, n, 0);
1057 lua_insert(globalconf.L, -n - 1);
1059 for (i = n; i > 0; i--)
1060 lua_rawseti(globalconf.L, -i - 1, i);
1063 /** Maps the top n elements of the stack to the result of
1064 * applying a function to that element.
1065 * \param n The number of elements to map.
1066 * \luastack
1067 * \lparam The function to map the elements by. This should be
1068 * at position -(n + 1).
1070 static void
1071 luaA_map(int n)
1073 int i;
1074 for (i = 0; i < n; i++)
1076 lua_pushvalue(globalconf.L, -n - 1); /* copy of the function */
1077 lua_pushvalue(globalconf.L, -n - 1); /* value to map */
1078 lua_pcall(globalconf.L, 1, 1, 0); /* call function */
1079 lua_remove(globalconf.L, -n - 1); /* remove old value */
1081 lua_remove(globalconf.L, -n - 1); /* remove function */
1084 static void luaA_conn_cleanup(EV_P_ ev_io *w)
1086 ev_ref(EV_DEFAULT_UC);
1087 ev_io_stop(EV_DEFAULT_UC_ w);
1088 if (close(w->fd))
1089 warn("error closing UNIX domain socket: %s", strerror(errno));
1090 p_delete(&w);
1093 static void
1094 luaA_cb(EV_P_ ev_io *w, int revents)
1096 char buf[1024];
1097 int r, els;
1098 const char *s;
1099 size_t len;
1101 switch(r = recv(w->fd, buf, sizeof(buf)-1, MSG_TRUNC))
1103 case -1:
1104 warn("error reading UNIX domain socket: %s", strerror(errno));
1105 case 0: /* 0 bytes are only transferred when the connection is closed */
1106 luaA_conn_cleanup(EV_DEFAULT_UC_ w);
1107 break;
1108 default:
1109 if(r >= ssizeof(buf))
1110 break;
1111 buf[r] = '\0';
1112 lua_getglobal(globalconf.L, "table");
1113 lua_getfield(globalconf.L, -1, "concat");
1114 lua_remove(globalconf.L, -2); /* remove table */
1116 lua_getglobal(globalconf.L, "tostring");
1117 els = luaA_docmd(buf);
1118 luaA_map(els); /* map results to strings */
1119 luaA_array(els); /* put strings in an array */
1121 lua_pushstring(globalconf.L, "\t");
1122 lua_pcall(globalconf.L, 2, 1, 0); /* concatenate results with tabs */
1124 s = lua_tolstring(globalconf.L, -1, &len);
1126 /* ignore ENOENT because the client may not read */
1127 if (send(w->fd, s, len, MSG_DONTWAIT) == -1)
1128 switch(errno)
1130 case ENOENT:
1131 case EAGAIN:
1132 break;
1133 default:
1134 warn("can't send back to client via domain socket: %s", strerror(errno));
1135 break;
1138 lua_pop(globalconf.L, 1); /* pop the string */
1140 awesome_refresh(globalconf.connection);
1144 static void
1145 luaA_conn_cb(EV_P_ ev_io *w, int revents)
1147 ev_io *csio_conn = p_new(ev_io, 1);
1148 int csfd = accept(w->fd, NULL, NULL);
1150 ev_io_init(csio_conn, &luaA_cb, csfd, EV_READ);
1151 ev_io_start(EV_DEFAULT_UC_ csio_conn);
1152 ev_unref(EV_DEFAULT_UC);
1155 void
1156 luaA_cs_init(void)
1158 int csfd = socket_getclient();
1160 if (csfd < 0 || fcntl(csfd, F_SETFD, FD_CLOEXEC) == -1)
1161 return;
1163 addr = socket_getaddr(getenv("DISPLAY"));
1165 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1167 if(errno == EADDRINUSE)
1169 if(unlink(addr->sun_path))
1170 warn("error unlinking existing file: %s", strerror(errno));
1171 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1172 return warn("error binding UNIX domain socket: %s", strerror(errno));
1174 else
1175 return warn("error binding UNIX domain socket: %s", strerror(errno));
1177 listen(csfd, 10);
1179 ev_io_init(&csio, &luaA_conn_cb, csfd, EV_READ);
1180 ev_io_start(EV_DEFAULT_UC_ &csio);
1181 ev_unref(EV_DEFAULT_UC);
1184 void
1185 luaA_cs_cleanup(void)
1187 if(csio.fd < 0)
1188 return;
1189 ev_ref(EV_DEFAULT_UC);
1190 ev_io_stop(EV_DEFAULT_UC_ &csio);
1191 if (close(csio.fd))
1192 warn("error closing UNIX domain socket: %s", strerror(errno));
1193 if(unlink(addr->sun_path))
1194 warn("error unlinking UNIX domain socket: %s", strerror(errno));
1195 p_delete(&addr);
1196 csio.fd = -1;
1199 void
1200 luaA_on_timer(EV_P_ ev_timer *w, int revents)
1202 if(globalconf.hooks.timer != LUA_REFNIL)
1203 luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0, 0);
1204 awesome_refresh(globalconf.connection);
1207 /** Push a color as a string onto the stack
1208 * \param L The Lua VM state.
1209 * \param c The color to push.
1210 * \return The number of elements pushed on stack.
1213 luaA_pushcolor(lua_State *L, const xcolor_t *c)
1215 uint8_t r = (unsigned)c->red * 0xff / 0xffff;
1216 uint8_t g = (unsigned)c->green * 0xff / 0xffff;
1217 uint8_t b = (unsigned)c->blue * 0xff / 0xffff;
1218 uint8_t a = (unsigned)c->alpha * 0xff / 0xffff;
1219 char s[10];
1220 int len;
1221 /* do not print alpha if it's full */
1222 if(a == 0xff)
1223 len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
1224 else
1225 len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
1226 lua_pushlstring(L, s, len);
1227 return 1;