Fix getting canvas.window
[screen-lua.git] / src / lua.c
blobb29ff0f21a97803f29e14bfbea0840188482674a
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 struct win *win = Layer2Window(c->c_layer);
385 if (win)
386 push_window(L, win);
387 else
388 lua_pushnil(L);
389 return 1;
392 static const struct Xet_reg canvas_getters[] = {
393 {"next", get_canvas, offsetof(struct canvas, c_next)},
394 {"xoff", get_int, offsetof(struct canvas, c_xoff)},
395 {"yoff", get_int, offsetof(struct canvas, c_yoff)},
396 {"xs", get_int, offsetof(struct canvas, c_xs)},
397 {"ys", get_int, offsetof(struct canvas, c_ys)},
398 {"xe", get_int, offsetof(struct canvas, c_xe)},
399 {"ye", get_int, offsetof(struct canvas, c_ye)},
400 {"window", 0, 0, canvas_get_window},
401 {0, 0}
404 /** }}} */
406 /** Display {{{ */
408 PUSH_TYPE(display, struct display)
410 CHECK_TYPE(display, struct display)
412 static int
413 display_get_canvases(lua_State *L)
415 struct display *d;
416 struct canvas *iter;
417 int count;
419 d = check_display(L, 1);
420 for (iter = d->d_cvlist, count = 0; iter; iter = iter->c_next, count++)
421 push_canvas(L, iter);
423 return count;
426 static const luaL_reg display_methods[] = {
427 {"get_canvases", display_get_canvases},
428 {0, 0}
431 static int
432 display_tostring(lua_State *L)
434 char str[128];
435 struct display *d = check_display(L, 1);
436 snprintf(str, sizeof(str), "{display: tty = '%s', term = '%s'}", d->d_usertty, d->d_termname);
437 lua_pushstring(L, str);
438 return 1;
441 static const luaL_reg display_metamethods[] = {
442 {"__tostring", display_tostring},
443 {0, 0}
446 static const struct Xet_reg display_setters[] = {
447 {0, 0}
450 static const struct Xet_reg display_getters[] = {
451 {"tty", get_string, offsetof(struct display, d_usertty)},
452 {"term", get_string, offsetof(struct display, d_termname)},
453 {"fore", get_window, offsetof(struct display, d_fore)},
454 {"other", get_window, offsetof(struct display, d_other)},
455 {"width", get_int, offsetof(struct display, d_width)},
456 {"height", get_int, offsetof(struct display, d_height)},
457 {"user", get_user, offsetof(struct display, d_user)},
458 {0, 0}
461 /** }}} */
463 /** Screen {{{ */
465 static int
466 screen_get_windows(lua_State *L)
468 struct win *iter;
469 int count;
471 for (iter = windows, count = 0; iter; iter = iter->w_next, count++)
472 push_window(L, iter);
474 return count;
477 static int
478 screen_get_displays(lua_State *L)
480 struct display *iter;
481 int count;
483 for (iter = displays, count = 0; iter; iter = iter->d_next, count++)
484 push_display(L, iter);
486 return count;
489 static int
490 screen_get_display(lua_State *L)
492 push_display(L, display);
493 return 1;
496 static int
497 screen_exec_command(lua_State *L)
499 const char *command;
500 unsigned int len;
502 command = luaL_checklstring(L, 1, &len);
503 if (command)
504 RcLine(command, len);
506 return 0;
509 static const luaL_reg screen_methods[] = {
510 {"windows", screen_get_windows},
511 {"displays", screen_get_displays},
512 {"display", screen_get_display},
513 {"command", screen_exec_command},
514 {0, 0}
517 static const luaL_reg screen_metamethods[] = {
518 {0, 0}
521 static const struct Xet_reg screen_setters[] = {
522 {0, 0}
525 static const struct Xet_reg screen_getters[] = {
526 {0, 0}
529 /** }}} */
531 /** Public functions {{{ */
532 static lua_State *L;
533 int LuaInit(void)
535 L = luaL_newstate();
537 luaL_openlibs(L);
539 #define REGISTER(X) struct_register(L, #X , X ## _methods, X##_metamethods, X##_setters, X##_getters)
541 REGISTER(screen);
542 REGISTER(window);
543 REGISTER(display);
544 REGISTER(user);
545 REGISTER(canvas);
547 return 0;
550 int LuaForeWindowChanged(void)
552 if (!L)
553 return 0;
554 lua_getfield(L, LUA_GLOBALSINDEX, "fore_changed");
555 if (lua_isnil(L, -1))
556 return 0;
557 push_display(L, display);
558 push_window(L, display ? D_fore : fore);
559 if (lua_pcall(L, 2, 0, 0) == LUA_ERRRUN)
561 if(lua_isstring(L, -1))
563 unsigned int len;
564 char *message = luaL_checklstring(L, -1, &len);
565 LMsg(1, "%s", message ? message : "Unknown error");
566 lua_pop(L, 1);
569 return 0;
572 int LuaSource(const char *file)
574 if (!L)
575 return 0;
576 struct stat st;
577 if (stat(file, &st) == -1)
578 Msg(errno, "Error loading lua script file '%s'", file);
579 else
581 int len = strlen(file);
582 if (len < 4 || strncmp(file + len - 4, ".lua", 4) != 0)
583 return 0;
584 luaL_dofile(L, file);
585 return 1;
587 return 0;
590 int LuaFinit(void)
592 if (!L)
593 return 0;
594 lua_close(L);
595 L = (lua_State*)0;
596 return 0;
599 int LuaCall(char **argv)
601 int argc;
602 if (!L)
603 return 0;
605 lua_getfield(L, LUA_GLOBALSINDEX, *argv);
606 for (argc = 0, argv++; *argv; argv++, argc++)
608 lua_pushstring(L, *argv);
610 if (lua_pcall(L, argc, 0, 0) == LUA_ERRRUN)
612 if(lua_isstring(L, -1))
614 unsigned int len;
615 char *message = luaL_checklstring(L, -1, &len);
616 LMsg(1, "%s", message ? message : "Unknown error");
617 lua_pop(L, 1);
618 return 0;
621 return 1;
625 /** }}} */
627 struct ScriptFuncs LuaFuncs =
629 LuaInit,
630 LuaFinit,
631 LuaForeWindowChanged,
632 LuaSource