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.
24 #include <sys/socket.h>
27 #include <sys/types.h>
41 #include "awesome-version-internal.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_hooks_lib
[];
58 extern const struct luaL_reg awesome_keygrabber_lib
[];
59 extern const struct luaL_reg awesome_button_methods
[];
60 extern const struct luaL_reg awesome_button_meta
[];
61 extern const struct luaL_reg awesome_image_methods
[];
62 extern const struct luaL_reg awesome_image_meta
[];
63 extern const struct luaL_reg awesome_mouse_methods
[];
64 extern const struct luaL_reg awesome_mouse_meta
[];
65 extern const struct luaL_reg awesome_screen_methods
[];
66 extern const struct luaL_reg awesome_screen_meta
[];
67 extern const struct luaL_reg awesome_client_methods
[];
68 extern const struct luaL_reg awesome_client_meta
[];
69 extern const struct luaL_reg awesome_titlebar_methods
[];
70 extern const struct luaL_reg awesome_titlebar_meta
[];
71 extern const struct luaL_reg awesome_tag_methods
[];
72 extern const struct luaL_reg awesome_tag_meta
[];
73 extern const struct luaL_reg awesome_widget_methods
[];
74 extern const struct luaL_reg awesome_widget_meta
[];
75 extern const struct luaL_reg awesome_statusbar_methods
[];
76 extern const struct luaL_reg awesome_statusbar_meta
[];
77 extern const struct luaL_reg awesome_wibox_methods
[];
78 extern const struct luaL_reg awesome_wibox_meta
[];
79 extern const struct luaL_reg awesome_keybinding_methods
[];
80 extern const struct luaL_reg awesome_keybinding_meta
[];
82 static struct sockaddr_un
*addr
;
83 static ev_io csio
= { .fd
= -1 };
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.
91 * \lparam An array of mouse button bindings objects, or nothing.
92 * \return The array of mouse button bindings objects of this client.
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
);
106 * \param L The Lua VM state.
107 * \return The number of elements pushed on stack.
110 luaA_quit(lua_State
*L
__attribute__ ((unused
)))
112 ev_unloop(globalconf
.loop
, 1);
116 /** Execute another application, probably a window manager, to replace
118 * \param L The Lua VM state.
119 * \return The number of elements pushed on stack.
121 * \lparam The command line to execute.
124 luaA_exec(lua_State
*L
)
126 const char *cmd
= luaL_checkstring(L
, 1);
137 luaA_restart(lua_State
*L
__attribute__ ((unused
)))
143 /** Set or get default font. (DEPRECATED)
144 * \param L The Lua VM state.
145 * \return The number of elements pushed on stack.
147 * \lparam An optional string with a font name in Pango format.
148 * \lreturn The font used, in Pango format.
151 luaA_font(lua_State
*L
)
155 if(lua_gettop(L
) == 1)
157 const char *newfont
= luaL_checkstring(L
, 1);
158 draw_font_delete(&globalconf
.font
);
159 globalconf
.font
= draw_font_new(newfont
);
162 font
= pango_font_description_to_string(globalconf
.font
->desc
);
163 lua_pushstring(L
, font
);
169 /** Set default font. (DEPRECATED)
170 * \param L The Lua VM state.
171 * \return The number of elements pushed on stack.
173 * \lparam A string with a font name in Pango format.
176 luaA_font_set(lua_State
*L
)
182 /** Get configuration file path used by awesome.
183 * \param L The Lua VM state.
184 * \return The number of elements pushed on stack.
186 * \lreturn The awesome configuration file path.
189 luaA_conffile(lua_State
*L
)
191 lua_pushstring(L
, globalconf
.conffile
);
195 /** Set default colors.
196 * \param L The Lua VM state.
197 * \return The number of elements pushed on stack.
199 * \lparam A table with `fg' and `bg' elements, containing colors.
200 * \lreturn A table with `fg' and `bg' elements, containing colors.
203 luaA_colors(lua_State
*L
)
205 if(lua_gettop(L
) == 1)
209 int8_t colors_nbr
= -1, i
;
210 xcolor_init_request_t reqs
[2];
212 luaA_checktable(L
, 1);
214 if((buf
= luaA_getopt_lstring(L
, 1, "fg", NULL
, &len
)))
215 reqs
[++colors_nbr
] = xcolor_init_unchecked(&globalconf
.colors
.fg
, buf
, len
);
217 if((buf
= luaA_getopt_lstring(L
, 1, "bg", NULL
, &len
)))
218 reqs
[++colors_nbr
] = xcolor_init_unchecked(&globalconf
.colors
.bg
, buf
, len
);
220 for(i
= 0; i
<= colors_nbr
; i
++)
221 xcolor_init_reply(reqs
[i
]);
225 luaA_pushcolor(L
, &globalconf
.colors
.fg
);
226 lua_setfield(L
, -2, "fg");
227 luaA_pushcolor(L
, &globalconf
.colors
.bg
);
228 lua_setfield(L
, -2, "bg");
233 /** Set default colors. (DEPRECATED)
234 * \param L The Lua VM state.
235 * \return The number of elements pushed on stack.
238 * \lparam A table with `fg' and `bg' elements, containing colors.
241 luaA_colors_set(lua_State
*L
)
244 return luaA_colors(L
);
248 luaA_openlib(lua_State
*L
, const char *name
,
249 const struct luaL_reg methods
[],
250 const struct luaL_reg meta
[])
252 luaL_newmetatable(L
, name
); /* 1 */
253 lua_pushvalue(L
, -1); /* dup metatable 2 */
254 lua_setfield(L
, -2, "__index"); /* metatable.__index = metatable 1 */
256 luaL_register(L
, NULL
, meta
); /* 1 */
257 luaL_register(L
, name
, methods
); /* 2 */
258 lua_pushvalue(L
, -1); /* dup self as metatable 3 */
259 lua_setmetatable(L
, -2); /* set self as metatable 2 */
263 /** UTF-8 aware string length computing.
264 * \param L The Lua VM state.
265 * \return The number of elements pushed on stack.
268 luaA_mbstrlen(lua_State
*L
)
270 const char *cmd
= luaL_checkstring(L
, 1);
271 lua_pushnumber(L
, mbstowcs(NULL
, NONULL(cmd
), 0));
275 /** Overload standard Lua next function to use __next key on metatable.
276 * \param L The Lua VM state.
277 * \param The number of elements pushed on stack.
280 luaAe_next(lua_State
*L
)
282 if(luaL_getmetafield(L
, 1, "__next"))
285 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
286 return lua_gettop(L
);
289 luaL_checktype(L
, 1, LUA_TTABLE
);
297 /** Overload lua_next() function by using __next metatable field
298 * to get next elements.
299 * \param L The Lua VM stack.
300 * \param idx The index number of elements in stack.
301 * \return 1 if more elements to come, 0 otherwise.
304 luaA_next(lua_State
*L
, int idx
)
306 if(luaL_getmetafield(L
, idx
, "__next"))
308 /* if idx is relative, reduce it since we got __next */
310 /* copy table and then move key */
311 lua_pushvalue(L
, idx
);
312 lua_pushvalue(L
, -3);
314 lua_pcall(L
, 2, 2, 0);
315 /* next returned nil, it's the end */
325 return lua_next(L
, idx
);
328 /** Generic pairs function.
329 * \param L The Lua VM state.
330 * \return The number of elements pushed on stack.
333 luaA_generic_pairs(lua_State
*L
)
335 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
336 lua_pushvalue(L
, 1); /* state, */
337 lua_pushnil(L
); /* and initial value */
341 /** Overload standard pairs function to use __pairs field of metatables.
342 * \param L The Lua VM state.
343 * \return The number of elements pushed on stack.
346 luaAe_pairs(lua_State
*L
)
348 if(luaL_getmetafield(L
, 1, "__pairs"))
351 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
352 return lua_gettop(L
);
355 luaL_checktype(L
, 1, LUA_TTABLE
);
356 return luaA_generic_pairs(L
);
359 /** Replace various standards Lua functions with our own.
360 * \param L The Lua VM state.
363 luaA_fixups(lua_State
*L
)
365 lua_getglobal(L
, "string");
366 lua_pushcfunction(L
, luaA_mbstrlen
);
367 lua_setfield(L
, -2, "len");
369 lua_pushliteral(L
, "next");
370 lua_pushcfunction(L
, luaAe_next
);
371 lua_settable(L
, LUA_GLOBALSINDEX
);
372 lua_pushliteral(L
, "pairs");
373 lua_pushcfunction(L
, luaAe_next
);
374 lua_pushcclosure(L
, luaAe_pairs
, 1); /* pairs get next as upvalue */
375 lua_settable(L
, LUA_GLOBALSINDEX
);
378 /** __next function for wtable objects.
379 * \param L The Lua VM state.
380 * \return The number of elements pushed on stack.
383 luaA_wtable_next(lua_State
*L
)
385 /* upvalue 1 is content table */
386 if(lua_next(L
, lua_upvalueindex(1)))
392 /** Index function of wtable objects.
393 * \param L The Lua VM state.
394 * \return The number of elements pushed on stack.
397 luaA_wtable_index(lua_State
*L
)
403 /* check for size, waiting lua 5.2 and __len on tables */
404 if((buf
= lua_tolstring(L
, -1, &len
)))
405 if(a_tokenize(buf
, len
) == A_TK_LEN
)
407 lua_pushnumber(L
, lua_objlen(L
, lua_upvalueindex(1)));
412 /* upvalue 1 is content table */
413 lua_rawget(L
, lua_upvalueindex(1));
417 /** Newndex function of wtable objects.
418 * \param L The Lua VM state.
419 * \return The number of elements pushed on stack.
422 luaA_wtable_newindex(lua_State
*L
)
424 bool invalid
= false;
426 /* push key on top */
428 /* get current key value in content table */
429 lua_rawget(L
, lua_upvalueindex(1));
430 /* if value is a widget, notify change */
431 if(lua_istable(L
, -1) || luaA_toudata(L
, -1, "widget"))
434 lua_pop(L
, 1); /* remove value */
436 /* if new value is a widget or a table */
437 if(lua_istable(L
, 3))
439 luaA_table2wtable(L
);
442 else if(!invalid
&& luaA_toudata(L
, 3, "widget"))
445 /* upvalue 1 is content table */
446 lua_rawset(L
, lua_upvalueindex(1));
449 luaA_wibox_invalidate_byitem(L
, lua_topointer(L
, 1));
454 /** Convert the top element of the stack to a proxied wtable.
455 * \param L The Lua VM state.
458 luaA_table2wtable(lua_State
*L
)
460 if(!lua_istable(L
, -1))
463 lua_newtable(L
); /* create *real* content table */
464 lua_newtable(L
); /* metatable */
465 lua_pushvalue(L
, -2); /* copy content table */
466 lua_pushcclosure(L
, luaA_wtable_next
, 1); /* __next has the content table as upvalue */
467 lua_pushvalue(L
, -3); /* copy content table */
468 lua_pushcclosure(L
, luaA_wtable_index
, 1); /* __index has the content table as upvalue */
469 lua_pushvalue(L
, -4); /* copy content table */
470 lua_pushcclosure(L
, luaA_wtable_newindex
, 1); /* __newindex has the content table as upvalue */
471 /* set metatable field with just pushed closure */
472 lua_setfield(L
, -4, "__newindex");
473 lua_setfield(L
, -3, "__index");
474 lua_setfield(L
, -2, "__next");
475 /* set metatable impossible to touch */
476 lua_pushliteral(L
, "wtable");
477 lua_setfield(L
, -2, "__metatable");
478 /* set new metatable on original table */
479 lua_setmetatable(L
, -3);
483 /* go through original table */
484 while(lua_next(L
, -3))
486 /* if convert value to wtable */
487 luaA_table2wtable(L
);
489 lua_pushvalue(L
, -2);
490 /* move key before value */
492 /* set same value in content table */
495 lua_pushvalue(L
, -1);
496 /* push the new value :-> */
498 /* set orig[k] = nil */
501 /* remove content table */
505 /** Look for an item: table, function, etc.
506 * \param L The Lua VM state.
507 * \param item The pointer item.
510 luaA_hasitem(lua_State
*L
, const void *item
)
513 while(luaA_next(L
, -2))
515 if(lua_topointer(L
, -1) == item
)
517 /* remove value and key */
521 if(lua_istable(L
, -1))
522 if(luaA_hasitem(L
, item
))
524 /* remove key and value */
534 /** Check if a table is a loop. When using table as direct acyclic digram,
536 * \param L The Lua VM state.
537 * \param idx The index of the table in the stack
538 * \return True if the table loops.
541 luaA_isloop(lua_State
*L
, int idx
)
543 if(lua_istable(L
, idx
))
545 lua_pushvalue(L
, idx
); /* push table on top */
546 if(luaA_hasitem(L
, lua_topointer(L
, -1)))
548 lua_pop(L
, 1); /* remove pushed table */
552 while(luaA_next(L
, -2))
554 /* check for recursivity */
555 if(luaA_isloop(L
, -1))
557 lua_pop(L
, 2); /* remove key and value */
560 lua_pop(L
, 1); /* remove value */
562 lua_pop(L
, 1); /* remove pushed table */
568 * This table can use safely object as key.
569 * \param L The Lua VM state.
570 * \return The number of elements pushed on stack.
573 luaA_otable_index(lua_State
*L
)
577 if((obj
= lua_touserdata(L
, 2)))
581 while(lua_next(L
, 1))
583 /* check the key against the key if the key is a userdata,
584 * otherwise check it again the value. */
585 if((v
= lua_touserdata(L
, -2))
589 else if((v
= lua_touserdata(L
, -1))
596 /* removes 'value'; keeps 'key' for next iteration */
607 * This table can use safely object as key.
608 * \param L The Lua VM state.
609 * \return The number of elements pushed on stack.
612 luaA_otable_newindex(lua_State
*L
)
616 if((obj
= lua_touserdata(L
, 2)))
620 while(lua_next(L
, 1))
622 if((v
= lua_touserdata(L
, -2))
627 /* push new value on top */
629 /* set in table key = value */
633 /* removes 'value'; keeps 'key' for next iteration */
644 * This function is multi-head (Zaphod) aware and will set display to
645 * the right screen according to mouse position.
646 * \param L The Lua VM state.
647 * \return The number of elements pushed on stack
649 * \lparam The command to launch.
650 * \lparam The optional screen number to spawn the command on.
653 luaA_spawn(lua_State
*L
)
655 char *host
, newdisplay
[128];
657 int screen
= 0, screenp
, displayp
;
659 if(lua_gettop(L
) == 2)
661 screen
= luaL_checknumber(L
, 2) - 1;
662 luaA_checkscreen(screen
);
665 cmd
= luaL_checkstring(L
, 1);
667 if(!globalconf
.xinerama_is_active
)
669 xcb_parse_display(NULL
, &host
, &displayp
, &screenp
);
670 snprintf(newdisplay
, sizeof(newdisplay
), "%s:%d.%d", host
, displayp
, screen
);
671 setenv("DISPLAY", newdisplay
, 1);
675 /* The double-fork construct avoids zombie processes and keeps the code
676 * clean from stupid signal handlers. */
681 if(globalconf
.connection
)
682 xcb_disconnect(globalconf
.connection
);
685 warn("execl '%s' failed: %s\n", cmd
, strerror(errno
));
694 /** Initialize the Lua VM
701 static const struct luaL_reg otable_methods
[] =
703 { "__call", luaA_otable_new
},
706 static const struct luaL_reg otable_meta
[] =
708 { "__index", luaA_otable_index
},
709 { "__newindex", luaA_otable_newindex
},
712 static const struct luaL_reg awesome_lib
[] =
714 { "quit", luaA_quit
},
715 { "exec", luaA_exec
},
716 { "spawn", luaA_spawn
},
717 { "restart", luaA_restart
},
718 { "buttons", luaA_buttons
},
719 { "font_set", luaA_font_set
},
720 { "colors_set", luaA_colors_set
},
721 { "font", luaA_font
},
722 { "colors", luaA_colors
},
723 { "conffile", luaA_conffile
},
727 L
= globalconf
.L
= luaL_newstate();
733 /* Export awesome lib */
734 luaL_register(L
, "awesome", awesome_lib
);
736 /* Export hooks lib */
737 luaL_register(L
, "hooks", awesome_hooks_lib
);
739 /* Export keygrabber lib */
740 luaL_register(L
, "keygrabber", awesome_keygrabber_lib
);
742 /* Export otable lib */
743 luaA_openlib(L
, "otable", otable_methods
, otable_meta
);
746 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
749 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
752 luaA_openlib(L
, "button", awesome_button_methods
, awesome_button_meta
);
755 luaA_openlib(L
, "image", awesome_image_methods
, awesome_image_meta
);
758 luaA_openlib(L
, "tag", awesome_tag_methods
, awesome_tag_meta
);
760 /* Export statusbar */
761 luaA_openlib(L
, "statusbar", awesome_statusbar_methods
, awesome_statusbar_meta
);
764 luaA_openlib(L
, "wibox", awesome_wibox_methods
, awesome_wibox_meta
);
767 luaA_openlib(L
, "widget", awesome_widget_methods
, awesome_widget_meta
);
770 luaA_openlib(L
, "client", awesome_client_methods
, awesome_client_meta
);
772 /* Export titlebar */
773 luaA_openlib(L
, "titlebar", awesome_titlebar_methods
, awesome_titlebar_meta
);
776 luaA_openlib(L
, "keybinding", awesome_keybinding_methods
, awesome_keybinding_meta
);
778 lua_pushliteral(L
, "AWESOME_VERSION");
779 lua_pushstring(L
, AWESOME_VERSION
);
780 lua_settable(L
, LUA_GLOBALSINDEX
);
782 lua_pushliteral(L
, "AWESOME_RELEASE");
783 lua_pushstring(L
, AWESOME_RELEASE
);
784 lua_settable(L
, LUA_GLOBALSINDEX
);
786 luaA_dostring(L
, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH
"/?.lua\"");
787 luaA_dostring(L
, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH
"/?/init.lua\"");
790 globalconf
.hooks
.manage
= LUA_REFNIL
;
791 globalconf
.hooks
.unmanage
= LUA_REFNIL
;
792 globalconf
.hooks
.focus
= LUA_REFNIL
;
793 globalconf
.hooks
.unfocus
= LUA_REFNIL
;
794 globalconf
.hooks
.mouse_enter
= LUA_REFNIL
;
795 globalconf
.hooks
.arrange
= LUA_REFNIL
;
796 globalconf
.hooks
.clients
= LUA_REFNIL
;
797 globalconf
.hooks
.tags
= LUA_REFNIL
;
798 globalconf
.hooks
.tagged
= LUA_REFNIL
;
799 globalconf
.hooks
.property
= LUA_REFNIL
;
800 globalconf
.hooks
.timer
= LUA_REFNIL
;
803 #define XDG_CONFIG_HOME_DEFAULT "/.config"
805 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
808 luaA_loadrc(const char *confpath
, bool run
)
812 if(!luaL_loadfile(globalconf
.L
, confpath
))
816 if(lua_pcall(globalconf
.L
, 0, LUA_MULTRET
, 0))
817 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
820 globalconf
.conffile
= a_strdup(confpath
);
825 lua_pop(globalconf
.L
, 1);
829 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
834 /** Load a configuration file.
835 * \param confpatharg The configuration file to load.
836 * \param run Run the configuration file.
839 luaA_parserc(const char *confpatharg
, bool run
)
842 const char *confdir
, *xdg_config_dirs
;
843 char *confpath
= NULL
, **xdg_files
, **buf
, path
[1024];
847 ret
= luaA_loadrc(confpatharg
, run
);
849 confdir
= getenv("XDG_CONFIG_HOME");
851 if((len
= a_strlen(confdir
)))
853 len
+= sizeof(AWESOME_CONFIG_FILE
);
854 confpath
= p_new(char, len
);
855 a_strcpy(confpath
, len
, confdir
);
856 /* update package.path */
857 snprintf(path
, sizeof(path
), "package.path = package.path .. \";%s/awesome/?.lua\"", confdir
);
858 luaA_dostring(globalconf
.L
, path
);
862 confdir
= getenv("HOME");
863 len
= a_strlen(confdir
) + sizeof(AWESOME_CONFIG_FILE
)-1 + sizeof(XDG_CONFIG_HOME_DEFAULT
);
864 confpath
= p_new(char, len
);
865 a_strcpy(confpath
, len
, confdir
);
866 a_strcat(confpath
, len
, XDG_CONFIG_HOME_DEFAULT
);
867 /* update package.path */
868 snprintf(path
, sizeof(path
), "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT
"/awesome/?.lua\"", confdir
);
869 luaA_dostring(globalconf
.L
, path
);
871 a_strcat(confpath
, len
, AWESOME_CONFIG_FILE
);
874 ret
= luaA_loadrc(confpath
, run
);
876 xdg_config_dirs
= getenv("XDG_CONFIG_DIRS");
878 if(!(len
= a_strlen(xdg_config_dirs
)))
880 xdg_config_dirs
= XDG_CONFIG_DIR
;
881 len
= sizeof(XDG_CONFIG_DIR
) - 1;
884 xdg_files
= a_strsplit(xdg_config_dirs
, len
, ':');
886 for(buf
= xdg_files
; *buf
; buf
++)
889 len
= a_strlen(*buf
) + sizeof("AWESOME_CONFIG_FILE");
890 confpath
= p_new(char, len
);
891 a_strcpy(confpath
, len
, *buf
);
892 a_strcat(confpath
, len
, AWESOME_CONFIG_FILE
);
893 snprintf(path
, sizeof(path
), "package.path = package.path .. \";%s/awesome/?.lua\"", *buf
);
894 luaA_dostring(globalconf
.L
, path
);
896 ret
= luaA_loadrc(confpath
, run
);
899 for(buf
= xdg_files
; *buf
; buf
++)
901 p_delete(&xdg_files
);
903 /* Assure there's at least one tag */
904 for(screen
= 0; screen
< globalconf
.nscreen
; screen
++)
905 if(!globalconf
.screens
[screen
].tags
.len
)
906 tag_append_to_screen(tag_new("default", sizeof("default")-1, layout_tile
, 0.5, 1, 0),
907 &globalconf
.screens
[screen
]);
915 * \param cmd The buffer to parse.
916 * \return the number of elements pushed on the stack by the last statement in cmd.
917 * If there's an error, the message is pushed onto the stack and this returns 1.
920 luaA_docmd(const char *cmd
)
923 int newtop
, oldtop
= lua_gettop(globalconf
.L
);
925 while((p
= strchr(cmd
, '\n')))
927 newtop
= lua_gettop(globalconf
.L
);
928 lua_pop(globalconf
.L
, newtop
- oldtop
);
932 if (luaL_dostring(globalconf
.L
, cmd
))
934 warn("error executing Lua code: %s", lua_tostring(globalconf
.L
, -1));
939 return lua_gettop(globalconf
.L
) - oldtop
;
942 /** Pushes a Lua array containing the top n elements of the stack.
943 * \param n The number of elements to put in the array.
949 lua_createtable(globalconf
.L
, n
, 0);
950 lua_insert(globalconf
.L
, -n
- 1);
952 for (i
= n
; i
> 0; i
--)
953 lua_rawseti(globalconf
.L
, -i
- 1, i
);
956 /** Maps the top n elements of the stack to the result of
957 * applying a function to that element.
958 * \param n The number of elements to map.
960 * \lparam The function to map the elements by. This should be
961 * at position -(n + 1).
967 for (i
= 0; i
< n
; i
++)
969 lua_pushvalue(globalconf
.L
, -n
- 1); /* copy of the function */
970 lua_pushvalue(globalconf
.L
, -n
- 1); /* value to map */
971 lua_pcall(globalconf
.L
, 1, 1, 0); /* call function */
972 lua_remove(globalconf
.L
, -n
- 1); /* remove old value */
974 lua_remove(globalconf
.L
, -n
- 1); /* remove function */
977 static void luaA_conn_cleanup(EV_P_ ev_io
*w
)
979 ev_ref(EV_DEFAULT_UC
);
980 ev_io_stop(EV_DEFAULT_UC_ w
);
982 warn("error closing UNIX domain socket: %s", strerror(errno
));
987 luaA_cb(EV_P_ ev_io
*w
, int revents
)
994 switch(r
= recv(w
->fd
, buf
, sizeof(buf
)-1, MSG_TRUNC
))
997 warn("error reading UNIX domain socket: %s", strerror(errno
));
998 case 0: /* 0 bytes are only transferred when the connection is closed */
999 luaA_conn_cleanup(EV_DEFAULT_UC_ w
);
1002 if(r
>= ssizeof(buf
))
1005 lua_getglobal(globalconf
.L
, "table");
1006 lua_getfield(globalconf
.L
, -1, "concat");
1007 lua_remove(globalconf
.L
, -2); /* remove table */
1009 lua_getglobal(globalconf
.L
, "tostring");
1010 els
= luaA_docmd(buf
);
1011 luaA_map(els
); /* map results to strings */
1012 luaA_array(els
); /* put strings in an array */
1014 lua_pushstring(globalconf
.L
, "\t");
1015 lua_pcall(globalconf
.L
, 2, 1, 0); /* concatenate results with tabs */
1017 s
= lua_tolstring(globalconf
.L
, -1, &len
);
1019 /* ignore ENOENT because the client may not read */
1020 if (send(w
->fd
, s
, len
, MSG_DONTWAIT
) == -1)
1027 warn("can't send back to client via domain socket: %s", strerror(errno
));
1031 lua_pop(globalconf
.L
, 1); /* pop the string */
1033 awesome_refresh(globalconf
.connection
);
1038 luaA_conn_cb(EV_P_ ev_io
*w
, int revents
)
1040 ev_io
*csio_conn
= p_new(ev_io
, 1);
1041 int csfd
= accept(w
->fd
, NULL
, NULL
);
1043 ev_io_init(csio_conn
, &luaA_cb
, csfd
, EV_READ
);
1044 ev_io_start(EV_DEFAULT_UC_ csio_conn
);
1045 ev_unref(EV_DEFAULT_UC
);
1051 int csfd
= socket_getclient();
1053 if (csfd
< 0 || fcntl(csfd
, F_SETFD
, FD_CLOEXEC
) == -1)
1056 addr
= socket_getaddr(getenv("DISPLAY"));
1058 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
1060 if(errno
== EADDRINUSE
)
1062 if(unlink(addr
->sun_path
))
1063 warn("error unlinking existing file: %s", strerror(errno
));
1064 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
1065 return warn("error binding UNIX domain socket: %s", strerror(errno
));
1068 return warn("error binding UNIX domain socket: %s", strerror(errno
));
1072 ev_io_init(&csio
, &luaA_conn_cb
, csfd
, EV_READ
);
1073 ev_io_start(EV_DEFAULT_UC_
&csio
);
1074 ev_unref(EV_DEFAULT_UC
);
1078 luaA_cs_cleanup(void)
1082 ev_ref(EV_DEFAULT_UC
);
1083 ev_io_stop(EV_DEFAULT_UC_
&csio
);
1085 warn("error closing UNIX domain socket: %s", strerror(errno
));
1086 if(unlink(addr
->sun_path
))
1087 warn("error unlinking UNIX domain socket: %s", strerror(errno
));
1093 luaA_on_timer(EV_P_ ev_timer
*w
, int revents
)
1095 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.timer
, 0, 0);
1096 awesome_refresh(globalconf
.connection
);
1099 /** Push a color as a string onto the stack
1100 * \param L The Lua VM state.
1101 * \param c The color to push.
1102 * \return The number of elements pushed on stack.
1105 luaA_pushcolor(lua_State
*L
, const xcolor_t
*c
)
1107 uint8_t r
= (unsigned)c
->red
* 0xff / 0xffff;
1108 uint8_t g
= (unsigned)c
->green
* 0xff / 0xffff;
1109 uint8_t b
= (unsigned)c
->blue
* 0xff / 0xffff;
1110 uint8_t a
= (unsigned)c
->alpha
* 0xff / 0xffff;
1113 /* do not print alpha if it's full */
1115 len
= snprintf(s
, sizeof(s
), "#%02x%02x%02x", r
, g
, b
);
1117 len
= snprintf(s
, sizeof(s
), "#%02x%02x%02x%02x", r
, g
, b
, a
);
1118 lua_pushlstring(L
, s
, len
);