1 /* Lua scripting support
3 * Copyright (c) 2008 Sadrul Habib Chowdhury (sadrul@users.sf.net)
5 * Sadrul Habib Chowdhury (sadrul@users.sf.net)
6 * Rui Guo (firemeteor.guo@gmail.com)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3, or (at your option)
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program (see the file COPYING); if not, write to the
20 * Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 ****************************************************************
25 #include <sys/types.h>
42 static int StackDump(lua_State
*L
, const char *message
)
44 FILE *f
= fopen("/tmp/debug/stack", "ab");
45 int i
= lua_gettop(L
);
47 fprintf(f
, "%s", message
);
50 int t
= lua_type(L
, i
);
54 fprintf(f
, "String: %s\n", lua_tostring(L
, i
));
58 fprintf(f
, "Boolean: %s\n", lua_toboolean(L
, i
) ? "true" : "false");
62 fprintf(f
, "Number: %g\n", lua_tonumber(L
, i
));
66 fprintf(f
, "Type: %s\n", lua_typename(L
, t
));
76 extern struct win
*windows
, *fore
;
77 extern struct display
*displays
, *display
;
78 extern struct LayFuncs WinLf
;
79 extern struct layer
*flayer
;
80 extern void AppendWinMsgRend(const char *str
, const char *color
);
81 extern void FocusCanvas(struct canvas
*cv
);
83 static int LuaDispatch(void *handler
, const char *params
, va_list va
);
84 static int LuaRegEvent(lua_State
*L
);
85 static void LuaShowErr(lua_State
*L
);
86 struct binding lua_binding
;
90 LUA_HANDLER_TYPE_N
= 1,
104 typedef struct lua_handler
*lua_handler
;
105 void LuaPushHandler(lua_State
*L
, lua_handler lh
);
106 void LuaHUnRef(lua_State
*L
, int key
);
107 lua_handler
LuaCheckHandler(lua_State
*L
, int idx
, int ref
);
110 LuaAllocHandler(const char *name
, int ref
)
112 struct lua_handler
*lh
;
113 lh
= (struct lua_handler
*) malloc(sizeof(struct lua_handler
));
119 lh
->type
= LUA_HANDLER_TYPE_N
;
120 lh
->u
.name
= SaveStr(name
);
124 lh
->type
= LUA_HANDLER_TYPE_F
;
125 lh
->u
.reference
= ref
;
132 LuaFreeHandler(lua_State
*L
, struct lua_handler
**lh
)
134 if ((*lh
)->type
== LUA_HANDLER_TYPE_N
)
138 LuaHUnRef(L
, (*lh
)->u
.reference
);
139 (*lh
)->u
.reference
= 0;
145 #define CHECK_TYPE(name, type) \
147 check_##name(lua_State *L, int index) \
150 luaL_checktype(L, index, LUA_TUSERDATA); \
151 var = (type **) luaL_checkudata(L, index, #name); \
153 luaL_typerror(L, index, #name); \
157 #define PUSH_TYPE(name, type) \
159 push_##name(lua_State *L, type **t) \
166 r = (type **)lua_newuserdata(L, sizeof(type *)); \
168 luaL_getmetatable(L, #name); \
169 lua_setmetatable(L,-2); \
173 /* Much of the following template comes from:
174 * http://lua-users.org/wiki/BindingWithMembersAndMethods
177 static int get_int (lua_State
*L
, void *v
)
179 lua_pushinteger (L
, *(int*)v
);
183 static int set_int (lua_State
*L
, void *v
)
185 *(int*)v
= luaL_checkint(L
, 3);
189 static int get_number (lua_State
*L
, void *v
)
191 lua_pushnumber(L
, *(lua_Number
*)v
);
195 static int set_number (lua_State
*L
, void *v
)
197 *(lua_Number
*)v
= luaL_checknumber(L
, 3);
201 static int get_string (lua_State
*L
, void *v
)
203 lua_pushstring(L
, (char*)v
);
207 static int set_string (lua_State
*L
, void *v
)
209 *(const char**)v
= luaL_checkstring(L
, 3);
213 typedef int (*Xet_func
) (lua_State
*L
, void *v
);
215 /* member info for get and set handlers */
218 const char *name
; /* member name */
219 Xet_func func
; /* get or set function for type of member */
220 size_t offset
; /* offset of member within the struct */
221 int (*absolute
)(lua_State
*);
224 static void Xet_add (lua_State
*L
, const struct Xet_reg
*l
)
230 lua_pushstring(L
, l
->name
);
231 lua_pushlightuserdata(L
, (void*)l
);
236 static int Xet_call (lua_State
*L
)
238 /* for get: stack has userdata, index, lightuserdata */
239 /* for set: stack has userdata, index, value, lightuserdata */
240 const struct Xet_reg
*m
= (const struct Xet_reg
*)lua_touserdata(L
, -1); /* member info */
241 lua_pop(L
, 1); /* drop lightuserdata */
242 luaL_checktype(L
, 1, LUA_TUSERDATA
);
244 return m
->absolute(L
);
245 return m
->func(L
, *(char**)lua_touserdata(L
, 1) + m
->offset
);
248 static int index_handler (lua_State
*L
)
250 /* stack has userdata, index */
251 lua_pushvalue(L
, 2); /* dup index */
252 lua_rawget(L
, lua_upvalueindex(1)); /* lookup member by name */
253 if (!lua_islightuserdata(L
, -1))
255 lua_pop(L
, 1); /* drop value */
256 lua_pushvalue(L
, 2); /* dup index */
257 lua_gettable(L
, lua_upvalueindex(2)); /* else try methods */
258 if (lua_isnil(L
, -1)) /* invalid member */
259 luaL_error(L
, "cannot get member '%s'", lua_tostring(L
, 2));
262 return Xet_call(L
); /* call get function */
265 static int newindex_handler (lua_State
*L
)
267 /* stack has userdata, index, value */
268 lua_pushvalue(L
, 2); /* dup index */
269 lua_rawget(L
, lua_upvalueindex(1)); /* lookup member by name */
270 if (!lua_islightuserdata(L
, -1)) /* invalid member */
271 luaL_error(L
, "cannot set member '%s'", lua_tostring(L
, 2));
272 return Xet_call(L
); /* call set function */
276 struct_register(lua_State
*L
, const char *name
, const luaL_reg fn_methods
[], const luaL_reg meta_methods
[],
277 const struct Xet_reg setters
[], const struct Xet_reg getters
[])
279 int metatable
, methods
;
281 /* create methods table & add it to the table of globals */
282 luaL_register(L
, name
, fn_methods
);
283 methods
= lua_gettop(L
);
285 /* create metatable & add it to the registry */
286 luaL_newmetatable(L
, name
);
287 luaL_register(L
, 0, meta_methods
); /* fill metatable */
289 /* To identify the type of object */
290 lua_pushstring(L
, "_objname");
291 lua_pushstring(L
, name
);
294 metatable
= lua_gettop(L
);
296 lua_pushliteral(L
, "__metatable");
297 lua_pushvalue(L
, methods
); /* dup methods table*/
298 lua_rawset(L
, metatable
); /* hide metatable:
299 metatable.__metatable = methods */
301 lua_pushliteral(L
, "__index");
302 lua_pushvalue(L
, metatable
); /* upvalue index 1 */
303 Xet_add(L
, getters
); /* fill metatable with getters */
304 lua_pushvalue(L
, methods
); /* upvalue index 2 */
305 lua_pushcclosure(L
, index_handler
, 2);
306 lua_rawset(L
, metatable
); /* metatable.__index = index_handler */
308 lua_pushliteral(L
, "__newindex");
309 lua_newtable(L
); /* table for members you can set */
310 Xet_add(L
, setters
); /* fill with setters */
311 lua_pushcclosure(L
, newindex_handler
, 1);
312 lua_rawset(L
, metatable
); /* metatable.__newindex = newindex_handler */
314 /* FIXME: Why do we leave an element on the stack? */
315 lua_pop(L
, 1); /* drop metatable */
316 return 1; /* return methods on the stack */
322 PUSH_TYPE(callback
, struct listener
)
325 internal_unhook(lua_State
*L
, int warn
)
327 /*Emulate check_callback() */
328 struct listener
**ppl
;
329 luaL_checktype(L
, 1, LUA_TUSERDATA
);
330 ppl
= (struct listener
**) luaL_checkudata(L
, 1, "callback");
333 luaL_typerror(L
, 1, "callback");
339 lua_pushboolean(L
, 0);
340 lua_pushstring(L
, "Callback already unhooked.");
346 LuaFreeHandler(L
, (lua_handler
*)&(*ppl
)->handler
);
347 unregister_listener(*ppl
);
350 lua_pushboolean(L
, 1);
355 callback_unhook(lua_State
*L
)
357 return internal_unhook(L
, 1);
360 static const luaL_reg callback_methods
[] = {
361 {"unhook", callback_unhook
},
366 callback_collect(lua_State
*L
)
368 return internal_unhook(L
, 0);
371 static const luaL_reg callback_metamethods
[] = {
372 {"__gc", callback_collect
},
376 static const struct Xet_reg callback_setters
[] = {
380 static const struct Xet_reg callback_getters
[] = {
389 PUSH_TYPE(window
, struct win
)
391 CHECK_TYPE(window
, struct win
)
393 static int get_window(lua_State
*L
, void *v
)
395 push_window(L
, (struct win
**)v
);
400 window_change_title(lua_State
*L
)
402 struct win
*w
= check_window(L
, 1);
404 const char *title
= luaL_checklstring(L
, 3, &len
);
405 ChangeAKA(w
, (char *)title
, len
);
410 window_change_number(lua_State
*L
)
412 struct win
*w
= check_window(L
, 1);
413 int newnum
= luaL_checkint(L
, 3);
414 ChangeWinNum(w
->w_number
, newnum
);
419 window_get_monitor_status(lua_State
*L
)
421 struct win
*w
= check_window(L
, 1);
422 int activity
= luaL_checkint(L
, 2);
425 lua_pushinteger(L
, w
->w_monitor
!= MON_OFF
);
428 lua_pushinteger(L
, w
->w_silence
== SILENCE_ON
? w
->w_silencewait
: 0);
434 window_stuff(lua_State
*L
)
437 struct layer
*oldflayer
= flayer
;
438 struct win
*w
= check_window(L
, 1);
439 const char *str
= luaL_checklstring(L
, 2, &len
);
441 flayer
= &w
->w_layer
;
443 LayProcess((char **)&str
, (int *) &len
);
449 window_activate(lua_State
*L
)
451 struct win
*w
= check_window(L
, 1);
456 static const luaL_reg window_methods
[] = {
457 {"get_monitor_status", window_get_monitor_status
},
458 {"stuff", window_stuff
},
459 {"activate", window_activate
},
460 {"hook", LuaRegEvent
},
465 window_tostring(lua_State
*L
)
468 struct win
*w
= check_window(L
, 1);
469 snprintf(str
, sizeof(str
), "{window #%d: '%s'}", w
->w_number
, w
->w_title
);
470 lua_pushstring(L
, str
);
475 window_equality(lua_State
*L
)
477 struct win
*w1
= check_window(L
, 1), *w2
= check_window(L
, 2);
478 lua_pushboolean(L
, (w1
== w2
|| (w1
&& w2
&& w1
->w_number
== w2
->w_number
)));
482 static const luaL_reg window_metamethods
[] = {
483 {"__tostring", window_tostring
},
484 {"__eq", window_equality
},
488 static const struct Xet_reg window_setters
[] = {
489 {"title", 0, 0, window_change_title
/*absolute setter*/},
490 {"number", 0, 0, window_change_number
/*absolute setter*/},
494 static const struct Xet_reg window_getters
[] = {
495 {"title", get_string
, offsetof(struct win
, w_title
) + 8},
496 {"number", get_int
, offsetof(struct win
, w_number
)},
497 {"dir", get_string
, offsetof(struct win
, w_dir
)},
498 {"tty", get_string
, offsetof(struct win
, w_tty
)},
499 {"pid", get_int
, offsetof(struct win
, w_pid
)},
500 {"group", get_window
, offsetof(struct win
, w_group
)},
501 {"bell", get_int
, offsetof(struct win
, w_bell
)},
510 PUSH_TYPE(user
, struct acluser
)
512 CHECK_TYPE(user
, struct acluser
)
515 get_user(lua_State
*L
, void *v
)
517 push_user(L
, (struct acluser
**)v
);
521 static const luaL_reg user_methods
[] = {
526 user_tostring(lua_State
*L
)
529 struct acluser
*u
= check_user(L
, 1);
530 snprintf(str
, sizeof(str
), "{user '%s'}", u
->u_name
);
531 lua_pushstring(L
, str
);
535 static const luaL_reg user_metamethods
[] = {
536 {"__tostring", user_tostring
},
540 static const struct Xet_reg user_setters
[] = {
544 static const struct Xet_reg user_getters
[] = {
545 {"name", get_string
, offsetof(struct acluser
, u_name
)},
546 {"password", get_string
, offsetof(struct acluser
, u_password
)},
552 static int get_display(lua_State
*L
, void *v
);
555 PUSH_TYPE(canvas
, struct canvas
)
557 CHECK_TYPE(canvas
, struct canvas
)
560 get_canvas(lua_State
*L
, void *v
)
562 push_canvas(L
, (struct canvas
**)v
);
567 canvas_select(lua_State
*L
)
569 struct canvas
*c
= check_canvas(L
, 1);
570 if (!display
|| D_forecv
== c
)
572 /* FIXME: why do we need this? */
573 SetCanvasWindow(c
, Layer2Window(c
->c_layer
));
580 canvas_split(lua_State
*L
)
582 struct canvas
*c
= check_canvas(L
, 1);
583 int hori
= lua_toboolean(L
, 2);
585 AddCanvas(SLICE_HORI
);
587 AddCanvas(SLICE_VERT
);
593 canvas_showwin(lua_State
*L
)
595 struct canvas
*c
= check_canvas(L
, 1);
597 SetCanvasWindow(c
, 0);
600 struct win
*w
= check_window(L
, 2);
601 SetCanvasWindow(c
, w
);
606 static const luaL_reg canvas_methods
[] = {
607 {"select", canvas_select
},
608 {"split", canvas_split
},
609 {"showwin", canvas_showwin
},
613 static const luaL_reg canvas_metamethods
[] = {
617 extern struct mchar mchar_so
;
619 canvas_update_caption(lua_State
*L
)
621 struct canvas
*cv
= check_canvas(L
, 1);
623 const char *caption
= luaL_checklstring(L
, 3, &len
);
625 padlen
= cv
->c_xe
- cv
->c_xs
+
626 (display
? (cv
->c_xe
+ 1 < D_width
|| D_CLP
): 0);
627 struct win
*w
= Layer2Window(cv
->c_layer
);
628 char * buf
= MakeWinMsgEv((char *)caption
, w
, '%', padlen
, &cv
->c_captev
, 0);
629 if (cv
->c_captev
.timeout
.tv_sec
)
630 evenq(&cv
->c_captev
);
633 GotoPos(cv
->c_xs
, cv
->c_ye
+1);
634 /*XXX:what does this mean?*/
635 SetRendition(&mchar_so
);
636 if (l
> cv
->c_xe
- cv
->c_xs
+ 1)
637 l
= cv
->c_xe
- cv
->c_xs
+ 1;
638 PutWinMsg(buf
, cv
->c_xs
, l
);
643 static const struct Xet_reg canvas_setters
[] = {
644 {"caption", 0, 0, canvas_update_caption
/*absolute setter*/},
649 canvas_get_window(lua_State
*L
)
651 struct canvas
*c
= check_canvas(L
, 1);
652 struct win
*win
= Layer2Window(c
->c_layer
);
654 push_window(L
, &win
);
660 static const struct Xet_reg canvas_getters
[] = {
661 {"next", get_canvas
, offsetof(struct canvas
, c_next
)},
662 {"xoff", get_int
, offsetof(struct canvas
, c_xoff
)},
663 {"yoff", get_int
, offsetof(struct canvas
, c_yoff
)},
664 {"xs", get_int
, offsetof(struct canvas
, c_xs
)},
665 {"ys", get_int
, offsetof(struct canvas
, c_ys
)},
666 {"xe", get_int
, offsetof(struct canvas
, c_xe
)},
667 {"ye", get_int
, offsetof(struct canvas
, c_ye
)},
668 {"window", 0, 0, canvas_get_window
},
669 {"display", get_display
, offsetof(struct canvas
, c_display
)},
677 PUSH_TYPE(layout
, struct layout
)
678 CHECK_TYPE(layout
, struct layout
)
680 static const struct Xet_reg layout_getters
[] = {
681 {"title", get_string
, offsetof(struct layout
, lay_title
)},
682 {"number", get_string
, offsetof(struct layout
, lay_title
)},
683 {"autosave", get_string
, offsetof(struct layout
, lay_autosave
)},
688 layout_change_title(lua_State
*L
)
690 struct layout
*lay
= check_layout(L
, 1);
692 const char *title
= luaL_checklstring(L
, 3, &len
);
693 free(lay
->lay_title
);
694 lay
->lay_title
= SaveStr(title
);
698 void LayoutChangeNumber(struct layout
*lay
, int newnum
);
701 layout_change_number(lua_State
*L
)
703 struct layout
*lay
= check_layout(L
, 1);
704 int newnum
= luaL_checkint(L
, 3);
705 LayoutChangeNumber(lay
, newnum
);
709 static const struct Xet_reg layout_setters
[] = {
710 {"title", 0, 0, layout_change_title
/*absolute setter*/},
711 {"number", 0, 0, layout_change_number
/*absolute setter*/},
712 {"autosave", get_string
, offsetof(struct layout
, lay_autosave
)},
717 layout_select(lua_State
*L
)
719 struct layout
*lay
= check_layout(L
, 1);
720 if (!display
) return;
721 LoadLayout(lay
, &D_canvas
);
726 static const luaL_reg layout_methods
[] = {
727 {"select", layout_select
},
732 get_layout(lua_State
*L
, void *v
)
734 push_layout(L
, (struct layout
**)v
);
742 PUSH_TYPE(display
, struct display
)
744 CHECK_TYPE(display
, struct display
)
747 get_display(lua_State
*L
, void *v
)
749 push_display(L
, (struct display
**)v
);
754 display_get_canvases(lua_State
*L
)
760 d
= check_display(L
, 1);
762 for (iter
= d
->d_cvlist
, count
= 0; iter
; iter
= iter
->c_next
, count
++) {
763 lua_pushinteger(L
, count
);
764 push_canvas(L
, &iter
);
772 display_top_canvas(lua_State
*L
)
775 d
= check_display(L
, 1);
776 push_canvas(L
, &d
->d_cvlist
);
782 display_bot_canvas(lua_State
*L
)
786 d
= check_display(L
, 1);
788 for (c
= d
->d_cvlist
; c
->c_next
; c
= c
->c_next
)
795 static const luaL_reg display_methods
[] = {
796 {"get_canvases", display_get_canvases
},
797 {"top_canvas", display_top_canvas
},
798 {"bottom_canvas", display_bot_canvas
},
803 display_tostring(lua_State
*L
)
806 struct display
*d
= check_display(L
, 1);
807 snprintf(str
, sizeof(str
), "{display: tty = '%s', term = '%s'}", d
->d_usertty
, d
->d_termname
);
808 lua_pushstring(L
, str
);
812 static const luaL_reg display_metamethods
[] = {
813 {"__tostring", display_tostring
},
818 display_new_idle_timeout(lua_State
*L
)
820 struct display
*disp
= check_display(L
, 1);
821 int timeout
= luaL_checkinteger(L
, 3) * 1000;
822 struct event
*ev
= &disp
->d_idleev
;
825 SetTimeout(ev
, timeout
);
835 static const struct Xet_reg display_setters
[] = {
836 {"idle_timeout", 0, 0, display_new_idle_timeout
/*absolute setter*/},
840 static const struct Xet_reg display_getters
[] = {
841 {"tty", get_string
, offsetof(struct display
, d_usertty
)},
842 {"term", get_string
, offsetof(struct display
, d_termname
)},
843 {"fore", get_window
, offsetof(struct display
, d_fore
)},
844 {"other", get_window
, offsetof(struct display
, d_other
)},
845 {"width", get_int
, offsetof(struct display
, d_width
)},
846 {"height", get_int
, offsetof(struct display
, d_height
)},
847 {"user", get_user
, offsetof(struct display
, d_user
)},
848 {"layout", get_layout
, offsetof(struct display
, d_layout
)},
857 screen_get_windows(lua_State
*L
)
863 for (iter
= windows
, count
= 0; iter
; iter
= iter
->w_next
, count
++) {
864 lua_pushinteger(L
, iter
->w_number
);
865 push_window(L
, &iter
);
873 screen_get_displays(lua_State
*L
)
875 struct display
*iter
;
879 for (iter
= displays
, count
= 0; iter
; iter
= iter
->d_next
, count
++) {
880 lua_pushinteger(L
, count
);
881 push_display(L
, &iter
);
888 extern struct layout
*layouts
;
890 screen_get_layouts(lua_State
*L
)
896 for (iter
= layouts
, count
= 0; iter
; iter
= iter
->lay_next
, count
++) {
897 lua_pushinteger(L
, iter
->lay_number
);
898 push_layout(L
, &iter
);
906 screen_get_display(lua_State
*L
)
908 push_display(L
, &display
);
913 screen_exec_command(lua_State
*L
)
918 command
= luaL_checklstring(L
, 1, &len
);
920 RcLine((char *)command
, len
);
926 screen_append_msg(lua_State
*L
)
928 const char *msg
, *color
;
930 msg
= luaL_checklstring(L
, 1, &len
);
934 color
= luaL_checklstring(L
, 2, &len
);
935 AppendWinMsgRend(msg
, color
);
946 script_input_fn(char *buf
, int len
, char *priv
)
948 struct sinput_data
*sidata
= (struct sinput_data
*)priv
;
949 lua_handler lh
= sidata
->lh
;
950 lua_State
*L
= sidata
->L
;
952 LuaPushHandler(L
, lh
);
953 lua_pushstring(L
, buf
);
954 if (lua_pcall(L
, 1, 0, 0) == LUA_ERRRUN
)
956 if(lua_isstring(L
, -1))
962 LuaFreeHandler(L
, &lh
);
966 screen_input(lua_State
*L
)
970 const char * prompt
= NULL
, *prefill
= NULL
;
972 struct sinput_data
*sidata
;
974 prompt
= luaL_checkstring(L
, 1);
975 lh
= LuaCheckHandler(L
, 2, 1);
977 luaL_error(L
, "Out of Memory");
979 if (lua_gettop(L
) >= 3)
980 prefill
= luaL_checkstring(L
, 3);
983 sidata
= (struct sinput_data
*)malloc(sizeof(struct sinput_data
));
986 LuaFreeHandler(L
, &lh
);
987 luaL_error(L
, "Out of Memory");
992 Input((char *)prompt
, 100, INP_COOKED
, script_input_fn
, (char *)sidata
, 0);
996 for (; *prefill
; prefill
++)
998 if ((*(unsigned char *)prefill
& 0x7f) < 0x20 || *prefill
== 0x7f)
1000 ss
= (char *)prefill
;
1002 LayProcess(&ss
, &n
);
1008 sev_schedule_fn(struct event
*ev
, char *data
)
1010 struct sinput_data
*si
= (struct sinput_data
*)data
;
1011 lua_handler lh
= si
->lh
;
1012 lua_State
*L
= si
->L
;
1017 LuaPushHandler(L
, lh
);
1018 if (lua_pcall(L
, 1, 0, 0) == LUA_ERRRUN
)
1020 if(lua_isstring(L
, -1))
1025 LuaFreeHandler(L
, &lh
);
1029 screen_schedule(lua_State
*L
)
1031 int timeout
= luaL_checkinteger(L
, 1);
1032 lua_handler lh
= LuaCheckHandler(L
, 2, 1);
1033 struct sinput_data
*si
;
1038 ev
= (struct event
*) calloc(1, sizeof(struct event
));
1041 LuaFreeHandler(L
, &lh
);
1042 luaL_error(L
, "Out of Memory");
1044 si
= (struct sinput_data
*) malloc(sizeof(struct sinput_data
));
1047 LuaFreeHandler(L
, &lh
);
1049 luaL_error(L
, "Out of Memory");
1054 ev
->type
= EV_TIMEOUT
;
1055 ev
->data
= (char *)si
;
1056 ev
->handler
= sev_schedule_fn
;
1058 SetTimeout(ev
, timeout
* 1000);
1062 static const luaL_reg screen_methods
[] = {
1063 {"windows", screen_get_windows
},
1064 {"displays", screen_get_displays
},
1065 {"layouts", screen_get_layouts
},
1066 {"display", screen_get_display
},
1067 {"command", screen_exec_command
},
1068 {"append_msg", screen_append_msg
},
1069 {"hook", LuaRegEvent
},
1070 {"input", screen_input
},
1071 {"schedule", screen_schedule
},
1075 static const luaL_reg screen_metamethods
[] = {
1079 static const struct Xet_reg screen_setters
[] = {
1083 static const struct Xet_reg screen_getters
[] = {
1089 /** Public functions {{{ */
1091 /* FIXME: This code only tracks one unhook ticket for a lua func.
1092 * So it does not work if one func is registered more than one times. */
1094 prepare_weak_table(lua_State
*L
, const char *name
, const char *mode
)
1096 luaL_getmetatable(L
, "screen");
1098 lua_pushstring(L
, name
);
1101 /* prepare a metatable to indicate weak table */
1103 lua_pushstring(L
, "__mode");
1104 lua_pushstring(L
, mode
);
1106 /* Mark weak table */
1107 lua_setmetatable(L
, -2);
1112 static lua_State
*L
;
1115 L
= luaL_newstate();
1119 #define REGISTER(X) struct_register(L, #X , X ## _methods, X##_metamethods, X##_setters, X##_getters)
1128 /* To store handler funcs */
1130 /*_two kinds of information are mantained:
1132 * funcreg[key]->func
1133 * The 'func' part of the tables are weak. That means the registered funcs
1134 * will be collected once the func is no longer available (e.g. overwritten
1135 * by a new instance of the script)
1137 * To make this process happens faster, GC is forced at the end of each
1139 * TODO: What if some events are triggered within the sourcing
1142 prepare_weak_table(L
, "_funcreg", "v");
1144 /* funcunhook[func]->listener
1145 * The listener is the unhook ticket of the hook. which should be collected
1146 * once the func is collected. The gc metamethod will be triggered
1148 prepare_weak_table(L
, "_funcunhook", "k");
1153 /* An error message on top of the stack. */
1155 LuaShowErr(lua_State
*L
)
1157 struct display
*d
= display
;
1159 const char *message
= luaL_checklstring(L
, -1, &len
);
1160 LMsg(0, "%s", message
? message
: "Unknown error");
1167 void (*push_fn
)(lua_State
*, void*);
1172 LuaCallProcess(const char *name
, struct fn_def defs
[])
1176 lua_getfield(L
, LUA_GLOBALSINDEX
, name
);
1177 if (lua_isnil(L
, -1))
1182 for (argc
= 0; defs
[argc
].push_fn
; argc
++)
1183 defs
[argc
].push_fn(L
, defs
[argc
].value
);
1184 if (lua_pcall(L
, argc
, 0, 0) == LUA_ERRRUN
&& lua_isstring(L
, -1))
1192 /* FIXME: Think about this: will it affect the registered handlers?*/
1193 int LuaSource(const char *file
, int async
)
1198 if (stat(file
, &st
) == -1)
1199 Msg(errno
, "Error loading lua script file '%s'", file
);
1202 int len
= strlen(file
);
1203 if (len
< 4 || strncmp(file
+ len
- 4, ".lua", 4) != 0)
1205 if (luaL_dofile(L
, file
) && lua_isstring(L
, -1))
1212 /* It seems that I need two GC passes to really collect the unhook
1213 * ticket, after changing the reference to ticket in the
1214 * 'funcunhook' table from weak to strong. This should not be
1215 * harmful, but how can I make sure that two passes is enough?
1218 * This seems reasonable, since the first pass will collect the func
1219 * itself and make the ticket garbage, and the second pass will
1220 * collect the ticket itself.
1222 * TODO: check this out. maybe ask some lua gurus. */
1223 lua_gc(L
, LUA_GCCOLLECT
, 0);
1224 lua_gc(L
, LUA_GCCOLLECT
, 0);
1241 push_stringarray(lua_State
*L
, char **args
)
1245 for (i
= 1; args
&& *args
; i
++) {
1246 lua_pushinteger(L
, i
);
1247 lua_pushstring(L
, *args
++);
1248 lua_settable(L
, -3);
1253 LuaPushParams(lua_State
*L
, const char *params
, va_list va
)
1261 lua_pushstring(L
, va_arg(va
, char *));
1264 push_stringarray(L
, va_arg(va
, char **));
1267 lua_pushinteger(L
, va_arg(va
, int));
1271 struct display
*d
= va_arg(va
, struct display
*);
1272 push_display(L
, &d
);
1277 struct win
*w
= va_arg(va
, struct win
*);
1283 struct canvas
*c
= va_arg(va
, struct canvas
*);
1294 int LuaCall(const char *func
, const char **argv
)
1300 StackDump(L
, "Before LuaCall\n");
1301 lua_getfield(L
, LUA_GLOBALSINDEX
, func
);
1302 if (lua_isnil(L
, -1))
1305 lua_pushstring(L
, "Could not find the script function\n");
1309 for (argc
= 0; *argv
; argv
++, argc
++)
1311 lua_pushstring(L
, *argv
);
1313 if (lua_pcall(L
, argc
, 0, 0) == LUA_ERRRUN
)
1315 if(lua_isstring(L
, -1))
1317 StackDump(L
, "After LuaCall\n");
1325 /*** Lua callback handler management {{{ */
1328 LuaPushHTable(lua_State
*L
, int screen
, const char * t
)
1330 lua_pushstring(L
, t
);
1331 lua_rawget(L
, screen
);
1332 /* FIXME: Do we need to balance the stack here? */
1333 if (lua_isnil(L
, -1))
1334 luaL_error(L
, "Fatal! Fail to get function registeration table!");
1335 return lua_gettop(L
);
1339 LuaPushHandler(lua_State
*L
, lua_handler lh
)
1342 if (lh
->type
== LUA_HANDLER_TYPE_F
)
1344 luaL_getmetatable(L
, "screen");
1345 funcreg
= LuaPushHTable(L
, lua_gettop(L
), "_funcreg");
1346 lua_rawgeti(L
, funcreg
, lh
->u
.reference
);
1352 lua_getfield(L
, LUA_GLOBALSINDEX
, lh
->u
.name
);
1357 LuaFuncKey(lua_State
*L
, int idx
, int ref
)
1359 int key
, funcreg
, sc
;
1360 luaL_getmetatable(L
, "screen");
1362 funcreg
= LuaPushHTable(L
, sc
, "_funcreg");
1364 lua_pushvalue(L
, idx
);
1365 key
= luaL_ref(L
, funcreg
);
1371 LuaHUnRef(lua_State
*L
, int key
)
1374 luaL_getmetatable(L
, "screen");
1376 funcreg
= LuaPushHTable(L
, sc
, "_funcreg");
1377 luaL_unref(L
, funcreg
, key
);
1381 LuaCheckHandler(lua_State
*L
, int idx
, int ref
)
1385 idx
= lua_gettop(L
) + 1 - idx
;
1387 if (lua_isstring(L
, idx
))
1389 const char * handler
;
1390 /* registered with func name.*/
1391 handler
= luaL_checkstring(L
, idx
);
1392 lua_getfield(L
, LUA_GLOBALSINDEX
, handler
);
1393 if (!lua_isfunction(L
, -1))
1394 luaL_error(L
, "The specified handler %s in param #%d is not a function", handler
, idx
);
1396 return LuaAllocHandler(handler
, 0);
1398 else if (!lua_isfunction(L
, idx
))
1399 luaL_error(L
, "Handler should be a function or the name of function");
1401 key
= LuaFuncKey(L
, idx
, ref
);
1402 return LuaAllocHandler(NULL
, key
);
1408 LuaDispatch(void *handler
, const char *params
, va_list va
)
1410 lua_handler lh
= (lua_handler
)handler
;
1413 StackDump(L
, "before dispatch");
1415 LuaPushHandler(L
, lh
);
1416 if (lua_isnil(L
, -1))
1421 argc
= LuaPushParams(L
, params
, va
);
1423 if (lua_pcall(L
, argc
, 1, 0) == LUA_ERRRUN
&& lua_isstring(L
, -1))
1425 StackDump(L
, "After LuaDispatch\n");
1429 retvalue
= lua_tonumber(L
, -1);
1434 /*FIXME: what if a func is registered twice or more? */
1436 LuaRegAutoUnHook(lua_State
*L
, lua_handler lh
, int ticket
)
1439 luaL_getmetatable(L
, "screen");
1441 funcunhook
= LuaPushHTable(L
, sc
, "_funcunhook");
1442 LuaPushHandler(L
, lh
);
1443 lua_pushvalue(L
, ticket
);
1444 lua_rawset(L
, funcunhook
);
1448 #define SEVNAME_MAX 30
1450 LuaRegEvent(lua_State
*L
)
1452 /* signature: hook(obj, event, handler, priv);
1453 * or: hook(event, handler, priv)
1454 * returns: A ticket for later unregister. */
1455 int idx
= 1, argc
= lua_gettop(L
);
1456 unsigned int priv
= 31; /* Default privilege */
1460 const char *objname
= "global";
1462 static char evbuf
[SEVNAME_MAX
];
1465 struct script_event
*sev
;
1467 StackDump(L
, "Before RegEvent\n");
1469 /* Identify the object, if specified */
1470 if (luaL_getmetafield(L
, 1, "_objname"))
1472 objname
= luaL_checkstring(L
, -1);
1474 if (!strcmp("screen", objname
))
1477 obj
= *(char **)lua_touserdata(L
, 1);
1481 event
= luaL_checkstring(L
, idx
++);
1482 snprintf(evbuf
, SEVNAME_MAX
, "%s_%s", objname
, event
);
1484 /* Check and get the handler */
1485 lh
= LuaCheckHandler(L
, idx
++, 1);
1487 luaL_error(L
, "Out of memory");
1489 StackDump(L
, "In RegEvent\n");
1492 priv
= luaL_checkinteger(L
, idx
);
1494 sev
= object_get_event(obj
, evbuf
);
1499 l
= (struct listener
*)malloc(sizeof(struct listener
));
1501 return luaL_error(L
, "Out of memory");
1503 l
->bd
= &lua_binding
;
1504 l
->handler
= (void *)lh
;
1505 register_listener(sev
, l
);
1506 /* Return the ticket for un-register */
1507 push_callback(L
, &l
);
1508 ticket
= lua_gettop(L
);
1510 LuaRegAutoUnHook(L
, lh
, ticket
);
1513 return luaL_error(L
, "Invalid event specified: %s for object %s", event
, objname
);
1515 StackDump(L
, "After RegEvent\n");
1521 struct binding lua_binding
=