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 return draw_load_image(L
, filename
);
115 /** UTF-8 aware string length computing.
116 * \param L The Lua VM state.
117 * \return The number of elements pushed on stack.
120 luaA_mbstrlen(lua_State
*L
)
122 const char *cmd
= luaL_checkstring(L
, 1);
123 lua_pushnumber(L
, (ssize_t
) mbstowcs(NULL
, NONULL(cmd
), 0));
127 /** Overload standard Lua next function to use __next key on metatable.
128 * \param L The Lua VM state.
129 * \return The number of elements pushed on stack.
132 luaAe_next(lua_State
*L
)
134 if(luaL_getmetafield(L
, 1, "__next"))
137 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
138 return lua_gettop(L
);
141 luaL_checktype(L
, 1, LUA_TTABLE
);
149 /** Overload lua_next() function by using __next metatable field
150 * to get next elements.
151 * \param L The Lua VM stack.
152 * \param idx The index number of elements in stack.
153 * \return 1 if more elements to come, 0 otherwise.
156 luaA_next(lua_State
*L
, int idx
)
158 if(luaL_getmetafield(L
, idx
, "__next"))
160 /* if idx is relative, reduce it since we got __next */
162 /* copy table and then move key */
163 lua_pushvalue(L
, idx
);
164 lua_pushvalue(L
, -3);
166 lua_pcall(L
, 2, 2, 0);
167 /* next returned nil, it's the end */
176 else if(lua_istable(L
, idx
))
177 return lua_next(L
, idx
);
183 /** Generic pairs function.
184 * \param L The Lua VM state.
185 * \return The number of elements pushed on stack.
188 luaA_generic_pairs(lua_State
*L
)
190 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
191 lua_pushvalue(L
, 1); /* state, */
192 lua_pushnil(L
); /* and initial value */
196 /** Overload standard pairs function to use __pairs field of metatables.
197 * \param L The Lua VM state.
198 * \return The number of elements pushed on stack.
201 luaAe_pairs(lua_State
*L
)
203 if(luaL_getmetafield(L
, 1, "__pairs"))
206 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
207 return lua_gettop(L
);
210 luaL_checktype(L
, 1, LUA_TTABLE
);
211 return luaA_generic_pairs(L
);
215 luaA_ipairs_aux(lua_State
*L
)
217 int i
= luaL_checkint(L
, 2) + 1;
218 luaL_checktype(L
, 1, LUA_TTABLE
);
219 lua_pushinteger(L
, i
);
220 lua_rawgeti(L
, 1, i
);
221 return (lua_isnil(L
, -1)) ? 0 : 2;
224 /** Overload standard ipairs function to use __ipairs field of metatables.
225 * \param L The Lua VM state.
226 * \return The number of elements pushed on stack.
229 luaAe_ipairs(lua_State
*L
)
231 if(luaL_getmetafield(L
, 1, "__ipairs"))
234 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
235 return lua_gettop(L
);
238 luaL_checktype(L
, 1, LUA_TTABLE
);
239 lua_pushvalue(L
, lua_upvalueindex(1));
241 lua_pushinteger(L
, 0); /* and initial value */
245 /** Enhanced type() function which recognize awesome objects.
246 * \param L The Lua VM state.
247 * \return The number of arguments pushed on the stack.
250 luaAe_type(lua_State
*L
)
253 lua_pushstring(L
, luaA_typename(L
, 1));
257 /** Replace various standards Lua functions with our own.
258 * \param L The Lua VM state.
261 luaA_fixups(lua_State
*L
)
263 /* export string.wlen */
264 lua_getglobal(L
, "string");
265 lua_pushcfunction(L
, luaA_mbstrlen
);
266 lua_setfield(L
, -2, "wlen");
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 /** Look for an item: table, function, etc.
293 * \param L The Lua VM state.
294 * \param item The pointer item.
297 luaA_hasitem(lua_State
*L
, const void *item
)
300 while(luaA_next(L
, -2))
302 if(lua_topointer(L
, -1) == item
)
304 /* remove value and key */
308 if(lua_istable(L
, -1))
309 if(luaA_hasitem(L
, item
))
311 /* remove key and value */
321 /** Browse a table pushed on top of the index, and put all its table and
322 * sub-table into an array.
323 * \param L The Lua VM state.
324 * \param elems The elements array.
325 * \return False if we encounter an elements already in list.
328 luaA_isloop_check(lua_State
*L
, cptr_array_t
*elems
)
330 if(lua_istable(L
, -1))
332 const void *object
= lua_topointer(L
, -1);
334 /* Check that the object table is not already in the list */
335 for(int i
= 0; i
< elems
->len
; i
++)
336 if(elems
->tab
[i
] == object
)
339 /* push the table in the elements list */
340 cptr_array_append(elems
, object
);
342 /* look every object in the "table" */
344 while(luaA_next(L
, -2))
346 if(!luaA_isloop_check(L
, elems
))
348 /* remove key and value */
352 /* remove value, keep key for next iteration */
359 /** Check if a table is a loop. When using tables as direct acyclic digram,
361 * \param L The Lua VM state.
362 * \param idx The index of the table in the stack
363 * \return True if the table loops.
366 luaA_isloop(lua_State
*L
, int idx
)
368 /* elems is an elements array that we will fill with all array we
369 * encounter while browsing the tables */
372 cptr_array_init(&elems
);
374 /* push table on top */
375 lua_pushvalue(L
, idx
);
377 bool ret
= luaA_isloop_check(L
, &elems
);
379 /* remove pushed table */
382 cptr_array_wipe(&elems
);
387 /** awesome global table.
388 * \param L The Lua VM state.
389 * \return The number of elements pushed on stack.
391 * \lfield font The default font.
392 * \lfield font_height The default font height.
393 * \lfield conffile The configuration file which has been loaded.
396 luaA_awesome_index(lua_State
*L
)
398 if(luaA_usemetatable(L
, 1, 2))
401 const char *buf
= luaL_checkstring(L
, 2);
403 if(A_STREQ(buf
, "conffile"))
405 lua_pushstring(L
, conffile
);
409 if(A_STREQ(buf
, "version"))
411 lua_pushliteral(L
, AWESOME_VERSION
);
415 if(A_STREQ(buf
, "release"))
417 lua_pushliteral(L
, AWESOME_RELEASE
);
421 if(A_STREQ(buf
, "startup_errors") && globalconf
.startup_errors
.len
!= 0)
423 lua_pushstring(L
, globalconf
.startup_errors
.s
);
430 /** Add a global signal.
431 * \param L The Lua VM state.
432 * \return The number of elements pushed on stack.
434 * \lparam A string with the event name.
435 * \lparam The function to call.
438 luaA_awesome_connect_signal(lua_State
*L
)
440 const char *name
= luaL_checkstring(L
, 1);
441 luaA_checkfunction(L
, 2);
442 signal_connect(&global_signals
, name
, luaA_object_ref(L
, 2));
446 /** Remove a global signal.
447 * \param L The Lua VM state.
448 * \return The number of elements pushed on stack.
450 * \lparam A string with the event name.
451 * \lparam The function to call.
454 luaA_awesome_disconnect_signal(lua_State
*L
)
456 const char *name
= luaL_checkstring(L
, 1);
457 luaA_checkfunction(L
, 2);
458 const void *func
= lua_topointer(L
, 2);
459 signal_disconnect(&global_signals
, name
, func
);
460 luaA_object_unref(L
, (void *) func
);
464 /** Emit a global signal.
465 * \param L The Lua VM state.
466 * \return The number of elements pushed on stack.
468 * \lparam A string with the event name.
469 * \lparam The function to call.
472 luaA_awesome_emit_signal(lua_State
*L
)
474 signal_object_emit(L
, &global_signals
, luaL_checkstring(L
, 1), lua_gettop(L
) - 1);
479 luaA_panic(lua_State
*L
)
481 warn("unprotected error in call to Lua API (%s)",
482 lua_tostring(L
, -1));
485 warn("dumping backtrace\n%s", buf
.s
);
486 warn("restarting awesome");
492 luaA_dofunction_on_error(lua_State
*L
)
494 /* duplicate string error */
495 lua_pushvalue(L
, -1);
496 /* emit error signal */
497 signal_object_emit(L
, &global_signals
, "debug::error", 1);
499 if(!luaL_dostring(L
, "return debug.traceback(\"error while running function\", 3)"))
501 /* Move traceback before error */
503 /* Insert sentence */
504 lua_pushliteral(L
, "\nerror: ");
505 /* Move it before error */
512 /** Initialize the Lua VM
513 * \param xdg An xdg handle to use to get XDG basedir.
516 luaA_init(xdgHandle
* xdg
)
519 static const struct luaL_reg awesome_lib
[] =
521 { "quit", luaA_quit
},
522 { "exec", luaA_exec
},
523 { "spawn", luaA_spawn
},
524 { "restart", luaA_restart
},
525 { "connect_signal", luaA_awesome_connect_signal
},
526 { "disconnect_signal", luaA_awesome_disconnect_signal
},
527 { "emit_signal", luaA_awesome_emit_signal
},
528 { "systray", luaA_systray
},
529 { "load_image", luaA_load_image
},
530 { "__index", luaA_awesome_index
},
534 L
= globalconf
.L
= luaL_newstate();
536 /* Set panic function */
537 lua_atpanic(L
, luaA_panic
);
539 /* Set error handling function */
540 lualib_dofunction_on_error
= luaA_dofunction_on_error
;
546 luaA_object_setup(L
);
548 /* Export awesome lib */
549 luaA_openlib(L
, "awesome", awesome_lib
, awesome_lib
);
551 /* Export root lib */
552 luaL_register(L
, "root", awesome_root_lib
);
553 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
556 /* Export D-Bus lib */
557 luaL_register(L
, "dbus", awesome_dbus_lib
);
558 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
561 /* Export keygrabber lib */
562 luaL_register(L
, "keygrabber", awesome_keygrabber_lib
);
563 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
565 /* Export mousegrabber lib */
566 luaL_register(L
, "mousegrabber", awesome_mousegrabber_lib
);
567 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
570 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
573 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
576 button_class_setup(L
);
582 window_class_setup(L
);
585 drawin_class_setup(L
);
588 client_class_setup(L
);
594 timer_class_setup(L
);
596 /* add Lua search paths */
597 lua_getglobal(L
, "package");
598 if (LUA_TTABLE
!= lua_type(L
, 1))
600 warn("package is not a table");
603 lua_getfield(L
, 1, "path");
604 if (LUA_TSTRING
!= lua_type(L
, 2))
606 warn("package.path is not a string");
611 /* add XDG_CONFIG_DIR as include path */
612 const char * const *xdgconfigdirs
= xdgSearchableConfigDirectories(xdg
);
613 for(; *xdgconfigdirs
; xdgconfigdirs
++)
615 size_t len
= a_strlen(*xdgconfigdirs
);
616 lua_pushliteral(L
, ";");
617 lua_pushlstring(L
, *xdgconfigdirs
, len
);
618 lua_pushliteral(L
, "/awesome/?.lua");
621 lua_pushliteral(L
, ";");
622 lua_pushlstring(L
, *xdgconfigdirs
, len
);
623 lua_pushliteral(L
, "/awesome/?/init.lua");
626 lua_concat(L
, 3); /* concatenate with package.path */
629 /* add Lua lib path (/usr/share/awesome/lib by default) */
630 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?.lua");
631 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?/init.lua");
632 lua_concat(L
, 3); /* concatenate with package.path */
633 lua_setfield(L
, 1, "path"); /* package.path = "concatenated string" */
635 lua_getfield(L
, 1, "loaded");
637 lua_pop(L
, 2); /* pop "package" and "package.loaded" */
639 signal_add(&global_signals
, "debug::error");
640 signal_add(&global_signals
, "debug::deprecation");
641 signal_add(&global_signals
, "debug::index::miss");
642 signal_add(&global_signals
, "debug::newindex::miss");
643 signal_add(&global_signals
, "systray::update");
644 signal_add(&global_signals
, "wallpaper_changed");
645 signal_add(&global_signals
, "refresh");
646 signal_add(&global_signals
, "exit");
650 luaA_startup_error(const char *err
)
652 if (globalconf
.startup_errors
.len
> 0)
653 buffer_addsl(&globalconf
.startup_errors
, "\n\n");
654 buffer_adds(&globalconf
.startup_errors
, err
);
658 luaA_loadrc(const char *confpath
, bool run
)
660 if(!luaL_loadfile(globalconf
.L
, confpath
))
664 /* Set the conffile right now so it can be used inside the
665 * configuration file. */
666 conffile
= a_strdup(confpath
);
667 if(lua_pcall(globalconf
.L
, 0, LUA_MULTRET
, 0))
669 const char *err
= lua_tostring(globalconf
.L
, -1);
670 luaA_startup_error(err
);
671 fprintf(stderr
, "%s\n", err
);
672 /* An error happened, so reset this. */
680 lua_pop(globalconf
.L
, 1);
686 const char *err
= lua_tostring(globalconf
.L
, -1);
687 luaA_startup_error(err
);
688 fprintf(stderr
, "%s\n", err
);
694 /** Load a configuration file.
695 * \param xdg An xdg handle to use to get XDG basedir.
696 * \param confpatharg The configuration file to load.
697 * \param run Run the configuration file.
700 luaA_parserc(xdgHandle
* xdg
, const char *confpatharg
, bool run
)
702 char *confpath
= NULL
;
705 /* try to load, return if it's ok */
708 if(luaA_loadrc(confpatharg
, run
))
717 confpath
= xdgConfigFind("awesome/rc.lua", xdg
);
719 char *tmp
= confpath
;
721 /* confpath is "string1\0string2\0string3\0\0" */
724 if(luaA_loadrc(tmp
, run
))
731 tmp
+= a_strlen(tmp
) + 1;
742 luaA_class_index_miss_property(lua_State
*L
, lua_object_t
*obj
)
744 signal_object_emit(L
, &global_signals
, "debug::index::miss", 2);
749 luaA_class_newindex_miss_property(lua_State
*L
, lua_object_t
*obj
)
751 signal_object_emit(L
, &global_signals
, "debug::newindex::miss", 3);
758 signal_object_emit(globalconf
.L
, &global_signals
, "refresh", 0);
761 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80