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>
33 #include "awesome-version-internal.h"
41 #include "selection.h"
43 #include "common/xcursor.h"
44 #include "common/buffer.h"
47 extern const struct luaL_reg awesome_dbus_lib
[];
49 extern const struct luaL_reg awesome_hooks_lib
[];
50 extern const struct luaL_reg awesome_keygrabber_lib
[];
51 extern const struct luaL_reg awesome_mousegrabber_lib
[];
52 extern const struct luaL_reg awesome_root_lib
[];
53 extern const struct luaL_reg awesome_button_methods
[];
54 extern const struct luaL_reg awesome_button_meta
[];
55 extern const struct luaL_reg awesome_image_methods
[];
56 extern const struct luaL_reg awesome_image_meta
[];
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
[];
61 extern const struct luaL_reg awesome_client_methods
[];
62 extern const struct luaL_reg awesome_client_meta
[];
63 extern const struct luaL_reg awesome_tag_methods
[];
64 extern const struct luaL_reg awesome_tag_meta
[];
65 extern const struct luaL_reg awesome_widget_methods
[];
66 extern const struct luaL_reg awesome_widget_meta
[];
67 extern const struct luaL_reg awesome_wibox_methods
[];
68 extern const struct luaL_reg awesome_wibox_meta
[];
69 extern const struct luaL_reg awesome_key_methods
[];
70 extern const struct luaL_reg awesome_key_meta
[];
73 * \param L The Lua VM state.
74 * \return The number of elements pushed on stack.
77 luaA_quit(lua_State
*L
__attribute__ ((unused
)))
79 ev_unloop(globalconf
.loop
, 1);
83 /** Execute another application, probably a window manager, to replace
85 * \param L The Lua VM state.
86 * \return The number of elements pushed on stack.
88 * \lparam The command line to execute.
91 luaA_exec(lua_State
*L
)
93 const char *cmd
= luaL_checkstring(L
, 1);
104 luaA_restart(lua_State
*L
__attribute__ ((unused
)))
110 /** UTF-8 aware string length computing.
111 * \param L The Lua VM state.
112 * \return The number of elements pushed on stack.
115 luaA_mbstrlen(lua_State
*L
)
117 const char *cmd
= luaL_checkstring(L
, 1);
118 lua_pushnumber(L
, (ssize_t
) mbstowcs(NULL
, NONULL(cmd
), 0));
122 /** Overload standard Lua next function to use __next key on metatable.
123 * \param L The Lua VM state.
124 * \return The number of elements pushed on stack.
127 luaAe_next(lua_State
*L
)
129 if(luaL_getmetafield(L
, 1, "__next"))
132 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
133 return lua_gettop(L
);
136 luaL_checktype(L
, 1, LUA_TTABLE
);
144 /** Overload lua_next() function by using __next metatable field
145 * to get next elements.
146 * \param L The Lua VM stack.
147 * \param idx The index number of elements in stack.
148 * \return 1 if more elements to come, 0 otherwise.
151 luaA_next(lua_State
*L
, int idx
)
153 if(luaL_getmetafield(L
, idx
, "__next"))
155 /* if idx is relative, reduce it since we got __next */
157 /* copy table and then move key */
158 lua_pushvalue(L
, idx
);
159 lua_pushvalue(L
, -3);
161 lua_pcall(L
, 2, 2, 0);
162 /* next returned nil, it's the end */
171 else if(lua_istable(L
, idx
))
172 return lua_next(L
, idx
);
178 /** Generic pairs function.
179 * \param L The Lua VM state.
180 * \return The number of elements pushed on stack.
183 luaA_generic_pairs(lua_State
*L
)
185 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
186 lua_pushvalue(L
, 1); /* state, */
187 lua_pushnil(L
); /* and initial value */
191 /** Overload standard pairs function to use __pairs field of metatables.
192 * \param L The Lua VM state.
193 * \return The number of elements pushed on stack.
196 luaAe_pairs(lua_State
*L
)
198 if(luaL_getmetafield(L
, 1, "__pairs"))
201 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
202 return lua_gettop(L
);
205 luaL_checktype(L
, 1, LUA_TTABLE
);
206 return luaA_generic_pairs(L
);
210 luaA_ipairs_aux(lua_State
*L
)
212 int i
= luaL_checkint(L
, 2) + 1;
213 luaL_checktype(L
, 1, LUA_TTABLE
);
214 lua_pushinteger(L
, i
);
215 lua_rawgeti(L
, 1, i
);
216 return (lua_isnil(L
, -1)) ? 0 : 2;
219 /** Overload standard ipairs function to use __ipairs field of metatables.
220 * \param L The Lua VM state.
221 * \return The number of elements pushed on stack.
224 luaAe_ipairs(lua_State
*L
)
226 if(luaL_getmetafield(L
, 1, "__ipairs"))
229 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
230 return lua_gettop(L
);
233 luaL_checktype(L
, 1, LUA_TTABLE
);
234 lua_pushvalue(L
, lua_upvalueindex(1));
236 lua_pushinteger(L
, 0); /* and initial value */
240 /** Enhanced type() function which recognize awesome objects.
241 * \param L The Lua VM state.
242 * \return The number of arguments pushed on the stack.
245 luaAe_type(lua_State
*L
)
248 lua_pushstring(L
, luaA_typename(L
, 1));
252 /** Replace various standards Lua functions with our own.
253 * \param L The Lua VM state.
256 luaA_fixups(lua_State
*L
)
258 /* export string.wlen */
259 lua_getglobal(L
, "string");
260 lua_pushcfunction(L
, luaA_mbstrlen
);
261 lua_setfield(L
, -2, "wlen");
264 lua_pushliteral(L
, "next");
265 lua_pushcfunction(L
, luaAe_next
);
266 lua_settable(L
, LUA_GLOBALSINDEX
);
268 lua_pushliteral(L
, "pairs");
269 lua_pushcfunction(L
, luaAe_next
);
270 lua_pushcclosure(L
, luaAe_pairs
, 1); /* pairs get next as upvalue */
271 lua_settable(L
, LUA_GLOBALSINDEX
);
273 lua_pushliteral(L
, "ipairs");
274 lua_pushcfunction(L
, luaA_ipairs_aux
);
275 lua_pushcclosure(L
, luaAe_ipairs
, 1);
276 lua_settable(L
, LUA_GLOBALSINDEX
);
278 lua_pushliteral(L
, "type");
279 lua_pushcfunction(L
, luaAe_type
);
280 lua_settable(L
, LUA_GLOBALSINDEX
);
282 lua_pushliteral(L
, "selection");
283 lua_pushcfunction(L
, luaA_selection_get
);
284 lua_settable(L
, LUA_GLOBALSINDEX
);
287 /** __next function for wtable objects.
288 * \param L The Lua VM state.
289 * \return The number of elements pushed on stack.
292 luaA_wtable_next(lua_State
*L
)
294 /* upvalue 1 is content table */
295 if(lua_next(L
, lua_upvalueindex(1)))
301 /** __ipairs function for wtable objects.
302 * \param L The Lua VM state.
303 * \return The number of elements pushed on stack.
306 luaA_wtable_ipairs(lua_State
*L
)
308 /* push ipairs_aux */
309 lua_pushvalue(L
, lua_upvalueindex(2));
310 /* push content table */
311 lua_pushvalue(L
, lua_upvalueindex(1));
312 lua_pushinteger(L
, 0); /* and initial value */
316 /** Index function of wtable objects.
317 * \param L The Lua VM state.
318 * \return The number of elements pushed on stack.
321 luaA_wtable_index(lua_State
*L
)
327 /* check for size, waiting lua 5.2 and __len on tables */
328 if((buf
= lua_tolstring(L
, -1, &len
)))
329 if(a_tokenize(buf
, len
) == A_TK_LEN
)
331 lua_pushnumber(L
, lua_objlen(L
, lua_upvalueindex(1)));
336 /* upvalue 1 is content table */
337 lua_rawget(L
, lua_upvalueindex(1));
341 /** Newndex function of wtable objects.
342 * \param L The Lua VM state.
343 * \return The number of elements pushed on stack.
346 luaA_wtable_newindex(lua_State
*L
)
348 bool invalid
= false;
350 /* push key on top */
352 /* get current key value in content table */
353 lua_rawget(L
, lua_upvalueindex(1));
354 /* if value is a widget, notify change */
355 if(lua_istable(L
, -1) || luaA_toudata(L
, -1, "widget"))
358 lua_pop(L
, 1); /* remove value */
360 /* if new value is a widget or a table */
361 if(lua_istable(L
, 3))
363 luaA_table2wtable(L
);
366 else if(!invalid
&& luaA_toudata(L
, 3, "widget"))
369 /* upvalue 1 is content table */
370 lua_rawset(L
, lua_upvalueindex(1));
373 luaA_wibox_invalidate_byitem(L
, lua_topointer(L
, 1));
378 /** Convert the top element of the stack to a proxied wtable.
379 * \param L The Lua VM state.
382 luaA_table2wtable(lua_State
*L
)
384 if(!lua_istable(L
, -1))
387 lua_newtable(L
); /* create *real* content table */
388 lua_createtable(L
, 0, 5); /* metatable */
389 lua_pushvalue(L
, -2); /* copy content table */
390 lua_pushcfunction(L
, luaA_ipairs_aux
); /* push ipairs aux */
391 lua_pushcclosure(L
, luaA_wtable_ipairs
, 2);
392 lua_pushvalue(L
, -3); /* copy content table */
393 lua_pushcclosure(L
, luaA_wtable_next
, 1); /* __next has the content table as upvalue */
394 lua_pushvalue(L
, -4); /* copy content table */
395 lua_pushcclosure(L
, luaA_wtable_index
, 1); /* __index has the content table as upvalue */
396 lua_pushvalue(L
, -5); /* copy content table */
397 lua_pushcclosure(L
, luaA_wtable_newindex
, 1); /* __newindex has the content table as upvalue */
398 /* set metatable field with just pushed closure */
399 lua_setfield(L
, -5, "__newindex");
400 lua_setfield(L
, -4, "__index");
401 lua_setfield(L
, -3, "__next");
402 lua_setfield(L
, -2, "__ipairs");
403 /* set metatable impossible to touch */
404 lua_pushliteral(L
, "wtable");
405 lua_setfield(L
, -2, "__metatable");
406 /* set new metatable on original table */
407 lua_setmetatable(L
, -3);
411 /* go through original table */
412 while(lua_next(L
, -3))
414 /* if convert value to wtable */
415 luaA_table2wtable(L
);
417 lua_pushvalue(L
, -2);
418 /* move key before value */
420 /* set same value in content table */
423 lua_pushvalue(L
, -1);
424 /* push the new value :-> */
426 /* set orig[k] = nil */
429 /* remove content table */
433 /** Look for an item: table, function, etc.
434 * \param L The Lua VM state.
435 * \param item The pointer item.
438 luaA_hasitem(lua_State
*L
, const void *item
)
441 while(luaA_next(L
, -2))
443 if(lua_topointer(L
, -1) == item
)
445 /* remove value and key */
449 if(lua_istable(L
, -1))
450 if(luaA_hasitem(L
, item
))
452 /* remove key and value */
462 /** Browse a table pushed on top of the index, and put all its table and
463 * sub-table into an array.
464 * \param L The Lua VM state.
465 * \param elems The elements array.
466 * \return False if we encounter an elements already in list.
469 luaA_isloop_check(lua_State
*L
, cptr_array_t
*elems
)
471 if(lua_istable(L
, -1))
473 const void *object
= lua_topointer(L
, -1);
475 /* Check that the object table is not already in the list */
476 for(int i
= 0; i
< elems
->len
; i
++)
477 if(elems
->tab
[i
] == object
)
480 /* push the table in the elements list */
481 cptr_array_append(elems
, object
);
483 /* look every object in the "table" */
485 while(luaA_next(L
, -2))
487 if(!luaA_isloop_check(L
, elems
))
489 /* remove key and value */
493 /* remove value, keep key for next iteration */
500 /** Check if a table is a loop. When using tables as direct acyclic digram,
502 * \param L The Lua VM state.
503 * \param idx The index of the table in the stack
504 * \return True if the table loops.
507 luaA_isloop(lua_State
*L
, int idx
)
509 /* elems is an elements array that we will fill with all array we
510 * encounter while browsing the tables */
513 cptr_array_init(&elems
);
515 /* push table on top */
516 lua_pushvalue(L
, idx
);
518 bool ret
= luaA_isloop_check(L
, &elems
);
520 /* remove pushed table */
523 cptr_array_wipe(&elems
);
528 /** awesome global table.
529 * \param L The Lua VM state.
530 * \return The number of elements pushed on stack.
532 * \lfield font The default font.
533 * \lfield font_height The default font height.
534 * \lfield conffile The configuration file which has been loaded.
537 luaA_awesome_index(lua_State
*L
)
539 if(luaA_usemetatable(L
, 1, 2))
543 const char *buf
= luaL_checklstring(L
, 2, &len
);
545 switch(a_tokenize(buf
, len
))
549 char *font
= pango_font_description_to_string(globalconf
.font
->desc
);
550 lua_pushstring(L
, font
);
554 case A_TK_FONT_HEIGHT
:
555 lua_pushnumber(L
, globalconf
.font
->height
);
558 lua_pushstring(L
, globalconf
.conffile
);
561 luaA_pushxcolor(L
, &globalconf
.colors
.fg
);
564 luaA_pushxcolor(L
, &globalconf
.colors
.bg
);
567 lua_pushliteral(L
, AWESOME_VERSION
);
570 lua_pushliteral(L
, AWESOME_RELEASE
);
579 /** Newindex function for the awesome global table.
580 * \param L The Lua VM state.
581 * \return The number of elements pushed on stack.
584 luaA_awesome_newindex(lua_State
*L
)
586 if(luaA_usemetatable(L
, 1, 2))
590 const char *buf
= luaL_checklstring(L
, 2, &len
);
592 switch(a_tokenize(buf
, len
))
596 const char *newfont
= luaL_checkstring(L
, 3);
597 draw_font_delete(&globalconf
.font
);
598 globalconf
.font
= draw_font_new(newfont
);
599 /* refresh all wiboxes */
600 foreach(wibox
, globalconf
.wiboxes
)
601 (*wibox
)->need_update
= true;
602 foreach(c
, globalconf
.clients
)
604 (*c
)->titlebar
->need_update
= true;
608 if((buf
= luaL_checklstring(L
, 3, &len
)))
609 xcolor_init_reply(xcolor_init_unchecked(&globalconf
.colors
.fg
, buf
, len
));
612 if((buf
= luaL_checklstring(L
, 3, &len
)))
613 xcolor_init_reply(xcolor_init_unchecked(&globalconf
.colors
.bg
, buf
, len
));
622 /** Initialize the Lua VM
623 * \param xdg An xdg handle to use to get XDG basedir.
626 luaA_init(xdgHandle
* xdg
)
629 static const struct luaL_reg awesome_lib
[] =
631 { "quit", luaA_quit
},
632 { "exec", luaA_exec
},
633 { "spawn", luaA_spawn
},
634 { "restart", luaA_restart
},
635 { "__index", luaA_awesome_index
},
636 { "__newindex", luaA_awesome_newindex
},
640 L
= globalconf
.L
= luaL_newstate();
646 luaA_object_setup(L
);
648 /* Export awesome lib */
649 luaA_openlib(L
, "awesome", awesome_lib
, awesome_lib
);
651 /* Export root lib */
652 luaL_register(L
, "root", awesome_root_lib
);
653 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
655 /* Export hooks lib */
656 luaL_register(L
, "hooks", awesome_hooks_lib
);
657 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
660 /* Export D-Bus lib */
661 luaL_register(L
, "dbus", awesome_dbus_lib
);
662 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
665 /* Export keygrabber lib */
666 luaL_register(L
, "keygrabber", awesome_keygrabber_lib
);
667 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
669 /* Export mousegrabber lib */
670 luaL_register(L
, "mousegrabber", awesome_mousegrabber_lib
);
671 lua_pop(L
, 1); /* luaL_register() leaves the table on stack */
674 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
677 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
680 luaA_class_setup(L
, &button_class
, "button", (lua_class_allocator_t
) button_new
,
681 awesome_button_methods
, awesome_button_meta
);
684 luaA_class_setup(L
, &image_class
, "image", (lua_class_allocator_t
) image_new
,
685 awesome_image_methods
, awesome_image_meta
);
688 luaA_class_setup(L
, &tag_class
, "tag", (lua_class_allocator_t
) tag_new
,
689 awesome_tag_methods
, awesome_tag_meta
);
692 luaA_class_setup(L
, &wibox_class
, "wibox", (lua_class_allocator_t
) wibox_new
,
693 awesome_wibox_methods
, awesome_wibox_meta
);
696 luaA_class_setup(L
, &widget_class
, "widget", (lua_class_allocator_t
) widget_new
,
697 awesome_widget_methods
, awesome_widget_meta
);
700 luaA_class_setup(L
, &client_class
, "client", (lua_class_allocator_t
) client_new
,
701 awesome_client_methods
, awesome_client_meta
);
704 luaA_class_setup(L
, &key_class
, "key", (lua_class_allocator_t
) key_new
,
705 awesome_key_methods
, awesome_key_meta
);
708 globalconf
.hooks
.manage
= LUA_REFNIL
;
709 globalconf
.hooks
.unmanage
= LUA_REFNIL
;
710 globalconf
.hooks
.focus
= LUA_REFNIL
;
711 globalconf
.hooks
.unfocus
= LUA_REFNIL
;
712 globalconf
.hooks
.mouse_enter
= LUA_REFNIL
;
713 globalconf
.hooks
.mouse_leave
= LUA_REFNIL
;
714 globalconf
.hooks
.clients
= LUA_REFNIL
;
715 globalconf
.hooks
.tags
= LUA_REFNIL
;
716 globalconf
.hooks
.tagged
= LUA_REFNIL
;
717 globalconf
.hooks
.property
= LUA_REFNIL
;
718 globalconf
.hooks
.startup_notification
= LUA_REFNIL
;
719 globalconf
.hooks
.timer
= LUA_REFNIL
;
720 globalconf
.hooks
.exit
= LUA_REFNIL
;
722 /* add Lua lib path (/usr/share/awesome/lib by default) */
723 lua_getglobal(L
, "package");
724 if (LUA_TTABLE
!= lua_type(L
, 1))
726 warn("package is not a table");
729 lua_getfield(L
, 1, "path");
730 if (LUA_TSTRING
!= lua_type(L
, 2))
732 warn("package.path is not a string");
736 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?.lua");
737 lua_pushliteral(L
, ";" AWESOME_LUA_LIB_PATH
"/?/init.lua");
738 lua_concat(L
, 3); /* concatenate with package.path */
740 /* add XDG_CONFIG_DIR as include path */
741 const char * const *xdgconfigdirs
= xdgSearchableConfigDirectories(xdg
);
742 for(; *xdgconfigdirs
; xdgconfigdirs
++)
744 size_t len
= a_strlen(*xdgconfigdirs
);
745 lua_pushliteral(L
, ";");
746 lua_pushlstring(L
, *xdgconfigdirs
, len
);
747 lua_pushliteral(L
, "/awesome/?.lua");
750 lua_pushliteral(L
, ";");
751 lua_pushlstring(L
, *xdgconfigdirs
, len
);
752 lua_pushliteral(L
, "/awesome/?/init.lua");
755 lua_concat(L
, 3); /* concatenate with package.path */
757 lua_setfield(L
, 1, "path"); /* package.path = "concatenated string" */
761 luaA_loadrc(const char *confpath
, bool run
)
763 if(!luaL_loadfile(globalconf
.L
, confpath
))
767 if(lua_pcall(globalconf
.L
, 0, LUA_MULTRET
, 0))
768 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
771 globalconf
.conffile
= a_strdup(confpath
);
776 lua_pop(globalconf
.L
, 1);
780 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
785 /** Load a configuration file.
786 * \param xdg An xdg handle to use to get XDG basedir.
787 * \param confpatharg The configuration file to load.
788 * \param run Run the configuration file.
791 luaA_parserc(xdgHandle
* xdg
, const char *confpatharg
, bool run
)
793 char *confpath
= NULL
;
796 /* try to load, return if it's ok */
799 if(luaA_loadrc(confpatharg
, run
))
808 confpath
= xdgConfigFind("awesome/rc.lua", xdg
);
810 char *tmp
= confpath
;
812 /* confpath is "string1\0string2\0string3\0\0" */
815 if(luaA_loadrc(tmp
, run
))
822 tmp
+= a_strlen(tmp
) + 1;
833 luaA_on_timer(EV_P_ ev_timer
*w
, int revents
)
835 if(globalconf
.hooks
.timer
!= LUA_REFNIL
)
836 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.timer
, 0, 0);
839 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80