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.
28 #include <basedir_fs.h>
32 #include "objects/timer.h"
33 #include "awesome-version-internal.h"
37 #include "objects/tag.h"
38 #include "objects/client.h"
39 #include "objects/drawin.h"
40 #include "objects/drawable.h"
43 #include "selection.h"
45 #include "common/xcursor.h"
46 #include "common/buffer.h"
47 #include "common/backtrace.h"
50 extern const struct luaL_Reg awesome_dbus_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
[];
60 /** Path to config file */
61 static char *conffile
;
64 * \param L The Lua VM state.
65 * \return The number of elements pushed on stack.
68 luaA_quit(lua_State
*L
)
70 g_main_loop_quit(globalconf
.loop
);
74 /** Execute another application, probably a window manager, to replace
76 * \param L The Lua VM state.
77 * \return The number of elements pushed on stack.
79 * \lparam The command line to execute.
82 luaA_exec(lua_State
*L
)
84 const char *cmd
= luaL_checkstring(L
, 1);
86 awesome_atexit(false);
95 luaA_restart(lua_State
*L
)
101 /** Load an image from a given path.
102 * \param L The Lua VM state.
103 * \return The number of elements pushed on stack.
105 * \lparam The command line to execute.
108 luaA_load_image(lua_State
*L
)
110 const char *filename
= luaL_checkstring(L
, 1);
111 cairo_surface_t
*surface
= draw_load_image(L
, filename
);
114 /* lua has to make sure to free the ref or we have a leak */
115 lua_pushlightuserdata(L
, surface
);
119 /** UTF-8 aware string length computing.
120 * \param L The Lua VM state.
121 * \return The number of elements pushed on stack.
124 luaA_mbstrlen(lua_State
*L
)
126 const char *cmd
= luaL_checkstring(L
, 1);
127 lua_pushnumber(L
, (ssize_t
) mbstowcs(NULL
, NONULL(cmd
), 0));
131 /** Overload standard Lua next function to use __next key on metatable.
132 * \param L The Lua VM state.
133 * \return The number of elements pushed on stack.
136 luaAe_next(lua_State
*L
)
138 if(luaL_getmetafield(L
, 1, "__next"))
141 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
142 return lua_gettop(L
);
145 luaL_checktype(L
, 1, LUA_TTABLE
);
153 /** Overload lua_next() function by using __next metatable field
154 * to get next elements.
155 * \param L The Lua VM stack.
156 * \param idx The index number of elements in stack.
157 * \return 1 if more elements to come, 0 otherwise.
160 luaA_next(lua_State
*L
, int idx
)
162 if(luaL_getmetafield(L
, idx
, "__next"))
164 /* if idx is relative, reduce it since we got __next */
166 /* copy table and then move key */
167 lua_pushvalue(L
, idx
);
168 lua_pushvalue(L
, -3);
170 lua_pcall(L
, 2, 2, 0);
171 /* next returned nil, it's the end */
180 else if(lua_istable(L
, idx
))
181 return lua_next(L
, idx
);
187 /** Generic pairs function.
188 * \param L The Lua VM state.
189 * \return The number of elements pushed on stack.
192 luaA_generic_pairs(lua_State
*L
)
194 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
195 lua_pushvalue(L
, 1); /* state, */
196 lua_pushnil(L
); /* and initial value */
200 /** Overload standard pairs function to use __pairs field of metatables.
201 * \param L The Lua VM state.
202 * \return The number of elements pushed on stack.
205 luaAe_pairs(lua_State
*L
)
207 if(luaL_getmetafield(L
, 1, "__pairs"))
210 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
211 return lua_gettop(L
);
214 luaL_checktype(L
, 1, LUA_TTABLE
);
215 return luaA_generic_pairs(L
);
219 luaA_ipairs_aux(lua_State
*L
)
221 int i
= luaL_checkint(L
, 2) + 1;
222 luaL_checktype(L
, 1, LUA_TTABLE
);
223 lua_pushinteger(L
, i
);
224 lua_rawgeti(L
, 1, i
);
225 return (lua_isnil(L
, -1)) ? 0 : 2;
228 /** Overload standard ipairs function to use __ipairs field of metatables.
229 * \param L The Lua VM state.
230 * \return The number of elements pushed on stack.
233 luaAe_ipairs(lua_State
*L
)
235 if(luaL_getmetafield(L
, 1, "__ipairs"))
238 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
239 return lua_gettop(L
);
242 luaL_checktype(L
, 1, LUA_TTABLE
);
243 lua_pushvalue(L
, lua_upvalueindex(1));
245 lua_pushinteger(L
, 0); /* and initial value */
249 /** Enhanced type() function which recognize awesome objects.
250 * \param L The Lua VM state.
251 * \return The number of arguments pushed on the stack.
254 luaAe_type(lua_State
*L
)
257 lua_pushstring(L
, luaA_typename(L
, 1));
261 /** Replace various standards Lua functions with our own.
262 * \param L The Lua VM state.
265 luaA_fixups(lua_State
*L
)
267 /* export string.wlen */
268 lua_getglobal(L
, "string");
269 lua_pushcfunction(L
, luaA_mbstrlen
);
270 lua_setfield(L
, -2, "wlen");
273 lua_pushcfunction(L
, luaAe_next
);
274 lua_setglobal(L
, "next");
276 lua_pushcfunction(L
, luaAe_next
);
277 lua_pushcclosure(L
, luaAe_pairs
, 1); /* pairs get next as upvalue */
278 lua_setglobal(L
, "pairs");
280 lua_pushcfunction(L
, luaA_ipairs_aux
);
281 lua_pushcclosure(L
, luaAe_ipairs
, 1);
282 lua_setglobal(L
, "ipairs");
284 lua_pushcfunction(L
, luaAe_type
);
285 lua_setglobal(L
, "type");
287 lua_pushcfunction(L
, luaA_selection_get
);
288 lua_setglobal(L
, "selection");
291 /** Look for an item: table, function, etc.
292 * \param L The Lua VM state.
293 * \param item The pointer item.
296 luaA_hasitem(lua_State
*L
, const void *item
)
299 while(luaA_next(L
, -2))
301 if(lua_topointer(L
, -1) == item
)
303 /* remove value and key */
307 if(lua_istable(L
, -1))
308 if(luaA_hasitem(L
, item
))
310 /* remove key and value */
320 /** Browse a table pushed on top of the index, and put all its table and
321 * sub-table into an array.
322 * \param L The Lua VM state.
323 * \param elems The elements array.
324 * \return False if we encounter an elements already in list.
327 luaA_isloop_check(lua_State
*L
, cptr_array_t
*elems
)
329 if(lua_istable(L
, -1))
331 const void *object
= lua_topointer(L
, -1);
333 /* Check that the object table is not already in the list */
334 for(int i
= 0; i
< elems
->len
; i
++)
335 if(elems
->tab
[i
] == object
)
338 /* push the table in the elements list */
339 cptr_array_append(elems
, object
);
341 /* look every object in the "table" */
343 while(luaA_next(L
, -2))
345 if(!luaA_isloop_check(L
, elems
))
347 /* remove key and value */
351 /* remove value, keep key for next iteration */
358 /** Check if a table is a loop. When using tables as direct acyclic digram,
360 * \param L The Lua VM state.
361 * \param idx The index of the table in the stack
362 * \return True if the table loops.
365 luaA_isloop(lua_State
*L
, int idx
)
367 /* elems is an elements array that we will fill with all array we
368 * encounter while browsing the tables */
371 cptr_array_init(&elems
);
373 /* push table on top */
374 lua_pushvalue(L
, idx
);
376 bool ret
= luaA_isloop_check(L
, &elems
);
378 /* remove pushed table */
381 cptr_array_wipe(&elems
);
386 /** awesome global table.
387 * \param L The Lua VM state.
388 * \return The number of elements pushed on stack.
390 * \lfield font The default font.
391 * \lfield font_height The default font height.
392 * \lfield conffile The configuration file which has been loaded.
395 luaA_awesome_index(lua_State
*L
)
397 if(luaA_usemetatable(L
, 1, 2))
400 const char *buf
= luaL_checkstring(L
, 2);
402 if(A_STREQ(buf
, "conffile"))
404 lua_pushstring(L
, conffile
);
408 if(A_STREQ(buf
, "version"))
410 lua_pushliteral(L
, AWESOME_VERSION
);
414 if(A_STREQ(buf
, "release"))
416 lua_pushliteral(L
, AWESOME_RELEASE
);
420 if(A_STREQ(buf
, "startup_errors") && globalconf
.startup_errors
.len
!= 0)
422 lua_pushstring(L
, globalconf
.startup_errors
.s
);
429 /** Add a global signal.
430 * \param L The Lua VM state.
431 * \return The number of elements pushed on stack.
433 * \lparam A string with the event name.
434 * \lparam The function to call.
437 luaA_awesome_connect_signal(lua_State
*L
)
439 const char *name
= luaL_checkstring(L
, 1);
440 luaA_checkfunction(L
, 2);
441 signal_connect(&global_signals
, name
, luaA_object_ref(L
, 2));
445 /** Remove a global signal.
446 * \param L The Lua VM state.
447 * \return The number of elements pushed on stack.
449 * \lparam A string with the event name.
450 * \lparam The function to call.
453 luaA_awesome_disconnect_signal(lua_State
*L
)
455 const char *name
= luaL_checkstring(L
, 1);
456 luaA_checkfunction(L
, 2);
457 const void *func
= lua_topointer(L
, 2);
458 signal_disconnect(&global_signals
, name
, func
);
459 luaA_object_unref(L
, (void *) func
);
463 /** Emit a global signal.
464 * \param L The Lua VM state.
465 * \return The number of elements pushed on stack.
467 * \lparam A string with the event name.
468 * \lparam The function to call.
471 luaA_awesome_emit_signal(lua_State
*L
)
473 signal_object_emit(L
, &global_signals
, luaL_checkstring(L
, 1), lua_gettop(L
) - 1);
478 luaA_panic(lua_State
*L
)
480 warn("unprotected error in call to Lua API (%s)",
481 lua_tostring(L
, -1));
484 warn("dumping backtrace\n%s", buf
.s
);
485 warn("restarting awesome");
491 luaA_dofunction_on_error(lua_State
*L
)
493 /* duplicate string error */
494 lua_pushvalue(L
, -1);
495 /* emit error signal */
496 signal_object_emit(L
, &global_signals
, "debug::error", 1);
498 if(!luaL_dostring(L
, "return debug.traceback(\"error while running function\", 3)"))
500 /* Move traceback before error */
502 /* Insert sentence */
503 lua_pushliteral(L
, "\nerror: ");
504 /* Move it before error */
511 /** Initialize the Lua VM
512 * \param xdg An xdg handle to use to get XDG basedir.
515 luaA_init(xdgHandle
* xdg
)
518 static const struct luaL_Reg awesome_lib
[] =
520 { "quit", luaA_quit
},
521 { "exec", luaA_exec
},
522 { "spawn", luaA_spawn
},
523 { "restart", luaA_restart
},
524 { "connect_signal", luaA_awesome_connect_signal
},
525 { "disconnect_signal", luaA_awesome_disconnect_signal
},
526 { "emit_signal", luaA_awesome_emit_signal
},
527 { "systray", luaA_systray
},
528 { "load_image", luaA_load_image
},
529 { "__index", luaA_awesome_index
},
533 L
= globalconf
.L
= luaL_newstate();
535 /* Set panic function */
536 lua_atpanic(L
, luaA_panic
);
538 /* Set error handling function */
539 lualib_dofunction_on_error
= luaA_dofunction_on_error
;
545 luaA_object_setup(L
);
547 /* Export awesome lib */
548 luaA_openlib(L
, "awesome", awesome_lib
, awesome_lib
);
550 /* Export root lib */
551 luaA_registerlib(L
, "root", awesome_root_lib
);
552 lua_pop(L
, 1); /* luaA_registerlib() leaves the table on stack */
555 /* Export D-Bus lib */
556 luaA_registerlib(L
, "dbus", awesome_dbus_lib
);
557 lua_pop(L
, 1); /* luaA_registerlib() leaves the table on stack */
560 /* Export keygrabber lib */
561 luaA_registerlib(L
, "keygrabber", awesome_keygrabber_lib
);
562 lua_pop(L
, 1); /* luaA_registerlib() leaves the table on stack */
564 /* Export mousegrabber lib */
565 luaA_registerlib(L
, "mousegrabber", awesome_mousegrabber_lib
);
566 lua_pop(L
, 1); /* luaA_registerlib() leaves the table on stack */
569 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
572 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
575 button_class_setup(L
);
581 window_class_setup(L
);
583 /* Export drawable */
584 drawable_class_setup(L
);
587 drawin_class_setup(L
);
590 client_class_setup(L
);
596 timer_class_setup(L
);
598 /* add Lua search paths */
599 lua_getglobal(L
, "package");
600 if (LUA_TTABLE
!= lua_type(L
, 1))
602 warn("package is not a table");
605 lua_getfield(L
, 1, "path");
606 if (LUA_TSTRING
!= lua_type(L
, 2))
608 warn("package.path is not a string");
613 /* add XDG_CONFIG_DIR as include path */
614 const char * const *xdgconfigdirs
= xdgSearchableConfigDirectories(xdg
);
615 for(; *xdgconfigdirs
; xdgconfigdirs
++)
617 size_t len
= a_strlen(*xdgconfigdirs
);
618 lua_pushliteral(L
, ";");
619 lua_pushlstring(L
, *xdgconfigdirs
, len
);
620 lua_pushliteral(L
, "/awesome/?.lua");
623 lua_pushliteral(L
, ";");
624 lua_pushlstring(L
, *xdgconfigdirs
, len
);
625 lua_pushliteral(L
, "/awesome/?/init.lua");
628 lua_concat(L
, 3); /* concatenate with package.path */
631 /* add Lua lib path (/usr/share/awesome/lib by default) */
632 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?.lua");
633 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?/init.lua");
634 lua_concat(L
, 3); /* concatenate with package.path */
635 lua_setfield(L
, 1, "path"); /* package.path = "concatenated string" */
637 lua_getfield(L
, 1, "loaded");
639 lua_pop(L
, 2); /* pop "package" and "package.loaded" */
641 signal_add(&global_signals
, "debug::error");
642 signal_add(&global_signals
, "debug::deprecation");
643 signal_add(&global_signals
, "debug::index::miss");
644 signal_add(&global_signals
, "debug::newindex::miss");
645 signal_add(&global_signals
, "systray::update");
646 signal_add(&global_signals
, "wallpaper_changed");
647 signal_add(&global_signals
, "refresh");
648 signal_add(&global_signals
, "exit");
652 luaA_startup_error(const char *err
)
654 if (globalconf
.startup_errors
.len
> 0)
655 buffer_addsl(&globalconf
.startup_errors
, "\n\n");
656 buffer_adds(&globalconf
.startup_errors
, err
);
660 luaA_loadrc(const char *confpath
, bool run
)
662 if(!luaL_loadfile(globalconf
.L
, confpath
))
666 /* Set the conffile right now so it can be used inside the
667 * configuration file. */
668 conffile
= a_strdup(confpath
);
669 if(lua_pcall(globalconf
.L
, 0, LUA_MULTRET
, 0))
671 const char *err
= lua_tostring(globalconf
.L
, -1);
672 luaA_startup_error(err
);
673 fprintf(stderr
, "%s\n", err
);
674 /* An error happened, so reset this. */
682 lua_pop(globalconf
.L
, 1);
688 const char *err
= lua_tostring(globalconf
.L
, -1);
689 luaA_startup_error(err
);
690 fprintf(stderr
, "%s\n", err
);
696 /** Load a configuration file.
697 * \param xdg An xdg handle to use to get XDG basedir.
698 * \param confpatharg The configuration file to load.
699 * \param run Run the configuration file.
702 luaA_parserc(xdgHandle
* xdg
, const char *confpatharg
, bool run
)
704 char *confpath
= NULL
;
707 /* try to load, return if it's ok */
710 if(luaA_loadrc(confpatharg
, run
))
719 confpath
= xdgConfigFind("awesome/rc.lua", xdg
);
721 char *tmp
= confpath
;
723 /* confpath is "string1\0string2\0string3\0\0" */
726 if(luaA_loadrc(tmp
, run
))
733 tmp
+= a_strlen(tmp
) + 1;
744 luaA_class_index_miss_property(lua_State
*L
, lua_object_t
*obj
)
746 signal_object_emit(L
, &global_signals
, "debug::index::miss", 2);
751 luaA_class_newindex_miss_property(lua_State
*L
, lua_object_t
*obj
)
753 signal_object_emit(L
, &global_signals
, "debug::newindex::miss", 3);
760 signal_object_emit(globalconf
.L
, &global_signals
, "refresh", 0);
763 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80