screen: export tags array
[awesome.git] / lua.c
blob396bd52cbc380a0019243fa2af1cb652c1c8e880
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 <errno.h>
23 #include <stdio.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <unistd.h>
28 #include <ev.h>
30 #include <lua.h>
31 #include <lauxlib.h>
32 #include <lualib.h>
34 #include <xcb/xcb.h>
35 #include <xcb/xcb_aux.h>
37 #include "awesome-version-internal.h"
38 #include "ewmh.h"
39 #include "config.h"
40 #include "lua.h"
41 #include "tag.h"
42 #include "client.h"
43 #include "statusbar.h"
44 #include "titlebar.h"
45 #include "screen.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.
76 * \luastack
77 * \lparam A mouse button binding.
79 static int
80 luaA_mouse_add(lua_State *L)
82 button_t **button = luaA_checkudata(L, 1, "mouse");
84 button_list_push(&globalconf.buttons.root, *button);
85 button_ref(button);
87 return 0;
90 /** Quit awesome.
92 static int
93 luaA_quit(lua_State *L __attribute__ ((unused)))
95 ev_unloop(globalconf.loop, 1);
96 return 0;
99 /** Execute another application, probably a window manager, to replace
100 * awesome.
101 * \param L The Lua VM state.
103 * \luastack
104 * \lparam The command line to execute.
106 static int
107 luaA_exec(lua_State *L)
109 client_t *c;
110 const char *cmd = luaL_checkstring(L, 1);
112 for(c = globalconf.clients; c; c = c->next)
113 client_unban(c);
115 xcb_aux_sync(globalconf.connection);
116 xcb_disconnect(globalconf.connection);
118 a_exec(cmd);
119 return 0;
122 /** Restart awesome.
124 static int
125 luaA_restart(lua_State *L __attribute__ ((unused)))
127 ewmh_restart();
128 return 0;
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.
136 * \luastack
137 * \lparam A function to call each time a client gets focus.
139 static int
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.
149 * \luastack
150 * \lparam A function to call each time a client loses focus.
152 static int
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.
162 * \luastack
163 * \lparam A function to call on each new client.
165 static int
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.
175 * \luastack
176 * \lparam A function to call when a client goes away.
178 static int
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.
188 * \luastack
189 * \lparam A function to call each time a client gets mouse over it.
191 static int
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.
201 * \luastack
202 * \lparam A function to call on each screen arrange.
204 static int
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.
214 * \luastack
215 * \lparam A function to call on each title update of each client.
217 static int
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.
227 * \luastack
228 * \lparam A function to call when a client get the urgent flag.
230 static int
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.
239 * \luastack
240 * \lparam The number of seconds to run function every. Set 0 to disable.
241 * \lparam A function to call every N seconds (optional).
243 static int
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);
252 return 0;
255 /** Set default font.
256 * \param L The Lua VM state.
258 * \luastack
259 * \lparam A string with a font name in Pango format.
261 static int
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);
268 return 0;
271 /** Set default colors.
272 * \param L The Lua VM state.
274 * \luastack
275 * \lparam A table with `fg' and `bg' elements, containing colors.
277 static int
278 luaA_colors_set(lua_State *L)
280 const char *buf;
281 size_t len;
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);
292 return 0;
295 /** Push a pointer onto the stack according to its type.
296 * \param p The pointer.
297 * \param type Its type.
299 void
300 luaA_pushpointer(lua_State *L, void *p, awesome_type_t type)
302 switch(type)
304 case AWESOME_TYPE_STATUSBAR:
305 luaA_statusbar_userdata_new(L, p);
306 break;
307 case AWESOME_TYPE_TITLEBAR:
308 luaA_titlebar_userdata_new(L, p);
309 break;
313 static void
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 */
326 lua_pop(L, 2);
329 static int
330 luaA_mbstrlen(lua_State *L)
332 const char *cmd = luaL_checkstring(L, 1);
333 lua_pushnumber(L, mbstowcs(NULL, NONULL(cmd), 0));
334 return 1;
337 static int
338 luaA_next(lua_State *L)
340 if(luaL_getmetafield(L, 1, "__next"))
342 lua_insert(L, 1);
343 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
344 return lua_gettop(L);
347 luaL_checktype(L, 1, LUA_TTABLE);
348 lua_settop(L, 2);
349 if(lua_next(L, 1))
350 return 2;
351 lua_pushnil(L);
352 return 1;
355 static int
356 luaA_pairs(lua_State *L)
358 if(luaL_getmetafield(L, 1, "__pairs"))
360 lua_insert(L, 1);
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);
369 static void
370 luaA_fixups(lua_State *L)
372 lua_getglobal(L, "string");
373 lua_pushcfunction(L, luaA_mbstrlen);
374 lua_setfield(L, -2, "len");
375 lua_pop(L, 1);
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);
385 /** Object table.
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)
393 void **obj, **v;
395 if((obj = lua_touserdata(L, 2)))
397 /* begins at nil */
398 lua_pushnil(L);
399 while(lua_next(L, 1))
401 if((v = lua_touserdata(L, -2))
402 && *v == *obj)
403 return 1;
404 /* removes 'value'; keeps 'key' for next iteration */
405 lua_pop(L, 1);
407 return 0;
410 lua_rawget(L, 1);
411 return 1;
414 /** Initialize the Lua VM
416 void
417 luaA_init(void)
419 lua_State *L;
421 static const struct luaL_reg otable_methods[] =
423 { "__call", luaA_otable_new },
424 { NULL, NULL }
426 static const struct luaL_reg otable_meta[] =
428 { "__index", luaA_otable_index },
429 { NULL, NULL }
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 },
439 { NULL, NULL }
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 },
452 { NULL, NULL }
455 L = globalconf.L = luaL_newstate();
457 luaL_openlibs(L);
459 luaA_fixups(L);
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);
473 /* Export screen */
474 luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
476 /* Export mouse */
477 luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
479 /* Export tag */
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);
485 /* Export widget */
486 luaA_openlib(L, "widget", awesome_widget_methods, awesome_widget_meta);
488 /* Export client */
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);
494 /* Export keys */
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\"");
504 /* init hooks */
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.
523 void
524 luaA_parserc(const char *confpatharg)
526 int screen;
527 const char *confdir, *xdg_config_dirs;
528 char *confpath = NULL, **xdg_files, **buf, path[1024];
529 ssize_t len;
531 if(confpatharg)
533 if(luaL_dofile(globalconf.L, confpatharg))
534 fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
535 else
536 goto bailout;
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);
550 else
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));
565 else
566 goto bailout;
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++)
580 p_delete(&confpath);
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));
589 else
590 break;
593 for(buf = xdg_files; *buf; buf++)
594 p_delete(buf);
595 p_delete(&xdg_files);
597 bailout:
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);
603 p_delete(&confpath);
606 /** Parse a command.
607 * \param cmd The buffer to parse.
608 * \return true on succes, false on failure.
610 static void
611 luaA_docmd(const char *cmd)
613 char *p;
615 while((p = strchr(cmd, '\n')))
617 *p = '\0';
618 luaA_dostring(globalconf.L, cmd);
619 cmd = p + 1;
623 static void
624 luaA_cb(EV_P_ ev_io *w, int revents)
626 char buf[1024];
627 int r;
629 switch(r = recv(w->fd, buf, sizeof(buf)-1, MSG_TRUNC))
631 case -1:
632 warn("error reading UNIX domain socket: %s", strerror(errno));
633 luaA_cs_cleanup();
634 break;
635 case 0:
636 break;
637 default:
638 if(r >= ssizeof(buf))
639 break;
640 buf[r] = '\0';
641 luaA_docmd(buf);
643 layout_refresh();
644 statusbar_refresh();
645 titlebar_refresh();
648 void
649 luaA_cs_init(void)
651 int csfd = socket_getclient();
653 if (csfd < 0)
654 return;
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));
666 else
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);
674 void
675 luaA_cs_cleanup(void)
677 if(csio.fd < 0)
678 return;
679 ev_ref(EV_DEFAULT_UC);
680 ev_io_stop(EV_DEFAULT_UC_ &csio);
681 if (close(csio.fd))
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));
685 p_delete(&addr);
686 csio.fd = -1;
689 void
690 luaA_on_timer(EV_P_ ev_timer *w, int revents)
692 luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0, 0);
693 layout_refresh();
694 statusbar_refresh();
695 titlebar_refresh();
698 void
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;
705 char s[10];
706 snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
707 lua_pushlstring(L, s, sizeof(s));