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"
42 #include "objects/drawable.h"
45 #include "selection.h"
47 #include "common/xcursor.h"
48 #include "common/buffer.h"
49 #include "common/backtrace.h"
52 extern const struct luaL_Reg awesome_dbus_lib
[];
54 extern const struct luaL_Reg awesome_keygrabber_lib
[];
55 extern const struct luaL_Reg awesome_mousegrabber_lib
[];
56 extern const struct luaL_Reg awesome_root_lib
[];
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
[];
62 /** Path to config file */
63 static char *conffile
;
66 * \param L The Lua VM state.
67 * \return The number of elements pushed on stack.
70 luaA_quit(lua_State
*L
)
72 ev_unloop(globalconf
.loop
, 1);
76 /** Execute another application, probably a window manager, to replace
78 * \param L The Lua VM state.
79 * \return The number of elements pushed on stack.
81 * \lparam The command line to execute.
84 luaA_exec(lua_State
*L
)
86 const char *cmd
= luaL_checkstring(L
, 1);
88 awesome_atexit(false);
97 luaA_restart(lua_State
*L
)
103 /** Load an image from a given path.
104 * \param L The Lua VM state.
105 * \return The number of elements pushed on stack.
107 * \lparam The command line to execute.
110 luaA_load_image(lua_State
*L
)
112 const char *filename
= luaL_checkstring(L
, 1);
113 cairo_surface_t
*surface
= draw_load_image(L
, filename
);
116 /* lua has to make sure to free the ref or we have a leak */
117 lua_pushlightuserdata(L
, surface
);
121 /** UTF-8 aware string length computing.
122 * \param L The Lua VM state.
123 * \return The number of elements pushed on stack.
126 luaA_mbstrlen(lua_State
*L
)
128 const char *cmd
= luaL_checkstring(L
, 1);
129 lua_pushnumber(L
, (ssize_t
) mbstowcs(NULL
, NONULL(cmd
), 0));
133 /** Overload standard Lua next function to use __next key on metatable.
134 * \param L The Lua VM state.
135 * \return The number of elements pushed on stack.
138 luaAe_next(lua_State
*L
)
140 if(luaL_getmetafield(L
, 1, "__next"))
143 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
144 return lua_gettop(L
);
147 luaL_checktype(L
, 1, LUA_TTABLE
);
155 /** Overload lua_next() function by using __next metatable field
156 * to get next elements.
157 * \param L The Lua VM stack.
158 * \param idx The index number of elements in stack.
159 * \return 1 if more elements to come, 0 otherwise.
162 luaA_next(lua_State
*L
, int idx
)
164 if(luaL_getmetafield(L
, idx
, "__next"))
166 /* if idx is relative, reduce it since we got __next */
168 /* copy table and then move key */
169 lua_pushvalue(L
, idx
);
170 lua_pushvalue(L
, -3);
172 lua_pcall(L
, 2, 2, 0);
173 /* next returned nil, it's the end */
182 else if(lua_istable(L
, idx
))
183 return lua_next(L
, idx
);
189 /** Generic pairs function.
190 * \param L The Lua VM state.
191 * \return The number of elements pushed on stack.
194 luaA_generic_pairs(lua_State
*L
)
196 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
197 lua_pushvalue(L
, 1); /* state, */
198 lua_pushnil(L
); /* and initial value */
202 /** Overload standard pairs function to use __pairs field of metatables.
203 * \param L The Lua VM state.
204 * \return The number of elements pushed on stack.
207 luaAe_pairs(lua_State
*L
)
209 if(luaL_getmetafield(L
, 1, "__pairs"))
212 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
213 return lua_gettop(L
);
216 luaL_checktype(L
, 1, LUA_TTABLE
);
217 return luaA_generic_pairs(L
);
221 luaA_ipairs_aux(lua_State
*L
)
223 int i
= luaL_checkint(L
, 2) + 1;
224 luaL_checktype(L
, 1, LUA_TTABLE
);
225 lua_pushinteger(L
, i
);
226 lua_rawgeti(L
, 1, i
);
227 return (lua_isnil(L
, -1)) ? 0 : 2;
230 /** Overload standard ipairs function to use __ipairs field of metatables.
231 * \param L The Lua VM state.
232 * \return The number of elements pushed on stack.
235 luaAe_ipairs(lua_State
*L
)
237 if(luaL_getmetafield(L
, 1, "__ipairs"))
240 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
241 return lua_gettop(L
);
244 luaL_checktype(L
, 1, LUA_TTABLE
);
245 lua_pushvalue(L
, lua_upvalueindex(1));
247 lua_pushinteger(L
, 0); /* and initial value */
251 /** Enhanced type() function which recognize awesome objects.
252 * \param L The Lua VM state.
253 * \return The number of arguments pushed on the stack.
256 luaAe_type(lua_State
*L
)
259 lua_pushstring(L
, luaA_typename(L
, 1));
263 /** Replace various standards Lua functions with our own.
264 * \param L The Lua VM state.
267 luaA_fixups(lua_State
*L
)
269 /* export string.wlen */
270 lua_getglobal(L
, "string");
271 lua_pushcfunction(L
, luaA_mbstrlen
);
272 lua_setfield(L
, -2, "wlen");
275 lua_pushcfunction(L
, luaAe_next
);
276 lua_setglobal(L
, "next");
278 lua_pushcfunction(L
, luaAe_next
);
279 lua_pushcclosure(L
, luaAe_pairs
, 1); /* pairs get next as upvalue */
280 lua_setglobal(L
, "pairs");
282 lua_pushcfunction(L
, luaA_ipairs_aux
);
283 lua_pushcclosure(L
, luaAe_ipairs
, 1);
284 lua_setglobal(L
, "ipairs");
286 lua_pushcfunction(L
, luaAe_type
);
287 lua_setglobal(L
, "type");
289 lua_pushcfunction(L
, luaA_selection_get
);
290 lua_setglobal(L
, "selection");
293 /** Look for an item: table, function, etc.
294 * \param L The Lua VM state.
295 * \param item The pointer item.
298 luaA_hasitem(lua_State
*L
, const void *item
)
301 while(luaA_next(L
, -2))
303 if(lua_topointer(L
, -1) == item
)
305 /* remove value and key */
309 if(lua_istable(L
, -1))
310 if(luaA_hasitem(L
, item
))
312 /* remove key and value */
322 /** Browse a table pushed on top of the index, and put all its table and
323 * sub-table into an array.
324 * \param L The Lua VM state.
325 * \param elems The elements array.
326 * \return False if we encounter an elements already in list.
329 luaA_isloop_check(lua_State
*L
, cptr_array_t
*elems
)
331 if(lua_istable(L
, -1))
333 const void *object
= lua_topointer(L
, -1);
335 /* Check that the object table is not already in the list */
336 for(int i
= 0; i
< elems
->len
; i
++)
337 if(elems
->tab
[i
] == object
)
340 /* push the table in the elements list */
341 cptr_array_append(elems
, object
);
343 /* look every object in the "table" */
345 while(luaA_next(L
, -2))
347 if(!luaA_isloop_check(L
, elems
))
349 /* remove key and value */
353 /* remove value, keep key for next iteration */
360 /** Check if a table is a loop. When using tables as direct acyclic digram,
362 * \param L The Lua VM state.
363 * \param idx The index of the table in the stack
364 * \return True if the table loops.
367 luaA_isloop(lua_State
*L
, int idx
)
369 /* elems is an elements array that we will fill with all array we
370 * encounter while browsing the tables */
373 cptr_array_init(&elems
);
375 /* push table on top */
376 lua_pushvalue(L
, idx
);
378 bool ret
= luaA_isloop_check(L
, &elems
);
380 /* remove pushed table */
383 cptr_array_wipe(&elems
);
388 /** awesome global table.
389 * \param L The Lua VM state.
390 * \return The number of elements pushed on stack.
392 * \lfield font The default font.
393 * \lfield font_height The default font height.
394 * \lfield conffile The configuration file which has been loaded.
397 luaA_awesome_index(lua_State
*L
)
399 if(luaA_usemetatable(L
, 1, 2))
402 const char *buf
= luaL_checkstring(L
, 2);
404 if(A_STREQ(buf
, "conffile"))
406 lua_pushstring(L
, conffile
);
410 if(A_STREQ(buf
, "version"))
412 lua_pushliteral(L
, AWESOME_VERSION
);
416 if(A_STREQ(buf
, "release"))
418 lua_pushliteral(L
, AWESOME_RELEASE
);
422 if(A_STREQ(buf
, "startup_errors") && globalconf
.startup_errors
.len
!= 0)
424 lua_pushstring(L
, globalconf
.startup_errors
.s
);
431 /** Add a global signal.
432 * \param L The Lua VM state.
433 * \return The number of elements pushed on stack.
435 * \lparam A string with the event name.
436 * \lparam The function to call.
439 luaA_awesome_connect_signal(lua_State
*L
)
441 const char *name
= luaL_checkstring(L
, 1);
442 luaA_checkfunction(L
, 2);
443 signal_connect(&global_signals
, name
, luaA_object_ref(L
, 2));
447 /** Remove a global signal.
448 * \param L The Lua VM state.
449 * \return The number of elements pushed on stack.
451 * \lparam A string with the event name.
452 * \lparam The function to call.
455 luaA_awesome_disconnect_signal(lua_State
*L
)
457 const char *name
= luaL_checkstring(L
, 1);
458 luaA_checkfunction(L
, 2);
459 const void *func
= lua_topointer(L
, 2);
460 signal_disconnect(&global_signals
, name
, func
);
461 luaA_object_unref(L
, (void *) func
);
465 /** Emit a global signal.
466 * \param L The Lua VM state.
467 * \return The number of elements pushed on stack.
469 * \lparam A string with the event name.
470 * \lparam The function to call.
473 luaA_awesome_emit_signal(lua_State
*L
)
475 signal_object_emit(L
, &global_signals
, luaL_checkstring(L
, 1), lua_gettop(L
) - 1);
480 luaA_panic(lua_State
*L
)
482 warn("unprotected error in call to Lua API (%s)",
483 lua_tostring(L
, -1));
486 warn("dumping backtrace\n%s", buf
.s
);
487 warn("restarting awesome");
493 luaA_dofunction_on_error(lua_State
*L
)
495 /* duplicate string error */
496 lua_pushvalue(L
, -1);
497 /* emit error signal */
498 signal_object_emit(L
, &global_signals
, "debug::error", 1);
500 if(!luaL_dostring(L
, "return debug.traceback(\"error while running function\", 3)"))
502 /* Move traceback before error */
504 /* Insert sentence */
505 lua_pushliteral(L
, "\nerror: ");
506 /* Move it before error */
513 /** Initialize the Lua VM
514 * \param xdg An xdg handle to use to get XDG basedir.
517 luaA_init(xdgHandle
* xdg
)
520 static const struct luaL_Reg awesome_lib
[] =
522 { "quit", luaA_quit
},
523 { "exec", luaA_exec
},
524 { "spawn", luaA_spawn
},
525 { "restart", luaA_restart
},
526 { "connect_signal", luaA_awesome_connect_signal
},
527 { "disconnect_signal", luaA_awesome_disconnect_signal
},
528 { "emit_signal", luaA_awesome_emit_signal
},
529 { "systray", luaA_systray
},
530 { "load_image", luaA_load_image
},
531 { "__index", luaA_awesome_index
},
535 L
= globalconf
.L
= luaL_newstate();
537 /* Set panic function */
538 lua_atpanic(L
, luaA_panic
);
540 /* Set error handling function */
541 lualib_dofunction_on_error
= luaA_dofunction_on_error
;
547 luaA_object_setup(L
);
549 /* Export awesome lib */
550 luaA_openlib(L
, "awesome", awesome_lib
, awesome_lib
);
552 /* Export root lib */
553 luaA_registerlib(L
, "root", awesome_root_lib
);
554 lua_pop(L
, 1); /* luaA_registerlib() leaves the table on stack */
557 /* Export D-Bus lib */
558 luaA_registerlib(L
, "dbus", awesome_dbus_lib
);
559 lua_pop(L
, 1); /* luaA_registerlib() leaves the table on stack */
562 /* Export keygrabber lib */
563 luaA_registerlib(L
, "keygrabber", awesome_keygrabber_lib
);
564 lua_pop(L
, 1); /* luaA_registerlib() leaves the table on stack */
566 /* Export mousegrabber lib */
567 luaA_registerlib(L
, "mousegrabber", awesome_mousegrabber_lib
);
568 lua_pop(L
, 1); /* luaA_registerlib() leaves the table on stack */
571 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
574 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
577 button_class_setup(L
);
583 window_class_setup(L
);
585 /* Export drawable */
586 drawable_class_setup(L
);
589 drawin_class_setup(L
);
592 client_class_setup(L
);
598 timer_class_setup(L
);
600 /* add Lua search paths */
601 lua_getglobal(L
, "package");
602 if (LUA_TTABLE
!= lua_type(L
, 1))
604 warn("package is not a table");
607 lua_getfield(L
, 1, "path");
608 if (LUA_TSTRING
!= lua_type(L
, 2))
610 warn("package.path is not a string");
615 /* add XDG_CONFIG_DIR as include path */
616 const char * const *xdgconfigdirs
= xdgSearchableConfigDirectories(xdg
);
617 for(; *xdgconfigdirs
; xdgconfigdirs
++)
619 size_t len
= a_strlen(*xdgconfigdirs
);
620 lua_pushliteral(L
, ";");
621 lua_pushlstring(L
, *xdgconfigdirs
, len
);
622 lua_pushliteral(L
, "/awesome/?.lua");
625 lua_pushliteral(L
, ";");
626 lua_pushlstring(L
, *xdgconfigdirs
, len
);
627 lua_pushliteral(L
, "/awesome/?/init.lua");
630 lua_concat(L
, 3); /* concatenate with package.path */
633 /* add Lua lib path (/usr/share/awesome/lib by default) */
634 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?.lua");
635 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?/init.lua");
636 lua_concat(L
, 3); /* concatenate with package.path */
637 lua_setfield(L
, 1, "path"); /* package.path = "concatenated string" */
639 lua_getfield(L
, 1, "loaded");
641 lua_pop(L
, 2); /* pop "package" and "package.loaded" */
643 signal_add(&global_signals
, "debug::error");
644 signal_add(&global_signals
, "debug::deprecation");
645 signal_add(&global_signals
, "debug::index::miss");
646 signal_add(&global_signals
, "debug::newindex::miss");
647 signal_add(&global_signals
, "systray::update");
648 signal_add(&global_signals
, "wallpaper_changed");
649 signal_add(&global_signals
, "refresh");
650 signal_add(&global_signals
, "exit");
654 luaA_startup_error(const char *err
)
656 if (globalconf
.startup_errors
.len
> 0)
657 buffer_addsl(&globalconf
.startup_errors
, "\n\n");
658 buffer_adds(&globalconf
.startup_errors
, err
);
662 luaA_loadrc(const char *confpath
, bool run
)
664 if(!luaL_loadfile(globalconf
.L
, confpath
))
668 /* Set the conffile right now so it can be used inside the
669 * configuration file. */
670 conffile
= a_strdup(confpath
);
671 if(lua_pcall(globalconf
.L
, 0, LUA_MULTRET
, 0))
673 const char *err
= lua_tostring(globalconf
.L
, -1);
674 luaA_startup_error(err
);
675 fprintf(stderr
, "%s\n", err
);
676 /* An error happened, so reset this. */
684 lua_pop(globalconf
.L
, 1);
690 const char *err
= lua_tostring(globalconf
.L
, -1);
691 luaA_startup_error(err
);
692 fprintf(stderr
, "%s\n", err
);
698 /** Load a configuration file.
699 * \param xdg An xdg handle to use to get XDG basedir.
700 * \param confpatharg The configuration file to load.
701 * \param run Run the configuration file.
704 luaA_parserc(xdgHandle
* xdg
, const char *confpatharg
, bool run
)
706 char *confpath
= NULL
;
709 /* try to load, return if it's ok */
712 if(luaA_loadrc(confpatharg
, run
))
721 confpath
= xdgConfigFind("awesome/rc.lua", xdg
);
723 char *tmp
= confpath
;
725 /* confpath is "string1\0string2\0string3\0\0" */
728 if(luaA_loadrc(tmp
, run
))
735 tmp
+= a_strlen(tmp
) + 1;
746 luaA_class_index_miss_property(lua_State
*L
, lua_object_t
*obj
)
748 signal_object_emit(L
, &global_signals
, "debug::index::miss", 2);
753 luaA_class_newindex_miss_property(lua_State
*L
, lua_object_t
*obj
)
755 signal_object_emit(L
, &global_signals
, "debug::newindex::miss", 3);
762 signal_object_emit(globalconf
.L
, &global_signals
, "refresh", 0);
765 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80