Allow calling a particular lua function from the loaded script.
[screen-lua.git] / src / lua.c
blob1b51808599b8927135059733771cbb5a495c063a
1 /* Lua scripting support
3 * Copyright (c) 2008 Sadrul Habib Chowdhury (sadrul@users.sf.net)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program (see the file COPYING); if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
20 ****************************************************************
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
30 #include "config.h"
32 #include "screen.h"
33 #include "extern.h"
34 #include "logfile.h"
36 #include <lua.h>
37 #include <lauxlib.h>
38 #include <lualib.h>
40 extern struct win *windows, *fore;
41 extern struct display *displays, *display;
42 extern struct LayFuncs WinLf;
43 extern struct layer *flayer;
45 /** Template {{{ */
47 #define CHECK_TYPE(name, type) \
48 static type * \
49 check_##name(lua_State *L, int index) \
50 { \
51 type *var; \
52 luaL_checktype(L, index, LUA_TUSERDATA); \
53 var = (type *) luaL_checkudata(L, index, #name); \
54 if (!var) \
55 luaL_typerror(L, index, #name); \
56 return var; \
59 #define PUSH_TYPE(name, type) \
60 static void \
61 push_##name(lua_State *L, type *t) \
62 { \
63 if (!t) \
64 lua_pushnil(L); \
65 else \
66 { \
67 type *r; \
68 r = (type *)lua_newuserdata(L, sizeof(type)); \
69 *r = *t; \
70 luaL_getmetatable(L, #name); \
71 lua_setmetatable(L,-2); \
72 } \
75 /* Much of the following template comes from:
76 * http://lua-users.org/wiki/BindingWithMembersAndMethods
79 static int get_int (lua_State *L, void *v)
81 lua_pushinteger (L, *(int*)v);
82 return 1;
85 static int set_int (lua_State *L, void *v)
87 *(int*)v = luaL_checkint(L, 3);
88 return 0;
91 static int get_number (lua_State *L, void *v)
93 lua_pushnumber(L, *(lua_Number*)v);
94 return 1;
97 static int set_number (lua_State *L, void *v)
99 *(lua_Number*)v = luaL_checknumber(L, 3);
100 return 0;
103 static int get_string (lua_State *L, void *v)
105 lua_pushstring(L, (char*)v );
106 return 1;
109 static int set_string (lua_State *L, void *v)
111 *(const char**)v = luaL_checkstring(L, 3);
112 return 0;
115 typedef int (*Xet_func) (lua_State *L, void *v);
117 /* member info for get and set handlers */
118 struct Xet_reg
120 const char *name; /* member name */
121 Xet_func func; /* get or set function for type of member */
122 size_t offset; /* offset of member within the struct */
123 int (*absolute)(lua_State *);
126 static void Xet_add (lua_State *L, const struct Xet_reg *l)
128 if (!l)
129 return;
130 for (; l->name; l++)
132 lua_pushstring(L, l->name);
133 lua_pushlightuserdata(L, (void*)l);
134 lua_settable(L, -3);
138 static int Xet_call (lua_State *L)
140 /* for get: stack has userdata, index, lightuserdata */
141 /* for set: stack has userdata, index, value, lightuserdata */
142 const struct Xet_reg *m = (const struct Xet_reg *)lua_touserdata(L, -1); /* member info */
143 lua_pop(L, 1); /* drop lightuserdata */
144 luaL_checktype(L, 1, LUA_TUSERDATA);
145 if (m->absolute)
146 return m->absolute(L);
147 return m->func(L, lua_touserdata(L, 1) + m->offset);
150 static int index_handler (lua_State *L)
152 /* stack has userdata, index */
153 lua_pushvalue(L, 2); /* dup index */
154 lua_rawget(L, lua_upvalueindex(1)); /* lookup member by name */
155 if (!lua_islightuserdata(L, -1))
157 lua_pop(L, 1); /* drop value */
158 lua_pushvalue(L, 2); /* dup index */
159 lua_gettable(L, lua_upvalueindex(2)); /* else try methods */
160 if (lua_isnil(L, -1)) /* invalid member */
161 luaL_error(L, "cannot get member '%s'", lua_tostring(L, 2));
162 return 1;
164 return Xet_call(L); /* call get function */
167 static int newindex_handler (lua_State *L)
169 /* stack has userdata, index, value */
170 lua_pushvalue(L, 2); /* dup index */
171 lua_rawget(L, lua_upvalueindex(1)); /* lookup member by name */
172 if (!lua_islightuserdata(L, -1)) /* invalid member */
173 luaL_error(L, "cannot set member '%s'", lua_tostring(L, 2));
174 return Xet_call(L); /* call set function */
177 static int
178 struct_register(lua_State *L, const char *name, const luaL_reg fn_methods[], const luaL_reg meta_methods[],
179 const struct Xet_reg setters[], const struct Xet_reg getters[])
181 int metatable, methods;
183 /* create methods table & add it to the table of globals */
184 luaL_register(L, name, fn_methods);
185 methods = lua_gettop(L);
187 /* create metatable & add it to the registry */
188 luaL_newmetatable(L, name);
189 luaL_register(L, 0, meta_methods); /* fill metatable */
190 metatable = lua_gettop(L);
192 lua_pushliteral(L, "__metatable");
193 lua_pushvalue(L, methods); /* dup methods table*/
194 lua_rawset(L, metatable); /* hide metatable:
195 metatable.__metatable = methods */
197 lua_pushliteral(L, "__index");
198 lua_pushvalue(L, metatable); /* upvalue index 1 */
199 Xet_add(L, getters); /* fill metatable with getters */
200 lua_pushvalue(L, methods); /* upvalue index 2 */
201 lua_pushcclosure(L, index_handler, 2);
202 lua_rawset(L, metatable); /* metatable.__index = index_handler */
204 lua_pushliteral(L, "__newindex");
205 lua_newtable(L); /* table for members you can set */
206 Xet_add(L, setters); /* fill with setters */
207 lua_pushcclosure(L, newindex_handler, 1);
208 lua_rawset(L, metatable); /* metatable.__newindex = newindex_handler */
210 lua_pop(L, 1); /* drop metatable */
211 return 1; /* return methods on the stack */
214 /** }}} */
216 /** Window {{{ */
218 PUSH_TYPE(window, struct win)
220 CHECK_TYPE(window, struct win)
222 static int get_window(lua_State *L, void *v)
224 push_window(L, *(struct win **)v);
225 return 1;
228 static int
229 window_change_title(lua_State *L)
231 struct win *w = check_window(L, 1);
232 unsigned int len;
233 const char *title = luaL_checklstring(L, 2, &len);
234 ChangeAKA(w, title, len);
235 return 0;
238 static const luaL_reg window_methods[] = {
239 {"change_title", window_change_title},
240 {0, 0}
243 static int
244 window_tostring(lua_State *L)
246 char str[128];
247 struct win *w = check_window(L, 1);
248 snprintf(str, sizeof(str), "{window #%d: '%s'}", w->w_number, w->w_title);
249 lua_pushstring(L, str);
250 return 1;
253 static const luaL_reg window_metamethods[] = {
254 {"__tostring", window_tostring},
255 {0, 0}
258 static const struct Xet_reg window_setters[] = {
259 {0, 0}
262 static const struct Xet_reg window_getters[] = {
263 {"title", get_string, offsetof(struct win, w_title) + 8},
264 {"number", get_int, offsetof(struct win, w_number)},
265 {"dir", get_string, offsetof(struct win, w_dir)},
266 {"tty", get_string, offsetof(struct win, w_tty)},
267 {"pid", get_int, offsetof(struct win, w_pid)},
268 {"group", get_window, offsetof(struct win, w_group)},
269 {"bell", get_int, offsetof(struct win, w_bell)},
270 {"monitor", get_int, offsetof(struct win, w_monitor)},
271 {0, 0}
275 /** }}} */
277 /** AclUser {{{ */
279 PUSH_TYPE(user, struct acluser)
281 CHECK_TYPE(user, struct acluser)
283 static int
284 get_user(lua_State *L, void *v)
286 push_user(L, *(struct acluser **)v);
287 return 1;
290 static const luaL_reg user_methods[] = {
291 {0, 0}
294 static int
295 user_tostring(lua_State *L)
297 char str[128];
298 struct acluser *u = check_user(L, 1);
299 snprintf(str, sizeof(str), "{user '%s'}", u->u_name);
300 lua_pushstring(L, str);
301 return 1;
304 static const luaL_reg user_metamethods[] = {
305 {"__tostring", user_tostring},
306 {0, 0}
309 static const struct Xet_reg user_setters[] = {
310 {0, 0}
313 static const struct Xet_reg user_getters[] = {
314 {"name", get_string, offsetof(struct acluser, u_name)},
315 {"password", get_string, offsetof(struct acluser, u_password)},
316 {0, 0}
319 /** }}} */
321 /** Canvas {{{ */
323 PUSH_TYPE(canvas, struct canvas)
325 CHECK_TYPE(canvas, struct canvas)
327 static int
328 get_canvas(lua_State *L, void *v)
330 push_canvas(L, *(struct canvas **)v);
331 return 1;
334 static int
335 canvas_select(lua_State *L)
337 struct canvas *c = check_canvas(L, 1);
338 if (!display || D_forecv == c)
339 return 0;
340 D_forecv = c;
342 /* XXX: the following all is duplicated from process.c:DoAction.
343 * Should these be in some better place?
345 ResizeCanvas(&D_canvas);
346 RecreateCanvasChain();
347 RethinkDisplayViewports();
348 ResizeLayersToCanvases(); /* redisplays */
349 fore = D_fore = Layer2Window(D_forecv->c_layer);
350 flayer = D_forecv->c_layer;
351 #ifdef RXVT_OSC
352 if (D_xtermosc[2] || D_xtermosc[3])
354 Activate(-1);
355 break;
357 #endif
358 RefreshHStatus();
359 #ifdef RXVT_OSC
360 RefreshXtermOSC();
361 #endif
362 flayer = D_forecv->c_layer;
363 CV_CALL(D_forecv, LayRestore();LaySetCursor());
364 WindowChanged(0, 'F');
367 static const luaL_reg canvas_methods[] = {
368 {"select", canvas_select},
369 {0, 0}
372 static const luaL_reg canvas_metamethods[] = {
373 {0, 0}
376 static const struct Xet_reg canvas_setters[] = {
377 {0, 0}
380 static int
381 canvas_get_window(lua_State *L)
383 struct canvas *c = check_canvas(L, 1);
384 if (c->c_layer && c->c_layer->l_data && c->c_layer->l_layfn == &WinLf) /* ... go figure */
385 push_window(L, c->c_layer->l_data);
386 else
387 lua_pushnil(L);
388 return 1;
391 static const struct Xet_reg canvas_getters[] = {
392 {"next", get_canvas, offsetof(struct canvas, c_next)},
393 {"xoff", get_int, offsetof(struct canvas, c_xoff)},
394 {"yoff", get_int, offsetof(struct canvas, c_yoff)},
395 {"xs", get_int, offsetof(struct canvas, c_xs)},
396 {"ys", get_int, offsetof(struct canvas, c_ys)},
397 {"xe", get_int, offsetof(struct canvas, c_xe)},
398 {"ye", get_int, offsetof(struct canvas, c_ye)},
399 {"window", 0, 0, canvas_get_window},
400 {0, 0}
403 /** }}} */
405 /** Display {{{ */
407 PUSH_TYPE(display, struct display)
409 CHECK_TYPE(display, struct display)
411 static int
412 display_get_canvases(lua_State *L)
414 struct display *d;
415 struct canvas *iter;
416 int count;
418 d = check_display(L, 1);
419 for (iter = d->d_cvlist, count = 0; iter; iter = iter->c_next, count++)
420 push_canvas(L, iter);
422 return count;
425 static const luaL_reg display_methods[] = {
426 {"get_canvases", display_get_canvases},
427 {0, 0}
430 static int
431 display_tostring(lua_State *L)
433 char str[128];
434 struct display *d = check_display(L, 1);
435 snprintf(str, sizeof(str), "{display: tty = '%s', term = '%s'}", d->d_usertty, d->d_termname);
436 lua_pushstring(L, str);
437 return 1;
440 static const luaL_reg display_metamethods[] = {
441 {"__tostring", display_tostring},
442 {0, 0}
445 static const struct Xet_reg display_setters[] = {
446 {0, 0}
449 static const struct Xet_reg display_getters[] = {
450 {"tty", get_string, offsetof(struct display, d_usertty)},
451 {"term", get_string, offsetof(struct display, d_termname)},
452 {"fore", get_window, offsetof(struct display, d_fore)},
453 {"other", get_window, offsetof(struct display, d_other)},
454 {"width", get_int, offsetof(struct display, d_width)},
455 {"height", get_int, offsetof(struct display, d_height)},
456 {"user", get_user, offsetof(struct display, d_user)},
457 {0, 0}
460 /** }}} */
462 /** Screen {{{ */
464 static int
465 screen_get_windows(lua_State *L)
467 struct win *iter;
468 int count;
470 for (iter = windows, count = 0; iter; iter = iter->w_next, count++)
471 push_window(L, iter);
473 return count;
476 static int
477 screen_get_displays(lua_State *L)
479 struct display *iter;
480 int count;
482 for (iter = displays, count = 0; iter; iter = iter->d_next, count++)
483 push_display(L, iter);
485 return count;
488 static int
489 screen_get_display(lua_State *L)
491 push_display(L, display);
492 return 1;
495 static int
496 screen_exec_command(lua_State *L)
498 const char *command;
499 unsigned int len;
501 command = luaL_checklstring(L, 1, &len);
502 if (command)
503 RcLine(command, len);
505 return 0;
508 static const luaL_reg screen_methods[] = {
509 {"windows", screen_get_windows},
510 {"displays", screen_get_displays},
511 {"display", screen_get_display},
512 {"command", screen_exec_command},
513 {0, 0}
516 static const luaL_reg screen_metamethods[] = {
517 {0, 0}
520 static const struct Xet_reg screen_setters[] = {
521 {0, 0}
524 static const struct Xet_reg screen_getters[] = {
525 {0, 0}
528 /** }}} */
530 /** Public functions {{{ */
531 static lua_State *L;
532 int LuaInit(void)
534 L = luaL_newstate();
536 luaL_openlibs(L);
538 #define REGISTER(X) struct_register(L, #X , X ## _methods, X##_metamethods, X##_setters, X##_getters)
540 REGISTER(screen);
541 REGISTER(window);
542 REGISTER(display);
543 REGISTER(user);
544 REGISTER(canvas);
546 return 0;
549 int LuaForeWindowChanged(void)
551 if (!L)
552 return 0;
553 lua_getfield(L, LUA_GLOBALSINDEX, "fore_changed");
554 if (lua_isnil(L, -1))
555 return 0;
556 push_display(L, display);
557 push_window(L, display ? D_fore : fore);
558 if (lua_pcall(L, 2, 0, 0) == LUA_ERRRUN)
560 if(lua_isstring(L, -1))
562 unsigned int len;
563 char *message = luaL_checklstring(L, -1, &len);
564 LMsg(1, "%s", message ? message : "Unknown error");
565 lua_pop(L, 1);
568 return 0;
571 int LuaSource(const char *file)
573 if (!L)
574 return 0;
575 struct stat st;
576 if (stat(file, &st) == -1)
577 Msg(errno, "Error loading lua script file '%s'", file);
578 else
580 int len = strlen(file);
581 if (len < 4 || strncmp(file + len - 4, ".lua", 4) != 0)
582 return 0;
583 luaL_dofile(L, file);
584 return 1;
586 return 0;
589 int LuaFinit(void)
591 if (!L)
592 return 0;
593 lua_close(L);
594 L = (lua_State*)0;
595 return 0;
598 int LuaCall(char **argv)
600 int argc;
601 if (!L)
602 return 0;
604 lua_getfield(L, LUA_GLOBALSINDEX, *argv);
605 for (argc = 0, argv++; *argv; argv++, argc++)
607 lua_pushstring(L, *argv);
609 if (lua_pcall(L, argc, 0, 0) == LUA_ERRRUN)
611 if(lua_isstring(L, -1))
613 unsigned int len;
614 char *message = luaL_checklstring(L, -1, &len);
615 LMsg(1, "%s", message ? message : "Unknown error");
616 lua_pop(L, 1);
617 return 0;
620 return 1;
624 /** }}} */
626 struct ScriptFuncs LuaFuncs =
628 LuaInit,
629 LuaFinit,
630 LuaForeWindowChanged,
631 LuaSource