screen: Add screen.by_coords()
[awesome.git] / luaa.c
blob103c63fa565aeed92fd48509d1222f97a437f875
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 /** UTF-8 aware string length computing.
106 * \param L The Lua VM state.
107 * \return The number of elements pushed on stack.
109 static int
110 luaA_mbstrlen(lua_State *L)
112 const char *cmd = luaL_checkstring(L, 1);
113 lua_pushnumber(L, (ssize_t) mbstowcs(NULL, NONULL(cmd), 0));
114 return 1;
117 /** Overload standard Lua next function to use __next key on metatable.
118 * \param L The Lua VM state.
119 * \return The number of elements pushed on stack.
121 static int
122 luaAe_next(lua_State *L)
124 if(luaL_getmetafield(L, 1, "__next"))
126 lua_insert(L, 1);
127 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
128 return lua_gettop(L);
131 luaL_checktype(L, 1, LUA_TTABLE);
132 lua_settop(L, 2);
133 if(lua_next(L, 1))
134 return 2;
135 lua_pushnil(L);
136 return 1;
139 /** Overload lua_next() function by using __next metatable field
140 * to get next elements.
141 * \param L The Lua VM stack.
142 * \param idx The index number of elements in stack.
143 * \return 1 if more elements to come, 0 otherwise.
145 static int
146 luaA_next(lua_State *L, int idx)
148 if(luaL_getmetafield(L, idx, "__next"))
150 /* if idx is relative, reduce it since we got __next */
151 if(idx < 0) idx--;
152 /* copy table and then move key */
153 lua_pushvalue(L, idx);
154 lua_pushvalue(L, -3);
155 lua_remove(L, -4);
156 lua_pcall(L, 2, 2, 0);
157 /* next returned nil, it's the end */
158 if(lua_isnil(L, -1))
160 /* remove nil */
161 lua_pop(L, 2);
162 return 0;
164 return 1;
166 else if(lua_istable(L, idx))
167 return lua_next(L, idx);
168 /* remove the key */
169 lua_pop(L, 1);
170 return 0;
173 /** Generic pairs function.
174 * \param L The Lua VM state.
175 * \return The number of elements pushed on stack.
177 static int
178 luaA_generic_pairs(lua_State *L)
180 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
181 lua_pushvalue(L, 1); /* state, */
182 lua_pushnil(L); /* and initial value */
183 return 3;
186 /** Overload standard pairs function to use __pairs field of metatables.
187 * \param L The Lua VM state.
188 * \return The number of elements pushed on stack.
190 static int
191 luaAe_pairs(lua_State *L)
193 if(luaL_getmetafield(L, 1, "__pairs"))
195 lua_insert(L, 1);
196 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
197 return lua_gettop(L);
200 luaL_checktype(L, 1, LUA_TTABLE);
201 return luaA_generic_pairs(L);
204 static int
205 luaA_ipairs_aux(lua_State *L)
207 int i = luaL_checkint(L, 2) + 1;
208 luaL_checktype(L, 1, LUA_TTABLE);
209 lua_pushinteger(L, i);
210 lua_rawgeti(L, 1, i);
211 return (lua_isnil(L, -1)) ? 0 : 2;
214 /** Overload standard ipairs function to use __ipairs field of metatables.
215 * \param L The Lua VM state.
216 * \return The number of elements pushed on stack.
218 static int
219 luaAe_ipairs(lua_State *L)
221 if(luaL_getmetafield(L, 1, "__ipairs"))
223 lua_insert(L, 1);
224 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
225 return lua_gettop(L);
228 luaL_checktype(L, 1, LUA_TTABLE);
229 lua_pushvalue(L, lua_upvalueindex(1));
230 lua_pushvalue(L, 1);
231 lua_pushinteger(L, 0); /* and initial value */
232 return 3;
235 /** Enhanced type() function which recognize awesome objects.
236 * \param L The Lua VM state.
237 * \return The number of arguments pushed on the stack.
239 static int
240 luaAe_type(lua_State *L)
242 luaL_checkany(L, 1);
243 lua_pushstring(L, luaA_typename(L, 1));
244 return 1;
247 /** Replace various standards Lua functions with our own.
248 * \param L The Lua VM state.
250 static void
251 luaA_fixups(lua_State *L)
253 /* export string.wlen */
254 lua_getglobal(L, "string");
255 lua_pushcfunction(L, luaA_mbstrlen);
256 lua_setfield(L, -2, "wlen");
257 lua_pop(L, 1);
258 /* replace next */
259 lua_pushliteral(L, "next");
260 lua_pushcfunction(L, luaAe_next);
261 lua_settable(L, LUA_GLOBALSINDEX);
262 /* replace pairs */
263 lua_pushliteral(L, "pairs");
264 lua_pushcfunction(L, luaAe_next);
265 lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
266 lua_settable(L, LUA_GLOBALSINDEX);
267 /* replace ipairs */
268 lua_pushliteral(L, "ipairs");
269 lua_pushcfunction(L, luaA_ipairs_aux);
270 lua_pushcclosure(L, luaAe_ipairs, 1);
271 lua_settable(L, LUA_GLOBALSINDEX);
272 /* replace type */
273 lua_pushliteral(L, "type");
274 lua_pushcfunction(L, luaAe_type);
275 lua_settable(L, LUA_GLOBALSINDEX);
276 /* set selection */
277 lua_pushliteral(L, "selection");
278 lua_pushcfunction(L, luaA_selection_get);
279 lua_settable(L, LUA_GLOBALSINDEX);
282 /** Look for an item: table, function, etc.
283 * \param L The Lua VM state.
284 * \param item The pointer item.
286 bool
287 luaA_hasitem(lua_State *L, const void *item)
289 lua_pushnil(L);
290 while(luaA_next(L, -2))
292 if(lua_topointer(L, -1) == item)
294 /* remove value and key */
295 lua_pop(L, 2);
296 return true;
298 if(lua_istable(L, -1))
299 if(luaA_hasitem(L, item))
301 /* remove key and value */
302 lua_pop(L, 2);
303 return true;
305 /* remove value */
306 lua_pop(L, 1);
308 return false;
311 /** Browse a table pushed on top of the index, and put all its table and
312 * sub-table into an array.
313 * \param L The Lua VM state.
314 * \param elems The elements array.
315 * \return False if we encounter an elements already in list.
317 static bool
318 luaA_isloop_check(lua_State *L, cptr_array_t *elems)
320 if(lua_istable(L, -1))
322 const void *object = lua_topointer(L, -1);
324 /* Check that the object table is not already in the list */
325 for(int i = 0; i < elems->len; i++)
326 if(elems->tab[i] == object)
327 return false;
329 /* push the table in the elements list */
330 cptr_array_append(elems, object);
332 /* look every object in the "table" */
333 lua_pushnil(L);
334 while(luaA_next(L, -2))
336 if(!luaA_isloop_check(L, elems))
338 /* remove key and value */
339 lua_pop(L, 2);
340 return false;
342 /* remove value, keep key for next iteration */
343 lua_pop(L, 1);
346 return true;
349 /** Check if a table is a loop. When using tables as direct acyclic digram,
350 * this is useful.
351 * \param L The Lua VM state.
352 * \param idx The index of the table in the stack
353 * \return True if the table loops.
355 bool
356 luaA_isloop(lua_State *L, int idx)
358 /* elems is an elements array that we will fill with all array we
359 * encounter while browsing the tables */
360 cptr_array_t elems;
362 cptr_array_init(&elems);
364 /* push table on top */
365 lua_pushvalue(L, idx);
367 bool ret = luaA_isloop_check(L, &elems);
369 /* remove pushed table */
370 lua_pop(L, 1);
372 cptr_array_wipe(&elems);
374 return !ret;
377 /** awesome global table.
378 * \param L The Lua VM state.
379 * \return The number of elements pushed on stack.
380 * \luastack
381 * \lfield font The default font.
382 * \lfield font_height The default font height.
383 * \lfield conffile The configuration file which has been loaded.
385 static int
386 luaA_awesome_index(lua_State *L)
388 if(luaA_usemetatable(L, 1, 2))
389 return 1;
391 const char *buf = luaL_checkstring(L, 2);
393 if(A_STREQ(buf, "conffile"))
395 lua_pushstring(L, conffile);
396 return 1;
399 if(A_STREQ(buf, "version"))
401 lua_pushliteral(L, AWESOME_VERSION);
402 return 1;
405 if(A_STREQ(buf, "release"))
407 lua_pushliteral(L, AWESOME_RELEASE);
408 return 1;
411 if(A_STREQ(buf, "startup_errors") && globalconf.startup_errors.len != 0)
413 lua_pushstring(L, globalconf.startup_errors.s);
414 return 1;
417 return 0;
420 /** Add a global signal.
421 * \param L The Lua VM state.
422 * \return The number of elements pushed on stack.
423 * \luastack
424 * \lparam A string with the event name.
425 * \lparam The function to call.
427 static int
428 luaA_awesome_connect_signal(lua_State *L)
430 const char *name = luaL_checkstring(L, 1);
431 luaA_checkfunction(L, 2);
432 signal_connect(&global_signals, name, luaA_object_ref(L, 2));
433 return 0;
436 /** Remove a global signal.
437 * \param L The Lua VM state.
438 * \return The number of elements pushed on stack.
439 * \luastack
440 * \lparam A string with the event name.
441 * \lparam The function to call.
443 static int
444 luaA_awesome_disconnect_signal(lua_State *L)
446 const char *name = luaL_checkstring(L, 1);
447 luaA_checkfunction(L, 2);
448 const void *func = lua_topointer(L, 2);
449 signal_disconnect(&global_signals, name, func);
450 luaA_object_unref(L, (void *) func);
451 return 0;
454 /** Emit a global signal.
455 * \param L The Lua VM state.
456 * \return The number of elements pushed on stack.
457 * \luastack
458 * \lparam A string with the event name.
459 * \lparam The function to call.
461 static int
462 luaA_awesome_emit_signal(lua_State *L)
464 signal_object_emit(L, &global_signals, luaL_checkstring(L, 1), lua_gettop(L) - 1);
465 return 0;
468 static int
469 luaA_panic(lua_State *L)
471 warn("unprotected error in call to Lua API (%s)",
472 lua_tostring(L, -1));
473 buffer_t buf;
474 backtrace_get(&buf);
475 warn("dumping backtrace\n%s", buf.s);
476 warn("restarting awesome");
477 awesome_restart();
478 return 0;
481 static int
482 luaA_dofunction_on_error(lua_State *L)
484 /* duplicate string error */
485 lua_pushvalue(L, -1);
486 /* emit error signal */
487 signal_object_emit(L, &global_signals, "debug::error", 1);
489 if(!luaL_dostring(L, "return debug.traceback(\"error while running function\", 3)"))
491 /* Move traceback before error */
492 lua_insert(L, -2);
493 /* Insert sentence */
494 lua_pushliteral(L, "\nerror: ");
495 /* Move it before error */
496 lua_insert(L, -2);
497 lua_concat(L, 3);
499 return 1;
502 /** Initialize the Lua VM
503 * \param xdg An xdg handle to use to get XDG basedir.
505 void
506 luaA_init(xdgHandle* xdg)
508 lua_State *L;
509 static const struct luaL_reg awesome_lib[] =
511 { "quit", luaA_quit },
512 { "exec", luaA_exec },
513 { "spawn", luaA_spawn },
514 { "restart", luaA_restart },
515 { "connect_signal", luaA_awesome_connect_signal },
516 { "disconnect_signal", luaA_awesome_disconnect_signal },
517 { "emit_signal", luaA_awesome_emit_signal },
518 { "systray", luaA_systray },
519 { "__index", luaA_awesome_index },
520 { NULL, NULL }
523 L = globalconf.L = luaL_newstate();
525 /* Set panic function */
526 lua_atpanic(L, luaA_panic);
528 /* Set error handling function */
529 lualib_dofunction_on_error = luaA_dofunction_on_error;
531 luaL_openlibs(L);
533 luaA_fixups(L);
535 luaA_object_setup(L);
537 /* Export awesome lib */
538 luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
540 /* Export root lib */
541 luaL_register(L, "root", awesome_root_lib);
542 lua_pop(L, 1); /* luaL_register() leaves the table on stack */
544 #ifdef WITH_DBUS
545 /* Export D-Bus lib */
546 luaL_register(L, "dbus", awesome_dbus_lib);
547 lua_pop(L, 1); /* luaL_register() leaves the table on stack */
548 #endif
550 /* Export keygrabber lib */
551 luaL_register(L, "keygrabber", awesome_keygrabber_lib);
552 lua_pop(L, 1); /* luaL_register() leaves the table on stack */
554 /* Export mousegrabber lib */
555 luaL_register(L, "mousegrabber", awesome_mousegrabber_lib);
556 lua_pop(L, 1); /* luaL_register() leaves the table on stack */
558 /* Export screen */
559 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
561 /* Export mouse */
562 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
564 /* Export button */
565 button_class_setup(L);
567 /* Export tag */
568 tag_class_setup(L);
570 /* Export window */
571 window_class_setup(L);
573 /* Export drawin */
574 drawin_class_setup(L);
576 /* Export client */
577 client_class_setup(L);
579 /* Export keys */
580 key_class_setup(L);
582 /* Export timer */
583 timer_class_setup(L);
585 /* add Lua search paths */
586 lua_getglobal(L, "package");
587 if (LUA_TTABLE != lua_type(L, 1))
589 warn("package is not a table");
590 return;
592 lua_getfield(L, 1, "path");
593 if (LUA_TSTRING != lua_type(L, 2))
595 warn("package.path is not a string");
596 lua_pop(L, 1);
597 return;
600 /* add XDG_CONFIG_DIR as include path */
601 const char * const *xdgconfigdirs = xdgSearchableConfigDirectories(xdg);
602 for(; *xdgconfigdirs; xdgconfigdirs++)
604 size_t len = a_strlen(*xdgconfigdirs);
605 lua_pushliteral(L, ";");
606 lua_pushlstring(L, *xdgconfigdirs, len);
607 lua_pushliteral(L, "/awesome/?.lua");
608 lua_concat(L, 3);
610 lua_pushliteral(L, ";");
611 lua_pushlstring(L, *xdgconfigdirs, len);
612 lua_pushliteral(L, "/awesome/?/init.lua");
613 lua_concat(L, 3);
615 lua_concat(L, 3); /* concatenate with package.path */
618 /* add Lua lib path (/usr/share/awesome/lib by default) */
619 lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?.lua");
620 lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?/init.lua");
621 lua_concat(L, 3); /* concatenate with package.path */
622 lua_setfield(L, 1, "path"); /* package.path = "concatenated string" */
624 lua_getfield(L, 1, "loaded");
626 /* Load oocairo */
627 if (luaopen_oocairo(L) != 1)
628 fatal("Loading oocairo failed");
629 lua_pushvalue(L, 3); /* Copy the module */
630 lua_setglobal(L, "oocairo"); /* Set the global entry */
631 lua_setfield(L, 2, "oocairo"); /* Make it require()able */
633 /* Load oopango */
634 if (luaopen_oopango(L) != 1)
635 fatal("Loading oopango failed");
636 lua_pushvalue(L, 3); /* Copy the module */
637 lua_setglobal(L, "oopango"); /* Set the global entry */
638 lua_setfield(L, 2, "oopango"); /* Make it require()able */
640 lua_pop(L, 2); /* pop "package" and "package.loaded" */
642 signal_add(&global_signals, "debug::error");
643 signal_add(&global_signals, "debug::deprecation");
644 signal_add(&global_signals, "debug::index::miss");
645 signal_add(&global_signals, "debug::newindex::miss");
646 signal_add(&global_signals, "systray::update");
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