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>
37 #include "objects/timer.h"
38 #include "awesome-version-internal.h"
42 #include "objects/tag.h"
43 #include "objects/client.h"
44 #include "objects/drawin.h"
47 #include "selection.h"
49 #include "common/xcursor.h"
50 #include "common/buffer.h"
51 #include "common/backtrace.h"
54 extern const struct luaL_reg awesome_dbus_lib
[];
56 extern const struct luaL_reg awesome_keygrabber_lib
[];
57 extern const struct luaL_reg awesome_mousegrabber_lib
[];
58 extern const struct luaL_reg awesome_root_lib
[];
59 extern const struct luaL_reg awesome_mouse_methods
[];
60 extern const struct luaL_reg awesome_mouse_meta
[];
61 extern const struct luaL_reg awesome_screen_methods
[];
62 extern const struct luaL_reg awesome_screen_meta
[];
64 /** Path to config file */
65 static char *conffile
;
68 * \param L The Lua VM state.
69 * \return The number of elements pushed on stack.
72 luaA_quit(lua_State
*L
)
74 ev_unloop(globalconf
.loop
, 1);
78 /** Execute another application, probably a window manager, to replace
80 * \param L The Lua VM state.
81 * \return The number of elements pushed on stack.
83 * \lparam The command line to execute.
86 luaA_exec(lua_State
*L
)
88 const char *cmd
= luaL_checkstring(L
, 1);
99 luaA_restart(lua_State
*L
)
105 /** UTF-8 aware string length computing.
106 * \param L The Lua VM state.
107 * \return The number of elements pushed on stack.
110 luaA_mbstrlen(lua_State
*L
)
112 const char *cmd
= luaL_checkstring(L
, 1);
113 lua_pushnumber(L
, (ssize_t
) mbstowcs(NULL
, NONULL(cmd
), 0));
117 /** Overload standard Lua next function to use __next key on metatable.
118 * \param L The Lua VM state.
119 * \return The number of elements pushed on stack.
122 luaAe_next(lua_State
*L
)
124 if(luaL_getmetafield(L
, 1, "__next"))
127 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
128 return lua_gettop(L
);
131 luaL_checktype(L
, 1, LUA_TTABLE
);
139 /** Overload lua_next() function by using __next metatable field
140 * to get next elements.
141 * \param L The Lua VM stack.
142 * \param idx The index number of elements in stack.
143 * \return 1 if more elements to come, 0 otherwise.
146 luaA_next(lua_State
*L
, int idx
)
148 if(luaL_getmetafield(L
, idx
, "__next"))
150 /* if idx is relative, reduce it since we got __next */
152 /* copy table and then move key */
153 lua_pushvalue(L
, idx
);
154 lua_pushvalue(L
, -3);
156 lua_pcall(L
, 2, 2, 0);
157 /* next returned nil, it's the end */
166 else if(lua_istable(L
, idx
))
167 return lua_next(L
, idx
);
173 /** Generic pairs function.
174 * \param L The Lua VM state.
175 * \return The number of elements pushed on stack.
178 luaA_generic_pairs(lua_State
*L
)
180 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
181 lua_pushvalue(L
, 1); /* state, */
182 lua_pushnil(L
); /* and initial value */
186 /** Overload standard pairs function to use __pairs field of metatables.
187 * \param L The Lua VM state.
188 * \return The number of elements pushed on stack.
191 luaAe_pairs(lua_State
*L
)
193 if(luaL_getmetafield(L
, 1, "__pairs"))
196 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
197 return lua_gettop(L
);
200 luaL_checktype(L
, 1, LUA_TTABLE
);
201 return luaA_generic_pairs(L
);
205 luaA_ipairs_aux(lua_State
*L
)
207 int i
= luaL_checkint(L
, 2) + 1;
208 luaL_checktype(L
, 1, LUA_TTABLE
);
209 lua_pushinteger(L
, i
);
210 lua_rawgeti(L
, 1, i
);
211 return (lua_isnil(L
, -1)) ? 0 : 2;
214 /** Overload standard ipairs function to use __ipairs field of metatables.
215 * \param L The Lua VM state.
216 * \return The number of elements pushed on stack.
219 luaAe_ipairs(lua_State
*L
)
221 if(luaL_getmetafield(L
, 1, "__ipairs"))
224 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
225 return lua_gettop(L
);
228 luaL_checktype(L
, 1, LUA_TTABLE
);
229 lua_pushvalue(L
, lua_upvalueindex(1));
231 lua_pushinteger(L
, 0); /* and initial value */
235 /** Enhanced type() function which recognize awesome objects.
236 * \param L The Lua VM state.
237 * \return The number of arguments pushed on the stack.
240 luaAe_type(lua_State
*L
)
243 lua_pushstring(L
, luaA_typename(L
, 1));
247 /** Replace various standards Lua functions with our own.
248 * \param L The Lua VM state.
251 luaA_fixups(lua_State
*L
)
253 /* export string.wlen */
254 lua_getglobal(L
, "string");
255 lua_pushcfunction(L
, luaA_mbstrlen
);
256 lua_setfield(L
, -2, "wlen");
259 lua_pushliteral(L
, "next");
260 lua_pushcfunction(L
, luaAe_next
);
261 lua_settable(L
, LUA_GLOBALSINDEX
);
263 lua_pushliteral(L
, "pairs");
264 lua_pushcfunction(L
, luaAe_next
);
265 lua_pushcclosure(L
, luaAe_pairs
, 1); /* pairs get next as upvalue */
266 lua_settable(L
, LUA_GLOBALSINDEX
);
268 lua_pushliteral(L
, "ipairs");
269 lua_pushcfunction(L
, luaA_ipairs_aux
);
270 lua_pushcclosure(L
, luaAe_ipairs
, 1);
271 lua_settable(L
, LUA_GLOBALSINDEX
);
273 lua_pushliteral(L
, "type");
274 lua_pushcfunction(L
, luaAe_type
);
275 lua_settable(L
, LUA_GLOBALSINDEX
);
277 lua_pushliteral(L
, "selection");
278 lua_pushcfunction(L
, luaA_selection_get
);
279 lua_settable(L
, LUA_GLOBALSINDEX
);
282 /** Look for an item: table, function, etc.
283 * \param L The Lua VM state.
284 * \param item The pointer item.
287 luaA_hasitem(lua_State
*L
, const void *item
)
290 while(luaA_next(L
, -2))
292 if(lua_topointer(L
, -1) == item
)
294 /* remove value and key */
298 if(lua_istable(L
, -1))
299 if(luaA_hasitem(L
, item
))
301 /* remove key and value */
311 /** Browse a table pushed on top of the index, and put all its table and
312 * sub-table into an array.
313 * \param L The Lua VM state.
314 * \param elems The elements array.
315 * \return False if we encounter an elements already in list.
318 luaA_isloop_check(lua_State
*L
, cptr_array_t
*elems
)
320 if(lua_istable(L
, -1))
322 const void *object
= lua_topointer(L
, -1);
324 /* Check that the object table is not already in the list */
325 for(int i
= 0; i
< elems
->len
; i
++)
326 if(elems
->tab
[i
] == object
)
329 /* push the table in the elements list */
330 cptr_array_append(elems
, object
);
332 /* look every object in the "table" */
334 while(luaA_next(L
, -2))
336 if(!luaA_isloop_check(L
, elems
))
338 /* remove key and value */
342 /* remove value, keep key for next iteration */
349 /** Check if a table is a loop. When using tables as direct acyclic digram,
351 * \param L The Lua VM state.
352 * \param idx The index of the table in the stack
353 * \return True if the table loops.
356 luaA_isloop(lua_State
*L
, int idx
)
358 /* elems is an elements array that we will fill with all array we
359 * encounter while browsing the tables */
362 cptr_array_init(&elems
);
364 /* push table on top */
365 lua_pushvalue(L
, idx
);
367 bool ret
= luaA_isloop_check(L
, &elems
);
369 /* remove pushed table */
372 cptr_array_wipe(&elems
);
377 /** awesome global table.
378 * \param L The Lua VM state.
379 * \return The number of elements pushed on stack.
381 * \lfield font The default font.
382 * \lfield font_height The default font height.
383 * \lfield conffile The configuration file which has been loaded.
386 luaA_awesome_index(lua_State
*L
)
388 if(luaA_usemetatable(L
, 1, 2))
391 const char *buf
= luaL_checkstring(L
, 2);
393 if(a_strcmp(buf
, "font") == 0)
395 char *font
= pango_font_description_to_string(globalconf
.font
->desc
);
396 lua_pushstring(L
, font
);
399 else if(a_strcmp(buf
, "font_height") == 0)
400 lua_pushnumber(L
, globalconf
.font
->height
);
401 else if(a_strcmp(buf
, "conffile") == 0)
402 lua_pushstring(L
, conffile
);
403 else if(a_strcmp(buf
, "fg") == 0)
404 luaA_pushxcolor(L
, globalconf
.colors
.fg
);
405 else if(a_strcmp(buf
, "bg") == 0)
406 luaA_pushxcolor(L
, globalconf
.colors
.bg
);
407 else if(a_strcmp(buf
, "version") == 0)
408 lua_pushliteral(L
, AWESOME_VERSION
);
409 else if(a_strcmp(buf
, "release") == 0)
410 lua_pushliteral(L
, AWESOME_RELEASE
);
417 /** Newindex function for the awesome global table.
418 * \param L The Lua VM state.
419 * \return The number of elements pushed on stack.
422 luaA_awesome_newindex(lua_State
*L
)
424 if(luaA_usemetatable(L
, 1, 2))
427 const char *buf
= luaL_checkstring(L
, 2);
429 if(a_strcmp(buf
, "font") == 0)
431 const char *newfont
= luaL_checkstring(L
, 3);
432 font_delete(&globalconf
.font
);
433 globalconf
.font
= font_new(newfont
);
435 else if(a_strcmp(buf
, "fg") == 0)
438 if((buf
= luaL_checklstring(L
, 3, &len
)))
439 xcolor_init_reply(xcolor_init_unchecked(&globalconf
.colors
.fg
, buf
, len
));
441 else if(a_strcmp(buf
, "bg") == 0)
444 if((buf
= luaL_checklstring(L
, 3, &len
)))
445 xcolor_init_reply(xcolor_init_unchecked(&globalconf
.colors
.bg
, buf
, len
));
451 /** Add 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_connect_signal(lua_State
*L
)
461 const char *name
= luaL_checkstring(L
, 1);
462 luaA_checkfunction(L
, 2);
463 signal_connect(&global_signals
, name
, luaA_object_ref(L
, 2));
467 /** Remove a global signal.
468 * \param L The Lua VM state.
469 * \return The number of elements pushed on stack.
471 * \lparam A string with the event name.
472 * \lparam The function to call.
475 luaA_awesome_disconnect_signal(lua_State
*L
)
477 const char *name
= luaL_checkstring(L
, 1);
478 luaA_checkfunction(L
, 2);
479 const void *func
= lua_topointer(L
, 2);
480 signal_disconnect(&global_signals
, name
, func
);
481 luaA_object_unref(L
, (void *) func
);
485 /** Emit a global signal.
486 * \param L The Lua VM state.
487 * \return The number of elements pushed on stack.
489 * \lparam A string with the event name.
490 * \lparam The function to call.
493 luaA_awesome_emit_signal(lua_State
*L
)
495 signal_object_emit(L
, &global_signals
, luaL_checkstring(L
, 1), lua_gettop(L
) - 1);
500 luaA_panic(lua_State
*L
)
502 warn("unprotected error in call to Lua API (%s)",
503 lua_tostring(L
, -1));
506 warn("dumping backtrace\n%s", buf
.s
);
507 warn("restarting awesome");
513 luaA_dofunction_on_error(lua_State
*L
)
515 /* duplicate string error */
516 lua_pushvalue(L
, -1);
517 /* emit error signal */
518 signal_object_emit(L
, &global_signals
, "debug::error", 1);
520 if(!luaL_dostring(L
, "return debug.traceback(\"error while running function\", 3)"))
522 /* Move traceback before error */
524 /* Insert sentence */
525 lua_pushliteral(L
, "\nerror: ");
526 /* Move it before error */
533 /** Initialize the Lua VM
534 * \param xdg An xdg handle to use to get XDG basedir.
537 luaA_init(xdgHandle
* xdg
)
540 static const struct luaL_reg awesome_lib
[] =
542 { "quit", luaA_quit
},
543 { "exec", luaA_exec
},
544 { "spawn", luaA_spawn
},
545 { "restart", luaA_restart
},
546 { "connect_signal", luaA_awesome_connect_signal
},
547 { "disconnect_signal", luaA_awesome_disconnect_signal
},
548 { "emit_signal", luaA_awesome_emit_signal
},
549 { "systray", luaA_systray
},
550 { "__index", luaA_awesome_index
},
551 { "__newindex", luaA_awesome_newindex
},
555 L
= globalconf
.L
= luaL_newstate();
557 /* Set panic function */
558 lua_atpanic(L
, luaA_panic
);
560 /* Set error handling function */
561 lualib_dofunction_on_error
= luaA_dofunction_on_error
;
567 luaA_object_setup(L
);
569 /* Export awesome lib */
570 luaA_openlib(L
, "awesome", awesome_lib
, awesome_lib
);
572 /* Export root lib */
573 luaL_register(L
, "root", awesome_root_lib
);
574 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
577 /* Export D-Bus lib */
578 luaL_register(L
, "dbus", awesome_dbus_lib
);
579 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
582 /* Export keygrabber lib */
583 luaL_register(L
, "keygrabber", awesome_keygrabber_lib
);
584 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
586 /* Export mousegrabber lib */
587 luaL_register(L
, "mousegrabber", awesome_mousegrabber_lib
);
588 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
591 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
594 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
597 button_class_setup(L
);
603 window_class_setup(L
);
606 drawin_class_setup(L
);
609 client_class_setup(L
);
615 timer_class_setup(L
);
617 /* add Lua search paths */
618 lua_getglobal(L
, "package");
619 if (LUA_TTABLE
!= lua_type(L
, 1))
621 warn("package is not a table");
624 lua_getfield(L
, 1, "path");
625 if (LUA_TSTRING
!= lua_type(L
, 2))
627 warn("package.path is not a string");
632 /* add XDG_CONFIG_DIR as include path */
633 const char * const *xdgconfigdirs
= xdgSearchableConfigDirectories(xdg
);
634 for(; *xdgconfigdirs
; xdgconfigdirs
++)
636 size_t len
= a_strlen(*xdgconfigdirs
);
637 lua_pushliteral(L
, ";");
638 lua_pushlstring(L
, *xdgconfigdirs
, len
);
639 lua_pushliteral(L
, "/awesome/?.lua");
642 lua_pushliteral(L
, ";");
643 lua_pushlstring(L
, *xdgconfigdirs
, len
);
644 lua_pushliteral(L
, "/awesome/?/init.lua");
647 lua_concat(L
, 3); /* concatenate with package.path */
650 /* add Lua lib path (/usr/share/awesome/lib by default) */
651 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?.lua");
652 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?/init.lua");
653 lua_concat(L
, 3); /* concatenate with package.path */
654 lua_setfield(L
, 1, "path"); /* package.path = "concatenated string" */
656 lua_getfield(L
, 1, "loaded");
659 if (luaopen_oocairo(L
) != 1)
660 fatal("Loading oocairo failed");
661 lua_pushvalue(L
, 3); /* Copy the module */
662 lua_setglobal(L
, "oocairo"); /* Set the global entry */
663 lua_setfield(L
, 2, "oocairo"); /* Make it require()able */
666 if (luaopen_oopango(L
) != 1)
667 fatal("Loading oopango failed");
668 lua_pushvalue(L
, 3); /* Copy the module */
669 lua_setglobal(L
, "oopango"); /* Set the global entry */
670 lua_setfield(L
, 2, "oopango"); /* Make it require()able */
672 lua_pop(L
, 2); /* pop "package" and "package.loaded" */
674 signal_add(&global_signals
, "debug::error");
675 signal_add(&global_signals
, "debug::index::miss");
676 signal_add(&global_signals
, "debug::newindex::miss");
677 signal_add(&global_signals
, "systray::update");
678 signal_add(&global_signals
, "exit");
682 luaA_loadrc(const char *confpath
, bool run
)
684 if(!luaL_loadfile(globalconf
.L
, confpath
))
688 if(lua_pcall(globalconf
.L
, 0, LUA_MULTRET
, 0))
689 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
692 conffile
= a_strdup(confpath
);
698 lua_pop(globalconf
.L
, 1);
703 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
708 /** Load a configuration file.
709 * \param xdg An xdg handle to use to get XDG basedir.
710 * \param confpatharg The configuration file to load.
711 * \param run Run the configuration file.
714 luaA_parserc(xdgHandle
* xdg
, const char *confpatharg
, bool run
)
716 char *confpath
= NULL
;
719 /* try to load, return if it's ok */
722 if(luaA_loadrc(confpatharg
, run
))
731 confpath
= xdgConfigFind("awesome/rc.lua", xdg
);
733 char *tmp
= confpath
;
735 /* confpath is "string1\0string2\0string3\0\0" */
738 if(luaA_loadrc(tmp
, run
))
745 tmp
+= a_strlen(tmp
) + 1;
756 luaA_class_index_miss_property(lua_State
*L
, lua_object_t
*obj
)
758 signal_object_emit(L
, &global_signals
, "debug::index::miss", 2);
763 luaA_class_newindex_miss_property(lua_State
*L
, lua_object_t
*obj
)
765 signal_object_emit(L
, &global_signals
, "debug::newindex::miss", 3);
769 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80