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"
25 #include <sys/socket.h>
28 #include <sys/types.h>
42 #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_tag_methods
[];
70 extern const struct luaL_reg awesome_tag_meta
[];
71 extern const struct luaL_reg awesome_widget_methods
[];
72 extern const struct luaL_reg awesome_widget_meta
[];
73 extern const struct luaL_reg awesome_wibox_methods
[];
74 extern const struct luaL_reg awesome_wibox_meta
[];
75 extern const struct luaL_reg awesome_keybinding_methods
[];
76 extern const struct luaL_reg awesome_keybinding_meta
[];
78 static struct sockaddr_un
*addr
;
79 static ev_io csio
= { .fd
= -1 };
81 #define XDG_CONFIG_HOME_DEFAULT "/.config"
83 /** Get or set global mouse bindings.
84 * This binding will be available when you'll click on root window.
85 * \param L The Lua VM state.
86 * \return The number of element pushed on stack.
89 * \lparam An array of mouse button bindings objects, or nothing.
90 * \return The array of mouse button bindings objects of this client.
93 luaA_buttons(lua_State
*L
)
95 button_array_t
*buttons
= &globalconf
.buttons
;
97 if(lua_gettop(L
) == 1)
98 luaA_button_array_set(L
, 1, buttons
);
100 return luaA_button_array_get(L
, buttons
);
104 * \param L The Lua VM state.
105 * \return The number of elements pushed on stack.
108 luaA_quit(lua_State
*L
__attribute__ ((unused
)))
110 ev_unloop(globalconf
.loop
, 1);
114 /** Execute another application, probably a window manager, to replace
116 * \param L The Lua VM state.
117 * \return The number of elements pushed on stack.
119 * \lparam The command line to execute.
122 luaA_exec(lua_State
*L
)
124 const char *cmd
= luaL_checkstring(L
, 1);
135 luaA_restart(lua_State
*L
__attribute__ ((unused
)))
142 luaA_openlib(lua_State
*L
, const char *name
,
143 const struct luaL_reg methods
[],
144 const struct luaL_reg meta
[])
146 luaL_newmetatable(L
, name
); /* 1 */
147 lua_pushvalue(L
, -1); /* dup metatable 2 */
148 lua_setfield(L
, -2, "__index"); /* metatable.__index = metatable 1 */
150 luaL_register(L
, NULL
, meta
); /* 1 */
151 luaL_register(L
, name
, methods
); /* 2 */
152 lua_pushvalue(L
, -1); /* dup self as metatable 3 */
153 lua_setmetatable(L
, -2); /* set self as metatable 2 */
157 /** UTF-8 aware string length computing.
158 * \param L The Lua VM state.
159 * \return The number of elements pushed on stack.
162 luaA_mbstrlen(lua_State
*L
)
164 const char *cmd
= luaL_checkstring(L
, 1);
165 lua_pushnumber(L
, mbstowcs(NULL
, NONULL(cmd
), 0));
169 /** Overload standard Lua next function to use __next key on metatable.
170 * \param L The Lua VM state.
171 * \param The number of elements pushed on stack.
174 luaAe_next(lua_State
*L
)
176 if(luaL_getmetafield(L
, 1, "__next"))
179 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
180 return lua_gettop(L
);
183 luaL_checktype(L
, 1, LUA_TTABLE
);
191 /** Overload lua_next() function by using __next metatable field
192 * to get next elements.
193 * \param L The Lua VM stack.
194 * \param idx The index number of elements in stack.
195 * \return 1 if more elements to come, 0 otherwise.
198 luaA_next(lua_State
*L
, int idx
)
200 if(luaL_getmetafield(L
, idx
, "__next"))
202 /* if idx is relative, reduce it since we got __next */
204 /* copy table and then move key */
205 lua_pushvalue(L
, idx
);
206 lua_pushvalue(L
, -3);
208 lua_pcall(L
, 2, 2, 0);
209 /* next returned nil, it's the end */
218 else if(lua_istable(L
, idx
))
219 return lua_next(L
, idx
);
225 /** Generic pairs function.
226 * \param L The Lua VM state.
227 * \return The number of elements pushed on stack.
230 luaA_generic_pairs(lua_State
*L
)
232 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
233 lua_pushvalue(L
, 1); /* state, */
234 lua_pushnil(L
); /* and initial value */
238 /** Overload standard pairs function to use __pairs field of metatables.
239 * \param L The Lua VM state.
240 * \return The number of elements pushed on stack.
243 luaAe_pairs(lua_State
*L
)
245 if(luaL_getmetafield(L
, 1, "__pairs"))
248 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
249 return lua_gettop(L
);
252 luaL_checktype(L
, 1, LUA_TTABLE
);
253 return luaA_generic_pairs(L
);
257 luaA_ipairs_aux(lua_State
*L
)
259 int i
= luaL_checkint(L
, 2) + 1;
260 luaL_checktype(L
, 1, LUA_TTABLE
);
261 lua_pushinteger(L
, i
);
262 lua_rawgeti(L
, 1, i
);
263 return (lua_isnil(L
, -1)) ? 0 : 2;
266 /** Overload standard ipairs function to use __ipairs field of metatables.
267 * \param L The Lua VM state.
268 * \return The number of elements pushed on stack.
271 luaAe_ipairs(lua_State
*L
)
273 if(luaL_getmetafield(L
, 1, "__ipairs"))
276 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
277 return lua_gettop(L
);
280 luaL_checktype(L
, 1, LUA_TTABLE
);
281 lua_pushvalue(L
, lua_upvalueindex(1));
283 lua_pushinteger(L
, 0); /* and initial value */
287 /** Replace various standards Lua functions with our own.
288 * \param L The Lua VM state.
291 luaA_fixups(lua_State
*L
)
293 /* replace string.len */
294 lua_getglobal(L
, "string");
295 lua_pushcfunction(L
, luaA_mbstrlen
);
296 lua_setfield(L
, -2, "len");
299 lua_pushliteral(L
, "next");
300 lua_pushcfunction(L
, luaAe_next
);
301 lua_settable(L
, LUA_GLOBALSINDEX
);
303 lua_pushliteral(L
, "pairs");
304 lua_pushcfunction(L
, luaAe_next
);
305 lua_pushcclosure(L
, luaAe_pairs
, 1); /* pairs get next as upvalue */
306 lua_settable(L
, LUA_GLOBALSINDEX
);
308 lua_pushliteral(L
, "ipairs");
309 lua_pushcfunction(L
, luaA_ipairs_aux
);
310 lua_pushcclosure(L
, luaAe_ipairs
, 1);
311 lua_settable(L
, LUA_GLOBALSINDEX
);
314 /** __next function for wtable objects.
315 * \param L The Lua VM state.
316 * \return The number of elements pushed on stack.
319 luaA_wtable_next(lua_State
*L
)
321 /* upvalue 1 is content table */
322 if(lua_next(L
, lua_upvalueindex(1)))
328 /** __ipairs function for wtable objects.
329 * \param L The Lua VM state.
330 * \return The number of elements pushed on stack.
333 luaA_wtable_ipairs(lua_State
*L
)
335 /* push ipairs_aux */
336 lua_pushvalue(L
, lua_upvalueindex(2));
337 /* push content table */
338 lua_pushvalue(L
, lua_upvalueindex(1));
339 lua_pushinteger(L
, 0); /* and initial value */
343 /** Index function of wtable objects.
344 * \param L The Lua VM state.
345 * \return The number of elements pushed on stack.
348 luaA_wtable_index(lua_State
*L
)
354 /* check for size, waiting lua 5.2 and __len on tables */
355 if((buf
= lua_tolstring(L
, -1, &len
)))
356 if(a_tokenize(buf
, len
) == A_TK_LEN
)
358 lua_pushnumber(L
, lua_objlen(L
, lua_upvalueindex(1)));
363 /* upvalue 1 is content table */
364 lua_rawget(L
, lua_upvalueindex(1));
368 /** Newndex function of wtable objects.
369 * \param L The Lua VM state.
370 * \return The number of elements pushed on stack.
373 luaA_wtable_newindex(lua_State
*L
)
375 bool invalid
= false;
377 /* push key on top */
379 /* get current key value in content table */
380 lua_rawget(L
, lua_upvalueindex(1));
381 /* if value is a widget, notify change */
382 if(lua_istable(L
, -1) || luaA_toudata(L
, -1, "widget"))
385 lua_pop(L
, 1); /* remove value */
387 /* if new value is a widget or a table */
388 if(lua_istable(L
, 3))
390 luaA_table2wtable(L
);
393 else if(!invalid
&& luaA_toudata(L
, 3, "widget"))
396 /* upvalue 1 is content table */
397 lua_rawset(L
, lua_upvalueindex(1));
400 luaA_wibox_invalidate_byitem(L
, lua_topointer(L
, 1));
405 /** Convert the top element of the stack to a proxied wtable.
406 * \param L The Lua VM state.
409 luaA_table2wtable(lua_State
*L
)
411 if(!lua_istable(L
, -1))
414 lua_newtable(L
); /* create *real* content table */
415 lua_newtable(L
); /* metatable */
416 lua_pushvalue(L
, -2); /* copy content table */
417 lua_pushcfunction(L
, luaA_ipairs_aux
); /* push ipairs aux */
418 lua_pushcclosure(L
, luaA_wtable_ipairs
, 2);
419 lua_pushvalue(L
, -3); /* copy content table */
420 lua_pushcclosure(L
, luaA_wtable_next
, 1); /* __next has the content table as upvalue */
421 lua_pushvalue(L
, -4); /* copy content table */
422 lua_pushcclosure(L
, luaA_wtable_index
, 1); /* __index has the content table as upvalue */
423 lua_pushvalue(L
, -5); /* copy content table */
424 lua_pushcclosure(L
, luaA_wtable_newindex
, 1); /* __newindex has the content table as upvalue */
425 /* set metatable field with just pushed closure */
426 lua_setfield(L
, -5, "__newindex");
427 lua_setfield(L
, -4, "__index");
428 lua_setfield(L
, -3, "__next");
429 lua_setfield(L
, -2, "__ipairs");
430 /* set metatable impossible to touch */
431 lua_pushliteral(L
, "wtable");
432 lua_setfield(L
, -2, "__metatable");
433 /* set new metatable on original table */
434 lua_setmetatable(L
, -3);
438 /* go through original table */
439 while(lua_next(L
, -3))
441 /* if convert value to wtable */
442 luaA_table2wtable(L
);
444 lua_pushvalue(L
, -2);
445 /* move key before value */
447 /* set same value in content table */
450 lua_pushvalue(L
, -1);
451 /* push the new value :-> */
453 /* set orig[k] = nil */
456 /* remove content table */
460 /** Look for an item: table, function, etc.
461 * \param L The Lua VM state.
462 * \param item The pointer item.
465 luaA_hasitem(lua_State
*L
, const void *item
)
468 while(luaA_next(L
, -2))
470 if(lua_topointer(L
, -1) == item
)
472 /* remove value and key */
476 if(lua_istable(L
, -1))
477 if(luaA_hasitem(L
, item
))
479 /* remove key and value */
489 /** Check if a table is a loop. When using table as direct acyclic digram,
491 * \param L The Lua VM state.
492 * \param idx The index of the table in the stack
493 * \return True if the table loops.
496 luaA_isloop(lua_State
*L
, int idx
)
498 if(lua_istable(L
, idx
))
500 lua_pushvalue(L
, idx
); /* push table on top */
501 if(luaA_hasitem(L
, lua_topointer(L
, -1)))
503 lua_pop(L
, 1); /* remove pushed table */
507 while(luaA_next(L
, -2))
509 /* check for recursivity */
510 if(luaA_isloop(L
, -1))
512 lua_pop(L
, 2); /* remove key and value */
515 lua_pop(L
, 1); /* remove value */
517 lua_pop(L
, 1); /* remove pushed table */
523 * This table can use safely object as key.
524 * \param L The Lua VM state.
525 * \return The number of elements pushed on stack.
528 luaA_otable_index(lua_State
*L
)
532 if((obj
= lua_touserdata(L
, 2)))
536 while(lua_next(L
, 1))
538 /* check the key against the key if the key is a userdata,
539 * otherwise check it again the value. */
540 if((v
= lua_touserdata(L
, -2))
544 else if((v
= lua_touserdata(L
, -1))
551 /* removes 'value'; keeps 'key' for next iteration */
562 * This table can use safely object as key.
563 * \param L The Lua VM state.
564 * \return The number of elements pushed on stack.
567 luaA_otable_newindex(lua_State
*L
)
571 if((obj
= lua_touserdata(L
, 2)))
575 while(lua_next(L
, 1))
577 if((v
= lua_touserdata(L
, -2))
582 /* push new value on top */
584 /* set in table key = value */
588 /* removes 'value'; keeps 'key' for next iteration */
599 * This function is multi-head (Zaphod) aware and will set display to
600 * the right screen according to mouse position.
601 * \param L The Lua VM state.
602 * \return The number of elements pushed on stack
604 * \lparam The command to launch.
605 * \lparam The optional screen number to spawn the command on.
608 luaA_spawn(lua_State
*L
)
610 char *host
, newdisplay
[128];
612 int screen
= 0, screenp
, displayp
;
614 if(lua_gettop(L
) == 2)
616 screen
= luaL_checknumber(L
, 2) - 1;
617 luaA_checkscreen(screen
);
620 cmd
= luaL_checkstring(L
, 1);
622 if(!globalconf
.xinerama_is_active
)
624 xcb_parse_display(NULL
, &host
, &displayp
, &screenp
);
625 snprintf(newdisplay
, sizeof(newdisplay
), "%s:%d.%d", host
, displayp
, screen
);
626 setenv("DISPLAY", newdisplay
, 1);
630 /* The double-fork construct avoids zombie processes and keeps the code
631 * clean from stupid signal handlers. */
636 if(globalconf
.connection
)
637 xcb_disconnect(globalconf
.connection
);
640 warn("execl '%s' failed: %s\n", cmd
, strerror(errno
));
649 /** awesome global table.
650 * \param L The Lua VM state.
651 * \return The number of elements pushed on stack.
653 * \lfield font The default font.
654 * \lfield conffile The configuration file which has been loaded.
657 luaA_awesome_index(lua_State
*L
)
659 if(luaA_usemetatable(L
, 1, 2))
663 const char *buf
= luaL_checklstring(L
, 2, &len
);
665 switch(a_tokenize(buf
, len
))
669 char *font
= pango_font_description_to_string(globalconf
.font
->desc
);
670 lua_pushstring(L
, font
);
675 lua_pushstring(L
, globalconf
.conffile
);
678 luaA_pushcolor(L
, &globalconf
.colors
.fg
);
681 luaA_pushcolor(L
, &globalconf
.colors
.bg
);
690 /** Newindex function for the awesome global table.
691 * \param L The Lua VM state.
692 * \return The number of elements pushed on stack.
695 luaA_awesome_newindex(lua_State
*L
)
697 if(luaA_usemetatable(L
, 1, 2))
701 const char *buf
= luaL_checklstring(L
, 2, &len
);
703 switch(a_tokenize(buf
, len
))
707 const char *newfont
= luaL_checkstring(L
, 3);
708 draw_font_delete(&globalconf
.font
);
709 globalconf
.font
= draw_font_new(newfont
);
713 if((buf
= luaL_checklstring(L
, 3, &len
)))
714 xcolor_init_reply(xcolor_init_unchecked(&globalconf
.colors
.fg
, buf
, len
));
717 if((buf
= luaL_checklstring(L
, 3, &len
)))
718 xcolor_init_reply(xcolor_init_unchecked(&globalconf
.colors
.bg
, buf
, len
));
727 /** Initialize the Lua VM
734 static const struct luaL_reg otable_methods
[] =
736 { "__call", luaA_otable_new
},
739 static const struct luaL_reg otable_meta
[] =
741 { "__index", luaA_otable_index
},
742 { "__newindex", luaA_otable_newindex
},
745 static const struct luaL_reg awesome_lib
[] =
747 { "quit", luaA_quit
},
748 { "exec", luaA_exec
},
749 { "spawn", luaA_spawn
},
750 { "restart", luaA_restart
},
751 { "buttons", luaA_buttons
},
752 { "__index", luaA_awesome_index
},
753 { "__newindex", luaA_awesome_newindex
},
757 L
= globalconf
.L
= luaL_newstate();
763 /* Export awesome lib */
764 luaA_openlib(L
, "awesome", awesome_lib
, awesome_lib
);
766 /* Export hooks lib */
767 luaL_register(L
, "hooks", awesome_hooks_lib
);
769 /* Export keygrabber lib */
770 luaL_register(L
, "keygrabber", awesome_keygrabber_lib
);
772 /* Export otable lib */
773 luaA_openlib(L
, "otable", otable_methods
, otable_meta
);
776 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
779 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
782 luaA_openlib(L
, "button", awesome_button_methods
, awesome_button_meta
);
785 luaA_openlib(L
, "image", awesome_image_methods
, awesome_image_meta
);
788 luaA_openlib(L
, "tag", awesome_tag_methods
, awesome_tag_meta
);
791 luaA_openlib(L
, "wibox", awesome_wibox_methods
, awesome_wibox_meta
);
794 luaA_openlib(L
, "widget", awesome_widget_methods
, awesome_widget_meta
);
797 luaA_openlib(L
, "client", awesome_client_methods
, awesome_client_meta
);
800 luaA_openlib(L
, "keybinding", awesome_keybinding_methods
, awesome_keybinding_meta
);
802 lua_pushliteral(L
, "AWESOME_VERSION");
803 lua_pushstring(L
, AWESOME_VERSION
);
804 lua_settable(L
, LUA_GLOBALSINDEX
);
806 lua_pushliteral(L
, "AWESOME_RELEASE");
807 lua_pushstring(L
, AWESOME_RELEASE
);
808 lua_settable(L
, LUA_GLOBALSINDEX
);
811 globalconf
.hooks
.manage
= LUA_REFNIL
;
812 globalconf
.hooks
.unmanage
= LUA_REFNIL
;
813 globalconf
.hooks
.focus
= LUA_REFNIL
;
814 globalconf
.hooks
.unfocus
= LUA_REFNIL
;
815 globalconf
.hooks
.mouse_enter
= LUA_REFNIL
;
816 globalconf
.hooks
.arrange
= LUA_REFNIL
;
817 globalconf
.hooks
.clients
= LUA_REFNIL
;
818 globalconf
.hooks
.tags
= LUA_REFNIL
;
819 globalconf
.hooks
.tagged
= LUA_REFNIL
;
820 globalconf
.hooks
.property
= LUA_REFNIL
;
821 globalconf
.hooks
.timer
= LUA_REFNIL
;
823 /* add Lua lib path (/usr/share/awesome/lib by default) */
824 luaA_dostring(L
, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH
"/?.lua\"");
825 luaA_dostring(L
, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH
"/?/init.lua\"");
827 /* add XDG_CONFIG_DIR (/etc/xdg/awesome by default) as include path */
828 luaA_dostring(L
, "package.path = package.path .. \";" XDG_CONFIG_DIR
"/awesome/?.lua\"");
829 luaA_dostring(L
, "package.path = package.path .. \";" XDG_CONFIG_DIR
"/awesome/?/init.lua\"");
831 /* add either XDG_CONFIG_HOME/awesome or HOME/.config/awesome to path */
833 if((dir
= getenv("XDG_CONFIG_HOME")))
836 a_asprintf(&path
, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"", dir
, dir
);
837 luaA_dostring(globalconf
.L
, path
);
842 char *path
, *homedir
= getenv("HOME");
844 "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT
"/awesome/?.lua;%s" XDG_CONFIG_HOME_DEFAULT
"/awesome/?/init.lua\"",
846 luaA_dostring(globalconf
.L
, path
);
850 /* add XDG_CONFIG_DIRS to include paths */
851 char *xdg_config_dirs
= getenv("XDG_CONFIG_DIRS");
854 if((len
= a_strlen(xdg_config_dirs
)))
856 char **buf
, **xdg_files
= a_strsplit(xdg_config_dirs
, len
, ':');
858 for(buf
= xdg_files
; *buf
; buf
++)
861 a_asprintf(&confpath
, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"",
863 luaA_dostring(globalconf
.L
, confpath
);
867 for(buf
= xdg_files
; *buf
; buf
++)
869 p_delete(&xdg_files
);
873 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
876 luaA_loadrc(const char *confpath
, bool run
)
880 if(!luaL_loadfile(globalconf
.L
, confpath
))
884 if(lua_pcall(globalconf
.L
, 0, LUA_MULTRET
, 0))
885 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
888 globalconf
.conffile
= a_strdup(confpath
);
893 lua_pop(globalconf
.L
, 1);
897 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
902 /** Load a configuration file.
903 * \param confpatharg The configuration file to load.
904 * \param run Run the configuration file.
907 luaA_parserc(const char *confpatharg
, bool run
)
910 const char *confdir
, *xdg_config_dirs
;
911 char *confpath
= NULL
, **xdg_files
= NULL
, **buf
;
915 /* try to load, return if it's ok */
916 if(luaA_loadrc(confpatharg
, run
))
922 if((confdir
= getenv("XDG_CONFIG_HOME")))
923 a_asprintf(&confpath
, "%s" AWESOME_CONFIG_FILE
, confdir
);
925 a_asprintf(&confpath
, "%s" XDG_CONFIG_HOME_DEFAULT AWESOME_CONFIG_FILE
, getenv("HOME"));
927 /* try to run XDG_CONFIG_HOME/awesome/rc.lua */
928 if(luaA_loadrc(confpath
, run
))
936 xdg_config_dirs
= getenv("XDG_CONFIG_DIRS");
938 if(!(len
= a_strlen(xdg_config_dirs
)))
940 xdg_config_dirs
= XDG_CONFIG_DIR
;
941 len
= sizeof(XDG_CONFIG_DIR
) - 1;
944 xdg_files
= a_strsplit(xdg_config_dirs
, len
, ':');
946 for(buf
= xdg_files
; *buf
&& !ret
; buf
++)
948 a_asprintf(&confpath
, "%s" AWESOME_CONFIG_FILE
, *buf
);
949 if(luaA_loadrc(confpath
, run
))
963 for(buf
= xdg_files
; *buf
; buf
++)
965 p_delete(&xdg_files
);
968 /* Assure there's at least one tag */
969 for(screen
= 0; screen
< globalconf
.nscreen
; screen
++)
970 if(!globalconf
.screens
[screen
].tags
.len
)
971 tag_append_to_screen(tag_new("default", sizeof("default") - 1, layout_tile
, 0.5, 1, 0),
972 &globalconf
.screens
[screen
]);
977 * \param cmd The buffer to parse.
978 * \return the number of elements pushed on the stack by the last statement in cmd.
979 * If there's an error, the message is pushed onto the stack and this returns 1.
982 luaA_docmd(const char *cmd
)
985 int newtop
, oldtop
= lua_gettop(globalconf
.L
);
987 while((p
= strchr(cmd
, '\n')))
989 newtop
= lua_gettop(globalconf
.L
);
990 lua_pop(globalconf
.L
, newtop
- oldtop
);
994 if (luaL_dostring(globalconf
.L
, cmd
))
996 warn("error executing Lua code: %s", lua_tostring(globalconf
.L
, -1));
1001 return lua_gettop(globalconf
.L
) - oldtop
;
1004 /** Pushes a Lua array containing the top n elements of the stack.
1005 * \param n The number of elements to put in the array.
1011 lua_createtable(globalconf
.L
, n
, 0);
1012 lua_insert(globalconf
.L
, -n
- 1);
1014 for (i
= n
; i
> 0; i
--)
1015 lua_rawseti(globalconf
.L
, -i
- 1, i
);
1018 /** Maps the top n elements of the stack to the result of
1019 * applying a function to that element.
1020 * \param n The number of elements to map.
1022 * \lparam The function to map the elements by. This should be
1023 * at position -(n + 1).
1029 for (i
= 0; i
< n
; i
++)
1031 lua_pushvalue(globalconf
.L
, -n
- 1); /* copy of the function */
1032 lua_pushvalue(globalconf
.L
, -n
- 1); /* value to map */
1033 lua_pcall(globalconf
.L
, 1, 1, 0); /* call function */
1034 lua_remove(globalconf
.L
, -n
- 1); /* remove old value */
1036 lua_remove(globalconf
.L
, -n
- 1); /* remove function */
1039 static void luaA_conn_cleanup(EV_P_ ev_io
*w
)
1041 ev_ref(EV_DEFAULT_UC
);
1042 ev_io_stop(EV_DEFAULT_UC_ w
);
1044 warn("error closing UNIX domain socket: %s", strerror(errno
));
1049 luaA_cb(EV_P_ ev_io
*w
, int revents
)
1056 switch(r
= recv(w
->fd
, buf
, sizeof(buf
)-1, MSG_TRUNC
))
1059 warn("error reading UNIX domain socket: %s", strerror(errno
));
1060 case 0: /* 0 bytes are only transferred when the connection is closed */
1061 luaA_conn_cleanup(EV_DEFAULT_UC_ w
);
1064 if(r
>= ssizeof(buf
))
1067 lua_getglobal(globalconf
.L
, "table");
1068 lua_getfield(globalconf
.L
, -1, "concat");
1069 lua_remove(globalconf
.L
, -2); /* remove table */
1071 lua_getglobal(globalconf
.L
, "tostring");
1072 els
= luaA_docmd(buf
);
1073 luaA_map(els
); /* map results to strings */
1074 luaA_array(els
); /* put strings in an array */
1076 lua_pushstring(globalconf
.L
, "\t");
1077 lua_pcall(globalconf
.L
, 2, 1, 0); /* concatenate results with tabs */
1079 s
= lua_tolstring(globalconf
.L
, -1, &len
);
1081 /* ignore ENOENT because the client may not read */
1082 if (send(w
->fd
, s
, len
, MSG_DONTWAIT
) == -1)
1089 warn("can't send back to client via domain socket: %s", strerror(errno
));
1093 lua_pop(globalconf
.L
, 1); /* pop the string */
1095 awesome_refresh(globalconf
.connection
);
1100 luaA_conn_cb(EV_P_ ev_io
*w
, int revents
)
1102 ev_io
*csio_conn
= p_new(ev_io
, 1);
1103 int csfd
= accept(w
->fd
, NULL
, NULL
);
1105 ev_io_init(csio_conn
, &luaA_cb
, csfd
, EV_READ
);
1106 ev_io_start(EV_DEFAULT_UC_ csio_conn
);
1107 ev_unref(EV_DEFAULT_UC
);
1113 int csfd
= socket_getclient();
1115 if (csfd
< 0 || fcntl(csfd
, F_SETFD
, FD_CLOEXEC
) == -1)
1118 addr
= socket_getaddr(getenv("DISPLAY"));
1120 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
1122 if(errno
== EADDRINUSE
)
1124 if(unlink(addr
->sun_path
))
1125 warn("error unlinking existing file: %s", strerror(errno
));
1126 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
1127 return warn("error binding UNIX domain socket: %s", strerror(errno
));
1130 return warn("error binding UNIX domain socket: %s", strerror(errno
));
1134 ev_io_init(&csio
, &luaA_conn_cb
, csfd
, EV_READ
);
1135 ev_io_start(EV_DEFAULT_UC_
&csio
);
1136 ev_unref(EV_DEFAULT_UC
);
1140 luaA_cs_cleanup(void)
1144 ev_ref(EV_DEFAULT_UC
);
1145 ev_io_stop(EV_DEFAULT_UC_
&csio
);
1147 warn("error closing UNIX domain socket: %s", strerror(errno
));
1148 if(unlink(addr
->sun_path
))
1149 warn("error unlinking UNIX domain socket: %s", strerror(errno
));
1155 luaA_on_timer(EV_P_ ev_timer
*w
, int revents
)
1157 if(globalconf
.hooks
.timer
!= LUA_REFNIL
)
1158 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.timer
, 0, 0);
1159 awesome_refresh(globalconf
.connection
);
1162 /** Push a color as a string onto the stack
1163 * \param L The Lua VM state.
1164 * \param c The color to push.
1165 * \return The number of elements pushed on stack.
1168 luaA_pushcolor(lua_State
*L
, const xcolor_t
*c
)
1170 uint8_t r
= (unsigned)c
->red
* 0xff / 0xffff;
1171 uint8_t g
= (unsigned)c
->green
* 0xff / 0xffff;
1172 uint8_t b
= (unsigned)c
->blue
* 0xff / 0xffff;
1173 uint8_t a
= (unsigned)c
->alpha
* 0xff / 0xffff;
1176 /* do not print alpha if it's full */
1178 len
= snprintf(s
, sizeof(s
), "#%02x%02x%02x", r
, g
, b
);
1180 len
= snprintf(s
, sizeof(s
), "#%02x%02x%02x%02x", r
, g
, b
, a
);
1181 lua_pushlstring(L
, s
, len
);