naughty: import
[awesome.git] / luaa.c
blobfe7f2ea7a84f7e221eaf2272ad4dec39ba45a42a
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 screen arrange. This function is called
222 * with the screen number as 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 screen arrange.
228 static int
229 luaA_hooks_arrange(lua_State *L)
231 return luaA_registerfct(L, 1, &globalconf.hooks.arrange);
234 /** Set the function called on each client's property change.
235 * This function is called with the client object as argument and the
236 * property name.
237 * \param L The Lua VM state.
238 * \return The number of elements pushed on stack.
239 * \luastack
240 * \lparam A function to call on each client property update.
242 static int
243 luaA_hooks_property(lua_State *L)
245 return luaA_registerfct(L, 1, &globalconf.hooks.property);
248 /** Set the function to be called every N seconds.
249 * \param L The Lua VM state.
250 * \return The number of elements pushed on stack.
251 * \luastack
252 * \lparam The number of seconds to run function every. Set 0 to disable.
253 * \lparam A function to call every N seconds (optional).
255 static int
256 luaA_hooks_timer(lua_State *L)
258 globalconf.timer.repeat = luaL_checknumber(L, 1);
260 if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
261 luaA_registerfct(L, 2, &globalconf.hooks.timer);
263 ev_timer_again(globalconf.loop, &globalconf.timer);
264 return 0;
267 /** Set or get default font. (DEPRECATED)
268 * \param L The Lua VM state.
269 * \return The number of elements pushed on stack.
270 * \luastack
271 * \lparam An optional string with a font name in Pango format.
272 * \lreturn The font used, in Pango format.
274 static int
275 luaA_font(lua_State *L)
277 char *font;
279 if(lua_gettop(L) == 1)
281 const char *newfont = luaL_checkstring(L, 1);
282 draw_font_delete(&globalconf.font);
283 globalconf.font = draw_font_new(globalconf.default_screen, newfont);
286 font = pango_font_description_to_string(globalconf.font->desc);
287 lua_pushstring(L, font);
288 g_free(font);
290 return 1;
293 /** Set default font. (DEPRECATED)
294 * \param L The Lua VM state.
295 * \return The number of elements pushed on stack.
296 * \luastack
297 * \lparam A string with a font name in Pango format.
299 static int
300 luaA_font_set(lua_State *L)
302 deprecate();
303 return luaA_font(L);
306 /** Get configuration file path used by awesome.
307 * \param L The Lua VM state.
308 * \return The number of elements pushed on stack.
309 * \luastack
310 * \lreturn The awesome configuration file path.
312 static int
313 luaA_conffile(lua_State *L)
315 lua_pushstring(L, globalconf.conffile);
316 return 1;
319 /** Set default colors.
320 * \param L The Lua VM state.
321 * \return The number of elements pushed on stack.
322 * \luastack
323 * \lparam A table with `fg' and `bg' elements, containing colors.
324 * \lreturn A table with `fg' and `bg' elements, containing colors.
326 static int
327 luaA_colors(lua_State *L)
329 if(lua_gettop(L) == 1)
331 const char *buf;
332 size_t len;
333 int8_t colors_nbr = -1, i;
334 xcolor_init_request_t reqs[2];
336 luaA_checktable(L, 1);
338 if((buf = luaA_getopt_lstring(L, 1, "fg", NULL, &len)))
339 reqs[++colors_nbr] = xcolor_init_unchecked(&globalconf.colors.fg, buf, len);
341 if((buf = luaA_getopt_lstring(L, 1, "bg", NULL, &len)))
342 reqs[++colors_nbr] = xcolor_init_unchecked(&globalconf.colors.bg, buf, len);
344 for(i = 0; i <= colors_nbr; i++)
345 xcolor_init_reply(reqs[i]);
348 lua_newtable(L);
349 luaA_pushcolor(L, &globalconf.colors.fg);
350 lua_setfield(L, -2, "fg");
351 luaA_pushcolor(L, &globalconf.colors.bg);
352 lua_setfield(L, -2, "bg");
354 return 1;
357 /** Set default colors. (DEPRECATED)
358 * \param L The Lua VM state.
359 * \return The number of elements pushed on stack.
361 * \luastack
362 * \lparam A table with `fg' and `bg' elements, containing colors.
364 static int
365 luaA_colors_set(lua_State *L)
367 deprecate();
368 return luaA_colors(L);
371 static void
372 luaA_openlib(lua_State *L, const char *name,
373 const struct luaL_reg methods[],
374 const struct luaL_reg meta[])
376 luaL_newmetatable(L, name); /* 1 */
377 lua_pushvalue(L, -1); /* dup metatable 2 */
378 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
380 luaL_register(L, NULL, meta); /* 1 */
381 luaL_register(L, name, methods); /* 2 */
382 lua_pushvalue(L, -1); /* dup self as metatable 3 */
383 lua_setmetatable(L, -2); /* set self as metatable 2 */
384 lua_pop(L, 2);
387 static int
388 luaA_mbstrlen(lua_State *L)
390 const char *cmd = luaL_checkstring(L, 1);
391 lua_pushnumber(L, mbstowcs(NULL, NONULL(cmd), 0));
392 return 1;
395 static int
396 luaA_next(lua_State *L)
398 if(luaL_getmetafield(L, 1, "__next"))
400 lua_insert(L, 1);
401 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
402 return lua_gettop(L);
405 luaL_checktype(L, 1, LUA_TTABLE);
406 lua_settop(L, 2);
407 if(lua_next(L, 1))
408 return 2;
409 lua_pushnil(L);
410 return 1;
413 static int
414 luaA_pairs(lua_State *L)
416 if(luaL_getmetafield(L, 1, "__pairs"))
418 lua_insert(L, 1);
419 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
420 return lua_gettop(L);
423 luaL_checktype(L, 1, LUA_TTABLE);
424 return luaA_generic_pairs(L);
427 /** Replace various standards Lua functions with our own.
428 * \param L The Lua VM state.
430 static void
431 luaA_fixups(lua_State *L)
433 lua_getglobal(L, "string");
434 lua_pushcfunction(L, luaA_mbstrlen);
435 lua_setfield(L, -2, "len");
436 lua_pop(L, 1);
437 lua_pushliteral(L, "next");
438 lua_pushcfunction(L, luaA_next);
439 lua_settable(L, LUA_GLOBALSINDEX);
440 lua_pushliteral(L, "pairs");
441 lua_pushcfunction(L, luaA_next);
442 lua_pushcclosure(L, luaA_pairs, 1); /* pairs get next as upvalue */
443 lua_settable(L, LUA_GLOBALSINDEX);
446 /** Object table.
447 * This table can use safely object as key.
448 * \param L The Lua VM state.
449 * \return The number of elements pushed on stack.
452 luaA_otable_index(lua_State *L)
454 void **obj, **v;
456 if((obj = lua_touserdata(L, 2)))
458 /* begins at nil */
459 lua_pushnil(L);
460 while(lua_next(L, 1))
462 /* check the key against the key if the key is a userdata,
463 * otherwise check it again the value. */
464 if((v = lua_touserdata(L, -2))
465 && *v == *obj)
466 /* return value */
467 return 1;
468 else if((v = lua_touserdata(L, -1))
469 && *v == *obj)
471 /* return key */
472 lua_pop(L, 1);
473 return 1;
475 /* removes 'value'; keeps 'key' for next iteration */
476 lua_pop(L, 1);
478 return 0;
481 lua_rawget(L, 1);
482 return 1;
485 /** Object table.
486 * This table can use safely object as key.
487 * \param L The Lua VM state.
488 * \return The number of elements pushed on stack.
490 static int
491 luaA_otable_newindex(lua_State *L)
493 void **obj, **v;
495 if((obj = lua_touserdata(L, 2)))
497 /* begins at nil */
498 lua_pushnil(L);
499 while(lua_next(L, 1))
501 if((v = lua_touserdata(L, -2))
502 && *v == *obj)
504 /* remove value */
505 lua_pop(L, 1);
506 /* push new value on top */
507 lua_pushvalue(L, 3);
508 /* set in table key = value */
509 lua_rawset(L, 1);
510 return 0;
512 /* removes 'value'; keeps 'key' for next iteration */
513 lua_pop(L, 1);
517 lua_rawset(L, 1);
519 return 0;
522 /** Spawn a program.
523 * This function is multi-head (Zaphod) aware and will set display to
524 * the right screen according to mouse position.
525 * \param L The Lua VM state.
526 * \return The number of elements pushed on stack
527 * \luastack
528 * \lparam The command to launch.
529 * \lparam The optional screen number to spawn the command on.
531 static int
532 luaA_spawn(lua_State *L)
534 char *host, newdisplay[128];
535 const char *cmd;
536 int screen = 0, screenp, displayp;
538 if(lua_gettop(L) == 2)
540 screen = luaL_checknumber(L, 2) - 1;
541 luaA_checkscreen(screen);
544 cmd = luaL_checkstring(L, 1);
546 if(!globalconf.xinerama_is_active)
548 xcb_parse_display(NULL, &host, &displayp, &screenp);
549 snprintf(newdisplay, sizeof(newdisplay), "%s:%d.%d", host, displayp, screen);
550 setenv("DISPLAY", newdisplay, 1);
551 p_delete(&host);
554 /* The double-fork construct avoids zombie processes and keeps the code
555 * clean from stupid signal handlers. */
556 if(fork() == 0)
558 if(fork() == 0)
560 if(globalconf.connection)
561 xcb_disconnect(globalconf.connection);
562 setsid();
563 a_exec(cmd);
564 warn("execl '%s' failed: %s\n", cmd, strerror(errno));
566 exit(EXIT_SUCCESS);
568 wait(0);
570 return 0;
573 /** Initialize the Lua VM
575 void
576 luaA_init(void)
578 lua_State *L;
580 static const struct luaL_reg otable_methods[] =
582 { "__call", luaA_otable_new },
583 { NULL, NULL }
585 static const struct luaL_reg otable_meta[] =
587 { "__index", luaA_otable_index },
588 { "__newindex", luaA_otable_newindex },
589 { NULL, NULL }
591 static const struct luaL_reg awesome_lib[] =
593 { "quit", luaA_quit },
594 { "exec", luaA_exec },
595 { "spawn", luaA_spawn },
596 { "restart", luaA_restart },
597 { "buttons", luaA_buttons },
598 { "font_set", luaA_font_set },
599 { "colors_set", luaA_colors_set },
600 { "font", luaA_font },
601 { "colors", luaA_colors },
602 { "conffile", luaA_conffile },
603 { NULL, NULL }
605 static const struct luaL_reg awesome_hooks_lib[] =
607 { "focus", luaA_hooks_focus },
608 { "unfocus", luaA_hooks_unfocus },
609 { "manage", luaA_hooks_manage },
610 { "unmanage", luaA_hooks_unmanage },
611 { "mouse_enter", luaA_hooks_mouse_enter },
612 { "property", luaA_hooks_property },
613 { "arrange", luaA_hooks_arrange },
614 { "timer", luaA_hooks_timer },
615 /* deprecated */
616 { "mouse_over", luaA_hooks_mouse_over },
617 { NULL, NULL }
620 L = globalconf.L = luaL_newstate();
622 luaL_openlibs(L);
624 luaA_fixups(L);
626 /* Export awesome lib */
627 luaL_register(L, "awesome", awesome_lib);
629 /* Export hooks lib */
630 luaL_register(L, "hooks", awesome_hooks_lib);
632 /* Export keygrabber lib */
633 luaL_register(L, "keygrabber", awesome_keygrabber_lib);
635 /* Export otable lib */
636 luaA_openlib(L, "otable", otable_methods, otable_meta);
638 /* Export screen */
639 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
641 /* Export mouse */
642 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
644 /* Export button */
645 luaA_openlib(L, "button", awesome_button_methods, awesome_button_meta);
647 /* Export image */
648 luaA_openlib(L, "image", awesome_image_methods, awesome_image_meta);
650 /* Export tag */
651 luaA_openlib(L, "tag", awesome_tag_methods, awesome_tag_meta);
653 /* Export statusbar */
654 luaA_openlib(L, "statusbar", awesome_statusbar_methods, awesome_statusbar_meta);
656 /* Export wibox */
657 luaA_openlib(L, "wibox", awesome_wibox_methods, awesome_wibox_meta);
659 /* Export widget */
660 luaA_openlib(L, "widget", awesome_widget_methods, awesome_widget_meta);
662 /* Export client */
663 luaA_openlib(L, "client", awesome_client_methods, awesome_client_meta);
665 /* Export titlebar */
666 luaA_openlib(L, "titlebar", awesome_titlebar_methods, awesome_titlebar_meta);
668 /* Export keys */
669 luaA_openlib(L, "keybinding", awesome_keybinding_methods, awesome_keybinding_meta);
671 lua_pushliteral(L, "AWESOME_VERSION");
672 lua_pushstring(L, AWESOME_VERSION);
673 lua_settable(L, LUA_GLOBALSINDEX);
675 lua_pushliteral(L, "AWESOME_RELEASE");
676 lua_pushstring(L, AWESOME_RELEASE);
677 lua_settable(L, LUA_GLOBALSINDEX);
679 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?.lua\"");
680 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?/init.lua\"");
682 /* init hooks */
683 globalconf.hooks.manage = LUA_REFNIL;
684 globalconf.hooks.unmanage = LUA_REFNIL;
685 globalconf.hooks.focus = LUA_REFNIL;
686 globalconf.hooks.unfocus = LUA_REFNIL;
687 globalconf.hooks.mouse_enter = LUA_REFNIL;
688 globalconf.hooks.arrange = LUA_REFNIL;
689 globalconf.hooks.property = LUA_REFNIL;
690 globalconf.hooks.timer = LUA_REFNIL;
693 #define XDG_CONFIG_HOME_DEFAULT "/.config"
695 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
697 static bool
698 luaA_loadrc(const char *confpath, bool run)
700 if(confpath)
702 if(!luaL_loadfile(globalconf.L, confpath))
704 if(run)
706 if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
707 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
708 else
710 globalconf.conffile = a_strdup(confpath);
711 return true;
714 else
715 lua_pop(globalconf.L, 1);
716 return true;
718 else
719 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
721 return false;
724 /** Load a configuration file.
725 * \param confpatharg The configuration file to load.
726 * \param run Run the configuration file.
728 bool
729 luaA_parserc(const char *confpatharg, bool run)
731 int screen;
732 const char *confdir, *xdg_config_dirs;
733 char *confpath = NULL, **xdg_files, **buf, path[1024];
734 ssize_t len;
735 bool ret;
737 ret = luaA_loadrc(confpatharg, run);
739 confdir = getenv("XDG_CONFIG_HOME");
741 if((len = a_strlen(confdir)))
743 len += sizeof(AWESOME_CONFIG_FILE);
744 confpath = p_new(char, len);
745 a_strcpy(confpath, len, confdir);
746 /* update package.path */
747 snprintf(path, sizeof(path), "package.path = package.path .. \";%s/awesome/?.lua\"", confdir);
748 luaA_dostring(globalconf.L, path);
750 else
752 confdir = getenv("HOME");
753 len = a_strlen(confdir) + sizeof(AWESOME_CONFIG_FILE)-1 + sizeof(XDG_CONFIG_HOME_DEFAULT);
754 confpath = p_new(char, len);
755 a_strcpy(confpath, len, confdir);
756 a_strcat(confpath, len, XDG_CONFIG_HOME_DEFAULT);
757 /* update package.path */
758 snprintf(path, sizeof(path), "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?.lua\"", confdir);
759 luaA_dostring(globalconf.L, path);
761 a_strcat(confpath, len, AWESOME_CONFIG_FILE);
763 if(!ret)
764 ret = luaA_loadrc(confpath, run);
766 xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
768 if(!(len = a_strlen(xdg_config_dirs)))
770 xdg_config_dirs = XDG_CONFIG_DIR;
771 len = sizeof(XDG_CONFIG_DIR) - 1;
774 xdg_files = a_strsplit(xdg_config_dirs, len, ':');
776 for(buf = xdg_files; *buf; buf++)
778 p_delete(&confpath);
779 len = a_strlen(*buf) + sizeof("AWESOME_CONFIG_FILE");
780 confpath = p_new(char, len);
781 a_strcpy(confpath, len, *buf);
782 a_strcat(confpath, len, AWESOME_CONFIG_FILE);
783 snprintf(path, sizeof(path), "package.path = package.path .. \";%s/awesome/?.lua\"", *buf);
784 luaA_dostring(globalconf.L, path);
785 if(!ret)
786 ret = luaA_loadrc(confpath, run);
789 for(buf = xdg_files; *buf; buf++)
790 p_delete(buf);
791 p_delete(&xdg_files);
793 /* Assure there's at least one tag */
794 for(screen = 0; screen < globalconf.nscreen; screen++)
795 if(!globalconf.screens[screen].tags.len)
796 tag_append_to_screen(tag_new("default", sizeof("default")-1, layout_tile, 0.5, 1, 0),
797 &globalconf.screens[screen]);
799 p_delete(&confpath);
801 return ret;
804 /** Parse a command.
805 * \param cmd The buffer to parse.
806 * \return the number of elements pushed on the stack by the last statement in cmd.
807 * If there's an error, the message is pushed onto the stack and this returns 1.
809 static int
810 luaA_docmd(const char *cmd)
812 char *p;
813 int newtop, oldtop = lua_gettop(globalconf.L);
815 while((p = strchr(cmd, '\n')))
817 newtop = lua_gettop(globalconf.L);
818 lua_pop(globalconf.L, newtop - oldtop);
819 oldtop = newtop;
821 *p = '\0';
822 if (luaL_dostring(globalconf.L, cmd))
824 warn("error executing Lua code: %s", lua_tostring(globalconf.L, -1));
825 return 1;
827 cmd = p + 1;
829 return lua_gettop(globalconf.L) - oldtop;
832 /** Pushes a Lua array containing the top n elements of the stack.
833 * \param n The number of elements to put in the array.
835 static void
836 luaA_array(int n)
838 int i;
839 lua_createtable(globalconf.L, n, 0);
840 lua_insert(globalconf.L, -n - 1);
842 for (i = n; i > 0; i--)
843 lua_rawseti(globalconf.L, -i - 1, i);
846 /** Maps the top n elements of the stack to the result of
847 * applying a function to that element.
848 * \param n The number of elements to map.
849 * \luastack
850 * \lparam The function to map the elements by. This should be
851 * at position -(n + 1).
853 static void
854 luaA_map(int n)
856 int i;
857 for (i = 0; i < n; i++)
859 lua_pushvalue(globalconf.L, -n - 1); /* copy of the function */
860 lua_pushvalue(globalconf.L, -n - 1); /* value to map */
861 lua_pcall(globalconf.L, 1, 1, 0); /* call function */
862 lua_remove(globalconf.L, -n - 1); /* remove old value */
864 lua_remove(globalconf.L, -n - 1); /* remove function */
867 static void luaA_conn_cleanup(EV_P_ ev_io *w)
869 ev_ref(EV_DEFAULT_UC);
870 ev_io_stop(EV_DEFAULT_UC_ w);
871 if (close(w->fd))
872 warn("error closing UNIX domain socket: %s", strerror(errno));
873 p_delete(&w);
876 static void
877 luaA_cb(EV_P_ ev_io *w, int revents)
879 char buf[1024];
880 int r, els;
881 const char *s;
882 size_t len;
884 switch(r = recv(w->fd, buf, sizeof(buf)-1, MSG_TRUNC))
886 case -1:
887 warn("error reading UNIX domain socket: %s", strerror(errno));
888 case 0: /* 0 bytes are only transferred when the connection is closed */
889 luaA_conn_cleanup(EV_DEFAULT_UC_ w);
890 break;
891 default:
892 if(r >= ssizeof(buf))
893 break;
894 buf[r] = '\0';
895 lua_getglobal(globalconf.L, "table");
896 lua_getfield(globalconf.L, -1, "concat");
897 lua_remove(globalconf.L, -2); /* remove table */
899 lua_getglobal(globalconf.L, "tostring");
900 els = luaA_docmd(buf);
901 luaA_map(els); /* map results to strings */
902 luaA_array(els); /* put strings in an array */
904 lua_pushstring(globalconf.L, "\t");
905 lua_pcall(globalconf.L, 2, 1, 0); /* concatenate results with tabs */
907 s = lua_tolstring(globalconf.L, -1, &len);
909 /* ignore ENOENT because the client may not read */
910 if (send(w->fd, s, len, MSG_DONTWAIT) == -1)
911 switch(errno)
913 case ENOENT:
914 case EAGAIN:
915 break;
916 default:
917 warn("can't send back to client via domain socket: %s", strerror(errno));
918 break;
921 lua_pop(globalconf.L, 1); /* pop the string */
923 awesome_refresh(globalconf.connection);
927 static void
928 luaA_conn_cb(EV_P_ ev_io *w, int revents)
930 ev_io *csio_conn = p_new(ev_io, 1);
931 int csfd = accept(w->fd, NULL, NULL);
933 ev_io_init(csio_conn, &luaA_cb, csfd, EV_READ);
934 ev_io_start(EV_DEFAULT_UC_ csio_conn);
935 ev_unref(EV_DEFAULT_UC);
938 void
939 luaA_cs_init(void)
941 int csfd = socket_getclient();
943 if (csfd < 0 || fcntl(csfd, F_SETFD, FD_CLOEXEC) == -1)
944 return;
946 addr = socket_getaddr(getenv("DISPLAY"));
948 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
950 if(errno == EADDRINUSE)
952 if(unlink(addr->sun_path))
953 warn("error unlinking existing file: %s", strerror(errno));
954 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
955 return warn("error binding UNIX domain socket: %s", strerror(errno));
957 else
958 return warn("error binding UNIX domain socket: %s", strerror(errno));
960 listen(csfd, 10);
962 ev_io_init(&csio, &luaA_conn_cb, csfd, EV_READ);
963 ev_io_start(EV_DEFAULT_UC_ &csio);
964 ev_unref(EV_DEFAULT_UC);
967 void
968 luaA_cs_cleanup(void)
970 if(csio.fd < 0)
971 return;
972 ev_ref(EV_DEFAULT_UC);
973 ev_io_stop(EV_DEFAULT_UC_ &csio);
974 if (close(csio.fd))
975 warn("error closing UNIX domain socket: %s", strerror(errno));
976 if(unlink(addr->sun_path))
977 warn("error unlinking UNIX domain socket: %s", strerror(errno));
978 p_delete(&addr);
979 csio.fd = -1;
982 void
983 luaA_on_timer(EV_P_ ev_timer *w, int revents)
985 luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0, 0);
986 awesome_refresh(globalconf.connection);
989 void
990 luaA_pushcolor(lua_State *L, const xcolor_t *c)
992 uint8_t r = (unsigned)c->red * 0xff / 0xffff;
993 uint8_t g = (unsigned)c->green * 0xff / 0xffff;
994 uint8_t b = (unsigned)c->blue * 0xff / 0xffff;
995 uint8_t a = (unsigned)c->alpha * 0xff / 0xffff;
996 char s[10];
997 /* do not print alpha if it's full */
998 if(a == 0xff)
999 snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
1000 else
1001 snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
1002 lua_pushlstring(L, s, sizeof(s));