xcursor: add new cursor infra
[awesome.git] / luaa.c
blob50a92803123ffe151097fa661248c0b9f101bcb9
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 "layouts/tile.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_keygrabber_lib[];
59 extern const struct luaL_reg awesome_button_methods[];
60 extern const struct luaL_reg awesome_button_meta[];
61 extern const struct luaL_reg awesome_image_methods[];
62 extern const struct luaL_reg awesome_image_meta[];
63 extern const struct luaL_reg awesome_mouse_methods[];
64 extern const struct luaL_reg awesome_mouse_meta[];
65 extern const struct luaL_reg awesome_screen_methods[];
66 extern const struct luaL_reg awesome_screen_meta[];
67 extern const struct luaL_reg awesome_client_methods[];
68 extern const struct luaL_reg awesome_client_meta[];
69 extern const struct luaL_reg awesome_tag_methods[];
70 extern const struct luaL_reg awesome_tag_meta[];
71 extern const struct luaL_reg awesome_widget_methods[];
72 extern const struct luaL_reg awesome_widget_meta[];
73 extern const struct luaL_reg awesome_wibox_methods[];
74 extern const struct luaL_reg awesome_wibox_meta[];
75 extern const struct luaL_reg awesome_keybinding_methods[];
76 extern const struct luaL_reg awesome_keybinding_meta[];
78 static struct sockaddr_un *addr;
79 static ev_io csio = { .fd = -1 };
81 #define XDG_CONFIG_HOME_DEFAULT "/.config"
83 /** Get or set global mouse bindings.
84 * This binding will be available when you'll click on root window.
85 * \param L The Lua VM state.
86 * \return The number of element pushed on stack.
87 * \luastack
88 * \lvalue A client.
89 * \lparam An array of mouse button bindings objects, or nothing.
90 * \return The array of mouse button bindings objects of this client.
92 static int
93 luaA_buttons(lua_State *L)
95 button_array_t *buttons = &globalconf.buttons;
97 if(lua_gettop(L) == 1)
98 luaA_button_array_set(L, 1, buttons);
100 return luaA_button_array_get(L, buttons);
103 /** Quit awesome.
104 * \param L The Lua VM state.
105 * \return The number of elements pushed on stack.
107 static int
108 luaA_quit(lua_State *L __attribute__ ((unused)))
110 ev_unloop(globalconf.loop, 1);
111 return 0;
114 /** Execute another application, probably a window manager, to replace
115 * awesome.
116 * \param L The Lua VM state.
117 * \return The number of elements pushed on stack.
118 * \luastack
119 * \lparam The command line to execute.
121 static int
122 luaA_exec(lua_State *L)
124 const char *cmd = luaL_checkstring(L, 1);
126 awesome_atexit();
128 a_exec(cmd);
129 return 0;
132 /** Restart awesome.
134 static int
135 luaA_restart(lua_State *L __attribute__ ((unused)))
137 awesome_restart();
138 return 0;
141 static void
142 luaA_openlib(lua_State *L, const char *name,
143 const struct luaL_reg methods[],
144 const struct luaL_reg meta[])
146 luaL_newmetatable(L, name); /* 1 */
147 lua_pushvalue(L, -1); /* dup metatable 2 */
148 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
150 luaL_register(L, NULL, meta); /* 1 */
151 luaL_register(L, name, methods); /* 2 */
152 lua_pushvalue(L, -1); /* dup self as metatable 3 */
153 lua_setmetatable(L, -2); /* set self as metatable 2 */
154 lua_pop(L, 2);
157 /** UTF-8 aware string length computing.
158 * \param L The Lua VM state.
159 * \return The number of elements pushed on stack.
161 static int
162 luaA_mbstrlen(lua_State *L)
164 const char *cmd = luaL_checkstring(L, 1);
165 lua_pushnumber(L, mbstowcs(NULL, NONULL(cmd), 0));
166 return 1;
169 /** Overload standard Lua next function to use __next key on metatable.
170 * \param L The Lua VM state.
171 * \param The number of elements pushed on stack.
173 static int
174 luaAe_next(lua_State *L)
176 if(luaL_getmetafield(L, 1, "__next"))
178 lua_insert(L, 1);
179 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
180 return lua_gettop(L);
183 luaL_checktype(L, 1, LUA_TTABLE);
184 lua_settop(L, 2);
185 if(lua_next(L, 1))
186 return 2;
187 lua_pushnil(L);
188 return 1;
191 /** Overload lua_next() function by using __next metatable field
192 * to get next elements.
193 * \param L The Lua VM stack.
194 * \param idx The index number of elements in stack.
195 * \return 1 if more elements to come, 0 otherwise.
198 luaA_next(lua_State *L, int idx)
200 if(luaL_getmetafield(L, idx, "__next"))
202 /* if idx is relative, reduce it since we got __next */
203 if(idx < 0) idx--;
204 /* copy table and then move key */
205 lua_pushvalue(L, idx);
206 lua_pushvalue(L, -3);
207 lua_remove(L, -4);
208 lua_pcall(L, 2, 2, 0);
209 /* next returned nil, it's the end */
210 if(lua_isnil(L, -1))
212 /* remove nil */
213 lua_pop(L, 2);
214 return 0;
216 return 1;
218 else if(lua_istable(L, idx))
219 return lua_next(L, idx);
220 /* remove the key */
221 lua_pop(L, 1);
222 return 0;
225 /** Generic pairs function.
226 * \param L The Lua VM state.
227 * \return The number of elements pushed on stack.
229 static int
230 luaA_generic_pairs(lua_State *L)
232 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
233 lua_pushvalue(L, 1); /* state, */
234 lua_pushnil(L); /* and initial value */
235 return 3;
238 /** Overload standard pairs function to use __pairs field of metatables.
239 * \param L The Lua VM state.
240 * \return The number of elements pushed on stack.
242 static int
243 luaAe_pairs(lua_State *L)
245 if(luaL_getmetafield(L, 1, "__pairs"))
247 lua_insert(L, 1);
248 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
249 return lua_gettop(L);
252 luaL_checktype(L, 1, LUA_TTABLE);
253 return luaA_generic_pairs(L);
256 static int
257 luaA_ipairs_aux(lua_State *L)
259 int i = luaL_checkint(L, 2) + 1;
260 luaL_checktype(L, 1, LUA_TTABLE);
261 lua_pushinteger(L, i);
262 lua_rawgeti(L, 1, i);
263 return (lua_isnil(L, -1)) ? 0 : 2;
266 /** Overload standard ipairs function to use __ipairs field of metatables.
267 * \param L The Lua VM state.
268 * \return The number of elements pushed on stack.
270 static int
271 luaAe_ipairs(lua_State *L)
273 if(luaL_getmetafield(L, 1, "__ipairs"))
275 lua_insert(L, 1);
276 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
277 return lua_gettop(L);
280 luaL_checktype(L, 1, LUA_TTABLE);
281 lua_pushvalue(L, lua_upvalueindex(1));
282 lua_pushvalue(L, 1);
283 lua_pushinteger(L, 0); /* and initial value */
284 return 3;
287 /** Replace various standards Lua functions with our own.
288 * \param L The Lua VM state.
290 static void
291 luaA_fixups(lua_State *L)
293 /* replace string.len */
294 lua_getglobal(L, "string");
295 lua_pushcfunction(L, luaA_mbstrlen);
296 lua_setfield(L, -2, "len");
297 lua_pop(L, 1);
298 /* replace next */
299 lua_pushliteral(L, "next");
300 lua_pushcfunction(L, luaAe_next);
301 lua_settable(L, LUA_GLOBALSINDEX);
302 /* replace pairs */
303 lua_pushliteral(L, "pairs");
304 lua_pushcfunction(L, luaAe_next);
305 lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
306 lua_settable(L, LUA_GLOBALSINDEX);
307 /* replace ipairs */
308 lua_pushliteral(L, "ipairs");
309 lua_pushcfunction(L, luaA_ipairs_aux);
310 lua_pushcclosure(L, luaAe_ipairs, 1);
311 lua_settable(L, LUA_GLOBALSINDEX);
314 /** __next function for wtable objects.
315 * \param L The Lua VM state.
316 * \return The number of elements pushed on stack.
318 static int
319 luaA_wtable_next(lua_State *L)
321 /* upvalue 1 is content table */
322 if(lua_next(L, lua_upvalueindex(1)))
323 return 2;
324 lua_pushnil(L);
325 return 1;
328 /** __ipairs function for wtable objects.
329 * \param L The Lua VM state.
330 * \return The number of elements pushed on stack.
332 static int
333 luaA_wtable_ipairs(lua_State *L)
335 /* push ipairs_aux */
336 lua_pushvalue(L, lua_upvalueindex(2));
337 /* push content table */
338 lua_pushvalue(L, lua_upvalueindex(1));
339 lua_pushinteger(L, 0); /* and initial value */
340 return 3;
343 /** Index function of wtable objects.
344 * \param L The Lua VM state.
345 * \return The number of elements pushed on stack.
347 static int
348 luaA_wtable_index(lua_State *L)
350 size_t len;
351 const char *buf;
353 lua_pushvalue(L, 2);
354 /* check for size, waiting lua 5.2 and __len on tables */
355 if((buf = lua_tolstring(L, -1, &len)))
356 if(a_tokenize(buf, len) == A_TK_LEN)
358 lua_pushnumber(L, lua_objlen(L, lua_upvalueindex(1)));
359 return 1;
361 lua_pop(L, 1);
363 /* upvalue 1 is content table */
364 lua_rawget(L, lua_upvalueindex(1));
365 return 1;
368 /** Newndex function of wtable objects.
369 * \param L The Lua VM state.
370 * \return The number of elements pushed on stack.
372 static int
373 luaA_wtable_newindex(lua_State *L)
375 bool invalid = false;
377 /* push key on top */
378 lua_pushvalue(L, 2);
379 /* get current key value in content table */
380 lua_rawget(L, lua_upvalueindex(1));
381 /* if value is a widget, notify change */
382 if(lua_istable(L, -1) || luaA_toudata(L, -1, "widget"))
383 invalid = true;
385 lua_pop(L, 1); /* remove value */
387 /* if new value is a widget or a table */
388 if(lua_istable(L, 3))
390 luaA_table2wtable(L);
391 invalid = true;
393 else if(!invalid && luaA_toudata(L, 3, "widget"))
394 invalid = true;
396 /* upvalue 1 is content table */
397 lua_rawset(L, lua_upvalueindex(1));
399 if(invalid)
400 luaA_wibox_invalidate_byitem(L, lua_topointer(L, 1));
402 return 0;
405 /** Convert the top element of the stack to a proxied wtable.
406 * \param L The Lua VM state.
408 void
409 luaA_table2wtable(lua_State *L)
411 if(!lua_istable(L, -1))
412 return;
414 lua_newtable(L); /* create *real* content table */
415 lua_newtable(L); /* metatable */
416 lua_pushvalue(L, -2); /* copy content table */
417 lua_pushcfunction(L, luaA_ipairs_aux); /* push ipairs aux */
418 lua_pushcclosure(L, luaA_wtable_ipairs, 2);
419 lua_pushvalue(L, -3); /* copy content table */
420 lua_pushcclosure(L, luaA_wtable_next, 1); /* __next has the content table as upvalue */
421 lua_pushvalue(L, -4); /* copy content table */
422 lua_pushcclosure(L, luaA_wtable_index, 1); /* __index has the content table as upvalue */
423 lua_pushvalue(L, -5); /* copy content table */
424 lua_pushcclosure(L, luaA_wtable_newindex, 1); /* __newindex has the content table as upvalue */
425 /* set metatable field with just pushed closure */
426 lua_setfield(L, -5, "__newindex");
427 lua_setfield(L, -4, "__index");
428 lua_setfield(L, -3, "__next");
429 lua_setfield(L, -2, "__ipairs");
430 /* set metatable impossible to touch */
431 lua_pushliteral(L, "wtable");
432 lua_setfield(L, -2, "__metatable");
433 /* set new metatable on original table */
434 lua_setmetatable(L, -3);
436 /* initial key */
437 lua_pushnil(L);
438 /* go through original table */
439 while(lua_next(L, -3))
441 /* if convert value to wtable */
442 luaA_table2wtable(L);
443 /* copy key */
444 lua_pushvalue(L, -2);
445 /* move key before value */
446 lua_insert(L, -2);
447 /* set same value in content table */
448 lua_rawset(L, -4);
449 /* copy key */
450 lua_pushvalue(L, -1);
451 /* push the new value :-> */
452 lua_pushnil(L);
453 /* set orig[k] = nil */
454 lua_rawset(L, -5);
456 /* remove content table */
457 lua_pop(L, 1);
460 /** Look for an item: table, function, etc.
461 * \param L The Lua VM state.
462 * \param item The pointer item.
464 bool
465 luaA_hasitem(lua_State *L, const void *item)
467 lua_pushnil(L);
468 while(luaA_next(L, -2))
470 if(lua_topointer(L, -1) == item)
472 /* remove value and key */
473 lua_pop(L, 2);
474 return true;
476 if(lua_istable(L, -1))
477 if(luaA_hasitem(L, item))
479 /* remove key and value */
480 lua_pop(L, 2);
481 return true;
483 /* remove value */
484 lua_pop(L, 1);
486 return false;
489 /** Check if a table is a loop. When using table as direct acyclic digram,
490 * this is useful.
491 * \param L The Lua VM state.
492 * \param idx The index of the table in the stack
493 * \return True if the table loops.
495 bool
496 luaA_isloop(lua_State *L, int idx)
498 if(lua_istable(L, idx))
500 lua_pushvalue(L, idx); /* push table on top */
501 if(luaA_hasitem(L, lua_topointer(L, -1)))
503 lua_pop(L, 1); /* remove pushed table */
504 return true;
506 lua_pushnil(L);
507 while(luaA_next(L, -2))
509 /* check for recursivity */
510 if(luaA_isloop(L, -1))
512 lua_pop(L, 2); /* remove key and value */
513 return true;
515 lua_pop(L, 1); /* remove value */
517 lua_pop(L, 1); /* remove pushed table */
519 return false;
522 /** Object table.
523 * This table can use safely object as key.
524 * \param L The Lua VM state.
525 * \return The number of elements pushed on stack.
528 luaA_otable_index(lua_State *L)
530 void **obj, **v;
532 if((obj = lua_touserdata(L, 2)))
534 /* begins at nil */
535 lua_pushnil(L);
536 while(lua_next(L, 1))
538 /* check the key against the key if the key is a userdata,
539 * otherwise check it again the value. */
540 if((v = lua_touserdata(L, -2))
541 && *v == *obj)
542 /* return value */
543 return 1;
544 else if((v = lua_touserdata(L, -1))
545 && *v == *obj)
547 /* return key */
548 lua_pop(L, 1);
549 return 1;
551 /* removes 'value'; keeps 'key' for next iteration */
552 lua_pop(L, 1);
554 return 0;
557 lua_rawget(L, 1);
558 return 1;
561 /** Object table.
562 * This table can use safely object as key.
563 * \param L The Lua VM state.
564 * \return The number of elements pushed on stack.
566 static int
567 luaA_otable_newindex(lua_State *L)
569 void **obj, **v;
571 if((obj = lua_touserdata(L, 2)))
573 /* begins at nil */
574 lua_pushnil(L);
575 while(lua_next(L, 1))
577 if((v = lua_touserdata(L, -2))
578 && *v == *obj)
580 /* remove value */
581 lua_pop(L, 1);
582 /* push new value on top */
583 lua_pushvalue(L, 3);
584 /* set in table key = value */
585 lua_rawset(L, 1);
586 return 0;
588 /* removes 'value'; keeps 'key' for next iteration */
589 lua_pop(L, 1);
593 lua_rawset(L, 1);
595 return 0;
598 /** Spawn a program.
599 * This function is multi-head (Zaphod) aware and will set display to
600 * the right screen according to mouse position.
601 * \param L The Lua VM state.
602 * \return The number of elements pushed on stack
603 * \luastack
604 * \lparam The command to launch.
605 * \lparam The optional screen number to spawn the command on.
607 static int
608 luaA_spawn(lua_State *L)
610 char *host, newdisplay[128];
611 const char *cmd;
612 int screen = 0, screenp, displayp;
614 if(lua_gettop(L) == 2)
616 screen = luaL_checknumber(L, 2) - 1;
617 luaA_checkscreen(screen);
620 cmd = luaL_checkstring(L, 1);
622 if(!globalconf.xinerama_is_active)
624 xcb_parse_display(NULL, &host, &displayp, &screenp);
625 snprintf(newdisplay, sizeof(newdisplay), "%s:%d.%d", host, displayp, screen);
626 setenv("DISPLAY", newdisplay, 1);
627 p_delete(&host);
630 /* The double-fork construct avoids zombie processes and keeps the code
631 * clean from stupid signal handlers. */
632 if(fork() == 0)
634 if(fork() == 0)
636 if(globalconf.connection)
637 xcb_disconnect(globalconf.connection);
638 setsid();
639 a_exec(cmd);
640 warn("execl '%s' failed: %s\n", cmd, strerror(errno));
642 exit(EXIT_SUCCESS);
644 wait(0);
646 return 0;
649 /** awesome global table.
650 * \param L The Lua VM state.
651 * \return The number of elements pushed on stack.
652 * \luastack
653 * \lfield font The default font.
654 * \lfield conffile The configuration file which has been loaded.
656 static int
657 luaA_awesome_index(lua_State *L)
659 if(luaA_usemetatable(L, 1, 2))
660 return 1;
662 size_t len;
663 const char *buf = luaL_checklstring(L, 2, &len);
665 switch(a_tokenize(buf, len))
667 case A_TK_FONT:
669 char *font = pango_font_description_to_string(globalconf.font->desc);
670 lua_pushstring(L, font);
671 g_free(font);
673 break;
674 case A_TK_CONFFILE:
675 lua_pushstring(L, globalconf.conffile);
676 break;
677 case A_TK_FG:
678 luaA_pushcolor(L, &globalconf.colors.fg);
679 break;
680 case A_TK_BG:
681 luaA_pushcolor(L, &globalconf.colors.bg);
682 break;
683 default:
684 return 0;
687 return 1;
690 /** Newindex function for the awesome global table.
691 * \param L The Lua VM state.
692 * \return The number of elements pushed on stack.
694 static int
695 luaA_awesome_newindex(lua_State *L)
697 if(luaA_usemetatable(L, 1, 2))
698 return 1;
700 size_t len;
701 const char *buf = luaL_checklstring(L, 2, &len);
703 switch(a_tokenize(buf, len))
705 case A_TK_FONT:
707 const char *newfont = luaL_checkstring(L, 3);
708 draw_font_delete(&globalconf.font);
709 globalconf.font = draw_font_new(newfont);
711 break;
712 case A_TK_FG:
713 if((buf = luaL_checklstring(L, 3, &len)))
714 xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.fg, buf, len));
715 break;
716 case A_TK_BG:
717 if((buf = luaL_checklstring(L, 3, &len)))
718 xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.bg, buf, len));
719 break;
720 default:
721 return 0;
724 return 0;
727 /** Initialize the Lua VM
729 void
730 luaA_init(void)
732 lua_State *L;
734 static const struct luaL_reg otable_methods[] =
736 { "__call", luaA_otable_new },
737 { NULL, NULL }
739 static const struct luaL_reg otable_meta[] =
741 { "__index", luaA_otable_index },
742 { "__newindex", luaA_otable_newindex },
743 { NULL, NULL }
745 static const struct luaL_reg awesome_lib[] =
747 { "quit", luaA_quit },
748 { "exec", luaA_exec },
749 { "spawn", luaA_spawn },
750 { "restart", luaA_restart },
751 { "buttons", luaA_buttons },
752 { "__index", luaA_awesome_index },
753 { "__newindex", luaA_awesome_newindex },
754 { NULL, NULL }
757 L = globalconf.L = luaL_newstate();
759 luaL_openlibs(L);
761 luaA_fixups(L);
763 /* Export awesome lib */
764 luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
766 /* Export hooks lib */
767 luaL_register(L, "hooks", awesome_hooks_lib);
769 /* Export keygrabber lib */
770 luaL_register(L, "keygrabber", awesome_keygrabber_lib);
772 /* Export otable lib */
773 luaA_openlib(L, "otable", otable_methods, otable_meta);
775 /* Export screen */
776 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
778 /* Export mouse */
779 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
781 /* Export button */
782 luaA_openlib(L, "button", awesome_button_methods, awesome_button_meta);
784 /* Export image */
785 luaA_openlib(L, "image", awesome_image_methods, awesome_image_meta);
787 /* Export tag */
788 luaA_openlib(L, "tag", awesome_tag_methods, awesome_tag_meta);
790 /* Export wibox */
791 luaA_openlib(L, "wibox", awesome_wibox_methods, awesome_wibox_meta);
793 /* Export widget */
794 luaA_openlib(L, "widget", awesome_widget_methods, awesome_widget_meta);
796 /* Export client */
797 luaA_openlib(L, "client", awesome_client_methods, awesome_client_meta);
799 /* Export keys */
800 luaA_openlib(L, "keybinding", awesome_keybinding_methods, awesome_keybinding_meta);
802 lua_pushliteral(L, "AWESOME_VERSION");
803 lua_pushstring(L, AWESOME_VERSION);
804 lua_settable(L, LUA_GLOBALSINDEX);
806 lua_pushliteral(L, "AWESOME_RELEASE");
807 lua_pushstring(L, AWESOME_RELEASE);
808 lua_settable(L, LUA_GLOBALSINDEX);
810 /* init hooks */
811 globalconf.hooks.manage = LUA_REFNIL;
812 globalconf.hooks.unmanage = LUA_REFNIL;
813 globalconf.hooks.focus = LUA_REFNIL;
814 globalconf.hooks.unfocus = LUA_REFNIL;
815 globalconf.hooks.mouse_enter = LUA_REFNIL;
816 globalconf.hooks.arrange = LUA_REFNIL;
817 globalconf.hooks.clients = LUA_REFNIL;
818 globalconf.hooks.tags = LUA_REFNIL;
819 globalconf.hooks.tagged = LUA_REFNIL;
820 globalconf.hooks.property = LUA_REFNIL;
821 globalconf.hooks.timer = LUA_REFNIL;
823 /* add Lua lib path (/usr/share/awesome/lib by default) */
824 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?.lua\"");
825 luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?/init.lua\"");
827 /* add XDG_CONFIG_DIR (/etc/xdg/awesome by default) as include path */
828 luaA_dostring(L, "package.path = package.path .. \";" XDG_CONFIG_DIR "/awesome/?.lua\"");
829 luaA_dostring(L, "package.path = package.path .. \";" XDG_CONFIG_DIR "/awesome/?/init.lua\"");
831 /* add either XDG_CONFIG_HOME/awesome or HOME/.config/awesome to path */
832 char *dir;
833 if((dir = getenv("XDG_CONFIG_HOME")))
835 char *path;
836 a_asprintf(&path, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"", dir, dir);
837 luaA_dostring(globalconf.L, path);
838 p_delete(&path);
840 else
842 char *path, *homedir = getenv("HOME");
843 a_asprintf(&path,
844 "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?.lua;%s" XDG_CONFIG_HOME_DEFAULT "/awesome/?/init.lua\"",
845 homedir, homedir);
846 luaA_dostring(globalconf.L, path);
847 p_delete(&path);
850 /* add XDG_CONFIG_DIRS to include paths */
851 char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
852 ssize_t len;
854 if((len = a_strlen(xdg_config_dirs)))
856 char **buf, **xdg_files = a_strsplit(xdg_config_dirs, len, ':');
858 for(buf = xdg_files; *buf; buf++)
860 char *confpath;
861 a_asprintf(&confpath, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"",
862 *buf, *buf);
863 luaA_dostring(globalconf.L, confpath);
864 p_delete(&confpath);
867 for(buf = xdg_files; *buf; buf++)
868 p_delete(buf);
869 p_delete(&xdg_files);
873 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
875 static bool
876 luaA_loadrc(const char *confpath, bool run)
878 if(confpath)
880 if(!luaL_loadfile(globalconf.L, confpath))
882 if(run)
884 if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
885 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
886 else
888 globalconf.conffile = a_strdup(confpath);
889 return true;
892 else
893 lua_pop(globalconf.L, 1);
894 return true;
896 else
897 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
899 return false;
902 /** Load a configuration file.
903 * \param confpatharg The configuration file to load.
904 * \param run Run the configuration file.
906 bool
907 luaA_parserc(const char *confpatharg, bool run)
909 int screen;
910 const char *confdir, *xdg_config_dirs;
911 char *confpath = NULL, **xdg_files = NULL, **buf;
912 ssize_t len;
913 bool ret = false;
915 /* try to load, return if it's ok */
916 if(luaA_loadrc(confpatharg, run))
918 ret = true;
919 goto bailout;
922 if((confdir = getenv("XDG_CONFIG_HOME")))
923 a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, confdir);
924 else
925 a_asprintf(&confpath, "%s" XDG_CONFIG_HOME_DEFAULT AWESOME_CONFIG_FILE, getenv("HOME"));
927 /* try to run XDG_CONFIG_HOME/awesome/rc.lua */
928 if(luaA_loadrc(confpath, run))
930 ret = true;
931 goto bailout;
934 p_delete(&confpath);
936 xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
938 if(!(len = a_strlen(xdg_config_dirs)))
940 xdg_config_dirs = XDG_CONFIG_DIR;
941 len = sizeof(XDG_CONFIG_DIR) - 1;
944 xdg_files = a_strsplit(xdg_config_dirs, len, ':');
946 for(buf = xdg_files; *buf && !ret; buf++)
948 a_asprintf(&confpath, "%s" AWESOME_CONFIG_FILE, *buf);
949 if(luaA_loadrc(confpath, run))
951 ret = true;
952 goto bailout;
954 p_delete(&confpath);
957 bailout:
959 p_delete(&confpath);
961 if(xdg_files)
963 for(buf = xdg_files; *buf; buf++)
964 p_delete(buf);
965 p_delete(&xdg_files);
968 /* Assure there's at least one tag */
969 for(screen = 0; screen < globalconf.nscreen; screen++)
970 if(!globalconf.screens[screen].tags.len)
971 tag_append_to_screen(tag_new("default", sizeof("default") - 1, layout_tile, 0.5, 1, 0),
972 &globalconf.screens[screen]);
973 return ret;
976 /** Parse a command.
977 * \param cmd The buffer to parse.
978 * \return the number of elements pushed on the stack by the last statement in cmd.
979 * If there's an error, the message is pushed onto the stack and this returns 1.
981 static int
982 luaA_docmd(const char *cmd)
984 char *p;
985 int newtop, oldtop = lua_gettop(globalconf.L);
987 while((p = strchr(cmd, '\n')))
989 newtop = lua_gettop(globalconf.L);
990 lua_pop(globalconf.L, newtop - oldtop);
991 oldtop = newtop;
993 *p = '\0';
994 if (luaL_dostring(globalconf.L, cmd))
996 warn("error executing Lua code: %s", lua_tostring(globalconf.L, -1));
997 return 1;
999 cmd = p + 1;
1001 return lua_gettop(globalconf.L) - oldtop;
1004 /** Pushes a Lua array containing the top n elements of the stack.
1005 * \param n The number of elements to put in the array.
1007 static void
1008 luaA_array(int n)
1010 int i;
1011 lua_createtable(globalconf.L, n, 0);
1012 lua_insert(globalconf.L, -n - 1);
1014 for (i = n; i > 0; i--)
1015 lua_rawseti(globalconf.L, -i - 1, i);
1018 /** Maps the top n elements of the stack to the result of
1019 * applying a function to that element.
1020 * \param n The number of elements to map.
1021 * \luastack
1022 * \lparam The function to map the elements by. This should be
1023 * at position -(n + 1).
1025 static void
1026 luaA_map(int n)
1028 int i;
1029 for (i = 0; i < n; i++)
1031 lua_pushvalue(globalconf.L, -n - 1); /* copy of the function */
1032 lua_pushvalue(globalconf.L, -n - 1); /* value to map */
1033 lua_pcall(globalconf.L, 1, 1, 0); /* call function */
1034 lua_remove(globalconf.L, -n - 1); /* remove old value */
1036 lua_remove(globalconf.L, -n - 1); /* remove function */
1039 static void luaA_conn_cleanup(EV_P_ ev_io *w)
1041 ev_ref(EV_DEFAULT_UC);
1042 ev_io_stop(EV_DEFAULT_UC_ w);
1043 if (close(w->fd))
1044 warn("error closing UNIX domain socket: %s", strerror(errno));
1045 p_delete(&w);
1048 static void
1049 luaA_cb(EV_P_ ev_io *w, int revents)
1051 char buf[1024];
1052 int r, els;
1053 const char *s;
1054 size_t len;
1056 switch(r = recv(w->fd, buf, sizeof(buf)-1, MSG_TRUNC))
1058 case -1:
1059 warn("error reading UNIX domain socket: %s", strerror(errno));
1060 case 0: /* 0 bytes are only transferred when the connection is closed */
1061 luaA_conn_cleanup(EV_DEFAULT_UC_ w);
1062 break;
1063 default:
1064 if(r >= ssizeof(buf))
1065 break;
1066 buf[r] = '\0';
1067 lua_getglobal(globalconf.L, "table");
1068 lua_getfield(globalconf.L, -1, "concat");
1069 lua_remove(globalconf.L, -2); /* remove table */
1071 lua_getglobal(globalconf.L, "tostring");
1072 els = luaA_docmd(buf);
1073 luaA_map(els); /* map results to strings */
1074 luaA_array(els); /* put strings in an array */
1076 lua_pushstring(globalconf.L, "\t");
1077 lua_pcall(globalconf.L, 2, 1, 0); /* concatenate results with tabs */
1079 s = lua_tolstring(globalconf.L, -1, &len);
1081 /* ignore ENOENT because the client may not read */
1082 if (send(w->fd, s, len, MSG_DONTWAIT) == -1)
1083 switch(errno)
1085 case ENOENT:
1086 case EAGAIN:
1087 break;
1088 default:
1089 warn("can't send back to client via domain socket: %s", strerror(errno));
1090 break;
1093 lua_pop(globalconf.L, 1); /* pop the string */
1095 awesome_refresh(globalconf.connection);
1099 static void
1100 luaA_conn_cb(EV_P_ ev_io *w, int revents)
1102 ev_io *csio_conn = p_new(ev_io, 1);
1103 int csfd = accept(w->fd, NULL, NULL);
1105 ev_io_init(csio_conn, &luaA_cb, csfd, EV_READ);
1106 ev_io_start(EV_DEFAULT_UC_ csio_conn);
1107 ev_unref(EV_DEFAULT_UC);
1110 void
1111 luaA_cs_init(void)
1113 int csfd = socket_getclient();
1115 if (csfd < 0 || fcntl(csfd, F_SETFD, FD_CLOEXEC) == -1)
1116 return;
1118 addr = socket_getaddr(getenv("DISPLAY"));
1120 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1122 if(errno == EADDRINUSE)
1124 if(unlink(addr->sun_path))
1125 warn("error unlinking existing file: %s", strerror(errno));
1126 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
1127 return warn("error binding UNIX domain socket: %s", strerror(errno));
1129 else
1130 return warn("error binding UNIX domain socket: %s", strerror(errno));
1132 listen(csfd, 10);
1134 ev_io_init(&csio, &luaA_conn_cb, csfd, EV_READ);
1135 ev_io_start(EV_DEFAULT_UC_ &csio);
1136 ev_unref(EV_DEFAULT_UC);
1139 void
1140 luaA_cs_cleanup(void)
1142 if(csio.fd < 0)
1143 return;
1144 ev_ref(EV_DEFAULT_UC);
1145 ev_io_stop(EV_DEFAULT_UC_ &csio);
1146 if (close(csio.fd))
1147 warn("error closing UNIX domain socket: %s", strerror(errno));
1148 if(unlink(addr->sun_path))
1149 warn("error unlinking UNIX domain socket: %s", strerror(errno));
1150 p_delete(&addr);
1151 csio.fd = -1;
1154 void
1155 luaA_on_timer(EV_P_ ev_timer *w, int revents)
1157 if(globalconf.hooks.timer != LUA_REFNIL)
1158 luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0, 0);
1159 awesome_refresh(globalconf.connection);
1162 /** Push a color as a string onto the stack
1163 * \param L The Lua VM state.
1164 * \param c The color to push.
1165 * \return The number of elements pushed on stack.
1168 luaA_pushcolor(lua_State *L, const xcolor_t *c)
1170 uint8_t r = (unsigned)c->red * 0xff / 0xffff;
1171 uint8_t g = (unsigned)c->green * 0xff / 0xffff;
1172 uint8_t b = (unsigned)c->blue * 0xff / 0xffff;
1173 uint8_t a = (unsigned)c->alpha * 0xff / 0xffff;
1174 char s[10];
1175 int len;
1176 /* do not print alpha if it's full */
1177 if(a == 0xff)
1178 len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
1179 else
1180 len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
1181 lua_pushlstring(L, s, len);
1182 return 1;