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);
90 awesome_atexit(false);
99 luaA_restart(lua_State
*L
)
105 /** Load an image from a given path.
106 * \param L The Lua VM state.
107 * \return The number of elements pushed on stack.
109 * \lparam The command line to execute.
112 luaA_load_image(lua_State
*L
)
114 const char *filename
= luaL_checkstring(L
, 1);
115 return draw_load_image(L
, filename
);
118 /** UTF-8 aware string length computing.
119 * \param L The Lua VM state.
120 * \return The number of elements pushed on stack.
123 luaA_mbstrlen(lua_State
*L
)
125 const char *cmd
= luaL_checkstring(L
, 1);
126 lua_pushnumber(L
, (ssize_t
) mbstowcs(NULL
, NONULL(cmd
), 0));
130 /** Overload standard Lua next function to use __next key on metatable.
131 * \param L The Lua VM state.
132 * \return The number of elements pushed on stack.
135 luaAe_next(lua_State
*L
)
137 if(luaL_getmetafield(L
, 1, "__next"))
140 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
141 return lua_gettop(L
);
144 luaL_checktype(L
, 1, LUA_TTABLE
);
152 /** Overload lua_next() function by using __next metatable field
153 * to get next elements.
154 * \param L The Lua VM stack.
155 * \param idx The index number of elements in stack.
156 * \return 1 if more elements to come, 0 otherwise.
159 luaA_next(lua_State
*L
, int idx
)
161 if(luaL_getmetafield(L
, idx
, "__next"))
163 /* if idx is relative, reduce it since we got __next */
165 /* copy table and then move key */
166 lua_pushvalue(L
, idx
);
167 lua_pushvalue(L
, -3);
169 lua_pcall(L
, 2, 2, 0);
170 /* next returned nil, it's the end */
179 else if(lua_istable(L
, idx
))
180 return lua_next(L
, idx
);
186 /** Generic pairs function.
187 * \param L The Lua VM state.
188 * \return The number of elements pushed on stack.
191 luaA_generic_pairs(lua_State
*L
)
193 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
194 lua_pushvalue(L
, 1); /* state, */
195 lua_pushnil(L
); /* and initial value */
199 /** Overload standard pairs function to use __pairs field of metatables.
200 * \param L The Lua VM state.
201 * \return The number of elements pushed on stack.
204 luaAe_pairs(lua_State
*L
)
206 if(luaL_getmetafield(L
, 1, "__pairs"))
209 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
210 return lua_gettop(L
);
213 luaL_checktype(L
, 1, LUA_TTABLE
);
214 return luaA_generic_pairs(L
);
218 luaA_ipairs_aux(lua_State
*L
)
220 int i
= luaL_checkint(L
, 2) + 1;
221 luaL_checktype(L
, 1, LUA_TTABLE
);
222 lua_pushinteger(L
, i
);
223 lua_rawgeti(L
, 1, i
);
224 return (lua_isnil(L
, -1)) ? 0 : 2;
227 /** Overload standard ipairs function to use __ipairs field of metatables.
228 * \param L The Lua VM state.
229 * \return The number of elements pushed on stack.
232 luaAe_ipairs(lua_State
*L
)
234 if(luaL_getmetafield(L
, 1, "__ipairs"))
237 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
238 return lua_gettop(L
);
241 luaL_checktype(L
, 1, LUA_TTABLE
);
242 lua_pushvalue(L
, lua_upvalueindex(1));
244 lua_pushinteger(L
, 0); /* and initial value */
248 /** Enhanced type() function which recognize awesome objects.
249 * \param L The Lua VM state.
250 * \return The number of arguments pushed on the stack.
253 luaAe_type(lua_State
*L
)
256 lua_pushstring(L
, luaA_typename(L
, 1));
260 /** Replace various standards Lua functions with our own.
261 * \param L The Lua VM state.
264 luaA_fixups(lua_State
*L
)
266 /* export string.wlen */
267 lua_getglobal(L
, "string");
268 lua_pushcfunction(L
, luaA_mbstrlen
);
269 lua_setfield(L
, -2, "wlen");
272 lua_pushliteral(L
, "next");
273 lua_pushcfunction(L
, luaAe_next
);
274 lua_settable(L
, LUA_GLOBALSINDEX
);
276 lua_pushliteral(L
, "pairs");
277 lua_pushcfunction(L
, luaAe_next
);
278 lua_pushcclosure(L
, luaAe_pairs
, 1); /* pairs get next as upvalue */
279 lua_settable(L
, LUA_GLOBALSINDEX
);
281 lua_pushliteral(L
, "ipairs");
282 lua_pushcfunction(L
, luaA_ipairs_aux
);
283 lua_pushcclosure(L
, luaAe_ipairs
, 1);
284 lua_settable(L
, LUA_GLOBALSINDEX
);
286 lua_pushliteral(L
, "type");
287 lua_pushcfunction(L
, luaAe_type
);
288 lua_settable(L
, LUA_GLOBALSINDEX
);
290 lua_pushliteral(L
, "selection");
291 lua_pushcfunction(L
, luaA_selection_get
);
292 lua_settable(L
, LUA_GLOBALSINDEX
);
295 /** Look for an item: table, function, etc.
296 * \param L The Lua VM state.
297 * \param item The pointer item.
300 luaA_hasitem(lua_State
*L
, const void *item
)
303 while(luaA_next(L
, -2))
305 if(lua_topointer(L
, -1) == item
)
307 /* remove value and key */
311 if(lua_istable(L
, -1))
312 if(luaA_hasitem(L
, item
))
314 /* remove key and value */
324 /** Browse a table pushed on top of the index, and put all its table and
325 * sub-table into an array.
326 * \param L The Lua VM state.
327 * \param elems The elements array.
328 * \return False if we encounter an elements already in list.
331 luaA_isloop_check(lua_State
*L
, cptr_array_t
*elems
)
333 if(lua_istable(L
, -1))
335 const void *object
= lua_topointer(L
, -1);
337 /* Check that the object table is not already in the list */
338 for(int i
= 0; i
< elems
->len
; i
++)
339 if(elems
->tab
[i
] == object
)
342 /* push the table in the elements list */
343 cptr_array_append(elems
, object
);
345 /* look every object in the "table" */
347 while(luaA_next(L
, -2))
349 if(!luaA_isloop_check(L
, elems
))
351 /* remove key and value */
355 /* remove value, keep key for next iteration */
362 /** Check if a table is a loop. When using tables as direct acyclic digram,
364 * \param L The Lua VM state.
365 * \param idx The index of the table in the stack
366 * \return True if the table loops.
369 luaA_isloop(lua_State
*L
, int idx
)
371 /* elems is an elements array that we will fill with all array we
372 * encounter while browsing the tables */
375 cptr_array_init(&elems
);
377 /* push table on top */
378 lua_pushvalue(L
, idx
);
380 bool ret
= luaA_isloop_check(L
, &elems
);
382 /* remove pushed table */
385 cptr_array_wipe(&elems
);
390 /** awesome global table.
391 * \param L The Lua VM state.
392 * \return The number of elements pushed on stack.
394 * \lfield font The default font.
395 * \lfield font_height The default font height.
396 * \lfield conffile The configuration file which has been loaded.
399 luaA_awesome_index(lua_State
*L
)
401 if(luaA_usemetatable(L
, 1, 2))
404 const char *buf
= luaL_checkstring(L
, 2);
406 if(A_STREQ(buf
, "conffile"))
408 lua_pushstring(L
, conffile
);
412 if(A_STREQ(buf
, "version"))
414 lua_pushliteral(L
, AWESOME_VERSION
);
418 if(A_STREQ(buf
, "release"))
420 lua_pushliteral(L
, AWESOME_RELEASE
);
424 if(A_STREQ(buf
, "startup_errors") && globalconf
.startup_errors
.len
!= 0)
426 lua_pushstring(L
, globalconf
.startup_errors
.s
);
433 /** Add a global signal.
434 * \param L The Lua VM state.
435 * \return The number of elements pushed on stack.
437 * \lparam A string with the event name.
438 * \lparam The function to call.
441 luaA_awesome_connect_signal(lua_State
*L
)
443 const char *name
= luaL_checkstring(L
, 1);
444 luaA_checkfunction(L
, 2);
445 signal_connect(&global_signals
, name
, luaA_object_ref(L
, 2));
449 /** Remove a global signal.
450 * \param L The Lua VM state.
451 * \return The number of elements pushed on stack.
453 * \lparam A string with the event name.
454 * \lparam The function to call.
457 luaA_awesome_disconnect_signal(lua_State
*L
)
459 const char *name
= luaL_checkstring(L
, 1);
460 luaA_checkfunction(L
, 2);
461 const void *func
= lua_topointer(L
, 2);
462 signal_disconnect(&global_signals
, name
, func
);
463 luaA_object_unref(L
, (void *) func
);
467 /** Emit 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_emit_signal(lua_State
*L
)
477 signal_object_emit(L
, &global_signals
, luaL_checkstring(L
, 1), lua_gettop(L
) - 1);
482 luaA_panic(lua_State
*L
)
484 warn("unprotected error in call to Lua API (%s)",
485 lua_tostring(L
, -1));
488 warn("dumping backtrace\n%s", buf
.s
);
489 warn("restarting awesome");
495 luaA_dofunction_on_error(lua_State
*L
)
497 /* duplicate string error */
498 lua_pushvalue(L
, -1);
499 /* emit error signal */
500 signal_object_emit(L
, &global_signals
, "debug::error", 1);
502 if(!luaL_dostring(L
, "return debug.traceback(\"error while running function\", 3)"))
504 /* Move traceback before error */
506 /* Insert sentence */
507 lua_pushliteral(L
, "\nerror: ");
508 /* Move it before error */
515 /** Initialize the Lua VM
516 * \param xdg An xdg handle to use to get XDG basedir.
519 luaA_init(xdgHandle
* xdg
)
522 static const struct luaL_reg awesome_lib
[] =
524 { "quit", luaA_quit
},
525 { "exec", luaA_exec
},
526 { "spawn", luaA_spawn
},
527 { "restart", luaA_restart
},
528 { "connect_signal", luaA_awesome_connect_signal
},
529 { "disconnect_signal", luaA_awesome_disconnect_signal
},
530 { "emit_signal", luaA_awesome_emit_signal
},
531 { "systray", luaA_systray
},
532 { "load_image", luaA_load_image
},
533 { "__index", luaA_awesome_index
},
537 L
= globalconf
.L
= luaL_newstate();
539 /* Set panic function */
540 lua_atpanic(L
, luaA_panic
);
542 /* Set error handling function */
543 lualib_dofunction_on_error
= luaA_dofunction_on_error
;
549 luaA_object_setup(L
);
551 /* Export awesome lib */
552 luaA_openlib(L
, "awesome", awesome_lib
, awesome_lib
);
554 /* Export root lib */
555 luaL_register(L
, "root", awesome_root_lib
);
556 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
559 /* Export D-Bus lib */
560 luaL_register(L
, "dbus", awesome_dbus_lib
);
561 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
564 /* Export keygrabber lib */
565 luaL_register(L
, "keygrabber", awesome_keygrabber_lib
);
566 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
568 /* Export mousegrabber lib */
569 luaL_register(L
, "mousegrabber", awesome_mousegrabber_lib
);
570 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
573 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
576 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
579 button_class_setup(L
);
585 window_class_setup(L
);
588 drawin_class_setup(L
);
591 client_class_setup(L
);
597 timer_class_setup(L
);
599 /* add Lua search paths */
600 lua_getglobal(L
, "package");
601 if (LUA_TTABLE
!= lua_type(L
, 1))
603 warn("package is not a table");
606 lua_getfield(L
, 1, "path");
607 if (LUA_TSTRING
!= lua_type(L
, 2))
609 warn("package.path is not a string");
614 /* add XDG_CONFIG_DIR as include path */
615 const char * const *xdgconfigdirs
= xdgSearchableConfigDirectories(xdg
);
616 for(; *xdgconfigdirs
; xdgconfigdirs
++)
618 size_t len
= a_strlen(*xdgconfigdirs
);
619 lua_pushliteral(L
, ";");
620 lua_pushlstring(L
, *xdgconfigdirs
, len
);
621 lua_pushliteral(L
, "/awesome/?.lua");
624 lua_pushliteral(L
, ";");
625 lua_pushlstring(L
, *xdgconfigdirs
, len
);
626 lua_pushliteral(L
, "/awesome/?/init.lua");
629 lua_concat(L
, 3); /* concatenate with package.path */
632 /* add Lua lib path (/usr/share/awesome/lib by default) */
633 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?.lua");
634 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?/init.lua");
635 lua_concat(L
, 3); /* concatenate with package.path */
636 lua_setfield(L
, 1, "path"); /* package.path = "concatenated string" */
638 lua_getfield(L
, 1, "loaded");
641 if (luaopen_oocairo(L
) != 1)
642 fatal("Loading oocairo failed");
643 lua_pushvalue(L
, 3); /* Copy the module */
644 lua_setglobal(L
, "oocairo"); /* Set the global entry */
645 lua_setfield(L
, 2, "oocairo"); /* Make it require()able */
648 if (luaopen_oopango(L
) != 1)
649 fatal("Loading oopango failed");
650 lua_pushvalue(L
, 3); /* Copy the module */
651 lua_setglobal(L
, "oopango"); /* Set the global entry */
652 lua_setfield(L
, 2, "oopango"); /* Make it require()able */
654 lua_pop(L
, 2); /* pop "package" and "package.loaded" */
656 signal_add(&global_signals
, "debug::error");
657 signal_add(&global_signals
, "debug::deprecation");
658 signal_add(&global_signals
, "debug::index::miss");
659 signal_add(&global_signals
, "debug::newindex::miss");
660 signal_add(&global_signals
, "systray::update");
661 signal_add(&global_signals
, "wallpaper_changed");
662 signal_add(&global_signals
, "refresh");
663 signal_add(&global_signals
, "exit");
667 luaA_startup_error(const char *err
)
669 if (globalconf
.startup_errors
.len
> 0)
670 buffer_addsl(&globalconf
.startup_errors
, "\n\n");
671 buffer_adds(&globalconf
.startup_errors
, err
);
675 luaA_loadrc(const char *confpath
, bool run
)
677 if(!luaL_loadfile(globalconf
.L
, confpath
))
681 /* Set the conffile right now so it can be used inside the
682 * configuration file. */
683 conffile
= a_strdup(confpath
);
684 if(lua_pcall(globalconf
.L
, 0, LUA_MULTRET
, 0))
686 const char *err
= lua_tostring(globalconf
.L
, -1);
687 luaA_startup_error(err
);
688 fprintf(stderr
, "%s\n", err
);
689 /* An error happened, so reset this. */
697 lua_pop(globalconf
.L
, 1);
703 const char *err
= lua_tostring(globalconf
.L
, -1);
704 luaA_startup_error(err
);
705 fprintf(stderr
, "%s\n", err
);
711 /** Load a configuration file.
712 * \param xdg An xdg handle to use to get XDG basedir.
713 * \param confpatharg The configuration file to load.
714 * \param run Run the configuration file.
717 luaA_parserc(xdgHandle
* xdg
, const char *confpatharg
, bool run
)
719 char *confpath
= NULL
;
722 /* try to load, return if it's ok */
725 if(luaA_loadrc(confpatharg
, run
))
734 confpath
= xdgConfigFind("awesome/rc.lua", xdg
);
736 char *tmp
= confpath
;
738 /* confpath is "string1\0string2\0string3\0\0" */
741 if(luaA_loadrc(tmp
, run
))
748 tmp
+= a_strlen(tmp
) + 1;
759 luaA_class_index_miss_property(lua_State
*L
, lua_object_t
*obj
)
761 signal_object_emit(L
, &global_signals
, "debug::index::miss", 2);
766 luaA_class_newindex_miss_property(lua_State
*L
, lua_object_t
*obj
)
768 signal_object_emit(L
, &global_signals
, "debug::newindex::miss", 3);
775 signal_object_emit(globalconf
.L
, &global_signals
, "refresh", 0);
778 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80