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)
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>
39 extern struct win
*windows
, *fore
;
40 extern struct display
*displays
, *display
;
41 extern struct LayFuncs WinLf
;
42 extern struct layer
*flayer
;
44 static int LuaDispatch(void *handler
, const char *params
, va_list va
);
45 static int LuaRegEvent(lua_State
*L
);
49 #define CHECK_TYPE(name, type) \
51 check_##name(lua_State *L, int index) \
54 luaL_checktype(L, index, LUA_TUSERDATA); \
55 var = (type **) luaL_checkudata(L, index, #name); \
57 luaL_typerror(L, index, #name); \
61 #define PUSH_TYPE(name, type) \
63 push_##name(lua_State *L, type **t) \
70 r = (type **)lua_newuserdata(L, sizeof(type *)); \
72 luaL_getmetatable(L, #name); \
73 lua_setmetatable(L,-2); \
77 /* Much of the following template comes from:
78 * http://lua-users.org/wiki/BindingWithMembersAndMethods
81 static int get_int (lua_State
*L
, void *v
)
83 lua_pushinteger (L
, *(int*)v
);
87 static int set_int (lua_State
*L
, void *v
)
89 *(int*)v
= luaL_checkint(L
, 3);
93 static int get_number (lua_State
*L
, void *v
)
95 lua_pushnumber(L
, *(lua_Number
*)v
);
99 static int set_number (lua_State
*L
, void *v
)
101 *(lua_Number
*)v
= luaL_checknumber(L
, 3);
105 static int get_string (lua_State
*L
, void *v
)
107 lua_pushstring(L
, (char*)v
);
111 static int set_string (lua_State
*L
, void *v
)
113 *(const char**)v
= luaL_checkstring(L
, 3);
117 typedef int (*Xet_func
) (lua_State
*L
, void *v
);
119 /* member info for get and set handlers */
122 const char *name
; /* member name */
123 Xet_func func
; /* get or set function for type of member */
124 size_t offset
; /* offset of member within the struct */
125 int (*absolute
)(lua_State
*);
128 static void Xet_add (lua_State
*L
, const struct Xet_reg
*l
)
134 lua_pushstring(L
, l
->name
);
135 lua_pushlightuserdata(L
, (void*)l
);
140 static int Xet_call (lua_State
*L
)
142 /* for get: stack has userdata, index, lightuserdata */
143 /* for set: stack has userdata, index, value, lightuserdata */
144 const struct Xet_reg
*m
= (const struct Xet_reg
*)lua_touserdata(L
, -1); /* member info */
145 lua_pop(L
, 1); /* drop lightuserdata */
146 luaL_checktype(L
, 1, LUA_TUSERDATA
);
148 return m
->absolute(L
);
149 return m
->func(L
, *(char**)lua_touserdata(L
, 1) + m
->offset
);
152 static int index_handler (lua_State
*L
)
154 /* stack has userdata, index */
155 lua_pushvalue(L
, 2); /* dup index */
156 lua_rawget(L
, lua_upvalueindex(1)); /* lookup member by name */
157 if (!lua_islightuserdata(L
, -1))
159 lua_pop(L
, 1); /* drop value */
160 lua_pushvalue(L
, 2); /* dup index */
161 lua_gettable(L
, lua_upvalueindex(2)); /* else try methods */
162 if (lua_isnil(L
, -1)) /* invalid member */
163 luaL_error(L
, "cannot get member '%s'", lua_tostring(L
, 2));
166 return Xet_call(L
); /* call get function */
169 static int newindex_handler (lua_State
*L
)
171 /* stack has userdata, index, value */
172 lua_pushvalue(L
, 2); /* dup index */
173 lua_rawget(L
, lua_upvalueindex(1)); /* lookup member by name */
174 if (!lua_islightuserdata(L
, -1)) /* invalid member */
175 luaL_error(L
, "cannot set member '%s'", lua_tostring(L
, 2));
176 return Xet_call(L
); /* call set function */
180 struct_register(lua_State
*L
, const char *name
, const luaL_reg fn_methods
[], const luaL_reg meta_methods
[],
181 const struct Xet_reg setters
[], const struct Xet_reg getters
[])
183 int metatable
, methods
;
185 /* create methods table & add it to the table of globals */
186 luaL_register(L
, name
, fn_methods
);
187 methods
= lua_gettop(L
);
189 /* create metatable & add it to the registry */
190 luaL_newmetatable(L
, name
);
191 luaL_register(L
, 0, meta_methods
); /* fill metatable */
193 /* To identify the type of object */
194 lua_pushstring(L
, "_objname");
195 lua_pushstring(L
, name
);
198 metatable
= lua_gettop(L
);
200 lua_pushliteral(L
, "__metatable");
201 lua_pushvalue(L
, methods
); /* dup methods table*/
202 lua_rawset(L
, metatable
); /* hide metatable:
203 metatable.__metatable = methods */
205 lua_pushliteral(L
, "__index");
206 lua_pushvalue(L
, metatable
); /* upvalue index 1 */
207 Xet_add(L
, getters
); /* fill metatable with getters */
208 lua_pushvalue(L
, methods
); /* upvalue index 2 */
209 lua_pushcclosure(L
, index_handler
, 2);
210 lua_rawset(L
, metatable
); /* metatable.__index = index_handler */
212 lua_pushliteral(L
, "__newindex");
213 lua_newtable(L
); /* table for members you can set */
214 Xet_add(L
, setters
); /* fill with setters */
215 lua_pushcclosure(L
, newindex_handler
, 1);
216 lua_rawset(L
, metatable
); /* metatable.__newindex = newindex_handler */
218 lua_pop(L
, 1); /* drop metatable */
219 return 1; /* return methods on the stack */
226 PUSH_TYPE(window
, struct win
)
228 CHECK_TYPE(window
, struct win
)
230 static int get_window(lua_State
*L
, void *v
)
232 push_window(L
, (struct win
**)v
);
237 window_change_title(lua_State
*L
)
239 struct win
*w
= check_window(L
, 1);
241 const char *title
= luaL_checklstring(L
, 2, &len
);
242 ChangeAKA(w
, (char *)title
, len
);
247 window_get_monitor_status(lua_State
*L
)
249 struct win
*w
= check_window(L
, 1);
250 int activity
= luaL_checkint(L
, 2);
253 lua_pushinteger(L
, w
->w_monitor
!= MON_OFF
);
256 lua_pushinteger(L
, w
->w_silence
== SILENCE_ON
? w
->w_silencewait
: 0);
261 static const luaL_reg window_methods
[] = {
262 {"get_monitor_status", window_get_monitor_status
},
263 {"listen_to", LuaRegEvent
},
268 window_tostring(lua_State
*L
)
271 struct win
*w
= check_window(L
, 1);
272 snprintf(str
, sizeof(str
), "{window #%d: '%s'}", w
->w_number
, w
->w_title
);
273 lua_pushstring(L
, str
);
278 window_equality(lua_State
*L
)
280 struct win
*w1
= check_window(L
, 1), *w2
= check_window(L
, 2);
281 lua_pushboolean(L
, (w1
== w2
|| (w1
&& w2
&& w1
->w_number
== w2
->w_number
)));
285 static const luaL_reg window_metamethods
[] = {
286 {"__tostring", window_tostring
},
287 {"__eq", window_equality
},
291 static const struct Xet_reg window_setters
[] = {
292 {"title", 0, 0, window_change_title
/*absolute setter*/},
296 static const struct Xet_reg window_getters
[] = {
297 {"title", get_string
, offsetof(struct win
, w_title
) + 8},
298 {"number", get_int
, offsetof(struct win
, w_number
)},
299 {"dir", get_string
, offsetof(struct win
, w_dir
)},
300 {"tty", get_string
, offsetof(struct win
, w_tty
)},
301 {"pid", get_int
, offsetof(struct win
, w_pid
)},
302 {"group", get_window
, offsetof(struct win
, w_group
)},
303 {"bell", get_int
, offsetof(struct win
, w_bell
)},
312 PUSH_TYPE(user
, struct acluser
)
314 CHECK_TYPE(user
, struct acluser
)
317 get_user(lua_State
*L
, void *v
)
319 push_user(L
, (struct acluser
**)v
);
323 static const luaL_reg user_methods
[] = {
328 user_tostring(lua_State
*L
)
331 struct acluser
*u
= check_user(L
, 1);
332 snprintf(str
, sizeof(str
), "{user '%s'}", u
->u_name
);
333 lua_pushstring(L
, str
);
337 static const luaL_reg user_metamethods
[] = {
338 {"__tostring", user_tostring
},
342 static const struct Xet_reg user_setters
[] = {
346 static const struct Xet_reg user_getters
[] = {
347 {"name", get_string
, offsetof(struct acluser
, u_name
)},
348 {"password", get_string
, offsetof(struct acluser
, u_password
)},
356 PUSH_TYPE(canvas
, struct canvas
)
358 CHECK_TYPE(canvas
, struct canvas
)
361 get_canvas(lua_State
*L
, void *v
)
363 push_canvas(L
, (struct canvas
**)v
);
368 canvas_select(lua_State
*L
)
370 struct canvas
*c
= check_canvas(L
, 1);
371 if (!display
|| D_forecv
== c
)
373 SetCanvasWindow(c
, Layer2Window(c
->c_layer
));
376 /* XXX: the following all is duplicated from process.c:DoAction.
377 * Should these be in some better place?
379 ResizeCanvas(&D_canvas
);
380 RecreateCanvasChain();
381 RethinkDisplayViewports();
382 ResizeLayersToCanvases(); /* redisplays */
383 fore
= D_fore
= Layer2Window(D_forecv
->c_layer
);
384 flayer
= D_forecv
->c_layer
;
386 if (D_xtermosc
[2] || D_xtermosc
[3])
396 flayer
= D_forecv
->c_layer
;
397 CV_CALL(D_forecv
, LayRestore();LaySetCursor());
398 WindowChanged(0, 'F');
402 static const luaL_reg canvas_methods
[] = {
403 {"select", canvas_select
},
407 static const luaL_reg canvas_metamethods
[] = {
411 static const struct Xet_reg canvas_setters
[] = {
416 canvas_get_window(lua_State
*L
)
418 struct canvas
*c
= check_canvas(L
, 1);
419 struct win
*win
= Layer2Window(c
->c_layer
);
421 push_window(L
, &win
);
427 static const struct Xet_reg canvas_getters
[] = {
428 {"next", get_canvas
, offsetof(struct canvas
, c_next
)},
429 {"xoff", get_int
, offsetof(struct canvas
, c_xoff
)},
430 {"yoff", get_int
, offsetof(struct canvas
, c_yoff
)},
431 {"xs", get_int
, offsetof(struct canvas
, c_xs
)},
432 {"ys", get_int
, offsetof(struct canvas
, c_ys
)},
433 {"xe", get_int
, offsetof(struct canvas
, c_xe
)},
434 {"ye", get_int
, offsetof(struct canvas
, c_ye
)},
435 {"window", 0, 0, canvas_get_window
},
443 PUSH_TYPE(layout
, struct layout
)
444 CHECK_TYPE(layout
, struct layout
)
446 static const struct Xet_reg layout_getters
[] = {
451 get_layout(lua_State
*L
, void *v
)
453 push_layout(L
, (struct layout
**)v
);
461 PUSH_TYPE(display
, struct display
)
463 CHECK_TYPE(display
, struct display
)
466 display_get_canvases(lua_State
*L
)
472 d
= check_display(L
, 1);
474 for (iter
= d
->d_cvlist
, count
= 0; iter
; iter
= iter
->c_next
, count
++) {
475 lua_pushinteger(L
, count
);
476 push_canvas(L
, &iter
);
483 static const luaL_reg display_methods
[] = {
484 {"get_canvases", display_get_canvases
},
489 display_tostring(lua_State
*L
)
492 struct display
*d
= check_display(L
, 1);
493 snprintf(str
, sizeof(str
), "{display: tty = '%s', term = '%s'}", d
->d_usertty
, d
->d_termname
);
494 lua_pushstring(L
, str
);
498 static const luaL_reg display_metamethods
[] = {
499 {"__tostring", display_tostring
},
503 static const struct Xet_reg display_setters
[] = {
507 static const struct Xet_reg display_getters
[] = {
508 {"tty", get_string
, offsetof(struct display
, d_usertty
)},
509 {"term", get_string
, offsetof(struct display
, d_termname
)},
510 {"fore", get_window
, offsetof(struct display
, d_fore
)},
511 {"other", get_window
, offsetof(struct display
, d_other
)},
512 {"width", get_int
, offsetof(struct display
, d_width
)},
513 {"height", get_int
, offsetof(struct display
, d_height
)},
514 {"user", get_user
, offsetof(struct display
, d_user
)},
515 {"layout", get_layout
, offsetof(struct display
, d_layout
)},
524 screen_get_windows(lua_State
*L
)
530 for (iter
= windows
, count
= 0; iter
; iter
= iter
->w_next
, count
++) {
531 lua_pushinteger(L
, iter
->w_number
);
532 push_window(L
, &iter
);
540 screen_get_displays(lua_State
*L
)
542 struct display
*iter
;
546 for (iter
= displays
, count
= 0; iter
; iter
= iter
->d_next
, count
++) {
547 lua_pushinteger(L
, count
);
548 push_display(L
, &iter
);
556 screen_get_display(lua_State
*L
)
558 push_display(L
, &display
);
563 screen_exec_command(lua_State
*L
)
568 command
= luaL_checklstring(L
, 1, &len
);
570 RcLine((char *)command
, len
);
576 screen_append_msg(lua_State
*L
)
578 const char *msg
, *color
;
580 msg
= luaL_checklstring(L
, 1, &len
);
584 color
= luaL_checklstring(L
, 2, &len
);
585 AppendWinMsgRend(msg
, color
);
589 static const luaL_reg screen_methods
[] = {
590 {"windows", screen_get_windows
},
591 {"displays", screen_get_displays
},
592 {"display", screen_get_display
},
593 {"command", screen_exec_command
},
594 {"append_msg", screen_append_msg
},
595 {"listen_to", LuaRegEvent
},
599 static const luaL_reg screen_metamethods
[] = {
603 static const struct Xet_reg screen_setters
[] = {
607 static const struct Xet_reg screen_getters
[] = {
613 /** Public functions {{{ */
621 #define REGISTER(X) struct_register(L, #X , X ## _methods, X##_metamethods, X##_setters, X##_getters)
632 /* An error message on top of the stack. */
634 LuaShowErr(lua_State
*L
)
636 struct display
*d
= display
;
638 const char *message
= luaL_checklstring(L
, -1, &len
);
639 LMsg(0, "%s", message
? message
: "Unknown error");
646 void (*push_fn
)(lua_State
*, void*);
651 LuaCallProcess(const char *name
, struct fn_def defs
[])
656 lua_getfield(L
, LUA_GLOBALSINDEX
, name
);
657 if (lua_isnil(L
, -1))
659 for (argc
= 0; defs
[argc
].push_fn
; argc
++)
660 defs
[argc
].push_fn(L
, defs
[argc
].value
);
661 if (lua_pcall(L
, argc
, 0, 0) == LUA_ERRRUN
&& lua_isstring(L
, -1))
669 int LuaSource(const char *file
, int async
)
674 if (stat(file
, &st
) == -1)
675 Msg(errno
, "Error loading lua script file '%s'", file
);
678 int len
= strlen(file
);
679 if (len
< 4 || strncmp(file
+ len
- 4, ".lua", 4) != 0)
681 if (luaL_dofile(L
, file
) && lua_isstring(L
, -1))
700 LuaProcessCaption(const char *caption
, struct win
*win
, int len
)
704 struct fn_def params
[] = {
705 {lua_pushstring
, caption
},
707 {lua_pushinteger
, len
},
710 return LuaCallProcess("process_caption", params
);
714 push_stringarray(lua_State
*L
, char **args
)
718 for (i
= 1; args
&& *args
; i
++) {
719 lua_pushinteger(L
, i
);
720 lua_pushstring(L
, *args
++);
726 LuaPushParams(lua_State
*L
, const char *params
, va_list va
)
734 lua_pushstring(L
, va_arg(va
, char *));
737 push_stringarray(L
, va_arg(va
, char **));
740 lua_pushinteger(L
, va_arg(va
, int));
748 int LuaCall(char *func
, char **argv
)
754 lua_getfield(L
, LUA_GLOBALSINDEX
, func
);
755 for (argc
= 0; *argv
; argv
++, argc
++)
757 lua_pushstring(L
, *argv
);
759 if (lua_pcall(L
, argc
, 0, 0) == LUA_ERRRUN
)
761 if(lua_isstring(L
, -1))
771 LuaDispatch(void *handler
, const char *params
, va_list va
)
773 const char *func
= handler
;
776 /*FIXME:Really need this?*/
779 lua_getfield(L
, LUA_GLOBALSINDEX
, func
);
780 if (lua_isnil(L
, -1))
782 argc
= LuaPushParams(L
, params
, va
);
784 if (lua_pcall(L
, argc
, 0, 0) == LUA_ERRRUN
&& lua_isstring(L
, -1))
792 int LuaForeWindowChanged(void)
794 struct fn_def params
[] = {
795 {push_display
, &display
},
796 {push_window
, display
? &D_fore
: &fore
},
801 return LuaCallProcess("fore_changed", params
);
806 LuaCommandExecuted(const char *command, const char **args, int argc)
810 struct fn_def params[] = {
811 {lua_pushstring, command},
812 {push_stringarray, args},
815 return LuaCallProcess("command_executed", params);
819 LuaRegEvent(lua_State
*L
)
821 /* signature: listen_to(obj, event, handler, priv);
822 * or: listen_to(event, handler, priv)
823 * returns: A ticket for later unregister. */
824 int idx
= 1, argc
= lua_gettop(L
);
828 const char *objname
= "global";
830 static char evbuf
[30];
831 const char *event
, *handler
;
833 struct script_event
*sev
;
835 /* Identify the object, if specified */
836 if (luaL_getmetafield(L
, 1, "_objname"))
838 objname
= luaL_checkstring(L
, -1);
840 if (!strcmp("screen", objname
))
843 obj
= *(char **)lua_touserdata(L
, 1);
847 event
= luaL_checkstring(L
, idx
++);
848 snprintf(evbuf
, 30, "%s_%s", objname
, event
);
849 handler
= luaL_checkstring(L
, idx
++);
851 priv
= luaL_checkinteger(L
, idx
);
853 sev
= object_get_event(obj
, evbuf
);
857 l
= (struct listener
*)malloc(sizeof(struct listener
));
859 return luaL_error(L
, "Out of memory");
860 l
->handler
= (void *)handler
;
862 l
->dispatcher
= LuaDispatch
;
863 register_listener(sev
, l
);
864 /*Return the handler for un-register*/
865 lua_pushlightuserdata(L
, l
);
868 return luaL_error(L
, "Invalid event specified: %s for object %s", event
, objname
);
874 LuaUnRegEvent(lua_State
*L
)
876 /* signature: release([obj], ticket, handler)
877 * returns: zero of success, non-zero otherwise */
881 if (lua_isuserdata(L
, idx
))
884 luaL_checktype(L
, idx
, LUA_TLIGHTUSERDATA
);
885 l
= (struct listener
*)lua_touserdata(L
, idx
++);
886 handler
= luaL_checkstring(L
, idx
++);
888 /*Validate the listener structure*/
889 if (strcmp((char *)handler
, (char *)l
->handler
))
892 lua_pushinteger(L
, 1);
896 unregister_listener(l
);
897 lua_pushinteger(L
, 0);
905 struct ScriptFuncs LuaFuncs
=
907 LuaForeWindowChanged
,
911 struct binding lua_binding
=