Don't change focus in response to FocusIn events
[awesome.git] / luaa.c
blobef161bda047d5fcd94e0e4a2eb3ffd3cf7734cdc
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_pushcfunction(L, luaAe_next);
275 lua_setglobal(L, "next");
276 /* replace pairs */
277 lua_pushcfunction(L, luaAe_next);
278 lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
279 lua_setglobal(L, "pairs");
280 /* replace ipairs */
281 lua_pushcfunction(L, luaA_ipairs_aux);
282 lua_pushcclosure(L, luaAe_ipairs, 1);
283 lua_setglobal(L, "ipairs");
284 /* replace type */
285 lua_pushcfunction(L, luaAe_type);
286 lua_setglobal(L, "type");
287 /* set selection */
288 lua_pushcfunction(L, luaA_selection_get);
289 lua_setglobal(L, "selection");
292 /** Look for an item: table, function, etc.
293 * \param L The Lua VM state.
294 * \param item The pointer item.
296 bool
297 luaA_hasitem(lua_State *L, const void *item)
299 lua_pushnil(L);
300 while(luaA_next(L, -2))
302 if(lua_topointer(L, -1) == item)
304 /* remove value and key */
305 lua_pop(L, 2);
306 return true;
308 if(lua_istable(L, -1))
309 if(luaA_hasitem(L, item))
311 /* remove key and value */
312 lua_pop(L, 2);
313 return true;
315 /* remove value */
316 lua_pop(L, 1);
318 return false;
321 /** Browse a table pushed on top of the index, and put all its table and
322 * sub-table into an array.
323 * \param L The Lua VM state.
324 * \param elems The elements array.
325 * \return False if we encounter an elements already in list.
327 static bool
328 luaA_isloop_check(lua_State *L, cptr_array_t *elems)
330 if(lua_istable(L, -1))
332 const void *object = lua_topointer(L, -1);
334 /* Check that the object table is not already in the list */
335 for(int i = 0; i < elems->len; i++)
336 if(elems->tab[i] == object)
337 return false;
339 /* push the table in the elements list */
340 cptr_array_append(elems, object);
342 /* look every object in the "table" */
343 lua_pushnil(L);
344 while(luaA_next(L, -2))
346 if(!luaA_isloop_check(L, elems))
348 /* remove key and value */
349 lua_pop(L, 2);
350 return false;
352 /* remove value, keep key for next iteration */
353 lua_pop(L, 1);
356 return true;
359 /** Check if a table is a loop. When using tables as direct acyclic digram,
360 * this is useful.
361 * \param L The Lua VM state.
362 * \param idx The index of the table in the stack
363 * \return True if the table loops.
365 bool
366 luaA_isloop(lua_State *L, int idx)
368 /* elems is an elements array that we will fill with all array we
369 * encounter while browsing the tables */
370 cptr_array_t elems;
372 cptr_array_init(&elems);
374 /* push table on top */
375 lua_pushvalue(L, idx);
377 bool ret = luaA_isloop_check(L, &elems);
379 /* remove pushed table */
380 lua_pop(L, 1);
382 cptr_array_wipe(&elems);
384 return !ret;
387 /** awesome global table.
388 * \param L The Lua VM state.
389 * \return The number of elements pushed on stack.
390 * \luastack
391 * \lfield font The default font.
392 * \lfield font_height The default font height.
393 * \lfield conffile The configuration file which has been loaded.
395 static int
396 luaA_awesome_index(lua_State *L)
398 if(luaA_usemetatable(L, 1, 2))
399 return 1;
401 const char *buf = luaL_checkstring(L, 2);
403 if(A_STREQ(buf, "conffile"))
405 lua_pushstring(L, conffile);
406 return 1;
409 if(A_STREQ(buf, "version"))
411 lua_pushliteral(L, AWESOME_VERSION);
412 return 1;
415 if(A_STREQ(buf, "release"))
417 lua_pushliteral(L, AWESOME_RELEASE);
418 return 1;
421 if(A_STREQ(buf, "startup_errors") && globalconf.startup_errors.len != 0)
423 lua_pushstring(L, globalconf.startup_errors.s);
424 return 1;
427 return 0;
430 /** Add a global signal.
431 * \param L The Lua VM state.
432 * \return The number of elements pushed on stack.
433 * \luastack
434 * \lparam A string with the event name.
435 * \lparam The function to call.
437 static int
438 luaA_awesome_connect_signal(lua_State *L)
440 const char *name = luaL_checkstring(L, 1);
441 luaA_checkfunction(L, 2);
442 signal_connect(&global_signals, name, luaA_object_ref(L, 2));
443 return 0;
446 /** Remove a global signal.
447 * \param L The Lua VM state.
448 * \return The number of elements pushed on stack.
449 * \luastack
450 * \lparam A string with the event name.
451 * \lparam The function to call.
453 static int
454 luaA_awesome_disconnect_signal(lua_State *L)
456 const char *name = luaL_checkstring(L, 1);
457 luaA_checkfunction(L, 2);
458 const void *func = lua_topointer(L, 2);
459 signal_disconnect(&global_signals, name, func);
460 luaA_object_unref(L, (void *) func);
461 return 0;
464 /** Emit a global signal.
465 * \param L The Lua VM state.
466 * \return The number of elements pushed on stack.
467 * \luastack
468 * \lparam A string with the event name.
469 * \lparam The function to call.
471 static int
472 luaA_awesome_emit_signal(lua_State *L)
474 signal_object_emit(L, &global_signals, luaL_checkstring(L, 1), lua_gettop(L) - 1);
475 return 0;
478 static int
479 luaA_panic(lua_State *L)
481 warn("unprotected error in call to Lua API (%s)",
482 lua_tostring(L, -1));
483 buffer_t buf;
484 backtrace_get(&buf);
485 warn("dumping backtrace\n%s", buf.s);
486 warn("restarting awesome");
487 awesome_restart();
488 return 0;
491 static int
492 luaA_dofunction_on_error(lua_State *L)
494 /* duplicate string error */
495 lua_pushvalue(L, -1);
496 /* emit error signal */
497 signal_object_emit(L, &global_signals, "debug::error", 1);
499 if(!luaL_dostring(L, "return debug.traceback(\"error while running function\", 3)"))
501 /* Move traceback before error */
502 lua_insert(L, -2);
503 /* Insert sentence */
504 lua_pushliteral(L, "\nerror: ");
505 /* Move it before error */
506 lua_insert(L, -2);
507 lua_concat(L, 3);
509 return 1;
512 /** Initialize the Lua VM
513 * \param xdg An xdg handle to use to get XDG basedir.
515 void
516 luaA_init(xdgHandle* xdg)
518 lua_State *L;
519 static const struct luaL_Reg awesome_lib[] =
521 { "quit", luaA_quit },
522 { "exec", luaA_exec },
523 { "spawn", luaA_spawn },
524 { "restart", luaA_restart },
525 { "connect_signal", luaA_awesome_connect_signal },
526 { "disconnect_signal", luaA_awesome_disconnect_signal },
527 { "emit_signal", luaA_awesome_emit_signal },
528 { "systray", luaA_systray },
529 { "load_image", luaA_load_image },
530 { "__index", luaA_awesome_index },
531 { NULL, NULL }
534 L = globalconf.L = luaL_newstate();
536 /* Set panic function */
537 lua_atpanic(L, luaA_panic);
539 /* Set error handling function */
540 lualib_dofunction_on_error = luaA_dofunction_on_error;
542 luaL_openlibs(L);
544 luaA_fixups(L);
546 luaA_object_setup(L);
548 /* Export awesome lib */
549 luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
551 /* Export root lib */
552 luaA_registerlib(L, "root", awesome_root_lib);
553 lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
555 #ifdef WITH_DBUS
556 /* Export D-Bus lib */
557 luaA_registerlib(L, "dbus", awesome_dbus_lib);
558 lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
559 #endif
561 /* Export keygrabber lib */
562 luaA_registerlib(L, "keygrabber", awesome_keygrabber_lib);
563 lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
565 /* Export mousegrabber lib */
566 luaA_registerlib(L, "mousegrabber", awesome_mousegrabber_lib);
567 lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
569 /* Export screen */
570 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
572 /* Export mouse */
573 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
575 /* Export button */
576 button_class_setup(L);
578 /* Export tag */
579 tag_class_setup(L);
581 /* Export window */
582 window_class_setup(L);
584 /* Export drawin */
585 drawin_class_setup(L);
587 /* Export client */
588 client_class_setup(L);
590 /* Export keys */
591 key_class_setup(L);
593 /* Export timer */
594 timer_class_setup(L);
596 /* add Lua search paths */
597 lua_getglobal(L, "package");
598 if (LUA_TTABLE != lua_type(L, 1))
600 warn("package is not a table");
601 return;
603 lua_getfield(L, 1, "path");
604 if (LUA_TSTRING != lua_type(L, 2))
606 warn("package.path is not a string");
607 lua_pop(L, 1);
608 return;
611 /* add XDG_CONFIG_DIR as include path */
612 const char * const *xdgconfigdirs = xdgSearchableConfigDirectories(xdg);
613 for(; *xdgconfigdirs; xdgconfigdirs++)
615 size_t len = a_strlen(*xdgconfigdirs);
616 lua_pushliteral(L, ";");
617 lua_pushlstring(L, *xdgconfigdirs, len);
618 lua_pushliteral(L, "/awesome/?.lua");
619 lua_concat(L, 3);
621 lua_pushliteral(L, ";");
622 lua_pushlstring(L, *xdgconfigdirs, len);
623 lua_pushliteral(L, "/awesome/?/init.lua");
624 lua_concat(L, 3);
626 lua_concat(L, 3); /* concatenate with package.path */
629 /* add Lua lib path (/usr/share/awesome/lib by default) */
630 lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?.lua");
631 lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?/init.lua");
632 lua_concat(L, 3); /* concatenate with package.path */
633 lua_setfield(L, 1, "path"); /* package.path = "concatenated string" */
635 lua_getfield(L, 1, "loaded");
637 lua_pop(L, 2); /* pop "package" and "package.loaded" */
639 signal_add(&global_signals, "debug::error");
640 signal_add(&global_signals, "debug::deprecation");
641 signal_add(&global_signals, "debug::index::miss");
642 signal_add(&global_signals, "debug::newindex::miss");
643 signal_add(&global_signals, "systray::update");
644 signal_add(&global_signals, "wallpaper_changed");
645 signal_add(&global_signals, "refresh");
646 signal_add(&global_signals, "exit");
649 static void
650 luaA_startup_error(const char *err)
652 if (globalconf.startup_errors.len > 0)
653 buffer_addsl(&globalconf.startup_errors, "\n\n");
654 buffer_adds(&globalconf.startup_errors, err);
657 static bool
658 luaA_loadrc(const char *confpath, bool run)
660 if(!luaL_loadfile(globalconf.L, confpath))
662 if(run)
664 /* Set the conffile right now so it can be used inside the
665 * configuration file. */
666 conffile = a_strdup(confpath);
667 if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
669 const char *err = lua_tostring(globalconf.L, -1);
670 luaA_startup_error(err);
671 fprintf(stderr, "%s\n", err);
672 /* An error happened, so reset this. */
673 conffile = NULL;
675 else
676 return true;
678 else
680 lua_pop(globalconf.L, 1);
681 return true;
684 else
686 const char *err = lua_tostring(globalconf.L, -1);
687 luaA_startup_error(err);
688 fprintf(stderr, "%s\n", err);
691 return false;
694 /** Load a configuration file.
695 * \param xdg An xdg handle to use to get XDG basedir.
696 * \param confpatharg The configuration file to load.
697 * \param run Run the configuration file.
699 bool
700 luaA_parserc(xdgHandle* xdg, const char *confpatharg, bool run)
702 char *confpath = NULL;
703 bool ret = false;
705 /* try to load, return if it's ok */
706 if(confpatharg)
708 if(luaA_loadrc(confpatharg, run))
710 ret = true;
711 goto bailout;
713 else if(!run)
714 goto bailout;
717 confpath = xdgConfigFind("awesome/rc.lua", xdg);
719 char *tmp = confpath;
721 /* confpath is "string1\0string2\0string3\0\0" */
722 while(*tmp)
724 if(luaA_loadrc(tmp, run))
726 ret = true;
727 goto bailout;
729 else if(!run)
730 goto bailout;
731 tmp += a_strlen(tmp) + 1;
734 bailout:
736 p_delete(&confpath);
738 return ret;
742 luaA_class_index_miss_property(lua_State *L, lua_object_t *obj)
744 signal_object_emit(L, &global_signals, "debug::index::miss", 2);
745 return 0;
749 luaA_class_newindex_miss_property(lua_State *L, lua_object_t *obj)
751 signal_object_emit(L, &global_signals, "debug::newindex::miss", 3);
752 return 0;
755 void
756 luaA_emit_refresh()
758 signal_object_emit(globalconf.L, &global_signals, "refresh", 0);
761 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80