tests: don't test for specific device labels
[pygobject.git] / gi / pygi-foreign.c
blob7db28fd785a5887918ae6bfae459312cc11443d7
1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /*
3 * Copyright (c) 2010 litl, LLC
4 * Copyright (c) 2010 Collabora Ltd. <http://www.collabora.co.uk/>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
25 #include <config.h>
27 #include "pygobject-internal.h"
28 #include "pygi-foreign.h"
30 #include <girepository.h>
32 typedef struct {
33 const char *namespace;
34 const char *name;
35 PyGIArgOverrideToGIArgumentFunc to_func;
36 PyGIArgOverrideFromGIArgumentFunc from_func;
37 PyGIArgOverrideReleaseFunc release_func;
38 } PyGIForeignStruct;
40 static GPtrArray *foreign_structs = NULL;
42 static void
43 init_foreign_structs (void)
45 foreign_structs = g_ptr_array_new ();
48 static PyGIForeignStruct *
49 do_lookup (const gchar *namespace, const gchar *name)
51 guint i;
52 for (i = 0; i < foreign_structs->len; i++) {
53 PyGIForeignStruct *foreign_struct = \
54 g_ptr_array_index (foreign_structs, i);
56 if ( (strcmp (namespace, foreign_struct->namespace) == 0) &&
57 (strcmp (name, foreign_struct->name) == 0)) {
58 return foreign_struct;
61 return NULL;
64 static PyObject *
65 pygi_struct_foreign_load_module (const char *namespace)
67 gchar *module_name = g_strconcat ("gi._gi_", namespace, NULL);
68 PyObject *module = PyImport_ImportModule (module_name);
69 g_free (module_name);
70 return module;
73 static PyGIForeignStruct *
74 pygi_struct_foreign_lookup_by_name (const char *namespace, const char *name)
76 PyGIForeignStruct *result;
78 result = do_lookup (namespace, name);
80 if (result == NULL) {
81 PyObject *module = pygi_struct_foreign_load_module (namespace);
83 if (module == NULL)
84 PyErr_Clear ();
85 else {
86 Py_DECREF (module);
87 result = do_lookup (namespace, name);
91 if (result == NULL) {
92 PyErr_Format (PyExc_TypeError,
93 "Couldn't find foreign struct converter for '%s.%s'",
94 namespace,
95 name);
98 return result;
101 static PyGIForeignStruct *
102 pygi_struct_foreign_lookup (GIBaseInfo *base_info)
104 const gchar *namespace = g_base_info_get_namespace (base_info);
105 const gchar *name = g_base_info_get_name (base_info);
107 return pygi_struct_foreign_lookup_by_name (namespace, name);
110 PyObject *
111 pygi_struct_foreign_convert_to_g_argument (PyObject *value,
112 GIInterfaceInfo *interface_info,
113 GITransfer transfer,
114 GIArgument *arg)
116 PyObject *result;
118 GIBaseInfo *base_info = (GIBaseInfo *) interface_info;
119 PyGIForeignStruct *foreign_struct = pygi_struct_foreign_lookup (base_info);
121 if (foreign_struct == NULL) {
122 PyErr_Format(PyExc_KeyError, "could not find foreign type %s",
123 g_base_info_get_name (base_info));
124 return FALSE;
127 result = foreign_struct->to_func (value, interface_info, transfer, arg);
128 return result;
131 PyObject *
132 pygi_struct_foreign_convert_from_g_argument (GIInterfaceInfo *interface_info,
133 GITransfer transfer,
134 GIArgument *arg)
136 GIBaseInfo *base_info = (GIBaseInfo *) interface_info;
137 PyGIForeignStruct *foreign_struct = pygi_struct_foreign_lookup (base_info);
139 if (foreign_struct == NULL)
140 return NULL;
142 return foreign_struct->from_func (interface_info, transfer, arg);
145 PyObject *
146 pygi_struct_foreign_release (GIBaseInfo *base_info,
147 gpointer struct_)
149 PyGIForeignStruct *foreign_struct = pygi_struct_foreign_lookup (base_info);
151 if (foreign_struct == NULL)
152 return NULL;
154 if (!foreign_struct->release_func)
155 Py_RETURN_NONE;
157 return foreign_struct->release_func (base_info, struct_);
160 void
161 pygi_register_foreign_struct (const char* namespace_,
162 const char* name,
163 PyGIArgOverrideToGIArgumentFunc to_func,
164 PyGIArgOverrideFromGIArgumentFunc from_func,
165 PyGIArgOverrideReleaseFunc release_func)
167 PyGIForeignStruct *new_struct = g_slice_new (PyGIForeignStruct);
168 new_struct->namespace = namespace_;
169 new_struct->name = name;
170 new_struct->to_func = to_func;
171 new_struct->from_func = from_func;
172 new_struct->release_func = release_func;
174 g_ptr_array_add (foreign_structs, new_struct);
177 PyObject *
178 pygi_require_foreign (PyObject *self, PyObject *args, PyObject *kwargs)
180 static char *kwlist[] = { "namespace", "symbol", NULL };
181 const char *namespace = NULL;
182 const char *symbol = NULL;
184 if (!PyArg_ParseTupleAndKeywords (args, kwargs,
185 "s|z:require_foreign",
186 kwlist, &namespace, &symbol)) {
187 return NULL;
190 if (symbol) {
191 PyGIForeignStruct *foreign;
192 foreign = pygi_struct_foreign_lookup_by_name (namespace, symbol);
193 if (foreign == NULL) {
194 return NULL;
196 } else {
197 PyObject *module = pygi_struct_foreign_load_module (namespace);
198 if (module) {
199 Py_DECREF (module);
200 } else {
201 return NULL;
205 Py_RETURN_NONE;
209 * Returns 0 on success, or -1 and sets an exception.
212 pygi_foreign_init (void)
214 if (foreign_structs == NULL) {
215 init_foreign_structs ();
218 return 0;