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.
28 #include <basedir_fs.h>
30 #include <xcb/xcb_atom.h>
34 #include "objects/timer.h"
35 #include "awesome-version-internal.h"
39 #include "objects/tag.h"
40 #include "objects/client.h"
41 #include "objects/drawin.h"
42 #include "objects/drawable.h"
46 #include "selection.h"
48 #include "common/xcursor.h"
49 #include "common/buffer.h"
50 #include "common/backtrace.h"
53 extern const struct luaL_Reg awesome_dbus_lib
[];
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.
70 composite_manager_running(void)
72 xcb_intern_atom_reply_t
*atom_r
;
73 xcb_get_selection_owner_reply_t
*selection_r
;
77 if(!(atom_name
= xcb_atom_name_by_screen("_NET_WM_CM", globalconf
.default_screen
)))
79 warn("error getting composite manager atom");
83 atom_r
= xcb_intern_atom_reply(globalconf
.connection
,
84 xcb_intern_atom_unchecked(globalconf
.connection
, false,
85 a_strlen(atom_name
), atom_name
),
91 selection_r
= xcb_get_selection_owner_reply(globalconf
.connection
,
92 xcb_get_selection_owner_unchecked(globalconf
.connection
,
97 result
= selection_r
!= NULL
&& selection_r
->owner
!= XCB_NONE
;
98 p_delete(&selection_r
);
104 * \param L The Lua VM state.
105 * \return The number of elements pushed on stack.
108 luaA_quit(lua_State
*L
)
110 g_main_loop_quit(globalconf
.loop
);
114 /** Execute another application, probably a window manager, to replace
116 * \param L The Lua VM state.
117 * \return The number of elements pushed on stack.
119 * \lparam The command line to execute.
122 luaA_exec(lua_State
*L
)
124 const char *cmd
= luaL_checkstring(L
, 1);
126 awesome_atexit(false);
135 luaA_restart(lua_State
*L
)
141 /** Load an image from a given path.
142 * \param L The Lua VM state.
143 * \return The number of elements pushed on stack.
145 * \lparam The command line to execute.
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
);
154 /* lua has to make sure to free the ref or we have a leak */
155 lua_pushlightuserdata(L
, surface
);
159 /** UTF-8 aware string length computing.
160 * \param L The Lua VM state.
161 * \return The number of elements pushed on stack.
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));
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.
176 luaAe_next(lua_State
*L
)
178 if(luaL_getmetafield(L
, 1, "__next"))
181 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
182 return lua_gettop(L
);
185 luaL_checktype(L
, 1, LUA_TTABLE
);
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.
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 */
206 /* copy table and then move key */
207 lua_pushvalue(L
, idx
);
208 lua_pushvalue(L
, -3);
210 lua_pcall(L
, 2, 2, 0);
211 /* next returned nil, it's the end */
220 else if(lua_istable(L
, idx
))
221 return lua_next(L
, idx
);
227 /** Generic pairs function.
228 * \param L The Lua VM state.
229 * \return The number of elements pushed on stack.
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 */
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.
245 luaAe_pairs(lua_State
*L
)
247 if(luaL_getmetafield(L
, 1, "__pairs"))
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
);
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.
273 luaAe_ipairs(lua_State
*L
)
275 if(luaL_getmetafield(L
, 1, "__ipairs"))
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));
285 lua_pushinteger(L
, 0); /* and initial value */
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.
294 luaAe_type(lua_State
*L
)
297 lua_pushstring(L
, luaA_typename(L
, 1));
301 /** Replace various standards Lua functions with our own.
302 * \param L The Lua VM state.
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");
313 lua_pushcfunction(L
, luaAe_next
);
314 lua_setglobal(L
, "next");
316 lua_pushcfunction(L
, luaAe_next
);
317 lua_pushcclosure(L
, luaAe_pairs
, 1); /* pairs get next as upvalue */
318 lua_setglobal(L
, "pairs");
320 lua_pushcfunction(L
, luaA_ipairs_aux
);
321 lua_pushcclosure(L
, luaAe_ipairs
, 1);
322 lua_setglobal(L
, "ipairs");
324 lua_pushcfunction(L
, luaAe_type
);
325 lua_setglobal(L
, "type");
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.
336 luaA_hasitem(lua_State
*L
, const void *item
)
339 while(luaA_next(L
, -2))
341 if(lua_topointer(L
, -1) == item
)
343 /* remove value and key */
347 if(lua_istable(L
, -1))
348 if(luaA_hasitem(L
, item
))
350 /* remove key and value */
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.
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
)
378 /* push the table in the elements list */
379 cptr_array_append(elems
, object
);
381 /* look every object in the "table" */
383 while(luaA_next(L
, -2))
385 if(!luaA_isloop_check(L
, elems
))
387 /* remove key and value */
391 /* remove value, keep key for next iteration */
398 /** Check if a table is a loop. When using tables as direct acyclic digram,
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.
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 */
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 */
421 cptr_array_wipe(&elems
);
426 /** awesome global table.
427 * \param L The Lua VM state.
428 * \return The number of elements pushed on stack.
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.
438 luaA_awesome_index(lua_State
*L
)
440 if(luaA_usemetatable(L
, 1, 2))
443 const char *buf
= luaL_checkstring(L
, 2);
445 if(A_STREQ(buf
, "conffile"))
447 lua_pushstring(L
, conffile
);
451 if(A_STREQ(buf
, "version"))
453 lua_pushliteral(L
, AWESOME_VERSION
);
457 if(A_STREQ(buf
, "release"))
459 lua_pushliteral(L
, AWESOME_RELEASE
);
463 if(A_STREQ(buf
, "startup"))
465 lua_pushboolean(L
, globalconf
.loop
== NULL
);
469 if(A_STREQ(buf
, "startup_errors") && globalconf
.startup_errors
.len
!= 0)
471 lua_pushstring(L
, globalconf
.startup_errors
.s
);
475 if(A_STREQ(buf
, "composite_manager_running"))
477 lua_pushboolean(L
, composite_manager_running());
484 /** Add a global signal.
485 * \param L The Lua VM state.
486 * \return The number of elements pushed on stack.
488 * \lparam A string with the event name.
489 * \lparam The function to call.
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));
500 /** Remove a global signal.
501 * \param L The Lua VM state.
502 * \return The number of elements pushed on stack.
504 * \lparam A string with the event name.
505 * \lparam The function to call.
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
);
518 /** Emit a global signal.
519 * \param L The Lua VM state.
520 * \return The number of elements pushed on stack.
522 * \lparam A string with the event name.
523 * \lparam The function to call.
526 luaA_awesome_emit_signal(lua_State
*L
)
528 signal_object_emit(L
, &global_signals
, luaL_checkstring(L
, 1), lua_gettop(L
) - 1);
533 luaA_panic(lua_State
*L
)
535 warn("unprotected error in call to Lua API (%s)",
536 lua_tostring(L
, -1));
539 warn("dumping backtrace\n%s", buf
.s
);
540 warn("restarting awesome");
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 */
557 /* Insert sentence */
558 lua_pushliteral(L
, "\nerror: ");
559 /* Move it before error */
566 /** Initialize the Lua VM
567 * \param xdg An xdg handle to use to get XDG basedir.
570 luaA_init(xdgHandle
* xdg
)
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
},
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
;
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 */
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 */
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 */
625 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
628 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
631 button_class_setup(L
);
637 window_class_setup(L
);
639 /* Export drawable */
640 drawable_class_setup(L
);
643 drawin_class_setup(L
);
646 client_class_setup(L
);
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");
661 lua_getfield(L
, 1, "path");
662 if (LUA_TSTRING
!= lua_type(L
, 2))
664 warn("package.path is not a string");
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");
679 lua_pushliteral(L
, ";");
680 lua_pushlstring(L
, *xdgconfigdirs
, len
);
681 lua_pushliteral(L
, "/awesome/?/init.lua");
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");
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
);
716 luaA_loadrc(const char *confpath
, bool run
)
718 if(!luaL_loadfile(globalconf
.L
, confpath
))
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. */
741 lua_pop(globalconf
.L
, 1);
747 const char *err
= lua_tostring(globalconf
.L
, -1);
748 luaA_startup_error(err
);
749 fprintf(stderr
, "%s\n", err
);
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.
761 luaA_parserc(xdgHandle
* xdg
, const char *confpatharg
, bool run
)
763 char *confpath
= NULL
;
766 /* try to load, return if it's ok */
769 if(luaA_loadrc(confpatharg
, run
))
778 confpath
= xdgConfigFind("awesome/rc.lua", xdg
);
780 char *tmp
= confpath
;
782 /* confpath is "string1\0string2\0string3\0\0" */
785 if(luaA_loadrc(tmp
, run
))
792 tmp
+= a_strlen(tmp
) + 1;
803 luaA_class_index_miss_property(lua_State
*L
, lua_object_t
*obj
)
805 signal_object_emit(L
, &global_signals
, "debug::index::miss", 2);
810 luaA_class_newindex_miss_property(lua_State
*L
, lua_object_t
*obj
)
812 signal_object_emit(L
, &global_signals
, "debug::newindex::miss", 3);
819 signal_object_emit(globalconf
.L
, &global_signals
, "refresh", 0);
822 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80