2 * luaa.c - Lua configuration management
4 * Copyright © 2008-2009 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.
30 #include <basedir_fs.h>
33 #include "awesome-version-internal.h"
41 #include "selection.h"
43 #include "common/xcursor.h"
44 #include "common/buffer.h"
47 extern const struct luaL_reg awesome_dbus_lib
[];
49 extern const struct luaL_reg awesome_hooks_lib
[];
50 extern const struct luaL_reg awesome_keygrabber_lib
[];
51 extern const struct luaL_reg awesome_mousegrabber_lib
[];
52 extern const struct luaL_reg awesome_root_lib
[];
53 extern const struct luaL_reg awesome_button_methods
[];
54 extern const struct luaL_reg awesome_button_meta
[];
55 extern const struct luaL_reg awesome_image_methods
[];
56 extern const struct luaL_reg awesome_image_meta
[];
57 extern const struct luaL_reg awesome_mouse_methods
[];
58 extern const struct luaL_reg awesome_mouse_meta
[];
59 extern const struct luaL_reg awesome_screen_methods
[];
60 extern const struct luaL_reg awesome_screen_meta
[];
61 extern const struct luaL_reg awesome_client_methods
[];
62 extern const struct luaL_reg awesome_client_meta
[];
63 extern const struct luaL_reg awesome_tag_methods
[];
64 extern const struct luaL_reg awesome_tag_meta
[];
65 extern const struct luaL_reg awesome_widget_methods
[];
66 extern const struct luaL_reg awesome_widget_meta
[];
67 extern const struct luaL_reg awesome_wibox_methods
[];
68 extern const struct luaL_reg awesome_wibox_meta
[];
69 extern const struct luaL_reg awesome_key_methods
[];
70 extern const struct luaL_reg awesome_key_meta
[];
72 DO_ARRAY(const void *, void, DO_NOTHING
)
75 * \param L The Lua VM state.
76 * \return The number of elements pushed on stack.
79 luaA_quit(lua_State
*L
__attribute__ ((unused
)))
81 ev_unloop(globalconf
.loop
, 1);
85 /** Execute another application, probably a window manager, to replace
87 * \param L The Lua VM state.
88 * \return The number of elements pushed on stack.
90 * \lparam The command line to execute.
93 luaA_exec(lua_State
*L
)
95 const char *cmd
= luaL_checkstring(L
, 1);
106 luaA_restart(lua_State
*L
__attribute__ ((unused
)))
113 luaA_openlib(lua_State
*L
, const char *name
,
114 const struct luaL_reg methods
[],
115 const struct luaL_reg meta
[])
117 luaL_newmetatable(L
, name
); /* 1 */
118 lua_pushvalue(L
, -1); /* dup metatable 2 */
119 lua_setfield(L
, -2, "__index"); /* metatable.__index = metatable 1 */
121 luaL_register(L
, NULL
, meta
); /* 1 */
122 luaL_register(L
, name
, methods
); /* 2 */
123 lua_pushvalue(L
, -1); /* dup self as metatable 3 */
124 lua_setmetatable(L
, -2); /* set self as metatable 2 */
128 /** UTF-8 aware string length computing.
129 * \param L The Lua VM state.
130 * \return The number of elements pushed on stack.
133 luaA_mbstrlen(lua_State
*L
)
135 const char *cmd
= luaL_checkstring(L
, 1);
136 lua_pushnumber(L
, (ssize_t
) mbstowcs(NULL
, NONULL(cmd
), 0));
140 /** Overload standard Lua next function to use __next key on metatable.
141 * \param L The Lua VM state.
142 * \param The number of elements pushed on stack.
145 luaAe_next(lua_State
*L
)
147 if(luaL_getmetafield(L
, 1, "__next"))
150 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
151 return lua_gettop(L
);
154 luaL_checktype(L
, 1, LUA_TTABLE
);
162 /** Overload lua_next() function by using __next metatable field
163 * to get next elements.
164 * \param L The Lua VM stack.
165 * \param idx The index number of elements in stack.
166 * \return 1 if more elements to come, 0 otherwise.
169 luaA_next(lua_State
*L
, int idx
)
171 if(luaL_getmetafield(L
, idx
, "__next"))
173 /* if idx is relative, reduce it since we got __next */
175 /* copy table and then move key */
176 lua_pushvalue(L
, idx
);
177 lua_pushvalue(L
, -3);
179 lua_pcall(L
, 2, 2, 0);
180 /* next returned nil, it's the end */
189 else if(lua_istable(L
, idx
))
190 return lua_next(L
, idx
);
196 /** Generic pairs function.
197 * \param L The Lua VM state.
198 * \return The number of elements pushed on stack.
201 luaA_generic_pairs(lua_State
*L
)
203 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
204 lua_pushvalue(L
, 1); /* state, */
205 lua_pushnil(L
); /* and initial value */
209 /** Overload standard pairs function to use __pairs field of metatables.
210 * \param L The Lua VM state.
211 * \return The number of elements pushed on stack.
214 luaAe_pairs(lua_State
*L
)
216 if(luaL_getmetafield(L
, 1, "__pairs"))
219 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
220 return lua_gettop(L
);
223 luaL_checktype(L
, 1, LUA_TTABLE
);
224 return luaA_generic_pairs(L
);
228 luaA_ipairs_aux(lua_State
*L
)
230 int i
= luaL_checkint(L
, 2) + 1;
231 luaL_checktype(L
, 1, LUA_TTABLE
);
232 lua_pushinteger(L
, i
);
233 lua_rawgeti(L
, 1, i
);
234 return (lua_isnil(L
, -1)) ? 0 : 2;
237 /** Overload standard ipairs function to use __ipairs field of metatables.
238 * \param L The Lua VM state.
239 * \return The number of elements pushed on stack.
242 luaAe_ipairs(lua_State
*L
)
244 if(luaL_getmetafield(L
, 1, "__ipairs"))
247 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
248 return lua_gettop(L
);
251 luaL_checktype(L
, 1, LUA_TTABLE
);
252 lua_pushvalue(L
, lua_upvalueindex(1));
254 lua_pushinteger(L
, 0); /* and initial value */
258 /** Enhanced type() function which recognize awesome objects.
259 * \param L The Lua VM state.
260 * \return The number of arguments pushed on the stack.
263 luaAe_type(lua_State
*L
)
266 #define CHECK_TYPE(type) \
268 if(luaA_toudata(L, 1, #type)) \
270 lua_pushliteral(L, #type); \
282 lua_pushstring(L
, luaL_typename(L
, 1));
286 /** Replace various standards Lua functions with our own.
287 * \param L The Lua VM state.
290 luaA_fixups(lua_State
*L
)
292 /* export string.wlen */
293 lua_getglobal(L
, "string");
294 lua_pushcfunction(L
, luaA_mbstrlen
);
295 lua_setfield(L
, -2, "wlen");
298 lua_pushliteral(L
, "next");
299 lua_pushcfunction(L
, luaAe_next
);
300 lua_settable(L
, LUA_GLOBALSINDEX
);
302 lua_pushliteral(L
, "pairs");
303 lua_pushcfunction(L
, luaAe_next
);
304 lua_pushcclosure(L
, luaAe_pairs
, 1); /* pairs get next as upvalue */
305 lua_settable(L
, LUA_GLOBALSINDEX
);
307 lua_pushliteral(L
, "ipairs");
308 lua_pushcfunction(L
, luaA_ipairs_aux
);
309 lua_pushcclosure(L
, luaAe_ipairs
, 1);
310 lua_settable(L
, LUA_GLOBALSINDEX
);
312 lua_pushliteral(L
, "type");
313 lua_pushcfunction(L
, luaAe_type
);
314 lua_settable(L
, LUA_GLOBALSINDEX
);
316 lua_pushliteral(L
, "selection");
317 lua_pushcfunction(L
, luaA_selection_get
);
318 lua_settable(L
, LUA_GLOBALSINDEX
);
321 /** __next function for wtable objects.
322 * \param L The Lua VM state.
323 * \return The number of elements pushed on stack.
326 luaA_wtable_next(lua_State
*L
)
328 /* upvalue 1 is content table */
329 if(lua_next(L
, lua_upvalueindex(1)))
335 /** __ipairs function for wtable objects.
336 * \param L The Lua VM state.
337 * \return The number of elements pushed on stack.
340 luaA_wtable_ipairs(lua_State
*L
)
342 /* push ipairs_aux */
343 lua_pushvalue(L
, lua_upvalueindex(2));
344 /* push content table */
345 lua_pushvalue(L
, lua_upvalueindex(1));
346 lua_pushinteger(L
, 0); /* and initial value */
350 /** Index function of wtable objects.
351 * \param L The Lua VM state.
352 * \return The number of elements pushed on stack.
355 luaA_wtable_index(lua_State
*L
)
361 /* check for size, waiting lua 5.2 and __len on tables */
362 if((buf
= lua_tolstring(L
, -1, &len
)))
363 if(a_tokenize(buf
, len
) == A_TK_LEN
)
365 lua_pushnumber(L
, lua_objlen(L
, lua_upvalueindex(1)));
370 /* upvalue 1 is content table */
371 lua_rawget(L
, lua_upvalueindex(1));
375 /** Newndex function of wtable objects.
376 * \param L The Lua VM state.
377 * \return The number of elements pushed on stack.
380 luaA_wtable_newindex(lua_State
*L
)
382 bool invalid
= false;
384 /* push key on top */
386 /* get current key value in content table */
387 lua_rawget(L
, lua_upvalueindex(1));
388 /* if value is a widget, notify change */
389 if(lua_istable(L
, -1) || luaA_toudata(L
, -1, "widget"))
392 lua_pop(L
, 1); /* remove value */
394 /* if new value is a widget or a table */
395 if(lua_istable(L
, 3))
397 luaA_table2wtable(L
);
400 else if(!invalid
&& luaA_toudata(L
, 3, "widget"))
403 /* upvalue 1 is content table */
404 lua_rawset(L
, lua_upvalueindex(1));
407 luaA_wibox_invalidate_byitem(L
, lua_topointer(L
, 1));
412 /** Convert the top element of the stack to a proxied wtable.
413 * \param L The Lua VM state.
416 luaA_table2wtable(lua_State
*L
)
418 if(!lua_istable(L
, -1))
421 lua_newtable(L
); /* create *real* content table */
422 lua_createtable(L
, 0, 5); /* metatable */
423 lua_pushvalue(L
, -2); /* copy content table */
424 lua_pushcfunction(L
, luaA_ipairs_aux
); /* push ipairs aux */
425 lua_pushcclosure(L
, luaA_wtable_ipairs
, 2);
426 lua_pushvalue(L
, -3); /* copy content table */
427 lua_pushcclosure(L
, luaA_wtable_next
, 1); /* __next has the content table as upvalue */
428 lua_pushvalue(L
, -4); /* copy content table */
429 lua_pushcclosure(L
, luaA_wtable_index
, 1); /* __index has the content table as upvalue */
430 lua_pushvalue(L
, -5); /* copy content table */
431 lua_pushcclosure(L
, luaA_wtable_newindex
, 1); /* __newindex has the content table as upvalue */
432 /* set metatable field with just pushed closure */
433 lua_setfield(L
, -5, "__newindex");
434 lua_setfield(L
, -4, "__index");
435 lua_setfield(L
, -3, "__next");
436 lua_setfield(L
, -2, "__ipairs");
437 /* set metatable impossible to touch */
438 lua_pushliteral(L
, "wtable");
439 lua_setfield(L
, -2, "__metatable");
440 /* set new metatable on original table */
441 lua_setmetatable(L
, -3);
445 /* go through original table */
446 while(lua_next(L
, -3))
448 /* if convert value to wtable */
449 luaA_table2wtable(L
);
451 lua_pushvalue(L
, -2);
452 /* move key before value */
454 /* set same value in content table */
457 lua_pushvalue(L
, -1);
458 /* push the new value :-> */
460 /* set orig[k] = nil */
463 /* remove content table */
467 /** Look for an item: table, function, etc.
468 * \param L The Lua VM state.
469 * \param item The pointer item.
472 luaA_hasitem(lua_State
*L
, const void *item
)
475 while(luaA_next(L
, -2))
477 if(lua_topointer(L
, -1) == item
)
479 /* remove value and key */
483 if(lua_istable(L
, -1))
484 if(luaA_hasitem(L
, item
))
486 /* remove key and value */
496 /** Browse a table pushed on top of the index, and put all its table and
497 * sub-table into an array.
498 * \param L The Lua VM state.
499 * \param elems The elements array.
500 * \return False if we encounter an elements already in list.
503 luaA_isloop_check(lua_State
*L
, void_array_t
*elems
)
505 if(lua_istable(L
, -1))
507 const void *object
= lua_topointer(L
, -1);
509 /* Check that the object table is not already in the list */
510 for(int i
= 0; i
< elems
->len
; i
++)
511 if(elems
->tab
[i
] == object
)
514 /* push the table in the elements list */
515 void_array_append(elems
, object
);
517 /* look every object in the "table" */
519 while(luaA_next(L
, -2))
521 if(!luaA_isloop_check(L
, elems
))
523 /* remove key and value */
527 /* remove value, keep key for next iteration */
534 /** Check if a table is a loop. When using tables 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 /* elems is an elements array that we will fill with all array we
544 * encounter while browsing the tables */
547 void_array_init(&elems
);
549 /* push table on top */
550 lua_pushvalue(L
, idx
);
552 bool ret
= luaA_isloop_check(L
, &elems
);
554 /* remove pushed table */
557 void_array_wipe(&elems
);
562 /** awesome global table.
563 * \param L The Lua VM state.
564 * \return The number of elements pushed on stack.
566 * \lfield font The default font.
567 * \lfield conffile The configuration file which has been loaded.
570 luaA_awesome_index(lua_State
*L
)
572 if(luaA_usemetatable(L
, 1, 2))
576 const char *buf
= luaL_checklstring(L
, 2, &len
);
578 switch(a_tokenize(buf
, len
))
582 char *font
= pango_font_description_to_string(globalconf
.font
->desc
);
583 lua_pushstring(L
, font
);
588 lua_pushstring(L
, globalconf
.conffile
);
591 luaA_pushxcolor(L
, &globalconf
.colors
.fg
);
594 luaA_pushxcolor(L
, &globalconf
.colors
.bg
);
597 lua_pushliteral(L
, AWESOME_VERSION
);
600 lua_pushliteral(L
, AWESOME_RELEASE
);
609 /** Newindex function for the awesome global table.
610 * \param L The Lua VM state.
611 * \return The number of elements pushed on stack.
614 luaA_awesome_newindex(lua_State
*L
)
616 if(luaA_usemetatable(L
, 1, 2))
620 const char *buf
= luaL_checklstring(L
, 2, &len
);
622 switch(a_tokenize(buf
, len
))
626 const char *newfont
= luaL_checkstring(L
, 3);
627 draw_font_delete(&globalconf
.font
);
628 globalconf
.font
= draw_font_new(newfont
);
629 /* refresh all wiboxes */
630 foreach(screen
, globalconf
.screens
)
631 foreach(wibox
, screen
->wiboxes
)
632 (*wibox
)->need_update
= true;
633 foreach(c
, globalconf
.clients
)
635 (*c
)->titlebar
->need_update
= true;
639 if((buf
= luaL_checklstring(L
, 3, &len
)))
640 xcolor_init_reply(xcolor_init_unchecked(&globalconf
.colors
.fg
, buf
, len
));
643 if((buf
= luaL_checklstring(L
, 3, &len
)))
644 xcolor_init_reply(xcolor_init_unchecked(&globalconf
.colors
.bg
, buf
, len
));
653 /** Initialize the Lua VM
654 * \param xdg An xdg handle to use to get XDG basedir.
657 luaA_init(xdgHandle
* xdg
)
660 static const struct luaL_reg awesome_lib
[] =
662 { "quit", luaA_quit
},
663 { "exec", luaA_exec
},
664 { "spawn", luaA_spawn
},
665 { "restart", luaA_restart
},
666 { "__index", luaA_awesome_index
},
667 { "__newindex", luaA_awesome_newindex
},
671 L
= globalconf
.L
= luaL_newstate();
677 /* Export awesome lib */
678 luaA_openlib(L
, "awesome", awesome_lib
, awesome_lib
);
680 /* Export root lib */
681 luaL_register(L
, "root", awesome_root_lib
);
682 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
684 /* Export hooks lib */
685 luaL_register(L
, "hooks", awesome_hooks_lib
);
686 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
689 /* Export D-Bus lib */
690 luaL_register(L
, "dbus", awesome_dbus_lib
);
691 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
694 /* Export keygrabber lib */
695 luaL_register(L
, "keygrabber", awesome_keygrabber_lib
);
696 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
698 /* Export mousegrabber lib */
699 luaL_register(L
, "mousegrabber", awesome_mousegrabber_lib
);
700 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
703 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
706 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
709 luaA_openlib(L
, "button", awesome_button_methods
, awesome_button_meta
);
712 luaA_openlib(L
, "image", awesome_image_methods
, awesome_image_meta
);
715 luaA_openlib(L
, "tag", awesome_tag_methods
, awesome_tag_meta
);
718 luaA_openlib(L
, "wibox", awesome_wibox_methods
, awesome_wibox_meta
);
721 luaA_openlib(L
, "widget", awesome_widget_methods
, awesome_widget_meta
);
724 luaA_openlib(L
, "client", awesome_client_methods
, awesome_client_meta
);
727 luaA_openlib(L
, "key", awesome_key_methods
, awesome_key_meta
);
730 globalconf
.hooks
.manage
= LUA_REFNIL
;
731 globalconf
.hooks
.unmanage
= LUA_REFNIL
;
732 globalconf
.hooks
.focus
= LUA_REFNIL
;
733 globalconf
.hooks
.unfocus
= LUA_REFNIL
;
734 globalconf
.hooks
.mouse_enter
= LUA_REFNIL
;
735 globalconf
.hooks
.mouse_leave
= LUA_REFNIL
;
736 globalconf
.hooks
.arrange
= LUA_REFNIL
;
737 globalconf
.hooks
.clients
= LUA_REFNIL
;
738 globalconf
.hooks
.tags
= LUA_REFNIL
;
739 globalconf
.hooks
.tagged
= LUA_REFNIL
;
740 globalconf
.hooks
.property
= LUA_REFNIL
;
741 globalconf
.hooks
.startup_notification
= LUA_REFNIL
;
742 globalconf
.hooks
.timer
= LUA_REFNIL
;
744 globalconf
.hooks
.dbus
= LUA_REFNIL
;
747 /* add Lua lib path (/usr/share/awesome/lib by default) */
748 lua_getglobal(L
, "package");
749 if (LUA_TTABLE
!= lua_type(L
, 1))
751 warn("package is not a table");
754 lua_getfield(L
, 1, "path");
755 if (LUA_TSTRING
!= lua_type(L
, 2))
757 warn("package.path is not a string");
761 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?.lua");
762 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?/init.lua");
763 lua_concat(L
, 3); /* concatenate with package.path */
765 /* add XDG_CONFIG_DIR as include path */
766 const char * const *xdgconfigdirs
= xdgSearchableConfigDirectories(xdg
);
767 for(; *xdgconfigdirs
; xdgconfigdirs
++)
769 size_t len
= a_strlen(*xdgconfigdirs
);
770 lua_pushliteral(L
, ";");
771 lua_pushlstring(L
, *xdgconfigdirs
, len
);
772 lua_pushliteral(L
, "/awesome/?.lua");
775 lua_pushliteral(L
, ";");
776 lua_pushlstring(L
, *xdgconfigdirs
, len
);
777 lua_pushliteral(L
, "/awesome/?/init.lua");
780 lua_concat(L
, 3); /* concatenate with package.path */
782 lua_setfield(L
, 1, "path"); /* package.path = "concatenated string" */
786 luaA_loadrc(const char *confpath
, bool run
)
788 if(!luaL_loadfile(globalconf
.L
, confpath
))
792 if(lua_pcall(globalconf
.L
, 0, LUA_MULTRET
, 0))
793 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
796 globalconf
.conffile
= a_strdup(confpath
);
801 lua_pop(globalconf
.L
, 1);
805 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
810 /** Load a configuration file.
811 * \param xdg An xdg handle to use to get XDG basedir.
812 * \param confpatharg The configuration file to load.
813 * \param run Run the configuration file.
816 luaA_parserc(xdgHandle
* xdg
, const char *confpatharg
, bool run
)
818 char *confpath
= NULL
;
821 /* try to load, return if it's ok */
824 if(luaA_loadrc(confpatharg
, run
))
833 confpath
= xdgConfigFind("awesome/rc.lua", xdg
);
835 char *tmp
= confpath
;
837 /* confpath is "string1\0string2\0string3\0\0" */
840 if(luaA_loadrc(tmp
, run
))
847 tmp
+= a_strlen(tmp
) + 1;
858 luaA_on_timer(EV_P_ ev_timer
*w
, int revents
)
860 if(globalconf
.hooks
.timer
!= LUA_REFNIL
)
861 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.timer
, 0, 0);
865 /** Push a color as a string onto the stack
866 * \param L The Lua VM state.
867 * \param c The color to push.
868 * \return The number of elements pushed on stack.
871 luaA_pushxcolor(lua_State
*L
, const xcolor_t
*c
)
873 uint8_t r
= (unsigned)c
->red
* 0xff / 0xffff;
874 uint8_t g
= (unsigned)c
->green
* 0xff / 0xffff;
875 uint8_t b
= (unsigned)c
->blue
* 0xff / 0xffff;
876 uint8_t a
= (unsigned)c
->alpha
* 0xff / 0xffff;
879 /* do not print alpha if it's full */
881 len
= snprintf(s
, sizeof(s
), "#%02x%02x%02x", r
, g
, b
);
883 len
= snprintf(s
, sizeof(s
), "#%02x%02x%02x%02x", r
, g
, b
, a
);
884 lua_pushlstring(L
, s
, len
);
888 /** Push a color as a string onto the stack
889 * \param L The Lua VM state.
890 * \param c The color to push.
891 * \return The number of elements pushed on stack.
894 luaA_pushcolor(lua_State
*L
, const color_t
*c
)
897 uint8_t g
= c
->green
;
899 uint8_t a
= c
->alpha
;
902 /* do not print alpha if it's full */
904 len
= snprintf(s
, sizeof(s
), "#%02x%02x%02x", r
, g
, b
);
906 len
= snprintf(s
, sizeof(s
), "#%02x%02x%02x%02x", r
, g
, b
, a
);
907 lua_pushlstring(L
, s
, len
);
911 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80