Add a wallpaper_changed signal
[awesome.git] / luaa.c
blob2b61289e8c765c846a5eaf42158cae85e7f73dd6
1 /*
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.
22 #define _GNU_SOURCE
24 #include <ev.h>
26 #include <lua.h>
27 #include <lauxlib.h>
28 #include <lualib.h>
30 #include <basedir_fs.h>
32 #include <oopango.h>
33 #include <oocairo.h>
35 #include "awesome.h"
36 #include "config.h"
37 #include "objects/timer.h"
38 #include "awesome-version-internal.h"
39 #include "ewmh.h"
40 #include "luaa.h"
41 #include "spawn.h"
42 #include "objects/tag.h"
43 #include "objects/client.h"
44 #include "objects/drawin.h"
45 #include "screen.h"
46 #include "event.h"
47 #include "selection.h"
48 #include "systray.h"
49 #include "common/xcursor.h"
50 #include "common/buffer.h"
51 #include "common/backtrace.h"
53 #ifdef WITH_DBUS
54 extern const struct luaL_reg awesome_dbus_lib[];
55 #endif
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;
67 /** Quit awesome.
68 * \param L The Lua VM state.
69 * \return The number of elements pushed on stack.
71 static int
72 luaA_quit(lua_State *L)
74 ev_unloop(globalconf.loop, 1);
75 return 0;
78 /** Execute another application, probably a window manager, to replace
79 * awesome.
80 * \param L The Lua VM state.
81 * \return The number of elements pushed on stack.
82 * \luastack
83 * \lparam The command line to execute.
85 static int
86 luaA_exec(lua_State *L)
88 const char *cmd = luaL_checkstring(L, 1);
90 awesome_atexit(false);
92 a_exec(cmd);
93 return 0;
96 /** Restart awesome.
98 static int
99 luaA_restart(lua_State *L)
101 awesome_restart();
102 return 0;
105 /** Load an image from a given path.
106 * \param L The Lua VM state.
107 * \return The number of elements pushed on stack.
108 * \luastack
109 * \lparam The command line to execute.
111 static int
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.
122 static int
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));
127 return 1;
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.
134 static int
135 luaAe_next(lua_State *L)
137 if(luaL_getmetafield(L, 1, "__next"))
139 lua_insert(L, 1);
140 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
141 return lua_gettop(L);
144 luaL_checktype(L, 1, LUA_TTABLE);
145 lua_settop(L, 2);
146 if(lua_next(L, 1))
147 return 2;
148 lua_pushnil(L);
149 return 1;
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.
158 static int
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 */
164 if(idx < 0) idx--;
165 /* copy table and then move key */
166 lua_pushvalue(L, idx);
167 lua_pushvalue(L, -3);
168 lua_remove(L, -4);
169 lua_pcall(L, 2, 2, 0);
170 /* next returned nil, it's the end */
171 if(lua_isnil(L, -1))
173 /* remove nil */
174 lua_pop(L, 2);
175 return 0;
177 return 1;
179 else if(lua_istable(L, idx))
180 return lua_next(L, idx);
181 /* remove the key */
182 lua_pop(L, 1);
183 return 0;
186 /** Generic pairs function.
187 * \param L The Lua VM state.
188 * \return The number of elements pushed on stack.
190 static int
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 */
196 return 3;
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.
203 static int
204 luaAe_pairs(lua_State *L)
206 if(luaL_getmetafield(L, 1, "__pairs"))
208 lua_insert(L, 1);
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);
217 static int
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.
231 static int
232 luaAe_ipairs(lua_State *L)
234 if(luaL_getmetafield(L, 1, "__ipairs"))
236 lua_insert(L, 1);
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));
243 lua_pushvalue(L, 1);
244 lua_pushinteger(L, 0); /* and initial value */
245 return 3;
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.
252 static int
253 luaAe_type(lua_State *L)
255 luaL_checkany(L, 1);
256 lua_pushstring(L, luaA_typename(L, 1));
257 return 1;
260 /** Replace various standards Lua functions with our own.
261 * \param L The Lua VM state.
263 static void
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");
270 lua_pop(L, 1);
271 /* replace next */
272 lua_pushliteral(L, "next");
273 lua_pushcfunction(L, luaAe_next);
274 lua_settable(L, LUA_GLOBALSINDEX);
275 /* replace pairs */
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);
280 /* replace ipairs */
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);
285 /* replace type */
286 lua_pushliteral(L, "type");
287 lua_pushcfunction(L, luaAe_type);
288 lua_settable(L, LUA_GLOBALSINDEX);
289 /* set selection */
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.
299 bool
300 luaA_hasitem(lua_State *L, const void *item)
302 lua_pushnil(L);
303 while(luaA_next(L, -2))
305 if(lua_topointer(L, -1) == item)
307 /* remove value and key */
308 lua_pop(L, 2);
309 return true;
311 if(lua_istable(L, -1))
312 if(luaA_hasitem(L, item))
314 /* remove key and value */
315 lua_pop(L, 2);
316 return true;
318 /* remove value */
319 lua_pop(L, 1);
321 return false;
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.
330 static bool
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)
340 return false;
342 /* push the table in the elements list */
343 cptr_array_append(elems, object);
345 /* look every object in the "table" */
346 lua_pushnil(L);
347 while(luaA_next(L, -2))
349 if(!luaA_isloop_check(L, elems))
351 /* remove key and value */
352 lua_pop(L, 2);
353 return false;
355 /* remove value, keep key for next iteration */
356 lua_pop(L, 1);
359 return true;
362 /** Check if a table is a loop. When using tables as direct acyclic digram,
363 * this is useful.
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.
368 bool
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 */
373 cptr_array_t elems;
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 */
383 lua_pop(L, 1);
385 cptr_array_wipe(&elems);
387 return !ret;
390 /** awesome global table.
391 * \param L The Lua VM state.
392 * \return The number of elements pushed on stack.
393 * \luastack
394 * \lfield font The default font.
395 * \lfield font_height The default font height.
396 * \lfield conffile The configuration file which has been loaded.
398 static int
399 luaA_awesome_index(lua_State *L)
401 if(luaA_usemetatable(L, 1, 2))
402 return 1;
404 const char *buf = luaL_checkstring(L, 2);
406 if(A_STREQ(buf, "conffile"))
408 lua_pushstring(L, conffile);
409 return 1;
412 if(A_STREQ(buf, "version"))
414 lua_pushliteral(L, AWESOME_VERSION);
415 return 1;
418 if(A_STREQ(buf, "release"))
420 lua_pushliteral(L, AWESOME_RELEASE);
421 return 1;
424 if(A_STREQ(buf, "startup_errors") && globalconf.startup_errors.len != 0)
426 lua_pushstring(L, globalconf.startup_errors.s);
427 return 1;
430 return 0;
433 /** Add a global signal.
434 * \param L The Lua VM state.
435 * \return The number of elements pushed on stack.
436 * \luastack
437 * \lparam A string with the event name.
438 * \lparam The function to call.
440 static int
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));
446 return 0;
449 /** Remove a global signal.
450 * \param L The Lua VM state.
451 * \return The number of elements pushed on stack.
452 * \luastack
453 * \lparam A string with the event name.
454 * \lparam The function to call.
456 static int
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);
464 return 0;
467 /** Emit a global signal.
468 * \param L The Lua VM state.
469 * \return The number of elements pushed on stack.
470 * \luastack
471 * \lparam A string with the event name.
472 * \lparam The function to call.
474 static int
475 luaA_awesome_emit_signal(lua_State *L)
477 signal_object_emit(L, &global_signals, luaL_checkstring(L, 1), lua_gettop(L) - 1);
478 return 0;
481 static int
482 luaA_panic(lua_State *L)
484 warn("unprotected error in call to Lua API (%s)",
485 lua_tostring(L, -1));
486 buffer_t buf;
487 backtrace_get(&buf);
488 warn("dumping backtrace\n%s", buf.s);
489 warn("restarting awesome");
490 awesome_restart();
491 return 0;
494 static int
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 */
505 lua_insert(L, -2);
506 /* Insert sentence */
507 lua_pushliteral(L, "\nerror: ");
508 /* Move it before error */
509 lua_insert(L, -2);
510 lua_concat(L, 3);
512 return 1;
515 /** Initialize the Lua VM
516 * \param xdg An xdg handle to use to get XDG basedir.
518 void
519 luaA_init(xdgHandle* xdg)
521 lua_State *L;
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 },
534 { NULL, NULL }
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;
545 luaL_openlibs(L);
547 luaA_fixups(L);
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 */
558 #ifdef WITH_DBUS
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 */
562 #endif
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 */
572 /* Export screen */
573 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
575 /* Export mouse */
576 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
578 /* Export button */
579 button_class_setup(L);
581 /* Export tag */
582 tag_class_setup(L);
584 /* Export window */
585 window_class_setup(L);
587 /* Export drawin */
588 drawin_class_setup(L);
590 /* Export client */
591 client_class_setup(L);
593 /* Export keys */
594 key_class_setup(L);
596 /* Export timer */
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");
604 return;
606 lua_getfield(L, 1, "path");
607 if (LUA_TSTRING != lua_type(L, 2))
609 warn("package.path is not a string");
610 lua_pop(L, 1);
611 return;
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");
622 lua_concat(L, 3);
624 lua_pushliteral(L, ";");
625 lua_pushlstring(L, *xdgconfigdirs, len);
626 lua_pushliteral(L, "/awesome/?/init.lua");
627 lua_concat(L, 3);
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");
640 /* Load oocairo */
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 */
647 /* Load oopango */
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");
666 static void
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);
674 static bool
675 luaA_loadrc(const char *confpath, bool run)
677 if(!luaL_loadfile(globalconf.L, confpath))
679 if(run)
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. */
690 conffile = NULL;
692 else
693 return true;
695 else
697 lua_pop(globalconf.L, 1);
698 return true;
701 else
703 const char *err = lua_tostring(globalconf.L, -1);
704 luaA_startup_error(err);
705 fprintf(stderr, "%s\n", err);
708 return false;
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.
716 bool
717 luaA_parserc(xdgHandle* xdg, const char *confpatharg, bool run)
719 char *confpath = NULL;
720 bool ret = false;
722 /* try to load, return if it's ok */
723 if(confpatharg)
725 if(luaA_loadrc(confpatharg, run))
727 ret = true;
728 goto bailout;
730 else if(!run)
731 goto bailout;
734 confpath = xdgConfigFind("awesome/rc.lua", xdg);
736 char *tmp = confpath;
738 /* confpath is "string1\0string2\0string3\0\0" */
739 while(*tmp)
741 if(luaA_loadrc(tmp, run))
743 ret = true;
744 goto bailout;
746 else if(!run)
747 goto bailout;
748 tmp += a_strlen(tmp) + 1;
751 bailout:
753 p_delete(&confpath);
755 return ret;
759 luaA_class_index_miss_property(lua_State *L, lua_object_t *obj)
761 signal_object_emit(L, &global_signals, "debug::index::miss", 2);
762 return 0;
766 luaA_class_newindex_miss_property(lua_State *L, lua_object_t *obj)
768 signal_object_emit(L, &global_signals, "debug::newindex::miss", 3);
769 return 0;
772 void
773 luaA_emit_refresh()
775 signal_object_emit(globalconf.L, &global_signals, "refresh", 0);
778 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80