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.
30 #include <basedir_fs.h>
34 #include "awesome-version-internal.h"
42 #include "selection.h"
44 #include "common/xcursor.h"
45 #include "common/buffer.h"
48 extern const struct luaL_reg awesome_dbus_lib
[];
50 extern const struct luaL_reg awesome_hooks_lib
[];
51 extern const struct luaL_reg awesome_keygrabber_lib
[];
52 extern const struct luaL_reg awesome_mousegrabber_lib
[];
53 extern const struct luaL_reg awesome_root_lib
[];
54 extern const struct luaL_reg awesome_mouse_methods
[];
55 extern const struct luaL_reg awesome_mouse_meta
[];
56 extern const struct luaL_reg awesome_screen_methods
[];
57 extern const struct luaL_reg awesome_screen_meta
[];
60 * \param L The Lua VM state.
61 * \return The number of elements pushed on stack.
64 luaA_quit(lua_State
*L
__attribute__ ((unused
)))
66 ev_unloop(globalconf
.loop
, 1);
70 /** Execute another application, probably a window manager, to replace
72 * \param L The Lua VM state.
73 * \return The number of elements pushed on stack.
75 * \lparam The command line to execute.
78 luaA_exec(lua_State
*L
)
80 const char *cmd
= luaL_checkstring(L
, 1);
91 luaA_restart(lua_State
*L
__attribute__ ((unused
)))
97 /** UTF-8 aware string length computing.
98 * \param L The Lua VM state.
99 * \return The number of elements pushed on stack.
102 luaA_mbstrlen(lua_State
*L
)
104 const char *cmd
= luaL_checkstring(L
, 1);
105 lua_pushnumber(L
, (ssize_t
) mbstowcs(NULL
, NONULL(cmd
), 0));
109 /** Overload standard Lua next function to use __next key on metatable.
110 * \param L The Lua VM state.
111 * \return The number of elements pushed on stack.
114 luaAe_next(lua_State
*L
)
116 if(luaL_getmetafield(L
, 1, "__next"))
119 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
120 return lua_gettop(L
);
123 luaL_checktype(L
, 1, LUA_TTABLE
);
131 /** Overload lua_next() function by using __next metatable field
132 * to get next elements.
133 * \param L The Lua VM stack.
134 * \param idx The index number of elements in stack.
135 * \return 1 if more elements to come, 0 otherwise.
138 luaA_next(lua_State
*L
, int idx
)
140 if(luaL_getmetafield(L
, idx
, "__next"))
142 /* if idx is relative, reduce it since we got __next */
144 /* copy table and then move key */
145 lua_pushvalue(L
, idx
);
146 lua_pushvalue(L
, -3);
148 lua_pcall(L
, 2, 2, 0);
149 /* next returned nil, it's the end */
158 else if(lua_istable(L
, idx
))
159 return lua_next(L
, idx
);
165 /** Generic pairs function.
166 * \param L The Lua VM state.
167 * \return The number of elements pushed on stack.
170 luaA_generic_pairs(lua_State
*L
)
172 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
173 lua_pushvalue(L
, 1); /* state, */
174 lua_pushnil(L
); /* and initial value */
178 /** Overload standard pairs function to use __pairs field of metatables.
179 * \param L The Lua VM state.
180 * \return The number of elements pushed on stack.
183 luaAe_pairs(lua_State
*L
)
185 if(luaL_getmetafield(L
, 1, "__pairs"))
188 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
189 return lua_gettop(L
);
192 luaL_checktype(L
, 1, LUA_TTABLE
);
193 return luaA_generic_pairs(L
);
197 luaA_ipairs_aux(lua_State
*L
)
199 int i
= luaL_checkint(L
, 2) + 1;
200 luaL_checktype(L
, 1, LUA_TTABLE
);
201 lua_pushinteger(L
, i
);
202 lua_rawgeti(L
, 1, i
);
203 return (lua_isnil(L
, -1)) ? 0 : 2;
206 /** Overload standard ipairs function to use __ipairs field of metatables.
207 * \param L The Lua VM state.
208 * \return The number of elements pushed on stack.
211 luaAe_ipairs(lua_State
*L
)
213 if(luaL_getmetafield(L
, 1, "__ipairs"))
216 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
217 return lua_gettop(L
);
220 luaL_checktype(L
, 1, LUA_TTABLE
);
221 lua_pushvalue(L
, lua_upvalueindex(1));
223 lua_pushinteger(L
, 0); /* and initial value */
227 /** Enhanced type() function which recognize awesome objects.
228 * \param L The Lua VM state.
229 * \return The number of arguments pushed on the stack.
232 luaAe_type(lua_State
*L
)
235 lua_pushstring(L
, luaA_typename(L
, 1));
239 /** Replace various standards Lua functions with our own.
240 * \param L The Lua VM state.
243 luaA_fixups(lua_State
*L
)
245 /* export string.wlen */
246 lua_getglobal(L
, "string");
247 lua_pushcfunction(L
, luaA_mbstrlen
);
248 lua_setfield(L
, -2, "wlen");
251 lua_pushliteral(L
, "next");
252 lua_pushcfunction(L
, luaAe_next
);
253 lua_settable(L
, LUA_GLOBALSINDEX
);
255 lua_pushliteral(L
, "pairs");
256 lua_pushcfunction(L
, luaAe_next
);
257 lua_pushcclosure(L
, luaAe_pairs
, 1); /* pairs get next as upvalue */
258 lua_settable(L
, LUA_GLOBALSINDEX
);
260 lua_pushliteral(L
, "ipairs");
261 lua_pushcfunction(L
, luaA_ipairs_aux
);
262 lua_pushcclosure(L
, luaAe_ipairs
, 1);
263 lua_settable(L
, LUA_GLOBALSINDEX
);
265 lua_pushliteral(L
, "type");
266 lua_pushcfunction(L
, luaAe_type
);
267 lua_settable(L
, LUA_GLOBALSINDEX
);
269 lua_pushliteral(L
, "selection");
270 lua_pushcfunction(L
, luaA_selection_get
);
271 lua_settable(L
, LUA_GLOBALSINDEX
);
274 /** __next function for wtable objects.
275 * \param L The Lua VM state.
276 * \return The number of elements pushed on stack.
279 luaA_wtable_next(lua_State
*L
)
281 /* upvalue 1 is content table */
282 if(lua_next(L
, lua_upvalueindex(1)))
288 /** __ipairs function for wtable objects.
289 * \param L The Lua VM state.
290 * \return The number of elements pushed on stack.
293 luaA_wtable_ipairs(lua_State
*L
)
295 /* push ipairs_aux */
296 lua_pushvalue(L
, lua_upvalueindex(2));
297 /* push content table */
298 lua_pushvalue(L
, lua_upvalueindex(1));
299 lua_pushinteger(L
, 0); /* and initial value */
303 /** Index function of wtable objects.
304 * \param L The Lua VM state.
305 * \return The number of elements pushed on stack.
308 luaA_wtable_index(lua_State
*L
)
314 /* check for size, waiting lua 5.2 and __len on tables */
315 if((buf
= lua_tolstring(L
, -1, &len
)))
316 if(a_tokenize(buf
, len
) == A_TK_LEN
)
318 lua_pushnumber(L
, lua_objlen(L
, lua_upvalueindex(1)));
323 /* upvalue 1 is content table */
324 lua_rawget(L
, lua_upvalueindex(1));
328 /** Newndex function of wtable objects.
329 * \param L The Lua VM state.
330 * \return The number of elements pushed on stack.
333 luaA_wtable_newindex(lua_State
*L
)
335 bool invalid
= false;
337 /* push key on top */
339 /* get current key value in content table */
340 lua_rawget(L
, lua_upvalueindex(1));
341 /* if value is a widget, notify change */
342 if(lua_istable(L
, -1) || luaA_toudata(L
, -1, &widget_class
))
345 lua_pop(L
, 1); /* remove value */
347 /* if new value is a widget or a table */
348 if(lua_istable(L
, 3))
350 luaA_table2wtable(L
);
353 else if(!invalid
&& luaA_toudata(L
, 3, &widget_class
))
356 /* upvalue 1 is content table */
357 lua_rawset(L
, lua_upvalueindex(1));
360 luaA_wibox_invalidate_byitem(L
, lua_topointer(L
, 1));
365 /** Convert the top element of the stack to a proxied wtable.
366 * \param L The Lua VM state.
369 luaA_table2wtable(lua_State
*L
)
371 if(!lua_istable(L
, -1))
374 lua_newtable(L
); /* create *real* content table */
375 lua_createtable(L
, 0, 5); /* metatable */
376 lua_pushvalue(L
, -2); /* copy content table */
377 lua_pushcfunction(L
, luaA_ipairs_aux
); /* push ipairs aux */
378 lua_pushcclosure(L
, luaA_wtable_ipairs
, 2);
379 lua_pushvalue(L
, -3); /* copy content table */
380 lua_pushcclosure(L
, luaA_wtable_next
, 1); /* __next has the content table as upvalue */
381 lua_pushvalue(L
, -4); /* copy content table */
382 lua_pushcclosure(L
, luaA_wtable_index
, 1); /* __index has the content table as upvalue */
383 lua_pushvalue(L
, -5); /* copy content table */
384 lua_pushcclosure(L
, luaA_wtable_newindex
, 1); /* __newindex has the content table as upvalue */
385 /* set metatable field with just pushed closure */
386 lua_setfield(L
, -5, "__newindex");
387 lua_setfield(L
, -4, "__index");
388 lua_setfield(L
, -3, "__next");
389 lua_setfield(L
, -2, "__ipairs");
390 /* set metatable impossible to touch */
391 lua_pushliteral(L
, "wtable");
392 lua_setfield(L
, -2, "__metatable");
393 /* set new metatable on original table */
394 lua_setmetatable(L
, -3);
398 /* go through original table */
399 while(lua_next(L
, -3))
401 /* if convert value to wtable */
402 luaA_table2wtable(L
);
404 lua_pushvalue(L
, -2);
405 /* move key before value */
407 /* set same value in content table */
410 lua_pushvalue(L
, -1);
411 /* push the new value :-> */
413 /* set orig[k] = nil */
416 /* remove content table */
420 /** Look for an item: table, function, etc.
421 * \param L The Lua VM state.
422 * \param item The pointer item.
425 luaA_hasitem(lua_State
*L
, const void *item
)
428 while(luaA_next(L
, -2))
430 if(lua_topointer(L
, -1) == item
)
432 /* remove value and key */
436 if(lua_istable(L
, -1))
437 if(luaA_hasitem(L
, item
))
439 /* remove key and value */
449 /** Browse a table pushed on top of the index, and put all its table and
450 * sub-table into an array.
451 * \param L The Lua VM state.
452 * \param elems The elements array.
453 * \return False if we encounter an elements already in list.
456 luaA_isloop_check(lua_State
*L
, cptr_array_t
*elems
)
458 if(lua_istable(L
, -1))
460 const void *object
= lua_topointer(L
, -1);
462 /* Check that the object table is not already in the list */
463 for(int i
= 0; i
< elems
->len
; i
++)
464 if(elems
->tab
[i
] == object
)
467 /* push the table in the elements list */
468 cptr_array_append(elems
, object
);
470 /* look every object in the "table" */
472 while(luaA_next(L
, -2))
474 if(!luaA_isloop_check(L
, elems
))
476 /* remove key and value */
480 /* remove value, keep key for next iteration */
487 /** Check if a table is a loop. When using tables as direct acyclic digram,
489 * \param L The Lua VM state.
490 * \param idx The index of the table in the stack
491 * \return True if the table loops.
494 luaA_isloop(lua_State
*L
, int idx
)
496 /* elems is an elements array that we will fill with all array we
497 * encounter while browsing the tables */
500 cptr_array_init(&elems
);
502 /* push table on top */
503 lua_pushvalue(L
, idx
);
505 bool ret
= luaA_isloop_check(L
, &elems
);
507 /* remove pushed table */
510 cptr_array_wipe(&elems
);
515 /** awesome global table.
516 * \param L The Lua VM state.
517 * \return The number of elements pushed on stack.
519 * \lfield font The default font.
520 * \lfield font_height The default font height.
521 * \lfield conffile The configuration file which has been loaded.
524 luaA_awesome_index(lua_State
*L
)
526 if(luaA_usemetatable(L
, 1, 2))
530 const char *buf
= luaL_checklstring(L
, 2, &len
);
532 switch(a_tokenize(buf
, len
))
536 char *font
= pango_font_description_to_string(globalconf
.font
->desc
);
537 lua_pushstring(L
, font
);
541 case A_TK_FONT_HEIGHT
:
542 lua_pushnumber(L
, globalconf
.font
->height
);
545 lua_pushstring(L
, globalconf
.conffile
);
548 luaA_pushxcolor(L
, globalconf
.colors
.fg
);
551 luaA_pushxcolor(L
, globalconf
.colors
.bg
);
554 lua_pushliteral(L
, AWESOME_VERSION
);
557 lua_pushliteral(L
, AWESOME_RELEASE
);
566 /** Newindex function for the awesome global table.
567 * \param L The Lua VM state.
568 * \return The number of elements pushed on stack.
571 luaA_awesome_newindex(lua_State
*L
)
573 if(luaA_usemetatable(L
, 1, 2))
577 const char *buf
= luaL_checklstring(L
, 2, &len
);
579 switch(a_tokenize(buf
, len
))
583 const char *newfont
= luaL_checkstring(L
, 3);
584 draw_font_delete(&globalconf
.font
);
585 globalconf
.font
= draw_font_new(newfont
);
586 /* refresh all wiboxes */
587 foreach(wibox
, globalconf
.wiboxes
)
588 (*wibox
)->need_update
= true;
589 foreach(c
, globalconf
.clients
)
591 (*c
)->titlebar
->need_update
= true;
595 if((buf
= luaL_checklstring(L
, 3, &len
)))
596 xcolor_init_reply(xcolor_init_unchecked(&globalconf
.colors
.fg
, buf
, len
));
599 if((buf
= luaL_checklstring(L
, 3, &len
)))
600 xcolor_init_reply(xcolor_init_unchecked(&globalconf
.colors
.bg
, buf
, len
));
609 /** Add a global signal.
610 * \param L The Lua VM state.
611 * \return The number of elements pushed on stack.
613 * \lparam A string with the event name.
614 * \lparam The function to call.
617 luaA_awesome_add_signal(lua_State
*L
)
619 const char *name
= luaL_checkstring(L
, 1);
620 luaA_checkfunction(L
, 2);
621 signal_add(&global_signals
, name
, luaA_object_ref(L
, 2));
625 /** Remove a global signal.
626 * \param L The Lua VM state.
627 * \return The number of elements pushed on stack.
629 * \lparam A string with the event name.
630 * \lparam The function to call.
633 luaA_awesome_remove_signal(lua_State
*L
)
635 const char *name
= luaL_checkstring(L
, 1);
636 luaA_checkfunction(L
, 2);
637 const void *func
= lua_topointer(L
, 2);
638 signal_remove(&global_signals
, name
, func
);
639 luaA_object_unref(L
, (void *) func
);
643 /** Emit a global signal.
644 * \param L The Lua VM state.
645 * \return The number of elements pushed on stack.
647 * \lparam A string with the event name.
648 * \lparam The function to call.
651 luaA_awesome_emit_signal(lua_State
*L
)
653 signal_object_emit(L
, &global_signals
, luaL_checkstring(L
, 1), lua_gettop(L
) - 1);
657 /** Initialize the Lua VM
658 * \param xdg An xdg handle to use to get XDG basedir.
661 luaA_init(xdgHandle
* xdg
)
664 static const struct luaL_reg awesome_lib
[] =
666 { "quit", luaA_quit
},
667 { "exec", luaA_exec
},
668 { "spawn", luaA_spawn
},
669 { "restart", luaA_restart
},
670 { "add_signal", luaA_awesome_add_signal
},
671 { "remove_signal", luaA_awesome_remove_signal
},
672 { "emit_signal", luaA_awesome_emit_signal
},
673 { "__index", luaA_awesome_index
},
674 { "__newindex", luaA_awesome_newindex
},
678 L
= globalconf
.L
= luaL_newstate();
684 luaA_object_setup(L
);
686 /* Export awesome lib */
687 luaA_openlib(L
, "awesome", awesome_lib
, awesome_lib
);
689 /* Export root lib */
690 luaL_register(L
, "root", awesome_root_lib
);
691 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
693 /* Export hooks lib */
694 luaL_register(L
, "hooks", awesome_hooks_lib
);
695 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
698 /* Export D-Bus lib */
699 luaL_register(L
, "dbus", awesome_dbus_lib
);
700 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
703 /* Export keygrabber lib */
704 luaL_register(L
, "keygrabber", awesome_keygrabber_lib
);
705 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
707 /* Export mousegrabber lib */
708 luaL_register(L
, "mousegrabber", awesome_mousegrabber_lib
);
709 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
712 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
715 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
718 button_class_setup(L
);
721 image_class_setup(L
);
727 wibox_class_setup(L
);
730 widget_class_setup(L
);
733 client_class_setup(L
);
739 timer_class_setup(L
);
742 globalconf
.hooks
.manage
= LUA_REFNIL
;
743 globalconf
.hooks
.unmanage
= LUA_REFNIL
;
744 globalconf
.hooks
.focus
= LUA_REFNIL
;
745 globalconf
.hooks
.unfocus
= LUA_REFNIL
;
746 globalconf
.hooks
.mouse_enter
= LUA_REFNIL
;
747 globalconf
.hooks
.mouse_leave
= LUA_REFNIL
;
748 globalconf
.hooks
.clients
= LUA_REFNIL
;
749 globalconf
.hooks
.tags
= LUA_REFNIL
;
750 globalconf
.hooks
.tagged
= LUA_REFNIL
;
751 globalconf
.hooks
.property
= LUA_REFNIL
;
752 globalconf
.hooks
.timer
= LUA_REFNIL
;
753 globalconf
.hooks
.exit
= LUA_REFNIL
;
755 /* add Lua lib path (/usr/share/awesome/lib by default) */
756 lua_getglobal(L
, "package");
757 if (LUA_TTABLE
!= lua_type(L
, 1))
759 warn("package is not a table");
762 lua_getfield(L
, 1, "path");
763 if (LUA_TSTRING
!= lua_type(L
, 2))
765 warn("package.path is not a string");
769 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?.lua");
770 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?/init.lua");
771 lua_concat(L
, 3); /* concatenate with package.path */
773 /* add XDG_CONFIG_DIR as include path */
774 const char * const *xdgconfigdirs
= xdgSearchableConfigDirectories(xdg
);
775 for(; *xdgconfigdirs
; xdgconfigdirs
++)
777 size_t len
= a_strlen(*xdgconfigdirs
);
778 lua_pushliteral(L
, ";");
779 lua_pushlstring(L
, *xdgconfigdirs
, len
);
780 lua_pushliteral(L
, "/awesome/?.lua");
783 lua_pushliteral(L
, ";");
784 lua_pushlstring(L
, *xdgconfigdirs
, len
);
785 lua_pushliteral(L
, "/awesome/?/init.lua");
788 lua_concat(L
, 3); /* concatenate with package.path */
790 lua_setfield(L
, 1, "path"); /* package.path = "concatenated string" */
794 luaA_loadrc(const char *confpath
, bool run
)
796 if(!luaL_loadfile(globalconf
.L
, confpath
))
800 if(lua_pcall(globalconf
.L
, 0, LUA_MULTRET
, 0))
801 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
804 globalconf
.conffile
= a_strdup(confpath
);
809 lua_pop(globalconf
.L
, 1);
813 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
818 /** Load a configuration file.
819 * \param xdg An xdg handle to use to get XDG basedir.
820 * \param confpatharg The configuration file to load.
821 * \param run Run the configuration file.
824 luaA_parserc(xdgHandle
* xdg
, const char *confpatharg
, bool run
)
826 char *confpath
= NULL
;
829 /* try to load, return if it's ok */
832 if(luaA_loadrc(confpatharg
, run
))
841 confpath
= xdgConfigFind("awesome/rc.lua", xdg
);
843 char *tmp
= confpath
;
845 /* confpath is "string1\0string2\0string3\0\0" */
848 if(luaA_loadrc(tmp
, run
))
855 tmp
+= a_strlen(tmp
) + 1;
866 luaA_on_timer(EV_P_ ev_timer
*w
, int revents
)
868 if(globalconf
.hooks
.timer
!= LUA_REFNIL
)
869 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.timer
, 0, 0);
873 luaA_class_index_miss_property(lua_State
*L
, lua_object_t
*obj
)
875 signal_object_emit(L
, &global_signals
, "debug::index::miss", 2);
880 luaA_class_newindex_miss_property(lua_State
*L
, lua_object_t
*obj
)
882 signal_object_emit(L
, &global_signals
, "debug::newindex::miss", 3);
886 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80