awful.prompt: add support for Shift+Insert to paste
[awesome.git] / luaa.c
blobf4788054e6f22b4007f3228fc1e95d4ed82ce0fe
1 /*
2 * lua.c - Lua configuration management
4 * Copyright © 2008 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 "common/socket.h"
52 #include "common/buffer.h"
54 extern awesome_t globalconf;
56 extern const struct luaL_reg awesome_hooks_lib[];
57 extern const struct luaL_reg awesome_dbus_lib[];
58 extern const struct luaL_reg awesome_keygrabber_lib[];
59 extern const struct luaL_reg awesome_mousegrabber_lib[];
60 extern const struct luaL_reg awesome_button_methods[];
61 extern const struct luaL_reg awesome_button_meta[];
62 extern const struct luaL_reg awesome_image_methods[];
63 extern const struct luaL_reg awesome_image_meta[];
64 extern const struct luaL_reg awesome_mouse_methods[];
65 extern const struct luaL_reg awesome_mouse_meta[];
66 extern const struct luaL_reg awesome_screen_methods[];
67 extern const struct luaL_reg awesome_screen_meta[];
68 extern const struct luaL_reg awesome_client_methods[];
69 extern const struct luaL_reg awesome_client_meta[];
70 extern const struct luaL_reg awesome_tag_methods[];
71 extern const struct luaL_reg awesome_tag_meta[];
72 extern const struct luaL_reg awesome_widget_methods[];
73 extern const struct luaL_reg awesome_widget_meta[];
74 extern const struct luaL_reg awesome_wibox_methods[];
75 extern const struct luaL_reg awesome_wibox_meta[];
76 extern const struct luaL_reg awesome_key_methods[];
77 extern const struct luaL_reg awesome_key_meta[];
78 extern const struct luaL_reg awesome_keybinding_methods[];
79 extern const struct luaL_reg awesome_selection_methods[];
80 extern const struct luaL_reg awesome_selection_meta[];
82 static struct sockaddr_un *addr;
83 static ev_io csio = { .fd = -1 };
85 #define XDG_CONFIG_HOME_DEFAULT "/.config"
87 /** Get or set global mouse bindings.
88 * This binding will be available when you'll click on root window.
89 * \param L The Lua VM state.
90 * \return The number of element pushed on stack.
91 * \luastack
92 * \lvalue A client.
93 * \lparam An array of mouse button bindings objects, or nothing.
94 * \return The array of mouse button bindings objects of this client.
96 static int
97 luaA_buttons(lua_State *L)
99 button_array_t *buttons = &globalconf.buttons;
101 if(lua_gettop(L) == 1)
102 luaA_button_array_set(L, 1, buttons);
104 return luaA_button_array_get(L, buttons);
107 /** Quit awesome.
108 * \param L The Lua VM state.
109 * \return The number of elements pushed on stack.
111 static int
112 luaA_quit(lua_State *L __attribute__ ((unused)))
114 ev_unloop(globalconf.loop, 1);
115 return 0;
118 /** Execute another application, probably a window manager, to replace
119 * awesome.
120 * \param L The Lua VM state.
121 * \return The number of elements pushed on stack.
122 * \luastack
123 * \lparam The command line to execute.
125 static int
126 luaA_exec(lua_State *L)
128 const char *cmd = luaL_checkstring(L, 1);
130 awesome_atexit();
132 a_exec(cmd);
133 return 0;
136 /** Restart awesome.
138 static int
139 luaA_restart(lua_State *L __attribute__ ((unused)))
141 awesome_restart();
142 return 0;
145 static void
146 luaA_openlib(lua_State *L, const char *name,
147 const struct luaL_reg methods[],
148 const struct luaL_reg meta[])
150 luaL_newmetatable(L, name); /* 1 */
151 lua_pushvalue(L, -1); /* dup metatable 2 */
152 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
154 luaL_register(L, NULL, meta); /* 1 */
155 luaL_register(L, name, methods); /* 2 */
156 lua_pushvalue(L, -1); /* dup self as metatable 3 */
157 lua_setmetatable(L, -2); /* set self as metatable 2 */
158 lua_pop(L, 2);
161 /** UTF-8 aware string length computing.
162 * \param L The Lua VM state.
163 * \return The number of elements pushed on stack.
165 static int
166 luaA_mbstrlen(lua_State *L)
168 const char *cmd = luaL_checkstring(L, 1);
169 lua_pushnumber(L, mbstowcs(NULL, NONULL(cmd), 0));
170 return 1;
173 /** Overload standard Lua next function to use __next key on metatable.
174 * \param L The Lua VM state.
175 * \param The number of elements pushed on stack.
177 static int
178 luaAe_next(lua_State *L)
180 if(luaL_getmetafield(L, 1, "__next"))
182 lua_insert(L, 1);
183 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
184 return lua_gettop(L);
187 luaL_checktype(L, 1, LUA_TTABLE);
188 lua_settop(L, 2);
189 if(lua_next(L, 1))
190 return 2;
191 lua_pushnil(L);
192 return 1;
195 /** Overload lua_next() function by using __next metatable field
196 * to get next elements.
197 * \param L The Lua VM stack.
198 * \param idx The index number of elements in stack.
199 * \return 1 if more elements to come, 0 otherwise.
202 luaA_next(lua_State *L, int idx)
204 if(luaL_getmetafield(L, idx, "__next"))
206 /* if idx is relative, reduce it since we got __next */
207 if(idx < 0) idx--;
208 /* copy table and then move key */
209 lua_pushvalue(L, idx);
210 lua_pushvalue(L, -3);
211 lua_remove(L, -4);
212 lua_pcall(L, 2, 2, 0);
213 /* next returned nil, it's the end */
214 if(lua_isnil(L, -1))
216 /* remove nil */
217 lua_pop(L, 2);
218 return 0;
220 return 1;
222 else if(lua_istable(L, idx))
223 return lua_next(L, idx);
224 /* remove the key */
225 lua_pop(L, 1);
226 return 0;
229 /** Generic pairs function.
230 * \param L The Lua VM state.
231 * \return The number of elements pushed on stack.
233 static int
234 luaA_generic_pairs(lua_State *L)
236 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
237 lua_pushvalue(L, 1); /* state, */
238 lua_pushnil(L); /* and initial value */
239 return 3;
242 /** Overload standard pairs function to use __pairs field of metatables.
243 * \param L The Lua VM state.
244 * \return The number of elements pushed on stack.
246 static int
247 luaAe_pairs(lua_State *L)
249 if(luaL_getmetafield(L, 1, "__pairs"))
251 lua_insert(L, 1);
252 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
253 return lua_gettop(L);
256 luaL_checktype(L, 1, LUA_TTABLE);
257 return luaA_generic_pairs(L);
260 static int
261 luaA_ipairs_aux(lua_State *L)
263 int i = luaL_checkint(L, 2) + 1;
264 luaL_checktype(L, 1, LUA_TTABLE);
265 lua_pushinteger(L, i);
266 lua_rawgeti(L, 1, i);
267 return (lua_isnil(L, -1)) ? 0 : 2;
270 /** Overload standard ipairs function to use __ipairs field of metatables.
271 * \param L The Lua VM state.
272 * \return The number of elements pushed on stack.
274 static int
275 luaAe_ipairs(lua_State *L)
277 if(luaL_getmetafield(L, 1, "__ipairs"))
279 lua_insert(L, 1);
280 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
281 return lua_gettop(L);
284 luaL_checktype(L, 1, LUA_TTABLE);
285 lua_pushvalue(L, lua_upvalueindex(1));
286 lua_pushvalue(L, 1);
287 lua_pushinteger(L, 0); /* and initial value */
288 return 3;
291 /** Enhanced type() function which recognize awesome objects.
292 * \param L The Lua VM state.
293 * \return The number of arguments pushed on the stack.
295 static int
296 luaAe_type(lua_State *L)
298 luaL_checkany(L, 1);
299 #define CHECK_TYPE(type) \
300 do { \
301 if(luaA_toudata(L, 1, #type)) \
303 lua_pushliteral(L, #type); \
304 return 1; \
306 } while(0)
307 CHECK_TYPE(wibox);
308 CHECK_TYPE(client);
309 CHECK_TYPE(image);
310 CHECK_TYPE(key);
311 CHECK_TYPE(button);
312 CHECK_TYPE(tag);
313 CHECK_TYPE(widget);
314 #undef CHECK_TYPE
315 lua_pushstring(L, luaL_typename(L, 1));
316 return 1;
319 /** Replace various standards Lua functions with our own.
320 * \param L The Lua VM state.
322 static void
323 luaA_fixups(lua_State *L)
325 /* export string.wlen */
326 lua_getglobal(L, "string");
327 lua_pushcfunction(L, luaA_mbstrlen);
328 lua_setfield(L, -2, "wlen");
329 lua_pop(L, 1);
330 /* replace next */
331 lua_pushliteral(L, "next");
332 lua_pushcfunction(L, luaAe_next);
333 lua_settable(L, LUA_GLOBALSINDEX);
334 /* replace pairs */
335 lua_pushliteral(L, "pairs");
336 lua_pushcfunction(L, luaAe_next);
337 lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
338 lua_settable(L, LUA_GLOBALSINDEX);
339 /* replace ipairs */
340 lua_pushliteral(L, "ipairs");
341 lua_pushcfunction(L, luaA_ipairs_aux);
342 lua_pushcclosure(L, luaAe_ipairs, 1);
343 lua_settable(L, LUA_GLOBALSINDEX);
344 /* replace type */
345 lua_pushliteral(L, "type");
346 lua_pushcfunction(L, luaAe_type);
347 lua_settable(L, LUA_GLOBALSINDEX);
350 /** __next function for wtable objects.
351 * \param L The Lua VM state.
352 * \return The number of elements pushed on stack.
354 static int
355 luaA_wtable_next(lua_State *L)
357 /* upvalue 1 is content table */
358 if(lua_next(L, lua_upvalueindex(1)))
359 return 2;
360 lua_pushnil(L);
361 return 1;
364 /** __ipairs function for wtable objects.
365 * \param L The Lua VM state.
366 * \return The number of elements pushed on stack.
368 static int
369 luaA_wtable_ipairs(lua_State *L)
371 /* push ipairs_aux */
372 lua_pushvalue(L, lua_upvalueindex(2));
373 /* push content table */
374 lua_pushvalue(L, lua_upvalueindex(1));
375 lua_pushinteger(L, 0); /* and initial value */
376 return 3;
379 /** Index function of wtable objects.
380 * \param L The Lua VM state.
381 * \return The number of elements pushed on stack.
383 static int
384 luaA_wtable_index(lua_State *L)
386 size_t len;
387 const char *buf;
389 lua_pushvalue(L, 2);
390 /* check for size, waiting lua 5.2 and __len on tables */
391 if((buf = lua_tolstring(L, -1, &len)))
392 if(a_tokenize(buf, len) == A_TK_LEN)
394 lua_pushnumber(L, lua_objlen(L, lua_upvalueindex(1)));
395 return 1;
397 lua_pop(L, 1);
399 /* upvalue 1 is content table */
400 lua_rawget(L, lua_upvalueindex(1));
401 return 1;
404 /** Newndex function of wtable objects.
405 * \param L The Lua VM state.
406 * \return The number of elements pushed on stack.
408 static int
409 luaA_wtable_newindex(lua_State *L)
411 bool invalid = false;
413 /* push key on top */
414 lua_pushvalue(L, 2);
415 /* get current key value in content table */
416 lua_rawget(L, lua_upvalueindex(1));
417 /* if value is a widget, notify change */
418 if(lua_istable(L, -1) || luaA_toudata(L, -1, "widget"))
419 invalid = true;
421 lua_pop(L, 1); /* remove value */
423 /* if new value is a widget or a table */
424 if(lua_istable(L, 3))
426 luaA_table2wtable(L);
427 invalid = true;
429 else if(!invalid && luaA_toudata(L, 3, "widget"))
430 invalid = true;
432 /* upvalue 1 is content table */
433 lua_rawset(L, lua_upvalueindex(1));
435 if(invalid)
436 luaA_wibox_invalidate_byitem(L, lua_topointer(L, 1));
438 return 0;
441 /** Convert the top element of the stack to a proxied wtable.
442 * \param L The Lua VM state.
444 void
445 luaA_table2wtable(lua_State *L)
447 if(!lua_istable(L, -1))
448 return;
450 lua_newtable(L); /* create *real* content table */
451 lua_newtable(L); /* metatable */
452 lua_pushvalue(L, -2); /* copy content table */
453 lua_pushcfunction(L, luaA_ipairs_aux); /* push ipairs aux */
454 lua_pushcclosure(L, luaA_wtable_ipairs, 2);
455 lua_pushvalue(L, -3); /* copy content table */
456 lua_pushcclosure(L, luaA_wtable_next, 1); /* __next has the content table as upvalue */
457 lua_pushvalue(L, -4); /* copy content table */
458 lua_pushcclosure(L, luaA_wtable_index, 1); /* __index has the content table as upvalue */
459 lua_pushvalue(L, -5); /* copy content table */
460 lua_pushcclosure(L, luaA_wtable_newindex, 1); /* __newindex has the content table as upvalue */
461 /* set metatable field with just pushed closure */
462 lua_setfield(L, -5, "__newindex");
463 lua_setfield(L, -4, "__index");
464 lua_setfield(L, -3, "__next");
465 lua_setfield(L, -2, "__ipairs");
466 /* set metatable impossible to touch */
467 lua_pushliteral(L, "wtable");
468 lua_setfield(L, -2, "__metatable");
469 /* set new metatable on original table */
470 lua_setmetatable(L, -3);
472 /* initial key */
473 lua_pushnil(L);
474 /* go through original table */
475 while(lua_next(L, -3))
477 /* if convert value to wtable */
478 luaA_table2wtable(L);
479 /* copy key */
480 lua_pushvalue(L, -2);
481 /* move key before value */
482 lua_insert(L, -2);
483 /* set same value in content table */
484 lua_rawset(L, -4);
485 /* copy key */
486 lua_pushvalue(L, -1);
487 /* push the new value :-> */
488 lua_pushnil(L);
489 /* set orig[k] = nil */
490 lua_rawset(L, -5);
492 /* remove content table */
493 lua_pop(L, 1);
496 /** Look for an item: table, function, etc.
497 * \param L The Lua VM state.
498 * \param item The pointer item.
500 bool
501 luaA_hasitem(lua_State *L, const void *item)
503 lua_pushnil(L);
504 while(luaA_next(L, -2))
506 if(lua_topointer(L, -1) == item)
508 /* remove value and key */
509 lua_pop(L, 2);
510 return true;
512 if(lua_istable(L, -1))
513 if(luaA_hasitem(L, item))
515 /* remove key and value */
516 lua_pop(L, 2);
517 return true;
519 /* remove value */
520 lua_pop(L, 1);
522 return false;
525 /** Check if a table is a loop. When using table as direct acyclic digram,
526 * this is useful.
527 * \param L The Lua VM state.
528 * \param idx The index of the table in the stack
529 * \return True if the table loops.
531 bool
532 luaA_isloop(lua_State *L, int idx)
534 if(lua_istable(L, idx))
536 lua_pushvalue(L, idx); /* push table on top */
537 if(luaA_hasitem(L, lua_topointer(L, -1)))
539 lua_pop(L, 1); /* remove pushed table */
540 return true;
542 lua_pushnil(L);
543 while(luaA_next(L, -2))
545 /* check for recursivity */
546 if(luaA_isloop(L, -1))
548 lua_pop(L, 2); /* remove key and value */
549 return true;
551 lua_pop(L, 1); /* remove value */
553 lua_pop(L, 1); /* remove pushed table */
555 return false;
558 /** Object table.
559 * This table can use safely object as key.
560 * \param L The Lua VM state.
561 * \return The number of elements pushed on stack.
564 luaA_otable_index(lua_State *L)
566 void **obj, **v;
568 if((obj = lua_touserdata(L, 2)))
570 /* begins at nil */
571 lua_pushnil(L);
572 while(lua_next(L, 1))
574 /* check the key against the key if the key is a userdata,
575 * otherwise check it again the value. */
576 if((v = lua_touserdata(L, -2))
577 && *v == *obj)
578 /* return value */
579 return 1;
580 else if((v = lua_touserdata(L, -1))
581 && *v == *obj)
583 /* return key */
584 lua_pop(L, 1);
585 return 1;
587 /* removes 'value'; keeps 'key' for next iteration */
588 lua_pop(L, 1);
590 return 0;
593 lua_rawget(L, 1);
594 return 1;
597 /** Object table.
598 * This table can use safely object as key.
599 * \param L The Lua VM state.
600 * \return The number of elements pushed on stack.
602 static int
603 luaA_otable_newindex(lua_State *L)
605 void **obj, **v;
607 if((obj = lua_touserdata(L, 2)))
609 /* begins at nil */
610 lua_pushnil(L);
611 while(lua_next(L, 1))
613 if((v = lua_touserdata(L, -2))
614 && *v == *obj)
616 /* remove value */
617 lua_pop(L, 1);
618 /* push new value on top */
619 lua_pushvalue(L, 3);
620 /* set in table key = value */
621 lua_rawset(L, 1);
622 return 0;
624 /* removes 'value'; keeps 'key' for next iteration */
625 lua_pop(L, 1);
629 lua_rawset(L, 1);
631 return 0;
634 /** Spawn a program.
635 * This function is multi-head (Zaphod) aware and will set display to
636 * the right screen according to mouse position.
637 * \param L The Lua VM state.
638 * \return The number of elements pushed on stack
639 * \luastack
640 * \lparam The command to launch.
641 * \lparam The optional screen number to spawn the command on.
643 static int
644 luaA_spawn(lua_State *L)
646 char *host, newdisplay[128];
647 const char *cmd;
648 int screen = 0, screenp, displayp;
650 if(lua_gettop(L) == 2)
652 screen = luaL_checknumber(L, 2) - 1;
653 luaA_checkscreen(screen);
656 cmd = luaL_checkstring(L, 1);
658 if(!globalconf.xinerama_is_active)
660 xcb_parse_display(NULL, &host, &displayp, &screenp);
661 snprintf(newdisplay, sizeof(newdisplay), "%s:%d.%d", host, displayp, screen);
662 setenv("DISPLAY", newdisplay, 1);
663 p_delete(&host);
666 /* The double-fork construct avoids zombie processes and keeps the code
667 * clean from stupid signal handlers. */
668 if(fork() == 0)
670 if(fork() == 0)
672 if(globalconf.connection)
673 xcb_disconnect(globalconf.connection);
674 setsid();
675 a_exec(cmd);
676 warn("execl '%s' failed: %s\n", cmd, strerror(errno));
678 exit(EXIT_SUCCESS);
680 wait(0);
682 return 0;
685 /** awesome global table.
686 * \param L The Lua VM state.
687 * \return The number of elements pushed on stack.
688 * \luastack
689 * \lfield font The default font.
690 * \lfield conffile The configuration file which has been loaded.
692 static int
693 luaA_awesome_index(lua_State *L)
695 if(luaA_usemetatable(L, 1, 2))
696 return 1;
698 size_t len;
699 const char *buf = luaL_checklstring(L, 2, &len);
701 switch(a_tokenize(buf, len))
703 case A_TK_FONT:
705 char *font = pango_font_description_to_string(globalconf.font->desc);
706 lua_pushstring(L, font);
707 g_free(font);
709 break;
710 case A_TK_CONFFILE:
711 lua_pushstring(L, globalconf.conffile);
712 break;
713 case A_TK_FG:
714 luaA_pushcolor(L, &globalconf.colors.fg);
715 break;
716 case A_TK_BG:
717 luaA_pushcolor(L, &globalconf.colors.bg);
718 break;
719 default:
720 return 0;
723 return 1;
726 /** Newindex function for the awesome global table.
727 * \param L The Lua VM state.
728 * \return The number of elements pushed on stack.
730 static int
731 luaA_awesome_newindex(lua_State *L)
733 if(luaA_usemetatable(L, 1, 2))
734 return 1;
736 size_t len;
737 const char *buf = luaL_checklstring(L, 2, &len);
739 switch(a_tokenize(buf, len))
741 case A_TK_FONT:
743 const char *newfont = luaL_checkstring(L, 3);
744 draw_font_delete(&globalconf.font);
745 globalconf.font = draw_font_new(newfont);
747 break;
748 case A_TK_FG:
749 if((buf = luaL_checklstring(L, 3, &len)))
750 xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.fg, buf, len));
751 break;
752 case A_TK_BG:
753 if((buf = luaL_checklstring(L, 3, &len)))
754 xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.bg, buf, len));
755 break;
756 default:
757 return 0;
760 return 0;
763 /** Initialize the Lua VM
765 void
766 luaA_init(void)
768 lua_State *L;
770 static const struct luaL_reg otable_methods[] =
772 { "__call", luaA_otable_new },
773 { NULL, NULL }
775 static const struct luaL_reg otable_meta[] =
777 { "__index", luaA_otable_index },
778 { "__newindex", luaA_otable_newindex },
779 { NULL, NULL }
781 static const struct luaL_reg awesome_lib[] =
783 { "quit", luaA_quit },
784 { "exec", luaA_exec },
785 { "spawn", luaA_spawn },
786 { "restart", luaA_restart },
787 { "buttons", luaA_buttons },
788 { "__index", luaA_awesome_index },
789 { "__newindex", luaA_awesome_newindex },
790 { NULL, NULL }
793 L = globalconf.L = luaL_newstate();
795 luaL_openlibs(L);
797 luaA_fixups(L);
799 /* Export awesome lib */
800 luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
802 /* Export hooks lib */
803 luaL_register(L, "hooks", awesome_hooks_lib);
805 /* Export D-Bus lib */
806 luaL_register(L, "dbus", awesome_dbus_lib);
808 /* Export keygrabber lib */
809 luaL_register(L, "keygrabber", awesome_keygrabber_lib);
811 /* Export mousegrabber lib */
812 luaL_register(L, "mousegrabber", awesome_mousegrabber_lib);
814 /* Export otable lib */
815 luaA_openlib(L, "otable", otable_methods, otable_meta);
817 /* Export screen */
818 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
820 /* Export mouse */
821 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
823 /* Export button */
824 luaA_openlib(L, "button", awesome_button_methods, awesome_button_meta);
826 /* Export image */
827 luaA_openlib(L, "image", awesome_image_methods, awesome_image_meta);
829 /* Export tag */
830 luaA_openlib(L, "tag", awesome_tag_methods, awesome_tag_meta);
832 /* Export wibox */
833 luaA_openlib(L, "wibox", awesome_wibox_methods, awesome_wibox_meta);
835 /* Export widget */
836 luaA_openlib(L, "widget", awesome_widget_methods, awesome_widget_meta);
838 /* Export client */
839 luaA_openlib(L, "client", awesome_client_methods, awesome_client_meta);
841 /* Export keys */
842 luaA_openlib(L, "key", awesome_key_methods, awesome_key_meta);
843 luaA_openlib(L, "keybinding", awesome_keybinding_methods, awesome_key_meta);
845 /* Export selection */
846 luaA_openlib(L, "selection", awesome_selection_methods, awesome_selection_meta);
848 lua_pushliteral(L, "AWESOME_VERSION");
849 lua_pushstring(L, AWESOME_VERSION);
850 lua_settable(L, LUA_GLOBALSINDEX);
852 lua_pushliteral(L, "AWESOME_RELEASE");
853 lua_pushstring(L, AWESOME_RELEASE);
854 lua_settable(L, LUA_GLOBALSINDEX);
856 /* init hooks */
857 globalconf.hooks.manage = LUA_REFNIL;
858 globalconf.hooks.unmanage = LUA_REFNIL;
859 globalconf.hooks.focus = LUA_REFNIL;
860 globalconf.hooks.unfocus = LUA_REFNIL;
861 globalconf.hooks.mouse_enter = LUA_REFNIL;
862 globalconf.hooks.mouse_leave = LUA_REFNIL;
863 globalconf.hooks.arrange = LUA_REFNIL;
864 globalconf.hooks.clients = LUA_REFNIL;
865 globalconf.hooks.tags = LUA_REFNIL;
866 globalconf.hooks.tagged = LUA_REFNIL;
867 globalconf.hooks.property = LUA_REFNIL;
868 globalconf.hooks.timer = LUA_REFNIL;
869 #ifdef WITH_DBUS
870 globalconf.hooks.dbus = LUA_REFNIL;
871 #endif
873 /* add Lua lib path (/usr/share/awesome/lib by default) */
874 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?.lua\"");
875 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?/init.lua\"");
877 /* add XDG_CONFIG_DIR (/etc/xdg/awesome by default) as include path */
878 luaA_dostring(L, "package.path = package.path .. \";" XDG_CONFIG_DIR "/awesome/?.lua\"");
879 luaA_dostring(L, "package.path = package.path .. \";" XDG_CONFIG_DIR "/awesome/?/init.lua\"");
881 /* add either XDG_CONFIG_HOME/awesome or HOME/.config/awesome to path */
882 char *dir;
883 if((dir = getenv("XDG_CONFIG_HOME")))
885 char *path;
886 a_asprintf(&path, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"", dir, dir);
887 luaA_dostring(globalconf.L, path);
888 p_delete(&path);
890 else
892 char *path, *homedir = getenv("HOME");
893 a_asprintf(&path,
894 "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?.lua;%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?/init.lua\"",
895 homedir, homedir);
896 luaA_dostring(globalconf.L, path);
897 p_delete(&path);
900 /* add XDG_CONFIG_DIRS to include paths */
901 char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
902 ssize_t len;
904 if((len = a_strlen(xdg_config_dirs)))
906 char **buf, **xdg_files = a_strsplit(xdg_config_dirs, len, ':');
908 for(buf = xdg_files; *buf; buf++)
910 char *confpath;
911 a_asprintf(&confpath, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"",
912 *buf, *buf);
913 luaA_dostring(globalconf.L, confpath);
914 p_delete(&confpath);
917 for(buf = xdg_files; *buf; buf++)
918 p_delete(buf);
919 p_delete(&xdg_files);
923 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
925 static bool
926 luaA_loadrc(const char *confpath, bool run)
928 if(confpath)
930 if(!luaL_loadfile(globalconf.L, confpath))
932 if(run)
934 if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
935 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
936 else
938 globalconf.conffile = a_strdup(confpath);
939 return true;
942 else
943 lua_pop(globalconf.L, 1);
944 return true;
946 else
947 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
949 return false;
952 /** Load a configuration file.
953 * \param confpatharg The configuration file to load.
954 * \param run Run the configuration file.
956 bool
957 luaA_parserc(const char *confpatharg, bool run)
959 int screen;
960 const char *confdir, *xdg_config_dirs;
961 char *confpath = NULL, **xdg_files = NULL, **buf;
962 ssize_t len;
963 bool ret = false;
965 /* try to load, return if it's ok */
966 if(luaA_loadrc(confpatharg, run))
968 ret = true;
969 goto bailout;
972 if((confdir = getenv("XDG_CONFIG_HOME")))
973 a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, confdir);
974 else
975 a_asprintf(&confpath, "%s" XDG_CONFIG_HOME_DEFAULT AWESOME_CONFIG_FILE, getenv("HOME"));
977 /* try to run XDG_CONFIG_HOME/awesome/rc.lua */
978 if(luaA_loadrc(confpath, run))
980 ret = true;
981 goto bailout;
984 p_delete(&confpath);
986 xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
988 if(!(len = a_strlen(xdg_config_dirs)))
990 xdg_config_dirs = XDG_CONFIG_DIR;
991 len = sizeof(XDG_CONFIG_DIR) - 1;
994 xdg_files = a_strsplit(xdg_config_dirs, len, ':');
996 for(buf = xdg_files; *buf && !ret; buf++)
998 a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, *buf);
999 if(luaA_loadrc(confpath, run))
1001 ret = true;
1002 goto bailout;
1004 p_delete(&confpath);
1007 bailout:
1009 p_delete(&confpath);
1011 if(xdg_files)
1013 for(buf = xdg_files; *buf; buf++)
1014 p_delete(buf);
1015 p_delete(&xdg_files);
1018 /* Assure there's at least one tag */
1019 for(screen = 0; screen < globalconf.nscreen; screen++)
1020 if(!globalconf.screens[screen].tags.len)
1021 tag_append_to_screen(tag_new("default", sizeof("default") - 1),
1022 &globalconf.screens[screen]);
1023 return ret;
1026 /** Parse a command.
1027 * \param cmd The buffer to parse.
1028 * \return the number of elements pushed on the stack by the last statement in cmd.
1029 * If there's an error, the message is pushed onto the stack and this returns 1.
1031 static int
1032 luaA_docmd(const char *cmd)
1034 char *p;
1035 int newtop, oldtop = lua_gettop(globalconf.L);
1037 while((p = strchr(cmd, '\n')))
1039 newtop = lua_gettop(globalconf.L);
1040 lua_pop(globalconf.L, newtop - oldtop);
1041 oldtop = newtop;
1043 *p = '\0';
1044 if (luaL_dostring(globalconf.L, cmd))
1046 warn("error executing Lua code: %s", lua_tostring(globalconf.L, -1));
1047 return 1;
1049 cmd = p + 1;
1051 return lua_gettop(globalconf.L) - oldtop;
1054 /** Pushes a Lua array containing the top n elements of the stack.
1055 * \param n The number of elements to put in the array.
1057 static void
1058 luaA_array(int n)
1060 int i;
1061 lua_createtable(globalconf.L, n, 0);
1062 lua_insert(globalconf.L, -n - 1);
1064 for (i = n; i > 0; i--)
1065 lua_rawseti(globalconf.L, -i - 1, i);
1068 /** Maps the top n elements of the stack to the result of
1069 * applying a function to that element.
1070 * \param n The number of elements to map.
1071 * \luastack
1072 * \lparam The function to map the elements by. This should be
1073 * at position -(n + 1).
1075 static void
1076 luaA_map(int n)
1078 int i;
1079 for (i = 0; i < n; i++)
1081 lua_pushvalue(globalconf.L, -n - 1); /* copy of the function */
1082 lua_pushvalue(globalconf.L, -n - 1); /* value to map */
1083 lua_pcall(globalconf.L, 1, 1, 0); /* call function */
1084 lua_remove(globalconf.L, -n - 1); /* remove old value */
1086 lua_remove(globalconf.L, -n - 1); /* remove function */
1089 static void luaA_conn_cleanup(EV_P_ ev_io *w)
1091 ev_ref(EV_DEFAULT_UC);
1092 ev_io_stop(EV_DEFAULT_UC_ w);
1093 if (close(w->fd))
1094 warn("error closing UNIX domain socket: %s", strerror(errno));
1095 p_delete(&w);
1098 static void
1099 luaA_cb(EV_P_ ev_io *w, int revents)
1101 char buf[1024];
1102 int r, els;
1103 const char *s;
1104 size_t len;
1106 switch(r = recv(w->fd, buf, sizeof(buf)-1, MSG_TRUNC))
1108 case -1:
1109 warn("error reading UNIX domain socket: %s", strerror(errno));
1110 case 0: /* 0 bytes are only transferred when the connection is closed */
1111 luaA_conn_cleanup(EV_DEFAULT_UC_ w);
1112 break;
1113 default:
1114 if(r >= ssizeof(buf))
1115 break;
1116 buf[r] = '\0';
1117 lua_getglobal(globalconf.L, "table");
1118 lua_getfield(globalconf.L, -1, "concat");
1119 lua_remove(globalconf.L, -2); /* remove table */
1121 lua_getglobal(globalconf.L, "tostring");
1122 els = luaA_docmd(buf);
1123 luaA_map(els); /* map results to strings */
1124 luaA_array(els); /* put strings in an array */
1126 lua_pushstring(globalconf.L, "\t");
1127 lua_pcall(globalconf.L, 2, 1, 0); /* concatenate results with tabs */
1129 s = lua_tolstring(globalconf.L, -1, &len);
1131 /* ignore ENOENT because the client may not read */
1132 if (send(w->fd, s, len, MSG_DONTWAIT) == -1)
1133 switch(errno)
1135 case ENOENT:
1136 case EAGAIN:
1137 break;
1138 default:
1139 warn("can't send back to client via domain socket: %s", strerror(errno));
1140 break;
1143 lua_pop(globalconf.L, 1); /* pop the string */
1145 awesome_refresh(globalconf.connection);
1149 static void
1150 luaA_conn_cb(EV_P_ ev_io *w, int revents)
1152 ev_io *csio_conn = p_new(ev_io, 1);
1153 int csfd = accept(w->fd, NULL, NULL);
1155 ev_io_init(csio_conn, &luaA_cb, csfd, EV_READ);
1156 ev_io_start(EV_DEFAULT_UC_ csio_conn);
1157 ev_unref(EV_DEFAULT_UC);
1160 void
1161 luaA_cs_init(void)
1163 int csfd = socket_getclient();
1165 if (csfd < 0 || fcntl(csfd, F_SETFD, FD_CLOEXEC) == -1)
1166 return;
1168 addr = socket_getaddr(getenv("DISPLAY"));
1170 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1172 if(errno == EADDRINUSE)
1174 if(unlink(addr->sun_path))
1175 warn("error unlinking existing file: %s", strerror(errno));
1176 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1177 return warn("error binding UNIX domain socket: %s", strerror(errno));
1179 else
1180 return warn("error binding UNIX domain socket: %s", strerror(errno));
1182 listen(csfd, 10);
1184 ev_io_init(&csio, &luaA_conn_cb, csfd, EV_READ);
1185 ev_io_start(EV_DEFAULT_UC_ &csio);
1186 ev_unref(EV_DEFAULT_UC);
1189 void
1190 luaA_cs_cleanup(void)
1192 if(csio.fd < 0)
1193 return;
1194 ev_ref(EV_DEFAULT_UC);
1195 ev_io_stop(EV_DEFAULT_UC_ &csio);
1196 if (close(csio.fd))
1197 warn("error closing UNIX domain socket: %s", strerror(errno));
1198 if(unlink(addr->sun_path))
1199 warn("error unlinking UNIX domain socket: %s", strerror(errno));
1200 p_delete(&addr);
1201 csio.fd = -1;
1204 void
1205 luaA_on_timer(EV_P_ ev_timer *w, int revents)
1207 if(globalconf.hooks.timer != LUA_REFNIL)
1208 luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0, 0);
1209 awesome_refresh(globalconf.connection);
1212 /** Push a color as a string onto the stack
1213 * \param L The Lua VM state.
1214 * \param c The color to push.
1215 * \return The number of elements pushed on stack.
1218 luaA_pushcolor(lua_State *L, const xcolor_t *c)
1220 uint8_t r = (unsigned)c->red * 0xff / 0xffff;
1221 uint8_t g = (unsigned)c->green * 0xff / 0xffff;
1222 uint8_t b = (unsigned)c->blue * 0xff / 0xffff;
1223 uint8_t a = (unsigned)c->alpha * 0xff / 0xffff;
1224 char s[10];
1225 int len;
1226 /* do not print alpha if it's full */
1227 if(a == 0xff)
1228 len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
1229 else
1230 len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
1231 lua_pushlstring(L, s, len);
1232 return 1;