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 "objects/timer.h"
35 #include "awesome-version-internal.h"
39 #include "objects/tag.h"
40 #include "objects/client.h"
41 #include "objects/drawin.h"
44 #include "selection.h"
46 #include "common/xcursor.h"
47 #include "common/buffer.h"
48 #include "common/backtrace.h"
51 extern const struct luaL_reg awesome_dbus_lib
[];
53 extern const struct luaL_reg awesome_keygrabber_lib
[];
54 extern const struct luaL_reg awesome_mousegrabber_lib
[];
55 extern const struct luaL_reg awesome_root_lib
[];
56 extern const struct luaL_reg awesome_mouse_methods
[];
57 extern const struct luaL_reg awesome_mouse_meta
[];
58 extern const struct luaL_reg awesome_screen_methods
[];
59 extern const struct luaL_reg awesome_screen_meta
[];
61 /** Path to config file */
62 static char *conffile
;
65 * \param L The Lua VM state.
66 * \return The number of elements pushed on stack.
69 luaA_quit(lua_State
*L
)
71 ev_unloop(globalconf
.loop
, 1);
75 /** Execute another application, probably a window manager, to replace
77 * \param L The Lua VM state.
78 * \return The number of elements pushed on stack.
80 * \lparam The command line to execute.
83 luaA_exec(lua_State
*L
)
85 const char *cmd
= luaL_checkstring(L
, 1);
87 awesome_atexit(false);
96 luaA_restart(lua_State
*L
)
102 /** Load an image from a given path.
103 * \param L The Lua VM state.
104 * \return The number of elements pushed on stack.
106 * \lparam The command line to execute.
109 luaA_load_image(lua_State
*L
)
111 const char *filename
= luaL_checkstring(L
, 1);
112 cairo_surface_t
*surface
= draw_load_image(L
, filename
);
115 /* lua has to make sure to free the ref or we have a leak */
116 lua_pushlightuserdata(L
, surface
);
120 /** UTF-8 aware string length computing.
121 * \param L The Lua VM state.
122 * \return The number of elements pushed on stack.
125 luaA_mbstrlen(lua_State
*L
)
127 const char *cmd
= luaL_checkstring(L
, 1);
128 lua_pushnumber(L
, (ssize_t
) mbstowcs(NULL
, NONULL(cmd
), 0));
132 /** Overload standard Lua next function to use __next key on metatable.
133 * \param L The Lua VM state.
134 * \return The number of elements pushed on stack.
137 luaAe_next(lua_State
*L
)
139 if(luaL_getmetafield(L
, 1, "__next"))
142 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
143 return lua_gettop(L
);
146 luaL_checktype(L
, 1, LUA_TTABLE
);
154 /** Overload lua_next() function by using __next metatable field
155 * to get next elements.
156 * \param L The Lua VM stack.
157 * \param idx The index number of elements in stack.
158 * \return 1 if more elements to come, 0 otherwise.
161 luaA_next(lua_State
*L
, int idx
)
163 if(luaL_getmetafield(L
, idx
, "__next"))
165 /* if idx is relative, reduce it since we got __next */
167 /* copy table and then move key */
168 lua_pushvalue(L
, idx
);
169 lua_pushvalue(L
, -3);
171 lua_pcall(L
, 2, 2, 0);
172 /* next returned nil, it's the end */
181 else if(lua_istable(L
, idx
))
182 return lua_next(L
, idx
);
188 /** Generic pairs function.
189 * \param L The Lua VM state.
190 * \return The number of elements pushed on stack.
193 luaA_generic_pairs(lua_State
*L
)
195 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
196 lua_pushvalue(L
, 1); /* state, */
197 lua_pushnil(L
); /* and initial value */
201 /** Overload standard pairs function to use __pairs field of metatables.
202 * \param L The Lua VM state.
203 * \return The number of elements pushed on stack.
206 luaAe_pairs(lua_State
*L
)
208 if(luaL_getmetafield(L
, 1, "__pairs"))
211 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
212 return lua_gettop(L
);
215 luaL_checktype(L
, 1, LUA_TTABLE
);
216 return luaA_generic_pairs(L
);
220 luaA_ipairs_aux(lua_State
*L
)
222 int i
= luaL_checkint(L
, 2) + 1;
223 luaL_checktype(L
, 1, LUA_TTABLE
);
224 lua_pushinteger(L
, i
);
225 lua_rawgeti(L
, 1, i
);
226 return (lua_isnil(L
, -1)) ? 0 : 2;
229 /** Overload standard ipairs function to use __ipairs field of metatables.
230 * \param L The Lua VM state.
231 * \return The number of elements pushed on stack.
234 luaAe_ipairs(lua_State
*L
)
236 if(luaL_getmetafield(L
, 1, "__ipairs"))
239 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
240 return lua_gettop(L
);
243 luaL_checktype(L
, 1, LUA_TTABLE
);
244 lua_pushvalue(L
, lua_upvalueindex(1));
246 lua_pushinteger(L
, 0); /* and initial value */
250 /** Enhanced type() function which recognize awesome objects.
251 * \param L The Lua VM state.
252 * \return The number of arguments pushed on the stack.
255 luaAe_type(lua_State
*L
)
258 lua_pushstring(L
, luaA_typename(L
, 1));
262 /** Replace various standards Lua functions with our own.
263 * \param L The Lua VM state.
266 luaA_fixups(lua_State
*L
)
268 /* export string.wlen */
269 lua_getglobal(L
, "string");
270 lua_pushcfunction(L
, luaA_mbstrlen
);
271 lua_setfield(L
, -2, "wlen");
274 lua_pushliteral(L
, "next");
275 lua_pushcfunction(L
, luaAe_next
);
276 lua_settable(L
, LUA_GLOBALSINDEX
);
278 lua_pushliteral(L
, "pairs");
279 lua_pushcfunction(L
, luaAe_next
);
280 lua_pushcclosure(L
, luaAe_pairs
, 1); /* pairs get next as upvalue */
281 lua_settable(L
, LUA_GLOBALSINDEX
);
283 lua_pushliteral(L
, "ipairs");
284 lua_pushcfunction(L
, luaA_ipairs_aux
);
285 lua_pushcclosure(L
, luaAe_ipairs
, 1);
286 lua_settable(L
, LUA_GLOBALSINDEX
);
288 lua_pushliteral(L
, "type");
289 lua_pushcfunction(L
, luaAe_type
);
290 lua_settable(L
, LUA_GLOBALSINDEX
);
292 lua_pushliteral(L
, "selection");
293 lua_pushcfunction(L
, luaA_selection_get
);
294 lua_settable(L
, LUA_GLOBALSINDEX
);
297 /** Look for an item: table, function, etc.
298 * \param L The Lua VM state.
299 * \param item The pointer item.
302 luaA_hasitem(lua_State
*L
, const void *item
)
305 while(luaA_next(L
, -2))
307 if(lua_topointer(L
, -1) == item
)
309 /* remove value and key */
313 if(lua_istable(L
, -1))
314 if(luaA_hasitem(L
, item
))
316 /* remove key and value */
326 /** Browse a table pushed on top of the index, and put all its table and
327 * sub-table into an array.
328 * \param L The Lua VM state.
329 * \param elems The elements array.
330 * \return False if we encounter an elements already in list.
333 luaA_isloop_check(lua_State
*L
, cptr_array_t
*elems
)
335 if(lua_istable(L
, -1))
337 const void *object
= lua_topointer(L
, -1);
339 /* Check that the object table is not already in the list */
340 for(int i
= 0; i
< elems
->len
; i
++)
341 if(elems
->tab
[i
] == object
)
344 /* push the table in the elements list */
345 cptr_array_append(elems
, object
);
347 /* look every object in the "table" */
349 while(luaA_next(L
, -2))
351 if(!luaA_isloop_check(L
, elems
))
353 /* remove key and value */
357 /* remove value, keep key for next iteration */
364 /** Check if a table is a loop. When using tables as direct acyclic digram,
366 * \param L The Lua VM state.
367 * \param idx The index of the table in the stack
368 * \return True if the table loops.
371 luaA_isloop(lua_State
*L
, int idx
)
373 /* elems is an elements array that we will fill with all array we
374 * encounter while browsing the tables */
377 cptr_array_init(&elems
);
379 /* push table on top */
380 lua_pushvalue(L
, idx
);
382 bool ret
= luaA_isloop_check(L
, &elems
);
384 /* remove pushed table */
387 cptr_array_wipe(&elems
);
392 /** awesome global table.
393 * \param L The Lua VM state.
394 * \return The number of elements pushed on stack.
396 * \lfield font The default font.
397 * \lfield font_height The default font height.
398 * \lfield conffile The configuration file which has been loaded.
401 luaA_awesome_index(lua_State
*L
)
403 if(luaA_usemetatable(L
, 1, 2))
406 const char *buf
= luaL_checkstring(L
, 2);
408 if(A_STREQ(buf
, "conffile"))
410 lua_pushstring(L
, conffile
);
414 if(A_STREQ(buf
, "version"))
416 lua_pushliteral(L
, AWESOME_VERSION
);
420 if(A_STREQ(buf
, "release"))
422 lua_pushliteral(L
, AWESOME_RELEASE
);
426 if(A_STREQ(buf
, "startup_errors") && globalconf
.startup_errors
.len
!= 0)
428 lua_pushstring(L
, globalconf
.startup_errors
.s
);
435 /** Add a global signal.
436 * \param L The Lua VM state.
437 * \return The number of elements pushed on stack.
439 * \lparam A string with the event name.
440 * \lparam The function to call.
443 luaA_awesome_connect_signal(lua_State
*L
)
445 const char *name
= luaL_checkstring(L
, 1);
446 luaA_checkfunction(L
, 2);
447 signal_connect(&global_signals
, name
, luaA_object_ref(L
, 2));
451 /** Remove a global signal.
452 * \param L The Lua VM state.
453 * \return The number of elements pushed on stack.
455 * \lparam A string with the event name.
456 * \lparam The function to call.
459 luaA_awesome_disconnect_signal(lua_State
*L
)
461 const char *name
= luaL_checkstring(L
, 1);
462 luaA_checkfunction(L
, 2);
463 const void *func
= lua_topointer(L
, 2);
464 signal_disconnect(&global_signals
, name
, func
);
465 luaA_object_unref(L
, (void *) func
);
469 /** Emit a global signal.
470 * \param L The Lua VM state.
471 * \return The number of elements pushed on stack.
473 * \lparam A string with the event name.
474 * \lparam The function to call.
477 luaA_awesome_emit_signal(lua_State
*L
)
479 signal_object_emit(L
, &global_signals
, luaL_checkstring(L
, 1), lua_gettop(L
) - 1);
484 luaA_panic(lua_State
*L
)
486 warn("unprotected error in call to Lua API (%s)",
487 lua_tostring(L
, -1));
490 warn("dumping backtrace\n%s", buf
.s
);
491 warn("restarting awesome");
497 luaA_dofunction_on_error(lua_State
*L
)
499 /* duplicate string error */
500 lua_pushvalue(L
, -1);
501 /* emit error signal */
502 signal_object_emit(L
, &global_signals
, "debug::error", 1);
504 if(!luaL_dostring(L
, "return debug.traceback(\"error while running function\", 3)"))
506 /* Move traceback before error */
508 /* Insert sentence */
509 lua_pushliteral(L
, "\nerror: ");
510 /* Move it before error */
517 /** Initialize the Lua VM
518 * \param xdg An xdg handle to use to get XDG basedir.
521 luaA_init(xdgHandle
* xdg
)
524 static const struct luaL_reg awesome_lib
[] =
526 { "quit", luaA_quit
},
527 { "exec", luaA_exec
},
528 { "spawn", luaA_spawn
},
529 { "restart", luaA_restart
},
530 { "connect_signal", luaA_awesome_connect_signal
},
531 { "disconnect_signal", luaA_awesome_disconnect_signal
},
532 { "emit_signal", luaA_awesome_emit_signal
},
533 { "systray", luaA_systray
},
534 { "load_image", luaA_load_image
},
535 { "__index", luaA_awesome_index
},
539 L
= globalconf
.L
= luaL_newstate();
541 /* Set panic function */
542 lua_atpanic(L
, luaA_panic
);
544 /* Set error handling function */
545 lualib_dofunction_on_error
= luaA_dofunction_on_error
;
551 luaA_object_setup(L
);
553 /* Export awesome lib */
554 luaA_openlib(L
, "awesome", awesome_lib
, awesome_lib
);
556 /* Export root lib */
557 luaL_register(L
, "root", awesome_root_lib
);
558 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
561 /* Export D-Bus lib */
562 luaL_register(L
, "dbus", awesome_dbus_lib
);
563 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
566 /* Export keygrabber lib */
567 luaL_register(L
, "keygrabber", awesome_keygrabber_lib
);
568 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
570 /* Export mousegrabber lib */
571 luaL_register(L
, "mousegrabber", awesome_mousegrabber_lib
);
572 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
575 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
578 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
581 button_class_setup(L
);
587 window_class_setup(L
);
590 drawin_class_setup(L
);
593 client_class_setup(L
);
599 timer_class_setup(L
);
601 /* add Lua search paths */
602 lua_getglobal(L
, "package");
603 if (LUA_TTABLE
!= lua_type(L
, 1))
605 warn("package is not a table");
608 lua_getfield(L
, 1, "path");
609 if (LUA_TSTRING
!= lua_type(L
, 2))
611 warn("package.path is not a string");
616 /* add XDG_CONFIG_DIR as include path */
617 const char * const *xdgconfigdirs
= xdgSearchableConfigDirectories(xdg
);
618 for(; *xdgconfigdirs
; xdgconfigdirs
++)
620 size_t len
= a_strlen(*xdgconfigdirs
);
621 lua_pushliteral(L
, ";");
622 lua_pushlstring(L
, *xdgconfigdirs
, len
);
623 lua_pushliteral(L
, "/awesome/?.lua");
626 lua_pushliteral(L
, ";");
627 lua_pushlstring(L
, *xdgconfigdirs
, len
);
628 lua_pushliteral(L
, "/awesome/?/init.lua");
631 lua_concat(L
, 3); /* concatenate with package.path */
634 /* add Lua lib path (/usr/share/awesome/lib by default) */
635 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?.lua");
636 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?/init.lua");
637 lua_concat(L
, 3); /* concatenate with package.path */
638 lua_setfield(L
, 1, "path"); /* package.path = "concatenated string" */
640 lua_getfield(L
, 1, "loaded");
642 lua_pop(L
, 2); /* pop "package" and "package.loaded" */
644 signal_add(&global_signals
, "debug::error");
645 signal_add(&global_signals
, "debug::deprecation");
646 signal_add(&global_signals
, "debug::index::miss");
647 signal_add(&global_signals
, "debug::newindex::miss");
648 signal_add(&global_signals
, "systray::update");
649 signal_add(&global_signals
, "wallpaper_changed");
650 signal_add(&global_signals
, "refresh");
651 signal_add(&global_signals
, "exit");
655 luaA_startup_error(const char *err
)
657 if (globalconf
.startup_errors
.len
> 0)
658 buffer_addsl(&globalconf
.startup_errors
, "\n\n");
659 buffer_adds(&globalconf
.startup_errors
, err
);
663 luaA_loadrc(const char *confpath
, bool run
)
665 if(!luaL_loadfile(globalconf
.L
, confpath
))
669 /* Set the conffile right now so it can be used inside the
670 * configuration file. */
671 conffile
= a_strdup(confpath
);
672 if(lua_pcall(globalconf
.L
, 0, LUA_MULTRET
, 0))
674 const char *err
= lua_tostring(globalconf
.L
, -1);
675 luaA_startup_error(err
);
676 fprintf(stderr
, "%s\n", err
);
677 /* An error happened, so reset this. */
685 lua_pop(globalconf
.L
, 1);
691 const char *err
= lua_tostring(globalconf
.L
, -1);
692 luaA_startup_error(err
);
693 fprintf(stderr
, "%s\n", err
);
699 /** Load a configuration file.
700 * \param xdg An xdg handle to use to get XDG basedir.
701 * \param confpatharg The configuration file to load.
702 * \param run Run the configuration file.
705 luaA_parserc(xdgHandle
* xdg
, const char *confpatharg
, bool run
)
707 char *confpath
= NULL
;
710 /* try to load, return if it's ok */
713 if(luaA_loadrc(confpatharg
, run
))
722 confpath
= xdgConfigFind("awesome/rc.lua", xdg
);
724 char *tmp
= confpath
;
726 /* confpath is "string1\0string2\0string3\0\0" */
729 if(luaA_loadrc(tmp
, run
))
736 tmp
+= a_strlen(tmp
) + 1;
747 luaA_class_index_miss_property(lua_State
*L
, lua_object_t
*obj
)
749 signal_object_emit(L
, &global_signals
, "debug::index::miss", 2);
754 luaA_class_newindex_miss_property(lua_State
*L
, lua_object_t
*obj
)
756 signal_object_emit(L
, &global_signals
, "debug::newindex::miss", 3);
763 signal_object_emit(globalconf
.L
, &global_signals
, "refresh", 0);
766 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80