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>
35 #include <xcb/xcb_aux.h>
37 #include "awesome-version-internal.h"
43 #include "statusbar.h"
46 #include "layouts/tile.h"
47 #include "common/socket.h"
49 extern awesome_t globalconf
;
51 extern const struct luaL_reg awesome_keygrabber_lib
[];
52 extern const struct luaL_reg awesome_mouse_methods
[];
53 extern const struct luaL_reg awesome_mouse_meta
[];
54 extern const struct luaL_reg awesome_screen_methods
[];
55 extern const struct luaL_reg awesome_screen_meta
[];
56 extern const struct luaL_reg awesome_client_methods
[];
57 extern const struct luaL_reg awesome_client_meta
[];
58 extern const struct luaL_reg awesome_titlebar_methods
[];
59 extern const struct luaL_reg awesome_titlebar_meta
[];
60 extern const struct luaL_reg awesome_tag_methods
[];
61 extern const struct luaL_reg awesome_tag_meta
[];
62 extern const struct luaL_reg awesome_widget_methods
[];
63 extern const struct luaL_reg awesome_widget_meta
[];
64 extern const struct luaL_reg awesome_statusbar_methods
[];
65 extern const struct luaL_reg awesome_statusbar_meta
[];
66 extern const struct luaL_reg awesome_keybinding_methods
[];
67 extern const struct luaL_reg awesome_keybinding_meta
[];
69 static struct sockaddr_un
*addr
;
70 static ev_io csio
= { .fd
= -1 };
72 /** Add a global mouse binding. This binding will be available when you'll
73 * click on root window.
74 * \param L The Lua VM state.
77 * \lparam A mouse button binding.
80 luaA_mouse_add(lua_State
*L
)
82 button_t
**button
= luaA_checkudata(L
, 1, "mouse");
84 button_list_push(&globalconf
.buttons
.root
, *button
);
93 luaA_quit(lua_State
*L
__attribute__ ((unused
)))
95 ev_unloop(globalconf
.loop
, 1);
99 /** Execute another application, probably a window manager, to replace
101 * \param L The Lua VM state.
104 * \lparam The command line to execute.
107 luaA_exec(lua_State
*L
)
110 const char *cmd
= luaL_checkstring(L
, 1);
112 for(c
= globalconf
.clients
; c
; c
= c
->next
)
115 xcb_aux_sync(globalconf
.connection
);
116 xcb_disconnect(globalconf
.connection
);
125 luaA_restart(lua_State
*L
__attribute__ ((unused
)))
131 /** Set the function called each time a client gets focus. This function is
132 * called with the client object as argument.
133 * \param L The Lua VM state.
134 * \return The number of elements pushed on stack.
137 * \lparam A function to call each time a client gets focus.
140 luaA_hooks_focus(lua_State
*L
)
142 return luaA_registerfct(L
, &globalconf
.hooks
.focus
);
145 /** Set the function called each time a client loses focus. This function is
146 * called with the client object as argument.
147 * \param L The Lua VM state.
150 * \lparam A function to call each time a client loses focus.
153 luaA_hooks_unfocus(lua_State
*L
)
155 return luaA_registerfct(L
, &globalconf
.hooks
.unfocus
);
158 /** Set the function called each time a new client appears. This function is
159 * called with the client object as argument.
160 * \param L The Lua VM state.
163 * \lparam A function to call on each new client.
166 luaA_hooks_manage(lua_State
*L
)
168 return luaA_registerfct(L
, &globalconf
.hooks
.manage
);
171 /** Set the function called each time a client goes away. This function is
172 * called with the client object as argument.
173 * \param L The Lua VM state.
176 * \lparam A function to call when a client goes away.
179 luaA_hooks_unmanage(lua_State
*L
)
181 return luaA_registerfct(L
, &globalconf
.hooks
.unmanage
);
184 /** Set the function called each time the mouse enter a new window. This
185 * function is called with the client object as argument.
186 * \param L The Lua VM state.
189 * \lparam A function to call each time a client gets mouse over it.
192 luaA_hooks_mouseover(lua_State
*L
)
194 return luaA_registerfct(L
, &globalconf
.hooks
.mouseover
);
197 /** Set the function called on each screen arrange. This function is called
198 * with the screen number as argument.
199 * \param L The Lua VM state.
202 * \lparam A function to call on each screen arrange.
205 luaA_hooks_arrange(lua_State
*L
)
207 return luaA_registerfct(L
, &globalconf
.hooks
.arrange
);
210 /** Set the function called on each title update. This function is called with
211 * the client object as argument.
212 * \param L The Lua VM state.
215 * \lparam A function to call on each title update of each client.
218 luaA_hooks_titleupdate(lua_State
*L
)
220 return luaA_registerfct(L
, &globalconf
.hooks
.titleupdate
);
223 /** Set the function called when a client get urgency flag. This function is called with
224 * the client object as argument.
225 * \param L The Lua VM state.
228 * \lparam A function to call when a client get the urgent flag.
231 luaA_hooks_urgent(lua_State
*L
)
233 return luaA_registerfct(L
, &globalconf
.hooks
.urgent
);
236 /** Set the function to be called every N seconds.
237 * \param L The Lua VM state.
240 * \lparam The number of seconds to run function every. Set 0 to disable.
241 * \lparam A function to call every N seconds (optional).
244 luaA_hooks_timer(lua_State
*L
)
246 globalconf
.timer
.repeat
= luaL_checknumber(L
, 1);
248 if(lua_gettop(L
) == 2 && !lua_isnil(L
, 2))
249 luaA_registerfct(L
, &globalconf
.hooks
.timer
);
251 ev_timer_again(globalconf
.loop
, &globalconf
.timer
);
255 /** Set default font.
256 * \param L The Lua VM state.
259 * \lparam A string with a font name in Pango format.
262 luaA_font_set(lua_State
*L
)
264 const char *font
= luaL_checkstring(L
, 1);
265 draw_font_delete(&globalconf
.font
);
266 globalconf
.font
= draw_font_new(globalconf
.connection
,
267 globalconf
.default_screen
, font
);
271 /** Set default colors.
272 * \param L The Lua VM state.
275 * \lparam A table with `fg' and `bg' elements, containing colors.
278 luaA_colors_set(lua_State
*L
)
282 int8_t colors_nbr
= -1, i
;
283 xcolor_init_request_t reqs
[2];
285 luaA_checktable(L
, 1);
287 if((buf
= luaA_getopt_lstring(L
, 1, "fg", NULL
, &len
)))
288 reqs
[++colors_nbr
] = xcolor_init_unchecked(globalconf
.connection
,
289 &globalconf
.colors
.fg
,
290 globalconf
.default_screen
,
293 if((buf
= luaA_getopt_lstring(L
, 1, "bg", NULL
, &len
)))
294 reqs
[++colors_nbr
] = xcolor_init_unchecked(globalconf
.connection
,
295 &globalconf
.colors
.bg
,
296 globalconf
.default_screen
,
299 for(i
= 0; i
<= colors_nbr
; i
++)
300 xcolor_init_reply(globalconf
.connection
, reqs
[i
]);
305 /** Push a pointer onto the stack according to its type.
306 * \param p The pointer.
307 * \param type Its type.
310 luaA_pushpointer(lua_State
*L
, void *p
, awesome_type_t type
)
314 case AWESOME_TYPE_STATUSBAR
:
315 luaA_statusbar_userdata_new(L
, p
);
317 case AWESOME_TYPE_TITLEBAR
:
318 luaA_titlebar_userdata_new(L
, p
);
324 luaA_openlib(lua_State
*L
, const char *name
,
325 const struct luaL_reg methods
[],
326 const struct luaL_reg meta
[])
328 luaL_newmetatable(L
, name
); /* 1 */
329 lua_pushvalue(L
, -1); /* dup metatable 2 */
330 lua_setfield(L
, -2, "__index"); /* metatable.__index = metatable 1 */
332 luaL_register(L
, NULL
, meta
); /* 1 */
333 luaL_register(L
, name
, methods
); /* 2 */
334 lua_pushvalue(L
, -1); /* dup self as metatable 3 */
335 lua_setmetatable(L
, -2); /* set self as metatable 2 */
340 luaA_mbstrlen(lua_State
*L
)
342 const char *cmd
= luaL_checkstring(L
, 1);
343 lua_pushnumber(L
, mbstowcs(NULL
, NONULL(cmd
), 0));
348 luaA_next(lua_State
*L
)
350 if(luaL_getmetafield(L
, 1, "__next"))
353 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
354 return lua_gettop(L
);
357 luaL_checktype(L
, 1, LUA_TTABLE
);
366 luaA_pairs(lua_State
*L
)
368 if(luaL_getmetafield(L
, 1, "__pairs"))
371 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
372 return lua_gettop(L
);
375 luaL_checktype(L
, 1, LUA_TTABLE
);
376 return luaA_generic_pairs(L
);
380 luaA_fixups(lua_State
*L
)
382 lua_getglobal(L
, "string");
383 lua_pushcfunction(L
, luaA_mbstrlen
);
384 lua_setfield(L
, -2, "len");
386 lua_pushliteral(L
, "next");
387 lua_pushcfunction(L
, luaA_next
);
388 lua_settable(L
, LUA_GLOBALSINDEX
);
389 lua_pushliteral(L
, "pairs");
390 lua_pushcfunction(L
, luaA_next
);
391 lua_pushcclosure(L
, luaA_pairs
, 1); /* pairs get next as upvalue */
392 lua_settable(L
, LUA_GLOBALSINDEX
);
396 * This table can use safely object as key.
397 * \param L The Lua VM state.
398 * \return The number of elements pushed on stack.
401 luaA_otable_index(lua_State
*L
)
405 if((obj
= lua_touserdata(L
, 2)))
409 while(lua_next(L
, 1))
411 if((v
= lua_touserdata(L
, -2))
414 /* removes 'value'; keeps 'key' for next iteration */
425 * This table can use safely object as key.
426 * \param L The Lua VM state.
427 * \return The number of elements pushed on stack.
430 luaA_otable_newindex(lua_State
*L
)
434 if((obj
= lua_touserdata(L
, 2)))
438 while(lua_next(L
, 1))
440 if((v
= lua_touserdata(L
, -2))
445 /* push new value on top */
447 /* set in table key = value */
451 /* removes 'value'; keeps 'key' for next iteration */
460 /** Initialize the Lua VM
467 static const struct luaL_reg otable_methods
[] =
469 { "__call", luaA_otable_new
},
472 static const struct luaL_reg otable_meta
[] =
474 { "__index", luaA_otable_index
},
475 { "__newindex", luaA_otable_newindex
},
478 static const struct luaL_reg awesome_lib
[] =
480 { "quit", luaA_quit
},
481 { "exec", luaA_exec
},
482 { "restart", luaA_restart
},
483 { "mouse_add", luaA_mouse_add
},
484 { "font_set", luaA_font_set
},
485 { "colors_set", luaA_colors_set
},
488 static const struct luaL_reg awesome_hooks_lib
[] =
490 { "focus", luaA_hooks_focus
},
491 { "unfocus", luaA_hooks_unfocus
},
492 { "manage", luaA_hooks_manage
},
493 { "unmanage", luaA_hooks_unmanage
},
494 { "mouseover", luaA_hooks_mouseover
},
495 { "arrange", luaA_hooks_arrange
},
496 { "titleupdate", luaA_hooks_titleupdate
},
497 { "urgent", luaA_hooks_urgent
},
498 { "timer", luaA_hooks_timer
},
502 L
= globalconf
.L
= luaL_newstate();
508 /* Export awesome lib */
509 luaL_register(L
, "awesome", awesome_lib
);
511 /* Export hooks lib */
512 luaL_register(L
, "hooks", awesome_hooks_lib
);
514 /* Export keygrabber lib */
515 luaL_register(L
, "keygrabber", awesome_keygrabber_lib
);
517 /* Export otable lib */
518 luaA_openlib(L
, "otable", otable_methods
, otable_meta
);
521 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
524 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
527 luaA_openlib(L
, "tag", awesome_tag_methods
, awesome_tag_meta
);
529 /* Export statusbar */
530 luaA_openlib(L
, "statusbar", awesome_statusbar_methods
, awesome_statusbar_meta
);
533 luaA_openlib(L
, "widget", awesome_widget_methods
, awesome_widget_meta
);
536 luaA_openlib(L
, "client", awesome_client_methods
, awesome_client_meta
);
538 /* Export titlebar */
539 luaA_openlib(L
, "titlebar", awesome_titlebar_methods
, awesome_titlebar_meta
);
542 luaA_openlib(L
, "keybinding", awesome_keybinding_methods
, awesome_keybinding_meta
);
544 lua_pushliteral(L
, "AWESOME_VERSION");
545 lua_pushstring(L
, AWESOME_VERSION
);
546 lua_settable(L
, LUA_GLOBALSINDEX
);
548 luaA_dostring(L
, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH
"/?.lua\"");
549 luaA_dostring(L
, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH
"/?/init.lua\"");
552 globalconf
.hooks
.manage
= LUA_REFNIL
;
553 globalconf
.hooks
.unmanage
= LUA_REFNIL
;
554 globalconf
.hooks
.focus
= LUA_REFNIL
;
555 globalconf
.hooks
.unfocus
= LUA_REFNIL
;
556 globalconf
.hooks
.mouseover
= LUA_REFNIL
;
557 globalconf
.hooks
.arrange
= LUA_REFNIL
;
558 globalconf
.hooks
.titleupdate
= LUA_REFNIL
;
559 globalconf
.hooks
.urgent
= LUA_REFNIL
;
560 globalconf
.hooks
.timer
= LUA_REFNIL
;
563 #define XDG_CONFIG_HOME_DEFAULT "/.config"
565 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
567 /** Load a configuration file.
568 * \param rcfile The configuration file to load.
571 luaA_parserc(const char *confpatharg
)
574 const char *confdir
, *xdg_config_dirs
;
575 char *confpath
= NULL
, **xdg_files
, **buf
, path
[1024];
580 if(luaL_dofile(globalconf
.L
, confpatharg
))
581 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
586 confdir
= getenv("XDG_CONFIG_HOME");
588 if((len
= a_strlen(confdir
)))
590 len
+= sizeof(AWESOME_CONFIG_FILE
);
591 confpath
= p_new(char, len
);
592 a_strcpy(confpath
, len
, confdir
);
593 /* update package.path */
594 snprintf(path
, sizeof(path
) - 1, "package.path = package.path .. \";%s/awesome/?.lua\"", confdir
);
595 luaA_dostring(globalconf
.L
, path
);
599 confdir
= getenv("HOME");
600 len
= a_strlen(confdir
) + sizeof(AWESOME_CONFIG_FILE
)-1 + sizeof(XDG_CONFIG_HOME_DEFAULT
);
601 confpath
= p_new(char, len
);
602 a_strcpy(confpath
, len
, confdir
);
603 a_strcat(confpath
, len
, XDG_CONFIG_HOME_DEFAULT
);
604 /* update package.path */
605 snprintf(path
, sizeof(path
) - 1, "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT
"/awesome/?.lua\"", confdir
);
606 luaA_dostring(globalconf
.L
, path
);
608 a_strcat(confpath
, len
, AWESOME_CONFIG_FILE
);
610 if(luaL_dofile(globalconf
.L
, confpath
))
611 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
615 xdg_config_dirs
= getenv("XDG_CONFIG_DIRS");
617 if(!(len
= a_strlen(xdg_config_dirs
)))
619 xdg_config_dirs
= XDG_CONFIG_DIR
;
620 len
= sizeof(XDG_CONFIG_DIR
) - 1;
623 xdg_files
= a_strsplit(xdg_config_dirs
, len
, ':');
625 for(buf
= xdg_files
; *buf
; buf
++)
628 len
= a_strlen(*buf
) + sizeof("AWESOME_CONFIG_FILE");
629 confpath
= p_new(char, len
);
630 a_strcpy(confpath
, len
, *buf
);
631 a_strcat(confpath
, len
, AWESOME_CONFIG_FILE
);
632 snprintf(path
, sizeof(path
) - 1, "package.path = package.path .. \";%s/awesome/?.lua\"", *buf
);
633 luaA_dostring(globalconf
.L
, path
);
634 if(luaL_dofile(globalconf
.L
, confpath
))
635 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
640 for(buf
= xdg_files
; *buf
; buf
++)
642 p_delete(&xdg_files
);
645 /* Assure there's at least one tag */
646 for(screen
= 0; screen
< globalconf
.screens_info
->nscreen
; screen
++)
647 if(!globalconf
.screens
[screen
].tags
.len
)
648 tag_append_to_screen(tag_new("default", sizeof("default")-1, layout_tile
, 0.5, 1, 0),
649 &globalconf
.screens
[screen
]);
655 * \param cmd The buffer to parse.
656 * \return true on succes, false on failure.
659 luaA_docmd(const char *cmd
)
663 while((p
= strchr(cmd
, '\n')))
666 luaA_dostring(globalconf
.L
, cmd
);
672 luaA_cb(EV_P_ ev_io
*w
, int revents
)
677 switch(r
= recv(w
->fd
, buf
, sizeof(buf
)-1, MSG_TRUNC
))
680 warn("error reading UNIX domain socket: %s", strerror(errno
));
686 if(r
>= ssizeof(buf
))
699 int csfd
= socket_getclient();
703 addr
= socket_getaddr(getenv("DISPLAY"));
705 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
707 if(errno
== EADDRINUSE
)
709 if(unlink(addr
->sun_path
))
710 warn("error unlinking existing file: %s", strerror(errno
));
711 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
712 warn("error binding UNIX domain socket: %s", strerror(errno
));
715 warn("error binding UNIX domain socket: %s", strerror(errno
));
717 ev_io_init(&csio
, &luaA_cb
, csfd
, EV_READ
);
718 ev_io_start(EV_DEFAULT_UC_
&csio
);
719 ev_unref(EV_DEFAULT_UC
);
723 luaA_cs_cleanup(void)
727 ev_ref(EV_DEFAULT_UC
);
728 ev_io_stop(EV_DEFAULT_UC_
&csio
);
730 warn("error closing UNIX domain socket: %s", strerror(errno
));
731 if(unlink(addr
->sun_path
))
732 warn("error unlinking UNIX domain socket: %s", strerror(errno
));
738 luaA_on_timer(EV_P_ ev_timer
*w
, int revents
)
740 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.timer
, 0, 0);
747 luaA_pushcolor(lua_State
*L
, const xcolor_t
*c
)
749 uint8_t r
= (unsigned)c
->red
* 0xff / 0xffff;
750 uint8_t g
= (unsigned)c
->green
* 0xff / 0xffff;
751 uint8_t b
= (unsigned)c
->blue
* 0xff / 0xffff;
752 uint8_t a
= (unsigned)c
->alpha
* 0xff / 0xffff;
754 snprintf(s
, sizeof(s
), "#%02x%02x%02x%02x", r
, g
, b
, a
);
755 lua_pushlstring(L
, s
, sizeof(s
));