1 /* adg-lua - lgob based Lua bindings for the ADG canvas
2 * Copyright (C) 2011 Nicola Fontana <ntd at entidi.it>
4 * adg-lua is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation, either version 3 of the License,
7 * or (at your option) any later version.
9 * adg-lua is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with adg-lua. If not, see <http://www.gnu.org/licenses/>.
23 #include <glib-object.h>
24 #include <lgob/common/types.h>
27 static const struct luaL_reg _st__global
[];
28 static const struct luaL_reg adg
[] = {
34 copy_interface(lua_State
*L
, const char *iface
,
35 const char *target
, const luaL_Reg
*fnames
)
37 lua_getfield(L
, -1, iface
);
38 lua_getfield(L
, -2, target
);
40 while (fnames
->name
) {
41 lua_getfield(L
, -2, fnames
->name
);
42 lua_setfield(L
, -2, fnames
->name
);
50 object_new(lua_State
* L
, gpointer ptr
, gboolean constructor
)
52 /* Remove floating reference: we are in lua, not in C */
53 if (g_object_is_floating(ptr
))
54 g_object_ref_sink(ptr
);
56 lua_pushliteral(L
, "lgobObjectNew");
57 lua_rawget(L
, LUA_REGISTRYINDEX
);
58 lua_pushlightuserdata(L
, ptr
);
59 lua_pushboolean(L
, constructor
);
64 register_class(lua_State
*L
, const char *name
,
65 const char *base
, const luaL_Reg
*reg
)
67 lua_pushstring(L
, name
);
69 luaL_register(L
, NULL
, reg
);
73 lua_pushliteral(L
, "__index");
74 lua_pushstring(L
, base
);
77 lua_setmetatable(L
, -2);
84 special_type_new(lua_State
*L
, const gchar
*mt
, gpointer ptr
)
93 object
= lua_newuserdata(L
, sizeof(Object
));
94 object
->pointer
= ptr
;
95 object
->need_unref
= TRUE
;
96 lua_getfield(L
, LUA_REGISTRYINDEX
, mt
);
97 lua_setmetatable(L
, -2);
101 special_gc(lua_State
*L
)
103 Object
*object
= (Object
*) lua_touserdata(L
, 1);
105 if (object
!= NULL
) {
106 g_free(object
->pointer
);
107 object
->pointer
= NULL
;
114 l_checkspecial(lua_State
*L
, int n
)
116 Object
*object
= (Object
*) lua_touserdata(L
, n
);
117 gpointer pointer
= NULL
;
120 pointer
= object
->pointer
;
123 luaL_typerror(L
, n
, "special type");
132 * @n: index in the stack of the AdgPair
134 * Given an AdgPair special object at index n and a key at index n+1,
135 * returns a pointer to the requested field on the provided instance.
136 * An exception is raised on errors or if the field name is invalid.
138 * Returns: a pointer to the field in the AdgPair instance.
141 l_checkpairfield(lua_State
*L
, int n
)
143 AdgPair
*pair
= (AdgPair
*) l_checkspecial(L
, n
);
144 const gchar
*key
= luaL_checkstring(L
, n
+ 1);
146 if (strcmp(key
, "x") == 0) {
148 } else if (strcmp(key
, "y") == 0) {
152 luaL_argerror(L
, n
+ 1, "field not found");
157 l_pair_index(lua_State
*L
)
159 gdouble
*value
= l_checkpairfield(L
, 1);
160 lua_pushnumber(L
, *value
);
165 l_pair_newindex(lua_State
*L
)
167 gdouble
*value
= l_checkpairfield(L
, 1);
168 *value
= lua_tonumber(L
, 3);
173 l_pair_tostring(lua_State
*L
)
175 AdgPair
*pair
= (AdgPair
*) l_checkspecial(L
, 1);
176 lua_pushfstring(L
, "(%f, %f)", pair
->x
, pair
->y
);
182 * l_checkmatrixfield:
184 * @n: index in the stack of the AdgMatrix
186 * Given an AdgMatrix special object at index n and a key at index n+1,
187 * returns a pointer to the requested field on the provided instance.
188 * An exception is raised on errors or if the field name is invalid.
190 * Returns: a pointer to the field in the AdgMatrix instance.
193 l_checkmatrixfield(lua_State
*L
, int n
)
195 AdgMatrix
*matrix
= (AdgMatrix
*) l_checkspecial(L
, n
);
196 const gchar
*key
= luaL_checkstring(L
, n
+ 1);
198 if (strcmp(key
, "xx") == 0) {
200 } else if (strcmp(key
, "yx") == 0) {
202 } else if (strcmp(key
, "xy") == 0) {
204 } else if (strcmp(key
, "yy") == 0) {
206 } else if (strcmp(key
, "x0") == 0) {
208 } else if (strcmp(key
, "y0") == 0) {
212 luaL_argerror(L
, n
+ 1, "field not found");
217 l_matrix_index(lua_State
*L
)
219 gdouble
*value
= l_checkmatrixfield(L
, 1);
220 lua_pushnumber(L
, *value
);
225 l_matrix_newindex(lua_State
*L
)
227 gdouble
*value
= l_checkmatrixfield(L
, 1);
228 *value
= lua_tonumber(L
, 3);
233 l_matrix_tostring(lua_State
*L
)
235 AdgMatrix
*matrix
= (AdgMatrix
*) l_checkspecial(L
, 1);
236 lua_pushfstring(L
, "(%f, %f, %f, %f, %f, %f)",
237 matrix
->xx
, matrix
->yx
,
238 matrix
->xy
, matrix
->yy
,
239 matrix
->x0
, matrix
->y0
);
245 _wrap_adg_init(lua_State
*L
)
247 luaL_register(L
, "adg", adg
);
248 luaL_register(L
, NULL
, _st__global
);
250 luaL_loadstring(L
, "require('lgob.cpml')\n"
251 "require('lgob.gtk')\n"
252 "adg.Object = gobject.Object\n"
253 "adg.InitiallyUnowned = gobject.Object\n");
256 lua_getfield(L
, LUA_REGISTRYINDEX
, "lgobPrefix");
257 lua_pushliteral(L
, "Adg");
258 lua_pushliteral(L
, "adg");
262 luaL_loadstring(L
, "glib.handle_log('Adg')");
267 _wrap_adg_ret(lua_State
*L
)
269 static const luaL_reg pair_metamethods
[] = {
270 { "__index", l_pair_index
},
271 { "__newindex", l_pair_newindex
},
272 { "__tostring", l_pair_tostring
},
273 { "__gc", special_gc
},
276 static const luaL_reg matrix_metamethods
[] = {
277 { "__index", l_matrix_index
},
278 { "__newindex", l_matrix_newindex
},
279 { "__tostring", l_matrix_tostring
},
280 { "__gc", special_gc
},
284 /* Register AdgPair meta methods */
285 luaL_getmetatable(L
, "adgPairMT");
286 luaL_register(L
, NULL
, pair_metamethods
);
289 /* Register AdgMatrix meta methods */
290 luaL_getmetatable(L
, "adgMatrixMT");
291 luaL_register(L
, NULL
, matrix_metamethods
);