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.
24 #include <sys/socket.h>
27 #include <sys/types.h>
41 #include "awesome-version-internal.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_keygrabber_lib
[];
58 extern const struct luaL_reg awesome_button_methods
[];
59 extern const struct luaL_reg awesome_button_meta
[];
60 extern const struct luaL_reg awesome_image_methods
[];
61 extern const struct luaL_reg awesome_image_meta
[];
62 extern const struct luaL_reg awesome_mouse_methods
[];
63 extern const struct luaL_reg awesome_mouse_meta
[];
64 extern const struct luaL_reg awesome_screen_methods
[];
65 extern const struct luaL_reg awesome_screen_meta
[];
66 extern const struct luaL_reg awesome_client_methods
[];
67 extern const struct luaL_reg awesome_client_meta
[];
68 extern const struct luaL_reg awesome_titlebar_methods
[];
69 extern const struct luaL_reg awesome_titlebar_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_statusbar_methods
[];
75 extern const struct luaL_reg awesome_statusbar_meta
[];
76 extern const struct luaL_reg awesome_wibox_methods
[];
77 extern const struct luaL_reg awesome_wibox_meta
[];
78 extern const struct luaL_reg awesome_keybinding_methods
[];
79 extern const struct luaL_reg awesome_keybinding_meta
[];
81 static struct sockaddr_un
*addr
;
82 static ev_io csio
= { .fd
= -1 };
84 /** Get or set global mouse bindings.
85 * This binding will be available when you'll click on root window.
86 * \param L The Lua VM state.
87 * \return The number of element pushed on stack.
90 * \lparam An array of mouse button bindings objects, or nothing.
91 * \return The array of mouse button bindings objects of this client.
94 luaA_buttons(lua_State
*L
)
96 button_array_t
*buttons
= &globalconf
.buttons
;
98 if(lua_gettop(L
) == 1)
99 luaA_button_array_set(L
, 1, buttons
);
101 return luaA_button_array_get(L
, buttons
);
105 * \param L The Lua VM state.
106 * \return The number of elements pushed on stack.
109 luaA_quit(lua_State
*L
__attribute__ ((unused
)))
111 ev_unloop(globalconf
.loop
, 1);
115 /** Execute another application, probably a window manager, to replace
117 * \param L The Lua VM state.
118 * \return The number of elements pushed on stack.
120 * \lparam The command line to execute.
123 luaA_exec(lua_State
*L
)
125 const char *cmd
= luaL_checkstring(L
, 1);
136 luaA_restart(lua_State
*L
__attribute__ ((unused
)))
142 /** Set the function called each time a client gets focus. This function is
143 * called with the client object as argument.
144 * \param L The Lua VM state.
145 * \return The number of elements pushed on stack.
147 * \lparam A function to call each time a client gets focus.
150 luaA_hooks_focus(lua_State
*L
)
152 return luaA_registerfct(L
, 1, &globalconf
.hooks
.focus
);
155 /** Set the function called each time a client loses focus. This function is
156 * called with the client object as argument.
157 * \param L The Lua VM state.
158 * \return The number of elements pushed on stack.
160 * \lparam A function to call each time a client loses focus.
163 luaA_hooks_unfocus(lua_State
*L
)
165 return luaA_registerfct(L
, 1, &globalconf
.hooks
.unfocus
);
168 /** Set the function called each time a new client appears. This function is
169 * called with the client object as argument.
170 * \param L The Lua VM state.
171 * \return The number of elements pushed on stack.
173 * \lparam A function to call on each new client.
176 luaA_hooks_manage(lua_State
*L
)
178 return luaA_registerfct(L
, 1, &globalconf
.hooks
.manage
);
181 /** Set the function called each time a client goes away. This function is
182 * called with the client object as argument.
183 * \param L The Lua VM state.
184 * \return The number of elements pushed on stack.
186 * \lparam A function to call when a client goes away.
189 luaA_hooks_unmanage(lua_State
*L
)
191 return luaA_registerfct(L
, 1, &globalconf
.hooks
.unmanage
);
194 /** Set the function called each time the mouse enter a new window. This
195 * function is called with the client object as argument.
196 * \param L The Lua VM state.
197 * \return The number of elements pushed on stack.
199 * \lparam A function to call each time a client gets mouse over it.
202 luaA_hooks_mouse_enter(lua_State
*L
)
204 return luaA_registerfct(L
, 1, &globalconf
.hooks
.mouse_enter
);
207 /** Set the function called each time the mouse enter a new window. This
208 * function is called with the client object as argument. (DEPRECATED)
209 * \param L The Lua VM state.
210 * \return The number of elements pushed on stack.
212 * \lparam A function to call each time a client gets mouse over it.
215 luaA_hooks_mouse_over(lua_State
*L
)
218 return luaA_hooks_mouse_enter(L
);
221 /** Set the function called on each screen arrange. This function is called
222 * with the screen number as argument.
223 * \param L The Lua VM state.
224 * \return The number of elements pushed on stack.
226 * \lparam A function to call on each screen arrange.
229 luaA_hooks_arrange(lua_State
*L
)
231 return luaA_registerfct(L
, 1, &globalconf
.hooks
.arrange
);
234 /** Set the function called on each client's property change.
235 * This function is called with the client object as argument and the
237 * \param L The Lua VM state.
238 * \return The number of elements pushed on stack.
240 * \lparam A function to call on each client property update.
243 luaA_hooks_property(lua_State
*L
)
245 return luaA_registerfct(L
, 1, &globalconf
.hooks
.property
);
248 /** Set the function to be called every N seconds.
249 * \param L The Lua VM state.
250 * \return The number of elements pushed on stack.
252 * \lparam The number of seconds to run function every. Set 0 to disable.
253 * \lparam A function to call every N seconds (optional).
256 luaA_hooks_timer(lua_State
*L
)
258 globalconf
.timer
.repeat
= luaL_checknumber(L
, 1);
260 if(lua_gettop(L
) == 2 && !lua_isnil(L
, 2))
261 luaA_registerfct(L
, 2, &globalconf
.hooks
.timer
);
263 ev_timer_again(globalconf
.loop
, &globalconf
.timer
);
267 /** Set or get default font. (DEPRECATED)
268 * \param L The Lua VM state.
269 * \return The number of elements pushed on stack.
271 * \lparam An optional string with a font name in Pango format.
272 * \lreturn The font used, in Pango format.
275 luaA_font(lua_State
*L
)
279 if(lua_gettop(L
) == 1)
281 const char *newfont
= luaL_checkstring(L
, 1);
282 draw_font_delete(&globalconf
.font
);
283 globalconf
.font
= draw_font_new(globalconf
.default_screen
, newfont
);
286 font
= pango_font_description_to_string(globalconf
.font
->desc
);
287 lua_pushstring(L
, font
);
293 /** Set default font. (DEPRECATED)
294 * \param L The Lua VM state.
295 * \return The number of elements pushed on stack.
297 * \lparam A string with a font name in Pango format.
300 luaA_font_set(lua_State
*L
)
306 /** Get configuration file path used by awesome.
307 * \param L The Lua VM state.
308 * \return The number of elements pushed on stack.
310 * \lreturn The awesome configuration file path.
313 luaA_conffile(lua_State
*L
)
315 lua_pushstring(L
, globalconf
.conffile
);
319 /** Set default colors.
320 * \param L The Lua VM state.
321 * \return The number of elements pushed on stack.
323 * \lparam A table with `fg' and `bg' elements, containing colors.
324 * \lreturn A table with `fg' and `bg' elements, containing colors.
327 luaA_colors(lua_State
*L
)
329 if(lua_gettop(L
) == 1)
333 int8_t colors_nbr
= -1, i
;
334 xcolor_init_request_t reqs
[2];
336 luaA_checktable(L
, 1);
338 if((buf
= luaA_getopt_lstring(L
, 1, "fg", NULL
, &len
)))
339 reqs
[++colors_nbr
] = xcolor_init_unchecked(&globalconf
.colors
.fg
, buf
, len
);
341 if((buf
= luaA_getopt_lstring(L
, 1, "bg", NULL
, &len
)))
342 reqs
[++colors_nbr
] = xcolor_init_unchecked(&globalconf
.colors
.bg
, buf
, len
);
344 for(i
= 0; i
<= colors_nbr
; i
++)
345 xcolor_init_reply(reqs
[i
]);
349 luaA_pushcolor(L
, &globalconf
.colors
.fg
);
350 lua_setfield(L
, -2, "fg");
351 luaA_pushcolor(L
, &globalconf
.colors
.bg
);
352 lua_setfield(L
, -2, "bg");
357 /** Set default colors. (DEPRECATED)
358 * \param L The Lua VM state.
359 * \return The number of elements pushed on stack.
362 * \lparam A table with `fg' and `bg' elements, containing colors.
365 luaA_colors_set(lua_State
*L
)
368 return luaA_colors(L
);
372 luaA_openlib(lua_State
*L
, const char *name
,
373 const struct luaL_reg methods
[],
374 const struct luaL_reg meta
[])
376 luaL_newmetatable(L
, name
); /* 1 */
377 lua_pushvalue(L
, -1); /* dup metatable 2 */
378 lua_setfield(L
, -2, "__index"); /* metatable.__index = metatable 1 */
380 luaL_register(L
, NULL
, meta
); /* 1 */
381 luaL_register(L
, name
, methods
); /* 2 */
382 lua_pushvalue(L
, -1); /* dup self as metatable 3 */
383 lua_setmetatable(L
, -2); /* set self as metatable 2 */
388 luaA_mbstrlen(lua_State
*L
)
390 const char *cmd
= luaL_checkstring(L
, 1);
391 lua_pushnumber(L
, mbstowcs(NULL
, NONULL(cmd
), 0));
396 luaA_next(lua_State
*L
)
398 if(luaL_getmetafield(L
, 1, "__next"))
401 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
402 return lua_gettop(L
);
405 luaL_checktype(L
, 1, LUA_TTABLE
);
414 luaA_pairs(lua_State
*L
)
416 if(luaL_getmetafield(L
, 1, "__pairs"))
419 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
420 return lua_gettop(L
);
423 luaL_checktype(L
, 1, LUA_TTABLE
);
424 return luaA_generic_pairs(L
);
427 /** Replace various standards Lua functions with our own.
428 * \param L The Lua VM state.
431 luaA_fixups(lua_State
*L
)
433 lua_getglobal(L
, "string");
434 lua_pushcfunction(L
, luaA_mbstrlen
);
435 lua_setfield(L
, -2, "len");
437 lua_pushliteral(L
, "next");
438 lua_pushcfunction(L
, luaA_next
);
439 lua_settable(L
, LUA_GLOBALSINDEX
);
440 lua_pushliteral(L
, "pairs");
441 lua_pushcfunction(L
, luaA_next
);
442 lua_pushcclosure(L
, luaA_pairs
, 1); /* pairs get next as upvalue */
443 lua_settable(L
, LUA_GLOBALSINDEX
);
447 * This table can use safely object as key.
448 * \param L The Lua VM state.
449 * \return The number of elements pushed on stack.
452 luaA_otable_index(lua_State
*L
)
456 if((obj
= lua_touserdata(L
, 2)))
460 while(lua_next(L
, 1))
462 /* check the key against the key if the key is a userdata,
463 * otherwise check it again the value. */
464 if((v
= lua_touserdata(L
, -2))
468 else if((v
= lua_touserdata(L
, -1))
475 /* removes 'value'; keeps 'key' for next iteration */
486 * This table can use safely object as key.
487 * \param L The Lua VM state.
488 * \return The number of elements pushed on stack.
491 luaA_otable_newindex(lua_State
*L
)
495 if((obj
= lua_touserdata(L
, 2)))
499 while(lua_next(L
, 1))
501 if((v
= lua_touserdata(L
, -2))
506 /* push new value on top */
508 /* set in table key = value */
512 /* removes 'value'; keeps 'key' for next iteration */
523 * This function is multi-head (Zaphod) aware and will set display to
524 * the right screen according to mouse position.
525 * \param L The Lua VM state.
526 * \return The number of elements pushed on stack
528 * \lparam The command to launch.
529 * \lparam The optional screen number to spawn the command on.
532 luaA_spawn(lua_State
*L
)
534 char *host
, newdisplay
[128];
536 int screen
= 0, screenp
, displayp
;
538 if(lua_gettop(L
) == 2)
540 screen
= luaL_checknumber(L
, 2) - 1;
541 luaA_checkscreen(screen
);
544 cmd
= luaL_checkstring(L
, 1);
546 if(!globalconf
.xinerama_is_active
)
548 xcb_parse_display(NULL
, &host
, &displayp
, &screenp
);
549 snprintf(newdisplay
, sizeof(newdisplay
), "%s:%d.%d", host
, displayp
, screen
);
550 setenv("DISPLAY", newdisplay
, 1);
554 /* The double-fork construct avoids zombie processes and keeps the code
555 * clean from stupid signal handlers. */
560 if(globalconf
.connection
)
561 xcb_disconnect(globalconf
.connection
);
564 warn("execl '%s' failed: %s\n", cmd
, strerror(errno
));
573 /** Initialize the Lua VM
580 static const struct luaL_reg otable_methods
[] =
582 { "__call", luaA_otable_new
},
585 static const struct luaL_reg otable_meta
[] =
587 { "__index", luaA_otable_index
},
588 { "__newindex", luaA_otable_newindex
},
591 static const struct luaL_reg awesome_lib
[] =
593 { "quit", luaA_quit
},
594 { "exec", luaA_exec
},
595 { "spawn", luaA_spawn
},
596 { "restart", luaA_restart
},
597 { "buttons", luaA_buttons
},
598 { "font_set", luaA_font_set
},
599 { "colors_set", luaA_colors_set
},
600 { "font", luaA_font
},
601 { "colors", luaA_colors
},
602 { "conffile", luaA_conffile
},
605 static const struct luaL_reg awesome_hooks_lib
[] =
607 { "focus", luaA_hooks_focus
},
608 { "unfocus", luaA_hooks_unfocus
},
609 { "manage", luaA_hooks_manage
},
610 { "unmanage", luaA_hooks_unmanage
},
611 { "mouse_enter", luaA_hooks_mouse_enter
},
612 { "property", luaA_hooks_property
},
613 { "arrange", luaA_hooks_arrange
},
614 { "timer", luaA_hooks_timer
},
616 { "mouse_over", luaA_hooks_mouse_over
},
620 L
= globalconf
.L
= luaL_newstate();
626 /* Export awesome lib */
627 luaL_register(L
, "awesome", awesome_lib
);
629 /* Export hooks lib */
630 luaL_register(L
, "hooks", awesome_hooks_lib
);
632 /* Export keygrabber lib */
633 luaL_register(L
, "keygrabber", awesome_keygrabber_lib
);
635 /* Export otable lib */
636 luaA_openlib(L
, "otable", otable_methods
, otable_meta
);
639 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
642 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
645 luaA_openlib(L
, "button", awesome_button_methods
, awesome_button_meta
);
648 luaA_openlib(L
, "image", awesome_image_methods
, awesome_image_meta
);
651 luaA_openlib(L
, "tag", awesome_tag_methods
, awesome_tag_meta
);
653 /* Export statusbar */
654 luaA_openlib(L
, "statusbar", awesome_statusbar_methods
, awesome_statusbar_meta
);
657 luaA_openlib(L
, "wibox", awesome_wibox_methods
, awesome_wibox_meta
);
660 luaA_openlib(L
, "widget", awesome_widget_methods
, awesome_widget_meta
);
663 luaA_openlib(L
, "client", awesome_client_methods
, awesome_client_meta
);
665 /* Export titlebar */
666 luaA_openlib(L
, "titlebar", awesome_titlebar_methods
, awesome_titlebar_meta
);
669 luaA_openlib(L
, "keybinding", awesome_keybinding_methods
, awesome_keybinding_meta
);
671 lua_pushliteral(L
, "AWESOME_VERSION");
672 lua_pushstring(L
, AWESOME_VERSION
);
673 lua_settable(L
, LUA_GLOBALSINDEX
);
675 lua_pushliteral(L
, "AWESOME_RELEASE");
676 lua_pushstring(L
, AWESOME_RELEASE
);
677 lua_settable(L
, LUA_GLOBALSINDEX
);
679 luaA_dostring(L
, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH
"/?.lua\"");
680 luaA_dostring(L
, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH
"/?/init.lua\"");
683 globalconf
.hooks
.manage
= LUA_REFNIL
;
684 globalconf
.hooks
.unmanage
= LUA_REFNIL
;
685 globalconf
.hooks
.focus
= LUA_REFNIL
;
686 globalconf
.hooks
.unfocus
= LUA_REFNIL
;
687 globalconf
.hooks
.mouse_enter
= LUA_REFNIL
;
688 globalconf
.hooks
.arrange
= LUA_REFNIL
;
689 globalconf
.hooks
.property
= LUA_REFNIL
;
690 globalconf
.hooks
.timer
= LUA_REFNIL
;
693 #define XDG_CONFIG_HOME_DEFAULT "/.config"
695 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
698 luaA_loadrc(const char *confpath
, bool run
)
702 if(!luaL_loadfile(globalconf
.L
, confpath
))
706 if(lua_pcall(globalconf
.L
, 0, LUA_MULTRET
, 0))
707 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
710 globalconf
.conffile
= a_strdup(confpath
);
715 lua_pop(globalconf
.L
, 1);
719 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
724 /** Load a configuration file.
725 * \param confpatharg The configuration file to load.
726 * \param run Run the configuration file.
729 luaA_parserc(const char *confpatharg
, bool run
)
732 const char *confdir
, *xdg_config_dirs
;
733 char *confpath
= NULL
, **xdg_files
, **buf
, path
[1024];
737 ret
= luaA_loadrc(confpatharg
, run
);
739 confdir
= getenv("XDG_CONFIG_HOME");
741 if((len
= a_strlen(confdir
)))
743 len
+= sizeof(AWESOME_CONFIG_FILE
);
744 confpath
= p_new(char, len
);
745 a_strcpy(confpath
, len
, confdir
);
746 /* update package.path */
747 snprintf(path
, sizeof(path
), "package.path = package.path .. \";%s/awesome/?.lua\"", confdir
);
748 luaA_dostring(globalconf
.L
, path
);
752 confdir
= getenv("HOME");
753 len
= a_strlen(confdir
) + sizeof(AWESOME_CONFIG_FILE
)-1 + sizeof(XDG_CONFIG_HOME_DEFAULT
);
754 confpath
= p_new(char, len
);
755 a_strcpy(confpath
, len
, confdir
);
756 a_strcat(confpath
, len
, XDG_CONFIG_HOME_DEFAULT
);
757 /* update package.path */
758 snprintf(path
, sizeof(path
), "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT
"/awesome/?.lua\"", confdir
);
759 luaA_dostring(globalconf
.L
, path
);
761 a_strcat(confpath
, len
, AWESOME_CONFIG_FILE
);
764 ret
= luaA_loadrc(confpath
, run
);
766 xdg_config_dirs
= getenv("XDG_CONFIG_DIRS");
768 if(!(len
= a_strlen(xdg_config_dirs
)))
770 xdg_config_dirs
= XDG_CONFIG_DIR
;
771 len
= sizeof(XDG_CONFIG_DIR
) - 1;
774 xdg_files
= a_strsplit(xdg_config_dirs
, len
, ':');
776 for(buf
= xdg_files
; *buf
; buf
++)
779 len
= a_strlen(*buf
) + sizeof("AWESOME_CONFIG_FILE");
780 confpath
= p_new(char, len
);
781 a_strcpy(confpath
, len
, *buf
);
782 a_strcat(confpath
, len
, AWESOME_CONFIG_FILE
);
783 snprintf(path
, sizeof(path
), "package.path = package.path .. \";%s/awesome/?.lua\"", *buf
);
784 luaA_dostring(globalconf
.L
, path
);
786 ret
= luaA_loadrc(confpath
, run
);
789 for(buf
= xdg_files
; *buf
; buf
++)
791 p_delete(&xdg_files
);
793 /* Assure there's at least one tag */
794 for(screen
= 0; screen
< globalconf
.nscreen
; screen
++)
795 if(!globalconf
.screens
[screen
].tags
.len
)
796 tag_append_to_screen(tag_new("default", sizeof("default")-1, layout_tile
, 0.5, 1, 0),
797 &globalconf
.screens
[screen
]);
805 * \param cmd The buffer to parse.
806 * \return the number of elements pushed on the stack by the last statement in cmd.
807 * If there's an error, the message is pushed onto the stack and this returns 1.
810 luaA_docmd(const char *cmd
)
813 int newtop
, oldtop
= lua_gettop(globalconf
.L
);
815 while((p
= strchr(cmd
, '\n')))
817 newtop
= lua_gettop(globalconf
.L
);
818 lua_pop(globalconf
.L
, newtop
- oldtop
);
822 if (luaL_dostring(globalconf
.L
, cmd
))
824 warn("error executing Lua code: %s", lua_tostring(globalconf
.L
, -1));
829 return lua_gettop(globalconf
.L
) - oldtop
;
832 /** Pushes a Lua array containing the top n elements of the stack.
833 * \param n The number of elements to put in the array.
839 lua_createtable(globalconf
.L
, n
, 0);
840 lua_insert(globalconf
.L
, -n
- 1);
842 for (i
= n
; i
> 0; i
--)
843 lua_rawseti(globalconf
.L
, -i
- 1, i
);
846 /** Maps the top n elements of the stack to the result of
847 * applying a function to that element.
848 * \param n The number of elements to map.
850 * \lparam The function to map the elements by. This should be
851 * at position -(n + 1).
857 for (i
= 0; i
< n
; i
++)
859 lua_pushvalue(globalconf
.L
, -n
- 1); /* copy of the function */
860 lua_pushvalue(globalconf
.L
, -n
- 1); /* value to map */
861 lua_pcall(globalconf
.L
, 1, 1, 0); /* call function */
862 lua_remove(globalconf
.L
, -n
- 1); /* remove old value */
864 lua_remove(globalconf
.L
, -n
- 1); /* remove function */
867 static void luaA_conn_cleanup(EV_P_ ev_io
*w
)
869 ev_ref(EV_DEFAULT_UC
);
870 ev_io_stop(EV_DEFAULT_UC_ w
);
872 warn("error closing UNIX domain socket: %s", strerror(errno
));
877 luaA_cb(EV_P_ ev_io
*w
, int revents
)
884 switch(r
= recv(w
->fd
, buf
, sizeof(buf
)-1, MSG_TRUNC
))
887 warn("error reading UNIX domain socket: %s", strerror(errno
));
888 case 0: /* 0 bytes are only transferred when the connection is closed */
889 luaA_conn_cleanup(EV_DEFAULT_UC_ w
);
892 if(r
>= ssizeof(buf
))
895 lua_getglobal(globalconf
.L
, "table");
896 lua_getfield(globalconf
.L
, -1, "concat");
897 lua_remove(globalconf
.L
, -2); /* remove table */
899 lua_getglobal(globalconf
.L
, "tostring");
900 els
= luaA_docmd(buf
);
901 luaA_map(els
); /* map results to strings */
902 luaA_array(els
); /* put strings in an array */
904 lua_pushstring(globalconf
.L
, "\t");
905 lua_pcall(globalconf
.L
, 2, 1, 0); /* concatenate results with tabs */
907 s
= lua_tolstring(globalconf
.L
, -1, &len
);
909 /* ignore ENOENT because the client may not read */
910 if (send(w
->fd
, s
, len
, MSG_DONTWAIT
) == -1)
917 warn("can't send back to client via domain socket: %s", strerror(errno
));
921 lua_pop(globalconf
.L
, 1); /* pop the string */
923 awesome_refresh(globalconf
.connection
);
928 luaA_conn_cb(EV_P_ ev_io
*w
, int revents
)
930 ev_io
*csio_conn
= p_new(ev_io
, 1);
931 int csfd
= accept(w
->fd
, NULL
, NULL
);
933 ev_io_init(csio_conn
, &luaA_cb
, csfd
, EV_READ
);
934 ev_io_start(EV_DEFAULT_UC_ csio_conn
);
935 ev_unref(EV_DEFAULT_UC
);
941 int csfd
= socket_getclient();
943 if (csfd
< 0 || fcntl(csfd
, F_SETFD
, FD_CLOEXEC
) == -1)
946 addr
= socket_getaddr(getenv("DISPLAY"));
948 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
950 if(errno
== EADDRINUSE
)
952 if(unlink(addr
->sun_path
))
953 warn("error unlinking existing file: %s", strerror(errno
));
954 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
955 return warn("error binding UNIX domain socket: %s", strerror(errno
));
958 return warn("error binding UNIX domain socket: %s", strerror(errno
));
962 ev_io_init(&csio
, &luaA_conn_cb
, csfd
, EV_READ
);
963 ev_io_start(EV_DEFAULT_UC_
&csio
);
964 ev_unref(EV_DEFAULT_UC
);
968 luaA_cs_cleanup(void)
972 ev_ref(EV_DEFAULT_UC
);
973 ev_io_stop(EV_DEFAULT_UC_
&csio
);
975 warn("error closing UNIX domain socket: %s", strerror(errno
));
976 if(unlink(addr
->sun_path
))
977 warn("error unlinking UNIX domain socket: %s", strerror(errno
));
983 luaA_on_timer(EV_P_ ev_timer
*w
, int revents
)
985 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.timer
, 0, 0);
986 awesome_refresh(globalconf
.connection
);
990 luaA_pushcolor(lua_State
*L
, const xcolor_t
*c
)
992 uint8_t r
= (unsigned)c
->red
* 0xff / 0xffff;
993 uint8_t g
= (unsigned)c
->green
* 0xff / 0xffff;
994 uint8_t b
= (unsigned)c
->blue
* 0xff / 0xffff;
995 uint8_t a
= (unsigned)c
->alpha
* 0xff / 0xffff;
997 /* do not print alpha if it's full */
999 snprintf(s
, sizeof(s
), "#%02x%02x%02x", r
, g
, b
);
1001 snprintf(s
, sizeof(s
), "#%02x%02x%02x%02x", r
, g
, b
, a
);
1002 lua_pushlstring(L
, s
, sizeof(s
));