Only set client's urgent after startup
[awesome.git] / luaa.c
blob340bf4635463413a78e00463060f4c81ef857066
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 <xcb/xcb_atom.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 "property.h"
46 #include "selection.h"
47 #include "systray.h"
48 #include "common/xcursor.h"
49 #include "common/buffer.h"
50 #include "common/backtrace.h"
52 #ifdef WITH_DBUS
53 extern const struct luaL_Reg awesome_dbus_lib[];
54 #endif
55 extern const struct luaL_Reg awesome_keygrabber_lib[];
56 extern const struct luaL_Reg awesome_mousegrabber_lib[];
57 extern const struct luaL_Reg awesome_root_lib[];
58 extern const struct luaL_Reg awesome_mouse_methods[];
59 extern const struct luaL_Reg awesome_mouse_meta[];
60 extern const struct luaL_Reg awesome_screen_methods[];
61 extern const struct luaL_Reg awesome_screen_meta[];
63 /** Path to config file */
64 static char *conffile;
66 /** Check whether a composite manager is running.
67 * \return True if such a manager is running.
69 static bool
70 composite_manager_running(void)
72 xcb_intern_atom_reply_t *atom_r;
73 xcb_get_selection_owner_reply_t *selection_r;
74 char *atom_name;
75 bool result;
77 if(!(atom_name = xcb_atom_name_by_screen("_NET_WM_CM", globalconf.default_screen)))
79 warn("error getting composite manager atom");
80 return false;
83 atom_r = xcb_intern_atom_reply(globalconf.connection,
84 xcb_intern_atom_unchecked(globalconf.connection, false,
85 a_strlen(atom_name), atom_name),
86 NULL);
87 p_delete(&atom_name);
88 if(!atom_r)
89 return false;
91 selection_r = xcb_get_selection_owner_reply(globalconf.connection,
92 xcb_get_selection_owner_unchecked(globalconf.connection,
93 atom_r->atom),
94 NULL);
95 p_delete(&atom_r);
97 result = selection_r != NULL && selection_r->owner != XCB_NONE;
98 p_delete(&selection_r);
100 return result;
103 /** Quit awesome.
104 * \param L The Lua VM state.
105 * \return The number of elements pushed on stack.
107 static int
108 luaA_quit(lua_State *L)
110 g_main_loop_quit(globalconf.loop);
111 return 0;
114 /** Execute another application, probably a window manager, to replace
115 * awesome.
116 * \param L The Lua VM state.
117 * \return The number of elements pushed on stack.
118 * \luastack
119 * \lparam The command line to execute.
121 static int
122 luaA_exec(lua_State *L)
124 const char *cmd = luaL_checkstring(L, 1);
126 awesome_atexit(false);
128 a_exec(cmd);
129 return 0;
132 /** Restart awesome.
134 static int
135 luaA_restart(lua_State *L)
137 awesome_restart();
138 return 0;
141 /** Load an image from a given path.
142 * \param L The Lua VM state.
143 * \return The number of elements pushed on stack.
144 * \luastack
145 * \lparam The command line to execute.
147 static int
148 luaA_load_image(lua_State *L)
150 const char *filename = luaL_checkstring(L, 1);
151 cairo_surface_t *surface = draw_load_image(L, filename);
152 if (!surface)
153 return 0;
154 /* lua has to make sure to free the ref or we have a leak */
155 lua_pushlightuserdata(L, surface);
156 return 1;
159 /** UTF-8 aware string length computing.
160 * \param L The Lua VM state.
161 * \return The number of elements pushed on stack.
163 static int
164 luaA_mbstrlen(lua_State *L)
166 const char *cmd = luaL_checkstring(L, 1);
167 lua_pushnumber(L, (ssize_t) mbstowcs(NULL, NONULL(cmd), 0));
168 return 1;
171 /** Overload standard Lua next function to use __next key on metatable.
172 * \param L The Lua VM state.
173 * \return The number of elements pushed on stack.
175 static int
176 luaAe_next(lua_State *L)
178 if(luaL_getmetafield(L, 1, "__next"))
180 lua_insert(L, 1);
181 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
182 return lua_gettop(L);
185 luaL_checktype(L, 1, LUA_TTABLE);
186 lua_settop(L, 2);
187 if(lua_next(L, 1))
188 return 2;
189 lua_pushnil(L);
190 return 1;
193 /** Overload lua_next() function by using __next metatable field
194 * to get next elements.
195 * \param L The Lua VM stack.
196 * \param idx The index number of elements in stack.
197 * \return 1 if more elements to come, 0 otherwise.
199 static int
200 luaA_next(lua_State *L, int idx)
202 if(luaL_getmetafield(L, idx, "__next"))
204 /* if idx is relative, reduce it since we got __next */
205 if(idx < 0) idx--;
206 /* copy table and then move key */
207 lua_pushvalue(L, idx);
208 lua_pushvalue(L, -3);
209 lua_remove(L, -4);
210 lua_pcall(L, 2, 2, 0);
211 /* next returned nil, it's the end */
212 if(lua_isnil(L, -1))
214 /* remove nil */
215 lua_pop(L, 2);
216 return 0;
218 return 1;
220 else if(lua_istable(L, idx))
221 return lua_next(L, idx);
222 /* remove the key */
223 lua_pop(L, 1);
224 return 0;
227 /** Generic pairs function.
228 * \param L The Lua VM state.
229 * \return The number of elements pushed on stack.
231 static int
232 luaA_generic_pairs(lua_State *L)
234 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
235 lua_pushvalue(L, 1); /* state, */
236 lua_pushnil(L); /* and initial value */
237 return 3;
240 /** Overload standard pairs function to use __pairs field of metatables.
241 * \param L The Lua VM state.
242 * \return The number of elements pushed on stack.
244 static int
245 luaAe_pairs(lua_State *L)
247 if(luaL_getmetafield(L, 1, "__pairs"))
249 lua_insert(L, 1);
250 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
251 return lua_gettop(L);
254 luaL_checktype(L, 1, LUA_TTABLE);
255 return luaA_generic_pairs(L);
258 static int
259 luaA_ipairs_aux(lua_State *L)
261 int i = luaL_checkint(L, 2) + 1;
262 luaL_checktype(L, 1, LUA_TTABLE);
263 lua_pushinteger(L, i);
264 lua_rawgeti(L, 1, i);
265 return (lua_isnil(L, -1)) ? 0 : 2;
268 /** Overload standard ipairs function to use __ipairs field of metatables.
269 * \param L The Lua VM state.
270 * \return The number of elements pushed on stack.
272 static int
273 luaAe_ipairs(lua_State *L)
275 if(luaL_getmetafield(L, 1, "__ipairs"))
277 lua_insert(L, 1);
278 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
279 return lua_gettop(L);
282 luaL_checktype(L, 1, LUA_TTABLE);
283 lua_pushvalue(L, lua_upvalueindex(1));
284 lua_pushvalue(L, 1);
285 lua_pushinteger(L, 0); /* and initial value */
286 return 3;
289 /** Enhanced type() function which recognize awesome objects.
290 * \param L The Lua VM state.
291 * \return The number of arguments pushed on the stack.
293 static int
294 luaAe_type(lua_State *L)
296 luaL_checkany(L, 1);
297 lua_pushstring(L, luaA_typename(L, 1));
298 return 1;
301 /** Replace various standards Lua functions with our own.
302 * \param L The Lua VM state.
304 static void
305 luaA_fixups(lua_State *L)
307 /* export string.wlen */
308 lua_getglobal(L, "string");
309 lua_pushcfunction(L, luaA_mbstrlen);
310 lua_setfield(L, -2, "wlen");
311 lua_pop(L, 1);
312 /* replace next */
313 lua_pushcfunction(L, luaAe_next);
314 lua_setglobal(L, "next");
315 /* replace pairs */
316 lua_pushcfunction(L, luaAe_next);
317 lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
318 lua_setglobal(L, "pairs");
319 /* replace ipairs */
320 lua_pushcfunction(L, luaA_ipairs_aux);
321 lua_pushcclosure(L, luaAe_ipairs, 1);
322 lua_setglobal(L, "ipairs");
323 /* replace type */
324 lua_pushcfunction(L, luaAe_type);
325 lua_setglobal(L, "type");
326 /* set selection */
327 lua_pushcfunction(L, luaA_selection_get);
328 lua_setglobal(L, "selection");
331 /** Look for an item: table, function, etc.
332 * \param L The Lua VM state.
333 * \param item The pointer item.
335 bool
336 luaA_hasitem(lua_State *L, const void *item)
338 lua_pushnil(L);
339 while(luaA_next(L, -2))
341 if(lua_topointer(L, -1) == item)
343 /* remove value and key */
344 lua_pop(L, 2);
345 return true;
347 if(lua_istable(L, -1))
348 if(luaA_hasitem(L, item))
350 /* remove key and value */
351 lua_pop(L, 2);
352 return true;
354 /* remove value */
355 lua_pop(L, 1);
357 return false;
360 /** Browse a table pushed on top of the index, and put all its table and
361 * sub-table into an array.
362 * \param L The Lua VM state.
363 * \param elems The elements array.
364 * \return False if we encounter an elements already in list.
366 static bool
367 luaA_isloop_check(lua_State *L, cptr_array_t *elems)
369 if(lua_istable(L, -1))
371 const void *object = lua_topointer(L, -1);
373 /* Check that the object table is not already in the list */
374 for(int i = 0; i < elems->len; i++)
375 if(elems->tab[i] == object)
376 return false;
378 /* push the table in the elements list */
379 cptr_array_append(elems, object);
381 /* look every object in the "table" */
382 lua_pushnil(L);
383 while(luaA_next(L, -2))
385 if(!luaA_isloop_check(L, elems))
387 /* remove key and value */
388 lua_pop(L, 2);
389 return false;
391 /* remove value, keep key for next iteration */
392 lua_pop(L, 1);
395 return true;
398 /** Check if a table is a loop. When using tables as direct acyclic digram,
399 * this is useful.
400 * \param L The Lua VM state.
401 * \param idx The index of the table in the stack
402 * \return True if the table loops.
404 bool
405 luaA_isloop(lua_State *L, int idx)
407 /* elems is an elements array that we will fill with all array we
408 * encounter while browsing the tables */
409 cptr_array_t elems;
411 cptr_array_init(&elems);
413 /* push table on top */
414 lua_pushvalue(L, idx);
416 bool ret = luaA_isloop_check(L, &elems);
418 /* remove pushed table */
419 lua_pop(L, 1);
421 cptr_array_wipe(&elems);
423 return !ret;
426 /** awesome global table.
427 * \param L The Lua VM state.
428 * \return The number of elements pushed on stack.
429 * \luastack
430 * \lfield conffile The configuration file which has been loaded.
431 * \lfield version The version of awesome.
432 * \lfield release The release name of awesome.
433 * \lfield startup True if we are still in startup, false otherwise.
434 * \lfield startup_errors Error message for errors that occured during startup.
435 * \lfield composite_manager_running True if a composite manager is running.
437 static int
438 luaA_awesome_index(lua_State *L)
440 if(luaA_usemetatable(L, 1, 2))
441 return 1;
443 const char *buf = luaL_checkstring(L, 2);
445 if(A_STREQ(buf, "conffile"))
447 lua_pushstring(L, conffile);
448 return 1;
451 if(A_STREQ(buf, "version"))
453 lua_pushliteral(L, AWESOME_VERSION);
454 return 1;
457 if(A_STREQ(buf, "release"))
459 lua_pushliteral(L, AWESOME_RELEASE);
460 return 1;
463 if(A_STREQ(buf, "startup"))
465 lua_pushboolean(L, globalconf.loop == NULL);
466 return 1;
469 if(A_STREQ(buf, "startup_errors") && globalconf.startup_errors.len != 0)
471 lua_pushstring(L, globalconf.startup_errors.s);
472 return 1;
475 if(A_STREQ(buf, "composite_manager_running"))
477 lua_pushboolean(L, composite_manager_running());
478 return 1;
481 return 0;
484 /** Add a global signal.
485 * \param L The Lua VM state.
486 * \return The number of elements pushed on stack.
487 * \luastack
488 * \lparam A string with the event name.
489 * \lparam The function to call.
491 static int
492 luaA_awesome_connect_signal(lua_State *L)
494 const char *name = luaL_checkstring(L, 1);
495 luaA_checkfunction(L, 2);
496 signal_connect(&global_signals, name, luaA_object_ref(L, 2));
497 return 0;
500 /** Remove a global signal.
501 * \param L The Lua VM state.
502 * \return The number of elements pushed on stack.
503 * \luastack
504 * \lparam A string with the event name.
505 * \lparam The function to call.
507 static int
508 luaA_awesome_disconnect_signal(lua_State *L)
510 const char *name = luaL_checkstring(L, 1);
511 luaA_checkfunction(L, 2);
512 const void *func = lua_topointer(L, 2);
513 signal_disconnect(&global_signals, name, func);
514 luaA_object_unref(L, (void *) func);
515 return 0;
518 /** Emit a global signal.
519 * \param L The Lua VM state.
520 * \return The number of elements pushed on stack.
521 * \luastack
522 * \lparam A string with the event name.
523 * \lparam The function to call.
525 static int
526 luaA_awesome_emit_signal(lua_State *L)
528 signal_object_emit(L, &global_signals, luaL_checkstring(L, 1), lua_gettop(L) - 1);
529 return 0;
532 static int
533 luaA_panic(lua_State *L)
535 warn("unprotected error in call to Lua API (%s)",
536 lua_tostring(L, -1));
537 buffer_t buf;
538 backtrace_get(&buf);
539 warn("dumping backtrace\n%s", buf.s);
540 warn("restarting awesome");
541 awesome_restart();
542 return 0;
545 static int
546 luaA_dofunction_on_error(lua_State *L)
548 /* duplicate string error */
549 lua_pushvalue(L, -1);
550 /* emit error signal */
551 signal_object_emit(L, &global_signals, "debug::error", 1);
553 if(!luaL_dostring(L, "return debug.traceback(\"error while running function\", 3)"))
555 /* Move traceback before error */
556 lua_insert(L, -2);
557 /* Insert sentence */
558 lua_pushliteral(L, "\nerror: ");
559 /* Move it before error */
560 lua_insert(L, -2);
561 lua_concat(L, 3);
563 return 1;
566 /** Initialize the Lua VM
567 * \param xdg An xdg handle to use to get XDG basedir.
569 void
570 luaA_init(xdgHandle* xdg)
572 lua_State *L;
573 static const struct luaL_Reg awesome_lib[] =
575 { "quit", luaA_quit },
576 { "exec", luaA_exec },
577 { "spawn", luaA_spawn },
578 { "restart", luaA_restart },
579 { "connect_signal", luaA_awesome_connect_signal },
580 { "disconnect_signal", luaA_awesome_disconnect_signal },
581 { "emit_signal", luaA_awesome_emit_signal },
582 { "systray", luaA_systray },
583 { "load_image", luaA_load_image },
584 { "register_xproperty", luaA_register_xproperty },
585 { "__index", luaA_awesome_index },
586 { NULL, NULL }
589 L = globalconf.L = luaL_newstate();
591 /* Set panic function */
592 lua_atpanic(L, luaA_panic);
594 /* Set error handling function */
595 lualib_dofunction_on_error = luaA_dofunction_on_error;
597 luaL_openlibs(L);
599 luaA_fixups(L);
601 luaA_object_setup(L);
603 /* Export awesome lib */
604 luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
606 /* Export root lib */
607 luaA_registerlib(L, "root", awesome_root_lib);
608 lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
610 #ifdef WITH_DBUS
611 /* Export D-Bus lib */
612 luaA_registerlib(L, "dbus", awesome_dbus_lib);
613 lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
614 #endif
616 /* Export keygrabber lib */
617 luaA_registerlib(L, "keygrabber", awesome_keygrabber_lib);
618 lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
620 /* Export mousegrabber lib */
621 luaA_registerlib(L, "mousegrabber", awesome_mousegrabber_lib);
622 lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
624 /* Export screen */
625 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
627 /* Export mouse */
628 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
630 /* Export button */
631 button_class_setup(L);
633 /* Export tag */
634 tag_class_setup(L);
636 /* Export window */
637 window_class_setup(L);
639 /* Export drawable */
640 drawable_class_setup(L);
642 /* Export drawin */
643 drawin_class_setup(L);
645 /* Export client */
646 client_class_setup(L);
648 /* Export keys */
649 key_class_setup(L);
651 /* Export timer */
652 timer_class_setup(L);
654 /* add Lua search paths */
655 lua_getglobal(L, "package");
656 if (LUA_TTABLE != lua_type(L, 1))
658 warn("package is not a table");
659 return;
661 lua_getfield(L, 1, "path");
662 if (LUA_TSTRING != lua_type(L, 2))
664 warn("package.path is not a string");
665 lua_pop(L, 1);
666 return;
669 /* add XDG_CONFIG_DIR as include path */
670 const char * const *xdgconfigdirs = xdgSearchableConfigDirectories(xdg);
671 for(; *xdgconfigdirs; xdgconfigdirs++)
673 size_t len = a_strlen(*xdgconfigdirs);
674 lua_pushliteral(L, ";");
675 lua_pushlstring(L, *xdgconfigdirs, len);
676 lua_pushliteral(L, "/awesome/?.lua");
677 lua_concat(L, 3);
679 lua_pushliteral(L, ";");
680 lua_pushlstring(L, *xdgconfigdirs, len);
681 lua_pushliteral(L, "/awesome/?/init.lua");
682 lua_concat(L, 3);
684 lua_concat(L, 3); /* concatenate with package.path */
687 /* add Lua lib path (/usr/share/awesome/lib by default) */
688 lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?.lua");
689 lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?/init.lua");
690 lua_concat(L, 3); /* concatenate with package.path */
691 lua_setfield(L, 1, "path"); /* package.path = "concatenated string" */
693 lua_getfield(L, 1, "loaded");
695 lua_pop(L, 2); /* pop "package" and "package.loaded" */
697 signal_add(&global_signals, "debug::error");
698 signal_add(&global_signals, "debug::deprecation");
699 signal_add(&global_signals, "debug::index::miss");
700 signal_add(&global_signals, "debug::newindex::miss");
701 signal_add(&global_signals, "systray::update");
702 signal_add(&global_signals, "wallpaper_changed");
703 signal_add(&global_signals, "refresh");
704 signal_add(&global_signals, "exit");
707 static void
708 luaA_startup_error(const char *err)
710 if (globalconf.startup_errors.len > 0)
711 buffer_addsl(&globalconf.startup_errors, "\n\n");
712 buffer_adds(&globalconf.startup_errors, err);
715 static bool
716 luaA_loadrc(const char *confpath, bool run)
718 if(!luaL_loadfile(globalconf.L, confpath))
720 if(run)
722 /* Set the conffile right now so it can be used inside the
723 * configuration file. */
724 conffile = a_strdup(confpath);
725 /* Move error handling function before function */
726 lua_pushcfunction(globalconf.L, luaA_dofunction_on_error);
727 lua_insert(globalconf.L, -2);
728 if(lua_pcall(globalconf.L, 0, LUA_MULTRET, -2))
730 const char *err = lua_tostring(globalconf.L, -1);
731 luaA_startup_error(err);
732 fprintf(stderr, "%s\n", err);
733 /* An error happened, so reset this. */
734 conffile = NULL;
736 else
737 return true;
739 else
741 lua_pop(globalconf.L, 1);
742 return true;
745 else
747 const char *err = lua_tostring(globalconf.L, -1);
748 luaA_startup_error(err);
749 fprintf(stderr, "%s\n", err);
752 return false;
755 /** Load a configuration file.
756 * \param xdg An xdg handle to use to get XDG basedir.
757 * \param confpatharg The configuration file to load.
758 * \param run Run the configuration file.
760 bool
761 luaA_parserc(xdgHandle* xdg, const char *confpatharg, bool run)
763 char *confpath = NULL;
764 bool ret = false;
766 /* try to load, return if it's ok */
767 if(confpatharg)
769 if(luaA_loadrc(confpatharg, run))
771 ret = true;
772 goto bailout;
774 else if(!run)
775 goto bailout;
778 confpath = xdgConfigFind("awesome/rc.lua", xdg);
780 char *tmp = confpath;
782 /* confpath is "string1\0string2\0string3\0\0" */
783 while(*tmp)
785 if(luaA_loadrc(tmp, run))
787 ret = true;
788 goto bailout;
790 else if(!run)
791 goto bailout;
792 tmp += a_strlen(tmp) + 1;
795 bailout:
797 p_delete(&confpath);
799 return ret;
803 luaA_class_index_miss_property(lua_State *L, lua_object_t *obj)
805 signal_object_emit(L, &global_signals, "debug::index::miss", 2);
806 return 0;
810 luaA_class_newindex_miss_property(lua_State *L, lua_object_t *obj)
812 signal_object_emit(L, &global_signals, "debug::newindex::miss", 3);
813 return 0;
816 void
817 luaA_emit_refresh()
819 signal_object_emit(globalconf.L, &global_signals, "refresh", 0);
822 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80