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_pushcfunction(L
, luaAe_next
);
275 lua_setglobal(L
, "next");
277 lua_pushcfunction(L
, luaAe_next
);
278 lua_pushcclosure(L
, luaAe_pairs
, 1); /* pairs get next as upvalue */
279 lua_setglobal(L
, "pairs");
281 lua_pushcfunction(L
, luaA_ipairs_aux
);
282 lua_pushcclosure(L
, luaAe_ipairs
, 1);
283 lua_setglobal(L
, "ipairs");
285 lua_pushcfunction(L
, luaAe_type
);
286 lua_setglobal(L
, "type");
288 lua_pushcfunction(L
, luaA_selection_get
);
289 lua_setglobal(L
, "selection");
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 luaA_registerlib(L
, "root", awesome_root_lib
);
553 lua_pop(L
, 1); /* luaA_registerlib() leaves the table on stack */
556 /* Export D-Bus lib */
557 luaA_registerlib(L
, "dbus", awesome_dbus_lib
);
558 lua_pop(L
, 1); /* luaA_registerlib() leaves the table on stack */
561 /* Export keygrabber lib */
562 luaA_registerlib(L
, "keygrabber", awesome_keygrabber_lib
);
563 lua_pop(L
, 1); /* luaA_registerlib() leaves the table on stack */
565 /* Export mousegrabber lib */
566 luaA_registerlib(L
, "mousegrabber", awesome_mousegrabber_lib
);
567 lua_pop(L
, 1); /* luaA_registerlib() 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