1 /* Lua scripting support
3 * Copyright (c) 2008 Sadrul Habib Chowdhury (sadrul@users.sf.net)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program (see the file COPYING); if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
20 ****************************************************************
22 #include <sys/types.h>
39 extern struct win
*windows
, *fore
;
40 extern struct display
*displays
, *display
;
41 extern struct LayFuncs WinLf
;
42 extern struct layer
*flayer
;
46 #define CHECK_TYPE(name, type) \
48 check_##name(lua_State *L, int index) \
51 luaL_checktype(L, index, LUA_TUSERDATA); \
52 var = (type **) luaL_checkudata(L, index, #name); \
54 luaL_typerror(L, index, #name); \
58 #define PUSH_TYPE(name, type) \
60 push_##name(lua_State *L, type **t) \
67 r = (type **)lua_newuserdata(L, sizeof(type *)); \
69 luaL_getmetatable(L, #name); \
70 lua_setmetatable(L,-2); \
74 /* Much of the following template comes from:
75 * http://lua-users.org/wiki/BindingWithMembersAndMethods
78 static int get_int (lua_State
*L
, void *v
)
80 lua_pushinteger (L
, *(int*)v
);
84 static int set_int (lua_State
*L
, void *v
)
86 *(int*)v
= luaL_checkint(L
, 3);
90 static int get_number (lua_State
*L
, void *v
)
92 lua_pushnumber(L
, *(lua_Number
*)v
);
96 static int set_number (lua_State
*L
, void *v
)
98 *(lua_Number
*)v
= luaL_checknumber(L
, 3);
102 static int get_string (lua_State
*L
, void *v
)
104 lua_pushstring(L
, (char*)v
);
108 static int set_string (lua_State
*L
, void *v
)
110 *(const char**)v
= luaL_checkstring(L
, 3);
114 typedef int (*Xet_func
) (lua_State
*L
, void *v
);
116 /* member info for get and set handlers */
119 const char *name
; /* member name */
120 Xet_func func
; /* get or set function for type of member */
121 size_t offset
; /* offset of member within the struct */
122 int (*absolute
)(lua_State
*);
125 static void Xet_add (lua_State
*L
, const struct Xet_reg
*l
)
131 lua_pushstring(L
, l
->name
);
132 lua_pushlightuserdata(L
, (void*)l
);
137 static int Xet_call (lua_State
*L
)
139 /* for get: stack has userdata, index, lightuserdata */
140 /* for set: stack has userdata, index, value, lightuserdata */
141 const struct Xet_reg
*m
= (const struct Xet_reg
*)lua_touserdata(L
, -1); /* member info */
142 lua_pop(L
, 1); /* drop lightuserdata */
143 luaL_checktype(L
, 1, LUA_TUSERDATA
);
145 return m
->absolute(L
);
146 return m
->func(L
, *(char**)lua_touserdata(L
, 1) + m
->offset
);
149 static int index_handler (lua_State
*L
)
151 /* stack has userdata, index */
152 lua_pushvalue(L
, 2); /* dup index */
153 lua_rawget(L
, lua_upvalueindex(1)); /* lookup member by name */
154 if (!lua_islightuserdata(L
, -1))
156 lua_pop(L
, 1); /* drop value */
157 lua_pushvalue(L
, 2); /* dup index */
158 lua_gettable(L
, lua_upvalueindex(2)); /* else try methods */
159 if (lua_isnil(L
, -1)) /* invalid member */
160 luaL_error(L
, "cannot get member '%s'", lua_tostring(L
, 2));
163 return Xet_call(L
); /* call get function */
166 static int newindex_handler (lua_State
*L
)
168 /* stack has userdata, index, value */
169 lua_pushvalue(L
, 2); /* dup index */
170 lua_rawget(L
, lua_upvalueindex(1)); /* lookup member by name */
171 if (!lua_islightuserdata(L
, -1)) /* invalid member */
172 luaL_error(L
, "cannot set member '%s'", lua_tostring(L
, 2));
173 return Xet_call(L
); /* call set function */
177 struct_register(lua_State
*L
, const char *name
, const luaL_reg fn_methods
[], const luaL_reg meta_methods
[],
178 const struct Xet_reg setters
[], const struct Xet_reg getters
[])
180 int metatable
, methods
;
182 /* create methods table & add it to the table of globals */
183 luaL_register(L
, name
, fn_methods
);
184 methods
= lua_gettop(L
);
186 /* create metatable & add it to the registry */
187 luaL_newmetatable(L
, name
);
188 luaL_register(L
, 0, meta_methods
); /* fill metatable */
189 metatable
= lua_gettop(L
);
191 lua_pushliteral(L
, "__metatable");
192 lua_pushvalue(L
, methods
); /* dup methods table*/
193 lua_rawset(L
, metatable
); /* hide metatable:
194 metatable.__metatable = methods */
196 lua_pushliteral(L
, "__index");
197 lua_pushvalue(L
, metatable
); /* upvalue index 1 */
198 Xet_add(L
, getters
); /* fill metatable with getters */
199 lua_pushvalue(L
, methods
); /* upvalue index 2 */
200 lua_pushcclosure(L
, index_handler
, 2);
201 lua_rawset(L
, metatable
); /* metatable.__index = index_handler */
203 lua_pushliteral(L
, "__newindex");
204 lua_newtable(L
); /* table for members you can set */
205 Xet_add(L
, setters
); /* fill with setters */
206 lua_pushcclosure(L
, newindex_handler
, 1);
207 lua_rawset(L
, metatable
); /* metatable.__newindex = newindex_handler */
209 lua_pop(L
, 1); /* drop metatable */
210 return 1; /* return methods on the stack */
217 PUSH_TYPE(window
, struct win
)
219 CHECK_TYPE(window
, struct win
)
221 static int get_window(lua_State
*L
, void *v
)
223 push_window(L
, (struct win
**)v
);
228 window_change_title(lua_State
*L
)
230 struct win
*w
= check_window(L
, 1);
232 const char *title
= luaL_checklstring(L
, 2, &len
);
233 ChangeAKA(w
, (char *)title
, len
);
238 window_get_monitor_status(lua_State
*L
)
240 struct win
*w
= check_window(L
, 1);
241 int activity
= luaL_checkint(L
, 2);
244 lua_pushinteger(L
, w
->w_monitor
!= MON_OFF
);
247 lua_pushinteger(L
, w
->w_silence
== SILENCE_ON
? w
->w_silencewait
: 0);
252 static const luaL_reg window_methods
[] = {
253 {"get_monitor_status", window_get_monitor_status
},
258 window_tostring(lua_State
*L
)
261 struct win
*w
= check_window(L
, 1);
262 snprintf(str
, sizeof(str
), "{window #%d: '%s'}", w
->w_number
, w
->w_title
);
263 lua_pushstring(L
, str
);
268 window_equality(lua_State
*L
)
270 struct win
*w1
= check_window(L
, 1), *w2
= check_window(L
, 2);
271 lua_pushboolean(L
, (w1
== w2
|| (w1
&& w2
&& w1
->w_number
== w2
->w_number
)));
275 static const luaL_reg window_metamethods
[] = {
276 {"__tostring", window_tostring
},
277 {"__eq", window_equality
},
281 static const struct Xet_reg window_setters
[] = {
282 {"title", 0, 0, window_change_title
/*absolute setter*/},
286 static const struct Xet_reg window_getters
[] = {
287 {"title", get_string
, offsetof(struct win
, w_title
) + 8},
288 {"number", get_int
, offsetof(struct win
, w_number
)},
289 {"dir", get_string
, offsetof(struct win
, w_dir
)},
290 {"tty", get_string
, offsetof(struct win
, w_tty
)},
291 {"pid", get_int
, offsetof(struct win
, w_pid
)},
292 {"group", get_window
, offsetof(struct win
, w_group
)},
293 {"bell", get_int
, offsetof(struct win
, w_bell
)},
302 PUSH_TYPE(user
, struct acluser
)
304 CHECK_TYPE(user
, struct acluser
)
307 get_user(lua_State
*L
, void *v
)
309 push_user(L
, (struct acluser
**)v
);
313 static const luaL_reg user_methods
[] = {
318 user_tostring(lua_State
*L
)
321 struct acluser
*u
= check_user(L
, 1);
322 snprintf(str
, sizeof(str
), "{user '%s'}", u
->u_name
);
323 lua_pushstring(L
, str
);
327 static const luaL_reg user_metamethods
[] = {
328 {"__tostring", user_tostring
},
332 static const struct Xet_reg user_setters
[] = {
336 static const struct Xet_reg user_getters
[] = {
337 {"name", get_string
, offsetof(struct acluser
, u_name
)},
338 {"password", get_string
, offsetof(struct acluser
, u_password
)},
346 PUSH_TYPE(canvas
, struct canvas
)
348 CHECK_TYPE(canvas
, struct canvas
)
351 get_canvas(lua_State
*L
, void *v
)
353 push_canvas(L
, (struct canvas
**)v
);
358 canvas_select(lua_State
*L
)
360 struct canvas
*c
= check_canvas(L
, 1);
361 if (!display
|| D_forecv
== c
)
363 SetCanvasWindow(c
, Layer2Window(c
->c_layer
));
366 /* XXX: the following all is duplicated from process.c:DoAction.
367 * Should these be in some better place?
369 ResizeCanvas(&D_canvas
);
370 RecreateCanvasChain();
371 RethinkDisplayViewports();
372 ResizeLayersToCanvases(); /* redisplays */
373 fore
= D_fore
= Layer2Window(D_forecv
->c_layer
);
374 flayer
= D_forecv
->c_layer
;
376 if (D_xtermosc
[2] || D_xtermosc
[3])
386 flayer
= D_forecv
->c_layer
;
387 CV_CALL(D_forecv
, LayRestore();LaySetCursor());
388 WindowChanged(0, 'F');
392 static const luaL_reg canvas_methods
[] = {
393 {"select", canvas_select
},
397 static const luaL_reg canvas_metamethods
[] = {
401 static const struct Xet_reg canvas_setters
[] = {
406 canvas_get_window(lua_State
*L
)
408 struct canvas
*c
= check_canvas(L
, 1);
409 struct win
*win
= Layer2Window(c
->c_layer
);
411 push_window(L
, &win
);
417 static const struct Xet_reg canvas_getters
[] = {
418 {"next", get_canvas
, offsetof(struct canvas
, c_next
)},
419 {"xoff", get_int
, offsetof(struct canvas
, c_xoff
)},
420 {"yoff", get_int
, offsetof(struct canvas
, c_yoff
)},
421 {"xs", get_int
, offsetof(struct canvas
, c_xs
)},
422 {"ys", get_int
, offsetof(struct canvas
, c_ys
)},
423 {"xe", get_int
, offsetof(struct canvas
, c_xe
)},
424 {"ye", get_int
, offsetof(struct canvas
, c_ye
)},
425 {"window", 0, 0, canvas_get_window
},
433 PUSH_TYPE(layout
, struct layout
)
434 CHECK_TYPE(layout
, struct layout
)
436 static const struct Xet_reg layout_getters
[] = {
441 get_layout(lua_State
*L
, void *v
)
443 push_layout(L
, (struct layout
**)v
);
451 PUSH_TYPE(display
, struct display
)
453 CHECK_TYPE(display
, struct display
)
456 display_get_canvases(lua_State
*L
)
462 d
= check_display(L
, 1);
464 for (iter
= d
->d_cvlist
, count
= 0; iter
; iter
= iter
->c_next
, count
++) {
465 lua_pushinteger(L
, count
);
466 push_canvas(L
, &iter
);
473 static const luaL_reg display_methods
[] = {
474 {"get_canvases", display_get_canvases
},
479 display_tostring(lua_State
*L
)
482 struct display
*d
= check_display(L
, 1);
483 snprintf(str
, sizeof(str
), "{display: tty = '%s', term = '%s'}", d
->d_usertty
, d
->d_termname
);
484 lua_pushstring(L
, str
);
488 static const luaL_reg display_metamethods
[] = {
489 {"__tostring", display_tostring
},
493 static const struct Xet_reg display_setters
[] = {
497 static const struct Xet_reg display_getters
[] = {
498 {"tty", get_string
, offsetof(struct display
, d_usertty
)},
499 {"term", get_string
, offsetof(struct display
, d_termname
)},
500 {"fore", get_window
, offsetof(struct display
, d_fore
)},
501 {"other", get_window
, offsetof(struct display
, d_other
)},
502 {"width", get_int
, offsetof(struct display
, d_width
)},
503 {"height", get_int
, offsetof(struct display
, d_height
)},
504 {"user", get_user
, offsetof(struct display
, d_user
)},
505 {"layout", get_layout
, offsetof(struct display
, d_layout
)},
514 screen_get_windows(lua_State
*L
)
520 for (iter
= windows
, count
= 0; iter
; iter
= iter
->w_next
, count
++) {
521 lua_pushinteger(L
, iter
->w_number
);
522 push_window(L
, &iter
);
530 screen_get_displays(lua_State
*L
)
532 struct display
*iter
;
536 for (iter
= displays
, count
= 0; iter
; iter
= iter
->d_next
, count
++) {
537 lua_pushinteger(L
, count
);
538 push_display(L
, &iter
);
546 screen_get_display(lua_State
*L
)
548 push_display(L
, &display
);
553 screen_exec_command(lua_State
*L
)
558 command
= luaL_checklstring(L
, 1, &len
);
560 RcLine(command
, len
);
566 screen_append_msg(lua_State
*L
)
568 const char *msg
, *color
;
570 msg
= luaL_checklstring(L
, 1, &len
);
574 color
= luaL_checklstring(L
, 2, &len
);
575 AppendWinMsgRend(msg
, color
);
579 static const luaL_reg screen_methods
[] = {
580 {"windows", screen_get_windows
},
581 {"displays", screen_get_displays
},
582 {"display", screen_get_display
},
583 {"command", screen_exec_command
},
584 {"append_msg", screen_append_msg
},
588 static const luaL_reg screen_metamethods
[] = {
592 static const struct Xet_reg screen_setters
[] = {
596 static const struct Xet_reg screen_getters
[] = {
602 /** Public functions {{{ */
610 #define REGISTER(X) struct_register(L, #X , X ## _methods, X##_metamethods, X##_setters, X##_getters)
623 void (*push_fn
)(lua_State
*, void*);
628 LuaCallProcess(const char *name
, struct fn_def defs
[])
633 lua_getfield(L
, LUA_GLOBALSINDEX
, name
);
634 if (lua_isnil(L
, -1))
636 for (argc
= 0; defs
[argc
].push_fn
; argc
++)
637 defs
[argc
].push_fn(L
, defs
[argc
].value
);
638 if (lua_pcall(L
, argc
, 0, 0) == LUA_ERRRUN
&& lua_isstring(L
, -1))
640 struct display
*d
= display
;
642 char *message
= luaL_checklstring(L
, -1, &len
);
643 LMsg(1, "%s", message
? message
: "Unknown error");
651 int LuaForeWindowChanged(void)
655 lua_getfield(L
, LUA_GLOBALSINDEX
, "fore_changed");
656 if (lua_isnil(L
, -1))
658 push_display(L
, &display
);
659 push_window(L
, display
? &D_fore
: &fore
);
660 if (lua_pcall(L
, 2, 0, 0) == LUA_ERRRUN
)
662 if(lua_isstring(L
, -1))
665 char *message
= luaL_checklstring(L
, -1, &len
);
666 LMsg(1, "%s", message
? message
: "Unknown error");
673 int LuaSource(const char *file
)
678 if (stat(file
, &st
) == -1)
679 Msg(errno
, "Error loading lua script file '%s'", file
);
682 int len
= strlen(file
);
683 if (len
< 4 || strncmp(file
+ len
- 4, ".lua", 4) != 0)
685 luaL_dofile(L
, file
);
700 int LuaCall(char **argv
)
706 lua_getfield(L
, LUA_GLOBALSINDEX
, *argv
);
707 for (argc
= 0, argv
++; *argv
; argv
++, argc
++)
709 lua_pushstring(L
, *argv
);
711 if (lua_pcall(L
, argc
, 0, 0) == LUA_ERRRUN
)
713 if(lua_isstring(L
, -1))
716 char *message
= luaL_checklstring(L
, -1, &len
);
717 LMsg(1, "%s", message
? message
: "Unknown error");
726 LuaProcessCaption(const char *caption
, struct win
*win
, int len
)
730 struct fn_def params
[] = {
731 {lua_pushstring
, caption
},
733 {lua_pushinteger
, len
},
736 return LuaCallProcess("process_caption", params
);
740 push_stringarray(lua_State
*L
, void *data
)
742 char **args
= (char **)data
;
745 for (i
= 1; args
&& *args
; i
++) {
746 lua_pushinteger(L
, i
);
747 lua_pushstring(L
, *args
++);
753 LuaCommandExecuted(const char *command
, const char **args
, int argc
)
757 struct fn_def params
[] = {
758 {lua_pushstring
, command
},
759 {push_stringarray
, args
},
762 return LuaCallProcess("command_executed", params
);
767 struct ScriptFuncs LuaFuncs
=
771 LuaForeWindowChanged
,