summaryrefslogtreecommitdiffstats
path: root/gi/pygi-foreign.c
blob: cde56b082e7cc5036c534cf494aa638d429f06e2 (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
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
/*
 * Copyright (c) 2010  litl, LLC
 * Copyright (c) 2010  Collabora Ltd. <http://www.collabora.co.uk/>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "pygi-foreign.h"

#include <config.h>
#include <girepository.h>

#include "pygi-foreign-cairo.h"

static struct {
    char *namespace;
    char *name;
    PyGIArgOverrideToGArgumentFunc to_func;
    PyGIArgOverrideFromGArgumentFunc from_func;
    PyGIArgOverrideReleaseGArgumentFunc release_func;
} foreign_structs[] = {
    { "cairo", "Context", cairo_context_to_arg, cairo_context_from_arg,
                          cairo_context_release_arg },
    { "cairo", "Surface", cairo_surface_to_arg, cairo_surface_from_arg,
                          cairo_surface_release_arg },
    { NULL }
};

static gint
pygi_struct_foreign_lookup(GITypeInfo *type_info)
{
    GIBaseInfo *base_info;

    base_info = g_type_info_get_interface(type_info);
    if (base_info) {
        gint i;
        const gchar *namespace = g_base_info_get_namespace(base_info);
        const gchar *name = g_base_info_get_name(base_info);

        for (i = 0; foreign_structs[i].namespace; ++i) {

            if ((strcmp(namespace, foreign_structs[i].namespace) == 0) &&
                (strcmp(name, foreign_structs[i].name) == 0)) {
                g_base_info_unref(base_info);
                return i;
            }
        }

        PyErr_Format(PyExc_TypeError, "Couldn't find type %s.%s", namespace,
                     name);

        g_base_info_unref(base_info);
    }
    return -1;
}

gboolean
pygi_struct_foreign_convert_to_g_argument(PyObject      *value,
                                         GITypeInfo     *type_info,
                                         GITransfer      transfer,
                                         GArgument      *arg)
{
    gint struct_index;

    struct_index = pygi_struct_foreign_lookup (type_info);
    if (struct_index < 0)
        return FALSE;

    if (!foreign_structs[struct_index].to_func(value, type_info, transfer, arg))
        return FALSE;

    return TRUE;
}

PyObject *
pygi_struct_foreign_convert_from_g_argument(GITypeInfo *type_info,
                                            GArgument  *arg)
{
    gint struct_index;

    struct_index = pygi_struct_foreign_lookup (type_info);
    if (struct_index < 0)
        return NULL;

    return foreign_structs[struct_index].from_func(type_info, arg);
}

gboolean
pygi_struct_foreign_release_g_argument(GITransfer  transfer,
                                       GITypeInfo *type_info,
                                       GArgument  *arg)
{
    gint struct_index;

    struct_index = pygi_struct_foreign_lookup (type_info);
    if (struct_index < 0)
        return FALSE;

    if (!foreign_structs[struct_index].release_func)
        return TRUE;

   if (!foreign_structs[struct_index].release_func(transfer, type_info, arg))
        return FALSE;

    return TRUE;
}