~ubuntu-branches/ubuntu/natty/lua-gtk/natty

« back to all changes in this revision

Viewing changes to src/gnome/luagnome.h

  • Committer: Bazaar Package Importer
  • Author(s): Enrico Tassi
  • Date: 2009-05-17 18:16:21 UTC
  • mfrom: (1.2.1 upstream) (4.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090517181621-9kmdd82nxg54jsio
* new upstream snapshot comprising many more GNOME libraries:
    Gtk, GDK, GLib, Pango, Atk, Libxml2, Cairo, Clutter, Gtkhtml, 
    GtkSourceView, Gio, Gtkspell and GConf. 
* new upstream release includes a new configure script written in Lua,
  no more bashisms there (Closes: #507205)
* renamed binary packages to liblua5.1-gnome-*
* updated standards-version to 3.8.1, no changes needed
* patch to load .so.* version of libraries and not .so (that was requiring
  -dev packages) (Closes: #522087)
* removed redundant Architecture line from the source stanza of control
  (Closes: #498120)
* updated copyright file, Wolfgang Oertl holds it for 2009 too.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// vim:sw=4:sts=4
 
2
//
 
3
// Include for the core module.  Here are functions and types that are used
 
4
// only within the "gnome" module, without being accessible directly by
 
5
// modules.
 
6
//
 
7
 
 
8
#include "common.h"
 
9
 
 
10
#ifdef LUAGTK_linux
 
11
#include <dlfcn.h>
 
12
#include <ffi.h>                /* foreign function interface library */
 
13
// #define EXPORT
 
14
#endif
 
15
 
 
16
#ifdef LUAGTK_win32
 
17
#include <windows.h>
 
18
#include "ffi.h"
 
19
// #define EXPORT __declspec(dllexport)
 
20
#ifndef __GNUC_PREREQ
 
21
# define __GNUC_PREREQ(maj,min) 0
 
22
#endif
 
23
#endif
 
24
 
 
25
// keys in the gtk module
 
26
extern char *lib_name;
 
27
#define LUAGTK_TBL          lib_name
 
28
#define LUAGTK_METATABLES   "metatables"
 
29
#define LUAGTK_WIDGETS      "objects"
 
30
#define LUAGTK_ALIASES      "aliases"
 
31
#define LUAGTK_EMPTYATTR    "emptyattr"
 
32
 
 
33
// Access to type and structure element names
 
34
#define FTYPE_NAME(t) (gnome_ffi_type_names + (t)->name_ofs)
 
35
 
 
36
extern int module_count;
 
37
extern struct module_info **modules;
 
38
extern const char *thismodulename;
 
39
 
 
40
// structure to store values that were converted from Lua to C (i.e. FFI)
 
41
union gtk_arg_types {
 
42
    void        *p;             // 4 or 8 bytes
 
43
    long        l;
 
44
    long long   ll;             // 8 bytes
 
45
    double      d;
 
46
    float       f;
 
47
    int         i;
 
48
    signed char sc;
 
49
    unsigned char uc;
 
50
};
 
51
 
 
52
 
 
53
 
 
54
 
 
55
/*-
 
56
 * All the lua2ffi_xxx and ffi2lua_xxx functions in types.c get this structure
 
57
 * as argument.  Passing the values individually would just be too much.
 
58
 *
 
59
 * If the function to be called is a Gtk function (Lua to Gtk), then ci->fi
 
60
 * is set.  Otherwise, a Lua function is to be called (as callback from Gtk
 
61
 * to Lua), ci->fi is not set.
 
62
 *
 
63
 * The typespec of the argument is already converted to the function's
 
64
 * module, so ts.module_idx need not be checked.
 
65
 */
 
66
struct argconv_t {
 
67
    lua_State   *L;
 
68
    int         mode;                   // 0=call, 1=callback
 
69
    int         func_arg_nr;            // number of argument to function
 
70
    cmi         mi;                     // module the type belogs to
 
71
    typespec_t  ts;                     // luagtk type of the argument
 
72
    int         arg_flags;              // flags for this argument (see below)
 
73
    int         index;                  // location of the input value
 
74
    union gtk_arg_types *arg;           // location of the output value
 
75
    const struct ffi_type_map_t *arg_type;
 
76
    int         lua_type;               // type of the input value
 
77
    struct call_info *ci;
 
78
    int         stack_top;              // index of last argument
 
79
    int         stack_curr_top;         // see below
 
80
};
 
81
#define ARGCONV_CALL 0
 
82
#define ARGCONV_CALLBACK 1
 
83
// stack_curr_top: While converting the Lua arguments to FFI values, the
 
84
// lua2ffi function shouldn't modify the Lua stack.  This is checked after
 
85
// each call.  When this is required, however, the lua2ffi function must
 
86
// update this variable.
 
87
//
 
88
// arg_flags: set for individual function's arguments in the library's config
 
89
// file.  Bits 0-3 are reserved for the core module, values are listed
 
90
// in script/util.lua.  The other bits can be used per module.
 
91
//
 
92
 
 
93
// argument to lua2struct/struct2lua functions
 
94
struct argconvs_t {
 
95
    lua_State   *L;
 
96
    typespec_t  ts;                     // type of the _structure_
 
97
    const struct struct_elem *se;       // info about the element to r/w
 
98
    unsigned char *ptr;                 // base address of the structure
 
99
    int index;                          // for lua2struct, stack index of input
 
100
};
 
101
 
 
102
typedef int (*lua2ffi_t)(struct argconv_t*);
 
103
typedef int (*ffi2lua_t)(struct argconv_t*);
 
104
typedef int (*lua2struct_t)(struct argconvs_t*);
 
105
typedef int (*struct2lua_t)(struct argconvs_t*);
 
106
 
 
107
/*-
 
108
 * One entry in the type map.  By replacing pointers by indices to separate
 
109
 * tables, the size of each entry is just 34 bits.  Could two bits be removed?
 
110
 */
 
111
struct ffi_type_map_t {
 
112
    unsigned int
 
113
        name_ofs : 10,          // offset into gnome_ffi_type_names (625)
 
114
        bit_len : 9,            // length in bits; max. 256
 
115
        indirections : 2,       // how many levels of pointers? max. 3
 
116
        conv_idx : 5,           // index into ffi_type_lua2ffi/ffi2lua
 
117
        structconv_idx : 4,     // index into ffi_type_lua2struct/struct2lua
 
118
        ffi_type_idx : 4;       // arg for FFI_TYPE() -> ffi_type*
 
119
};
 
120
extern struct ffi_type_map_t ffi_type_map[];
 
121
extern const int ffi_type_count;
 
122
extern const char gnome_ffi_type_names[];
 
123
extern const lua2ffi_t ffi_type_lua2ffi[];
 
124
extern const ffi2lua_t ffi_type_ffi2lua[];
 
125
extern const lua2struct_t ffi_type_lua2struct[];
 
126
extern const struct2lua_t ffi_type_struct2lua[];
 
127
 
 
128
// in types.c
 
129
void lg_empty_table(lua_State *L, int index);
 
130
void get_bits_long(struct argconvs_t *ar, char *dest);
 
131
 
 
132
// in voidptr.c
 
133
struct value_wrapper;
 
134
struct value_wrapper *lg_make_value_wrapper(lua_State *L, int index);
 
135
int lg_push_vwrapper_wrapper(lua_State *L, struct value_wrapper *wrp);
 
136
int lua2ffi_void_ptr(struct argconv_t *ar);
 
137
int lg_is_vwrapper(lua_State *L, void *p);
 
138
void lg_userdata_to_ffi(struct argconv_t *ar, ffi_type **argtype,
 
139
    int only_ptr);
 
140
int lg_vwrapper_get(lua_State *L, struct value_wrapper *wrp);
 
141
 
 
142
// in enum.c
 
143
int lg_push_constant(lua_State *L, typespec_t ts, int value);
 
144
struct lg_enum_t *lg_get_constant(lua_State *L, int index, typespec_t ts,
 
145
    int raise_error);
 
146
 
 
147
/*-
 
148
 * entry (type "userdata") in the meta table of a object.  These entries are
 
149
 * created on the first access to a method or attribute to make later
 
150
 * uses quicker.
 
151
 */
 
152
struct meta_entry {
 
153
    typespec_t ts;                      /* 0=function */
 
154
    union {
 
155
        struct func_info fi;            /* 16 bytes */
 
156
        const struct struct_elem *se;   /* 4 bytes */
 
157
    };
 
158
    typespec_t iface_ts;                /* see below */
 
159
    GType iface_type_id;                /* GType of the interface */
 
160
    char name[0];
 
161
};
 
162
/* iface_*: if the meta_entry refers to a function found in an Interface,
 
163
 * then it can be overridden.  To make this assignment easier, i.e. avoid
 
164
 * searching all interfaces again, the type_idx of the interface is stored
 
165
 * here.  If 0, then this is not a virtual function.
 
166
 */
 
167
 
 
168
/*-
 
169
 * Objects are represented in Lua by this userdata.  It also has a metatable
 
170
 * that contains more information, see gtk2.c:get_object_meta.
 
171
 *
 
172
 * The table "gnome.objects" maps the object address to a reference in a second
 
173
 * table, aliases. These aliases can form a singly linked circular list.
 
174
 * Entries in the object table are not weak; entries in the aliases table are.
 
175
 * Garbage collection of aliases works like this:
 
176
 *
 
177
 *  - get the matching entry in "objects" using the "p" field (pointer)
 
178
 *  - if "next" is not 0, follow until an alias is found whose "next" points
 
179
 *    to the current alias; set its "next" to this "next" or 0
 
180
 *  - if the "first" of the alias entry points here, set it to this "next";
 
181
 *    if this was the last alias, remove the entry in objects.
 
182
 *
 
183
 * mm_type: index into the object_types table with names and pointers to
 
184
 * handler functions; these functions manage refcounting, which differs
 
185
 * between different types of objects.
 
186
 */
 
187
 
 
188
 
 
189
#define OBJECT_NAME(o) lg_get_object_name(o)
 
190
 
 
191
// Max. length of a complete type name
 
192
#define LG_TYPE_NAME_LENGTH 80
 
193
 
 
194
/*-
 
195
 * Information about one argument to a library function, or to a callback.
 
196
 */
 
197
struct call_arg {
 
198
    union gtk_arg_types ffi_arg;                // the actual value
 
199
    unsigned int is_output : 1,                 // set if its an output arg
 
200
        free_method : 8;                        // set if needs to be freed
 
201
};
 
202
#define FREE_METHOD_BOXED 1
 
203
#define FREE_METHOD_GVALUE 2
 
204
 
 
205
 
 
206
/*-
 
207
 * This structure holds all the variables describing a C function call.
 
208
 * Using this structure, the complex call function can be split into multiple
 
209
 * parts easily.  Additionally, elimination of global variables is good
 
210
 * for reentrancy (which isn't required, but still...).
 
211
 *
 
212
 * For each argument, various things have to be given to libffi, mainly the
 
213
 * type of the argument (ffi_type*), a void* to the location of the argument's
 
214
 * value (argvalues) besides other things.  These two have to be arrays and
 
215
 * therefore can't be put into struct call_arg.
 
216
 */
 
217
struct call_info {
 
218
    /* function info */
 
219
    lua_State *L;
 
220
    int index;                  /* stack index of first parameter */
 
221
    struct func_info *fi;
 
222
    int warnings;               /* 0=no warning, 1=warning, 2=traced */
 
223
    int arg_count;              /* number of arguments including the retval */
 
224
    int arg_alloc;              /* number of slots allocated in args */
 
225
 
 
226
    /* arguments. [0] is for the return value */
 
227
    ffi_type **argtypes;
 
228
    void **argvalues;                       /* [0] not used */
 
229
    struct call_arg *args;
 
230
 
 
231
    union {
 
232
        struct call_info_list *first;       /* allocated extra memory */
 
233
        struct call_info *next;             /* chain of free call_infos */
 
234
    };
 
235
};
 
236
 
 
237
// in data.c
 
238
void lg_create_fundamental_map(lua_State *L);
 
239
int lg_register_module(lua_State *L, struct module_info *mi);
 
240
int lg_dl_init(lua_State *L, struct dynlink *dyn);
 
241
int lg_make_func_name(char *buf, int buf_size, const char *class_name,
 
242
    const char *attr_name);
 
243
GType lg_gtype_from_name(lua_State *L, cmi mi, const char *s);
 
244
void lg_get_type_name_full(lua_State *L, typespec_t ts, char *buf);
 
245
const char *lg_get_type_name(typespec_t ts);
 
246
int lg_find_func(lua_State *L, cmi mi, const char *func_name,
 
247
    struct func_info *fi);
 
248
int lg_find_global(lua_State *L, cmi mi, const char *name);
 
249
typespec_t lg_find_struct(lua_State*, const char *type_name, int indir);
 
250
typespec_t lg_get_type(lua_State *L, const char *type_name);
 
251
const struct struct_elem *find_attribute(typespec_t ts, const char *attr_name);
 
252
int lg_find_constant(lua_State *L, typespec_t *ts, const char *key,
 
253
    int keylen, int *result);
 
254
const char *lg_get_object_name(struct object *o);
 
255
int lg_type_equal(lua_State *L, typespec_t ts1, typespec_t ts2);
 
256
type_info_t lg_get_type_info(typespec_t ts);
 
257
const unsigned char *lg_get_prototype(typespec_t ts);
 
258
const char *lg_get_struct_elem_name(int module_idx,
 
259
    const struct struct_elem *se);
 
260
const struct ffi_type_map_t *lg_get_ffi_type(typespec_t ts);
 
261
void *lg_optional_func(lua_State *L, cmi mi, const char *name,
 
262
    const char *min_version);
 
263
typespec_t lg_type_normalize(lua_State *L, typespec_t ts);
 
264
struct object *lg_object_arg(lua_State *L, int index, const char *name);
 
265
/* cmi lg_get_module(lua_State *L, const char *module_name); */
 
266
int lg_get_type_indirections(typespec_t ts);
 
267
typespec_t lg_type_modify(lua_State *L, typespec_t ts, int ind_delta);
 
268
 
 
269
// in debug.c
 
270
void lg_init_debug(lua_State *L);
 
271
int lg_debug_flags_global(lua_State *L);
 
272
int lg_breakfunc(lua_State *L);
 
273
int lg_object_tostring(lua_State *L);
 
274
void lg_call_trace(lua_State *L, struct func_info *fi, int index);
 
275
 
 
276
extern int runtime_flags;
 
277
 
 
278
// in object.c
 
279
void lg_get_object(lua_State *L, void *p, typespec_t ts, int flags);
 
280
struct object *lg_check_object(lua_State *L, int index);
 
281
void lg_invalidate_object(lua_State *L, struct object *w);
 
282
 
 
283
// in object_types.c
 
284
void lg_init_object(lua_State *L);
 
285
void lg_guess_object_type(lua_State *L, struct object *w, int flags);
 
286
int lg_register_object_type(const char *name, object_handler handler);
 
287
int lg_find_object_type(const char *name);
 
288
int lg_get_refcount(lua_State *L, struct object *w);
 
289
void lg_inc_refcount(lua_State *L, struct object *w, int flags);
 
290
void lg_dec_refcount(lua_State *L, struct object *w);
 
291
struct object_type *lg_get_object_type(lua_State *L, struct object *w);
 
292
 
 
293
// in object_meta.c
 
294
int lg_object_index(lua_State *L);
 
295
int lg_object_newindex(lua_State *L);
 
296
 
 
297
 
 
298
// in init.c
 
299
extern const char msgprefix[];
 
300
int lg_push_closure(lua_State *L, const struct func_info *fi, int alloc_fi);
 
301
// int lg_argerror(lua_State *L, int narg, const char *format, ...);
 
302
struct func_info *lg_get_closure(lua_State *L, int index);
 
303
 
 
304
// in boxed.c
 
305
extern int lg_boxed_value_type;
 
306
void    lg_init_boxed(lua_State *L);
 
307
void*   lg_make_boxed_value(lua_State *L, int index);
 
308
int     lg_get_boxed_value(lua_State *L, const void *p);
 
309
void    lg_boxed_to_ffi(struct argconv_t *ar, ffi_type **argtype);
 
310
void    lg_boxed_free(gpointer val);
 
311
 
 
312
// in call.c
 
313
enum lg_msg_level { LUAGTK_DEBUG=0, LUAGTK_INFO, LUAGTK_WARNING, LUAGTK_ERROR };
 
314
int lg_call(lua_State *L, struct func_info *fi, int index);
 
315
int lg_call_byname(lua_State *L, cmi mi, const char *func_name);
 
316
int lg_call_function(lua_State *L, const char *mod_name, const char *func_name);
 
317
void call_info_warn(struct call_info *ci);
 
318
void call_info_msg(lua_State *L, struct call_info *ci, enum lg_msg_level level);
 
319
struct call_info *call_info_alloc();
 
320
void *call_info_alloc_item(struct call_info *ci, int size);
 
321
void call_info_check_argcount(struct call_info *ci, int n);
 
322
void call_info_free(struct call_info *ci);
 
323
void call_info_free_pool();
 
324
inline void get_next_argument(lua_State *L, const unsigned char **p,
 
325
    struct argconv_t *ar);
 
326
 
 
327
// closure.c
 
328
void lg_init_closure(lua_State *L);
 
329
void lg_done_closure();
 
330
int lg_create_closure(lua_State *L, int index, int is_automatic);
 
331
void *lg_use_closure(lua_State *L, int index, typespec_t ts,
 
332
    int arg_nr, const char *func_name);
 
333
int lg_use_c_closure(struct argconv_t *ar);
 
334
 
 
335
// gvalue.c
 
336
void lg_lua_to_gvalue_cast(lua_State *L, int index, GValue *gv, GType gtype);
 
337
GValue *lg_lua_to_gvalue(lua_State *L, int index, GValue *gvalue);
 
338
void lg_gvalue_to_lua(lua_State *L, GValue *gv);
 
339