Menubar utils fix the wm_name matching once more.
[awesome.git] / luaa.c
blobe8e56f863d216bf4b96b9f06d1a51785c4c76a5c
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 <lua.h>
25 #include <lauxlib.h>
26 #include <lualib.h>
28 #include <basedir_fs.h>
30 #include "awesome.h"
31 #include "config.h"
32 #include "objects/timer.h"
33 #include "awesome-version-internal.h"
34 #include "ewmh.h"
35 #include "luaa.h"
36 #include "spawn.h"
37 #include "objects/tag.h"
38 #include "objects/client.h"
39 #include "objects/drawin.h"
40 #include "objects/drawable.h"
41 #include "screen.h"
42 #include "event.h"
43 #include "selection.h"
44 #include "systray.h"
45 #include "common/xcursor.h"
46 #include "common/buffer.h"
47 #include "common/backtrace.h"
49 #ifdef WITH_DBUS
50 extern const struct luaL_Reg awesome_dbus_lib[];
51 #endif
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;
63 /** Quit awesome.
64 * \param L The Lua VM state.
65 * \return The number of elements pushed on stack.
67 static int
68 luaA_quit(lua_State *L)
70 g_main_loop_quit(globalconf.loop);
71 return 0;
74 /** Execute another application, probably a window manager, to replace
75 * awesome.
76 * \param L The Lua VM state.
77 * \return The number of elements pushed on stack.
78 * \luastack
79 * \lparam The command line to execute.
81 static int
82 luaA_exec(lua_State *L)
84 const char *cmd = luaL_checkstring(L, 1);
86 awesome_atexit(false);
88 a_exec(cmd);
89 return 0;
92 /** Restart awesome.
94 static int
95 luaA_restart(lua_State *L)
97 awesome_restart();
98 return 0;
101 /** Load an image from a given path.
102 * \param L The Lua VM state.
103 * \return The number of elements pushed on stack.
104 * \luastack
105 * \lparam The command line to execute.
107 static int
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);
112 if (!surface)
113 return 0;
114 /* lua has to make sure to free the ref or we have a leak */
115 lua_pushlightuserdata(L, surface);
116 return 1;
119 /** UTF-8 aware string length computing.
120 * \param L The Lua VM state.
121 * \return The number of elements pushed on stack.
123 static int
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));
128 return 1;
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.
135 static int
136 luaAe_next(lua_State *L)
138 if(luaL_getmetafield(L, 1, "__next"))
140 lua_insert(L, 1);
141 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
142 return lua_gettop(L);
145 luaL_checktype(L, 1, LUA_TTABLE);
146 lua_settop(L, 2);
147 if(lua_next(L, 1))
148 return 2;
149 lua_pushnil(L);
150 return 1;
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.
159 static int
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 */
165 if(idx < 0) idx--;
166 /* copy table and then move key */
167 lua_pushvalue(L, idx);
168 lua_pushvalue(L, -3);
169 lua_remove(L, -4);
170 lua_pcall(L, 2, 2, 0);
171 /* next returned nil, it's the end */
172 if(lua_isnil(L, -1))
174 /* remove nil */
175 lua_pop(L, 2);
176 return 0;
178 return 1;
180 else if(lua_istable(L, idx))
181 return lua_next(L, idx);
182 /* remove the key */
183 lua_pop(L, 1);
184 return 0;
187 /** Generic pairs function.
188 * \param L The Lua VM state.
189 * \return The number of elements pushed on stack.
191 static int
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 */
197 return 3;
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.
204 static int
205 luaAe_pairs(lua_State *L)
207 if(luaL_getmetafield(L, 1, "__pairs"))
209 lua_insert(L, 1);
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);
218 static int
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.
232 static int
233 luaAe_ipairs(lua_State *L)
235 if(luaL_getmetafield(L, 1, "__ipairs"))
237 lua_insert(L, 1);
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));
244 lua_pushvalue(L, 1);
245 lua_pushinteger(L, 0); /* and initial value */
246 return 3;
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.
253 static int
254 luaAe_type(lua_State *L)
256 luaL_checkany(L, 1);
257 lua_pushstring(L, luaA_typename(L, 1));
258 return 1;
261 /** Replace various standards Lua functions with our own.
262 * \param L The Lua VM state.
264 static void
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");
271 lua_pop(L, 1);
272 /* replace next */
273 lua_pushcfunction(L, luaAe_next);
274 lua_setglobal(L, "next");
275 /* replace pairs */
276 lua_pushcfunction(L, luaAe_next);
277 lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
278 lua_setglobal(L, "pairs");
279 /* replace ipairs */
280 lua_pushcfunction(L, luaA_ipairs_aux);
281 lua_pushcclosure(L, luaAe_ipairs, 1);
282 lua_setglobal(L, "ipairs");
283 /* replace type */
284 lua_pushcfunction(L, luaAe_type);
285 lua_setglobal(L, "type");
286 /* set selection */
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.
295 bool
296 luaA_hasitem(lua_State *L, const void *item)
298 lua_pushnil(L);
299 while(luaA_next(L, -2))
301 if(lua_topointer(L, -1) == item)
303 /* remove value and key */
304 lua_pop(L, 2);
305 return true;
307 if(lua_istable(L, -1))
308 if(luaA_hasitem(L, item))
310 /* remove key and value */
311 lua_pop(L, 2);
312 return true;
314 /* remove value */
315 lua_pop(L, 1);
317 return false;
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.
326 static bool
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)
336 return false;
338 /* push the table in the elements list */
339 cptr_array_append(elems, object);
341 /* look every object in the "table" */
342 lua_pushnil(L);
343 while(luaA_next(L, -2))
345 if(!luaA_isloop_check(L, elems))
347 /* remove key and value */
348 lua_pop(L, 2);
349 return false;
351 /* remove value, keep key for next iteration */
352 lua_pop(L, 1);
355 return true;
358 /** Check if a table is a loop. When using tables as direct acyclic digram,
359 * this is useful.
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.
364 bool
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 */
369 cptr_array_t elems;
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 */
379 lua_pop(L, 1);
381 cptr_array_wipe(&elems);
383 return !ret;
386 /** awesome global table.
387 * \param L The Lua VM state.
388 * \return The number of elements pushed on stack.
389 * \luastack
390 * \lfield font The default font.
391 * \lfield font_height The default font height.
392 * \lfield conffile The configuration file which has been loaded.
394 static int
395 luaA_awesome_index(lua_State *L)
397 if(luaA_usemetatable(L, 1, 2))
398 return 1;
400 const char *buf = luaL_checkstring(L, 2);
402 if(A_STREQ(buf, "conffile"))
404 lua_pushstring(L, conffile);
405 return 1;
408 if(A_STREQ(buf, "version"))
410 lua_pushliteral(L, AWESOME_VERSION);
411 return 1;
414 if(A_STREQ(buf, "release"))
416 lua_pushliteral(L, AWESOME_RELEASE);
417 return 1;
420 if(A_STREQ(buf, "startup_errors") && globalconf.startup_errors.len != 0)
422 lua_pushstring(L, globalconf.startup_errors.s);
423 return 1;
426 return 0;
429 /** Add a global signal.
430 * \param L The Lua VM state.
431 * \return The number of elements pushed on stack.
432 * \luastack
433 * \lparam A string with the event name.
434 * \lparam The function to call.
436 static int
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));
442 return 0;
445 /** Remove a global signal.
446 * \param L The Lua VM state.
447 * \return The number of elements pushed on stack.
448 * \luastack
449 * \lparam A string with the event name.
450 * \lparam The function to call.
452 static int
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);
460 return 0;
463 /** Emit a global signal.
464 * \param L The Lua VM state.
465 * \return The number of elements pushed on stack.
466 * \luastack
467 * \lparam A string with the event name.
468 * \lparam The function to call.
470 static int
471 luaA_awesome_emit_signal(lua_State *L)
473 signal_object_emit(L, &global_signals, luaL_checkstring(L, 1), lua_gettop(L) - 1);
474 return 0;
477 static int
478 luaA_panic(lua_State *L)
480 warn("unprotected error in call to Lua API (%s)",
481 lua_tostring(L, -1));
482 buffer_t buf;
483 backtrace_get(&buf);
484 warn("dumping backtrace\n%s", buf.s);
485 warn("restarting awesome");
486 awesome_restart();
487 return 0;
490 static int
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 */
501 lua_insert(L, -2);
502 /* Insert sentence */
503 lua_pushliteral(L, "\nerror: ");
504 /* Move it before error */
505 lua_insert(L, -2);
506 lua_concat(L, 3);
508 return 1;
511 /** Initialize the Lua VM
512 * \param xdg An xdg handle to use to get XDG basedir.
514 void
515 luaA_init(xdgHandle* xdg)
517 lua_State *L;
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 },
530 { NULL, NULL }
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;
541 luaL_openlibs(L);
543 luaA_fixups(L);
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 */
554 #ifdef WITH_DBUS
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 */
558 #endif
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 */
568 /* Export screen */
569 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
571 /* Export mouse */
572 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
574 /* Export button */
575 button_class_setup(L);
577 /* Export tag */
578 tag_class_setup(L);
580 /* Export window */
581 window_class_setup(L);
583 /* Export drawable */
584 drawable_class_setup(L);
586 /* Export drawin */
587 drawin_class_setup(L);
589 /* Export client */
590 client_class_setup(L);
592 /* Export keys */
593 key_class_setup(L);
595 /* Export timer */
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");
603 return;
605 lua_getfield(L, 1, "path");
606 if (LUA_TSTRING != lua_type(L, 2))
608 warn("package.path is not a string");
609 lua_pop(L, 1);
610 return;
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");
621 lua_concat(L, 3);
623 lua_pushliteral(L, ";");
624 lua_pushlstring(L, *xdgconfigdirs, len);
625 lua_pushliteral(L, "/awesome/?/init.lua");
626 lua_concat(L, 3);
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");
651 static void
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);
659 static bool
660 luaA_loadrc(const char *confpath, bool run)
662 if(!luaL_loadfile(globalconf.L, confpath))
664 if(run)
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. */
675 conffile = NULL;
677 else
678 return true;
680 else
682 lua_pop(globalconf.L, 1);
683 return true;
686 else
688 const char *err = lua_tostring(globalconf.L, -1);
689 luaA_startup_error(err);
690 fprintf(stderr, "%s\n", err);
693 return false;
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.
701 bool
702 luaA_parserc(xdgHandle* xdg, const char *confpatharg, bool run)
704 char *confpath = NULL;
705 bool ret = false;
707 /* try to load, return if it's ok */
708 if(confpatharg)
710 if(luaA_loadrc(confpatharg, run))
712 ret = true;
713 goto bailout;
715 else if(!run)
716 goto bailout;
719 confpath = xdgConfigFind("awesome/rc.lua", xdg);
721 char *tmp = confpath;
723 /* confpath is "string1\0string2\0string3\0\0" */
724 while(*tmp)
726 if(luaA_loadrc(tmp, run))
728 ret = true;
729 goto bailout;
731 else if(!run)
732 goto bailout;
733 tmp += a_strlen(tmp) + 1;
736 bailout:
738 p_delete(&confpath);
740 return ret;
744 luaA_class_index_miss_property(lua_State *L, lua_object_t *obj)
746 signal_object_emit(L, &global_signals, "debug::index::miss", 2);
747 return 0;
751 luaA_class_newindex_miss_property(lua_State *L, lua_object_t *obj)
753 signal_object_emit(L, &global_signals, "debug::newindex::miss", 3);
754 return 0;
757 void
758 luaA_emit_refresh()
760 signal_object_emit(globalconf.L, &global_signals, "refresh", 0);
763 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80