Don't pass cairo surfaces around on the lua stack
[awesome.git] / luaa.c
blob30f29983d727395d25b09369560411547309db65
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 "awesome.h"
33 #include "config.h"
34 #include "objects/timer.h"
35 #include "awesome-version-internal.h"
36 #include "ewmh.h"
37 #include "luaa.h"
38 #include "spawn.h"
39 #include "objects/tag.h"
40 #include "objects/client.h"
41 #include "objects/drawin.h"
42 #include "screen.h"
43 #include "event.h"
44 #include "selection.h"
45 #include "systray.h"
46 #include "common/xcursor.h"
47 #include "common/buffer.h"
48 #include "common/backtrace.h"
50 #ifdef WITH_DBUS
51 extern const struct luaL_reg awesome_dbus_lib[];
52 #endif
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;
64 /** Quit awesome.
65 * \param L The Lua VM state.
66 * \return The number of elements pushed on stack.
68 static int
69 luaA_quit(lua_State *L)
71 ev_unloop(globalconf.loop, 1);
72 return 0;
75 /** Execute another application, probably a window manager, to replace
76 * awesome.
77 * \param L The Lua VM state.
78 * \return The number of elements pushed on stack.
79 * \luastack
80 * \lparam The command line to execute.
82 static int
83 luaA_exec(lua_State *L)
85 const char *cmd = luaL_checkstring(L, 1);
87 awesome_atexit(false);
89 a_exec(cmd);
90 return 0;
93 /** Restart awesome.
95 static int
96 luaA_restart(lua_State *L)
98 awesome_restart();
99 return 0;
102 /** Load an image from a given path.
103 * \param L The Lua VM state.
104 * \return The number of elements pushed on stack.
105 * \luastack
106 * \lparam The command line to execute.
108 static int
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);
113 if (!surface)
114 return 0;
115 /* lua has to make sure to free the ref or we have a leak */
116 lua_pushlightuserdata(L, surface);
117 return 1;
120 /** UTF-8 aware string length computing.
121 * \param L The Lua VM state.
122 * \return The number of elements pushed on stack.
124 static int
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));
129 return 1;
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.
136 static int
137 luaAe_next(lua_State *L)
139 if(luaL_getmetafield(L, 1, "__next"))
141 lua_insert(L, 1);
142 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
143 return lua_gettop(L);
146 luaL_checktype(L, 1, LUA_TTABLE);
147 lua_settop(L, 2);
148 if(lua_next(L, 1))
149 return 2;
150 lua_pushnil(L);
151 return 1;
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.
160 static int
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 */
166 if(idx < 0) idx--;
167 /* copy table and then move key */
168 lua_pushvalue(L, idx);
169 lua_pushvalue(L, -3);
170 lua_remove(L, -4);
171 lua_pcall(L, 2, 2, 0);
172 /* next returned nil, it's the end */
173 if(lua_isnil(L, -1))
175 /* remove nil */
176 lua_pop(L, 2);
177 return 0;
179 return 1;
181 else if(lua_istable(L, idx))
182 return lua_next(L, idx);
183 /* remove the key */
184 lua_pop(L, 1);
185 return 0;
188 /** Generic pairs function.
189 * \param L The Lua VM state.
190 * \return The number of elements pushed on stack.
192 static int
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 */
198 return 3;
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.
205 static int
206 luaAe_pairs(lua_State *L)
208 if(luaL_getmetafield(L, 1, "__pairs"))
210 lua_insert(L, 1);
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);
219 static int
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.
233 static int
234 luaAe_ipairs(lua_State *L)
236 if(luaL_getmetafield(L, 1, "__ipairs"))
238 lua_insert(L, 1);
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));
245 lua_pushvalue(L, 1);
246 lua_pushinteger(L, 0); /* and initial value */
247 return 3;
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.
254 static int
255 luaAe_type(lua_State *L)
257 luaL_checkany(L, 1);
258 lua_pushstring(L, luaA_typename(L, 1));
259 return 1;
262 /** Replace various standards Lua functions with our own.
263 * \param L The Lua VM state.
265 static void
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");
272 lua_pop(L, 1);
273 /* replace next */
274 lua_pushliteral(L, "next");
275 lua_pushcfunction(L, luaAe_next);
276 lua_settable(L, LUA_GLOBALSINDEX);
277 /* replace pairs */
278 lua_pushliteral(L, "pairs");
279 lua_pushcfunction(L, luaAe_next);
280 lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
281 lua_settable(L, LUA_GLOBALSINDEX);
282 /* replace ipairs */
283 lua_pushliteral(L, "ipairs");
284 lua_pushcfunction(L, luaA_ipairs_aux);
285 lua_pushcclosure(L, luaAe_ipairs, 1);
286 lua_settable(L, LUA_GLOBALSINDEX);
287 /* replace type */
288 lua_pushliteral(L, "type");
289 lua_pushcfunction(L, luaAe_type);
290 lua_settable(L, LUA_GLOBALSINDEX);
291 /* set selection */
292 lua_pushliteral(L, "selection");
293 lua_pushcfunction(L, luaA_selection_get);
294 lua_settable(L, LUA_GLOBALSINDEX);
297 /** Look for an item: table, function, etc.
298 * \param L The Lua VM state.
299 * \param item The pointer item.
301 bool
302 luaA_hasitem(lua_State *L, const void *item)
304 lua_pushnil(L);
305 while(luaA_next(L, -2))
307 if(lua_topointer(L, -1) == item)
309 /* remove value and key */
310 lua_pop(L, 2);
311 return true;
313 if(lua_istable(L, -1))
314 if(luaA_hasitem(L, item))
316 /* remove key and value */
317 lua_pop(L, 2);
318 return true;
320 /* remove value */
321 lua_pop(L, 1);
323 return false;
326 /** Browse a table pushed on top of the index, and put all its table and
327 * sub-table into an array.
328 * \param L The Lua VM state.
329 * \param elems The elements array.
330 * \return False if we encounter an elements already in list.
332 static bool
333 luaA_isloop_check(lua_State *L, cptr_array_t *elems)
335 if(lua_istable(L, -1))
337 const void *object = lua_topointer(L, -1);
339 /* Check that the object table is not already in the list */
340 for(int i = 0; i < elems->len; i++)
341 if(elems->tab[i] == object)
342 return false;
344 /* push the table in the elements list */
345 cptr_array_append(elems, object);
347 /* look every object in the "table" */
348 lua_pushnil(L);
349 while(luaA_next(L, -2))
351 if(!luaA_isloop_check(L, elems))
353 /* remove key and value */
354 lua_pop(L, 2);
355 return false;
357 /* remove value, keep key for next iteration */
358 lua_pop(L, 1);
361 return true;
364 /** Check if a table is a loop. When using tables as direct acyclic digram,
365 * this is useful.
366 * \param L The Lua VM state.
367 * \param idx The index of the table in the stack
368 * \return True if the table loops.
370 bool
371 luaA_isloop(lua_State *L, int idx)
373 /* elems is an elements array that we will fill with all array we
374 * encounter while browsing the tables */
375 cptr_array_t elems;
377 cptr_array_init(&elems);
379 /* push table on top */
380 lua_pushvalue(L, idx);
382 bool ret = luaA_isloop_check(L, &elems);
384 /* remove pushed table */
385 lua_pop(L, 1);
387 cptr_array_wipe(&elems);
389 return !ret;
392 /** awesome global table.
393 * \param L The Lua VM state.
394 * \return The number of elements pushed on stack.
395 * \luastack
396 * \lfield font The default font.
397 * \lfield font_height The default font height.
398 * \lfield conffile The configuration file which has been loaded.
400 static int
401 luaA_awesome_index(lua_State *L)
403 if(luaA_usemetatable(L, 1, 2))
404 return 1;
406 const char *buf = luaL_checkstring(L, 2);
408 if(A_STREQ(buf, "conffile"))
410 lua_pushstring(L, conffile);
411 return 1;
414 if(A_STREQ(buf, "version"))
416 lua_pushliteral(L, AWESOME_VERSION);
417 return 1;
420 if(A_STREQ(buf, "release"))
422 lua_pushliteral(L, AWESOME_RELEASE);
423 return 1;
426 if(A_STREQ(buf, "startup_errors") && globalconf.startup_errors.len != 0)
428 lua_pushstring(L, globalconf.startup_errors.s);
429 return 1;
432 return 0;
435 /** Add a global signal.
436 * \param L The Lua VM state.
437 * \return The number of elements pushed on stack.
438 * \luastack
439 * \lparam A string with the event name.
440 * \lparam The function to call.
442 static int
443 luaA_awesome_connect_signal(lua_State *L)
445 const char *name = luaL_checkstring(L, 1);
446 luaA_checkfunction(L, 2);
447 signal_connect(&global_signals, name, luaA_object_ref(L, 2));
448 return 0;
451 /** Remove a global signal.
452 * \param L The Lua VM state.
453 * \return The number of elements pushed on stack.
454 * \luastack
455 * \lparam A string with the event name.
456 * \lparam The function to call.
458 static int
459 luaA_awesome_disconnect_signal(lua_State *L)
461 const char *name = luaL_checkstring(L, 1);
462 luaA_checkfunction(L, 2);
463 const void *func = lua_topointer(L, 2);
464 signal_disconnect(&global_signals, name, func);
465 luaA_object_unref(L, (void *) func);
466 return 0;
469 /** Emit a global signal.
470 * \param L The Lua VM state.
471 * \return The number of elements pushed on stack.
472 * \luastack
473 * \lparam A string with the event name.
474 * \lparam The function to call.
476 static int
477 luaA_awesome_emit_signal(lua_State *L)
479 signal_object_emit(L, &global_signals, luaL_checkstring(L, 1), lua_gettop(L) - 1);
480 return 0;
483 static int
484 luaA_panic(lua_State *L)
486 warn("unprotected error in call to Lua API (%s)",
487 lua_tostring(L, -1));
488 buffer_t buf;
489 backtrace_get(&buf);
490 warn("dumping backtrace\n%s", buf.s);
491 warn("restarting awesome");
492 awesome_restart();
493 return 0;
496 static int
497 luaA_dofunction_on_error(lua_State *L)
499 /* duplicate string error */
500 lua_pushvalue(L, -1);
501 /* emit error signal */
502 signal_object_emit(L, &global_signals, "debug::error", 1);
504 if(!luaL_dostring(L, "return debug.traceback(\"error while running function\", 3)"))
506 /* Move traceback before error */
507 lua_insert(L, -2);
508 /* Insert sentence */
509 lua_pushliteral(L, "\nerror: ");
510 /* Move it before error */
511 lua_insert(L, -2);
512 lua_concat(L, 3);
514 return 1;
517 /** Initialize the Lua VM
518 * \param xdg An xdg handle to use to get XDG basedir.
520 void
521 luaA_init(xdgHandle* xdg)
523 lua_State *L;
524 static const struct luaL_reg awesome_lib[] =
526 { "quit", luaA_quit },
527 { "exec", luaA_exec },
528 { "spawn", luaA_spawn },
529 { "restart", luaA_restart },
530 { "connect_signal", luaA_awesome_connect_signal },
531 { "disconnect_signal", luaA_awesome_disconnect_signal },
532 { "emit_signal", luaA_awesome_emit_signal },
533 { "systray", luaA_systray },
534 { "load_image", luaA_load_image },
535 { "__index", luaA_awesome_index },
536 { NULL, NULL }
539 L = globalconf.L = luaL_newstate();
541 /* Set panic function */
542 lua_atpanic(L, luaA_panic);
544 /* Set error handling function */
545 lualib_dofunction_on_error = luaA_dofunction_on_error;
547 luaL_openlibs(L);
549 luaA_fixups(L);
551 luaA_object_setup(L);
553 /* Export awesome lib */
554 luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
556 /* Export root lib */
557 luaL_register(L, "root", awesome_root_lib);
558 lua_pop(L, 1); /* luaL_register() leaves the table on stack */
560 #ifdef WITH_DBUS
561 /* Export D-Bus lib */
562 luaL_register(L, "dbus", awesome_dbus_lib);
563 lua_pop(L, 1); /* luaL_register() leaves the table on stack */
564 #endif
566 /* Export keygrabber lib */
567 luaL_register(L, "keygrabber", awesome_keygrabber_lib);
568 lua_pop(L, 1); /* luaL_register() leaves the table on stack */
570 /* Export mousegrabber lib */
571 luaL_register(L, "mousegrabber", awesome_mousegrabber_lib);
572 lua_pop(L, 1); /* luaL_register() leaves the table on stack */
574 /* Export screen */
575 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
577 /* Export mouse */
578 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
580 /* Export button */
581 button_class_setup(L);
583 /* Export tag */
584 tag_class_setup(L);
586 /* Export window */
587 window_class_setup(L);
589 /* Export drawin */
590 drawin_class_setup(L);
592 /* Export client */
593 client_class_setup(L);
595 /* Export keys */
596 key_class_setup(L);
598 /* Export timer */
599 timer_class_setup(L);
601 /* add Lua search paths */
602 lua_getglobal(L, "package");
603 if (LUA_TTABLE != lua_type(L, 1))
605 warn("package is not a table");
606 return;
608 lua_getfield(L, 1, "path");
609 if (LUA_TSTRING != lua_type(L, 2))
611 warn("package.path is not a string");
612 lua_pop(L, 1);
613 return;
616 /* add XDG_CONFIG_DIR as include path */
617 const char * const *xdgconfigdirs = xdgSearchableConfigDirectories(xdg);
618 for(; *xdgconfigdirs; xdgconfigdirs++)
620 size_t len = a_strlen(*xdgconfigdirs);
621 lua_pushliteral(L, ";");
622 lua_pushlstring(L, *xdgconfigdirs, len);
623 lua_pushliteral(L, "/awesome/?.lua");
624 lua_concat(L, 3);
626 lua_pushliteral(L, ";");
627 lua_pushlstring(L, *xdgconfigdirs, len);
628 lua_pushliteral(L, "/awesome/?/init.lua");
629 lua_concat(L, 3);
631 lua_concat(L, 3); /* concatenate with package.path */
634 /* add Lua lib path (/usr/share/awesome/lib by default) */
635 lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?.lua");
636 lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?/init.lua");
637 lua_concat(L, 3); /* concatenate with package.path */
638 lua_setfield(L, 1, "path"); /* package.path = "concatenated string" */
640 lua_getfield(L, 1, "loaded");
642 lua_pop(L, 2); /* pop "package" and "package.loaded" */
644 signal_add(&global_signals, "debug::error");
645 signal_add(&global_signals, "debug::deprecation");
646 signal_add(&global_signals, "debug::index::miss");
647 signal_add(&global_signals, "debug::newindex::miss");
648 signal_add(&global_signals, "systray::update");
649 signal_add(&global_signals, "wallpaper_changed");
650 signal_add(&global_signals, "refresh");
651 signal_add(&global_signals, "exit");
654 static void
655 luaA_startup_error(const char *err)
657 if (globalconf.startup_errors.len > 0)
658 buffer_addsl(&globalconf.startup_errors, "\n\n");
659 buffer_adds(&globalconf.startup_errors, err);
662 static bool
663 luaA_loadrc(const char *confpath, bool run)
665 if(!luaL_loadfile(globalconf.L, confpath))
667 if(run)
669 /* Set the conffile right now so it can be used inside the
670 * configuration file. */
671 conffile = a_strdup(confpath);
672 if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
674 const char *err = lua_tostring(globalconf.L, -1);
675 luaA_startup_error(err);
676 fprintf(stderr, "%s\n", err);
677 /* An error happened, so reset this. */
678 conffile = NULL;
680 else
681 return true;
683 else
685 lua_pop(globalconf.L, 1);
686 return true;
689 else
691 const char *err = lua_tostring(globalconf.L, -1);
692 luaA_startup_error(err);
693 fprintf(stderr, "%s\n", err);
696 return false;
699 /** Load a configuration file.
700 * \param xdg An xdg handle to use to get XDG basedir.
701 * \param confpatharg The configuration file to load.
702 * \param run Run the configuration file.
704 bool
705 luaA_parserc(xdgHandle* xdg, const char *confpatharg, bool run)
707 char *confpath = NULL;
708 bool ret = false;
710 /* try to load, return if it's ok */
711 if(confpatharg)
713 if(luaA_loadrc(confpatharg, run))
715 ret = true;
716 goto bailout;
718 else if(!run)
719 goto bailout;
722 confpath = xdgConfigFind("awesome/rc.lua", xdg);
724 char *tmp = confpath;
726 /* confpath is "string1\0string2\0string3\0\0" */
727 while(*tmp)
729 if(luaA_loadrc(tmp, run))
731 ret = true;
732 goto bailout;
734 else if(!run)
735 goto bailout;
736 tmp += a_strlen(tmp) + 1;
739 bailout:
741 p_delete(&confpath);
743 return ret;
747 luaA_class_index_miss_property(lua_State *L, lua_object_t *obj)
749 signal_object_emit(L, &global_signals, "debug::index::miss", 2);
750 return 0;
754 luaA_class_newindex_miss_property(lua_State *L, lua_object_t *obj)
756 signal_object_emit(L, &global_signals, "debug::newindex::miss", 3);
757 return 0;
760 void
761 luaA_emit_refresh()
763 signal_object_emit(globalconf.L, &global_signals, "refresh", 0);
766 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80