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 client list change.
222 * This function is called without any 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 client list change.
229 luaA_hooks_clients(lua_State
*L
)
231 return luaA_registerfct(L
, 1, &globalconf
.hooks
.clients
);
234 /** Set the function called on each screen tag list change.
235 * This function is called with a screen number as argument.
236 * \param L The Lua VM state.
237 * \return The number of elements pushed on stack.
239 * \lparam A function to call on each tag list change.
242 luaA_hooks_tags(lua_State
*L
)
244 return luaA_registerfct(L
, 1, &globalconf
.hooks
.tags
);
247 /** Set the function called on each client's tags change.
248 * This function is called with the client and the tag as argument.
249 * \param L The Lua VM state.
250 * \return The number of elements pushed on stack.
252 * \lparam A function to call on each client's tags change.
255 luaA_hooks_tagged(lua_State
*L
)
257 return luaA_registerfct(L
, 1, &globalconf
.hooks
.tagged
);
260 /** Set the function called on each screen arrange. This function is called
261 * with the screen number as argument.
262 * \param L The Lua VM state.
263 * \return The number of elements pushed on stack.
265 * \lparam A function to call on each screen arrange.
268 luaA_hooks_arrange(lua_State
*L
)
270 return luaA_registerfct(L
, 1, &globalconf
.hooks
.arrange
);
273 /** Set the function called on each client's property change.
274 * This function is called with the client object as argument and the
276 * \param L The Lua VM state.
277 * \return The number of elements pushed on stack.
279 * \lparam A function to call on each client property update.
282 luaA_hooks_property(lua_State
*L
)
284 return luaA_registerfct(L
, 1, &globalconf
.hooks
.property
);
287 /** Set the function to be called every N seconds.
288 * \param L The Lua VM state.
289 * \return The number of elements pushed on stack.
291 * \lparam The number of seconds to run function every. Set 0 to disable.
292 * \lparam A function to call every N seconds (optional).
295 luaA_hooks_timer(lua_State
*L
)
297 globalconf
.timer
.repeat
= luaL_checknumber(L
, 1);
299 if(lua_gettop(L
) == 2 && !lua_isnil(L
, 2))
300 luaA_registerfct(L
, 2, &globalconf
.hooks
.timer
);
302 ev_timer_again(globalconf
.loop
, &globalconf
.timer
);
306 /** Set or get default font. (DEPRECATED)
307 * \param L The Lua VM state.
308 * \return The number of elements pushed on stack.
310 * \lparam An optional string with a font name in Pango format.
311 * \lreturn The font used, in Pango format.
314 luaA_font(lua_State
*L
)
318 if(lua_gettop(L
) == 1)
320 const char *newfont
= luaL_checkstring(L
, 1);
321 draw_font_delete(&globalconf
.font
);
322 globalconf
.font
= draw_font_new(globalconf
.default_screen
, newfont
);
325 font
= pango_font_description_to_string(globalconf
.font
->desc
);
326 lua_pushstring(L
, font
);
332 /** Set default font. (DEPRECATED)
333 * \param L The Lua VM state.
334 * \return The number of elements pushed on stack.
336 * \lparam A string with a font name in Pango format.
339 luaA_font_set(lua_State
*L
)
345 /** Get configuration file path used by awesome.
346 * \param L The Lua VM state.
347 * \return The number of elements pushed on stack.
349 * \lreturn The awesome configuration file path.
352 luaA_conffile(lua_State
*L
)
354 lua_pushstring(L
, globalconf
.conffile
);
358 /** Set default colors.
359 * \param L The Lua VM state.
360 * \return The number of elements pushed on stack.
362 * \lparam A table with `fg' and `bg' elements, containing colors.
363 * \lreturn A table with `fg' and `bg' elements, containing colors.
366 luaA_colors(lua_State
*L
)
368 if(lua_gettop(L
) == 1)
372 int8_t colors_nbr
= -1, i
;
373 xcolor_init_request_t reqs
[2];
375 luaA_checktable(L
, 1);
377 if((buf
= luaA_getopt_lstring(L
, 1, "fg", NULL
, &len
)))
378 reqs
[++colors_nbr
] = xcolor_init_unchecked(&globalconf
.colors
.fg
, buf
, len
);
380 if((buf
= luaA_getopt_lstring(L
, 1, "bg", NULL
, &len
)))
381 reqs
[++colors_nbr
] = xcolor_init_unchecked(&globalconf
.colors
.bg
, buf
, len
);
383 for(i
= 0; i
<= colors_nbr
; i
++)
384 xcolor_init_reply(reqs
[i
]);
388 luaA_pushcolor(L
, &globalconf
.colors
.fg
);
389 lua_setfield(L
, -2, "fg");
390 luaA_pushcolor(L
, &globalconf
.colors
.bg
);
391 lua_setfield(L
, -2, "bg");
396 /** Set default colors. (DEPRECATED)
397 * \param L The Lua VM state.
398 * \return The number of elements pushed on stack.
401 * \lparam A table with `fg' and `bg' elements, containing colors.
404 luaA_colors_set(lua_State
*L
)
407 return luaA_colors(L
);
411 luaA_openlib(lua_State
*L
, const char *name
,
412 const struct luaL_reg methods
[],
413 const struct luaL_reg meta
[])
415 luaL_newmetatable(L
, name
); /* 1 */
416 lua_pushvalue(L
, -1); /* dup metatable 2 */
417 lua_setfield(L
, -2, "__index"); /* metatable.__index = metatable 1 */
419 luaL_register(L
, NULL
, meta
); /* 1 */
420 luaL_register(L
, name
, methods
); /* 2 */
421 lua_pushvalue(L
, -1); /* dup self as metatable 3 */
422 lua_setmetatable(L
, -2); /* set self as metatable 2 */
426 /** UTF-8 aware string length computing.
427 * \param L The Lua VM state.
428 * \return The number of elements pushed on stack.
431 luaA_mbstrlen(lua_State
*L
)
433 const char *cmd
= luaL_checkstring(L
, 1);
434 lua_pushnumber(L
, mbstowcs(NULL
, NONULL(cmd
), 0));
438 /** Overload standard Lua next function to use __next key on metatable.
439 * \param L The Lua VM state.
440 * \param The number of elements pushed on stack.
443 luaAe_next(lua_State
*L
)
445 if(luaL_getmetafield(L
, 1, "__next"))
448 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
449 return lua_gettop(L
);
452 luaL_checktype(L
, 1, LUA_TTABLE
);
460 /** Overload lua_next() function by using __next metatable field
461 * to get next elements.
462 * \param L The Lua VM stack.
463 * \param idx The index number of elements in stack.
464 * \return 1 if more elements to come, 0 otherwise.
467 luaA_next(lua_State
*L
, int idx
)
469 if(luaL_getmetafield(L
, idx
, "__next"))
471 /* if idx is relative, reduce it since we got __next */
473 /* copy table and then move key */
474 lua_pushvalue(L
, idx
);
475 lua_pushvalue(L
, -3);
477 lua_pcall(L
, 2, 2, 0);
478 /* next returned nil, it's the end */
488 return lua_next(L
, idx
);
491 /** Generic pairs function.
492 * \param L The Lua VM state.
493 * \return The number of elements pushed on stack.
496 luaA_generic_pairs(lua_State
*L
)
498 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
499 lua_pushvalue(L
, 1); /* state, */
500 lua_pushnil(L
); /* and initial value */
504 /** Overload standard pairs function to use __pairs field of metatables.
505 * \param L The Lua VM state.
506 * \return The number of elements pushed on stack.
509 luaAe_pairs(lua_State
*L
)
511 if(luaL_getmetafield(L
, 1, "__pairs"))
514 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
515 return lua_gettop(L
);
518 luaL_checktype(L
, 1, LUA_TTABLE
);
519 return luaA_generic_pairs(L
);
522 /** Replace various standards Lua functions with our own.
523 * \param L The Lua VM state.
526 luaA_fixups(lua_State
*L
)
528 lua_getglobal(L
, "string");
529 lua_pushcfunction(L
, luaA_mbstrlen
);
530 lua_setfield(L
, -2, "len");
532 lua_pushliteral(L
, "next");
533 lua_pushcfunction(L
, luaAe_next
);
534 lua_settable(L
, LUA_GLOBALSINDEX
);
535 lua_pushliteral(L
, "pairs");
536 lua_pushcfunction(L
, luaAe_next
);
537 lua_pushcclosure(L
, luaAe_pairs
, 1); /* pairs get next as upvalue */
538 lua_settable(L
, LUA_GLOBALSINDEX
);
541 /** __next function for wtable objects.
542 * \param L The Lua VM state.
543 * \return The number of elements pushed on stack.
546 luaA_wtable_next(lua_State
*L
)
548 /* upvalue 1 is content table */
549 if(lua_next(L
, lua_upvalueindex(1)))
555 /** Index function of wtable objects.
556 * \param L The Lua VM state.
557 * \return The number of elements pushed on stack.
560 luaA_wtable_index(lua_State
*L
)
566 /* check for size, waiting lua 5.2 and __len on tables */
567 if((buf
= lua_tolstring(L
, -1, &len
)))
568 if(a_tokenize(buf
, len
) == A_TK_LEN
)
570 lua_pushnumber(L
, lua_objlen(L
, lua_upvalueindex(1)));
575 /* upvalue 1 is content table */
576 lua_rawget(L
, lua_upvalueindex(1));
580 /** Newndex function of wtable objects.
581 * \param L The Lua VM state.
582 * \return The number of elements pushed on stack.
585 luaA_wtable_newindex(lua_State
*L
)
587 bool invalid
= false;
589 /* push key on top */
591 /* get current key value in content table */
592 lua_rawget(L
, lua_upvalueindex(1));
593 /* if value is a widget, notify change */
594 if(lua_istable(L
, -1) || luaA_toudata(L
, -1, "widget"))
597 lua_pop(L
, 1); /* remove value */
599 /* if new value is a widget or a table */
600 if(lua_istable(L
, 3))
602 luaA_table2wtable(L
);
605 else if(!invalid
&& luaA_toudata(L
, 3, "widget"))
608 /* upvalue 1 is content table */
609 lua_rawset(L
, lua_upvalueindex(1));
612 luaA_wibox_invalidate_byitem(L
, lua_topointer(L
, 1));
617 /** Convert the top element of the stack to a proxied wtable.
618 * \param L The Lua VM state.
621 luaA_table2wtable(lua_State
*L
)
623 if(!lua_istable(L
, -1))
626 lua_newtable(L
); /* create *real* content table */
627 lua_newtable(L
); /* metatable */
628 lua_pushvalue(L
, -2); /* copy content table */
629 lua_pushcclosure(L
, luaA_wtable_next
, 1); /* __next has the content table as upvalue */
630 lua_pushvalue(L
, -3); /* copy content table */
631 lua_pushcclosure(L
, luaA_wtable_index
, 1); /* __index has the content table as upvalue */
632 lua_pushvalue(L
, -4); /* copy content table */
633 lua_pushcclosure(L
, luaA_wtable_newindex
, 1); /* __newindex has the content table as upvalue */
634 /* set metatable field with just pushed closure */
635 lua_setfield(L
, -4, "__newindex");
636 lua_setfield(L
, -3, "__index");
637 lua_setfield(L
, -2, "__next");
638 /* set metatable impossible to touch */
639 lua_pushliteral(L
, "wtable");
640 lua_setfield(L
, -2, "__metatable");
641 /* set new metatable on original table */
642 lua_setmetatable(L
, -3);
646 /* go through original table */
647 while(lua_next(L
, -3))
649 /* if convert value to wtable */
650 luaA_table2wtable(L
);
652 lua_pushvalue(L
, -2);
653 /* move key before value */
655 /* set same value in content table */
658 lua_pushvalue(L
, -1);
659 /* push the new value :-> */
661 /* set orig[k] = nil */
664 /* remove content table */
668 /** Look for an item: table, function, etc.
669 * \param L The Lua VM state.
670 * \param item The pointer item.
673 luaA_hasitem(lua_State
*L
, const void *item
)
676 while(luaA_next(L
, -2))
678 if(lua_topointer(L
, -1) == item
)
680 /* remove value and key */
684 if(lua_istable(L
, -1))
685 if(luaA_hasitem(L
, item
))
687 /* remove key and value */
698 * This table can use safely object as key.
699 * \param L The Lua VM state.
700 * \return The number of elements pushed on stack.
703 luaA_otable_index(lua_State
*L
)
707 if((obj
= lua_touserdata(L
, 2)))
711 while(lua_next(L
, 1))
713 /* check the key against the key if the key is a userdata,
714 * otherwise check it again the value. */
715 if((v
= lua_touserdata(L
, -2))
719 else if((v
= lua_touserdata(L
, -1))
726 /* removes 'value'; keeps 'key' for next iteration */
737 * This table can use safely object as key.
738 * \param L The Lua VM state.
739 * \return The number of elements pushed on stack.
742 luaA_otable_newindex(lua_State
*L
)
746 if((obj
= lua_touserdata(L
, 2)))
750 while(lua_next(L
, 1))
752 if((v
= lua_touserdata(L
, -2))
757 /* push new value on top */
759 /* set in table key = value */
763 /* removes 'value'; keeps 'key' for next iteration */
774 * This function is multi-head (Zaphod) aware and will set display to
775 * the right screen according to mouse position.
776 * \param L The Lua VM state.
777 * \return The number of elements pushed on stack
779 * \lparam The command to launch.
780 * \lparam The optional screen number to spawn the command on.
783 luaA_spawn(lua_State
*L
)
785 char *host
, newdisplay
[128];
787 int screen
= 0, screenp
, displayp
;
789 if(lua_gettop(L
) == 2)
791 screen
= luaL_checknumber(L
, 2) - 1;
792 luaA_checkscreen(screen
);
795 cmd
= luaL_checkstring(L
, 1);
797 if(!globalconf
.xinerama_is_active
)
799 xcb_parse_display(NULL
, &host
, &displayp
, &screenp
);
800 snprintf(newdisplay
, sizeof(newdisplay
), "%s:%d.%d", host
, displayp
, screen
);
801 setenv("DISPLAY", newdisplay
, 1);
805 /* The double-fork construct avoids zombie processes and keeps the code
806 * clean from stupid signal handlers. */
811 if(globalconf
.connection
)
812 xcb_disconnect(globalconf
.connection
);
815 warn("execl '%s' failed: %s\n", cmd
, strerror(errno
));
824 /** Initialize the Lua VM
831 static const struct luaL_reg otable_methods
[] =
833 { "__call", luaA_otable_new
},
836 static const struct luaL_reg otable_meta
[] =
838 { "__index", luaA_otable_index
},
839 { "__newindex", luaA_otable_newindex
},
842 static const struct luaL_reg awesome_lib
[] =
844 { "quit", luaA_quit
},
845 { "exec", luaA_exec
},
846 { "spawn", luaA_spawn
},
847 { "restart", luaA_restart
},
848 { "buttons", luaA_buttons
},
849 { "font_set", luaA_font_set
},
850 { "colors_set", luaA_colors_set
},
851 { "font", luaA_font
},
852 { "colors", luaA_colors
},
853 { "conffile", luaA_conffile
},
856 static const struct luaL_reg awesome_hooks_lib
[] =
858 { "focus", luaA_hooks_focus
},
859 { "unfocus", luaA_hooks_unfocus
},
860 { "manage", luaA_hooks_manage
},
861 { "unmanage", luaA_hooks_unmanage
},
862 { "mouse_enter", luaA_hooks_mouse_enter
},
863 { "property", luaA_hooks_property
},
864 { "arrange", luaA_hooks_arrange
},
865 { "clients", luaA_hooks_clients
},
866 { "tags", luaA_hooks_tags
},
867 { "tagged", luaA_hooks_tagged
},
868 { "timer", luaA_hooks_timer
},
870 { "mouse_over", luaA_hooks_mouse_over
},
874 L
= globalconf
.L
= luaL_newstate();
880 /* Export awesome lib */
881 luaL_register(L
, "awesome", awesome_lib
);
883 /* Export hooks lib */
884 luaL_register(L
, "hooks", awesome_hooks_lib
);
886 /* Export keygrabber lib */
887 luaL_register(L
, "keygrabber", awesome_keygrabber_lib
);
889 /* Export otable lib */
890 luaA_openlib(L
, "otable", otable_methods
, otable_meta
);
893 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
896 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
899 luaA_openlib(L
, "button", awesome_button_methods
, awesome_button_meta
);
902 luaA_openlib(L
, "image", awesome_image_methods
, awesome_image_meta
);
905 luaA_openlib(L
, "tag", awesome_tag_methods
, awesome_tag_meta
);
907 /* Export statusbar */
908 luaA_openlib(L
, "statusbar", awesome_statusbar_methods
, awesome_statusbar_meta
);
911 luaA_openlib(L
, "wibox", awesome_wibox_methods
, awesome_wibox_meta
);
914 luaA_openlib(L
, "widget", awesome_widget_methods
, awesome_widget_meta
);
917 luaA_openlib(L
, "client", awesome_client_methods
, awesome_client_meta
);
919 /* Export titlebar */
920 luaA_openlib(L
, "titlebar", awesome_titlebar_methods
, awesome_titlebar_meta
);
923 luaA_openlib(L
, "keybinding", awesome_keybinding_methods
, awesome_keybinding_meta
);
925 lua_pushliteral(L
, "AWESOME_VERSION");
926 lua_pushstring(L
, AWESOME_VERSION
);
927 lua_settable(L
, LUA_GLOBALSINDEX
);
929 lua_pushliteral(L
, "AWESOME_RELEASE");
930 lua_pushstring(L
, AWESOME_RELEASE
);
931 lua_settable(L
, LUA_GLOBALSINDEX
);
933 luaA_dostring(L
, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH
"/?.lua\"");
934 luaA_dostring(L
, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH
"/?/init.lua\"");
937 globalconf
.hooks
.manage
= LUA_REFNIL
;
938 globalconf
.hooks
.unmanage
= LUA_REFNIL
;
939 globalconf
.hooks
.focus
= LUA_REFNIL
;
940 globalconf
.hooks
.unfocus
= LUA_REFNIL
;
941 globalconf
.hooks
.mouse_enter
= LUA_REFNIL
;
942 globalconf
.hooks
.arrange
= LUA_REFNIL
;
943 globalconf
.hooks
.clients
= LUA_REFNIL
;
944 globalconf
.hooks
.tags
= LUA_REFNIL
;
945 globalconf
.hooks
.tagged
= LUA_REFNIL
;
946 globalconf
.hooks
.property
= LUA_REFNIL
;
947 globalconf
.hooks
.timer
= LUA_REFNIL
;
950 #define XDG_CONFIG_HOME_DEFAULT "/.config"
952 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
955 luaA_loadrc(const char *confpath
, bool run
)
959 if(!luaL_loadfile(globalconf
.L
, confpath
))
963 if(lua_pcall(globalconf
.L
, 0, LUA_MULTRET
, 0))
964 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
967 globalconf
.conffile
= a_strdup(confpath
);
972 lua_pop(globalconf
.L
, 1);
976 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
981 /** Load a configuration file.
982 * \param confpatharg The configuration file to load.
983 * \param run Run the configuration file.
986 luaA_parserc(const char *confpatharg
, bool run
)
989 const char *confdir
, *xdg_config_dirs
;
990 char *confpath
= NULL
, **xdg_files
, **buf
, path
[1024];
994 ret
= luaA_loadrc(confpatharg
, run
);
996 confdir
= getenv("XDG_CONFIG_HOME");
998 if((len
= a_strlen(confdir
)))
1000 len
+= sizeof(AWESOME_CONFIG_FILE
);
1001 confpath
= p_new(char, len
);
1002 a_strcpy(confpath
, len
, confdir
);
1003 /* update package.path */
1004 snprintf(path
, sizeof(path
), "package.path = package.path .. \";%s/awesome/?.lua\"", confdir
);
1005 luaA_dostring(globalconf
.L
, path
);
1009 confdir
= getenv("HOME");
1010 len
= a_strlen(confdir
) + sizeof(AWESOME_CONFIG_FILE
)-1 + sizeof(XDG_CONFIG_HOME_DEFAULT
);
1011 confpath
= p_new(char, len
);
1012 a_strcpy(confpath
, len
, confdir
);
1013 a_strcat(confpath
, len
, XDG_CONFIG_HOME_DEFAULT
);
1014 /* update package.path */
1015 snprintf(path
, sizeof(path
), "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT
"/awesome/?.lua\"", confdir
);
1016 luaA_dostring(globalconf
.L
, path
);
1018 a_strcat(confpath
, len
, AWESOME_CONFIG_FILE
);
1021 ret
= luaA_loadrc(confpath
, run
);
1023 xdg_config_dirs
= getenv("XDG_CONFIG_DIRS");
1025 if(!(len
= a_strlen(xdg_config_dirs
)))
1027 xdg_config_dirs
= XDG_CONFIG_DIR
;
1028 len
= sizeof(XDG_CONFIG_DIR
) - 1;
1031 xdg_files
= a_strsplit(xdg_config_dirs
, len
, ':');
1033 for(buf
= xdg_files
; *buf
; buf
++)
1035 p_delete(&confpath
);
1036 len
= a_strlen(*buf
) + sizeof("AWESOME_CONFIG_FILE");
1037 confpath
= p_new(char, len
);
1038 a_strcpy(confpath
, len
, *buf
);
1039 a_strcat(confpath
, len
, AWESOME_CONFIG_FILE
);
1040 snprintf(path
, sizeof(path
), "package.path = package.path .. \";%s/awesome/?.lua\"", *buf
);
1041 luaA_dostring(globalconf
.L
, path
);
1043 ret
= luaA_loadrc(confpath
, run
);
1046 for(buf
= xdg_files
; *buf
; buf
++)
1048 p_delete(&xdg_files
);
1050 /* Assure there's at least one tag */
1051 for(screen
= 0; screen
< globalconf
.nscreen
; screen
++)
1052 if(!globalconf
.screens
[screen
].tags
.len
)
1053 tag_append_to_screen(tag_new("default", sizeof("default")-1, layout_tile
, 0.5, 1, 0),
1054 &globalconf
.screens
[screen
]);
1056 p_delete(&confpath
);
1061 /** Parse a command.
1062 * \param cmd The buffer to parse.
1063 * \return the number of elements pushed on the stack by the last statement in cmd.
1064 * If there's an error, the message is pushed onto the stack and this returns 1.
1067 luaA_docmd(const char *cmd
)
1070 int newtop
, oldtop
= lua_gettop(globalconf
.L
);
1072 while((p
= strchr(cmd
, '\n')))
1074 newtop
= lua_gettop(globalconf
.L
);
1075 lua_pop(globalconf
.L
, newtop
- oldtop
);
1079 if (luaL_dostring(globalconf
.L
, cmd
))
1081 warn("error executing Lua code: %s", lua_tostring(globalconf
.L
, -1));
1086 return lua_gettop(globalconf
.L
) - oldtop
;
1089 /** Pushes a Lua array containing the top n elements of the stack.
1090 * \param n The number of elements to put in the array.
1096 lua_createtable(globalconf
.L
, n
, 0);
1097 lua_insert(globalconf
.L
, -n
- 1);
1099 for (i
= n
; i
> 0; i
--)
1100 lua_rawseti(globalconf
.L
, -i
- 1, i
);
1103 /** Maps the top n elements of the stack to the result of
1104 * applying a function to that element.
1105 * \param n The number of elements to map.
1107 * \lparam The function to map the elements by. This should be
1108 * at position -(n + 1).
1114 for (i
= 0; i
< n
; i
++)
1116 lua_pushvalue(globalconf
.L
, -n
- 1); /* copy of the function */
1117 lua_pushvalue(globalconf
.L
, -n
- 1); /* value to map */
1118 lua_pcall(globalconf
.L
, 1, 1, 0); /* call function */
1119 lua_remove(globalconf
.L
, -n
- 1); /* remove old value */
1121 lua_remove(globalconf
.L
, -n
- 1); /* remove function */
1124 static void luaA_conn_cleanup(EV_P_ ev_io
*w
)
1126 ev_ref(EV_DEFAULT_UC
);
1127 ev_io_stop(EV_DEFAULT_UC_ w
);
1129 warn("error closing UNIX domain socket: %s", strerror(errno
));
1134 luaA_cb(EV_P_ ev_io
*w
, int revents
)
1141 switch(r
= recv(w
->fd
, buf
, sizeof(buf
)-1, MSG_TRUNC
))
1144 warn("error reading UNIX domain socket: %s", strerror(errno
));
1145 case 0: /* 0 bytes are only transferred when the connection is closed */
1146 luaA_conn_cleanup(EV_DEFAULT_UC_ w
);
1149 if(r
>= ssizeof(buf
))
1152 lua_getglobal(globalconf
.L
, "table");
1153 lua_getfield(globalconf
.L
, -1, "concat");
1154 lua_remove(globalconf
.L
, -2); /* remove table */
1156 lua_getglobal(globalconf
.L
, "tostring");
1157 els
= luaA_docmd(buf
);
1158 luaA_map(els
); /* map results to strings */
1159 luaA_array(els
); /* put strings in an array */
1161 lua_pushstring(globalconf
.L
, "\t");
1162 lua_pcall(globalconf
.L
, 2, 1, 0); /* concatenate results with tabs */
1164 s
= lua_tolstring(globalconf
.L
, -1, &len
);
1166 /* ignore ENOENT because the client may not read */
1167 if (send(w
->fd
, s
, len
, MSG_DONTWAIT
) == -1)
1174 warn("can't send back to client via domain socket: %s", strerror(errno
));
1178 lua_pop(globalconf
.L
, 1); /* pop the string */
1180 awesome_refresh(globalconf
.connection
);
1185 luaA_conn_cb(EV_P_ ev_io
*w
, int revents
)
1187 ev_io
*csio_conn
= p_new(ev_io
, 1);
1188 int csfd
= accept(w
->fd
, NULL
, NULL
);
1190 ev_io_init(csio_conn
, &luaA_cb
, csfd
, EV_READ
);
1191 ev_io_start(EV_DEFAULT_UC_ csio_conn
);
1192 ev_unref(EV_DEFAULT_UC
);
1198 int csfd
= socket_getclient();
1200 if (csfd
< 0 || fcntl(csfd
, F_SETFD
, FD_CLOEXEC
) == -1)
1203 addr
= socket_getaddr(getenv("DISPLAY"));
1205 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
1207 if(errno
== EADDRINUSE
)
1209 if(unlink(addr
->sun_path
))
1210 warn("error unlinking existing file: %s", strerror(errno
));
1211 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
1212 return warn("error binding UNIX domain socket: %s", strerror(errno
));
1215 return warn("error binding UNIX domain socket: %s", strerror(errno
));
1219 ev_io_init(&csio
, &luaA_conn_cb
, csfd
, EV_READ
);
1220 ev_io_start(EV_DEFAULT_UC_
&csio
);
1221 ev_unref(EV_DEFAULT_UC
);
1225 luaA_cs_cleanup(void)
1229 ev_ref(EV_DEFAULT_UC
);
1230 ev_io_stop(EV_DEFAULT_UC_
&csio
);
1232 warn("error closing UNIX domain socket: %s", strerror(errno
));
1233 if(unlink(addr
->sun_path
))
1234 warn("error unlinking UNIX domain socket: %s", strerror(errno
));
1240 luaA_on_timer(EV_P_ ev_timer
*w
, int revents
)
1242 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.timer
, 0, 0);
1243 awesome_refresh(globalconf
.connection
);
1246 /** Push a color as a string onto the stack
1247 * \param L The Lua VM state.
1248 * \param c The color to push.
1249 * \return The number of elements pushed on stack.
1252 luaA_pushcolor(lua_State
*L
, const xcolor_t
*c
)
1254 uint8_t r
= (unsigned)c
->red
* 0xff / 0xffff;
1255 uint8_t g
= (unsigned)c
->green
* 0xff / 0xffff;
1256 uint8_t b
= (unsigned)c
->blue
* 0xff / 0xffff;
1257 uint8_t a
= (unsigned)c
->alpha
* 0xff / 0xffff;
1259 /* do not print alpha if it's full */
1261 snprintf(s
, sizeof(s
), "#%02x%02x%02x", r
, g
, b
);
1263 snprintf(s
, sizeof(s
), "#%02x%02x%02x%02x", r
, g
, b
, a
);
1264 lua_pushlstring(L
, s
, sizeof(s
));