summaryrefslogtreecommitdiff
path: root/gi/pygi.h
blob: 3a1591f8e8234d6d428b824e8fb51e0ce565c82a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* -*- Mode: C; c-basic-offset: 4 -*-
 * vim: tabstop=4 shiftwidth=4 expandtab
 *
 * Copyright (C) 2005-2009 Johan Dahlin <johan@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
 * USA
 */

#ifndef __PYGI_H__
#define __PYGI_H__

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <pygobject-private.h>

#include <girepository.h>
#include "pygi-cache.h"

extern PyObject *PyGIDeprecationWarning;
extern PyObject *_PyGIDefaultArgPlaceholder;

typedef struct {
    PyObject_HEAD
    GIRepository *repository;
} PyGIRepository;

typedef struct {
    PyObject_HEAD
    GIBaseInfo *info;
    PyObject *inst_weakreflist;
    PyGICallableCache *cache;
} PyGIBaseInfo;

typedef struct {
    PyGIBaseInfo base;

    /* Reference the unbound version of this struct.
     * We use this for the actual call to invoke because it manages the cache.
     */
    struct PyGICallableInfo *py_unbound_info;

    /* Holds bound argument for instance, class, and vfunc methods. */
    PyObject *py_bound_arg;

} PyGICallableInfo;

typedef struct {
    PyGPointer base;
    gboolean free_on_dealloc;
} PyGIStruct;

typedef struct {
    PyGBoxed base;
    gboolean slice_allocated;
    gsize size;
} PyGIBoxed;

typedef struct {
    PyObject_HEAD
    GCallback callback;
    GIFunctionInfo *info;
    gpointer user_data;
    GIScopeType scope;
    GDestroyNotify destroy_notify_func;
    PyGICallableCache *cache;
} PyGICCallback;

typedef PyObject * (*PyGIArgOverrideToGIArgumentFunc) (PyObject        *value,
                                                       GIInterfaceInfo *interface_info,
                                                       GITransfer       transfer,
                                                       GIArgument      *arg);
typedef PyObject * (*PyGIArgOverrideFromGIArgumentFunc) (GIInterfaceInfo *interface_info,
                                                         gpointer         data);
typedef PyObject * (*PyGIArgOverrideReleaseFunc) (GITypeInfo *type_info,
                                                  gpointer  struct_);

struct PyGI_API {
    PyObject* (*type_import_by_g_type) (GType g_type);
    PyObject* (*get_property_value) (PyGObject *instance,
                                     GParamSpec *pspec);
    gint (*set_property_value) (PyGObject *instance,
                                GParamSpec *pspec,
                                PyObject *value);
    GClosure * (*signal_closure_new) (PyGObject *instance,
                                      GType g_type,
                                      const gchar *sig_name,
                                      PyObject *callback,
                                      PyObject *extra_args,
                                      PyObject *swap_data);
    void (*register_foreign_struct) (const char* namespace_,
                                     const char* name,
                                     PyGIArgOverrideToGIArgumentFunc to_func,
                                     PyGIArgOverrideFromGIArgumentFunc from_func,
                                     PyGIArgOverrideReleaseFunc release_func);
};

static struct PyGI_API *PyGI_API = NULL;

static int
_pygi_import (void)
{
    if (PyGI_API != NULL) {
        return 1;
    }
    PyGI_API = (struct PyGI_API*) PyCapsule_Import("gi._API", FALSE);
    if (PyGI_API == NULL) {
        return -1;
    }

    return 0;
}

static inline PyObject *
pygi_type_import_by_g_type (GType g_type)
{
    if (_pygi_import() < 0) {
        return NULL;
    }
    return PyGI_API->type_import_by_g_type(g_type);
}

static inline PyObject *
pygi_get_property_value (PyGObject *instance,
                         GParamSpec *pspec)
{
    if (_pygi_import() < 0) {
        return NULL;
    }
    return PyGI_API->get_property_value(instance, pspec);
}

static inline gint
pygi_set_property_value (PyGObject *instance,
                         GParamSpec *pspec,
                         PyObject *value)
{
    if (_pygi_import() < 0) {
        return -1;
    }
    return PyGI_API->set_property_value(instance, pspec, value);
}

static inline GClosure *
pygi_signal_closure_new (PyGObject *instance,
                         GType g_type,
                         const gchar *sig_name,
                         PyObject *callback,
                         PyObject *extra_args,
                         PyObject *swap_data)
{
    if (_pygi_import() < 0) {
        return NULL;
    }
    return PyGI_API->signal_closure_new(instance, g_type, sig_name, callback, extra_args, swap_data);
}

static inline PyObject *
pygi_register_foreign_struct (const char* namespace_,
                              const char* name,
                              PyGIArgOverrideToGIArgumentFunc to_func,
                              PyGIArgOverrideFromGIArgumentFunc from_func,
                              PyGIArgOverrideReleaseFunc release_func)
{
    if (_pygi_import() < 0) {
        return NULL;
    }
    PyGI_API->register_foreign_struct(namespace_,
                                      name,
                                      to_func,
                                      from_func,
                                      release_func);
    Py_RETURN_NONE;
}

#endif /* __PYGI_H__ */