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>
34 #include "awesome-version-internal.h"
42 #include "selection.h"
44 #include "common/xcursor.h"
45 #include "common/buffer.h"
48 extern const struct luaL_reg awesome_dbus_lib
[];
50 extern const struct luaL_reg awesome_hooks_lib
[];
51 extern const struct luaL_reg awesome_keygrabber_lib
[];
52 extern const struct luaL_reg awesome_mousegrabber_lib
[];
53 extern const struct luaL_reg awesome_root_lib
[];
54 extern const struct luaL_reg awesome_mouse_methods
[];
55 extern const struct luaL_reg awesome_mouse_meta
[];
56 extern const struct luaL_reg awesome_screen_methods
[];
57 extern const struct luaL_reg awesome_screen_meta
[];
60 * \param L The Lua VM state.
61 * \return The number of elements pushed on stack.
64 luaA_quit(lua_State
*L
__attribute__ ((unused
)))
66 ev_unloop(globalconf
.loop
, 1);
70 /** Execute another application, probably a window manager, to replace
72 * \param L The Lua VM state.
73 * \return The number of elements pushed on stack.
75 * \lparam The command line to execute.
78 luaA_exec(lua_State
*L
)
80 const char *cmd
= luaL_checkstring(L
, 1);
91 luaA_restart(lua_State
*L
__attribute__ ((unused
)))
97 /** UTF-8 aware string length computing.
98 * \param L The Lua VM state.
99 * \return The number of elements pushed on stack.
102 luaA_mbstrlen(lua_State
*L
)
104 const char *cmd
= luaL_checkstring(L
, 1);
105 lua_pushnumber(L
, (ssize_t
) mbstowcs(NULL
, NONULL(cmd
), 0));
109 /** Overload standard Lua next function to use __next key on metatable.
110 * \param L The Lua VM state.
111 * \return The number of elements pushed on stack.
114 luaAe_next(lua_State
*L
)
116 if(luaL_getmetafield(L
, 1, "__next"))
119 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
120 return lua_gettop(L
);
123 luaL_checktype(L
, 1, LUA_TTABLE
);
131 /** Overload lua_next() function by using __next metatable field
132 * to get next elements.
133 * \param L The Lua VM stack.
134 * \param idx The index number of elements in stack.
135 * \return 1 if more elements to come, 0 otherwise.
138 luaA_next(lua_State
*L
, int idx
)
140 if(luaL_getmetafield(L
, idx
, "__next"))
142 /* if idx is relative, reduce it since we got __next */
144 /* copy table and then move key */
145 lua_pushvalue(L
, idx
);
146 lua_pushvalue(L
, -3);
148 lua_pcall(L
, 2, 2, 0);
149 /* next returned nil, it's the end */
158 else if(lua_istable(L
, idx
))
159 return lua_next(L
, idx
);
165 /** Generic pairs function.
166 * \param L The Lua VM state.
167 * \return The number of elements pushed on stack.
170 luaA_generic_pairs(lua_State
*L
)
172 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
173 lua_pushvalue(L
, 1); /* state, */
174 lua_pushnil(L
); /* and initial value */
178 /** Overload standard pairs function to use __pairs field of metatables.
179 * \param L The Lua VM state.
180 * \return The number of elements pushed on stack.
183 luaAe_pairs(lua_State
*L
)
185 if(luaL_getmetafield(L
, 1, "__pairs"))
188 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
189 return lua_gettop(L
);
192 luaL_checktype(L
, 1, LUA_TTABLE
);
193 return luaA_generic_pairs(L
);
197 luaA_ipairs_aux(lua_State
*L
)
199 int i
= luaL_checkint(L
, 2) + 1;
200 luaL_checktype(L
, 1, LUA_TTABLE
);
201 lua_pushinteger(L
, i
);
202 lua_rawgeti(L
, 1, i
);
203 return (lua_isnil(L
, -1)) ? 0 : 2;
206 /** Overload standard ipairs function to use __ipairs field of metatables.
207 * \param L The Lua VM state.
208 * \return The number of elements pushed on stack.
211 luaAe_ipairs(lua_State
*L
)
213 if(luaL_getmetafield(L
, 1, "__ipairs"))
216 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
217 return lua_gettop(L
);
220 luaL_checktype(L
, 1, LUA_TTABLE
);
221 lua_pushvalue(L
, lua_upvalueindex(1));
223 lua_pushinteger(L
, 0); /* and initial value */
227 /** Enhanced type() function which recognize awesome objects.
228 * \param L The Lua VM state.
229 * \return The number of arguments pushed on the stack.
232 luaAe_type(lua_State
*L
)
235 lua_pushstring(L
, luaA_typename(L
, 1));
239 /** Modified version of os.execute() which use spawn code to reset signal
240 * handlers correctly before exec()ing the new program.
241 * \param L The Lua VM state.
242 * \return The number of arguments pushed on stack.
245 luaAe_os_execute(lua_State
*L
)
247 lua_pushinteger(L
, spawn_system(luaL_optstring(L
, 1, NULL
)));
251 /** Replace various standards Lua functions with our own.
252 * \param L The Lua VM state.
255 luaA_fixups(lua_State
*L
)
257 /* export string.wlen */
258 lua_getglobal(L
, "string");
259 lua_pushcfunction(L
, luaA_mbstrlen
);
260 lua_setfield(L
, -2, "wlen");
262 /* replace os.execute */
263 lua_getglobal(L
, "os");
264 lua_pushcfunction(L
, luaAe_os_execute
);
265 lua_setfield(L
, -2, "execute");
268 lua_pushliteral(L
, "next");
269 lua_pushcfunction(L
, luaAe_next
);
270 lua_settable(L
, LUA_GLOBALSINDEX
);
272 lua_pushliteral(L
, "pairs");
273 lua_pushcfunction(L
, luaAe_next
);
274 lua_pushcclosure(L
, luaAe_pairs
, 1); /* pairs get next as upvalue */
275 lua_settable(L
, LUA_GLOBALSINDEX
);
277 lua_pushliteral(L
, "ipairs");
278 lua_pushcfunction(L
, luaA_ipairs_aux
);
279 lua_pushcclosure(L
, luaAe_ipairs
, 1);
280 lua_settable(L
, LUA_GLOBALSINDEX
);
282 lua_pushliteral(L
, "type");
283 lua_pushcfunction(L
, luaAe_type
);
284 lua_settable(L
, LUA_GLOBALSINDEX
);
286 lua_pushliteral(L
, "selection");
287 lua_pushcfunction(L
, luaA_selection_get
);
288 lua_settable(L
, LUA_GLOBALSINDEX
);
291 /** __next function for wtable objects.
292 * \param L The Lua VM state.
293 * \return The number of elements pushed on stack.
296 luaA_wtable_next(lua_State
*L
)
298 /* upvalue 1 is content table */
299 if(lua_next(L
, lua_upvalueindex(1)))
305 /** __ipairs function for wtable objects.
306 * \param L The Lua VM state.
307 * \return The number of elements pushed on stack.
310 luaA_wtable_ipairs(lua_State
*L
)
312 /* push ipairs_aux */
313 lua_pushvalue(L
, lua_upvalueindex(2));
314 /* push content table */
315 lua_pushvalue(L
, lua_upvalueindex(1));
316 lua_pushinteger(L
, 0); /* and initial value */
320 /** Index function of wtable objects.
321 * \param L The Lua VM state.
322 * \return The number of elements pushed on stack.
325 luaA_wtable_index(lua_State
*L
)
331 /* check for size, waiting lua 5.2 and __len on tables */
332 if((buf
= lua_tolstring(L
, -1, &len
)))
333 if(a_tokenize(buf
, len
) == A_TK_LEN
)
335 lua_pushnumber(L
, lua_objlen(L
, lua_upvalueindex(1)));
340 /* upvalue 1 is content table */
341 lua_rawget(L
, lua_upvalueindex(1));
345 /** Newndex function of wtable objects.
346 * \param L The Lua VM state.
347 * \return The number of elements pushed on stack.
350 luaA_wtable_newindex(lua_State
*L
)
352 bool invalid
= false;
354 /* push key on top */
356 /* get current key value in content table */
357 lua_rawget(L
, lua_upvalueindex(1));
358 /* if value is a widget, notify change */
359 if(lua_istable(L
, -1) || luaA_toudata(L
, -1, &widget_class
))
362 lua_pop(L
, 1); /* remove value */
364 /* if new value is a widget or a table */
365 if(lua_istable(L
, 3))
367 luaA_table2wtable(L
);
370 else if(!invalid
&& luaA_toudata(L
, 3, &widget_class
))
373 /* upvalue 1 is content table */
374 lua_rawset(L
, lua_upvalueindex(1));
377 luaA_wibox_invalidate_byitem(L
, lua_topointer(L
, 1));
382 /** Convert the top element of the stack to a proxied wtable.
383 * \param L The Lua VM state.
386 luaA_table2wtable(lua_State
*L
)
388 if(!lua_istable(L
, -1))
391 lua_newtable(L
); /* create *real* content table */
392 lua_createtable(L
, 0, 5); /* metatable */
393 lua_pushvalue(L
, -2); /* copy content table */
394 lua_pushcfunction(L
, luaA_ipairs_aux
); /* push ipairs aux */
395 lua_pushcclosure(L
, luaA_wtable_ipairs
, 2);
396 lua_pushvalue(L
, -3); /* copy content table */
397 lua_pushcclosure(L
, luaA_wtable_next
, 1); /* __next has the content table as upvalue */
398 lua_pushvalue(L
, -4); /* copy content table */
399 lua_pushcclosure(L
, luaA_wtable_index
, 1); /* __index has the content table as upvalue */
400 lua_pushvalue(L
, -5); /* copy content table */
401 lua_pushcclosure(L
, luaA_wtable_newindex
, 1); /* __newindex has the content table as upvalue */
402 /* set metatable field with just pushed closure */
403 lua_setfield(L
, -5, "__newindex");
404 lua_setfield(L
, -4, "__index");
405 lua_setfield(L
, -3, "__next");
406 lua_setfield(L
, -2, "__ipairs");
407 /* set metatable impossible to touch */
408 lua_pushliteral(L
, "wtable");
409 lua_setfield(L
, -2, "__metatable");
410 /* set new metatable on original table */
411 lua_setmetatable(L
, -3);
415 /* go through original table */
416 while(lua_next(L
, -3))
418 /* if convert value to wtable */
419 luaA_table2wtable(L
);
421 lua_pushvalue(L
, -2);
422 /* move key before value */
424 /* set same value in content table */
427 lua_pushvalue(L
, -1);
428 /* push the new value :-> */
430 /* set orig[k] = nil */
433 /* remove content table */
437 /** Look for an item: table, function, etc.
438 * \param L The Lua VM state.
439 * \param item The pointer item.
442 luaA_hasitem(lua_State
*L
, const void *item
)
445 while(luaA_next(L
, -2))
447 if(lua_topointer(L
, -1) == item
)
449 /* remove value and key */
453 if(lua_istable(L
, -1))
454 if(luaA_hasitem(L
, item
))
456 /* remove key and value */
466 /** Browse a table pushed on top of the index, and put all its table and
467 * sub-table into an array.
468 * \param L The Lua VM state.
469 * \param elems The elements array.
470 * \return False if we encounter an elements already in list.
473 luaA_isloop_check(lua_State
*L
, cptr_array_t
*elems
)
475 if(lua_istable(L
, -1))
477 const void *object
= lua_topointer(L
, -1);
479 /* Check that the object table is not already in the list */
480 for(int i
= 0; i
< elems
->len
; i
++)
481 if(elems
->tab
[i
] == object
)
484 /* push the table in the elements list */
485 cptr_array_append(elems
, object
);
487 /* look every object in the "table" */
489 while(luaA_next(L
, -2))
491 if(!luaA_isloop_check(L
, elems
))
493 /* remove key and value */
497 /* remove value, keep key for next iteration */
504 /** Check if a table is a loop. When using tables as direct acyclic digram,
506 * \param L The Lua VM state.
507 * \param idx The index of the table in the stack
508 * \return True if the table loops.
511 luaA_isloop(lua_State
*L
, int idx
)
513 /* elems is an elements array that we will fill with all array we
514 * encounter while browsing the tables */
517 cptr_array_init(&elems
);
519 /* push table on top */
520 lua_pushvalue(L
, idx
);
522 bool ret
= luaA_isloop_check(L
, &elems
);
524 /* remove pushed table */
527 cptr_array_wipe(&elems
);
532 /** awesome global table.
533 * \param L The Lua VM state.
534 * \return The number of elements pushed on stack.
536 * \lfield font The default font.
537 * \lfield font_height The default font height.
538 * \lfield conffile The configuration file which has been loaded.
541 luaA_awesome_index(lua_State
*L
)
543 if(luaA_usemetatable(L
, 1, 2))
547 const char *buf
= luaL_checklstring(L
, 2, &len
);
549 switch(a_tokenize(buf
, len
))
553 char *font
= pango_font_description_to_string(globalconf
.font
->desc
);
554 lua_pushstring(L
, font
);
558 case A_TK_FONT_HEIGHT
:
559 lua_pushnumber(L
, globalconf
.font
->height
);
562 lua_pushstring(L
, globalconf
.conffile
);
565 luaA_pushxcolor(L
, globalconf
.colors
.fg
);
568 luaA_pushxcolor(L
, globalconf
.colors
.bg
);
571 lua_pushliteral(L
, AWESOME_VERSION
);
574 lua_pushliteral(L
, AWESOME_RELEASE
);
583 /** Newindex function for the awesome global table.
584 * \param L The Lua VM state.
585 * \return The number of elements pushed on stack.
588 luaA_awesome_newindex(lua_State
*L
)
590 if(luaA_usemetatable(L
, 1, 2))
594 const char *buf
= luaL_checklstring(L
, 2, &len
);
596 switch(a_tokenize(buf
, len
))
600 const char *newfont
= luaL_checkstring(L
, 3);
601 draw_font_delete(&globalconf
.font
);
602 globalconf
.font
= draw_font_new(newfont
);
603 /* refresh all wiboxes */
604 foreach(wibox
, globalconf
.wiboxes
)
605 (*wibox
)->need_update
= true;
606 foreach(c
, globalconf
.clients
)
608 (*c
)->titlebar
->need_update
= true;
612 if((buf
= luaL_checklstring(L
, 3, &len
)))
613 xcolor_init_reply(xcolor_init_unchecked(&globalconf
.colors
.fg
, buf
, len
));
616 if((buf
= luaL_checklstring(L
, 3, &len
)))
617 xcolor_init_reply(xcolor_init_unchecked(&globalconf
.colors
.bg
, buf
, len
));
626 /** Add a global signal.
627 * \param L The Lua VM state.
628 * \return The number of elements pushed on stack.
630 * \lparam A string with the event name.
631 * \lparam The function to call.
634 luaA_awesome_add_signal(lua_State
*L
)
636 const char *name
= luaL_checkstring(L
, 1);
637 luaA_checkfunction(L
, 2);
638 signal_add(&global_signals
, name
, luaA_object_ref(L
, 2));
642 /** Remove a global signal.
643 * \param L The Lua VM state.
644 * \return The number of elements pushed on stack.
646 * \lparam A string with the event name.
647 * \lparam The function to call.
650 luaA_awesome_remove_signal(lua_State
*L
)
652 const char *name
= luaL_checkstring(L
, 1);
653 luaA_checkfunction(L
, 2);
654 const void *func
= lua_topointer(L
, 2);
655 signal_remove(&global_signals
, name
, func
);
656 luaA_object_unref(L
, (void *) func
);
660 /** Emit a global signal.
661 * \param L The Lua VM state.
662 * \return The number of elements pushed on stack.
664 * \lparam A string with the event name.
665 * \lparam The function to call.
668 luaA_awesome_emit_signal(lua_State
*L
)
670 signal_object_emit(L
, &global_signals
, luaL_checkstring(L
, 1), lua_gettop(L
) - 1);
675 luaA_panic(lua_State
*L
)
677 warn("unprotected error in call to Lua API (%s), restarting awesome",
678 lua_tostring(L
, -1));
684 luaA_dofunction_on_error(lua_State
*L
)
686 /* duplicate string error */
687 lua_pushvalue(L
, -1);
688 /* emit error signal */
689 signal_object_emit(L
, &global_signals
, "debug::error", 1);
691 if(!luaL_dostring(L
, "return debug.traceback(\"error while running function\", 3)"))
693 /* Move traceback before error */
695 /* Insert sentence */
696 lua_pushliteral(L
, "\nerror: ");
697 /* Move it before error */
704 /** Initialize the Lua VM
705 * \param xdg An xdg handle to use to get XDG basedir.
708 luaA_init(xdgHandle
* xdg
)
711 static const struct luaL_reg awesome_lib
[] =
713 { "quit", luaA_quit
},
714 { "exec", luaA_exec
},
715 { "spawn", luaA_spawn
},
716 { "restart", luaA_restart
},
717 { "add_signal", luaA_awesome_add_signal
},
718 { "remove_signal", luaA_awesome_remove_signal
},
719 { "emit_signal", luaA_awesome_emit_signal
},
720 { "__index", luaA_awesome_index
},
721 { "__newindex", luaA_awesome_newindex
},
725 L
= globalconf
.L
= luaL_newstate();
727 /* Set panic function */
728 lua_atpanic(L
, luaA_panic
);
730 /* Set error handling function */
731 lualib_dofunction_on_error
= luaA_dofunction_on_error
;
737 luaA_object_setup(L
);
739 /* Export awesome lib */
740 luaA_openlib(L
, "awesome", awesome_lib
, awesome_lib
);
742 /* Export root lib */
743 luaL_register(L
, "root", awesome_root_lib
);
744 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
746 /* Export hooks lib */
747 luaL_register(L
, "hooks", awesome_hooks_lib
);
748 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
751 /* Export D-Bus lib */
752 luaL_register(L
, "dbus", awesome_dbus_lib
);
753 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
756 /* Export keygrabber lib */
757 luaL_register(L
, "keygrabber", awesome_keygrabber_lib
);
758 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
760 /* Export mousegrabber lib */
761 luaL_register(L
, "mousegrabber", awesome_mousegrabber_lib
);
762 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
765 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
768 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
771 button_class_setup(L
);
774 image_class_setup(L
);
780 wibox_class_setup(L
);
783 widget_class_setup(L
);
786 client_class_setup(L
);
792 timer_class_setup(L
);
795 globalconf
.hooks
.manage
= LUA_REFNIL
;
796 globalconf
.hooks
.unmanage
= LUA_REFNIL
;
797 globalconf
.hooks
.focus
= LUA_REFNIL
;
798 globalconf
.hooks
.unfocus
= LUA_REFNIL
;
799 globalconf
.hooks
.mouse_enter
= LUA_REFNIL
;
800 globalconf
.hooks
.mouse_leave
= LUA_REFNIL
;
801 globalconf
.hooks
.clients
= LUA_REFNIL
;
802 globalconf
.hooks
.tags
= LUA_REFNIL
;
803 globalconf
.hooks
.tagged
= LUA_REFNIL
;
804 globalconf
.hooks
.property
= LUA_REFNIL
;
805 globalconf
.hooks
.timer
= LUA_REFNIL
;
806 globalconf
.hooks
.exit
= LUA_REFNIL
;
808 /* add Lua lib path (/usr/share/awesome/lib by default) */
809 lua_getglobal(L
, "package");
810 if (LUA_TTABLE
!= lua_type(L
, 1))
812 warn("package is not a table");
815 lua_getfield(L
, 1, "path");
816 if (LUA_TSTRING
!= lua_type(L
, 2))
818 warn("package.path is not a string");
822 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?.lua");
823 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?/init.lua");
824 lua_concat(L
, 3); /* concatenate with package.path */
826 /* add XDG_CONFIG_DIR as include path */
827 const char * const *xdgconfigdirs
= xdgSearchableConfigDirectories(xdg
);
828 for(; *xdgconfigdirs
; xdgconfigdirs
++)
830 size_t len
= a_strlen(*xdgconfigdirs
);
831 lua_pushliteral(L
, ";");
832 lua_pushlstring(L
, *xdgconfigdirs
, len
);
833 lua_pushliteral(L
, "/awesome/?.lua");
836 lua_pushliteral(L
, ";");
837 lua_pushlstring(L
, *xdgconfigdirs
, len
);
838 lua_pushliteral(L
, "/awesome/?/init.lua");
841 lua_concat(L
, 3); /* concatenate with package.path */
843 lua_setfield(L
, 1, "path"); /* package.path = "concatenated string" */
847 luaA_loadrc(const char *confpath
, bool run
)
849 if(!luaL_loadfile(globalconf
.L
, confpath
))
853 if(lua_pcall(globalconf
.L
, 0, LUA_MULTRET
, 0))
854 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
857 globalconf
.conffile
= a_strdup(confpath
);
862 lua_pop(globalconf
.L
, 1);
866 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
871 /** Load a configuration file.
872 * \param xdg An xdg handle to use to get XDG basedir.
873 * \param confpatharg The configuration file to load.
874 * \param run Run the configuration file.
877 luaA_parserc(xdgHandle
* xdg
, const char *confpatharg
, bool run
)
879 char *confpath
= NULL
;
882 /* try to load, return if it's ok */
885 if(luaA_loadrc(confpatharg
, run
))
894 confpath
= xdgConfigFind("awesome/rc.lua", xdg
);
896 char *tmp
= confpath
;
898 /* confpath is "string1\0string2\0string3\0\0" */
901 if(luaA_loadrc(tmp
, run
))
908 tmp
+= a_strlen(tmp
) + 1;
919 luaA_on_timer(EV_P_ ev_timer
*w
, int revents
)
921 if(globalconf
.hooks
.timer
!= LUA_REFNIL
)
922 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.timer
, 0, 0);
926 luaA_class_index_miss_property(lua_State
*L
, lua_object_t
*obj
)
928 signal_object_emit(L
, &global_signals
, "debug::index::miss", 2);
933 luaA_class_newindex_miss_property(lua_State
*L
, lua_object_t
*obj
)
935 signal_object_emit(L
, &global_signals
, "debug::newindex::miss", 3);
939 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80