1 /* -*- Mode: C; c-basic-offset: 4 -*- */
3 * Copyright (C) 2005 Johan Dahlin
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Johan Dahlin <johan@gnome.org>
24 gst_structure_get_boolean
26 gst_structure_get_fourcc
27 gst_structure_get_double
28 gst_structure_get_date
29 gst_structure_get_clock_time
30 gst_structure_get_string
31 gst_structure_get_value
32 gst_structure_get_enum
33 gst_structure_get_fraction
35 gst_structure_get_name_id
36 gst_structure_id_get_value
37 gst_structure_id_set_value
38 gst_structure_set_parent_refcount
39 gst_structure_remove_fields
40 gst_structure_map_in_place
41 gst_structure_fixate_field_nearest_fraction
43 override gst_structure_new kwargs
45 _wrap_gst_structure_new(PyGBoxed *self, PyObject *args, PyObject *kwargs)
47 static char *kwlist[] = { "name", NULL };
50 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:GstStructure.__init__", kwlist, &name))
53 self->gtype = GST_TYPE_STRUCTURE;
54 self->free_on_dealloc = FALSE;
56 self->boxed = gst_structure_new(name, NULL);
59 PyErr_SetString(PyExc_RuntimeError, "could not create GstStructure object");
65 override gst_structure_set_value kwargs
67 _wrap_gst_structure_set_value(PyObject *self, PyObject *args, PyObject *kwargs)
69 static char *kwlist[] = { "field", "value", "type_name", NULL };
71 PyObject *py_value = NULL;
72 char *type_name = NULL;
76 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
77 "sO|s:GstStructure.set_value",
78 kwlist, &field, &py_value,
84 if (strcmp (type_name, "char") == 0) {
86 } else if (strcmp (type_name, "uchar") == 0) {
88 } else if (strcmp (type_name, "boolean") == 0) {
89 type = G_TYPE_BOOLEAN;
90 } else if (strcmp (type_name, "int") == 0) {
92 } else if (strcmp (type_name, "uint") == 0) {
94 } else if (strcmp (type_name, "long") == 0) {
96 } else if (strcmp (type_name, "ulong") == 0) {
98 } else if (strcmp (type_name, "int64") == 0) {
100 } else if (strcmp (type_name, "uint64") == 0) {
101 type = G_TYPE_UINT64;
102 } else if (strcmp (type_name, "float") == 0) {
104 } else if (strcmp (type_name, "double") == 0) {
105 type = G_TYPE_DOUBLE;
106 } else if (strcmp (type_name, "string") == 0) {
107 type = G_TYPE_STRING;
109 PyErr_SetString(PyExc_TypeError,
110 "invalid type name");
113 } else if (py_value == Py_None) {
114 PyErr_SetString(PyExc_TypeError, "value can't be None");
117 type = pyg_type_from_object((PyObject *) py_value->ob_type);
120 if (type != G_TYPE_INVALID) {
121 g_value_init(&value, type);
122 } else if (!pygst_value_init_for_pyobject(&value, py_value)) {
126 if (pygst_value_from_pyobject(&value, py_value) != 0) {
129 gst_structure_set_value(pyg_boxed_get(self, GstStructure), field,
136 define GstStructure.has_key args
138 _wrap_gst_structure_has_key(PyGObject *self, PyObject *args)
143 if (!PyArg_ParseTuple(args, "s:GstStructure.has_key", &key))
146 has_field = gst_structure_has_field((GstStructure*)self->obj, key);
148 return PyBool_FromLong(has_field);
152 override gst_structure_keys noargs
154 _wrap_gst_structure_keys (PyObject *self)
160 s = pyg_boxed_get(self, GstStructure);
161 n = gst_structure_n_fields(s);
164 for (i = 0; i < n; ++i) {
165 const gchar *name = gst_structure_nth_field_name (s, i);
166 PyList_SetItem(ret, i, PyString_FromString(name));
174 override-slot GstStructure.tp_as_mapping
176 _wrap_gst_structure_length(PyObject *self)
178 PyGObject *gself = (PyGObject *)self;
179 return gst_structure_n_fields((GstStructure*)gself->obj);
183 _wrap_gst_structure_subscript(PyGObject *self, PyObject *py_key)
186 const char *field = PyString_AsString(py_key);
188 if (gst_structure_has_field((GstStructure*)self->obj, field)) {
189 const GValue *gvalue;
190 gvalue = gst_structure_get_value((GstStructure*)self->obj, field);
191 g_assert(gvalue != NULL);
192 v = pygst_value_as_pyobject(gvalue, TRUE);
194 PyErr_SetString(PyExc_KeyError, field);
201 _wrap_gst_structure_ass_subscript(PyGObject *self,
206 GstStructure* structure;
208 structure = (GstStructure*)self->obj;
209 key = PyString_AsString(py_key);
210 if (py_value != NULL) {
212 if (!pygst_value_init_for_pyobject (&v, py_value))
214 if (pygst_value_from_pyobject(&v, py_value))
216 gst_structure_set_value(structure, key, &v);
219 gst_structure_remove_field(structure, key);
225 static PyMappingMethods _wrap_gst_structure_tp_as_mapping = {
226 _wrap_gst_structure_length, /* mp_length */
227 (binaryfunc)_wrap_gst_structure_subscript, /* mp_subscript */
228 (objobjargproc)_wrap_gst_structure_ass_subscript /* mp_ass_subscript */
232 override gst_structure_foreach kwargs
234 pygst_structure_foreach_marshal(GQuark field_id,
238 PyGstCustomNotify *cunote = user_data;
239 PyObject *py_field, *py_value, *retobj;
240 gboolean retval = TRUE;
241 PyGILState_STATE state;
243 g_assert(cunote->func);
245 state = pyg_gil_state_ensure();
247 py_field = Py_BuildValue("s", g_quark_to_string(field_id));
248 py_value = pygst_value_as_pyobject(value, FALSE);
250 retobj = PyEval_CallFunction(cunote->func, "(NNO)",
254 retobj = PyEval_CallFunction(cunote->func, "(NN)",
257 if (PyErr_Occurred () || (retobj == NULL) || (retobj == Py_None)) {
260 } else if (retobj != Py_None) {
261 retval = PyInt_AsLong(retobj);
266 pyg_gil_state_release(state);
272 _wrap_gst_structure_foreach (PyGObject *self,
276 static char *kwlist[] = { "foreach_function", "args", NULL };
277 PyObject *pyfunc, *pyarg = NULL;
278 PyGstCustomNotify cunote;
280 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
281 "O|O:GstStructure.foreach",
287 if (!PyCallable_Check(pyfunc)) {
288 PyErr_SetString(PyExc_TypeError, "foreach_function not callable");
292 cunote.func = pyfunc;
294 gst_structure_foreach(pyg_boxed_get(self, GstStructure),
295 pygst_structure_foreach_marshal,
302 override-slot GstStructure.tp_repr
304 _wrap_gst_structure_tp_repr (PyGObject *self)
309 buf = g_strdup_printf("<GstStructure (%s) at %lx>",
310 gst_structure_get_name((GstStructure*)self->obj),
313 retval = PyString_FromString(buf);
318 override gst_structure_from_string kwargs
320 _wrap_gst_structure_from_string(PyObject *self, PyObject *args, PyObject *kwargs)
322 static char *kwlist[] = { "string", NULL };
326 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:structure_from_string", kwlist, &string))
329 ret = gst_structure_from_string(string, NULL);
331 /* pyg_boxed_new handles NULL checking */
332 return pyg_boxed_new(GST_TYPE_STRUCTURE, ret, FALSE, TRUE);
335 override-slot GstStructure.tp_dealloc
337 _wrap_gst_structure_tp_dealloc (PyObject *self)
339 PyGBoxed *boxed = (PyGBoxed *) self;
341 if (boxed->free_on_dealloc && boxed->boxed) {
342 gst_structure_free (boxed->boxed);
343 } else if (boxed->boxed) {
344 pygst_caps_map_remove_structure (self);
347 self->ob_type->tp_free((PyObject *)self);