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
);
46 static int LuaUnRegEvent(lua_State
*L
);
50 #define CHECK_TYPE(name, type) \
52 check_##name(lua_State *L, int index) \
55 luaL_checktype(L, index, LUA_TUSERDATA); \
56 var = (type **) luaL_checkudata(L, index, #name); \
58 luaL_typerror(L, index, #name); \
62 #define PUSH_TYPE(name, type) \
64 push_##name(lua_State *L, type **t) \
71 r = (type **)lua_newuserdata(L, sizeof(type *)); \
73 luaL_getmetatable(L, #name); \
74 lua_setmetatable(L,-2); \
78 /* Much of the following template comes from:
79 * http://lua-users.org/wiki/BindingWithMembersAndMethods
82 static int get_int (lua_State
*L
, void *v
)
84 lua_pushinteger (L
, *(int*)v
);
88 static int set_int (lua_State
*L
, void *v
)
90 *(int*)v
= luaL_checkint(L
, 3);
94 static int get_number (lua_State
*L
, void *v
)
96 lua_pushnumber(L
, *(lua_Number
*)v
);
100 static int set_number (lua_State
*L
, void *v
)
102 *(lua_Number
*)v
= luaL_checknumber(L
, 3);
106 static int get_string (lua_State
*L
, void *v
)
108 lua_pushstring(L
, (char*)v
);
112 static int set_string (lua_State
*L
, void *v
)
114 *(const char**)v
= luaL_checkstring(L
, 3);
118 typedef int (*Xet_func
) (lua_State
*L
, void *v
);
120 /* member info for get and set handlers */
123 const char *name
; /* member name */
124 Xet_func func
; /* get or set function for type of member */
125 size_t offset
; /* offset of member within the struct */
126 int (*absolute
)(lua_State
*);
129 static void Xet_add (lua_State
*L
, const struct Xet_reg
*l
)
135 lua_pushstring(L
, l
->name
);
136 lua_pushlightuserdata(L
, (void*)l
);
141 static int Xet_call (lua_State
*L
)
143 /* for get: stack has userdata, index, lightuserdata */
144 /* for set: stack has userdata, index, value, lightuserdata */
145 const struct Xet_reg
*m
= (const struct Xet_reg
*)lua_touserdata(L
, -1); /* member info */
146 lua_pop(L
, 1); /* drop lightuserdata */
147 luaL_checktype(L
, 1, LUA_TUSERDATA
);
149 return m
->absolute(L
);
150 return m
->func(L
, *(char**)lua_touserdata(L
, 1) + m
->offset
);
153 static int index_handler (lua_State
*L
)
155 /* stack has userdata, index */
156 lua_pushvalue(L
, 2); /* dup index */
157 lua_rawget(L
, lua_upvalueindex(1)); /* lookup member by name */
158 if (!lua_islightuserdata(L
, -1))
160 lua_pop(L
, 1); /* drop value */
161 lua_pushvalue(L
, 2); /* dup index */
162 lua_gettable(L
, lua_upvalueindex(2)); /* else try methods */
163 if (lua_isnil(L
, -1)) /* invalid member */
164 luaL_error(L
, "cannot get member '%s'", lua_tostring(L
, 2));
167 return Xet_call(L
); /* call get function */
170 static int newindex_handler (lua_State
*L
)
172 /* stack has userdata, index, value */
173 lua_pushvalue(L
, 2); /* dup index */
174 lua_rawget(L
, lua_upvalueindex(1)); /* lookup member by name */
175 if (!lua_islightuserdata(L
, -1)) /* invalid member */
176 luaL_error(L
, "cannot set member '%s'", lua_tostring(L
, 2));
177 return Xet_call(L
); /* call set function */
181 struct_register(lua_State
*L
, const char *name
, const luaL_reg fn_methods
[], const luaL_reg meta_methods
[],
182 const struct Xet_reg setters
[], const struct Xet_reg getters
[])
184 int metatable
, methods
;
186 /* create methods table & add it to the table of globals */
187 luaL_register(L
, name
, fn_methods
);
188 methods
= lua_gettop(L
);
190 /* create metatable & add it to the registry */
191 luaL_newmetatable(L
, name
);
192 luaL_register(L
, 0, meta_methods
); /* fill metatable */
194 /* To identify the type of object */
195 lua_pushstring(L
, "_objname");
196 lua_pushstring(L
, name
);
199 metatable
= lua_gettop(L
);
201 lua_pushliteral(L
, "__metatable");
202 lua_pushvalue(L
, methods
); /* dup methods table*/
203 lua_rawset(L
, metatable
); /* hide metatable:
204 metatable.__metatable = methods */
206 lua_pushliteral(L
, "__index");
207 lua_pushvalue(L
, metatable
); /* upvalue index 1 */
208 Xet_add(L
, getters
); /* fill metatable with getters */
209 lua_pushvalue(L
, methods
); /* upvalue index 2 */
210 lua_pushcclosure(L
, index_handler
, 2);
211 lua_rawset(L
, metatable
); /* metatable.__index = index_handler */
213 lua_pushliteral(L
, "__newindex");
214 lua_newtable(L
); /* table for members you can set */
215 Xet_add(L
, setters
); /* fill with setters */
216 lua_pushcclosure(L
, newindex_handler
, 1);
217 lua_rawset(L
, metatable
); /* metatable.__newindex = newindex_handler */
219 lua_pop(L
, 1); /* drop metatable */
220 return 1; /* return methods on the stack */
227 PUSH_TYPE(window
, struct win
)
229 CHECK_TYPE(window
, struct win
)
231 static int get_window(lua_State
*L
, void *v
)
233 push_window(L
, (struct win
**)v
);
238 window_change_title(lua_State
*L
)
240 struct win
*w
= check_window(L
, 1);
242 const char *title
= luaL_checklstring(L
, 2, &len
);
243 ChangeAKA(w
, (char *)title
, len
);
248 window_get_monitor_status(lua_State
*L
)
250 struct win
*w
= check_window(L
, 1);
251 int activity
= luaL_checkint(L
, 2);
254 lua_pushinteger(L
, w
->w_monitor
!= MON_OFF
);
257 lua_pushinteger(L
, w
->w_silence
== SILENCE_ON
? w
->w_silencewait
: 0);
262 static const luaL_reg window_methods
[] = {
263 {"get_monitor_status", window_get_monitor_status
},
264 {"hook", LuaRegEvent
},
269 window_tostring(lua_State
*L
)
272 struct win
*w
= check_window(L
, 1);
273 snprintf(str
, sizeof(str
), "{window #%d: '%s'}", w
->w_number
, w
->w_title
);
274 lua_pushstring(L
, str
);
279 window_equality(lua_State
*L
)
281 struct win
*w1
= check_window(L
, 1), *w2
= check_window(L
, 2);
282 lua_pushboolean(L
, (w1
== w2
|| (w1
&& w2
&& w1
->w_number
== w2
->w_number
)));
286 static const luaL_reg window_metamethods
[] = {
287 {"__tostring", window_tostring
},
288 {"__eq", window_equality
},
292 static const struct Xet_reg window_setters
[] = {
293 {"title", 0, 0, window_change_title
/*absolute setter*/},
297 static const struct Xet_reg window_getters
[] = {
298 {"title", get_string
, offsetof(struct win
, w_title
) + 8},
299 {"number", get_int
, offsetof(struct win
, w_number
)},
300 {"dir", get_string
, offsetof(struct win
, w_dir
)},
301 {"tty", get_string
, offsetof(struct win
, w_tty
)},
302 {"pid", get_int
, offsetof(struct win
, w_pid
)},
303 {"group", get_window
, offsetof(struct win
, w_group
)},
304 {"bell", get_int
, offsetof(struct win
, w_bell
)},
313 PUSH_TYPE(user
, struct acluser
)
315 CHECK_TYPE(user
, struct acluser
)
318 get_user(lua_State
*L
, void *v
)
320 push_user(L
, (struct acluser
**)v
);
324 static const luaL_reg user_methods
[] = {
329 user_tostring(lua_State
*L
)
332 struct acluser
*u
= check_user(L
, 1);
333 snprintf(str
, sizeof(str
), "{user '%s'}", u
->u_name
);
334 lua_pushstring(L
, str
);
338 static const luaL_reg user_metamethods
[] = {
339 {"__tostring", user_tostring
},
343 static const struct Xet_reg user_setters
[] = {
347 static const struct Xet_reg user_getters
[] = {
348 {"name", get_string
, offsetof(struct acluser
, u_name
)},
349 {"password", get_string
, offsetof(struct acluser
, u_password
)},
357 PUSH_TYPE(canvas
, struct canvas
)
359 CHECK_TYPE(canvas
, struct canvas
)
362 get_canvas(lua_State
*L
, void *v
)
364 push_canvas(L
, (struct canvas
**)v
);
369 canvas_select(lua_State
*L
)
371 struct canvas
*c
= check_canvas(L
, 1);
372 if (!display
|| D_forecv
== c
)
374 SetCanvasWindow(c
, Layer2Window(c
->c_layer
));
377 /* XXX: the following all is duplicated from process.c:DoAction.
378 * Should these be in some better place?
380 ResizeCanvas(&D_canvas
);
381 RecreateCanvasChain();
382 RethinkDisplayViewports();
383 ResizeLayersToCanvases(); /* redisplays */
384 fore
= D_fore
= Layer2Window(D_forecv
->c_layer
);
385 flayer
= D_forecv
->c_layer
;
387 if (D_xtermosc
[2] || D_xtermosc
[3])
397 flayer
= D_forecv
->c_layer
;
398 CV_CALL(D_forecv
, LayRestore();LaySetCursor());
399 WindowChanged(0, 'F');
403 static const luaL_reg canvas_methods
[] = {
404 {"select", canvas_select
},
408 static const luaL_reg canvas_metamethods
[] = {
412 static const struct Xet_reg canvas_setters
[] = {
417 canvas_get_window(lua_State
*L
)
419 struct canvas
*c
= check_canvas(L
, 1);
420 struct win
*win
= Layer2Window(c
->c_layer
);
422 push_window(L
, &win
);
428 static const struct Xet_reg canvas_getters
[] = {
429 {"next", get_canvas
, offsetof(struct canvas
, c_next
)},
430 {"xoff", get_int
, offsetof(struct canvas
, c_xoff
)},
431 {"yoff", get_int
, offsetof(struct canvas
, c_yoff
)},
432 {"xs", get_int
, offsetof(struct canvas
, c_xs
)},
433 {"ys", get_int
, offsetof(struct canvas
, c_ys
)},
434 {"xe", get_int
, offsetof(struct canvas
, c_xe
)},
435 {"ye", get_int
, offsetof(struct canvas
, c_ye
)},
436 {"window", 0, 0, canvas_get_window
},
444 PUSH_TYPE(layout
, struct layout
)
445 CHECK_TYPE(layout
, struct layout
)
447 static const struct Xet_reg layout_getters
[] = {
452 get_layout(lua_State
*L
, void *v
)
454 push_layout(L
, (struct layout
**)v
);
462 PUSH_TYPE(display
, struct display
)
464 CHECK_TYPE(display
, struct display
)
467 display_get_canvases(lua_State
*L
)
473 d
= check_display(L
, 1);
475 for (iter
= d
->d_cvlist
, count
= 0; iter
; iter
= iter
->c_next
, count
++) {
476 lua_pushinteger(L
, count
);
477 push_canvas(L
, &iter
);
484 static const luaL_reg display_methods
[] = {
485 {"get_canvases", display_get_canvases
},
490 display_tostring(lua_State
*L
)
493 struct display
*d
= check_display(L
, 1);
494 snprintf(str
, sizeof(str
), "{display: tty = '%s', term = '%s'}", d
->d_usertty
, d
->d_termname
);
495 lua_pushstring(L
, str
);
499 static const luaL_reg display_metamethods
[] = {
500 {"__tostring", display_tostring
},
504 static const struct Xet_reg display_setters
[] = {
508 static const struct Xet_reg display_getters
[] = {
509 {"tty", get_string
, offsetof(struct display
, d_usertty
)},
510 {"term", get_string
, offsetof(struct display
, d_termname
)},
511 {"fore", get_window
, offsetof(struct display
, d_fore
)},
512 {"other", get_window
, offsetof(struct display
, d_other
)},
513 {"width", get_int
, offsetof(struct display
, d_width
)},
514 {"height", get_int
, offsetof(struct display
, d_height
)},
515 {"user", get_user
, offsetof(struct display
, d_user
)},
516 {"layout", get_layout
, offsetof(struct display
, d_layout
)},
525 screen_get_windows(lua_State
*L
)
531 for (iter
= windows
, count
= 0; iter
; iter
= iter
->w_next
, count
++) {
532 lua_pushinteger(L
, iter
->w_number
);
533 push_window(L
, &iter
);
541 screen_get_displays(lua_State
*L
)
543 struct display
*iter
;
547 for (iter
= displays
, count
= 0; iter
; iter
= iter
->d_next
, count
++) {
548 lua_pushinteger(L
, count
);
549 push_display(L
, &iter
);
557 screen_get_display(lua_State
*L
)
559 push_display(L
, &display
);
564 screen_exec_command(lua_State
*L
)
569 command
= luaL_checklstring(L
, 1, &len
);
571 RcLine((char *)command
, len
);
577 screen_append_msg(lua_State
*L
)
579 const char *msg
, *color
;
581 msg
= luaL_checklstring(L
, 1, &len
);
585 color
= luaL_checklstring(L
, 2, &len
);
586 AppendWinMsgRend(msg
, color
);
590 static const luaL_reg screen_methods
[] = {
591 {"windows", screen_get_windows
},
592 {"displays", screen_get_displays
},
593 {"display", screen_get_display
},
594 {"command", screen_exec_command
},
595 {"append_msg", screen_append_msg
},
596 {"hook", LuaRegEvent
},
597 {"unhook", LuaUnRegEvent
},
601 static const luaL_reg screen_metamethods
[] = {
605 static const struct Xet_reg screen_setters
[] = {
609 static const struct Xet_reg screen_getters
[] = {
615 /** Public functions {{{ */
623 #define REGISTER(X) struct_register(L, #X , X ## _methods, X##_metamethods, X##_setters, X##_getters)
634 /* An error message on top of the stack. */
636 LuaShowErr(lua_State
*L
)
638 struct display
*d
= display
;
640 const char *message
= luaL_checklstring(L
, -1, &len
);
641 LMsg(0, "%s", message
? message
: "Unknown error");
648 void (*push_fn
)(lua_State
*, void*);
653 LuaCallProcess(const char *name
, struct fn_def defs
[])
658 lua_getfield(L
, LUA_GLOBALSINDEX
, name
);
659 if (lua_isnil(L
, -1))
661 for (argc
= 0; defs
[argc
].push_fn
; argc
++)
662 defs
[argc
].push_fn(L
, defs
[argc
].value
);
663 if (lua_pcall(L
, argc
, 0, 0) == LUA_ERRRUN
&& lua_isstring(L
, -1))
671 int LuaSource(const char *file
, int async
)
676 if (stat(file
, &st
) == -1)
677 Msg(errno
, "Error loading lua script file '%s'", file
);
680 int len
= strlen(file
);
681 if (len
< 4 || strncmp(file
+ len
- 4, ".lua", 4) != 0)
683 if (luaL_dofile(L
, file
) && lua_isstring(L
, -1))
702 LuaProcessCaption(const char *caption
, struct win
*win
, int len
)
706 struct fn_def params
[] = {
707 {lua_pushstring
, caption
},
709 {lua_pushinteger
, len
},
712 return LuaCallProcess("process_caption", params
);
716 push_stringarray(lua_State
*L
, char **args
)
720 for (i
= 1; args
&& *args
; i
++) {
721 lua_pushinteger(L
, i
);
722 lua_pushstring(L
, *args
++);
728 LuaPushParams(lua_State
*L
, const char *params
, va_list va
)
736 lua_pushstring(L
, va_arg(va
, char *));
739 push_stringarray(L
, va_arg(va
, char **));
742 lua_pushinteger(L
, va_arg(va
, int));
750 int LuaCall(char *func
, char **argv
)
756 lua_getfield(L
, LUA_GLOBALSINDEX
, func
);
757 for (argc
= 0; *argv
; argv
++, argc
++)
759 lua_pushstring(L
, *argv
);
761 if (lua_pcall(L
, argc
, 0, 0) == LUA_ERRRUN
)
763 if(lua_isstring(L
, -1))
773 LuaDispatch(void *handler
, const char *params
, va_list va
)
775 const char *func
= handler
;
778 /*FIXME:Really need this?*/
781 lua_getfield(L
, LUA_GLOBALSINDEX
, func
);
782 if (lua_isnil(L
, -1))
784 argc
= LuaPushParams(L
, params
, va
);
786 if (lua_pcall(L
, argc
, 0, 0) == LUA_ERRRUN
&& lua_isstring(L
, -1))
794 int LuaForeWindowChanged(void)
796 struct fn_def params
[] = {
797 {push_display
, &display
},
798 {push_window
, display
? &D_fore
: &fore
},
803 return LuaCallProcess("fore_changed", params
);
808 LuaCommandExecuted(const char *command, const char **args, int argc)
812 struct fn_def params[] = {
813 {lua_pushstring, command},
814 {push_stringarray, args},
817 return LuaCallProcess("command_executed", params);
820 #define SEVNAME_MAX 30
822 LuaRegEvent(lua_State
*L
)
824 /* signature: hook(obj, event, handler, priv);
825 * or: hook(event, handler, priv)
826 * returns: A ticket for later unregister. */
827 int idx
= 1, argc
= lua_gettop(L
);
828 int priv
= 31; /* Default privilege */
831 const char *objname
= "global";
833 static char evbuf
[SEVNAME_MAX
];
834 const char *event
, *handler
;
836 struct script_event
*sev
;
838 /* Identify the object, if specified */
839 if (luaL_getmetafield(L
, 1, "_objname"))
841 objname
= luaL_checkstring(L
, -1);
843 if (!strcmp("screen", objname
))
846 obj
= *(char **)lua_touserdata(L
, 1);
850 event
= luaL_checkstring(L
, idx
++);
851 snprintf(evbuf
, SEVNAME_MAX
, "%s_%s", objname
, event
);
852 handler
= luaL_checkstring(L
, idx
++);
854 priv
= luaL_checkinteger(L
, idx
);
856 sev
= object_get_event(obj
, evbuf
);
860 l
= (struct listener
*)malloc(sizeof(struct listener
));
862 return luaL_error(L
, "Out of memory");
863 l
->handler
= (void *)handler
;
865 l
->dispatcher
= LuaDispatch
;
866 if (register_listener(sev
, l
))
869 return luaL_error(L
, "Handler %s has already been registered", handler
);
871 /*Return the handler for un-register*/
872 lua_pushlightuserdata(L
, l
);
875 return luaL_error(L
, "Invalid event specified: %s for object %s", event
, objname
);
881 LuaUnRegEvent(lua_State
*L
)
883 /* signature: release([obj], ticket, handler)
884 * returns: true of success, false otherwise */
889 /* If the param is not as expected */
890 if (!lua_islightuserdata(L
, idx
))
892 /* Then it should be the userdata to be ignore, but if not ... */
893 if (!lua_isuserdata(L
, idx
))
894 luaL_checktype(L
, idx
, LUA_TLIGHTUSERDATA
);
896 luaL_checktype(L
, idx
, LUA_TLIGHTUSERDATA
);
899 l
= (struct listener
*)lua_touserdata(L
, idx
++);
900 handler
= luaL_checkstring(L
, idx
++);
902 /*Validate the listener structure*/
903 if (!l
|| !l
->handler
904 || strncmp((char *)handler
, (char *)l
->handler
, SEVNAME_MAX
))
907 lua_pushboolean(L
,0);
911 unregister_listener(l
);
912 lua_pushboolean(L
, 1);
920 struct ScriptFuncs LuaFuncs
=
922 LuaForeWindowChanged
,
926 struct binding lua_binding
=