Functionality for deleting a tag using awful.tag.
[awesome.git] / luaa.c
blob54640367d308360aba60ba9170e2d1ab5c5c8fc0
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 __attribute__ ((unused)))
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();
85 a_exec(cmd);
86 return 0;
89 /** Restart awesome.
91 static int
92 luaA_restart(lua_State *L __attribute__ ((unused)))
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 default:
578 return 0;
581 return 1;
584 /** Newindex function for the awesome global table.
585 * \param L The Lua VM state.
586 * \return The number of elements pushed on stack.
588 static int
589 luaA_awesome_newindex(lua_State *L)
591 if(luaA_usemetatable(L, 1, 2))
592 return 1;
594 size_t len;
595 const char *buf = luaL_checklstring(L, 2, &len);
597 switch(a_tokenize(buf, len))
599 case A_TK_FONT:
601 const char *newfont = luaL_checkstring(L, 3);
602 draw_font_delete(&globalconf.font);
603 globalconf.font = draw_font_new(newfont);
604 /* refresh all wiboxes */
605 foreach(wibox, globalconf.wiboxes)
606 (*wibox)->need_update = true;
607 foreach(c, globalconf.clients)
608 if((*c)->titlebar)
609 (*c)->titlebar->need_update = true;
611 break;
612 case A_TK_FG:
613 if((buf = luaL_checklstring(L, 3, &len)))
614 xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.fg, buf, len));
615 break;
616 case A_TK_BG:
617 if((buf = luaL_checklstring(L, 3, &len)))
618 xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.bg, buf, len));
619 break;
620 default:
621 return 0;
624 return 0;
627 /** Add a global signal.
628 * \param L The Lua VM state.
629 * \return The number of elements pushed on stack.
630 * \luastack
631 * \lparam A string with the event name.
632 * \lparam The function to call.
634 static int
635 luaA_awesome_add_signal(lua_State *L)
637 const char *name = luaL_checkstring(L, 1);
638 luaA_checkfunction(L, 2);
639 signal_add(&global_signals, name, luaA_object_ref(L, 2));
640 return 0;
643 /** Remove a global signal.
644 * \param L The Lua VM state.
645 * \return The number of elements pushed on stack.
646 * \luastack
647 * \lparam A string with the event name.
648 * \lparam The function to call.
650 static int
651 luaA_awesome_remove_signal(lua_State *L)
653 const char *name = luaL_checkstring(L, 1);
654 luaA_checkfunction(L, 2);
655 const void *func = lua_topointer(L, 2);
656 signal_remove(&global_signals, name, func);
657 luaA_object_unref(L, (void *) func);
658 return 0;
661 /** Emit a global signal.
662 * \param L The Lua VM state.
663 * \return The number of elements pushed on stack.
664 * \luastack
665 * \lparam A string with the event name.
666 * \lparam The function to call.
668 static int
669 luaA_awesome_emit_signal(lua_State *L)
671 signal_object_emit(L, &global_signals, luaL_checkstring(L, 1), lua_gettop(L) - 1);
672 return 0;
675 static int
676 luaA_panic(lua_State *L)
678 warn("unprotected error in call to Lua API (%s), restarting awesome",
679 lua_tostring(L, -1));
680 awesome_restart();
681 return 0;
684 static int
685 luaA_dofunction_on_error(lua_State *L)
687 /* duplicate string error */
688 lua_pushvalue(L, -1);
689 /* emit error signal */
690 signal_object_emit(L, &global_signals, "debug::error", 1);
692 if(!luaL_dostring(L, "return debug.traceback(\"error while running function\", 3)"))
694 /* Move traceback before error */
695 lua_insert(L, -2);
696 /* Insert sentence */
697 lua_pushliteral(L, "\nerror: ");
698 /* Move it before error */
699 lua_insert(L, -2);
700 lua_concat(L, 3);
702 return 1;
705 /** Initialize the Lua VM
706 * \param xdg An xdg handle to use to get XDG basedir.
708 void
709 luaA_init(xdgHandle* xdg)
711 lua_State *L;
712 static const struct luaL_reg awesome_lib[] =
714 { "quit", luaA_quit },
715 { "exec", luaA_exec },
716 { "spawn", luaA_spawn },
717 { "restart", luaA_restart },
718 { "add_signal", luaA_awesome_add_signal },
719 { "remove_signal", luaA_awesome_remove_signal },
720 { "emit_signal", luaA_awesome_emit_signal },
721 { "__index", luaA_awesome_index },
722 { "__newindex", luaA_awesome_newindex },
723 { NULL, NULL }
726 L = globalconf.L = luaL_newstate();
728 /* Set panic function */
729 lua_atpanic(L, luaA_panic);
731 /* Set error handling function */
732 lualib_dofunction_on_error = luaA_dofunction_on_error;
734 luaL_openlibs(L);
736 luaA_fixups(L);
738 luaA_object_setup(L);
740 /* Export awesome lib */
741 luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
743 /* Export root lib */
744 luaL_register(L, "root", awesome_root_lib);
745 lua_pop(L, 1); /* luaL_register() leaves the table on stack */
747 /* Export hooks lib */
748 luaL_register(L, "hooks", awesome_hooks_lib);
749 lua_pop(L, 1); /* luaL_register() leaves the table on stack */
751 #ifdef WITH_DBUS
752 /* Export D-Bus lib */
753 luaL_register(L, "dbus", awesome_dbus_lib);
754 lua_pop(L, 1); /* luaL_register() leaves the table on stack */
755 #endif
757 /* Export keygrabber lib */
758 luaL_register(L, "keygrabber", awesome_keygrabber_lib);
759 lua_pop(L, 1); /* luaL_register() leaves the table on stack */
761 /* Export mousegrabber lib */
762 luaL_register(L, "mousegrabber", awesome_mousegrabber_lib);
763 lua_pop(L, 1); /* luaL_register() leaves the table on stack */
765 /* Export screen */
766 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
768 /* Export mouse */
769 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
771 /* Export button */
772 button_class_setup(L);
774 /* Export image */
775 image_class_setup(L);
777 /* Export tag */
778 tag_class_setup(L);
780 /* Export wibox */
781 wibox_class_setup(L);
783 /* Export widget */
784 widget_class_setup(L);
786 /* Export client */
787 client_class_setup(L);
789 /* Export keys */
790 key_class_setup(L);
792 /* Export timer */
793 timer_class_setup(L);
795 /* init hooks */
796 globalconf.hooks.manage = LUA_REFNIL;
797 globalconf.hooks.unmanage = LUA_REFNIL;
798 globalconf.hooks.focus = LUA_REFNIL;
799 globalconf.hooks.unfocus = LUA_REFNIL;
800 globalconf.hooks.mouse_enter = LUA_REFNIL;
801 globalconf.hooks.mouse_leave = LUA_REFNIL;
802 globalconf.hooks.clients = LUA_REFNIL;
803 globalconf.hooks.tags = LUA_REFNIL;
804 globalconf.hooks.tagged = LUA_REFNIL;
805 globalconf.hooks.property = LUA_REFNIL;
806 globalconf.hooks.timer = LUA_REFNIL;
807 globalconf.hooks.exit = LUA_REFNIL;
809 /* add Lua search paths */
810 lua_getglobal(L, "package");
811 if (LUA_TTABLE != lua_type(L, 1))
813 warn("package is not a table");
814 return;
816 lua_getfield(L, 1, "path");
817 if (LUA_TSTRING != lua_type(L, 2))
819 warn("package.path is not a string");
820 lua_pop(L, 1);
821 return;
824 /* add XDG_CONFIG_DIR as include path */
825 const char * const *xdgconfigdirs = xdgSearchableConfigDirectories(xdg);
826 for(; *xdgconfigdirs; xdgconfigdirs++)
828 size_t len = a_strlen(*xdgconfigdirs);
829 lua_pushliteral(L, ";");
830 lua_pushlstring(L, *xdgconfigdirs, len);
831 lua_pushliteral(L, "/awesome/?.lua");
832 lua_concat(L, 3);
834 lua_pushliteral(L, ";");
835 lua_pushlstring(L, *xdgconfigdirs, len);
836 lua_pushliteral(L, "/awesome/?/init.lua");
837 lua_concat(L, 3);
839 lua_concat(L, 3); /* concatenate with package.path */
842 /* add Lua lib path (/usr/share/awesome/lib by default) */
843 lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?.lua");
844 lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?/init.lua");
845 lua_concat(L, 3); /* concatenate with package.path */
846 lua_setfield(L, 1, "path"); /* package.path = "concatenated string" */
849 static bool
850 luaA_loadrc(const char *confpath, bool run)
852 if(!luaL_loadfile(globalconf.L, confpath))
854 if(run)
856 if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
857 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
858 else
860 globalconf.conffile = a_strdup(confpath);
861 return true;
864 else
866 lua_pop(globalconf.L, 1);
867 return true;
870 else
871 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
873 return false;
876 /** Load a configuration file.
877 * \param xdg An xdg handle to use to get XDG basedir.
878 * \param confpatharg The configuration file to load.
879 * \param run Run the configuration file.
881 bool
882 luaA_parserc(xdgHandle* xdg, const char *confpatharg, bool run)
884 char *confpath = NULL;
885 bool ret = false;
887 /* try to load, return if it's ok */
888 if(confpatharg)
890 if(luaA_loadrc(confpatharg, run))
892 ret = true;
893 goto bailout;
895 else if(!run)
896 goto bailout;
899 confpath = xdgConfigFind("awesome/rc.lua", xdg);
901 char *tmp = confpath;
903 /* confpath is "string1\0string2\0string3\0\0" */
904 while(*tmp)
906 if(luaA_loadrc(tmp, run))
908 ret = true;
909 goto bailout;
911 else if(!run)
912 goto bailout;
913 tmp += a_strlen(tmp) + 1;
916 bailout:
918 p_delete(&confpath);
920 return ret;
923 void
924 luaA_on_timer(EV_P_ ev_timer *w, int revents)
926 if(globalconf.hooks.timer != LUA_REFNIL)
927 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.timer, 0, 0);
931 luaA_class_index_miss_property(lua_State *L, lua_object_t *obj)
933 signal_object_emit(L, &global_signals, "debug::index::miss", 2);
934 return 0;
938 luaA_class_newindex_miss_property(lua_State *L, lua_object_t *obj)
940 signal_object_emit(L, &global_signals, "debug::newindex::miss", 3);
941 return 0;
944 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80