awesomerc: remove useless wibox name
[awesome.git] / luaa.c
blob354ed3f28f4462dd98c8ed2eb1e0dfa3c09cbee6
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 <errno.h>
23 #include <stdio.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #include <fcntl.h>
32 #include <ev.h>
34 #include <lua.h>
35 #include <lauxlib.h>
36 #include <lualib.h>
38 #include <xcb/xcb.h>
40 #include "awesome.h"
41 #include "awesome-version-internal.h"
42 #include "ewmh.h"
43 #include "config.h"
44 #include "luaa.h"
45 #include "tag.h"
46 #include "client.h"
47 #include "screen.h"
48 #include "event.h"
49 #include "titlebar.h"
50 #include "mouse.h"
51 #include "layouts/tile.h"
52 #include "common/socket.h"
53 #include "common/buffer.h"
55 extern awesome_t globalconf;
57 extern const struct luaL_reg awesome_keygrabber_lib[];
58 extern const struct luaL_reg awesome_button_methods[];
59 extern const struct luaL_reg awesome_button_meta[];
60 extern const struct luaL_reg awesome_image_methods[];
61 extern const struct luaL_reg awesome_image_meta[];
62 extern const struct luaL_reg awesome_mouse_methods[];
63 extern const struct luaL_reg awesome_mouse_meta[];
64 extern const struct luaL_reg awesome_screen_methods[];
65 extern const struct luaL_reg awesome_screen_meta[];
66 extern const struct luaL_reg awesome_client_methods[];
67 extern const struct luaL_reg awesome_client_meta[];
68 extern const struct luaL_reg awesome_titlebar_methods[];
69 extern const struct luaL_reg awesome_titlebar_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_statusbar_methods[];
75 extern const struct luaL_reg awesome_statusbar_meta[];
76 extern const struct luaL_reg awesome_wibox_methods[];
77 extern const struct luaL_reg awesome_wibox_meta[];
78 extern const struct luaL_reg awesome_keybinding_methods[];
79 extern const struct luaL_reg awesome_keybinding_meta[];
81 static struct sockaddr_un *addr;
82 static ev_io csio = { .fd = -1 };
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 /** Set the function called each time a client gets focus. This function is
143 * called with the client object as argument.
144 * \param L The Lua VM state.
145 * \return The number of elements pushed on stack.
146 * \luastack
147 * \lparam A function to call each time a client gets focus.
149 static int
150 luaA_hooks_focus(lua_State *L)
152 return luaA_registerfct(L, 1, &globalconf.hooks.focus);
155 /** Set the function called each time a client loses focus. This function is
156 * called with the client object as argument.
157 * \param L The Lua VM state.
158 * \return The number of elements pushed on stack.
159 * \luastack
160 * \lparam A function to call each time a client loses focus.
162 static int
163 luaA_hooks_unfocus(lua_State *L)
165 return luaA_registerfct(L, 1, &globalconf.hooks.unfocus);
168 /** Set the function called each time a new client appears. This function is
169 * called with the client object as argument.
170 * \param L The Lua VM state.
171 * \return The number of elements pushed on stack.
172 * \luastack
173 * \lparam A function to call on each new client.
175 static int
176 luaA_hooks_manage(lua_State *L)
178 return luaA_registerfct(L, 1, &globalconf.hooks.manage);
181 /** Set the function called each time a client goes away. This function is
182 * called with the client object as argument.
183 * \param L The Lua VM state.
184 * \return The number of elements pushed on stack.
185 * \luastack
186 * \lparam A function to call when a client goes away.
188 static int
189 luaA_hooks_unmanage(lua_State *L)
191 return luaA_registerfct(L, 1, &globalconf.hooks.unmanage);
194 /** Set the function called each time the mouse enter a new window. This
195 * function is called with the client object as argument.
196 * \param L The Lua VM state.
197 * \return The number of elements pushed on stack.
198 * \luastack
199 * \lparam A function to call each time a client gets mouse over it.
201 static int
202 luaA_hooks_mouse_enter(lua_State *L)
204 return luaA_registerfct(L, 1, &globalconf.hooks.mouse_enter);
207 /** Set the function called each time the mouse enter a new window. This
208 * function is called with the client object as argument. (DEPRECATED)
209 * \param L The Lua VM state.
210 * \return The number of elements pushed on stack.
211 * \luastack
212 * \lparam A function to call each time a client gets mouse over it.
214 static int
215 luaA_hooks_mouse_over(lua_State *L)
217 deprecate();
218 return luaA_hooks_mouse_enter(L);
221 /** Set the function called on each client list change.
222 * This function is called without any argument.
223 * \param L The Lua VM state.
224 * \return The number of elements pushed on stack.
225 * \luastack
226 * \lparam A function to call on each client list change.
228 static int
229 luaA_hooks_clients(lua_State *L)
231 return luaA_registerfct(L, 1, &globalconf.hooks.clients);
234 /** Set the function called on each screen tag list change.
235 * This function is called with a screen number as argument.
236 * \param L The Lua VM state.
237 * \return The number of elements pushed on stack.
238 * \luastack
239 * \lparam A function to call on each tag list change.
241 static int
242 luaA_hooks_tags(lua_State *L)
244 return luaA_registerfct(L, 1, &globalconf.hooks.tags);
247 /** Set the function called on each client's tags change.
248 * This function is called with the client and the tag as argument.
249 * \param L The Lua VM state.
250 * \return The number of elements pushed on stack.
251 * \luastack
252 * \lparam A function to call on each client's tags change.
254 static int
255 luaA_hooks_tagged(lua_State *L)
257 return luaA_registerfct(L, 1, &globalconf.hooks.tagged);
260 /** Set the function called on each screen arrange. This function is called
261 * with the screen number as argument.
262 * \param L The Lua VM state.
263 * \return The number of elements pushed on stack.
264 * \luastack
265 * \lparam A function to call on each screen arrange.
267 static int
268 luaA_hooks_arrange(lua_State *L)
270 return luaA_registerfct(L, 1, &globalconf.hooks.arrange);
273 /** Set the function called on each client's property change.
274 * This function is called with the client object as argument and the
275 * property name.
276 * \param L The Lua VM state.
277 * \return The number of elements pushed on stack.
278 * \luastack
279 * \lparam A function to call on each client property update.
281 static int
282 luaA_hooks_property(lua_State *L)
284 return luaA_registerfct(L, 1, &globalconf.hooks.property);
287 /** Set the function to be called every N seconds.
288 * \param L The Lua VM state.
289 * \return The number of elements pushed on stack.
290 * \luastack
291 * \lparam The number of seconds to run function every. Set 0 to disable.
292 * \lparam A function to call every N seconds (optional).
294 static int
295 luaA_hooks_timer(lua_State *L)
297 globalconf.timer.repeat = luaL_checknumber(L, 1);
299 if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
300 luaA_registerfct(L, 2, &globalconf.hooks.timer);
302 ev_timer_again(globalconf.loop, &globalconf.timer);
303 return 0;
306 /** Set or get default font. (DEPRECATED)
307 * \param L The Lua VM state.
308 * \return The number of elements pushed on stack.
309 * \luastack
310 * \lparam An optional string with a font name in Pango format.
311 * \lreturn The font used, in Pango format.
313 static int
314 luaA_font(lua_State *L)
316 char *font;
318 if(lua_gettop(L) == 1)
320 const char *newfont = luaL_checkstring(L, 1);
321 draw_font_delete(&globalconf.font);
322 globalconf.font = draw_font_new(globalconf.default_screen, newfont);
325 font = pango_font_description_to_string(globalconf.font->desc);
326 lua_pushstring(L, font);
327 g_free(font);
329 return 1;
332 /** Set default font. (DEPRECATED)
333 * \param L The Lua VM state.
334 * \return The number of elements pushed on stack.
335 * \luastack
336 * \lparam A string with a font name in Pango format.
338 static int
339 luaA_font_set(lua_State *L)
341 deprecate();
342 return luaA_font(L);
345 /** Get configuration file path used by awesome.
346 * \param L The Lua VM state.
347 * \return The number of elements pushed on stack.
348 * \luastack
349 * \lreturn The awesome configuration file path.
351 static int
352 luaA_conffile(lua_State *L)
354 lua_pushstring(L, globalconf.conffile);
355 return 1;
358 /** Set default colors.
359 * \param L The Lua VM state.
360 * \return The number of elements pushed on stack.
361 * \luastack
362 * \lparam A table with `fg' and `bg' elements, containing colors.
363 * \lreturn A table with `fg' and `bg' elements, containing colors.
365 static int
366 luaA_colors(lua_State *L)
368 if(lua_gettop(L) == 1)
370 const char *buf;
371 size_t len;
372 int8_t colors_nbr = -1, i;
373 xcolor_init_request_t reqs[2];
375 luaA_checktable(L, 1);
377 if((buf = luaA_getopt_lstring(L, 1, "fg", NULL, &len)))
378 reqs[++colors_nbr] = xcolor_init_unchecked(&globalconf.colors.fg, buf, len);
380 if((buf = luaA_getopt_lstring(L, 1, "bg", NULL, &len)))
381 reqs[++colors_nbr] = xcolor_init_unchecked(&globalconf.colors.bg, buf, len);
383 for(i = 0; i <= colors_nbr; i++)
384 xcolor_init_reply(reqs[i]);
387 lua_newtable(L);
388 luaA_pushcolor(L, &globalconf.colors.fg);
389 lua_setfield(L, -2, "fg");
390 luaA_pushcolor(L, &globalconf.colors.bg);
391 lua_setfield(L, -2, "bg");
393 return 1;
396 /** Set default colors. (DEPRECATED)
397 * \param L The Lua VM state.
398 * \return The number of elements pushed on stack.
400 * \luastack
401 * \lparam A table with `fg' and `bg' elements, containing colors.
403 static int
404 luaA_colors_set(lua_State *L)
406 deprecate();
407 return luaA_colors(L);
410 static void
411 luaA_openlib(lua_State *L, const char *name,
412 const struct luaL_reg methods[],
413 const struct luaL_reg meta[])
415 luaL_newmetatable(L, name); /* 1 */
416 lua_pushvalue(L, -1); /* dup metatable 2 */
417 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
419 luaL_register(L, NULL, meta); /* 1 */
420 luaL_register(L, name, methods); /* 2 */
421 lua_pushvalue(L, -1); /* dup self as metatable 3 */
422 lua_setmetatable(L, -2); /* set self as metatable 2 */
423 lua_pop(L, 2);
426 /** UTF-8 aware string length computing.
427 * \param L The Lua VM state.
428 * \return The number of elements pushed on stack.
430 static int
431 luaA_mbstrlen(lua_State *L)
433 const char *cmd = luaL_checkstring(L, 1);
434 lua_pushnumber(L, mbstowcs(NULL, NONULL(cmd), 0));
435 return 1;
438 /** Overload standard Lua next function to use __next key on metatable.
439 * \param L The Lua VM state.
440 * \param The number of elements pushed on stack.
442 static int
443 luaAe_next(lua_State *L)
445 if(luaL_getmetafield(L, 1, "__next"))
447 lua_insert(L, 1);
448 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
449 return lua_gettop(L);
452 luaL_checktype(L, 1, LUA_TTABLE);
453 lua_settop(L, 2);
454 if(lua_next(L, 1))
455 return 2;
456 lua_pushnil(L);
457 return 1;
460 /** Overload lua_next() function by using __next metatable field
461 * to get next elements.
462 * \param L The Lua VM stack.
463 * \param idx The index number of elements in stack.
464 * \return 1 if more elements to come, 0 otherwise.
467 luaA_next(lua_State *L, int idx)
469 if(luaL_getmetafield(L, idx, "__next"))
471 /* if idx is relative, reduce it since we got __next */
472 if(idx < 0) idx--;
473 /* copy table and then move key */
474 lua_pushvalue(L, idx);
475 lua_pushvalue(L, -3);
476 lua_remove(L, -4);
477 lua_pcall(L, 2, 2, 0);
478 /* next returned nil, it's the end */
479 if(lua_isnil(L, -1))
481 /* remove nil */
482 lua_pop(L, 2);
483 return 0;
485 return 1;
488 return lua_next(L, idx);
491 /** Generic pairs function.
492 * \param L The Lua VM state.
493 * \return The number of elements pushed on stack.
495 static int
496 luaA_generic_pairs(lua_State *L)
498 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
499 lua_pushvalue(L, 1); /* state, */
500 lua_pushnil(L); /* and initial value */
501 return 3;
504 /** Overload standard pairs function to use __pairs field of metatables.
505 * \param L The Lua VM state.
506 * \return The number of elements pushed on stack.
508 static int
509 luaAe_pairs(lua_State *L)
511 if(luaL_getmetafield(L, 1, "__pairs"))
513 lua_insert(L, 1);
514 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
515 return lua_gettop(L);
518 luaL_checktype(L, 1, LUA_TTABLE);
519 return luaA_generic_pairs(L);
522 /** Replace various standards Lua functions with our own.
523 * \param L The Lua VM state.
525 static void
526 luaA_fixups(lua_State *L)
528 lua_getglobal(L, "string");
529 lua_pushcfunction(L, luaA_mbstrlen);
530 lua_setfield(L, -2, "len");
531 lua_pop(L, 1);
532 lua_pushliteral(L, "next");
533 lua_pushcfunction(L, luaAe_next);
534 lua_settable(L, LUA_GLOBALSINDEX);
535 lua_pushliteral(L, "pairs");
536 lua_pushcfunction(L, luaAe_next);
537 lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
538 lua_settable(L, LUA_GLOBALSINDEX);
541 /** __next function for wtable objects.
542 * \param L The Lua VM state.
543 * \return The number of elements pushed on stack.
545 static int
546 luaA_wtable_next(lua_State *L)
548 /* upvalue 1 is content table */
549 if(lua_next(L, lua_upvalueindex(1)))
550 return 2;
551 lua_pushnil(L);
552 return 1;
555 /** Index function of wtable objects.
556 * \param L The Lua VM state.
557 * \return The number of elements pushed on stack.
559 static int
560 luaA_wtable_index(lua_State *L)
562 size_t len;
563 const char *buf;
565 lua_pushvalue(L, 2);
566 /* check for size, waiting lua 5.2 and __len on tables */
567 if((buf = lua_tolstring(L, -1, &len)))
568 if(a_tokenize(buf, len) == A_TK_LEN)
570 lua_pushnumber(L, lua_objlen(L, lua_upvalueindex(1)));
571 return 1;
573 lua_pop(L, 1);
575 /* upvalue 1 is content table */
576 lua_rawget(L, lua_upvalueindex(1));
577 return 1;
580 /** Newndex function of wtable objects.
581 * \param L The Lua VM state.
582 * \return The number of elements pushed on stack.
584 static int
585 luaA_wtable_newindex(lua_State *L)
587 bool invalid = false;
589 /* push key on top */
590 lua_pushvalue(L, 2);
591 /* get current key value in content table */
592 lua_rawget(L, lua_upvalueindex(1));
593 /* if value is a widget, notify change */
594 if(lua_istable(L, -1) || luaA_toudata(L, -1, "widget"))
595 invalid = true;
597 lua_pop(L, 1); /* remove value */
599 /* if new value is a widget or a table */
600 if(lua_istable(L, 3))
602 luaA_table2wtable(L);
603 invalid = true;
605 else if(!invalid && luaA_toudata(L, 3, "widget"))
606 invalid = true;
608 /* upvalue 1 is content table */
609 lua_rawset(L, lua_upvalueindex(1));
611 if(invalid)
612 luaA_wibox_invalidate_byitem(L, lua_topointer(L, 1));
614 return 0;
617 /** Convert the top element of the stack to a proxied wtable.
618 * \param L The Lua VM state.
620 void
621 luaA_table2wtable(lua_State *L)
623 if(!lua_istable(L, -1))
624 return;
626 lua_newtable(L); /* create *real* content table */
627 lua_newtable(L); /* metatable */
628 lua_pushvalue(L, -2); /* copy content table */
629 lua_pushcclosure(L, luaA_wtable_next, 1); /* __next has the content table as upvalue */
630 lua_pushvalue(L, -3); /* copy content table */
631 lua_pushcclosure(L, luaA_wtable_index, 1); /* __index has the content table as upvalue */
632 lua_pushvalue(L, -4); /* copy content table */
633 lua_pushcclosure(L, luaA_wtable_newindex, 1); /* __newindex has the content table as upvalue */
634 /* set metatable field with just pushed closure */
635 lua_setfield(L, -4, "__newindex");
636 lua_setfield(L, -3, "__index");
637 lua_setfield(L, -2, "__next");
638 /* set metatable impossible to touch */
639 lua_pushliteral(L, "wtable");
640 lua_setfield(L, -2, "__metatable");
641 /* set new metatable on original table */
642 lua_setmetatable(L, -3);
644 /* initial key */
645 lua_pushnil(L);
646 /* go through original table */
647 while(lua_next(L, -3))
649 /* if convert value to wtable */
650 luaA_table2wtable(L);
651 /* copy key */
652 lua_pushvalue(L, -2);
653 /* move key before value */
654 lua_insert(L, -2);
655 /* set same value in content table */
656 lua_rawset(L, -4);
657 /* copy key */
658 lua_pushvalue(L, -1);
659 /* push the new value :-> */
660 lua_pushnil(L);
661 /* set orig[k] = nil */
662 lua_rawset(L, -5);
664 /* remove content table */
665 lua_pop(L, 1);
668 /** Look for an item: table, function, etc.
669 * \param L The Lua VM state.
670 * \param item The pointer item.
672 bool
673 luaA_hasitem(lua_State *L, const void *item)
675 lua_pushnil(L);
676 while(luaA_next(L, -2))
678 if(lua_topointer(L, -1) == item)
680 /* remove value and key */
681 lua_pop(L, 2);
682 return true;
684 if(lua_istable(L, -1))
685 if(luaA_hasitem(L, item))
687 /* remove key and value */
688 lua_pop(L, 2);
689 return true;
691 /* remove value */
692 lua_pop(L, 1);
694 return false;
697 /** Object table.
698 * This table can use safely object as key.
699 * \param L The Lua VM state.
700 * \return The number of elements pushed on stack.
703 luaA_otable_index(lua_State *L)
705 void **obj, **v;
707 if((obj = lua_touserdata(L, 2)))
709 /* begins at nil */
710 lua_pushnil(L);
711 while(lua_next(L, 1))
713 /* check the key against the key if the key is a userdata,
714 * otherwise check it again the value. */
715 if((v = lua_touserdata(L, -2))
716 && *v == *obj)
717 /* return value */
718 return 1;
719 else if((v = lua_touserdata(L, -1))
720 && *v == *obj)
722 /* return key */
723 lua_pop(L, 1);
724 return 1;
726 /* removes 'value'; keeps 'key' for next iteration */
727 lua_pop(L, 1);
729 return 0;
732 lua_rawget(L, 1);
733 return 1;
736 /** Object table.
737 * This table can use safely object as key.
738 * \param L The Lua VM state.
739 * \return The number of elements pushed on stack.
741 static int
742 luaA_otable_newindex(lua_State *L)
744 void **obj, **v;
746 if((obj = lua_touserdata(L, 2)))
748 /* begins at nil */
749 lua_pushnil(L);
750 while(lua_next(L, 1))
752 if((v = lua_touserdata(L, -2))
753 && *v == *obj)
755 /* remove value */
756 lua_pop(L, 1);
757 /* push new value on top */
758 lua_pushvalue(L, 3);
759 /* set in table key = value */
760 lua_rawset(L, 1);
761 return 0;
763 /* removes 'value'; keeps 'key' for next iteration */
764 lua_pop(L, 1);
768 lua_rawset(L, 1);
770 return 0;
773 /** Spawn a program.
774 * This function is multi-head (Zaphod) aware and will set display to
775 * the right screen according to mouse position.
776 * \param L The Lua VM state.
777 * \return The number of elements pushed on stack
778 * \luastack
779 * \lparam The command to launch.
780 * \lparam The optional screen number to spawn the command on.
782 static int
783 luaA_spawn(lua_State *L)
785 char *host, newdisplay[128];
786 const char *cmd;
787 int screen = 0, screenp, displayp;
789 if(lua_gettop(L) == 2)
791 screen = luaL_checknumber(L, 2) - 1;
792 luaA_checkscreen(screen);
795 cmd = luaL_checkstring(L, 1);
797 if(!globalconf.xinerama_is_active)
799 xcb_parse_display(NULL, &host, &displayp, &screenp);
800 snprintf(newdisplay, sizeof(newdisplay), "%s:%d.%d", host, displayp, screen);
801 setenv("DISPLAY", newdisplay, 1);
802 p_delete(&host);
805 /* The double-fork construct avoids zombie processes and keeps the code
806 * clean from stupid signal handlers. */
807 if(fork() == 0)
809 if(fork() == 0)
811 if(globalconf.connection)
812 xcb_disconnect(globalconf.connection);
813 setsid();
814 a_exec(cmd);
815 warn("execl '%s' failed: %s\n", cmd, strerror(errno));
817 exit(EXIT_SUCCESS);
819 wait(0);
821 return 0;
824 /** Initialize the Lua VM
826 void
827 luaA_init(void)
829 lua_State *L;
831 static const struct luaL_reg otable_methods[] =
833 { "__call", luaA_otable_new },
834 { NULL, NULL }
836 static const struct luaL_reg otable_meta[] =
838 { "__index", luaA_otable_index },
839 { "__newindex", luaA_otable_newindex },
840 { NULL, NULL }
842 static const struct luaL_reg awesome_lib[] =
844 { "quit", luaA_quit },
845 { "exec", luaA_exec },
846 { "spawn", luaA_spawn },
847 { "restart", luaA_restart },
848 { "buttons", luaA_buttons },
849 { "font_set", luaA_font_set },
850 { "colors_set", luaA_colors_set },
851 { "font", luaA_font },
852 { "colors", luaA_colors },
853 { "conffile", luaA_conffile },
854 { NULL, NULL }
856 static const struct luaL_reg awesome_hooks_lib[] =
858 { "focus", luaA_hooks_focus },
859 { "unfocus", luaA_hooks_unfocus },
860 { "manage", luaA_hooks_manage },
861 { "unmanage", luaA_hooks_unmanage },
862 { "mouse_enter", luaA_hooks_mouse_enter },
863 { "property", luaA_hooks_property },
864 { "arrange", luaA_hooks_arrange },
865 { "clients", luaA_hooks_clients },
866 { "tags", luaA_hooks_tags },
867 { "tagged", luaA_hooks_tagged },
868 { "timer", luaA_hooks_timer },
869 /* deprecated */
870 { "mouse_over", luaA_hooks_mouse_over },
871 { NULL, NULL }
874 L = globalconf.L = luaL_newstate();
876 luaL_openlibs(L);
878 luaA_fixups(L);
880 /* Export awesome lib */
881 luaL_register(L, "awesome", awesome_lib);
883 /* Export hooks lib */
884 luaL_register(L, "hooks", awesome_hooks_lib);
886 /* Export keygrabber lib */
887 luaL_register(L, "keygrabber", awesome_keygrabber_lib);
889 /* Export otable lib */
890 luaA_openlib(L, "otable", otable_methods, otable_meta);
892 /* Export screen */
893 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
895 /* Export mouse */
896 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
898 /* Export button */
899 luaA_openlib(L, "button", awesome_button_methods, awesome_button_meta);
901 /* Export image */
902 luaA_openlib(L, "image", awesome_image_methods, awesome_image_meta);
904 /* Export tag */
905 luaA_openlib(L, "tag", awesome_tag_methods, awesome_tag_meta);
907 /* Export statusbar */
908 luaA_openlib(L, "statusbar", awesome_statusbar_methods, awesome_statusbar_meta);
910 /* Export wibox */
911 luaA_openlib(L, "wibox", awesome_wibox_methods, awesome_wibox_meta);
913 /* Export widget */
914 luaA_openlib(L, "widget", awesome_widget_methods, awesome_widget_meta);
916 /* Export client */
917 luaA_openlib(L, "client", awesome_client_methods, awesome_client_meta);
919 /* Export titlebar */
920 luaA_openlib(L, "titlebar", awesome_titlebar_methods, awesome_titlebar_meta);
922 /* Export keys */
923 luaA_openlib(L, "keybinding", awesome_keybinding_methods, awesome_keybinding_meta);
925 lua_pushliteral(L, "AWESOME_VERSION");
926 lua_pushstring(L, AWESOME_VERSION);
927 lua_settable(L, LUA_GLOBALSINDEX);
929 lua_pushliteral(L, "AWESOME_RELEASE");
930 lua_pushstring(L, AWESOME_RELEASE);
931 lua_settable(L, LUA_GLOBALSINDEX);
933 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?.lua\"");
934 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?/init.lua\"");
936 /* init hooks */
937 globalconf.hooks.manage = LUA_REFNIL;
938 globalconf.hooks.unmanage = LUA_REFNIL;
939 globalconf.hooks.focus = LUA_REFNIL;
940 globalconf.hooks.unfocus = LUA_REFNIL;
941 globalconf.hooks.mouse_enter = LUA_REFNIL;
942 globalconf.hooks.arrange = LUA_REFNIL;
943 globalconf.hooks.clients = LUA_REFNIL;
944 globalconf.hooks.tags = LUA_REFNIL;
945 globalconf.hooks.tagged = LUA_REFNIL;
946 globalconf.hooks.property = LUA_REFNIL;
947 globalconf.hooks.timer = LUA_REFNIL;
950 #define XDG_CONFIG_HOME_DEFAULT "/.config"
952 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
954 static bool
955 luaA_loadrc(const char *confpath, bool run)
957 if(confpath)
959 if(!luaL_loadfile(globalconf.L, confpath))
961 if(run)
963 if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
964 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
965 else
967 globalconf.conffile = a_strdup(confpath);
968 return true;
971 else
972 lua_pop(globalconf.L, 1);
973 return true;
975 else
976 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
978 return false;
981 /** Load a configuration file.
982 * \param confpatharg The configuration file to load.
983 * \param run Run the configuration file.
985 bool
986 luaA_parserc(const char *confpatharg, bool run)
988 int screen;
989 const char *confdir, *xdg_config_dirs;
990 char *confpath = NULL, **xdg_files, **buf, path[1024];
991 ssize_t len;
992 bool ret;
994 ret = luaA_loadrc(confpatharg, run);
996 confdir = getenv("XDG_CONFIG_HOME");
998 if((len = a_strlen(confdir)))
1000 len += sizeof(AWESOME_CONFIG_FILE);
1001 confpath = p_new(char, len);
1002 a_strcpy(confpath, len, confdir);
1003 /* update package.path */
1004 snprintf(path, sizeof(path), "package.path = package.path .. \";%s/awesome/?.lua\"", confdir);
1005 luaA_dostring(globalconf.L, path);
1007 else
1009 confdir = getenv("HOME");
1010 len = a_strlen(confdir) + sizeof(AWESOME_CONFIG_FILE)-1 + sizeof(XDG_CONFIG_HOME_DEFAULT);
1011 confpath = p_new(char, len);
1012 a_strcpy(confpath, len, confdir);
1013 a_strcat(confpath, len, XDG_CONFIG_HOME_DEFAULT);
1014 /* update package.path */
1015 snprintf(path, sizeof(path), "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?.lua\"", confdir);
1016 luaA_dostring(globalconf.L, path);
1018 a_strcat(confpath, len, AWESOME_CONFIG_FILE);
1020 if(!ret)
1021 ret = luaA_loadrc(confpath, run);
1023 xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
1025 if(!(len = a_strlen(xdg_config_dirs)))
1027 xdg_config_dirs = XDG_CONFIG_DIR;
1028 len = sizeof(XDG_CONFIG_DIR) - 1;
1031 xdg_files = a_strsplit(xdg_config_dirs, len, ':');
1033 for(buf = xdg_files; *buf; buf++)
1035 p_delete(&confpath);
1036 len = a_strlen(*buf) + sizeof("AWESOME_CONFIG_FILE");
1037 confpath = p_new(char, len);
1038 a_strcpy(confpath, len, *buf);
1039 a_strcat(confpath, len, AWESOME_CONFIG_FILE);
1040 snprintf(path, sizeof(path), "package.path = package.path .. \";%s/awesome/?.lua\"", *buf);
1041 luaA_dostring(globalconf.L, path);
1042 if(!ret)
1043 ret = luaA_loadrc(confpath, run);
1046 for(buf = xdg_files; *buf; buf++)
1047 p_delete(buf);
1048 p_delete(&xdg_files);
1050 /* Assure there's at least one tag */
1051 for(screen = 0; screen < globalconf.nscreen; screen++)
1052 if(!globalconf.screens[screen].tags.len)
1053 tag_append_to_screen(tag_new("default", sizeof("default")-1, layout_tile, 0.5, 1, 0),
1054 &globalconf.screens[screen]);
1056 p_delete(&confpath);
1058 return ret;
1061 /** Parse a command.
1062 * \param cmd The buffer to parse.
1063 * \return the number of elements pushed on the stack by the last statement in cmd.
1064 * If there's an error, the message is pushed onto the stack and this returns 1.
1066 static int
1067 luaA_docmd(const char *cmd)
1069 char *p;
1070 int newtop, oldtop = lua_gettop(globalconf.L);
1072 while((p = strchr(cmd, '\n')))
1074 newtop = lua_gettop(globalconf.L);
1075 lua_pop(globalconf.L, newtop - oldtop);
1076 oldtop = newtop;
1078 *p = '\0';
1079 if (luaL_dostring(globalconf.L, cmd))
1081 warn("error executing Lua code: %s", lua_tostring(globalconf.L, -1));
1082 return 1;
1084 cmd = p + 1;
1086 return lua_gettop(globalconf.L) - oldtop;
1089 /** Pushes a Lua array containing the top n elements of the stack.
1090 * \param n The number of elements to put in the array.
1092 static void
1093 luaA_array(int n)
1095 int i;
1096 lua_createtable(globalconf.L, n, 0);
1097 lua_insert(globalconf.L, -n - 1);
1099 for (i = n; i > 0; i--)
1100 lua_rawseti(globalconf.L, -i - 1, i);
1103 /** Maps the top n elements of the stack to the result of
1104 * applying a function to that element.
1105 * \param n The number of elements to map.
1106 * \luastack
1107 * \lparam The function to map the elements by. This should be
1108 * at position -(n + 1).
1110 static void
1111 luaA_map(int n)
1113 int i;
1114 for (i = 0; i < n; i++)
1116 lua_pushvalue(globalconf.L, -n - 1); /* copy of the function */
1117 lua_pushvalue(globalconf.L, -n - 1); /* value to map */
1118 lua_pcall(globalconf.L, 1, 1, 0); /* call function */
1119 lua_remove(globalconf.L, -n - 1); /* remove old value */
1121 lua_remove(globalconf.L, -n - 1); /* remove function */
1124 static void luaA_conn_cleanup(EV_P_ ev_io *w)
1126 ev_ref(EV_DEFAULT_UC);
1127 ev_io_stop(EV_DEFAULT_UC_ w);
1128 if (close(w->fd))
1129 warn("error closing UNIX domain socket: %s", strerror(errno));
1130 p_delete(&w);
1133 static void
1134 luaA_cb(EV_P_ ev_io *w, int revents)
1136 char buf[1024];
1137 int r, els;
1138 const char *s;
1139 size_t len;
1141 switch(r = recv(w->fd, buf, sizeof(buf)-1, MSG_TRUNC))
1143 case -1:
1144 warn("error reading UNIX domain socket: %s", strerror(errno));
1145 case 0: /* 0 bytes are only transferred when the connection is closed */
1146 luaA_conn_cleanup(EV_DEFAULT_UC_ w);
1147 break;
1148 default:
1149 if(r >= ssizeof(buf))
1150 break;
1151 buf[r] = '\0';
1152 lua_getglobal(globalconf.L, "table");
1153 lua_getfield(globalconf.L, -1, "concat");
1154 lua_remove(globalconf.L, -2); /* remove table */
1156 lua_getglobal(globalconf.L, "tostring");
1157 els = luaA_docmd(buf);
1158 luaA_map(els); /* map results to strings */
1159 luaA_array(els); /* put strings in an array */
1161 lua_pushstring(globalconf.L, "\t");
1162 lua_pcall(globalconf.L, 2, 1, 0); /* concatenate results with tabs */
1164 s = lua_tolstring(globalconf.L, -1, &len);
1166 /* ignore ENOENT because the client may not read */
1167 if (send(w->fd, s, len, MSG_DONTWAIT) == -1)
1168 switch(errno)
1170 case ENOENT:
1171 case EAGAIN:
1172 break;
1173 default:
1174 warn("can't send back to client via domain socket: %s", strerror(errno));
1175 break;
1178 lua_pop(globalconf.L, 1); /* pop the string */
1180 awesome_refresh(globalconf.connection);
1184 static void
1185 luaA_conn_cb(EV_P_ ev_io *w, int revents)
1187 ev_io *csio_conn = p_new(ev_io, 1);
1188 int csfd = accept(w->fd, NULL, NULL);
1190 ev_io_init(csio_conn, &luaA_cb, csfd, EV_READ);
1191 ev_io_start(EV_DEFAULT_UC_ csio_conn);
1192 ev_unref(EV_DEFAULT_UC);
1195 void
1196 luaA_cs_init(void)
1198 int csfd = socket_getclient();
1200 if (csfd < 0 || fcntl(csfd, F_SETFD, FD_CLOEXEC) == -1)
1201 return;
1203 addr = socket_getaddr(getenv("DISPLAY"));
1205 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1207 if(errno == EADDRINUSE)
1209 if(unlink(addr->sun_path))
1210 warn("error unlinking existing file: %s", strerror(errno));
1211 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1212 return warn("error binding UNIX domain socket: %s", strerror(errno));
1214 else
1215 return warn("error binding UNIX domain socket: %s", strerror(errno));
1217 listen(csfd, 10);
1219 ev_io_init(&csio, &luaA_conn_cb, csfd, EV_READ);
1220 ev_io_start(EV_DEFAULT_UC_ &csio);
1221 ev_unref(EV_DEFAULT_UC);
1224 void
1225 luaA_cs_cleanup(void)
1227 if(csio.fd < 0)
1228 return;
1229 ev_ref(EV_DEFAULT_UC);
1230 ev_io_stop(EV_DEFAULT_UC_ &csio);
1231 if (close(csio.fd))
1232 warn("error closing UNIX domain socket: %s", strerror(errno));
1233 if(unlink(addr->sun_path))
1234 warn("error unlinking UNIX domain socket: %s", strerror(errno));
1235 p_delete(&addr);
1236 csio.fd = -1;
1239 void
1240 luaA_on_timer(EV_P_ ev_timer *w, int revents)
1242 luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0, 0);
1243 awesome_refresh(globalconf.connection);
1246 /** Push a color as a string onto the stack
1247 * \param L The Lua VM state.
1248 * \param c The color to push.
1249 * \return The number of elements pushed on stack.
1252 luaA_pushcolor(lua_State *L, const xcolor_t *c)
1254 uint8_t r = (unsigned)c->red * 0xff / 0xffff;
1255 uint8_t g = (unsigned)c->green * 0xff / 0xffff;
1256 uint8_t b = (unsigned)c->blue * 0xff / 0xffff;
1257 uint8_t a = (unsigned)c->alpha * 0xff / 0xffff;
1258 char s[10];
1259 /* do not print alpha if it's full */
1260 if(a == 0xff)
1261 snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
1262 else
1263 snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
1264 lua_pushlstring(L, s, sizeof(s));
1265 return 1;