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>
35 #include "awesome-version-internal.h"
43 #include "selection.h"
45 #include "common/xcursor.h"
46 #include "common/buffer.h"
49 extern const struct luaL_reg awesome_dbus_lib
[];
51 extern const struct luaL_reg awesome_hooks_lib
[];
52 extern const struct luaL_reg awesome_keygrabber_lib
[];
53 extern const struct luaL_reg awesome_mousegrabber_lib
[];
54 extern const struct luaL_reg awesome_root_lib
[];
55 extern const struct luaL_reg awesome_mouse_methods
[];
56 extern const struct luaL_reg awesome_mouse_meta
[];
57 extern const struct luaL_reg awesome_screen_methods
[];
58 extern const struct luaL_reg awesome_screen_meta
[];
61 * \param L The Lua VM state.
62 * \return The number of elements pushed on stack.
65 luaA_quit(lua_State
*L
__attribute__ ((unused
)))
67 ev_unloop(globalconf
.loop
, 1);
71 /** Execute another application, probably a window manager, to replace
73 * \param L The Lua VM state.
74 * \return The number of elements pushed on stack.
76 * \lparam The command line to execute.
79 luaA_exec(lua_State
*L
)
81 const char *cmd
= luaL_checkstring(L
, 1);
92 luaA_restart(lua_State
*L
__attribute__ ((unused
)))
98 /** UTF-8 aware string length computing.
99 * \param L The Lua VM state.
100 * \return The number of elements pushed on stack.
103 luaA_mbstrlen(lua_State
*L
)
105 const char *cmd
= luaL_checkstring(L
, 1);
106 lua_pushnumber(L
, (ssize_t
) mbstowcs(NULL
, NONULL(cmd
), 0));
110 /** Overload standard Lua next function to use __next key on metatable.
111 * \param L The Lua VM state.
112 * \return The number of elements pushed on stack.
115 luaAe_next(lua_State
*L
)
117 if(luaL_getmetafield(L
, 1, "__next"))
120 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
121 return lua_gettop(L
);
124 luaL_checktype(L
, 1, LUA_TTABLE
);
132 /** Overload lua_next() function by using __next metatable field
133 * to get next elements.
134 * \param L The Lua VM stack.
135 * \param idx The index number of elements in stack.
136 * \return 1 if more elements to come, 0 otherwise.
139 luaA_next(lua_State
*L
, int idx
)
141 if(luaL_getmetafield(L
, idx
, "__next"))
143 /* if idx is relative, reduce it since we got __next */
145 /* copy table and then move key */
146 lua_pushvalue(L
, idx
);
147 lua_pushvalue(L
, -3);
149 lua_pcall(L
, 2, 2, 0);
150 /* next returned nil, it's the end */
159 else if(lua_istable(L
, idx
))
160 return lua_next(L
, idx
);
166 /** Generic pairs function.
167 * \param L The Lua VM state.
168 * \return The number of elements pushed on stack.
171 luaA_generic_pairs(lua_State
*L
)
173 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
174 lua_pushvalue(L
, 1); /* state, */
175 lua_pushnil(L
); /* and initial value */
179 /** Overload standard pairs function to use __pairs field of metatables.
180 * \param L The Lua VM state.
181 * \return The number of elements pushed on stack.
184 luaAe_pairs(lua_State
*L
)
186 if(luaL_getmetafield(L
, 1, "__pairs"))
189 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
190 return lua_gettop(L
);
193 luaL_checktype(L
, 1, LUA_TTABLE
);
194 return luaA_generic_pairs(L
);
198 luaA_ipairs_aux(lua_State
*L
)
200 int i
= luaL_checkint(L
, 2) + 1;
201 luaL_checktype(L
, 1, LUA_TTABLE
);
202 lua_pushinteger(L
, i
);
203 lua_rawgeti(L
, 1, i
);
204 return (lua_isnil(L
, -1)) ? 0 : 2;
207 /** Overload standard ipairs function to use __ipairs field of metatables.
208 * \param L The Lua VM state.
209 * \return The number of elements pushed on stack.
212 luaAe_ipairs(lua_State
*L
)
214 if(luaL_getmetafield(L
, 1, "__ipairs"))
217 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
218 return lua_gettop(L
);
221 luaL_checktype(L
, 1, LUA_TTABLE
);
222 lua_pushvalue(L
, lua_upvalueindex(1));
224 lua_pushinteger(L
, 0); /* and initial value */
228 /** Enhanced type() function which recognize awesome objects.
229 * \param L The Lua VM state.
230 * \return The number of arguments pushed on the stack.
233 luaAe_type(lua_State
*L
)
236 lua_pushstring(L
, luaA_typename(L
, 1));
240 /** Modified version of os.execute() which use spawn code to reset signal
241 * handlers correctly before exec()'ing the new program.
242 * \param L The Lua VM state.
243 * \return The number of arguments pushed on stack.
246 luaAe_os_execute(lua_State
*L
)
248 lua_pushinteger(L
, spawn_system(luaL_optstring(L
, 1, NULL
)));
252 /** Replace various standards Lua functions with our own.
253 * \param L The Lua VM state.
256 luaA_fixups(lua_State
*L
)
258 /* export string.wlen */
259 lua_getglobal(L
, "string");
260 lua_pushcfunction(L
, luaA_mbstrlen
);
261 lua_setfield(L
, -2, "wlen");
263 /* replace os.execute */
264 lua_getglobal(L
, "os");
265 lua_pushcfunction(L
, luaAe_os_execute
);
266 lua_setfield(L
, -2, "execute");
269 lua_pushliteral(L
, "next");
270 lua_pushcfunction(L
, luaAe_next
);
271 lua_settable(L
, LUA_GLOBALSINDEX
);
273 lua_pushliteral(L
, "pairs");
274 lua_pushcfunction(L
, luaAe_next
);
275 lua_pushcclosure(L
, luaAe_pairs
, 1); /* pairs get next as upvalue */
276 lua_settable(L
, LUA_GLOBALSINDEX
);
278 lua_pushliteral(L
, "ipairs");
279 lua_pushcfunction(L
, luaA_ipairs_aux
);
280 lua_pushcclosure(L
, luaAe_ipairs
, 1);
281 lua_settable(L
, LUA_GLOBALSINDEX
);
283 lua_pushliteral(L
, "type");
284 lua_pushcfunction(L
, luaAe_type
);
285 lua_settable(L
, LUA_GLOBALSINDEX
);
287 lua_pushliteral(L
, "selection");
288 lua_pushcfunction(L
, luaA_selection_get
);
289 lua_settable(L
, LUA_GLOBALSINDEX
);
292 /** __next function for wtable objects.
293 * \param L The Lua VM state.
294 * \return The number of elements pushed on stack.
297 luaA_wtable_next(lua_State
*L
)
299 /* upvalue 1 is content table */
300 if(lua_next(L
, lua_upvalueindex(1)))
306 /** __ipairs function for wtable objects.
307 * \param L The Lua VM state.
308 * \return The number of elements pushed on stack.
311 luaA_wtable_ipairs(lua_State
*L
)
313 /* push ipairs_aux */
314 lua_pushvalue(L
, lua_upvalueindex(2));
315 /* push content table */
316 lua_pushvalue(L
, lua_upvalueindex(1));
317 lua_pushinteger(L
, 0); /* and initial value */
321 /** Index function of wtable objects.
322 * \param L The Lua VM state.
323 * \return The number of elements pushed on stack.
326 luaA_wtable_index(lua_State
*L
)
332 /* check for size, waiting lua 5.2 and __len on tables */
333 if((buf
= lua_tolstring(L
, -1, &len
)))
334 if(a_tokenize(buf
, len
) == A_TK_LEN
)
336 lua_pushnumber(L
, lua_objlen(L
, lua_upvalueindex(1)));
341 /* upvalue 1 is content table */
342 lua_rawget(L
, lua_upvalueindex(1));
346 /** Newindex function of wtable objects.
347 * \param L The Lua VM state.
348 * \return The number of elements pushed on stack.
351 luaA_wtable_newindex(lua_State
*L
)
353 bool invalid
= false;
355 /* push key on top */
357 /* get current key value in content table */
358 lua_rawget(L
, lua_upvalueindex(1));
359 /* if value is a widget, notify change */
360 if(lua_istable(L
, -1) || luaA_toudata(L
, -1, &widget_class
))
363 lua_pop(L
, 1); /* remove value */
365 /* if new value is a widget or a table */
366 if(lua_istable(L
, 3))
368 luaA_table2wtable(L
);
371 else if(!invalid
&& luaA_toudata(L
, 3, &widget_class
))
374 /* upvalue 1 is content table */
375 lua_rawset(L
, lua_upvalueindex(1));
378 luaA_wibox_invalidate_byitem(L
, lua_topointer(L
, 1));
383 /** Convert the top element of the stack to a proxied wtable.
384 * \param L The Lua VM state.
387 luaA_table2wtable(lua_State
*L
)
389 if(!lua_istable(L
, -1))
392 lua_newtable(L
); /* create *real* content table */
393 lua_createtable(L
, 0, 5); /* metatable */
394 lua_pushvalue(L
, -2); /* copy content table */
395 lua_pushcfunction(L
, luaA_ipairs_aux
); /* push ipairs aux */
396 lua_pushcclosure(L
, luaA_wtable_ipairs
, 2);
397 lua_pushvalue(L
, -3); /* copy content table */
398 lua_pushcclosure(L
, luaA_wtable_next
, 1); /* __next has the content table as upvalue */
399 lua_pushvalue(L
, -4); /* copy content table */
400 lua_pushcclosure(L
, luaA_wtable_index
, 1); /* __index has the content table as upvalue */
401 lua_pushvalue(L
, -5); /* copy content table */
402 lua_pushcclosure(L
, luaA_wtable_newindex
, 1); /* __newindex has the content table as upvalue */
403 /* set metatable field with just pushed closure */
404 lua_setfield(L
, -5, "__newindex");
405 lua_setfield(L
, -4, "__index");
406 lua_setfield(L
, -3, "__next");
407 lua_setfield(L
, -2, "__ipairs");
408 /* set metatable impossible to touch */
409 lua_pushliteral(L
, "wtable");
410 lua_setfield(L
, -2, "__metatable");
411 /* set new metatable on original table */
412 lua_setmetatable(L
, -3);
416 /* go through original table */
417 while(lua_next(L
, -3))
419 /* if convert value to wtable */
420 luaA_table2wtable(L
);
422 lua_pushvalue(L
, -2);
423 /* move key before value */
425 /* set same value in content table */
428 lua_pushvalue(L
, -1);
429 /* push the new value :-> */
431 /* set orig[k] = nil */
434 /* remove content table */
438 /** Look for an item: table, function, etc.
439 * \param L The Lua VM state.
440 * \param item The pointer item.
443 luaA_hasitem(lua_State
*L
, const void *item
)
446 while(luaA_next(L
, -2))
448 if(lua_topointer(L
, -1) == item
)
450 /* remove value and key */
454 if(lua_istable(L
, -1))
455 if(luaA_hasitem(L
, item
))
457 /* remove key and value */
467 /** Browse a table pushed on top of the index, and put all its table and
468 * sub-table into an array.
469 * \param L The Lua VM state.
470 * \param elems The elements array.
471 * \return False if we encounter an elements already in list.
474 luaA_isloop_check(lua_State
*L
, cptr_array_t
*elems
)
476 if(lua_istable(L
, -1))
478 const void *object
= lua_topointer(L
, -1);
480 /* Check that the object table is not already in the list */
481 for(int i
= 0; i
< elems
->len
; i
++)
482 if(elems
->tab
[i
] == object
)
485 /* push the table in the elements list */
486 cptr_array_append(elems
, object
);
488 /* look every object in the "table" */
490 while(luaA_next(L
, -2))
492 if(!luaA_isloop_check(L
, elems
))
494 /* remove key and value */
498 /* remove value, keep key for next iteration */
505 /** Check if a table is a loop. When using tables as direct acyclic digram,
507 * \param L The Lua VM state.
508 * \param idx The index of the table in the stack
509 * \return True if the table loops.
512 luaA_isloop(lua_State
*L
, int idx
)
514 /* elems is an elements array that we will fill with all array we
515 * encounter while browsing the tables */
518 cptr_array_init(&elems
);
520 /* push table on top */
521 lua_pushvalue(L
, idx
);
523 bool ret
= luaA_isloop_check(L
, &elems
);
525 /* remove pushed table */
528 cptr_array_wipe(&elems
);
533 /** awesome global table.
534 * \param L The Lua VM state.
535 * \return The number of elements pushed on stack.
537 * \lfield font The default font.
538 * \lfield font_height The default font height.
539 * \lfield conffile The configuration file which has been loaded.
542 luaA_awesome_index(lua_State
*L
)
544 if(luaA_usemetatable(L
, 1, 2))
548 const char *buf
= luaL_checklstring(L
, 2, &len
);
550 switch(a_tokenize(buf
, len
))
554 char *font
= pango_font_description_to_string(globalconf
.font
->desc
);
555 lua_pushstring(L
, font
);
559 case A_TK_FONT_HEIGHT
:
560 lua_pushnumber(L
, globalconf
.font
->height
);
563 lua_pushstring(L
, globalconf
.conffile
);
566 luaA_pushxcolor(L
, globalconf
.colors
.fg
);
569 luaA_pushxcolor(L
, globalconf
.colors
.bg
);
572 lua_pushliteral(L
, AWESOME_VERSION
);
575 lua_pushliteral(L
, AWESOME_RELEASE
);
584 /** Newindex function for the awesome global table.
585 * \param L The Lua VM state.
586 * \return The number of elements pushed on stack.
589 luaA_awesome_newindex(lua_State
*L
)
591 if(luaA_usemetatable(L
, 1, 2))
595 const char *buf
= luaL_checklstring(L
, 2, &len
);
597 switch(a_tokenize(buf
, len
))
601 const char *newfont
= luaL_checkstring(L
, 3);
602 draw_font_delete(&globalconf
.font
);
603 globalconf
.font
= draw_font_new(newfont
);
604 /* refresh all wiboxes */
605 foreach(wibox
, globalconf
.wiboxes
)
606 (*wibox
)->need_update
= true;
607 foreach(c
, globalconf
.clients
)
609 (*c
)->titlebar
->need_update
= true;
613 if((buf
= luaL_checklstring(L
, 3, &len
)))
614 xcolor_init_reply(xcolor_init_unchecked(&globalconf
.colors
.fg
, buf
, len
));
617 if((buf
= luaL_checklstring(L
, 3, &len
)))
618 xcolor_init_reply(xcolor_init_unchecked(&globalconf
.colors
.bg
, buf
, len
));
627 /** Add a global signal.
628 * \param L The Lua VM state.
629 * \return The number of elements pushed on stack.
631 * \lparam A string with the event name.
632 * \lparam The function to call.
635 luaA_awesome_add_signal(lua_State
*L
)
637 const char *name
= luaL_checkstring(L
, 1);
638 luaA_checkfunction(L
, 2);
639 signal_add(&global_signals
, name
, luaA_object_ref(L
, 2));
643 /** Remove a global signal.
644 * \param L The Lua VM state.
645 * \return The number of elements pushed on stack.
647 * \lparam A string with the event name.
648 * \lparam The function to call.
651 luaA_awesome_remove_signal(lua_State
*L
)
653 const char *name
= luaL_checkstring(L
, 1);
654 luaA_checkfunction(L
, 2);
655 const void *func
= lua_topointer(L
, 2);
656 signal_remove(&global_signals
, name
, func
);
657 luaA_object_unref(L
, (void *) func
);
661 /** Emit a global signal.
662 * \param L The Lua VM state.
663 * \return The number of elements pushed on stack.
665 * \lparam A string with the event name.
666 * \lparam The function to call.
669 luaA_awesome_emit_signal(lua_State
*L
)
671 signal_object_emit(L
, &global_signals
, luaL_checkstring(L
, 1), lua_gettop(L
) - 1);
676 luaA_panic(lua_State
*L
)
678 warn("unprotected error in call to Lua API (%s), restarting awesome",
679 lua_tostring(L
, -1));
685 luaA_dofunction_on_error(lua_State
*L
)
687 /* duplicate string error */
688 lua_pushvalue(L
, -1);
689 /* emit error signal */
690 signal_object_emit(L
, &global_signals
, "debug::error", 1);
692 if(!luaL_dostring(L
, "return debug.traceback(\"error while running function\", 3)"))
694 /* Move traceback before error */
696 /* Insert sentence */
697 lua_pushliteral(L
, "\nerror: ");
698 /* Move it before error */
705 /** Initialize the Lua VM
706 * \param xdg An xdg handle to use to get XDG basedir.
709 luaA_init(xdgHandle
* xdg
)
712 static const struct luaL_reg awesome_lib
[] =
714 { "quit", luaA_quit
},
715 { "exec", luaA_exec
},
716 { "spawn", luaA_spawn
},
717 { "restart", luaA_restart
},
718 { "add_signal", luaA_awesome_add_signal
},
719 { "remove_signal", luaA_awesome_remove_signal
},
720 { "emit_signal", luaA_awesome_emit_signal
},
721 { "__index", luaA_awesome_index
},
722 { "__newindex", luaA_awesome_newindex
},
726 L
= globalconf
.L
= luaL_newstate();
728 /* Set panic function */
729 lua_atpanic(L
, luaA_panic
);
731 /* Set error handling function */
732 lualib_dofunction_on_error
= luaA_dofunction_on_error
;
738 luaA_object_setup(L
);
740 /* Export awesome lib */
741 luaA_openlib(L
, "awesome", awesome_lib
, awesome_lib
);
743 /* Export root lib */
744 luaL_register(L
, "root", awesome_root_lib
);
745 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
747 /* Export hooks lib */
748 luaL_register(L
, "hooks", awesome_hooks_lib
);
749 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
752 /* Export D-Bus lib */
753 luaL_register(L
, "dbus", awesome_dbus_lib
);
754 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
757 /* Export keygrabber lib */
758 luaL_register(L
, "keygrabber", awesome_keygrabber_lib
);
759 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
761 /* Export mousegrabber lib */
762 luaL_register(L
, "mousegrabber", awesome_mousegrabber_lib
);
763 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
766 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
769 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
772 button_class_setup(L
);
775 image_class_setup(L
);
781 wibox_class_setup(L
);
784 widget_class_setup(L
);
787 client_class_setup(L
);
793 timer_class_setup(L
);
796 globalconf
.hooks
.manage
= LUA_REFNIL
;
797 globalconf
.hooks
.unmanage
= LUA_REFNIL
;
798 globalconf
.hooks
.focus
= LUA_REFNIL
;
799 globalconf
.hooks
.unfocus
= LUA_REFNIL
;
800 globalconf
.hooks
.mouse_enter
= LUA_REFNIL
;
801 globalconf
.hooks
.mouse_leave
= LUA_REFNIL
;
802 globalconf
.hooks
.clients
= LUA_REFNIL
;
803 globalconf
.hooks
.tags
= LUA_REFNIL
;
804 globalconf
.hooks
.tagged
= LUA_REFNIL
;
805 globalconf
.hooks
.property
= LUA_REFNIL
;
806 globalconf
.hooks
.timer
= LUA_REFNIL
;
807 globalconf
.hooks
.exit
= LUA_REFNIL
;
809 /* add Lua search paths */
810 lua_getglobal(L
, "package");
811 if (LUA_TTABLE
!= lua_type(L
, 1))
813 warn("package is not a table");
816 lua_getfield(L
, 1, "path");
817 if (LUA_TSTRING
!= lua_type(L
, 2))
819 warn("package.path is not a string");
824 /* add XDG_CONFIG_DIR as include path */
825 const char * const *xdgconfigdirs
= xdgSearchableConfigDirectories(xdg
);
826 for(; *xdgconfigdirs
; xdgconfigdirs
++)
828 size_t len
= a_strlen(*xdgconfigdirs
);
829 lua_pushliteral(L
, ";");
830 lua_pushlstring(L
, *xdgconfigdirs
, len
);
831 lua_pushliteral(L
, "/awesome/?.lua");
834 lua_pushliteral(L
, ";");
835 lua_pushlstring(L
, *xdgconfigdirs
, len
);
836 lua_pushliteral(L
, "/awesome/?/init.lua");
839 lua_concat(L
, 3); /* concatenate with package.path */
842 /* add Lua lib path (/usr/share/awesome/lib by default) */
843 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?.lua");
844 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?/init.lua");
845 lua_concat(L
, 3); /* concatenate with package.path */
846 lua_setfield(L
, 1, "path"); /* package.path = "concatenated string" */
850 luaA_loadrc(const char *confpath
, bool run
)
852 if(!luaL_loadfile(globalconf
.L
, confpath
))
856 if(lua_pcall(globalconf
.L
, 0, LUA_MULTRET
, 0))
857 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
860 globalconf
.conffile
= a_strdup(confpath
);
866 lua_pop(globalconf
.L
, 1);
871 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
876 /** Load a configuration file.
877 * \param xdg An xdg handle to use to get XDG basedir.
878 * \param confpatharg The configuration file to load.
879 * \param run Run the configuration file.
882 luaA_parserc(xdgHandle
* xdg
, const char *confpatharg
, bool run
)
884 char *confpath
= NULL
;
887 /* try to load, return if it's ok */
890 if(luaA_loadrc(confpatharg
, run
))
899 confpath
= xdgConfigFind("awesome/rc.lua", xdg
);
901 char *tmp
= confpath
;
903 /* confpath is "string1\0string2\0string3\0\0" */
906 if(luaA_loadrc(tmp
, run
))
913 tmp
+= a_strlen(tmp
) + 1;
924 luaA_on_timer(EV_P_ ev_timer
*w
, int revents
)
926 if(globalconf
.hooks
.timer
!= LUA_REFNIL
)
927 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.timer
, 0, 0);
931 luaA_class_index_miss_property(lua_State
*L
, lua_object_t
*obj
)
933 signal_object_emit(L
, &global_signals
, "debug::index::miss", 2);
938 luaA_class_newindex_miss_property(lua_State
*L
, lua_object_t
*obj
)
940 signal_object_emit(L
, &global_signals
, "debug::newindex::miss", 3);
944 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80