event: push button state correctly to mouse grabber
[awesome.git] / luaa.c
blob32903f9ee681562a9e3787a423c438832ba5719a
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 "awesome-version-internal.h"
34 #include "ewmh.h"
35 #include "luaa.h"
36 #include "spawn.h"
37 #include "tag.h"
38 #include "client.h"
39 #include "screen.h"
40 #include "event.h"
41 #include "selection.h"
42 #include "window.h"
43 #include "common/xcursor.h"
44 #include "common/buffer.h"
46 #ifdef WITH_DBUS
47 extern const struct luaL_reg awesome_dbus_lib[];
48 #endif
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[];
72 DO_ARRAY(const void *, void, DO_NOTHING)
74 /** Quit awesome.
75 * \param L The Lua VM state.
76 * \return The number of elements pushed on stack.
78 static int
79 luaA_quit(lua_State *L __attribute__ ((unused)))
81 ev_unloop(globalconf.loop, 1);
82 return 0;
85 /** Execute another application, probably a window manager, to replace
86 * awesome.
87 * \param L The Lua VM state.
88 * \return The number of elements pushed on stack.
89 * \luastack
90 * \lparam The command line to execute.
92 static int
93 luaA_exec(lua_State *L)
95 const char *cmd = luaL_checkstring(L, 1);
97 awesome_atexit();
99 a_exec(cmd);
100 return 0;
103 /** Restart awesome.
105 static int
106 luaA_restart(lua_State *L __attribute__ ((unused)))
108 awesome_restart();
109 return 0;
112 static void
113 luaA_openlib(lua_State *L, const char *name,
114 const struct luaL_reg methods[],
115 const struct luaL_reg meta[])
117 luaL_newmetatable(L, name); /* 1 */
118 lua_pushvalue(L, -1); /* dup metatable 2 */
119 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
121 luaL_register(L, NULL, meta); /* 1 */
122 luaL_register(L, name, methods); /* 2 */
123 lua_pushvalue(L, -1); /* dup self as metatable 3 */
124 lua_setmetatable(L, -2); /* set self as metatable 2 */
125 lua_pop(L, 2);
128 /** UTF-8 aware string length computing.
129 * \param L The Lua VM state.
130 * \return The number of elements pushed on stack.
132 static int
133 luaA_mbstrlen(lua_State *L)
135 const char *cmd = luaL_checkstring(L, 1);
136 lua_pushnumber(L, (ssize_t) mbstowcs(NULL, NONULL(cmd), 0));
137 return 1;
140 /** Overload standard Lua next function to use __next key on metatable.
141 * \param L The Lua VM state.
142 * \param The number of elements pushed on stack.
144 static int
145 luaAe_next(lua_State *L)
147 if(luaL_getmetafield(L, 1, "__next"))
149 lua_insert(L, 1);
150 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
151 return lua_gettop(L);
154 luaL_checktype(L, 1, LUA_TTABLE);
155 lua_settop(L, 2);
156 if(lua_next(L, 1))
157 return 2;
158 lua_pushnil(L);
159 return 1;
162 /** Overload lua_next() function by using __next metatable field
163 * to get next elements.
164 * \param L The Lua VM stack.
165 * \param idx The index number of elements in stack.
166 * \return 1 if more elements to come, 0 otherwise.
169 luaA_next(lua_State *L, int idx)
171 if(luaL_getmetafield(L, idx, "__next"))
173 /* if idx is relative, reduce it since we got __next */
174 if(idx < 0) idx--;
175 /* copy table and then move key */
176 lua_pushvalue(L, idx);
177 lua_pushvalue(L, -3);
178 lua_remove(L, -4);
179 lua_pcall(L, 2, 2, 0);
180 /* next returned nil, it's the end */
181 if(lua_isnil(L, -1))
183 /* remove nil */
184 lua_pop(L, 2);
185 return 0;
187 return 1;
189 else if(lua_istable(L, idx))
190 return lua_next(L, idx);
191 /* remove the key */
192 lua_pop(L, 1);
193 return 0;
196 /** Generic pairs function.
197 * \param L The Lua VM state.
198 * \return The number of elements pushed on stack.
200 static int
201 luaA_generic_pairs(lua_State *L)
203 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
204 lua_pushvalue(L, 1); /* state, */
205 lua_pushnil(L); /* and initial value */
206 return 3;
209 /** Overload standard pairs function to use __pairs field of metatables.
210 * \param L The Lua VM state.
211 * \return The number of elements pushed on stack.
213 static int
214 luaAe_pairs(lua_State *L)
216 if(luaL_getmetafield(L, 1, "__pairs"))
218 lua_insert(L, 1);
219 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
220 return lua_gettop(L);
223 luaL_checktype(L, 1, LUA_TTABLE);
224 return luaA_generic_pairs(L);
227 static int
228 luaA_ipairs_aux(lua_State *L)
230 int i = luaL_checkint(L, 2) + 1;
231 luaL_checktype(L, 1, LUA_TTABLE);
232 lua_pushinteger(L, i);
233 lua_rawgeti(L, 1, i);
234 return (lua_isnil(L, -1)) ? 0 : 2;
237 /** Overload standard ipairs function to use __ipairs field of metatables.
238 * \param L The Lua VM state.
239 * \return The number of elements pushed on stack.
241 static int
242 luaAe_ipairs(lua_State *L)
244 if(luaL_getmetafield(L, 1, "__ipairs"))
246 lua_insert(L, 1);
247 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
248 return lua_gettop(L);
251 luaL_checktype(L, 1, LUA_TTABLE);
252 lua_pushvalue(L, lua_upvalueindex(1));
253 lua_pushvalue(L, 1);
254 lua_pushinteger(L, 0); /* and initial value */
255 return 3;
258 /** Enhanced type() function which recognize awesome objects.
259 * \param L The Lua VM state.
260 * \return The number of arguments pushed on the stack.
262 static int
263 luaAe_type(lua_State *L)
265 luaL_checkany(L, 1);
266 #define CHECK_TYPE(type) \
267 do { \
268 if(luaA_toudata(L, 1, #type)) \
270 lua_pushliteral(L, #type); \
271 return 1; \
273 } while(0)
274 CHECK_TYPE(wibox);
275 CHECK_TYPE(client);
276 CHECK_TYPE(image);
277 CHECK_TYPE(key);
278 CHECK_TYPE(button);
279 CHECK_TYPE(tag);
280 CHECK_TYPE(widget);
281 #undef CHECK_TYPE
282 lua_pushstring(L, luaL_typename(L, 1));
283 return 1;
286 /** Replace various standards Lua functions with our own.
287 * \param L The Lua VM state.
289 static void
290 luaA_fixups(lua_State *L)
292 /* export string.wlen */
293 lua_getglobal(L, "string");
294 lua_pushcfunction(L, luaA_mbstrlen);
295 lua_setfield(L, -2, "wlen");
296 lua_pop(L, 1);
297 /* replace next */
298 lua_pushliteral(L, "next");
299 lua_pushcfunction(L, luaAe_next);
300 lua_settable(L, LUA_GLOBALSINDEX);
301 /* replace pairs */
302 lua_pushliteral(L, "pairs");
303 lua_pushcfunction(L, luaAe_next);
304 lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
305 lua_settable(L, LUA_GLOBALSINDEX);
306 /* replace ipairs */
307 lua_pushliteral(L, "ipairs");
308 lua_pushcfunction(L, luaA_ipairs_aux);
309 lua_pushcclosure(L, luaAe_ipairs, 1);
310 lua_settable(L, LUA_GLOBALSINDEX);
311 /* replace type */
312 lua_pushliteral(L, "type");
313 lua_pushcfunction(L, luaAe_type);
314 lua_settable(L, LUA_GLOBALSINDEX);
315 /* set selection */
316 lua_pushliteral(L, "selection");
317 lua_pushcfunction(L, luaA_selection_get);
318 lua_settable(L, LUA_GLOBALSINDEX);
321 /** __next function for wtable objects.
322 * \param L The Lua VM state.
323 * \return The number of elements pushed on stack.
325 static int
326 luaA_wtable_next(lua_State *L)
328 /* upvalue 1 is content table */
329 if(lua_next(L, lua_upvalueindex(1)))
330 return 2;
331 lua_pushnil(L);
332 return 1;
335 /** __ipairs function for wtable objects.
336 * \param L The Lua VM state.
337 * \return The number of elements pushed on stack.
339 static int
340 luaA_wtable_ipairs(lua_State *L)
342 /* push ipairs_aux */
343 lua_pushvalue(L, lua_upvalueindex(2));
344 /* push content table */
345 lua_pushvalue(L, lua_upvalueindex(1));
346 lua_pushinteger(L, 0); /* and initial value */
347 return 3;
350 /** Index function of wtable objects.
351 * \param L The Lua VM state.
352 * \return The number of elements pushed on stack.
354 static int
355 luaA_wtable_index(lua_State *L)
357 size_t len;
358 const char *buf;
360 lua_pushvalue(L, 2);
361 /* check for size, waiting lua 5.2 and __len on tables */
362 if((buf = lua_tolstring(L, -1, &len)))
363 if(a_tokenize(buf, len) == A_TK_LEN)
365 lua_pushnumber(L, lua_objlen(L, lua_upvalueindex(1)));
366 return 1;
368 lua_pop(L, 1);
370 /* upvalue 1 is content table */
371 lua_rawget(L, lua_upvalueindex(1));
372 return 1;
375 /** Newndex function of wtable objects.
376 * \param L The Lua VM state.
377 * \return The number of elements pushed on stack.
379 static int
380 luaA_wtable_newindex(lua_State *L)
382 bool invalid = false;
384 /* push key on top */
385 lua_pushvalue(L, 2);
386 /* get current key value in content table */
387 lua_rawget(L, lua_upvalueindex(1));
388 /* if value is a widget, notify change */
389 if(lua_istable(L, -1) || luaA_toudata(L, -1, "widget"))
390 invalid = true;
392 lua_pop(L, 1); /* remove value */
394 /* if new value is a widget or a table */
395 if(lua_istable(L, 3))
397 luaA_table2wtable(L);
398 invalid = true;
400 else if(!invalid && luaA_toudata(L, 3, "widget"))
401 invalid = true;
403 /* upvalue 1 is content table */
404 lua_rawset(L, lua_upvalueindex(1));
406 if(invalid)
407 luaA_wibox_invalidate_byitem(L, lua_topointer(L, 1));
409 return 0;
412 /** Convert the top element of the stack to a proxied wtable.
413 * \param L The Lua VM state.
415 void
416 luaA_table2wtable(lua_State *L)
418 if(!lua_istable(L, -1))
419 return;
421 lua_newtable(L); /* create *real* content table */
422 lua_createtable(L, 0, 5); /* metatable */
423 lua_pushvalue(L, -2); /* copy content table */
424 lua_pushcfunction(L, luaA_ipairs_aux); /* push ipairs aux */
425 lua_pushcclosure(L, luaA_wtable_ipairs, 2);
426 lua_pushvalue(L, -3); /* copy content table */
427 lua_pushcclosure(L, luaA_wtable_next, 1); /* __next has the content table as upvalue */
428 lua_pushvalue(L, -4); /* copy content table */
429 lua_pushcclosure(L, luaA_wtable_index, 1); /* __index has the content table as upvalue */
430 lua_pushvalue(L, -5); /* copy content table */
431 lua_pushcclosure(L, luaA_wtable_newindex, 1); /* __newindex has the content table as upvalue */
432 /* set metatable field with just pushed closure */
433 lua_setfield(L, -5, "__newindex");
434 lua_setfield(L, -4, "__index");
435 lua_setfield(L, -3, "__next");
436 lua_setfield(L, -2, "__ipairs");
437 /* set metatable impossible to touch */
438 lua_pushliteral(L, "wtable");
439 lua_setfield(L, -2, "__metatable");
440 /* set new metatable on original table */
441 lua_setmetatable(L, -3);
443 /* initial key */
444 lua_pushnil(L);
445 /* go through original table */
446 while(lua_next(L, -3))
448 /* if convert value to wtable */
449 luaA_table2wtable(L);
450 /* copy key */
451 lua_pushvalue(L, -2);
452 /* move key before value */
453 lua_insert(L, -2);
454 /* set same value in content table */
455 lua_rawset(L, -4);
456 /* copy key */
457 lua_pushvalue(L, -1);
458 /* push the new value :-> */
459 lua_pushnil(L);
460 /* set orig[k] = nil */
461 lua_rawset(L, -5);
463 /* remove content table */
464 lua_pop(L, 1);
467 /** Look for an item: table, function, etc.
468 * \param L The Lua VM state.
469 * \param item The pointer item.
471 bool
472 luaA_hasitem(lua_State *L, const void *item)
474 lua_pushnil(L);
475 while(luaA_next(L, -2))
477 if(lua_topointer(L, -1) == item)
479 /* remove value and key */
480 lua_pop(L, 2);
481 return true;
483 if(lua_istable(L, -1))
484 if(luaA_hasitem(L, item))
486 /* remove key and value */
487 lua_pop(L, 2);
488 return true;
490 /* remove value */
491 lua_pop(L, 1);
493 return false;
496 /** Browse a table pushed on top of the index, and put all its table and
497 * sub-table into an array.
498 * \param L The Lua VM state.
499 * \param elems The elements array.
500 * \return False if we encounter an elements already in list.
502 static bool
503 luaA_isloop_check(lua_State *L, void_array_t *elems)
505 if(lua_istable(L, -1))
507 const void *object = lua_topointer(L, -1);
509 /* Check that the object table is not already in the list */
510 for(int i = 0; i < elems->len; i++)
511 if(elems->tab[i] == object)
512 return false;
514 /* push the table in the elements list */
515 void_array_append(elems, object);
517 /* look every object in the "table" */
518 lua_pushnil(L);
519 while(luaA_next(L, -2))
521 if(!luaA_isloop_check(L, elems))
523 /* remove key and value */
524 lua_pop(L, 2);
525 return false;
527 /* remove value, keep key for next iteration */
528 lua_pop(L, 1);
531 return true;
534 /** Check if a table is a loop. When using tables as direct acyclic digram,
535 * this is useful.
536 * \param L The Lua VM state.
537 * \param idx The index of the table in the stack
538 * \return True if the table loops.
540 bool
541 luaA_isloop(lua_State *L, int idx)
543 /* elems is an elements array that we will fill with all array we
544 * encounter while browsing the tables */
545 void_array_t elems;
547 void_array_init(&elems);
549 /* push table on top */
550 lua_pushvalue(L, idx);
552 bool ret = luaA_isloop_check(L, &elems);
554 /* remove pushed table */
555 lua_pop(L, 1);
557 void_array_wipe(&elems);
559 return !ret;
562 /** awesome global table.
563 * \param L The Lua VM state.
564 * \return The number of elements pushed on stack.
565 * \luastack
566 * \lfield font The default font.
567 * \lfield conffile The configuration file which has been loaded.
569 static int
570 luaA_awesome_index(lua_State *L)
572 if(luaA_usemetatable(L, 1, 2))
573 return 1;
575 size_t len;
576 const char *buf = luaL_checklstring(L, 2, &len);
578 switch(a_tokenize(buf, len))
580 case A_TK_FONT:
582 char *font = pango_font_description_to_string(globalconf.font->desc);
583 lua_pushstring(L, font);
584 g_free(font);
586 break;
587 case A_TK_CONFFILE:
588 lua_pushstring(L, globalconf.conffile);
589 break;
590 case A_TK_FG:
591 luaA_pushxcolor(L, &globalconf.colors.fg);
592 break;
593 case A_TK_BG:
594 luaA_pushxcolor(L, &globalconf.colors.bg);
595 break;
596 case A_TK_VERSION:
597 lua_pushliteral(L, AWESOME_VERSION);
598 break;
599 case A_TK_RELEASE:
600 lua_pushliteral(L, AWESOME_RELEASE);
601 break;
602 default:
603 return 0;
606 return 1;
609 /** Newindex function for the awesome global table.
610 * \param L The Lua VM state.
611 * \return The number of elements pushed on stack.
613 static int
614 luaA_awesome_newindex(lua_State *L)
616 if(luaA_usemetatable(L, 1, 2))
617 return 1;
619 size_t len;
620 const char *buf = luaL_checklstring(L, 2, &len);
622 switch(a_tokenize(buf, len))
624 case A_TK_FONT:
626 const char *newfont = luaL_checkstring(L, 3);
627 draw_font_delete(&globalconf.font);
628 globalconf.font = draw_font_new(newfont);
629 /* refresh all wiboxes */
630 foreach(screen, globalconf.screens)
631 foreach(wibox, screen->wiboxes)
632 (*wibox)->need_update = true;
633 foreach(c, globalconf.clients)
634 if((*c)->titlebar)
635 (*c)->titlebar->need_update = true;
637 break;
638 case A_TK_FG:
639 if((buf = luaL_checklstring(L, 3, &len)))
640 xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.fg, buf, len));
641 break;
642 case A_TK_BG:
643 if((buf = luaL_checklstring(L, 3, &len)))
644 xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.bg, buf, len));
645 break;
646 default:
647 return 0;
650 return 0;
653 /** Initialize the Lua VM
654 * \param xdg An xdg handle to use to get XDG basedir.
656 void
657 luaA_init(xdgHandle* xdg)
659 lua_State *L;
660 static const struct luaL_reg awesome_lib[] =
662 { "quit", luaA_quit },
663 { "exec", luaA_exec },
664 { "spawn", luaA_spawn },
665 { "restart", luaA_restart },
666 { "__index", luaA_awesome_index },
667 { "__newindex", luaA_awesome_newindex },
668 { NULL, NULL }
671 L = globalconf.L = luaL_newstate();
673 luaL_openlibs(L);
675 luaA_fixups(L);
677 /* Export awesome lib */
678 luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
680 /* Export root lib */
681 luaL_register(L, "root", awesome_root_lib);
683 /* Export hooks lib */
684 luaL_register(L, "hooks", awesome_hooks_lib);
686 #ifdef WITH_DBUS
687 /* Export D-Bus lib */
688 luaL_register(L, "dbus", awesome_dbus_lib);
689 #endif
691 /* Export keygrabber lib */
692 luaL_register(L, "keygrabber", awesome_keygrabber_lib);
694 /* Export mousegrabber lib */
695 luaL_register(L, "mousegrabber", awesome_mousegrabber_lib);
697 /* Export screen */
698 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
700 /* Export mouse */
701 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
703 /* Export button */
704 luaA_openlib(L, "button", awesome_button_methods, awesome_button_meta);
706 /* Export image */
707 luaA_openlib(L, "image", awesome_image_methods, awesome_image_meta);
709 /* Export tag */
710 luaA_openlib(L, "tag", awesome_tag_methods, awesome_tag_meta);
712 /* Export wibox */
713 luaA_openlib(L, "wibox", awesome_wibox_methods, awesome_wibox_meta);
715 /* Export widget */
716 luaA_openlib(L, "widget", awesome_widget_methods, awesome_widget_meta);
718 /* Export client */
719 luaA_openlib(L, "client", awesome_client_methods, awesome_client_meta);
721 /* Export keys */
722 luaA_openlib(L, "key", awesome_key_methods, awesome_key_meta);
724 /* init hooks */
725 globalconf.hooks.manage = LUA_REFNIL;
726 globalconf.hooks.unmanage = LUA_REFNIL;
727 globalconf.hooks.focus = LUA_REFNIL;
728 globalconf.hooks.unfocus = LUA_REFNIL;
729 globalconf.hooks.mouse_enter = LUA_REFNIL;
730 globalconf.hooks.mouse_leave = LUA_REFNIL;
731 globalconf.hooks.arrange = LUA_REFNIL;
732 globalconf.hooks.clients = LUA_REFNIL;
733 globalconf.hooks.tags = LUA_REFNIL;
734 globalconf.hooks.tagged = LUA_REFNIL;
735 globalconf.hooks.property = LUA_REFNIL;
736 globalconf.hooks.startup_notification = LUA_REFNIL;
737 globalconf.hooks.timer = LUA_REFNIL;
738 #ifdef WITH_DBUS
739 globalconf.hooks.dbus = LUA_REFNIL;
740 #endif
742 /* add Lua lib path (/usr/share/awesome/lib by default) */
743 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?.lua\"");
744 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?/init.lua\"");
746 /* add XDG_CONFIG_DIR as include path */
747 const char * const *xdgconfigdirs = xdgSearchableConfigDirectories(xdg);
748 buffer_t buf;
749 int prev_len = 0;
750 buffer_init(&buf);
751 #define A_LUAA_FIRST_STRING "package.path = package.path .. \";"
752 #define A_LUAA_SECOND_STRING "/awesome/?.lua;"
753 #define A_LUAA_FULL_STRING A_LUAA_FIRST_STRING A_LUAA_SECOND_STRING "/awesome/?/init.lua\""
754 buffer_addsl(&buf, A_LUAA_FULL_STRING);
755 for(; *xdgconfigdirs; xdgconfigdirs++)
757 size_t len = a_strlen(*xdgconfigdirs);
758 buffer_splice(&buf, sizeof(A_LUAA_FIRST_STRING) - 1, prev_len, *xdgconfigdirs, len);
759 buffer_splice(&buf,
760 sizeof(A_LUAA_FIRST_STRING) - 1
761 + len
762 + sizeof(A_LUAA_SECOND_STRING) - 1,
763 prev_len, *xdgconfigdirs, len);
764 luaA_dostring(L, buf.s);
765 prev_len = len;
767 #undef A_LUAA_FIRST_STRING
768 #undef A_LUAA_SECOND_STRING
769 #undef A_LUAA_FULL_STRING
770 buffer_wipe(&buf);
773 static bool
774 luaA_loadrc(const char *confpath, bool run)
776 if(!luaL_loadfile(globalconf.L, confpath))
778 if(run)
780 if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
781 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
782 else
784 globalconf.conffile = a_strdup(confpath);
785 return true;
788 else
789 lua_pop(globalconf.L, 1);
790 return true;
792 else
793 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
795 return false;
798 /** Load a configuration file.
799 * \param xdg An xdg handle to use to get XDG basedir.
800 * \param confpatharg The configuration file to load.
801 * \param run Run the configuration file.
803 bool
804 luaA_parserc(xdgHandle* xdg, const char *confpatharg, bool run)
806 char *confpath = NULL;
807 bool ret = false;
809 /* try to load, return if it's ok */
810 if(confpatharg)
812 if(luaA_loadrc(confpatharg, run))
814 ret = true;
815 goto bailout;
817 else if(!run)
818 goto bailout;
821 confpath = xdgConfigFind("awesome/rc.lua", xdg);
823 char *tmp = confpath;
825 /* confpath is "string1\0string2\0string3\0\0" */
828 if(luaA_loadrc(tmp, run))
830 ret = true;
831 goto bailout;
833 else if(!run)
834 goto bailout;
835 tmp += a_strlen(tmp) + 1;
836 } while(*tmp != 0);
838 bailout:
840 p_delete(&confpath);
842 return ret;
845 void
846 luaA_on_timer(EV_P_ ev_timer *w, int revents)
848 if(globalconf.hooks.timer != LUA_REFNIL)
849 luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0, 0);
850 awesome_refresh();
853 /** Push a color as a string onto the stack
854 * \param L The Lua VM state.
855 * \param c The color to push.
856 * \return The number of elements pushed on stack.
859 luaA_pushxcolor(lua_State *L, const xcolor_t *c)
861 uint8_t r = (unsigned)c->red * 0xff / 0xffff;
862 uint8_t g = (unsigned)c->green * 0xff / 0xffff;
863 uint8_t b = (unsigned)c->blue * 0xff / 0xffff;
864 uint8_t a = (unsigned)c->alpha * 0xff / 0xffff;
865 char s[10];
866 int len;
867 /* do not print alpha if it's full */
868 if(a == 0xff)
869 len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
870 else
871 len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
872 lua_pushlstring(L, s, len);
873 return 1;
876 /** Push a color as a string onto the stack
877 * \param L The Lua VM state.
878 * \param c The color to push.
879 * \return The number of elements pushed on stack.
882 luaA_pushcolor(lua_State *L, const color_t *c)
884 uint8_t r = c->red;
885 uint8_t g = c->green;
886 uint8_t b = c->blue;
887 uint8_t a = c->alpha;
888 char s[10];
889 int len;
890 /* do not print alpha if it's full */
891 if(a == 0xff)
892 len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
893 else
894 len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
895 lua_pushlstring(L, s, len);
896 return 1;
899 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80