The script decides what value to return to the script system.
[screen-lua.git] / src / lua.c
blob0fa4c066a3bebe81332101254ebdc441c6c11ed3
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)
8 * any later version.
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>
23 #include "config.h"
24 #include "screen.h"
25 #include <sys/stat.h>
26 #include <unistd.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
32 #include "extern.h"
33 #include "logfile.h"
35 #include <lua.h>
36 #include <lauxlib.h>
37 #include <lualib.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);
43 if (message)
44 fprintf(f, "%s", message);
45 while (i)
47 int t = lua_type(L, i);
48 switch (t)
50 case LUA_TSTRING:
51 fprintf(f, "String: %s\n", lua_tostring(L, i));
52 break;
54 case LUA_TBOOLEAN:
55 fprintf(f, "Boolean: %s\n", lua_toboolean(L, i) ? "true" : "false");
56 break;
58 case LUA_TNUMBER:
59 fprintf(f, "Number: %g\n", lua_tonumber(L, i));
60 break;
62 default:
63 fprintf(f, "Type: %s\n", lua_typename(L, t));
65 i--;
67 if (message)
68 fprintf(f, "----\n");
69 fclose(f);
70 return 0;
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);
82 /** Template {{{ */
84 #define CHECK_TYPE(name, type) \
85 static type * \
86 check_##name(lua_State *L, int index) \
87 { \
88 type **var; \
89 luaL_checktype(L, index, LUA_TUSERDATA); \
90 var = (type **) luaL_checkudata(L, index, #name); \
91 if (!var || !*var) \
92 luaL_typerror(L, index, #name); \
93 return *var; \
96 #define PUSH_TYPE(name, type) \
97 static void \
98 push_##name(lua_State *L, type **t) \
99 { \
100 if (!t || !*t) \
101 lua_pushnil(L); \
102 else \
104 type **r; \
105 r = (type **)lua_newuserdata(L, sizeof(type *)); \
106 *r = *t; \
107 luaL_getmetatable(L, #name); \
108 lua_setmetatable(L,-2); \
112 /* Much of the following template comes from:
113 * http://lua-users.org/wiki/BindingWithMembersAndMethods
116 static int get_int (lua_State *L, void *v)
118 lua_pushinteger (L, *(int*)v);
119 return 1;
122 static int set_int (lua_State *L, void *v)
124 *(int*)v = luaL_checkint(L, 3);
125 return 0;
128 static int get_number (lua_State *L, void *v)
130 lua_pushnumber(L, *(lua_Number*)v);
131 return 1;
134 static int set_number (lua_State *L, void *v)
136 *(lua_Number*)v = luaL_checknumber(L, 3);
137 return 0;
140 static int get_string (lua_State *L, void *v)
142 lua_pushstring(L, (char*)v );
143 return 1;
146 static int set_string (lua_State *L, void *v)
148 *(const char**)v = luaL_checkstring(L, 3);
149 return 0;
152 typedef int (*Xet_func) (lua_State *L, void *v);
154 /* member info for get and set handlers */
155 struct Xet_reg
157 const char *name; /* member name */
158 Xet_func func; /* get or set function for type of member */
159 size_t offset; /* offset of member within the struct */
160 int (*absolute)(lua_State *);
163 static void Xet_add (lua_State *L, const struct Xet_reg *l)
165 if (!l)
166 return;
167 for (; l->name; l++)
169 lua_pushstring(L, l->name);
170 lua_pushlightuserdata(L, (void*)l);
171 lua_settable(L, -3);
175 static int Xet_call (lua_State *L)
177 /* for get: stack has userdata, index, lightuserdata */
178 /* for set: stack has userdata, index, value, lightuserdata */
179 const struct Xet_reg *m = (const struct Xet_reg *)lua_touserdata(L, -1); /* member info */
180 lua_pop(L, 1); /* drop lightuserdata */
181 luaL_checktype(L, 1, LUA_TUSERDATA);
182 if (m->absolute)
183 return m->absolute(L);
184 return m->func(L, *(char**)lua_touserdata(L, 1) + m->offset);
187 static int index_handler (lua_State *L)
189 /* stack has userdata, index */
190 lua_pushvalue(L, 2); /* dup index */
191 lua_rawget(L, lua_upvalueindex(1)); /* lookup member by name */
192 if (!lua_islightuserdata(L, -1))
194 lua_pop(L, 1); /* drop value */
195 lua_pushvalue(L, 2); /* dup index */
196 lua_gettable(L, lua_upvalueindex(2)); /* else try methods */
197 if (lua_isnil(L, -1)) /* invalid member */
198 luaL_error(L, "cannot get member '%s'", lua_tostring(L, 2));
199 return 1;
201 return Xet_call(L); /* call get function */
204 static int newindex_handler (lua_State *L)
206 /* stack has userdata, index, value */
207 lua_pushvalue(L, 2); /* dup index */
208 lua_rawget(L, lua_upvalueindex(1)); /* lookup member by name */
209 if (!lua_islightuserdata(L, -1)) /* invalid member */
210 luaL_error(L, "cannot set member '%s'", lua_tostring(L, 2));
211 return Xet_call(L); /* call set function */
214 static int
215 struct_register(lua_State *L, const char *name, const luaL_reg fn_methods[], const luaL_reg meta_methods[],
216 const struct Xet_reg setters[], const struct Xet_reg getters[])
218 int metatable, methods;
220 /* create methods table & add it to the table of globals */
221 luaL_register(L, name, fn_methods);
222 methods = lua_gettop(L);
224 /* create metatable & add it to the registry */
225 luaL_newmetatable(L, name);
226 luaL_register(L, 0, meta_methods); /* fill metatable */
228 /* To identify the type of object */
229 lua_pushstring(L, "_objname");
230 lua_pushstring(L, name);
231 lua_settable(L, -3);
233 metatable = lua_gettop(L);
235 lua_pushliteral(L, "__metatable");
236 lua_pushvalue(L, methods); /* dup methods table*/
237 lua_rawset(L, metatable); /* hide metatable:
238 metatable.__metatable = methods */
240 lua_pushliteral(L, "__index");
241 lua_pushvalue(L, metatable); /* upvalue index 1 */
242 Xet_add(L, getters); /* fill metatable with getters */
243 lua_pushvalue(L, methods); /* upvalue index 2 */
244 lua_pushcclosure(L, index_handler, 2);
245 lua_rawset(L, metatable); /* metatable.__index = index_handler */
247 lua_pushliteral(L, "__newindex");
248 lua_newtable(L); /* table for members you can set */
249 Xet_add(L, setters); /* fill with setters */
250 lua_pushcclosure(L, newindex_handler, 1);
251 lua_rawset(L, metatable); /* metatable.__newindex = newindex_handler */
253 lua_pop(L, 1); /* drop metatable */
254 return 1; /* return methods on the stack */
257 /** }}} */
259 /** Window {{{ */
261 PUSH_TYPE(window, struct win)
263 CHECK_TYPE(window, struct win)
265 static int get_window(lua_State *L, void *v)
267 push_window(L, (struct win **)v);
268 return 1;
271 static int
272 window_change_title(lua_State *L)
274 struct win *w = check_window(L, 1);
275 unsigned int len;
276 const char *title = luaL_checklstring(L, 2, &len);
277 ChangeAKA(w, (char *)title, len);
278 return 0;
281 static int
282 window_get_monitor_status(lua_State *L)
284 struct win *w = check_window(L, 1);
285 int activity = luaL_checkint(L, 2);
286 if (activity)
287 /*monitor*/
288 lua_pushinteger(L, w->w_monitor != MON_OFF);
289 else
290 /*silence*/
291 lua_pushinteger(L, w->w_silence == SILENCE_ON ? w->w_silencewait: 0);
293 return 1;
296 static const luaL_reg window_methods[] = {
297 {"get_monitor_status", window_get_monitor_status},
298 {"hook", LuaRegEvent},
299 {0, 0}
302 static int
303 window_tostring(lua_State *L)
305 char str[128];
306 struct win *w = check_window(L, 1);
307 snprintf(str, sizeof(str), "{window #%d: '%s'}", w->w_number, w->w_title);
308 lua_pushstring(L, str);
309 return 1;
312 static int
313 window_equality(lua_State *L)
315 struct win *w1 = check_window(L, 1), *w2 = check_window(L, 2);
316 lua_pushboolean(L, (w1 == w2 || (w1 && w2 && w1->w_number == w2->w_number)));
317 return 1;
320 static const luaL_reg window_metamethods[] = {
321 {"__tostring", window_tostring},
322 {"__eq", window_equality},
323 {0, 0}
326 static const struct Xet_reg window_setters[] = {
327 {"title", 0, 0, window_change_title/*absolute setter*/},
328 {0, 0}
331 static const struct Xet_reg window_getters[] = {
332 {"title", get_string, offsetof(struct win, w_title) + 8},
333 {"number", get_int, offsetof(struct win, w_number)},
334 {"dir", get_string, offsetof(struct win, w_dir)},
335 {"tty", get_string, offsetof(struct win, w_tty)},
336 {"pid", get_int, offsetof(struct win, w_pid)},
337 {"group", get_window, offsetof(struct win, w_group)},
338 {"bell", get_int, offsetof(struct win, w_bell)},
339 {0, 0}
343 /** }}} */
345 /** AclUser {{{ */
347 PUSH_TYPE(user, struct acluser)
349 CHECK_TYPE(user, struct acluser)
351 static int
352 get_user(lua_State *L, void *v)
354 push_user(L, (struct acluser **)v);
355 return 1;
358 static const luaL_reg user_methods[] = {
359 {0, 0}
362 static int
363 user_tostring(lua_State *L)
365 char str[128];
366 struct acluser *u = check_user(L, 1);
367 snprintf(str, sizeof(str), "{user '%s'}", u->u_name);
368 lua_pushstring(L, str);
369 return 1;
372 static const luaL_reg user_metamethods[] = {
373 {"__tostring", user_tostring},
374 {0, 0}
377 static const struct Xet_reg user_setters[] = {
378 {0, 0}
381 static const struct Xet_reg user_getters[] = {
382 {"name", get_string, offsetof(struct acluser, u_name)},
383 {"password", get_string, offsetof(struct acluser, u_password)},
384 {0, 0}
387 /** }}} */
389 /** Canvas {{{ */
391 PUSH_TYPE(canvas, struct canvas)
393 CHECK_TYPE(canvas, struct canvas)
395 static int
396 get_canvas(lua_State *L, void *v)
398 push_canvas(L, (struct canvas **)v);
399 return 1;
402 static int
403 canvas_select(lua_State *L)
405 struct canvas *c = check_canvas(L, 1);
406 if (!display || D_forecv == c)
407 return 0;
408 SetCanvasWindow(c, Layer2Window(c->c_layer));
409 D_forecv = c;
411 /* XXX: the following all is duplicated from process.c:DoAction.
412 * Should these be in some better place?
414 ResizeCanvas(&D_canvas);
415 RecreateCanvasChain();
416 RethinkDisplayViewports();
417 ResizeLayersToCanvases(); /* redisplays */
418 fore = D_fore = Layer2Window(D_forecv->c_layer);
419 flayer = D_forecv->c_layer;
420 #ifdef RXVT_OSC
421 if (D_xtermosc[2] || D_xtermosc[3])
423 Activate(-1);
424 break;
426 #endif
427 RefreshHStatus();
428 #ifdef RXVT_OSC
429 RefreshXtermOSC();
430 #endif
431 flayer = D_forecv->c_layer;
432 CV_CALL(D_forecv, LayRestore();LaySetCursor());
433 WindowChanged(0, 'F');
434 return 1;
437 static const luaL_reg canvas_methods[] = {
438 {"select", canvas_select},
439 {0, 0}
442 static const luaL_reg canvas_metamethods[] = {
443 {0, 0}
446 static const struct Xet_reg canvas_setters[] = {
447 {0, 0}
450 static int
451 canvas_get_window(lua_State *L)
453 struct canvas *c = check_canvas(L, 1);
454 struct win *win = Layer2Window(c->c_layer);
455 if (win)
456 push_window(L, &win);
457 else
458 lua_pushnil(L);
459 return 1;
462 static const struct Xet_reg canvas_getters[] = {
463 {"next", get_canvas, offsetof(struct canvas, c_next)},
464 {"xoff", get_int, offsetof(struct canvas, c_xoff)},
465 {"yoff", get_int, offsetof(struct canvas, c_yoff)},
466 {"xs", get_int, offsetof(struct canvas, c_xs)},
467 {"ys", get_int, offsetof(struct canvas, c_ys)},
468 {"xe", get_int, offsetof(struct canvas, c_xe)},
469 {"ye", get_int, offsetof(struct canvas, c_ye)},
470 {"window", 0, 0, canvas_get_window},
471 {0, 0}
474 /** }}} */
476 /** Layout {{{ */
478 PUSH_TYPE(layout, struct layout)
479 CHECK_TYPE(layout, struct layout)
481 static const struct Xet_reg layout_getters[] = {
482 {0,0}
485 static int
486 get_layout(lua_State *L, void *v)
488 push_layout(L, (struct layout **)v);
489 return 1;
492 /** }}} */
494 /** Display {{{ */
496 PUSH_TYPE(display, struct display)
498 CHECK_TYPE(display, struct display)
500 static int
501 display_get_canvases(lua_State *L)
503 struct display *d;
504 struct canvas *iter;
505 int count;
507 d = check_display(L, 1);
508 lua_newtable(L);
509 for (iter = d->d_cvlist, count = 0; iter; iter = iter->c_next, count++) {
510 lua_pushinteger(L, count);
511 push_canvas(L, &iter);
512 lua_settable(L, -3);
515 return 1;
518 static const luaL_reg display_methods[] = {
519 {"get_canvases", display_get_canvases},
520 {0, 0}
523 static int
524 display_tostring(lua_State *L)
526 char str[128];
527 struct display *d = check_display(L, 1);
528 snprintf(str, sizeof(str), "{display: tty = '%s', term = '%s'}", d->d_usertty, d->d_termname);
529 lua_pushstring(L, str);
530 return 1;
533 static const luaL_reg display_metamethods[] = {
534 {"__tostring", display_tostring},
535 {0, 0}
538 static const struct Xet_reg display_setters[] = {
539 {0, 0}
542 static const struct Xet_reg display_getters[] = {
543 {"tty", get_string, offsetof(struct display, d_usertty)},
544 {"term", get_string, offsetof(struct display, d_termname)},
545 {"fore", get_window, offsetof(struct display, d_fore)},
546 {"other", get_window, offsetof(struct display, d_other)},
547 {"width", get_int, offsetof(struct display, d_width)},
548 {"height", get_int, offsetof(struct display, d_height)},
549 {"user", get_user, offsetof(struct display, d_user)},
550 {"layout", get_layout, offsetof(struct display, d_layout)},
551 {0, 0}
554 /** }}} */
556 /** Screen {{{ */
558 static int
559 screen_get_windows(lua_State *L)
561 struct win *iter;
562 int count;
564 lua_newtable(L);
565 for (iter = windows, count = 0; iter; iter = iter->w_next, count++) {
566 lua_pushinteger(L, iter->w_number);
567 push_window(L, &iter);
568 lua_settable(L, -3);
571 return 1;
574 static int
575 screen_get_displays(lua_State *L)
577 struct display *iter;
578 int count;
580 lua_newtable(L);
581 for (iter = displays, count = 0; iter; iter = iter->d_next, count++) {
582 lua_pushinteger(L, count);
583 push_display(L, &iter);
584 lua_settable(L, -3);
587 return 1;
590 static int
591 screen_get_display(lua_State *L)
593 push_display(L, &display);
594 return 1;
597 static int
598 screen_exec_command(lua_State *L)
600 const char *command;
601 unsigned int len;
603 command = luaL_checklstring(L, 1, &len);
604 if (command)
605 RcLine((char *)command, len);
607 return 0;
610 static int
611 screen_append_msg(lua_State *L)
613 const char *msg, *color;
614 int len;
615 msg = luaL_checklstring(L, 1, &len);
616 if (lua_isnil(L, 2))
617 color = NULL;
618 else
619 color = luaL_checklstring(L, 2, &len);
620 AppendWinMsgRend(msg, color);
621 return 0;
624 static const luaL_reg screen_methods[] = {
625 {"windows", screen_get_windows},
626 {"displays", screen_get_displays},
627 {"display", screen_get_display},
628 {"command", screen_exec_command},
629 {"append_msg", screen_append_msg},
630 {"hook", LuaRegEvent},
631 {"unhook", LuaUnRegEvent},
632 {0, 0}
635 static const luaL_reg screen_metamethods[] = {
636 {0, 0}
639 static const struct Xet_reg screen_setters[] = {
640 {0, 0}
643 static const struct Xet_reg screen_getters[] = {
644 {0, 0}
647 /** }}} */
649 /** Public functions {{{ */
650 static lua_State *L;
651 int LuaInit(void)
653 L = luaL_newstate();
655 luaL_openlibs(L);
657 #define REGISTER(X) struct_register(L, #X , X ## _methods, X##_metamethods, X##_setters, X##_getters)
659 REGISTER(screen);
660 REGISTER(window);
661 REGISTER(display);
662 REGISTER(user);
663 REGISTER(canvas);
665 return 0;
668 /* An error message on top of the stack. */
669 static void
670 LuaShowErr(lua_State *L)
672 struct display *d = display;
673 unsigned int len;
674 const char *message = luaL_checklstring(L, -1, &len);
675 LMsg(0, "%s", message ? message : "Unknown error");
676 lua_pop(L, 1);
677 display = d;
680 struct fn_def
682 void (*push_fn)(lua_State *, void*);
683 void *value;
686 static int
687 LuaCallProcess(const char *name, struct fn_def defs[])
689 int argc = 0;
691 lua_getfield(L, LUA_GLOBALSINDEX, name);
692 if (lua_isnil(L, -1))
693 return 0;
694 for (argc = 0; defs[argc].push_fn; argc++)
695 defs[argc].push_fn(L, defs[argc].value);
696 if (lua_pcall(L, argc, 0, 0) == LUA_ERRRUN && lua_isstring(L, -1))
698 LuaShowErr(L);
699 return 0;
701 return 1;
704 int LuaSource(const char *file, int async)
706 if (!L)
707 return 0;
708 struct stat st;
709 if (stat(file, &st) == -1)
710 Msg(errno, "Error loading lua script file '%s'", file);
711 else
713 int len = strlen(file);
714 if (len < 4 || strncmp(file + len - 4, ".lua", 4) != 0)
715 return 0;
716 if (luaL_dofile(L, file) && lua_isstring(L, -1))
718 LuaShowErr(L);
720 return 1;
722 return 0;
725 int LuaFinit(void)
727 if (!L)
728 return 0;
729 lua_close(L);
730 L = (lua_State*)0;
731 return 0;
735 LuaProcessCaption(const char *caption, struct win *win, int len)
737 if (!L)
738 return 0;
739 struct fn_def params[] = {
740 {lua_pushstring, caption},
741 {push_window, &win},
742 {lua_pushinteger, len},
743 {NULL, NULL}
745 return LuaCallProcess("process_caption", params);
748 static void
749 push_stringarray(lua_State *L, char **args)
751 int i;
752 lua_newtable(L);
753 for (i = 1; args && *args; i++) {
754 lua_pushinteger(L, i);
755 lua_pushstring(L, *args++);
756 lua_settable(L, -3);
761 LuaPushParams(lua_State *L, const char *params, va_list va)
763 int num = 0;
764 while (*params)
766 switch (*params)
768 case 's':
769 lua_pushstring(L, va_arg(va, char *));
770 break;
771 case 'S':
772 push_stringarray(L, va_arg(va, char **));
773 break;
774 case 'i':
775 lua_pushinteger(L, va_arg(va, int));
777 params++;
778 num++;
780 return num;
783 int LuaCall(char *func, char **argv)
785 int argc;
786 if (!L)
787 return 0;
789 StackDump(L, "Before LuaCall\n");
790 lua_getfield(L, LUA_GLOBALSINDEX, func);
791 if (lua_isnil(L, -1))
793 lua_pushstring(L, "Could not find the script function\n");
794 LuaShowErr(L);
795 return 0;
797 for (argc = 0; *argv; argv++, argc++)
799 lua_pushstring(L, *argv);
801 if (lua_pcall(L, argc, 0, 0) == LUA_ERRRUN)
803 if(lua_isstring(L, -1))
805 StackDump(L, "After LuaCall\n");
806 LuaShowErr(L);
807 return 0;
810 return 1;
813 static int
814 LuaDispatch(void *handler, const char *params, va_list va)
816 const char *func = handler;
817 int argc, retvalue;
819 lua_getfield(L, LUA_GLOBALSINDEX, func);
820 if (lua_isnil(L, -1))
821 return 0;
822 argc = LuaPushParams(L, params, va);
824 if (lua_pcall(L, argc, 1, 0) == LUA_ERRRUN && lua_isstring(L, -1))
826 StackDump(L, "After LuaDispatch\n");
827 LuaShowErr(L);
828 return 0;
830 retvalue = lua_tonumber(L, -1);
831 lua_pop(L, 1);
832 return retvalue;
835 int LuaForeWindowChanged(void)
837 struct fn_def params[] = {
838 {push_display, &display},
839 {push_window, display ? &D_fore : &fore},
840 {NULL, NULL}
842 if (!L)
843 return 0;
844 return LuaCallProcess("fore_changed", params);
849 LuaCommandExecuted(const char *command, const char **args, int argc)
851 if (!L)
852 return 0;
853 struct fn_def params[] = {
854 {lua_pushstring, command},
855 {push_stringarray, args},
856 {NULL, NULL}
858 return LuaCallProcess("command_executed", params);
861 #define SEVNAME_MAX 30
862 static int
863 LuaRegEvent(lua_State *L)
865 /* signature: hook(obj, event, handler, priv);
866 * or: hook(event, handler, priv)
867 * returns: A ticket for later unregister. */
868 int idx = 1, argc = lua_gettop(L);
869 int priv = 31; /* Default privilege */
871 char *obj = NULL;
872 const char *objname = "global";
874 static char evbuf[SEVNAME_MAX];
875 const char *event, *handler;
877 struct script_event *sev;
879 StackDump(L, "Before RegEvent\n");
881 /* Identify the object, if specified */
882 if (luaL_getmetafield(L, 1, "_objname"))
884 objname = luaL_checkstring(L, -1);
885 lua_pop(L, 1);
886 if (!strcmp("screen", objname))
887 objname = "global";
888 else
889 obj = *(char **)lua_touserdata(L, 1);
890 idx++;
893 event = luaL_checkstring(L, idx++);
894 snprintf(evbuf, SEVNAME_MAX, "%s_%s", objname, event);
895 handler = luaL_checkstring(L, idx++);
897 StackDump(L, "In RegEvent\n");
899 if (idx <= argc)
900 priv = luaL_checkinteger(L, idx);
902 sev = object_get_event(obj, evbuf);
903 if (sev)
905 struct listener *l;
906 l = (struct listener *)malloc(sizeof(struct listener));
907 if (!l)
908 return luaL_error(L, "Out of memory");
909 l->handler = (void *)handler;
910 l->priv = priv;
911 l->dispatcher = LuaDispatch;
912 if (register_listener(sev, l))
914 free(l);
915 return luaL_error(L, "Handler %s has already been registered", handler);
917 /*Return the handler for un-register*/
918 lua_pushlightuserdata(L, l);
920 else
921 return luaL_error(L, "Invalid event specified: %s for object %s", event, objname);
923 StackDump(L, "After RegEvent\n");
924 return 1;
927 static int
928 LuaUnRegEvent(lua_State *L)
930 /* signature: release([obj], ticket, handler)
931 * returns: true of success, false otherwise */
932 int idx = 1;
933 struct listener *l;
934 const char *handler;
936 /* If the param is not as expected */
937 if (!lua_islightuserdata(L, idx))
939 /* Then it should be the userdata to be ignore, but if not ... */
940 if (!lua_isuserdata(L, idx))
941 luaL_checktype(L, idx, LUA_TLIGHTUSERDATA);
942 idx++;
943 luaL_checktype(L, idx, LUA_TLIGHTUSERDATA);
946 l = (struct listener*)lua_touserdata(L, idx++);
947 handler = luaL_checkstring(L, idx++);
949 /*Validate the listener structure*/
950 if (!l || !l->handler
951 || strncmp((char *)handler, (char *)l->handler, SEVNAME_MAX))
953 /* invalid */
954 lua_pushboolean(L,0);
956 else
958 unregister_listener(l);
959 lua_pushboolean(L, 1);
962 return 1;
965 /** }}} */
967 struct ScriptFuncs LuaFuncs =
969 LuaForeWindowChanged,
970 LuaProcessCaption
973 struct binding lua_binding =
975 "lua", /*name*/
976 0, /*inited*/
977 0, /*registered*/
978 LuaInit,
979 LuaFinit,
980 LuaCall,
981 LuaSource,
982 0, /*b_next*/
983 &LuaFuncs