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>
40 extern struct win
*windows
, *fore
;
41 extern struct display
*displays
, *display
;
42 extern struct LayFuncs WinLf
;
43 extern struct layer
*flayer
;
47 #define CHECK_TYPE(name, type) \
49 check_##name(lua_State *L, int index) \
52 luaL_checktype(L, index, LUA_TUSERDATA); \
53 var = (type *) luaL_checkudata(L, index, #name); \
55 luaL_typerror(L, index, #name); \
59 #define PUSH_TYPE(name, type) \
61 push_##name(lua_State *L, type *t) \
68 r = (type *)lua_newuserdata(L, sizeof(type)); \
70 luaL_getmetatable(L, #name); \
71 lua_setmetatable(L,-2); \
75 /* Much of the following template comes from:
76 * http://lua-users.org/wiki/BindingWithMembersAndMethods
79 static int get_int (lua_State
*L
, void *v
)
81 lua_pushinteger (L
, *(int*)v
);
85 static int set_int (lua_State
*L
, void *v
)
87 *(int*)v
= luaL_checkint(L
, 3);
91 static int get_number (lua_State
*L
, void *v
)
93 lua_pushnumber(L
, *(lua_Number
*)v
);
97 static int set_number (lua_State
*L
, void *v
)
99 *(lua_Number
*)v
= luaL_checknumber(L
, 3);
103 static int get_string (lua_State
*L
, void *v
)
105 lua_pushstring(L
, (char*)v
);
109 static int set_string (lua_State
*L
, void *v
)
111 *(const char**)v
= luaL_checkstring(L
, 3);
115 typedef int (*Xet_func
) (lua_State
*L
, void *v
);
117 /* member info for get and set handlers */
120 const char *name
; /* member name */
121 Xet_func func
; /* get or set function for type of member */
122 size_t offset
; /* offset of member within the struct */
123 int (*absolute
)(lua_State
*);
126 static void Xet_add (lua_State
*L
, const struct Xet_reg
*l
)
132 lua_pushstring(L
, l
->name
);
133 lua_pushlightuserdata(L
, (void*)l
);
138 static int Xet_call (lua_State
*L
)
140 /* for get: stack has userdata, index, lightuserdata */
141 /* for set: stack has userdata, index, value, lightuserdata */
142 const struct Xet_reg
*m
= (const struct Xet_reg
*)lua_touserdata(L
, -1); /* member info */
143 lua_pop(L
, 1); /* drop lightuserdata */
144 luaL_checktype(L
, 1, LUA_TUSERDATA
);
146 return m
->absolute(L
);
147 return m
->func(L
, lua_touserdata(L
, 1) + m
->offset
);
150 static int index_handler (lua_State
*L
)
152 /* stack has userdata, index */
153 lua_pushvalue(L
, 2); /* dup index */
154 lua_rawget(L
, lua_upvalueindex(1)); /* lookup member by name */
155 if (!lua_islightuserdata(L
, -1))
157 lua_pop(L
, 1); /* drop value */
158 lua_pushvalue(L
, 2); /* dup index */
159 lua_gettable(L
, lua_upvalueindex(2)); /* else try methods */
160 if (lua_isnil(L
, -1)) /* invalid member */
161 luaL_error(L
, "cannot get member '%s'", lua_tostring(L
, 2));
164 return Xet_call(L
); /* call get function */
167 static int newindex_handler (lua_State
*L
)
169 /* stack has userdata, index, value */
170 lua_pushvalue(L
, 2); /* dup index */
171 lua_rawget(L
, lua_upvalueindex(1)); /* lookup member by name */
172 if (!lua_islightuserdata(L
, -1)) /* invalid member */
173 luaL_error(L
, "cannot set member '%s'", lua_tostring(L
, 2));
174 return Xet_call(L
); /* call set function */
178 struct_register(lua_State
*L
, const char *name
, const luaL_reg fn_methods
[], const luaL_reg meta_methods
[],
179 const struct Xet_reg setters
[], const struct Xet_reg getters
[])
181 int metatable
, methods
;
183 /* create methods table & add it to the table of globals */
184 luaL_register(L
, name
, fn_methods
);
185 methods
= lua_gettop(L
);
187 /* create metatable & add it to the registry */
188 luaL_newmetatable(L
, name
);
189 luaL_register(L
, 0, meta_methods
); /* fill metatable */
190 metatable
= lua_gettop(L
);
192 lua_pushliteral(L
, "__metatable");
193 lua_pushvalue(L
, methods
); /* dup methods table*/
194 lua_rawset(L
, metatable
); /* hide metatable:
195 metatable.__metatable = methods */
197 lua_pushliteral(L
, "__index");
198 lua_pushvalue(L
, metatable
); /* upvalue index 1 */
199 Xet_add(L
, getters
); /* fill metatable with getters */
200 lua_pushvalue(L
, methods
); /* upvalue index 2 */
201 lua_pushcclosure(L
, index_handler
, 2);
202 lua_rawset(L
, metatable
); /* metatable.__index = index_handler */
204 lua_pushliteral(L
, "__newindex");
205 lua_newtable(L
); /* table for members you can set */
206 Xet_add(L
, setters
); /* fill with setters */
207 lua_pushcclosure(L
, newindex_handler
, 1);
208 lua_rawset(L
, metatable
); /* metatable.__newindex = newindex_handler */
210 lua_pop(L
, 1); /* drop metatable */
211 return 1; /* return methods on the stack */
218 PUSH_TYPE(window
, struct win
)
220 CHECK_TYPE(window
, struct win
)
222 static int get_window(lua_State
*L
, void *v
)
224 push_window(L
, *(struct win
**)v
);
229 window_change_title(lua_State
*L
)
231 struct win
*w
= check_window(L
, 1);
233 const char *title
= luaL_checklstring(L
, 2, &len
);
234 ChangeAKA(w
, title
, len
);
238 static const luaL_reg window_methods
[] = {
239 {"change_title", window_change_title
},
244 window_tostring(lua_State
*L
)
247 struct win
*w
= check_window(L
, 1);
248 snprintf(str
, sizeof(str
), "{window #%d: '%s'}", w
->w_number
, w
->w_title
);
249 lua_pushstring(L
, str
);
253 static const luaL_reg window_metamethods
[] = {
254 {"__tostring", window_tostring
},
258 static const struct Xet_reg window_setters
[] = {
262 static const struct Xet_reg window_getters
[] = {
263 {"title", get_string
, offsetof(struct win
, w_title
) + 8},
264 {"number", get_int
, offsetof(struct win
, w_number
)},
265 {"dir", get_string
, offsetof(struct win
, w_dir
)},
266 {"tty", get_string
, offsetof(struct win
, w_tty
)},
267 {"pid", get_int
, offsetof(struct win
, w_pid
)},
268 {"group", get_window
, offsetof(struct win
, w_group
)},
269 {"bell", get_int
, offsetof(struct win
, w_bell
)},
270 {"monitor", get_int
, offsetof(struct win
, w_monitor
)},
279 PUSH_TYPE(user
, struct acluser
)
281 CHECK_TYPE(user
, struct acluser
)
284 get_user(lua_State
*L
, void *v
)
286 push_user(L
, *(struct acluser
**)v
);
290 static const luaL_reg user_methods
[] = {
295 user_tostring(lua_State
*L
)
298 struct acluser
*u
= check_user(L
, 1);
299 snprintf(str
, sizeof(str
), "{user '%s'}", u
->u_name
);
300 lua_pushstring(L
, str
);
304 static const luaL_reg user_metamethods
[] = {
305 {"__tostring", user_tostring
},
309 static const struct Xet_reg user_setters
[] = {
313 static const struct Xet_reg user_getters
[] = {
314 {"name", get_string
, offsetof(struct acluser
, u_name
)},
315 {"password", get_string
, offsetof(struct acluser
, u_password
)},
323 PUSH_TYPE(canvas
, struct canvas
)
325 CHECK_TYPE(canvas
, struct canvas
)
328 get_canvas(lua_State
*L
, void *v
)
330 push_canvas(L
, *(struct canvas
**)v
);
335 canvas_select(lua_State
*L
)
337 struct canvas
*c
= check_canvas(L
, 1);
338 if (!display
|| D_forecv
== c
)
342 /* XXX: the following all is duplicated from process.c:DoAction.
343 * Should these be in some better place?
345 ResizeCanvas(&D_canvas
);
346 RecreateCanvasChain();
347 RethinkDisplayViewports();
348 ResizeLayersToCanvases(); /* redisplays */
349 fore
= D_fore
= Layer2Window(D_forecv
->c_layer
);
350 flayer
= D_forecv
->c_layer
;
352 if (D_xtermosc
[2] || D_xtermosc
[3])
362 flayer
= D_forecv
->c_layer
;
363 CV_CALL(D_forecv
, LayRestore();LaySetCursor());
364 WindowChanged(0, 'F');
367 static const luaL_reg canvas_methods
[] = {
368 {"select", canvas_select
},
372 static const luaL_reg canvas_metamethods
[] = {
376 static const struct Xet_reg canvas_setters
[] = {
381 canvas_get_window(lua_State
*L
)
383 struct canvas
*c
= check_canvas(L
, 1);
384 struct win
*win
= Layer2Window(c
->c_layer
);
392 static const struct Xet_reg canvas_getters
[] = {
393 {"next", get_canvas
, offsetof(struct canvas
, c_next
)},
394 {"xoff", get_int
, offsetof(struct canvas
, c_xoff
)},
395 {"yoff", get_int
, offsetof(struct canvas
, c_yoff
)},
396 {"xs", get_int
, offsetof(struct canvas
, c_xs
)},
397 {"ys", get_int
, offsetof(struct canvas
, c_ys
)},
398 {"xe", get_int
, offsetof(struct canvas
, c_xe
)},
399 {"ye", get_int
, offsetof(struct canvas
, c_ye
)},
400 {"window", 0, 0, canvas_get_window
},
408 PUSH_TYPE(display
, struct display
)
410 CHECK_TYPE(display
, struct display
)
413 display_get_canvases(lua_State
*L
)
419 d
= check_display(L
, 1);
420 for (iter
= d
->d_cvlist
, count
= 0; iter
; iter
= iter
->c_next
, count
++)
421 push_canvas(L
, iter
);
426 static const luaL_reg display_methods
[] = {
427 {"get_canvases", display_get_canvases
},
432 display_tostring(lua_State
*L
)
435 struct display
*d
= check_display(L
, 1);
436 snprintf(str
, sizeof(str
), "{display: tty = '%s', term = '%s'}", d
->d_usertty
, d
->d_termname
);
437 lua_pushstring(L
, str
);
441 static const luaL_reg display_metamethods
[] = {
442 {"__tostring", display_tostring
},
446 static const struct Xet_reg display_setters
[] = {
450 static const struct Xet_reg display_getters
[] = {
451 {"tty", get_string
, offsetof(struct display
, d_usertty
)},
452 {"term", get_string
, offsetof(struct display
, d_termname
)},
453 {"fore", get_window
, offsetof(struct display
, d_fore
)},
454 {"other", get_window
, offsetof(struct display
, d_other
)},
455 {"width", get_int
, offsetof(struct display
, d_width
)},
456 {"height", get_int
, offsetof(struct display
, d_height
)},
457 {"user", get_user
, offsetof(struct display
, d_user
)},
466 screen_get_windows(lua_State
*L
)
471 for (iter
= windows
, count
= 0; iter
; iter
= iter
->w_next
, count
++)
472 push_window(L
, iter
);
478 screen_get_displays(lua_State
*L
)
480 struct display
*iter
;
483 for (iter
= displays
, count
= 0; iter
; iter
= iter
->d_next
, count
++)
484 push_display(L
, iter
);
490 screen_get_display(lua_State
*L
)
492 push_display(L
, display
);
497 screen_exec_command(lua_State
*L
)
502 command
= luaL_checklstring(L
, 1, &len
);
504 RcLine(command
, len
);
509 static const luaL_reg screen_methods
[] = {
510 {"windows", screen_get_windows
},
511 {"displays", screen_get_displays
},
512 {"display", screen_get_display
},
513 {"command", screen_exec_command
},
517 static const luaL_reg screen_metamethods
[] = {
521 static const struct Xet_reg screen_setters
[] = {
525 static const struct Xet_reg screen_getters
[] = {
531 /** Public functions {{{ */
539 #define REGISTER(X) struct_register(L, #X , X ## _methods, X##_metamethods, X##_setters, X##_getters)
550 int LuaForeWindowChanged(void)
554 lua_getfield(L
, LUA_GLOBALSINDEX
, "fore_changed");
555 if (lua_isnil(L
, -1))
557 push_display(L
, display
);
558 push_window(L
, display
? D_fore
: fore
);
559 if (lua_pcall(L
, 2, 0, 0) == LUA_ERRRUN
)
561 if(lua_isstring(L
, -1))
564 char *message
= luaL_checklstring(L
, -1, &len
);
565 LMsg(1, "%s", message
? message
: "Unknown error");
572 int LuaSource(const char *file
)
577 if (stat(file
, &st
) == -1)
578 Msg(errno
, "Error loading lua script file '%s'", file
);
581 int len
= strlen(file
);
582 if (len
< 4 || strncmp(file
+ len
- 4, ".lua", 4) != 0)
584 luaL_dofile(L
, file
);
599 int LuaCall(char **argv
)
605 lua_getfield(L
, LUA_GLOBALSINDEX
, *argv
);
606 for (argc
= 0, argv
++; *argv
; argv
++, argc
++)
608 lua_pushstring(L
, *argv
);
610 if (lua_pcall(L
, argc
, 0, 0) == LUA_ERRRUN
)
612 if(lua_isstring(L
, -1))
615 char *message
= luaL_checklstring(L
, -1, &len
);
616 LMsg(1, "%s", message
? message
: "Unknown error");
627 struct ScriptFuncs LuaFuncs
=
631 LuaForeWindowChanged
,