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 default font. (DEPRECATED)
147 * \param L The Lua VM state.
148 * \return The number of elements pushed on stack.
150 * \lparam A string with a font name in Pango format.
153 luaA_font_set(lua_State
*L
)
155 deprecate(L
, "awesome.font attribute");
156 if(lua_gettop(L
) == 1)
158 const char *newfont
= luaL_checkstring(L
, 1);
159 draw_font_delete(&globalconf
.font
);
160 globalconf
.font
= draw_font_new(newfont
);
163 char *font
= pango_font_description_to_string(globalconf
.font
->desc
);
164 lua_pushstring(L
, font
);
169 /** Set default colors (DEPRECATED).
170 * \param L The Lua VM state.
171 * \return The number of elements pushed on stack.
173 * \lparam A table with `fg' and `bg' elements, containing colors.
174 * \lreturn A table with `fg' and `bg' elements, containing colors.
177 luaA_colors(lua_State
*L
)
179 deprecate(L
, "awesome.fg and awesome.bg attributes");
180 if(lua_gettop(L
) == 1)
184 int8_t colors_nbr
= -1, i
;
185 xcolor_init_request_t reqs
[2];
187 luaA_checktable(L
, 1);
189 if((buf
= luaA_getopt_lstring(L
, 1, "fg", NULL
, &len
)))
190 reqs
[++colors_nbr
] = xcolor_init_unchecked(&globalconf
.colors
.fg
, buf
, len
);
192 if((buf
= luaA_getopt_lstring(L
, 1, "bg", NULL
, &len
)))
193 reqs
[++colors_nbr
] = xcolor_init_unchecked(&globalconf
.colors
.bg
, buf
, len
);
195 for(i
= 0; i
<= colors_nbr
; i
++)
196 xcolor_init_reply(reqs
[i
]);
200 luaA_pushcolor(L
, &globalconf
.colors
.fg
);
201 lua_setfield(L
, -2, "fg");
202 luaA_pushcolor(L
, &globalconf
.colors
.bg
);
203 lua_setfield(L
, -2, "bg");
208 /** Set default colors. (DEPRECATED)
209 * \param L The Lua VM state.
210 * \return The number of elements pushed on stack.
213 * \lparam A table with `fg' and `bg' elements, containing colors.
216 luaA_colors_set(lua_State
*L
)
218 return luaA_colors(L
);
222 luaA_openlib(lua_State
*L
, const char *name
,
223 const struct luaL_reg methods
[],
224 const struct luaL_reg meta
[])
226 luaL_newmetatable(L
, name
); /* 1 */
227 lua_pushvalue(L
, -1); /* dup metatable 2 */
228 lua_setfield(L
, -2, "__index"); /* metatable.__index = metatable 1 */
230 luaL_register(L
, NULL
, meta
); /* 1 */
231 luaL_register(L
, name
, methods
); /* 2 */
232 lua_pushvalue(L
, -1); /* dup self as metatable 3 */
233 lua_setmetatable(L
, -2); /* set self as metatable 2 */
237 /** UTF-8 aware string length computing.
238 * \param L The Lua VM state.
239 * \return The number of elements pushed on stack.
242 luaA_mbstrlen(lua_State
*L
)
244 const char *cmd
= luaL_checkstring(L
, 1);
245 lua_pushnumber(L
, mbstowcs(NULL
, NONULL(cmd
), 0));
249 /** Overload standard Lua next function to use __next key on metatable.
250 * \param L The Lua VM state.
251 * \param The number of elements pushed on stack.
254 luaAe_next(lua_State
*L
)
256 if(luaL_getmetafield(L
, 1, "__next"))
259 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
260 return lua_gettop(L
);
263 luaL_checktype(L
, 1, LUA_TTABLE
);
271 /** Overload lua_next() function by using __next metatable field
272 * to get next elements.
273 * \param L The Lua VM stack.
274 * \param idx The index number of elements in stack.
275 * \return 1 if more elements to come, 0 otherwise.
278 luaA_next(lua_State
*L
, int idx
)
280 if(luaL_getmetafield(L
, idx
, "__next"))
282 /* if idx is relative, reduce it since we got __next */
284 /* copy table and then move key */
285 lua_pushvalue(L
, idx
);
286 lua_pushvalue(L
, -3);
288 lua_pcall(L
, 2, 2, 0);
289 /* next returned nil, it's the end */
298 else if(lua_istable(L
, idx
))
299 return lua_next(L
, idx
);
305 /** Generic pairs function.
306 * \param L The Lua VM state.
307 * \return The number of elements pushed on stack.
310 luaA_generic_pairs(lua_State
*L
)
312 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
313 lua_pushvalue(L
, 1); /* state, */
314 lua_pushnil(L
); /* and initial value */
318 /** Overload standard pairs function to use __pairs field of metatables.
319 * \param L The Lua VM state.
320 * \return The number of elements pushed on stack.
323 luaAe_pairs(lua_State
*L
)
325 if(luaL_getmetafield(L
, 1, "__pairs"))
328 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
329 return lua_gettop(L
);
332 luaL_checktype(L
, 1, LUA_TTABLE
);
333 return luaA_generic_pairs(L
);
337 luaA_ipairs_aux(lua_State
*L
)
339 int i
= luaL_checkint(L
, 2) + 1;
340 luaL_checktype(L
, 1, LUA_TTABLE
);
341 lua_pushinteger(L
, i
);
342 lua_rawgeti(L
, 1, i
);
343 return (lua_isnil(L
, -1)) ? 0 : 2;
346 /** Overload standard ipairs function to use __ipairs field of metatables.
347 * \param L The Lua VM state.
348 * \return The number of elements pushed on stack.
351 luaAe_ipairs(lua_State
*L
)
353 if(luaL_getmetafield(L
, 1, "__ipairs"))
356 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
357 return lua_gettop(L
);
360 luaL_checktype(L
, 1, LUA_TTABLE
);
361 lua_pushvalue(L
, lua_upvalueindex(1));
363 lua_pushinteger(L
, 0); /* and initial value */
367 /** Replace various standards Lua functions with our own.
368 * \param L The Lua VM state.
371 luaA_fixups(lua_State
*L
)
373 /* replace string.len */
374 lua_getglobal(L
, "string");
375 lua_pushcfunction(L
, luaA_mbstrlen
);
376 lua_setfield(L
, -2, "len");
379 lua_pushliteral(L
, "next");
380 lua_pushcfunction(L
, luaAe_next
);
381 lua_settable(L
, LUA_GLOBALSINDEX
);
383 lua_pushliteral(L
, "pairs");
384 lua_pushcfunction(L
, luaAe_next
);
385 lua_pushcclosure(L
, luaAe_pairs
, 1); /* pairs get next as upvalue */
386 lua_settable(L
, LUA_GLOBALSINDEX
);
388 lua_pushliteral(L
, "ipairs");
389 lua_pushcfunction(L
, luaA_ipairs_aux
);
390 lua_pushcclosure(L
, luaAe_ipairs
, 1);
391 lua_settable(L
, LUA_GLOBALSINDEX
);
394 /** __next function for wtable objects.
395 * \param L The Lua VM state.
396 * \return The number of elements pushed on stack.
399 luaA_wtable_next(lua_State
*L
)
401 /* upvalue 1 is content table */
402 if(lua_next(L
, lua_upvalueindex(1)))
408 /** __ipairs function for wtable objects.
409 * \param L The Lua VM state.
410 * \return The number of elements pushed on stack.
413 luaA_wtable_ipairs(lua_State
*L
)
415 /* push ipairs_aux */
416 lua_pushvalue(L
, lua_upvalueindex(2));
417 /* push content table */
418 lua_pushvalue(L
, lua_upvalueindex(1));
419 lua_pushinteger(L
, 0); /* and initial value */
423 /** Index function of wtable objects.
424 * \param L The Lua VM state.
425 * \return The number of elements pushed on stack.
428 luaA_wtable_index(lua_State
*L
)
434 /* check for size, waiting lua 5.2 and __len on tables */
435 if((buf
= lua_tolstring(L
, -1, &len
)))
436 if(a_tokenize(buf
, len
) == A_TK_LEN
)
438 lua_pushnumber(L
, lua_objlen(L
, lua_upvalueindex(1)));
443 /* upvalue 1 is content table */
444 lua_rawget(L
, lua_upvalueindex(1));
448 /** Newndex function of wtable objects.
449 * \param L The Lua VM state.
450 * \return The number of elements pushed on stack.
453 luaA_wtable_newindex(lua_State
*L
)
455 bool invalid
= false;
457 /* push key on top */
459 /* get current key value in content table */
460 lua_rawget(L
, lua_upvalueindex(1));
461 /* if value is a widget, notify change */
462 if(lua_istable(L
, -1) || luaA_toudata(L
, -1, "widget"))
465 lua_pop(L
, 1); /* remove value */
467 /* if new value is a widget or a table */
468 if(lua_istable(L
, 3))
470 luaA_table2wtable(L
);
473 else if(!invalid
&& luaA_toudata(L
, 3, "widget"))
476 /* upvalue 1 is content table */
477 lua_rawset(L
, lua_upvalueindex(1));
480 luaA_wibox_invalidate_byitem(L
, lua_topointer(L
, 1));
485 /** Convert the top element of the stack to a proxied wtable.
486 * \param L The Lua VM state.
489 luaA_table2wtable(lua_State
*L
)
491 if(!lua_istable(L
, -1))
494 lua_newtable(L
); /* create *real* content table */
495 lua_newtable(L
); /* metatable */
496 lua_pushvalue(L
, -2); /* copy content table */
497 lua_pushcfunction(L
, luaA_ipairs_aux
); /* push ipairs aux */
498 lua_pushcclosure(L
, luaA_wtable_ipairs
, 2);
499 lua_pushvalue(L
, -3); /* copy content table */
500 lua_pushcclosure(L
, luaA_wtable_next
, 1); /* __next has the content table as upvalue */
501 lua_pushvalue(L
, -4); /* copy content table */
502 lua_pushcclosure(L
, luaA_wtable_index
, 1); /* __index has the content table as upvalue */
503 lua_pushvalue(L
, -5); /* copy content table */
504 lua_pushcclosure(L
, luaA_wtable_newindex
, 1); /* __newindex has the content table as upvalue */
505 /* set metatable field with just pushed closure */
506 lua_setfield(L
, -5, "__newindex");
507 lua_setfield(L
, -4, "__index");
508 lua_setfield(L
, -3, "__next");
509 lua_setfield(L
, -2, "__ipairs");
510 /* set metatable impossible to touch */
511 lua_pushliteral(L
, "wtable");
512 lua_setfield(L
, -2, "__metatable");
513 /* set new metatable on original table */
514 lua_setmetatable(L
, -3);
518 /* go through original table */
519 while(lua_next(L
, -3))
521 /* if convert value to wtable */
522 luaA_table2wtable(L
);
524 lua_pushvalue(L
, -2);
525 /* move key before value */
527 /* set same value in content table */
530 lua_pushvalue(L
, -1);
531 /* push the new value :-> */
533 /* set orig[k] = nil */
536 /* remove content table */
540 /** Look for an item: table, function, etc.
541 * \param L The Lua VM state.
542 * \param item The pointer item.
545 luaA_hasitem(lua_State
*L
, const void *item
)
548 while(luaA_next(L
, -2))
550 if(lua_topointer(L
, -1) == item
)
552 /* remove value and key */
556 if(lua_istable(L
, -1))
557 if(luaA_hasitem(L
, item
))
559 /* remove key and value */
569 /** Check if a table is a loop. When using table as direct acyclic digram,
571 * \param L The Lua VM state.
572 * \param idx The index of the table in the stack
573 * \return True if the table loops.
576 luaA_isloop(lua_State
*L
, int idx
)
578 if(lua_istable(L
, idx
))
580 lua_pushvalue(L
, idx
); /* push table on top */
581 if(luaA_hasitem(L
, lua_topointer(L
, -1)))
583 lua_pop(L
, 1); /* remove pushed table */
587 while(luaA_next(L
, -2))
589 /* check for recursivity */
590 if(luaA_isloop(L
, -1))
592 lua_pop(L
, 2); /* remove key and value */
595 lua_pop(L
, 1); /* remove value */
597 lua_pop(L
, 1); /* remove pushed table */
603 * This table can use safely object as key.
604 * \param L The Lua VM state.
605 * \return The number of elements pushed on stack.
608 luaA_otable_index(lua_State
*L
)
612 if((obj
= lua_touserdata(L
, 2)))
616 while(lua_next(L
, 1))
618 /* check the key against the key if the key is a userdata,
619 * otherwise check it again the value. */
620 if((v
= lua_touserdata(L
, -2))
624 else if((v
= lua_touserdata(L
, -1))
631 /* removes 'value'; keeps 'key' for next iteration */
642 * This table can use safely object as key.
643 * \param L The Lua VM state.
644 * \return The number of elements pushed on stack.
647 luaA_otable_newindex(lua_State
*L
)
651 if((obj
= lua_touserdata(L
, 2)))
655 while(lua_next(L
, 1))
657 if((v
= lua_touserdata(L
, -2))
662 /* push new value on top */
664 /* set in table key = value */
668 /* removes 'value'; keeps 'key' for next iteration */
679 * This function is multi-head (Zaphod) aware and will set display to
680 * the right screen according to mouse position.
681 * \param L The Lua VM state.
682 * \return The number of elements pushed on stack
684 * \lparam The command to launch.
685 * \lparam The optional screen number to spawn the command on.
688 luaA_spawn(lua_State
*L
)
690 char *host
, newdisplay
[128];
692 int screen
= 0, screenp
, displayp
;
694 if(lua_gettop(L
) == 2)
696 screen
= luaL_checknumber(L
, 2) - 1;
697 luaA_checkscreen(screen
);
700 cmd
= luaL_checkstring(L
, 1);
702 if(!globalconf
.xinerama_is_active
)
704 xcb_parse_display(NULL
, &host
, &displayp
, &screenp
);
705 snprintf(newdisplay
, sizeof(newdisplay
), "%s:%d.%d", host
, displayp
, screen
);
706 setenv("DISPLAY", newdisplay
, 1);
710 /* The double-fork construct avoids zombie processes and keeps the code
711 * clean from stupid signal handlers. */
716 if(globalconf
.connection
)
717 xcb_disconnect(globalconf
.connection
);
720 warn("execl '%s' failed: %s\n", cmd
, strerror(errno
));
729 /** Deprecated function, does nothing.
732 luaA_mouse_add(lua_State
*L
)
734 deprecate(L
, "awesome.buttons()");
738 /** awesome global table.
739 * \param L The Lua VM state.
740 * \return The number of elements pushed on stack.
742 * \lfield font The default font.
743 * \lfield conffile The configuration file which has been loaded.
746 luaA_awesome_index(lua_State
*L
)
748 if(luaA_usemetatable(L
, 1, 2))
752 const char *buf
= luaL_checklstring(L
, 2, &len
);
754 switch(a_tokenize(buf
, len
))
758 char *font
= pango_font_description_to_string(globalconf
.font
->desc
);
759 lua_pushstring(L
, font
);
764 lua_pushstring(L
, globalconf
.conffile
);
767 luaA_pushcolor(L
, &globalconf
.colors
.fg
);
770 luaA_pushcolor(L
, &globalconf
.colors
.bg
);
779 /** Newindex function for the awesome global table.
780 * \param L The Lua VM state.
781 * \return The number of elements pushed on stack.
784 luaA_awesome_newindex(lua_State
*L
)
786 if(luaA_usemetatable(L
, 1, 2))
790 const char *buf
= luaL_checklstring(L
, 2, &len
);
792 switch(a_tokenize(buf
, len
))
796 const char *newfont
= luaL_checkstring(L
, 3);
797 draw_font_delete(&globalconf
.font
);
798 globalconf
.font
= draw_font_new(newfont
);
802 if((buf
= luaL_checklstring(L
, 3, &len
)))
803 xcolor_init_reply(xcolor_init_unchecked(&globalconf
.colors
.fg
, buf
, len
));
806 if((buf
= luaL_checklstring(L
, 3, &len
)))
807 xcolor_init_reply(xcolor_init_unchecked(&globalconf
.colors
.bg
, buf
, len
));
816 /** Initialize the Lua VM
823 static const struct luaL_reg otable_methods
[] =
825 { "__call", luaA_otable_new
},
828 static const struct luaL_reg otable_meta
[] =
830 { "__index", luaA_otable_index
},
831 { "__newindex", luaA_otable_newindex
},
834 static const struct luaL_reg awesome_lib
[] =
836 { "quit", luaA_quit
},
837 { "exec", luaA_exec
},
838 { "spawn", luaA_spawn
},
839 { "restart", luaA_restart
},
840 { "buttons", luaA_buttons
},
841 { "font_set", luaA_font_set
},
842 { "colors_set", luaA_colors_set
},
843 { "colors", luaA_colors
},
844 { "__index", luaA_awesome_index
},
845 { "__newindex", luaA_awesome_newindex
},
847 { "mouse_add", luaA_mouse_add
},
851 L
= globalconf
.L
= luaL_newstate();
857 /* Export awesome lib */
858 luaA_openlib(L
, "awesome", awesome_lib
, awesome_lib
);
860 /* Export hooks lib */
861 luaL_register(L
, "hooks", awesome_hooks_lib
);
863 /* Export keygrabber lib */
864 luaL_register(L
, "keygrabber", awesome_keygrabber_lib
);
866 /* Export otable lib */
867 luaA_openlib(L
, "otable", otable_methods
, otable_meta
);
870 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
873 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
876 luaA_openlib(L
, "button", awesome_button_methods
, awesome_button_meta
);
879 luaA_openlib(L
, "image", awesome_image_methods
, awesome_image_meta
);
882 luaA_openlib(L
, "tag", awesome_tag_methods
, awesome_tag_meta
);
884 /* Export statusbar */
885 luaA_openlib(L
, "statusbar", awesome_statusbar_methods
, awesome_statusbar_meta
);
888 luaA_openlib(L
, "wibox", awesome_wibox_methods
, awesome_wibox_meta
);
891 luaA_openlib(L
, "widget", awesome_widget_methods
, awesome_widget_meta
);
894 luaA_openlib(L
, "client", awesome_client_methods
, awesome_client_meta
);
896 /* Export titlebar */
897 luaA_openlib(L
, "titlebar", awesome_titlebar_methods
, awesome_titlebar_meta
);
900 luaA_openlib(L
, "keybinding", awesome_keybinding_methods
, awesome_keybinding_meta
);
902 lua_pushliteral(L
, "AWESOME_VERSION");
903 lua_pushstring(L
, AWESOME_VERSION
);
904 lua_settable(L
, LUA_GLOBALSINDEX
);
906 lua_pushliteral(L
, "AWESOME_RELEASE");
907 lua_pushstring(L
, AWESOME_RELEASE
);
908 lua_settable(L
, LUA_GLOBALSINDEX
);
911 globalconf
.hooks
.manage
= LUA_REFNIL
;
912 globalconf
.hooks
.unmanage
= LUA_REFNIL
;
913 globalconf
.hooks
.focus
= LUA_REFNIL
;
914 globalconf
.hooks
.unfocus
= LUA_REFNIL
;
915 globalconf
.hooks
.mouse_enter
= LUA_REFNIL
;
916 globalconf
.hooks
.arrange
= LUA_REFNIL
;
917 globalconf
.hooks
.clients
= LUA_REFNIL
;
918 globalconf
.hooks
.tags
= LUA_REFNIL
;
919 globalconf
.hooks
.tagged
= LUA_REFNIL
;
920 globalconf
.hooks
.property
= LUA_REFNIL
;
921 globalconf
.hooks
.timer
= LUA_REFNIL
;
923 /* add Lua lib path (/usr/share/awesome/lib by default) */
924 luaA_dostring(L
, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH
"/?.lua\"");
925 luaA_dostring(L
, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH
"/?/init.lua\"");
927 /* add XDG_CONFIG_DIR (/etc/xdg/awesome by default) as include path */
928 luaA_dostring(L
, "package.path = package.path .. \";" XDG_CONFIG_DIR
"/awesome/?.lua\"");
929 luaA_dostring(L
, "package.path = package.path .. \";" XDG_CONFIG_DIR
"/awesome/?/init.lua\"");
931 /* add either XDG_CONFIG_HOME/awesome or HOME/.config/awesome to path */
933 if((dir
= getenv("XDG_CONFIG_HOME")))
936 a_asprintf(&path
, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"", dir
, dir
);
937 luaA_dostring(globalconf
.L
, path
);
942 char *path
, *homedir
= getenv("HOME");
944 "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT
"/awesome/?.lua;%s" XDG_CONFIG_HOME_DEFAULT
"/awesome/?/init.lua\"",
946 luaA_dostring(globalconf
.L
, path
);
950 /* add XDG_CONFIG_DIRS to include paths */
951 char *xdg_config_dirs
= getenv("XDG_CONFIG_DIRS");
954 if((len
= a_strlen(xdg_config_dirs
)))
956 char **buf
, **xdg_files
= a_strsplit(xdg_config_dirs
, len
, ':');
958 for(buf
= xdg_files
; *buf
; buf
++)
961 a_asprintf(&confpath
, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"",
963 luaA_dostring(globalconf
.L
, confpath
);
967 for(buf
= xdg_files
; *buf
; buf
++)
969 p_delete(&xdg_files
);
973 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
976 luaA_loadrc(const char *confpath
, bool run
)
980 if(!luaL_loadfile(globalconf
.L
, confpath
))
984 if(lua_pcall(globalconf
.L
, 0, LUA_MULTRET
, 0))
985 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
988 globalconf
.conffile
= a_strdup(confpath
);
993 lua_pop(globalconf
.L
, 1);
997 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
1002 /** Load a configuration file.
1003 * \param confpatharg The configuration file to load.
1004 * \param run Run the configuration file.
1007 luaA_parserc(const char *confpatharg
, bool run
)
1010 const char *confdir
, *xdg_config_dirs
;
1011 char *confpath
= NULL
, **xdg_files
= NULL
, **buf
;
1015 /* try to load, return if it's ok */
1016 if(luaA_loadrc(confpatharg
, run
))
1022 if((confdir
= getenv("XDG_CONFIG_HOME")))
1023 a_asprintf(&confpath
, "%s" AWESOME_CONFIG_FILE
, confdir
);
1025 a_asprintf(&confpath
, "%s" XDG_CONFIG_HOME_DEFAULT AWESOME_CONFIG_FILE
, getenv("HOME"));
1027 /* try to run XDG_CONFIG_HOME/awesome/rc.lua */
1028 if(luaA_loadrc(confpath
, run
))
1034 p_delete(&confpath
);
1036 xdg_config_dirs
= getenv("XDG_CONFIG_DIRS");
1038 if(!(len
= a_strlen(xdg_config_dirs
)))
1040 xdg_config_dirs
= XDG_CONFIG_DIR
;
1041 len
= sizeof(XDG_CONFIG_DIR
) - 1;
1044 xdg_files
= a_strsplit(xdg_config_dirs
, len
, ':');
1046 for(buf
= xdg_files
; *buf
&& !ret
; buf
++)
1048 a_asprintf(&confpath
, "%s" AWESOME_CONFIG_FILE
, *buf
);
1049 if(luaA_loadrc(confpath
, run
))
1054 p_delete(&confpath
);
1059 p_delete(&confpath
);
1063 for(buf
= xdg_files
; *buf
; buf
++)
1065 p_delete(&xdg_files
);
1068 /* Assure there's at least one tag */
1069 for(screen
= 0; screen
< globalconf
.nscreen
; screen
++)
1070 if(!globalconf
.screens
[screen
].tags
.len
)
1071 tag_append_to_screen(tag_new("default", sizeof("default") - 1, layout_tile
, 0.5, 1, 0),
1072 &globalconf
.screens
[screen
]);
1076 /** Parse a command.
1077 * \param cmd The buffer to parse.
1078 * \return the number of elements pushed on the stack by the last statement in cmd.
1079 * If there's an error, the message is pushed onto the stack and this returns 1.
1082 luaA_docmd(const char *cmd
)
1085 int newtop
, oldtop
= lua_gettop(globalconf
.L
);
1087 while((p
= strchr(cmd
, '\n')))
1089 newtop
= lua_gettop(globalconf
.L
);
1090 lua_pop(globalconf
.L
, newtop
- oldtop
);
1094 if (luaL_dostring(globalconf
.L
, cmd
))
1096 warn("error executing Lua code: %s", lua_tostring(globalconf
.L
, -1));
1101 return lua_gettop(globalconf
.L
) - oldtop
;
1104 /** Pushes a Lua array containing the top n elements of the stack.
1105 * \param n The number of elements to put in the array.
1111 lua_createtable(globalconf
.L
, n
, 0);
1112 lua_insert(globalconf
.L
, -n
- 1);
1114 for (i
= n
; i
> 0; i
--)
1115 lua_rawseti(globalconf
.L
, -i
- 1, i
);
1118 /** Maps the top n elements of the stack to the result of
1119 * applying a function to that element.
1120 * \param n The number of elements to map.
1122 * \lparam The function to map the elements by. This should be
1123 * at position -(n + 1).
1129 for (i
= 0; i
< n
; i
++)
1131 lua_pushvalue(globalconf
.L
, -n
- 1); /* copy of the function */
1132 lua_pushvalue(globalconf
.L
, -n
- 1); /* value to map */
1133 lua_pcall(globalconf
.L
, 1, 1, 0); /* call function */
1134 lua_remove(globalconf
.L
, -n
- 1); /* remove old value */
1136 lua_remove(globalconf
.L
, -n
- 1); /* remove function */
1139 static void luaA_conn_cleanup(EV_P_ ev_io
*w
)
1141 ev_ref(EV_DEFAULT_UC
);
1142 ev_io_stop(EV_DEFAULT_UC_ w
);
1144 warn("error closing UNIX domain socket: %s", strerror(errno
));
1149 luaA_cb(EV_P_ ev_io
*w
, int revents
)
1156 switch(r
= recv(w
->fd
, buf
, sizeof(buf
)-1, MSG_TRUNC
))
1159 warn("error reading UNIX domain socket: %s", strerror(errno
));
1160 case 0: /* 0 bytes are only transferred when the connection is closed */
1161 luaA_conn_cleanup(EV_DEFAULT_UC_ w
);
1164 if(r
>= ssizeof(buf
))
1167 lua_getglobal(globalconf
.L
, "table");
1168 lua_getfield(globalconf
.L
, -1, "concat");
1169 lua_remove(globalconf
.L
, -2); /* remove table */
1171 lua_getglobal(globalconf
.L
, "tostring");
1172 els
= luaA_docmd(buf
);
1173 luaA_map(els
); /* map results to strings */
1174 luaA_array(els
); /* put strings in an array */
1176 lua_pushstring(globalconf
.L
, "\t");
1177 lua_pcall(globalconf
.L
, 2, 1, 0); /* concatenate results with tabs */
1179 s
= lua_tolstring(globalconf
.L
, -1, &len
);
1181 /* ignore ENOENT because the client may not read */
1182 if (send(w
->fd
, s
, len
, MSG_DONTWAIT
) == -1)
1189 warn("can't send back to client via domain socket: %s", strerror(errno
));
1193 lua_pop(globalconf
.L
, 1); /* pop the string */
1195 awesome_refresh(globalconf
.connection
);
1200 luaA_conn_cb(EV_P_ ev_io
*w
, int revents
)
1202 ev_io
*csio_conn
= p_new(ev_io
, 1);
1203 int csfd
= accept(w
->fd
, NULL
, NULL
);
1205 ev_io_init(csio_conn
, &luaA_cb
, csfd
, EV_READ
);
1206 ev_io_start(EV_DEFAULT_UC_ csio_conn
);
1207 ev_unref(EV_DEFAULT_UC
);
1213 int csfd
= socket_getclient();
1215 if (csfd
< 0 || fcntl(csfd
, F_SETFD
, FD_CLOEXEC
) == -1)
1218 addr
= socket_getaddr(getenv("DISPLAY"));
1220 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
1222 if(errno
== EADDRINUSE
)
1224 if(unlink(addr
->sun_path
))
1225 warn("error unlinking existing file: %s", strerror(errno
));
1226 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
1227 return warn("error binding UNIX domain socket: %s", strerror(errno
));
1230 return warn("error binding UNIX domain socket: %s", strerror(errno
));
1234 ev_io_init(&csio
, &luaA_conn_cb
, csfd
, EV_READ
);
1235 ev_io_start(EV_DEFAULT_UC_
&csio
);
1236 ev_unref(EV_DEFAULT_UC
);
1240 luaA_cs_cleanup(void)
1244 ev_ref(EV_DEFAULT_UC
);
1245 ev_io_stop(EV_DEFAULT_UC_
&csio
);
1247 warn("error closing UNIX domain socket: %s", strerror(errno
));
1248 if(unlink(addr
->sun_path
))
1249 warn("error unlinking UNIX domain socket: %s", strerror(errno
));
1255 luaA_on_timer(EV_P_ ev_timer
*w
, int revents
)
1257 if(globalconf
.hooks
.timer
!= LUA_REFNIL
)
1258 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.timer
, 0, 0);
1259 awesome_refresh(globalconf
.connection
);
1262 /** Push a color as a string onto the stack
1263 * \param L The Lua VM state.
1264 * \param c The color to push.
1265 * \return The number of elements pushed on stack.
1268 luaA_pushcolor(lua_State
*L
, const xcolor_t
*c
)
1270 uint8_t r
= (unsigned)c
->red
* 0xff / 0xffff;
1271 uint8_t g
= (unsigned)c
->green
* 0xff / 0xffff;
1272 uint8_t b
= (unsigned)c
->blue
* 0xff / 0xffff;
1273 uint8_t a
= (unsigned)c
->alpha
* 0xff / 0xffff;
1276 /* do not print alpha if it's full */
1278 len
= snprintf(s
, sizeof(s
), "#%02x%02x%02x", r
, g
, b
);
1280 len
= snprintf(s
, sizeof(s
), "#%02x%02x%02x%02x", r
, g
, b
, a
);
1281 lua_pushlstring(L
, s
, len
);