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
)
283 luaA_checktable(L
, 1);
285 if((buf
= luaA_getopt_lstring(L
, 1, "fg", NULL
, &len
)))
286 xcolor_init(&globalconf
.colors
.fg
, globalconf
.connection
,
287 globalconf
.default_screen
, buf
, len
);
289 if((buf
= luaA_getopt_lstring(L
, 1, "bg", NULL
, &len
)))
290 xcolor_init(&globalconf
.colors
.bg
, globalconf
.connection
,
291 globalconf
.default_screen
, buf
, len
);
295 /** Push a pointer onto the stack according to its type.
296 * \param p The pointer.
297 * \param type Its type.
300 luaA_pushpointer(lua_State
*L
, void *p
, awesome_type_t type
)
304 case AWESOME_TYPE_STATUSBAR
:
305 luaA_statusbar_userdata_new(L
, p
);
307 case AWESOME_TYPE_TITLEBAR
:
308 luaA_titlebar_userdata_new(L
, p
);
314 luaA_openlib(lua_State
*L
, const char *name
,
315 const struct luaL_reg methods
[],
316 const struct luaL_reg meta
[])
318 luaL_newmetatable(L
, name
); /* 1 */
319 lua_pushvalue(L
, -1); /* dup metatable 2 */
320 lua_setfield(L
, -2, "__index"); /* metatable.__index = metatable 1 */
322 luaL_register(L
, NULL
, meta
); /* 1 */
323 luaL_register(L
, name
, methods
); /* 2 */
324 lua_pushvalue(L
, -1); /* dup self as metatable 3 */
325 lua_setmetatable(L
, -2); /* set self as metatable 2 */
330 luaA_mbstrlen(lua_State
*L
)
332 const char *cmd
= luaL_checkstring(L
, 1);
333 lua_pushnumber(L
, mbstowcs(NULL
, NONULL(cmd
), 0));
338 luaA_next(lua_State
*L
)
340 if(luaL_getmetafield(L
, 1, "__next"))
343 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
344 return lua_gettop(L
);
347 luaL_checktype(L
, 1, LUA_TTABLE
);
356 luaA_pairs(lua_State
*L
)
358 if(luaL_getmetafield(L
, 1, "__pairs"))
361 lua_call(L
, lua_gettop(L
) - 1, LUA_MULTRET
);
362 return lua_gettop(L
);
365 luaL_checktype(L
, 1, LUA_TTABLE
);
366 return luaA_generic_pairs(L
);
370 luaA_fixups(lua_State
*L
)
372 lua_getglobal(L
, "string");
373 lua_pushcfunction(L
, luaA_mbstrlen
);
374 lua_setfield(L
, -2, "len");
376 lua_pushliteral(L
, "next");
377 lua_pushcfunction(L
, luaA_next
);
378 lua_settable(L
, LUA_GLOBALSINDEX
);
379 lua_pushliteral(L
, "pairs");
380 lua_pushcfunction(L
, luaA_next
);
381 lua_pushcclosure(L
, luaA_pairs
, 1); /* pairs get next as upvalue */
382 lua_settable(L
, LUA_GLOBALSINDEX
);
386 * This table can use safely object as key.
387 * \param L The Lua VM state.
388 * \return The number of elements pushed on stack.
391 luaA_otable_index(lua_State
*L
)
395 if((obj
= lua_touserdata(L
, 2)))
399 while(lua_next(L
, 1))
401 if((v
= lua_touserdata(L
, -2))
404 /* removes 'value'; keeps 'key' for next iteration */
414 /** Initialize the Lua VM
421 static const struct luaL_reg otable_methods
[] =
423 { "__call", luaA_otable_new
},
426 static const struct luaL_reg otable_meta
[] =
428 { "__index", luaA_otable_index
},
431 static const struct luaL_reg awesome_lib
[] =
433 { "quit", luaA_quit
},
434 { "exec", luaA_exec
},
435 { "restart", luaA_restart
},
436 { "mouse_add", luaA_mouse_add
},
437 { "font_set", luaA_font_set
},
438 { "colors_set", luaA_colors_set
},
441 static const struct luaL_reg awesome_hooks_lib
[] =
443 { "focus", luaA_hooks_focus
},
444 { "unfocus", luaA_hooks_unfocus
},
445 { "manage", luaA_hooks_manage
},
446 { "unmanage", luaA_hooks_unmanage
},
447 { "mouseover", luaA_hooks_mouseover
},
448 { "arrange", luaA_hooks_arrange
},
449 { "titleupdate", luaA_hooks_titleupdate
},
450 { "urgent", luaA_hooks_urgent
},
451 { "timer", luaA_hooks_timer
},
455 L
= globalconf
.L
= luaL_newstate();
461 /* Export awesome lib */
462 luaL_register(L
, "awesome", awesome_lib
);
464 /* Export hooks lib */
465 luaL_register(L
, "hooks", awesome_hooks_lib
);
467 /* Export keygrabber lib */
468 luaL_register(L
, "keygrabber", awesome_keygrabber_lib
);
470 /* Export otable lib */
471 luaA_openlib(L
, "otable", otable_methods
, otable_meta
);
474 luaA_openlib(L
, "screen", awesome_screen_methods
, awesome_screen_meta
);
477 luaA_openlib(L
, "mouse", awesome_mouse_methods
, awesome_mouse_meta
);
480 luaA_openlib(L
, "tag", awesome_tag_methods
, awesome_tag_meta
);
482 /* Export statusbar */
483 luaA_openlib(L
, "statusbar", awesome_statusbar_methods
, awesome_statusbar_meta
);
486 luaA_openlib(L
, "widget", awesome_widget_methods
, awesome_widget_meta
);
489 luaA_openlib(L
, "client", awesome_client_methods
, awesome_client_meta
);
491 /* Export titlebar */
492 luaA_openlib(L
, "titlebar", awesome_titlebar_methods
, awesome_titlebar_meta
);
495 luaA_openlib(L
, "keybinding", awesome_keybinding_methods
, awesome_keybinding_meta
);
497 lua_pushliteral(L
, "AWESOME_VERSION");
498 lua_pushstring(L
, AWESOME_VERSION
);
499 lua_settable(L
, LUA_GLOBALSINDEX
);
501 luaA_dostring(L
, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH
"/?.lua\"");
502 luaA_dostring(L
, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH
"/?/init.lua\"");
505 globalconf
.hooks
.manage
= LUA_REFNIL
;
506 globalconf
.hooks
.unmanage
= LUA_REFNIL
;
507 globalconf
.hooks
.focus
= LUA_REFNIL
;
508 globalconf
.hooks
.unfocus
= LUA_REFNIL
;
509 globalconf
.hooks
.mouseover
= LUA_REFNIL
;
510 globalconf
.hooks
.arrange
= LUA_REFNIL
;
511 globalconf
.hooks
.titleupdate
= LUA_REFNIL
;
512 globalconf
.hooks
.urgent
= LUA_REFNIL
;
513 globalconf
.hooks
.timer
= LUA_REFNIL
;
516 #define XDG_CONFIG_HOME_DEFAULT "/.config"
518 #define AWESOME_CONFIG_FILE "/awesome/rc.lua"
520 /** Load a configuration file.
521 * \param rcfile The configuration file to load.
524 luaA_parserc(const char *confpatharg
)
527 const char *confdir
, *xdg_config_dirs
;
528 char *confpath
= NULL
, **xdg_files
, **buf
, path
[1024];
533 if(luaL_dofile(globalconf
.L
, confpatharg
))
534 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
539 confdir
= getenv("XDG_CONFIG_HOME");
541 if((len
= a_strlen(confdir
)))
543 len
+= sizeof(AWESOME_CONFIG_FILE
);
544 confpath
= p_new(char, len
);
545 a_strcpy(confpath
, len
, confdir
);
546 /* update package.path */
547 snprintf(path
, sizeof(path
) - 1, "package.path = package.path .. \";%s/awesome/?.lua\"", confdir
);
548 luaA_dostring(globalconf
.L
, path
);
552 confdir
= getenv("HOME");
553 len
= a_strlen(confdir
) + sizeof(AWESOME_CONFIG_FILE
)-1 + sizeof(XDG_CONFIG_HOME_DEFAULT
);
554 confpath
= p_new(char, len
);
555 a_strcpy(confpath
, len
, confdir
);
556 a_strcat(confpath
, len
, XDG_CONFIG_HOME_DEFAULT
);
557 /* update package.path */
558 snprintf(path
, sizeof(path
) - 1, "package.path = package.path .. \";%s" XDG_CONFIG_HOME_DEFAULT
"/awesome/?.lua\"", confdir
);
559 luaA_dostring(globalconf
.L
, path
);
561 a_strcat(confpath
, len
, AWESOME_CONFIG_FILE
);
563 if(luaL_dofile(globalconf
.L
, confpath
))
564 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
568 xdg_config_dirs
= getenv("XDG_CONFIG_DIRS");
570 if(!(len
= a_strlen(xdg_config_dirs
)))
572 xdg_config_dirs
= XDG_CONFIG_DIR
;
573 len
= sizeof(XDG_CONFIG_DIR
) - 1;
576 xdg_files
= a_strsplit(xdg_config_dirs
, len
, ':');
578 for(buf
= xdg_files
; *buf
; buf
++)
581 len
= a_strlen(*buf
) + sizeof("AWESOME_CONFIG_FILE");
582 confpath
= p_new(char, len
);
583 a_strcpy(confpath
, len
, *buf
);
584 a_strcat(confpath
, len
, AWESOME_CONFIG_FILE
);
585 snprintf(path
, sizeof(path
) - 1, "package.path = package.path .. \";%s/awesome/?.lua\"", *buf
);
586 luaA_dostring(globalconf
.L
, path
);
587 if(luaL_dofile(globalconf
.L
, confpath
))
588 fprintf(stderr
, "%s\n", lua_tostring(globalconf
.L
, -1));
593 for(buf
= xdg_files
; *buf
; buf
++)
595 p_delete(&xdg_files
);
598 /* Assure there's at least one tag */
599 for(screen
= 0; screen
< globalconf
.screens_info
->nscreen
; screen
++)
600 if(!globalconf
.screens
[screen
].tags
.len
)
601 tag_append_to_screen(tag_new("default", sizeof("default")-1, layout_tile
, 0.5, 1, 0), screen
);
607 * \param cmd The buffer to parse.
608 * \return true on succes, false on failure.
611 luaA_docmd(const char *cmd
)
615 while((p
= strchr(cmd
, '\n')))
618 luaA_dostring(globalconf
.L
, cmd
);
624 luaA_cb(EV_P_ ev_io
*w
, int revents
)
629 switch(r
= recv(w
->fd
, buf
, sizeof(buf
)-1, MSG_TRUNC
))
632 warn("error reading UNIX domain socket: %s", strerror(errno
));
638 if(r
>= ssizeof(buf
))
651 int csfd
= socket_getclient();
655 addr
= socket_getaddr(getenv("DISPLAY"));
657 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
659 if(errno
== EADDRINUSE
)
661 if(unlink(addr
->sun_path
))
662 warn("error unlinking existing file: %s", strerror(errno
));
663 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
664 warn("error binding UNIX domain socket: %s", strerror(errno
));
667 warn("error binding UNIX domain socket: %s", strerror(errno
));
669 ev_io_init(&csio
, &luaA_cb
, csfd
, EV_READ
);
670 ev_io_start(EV_DEFAULT_UC_
&csio
);
671 ev_unref(EV_DEFAULT_UC
);
675 luaA_cs_cleanup(void)
679 ev_ref(EV_DEFAULT_UC
);
680 ev_io_stop(EV_DEFAULT_UC_
&csio
);
682 warn("error closing UNIX domain socket: %s", strerror(errno
));
683 if(unlink(addr
->sun_path
))
684 warn("error unlinking UNIX domain socket: %s", strerror(errno
));
690 luaA_on_timer(EV_P_ ev_timer
*w
, int revents
)
692 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.timer
, 0, 0);
699 luaA_pushcolor(lua_State
*L
, const xcolor_t
*c
)
701 uint8_t r
= (unsigned)c
->red
* 0xff / 0xffff;
702 uint8_t g
= (unsigned)c
->green
* 0xff / 0xffff;
703 uint8_t b
= (unsigned)c
->blue
* 0xff / 0xffff;
704 uint8_t a
= (unsigned)c
->alpha
* 0xff / 0xffff;
706 snprintf(s
, sizeof(s
), "#%02x%02x%02x%02x", r
, g
, b
, a
);
707 lua_pushlstring(L
, s
, sizeof(s
));