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"
52 #include "layouts/tile.h"
53 #include "common/socket.h"
54 #include "common/buffer.h"
56 extern awesome_t globalconf
;
58 extern const struct luaL_reg awesome_hooks_lib
[];
59 extern const struct luaL_reg awesome_keygrabber_lib
[];
60 extern const struct luaL_reg awesome_button_methods
[];
61 extern const struct luaL_reg awesome_button_meta
[];
62 extern const struct luaL_reg awesome_image_methods
[];
63 extern const struct luaL_reg awesome_image_meta
[];
64 extern const struct luaL_reg awesome_mouse_methods
[];
65 extern const struct luaL_reg awesome_mouse_meta
[];
66 extern const struct luaL_reg awesome_screen_methods
[];
67 extern const struct luaL_reg awesome_screen_meta
[];
68 extern const struct luaL_reg awesome_client_methods
[];
69 extern const struct luaL_reg awesome_client_meta
[];
70 extern const struct luaL_reg awesome_titlebar_methods
[];
71 extern const struct luaL_reg awesome_titlebar_meta
[];
72 extern const struct luaL_reg awesome_tag_methods
[];
73 extern const struct luaL_reg awesome_tag_meta
[];
74 extern const struct luaL_reg awesome_widget_methods
[];
75 extern const struct luaL_reg awesome_widget_meta
[];
76 extern const struct luaL_reg awesome_statusbar_methods
[];
77 extern const struct luaL_reg awesome_statusbar_meta
[];
78 extern const struct luaL_reg awesome_wibox_methods
[];
79 extern const struct luaL_reg awesome_wibox_meta
[];
80 extern const struct luaL_reg awesome_keybinding_methods
[];
81 extern const struct luaL_reg awesome_keybinding_meta
[];
83 static struct sockaddr_un
*addr
;
84 static ev_io csio
= { .fd
= -1 };
86 #define XDG_CONFIG_HOME_DEFAULT "/.config"
88 /** Get or set global mouse bindings.
89 * This binding will be available when you'll click on root window.
90 * \param L The Lua VM state.
91 * \return The number of element pushed on stack.
94 * \lparam An array of mouse button bindings objects, or nothing.
95 * \return The array of mouse button bindings objects of this client.
98 luaA_buttons(lua_State
*L
)
100 button_array_t
*buttons
= &globalconf
.buttons
;
102 if(lua_gettop(L
) == 1)
103 luaA_button_array_set(L
, 1, buttons
);
105 return luaA_button_array_get(L
, buttons
);
109 * \param L The Lua VM state.
110 * \return The number of elements pushed on stack.
113 luaA_quit(lua_State
*L
__attribute__ ((unused
)))
115 ev_unloop(globalconf
.loop
, 1);
119 /** Execute another application, probably a window manager, to replace
121 * \param L The Lua VM state.
122 * \return The number of elements pushed on stack.
124 * \lparam The command line to execute.
127 luaA_exec(lua_State
*L
)
129 const char *cmd
= luaL_checkstring(L
, 1);
140 luaA_restart(lua_State
*L
__attribute__ ((unused
)))
146 /** Set or get default font. (DEPRECATED)
147 * \param L The Lua VM state.
148 * \return The number of elements pushed on stack.
150 * \lparam An optional string with a font name in Pango format.
151 * \lreturn The font used, in Pango format.
154 luaA_font(lua_State
*L
)
158 if(lua_gettop(L
) == 1)
160 const char *newfont
= luaL_checkstring(L
, 1);
161 draw_font_delete(&globalconf
.font
);
162 globalconf
.font
= draw_font_new(newfont
);
165 font
= pango_font_description_to_string(globalconf
.font
->desc
);
166 lua_pushstring(L
, font
);
172 /** Set default font. (DEPRECATED)
173 * \param L The Lua VM state.
174 * \return The number of elements pushed on stack.
176 * \lparam A string with a font name in Pango format.
179 luaA_font_set(lua_State
*L
)
185 /** Get configuration file path used by awesome.
186 * \param L The Lua VM state.
187 * \return The number of elements pushed on stack.
189 * \lreturn The awesome configuration file path.
192 luaA_conffile(lua_State
*L
)
194 lua_pushstring(L
, globalconf
.conffile
);
198 /** Set default colors.
199 * \param L The Lua VM state.
200 * \return The number of elements pushed on stack.
202 * \lparam A table with `fg' and `bg' elements, containing colors.
203 * \lreturn A table with `fg' and `bg' elements, containing colors.
206 luaA_colors(lua_State
*L
)
208 if(lua_gettop(L
) == 1)
212 int8_t colors_nbr
= -1, i
;
213 xcolor_init_request_t reqs
[2];
215 luaA_checktable(L
, 1);
217 if((buf
= luaA_getopt_lstring(L
, 1, "fg", NULL
, &len
)))
218 reqs
[++colors_nbr
] = xcolor_init_unchecked(&globalconf
.colors
.fg
, buf
, len
);
220 if((buf
= luaA_getopt_lstring(L
, 1, "bg", NULL
, &len
)))
221 reqs
[++colors_nbr
] = xcolor_init_unchecked(&globalconf
.colors
.bg
, buf
, len
);
223 for(i
= 0; i
<= colors_nbr
; i
++)
224 xcolor_init_reply(reqs
[i
]);
228 luaA_pushcolor(L
, &globalconf
.colors
.fg
);
229 lua_setfield(L
, -2, "fg");
230 luaA_pushcolor(L
, &globalconf
.colors
.bg
);
231 lua_setfield(L
, -2, "bg");
236 /** Set default colors. (DEPRECATED)
237 * \param L The Lua VM state.
238 * \return The number of elements pushed on stack.
241 * \lparam A table with `fg' and `bg' elements, containing colors.
244 luaA_colors_set(lua_State
*L
)
247 return luaA_colors(L
);
251 luaA_openlib(lua_State
*L
, const char *name
,
252 const struct luaL_reg methods
[],
253 const struct luaL_reg meta
[])
255 luaL_newmetatable(L
, name
); /* 1 */
256 lua_pushvalue(L
, -1); /* dup metatable 2 */
257 lua_setfield(L
, -2, "__index"); /* metatable.__index = metatable 1 */
259 luaL_register(L
, NULL
, meta
); /* 1 */
260 luaL_register(L
, name
, methods
); /* 2 */
261 lua_pushvalue(L
, -1); /* dup self as metatable 3 */
262 lua_setmetatable(L
, -2); /* set self as metatable 2 */
266 /** UTF-8 aware string length computing.
267 * \param L The Lua VM state.
268 * \return The number of elements pushed on stack.
271 luaA_mbstrlen(lua_State
*L
)
273 const char *cmd
= luaL_checkstring(L
, 1);
274 lua_pushnumber(L
, mbstowcs(NULL
, NONULL(cmd
), 0));
278 /** Overload standard Lua next function to use __next key on metatable.
279 * \param L The Lua VM state.
280 * \param The number of elements pushed on stack.
283 luaAe_next(lua_State
*L
)
285 if(luaL_getmetafield(L
, 1, "__next"))
288 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
289 return lua_gettop(L
);
292 luaL_checktype(L
, 1, LUA_TTABLE
);
300 /** Overload lua_next() function by using __next metatable field
301 * to get next elements.
302 * \param L The Lua VM stack.
303 * \param idx The index number of elements in stack.
304 * \return 1 if more elements to come, 0 otherwise.
307 luaA_next(lua_State
*L
, int idx
)
309 if(luaL_getmetafield(L
, idx
, "__next"))
311 /* if idx is relative, reduce it since we got __next */
313 /* copy table and then move key */
314 lua_pushvalue(L
, idx
);
315 lua_pushvalue(L
, -3);
317 lua_pcall(L
, 2, 2, 0);
318 /* next returned nil, it's the end */
328 return lua_next(L
, idx
);
331 /** Generic pairs function.
332 * \param L The Lua VM state.
333 * \return The number of elements pushed on stack.
336 luaA_generic_pairs(lua_State
*L
)
338 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
339 lua_pushvalue(L
, 1); /* state, */
340 lua_pushnil(L
); /* and initial value */
344 /** Overload standard pairs function to use __pairs field of metatables.
345 * \param L The Lua VM state.
346 * \return The number of elements pushed on stack.
349 luaAe_pairs(lua_State
*L
)
351 if(luaL_getmetafield(L
, 1, "__pairs"))
354 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
355 return lua_gettop(L
);
358 luaL_checktype(L
, 1, LUA_TTABLE
);
359 return luaA_generic_pairs(L
);
363 luaA_ipairs_aux(lua_State
*L
)
365 int i
= luaL_checkint(L
, 2) + 1;
366 luaL_checktype(L
, 1, LUA_TTABLE
);
367 lua_pushinteger(L
, i
);
368 lua_rawgeti(L
, 1, i
);
369 return (lua_isnil(L
, -1)) ? 0 : 2;
372 /** Overload standard ipairs function to use __ipairs field of metatables.
373 * \param L The Lua VM state.
374 * \return The number of elements pushed on stack.
377 luaAe_ipairs(lua_State
*L
)
379 if(luaL_getmetafield(L
, 1, "__ipairs"))
382 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
383 return lua_gettop(L
);
386 luaL_checktype(L
, 1, LUA_TTABLE
);
387 lua_pushvalue(L
, lua_upvalueindex(1));
389 lua_pushinteger(L
, 0); /* and initial value */
393 /** Replace various standards Lua functions with our own.
394 * \param L The Lua VM state.
397 luaA_fixups(lua_State
*L
)
399 /* replace string.len */
400 lua_getglobal(L
, "string");
401 lua_pushcfunction(L
, luaA_mbstrlen
);
402 lua_setfield(L
, -2, "len");
405 lua_pushliteral(L
, "next");
406 lua_pushcfunction(L
, luaAe_next
);
407 lua_settable(L
, LUA_GLOBALSINDEX
);
409 lua_pushliteral(L
, "pairs");
410 lua_pushcfunction(L
, luaAe_next
);
411 lua_pushcclosure(L
, luaAe_pairs
, 1); /* pairs get next as upvalue */
412 lua_settable(L
, LUA_GLOBALSINDEX
);
414 lua_pushliteral(L
, "ipairs");
415 lua_pushcfunction(L
, luaA_ipairs_aux
);
416 lua_pushcclosure(L
, luaAe_ipairs
, 1);
417 lua_settable(L
, LUA_GLOBALSINDEX
);
420 /** __next function for wtable objects.
421 * \param L The Lua VM state.
422 * \return The number of elements pushed on stack.
425 luaA_wtable_next(lua_State
*L
)
427 /* upvalue 1 is content table */
428 if(lua_next(L
, lua_upvalueindex(1)))
434 /** __ipairs function for wtable objects.
435 * \param L The Lua VM state.
436 * \return The number of elements pushed on stack.
439 luaA_wtable_ipairs(lua_State
*L
)
441 /* push ipairs_aux */
442 lua_pushvalue(L
, lua_upvalueindex(2));
443 /* push content table */
444 lua_pushvalue(L
, lua_upvalueindex(1));
445 lua_pushinteger(L
, 0); /* and initial value */
449 /** Index function of wtable objects.
450 * \param L The Lua VM state.
451 * \return The number of elements pushed on stack.
454 luaA_wtable_index(lua_State
*L
)
460 /* check for size, waiting lua 5.2 and __len on tables */
461 if((buf
= lua_tolstring(L
, -1, &len
)))
462 if(a_tokenize(buf
, len
) == A_TK_LEN
)
464 lua_pushnumber(L
, lua_objlen(L
, lua_upvalueindex(1)));
469 /* upvalue 1 is content table */
470 lua_rawget(L
, lua_upvalueindex(1));
474 /** Newndex function of wtable objects.
475 * \param L The Lua VM state.
476 * \return The number of elements pushed on stack.
479 luaA_wtable_newindex(lua_State
*L
)
481 bool invalid
= false;
483 /* push key on top */
485 /* get current key value in content table */
486 lua_rawget(L
, lua_upvalueindex(1));
487 /* if value is a widget, notify change */
488 if(lua_istable(L
, -1) || luaA_toudata(L
, -1, "widget"))
491 lua_pop(L
, 1); /* remove value */
493 /* if new value is a widget or a table */
494 if(lua_istable(L
, 3))
496 luaA_table2wtable(L
);
499 else if(!invalid
&& luaA_toudata(L
, 3, "widget"))
502 /* upvalue 1 is content table */
503 lua_rawset(L
, lua_upvalueindex(1));
506 luaA_wibox_invalidate_byitem(L
, lua_topointer(L
, 1));
511 /** Convert the top element of the stack to a proxied wtable.
512 * \param L The Lua VM state.
515 luaA_table2wtable(lua_State
*L
)
517 if(!lua_istable(L
, -1))
520 lua_newtable(L
); /* create *real* content table */
521 lua_newtable(L
); /* metatable */
522 lua_pushvalue(L
, -2); /* copy content table */
523 lua_pushcfunction(L
, luaA_ipairs_aux
); /* push ipairs aux */
524 lua_pushcclosure(L
, luaA_wtable_ipairs
, 2);
525 lua_pushvalue(L
, -3); /* copy content table */
526 lua_pushcclosure(L
, luaA_wtable_next
, 1); /* __next has the content table as upvalue */
527 lua_pushvalue(L
, -4); /* copy content table */
528 lua_pushcclosure(L
, luaA_wtable_index
, 1); /* __index has the content table as upvalue */
529 lua_pushvalue(L
, -5); /* copy content table */
530 lua_pushcclosure(L
, luaA_wtable_newindex
, 1); /* __newindex has the content table as upvalue */
531 /* set metatable field with just pushed closure */
532 lua_setfield(L
, -5, "__newindex");
533 lua_setfield(L
, -4, "__index");
534 lua_setfield(L
, -3, "__next");
535 lua_setfield(L
, -2, "__ipairs");
536 /* set metatable impossible to touch */
537 lua_pushliteral(L
, "wtable");
538 lua_setfield(L
, -2, "__metatable");
539 /* set new metatable on original table */
540 lua_setmetatable(L
, -3);
544 /* go through original table */
545 while(lua_next(L
, -3))
547 /* if convert value to wtable */
548 luaA_table2wtable(L
);
550 lua_pushvalue(L
, -2);
551 /* move key before value */
553 /* set same value in content table */
556 lua_pushvalue(L
, -1);
557 /* push the new value :-> */
559 /* set orig[k] = nil */
562 /* remove content table */
566 /** Look for an item: table, function, etc.
567 * \param L The Lua VM state.
568 * \param item The pointer item.
571 luaA_hasitem(lua_State
*L
, const void *item
)
574 while(luaA_next(L
, -2))
576 if(lua_topointer(L
, -1) == item
)
578 /* remove value and key */
582 if(lua_istable(L
, -1))
583 if(luaA_hasitem(L
, item
))
585 /* remove key and value */
595 /** Check if a table is a loop. When using table as direct acyclic digram,
597 * \param L The Lua VM state.
598 * \param idx The index of the table in the stack
599 * \return True if the table loops.
602 luaA_isloop(lua_State
*L
, int idx
)
604 if(lua_istable(L
, idx
))
606 lua_pushvalue(L
, idx
); /* push table on top */
607 if(luaA_hasitem(L
, lua_topointer(L
, -1)))
609 lua_pop(L
, 1); /* remove pushed table */
613 while(luaA_next(L
, -2))
615 /* check for recursivity */
616 if(luaA_isloop(L
, -1))
618 lua_pop(L
, 2); /* remove key and value */
621 lua_pop(L
, 1); /* remove value */
623 lua_pop(L
, 1); /* remove pushed table */
629 * This table can use safely object as key.
630 * \param L The Lua VM state.
631 * \return The number of elements pushed on stack.
634 luaA_otable_index(lua_State
*L
)
638 if((obj
= lua_touserdata(L
, 2)))
642 while(lua_next(L
, 1))
644 /* check the key against the key if the key is a userdata,
645 * otherwise check it again the value. */
646 if((v
= lua_touserdata(L
, -2))
650 else if((v
= lua_touserdata(L
, -1))
657 /* removes 'value'; keeps 'key' for next iteration */
668 * This table can use safely object as key.
669 * \param L The Lua VM state.
670 * \return The number of elements pushed on stack.
673 luaA_otable_newindex(lua_State
*L
)
677 if((obj
= lua_touserdata(L
, 2)))
681 while(lua_next(L
, 1))
683 if((v
= lua_touserdata(L
, -2))
688 /* push new value on top */
690 /* set in table key = value */
694 /* removes 'value'; keeps 'key' for next iteration */
705 * This function is multi-head (Zaphod) aware and will set display to
706 * the right screen according to mouse position.
707 * \param L The Lua VM state.
708 * \return The number of elements pushed on stack
710 * \lparam The command to launch.
711 * \lparam The optional screen number to spawn the command on.
714 luaA_spawn(lua_State
*L
)
716 char *host
, newdisplay
[128];
718 int screen
= 0, screenp
, displayp
;
720 if(lua_gettop(L
) == 2)
722 screen
= luaL_checknumber(L
, 2) - 1;
723 luaA_checkscreen(screen
);
726 cmd
= luaL_checkstring(L
, 1);
728 if(!globalconf
.xinerama_is_active
)
730 xcb_parse_display(NULL
, &host
, &displayp
, &screenp
);
731 snprintf(newdisplay
, sizeof(newdisplay
), "%s:%d.%d", host
, displayp
, screen
);
732 setenv("DISPLAY", newdisplay
, 1);
736 /* The double-fork construct avoids zombie processes and keeps the code
737 * clean from stupid signal handlers. */
742 if(globalconf
.connection
)
743 xcb_disconnect(globalconf
.connection
);
746 warn("execl '%s' failed: %s\n", cmd
, strerror(errno
));
755 /** Deprecated function, does nothing.
758 luaA_mouse_add(lua_State
*L
)
764 /** Initialize the Lua VM
771 static const struct luaL_reg otable_methods
[] =
773 { "__call", luaA_otable_new
},
776 static const struct luaL_reg otable_meta
[] =
778 { "__index", luaA_otable_index
},
779 { "__newindex", luaA_otable_newindex
},
782 static const struct luaL_reg awesome_lib
[] =
784 { "quit", luaA_quit
},
785 { "exec", luaA_exec
},
786 { "spawn", luaA_spawn
},
787 { "restart", luaA_restart
},
788 { "buttons", luaA_buttons
},
789 { "font_set", luaA_font_set
},
790 { "colors_set", luaA_colors_set
},
791 { "font", luaA_font
},
792 { "colors", luaA_colors
},
793 { "conffile", luaA_conffile
},
795 { "mouse_add", luaA_mouse_add
},
799 L
= globalconf
.L
= luaL_newstate();
805 /* Export awesome lib */
806 luaL_register(L
, "awesome", awesome_lib
);
808 /* Export hooks lib */
809 luaL_register(L
, "hooks", awesome_hooks_lib
);
811 /* Export keygrabber lib */
812 luaL_register(L
, "keygrabber", awesome_keygrabber_lib
);
814 /* Export otable lib */
815 luaA_openlib(L
, "otable", otable_methods
, otable_meta
);
818 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
821 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
824 luaA_openlib(L
, "button", awesome_button_methods
, awesome_button_meta
);
827 luaA_openlib(L
, "image", awesome_image_methods
, awesome_image_meta
);
830 luaA_openlib(L
, "tag", awesome_tag_methods
, awesome_tag_meta
);
832 /* Export statusbar */
833 luaA_openlib(L
, "statusbar", awesome_statusbar_methods
, awesome_statusbar_meta
);
836 luaA_openlib(L
, "wibox", awesome_wibox_methods
, awesome_wibox_meta
);
839 luaA_openlib(L
, "widget", awesome_widget_methods
, awesome_widget_meta
);
842 luaA_openlib(L
, "client", awesome_client_methods
, awesome_client_meta
);
844 /* Export titlebar */
845 luaA_openlib(L
, "titlebar", awesome_titlebar_methods
, awesome_titlebar_meta
);
848 luaA_openlib(L
, "keybinding", awesome_keybinding_methods
, awesome_keybinding_meta
);
850 lua_pushliteral(L
, "AWESOME_VERSION");
851 lua_pushstring(L
, AWESOME_VERSION
);
852 lua_settable(L
, LUA_GLOBALSINDEX
);
854 lua_pushliteral(L
, "AWESOME_RELEASE");
855 lua_pushstring(L
, AWESOME_RELEASE
);
856 lua_settable(L
, LUA_GLOBALSINDEX
);
859 globalconf
.hooks
.manage
= LUA_REFNIL
;
860 globalconf
.hooks
.unmanage
= LUA_REFNIL
;
861 globalconf
.hooks
.focus
= LUA_REFNIL
;
862 globalconf
.hooks
.unfocus
= LUA_REFNIL
;
863 globalconf
.hooks
.mouse_enter
= LUA_REFNIL
;
864 globalconf
.hooks
.arrange
= LUA_REFNIL
;
865 globalconf
.hooks
.clients
= LUA_REFNIL
;
866 globalconf
.hooks
.tags
= LUA_REFNIL
;
867 globalconf
.hooks
.tagged
= LUA_REFNIL
;
868 globalconf
.hooks
.property
= LUA_REFNIL
;
869 globalconf
.hooks
.timer
= LUA_REFNIL
;
871 /* add Lua lib path (/usr/share/awesome/lib by default) */
872 luaA_dostring(L
, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH
"/?.lua\"");
873 luaA_dostring(L
, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH
"/?/init.lua\"");
875 /* add XDG_CONFIG_DIR (/etc/xdg/awesome by default) as include path */
876 luaA_dostring(L
, "package.path = package.path .. \";" XDG_CONFIG_DIR
"/awesome/?.lua\"");
877 luaA_dostring(L
, "package.path = package.path .. \";" XDG_CONFIG_DIR
"/awesome/?/init.lua\"");
879 /* add either XDG_CONFIG_HOME/awesome or HOME/.config/awesome to path */
881 if((dir
= getenv("XDG_CONFIG_HOME")))
884 a_asprintf(&path
, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"", dir
, dir
);
885 luaA_dostring(globalconf
.L
, path
);
890 char *path
, *homedir
= getenv("HOME");
892 "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT
"/awesome/?.lua;%s" XDG_CONFIG_HOME_DEFAULT
"/awesome/?/init.lua\"",
894 luaA_dostring(globalconf
.L
, path
);
898 /* add XDG_CONFIG_DIRS to include paths */
899 char *xdg_config_dirs
= getenv("XDG_CONFIG_DIRS");
902 if((len
= a_strlen(xdg_config_dirs
)))
904 char **buf
, **xdg_files
= a_strsplit(xdg_config_dirs
, len
, ':');
906 for(buf
= xdg_files
; *buf
; buf
++)
909 a_asprintf(&confpath
, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"",
911 luaA_dostring(globalconf
.L
, confpath
);
915 for(buf
= xdg_files
; *buf
; buf
++)
917 p_delete(&xdg_files
);
921 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
924 luaA_loadrc(const char *confpath
, bool run
)
928 if(!luaL_loadfile(globalconf
.L
, confpath
))
932 if(lua_pcall(globalconf
.L
, 0, LUA_MULTRET
, 0))
933 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
936 globalconf
.conffile
= a_strdup(confpath
);
941 lua_pop(globalconf
.L
, 1);
945 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
950 /** Load a configuration file.
951 * \param confpatharg The configuration file to load.
952 * \param run Run the configuration file.
955 luaA_parserc(const char *confpatharg
, bool run
)
958 const char *confdir
, *xdg_config_dirs
;
959 char *confpath
= NULL
, **xdg_files
= NULL
, **buf
;
963 /* try to load, return if it's ok */
964 if(luaA_loadrc(confpatharg
, run
))
970 if((confdir
= getenv("XDG_CONFIG_HOME")))
971 a_asprintf(&confpath
, "%s" AWESOME_CONFIG_FILE
, confdir
);
973 a_asprintf(&confpath
, "%s" XDG_CONFIG_HOME_DEFAULT AWESOME_CONFIG_FILE
, getenv("HOME"));
975 /* try to run XDG_CONFIG_HOME/awesome/rc.lua */
976 if(luaA_loadrc(confpath
, run
))
984 xdg_config_dirs
= getenv("XDG_CONFIG_DIRS");
986 if(!(len
= a_strlen(xdg_config_dirs
)))
988 xdg_config_dirs
= XDG_CONFIG_DIR
;
989 len
= sizeof(XDG_CONFIG_DIR
) - 1;
992 xdg_files
= a_strsplit(xdg_config_dirs
, len
, ':');
994 for(buf
= xdg_files
; *buf
&& !ret
; buf
++)
996 a_asprintf(&confpath
, "%s" AWESOME_CONFIG_FILE
, *buf
);
997 if(luaA_loadrc(confpath
, run
))
1002 p_delete(&confpath
);
1007 p_delete(&confpath
);
1011 for(buf
= xdg_files
; *buf
; buf
++)
1013 p_delete(&xdg_files
);
1016 /* Assure there's at least one tag */
1017 for(screen
= 0; screen
< globalconf
.nscreen
; screen
++)
1018 if(!globalconf
.screens
[screen
].tags
.len
)
1019 tag_append_to_screen(tag_new("default", sizeof("default") - 1, layout_tile
, 0.5, 1, 0),
1020 &globalconf
.screens
[screen
]);
1024 /** Parse a command.
1025 * \param cmd The buffer to parse.
1026 * \return the number of elements pushed on the stack by the last statement in cmd.
1027 * If there's an error, the message is pushed onto the stack and this returns 1.
1030 luaA_docmd(const char *cmd
)
1033 int newtop
, oldtop
= lua_gettop(globalconf
.L
);
1035 while((p
= strchr(cmd
, '\n')))
1037 newtop
= lua_gettop(globalconf
.L
);
1038 lua_pop(globalconf
.L
, newtop
- oldtop
);
1042 if (luaL_dostring(globalconf
.L
, cmd
))
1044 warn("error executing Lua code: %s", lua_tostring(globalconf
.L
, -1));
1049 return lua_gettop(globalconf
.L
) - oldtop
;
1052 /** Pushes a Lua array containing the top n elements of the stack.
1053 * \param n The number of elements to put in the array.
1059 lua_createtable(globalconf
.L
, n
, 0);
1060 lua_insert(globalconf
.L
, -n
- 1);
1062 for (i
= n
; i
> 0; i
--)
1063 lua_rawseti(globalconf
.L
, -i
- 1, i
);
1066 /** Maps the top n elements of the stack to the result of
1067 * applying a function to that element.
1068 * \param n The number of elements to map.
1070 * \lparam The function to map the elements by. This should be
1071 * at position -(n + 1).
1077 for (i
= 0; i
< n
; i
++)
1079 lua_pushvalue(globalconf
.L
, -n
- 1); /* copy of the function */
1080 lua_pushvalue(globalconf
.L
, -n
- 1); /* value to map */
1081 lua_pcall(globalconf
.L
, 1, 1, 0); /* call function */
1082 lua_remove(globalconf
.L
, -n
- 1); /* remove old value */
1084 lua_remove(globalconf
.L
, -n
- 1); /* remove function */
1087 static void luaA_conn_cleanup(EV_P_ ev_io
*w
)
1089 ev_ref(EV_DEFAULT_UC
);
1090 ev_io_stop(EV_DEFAULT_UC_ w
);
1092 warn("error closing UNIX domain socket: %s", strerror(errno
));
1097 luaA_cb(EV_P_ ev_io
*w
, int revents
)
1104 switch(r
= recv(w
->fd
, buf
, sizeof(buf
)-1, MSG_TRUNC
))
1107 warn("error reading UNIX domain socket: %s", strerror(errno
));
1108 case 0: /* 0 bytes are only transferred when the connection is closed */
1109 luaA_conn_cleanup(EV_DEFAULT_UC_ w
);
1112 if(r
>= ssizeof(buf
))
1115 lua_getglobal(globalconf
.L
, "table");
1116 lua_getfield(globalconf
.L
, -1, "concat");
1117 lua_remove(globalconf
.L
, -2); /* remove table */
1119 lua_getglobal(globalconf
.L
, "tostring");
1120 els
= luaA_docmd(buf
);
1121 luaA_map(els
); /* map results to strings */
1122 luaA_array(els
); /* put strings in an array */
1124 lua_pushstring(globalconf
.L
, "\t");
1125 lua_pcall(globalconf
.L
, 2, 1, 0); /* concatenate results with tabs */
1127 s
= lua_tolstring(globalconf
.L
, -1, &len
);
1129 /* ignore ENOENT because the client may not read */
1130 if (send(w
->fd
, s
, len
, MSG_DONTWAIT
) == -1)
1137 warn("can't send back to client via domain socket: %s", strerror(errno
));
1141 lua_pop(globalconf
.L
, 1); /* pop the string */
1143 awesome_refresh(globalconf
.connection
);
1148 luaA_conn_cb(EV_P_ ev_io
*w
, int revents
)
1150 ev_io
*csio_conn
= p_new(ev_io
, 1);
1151 int csfd
= accept(w
->fd
, NULL
, NULL
);
1153 ev_io_init(csio_conn
, &luaA_cb
, csfd
, EV_READ
);
1154 ev_io_start(EV_DEFAULT_UC_ csio_conn
);
1155 ev_unref(EV_DEFAULT_UC
);
1161 int csfd
= socket_getclient();
1163 if (csfd
< 0 || fcntl(csfd
, F_SETFD
, FD_CLOEXEC
) == -1)
1166 addr
= socket_getaddr(getenv("DISPLAY"));
1168 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
1170 if(errno
== EADDRINUSE
)
1172 if(unlink(addr
->sun_path
))
1173 warn("error unlinking existing file: %s", strerror(errno
));
1174 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
1175 return warn("error binding UNIX domain socket: %s", strerror(errno
));
1178 return warn("error binding UNIX domain socket: %s", strerror(errno
));
1182 ev_io_init(&csio
, &luaA_conn_cb
, csfd
, EV_READ
);
1183 ev_io_start(EV_DEFAULT_UC_
&csio
);
1184 ev_unref(EV_DEFAULT_UC
);
1188 luaA_cs_cleanup(void)
1192 ev_ref(EV_DEFAULT_UC
);
1193 ev_io_stop(EV_DEFAULT_UC_
&csio
);
1195 warn("error closing UNIX domain socket: %s", strerror(errno
));
1196 if(unlink(addr
->sun_path
))
1197 warn("error unlinking UNIX domain socket: %s", strerror(errno
));
1203 luaA_on_timer(EV_P_ ev_timer
*w
, int revents
)
1205 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.timer
, 0, 0);
1206 awesome_refresh(globalconf
.connection
);
1209 /** Push a color as a string onto the stack
1210 * \param L The Lua VM state.
1211 * \param c The color to push.
1212 * \return The number of elements pushed on stack.
1215 luaA_pushcolor(lua_State
*L
, const xcolor_t
*c
)
1217 uint8_t r
= (unsigned)c
->red
* 0xff / 0xffff;
1218 uint8_t g
= (unsigned)c
->green
* 0xff / 0xffff;
1219 uint8_t b
= (unsigned)c
->blue
* 0xff / 0xffff;
1220 uint8_t a
= (unsigned)c
->alpha
* 0xff / 0xffff;
1223 /* do not print alpha if it's full */
1225 len
= snprintf(s
, sizeof(s
), "#%02x%02x%02x", r
, g
, b
);
1227 len
= snprintf(s
, sizeof(s
), "#%02x%02x%02x%02x", r
, g
, b
, a
);
1228 lua_pushlstring(L
, s
, len
);