Hide titlebars on fullscreen clients
[awesome.git] / luaa.c
blob3fb233b961bbfccbd03e6100c8ce006b36487e71
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 "objects/drawable.h"
43 #include "screen.h"
44 #include "event.h"
45 #include "selection.h"
46 #include "systray.h"
47 #include "common/xcursor.h"
48 #include "common/buffer.h"
49 #include "common/backtrace.h"
51 #ifdef WITH_DBUS
52 extern const struct luaL_Reg awesome_dbus_lib[];
53 #endif
54 extern const struct luaL_Reg awesome_keygrabber_lib[];
55 extern const struct luaL_Reg awesome_mousegrabber_lib[];
56 extern const struct luaL_Reg awesome_root_lib[];
57 extern const struct luaL_Reg awesome_mouse_methods[];
58 extern const struct luaL_Reg awesome_mouse_meta[];
59 extern const struct luaL_Reg awesome_screen_methods[];
60 extern const struct luaL_Reg awesome_screen_meta[];
62 /** Path to config file */
63 static char *conffile;
65 /** Quit awesome.
66 * \param L The Lua VM state.
67 * \return The number of elements pushed on stack.
69 static int
70 luaA_quit(lua_State *L)
72 ev_unloop(globalconf.loop, 1);
73 return 0;
76 /** Execute another application, probably a window manager, to replace
77 * awesome.
78 * \param L The Lua VM state.
79 * \return The number of elements pushed on stack.
80 * \luastack
81 * \lparam The command line to execute.
83 static int
84 luaA_exec(lua_State *L)
86 const char *cmd = luaL_checkstring(L, 1);
88 awesome_atexit(false);
90 a_exec(cmd);
91 return 0;
94 /** Restart awesome.
96 static int
97 luaA_restart(lua_State *L)
99 awesome_restart();
100 return 0;
103 /** Load an image from a given path.
104 * \param L The Lua VM state.
105 * \return The number of elements pushed on stack.
106 * \luastack
107 * \lparam The command line to execute.
109 static int
110 luaA_load_image(lua_State *L)
112 const char *filename = luaL_checkstring(L, 1);
113 cairo_surface_t *surface = draw_load_image(L, filename);
114 if (!surface)
115 return 0;
116 /* lua has to make sure to free the ref or we have a leak */
117 lua_pushlightuserdata(L, surface);
118 return 1;
121 /** UTF-8 aware string length computing.
122 * \param L The Lua VM state.
123 * \return The number of elements pushed on stack.
125 static int
126 luaA_mbstrlen(lua_State *L)
128 const char *cmd = luaL_checkstring(L, 1);
129 lua_pushnumber(L, (ssize_t) mbstowcs(NULL, NONULL(cmd), 0));
130 return 1;
133 /** Overload standard Lua next function to use __next key on metatable.
134 * \param L The Lua VM state.
135 * \return The number of elements pushed on stack.
137 static int
138 luaAe_next(lua_State *L)
140 if(luaL_getmetafield(L, 1, "__next"))
142 lua_insert(L, 1);
143 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
144 return lua_gettop(L);
147 luaL_checktype(L, 1, LUA_TTABLE);
148 lua_settop(L, 2);
149 if(lua_next(L, 1))
150 return 2;
151 lua_pushnil(L);
152 return 1;
155 /** Overload lua_next() function by using __next metatable field
156 * to get next elements.
157 * \param L The Lua VM stack.
158 * \param idx The index number of elements in stack.
159 * \return 1 if more elements to come, 0 otherwise.
161 static int
162 luaA_next(lua_State *L, int idx)
164 if(luaL_getmetafield(L, idx, "__next"))
166 /* if idx is relative, reduce it since we got __next */
167 if(idx < 0) idx--;
168 /* copy table and then move key */
169 lua_pushvalue(L, idx);
170 lua_pushvalue(L, -3);
171 lua_remove(L, -4);
172 lua_pcall(L, 2, 2, 0);
173 /* next returned nil, it's the end */
174 if(lua_isnil(L, -1))
176 /* remove nil */
177 lua_pop(L, 2);
178 return 0;
180 return 1;
182 else if(lua_istable(L, idx))
183 return lua_next(L, idx);
184 /* remove the key */
185 lua_pop(L, 1);
186 return 0;
189 /** Generic pairs function.
190 * \param L The Lua VM state.
191 * \return The number of elements pushed on stack.
193 static int
194 luaA_generic_pairs(lua_State *L)
196 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
197 lua_pushvalue(L, 1); /* state, */
198 lua_pushnil(L); /* and initial value */
199 return 3;
202 /** Overload standard pairs function to use __pairs field of metatables.
203 * \param L The Lua VM state.
204 * \return The number of elements pushed on stack.
206 static int
207 luaAe_pairs(lua_State *L)
209 if(luaL_getmetafield(L, 1, "__pairs"))
211 lua_insert(L, 1);
212 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
213 return lua_gettop(L);
216 luaL_checktype(L, 1, LUA_TTABLE);
217 return luaA_generic_pairs(L);
220 static int
221 luaA_ipairs_aux(lua_State *L)
223 int i = luaL_checkint(L, 2) + 1;
224 luaL_checktype(L, 1, LUA_TTABLE);
225 lua_pushinteger(L, i);
226 lua_rawgeti(L, 1, i);
227 return (lua_isnil(L, -1)) ? 0 : 2;
230 /** Overload standard ipairs function to use __ipairs field of metatables.
231 * \param L The Lua VM state.
232 * \return The number of elements pushed on stack.
234 static int
235 luaAe_ipairs(lua_State *L)
237 if(luaL_getmetafield(L, 1, "__ipairs"))
239 lua_insert(L, 1);
240 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
241 return lua_gettop(L);
244 luaL_checktype(L, 1, LUA_TTABLE);
245 lua_pushvalue(L, lua_upvalueindex(1));
246 lua_pushvalue(L, 1);
247 lua_pushinteger(L, 0); /* and initial value */
248 return 3;
251 /** Enhanced type() function which recognize awesome objects.
252 * \param L The Lua VM state.
253 * \return The number of arguments pushed on the stack.
255 static int
256 luaAe_type(lua_State *L)
258 luaL_checkany(L, 1);
259 lua_pushstring(L, luaA_typename(L, 1));
260 return 1;
263 /** Replace various standards Lua functions with our own.
264 * \param L The Lua VM state.
266 static void
267 luaA_fixups(lua_State *L)
269 /* export string.wlen */
270 lua_getglobal(L, "string");
271 lua_pushcfunction(L, luaA_mbstrlen);
272 lua_setfield(L, -2, "wlen");
273 lua_pop(L, 1);
274 /* replace next */
275 lua_pushcfunction(L, luaAe_next);
276 lua_setglobal(L, "next");
277 /* replace pairs */
278 lua_pushcfunction(L, luaAe_next);
279 lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
280 lua_setglobal(L, "pairs");
281 /* replace ipairs */
282 lua_pushcfunction(L, luaA_ipairs_aux);
283 lua_pushcclosure(L, luaAe_ipairs, 1);
284 lua_setglobal(L, "ipairs");
285 /* replace type */
286 lua_pushcfunction(L, luaAe_type);
287 lua_setglobal(L, "type");
288 /* set selection */
289 lua_pushcfunction(L, luaA_selection_get);
290 lua_setglobal(L, "selection");
293 /** Look for an item: table, function, etc.
294 * \param L The Lua VM state.
295 * \param item The pointer item.
297 bool
298 luaA_hasitem(lua_State *L, const void *item)
300 lua_pushnil(L);
301 while(luaA_next(L, -2))
303 if(lua_topointer(L, -1) == item)
305 /* remove value and key */
306 lua_pop(L, 2);
307 return true;
309 if(lua_istable(L, -1))
310 if(luaA_hasitem(L, item))
312 /* remove key and value */
313 lua_pop(L, 2);
314 return true;
316 /* remove value */
317 lua_pop(L, 1);
319 return false;
322 /** Browse a table pushed on top of the index, and put all its table and
323 * sub-table into an array.
324 * \param L The Lua VM state.
325 * \param elems The elements array.
326 * \return False if we encounter an elements already in list.
328 static bool
329 luaA_isloop_check(lua_State *L, cptr_array_t *elems)
331 if(lua_istable(L, -1))
333 const void *object = lua_topointer(L, -1);
335 /* Check that the object table is not already in the list */
336 for(int i = 0; i < elems->len; i++)
337 if(elems->tab[i] == object)
338 return false;
340 /* push the table in the elements list */
341 cptr_array_append(elems, object);
343 /* look every object in the "table" */
344 lua_pushnil(L);
345 while(luaA_next(L, -2))
347 if(!luaA_isloop_check(L, elems))
349 /* remove key and value */
350 lua_pop(L, 2);
351 return false;
353 /* remove value, keep key for next iteration */
354 lua_pop(L, 1);
357 return true;
360 /** Check if a table is a loop. When using tables as direct acyclic digram,
361 * this is useful.
362 * \param L The Lua VM state.
363 * \param idx The index of the table in the stack
364 * \return True if the table loops.
366 bool
367 luaA_isloop(lua_State *L, int idx)
369 /* elems is an elements array that we will fill with all array we
370 * encounter while browsing the tables */
371 cptr_array_t elems;
373 cptr_array_init(&elems);
375 /* push table on top */
376 lua_pushvalue(L, idx);
378 bool ret = luaA_isloop_check(L, &elems);
380 /* remove pushed table */
381 lua_pop(L, 1);
383 cptr_array_wipe(&elems);
385 return !ret;
388 /** awesome global table.
389 * \param L The Lua VM state.
390 * \return The number of elements pushed on stack.
391 * \luastack
392 * \lfield font The default font.
393 * \lfield font_height The default font height.
394 * \lfield conffile The configuration file which has been loaded.
396 static int
397 luaA_awesome_index(lua_State *L)
399 if(luaA_usemetatable(L, 1, 2))
400 return 1;
402 const char *buf = luaL_checkstring(L, 2);
404 if(A_STREQ(buf, "conffile"))
406 lua_pushstring(L, conffile);
407 return 1;
410 if(A_STREQ(buf, "version"))
412 lua_pushliteral(L, AWESOME_VERSION);
413 return 1;
416 if(A_STREQ(buf, "release"))
418 lua_pushliteral(L, AWESOME_RELEASE);
419 return 1;
422 if(A_STREQ(buf, "startup_errors") && globalconf.startup_errors.len != 0)
424 lua_pushstring(L, globalconf.startup_errors.s);
425 return 1;
428 return 0;
431 /** Add a global signal.
432 * \param L The Lua VM state.
433 * \return The number of elements pushed on stack.
434 * \luastack
435 * \lparam A string with the event name.
436 * \lparam The function to call.
438 static int
439 luaA_awesome_connect_signal(lua_State *L)
441 const char *name = luaL_checkstring(L, 1);
442 luaA_checkfunction(L, 2);
443 signal_connect(&global_signals, name, luaA_object_ref(L, 2));
444 return 0;
447 /** Remove a global signal.
448 * \param L The Lua VM state.
449 * \return The number of elements pushed on stack.
450 * \luastack
451 * \lparam A string with the event name.
452 * \lparam The function to call.
454 static int
455 luaA_awesome_disconnect_signal(lua_State *L)
457 const char *name = luaL_checkstring(L, 1);
458 luaA_checkfunction(L, 2);
459 const void *func = lua_topointer(L, 2);
460 signal_disconnect(&global_signals, name, func);
461 luaA_object_unref(L, (void *) func);
462 return 0;
465 /** Emit a global signal.
466 * \param L The Lua VM state.
467 * \return The number of elements pushed on stack.
468 * \luastack
469 * \lparam A string with the event name.
470 * \lparam The function to call.
472 static int
473 luaA_awesome_emit_signal(lua_State *L)
475 signal_object_emit(L, &global_signals, luaL_checkstring(L, 1), lua_gettop(L) - 1);
476 return 0;
479 static int
480 luaA_panic(lua_State *L)
482 warn("unprotected error in call to Lua API (%s)",
483 lua_tostring(L, -1));
484 buffer_t buf;
485 backtrace_get(&buf);
486 warn("dumping backtrace\n%s", buf.s);
487 warn("restarting awesome");
488 awesome_restart();
489 return 0;
492 static int
493 luaA_dofunction_on_error(lua_State *L)
495 /* duplicate string error */
496 lua_pushvalue(L, -1);
497 /* emit error signal */
498 signal_object_emit(L, &global_signals, "debug::error", 1);
500 if(!luaL_dostring(L, "return debug.traceback(\"error while running function\", 3)"))
502 /* Move traceback before error */
503 lua_insert(L, -2);
504 /* Insert sentence */
505 lua_pushliteral(L, "\nerror: ");
506 /* Move it before error */
507 lua_insert(L, -2);
508 lua_concat(L, 3);
510 return 1;
513 /** Initialize the Lua VM
514 * \param xdg An xdg handle to use to get XDG basedir.
516 void
517 luaA_init(xdgHandle* xdg)
519 lua_State *L;
520 static const struct luaL_Reg awesome_lib[] =
522 { "quit", luaA_quit },
523 { "exec", luaA_exec },
524 { "spawn", luaA_spawn },
525 { "restart", luaA_restart },
526 { "connect_signal", luaA_awesome_connect_signal },
527 { "disconnect_signal", luaA_awesome_disconnect_signal },
528 { "emit_signal", luaA_awesome_emit_signal },
529 { "systray", luaA_systray },
530 { "load_image", luaA_load_image },
531 { "__index", luaA_awesome_index },
532 { NULL, NULL }
535 L = globalconf.L = luaL_newstate();
537 /* Set panic function */
538 lua_atpanic(L, luaA_panic);
540 /* Set error handling function */
541 lualib_dofunction_on_error = luaA_dofunction_on_error;
543 luaL_openlibs(L);
545 luaA_fixups(L);
547 luaA_object_setup(L);
549 /* Export awesome lib */
550 luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
552 /* Export root lib */
553 luaA_registerlib(L, "root", awesome_root_lib);
554 lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
556 #ifdef WITH_DBUS
557 /* Export D-Bus lib */
558 luaA_registerlib(L, "dbus", awesome_dbus_lib);
559 lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
560 #endif
562 /* Export keygrabber lib */
563 luaA_registerlib(L, "keygrabber", awesome_keygrabber_lib);
564 lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
566 /* Export mousegrabber lib */
567 luaA_registerlib(L, "mousegrabber", awesome_mousegrabber_lib);
568 lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
570 /* Export screen */
571 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
573 /* Export mouse */
574 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
576 /* Export button */
577 button_class_setup(L);
579 /* Export tag */
580 tag_class_setup(L);
582 /* Export window */
583 window_class_setup(L);
585 /* Export drawable */
586 drawable_class_setup(L);
588 /* Export drawin */
589 drawin_class_setup(L);
591 /* Export client */
592 client_class_setup(L);
594 /* Export keys */
595 key_class_setup(L);
597 /* Export timer */
598 timer_class_setup(L);
600 /* add Lua search paths */
601 lua_getglobal(L, "package");
602 if (LUA_TTABLE != lua_type(L, 1))
604 warn("package is not a table");
605 return;
607 lua_getfield(L, 1, "path");
608 if (LUA_TSTRING != lua_type(L, 2))
610 warn("package.path is not a string");
611 lua_pop(L, 1);
612 return;
615 /* add XDG_CONFIG_DIR as include path */
616 const char * const *xdgconfigdirs = xdgSearchableConfigDirectories(xdg);
617 for(; *xdgconfigdirs; xdgconfigdirs++)
619 size_t len = a_strlen(*xdgconfigdirs);
620 lua_pushliteral(L, ";");
621 lua_pushlstring(L, *xdgconfigdirs, len);
622 lua_pushliteral(L, "/awesome/?.lua");
623 lua_concat(L, 3);
625 lua_pushliteral(L, ";");
626 lua_pushlstring(L, *xdgconfigdirs, len);
627 lua_pushliteral(L, "/awesome/?/init.lua");
628 lua_concat(L, 3);
630 lua_concat(L, 3); /* concatenate with package.path */
633 /* add Lua lib path (/usr/share/awesome/lib by default) */
634 lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?.lua");
635 lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?/init.lua");
636 lua_concat(L, 3); /* concatenate with package.path */
637 lua_setfield(L, 1, "path"); /* package.path = "concatenated string" */
639 lua_getfield(L, 1, "loaded");
641 lua_pop(L, 2); /* pop "package" and "package.loaded" */
643 signal_add(&global_signals, "debug::error");
644 signal_add(&global_signals, "debug::deprecation");
645 signal_add(&global_signals, "debug::index::miss");
646 signal_add(&global_signals, "debug::newindex::miss");
647 signal_add(&global_signals, "systray::update");
648 signal_add(&global_signals, "wallpaper_changed");
649 signal_add(&global_signals, "refresh");
650 signal_add(&global_signals, "exit");
653 static void
654 luaA_startup_error(const char *err)
656 if (globalconf.startup_errors.len > 0)
657 buffer_addsl(&globalconf.startup_errors, "\n\n");
658 buffer_adds(&globalconf.startup_errors, err);
661 static bool
662 luaA_loadrc(const char *confpath, bool run)
664 if(!luaL_loadfile(globalconf.L, confpath))
666 if(run)
668 /* Set the conffile right now so it can be used inside the
669 * configuration file. */
670 conffile = a_strdup(confpath);
671 if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
673 const char *err = lua_tostring(globalconf.L, -1);
674 luaA_startup_error(err);
675 fprintf(stderr, "%s\n", err);
676 /* An error happened, so reset this. */
677 conffile = NULL;
679 else
680 return true;
682 else
684 lua_pop(globalconf.L, 1);
685 return true;
688 else
690 const char *err = lua_tostring(globalconf.L, -1);
691 luaA_startup_error(err);
692 fprintf(stderr, "%s\n", err);
695 return false;
698 /** Load a configuration file.
699 * \param xdg An xdg handle to use to get XDG basedir.
700 * \param confpatharg The configuration file to load.
701 * \param run Run the configuration file.
703 bool
704 luaA_parserc(xdgHandle* xdg, const char *confpatharg, bool run)
706 char *confpath = NULL;
707 bool ret = false;
709 /* try to load, return if it's ok */
710 if(confpatharg)
712 if(luaA_loadrc(confpatharg, run))
714 ret = true;
715 goto bailout;
717 else if(!run)
718 goto bailout;
721 confpath = xdgConfigFind("awesome/rc.lua", xdg);
723 char *tmp = confpath;
725 /* confpath is "string1\0string2\0string3\0\0" */
726 while(*tmp)
728 if(luaA_loadrc(tmp, run))
730 ret = true;
731 goto bailout;
733 else if(!run)
734 goto bailout;
735 tmp += a_strlen(tmp) + 1;
738 bailout:
740 p_delete(&confpath);
742 return ret;
746 luaA_class_index_miss_property(lua_State *L, lua_object_t *obj)
748 signal_object_emit(L, &global_signals, "debug::index::miss", 2);
749 return 0;
753 luaA_class_newindex_miss_property(lua_State *L, lua_object_t *obj)
755 signal_object_emit(L, &global_signals, "debug::newindex::miss", 3);
756 return 0;
759 void
760 luaA_emit_refresh()
762 signal_object_emit(globalconf.L, &global_signals, "refresh", 0);
765 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80