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) \
149 struct broker **broker; \
151 luaL_checktype(L, index, LUA_TUSERDATA); \
152 broker = (struct broker **) luaL_checkudata(L, index, #name); \
154 var = (type *)get_broker_obj(broker); \
156 if (!broker || !*broker) \
157 luaL_typerror(L, index, #name); \
161 #define PUSH_TYPE(name, type) \
163 push_##name(lua_State *L, type **t) \
170 r = (struct broker **)lua_newuserdata(L, sizeof(struct broker *)); \
171 *r = get_obj_broker(*t); \
172 luaL_getmetatable(L, #name); \
173 lua_setmetatable(L,-2); \
177 /* Much of the following template comes from:
178 * http://lua-users.org/wiki/BindingWithMembersAndMethods
181 static int get_int (lua_State
*L
, void *v
)
183 lua_pushinteger (L
, *(int*)v
);
187 static int set_int (lua_State
*L
, void *v
)
189 *(int*)v
= luaL_checkint(L
, 3);
193 static int get_number (lua_State
*L
, void *v
)
195 lua_pushnumber(L
, *(lua_Number
*)v
);
199 static int set_number (lua_State
*L
, void *v
)
201 *(lua_Number
*)v
= luaL_checknumber(L
, 3);
205 static int get_string (lua_State
*L
, void *v
)
207 lua_pushstring(L
, (char*)v
);
211 static int set_string (lua_State
*L
, void *v
)
213 *(const char**)v
= luaL_checkstring(L
, 3);
217 typedef int (*Xet_func
) (lua_State
*L
, void *v
);
219 /* member info for get and set handlers */
222 const char *name
; /* member name */
223 Xet_func func
; /* get or set function for type of member */
224 size_t offset
; /* offset of member within the struct */
225 int (*absolute
)(lua_State
*);
228 static void Xet_add (lua_State
*L
, const struct Xet_reg
*l
)
234 lua_pushstring(L
, l
->name
);
235 lua_pushlightuserdata(L
, (void*)l
);
240 static int Xet_call (lua_State
*L
)
242 /* for get: stack has userdata, index, lightuserdata */
243 /* for set: stack has userdata, index, value, lightuserdata */
244 const struct Xet_reg
*m
= (const struct Xet_reg
*)lua_touserdata(L
, -1); /* member info */
246 lua_pop(L
, 1); /* drop lightuserdata */
247 luaL_checktype(L
, 1, LUA_TUSERDATA
);
249 return m
->absolute(L
);
250 obj
= get_broker_obj((struct broker
**)lua_touserdata(L
, 1));
251 return m
->func(L
, (char *)obj
+ m
->offset
);
254 static int index_handler (lua_State
*L
)
256 /* stack has userdata, index */
257 lua_pushvalue(L
, 2); /* dup index */
258 lua_rawget(L
, lua_upvalueindex(1)); /* lookup member by name */
259 if (!lua_islightuserdata(L
, -1))
261 lua_pop(L
, 1); /* drop value */
262 lua_pushvalue(L
, 2); /* dup index */
263 lua_gettable(L
, lua_upvalueindex(2)); /* else try methods */
264 if (lua_isnil(L
, -1)) /* invalid member */
265 luaL_error(L
, "cannot get member '%s'", lua_tostring(L
, 2));
268 return Xet_call(L
); /* call get function */
271 static int newindex_handler (lua_State
*L
)
273 /* stack has userdata, index, value */
274 lua_pushvalue(L
, 2); /* dup index */
275 lua_rawget(L
, lua_upvalueindex(1)); /* lookup member by name */
276 if (!lua_islightuserdata(L
, -1)) /* invalid member */
277 luaL_error(L
, "cannot set member '%s'", lua_tostring(L
, 2));
278 return Xet_call(L
); /* call set function */
282 struct_register(lua_State
*L
, const char *name
, const luaL_reg fn_methods
[], const luaL_reg meta_methods
[],
283 const struct Xet_reg setters
[], const struct Xet_reg getters
[])
285 int metatable
, methods
;
287 /* create methods table & add it to the table of globals */
288 luaL_register(L
, name
, fn_methods
);
289 methods
= lua_gettop(L
);
291 /* create metatable & add it to the registry */
292 luaL_newmetatable(L
, name
);
293 luaL_register(L
, 0, meta_methods
); /* fill metatable */
295 /* To identify the type of object */
296 lua_pushstring(L
, "_objname");
297 lua_pushstring(L
, name
);
300 metatable
= lua_gettop(L
);
302 lua_pushliteral(L
, "__metatable");
303 lua_pushvalue(L
, methods
); /* dup methods table*/
304 lua_rawset(L
, metatable
); /* hide metatable:
305 metatable.__metatable = methods */
307 lua_pushliteral(L
, "__index");
308 lua_pushvalue(L
, metatable
); /* upvalue index 1 */
309 Xet_add(L
, getters
); /* fill metatable with getters */
310 lua_pushvalue(L
, methods
); /* upvalue index 2 */
311 lua_pushcclosure(L
, index_handler
, 2);
312 lua_rawset(L
, metatable
); /* metatable.__index = index_handler */
314 lua_pushliteral(L
, "__newindex");
315 lua_newtable(L
); /* table for members you can set */
316 Xet_add(L
, setters
); /* fill with setters */
317 lua_pushcclosure(L
, newindex_handler
, 1);
318 lua_rawset(L
, metatable
); /* metatable.__newindex = newindex_handler */
320 /* FIXME: Why do we leave an element on the stack? */
321 lua_pop(L
, 1); /* drop metatable */
322 return 1; /* return methods on the stack */
329 push_callback(lua_State
*L
, struct listener
**t
)
336 r
= (struct listener
**)lua_newuserdata(L
, sizeof(struct listener
*));
338 luaL_getmetatable(L
, "callback");
339 lua_setmetatable(L
,-2);
345 internal_unhook(lua_State
*L
, int warn
)
347 /*Emulate check_callback() */
348 struct listener
**ppl
;
349 luaL_checktype(L
, 1, LUA_TUSERDATA
);
350 ppl
= (struct listener
**) luaL_checkudata(L
, 1, "callback");
353 luaL_typerror(L
, 1, "callback");
359 lua_pushboolean(L
, 0);
360 lua_pushstring(L
, "Callback already unhooked.");
366 LuaFreeHandler(L
, (lua_handler
*)&(*ppl
)->handler
);
367 unregister_listener(*ppl
);
370 lua_pushboolean(L
, 1);
375 callback_unhook(lua_State
*L
)
377 return internal_unhook(L
, 1);
380 static const luaL_reg callback_methods
[] = {
381 {"unhook", callback_unhook
},
386 callback_collect(lua_State
*L
)
388 return internal_unhook(L
, 0);
391 static const luaL_reg callback_metamethods
[] = {
392 {"__gc", callback_collect
},
396 static const struct Xet_reg callback_setters
[] = {
400 static const struct Xet_reg callback_getters
[] = {
408 screen_object_collect(lua_State
*L
)
410 struct broker
**pbroker
;
411 luaL_checktype(L
, 1, LUA_TUSERDATA
);
412 pbroker
= (struct broker
**)lua_touserdata(L
, 1);
413 broker_unref(pbroker
);
419 PUSH_TYPE(window
, struct win
)
421 CHECK_TYPE(window
, struct win
)
423 static int get_window(lua_State
*L
, void *v
)
425 push_window(L
, (struct win
**)v
);
430 window_change_title(lua_State
*L
)
432 struct win
*w
= check_window(L
, 1);
434 const char *title
= luaL_checklstring(L
, 3, &len
);
435 ChangeAKA(w
, (char *)title
, len
);
440 window_change_number(lua_State
*L
)
442 struct win
*w
= check_window(L
, 1);
443 int newnum
= luaL_checkint(L
, 3);
444 ChangeWinNum(w
->w_number
, newnum
);
449 window_get_monitor_status(lua_State
*L
)
451 struct win
*w
= check_window(L
, 1);
452 int activity
= luaL_checkint(L
, 2);
455 lua_pushinteger(L
, w
->w_monitor
!= MON_OFF
);
458 lua_pushinteger(L
, w
->w_silence
== SILENCE_ON
? w
->w_silencewait
: 0);
464 window_stuff(lua_State
*L
)
467 struct layer
*oldflayer
= flayer
;
468 struct win
*w
= check_window(L
, 1);
469 const char *str
= luaL_checklstring(L
, 2, &len
);
471 flayer
= &w
->w_layer
;
473 LayProcess((char **)&str
, (int *) &len
);
479 window_activate(lua_State
*L
)
481 struct win
*w
= check_window(L
, 1);
486 static const luaL_reg window_methods
[] = {
487 {"get_monitor_status", window_get_monitor_status
},
488 {"stuff", window_stuff
},
489 {"activate", window_activate
},
490 {"hook", LuaRegEvent
},
495 window_tostring(lua_State
*L
)
498 struct win
*w
= check_window(L
, 1);
499 snprintf(str
, sizeof(str
), "{window #%d: '%s'}", w
->w_number
, w
->w_title
);
500 lua_pushstring(L
, str
);
505 window_equality(lua_State
*L
)
507 struct win
*w1
= check_window(L
, 1), *w2
= check_window(L
, 2);
508 lua_pushboolean(L
, (w1
== w2
|| (w1
&& w2
&& w1
->w_number
== w2
->w_number
)));
512 static const luaL_reg window_metamethods
[] = {
513 {"__tostring", window_tostring
},
514 {"__eq", window_equality
},
515 {"__gc", screen_object_collect
},
519 static const struct Xet_reg window_setters
[] = {
520 {"title", 0, 0, window_change_title
/*absolute setter*/},
521 {"number", 0, 0, window_change_number
/*absolute setter*/},
525 static const struct Xet_reg window_getters
[] = {
526 {"title", get_string
, offsetof(struct win
, w_title
) + 8},
527 {"number", get_int
, offsetof(struct win
, w_number
)},
528 {"dir", get_string
, offsetof(struct win
, w_dir
)},
529 {"tty", get_string
, offsetof(struct win
, w_tty
)},
530 {"pid", get_int
, offsetof(struct win
, w_pid
)},
531 {"group", get_window
, offsetof(struct win
, w_group
)},
532 {"bell", get_int
, offsetof(struct win
, w_bell
)},
541 PUSH_TYPE(user
, struct acluser
)
543 CHECK_TYPE(user
, struct acluser
)
546 get_user(lua_State
*L
, void *v
)
548 push_user(L
, (struct acluser
**)v
);
552 static const luaL_reg user_methods
[] = {
557 user_tostring(lua_State
*L
)
560 struct acluser
*u
= check_user(L
, 1);
561 snprintf(str
, sizeof(str
), "{user '%s'}", u
->u_name
);
562 lua_pushstring(L
, str
);
566 static const luaL_reg user_metamethods
[] = {
567 {"__tostring", user_tostring
},
568 {"__gc", screen_object_collect
},
572 static const struct Xet_reg user_setters
[] = {
576 static const struct Xet_reg user_getters
[] = {
577 {"name", get_string
, offsetof(struct acluser
, u_name
)},
578 {"password", get_string
, offsetof(struct acluser
, u_password
)},
584 static int get_display(lua_State
*L
, void *v
);
587 PUSH_TYPE(canvas
, struct canvas
)
589 CHECK_TYPE(canvas
, struct canvas
)
592 get_canvas(lua_State
*L
, void *v
)
594 push_canvas(L
, (struct canvas
**)v
);
599 canvas_select(lua_State
*L
)
601 struct canvas
*c
= check_canvas(L
, 1);
602 if (!display
|| D_forecv
== c
)
604 /* FIXME: why do we need this? */
605 SetCanvasWindow(c
, Layer2Window(c
->c_layer
));
612 canvas_split(lua_State
*L
)
614 struct canvas
*c
= check_canvas(L
, 1);
615 int hori
= lua_toboolean(L
, 2);
617 AddCanvas(SLICE_HORI
);
619 AddCanvas(SLICE_VERT
);
625 canvas_showwin(lua_State
*L
)
627 struct canvas
*c
= check_canvas(L
, 1);
629 SetCanvasWindow(c
, 0);
632 struct win
*w
= check_window(L
, 2);
633 SetCanvasWindow(c
, w
);
638 static const luaL_reg canvas_methods
[] = {
639 {"select", canvas_select
},
640 {"split", canvas_split
},
641 {"showwin", canvas_showwin
},
645 static const luaL_reg canvas_metamethods
[] = {
646 {"__gc", screen_object_collect
},
650 extern struct mchar mchar_so
;
652 canvas_update_caption(lua_State
*L
)
654 struct canvas
*cv
= check_canvas(L
, 1);
656 const char *caption
= luaL_checklstring(L
, 3, &len
);
658 padlen
= cv
->c_xe
- cv
->c_xs
+
659 (display
? (cv
->c_xe
+ 1 < D_width
|| D_CLP
): 0);
660 struct win
*w
= Layer2Window(cv
->c_layer
);
661 char * buf
= MakeWinMsgEv((char *)caption
, w
, '%', padlen
, &cv
->c_captev
, 0);
662 if (cv
->c_captev
.timeout
.tv_sec
)
663 evenq(&cv
->c_captev
);
666 GotoPos(cv
->c_xs
, cv
->c_ye
+1);
667 /*XXX:what does this mean?*/
668 SetRendition(&mchar_so
);
669 if (l
> cv
->c_xe
- cv
->c_xs
+ 1)
670 l
= cv
->c_xe
- cv
->c_xs
+ 1;
671 PutWinMsg(buf
, cv
->c_xs
, l
);
673 for (; l
<= cv
->c_xe
; l
++)
679 static const struct Xet_reg canvas_setters
[] = {
680 {"caption", 0, 0, canvas_update_caption
/*absolute setter*/},
685 canvas_get_window(lua_State
*L
)
687 struct canvas
*c
= check_canvas(L
, 1);
688 struct win
*win
= Layer2Window(c
->c_layer
);
690 push_window(L
, &win
);
696 static const struct Xet_reg canvas_getters
[] = {
697 {"next", get_canvas
, offsetof(struct canvas
, c_next
)},
698 {"xoff", get_int
, offsetof(struct canvas
, c_xoff
)},
699 {"yoff", get_int
, offsetof(struct canvas
, c_yoff
)},
700 {"xs", get_int
, offsetof(struct canvas
, c_xs
)},
701 {"ys", get_int
, offsetof(struct canvas
, c_ys
)},
702 {"xe", get_int
, offsetof(struct canvas
, c_xe
)},
703 {"ye", get_int
, offsetof(struct canvas
, c_ye
)},
704 {"window", 0, 0, canvas_get_window
},
705 {"display", get_display
, offsetof(struct canvas
, c_display
)},
713 PUSH_TYPE(layout
, struct layout
)
714 CHECK_TYPE(layout
, struct layout
)
716 static const struct Xet_reg layout_getters
[] = {
717 {"title", get_string
, offsetof(struct layout
, lay_title
)},
718 {"number", get_string
, offsetof(struct layout
, lay_title
)},
719 {"autosave", get_string
, offsetof(struct layout
, lay_autosave
)},
724 layout_change_title(lua_State
*L
)
726 struct layout
*lay
= check_layout(L
, 1);
728 const char *title
= luaL_checklstring(L
, 3, &len
);
729 free(lay
->lay_title
);
730 lay
->lay_title
= SaveStr(title
);
734 void LayoutChangeNumber(struct layout
*lay
, int newnum
);
737 layout_change_number(lua_State
*L
)
739 struct layout
*lay
= check_layout(L
, 1);
740 int newnum
= luaL_checkint(L
, 3);
741 LayoutChangeNumber(lay
, newnum
);
745 static const struct Xet_reg layout_setters
[] = {
746 {"title", 0, 0, layout_change_title
/*absolute setter*/},
747 {"number", 0, 0, layout_change_number
/*absolute setter*/},
748 {"autosave", get_string
, offsetof(struct layout
, lay_autosave
)},
753 layout_select(lua_State
*L
)
755 struct layout
*lay
= check_layout(L
, 1);
756 if (!display
) return;
757 LoadLayout(lay
, &D_canvas
);
762 static const luaL_reg layout_methods
[] = {
763 {"select", layout_select
},
767 static const luaL_reg layout_metamethods
[] = {
768 {"__gc", screen_object_collect
},
773 get_layout(lua_State
*L
, void *v
)
775 push_layout(L
, (struct layout
**)v
);
783 PUSH_TYPE(display
, struct display
)
785 CHECK_TYPE(display
, struct display
)
788 get_display(lua_State
*L
, void *v
)
790 push_display(L
, (struct display
**)v
);
795 display_get_canvases(lua_State
*L
)
801 d
= check_display(L
, 1);
803 for (iter
= d
->d_cvlist
, count
= 0; iter
; iter
= iter
->c_next
, count
++) {
804 lua_pushinteger(L
, count
);
805 push_canvas(L
, &iter
);
813 display_top_canvas(lua_State
*L
)
816 d
= check_display(L
, 1);
817 push_canvas(L
, &d
->d_cvlist
);
823 display_bot_canvas(lua_State
*L
)
827 d
= check_display(L
, 1);
829 for (c
= d
->d_cvlist
; c
->c_next
; c
= c
->c_next
)
836 static const luaL_reg display_methods
[] = {
837 {"get_canvases", display_get_canvases
},
838 {"top_canvas", display_top_canvas
},
839 {"bottom_canvas", display_bot_canvas
},
844 display_tostring(lua_State
*L
)
847 struct display
*d
= check_display(L
, 1);
848 snprintf(str
, sizeof(str
), "{display: tty = '%s', term = '%s'}", d
->d_usertty
, d
->d_termname
);
849 lua_pushstring(L
, str
);
853 static const luaL_reg display_metamethods
[] = {
854 {"__tostring", display_tostring
},
855 {"__gc", screen_object_collect
},
860 display_new_idle_timeout(lua_State
*L
)
862 struct display
*disp
= check_display(L
, 1);
863 int timeout
= luaL_checkinteger(L
, 3) * 1000;
864 struct event
*ev
= &disp
->d_idleev
;
867 SetTimeout(ev
, timeout
);
877 static const struct Xet_reg display_setters
[] = {
878 {"idle_timeout", 0, 0, display_new_idle_timeout
/*absolute setter*/},
882 static const struct Xet_reg display_getters
[] = {
883 {"tty", get_string
, offsetof(struct display
, d_usertty
)},
884 {"term", get_string
, offsetof(struct display
, d_termname
)},
885 {"fore", get_window
, offsetof(struct display
, d_fore
)},
886 {"other", get_window
, offsetof(struct display
, d_other
)},
887 {"width", get_int
, offsetof(struct display
, d_width
)},
888 {"height", get_int
, offsetof(struct display
, d_height
)},
889 {"user", get_user
, offsetof(struct display
, d_user
)},
890 {"layout", get_layout
, offsetof(struct display
, d_layout
)},
899 screen_get_windows(lua_State
*L
)
905 for (iter
= windows
, count
= 0; iter
; iter
= iter
->w_next
, count
++) {
906 lua_pushinteger(L
, iter
->w_number
);
907 push_window(L
, &iter
);
915 screen_get_displays(lua_State
*L
)
917 struct display
*iter
;
921 for (iter
= displays
, count
= 0; iter
; iter
= iter
->d_next
, count
++) {
922 lua_pushinteger(L
, count
);
923 push_display(L
, &iter
);
930 extern struct layout
*layouts
;
932 screen_get_layouts(lua_State
*L
)
938 for (iter
= layouts
, count
= 0; iter
; iter
= iter
->lay_next
, count
++) {
939 lua_pushinteger(L
, iter
->lay_number
);
940 push_layout(L
, &iter
);
948 screen_get_display(lua_State
*L
)
950 push_display(L
, &display
);
955 screen_exec_command(lua_State
*L
)
960 command
= luaL_checklstring(L
, 1, &len
);
962 RcLine((char *)command
, len
);
968 screen_append_msg(lua_State
*L
)
970 const char *msg
, *color
;
972 msg
= luaL_checklstring(L
, 1, &len
);
976 color
= luaL_checklstring(L
, 2, &len
);
977 AppendWinMsgRend(msg
, color
);
988 script_input_fn(char *buf
, int len
, char *priv
)
990 struct sinput_data
*sidata
= (struct sinput_data
*)priv
;
991 lua_handler lh
= sidata
->lh
;
992 lua_State
*L
= sidata
->L
;
994 LuaPushHandler(L
, lh
);
995 lua_pushstring(L
, buf
);
996 if (lua_pcall(L
, 1, 0, 0) == LUA_ERRRUN
)
998 if(lua_isstring(L
, -1))
1004 LuaFreeHandler(L
, &lh
);
1008 screen_input(lua_State
*L
)
1012 const char * prompt
= NULL
, *prefill
= NULL
;
1014 struct sinput_data
*sidata
;
1016 prompt
= luaL_checkstring(L
, 1);
1017 lh
= LuaCheckHandler(L
, 2, 1);
1019 luaL_error(L
, "Out of Memory");
1021 if (lua_gettop(L
) >= 3)
1022 prefill
= luaL_checkstring(L
, 3);
1025 sidata
= (struct sinput_data
*)malloc(sizeof(struct sinput_data
));
1028 LuaFreeHandler(L
, &lh
);
1029 luaL_error(L
, "Out of Memory");
1034 Input((char *)prompt
, 100, INP_COOKED
, script_input_fn
, (char *)sidata
, 0);
1038 for (; *prefill
; prefill
++)
1040 if ((*(unsigned char *)prefill
& 0x7f) < 0x20 || *prefill
== 0x7f)
1042 ss
= (char *)prefill
;
1044 LayProcess(&ss
, &n
);
1050 sev_schedule_fn(struct event
*ev
, char *data
)
1052 struct sinput_data
*si
= (struct sinput_data
*)data
;
1053 lua_handler lh
= si
->lh
;
1054 lua_State
*L
= si
->L
;
1059 LuaPushHandler(L
, lh
);
1060 if (lua_pcall(L
, 1, 0, 0) == LUA_ERRRUN
)
1062 if(lua_isstring(L
, -1))
1067 LuaFreeHandler(L
, &lh
);
1071 screen_schedule(lua_State
*L
)
1073 int timeout
= luaL_checkinteger(L
, 1);
1074 lua_handler lh
= LuaCheckHandler(L
, 2, 1);
1075 struct sinput_data
*si
;
1080 ev
= (struct event
*) calloc(1, sizeof(struct event
));
1083 LuaFreeHandler(L
, &lh
);
1084 luaL_error(L
, "Out of Memory");
1086 si
= (struct sinput_data
*) malloc(sizeof(struct sinput_data
));
1089 LuaFreeHandler(L
, &lh
);
1091 luaL_error(L
, "Out of Memory");
1096 ev
->type
= EV_TIMEOUT
;
1097 ev
->data
= (char *)si
;
1098 ev
->handler
= sev_schedule_fn
;
1100 SetTimeout(ev
, timeout
* 1000);
1104 static const luaL_reg screen_methods
[] = {
1105 {"windows", screen_get_windows
},
1106 {"displays", screen_get_displays
},
1107 {"layouts", screen_get_layouts
},
1108 {"display", screen_get_display
},
1109 {"command", screen_exec_command
},
1110 {"append_msg", screen_append_msg
},
1111 {"hook", LuaRegEvent
},
1112 {"input", screen_input
},
1113 {"schedule", screen_schedule
},
1117 static const luaL_reg screen_metamethods
[] = {
1121 static const struct Xet_reg screen_setters
[] = {
1125 static const struct Xet_reg screen_getters
[] = {
1131 /** Public functions {{{ */
1133 /* FIXME: This code only tracks one unhook ticket for a lua func.
1134 * So it does not work if one func is registered more than one times. */
1136 prepare_weak_table(lua_State
*L
, const char *name
, const char *mode
)
1138 luaL_getmetatable(L
, "screen");
1140 lua_pushstring(L
, name
);
1143 /* prepare a metatable to indicate weak table */
1145 lua_pushstring(L
, "__mode");
1146 lua_pushstring(L
, mode
);
1148 /* Mark weak table */
1149 lua_setmetatable(L
, -2);
1154 static lua_State
*L
;
1157 L
= luaL_newstate();
1161 #define REGISTER(X) struct_register(L, #X , X ## _methods, X##_metamethods, X##_setters, X##_getters)
1170 /* To store handler funcs */
1172 /*_two kinds of information are mantained:
1174 * funcreg[key]->func
1175 * The 'func' part of the tables are weak. That means the registered funcs
1176 * will be collected once the func is no longer available (e.g. overwritten
1177 * by a new instance of the script)
1179 * To make this process happens faster, GC is forced at the end of each
1181 * TODO: What if some events are triggered within the sourcing
1184 prepare_weak_table(L
, "_funcreg", "v");
1186 /* funcunhook[func]->listener
1187 * The listener is the unhook ticket of the hook. which should be collected
1188 * once the func is collected. The gc metamethod will be triggered
1190 prepare_weak_table(L
, "_funcunhook", "k");
1195 /* An error message on top of the stack. */
1197 LuaShowErr(lua_State
*L
)
1199 struct display
*d
= display
;
1201 const char *message
= luaL_checklstring(L
, -1, &len
);
1203 LMsg(0, "%s", message
? message
: "Unknown error");
1209 void (*push_fn
)(lua_State
*, void*);
1214 LuaCallProcess(const char *name
, struct fn_def defs
[])
1218 lua_getfield(L
, LUA_GLOBALSINDEX
, name
);
1219 if (lua_isnil(L
, -1))
1224 for (argc
= 0; defs
[argc
].push_fn
; argc
++)
1225 defs
[argc
].push_fn(L
, defs
[argc
].value
);
1226 if (lua_pcall(L
, argc
, 0, 0) == LUA_ERRRUN
&& lua_isstring(L
, -1))
1234 /* FIXME: Think about this: will it affect the registered handlers?*/
1235 int LuaSource(const char *file
, int async
)
1240 if (stat(file
, &st
) == -1)
1241 Msg(errno
, "Error loading lua script file '%s'", file
);
1244 int len
= strlen(file
);
1245 if (len
< 4 || strncmp(file
+ len
- 4, ".lua", 4) != 0)
1247 if (luaL_dofile(L
, file
) && lua_isstring(L
, -1))
1254 /* It seems that I need two GC passes to really collect the unhook
1255 * ticket, after changing the reference to ticket in the
1256 * 'funcunhook' table from weak to strong. This should not be
1257 * harmful, but how can I make sure that two passes is enough?
1260 * This seems reasonable, since the first pass will collect the func
1261 * itself and make the ticket garbage, and the second pass will
1262 * collect the ticket itself.
1264 * TODO: check this out. maybe ask some lua gurus. */
1265 lua_gc(L
, LUA_GCCOLLECT
, 0);
1266 lua_gc(L
, LUA_GCCOLLECT
, 0);
1283 push_stringarray(lua_State
*L
, char **args
)
1287 for (i
= 1; args
&& *args
; i
++) {
1288 lua_pushinteger(L
, i
);
1289 lua_pushstring(L
, *args
++);
1290 lua_settable(L
, -3);
1295 LuaPushParams(lua_State
*L
, const char *params
, va_list va
)
1303 lua_pushstring(L
, va_arg(va
, char *));
1306 push_stringarray(L
, va_arg(va
, char **));
1309 lua_pushinteger(L
, va_arg(va
, int));
1313 struct display
*d
= va_arg(va
, struct display
*);
1314 push_display(L
, &d
);
1319 struct win
*w
= va_arg(va
, struct win
*);
1325 struct canvas
*c
= va_arg(va
, struct canvas
*);
1336 int LuaCall(const char *func
, const char **argv
)
1342 StackDump(L
, "Before LuaCall\n");
1343 lua_getfield(L
, LUA_GLOBALSINDEX
, func
);
1344 if (lua_isnil(L
, -1))
1347 lua_pushstring(L
, "Could not find the script function\n");
1351 for (argc
= 0; *argv
; argv
++, argc
++)
1353 lua_pushstring(L
, *argv
);
1355 if (lua_pcall(L
, argc
, 0, 0) == LUA_ERRRUN
)
1357 if(lua_isstring(L
, -1))
1359 StackDump(L
, "After LuaCall\n");
1367 /*** Lua callback handler management {{{ */
1370 LuaPushHTable(lua_State
*L
, int screen
, const char * t
)
1372 lua_pushstring(L
, t
);
1373 lua_rawget(L
, screen
);
1374 /* FIXME: Do we need to balance the stack here? */
1375 if (lua_isnil(L
, -1))
1376 luaL_error(L
, "Fatal! Fail to get function registeration table!");
1377 return lua_gettop(L
);
1381 LuaPushHandler(lua_State
*L
, lua_handler lh
)
1384 if (lh
->type
== LUA_HANDLER_TYPE_F
)
1386 luaL_getmetatable(L
, "screen");
1387 funcreg
= LuaPushHTable(L
, lua_gettop(L
), "_funcreg");
1388 lua_rawgeti(L
, funcreg
, lh
->u
.reference
);
1394 lua_getfield(L
, LUA_GLOBALSINDEX
, lh
->u
.name
);
1399 LuaFuncKey(lua_State
*L
, int idx
, int ref
)
1401 int key
, funcreg
, sc
;
1402 luaL_getmetatable(L
, "screen");
1404 funcreg
= LuaPushHTable(L
, sc
, "_funcreg");
1406 lua_pushvalue(L
, idx
);
1407 key
= luaL_ref(L
, funcreg
);
1413 LuaHUnRef(lua_State
*L
, int key
)
1416 luaL_getmetatable(L
, "screen");
1418 funcreg
= LuaPushHTable(L
, sc
, "_funcreg");
1419 luaL_unref(L
, funcreg
, key
);
1423 LuaCheckHandler(lua_State
*L
, int idx
, int ref
)
1427 idx
= lua_gettop(L
) + 1 - idx
;
1429 if (lua_isstring(L
, idx
))
1431 const char * handler
;
1432 /* registered with func name.*/
1433 handler
= luaL_checkstring(L
, idx
);
1434 lua_getfield(L
, LUA_GLOBALSINDEX
, handler
);
1435 if (!lua_isfunction(L
, -1))
1436 luaL_error(L
, "The specified handler %s in param #%d is not a function", handler
, idx
);
1438 return LuaAllocHandler(handler
, 0);
1440 else if (!lua_isfunction(L
, idx
))
1441 luaL_error(L
, "Handler should be a function or the name of function");
1443 key
= LuaFuncKey(L
, idx
, ref
);
1444 return LuaAllocHandler(NULL
, key
);
1450 LuaDispatch(void *handler
, const char *params
, va_list va
)
1452 lua_handler lh
= (lua_handler
)handler
;
1455 StackDump(L
, "before dispatch");
1457 LuaPushHandler(L
, lh
);
1458 if (lua_isnil(L
, -1))
1463 argc
= LuaPushParams(L
, params
, va
);
1465 if (lua_pcall(L
, argc
, 1, 0) == LUA_ERRRUN
&& lua_isstring(L
, -1))
1467 StackDump(L
, "After LuaDispatch\n");
1471 retvalue
= lua_tonumber(L
, -1);
1476 /*FIXME: what if a func is registered twice or more? */
1478 LuaRegAutoUnHook(lua_State
*L
, lua_handler lh
, int ticket
)
1481 luaL_getmetatable(L
, "screen");
1483 funcunhook
= LuaPushHTable(L
, sc
, "_funcunhook");
1484 LuaPushHandler(L
, lh
);
1485 lua_pushvalue(L
, ticket
);
1486 lua_rawset(L
, funcunhook
);
1490 #define SEVNAME_MAX 30
1492 LuaRegEvent(lua_State
*L
)
1494 /* signature: hook(obj, event, handler, priv);
1495 * or: hook(event, handler, priv)
1496 * returns: A ticket for later unregister. */
1497 int idx
= 1, argc
= lua_gettop(L
);
1498 unsigned int priv
= 31; /* Default privilege */
1502 const char *objname
= "global";
1504 static char evbuf
[SEVNAME_MAX
];
1507 struct script_event
*sev
;
1509 StackDump(L
, "Before RegEvent\n");
1511 /* Identify the object, if specified */
1512 if (luaL_getmetafield(L
, 1, "_objname"))
1514 objname
= luaL_checkstring(L
, -1);
1516 if (!strcmp("screen", objname
))
1520 obj
= get_broker_obj((struct broker
**)lua_touserdata(L
, 1));
1522 return luaL_error(L
, "Invalid object specified");
1527 event
= luaL_checkstring(L
, idx
++);
1528 snprintf(evbuf
, SEVNAME_MAX
, "%s_%s", objname
, event
);
1530 /* Check and get the handler */
1531 lh
= LuaCheckHandler(L
, idx
++, 1);
1533 luaL_error(L
, "Out of memory");
1535 StackDump(L
, "In RegEvent\n");
1538 priv
= luaL_checkinteger(L
, idx
);
1540 sev
= object_get_event(obj
, evbuf
);
1545 l
= (struct listener
*)malloc(sizeof(struct listener
));
1547 return luaL_error(L
, "Out of memory");
1549 l
->bd
= &lua_binding
;
1550 l
->handler
= (void *)lh
;
1551 register_listener(sev
, l
);
1552 /* Return the ticket for un-register */
1553 push_callback(L
, &l
);
1554 ticket
= lua_gettop(L
);
1556 LuaRegAutoUnHook(L
, lh
, ticket
);
1559 return luaL_error(L
, "Invalid event specified: %s for object %s", event
, objname
);
1561 StackDump(L
, "After RegEvent\n");
1567 struct binding lua_binding
=