Imported Upstream version 3.9.91
[platform/upstream/python-gobject.git] / gi / pygi.h
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * vim: tabstop=4 shiftwidth=4 expandtab
3  *
4  * Copyright (C) 2005-2009 Johan Dahlin <johan@gnome.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19  * USA
20  */
21
22 #ifndef __PYGI_H__
23 #define __PYGI_H__
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #define NO_IMPORT_PYGOBJECT
30 #include <pygobject.h>
31
32 #include <girepository.h>
33 #include "pygi-cache.h"
34
35 extern PyObject *PyGIDeprecationWarning;
36
37 typedef struct {
38     PyObject_HEAD
39     GIRepository *repository;
40 } PyGIRepository;
41
42 typedef struct {
43     PyObject_HEAD
44     GIBaseInfo *info;
45     PyObject *inst_weakreflist;
46     PyGICallableCache *cache;
47 } PyGIBaseInfo;
48
49 typedef struct {
50     PyGIBaseInfo base;
51
52     /* Reference the unbound version of this struct.
53      * We use this for the actual call to invoke because it manages the cache.
54      */
55     struct PyGICallableInfo *py_unbound_info;
56
57     /* Holds bound argument for instance, class, and vfunc methods. */
58     PyObject *py_bound_arg;
59
60 } PyGICallableInfo;
61
62 typedef struct {
63     PyGPointer base;
64     gboolean free_on_dealloc;
65 } PyGIStruct;
66
67 typedef struct {
68     PyGBoxed base;
69     gboolean slice_allocated;
70     gsize size;
71 } PyGIBoxed;
72
73 typedef struct {
74     PyObject_HEAD
75     GCallback callback;
76     GIFunctionInfo *info;
77     gpointer user_data;
78     GIScopeType scope;
79     GDestroyNotify destroy_notify_func;
80     PyGICallableCache *cache;
81 } PyGICCallback;
82
83 typedef PyObject * (*PyGIArgOverrideToGIArgumentFunc) (PyObject        *value,
84                                                        GIInterfaceInfo *interface_info,
85                                                        GITransfer       transfer,
86                                                        GIArgument      *arg);
87 typedef PyObject * (*PyGIArgOverrideFromGIArgumentFunc) (GIInterfaceInfo *interface_info,
88                                                          gpointer         data);
89 typedef PyObject * (*PyGIArgOverrideReleaseFunc) (GITypeInfo *type_info,
90                                                   gpointer  struct_);
91
92 struct PyGI_API {
93     PyObject* (*type_import_by_g_type) (GType g_type);
94     PyObject* (*get_property_value) (PyGObject *instance,
95                                      GParamSpec *pspec);
96     gint (*set_property_value) (PyGObject *instance,
97                                 GParamSpec *pspec,
98                                 PyObject *value);
99     GClosure * (*signal_closure_new) (PyGObject *instance,
100                                       GType g_type,
101                                       const gchar *sig_name,
102                                       PyObject *callback,
103                                       PyObject *extra_args,
104                                       PyObject *swap_data);
105     void (*register_foreign_struct) (const char* namespace_,
106                                      const char* name,
107                                      PyGIArgOverrideToGIArgumentFunc to_func,
108                                      PyGIArgOverrideFromGIArgumentFunc from_func,
109                                      PyGIArgOverrideReleaseFunc release_func);
110 };
111
112 static struct PyGI_API *PyGI_API = NULL;
113
114 static int
115 _pygi_import (void)
116 {
117     if (PyGI_API != NULL) {
118         return 1;
119     }
120     PyGI_API = (struct PyGI_API*) PyCapsule_Import("gi._API", FALSE);
121     if (PyGI_API == NULL) {
122         return -1;
123     }
124
125     return 0;
126 }
127
128 static inline PyObject *
129 pygi_type_import_by_g_type (GType g_type)
130 {
131     if (_pygi_import() < 0) {
132         return NULL;
133     }
134     return PyGI_API->type_import_by_g_type(g_type);
135 }
136
137 static inline PyObject *
138 pygi_get_property_value (PyGObject *instance,
139                          GParamSpec *pspec)
140 {
141     if (_pygi_import() < 0) {
142         return NULL;
143     }
144     return PyGI_API->get_property_value(instance, pspec);
145 }
146
147 static inline gint
148 pygi_set_property_value (PyGObject *instance,
149                          GParamSpec *pspec,
150                          PyObject *value)
151 {
152     if (_pygi_import() < 0) {
153         return -1;
154     }
155     return PyGI_API->set_property_value(instance, pspec, value);
156 }
157
158 static inline GClosure *
159 pygi_signal_closure_new (PyGObject *instance,
160                          GType g_type,
161                          const gchar *sig_name,
162                          PyObject *callback,
163                          PyObject *extra_args,
164                          PyObject *swap_data)
165 {
166     if (_pygi_import() < 0) {
167         return NULL;
168     }
169     return PyGI_API->signal_closure_new(instance, g_type, sig_name, callback, extra_args, swap_data);
170 }
171
172 static inline PyObject *
173 pygi_register_foreign_struct (const char* namespace_,
174                               const char* name,
175                               PyGIArgOverrideToGIArgumentFunc to_func,
176                               PyGIArgOverrideFromGIArgumentFunc from_func,
177                               PyGIArgOverrideReleaseFunc release_func)
178 {
179     if (_pygi_import() < 0) {
180         return NULL;
181     }
182     PyGI_API->register_foreign_struct(namespace_,
183                                       name,
184                                       to_func,
185                                       from_func,
186                                       release_func);
187     Py_RETURN_NONE;
188 }
189
190 #endif /* __PYGI_H__ */