86da07f27183ccb9054b86b4aa3d83b6e158fba3
[platform/upstream/pygobject2.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 typedef struct {
36     PyObject_HEAD
37     GIRepository *repository;
38 } PyGIRepository;
39
40 typedef struct {
41     PyObject_HEAD
42     GIBaseInfo *info;
43     PyObject *inst_weakreflist;
44     PyGICallableCache *cache;
45 } PyGIBaseInfo;
46
47 typedef struct {
48     PyGPointer base;
49     gboolean free_on_dealloc;
50 } PyGIStruct;
51
52 typedef struct {
53     PyGBoxed base;
54     gboolean slice_allocated;
55     gsize size;
56 } PyGIBoxed;
57
58 typedef struct {
59     PyObject_HEAD
60     GCallback callback;
61     GIFunctionInfo *info;
62     gpointer user_data;
63     GIScopeType scope;
64     GDestroyNotify destroy_notify_func;
65     PyGICallableCache *cache;
66 } PyGICCallback;
67
68 typedef PyObject * (*PyGIArgOverrideToGIArgumentFunc) (PyObject        *value,
69                                                        GIInterfaceInfo *interface_info,
70                                                        GITransfer       transfer,
71                                                        GIArgument      *arg);
72 typedef PyObject * (*PyGIArgOverrideFromGIArgumentFunc) (GIInterfaceInfo *interface_info,
73                                                          gpointer         data);
74 typedef PyObject * (*PyGIArgOverrideReleaseFunc) (GITypeInfo *type_info,
75                                                   gpointer  struct_);
76
77 struct PyGI_API {
78     PyObject* (*type_import_by_g_type) (GType g_type);
79     PyObject* (*get_property_value) (PyGObject *instance,
80                                      GParamSpec *pspec);
81     gint (*set_property_value) (PyGObject *instance,
82                                 GParamSpec *pspec,
83                                 PyObject *value);
84     GClosure * (*signal_closure_new) (PyGObject *instance,
85                                       const gchar *sig_name,
86                                       PyObject *callback,
87                                       PyObject *extra_args,
88                                       PyObject *swap_data);
89     void (*register_foreign_struct) (const char* namespace_,
90                                      const char* name,
91                                      PyGIArgOverrideToGIArgumentFunc to_func,
92                                      PyGIArgOverrideFromGIArgumentFunc from_func,
93                                      PyGIArgOverrideReleaseFunc release_func);
94 };
95
96 static struct PyGI_API *PyGI_API = NULL;
97
98 static int
99 _pygi_import (void)
100 {
101     if (PyGI_API != NULL) {
102         return 1;
103     }
104 #if PY_VERSION_HEX >= 0x03000000
105     PyGI_API = (struct PyGI_API*) PyCapsule_Import("gi._API", FALSE);
106 #else
107     PyGI_API = (struct PyGI_API*) PyCObject_Import("gi", "_API");
108 #endif
109     if (PyGI_API == NULL) {
110         return -1;
111     }
112
113     return 0;
114 }
115
116 static inline PyObject *
117 pygi_type_import_by_g_type (GType g_type)
118 {
119     if (_pygi_import() < 0) {
120         return NULL;
121     }
122     return PyGI_API->type_import_by_g_type(g_type);
123 }
124
125 static inline PyObject *
126 pygi_get_property_value (PyGObject *instance,
127                          GParamSpec *pspec)
128 {
129     if (_pygi_import() < 0) {
130         return NULL;
131     }
132     return PyGI_API->get_property_value(instance, pspec);
133 }
134
135 static inline gint
136 pygi_set_property_value (PyGObject *instance,
137                          GParamSpec *pspec,
138                          PyObject *value)
139 {
140     if (_pygi_import() < 0) {
141         return -1;
142     }
143     return PyGI_API->set_property_value(instance, pspec, value);
144 }
145
146 static inline GClosure *
147 pygi_signal_closure_new (PyGObject *instance,
148                          const gchar *sig_name,
149                          PyObject *callback,
150                          PyObject *extra_args,
151                          PyObject *swap_data)
152 {
153     if (_pygi_import() < 0) {
154         return NULL;
155     }
156     return PyGI_API->signal_closure_new(instance, sig_name, callback, extra_args, swap_data);
157 }
158
159 static inline PyObject *
160 pygi_register_foreign_struct (const char* namespace_,
161                               const char* name,
162                               PyGIArgOverrideToGIArgumentFunc to_func,
163                               PyGIArgOverrideFromGIArgumentFunc from_func,
164                               PyGIArgOverrideReleaseFunc release_func)
165 {
166     if (_pygi_import() < 0) {
167         return NULL;
168     }
169     PyGI_API->register_foreign_struct(namespace_,
170                                       name,
171                                       to_func,
172                                       from_func,
173                                       release_func);
174     Py_RETURN_NONE;
175 }
176
177 #endif /* __PYGI_H__ */