widgets: use integer instead of int8_t
[awesome.git] / luaa.c
blob6a83ec062430660858f190c3c666fbbfaf9b9421
1 /*
2 * lua.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 #include "common/util.h"
24 #include <errno.h>
25 #include <sys/socket.h>
26 #include <sys/un.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31 #include <fcntl.h>
33 #include <ev.h>
35 #include <lua.h>
36 #include <lauxlib.h>
37 #include <lualib.h>
39 #include <xcb/xcb.h>
41 #include "awesome.h"
42 #include "awesome-version-internal.h"
43 #include "ewmh.h"
44 #include "config.h"
45 #include "luaa.h"
46 #include "tag.h"
47 #include "client.h"
48 #include "screen.h"
49 #include "event.h"
50 #include "mouse.h"
51 #include "selection.h"
52 #include "common/socket.h"
53 #include "common/buffer.h"
55 extern awesome_t globalconf;
57 extern const struct luaL_reg awesome_hooks_lib[];
58 extern const struct luaL_reg awesome_dbus_lib[];
59 extern const struct luaL_reg awesome_keygrabber_lib[];
60 extern const struct luaL_reg awesome_mousegrabber_lib[];
61 extern const struct luaL_reg awesome_button_methods[];
62 extern const struct luaL_reg awesome_button_meta[];
63 extern const struct luaL_reg awesome_image_methods[];
64 extern const struct luaL_reg awesome_image_meta[];
65 extern const struct luaL_reg awesome_mouse_methods[];
66 extern const struct luaL_reg awesome_mouse_meta[];
67 extern const struct luaL_reg awesome_screen_methods[];
68 extern const struct luaL_reg awesome_screen_meta[];
69 extern const struct luaL_reg awesome_client_methods[];
70 extern const struct luaL_reg awesome_client_meta[];
71 extern const struct luaL_reg awesome_tag_methods[];
72 extern const struct luaL_reg awesome_tag_meta[];
73 extern const struct luaL_reg awesome_widget_methods[];
74 extern const struct luaL_reg awesome_widget_meta[];
75 extern const struct luaL_reg awesome_wibox_methods[];
76 extern const struct luaL_reg awesome_wibox_meta[];
77 extern const struct luaL_reg awesome_key_methods[];
78 extern const struct luaL_reg awesome_key_meta[];
79 extern const struct luaL_reg awesome_keybinding_methods[];
81 static struct sockaddr_un *addr;
82 static ev_io csio = { .fd = -1 };
84 #define XDG_CONFIG_HOME_DEFAULT "/.config"
86 /** Get or set global key bindings.
87 * This binding will be available when you'll press keys on root window.
88 * \param L The Lua VM state.
89 * \return The number of element pushed on stack.
90 * \luastack
91 * \lvalue A client.
92 * \lparam An array of key bindings objects, or nothing.
93 * \return The array of key bindings objects of this client.
95 static int
96 luaA_root_keys(lua_State *L)
98 if(lua_gettop(L) == 1)
100 luaA_key_array_set(L, 1, &globalconf.keys);
102 int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
104 for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
106 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
107 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
108 window_grabkeys(s->root, &globalconf.keys);
112 return luaA_key_array_get(L, &globalconf.keys);
115 /** Get or set global mouse bindings.
116 * This binding will be available when you'll click on root window.
117 * \param L The Lua VM state.
118 * \return The number of element pushed on stack.
119 * \luastack
120 * \lvalue A client.
121 * \lparam An array of mouse button bindings objects, or nothing.
122 * \return The array of mouse button bindings objects.
124 static int
125 luaA_root_buttons(lua_State *L)
127 if(lua_gettop(L) == 1)
128 luaA_button_array_set(L, 1, &globalconf.buttons);
130 return luaA_button_array_get(L, &globalconf.buttons);
133 /** Get or set global mouse bindings (DEPRECATED).
134 * This binding will be available when you'll click on root window.
135 * \param L The Lua VM state.
136 * \return The number of element pushed on stack.
137 * \luastack
138 * \lvalue A client.
139 * \lparam An array of mouse button bindings objects, or nothing.
140 * \return The array of mouse button bindings objects.
142 static int
143 luaA_buttons(lua_State *L)
145 luaA_deprecate(L, "root.buttons");
146 return luaA_root_buttons(L);
149 /** Quit awesome.
150 * \param L The Lua VM state.
151 * \return The number of elements pushed on stack.
153 static int
154 luaA_quit(lua_State *L __attribute__ ((unused)))
156 ev_unloop(globalconf.loop, 1);
157 return 0;
160 /** Execute another application, probably a window manager, to replace
161 * awesome.
162 * \param L The Lua VM state.
163 * \return The number of elements pushed on stack.
164 * \luastack
165 * \lparam The command line to execute.
167 static int
168 luaA_exec(lua_State *L)
170 const char *cmd = luaL_checkstring(L, 1);
172 awesome_atexit();
174 a_exec(cmd);
175 return 0;
178 /** Restart awesome.
180 static int
181 luaA_restart(lua_State *L __attribute__ ((unused)))
183 awesome_restart();
184 return 0;
187 static void
188 luaA_openlib(lua_State *L, const char *name,
189 const struct luaL_reg methods[],
190 const struct luaL_reg meta[])
192 luaL_newmetatable(L, name); /* 1 */
193 lua_pushvalue(L, -1); /* dup metatable 2 */
194 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
196 luaL_register(L, NULL, meta); /* 1 */
197 luaL_register(L, name, methods); /* 2 */
198 lua_pushvalue(L, -1); /* dup self as metatable 3 */
199 lua_setmetatable(L, -2); /* set self as metatable 2 */
200 lua_pop(L, 2);
203 /** UTF-8 aware string length computing.
204 * \param L The Lua VM state.
205 * \return The number of elements pushed on stack.
207 static int
208 luaA_mbstrlen(lua_State *L)
210 const char *cmd = luaL_checkstring(L, 1);
211 lua_pushnumber(L, mbstowcs(NULL, NONULL(cmd), 0));
212 return 1;
215 /** Overload standard Lua next function to use __next key on metatable.
216 * \param L The Lua VM state.
217 * \param The number of elements pushed on stack.
219 static int
220 luaAe_next(lua_State *L)
222 if(luaL_getmetafield(L, 1, "__next"))
224 lua_insert(L, 1);
225 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
226 return lua_gettop(L);
229 luaL_checktype(L, 1, LUA_TTABLE);
230 lua_settop(L, 2);
231 if(lua_next(L, 1))
232 return 2;
233 lua_pushnil(L);
234 return 1;
237 /** Overload lua_next() function by using __next metatable field
238 * to get next elements.
239 * \param L The Lua VM stack.
240 * \param idx The index number of elements in stack.
241 * \return 1 if more elements to come, 0 otherwise.
244 luaA_next(lua_State *L, int idx)
246 if(luaL_getmetafield(L, idx, "__next"))
248 /* if idx is relative, reduce it since we got __next */
249 if(idx < 0) idx--;
250 /* copy table and then move key */
251 lua_pushvalue(L, idx);
252 lua_pushvalue(L, -3);
253 lua_remove(L, -4);
254 lua_pcall(L, 2, 2, 0);
255 /* next returned nil, it's the end */
256 if(lua_isnil(L, -1))
258 /* remove nil */
259 lua_pop(L, 2);
260 return 0;
262 return 1;
264 else if(lua_istable(L, idx))
265 return lua_next(L, idx);
266 /* remove the key */
267 lua_pop(L, 1);
268 return 0;
271 /** Generic pairs function.
272 * \param L The Lua VM state.
273 * \return The number of elements pushed on stack.
275 static int
276 luaA_generic_pairs(lua_State *L)
278 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
279 lua_pushvalue(L, 1); /* state, */
280 lua_pushnil(L); /* and initial value */
281 return 3;
284 /** Overload standard pairs function to use __pairs field of metatables.
285 * \param L The Lua VM state.
286 * \return The number of elements pushed on stack.
288 static int
289 luaAe_pairs(lua_State *L)
291 if(luaL_getmetafield(L, 1, "__pairs"))
293 lua_insert(L, 1);
294 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
295 return lua_gettop(L);
298 luaL_checktype(L, 1, LUA_TTABLE);
299 return luaA_generic_pairs(L);
302 static int
303 luaA_ipairs_aux(lua_State *L)
305 int i = luaL_checkint(L, 2) + 1;
306 luaL_checktype(L, 1, LUA_TTABLE);
307 lua_pushinteger(L, i);
308 lua_rawgeti(L, 1, i);
309 return (lua_isnil(L, -1)) ? 0 : 2;
312 /** Overload standard ipairs function to use __ipairs field of metatables.
313 * \param L The Lua VM state.
314 * \return The number of elements pushed on stack.
316 static int
317 luaAe_ipairs(lua_State *L)
319 if(luaL_getmetafield(L, 1, "__ipairs"))
321 lua_insert(L, 1);
322 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
323 return lua_gettop(L);
326 luaL_checktype(L, 1, LUA_TTABLE);
327 lua_pushvalue(L, lua_upvalueindex(1));
328 lua_pushvalue(L, 1);
329 lua_pushinteger(L, 0); /* and initial value */
330 return 3;
333 /** Enhanced type() function which recognize awesome objects.
334 * \param L The Lua VM state.
335 * \return The number of arguments pushed on the stack.
337 static int
338 luaAe_type(lua_State *L)
340 luaL_checkany(L, 1);
341 #define CHECK_TYPE(type) \
342 do { \
343 if(luaA_toudata(L, 1, #type)) \
345 lua_pushliteral(L, #type); \
346 return 1; \
348 } while(0)
349 CHECK_TYPE(wibox);
350 CHECK_TYPE(client);
351 CHECK_TYPE(image);
352 CHECK_TYPE(key);
353 CHECK_TYPE(button);
354 CHECK_TYPE(tag);
355 CHECK_TYPE(widget);
356 #undef CHECK_TYPE
357 lua_pushstring(L, luaL_typename(L, 1));
358 return 1;
361 /** Replace various standards Lua functions with our own.
362 * \param L The Lua VM state.
364 static void
365 luaA_fixups(lua_State *L)
367 /* export string.wlen */
368 lua_getglobal(L, "string");
369 lua_pushcfunction(L, luaA_mbstrlen);
370 lua_setfield(L, -2, "wlen");
371 lua_pop(L, 1);
372 /* replace next */
373 lua_pushliteral(L, "next");
374 lua_pushcfunction(L, luaAe_next);
375 lua_settable(L, LUA_GLOBALSINDEX);
376 /* replace pairs */
377 lua_pushliteral(L, "pairs");
378 lua_pushcfunction(L, luaAe_next);
379 lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
380 lua_settable(L, LUA_GLOBALSINDEX);
381 /* replace ipairs */
382 lua_pushliteral(L, "ipairs");
383 lua_pushcfunction(L, luaA_ipairs_aux);
384 lua_pushcclosure(L, luaAe_ipairs, 1);
385 lua_settable(L, LUA_GLOBALSINDEX);
386 /* replace type */
387 lua_pushliteral(L, "type");
388 lua_pushcfunction(L, luaAe_type);
389 lua_settable(L, LUA_GLOBALSINDEX);
390 /* set selection */
391 lua_pushliteral(L, "selection");
392 lua_pushcfunction(L, luaA_selection_get);
393 lua_settable(L, LUA_GLOBALSINDEX);
396 /** __next function for wtable objects.
397 * \param L The Lua VM state.
398 * \return The number of elements pushed on stack.
400 static int
401 luaA_wtable_next(lua_State *L)
403 /* upvalue 1 is content table */
404 if(lua_next(L, lua_upvalueindex(1)))
405 return 2;
406 lua_pushnil(L);
407 return 1;
410 /** __ipairs function for wtable objects.
411 * \param L The Lua VM state.
412 * \return The number of elements pushed on stack.
414 static int
415 luaA_wtable_ipairs(lua_State *L)
417 /* push ipairs_aux */
418 lua_pushvalue(L, lua_upvalueindex(2));
419 /* push content table */
420 lua_pushvalue(L, lua_upvalueindex(1));
421 lua_pushinteger(L, 0); /* and initial value */
422 return 3;
425 /** Index function of wtable objects.
426 * \param L The Lua VM state.
427 * \return The number of elements pushed on stack.
429 static int
430 luaA_wtable_index(lua_State *L)
432 size_t len;
433 const char *buf;
435 lua_pushvalue(L, 2);
436 /* check for size, waiting lua 5.2 and __len on tables */
437 if((buf = lua_tolstring(L, -1, &len)))
438 if(a_tokenize(buf, len) == A_TK_LEN)
440 lua_pushnumber(L, lua_objlen(L, lua_upvalueindex(1)));
441 return 1;
443 lua_pop(L, 1);
445 /* upvalue 1 is content table */
446 lua_rawget(L, lua_upvalueindex(1));
447 return 1;
450 /** Newndex function of wtable objects.
451 * \param L The Lua VM state.
452 * \return The number of elements pushed on stack.
454 static int
455 luaA_wtable_newindex(lua_State *L)
457 bool invalid = false;
459 /* push key on top */
460 lua_pushvalue(L, 2);
461 /* get current key value in content table */
462 lua_rawget(L, lua_upvalueindex(1));
463 /* if value is a widget, notify change */
464 if(lua_istable(L, -1) || luaA_toudata(L, -1, "widget"))
465 invalid = true;
467 lua_pop(L, 1); /* remove value */
469 /* if new value is a widget or a table */
470 if(lua_istable(L, 3))
472 luaA_table2wtable(L);
473 invalid = true;
475 else if(!invalid && luaA_toudata(L, 3, "widget"))
476 invalid = true;
478 /* upvalue 1 is content table */
479 lua_rawset(L, lua_upvalueindex(1));
481 if(invalid)
482 luaA_wibox_invalidate_byitem(L, lua_topointer(L, 1));
484 return 0;
487 /** Convert the top element of the stack to a proxied wtable.
488 * \param L The Lua VM state.
490 void
491 luaA_table2wtable(lua_State *L)
493 if(!lua_istable(L, -1))
494 return;
496 lua_newtable(L); /* create *real* content table */
497 lua_newtable(L); /* metatable */
498 lua_pushvalue(L, -2); /* copy content table */
499 lua_pushcfunction(L, luaA_ipairs_aux); /* push ipairs aux */
500 lua_pushcclosure(L, luaA_wtable_ipairs, 2);
501 lua_pushvalue(L, -3); /* copy content table */
502 lua_pushcclosure(L, luaA_wtable_next, 1); /* __next has the content table as upvalue */
503 lua_pushvalue(L, -4); /* copy content table */
504 lua_pushcclosure(L, luaA_wtable_index, 1); /* __index has the content table as upvalue */
505 lua_pushvalue(L, -5); /* copy content table */
506 lua_pushcclosure(L, luaA_wtable_newindex, 1); /* __newindex has the content table as upvalue */
507 /* set metatable field with just pushed closure */
508 lua_setfield(L, -5, "__newindex");
509 lua_setfield(L, -4, "__index");
510 lua_setfield(L, -3, "__next");
511 lua_setfield(L, -2, "__ipairs");
512 /* set metatable impossible to touch */
513 lua_pushliteral(L, "wtable");
514 lua_setfield(L, -2, "__metatable");
515 /* set new metatable on original table */
516 lua_setmetatable(L, -3);
518 /* initial key */
519 lua_pushnil(L);
520 /* go through original table */
521 while(lua_next(L, -3))
523 /* if convert value to wtable */
524 luaA_table2wtable(L);
525 /* copy key */
526 lua_pushvalue(L, -2);
527 /* move key before value */
528 lua_insert(L, -2);
529 /* set same value in content table */
530 lua_rawset(L, -4);
531 /* copy key */
532 lua_pushvalue(L, -1);
533 /* push the new value :-> */
534 lua_pushnil(L);
535 /* set orig[k] = nil */
536 lua_rawset(L, -5);
538 /* remove content table */
539 lua_pop(L, 1);
542 /** Look for an item: table, function, etc.
543 * \param L The Lua VM state.
544 * \param item The pointer item.
546 bool
547 luaA_hasitem(lua_State *L, const void *item)
549 lua_pushnil(L);
550 while(luaA_next(L, -2))
552 if(lua_topointer(L, -1) == item)
554 /* remove value and key */
555 lua_pop(L, 2);
556 return true;
558 if(lua_istable(L, -1))
559 if(luaA_hasitem(L, item))
561 /* remove key and value */
562 lua_pop(L, 2);
563 return true;
565 /* remove value */
566 lua_pop(L, 1);
568 return false;
571 /** Check if a table is a loop. When using table as direct acyclic digram,
572 * this is useful.
573 * \param L The Lua VM state.
574 * \param idx The index of the table in the stack
575 * \return True if the table loops.
577 bool
578 luaA_isloop(lua_State *L, int idx)
580 if(lua_istable(L, idx))
582 lua_pushvalue(L, idx); /* push table on top */
583 if(luaA_hasitem(L, lua_topointer(L, -1)))
585 lua_pop(L, 1); /* remove pushed table */
586 return true;
588 lua_pushnil(L);
589 while(luaA_next(L, -2))
591 /* check for recursivity */
592 if(luaA_isloop(L, -1))
594 lua_pop(L, 2); /* remove key and value */
595 return true;
597 lua_pop(L, 1); /* remove value */
599 lua_pop(L, 1); /* remove pushed table */
601 return false;
604 /** Object table.
605 * This table can use safely object as key.
606 * \param L The Lua VM state.
607 * \return The number of elements pushed on stack.
610 luaA_otable_index(lua_State *L)
612 void **obj, **v;
614 if((obj = lua_touserdata(L, 2)))
616 /* begins at nil */
617 lua_pushnil(L);
618 while(lua_next(L, 1))
620 /* check the key against the key if the key is a userdata,
621 * otherwise check it again the value. */
622 if((v = lua_touserdata(L, -2))
623 && *v == *obj)
624 /* return value */
625 return 1;
626 else if((v = lua_touserdata(L, -1))
627 && *v == *obj)
629 /* return key */
630 lua_pop(L, 1);
631 return 1;
633 /* removes 'value'; keeps 'key' for next iteration */
634 lua_pop(L, 1);
636 return 0;
639 lua_rawget(L, 1);
640 return 1;
643 /** Object table.
644 * This table can use safely object as key.
645 * \param L The Lua VM state.
646 * \return The number of elements pushed on stack.
648 static int
649 luaA_otable_newindex(lua_State *L)
651 void **obj, **v;
653 if((obj = lua_touserdata(L, 2)))
655 /* begins at nil */
656 lua_pushnil(L);
657 while(lua_next(L, 1))
659 if((v = lua_touserdata(L, -2))
660 && *v == *obj)
662 /* remove value */
663 lua_pop(L, 1);
664 /* push new value on top */
665 lua_pushvalue(L, 3);
666 /* set in table key = value */
667 lua_rawset(L, 1);
668 return 0;
670 /* removes 'value'; keeps 'key' for next iteration */
671 lua_pop(L, 1);
675 lua_rawset(L, 1);
677 return 0;
680 /** Spawn a program.
681 * This function is multi-head (Zaphod) aware and will set display to
682 * the right screen according to mouse position.
683 * \param L The Lua VM state.
684 * \return The number of elements pushed on stack
685 * \luastack
686 * \lparam The command to launch.
687 * \lparam The optional screen number to spawn the command on.
689 static int
690 luaA_spawn(lua_State *L)
692 char *host, newdisplay[128];
693 const char *cmd;
694 int screen = 0, screenp, displayp;
696 if(lua_gettop(L) == 2)
698 screen = luaL_checknumber(L, 2) - 1;
699 luaA_checkscreen(screen);
702 cmd = luaL_checkstring(L, 1);
704 if(!globalconf.xinerama_is_active)
706 xcb_parse_display(NULL, &host, &displayp, &screenp);
707 snprintf(newdisplay, sizeof(newdisplay), "%s:%d.%d", host, displayp, screen);
708 setenv("DISPLAY", newdisplay, 1);
709 p_delete(&host);
712 /* The double-fork construct avoids zombie processes and keeps the code
713 * clean from stupid signal handlers. */
714 if(fork() == 0)
716 if(fork() == 0)
718 if(globalconf.connection)
719 xcb_disconnect(globalconf.connection);
720 setsid();
721 a_exec(cmd);
722 warn("execl '%s' failed: %s\n", cmd, strerror(errno));
724 exit(EXIT_SUCCESS);
726 wait(0);
728 return 0;
731 /** awesome global table.
732 * \param L The Lua VM state.
733 * \return The number of elements pushed on stack.
734 * \luastack
735 * \lfield font The default font.
736 * \lfield conffile The configuration file which has been loaded.
738 static int
739 luaA_awesome_index(lua_State *L)
741 if(luaA_usemetatable(L, 1, 2))
742 return 1;
744 size_t len;
745 const char *buf = luaL_checklstring(L, 2, &len);
747 switch(a_tokenize(buf, len))
749 case A_TK_FONT:
751 char *font = pango_font_description_to_string(globalconf.font->desc);
752 lua_pushstring(L, font);
753 g_free(font);
755 break;
756 case A_TK_CONFFILE:
757 lua_pushstring(L, globalconf.conffile);
758 break;
759 case A_TK_FG:
760 luaA_pushcolor(L, &globalconf.colors.fg);
761 break;
762 case A_TK_BG:
763 luaA_pushcolor(L, &globalconf.colors.bg);
764 break;
765 default:
766 return 0;
769 return 1;
772 /** Newindex function for the awesome global table.
773 * \param L The Lua VM state.
774 * \return The number of elements pushed on stack.
776 static int
777 luaA_awesome_newindex(lua_State *L)
779 if(luaA_usemetatable(L, 1, 2))
780 return 1;
782 size_t len;
783 const char *buf = luaL_checklstring(L, 2, &len);
785 switch(a_tokenize(buf, len))
787 case A_TK_FONT:
789 const char *newfont = luaL_checkstring(L, 3);
790 draw_font_delete(&globalconf.font);
791 globalconf.font = draw_font_new(newfont);
793 break;
794 case A_TK_FG:
795 if((buf = luaL_checklstring(L, 3, &len)))
796 xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.fg, buf, len));
797 break;
798 case A_TK_BG:
799 if((buf = luaL_checklstring(L, 3, &len)))
800 xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.bg, buf, len));
801 break;
802 default:
803 return 0;
806 return 0;
809 /** Initialize the Lua VM
811 void
812 luaA_init(void)
814 lua_State *L;
816 static const struct luaL_reg otable_methods[] =
818 { "__call", luaA_otable_new },
819 { NULL, NULL }
821 static const struct luaL_reg otable_meta[] =
823 { "__index", luaA_otable_index },
824 { "__newindex", luaA_otable_newindex },
825 { NULL, NULL }
827 static const struct luaL_reg awesome_lib[] =
829 { "quit", luaA_quit },
830 { "exec", luaA_exec },
831 { "spawn", luaA_spawn },
832 { "restart", luaA_restart },
833 { "buttons", luaA_buttons },
834 { "__index", luaA_awesome_index },
835 { "__newindex", luaA_awesome_newindex },
836 { NULL, NULL }
838 static const struct luaL_reg root_lib[] =
840 { "buttons", luaA_root_buttons },
841 { "keys", luaA_root_keys },
842 { NULL, NULL }
845 L = globalconf.L = luaL_newstate();
847 luaL_openlibs(L);
849 luaA_fixups(L);
851 /* Export awesome lib */
852 luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
854 /* Export root lib */
855 luaA_openlib(L, "root", root_lib, root_lib);
857 /* Export hooks lib */
858 luaL_register(L, "hooks", awesome_hooks_lib);
860 /* Export D-Bus lib */
861 luaL_register(L, "dbus", awesome_dbus_lib);
863 /* Export keygrabber lib */
864 luaL_register(L, "keygrabber", awesome_keygrabber_lib);
866 /* Export mousegrabber lib */
867 luaL_register(L, "mousegrabber", awesome_mousegrabber_lib);
869 /* Export otable lib */
870 luaA_openlib(L, "otable", otable_methods, otable_meta);
872 /* Export screen */
873 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
875 /* Export mouse */
876 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
878 /* Export button */
879 luaA_openlib(L, "button", awesome_button_methods, awesome_button_meta);
881 /* Export image */
882 luaA_openlib(L, "image", awesome_image_methods, awesome_image_meta);
884 /* Export tag */
885 luaA_openlib(L, "tag", awesome_tag_methods, awesome_tag_meta);
887 /* Export wibox */
888 luaA_openlib(L, "wibox", awesome_wibox_methods, awesome_wibox_meta);
890 /* Export widget */
891 luaA_openlib(L, "widget", awesome_widget_methods, awesome_widget_meta);
893 /* Export client */
894 luaA_openlib(L, "client", awesome_client_methods, awesome_client_meta);
896 /* Export keys */
897 luaA_openlib(L, "key", awesome_key_methods, awesome_key_meta);
898 luaA_openlib(L, "keybinding", awesome_keybinding_methods, awesome_key_meta);
900 lua_pushliteral(L, "AWESOME_VERSION");
901 lua_pushstring(L, AWESOME_VERSION);
902 lua_settable(L, LUA_GLOBALSINDEX);
904 lua_pushliteral(L, "AWESOME_RELEASE");
905 lua_pushstring(L, AWESOME_RELEASE);
906 lua_settable(L, LUA_GLOBALSINDEX);
908 /* init hooks */
909 globalconf.hooks.manage = LUA_REFNIL;
910 globalconf.hooks.unmanage = LUA_REFNIL;
911 globalconf.hooks.focus = LUA_REFNIL;
912 globalconf.hooks.unfocus = LUA_REFNIL;
913 globalconf.hooks.mouse_enter = LUA_REFNIL;
914 globalconf.hooks.mouse_leave = LUA_REFNIL;
915 globalconf.hooks.arrange = LUA_REFNIL;
916 globalconf.hooks.clients = LUA_REFNIL;
917 globalconf.hooks.tags = LUA_REFNIL;
918 globalconf.hooks.tagged = LUA_REFNIL;
919 globalconf.hooks.property = LUA_REFNIL;
920 globalconf.hooks.timer = LUA_REFNIL;
921 #ifdef WITH_DBUS
922 globalconf.hooks.dbus = LUA_REFNIL;
923 #endif
925 /* add Lua lib path (/usr/share/awesome/lib by default) */
926 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?.lua\"");
927 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?/init.lua\"");
929 /* add XDG_CONFIG_DIR (/etc/xdg/awesome by default) as include path */
930 luaA_dostring(L, "package.path = package.path .. \";" XDG_CONFIG_DIR "/awesome/?.lua\"");
931 luaA_dostring(L, "package.path = package.path .. \";" XDG_CONFIG_DIR "/awesome/?/init.lua\"");
933 /* add either XDG_CONFIG_HOME/awesome or HOME/.config/awesome to path */
934 char *dir;
935 if((dir = getenv("XDG_CONFIG_HOME")))
937 char *path;
938 a_asprintf(&path, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"", dir, dir);
939 luaA_dostring(globalconf.L, path);
940 p_delete(&path);
942 else
944 char *path, *homedir = getenv("HOME");
945 a_asprintf(&path,
946 "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?.lua;%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?/init.lua\"",
947 homedir, homedir);
948 luaA_dostring(globalconf.L, path);
949 p_delete(&path);
952 /* add XDG_CONFIG_DIRS to include paths */
953 char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
954 ssize_t len;
956 if((len = a_strlen(xdg_config_dirs)))
958 char **buf, **xdg_files = a_strsplit(xdg_config_dirs, len, ':');
960 for(buf = xdg_files; *buf; buf++)
962 char *confpath;
963 a_asprintf(&confpath, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"",
964 *buf, *buf);
965 luaA_dostring(globalconf.L, confpath);
966 p_delete(&confpath);
969 for(buf = xdg_files; *buf; buf++)
970 p_delete(buf);
971 p_delete(&xdg_files);
975 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
977 static bool
978 luaA_loadrc(const char *confpath, bool run)
980 if(confpath)
982 if(!luaL_loadfile(globalconf.L, confpath))
984 if(run)
986 if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
987 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
988 else
990 globalconf.conffile = a_strdup(confpath);
991 return true;
994 else
995 lua_pop(globalconf.L, 1);
996 return true;
998 else
999 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
1001 return false;
1004 /** Load a configuration file.
1005 * \param confpatharg The configuration file to load.
1006 * \param run Run the configuration file.
1008 bool
1009 luaA_parserc(const char *confpatharg, bool run)
1011 int screen;
1012 const char *confdir, *xdg_config_dirs;
1013 char *confpath = NULL, **xdg_files = NULL, **buf;
1014 ssize_t len;
1015 bool ret = false;
1017 /* try to load, return if it's ok */
1018 if(luaA_loadrc(confpatharg, run))
1020 ret = true;
1021 goto bailout;
1024 if((confdir = getenv("XDG_CONFIG_HOME")))
1025 a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, confdir);
1026 else
1027 a_asprintf(&confpath, "%s" XDG_CONFIG_HOME_DEFAULT AWESOME_CONFIG_FILE, getenv("HOME"));
1029 /* try to run XDG_CONFIG_HOME/awesome/rc.lua */
1030 if(luaA_loadrc(confpath, run))
1032 ret = true;
1033 goto bailout;
1036 p_delete(&confpath);
1038 xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
1040 if(!(len = a_strlen(xdg_config_dirs)))
1042 xdg_config_dirs = XDG_CONFIG_DIR;
1043 len = sizeof(XDG_CONFIG_DIR) - 1;
1046 xdg_files = a_strsplit(xdg_config_dirs, len, ':');
1048 for(buf = xdg_files; *buf && !ret; buf++)
1050 a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, *buf);
1051 if(luaA_loadrc(confpath, run))
1053 ret = true;
1054 goto bailout;
1056 p_delete(&confpath);
1059 bailout:
1061 p_delete(&confpath);
1063 if(xdg_files)
1065 for(buf = xdg_files; *buf; buf++)
1066 p_delete(buf);
1067 p_delete(&xdg_files);
1070 /* Assure there's at least one tag */
1071 for(screen = 0; screen < globalconf.nscreen; screen++)
1072 if(!globalconf.screens[screen].tags.len)
1073 tag_append_to_screen(tag_new("default", sizeof("default") - 1),
1074 &globalconf.screens[screen]);
1075 return ret;
1078 /** Parse a command.
1079 * \param cmd The buffer to parse.
1080 * \return the number of elements pushed on the stack by the last statement in cmd.
1081 * If there's an error, the message is pushed onto the stack and this returns 1.
1083 static int
1084 luaA_docmd(const char *cmd)
1086 char *p;
1087 int newtop, oldtop = lua_gettop(globalconf.L);
1089 while((p = strchr(cmd, '\n')))
1091 newtop = lua_gettop(globalconf.L);
1092 lua_pop(globalconf.L, newtop - oldtop);
1093 oldtop = newtop;
1095 *p = '\0';
1096 if (luaL_dostring(globalconf.L, cmd))
1098 warn("error executing Lua code: %s", lua_tostring(globalconf.L, -1));
1099 return 1;
1101 cmd = p + 1;
1103 return lua_gettop(globalconf.L) - oldtop;
1106 /** Pushes a Lua array containing the top n elements of the stack.
1107 * \param n The number of elements to put in the array.
1109 static void
1110 luaA_array(int n)
1112 int i;
1113 lua_createtable(globalconf.L, n, 0);
1114 lua_insert(globalconf.L, -n - 1);
1116 for (i = n; i > 0; i--)
1117 lua_rawseti(globalconf.L, -i - 1, i);
1120 /** Maps the top n elements of the stack to the result of
1121 * applying a function to that element.
1122 * \param n The number of elements to map.
1123 * \luastack
1124 * \lparam The function to map the elements by. This should be
1125 * at position -(n + 1).
1127 static void
1128 luaA_map(int n)
1130 int i;
1131 for (i = 0; i < n; i++)
1133 lua_pushvalue(globalconf.L, -n - 1); /* copy of the function */
1134 lua_pushvalue(globalconf.L, -n - 1); /* value to map */
1135 lua_pcall(globalconf.L, 1, 1, 0); /* call function */
1136 lua_remove(globalconf.L, -n - 1); /* remove old value */
1138 lua_remove(globalconf.L, -n - 1); /* remove function */
1141 static void luaA_conn_cleanup(EV_P_ ev_io *w)
1143 ev_ref(EV_DEFAULT_UC);
1144 ev_io_stop(EV_DEFAULT_UC_ w);
1145 if (close(w->fd))
1146 warn("error closing UNIX domain socket: %s", strerror(errno));
1147 p_delete(&w);
1150 static void
1151 luaA_cb(EV_P_ ev_io *w, int revents)
1153 char buf[1024];
1154 int r, els;
1155 const char *s;
1156 size_t len;
1158 switch(r = recv(w->fd, buf, sizeof(buf)-1, MSG_TRUNC))
1160 case -1:
1161 warn("error reading UNIX domain socket: %s", strerror(errno));
1162 case 0: /* 0 bytes are only transferred when the connection is closed */
1163 luaA_conn_cleanup(EV_DEFAULT_UC_ w);
1164 break;
1165 default:
1166 if(r >= ssizeof(buf))
1167 break;
1168 buf[r] = '\0';
1169 lua_getglobal(globalconf.L, "table");
1170 lua_getfield(globalconf.L, -1, "concat");
1171 lua_remove(globalconf.L, -2); /* remove table */
1173 lua_getglobal(globalconf.L, "tostring");
1174 els = luaA_docmd(buf);
1175 luaA_map(els); /* map results to strings */
1176 luaA_array(els); /* put strings in an array */
1178 lua_pushstring(globalconf.L, "\t");
1179 lua_pcall(globalconf.L, 2, 1, 0); /* concatenate results with tabs */
1181 s = lua_tolstring(globalconf.L, -1, &len);
1183 /* ignore ENOENT because the client may not read */
1184 if (send(w->fd, s, len, MSG_DONTWAIT) == -1)
1185 switch(errno)
1187 case ENOENT:
1188 case EAGAIN:
1189 break;
1190 default:
1191 warn("can't send back to client via domain socket: %s", strerror(errno));
1192 break;
1195 lua_pop(globalconf.L, 1); /* pop the string */
1197 awesome_refresh(globalconf.connection);
1201 static void
1202 luaA_conn_cb(EV_P_ ev_io *w, int revents)
1204 ev_io *csio_conn = p_new(ev_io, 1);
1205 int csfd = accept(w->fd, NULL, NULL);
1207 ev_io_init(csio_conn, &luaA_cb, csfd, EV_READ);
1208 ev_io_start(EV_DEFAULT_UC_ csio_conn);
1209 ev_unref(EV_DEFAULT_UC);
1212 void
1213 luaA_cs_init(void)
1215 int csfd = socket_getclient();
1217 if (csfd < 0 || fcntl(csfd, F_SETFD, FD_CLOEXEC) == -1)
1218 return;
1220 addr = socket_getaddr(getenv("DISPLAY"));
1222 /* Needed for some OSes like Solaris */
1223 #ifndef SUN_LEN
1224 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) + strlen ((ptr)->sun_path))
1225 #endif
1227 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1229 if(errno == EADDRINUSE)
1231 if(unlink(addr->sun_path))
1232 warn("error unlinking existing file: %s", strerror(errno));
1233 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1234 return warn("error binding UNIX domain socket: %s", strerror(errno));
1236 else
1237 return warn("error binding UNIX domain socket: %s", strerror(errno));
1239 listen(csfd, 10);
1241 ev_io_init(&csio, &luaA_conn_cb, csfd, EV_READ);
1242 ev_io_start(EV_DEFAULT_UC_ &csio);
1243 ev_unref(EV_DEFAULT_UC);
1246 void
1247 luaA_cs_cleanup(void)
1249 if(csio.fd < 0)
1250 return;
1251 ev_ref(EV_DEFAULT_UC);
1252 ev_io_stop(EV_DEFAULT_UC_ &csio);
1253 if (close(csio.fd))
1254 warn("error closing UNIX domain socket: %s", strerror(errno));
1255 if(unlink(addr->sun_path))
1256 warn("error unlinking UNIX domain socket: %s", strerror(errno));
1257 p_delete(&addr);
1258 csio.fd = -1;
1261 void
1262 luaA_on_timer(EV_P_ ev_timer *w, int revents)
1264 if(globalconf.hooks.timer != LUA_REFNIL)
1265 luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0, 0);
1266 awesome_refresh(globalconf.connection);
1269 /** Push a color as a string onto the stack
1270 * \param L The Lua VM state.
1271 * \param c The color to push.
1272 * \return The number of elements pushed on stack.
1275 luaA_pushcolor(lua_State *L, const xcolor_t *c)
1277 uint8_t r = (unsigned)c->red * 0xff / 0xffff;
1278 uint8_t g = (unsigned)c->green * 0xff / 0xffff;
1279 uint8_t b = (unsigned)c->blue * 0xff / 0xffff;
1280 uint8_t a = (unsigned)c->alpha * 0xff / 0xffff;
1281 char s[10];
1282 int len;
1283 /* do not print alpha if it's full */
1284 if(a == 0xff)
1285 len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
1286 else
1287 len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
1288 lua_pushlstring(L, s, len);
1289 return 1;