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 static int StackDump(lua_State
*L
, const char *message
)
41 FILE *f
= fopen("/tmp/debug/stack", "ab");
42 int i
= lua_gettop(L
);
44 fprintf(f
, "%s", message
);
47 int t
= lua_type(L
, i
);
51 fprintf(f
, "String: %s\n", lua_tostring(L
, i
));
55 fprintf(f
, "Boolean: %s\n", lua_toboolean(L
, i
) ? "true" : "false");
59 fprintf(f
, "Number: %g\n", lua_tonumber(L
, i
));
63 fprintf(f
, "Type: %s\n", lua_typename(L
, t
));
73 extern struct win
*windows
, *fore
;
74 extern struct display
*displays
, *display
;
75 extern struct LayFuncs WinLf
;
76 extern struct layer
*flayer
;
78 static int LuaDispatch(void *handler
, const char *params
, va_list va
);
79 static int LuaRegEvent(lua_State
*L
);
80 static int LuaUnRegEvent(lua_State
*L
);
81 static void LuaShowErr(lua_State
*L
);
85 LUA_HANDLER_TYPE_N
= 1,
100 LuaAllocHandler(const char *name
, int ref
)
102 struct lua_handler
*lh
;
103 lh
= (struct lua_handler
*) malloc(sizeof(struct lua_handler
));
109 lh
->type
= LUA_HANDLER_TYPE_N
;
114 lh
->type
= LUA_HANDLER_TYPE_F
;
115 lh
->u
.reference
= ref
;
122 LuaFreeHandler(struct lua_handler
**lh
)
124 if ((*lh
)->type
== LUA_HANDLER_TYPE_N
)
131 #define CHECK_TYPE(name, type) \
133 check_##name(lua_State *L, int index) \
136 luaL_checktype(L, index, LUA_TUSERDATA); \
137 var = (type **) luaL_checkudata(L, index, #name); \
139 luaL_typerror(L, index, #name); \
143 #define PUSH_TYPE(name, type) \
145 push_##name(lua_State *L, type **t) \
152 r = (type **)lua_newuserdata(L, sizeof(type *)); \
154 luaL_getmetatable(L, #name); \
155 lua_setmetatable(L,-2); \
159 /* Much of the following template comes from:
160 * http://lua-users.org/wiki/BindingWithMembersAndMethods
163 static int get_int (lua_State
*L
, void *v
)
165 lua_pushinteger (L
, *(int*)v
);
169 static int set_int (lua_State
*L
, void *v
)
171 *(int*)v
= luaL_checkint(L
, 3);
175 static int get_number (lua_State
*L
, void *v
)
177 lua_pushnumber(L
, *(lua_Number
*)v
);
181 static int set_number (lua_State
*L
, void *v
)
183 *(lua_Number
*)v
= luaL_checknumber(L
, 3);
187 static int get_string (lua_State
*L
, void *v
)
189 lua_pushstring(L
, (char*)v
);
193 static int set_string (lua_State
*L
, void *v
)
195 *(const char**)v
= luaL_checkstring(L
, 3);
199 typedef int (*Xet_func
) (lua_State
*L
, void *v
);
201 /* member info for get and set handlers */
204 const char *name
; /* member name */
205 Xet_func func
; /* get or set function for type of member */
206 size_t offset
; /* offset of member within the struct */
207 int (*absolute
)(lua_State
*);
210 static void Xet_add (lua_State
*L
, const struct Xet_reg
*l
)
216 lua_pushstring(L
, l
->name
);
217 lua_pushlightuserdata(L
, (void*)l
);
222 static int Xet_call (lua_State
*L
)
224 /* for get: stack has userdata, index, lightuserdata */
225 /* for set: stack has userdata, index, value, lightuserdata */
226 const struct Xet_reg
*m
= (const struct Xet_reg
*)lua_touserdata(L
, -1); /* member info */
227 lua_pop(L
, 1); /* drop lightuserdata */
228 luaL_checktype(L
, 1, LUA_TUSERDATA
);
230 return m
->absolute(L
);
231 return m
->func(L
, *(char**)lua_touserdata(L
, 1) + m
->offset
);
234 static int index_handler (lua_State
*L
)
236 /* stack has userdata, index */
237 lua_pushvalue(L
, 2); /* dup index */
238 lua_rawget(L
, lua_upvalueindex(1)); /* lookup member by name */
239 if (!lua_islightuserdata(L
, -1))
241 lua_pop(L
, 1); /* drop value */
242 lua_pushvalue(L
, 2); /* dup index */
243 lua_gettable(L
, lua_upvalueindex(2)); /* else try methods */
244 if (lua_isnil(L
, -1)) /* invalid member */
245 luaL_error(L
, "cannot get member '%s'", lua_tostring(L
, 2));
248 return Xet_call(L
); /* call get function */
251 static int newindex_handler (lua_State
*L
)
253 /* stack has userdata, index, value */
254 lua_pushvalue(L
, 2); /* dup index */
255 lua_rawget(L
, lua_upvalueindex(1)); /* lookup member by name */
256 if (!lua_islightuserdata(L
, -1)) /* invalid member */
257 luaL_error(L
, "cannot set member '%s'", lua_tostring(L
, 2));
258 return Xet_call(L
); /* call set function */
262 struct_register(lua_State
*L
, const char *name
, const luaL_reg fn_methods
[], const luaL_reg meta_methods
[],
263 const struct Xet_reg setters
[], const struct Xet_reg getters
[])
265 int metatable
, methods
;
267 /* create methods table & add it to the table of globals */
268 luaL_register(L
, name
, fn_methods
);
269 methods
= lua_gettop(L
);
271 /* create metatable & add it to the registry */
272 luaL_newmetatable(L
, name
);
273 luaL_register(L
, 0, meta_methods
); /* fill metatable */
275 /* To identify the type of object */
276 lua_pushstring(L
, "_objname");
277 lua_pushstring(L
, name
);
280 metatable
= lua_gettop(L
);
282 lua_pushliteral(L
, "__metatable");
283 lua_pushvalue(L
, methods
); /* dup methods table*/
284 lua_rawset(L
, metatable
); /* hide metatable:
285 metatable.__metatable = methods */
287 lua_pushliteral(L
, "__index");
288 lua_pushvalue(L
, metatable
); /* upvalue index 1 */
289 Xet_add(L
, getters
); /* fill metatable with getters */
290 lua_pushvalue(L
, methods
); /* upvalue index 2 */
291 lua_pushcclosure(L
, index_handler
, 2);
292 lua_rawset(L
, metatable
); /* metatable.__index = index_handler */
294 lua_pushliteral(L
, "__newindex");
295 lua_newtable(L
); /* table for members you can set */
296 Xet_add(L
, setters
); /* fill with setters */
297 lua_pushcclosure(L
, newindex_handler
, 1);
298 lua_rawset(L
, metatable
); /* metatable.__newindex = newindex_handler */
300 /* FIXME: Why do we leave an element on the stack? */
301 lua_pop(L
, 1); /* drop metatable */
302 return 1; /* return methods on the stack */
309 PUSH_TYPE(window
, struct win
)
311 CHECK_TYPE(window
, struct win
)
313 static int get_window(lua_State
*L
, void *v
)
315 push_window(L
, (struct win
**)v
);
320 window_change_title(lua_State
*L
)
322 struct win
*w
= check_window(L
, 1);
324 const char *title
= luaL_checklstring(L
, 2, &len
);
325 ChangeAKA(w
, (char *)title
, len
);
330 window_get_monitor_status(lua_State
*L
)
332 struct win
*w
= check_window(L
, 1);
333 int activity
= luaL_checkint(L
, 2);
336 lua_pushinteger(L
, w
->w_monitor
!= MON_OFF
);
339 lua_pushinteger(L
, w
->w_silence
== SILENCE_ON
? w
->w_silencewait
: 0);
344 static const luaL_reg window_methods
[] = {
345 {"get_monitor_status", window_get_monitor_status
},
346 {"hook", LuaRegEvent
},
351 window_tostring(lua_State
*L
)
354 struct win
*w
= check_window(L
, 1);
355 snprintf(str
, sizeof(str
), "{window #%d: '%s'}", w
->w_number
, w
->w_title
);
356 lua_pushstring(L
, str
);
361 window_equality(lua_State
*L
)
363 struct win
*w1
= check_window(L
, 1), *w2
= check_window(L
, 2);
364 lua_pushboolean(L
, (w1
== w2
|| (w1
&& w2
&& w1
->w_number
== w2
->w_number
)));
368 static const luaL_reg window_metamethods
[] = {
369 {"__tostring", window_tostring
},
370 {"__eq", window_equality
},
374 static const struct Xet_reg window_setters
[] = {
375 {"title", 0, 0, window_change_title
/*absolute setter*/},
379 static const struct Xet_reg window_getters
[] = {
380 {"title", get_string
, offsetof(struct win
, w_title
) + 8},
381 {"number", get_int
, offsetof(struct win
, w_number
)},
382 {"dir", get_string
, offsetof(struct win
, w_dir
)},
383 {"tty", get_string
, offsetof(struct win
, w_tty
)},
384 {"pid", get_int
, offsetof(struct win
, w_pid
)},
385 {"group", get_window
, offsetof(struct win
, w_group
)},
386 {"bell", get_int
, offsetof(struct win
, w_bell
)},
395 PUSH_TYPE(user
, struct acluser
)
397 CHECK_TYPE(user
, struct acluser
)
400 get_user(lua_State
*L
, void *v
)
402 push_user(L
, (struct acluser
**)v
);
406 static const luaL_reg user_methods
[] = {
411 user_tostring(lua_State
*L
)
414 struct acluser
*u
= check_user(L
, 1);
415 snprintf(str
, sizeof(str
), "{user '%s'}", u
->u_name
);
416 lua_pushstring(L
, str
);
420 static const luaL_reg user_metamethods
[] = {
421 {"__tostring", user_tostring
},
425 static const struct Xet_reg user_setters
[] = {
429 static const struct Xet_reg user_getters
[] = {
430 {"name", get_string
, offsetof(struct acluser
, u_name
)},
431 {"password", get_string
, offsetof(struct acluser
, u_password
)},
439 PUSH_TYPE(canvas
, struct canvas
)
441 CHECK_TYPE(canvas
, struct canvas
)
444 get_canvas(lua_State
*L
, void *v
)
446 push_canvas(L
, (struct canvas
**)v
);
451 canvas_select(lua_State
*L
)
453 struct canvas
*c
= check_canvas(L
, 1);
454 if (!display
|| D_forecv
== c
)
456 SetCanvasWindow(c
, Layer2Window(c
->c_layer
));
459 /* XXX: the following all is duplicated from process.c:DoAction.
460 * Should these be in some better place?
462 ResizeCanvas(&D_canvas
);
463 RecreateCanvasChain();
464 RethinkDisplayViewports();
465 ResizeLayersToCanvases(); /* redisplays */
466 fore
= D_fore
= Layer2Window(D_forecv
->c_layer
);
467 flayer
= D_forecv
->c_layer
;
469 if (D_xtermosc
[2] || D_xtermosc
[3])
479 flayer
= D_forecv
->c_layer
;
480 CV_CALL(D_forecv
, LayRestore();LaySetCursor());
481 WindowChanged(0, 'F');
485 static const luaL_reg canvas_methods
[] = {
486 {"select", canvas_select
},
490 static const luaL_reg canvas_metamethods
[] = {
494 static const struct Xet_reg canvas_setters
[] = {
499 canvas_get_window(lua_State
*L
)
501 struct canvas
*c
= check_canvas(L
, 1);
502 struct win
*win
= Layer2Window(c
->c_layer
);
504 push_window(L
, &win
);
510 static const struct Xet_reg canvas_getters
[] = {
511 {"next", get_canvas
, offsetof(struct canvas
, c_next
)},
512 {"xoff", get_int
, offsetof(struct canvas
, c_xoff
)},
513 {"yoff", get_int
, offsetof(struct canvas
, c_yoff
)},
514 {"xs", get_int
, offsetof(struct canvas
, c_xs
)},
515 {"ys", get_int
, offsetof(struct canvas
, c_ys
)},
516 {"xe", get_int
, offsetof(struct canvas
, c_xe
)},
517 {"ye", get_int
, offsetof(struct canvas
, c_ye
)},
518 {"window", 0, 0, canvas_get_window
},
526 PUSH_TYPE(layout
, struct layout
)
527 CHECK_TYPE(layout
, struct layout
)
529 static const struct Xet_reg layout_getters
[] = {
534 get_layout(lua_State
*L
, void *v
)
536 push_layout(L
, (struct layout
**)v
);
544 PUSH_TYPE(display
, struct display
)
546 CHECK_TYPE(display
, struct display
)
549 display_get_canvases(lua_State
*L
)
555 d
= check_display(L
, 1);
557 for (iter
= d
->d_cvlist
, count
= 0; iter
; iter
= iter
->c_next
, count
++) {
558 lua_pushinteger(L
, count
);
559 push_canvas(L
, &iter
);
566 static const luaL_reg display_methods
[] = {
567 {"get_canvases", display_get_canvases
},
572 display_tostring(lua_State
*L
)
575 struct display
*d
= check_display(L
, 1);
576 snprintf(str
, sizeof(str
), "{display: tty = '%s', term = '%s'}", d
->d_usertty
, d
->d_termname
);
577 lua_pushstring(L
, str
);
581 static const luaL_reg display_metamethods
[] = {
582 {"__tostring", display_tostring
},
586 static const struct Xet_reg display_setters
[] = {
590 static const struct Xet_reg display_getters
[] = {
591 {"tty", get_string
, offsetof(struct display
, d_usertty
)},
592 {"term", get_string
, offsetof(struct display
, d_termname
)},
593 {"fore", get_window
, offsetof(struct display
, d_fore
)},
594 {"other", get_window
, offsetof(struct display
, d_other
)},
595 {"width", get_int
, offsetof(struct display
, d_width
)},
596 {"height", get_int
, offsetof(struct display
, d_height
)},
597 {"user", get_user
, offsetof(struct display
, d_user
)},
598 {"layout", get_layout
, offsetof(struct display
, d_layout
)},
607 screen_get_windows(lua_State
*L
)
613 for (iter
= windows
, count
= 0; iter
; iter
= iter
->w_next
, count
++) {
614 lua_pushinteger(L
, iter
->w_number
);
615 push_window(L
, &iter
);
623 screen_get_displays(lua_State
*L
)
625 struct display
*iter
;
629 for (iter
= displays
, count
= 0; iter
; iter
= iter
->d_next
, count
++) {
630 lua_pushinteger(L
, count
);
631 push_display(L
, &iter
);
639 screen_get_display(lua_State
*L
)
641 push_display(L
, &display
);
646 screen_exec_command(lua_State
*L
)
651 command
= luaL_checklstring(L
, 1, &len
);
653 RcLine((char *)command
, len
);
659 screen_append_msg(lua_State
*L
)
661 const char *msg
, *color
;
663 msg
= luaL_checklstring(L
, 1, &len
);
667 color
= luaL_checklstring(L
, 2, &len
);
668 AppendWinMsgRend(msg
, color
);
673 script_input_fn(char *buf
, int len
, char *priv
)
675 lua_State
*L
= (lua_State
*)priv
;
676 lua_pushstring(L
, buf
);
677 if (lua_pcall(L
, 1, 0, 0) == LUA_ERRRUN
)
679 if(lua_isstring(L
, -1))
687 screen_input(lua_State
*L
)
689 char * prompt
= NULL
;
691 prompt
= (char *)luaL_checkstring(L
, 1);
692 Input(prompt
, 100, INP_COOKED
, script_input_fn
, (char *)L
, 0);
697 static const luaL_reg screen_methods
[] = {
698 {"windows", screen_get_windows
},
699 {"displays", screen_get_displays
},
700 {"display", screen_get_display
},
701 {"command", screen_exec_command
},
702 {"append_msg", screen_append_msg
},
703 {"hook", LuaRegEvent
},
704 {"unhook", LuaUnRegEvent
},
705 {"input", screen_input
},
709 static const luaL_reg screen_metamethods
[] = {
713 static const struct Xet_reg screen_setters
[] = {
717 static const struct Xet_reg screen_getters
[] = {
723 /** Public functions {{{ */
731 #define REGISTER(X) struct_register(L, #X , X ## _methods, X##_metamethods, X##_setters, X##_getters)
739 /* To store handler funcs */
740 luaL_getmetatable(L
, "screen");
741 /* two kinds of info in this table:
742 * _funckey[func]->key
743 * _funckey[key]->refcnt */
744 lua_pushstring(L
, "_funckey");
747 /* two kinds of info in this table:
748 * _keyfunc[key]->func
749 * _keyfunc[func]->name */
750 lua_pushstring(L
, "_keyfunc");
758 /* An error message on top of the stack. */
760 LuaShowErr(lua_State
*L
)
762 struct display
*d
= display
;
764 const char *message
= luaL_checklstring(L
, -1, &len
);
765 LMsg(0, "%s", message
? message
: "Unknown error");
772 void (*push_fn
)(lua_State
*, void*);
777 LuaCallProcess(const char *name
, struct fn_def defs
[])
781 lua_getfield(L
, LUA_GLOBALSINDEX
, name
);
782 if (lua_isnil(L
, -1))
787 for (argc
= 0; defs
[argc
].push_fn
; argc
++)
788 defs
[argc
].push_fn(L
, defs
[argc
].value
);
789 if (lua_pcall(L
, argc
, 0, 0) == LUA_ERRRUN
&& lua_isstring(L
, -1))
797 int LuaSource(const char *file
, int async
)
802 if (stat(file
, &st
) == -1)
803 Msg(errno
, "Error loading lua script file '%s'", file
);
806 int len
= strlen(file
);
807 if (len
< 4 || strncmp(file
+ len
- 4, ".lua", 4) != 0)
809 if (luaL_dofile(L
, file
) && lua_isstring(L
, -1))
828 LuaProcessCaption(const char *caption
, struct win
*win
, int len
)
832 struct fn_def params
[] = {
833 {lua_pushstring
, caption
},
835 {lua_pushinteger
, len
},
838 return LuaCallProcess("process_caption", params
);
842 push_stringarray(lua_State
*L
, char **args
)
846 for (i
= 1; args
&& *args
; i
++) {
847 lua_pushinteger(L
, i
);
848 lua_pushstring(L
, *args
++);
854 LuaPushParams(lua_State
*L
, const char *params
, va_list va
)
862 lua_pushstring(L
, va_arg(va
, char *));
865 push_stringarray(L
, va_arg(va
, char **));
868 lua_pushinteger(L
, va_arg(va
, int));
876 int LuaCall(char *func
, char **argv
)
882 StackDump(L
, "Before LuaCall\n");
883 lua_getfield(L
, LUA_GLOBALSINDEX
, func
);
884 if (lua_isnil(L
, -1))
887 lua_pushstring(L
, "Could not find the script function\n");
891 for (argc
= 0; *argv
; argv
++, argc
++)
893 lua_pushstring(L
, *argv
);
895 if (lua_pcall(L
, argc
, 0, 0) == LUA_ERRRUN
)
897 if(lua_isstring(L
, -1))
899 StackDump(L
, "After LuaCall\n");
907 /* Push nil if failed */
909 LuaPushHandler(lua_State
*L
, struct lua_handler
* lh
)
911 if (lh
->type
== LUA_HANDLER_TYPE_N
)
912 lua_getfield(L
, LUA_GLOBALSINDEX
, lh
->u
.name
);
915 luaL_getmetatable(L
, "screen");
916 lua_pushstring(L
, "_keyfunc");
918 lua_rawgeti(L
, -1, lh
->u
.reference
);
925 LuaDispatch(void *handler
, const char *params
, va_list va
)
927 struct lua_handler
*lh
= handler
;
930 StackDump(L
, "before dispatch");
932 LuaPushHandler(L
, lh
);
933 if (lua_isnil(L
, -1))
938 argc
= LuaPushParams(L
, params
, va
);
940 if (lua_pcall(L
, argc
, 1, 0) == LUA_ERRRUN
&& lua_isstring(L
, -1))
942 StackDump(L
, "After LuaDispatch\n");
946 retvalue
= lua_tonumber(L
, -1);
951 int LuaForeWindowChanged(void)
953 struct fn_def params
[] = {
954 {push_display
, &display
},
955 {push_window
, display
? &D_fore
: &fore
},
960 return LuaCallProcess("fore_changed", params
);
964 LuaPushHTable(lua_State
*L
, int screen
, const char * t
)
966 lua_pushstring(L
, t
);
967 lua_rawget(L
, screen
);
968 /* FIXME: Do we need to balance the stack here? */
969 if (lua_isnil(L
, -1))
970 luaL_error(L
, "Fatal! Fail to get function registeration table!");
971 return lua_gettop(L
);
975 LuaFuncKey(lua_State
*L
, int idx
)
977 int key
, funckey
, sc
;
978 luaL_getmetatable(L
, "screen");
980 funckey
= LuaPushHTable(L
, sc
, "_funckey");
982 lua_pushvalue(L
, idx
);
983 lua_gettable(L
, funckey
);/*funckey[func] ==?*/
984 if (lua_isnil(L
, -1))
986 /* Not found, alloc a new key */
989 lua_pushinteger(L
, 1);
990 key
= luaL_ref(L
, funckey
);
992 /*funckey[func]=key*/
993 lua_pushvalue(L
, idx
);
994 lua_pushinteger(L
, key
);
995 lua_settable(L
, funckey
);
997 int keyfunc
= LuaPushHTable(L
, sc
, "_keyfunc");
998 /*keyfunc[key] = func*/
999 lua_pushinteger(L
, key
);
1000 lua_pushvalue(L
, idx
);
1001 lua_settable(L
, keyfunc
);
1006 key
= lua_tointeger(L
, -1);/*key = funckey[func]*/
1013 LuaHRef(lua_State
*L
, int key
, int reg
)
1015 int funckey
, keyfunc
, cnt
, sc
, idx
;
1016 luaL_getmetatable(L
, "screen");
1018 funckey
= LuaPushHTable(L
, sc
, "_funckey");
1019 keyfunc
= LuaPushHTable(L
, sc
, "_keyfunc");
1021 lua_rawgeti(L
, keyfunc
, key
);
1022 idx
= lua_gettop(L
);
1024 lua_rawgeti(L
, funckey
, key
);
1025 cnt
= lua_tointeger(L
, -1);/*cnt = htable[key]*/
1026 if (!reg
&& cnt
<= 1)
1028 /*release the last reference*/
1029 luaL_unref(L
, funckey
, key
);
1030 lua_pushvalue(L
, idx
);
1032 lua_settable(L
, funckey
); /*htable[func]=key*/
1036 lua_pushinteger(L
, key
);
1037 lua_pushinteger(L
, reg
? cnt
+ 1 : cnt
- 1);
1038 lua_settable(L
, funckey
); /*htable[key] = cnt + 1*/
1044 LuaHSetName(lua_State
*L
, int key
, int name
)
1047 luaL_getmetatable(L
, "screen");
1049 keyfunc
= LuaPushHTable(L
, sc
, "_keyfunc");
1051 lua_rawgeti(L
, keyfunc
, key
);
1052 lua_pushvalue(L
, name
);
1053 lua_rawset(L
, keyfunc
);
1058 struct lua_handler
*
1059 LuaCheckHandler(lua_State
*L
, int idx
, int reg
)
1061 int name
= 0, key
, sc
;
1062 if (lua_isstring(L
, idx
))
1064 const char * handler
;
1065 /* registered with func name.*/
1066 handler
= luaL_checkstring(L
, idx
);
1068 lua_getfield(L
, LUA_GLOBALSINDEX
, handler
);
1069 if (!lua_isfunction(L
, -1))
1070 luaL_error(L
, "The specified handler %s in param #%d is not a function", handler
, idx
);
1072 else if (!lua_isfunction(L
, idx
))
1073 luaL_error(L
, "Handler should be a function or the name of function");
1076 key
= LuaFuncKey(L
, idx
);
1077 LuaHRef(L
, key
, reg
);
1080 LuaHSetName(L
, key
, name
);
1083 return LuaAllocHandler(NULL
, key
);
1086 #define SEVNAME_MAX 30
1088 LuaRegEvent(lua_State
*L
)
1090 /* signature: hook(obj, event, handler, priv);
1091 * or: hook(event, handler, priv)
1092 * returns: A ticket for later unregister. */
1093 int idx
= 1, argc
= lua_gettop(L
);
1094 unsigned int priv
= 31; /* Default privilege */
1095 struct lua_handler
*lh
;
1098 const char *objname
= "global";
1100 static char evbuf
[SEVNAME_MAX
];
1103 struct script_event
*sev
;
1105 StackDump(L
, "Before RegEvent\n");
1107 /* Identify the object, if specified */
1108 if (luaL_getmetafield(L
, 1, "_objname"))
1110 objname
= luaL_checkstring(L
, -1);
1112 if (!strcmp("screen", objname
))
1115 obj
= *(char **)lua_touserdata(L
, 1);
1119 event
= luaL_checkstring(L
, idx
++);
1120 snprintf(evbuf
, SEVNAME_MAX
, "%s_%s", objname
, event
);
1122 /* Check and get the handler */
1123 lh
= LuaCheckHandler(L
, idx
++, 1);
1125 luaL_error(L
, "Out of memory");
1127 StackDump(L
, "In RegEvent\n");
1130 priv
= luaL_checkinteger(L
, idx
);
1132 sev
= object_get_event(obj
, evbuf
);
1136 l
= (struct listener
*)malloc(sizeof(struct listener
));
1138 return luaL_error(L
, "Out of memory");
1140 l
->dispatcher
= LuaDispatch
;
1141 if (register_listener(sev
, l
))
1144 if (lh
->type
== LUA_HANDLER_TYPE_N
)
1145 return luaL_error(L
, "Handler %s has already been registered", lh
->u
.name
);
1147 return luaL_error(L
, "Handler has already been registered");
1149 /* Return the handler for un-register */
1151 lua_pushlightuserdata(L
, l
);
1154 return luaL_error(L
, "Invalid event specified: %s for object %s", event
, objname
);
1156 StackDump(L
, "After RegEvent\n");
1161 LuaUnRegEvent(lua_State
*L
)
1163 /* signature: unhook([obj], ticket)
1164 * returns: true of success, false otherwise */
1168 /* If the param is not as expected */
1169 if (!lua_islightuserdata(L
, idx
))
1171 /* Then it should be the userdata to be ignore, but if not ... */
1172 if (!lua_isuserdata(L
, idx
))
1173 luaL_checktype(L
, idx
, LUA_TLIGHTUSERDATA
);
1175 luaL_checktype(L
, idx
, LUA_TLIGHTUSERDATA
);
1178 l
= (struct listener
*)lua_touserdata(L
, idx
++);
1180 /* Validate the listener structure */
1181 if (!l
|| !l
->handler
)
1184 lua_pushboolean(L
,0);
1188 LuaFreeHandler(((struct lua_handler
**)&(l
->handler
)));
1189 unregister_listener(l
);
1190 lua_pushboolean(L
, 1);
1198 struct ScriptFuncs LuaFuncs
=
1200 LuaForeWindowChanged
,
1204 struct binding lua_binding
=