Remove "encoding=utf-8" from Vim modelines
[awesome.git] / luaa.c
blob63886044ac12541dd01a0958c3bf18c7d6655824
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 <ev.h>
26 #include <lua.h>
27 #include <lauxlib.h>
28 #include <lualib.h>
30 #include <basedir_fs.h>
32 #include "awesome.h"
33 #include "config.h"
34 #include "timer.h"
35 #include "awesome-version-internal.h"
36 #include "ewmh.h"
37 #include "luaa.h"
38 #include "spawn.h"
39 #include "tag.h"
40 #include "client.h"
41 #include "screen.h"
42 #include "event.h"
43 #include "selection.h"
44 #include "window.h"
45 #include "common/xcursor.h"
46 #include "common/buffer.h"
48 #ifdef WITH_DBUS
49 extern const struct luaL_reg awesome_dbus_lib[];
50 #endif
51 extern const struct luaL_reg awesome_hooks_lib[];
52 extern const struct luaL_reg awesome_keygrabber_lib[];
53 extern const struct luaL_reg awesome_mousegrabber_lib[];
54 extern const struct luaL_reg awesome_root_lib[];
55 extern const struct luaL_reg awesome_mouse_methods[];
56 extern const struct luaL_reg awesome_mouse_meta[];
57 extern const struct luaL_reg awesome_screen_methods[];
58 extern const struct luaL_reg awesome_screen_meta[];
60 /** Quit awesome.
61 * \param L The Lua VM state.
62 * \return The number of elements pushed on stack.
64 static int
65 luaA_quit(lua_State *L)
67 ev_unloop(globalconf.loop, 1);
68 return 0;
71 /** Execute another application, probably a window manager, to replace
72 * awesome.
73 * \param L The Lua VM state.
74 * \return The number of elements pushed on stack.
75 * \luastack
76 * \lparam The command line to execute.
78 static int
79 luaA_exec(lua_State *L)
81 const char *cmd = luaL_checkstring(L, 1);
83 awesome_atexit(false);
85 a_exec(cmd);
86 return 0;
89 /** Restart awesome.
91 static int
92 luaA_restart(lua_State *L)
94 awesome_restart();
95 return 0;
98 /** UTF-8 aware string length computing.
99 * \param L The Lua VM state.
100 * \return The number of elements pushed on stack.
102 static int
103 luaA_mbstrlen(lua_State *L)
105 const char *cmd = luaL_checkstring(L, 1);
106 lua_pushnumber(L, (ssize_t) mbstowcs(NULL, NONULL(cmd), 0));
107 return 1;
110 /** Overload standard Lua next function to use __next key on metatable.
111 * \param L The Lua VM state.
112 * \return The number of elements pushed on stack.
114 static int
115 luaAe_next(lua_State *L)
117 if(luaL_getmetafield(L, 1, "__next"))
119 lua_insert(L, 1);
120 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
121 return lua_gettop(L);
124 luaL_checktype(L, 1, LUA_TTABLE);
125 lua_settop(L, 2);
126 if(lua_next(L, 1))
127 return 2;
128 lua_pushnil(L);
129 return 1;
132 /** Overload lua_next() function by using __next metatable field
133 * to get next elements.
134 * \param L The Lua VM stack.
135 * \param idx The index number of elements in stack.
136 * \return 1 if more elements to come, 0 otherwise.
139 luaA_next(lua_State *L, int idx)
141 if(luaL_getmetafield(L, idx, "__next"))
143 /* if idx is relative, reduce it since we got __next */
144 if(idx < 0) idx--;
145 /* copy table and then move key */
146 lua_pushvalue(L, idx);
147 lua_pushvalue(L, -3);
148 lua_remove(L, -4);
149 lua_pcall(L, 2, 2, 0);
150 /* next returned nil, it's the end */
151 if(lua_isnil(L, -1))
153 /* remove nil */
154 lua_pop(L, 2);
155 return 0;
157 return 1;
159 else if(lua_istable(L, idx))
160 return lua_next(L, idx);
161 /* remove the key */
162 lua_pop(L, 1);
163 return 0;
166 /** Generic pairs function.
167 * \param L The Lua VM state.
168 * \return The number of elements pushed on stack.
170 static int
171 luaA_generic_pairs(lua_State *L)
173 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
174 lua_pushvalue(L, 1); /* state, */
175 lua_pushnil(L); /* and initial value */
176 return 3;
179 /** Overload standard pairs function to use __pairs field of metatables.
180 * \param L The Lua VM state.
181 * \return The number of elements pushed on stack.
183 static int
184 luaAe_pairs(lua_State *L)
186 if(luaL_getmetafield(L, 1, "__pairs"))
188 lua_insert(L, 1);
189 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
190 return lua_gettop(L);
193 luaL_checktype(L, 1, LUA_TTABLE);
194 return luaA_generic_pairs(L);
197 static int
198 luaA_ipairs_aux(lua_State *L)
200 int i = luaL_checkint(L, 2) + 1;
201 luaL_checktype(L, 1, LUA_TTABLE);
202 lua_pushinteger(L, i);
203 lua_rawgeti(L, 1, i);
204 return (lua_isnil(L, -1)) ? 0 : 2;
207 /** Overload standard ipairs function to use __ipairs field of metatables.
208 * \param L The Lua VM state.
209 * \return The number of elements pushed on stack.
211 static int
212 luaAe_ipairs(lua_State *L)
214 if(luaL_getmetafield(L, 1, "__ipairs"))
216 lua_insert(L, 1);
217 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
218 return lua_gettop(L);
221 luaL_checktype(L, 1, LUA_TTABLE);
222 lua_pushvalue(L, lua_upvalueindex(1));
223 lua_pushvalue(L, 1);
224 lua_pushinteger(L, 0); /* and initial value */
225 return 3;
228 /** Enhanced type() function which recognize awesome objects.
229 * \param L The Lua VM state.
230 * \return The number of arguments pushed on the stack.
232 static int
233 luaAe_type(lua_State *L)
235 luaL_checkany(L, 1);
236 lua_pushstring(L, luaA_typename(L, 1));
237 return 1;
240 /** Modified version of os.execute() which use spawn code to reset signal
241 * handlers correctly before exec()'ing the new program.
242 * \param L The Lua VM state.
243 * \return The number of arguments pushed on stack.
245 static int
246 luaAe_os_execute(lua_State *L)
248 lua_pushinteger(L, spawn_system(luaL_optstring(L, 1, NULL)));
249 return 1;
252 /** Replace various standards Lua functions with our own.
253 * \param L The Lua VM state.
255 static void
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");
262 lua_pop(L, 1);
263 /* replace os.execute */
264 lua_getglobal(L, "os");
265 lua_pushcfunction(L, luaAe_os_execute);
266 lua_setfield(L, -2, "execute");
267 lua_pop(L, 1);
268 /* replace next */
269 lua_pushliteral(L, "next");
270 lua_pushcfunction(L, luaAe_next);
271 lua_settable(L, LUA_GLOBALSINDEX);
272 /* replace pairs */
273 lua_pushliteral(L, "pairs");
274 lua_pushcfunction(L, luaAe_next);
275 lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
276 lua_settable(L, LUA_GLOBALSINDEX);
277 /* replace ipairs */
278 lua_pushliteral(L, "ipairs");
279 lua_pushcfunction(L, luaA_ipairs_aux);
280 lua_pushcclosure(L, luaAe_ipairs, 1);
281 lua_settable(L, LUA_GLOBALSINDEX);
282 /* replace type */
283 lua_pushliteral(L, "type");
284 lua_pushcfunction(L, luaAe_type);
285 lua_settable(L, LUA_GLOBALSINDEX);
286 /* set selection */
287 lua_pushliteral(L, "selection");
288 lua_pushcfunction(L, luaA_selection_get);
289 lua_settable(L, LUA_GLOBALSINDEX);
292 /** __next function for wtable objects.
293 * \param L The Lua VM state.
294 * \return The number of elements pushed on stack.
296 static int
297 luaA_wtable_next(lua_State *L)
299 /* upvalue 1 is content table */
300 if(lua_next(L, lua_upvalueindex(1)))
301 return 2;
302 lua_pushnil(L);
303 return 1;
306 /** __ipairs function for wtable objects.
307 * \param L The Lua VM state.
308 * \return The number of elements pushed on stack.
310 static int
311 luaA_wtable_ipairs(lua_State *L)
313 /* push ipairs_aux */
314 lua_pushvalue(L, lua_upvalueindex(2));
315 /* push content table */
316 lua_pushvalue(L, lua_upvalueindex(1));
317 lua_pushinteger(L, 0); /* and initial value */
318 return 3;
321 /** Index function of wtable objects.
322 * \param L The Lua VM state.
323 * \return The number of elements pushed on stack.
325 static int
326 luaA_wtable_index(lua_State *L)
328 size_t len;
329 const char *buf;
331 lua_pushvalue(L, 2);
332 /* check for size, waiting lua 5.2 and __len on tables */
333 if((buf = lua_tolstring(L, -1, &len)))
334 if(a_tokenize(buf, len) == A_TK_LEN)
336 lua_pushnumber(L, lua_objlen(L, lua_upvalueindex(1)));
337 return 1;
339 lua_pop(L, 1);
341 /* upvalue 1 is content table */
342 lua_rawget(L, lua_upvalueindex(1));
343 return 1;
346 /** Newindex function of wtable objects.
347 * \param L The Lua VM state.
348 * \return The number of elements pushed on stack.
350 static int
351 luaA_wtable_newindex(lua_State *L)
353 bool invalid = false;
355 /* push key on top */
356 lua_pushvalue(L, 2);
357 /* get current key value in content table */
358 lua_rawget(L, lua_upvalueindex(1));
359 /* if value is a widget, notify change */
360 if(lua_istable(L, -1) || luaA_toudata(L, -1, &widget_class))
361 invalid = true;
363 lua_pop(L, 1); /* remove value */
365 /* if new value is a widget or a table */
366 if(lua_istable(L, 3))
368 luaA_table2wtable(L);
369 invalid = true;
371 else if(!invalid && luaA_toudata(L, 3, &widget_class))
372 invalid = true;
374 /* upvalue 1 is content table */
375 lua_rawset(L, lua_upvalueindex(1));
377 if(invalid)
378 luaA_wibox_invalidate_byitem(L, lua_topointer(L, 1));
380 return 0;
383 /** Convert the top element of the stack to a proxied wtable.
384 * \param L The Lua VM state.
386 void
387 luaA_table2wtable(lua_State *L)
389 if(!lua_istable(L, -1))
390 return;
392 lua_newtable(L); /* create *real* content table */
393 lua_createtable(L, 0, 5); /* metatable */
394 lua_pushvalue(L, -2); /* copy content table */
395 lua_pushcfunction(L, luaA_ipairs_aux); /* push ipairs aux */
396 lua_pushcclosure(L, luaA_wtable_ipairs, 2);
397 lua_pushvalue(L, -3); /* copy content table */
398 lua_pushcclosure(L, luaA_wtable_next, 1); /* __next has the content table as upvalue */
399 lua_pushvalue(L, -4); /* copy content table */
400 lua_pushcclosure(L, luaA_wtable_index, 1); /* __index has the content table as upvalue */
401 lua_pushvalue(L, -5); /* copy content table */
402 lua_pushcclosure(L, luaA_wtable_newindex, 1); /* __newindex has the content table as upvalue */
403 /* set metatable field with just pushed closure */
404 lua_setfield(L, -5, "__newindex");
405 lua_setfield(L, -4, "__index");
406 lua_setfield(L, -3, "__next");
407 lua_setfield(L, -2, "__ipairs");
408 /* set metatable impossible to touch */
409 lua_pushliteral(L, "wtable");
410 lua_setfield(L, -2, "__metatable");
411 /* set new metatable on original table */
412 lua_setmetatable(L, -3);
414 /* initial key */
415 lua_pushnil(L);
416 /* go through original table */
417 while(lua_next(L, -3))
419 /* if convert value to wtable */
420 luaA_table2wtable(L);
421 /* copy key */
422 lua_pushvalue(L, -2);
423 /* move key before value */
424 lua_insert(L, -2);
425 /* set same value in content table */
426 lua_rawset(L, -4);
427 /* copy key */
428 lua_pushvalue(L, -1);
429 /* push the new value :-> */
430 lua_pushnil(L);
431 /* set orig[k] = nil */
432 lua_rawset(L, -5);
434 /* remove content table */
435 lua_pop(L, 1);
438 /** Look for an item: table, function, etc.
439 * \param L The Lua VM state.
440 * \param item The pointer item.
442 bool
443 luaA_hasitem(lua_State *L, const void *item)
445 lua_pushnil(L);
446 while(luaA_next(L, -2))
448 if(lua_topointer(L, -1) == item)
450 /* remove value and key */
451 lua_pop(L, 2);
452 return true;
454 if(lua_istable(L, -1))
455 if(luaA_hasitem(L, item))
457 /* remove key and value */
458 lua_pop(L, 2);
459 return true;
461 /* remove value */
462 lua_pop(L, 1);
464 return false;
467 /** Browse a table pushed on top of the index, and put all its table and
468 * sub-table into an array.
469 * \param L The Lua VM state.
470 * \param elems The elements array.
471 * \return False if we encounter an elements already in list.
473 static bool
474 luaA_isloop_check(lua_State *L, cptr_array_t *elems)
476 if(lua_istable(L, -1))
478 const void *object = lua_topointer(L, -1);
480 /* Check that the object table is not already in the list */
481 for(int i = 0; i < elems->len; i++)
482 if(elems->tab[i] == object)
483 return false;
485 /* push the table in the elements list */
486 cptr_array_append(elems, object);
488 /* look every object in the "table" */
489 lua_pushnil(L);
490 while(luaA_next(L, -2))
492 if(!luaA_isloop_check(L, elems))
494 /* remove key and value */
495 lua_pop(L, 2);
496 return false;
498 /* remove value, keep key for next iteration */
499 lua_pop(L, 1);
502 return true;
505 /** Check if a table is a loop. When using tables as direct acyclic digram,
506 * this is useful.
507 * \param L The Lua VM state.
508 * \param idx The index of the table in the stack
509 * \return True if the table loops.
511 bool
512 luaA_isloop(lua_State *L, int idx)
514 /* elems is an elements array that we will fill with all array we
515 * encounter while browsing the tables */
516 cptr_array_t elems;
518 cptr_array_init(&elems);
520 /* push table on top */
521 lua_pushvalue(L, idx);
523 bool ret = luaA_isloop_check(L, &elems);
525 /* remove pushed table */
526 lua_pop(L, 1);
528 cptr_array_wipe(&elems);
530 return !ret;
533 /** awesome global table.
534 * \param L The Lua VM state.
535 * \return The number of elements pushed on stack.
536 * \luastack
537 * \lfield font The default font.
538 * \lfield font_height The default font height.
539 * \lfield conffile The configuration file which has been loaded.
541 static int
542 luaA_awesome_index(lua_State *L)
544 if(luaA_usemetatable(L, 1, 2))
545 return 1;
547 size_t len;
548 const char *buf = luaL_checklstring(L, 2, &len);
550 switch(a_tokenize(buf, len))
552 case A_TK_FONT:
554 char *font = pango_font_description_to_string(globalconf.font->desc);
555 lua_pushstring(L, font);
556 g_free(font);
558 break;
559 case A_TK_FONT_HEIGHT:
560 lua_pushnumber(L, globalconf.font->height);
561 break;
562 case A_TK_CONFFILE:
563 lua_pushstring(L, globalconf.conffile);
564 break;
565 case A_TK_FG:
566 luaA_pushxcolor(L, globalconf.colors.fg);
567 break;
568 case A_TK_BG:
569 luaA_pushxcolor(L, globalconf.colors.bg);
570 break;
571 case A_TK_VERSION:
572 lua_pushliteral(L, AWESOME_VERSION);
573 break;
574 case A_TK_RELEASE:
575 lua_pushliteral(L, AWESOME_RELEASE);
576 break;
577 case A_TK_STARTUP_ERRORS:
578 if (globalconf.startup_errors.len == 0)
579 return 0;
580 lua_pushstring(L, globalconf.startup_errors.s);
581 default:
582 return 0;
585 return 1;
588 /** Newindex function for the awesome global table.
589 * \param L The Lua VM state.
590 * \return The number of elements pushed on stack.
592 static int
593 luaA_awesome_newindex(lua_State *L)
595 if(luaA_usemetatable(L, 1, 2))
596 return 1;
598 size_t len;
599 const char *buf = luaL_checklstring(L, 2, &len);
601 switch(a_tokenize(buf, len))
603 case A_TK_FONT:
605 const char *newfont = luaL_checkstring(L, 3);
606 draw_font_delete(&globalconf.font);
607 globalconf.font = draw_font_new(newfont);
608 /* refresh all wiboxes */
609 foreach(wibox, globalconf.wiboxes)
610 (*wibox)->need_update = true;
611 foreach(c, globalconf.clients)
612 if((*c)->titlebar)
613 (*c)->titlebar->need_update = true;
615 break;
616 case A_TK_FG:
617 if((buf = luaL_checklstring(L, 3, &len)))
618 xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.fg, buf, len));
619 break;
620 case A_TK_BG:
621 if((buf = luaL_checklstring(L, 3, &len)))
622 xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.bg, buf, len));
623 break;
624 default:
625 return 0;
628 return 0;
631 /** Add a global signal.
632 * \param L The Lua VM state.
633 * \return The number of elements pushed on stack.
634 * \luastack
635 * \lparam A string with the event name.
636 * \lparam The function to call.
638 static int
639 luaA_awesome_add_signal(lua_State *L)
641 const char *name = luaL_checkstring(L, 1);
642 luaA_checkfunction(L, 2);
643 signal_add(&global_signals, name, luaA_object_ref(L, 2));
644 return 0;
647 /** Remove a global signal.
648 * \param L The Lua VM state.
649 * \return The number of elements pushed on stack.
650 * \luastack
651 * \lparam A string with the event name.
652 * \lparam The function to call.
654 static int
655 luaA_awesome_remove_signal(lua_State *L)
657 const char *name = luaL_checkstring(L, 1);
658 luaA_checkfunction(L, 2);
659 const void *func = lua_topointer(L, 2);
660 signal_remove(&global_signals, name, func);
661 luaA_object_unref(L, (void *) func);
662 return 0;
665 /** Emit a global signal.
666 * \param L The Lua VM state.
667 * \return The number of elements pushed on stack.
668 * \luastack
669 * \lparam A string with the event name.
670 * \lparam The function to call.
672 static int
673 luaA_awesome_emit_signal(lua_State *L)
675 signal_object_emit(L, &global_signals, luaL_checkstring(L, 1), lua_gettop(L) - 1);
676 return 0;
679 static int
680 luaA_panic(lua_State *L)
682 warn("unprotected error in call to Lua API (%s), restarting awesome",
683 lua_tostring(L, -1));
684 awesome_restart();
685 return 0;
688 static int
689 luaA_dofunction_on_error(lua_State *L)
691 /* duplicate string error */
692 lua_pushvalue(L, -1);
693 /* emit error signal */
694 signal_object_emit(L, &global_signals, "debug::error", 1);
696 if(!luaL_dostring(L, "return debug.traceback(\"error while running function\", 3)"))
698 /* Move traceback before error */
699 lua_insert(L, -2);
700 /* Insert sentence */
701 lua_pushliteral(L, "\nerror: ");
702 /* Move it before error */
703 lua_insert(L, -2);
704 lua_concat(L, 3);
706 return 1;
709 /** Initialize the Lua VM
710 * \param xdg An xdg handle to use to get XDG basedir.
712 void
713 luaA_init(xdgHandle* xdg)
715 lua_State *L;
716 static const struct luaL_reg awesome_lib[] =
718 { "quit", luaA_quit },
719 { "exec", luaA_exec },
720 { "spawn", luaA_spawn },
721 { "restart", luaA_restart },
722 { "add_signal", luaA_awesome_add_signal },
723 { "remove_signal", luaA_awesome_remove_signal },
724 { "emit_signal", luaA_awesome_emit_signal },
725 { "__index", luaA_awesome_index },
726 { "__newindex", luaA_awesome_newindex },
727 { NULL, NULL }
730 L = globalconf.L = luaL_newstate();
732 /* Set panic function */
733 lua_atpanic(L, luaA_panic);
735 /* Set error handling function */
736 lualib_dofunction_on_error = luaA_dofunction_on_error;
738 luaL_openlibs(L);
740 luaA_fixups(L);
742 luaA_object_setup(L);
744 /* Export awesome lib */
745 luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
747 /* Export root lib */
748 luaL_register(L, "root", awesome_root_lib);
749 lua_pop(L, 1); /* luaL_register() leaves the table on stack */
751 /* Export hooks lib */
752 luaL_register(L, "hooks", awesome_hooks_lib);
753 lua_pop(L, 1); /* luaL_register() leaves the table on stack */
755 #ifdef WITH_DBUS
756 /* Export D-Bus lib */
757 luaL_register(L, "dbus", awesome_dbus_lib);
758 lua_pop(L, 1); /* luaL_register() leaves the table on stack */
759 #endif
761 /* Export keygrabber lib */
762 luaL_register(L, "keygrabber", awesome_keygrabber_lib);
763 lua_pop(L, 1); /* luaL_register() leaves the table on stack */
765 /* Export mousegrabber lib */
766 luaL_register(L, "mousegrabber", awesome_mousegrabber_lib);
767 lua_pop(L, 1); /* luaL_register() leaves the table on stack */
769 /* Export screen */
770 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
772 /* Export mouse */
773 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
775 /* Export button */
776 button_class_setup(L);
778 /* Export image */
779 image_class_setup(L);
781 /* Export tag */
782 tag_class_setup(L);
784 /* Export wibox */
785 wibox_class_setup(L);
787 /* Export widget */
788 widget_class_setup(L);
790 /* Export client */
791 client_class_setup(L);
793 /* Export keys */
794 key_class_setup(L);
796 /* Export timer */
797 timer_class_setup(L);
799 /* init hooks */
800 globalconf.hooks.manage = LUA_REFNIL;
801 globalconf.hooks.unmanage = LUA_REFNIL;
802 globalconf.hooks.focus = LUA_REFNIL;
803 globalconf.hooks.unfocus = LUA_REFNIL;
804 globalconf.hooks.mouse_enter = LUA_REFNIL;
805 globalconf.hooks.mouse_leave = LUA_REFNIL;
806 globalconf.hooks.clients = LUA_REFNIL;
807 globalconf.hooks.tags = LUA_REFNIL;
808 globalconf.hooks.tagged = LUA_REFNIL;
809 globalconf.hooks.property = LUA_REFNIL;
810 globalconf.hooks.timer = LUA_REFNIL;
811 globalconf.hooks.exit = LUA_REFNIL;
813 /* add Lua search paths */
814 lua_getglobal(L, "package");
815 if (LUA_TTABLE != lua_type(L, 1))
817 warn("package is not a table");
818 return;
820 lua_getfield(L, 1, "path");
821 if (LUA_TSTRING != lua_type(L, 2))
823 warn("package.path is not a string");
824 lua_pop(L, 1);
825 return;
828 /* add XDG_CONFIG_DIR as include path */
829 const char * const *xdgconfigdirs = xdgSearchableConfigDirectories(xdg);
830 for(; *xdgconfigdirs; xdgconfigdirs++)
832 size_t len = a_strlen(*xdgconfigdirs);
833 lua_pushliteral(L, ";");
834 lua_pushlstring(L, *xdgconfigdirs, len);
835 lua_pushliteral(L, "/awesome/?.lua");
836 lua_concat(L, 3);
838 lua_pushliteral(L, ";");
839 lua_pushlstring(L, *xdgconfigdirs, len);
840 lua_pushliteral(L, "/awesome/?/init.lua");
841 lua_concat(L, 3);
843 lua_concat(L, 3); /* concatenate with package.path */
846 /* add Lua lib path (/usr/share/awesome/lib by default) */
847 lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?.lua");
848 lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?/init.lua");
849 lua_concat(L, 3); /* concatenate with package.path */
850 lua_setfield(L, 1, "path"); /* package.path = "concatenated string" */
853 static void
854 luaA_startup_error(const char *err)
856 if (globalconf.startup_errors.len > 0)
857 buffer_addsl(&globalconf.startup_errors, "\n\n");
858 buffer_adds(&globalconf.startup_errors, err);
861 static bool
862 luaA_loadrc(const char *confpath, bool run)
864 if(!luaL_loadfile(globalconf.L, confpath))
866 if(run)
868 /* Set the conffile right now so it can be used inside the
869 * configuration file. */
870 globalconf.conffile = a_strdup(confpath);
871 if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
873 const char *err = lua_tostring(globalconf.L, -1);
874 luaA_startup_error(err);
875 fprintf(stderr, "%s\n", err);
876 /* An error happened, so reset this. */
877 globalconf.conffile = NULL;
879 else
880 return true;
882 else
884 lua_pop(globalconf.L, 1);
885 return true;
888 else
890 const char *err = lua_tostring(globalconf.L, -1);
891 luaA_startup_error(err);
892 fprintf(stderr, "%s\n", err);
895 return false;
898 /** Load a configuration file.
899 * \param xdg An xdg handle to use to get XDG basedir.
900 * \param confpatharg The configuration file to load.
901 * \param run Run the configuration file.
903 bool
904 luaA_parserc(xdgHandle* xdg, const char *confpatharg, bool run)
906 char *confpath = NULL;
907 bool ret = false;
909 /* try to load, return if it's ok */
910 if(confpatharg)
912 if(luaA_loadrc(confpatharg, run))
914 ret = true;
915 goto bailout;
917 else if(!run)
918 goto bailout;
921 confpath = xdgConfigFind("awesome/rc.lua", xdg);
923 char *tmp = confpath;
925 /* confpath is "string1\0string2\0string3\0\0" */
926 while(*tmp)
928 if(luaA_loadrc(tmp, run))
930 ret = true;
931 goto bailout;
933 else if(!run)
934 goto bailout;
935 tmp += a_strlen(tmp) + 1;
938 bailout:
940 p_delete(&confpath);
942 return ret;
945 void
946 luaA_on_timer(EV_P_ ev_timer *w, int revents)
948 if(globalconf.hooks.timer != LUA_REFNIL)
949 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.timer, 0, 0);
953 luaA_class_index_miss_property(lua_State *L, lua_object_t *obj)
955 signal_object_emit(L, &global_signals, "debug::index::miss", 2);
956 return 0;
960 luaA_class_newindex_miss_property(lua_State *L, lua_object_t *obj)
962 signal_object_emit(L, &global_signals, "debug::newindex::miss", 3);
963 return 0;
966 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80