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_methods
[];
53 extern const struct luaL_reg awesome_root_meta
[];
54 extern const struct luaL_reg awesome_button_methods
[];
55 extern const struct luaL_reg awesome_button_meta
[];
56 extern const struct luaL_reg awesome_image_methods
[];
57 extern const struct luaL_reg awesome_image_meta
[];
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
[];
62 extern const struct luaL_reg awesome_client_methods
[];
63 extern const struct luaL_reg awesome_client_meta
[];
64 extern const struct luaL_reg awesome_tag_methods
[];
65 extern const struct luaL_reg awesome_tag_meta
[];
66 extern const struct luaL_reg awesome_widget_methods
[];
67 extern const struct luaL_reg awesome_widget_meta
[];
68 extern const struct luaL_reg awesome_wibox_methods
[];
69 extern const struct luaL_reg awesome_wibox_meta
[];
70 extern const struct luaL_reg awesome_key_methods
[];
71 extern const struct luaL_reg awesome_key_meta
[];
74 * \param L The Lua VM state.
75 * \return The number of elements pushed on stack.
78 luaA_quit(lua_State
*L
__attribute__ ((unused
)))
80 ev_unloop(globalconf
.loop
, 1);
84 /** Execute another application, probably a window manager, to replace
86 * \param L The Lua VM state.
87 * \return The number of elements pushed on stack.
89 * \lparam The command line to execute.
92 luaA_exec(lua_State
*L
)
94 const char *cmd
= luaL_checkstring(L
, 1);
105 luaA_restart(lua_State
*L
__attribute__ ((unused
)))
111 /** UTF-8 aware string length computing.
112 * \param L The Lua VM state.
113 * \return The number of elements pushed on stack.
116 luaA_mbstrlen(lua_State
*L
)
118 const char *cmd
= luaL_checkstring(L
, 1);
119 lua_pushnumber(L
, (ssize_t
) mbstowcs(NULL
, NONULL(cmd
), 0));
123 /** Overload standard Lua next function to use __next key on metatable.
124 * \param L The Lua VM state.
125 * \return The number of elements pushed on stack.
128 luaAe_next(lua_State
*L
)
130 if(luaL_getmetafield(L
, 1, "__next"))
133 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
134 return lua_gettop(L
);
137 luaL_checktype(L
, 1, LUA_TTABLE
);
145 /** Overload lua_next() function by using __next metatable field
146 * to get next elements.
147 * \param L The Lua VM stack.
148 * \param idx The index number of elements in stack.
149 * \return 1 if more elements to come, 0 otherwise.
152 luaA_next(lua_State
*L
, int idx
)
154 if(luaL_getmetafield(L
, idx
, "__next"))
156 /* if idx is relative, reduce it since we got __next */
158 /* copy table and then move key */
159 lua_pushvalue(L
, idx
);
160 lua_pushvalue(L
, -3);
162 lua_pcall(L
, 2, 2, 0);
163 /* next returned nil, it's the end */
172 else if(lua_istable(L
, idx
))
173 return lua_next(L
, idx
);
179 /** Generic pairs function.
180 * \param L The Lua VM state.
181 * \return The number of elements pushed on stack.
184 luaA_generic_pairs(lua_State
*L
)
186 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
187 lua_pushvalue(L
, 1); /* state, */
188 lua_pushnil(L
); /* and initial value */
192 /** Overload standard pairs function to use __pairs field of metatables.
193 * \param L The Lua VM state.
194 * \return The number of elements pushed on stack.
197 luaAe_pairs(lua_State
*L
)
199 if(luaL_getmetafield(L
, 1, "__pairs"))
202 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
203 return lua_gettop(L
);
206 luaL_checktype(L
, 1, LUA_TTABLE
);
207 return luaA_generic_pairs(L
);
211 luaA_ipairs_aux(lua_State
*L
)
213 int i
= luaL_checkint(L
, 2) + 1;
214 luaL_checktype(L
, 1, LUA_TTABLE
);
215 lua_pushinteger(L
, i
);
216 lua_rawgeti(L
, 1, i
);
217 return (lua_isnil(L
, -1)) ? 0 : 2;
220 /** Overload standard ipairs function to use __ipairs field of metatables.
221 * \param L The Lua VM state.
222 * \return The number of elements pushed on stack.
225 luaAe_ipairs(lua_State
*L
)
227 if(luaL_getmetafield(L
, 1, "__ipairs"))
230 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
231 return lua_gettop(L
);
234 luaL_checktype(L
, 1, LUA_TTABLE
);
235 lua_pushvalue(L
, lua_upvalueindex(1));
237 lua_pushinteger(L
, 0); /* and initial value */
241 /** Enhanced type() function which recognize awesome objects.
242 * \param L The Lua VM state.
243 * \return The number of arguments pushed on the stack.
246 luaAe_type(lua_State
*L
)
249 lua_pushstring(L
, luaA_typename(L
, 1));
253 /** Replace various standards Lua functions with our own.
254 * \param L The Lua VM state.
257 luaA_fixups(lua_State
*L
)
259 /* export string.wlen */
260 lua_getglobal(L
, "string");
261 lua_pushcfunction(L
, luaA_mbstrlen
);
262 lua_setfield(L
, -2, "wlen");
265 lua_pushliteral(L
, "next");
266 lua_pushcfunction(L
, luaAe_next
);
267 lua_settable(L
, LUA_GLOBALSINDEX
);
269 lua_pushliteral(L
, "pairs");
270 lua_pushcfunction(L
, luaAe_next
);
271 lua_pushcclosure(L
, luaAe_pairs
, 1); /* pairs get next as upvalue */
272 lua_settable(L
, LUA_GLOBALSINDEX
);
274 lua_pushliteral(L
, "ipairs");
275 lua_pushcfunction(L
, luaA_ipairs_aux
);
276 lua_pushcclosure(L
, luaAe_ipairs
, 1);
277 lua_settable(L
, LUA_GLOBALSINDEX
);
279 lua_pushliteral(L
, "type");
280 lua_pushcfunction(L
, luaAe_type
);
281 lua_settable(L
, LUA_GLOBALSINDEX
);
283 lua_pushliteral(L
, "selection");
284 lua_pushcfunction(L
, luaA_selection_get
);
285 lua_settable(L
, LUA_GLOBALSINDEX
);
288 /** __next function for wtable objects.
289 * \param L The Lua VM state.
290 * \return The number of elements pushed on stack.
293 luaA_wtable_next(lua_State
*L
)
295 /* upvalue 1 is content table */
296 if(lua_next(L
, lua_upvalueindex(1)))
302 /** __ipairs function for wtable objects.
303 * \param L The Lua VM state.
304 * \return The number of elements pushed on stack.
307 luaA_wtable_ipairs(lua_State
*L
)
309 /* push ipairs_aux */
310 lua_pushvalue(L
, lua_upvalueindex(2));
311 /* push content table */
312 lua_pushvalue(L
, lua_upvalueindex(1));
313 lua_pushinteger(L
, 0); /* and initial value */
317 /** Index function of wtable objects.
318 * \param L The Lua VM state.
319 * \return The number of elements pushed on stack.
322 luaA_wtable_index(lua_State
*L
)
328 /* check for size, waiting lua 5.2 and __len on tables */
329 if((buf
= lua_tolstring(L
, -1, &len
)))
330 if(a_tokenize(buf
, len
) == A_TK_LEN
)
332 lua_pushnumber(L
, lua_objlen(L
, lua_upvalueindex(1)));
337 /* upvalue 1 is content table */
338 lua_rawget(L
, lua_upvalueindex(1));
342 /** Newndex function of wtable objects.
343 * \param L The Lua VM state.
344 * \return The number of elements pushed on stack.
347 luaA_wtable_newindex(lua_State
*L
)
349 bool invalid
= false;
351 /* push key on top */
353 /* get current key value in content table */
354 lua_rawget(L
, lua_upvalueindex(1));
355 /* if value is a widget, notify change */
356 if(lua_istable(L
, -1) || luaA_toudata(L
, -1, "widget"))
359 lua_pop(L
, 1); /* remove value */
361 /* if new value is a widget or a table */
362 if(lua_istable(L
, 3))
364 luaA_table2wtable(L
);
367 else if(!invalid
&& luaA_toudata(L
, 3, "widget"))
370 /* upvalue 1 is content table */
371 lua_rawset(L
, lua_upvalueindex(1));
374 luaA_wibox_invalidate_byitem(L
, lua_topointer(L
, 1));
379 /** Convert the top element of the stack to a proxied wtable.
380 * \param L The Lua VM state.
383 luaA_table2wtable(lua_State
*L
)
385 if(!lua_istable(L
, -1))
388 lua_newtable(L
); /* create *real* content table */
389 lua_createtable(L
, 0, 5); /* metatable */
390 lua_pushvalue(L
, -2); /* copy content table */
391 lua_pushcfunction(L
, luaA_ipairs_aux
); /* push ipairs aux */
392 lua_pushcclosure(L
, luaA_wtable_ipairs
, 2);
393 lua_pushvalue(L
, -3); /* copy content table */
394 lua_pushcclosure(L
, luaA_wtable_next
, 1); /* __next has the content table as upvalue */
395 lua_pushvalue(L
, -4); /* copy content table */
396 lua_pushcclosure(L
, luaA_wtable_index
, 1); /* __index has the content table as upvalue */
397 lua_pushvalue(L
, -5); /* copy content table */
398 lua_pushcclosure(L
, luaA_wtable_newindex
, 1); /* __newindex has the content table as upvalue */
399 /* set metatable field with just pushed closure */
400 lua_setfield(L
, -5, "__newindex");
401 lua_setfield(L
, -4, "__index");
402 lua_setfield(L
, -3, "__next");
403 lua_setfield(L
, -2, "__ipairs");
404 /* set metatable impossible to touch */
405 lua_pushliteral(L
, "wtable");
406 lua_setfield(L
, -2, "__metatable");
407 /* set new metatable on original table */
408 lua_setmetatable(L
, -3);
412 /* go through original table */
413 while(lua_next(L
, -3))
415 /* if convert value to wtable */
416 luaA_table2wtable(L
);
418 lua_pushvalue(L
, -2);
419 /* move key before value */
421 /* set same value in content table */
424 lua_pushvalue(L
, -1);
425 /* push the new value :-> */
427 /* set orig[k] = nil */
430 /* remove content table */
434 /** Look for an item: table, function, etc.
435 * \param L The Lua VM state.
436 * \param item The pointer item.
439 luaA_hasitem(lua_State
*L
, const void *item
)
442 while(luaA_next(L
, -2))
444 if(lua_topointer(L
, -1) == item
)
446 /* remove value and key */
450 if(lua_istable(L
, -1))
451 if(luaA_hasitem(L
, item
))
453 /* remove key and value */
463 /** Browse a table pushed on top of the index, and put all its table and
464 * sub-table into an array.
465 * \param L The Lua VM state.
466 * \param elems The elements array.
467 * \return False if we encounter an elements already in list.
470 luaA_isloop_check(lua_State
*L
, cptr_array_t
*elems
)
472 if(lua_istable(L
, -1))
474 const void *object
= lua_topointer(L
, -1);
476 /* Check that the object table is not already in the list */
477 for(int i
= 0; i
< elems
->len
; i
++)
478 if(elems
->tab
[i
] == object
)
481 /* push the table in the elements list */
482 cptr_array_append(elems
, object
);
484 /* look every object in the "table" */
486 while(luaA_next(L
, -2))
488 if(!luaA_isloop_check(L
, elems
))
490 /* remove key and value */
494 /* remove value, keep key for next iteration */
501 /** Check if a table is a loop. When using tables as direct acyclic digram,
503 * \param L The Lua VM state.
504 * \param idx The index of the table in the stack
505 * \return True if the table loops.
508 luaA_isloop(lua_State
*L
, int idx
)
510 /* elems is an elements array that we will fill with all array we
511 * encounter while browsing the tables */
514 cptr_array_init(&elems
);
516 /* push table on top */
517 lua_pushvalue(L
, idx
);
519 bool ret
= luaA_isloop_check(L
, &elems
);
521 /* remove pushed table */
524 cptr_array_wipe(&elems
);
529 /** awesome global table.
530 * \param L The Lua VM state.
531 * \return The number of elements pushed on stack.
533 * \lfield font The default font.
534 * \lfield font_height The default font height.
535 * \lfield conffile The configuration file which has been loaded.
538 luaA_awesome_index(lua_State
*L
)
540 if(luaA_usemetatable(L
, 1, 2))
544 const char *buf
= luaL_checklstring(L
, 2, &len
);
546 switch(a_tokenize(buf
, len
))
550 char *font
= pango_font_description_to_string(globalconf
.font
->desc
);
551 lua_pushstring(L
, font
);
555 case A_TK_FONT_HEIGHT
:
556 lua_pushnumber(L
, globalconf
.font
->height
);
559 lua_pushstring(L
, globalconf
.conffile
);
562 luaA_pushxcolor(L
, &globalconf
.colors
.fg
);
565 luaA_pushxcolor(L
, &globalconf
.colors
.bg
);
568 lua_pushliteral(L
, AWESOME_VERSION
);
571 lua_pushliteral(L
, AWESOME_RELEASE
);
580 /** Newindex function for the awesome global table.
581 * \param L The Lua VM state.
582 * \return The number of elements pushed on stack.
585 luaA_awesome_newindex(lua_State
*L
)
587 if(luaA_usemetatable(L
, 1, 2))
591 const char *buf
= luaL_checklstring(L
, 2, &len
);
593 switch(a_tokenize(buf
, len
))
597 const char *newfont
= luaL_checkstring(L
, 3);
598 draw_font_delete(&globalconf
.font
);
599 globalconf
.font
= draw_font_new(newfont
);
600 /* refresh all wiboxes */
601 foreach(wibox
, globalconf
.wiboxes
)
602 (*wibox
)->need_update
= true;
603 foreach(c
, globalconf
.clients
)
605 (*c
)->titlebar
->need_update
= true;
609 if((buf
= luaL_checklstring(L
, 3, &len
)))
610 xcolor_init_reply(xcolor_init_unchecked(&globalconf
.colors
.fg
, buf
, len
));
613 if((buf
= luaL_checklstring(L
, 3, &len
)))
614 xcolor_init_reply(xcolor_init_unchecked(&globalconf
.colors
.bg
, buf
, len
));
623 /** Initialize the Lua VM
624 * \param xdg An xdg handle to use to get XDG basedir.
627 luaA_init(xdgHandle
* xdg
)
630 static const struct luaL_reg awesome_lib
[] =
632 { "quit", luaA_quit
},
633 { "exec", luaA_exec
},
634 { "spawn", luaA_spawn
},
635 { "restart", luaA_restart
},
636 { "__index", luaA_awesome_index
},
637 { "__newindex", luaA_awesome_newindex
},
641 L
= globalconf
.L
= luaL_newstate();
647 luaA_object_setup(L
);
649 /* Export awesome lib */
650 luaA_openlib(L
, "awesome", awesome_lib
, awesome_lib
);
653 luaA_openlib(L
, "root", awesome_root_methods
, awesome_root_meta
);
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