Initial import to Tizen
[profile/ivi/gstreamer-python.git] / gst / gststructure.override
1 /* -*- Mode: C; c-basic-offset: 4 -*- */
2 /* gst-python
3  * Copyright (C) 2005 Johan Dahlin
4  *
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.
9  *
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.
14  *
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.
19  * 
20  * Author: Johan Dahlin <johan@gnome.org>
21  */
22 %%
23 ignore
24   gst_structure_get_boolean
25   gst_structure_get_int
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
34   gst_structure_set
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
42 %%
43 override gst_structure_new kwargs
44 static int
45 _wrap_gst_structure_new(PyGBoxed *self, PyObject *args, PyObject *kwargs)
46 {
47         static char *kwlist[] = { "name", NULL };
48         char *name;
49
50         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:GstStructure.__init__", kwlist, &name))
51                 return -1;
52         
53         self->gtype = GST_TYPE_STRUCTURE;
54         self->free_on_dealloc = FALSE;
55
56         self->boxed = gst_structure_new(name, NULL);
57
58         if (!self->boxed) {
59                 PyErr_SetString(PyExc_RuntimeError, "could not create GstStructure object");
60                 return -1;
61         }
62         return 0;
63 }
64 %%
65 override gst_structure_set_value kwargs
66 static PyObject *
67 _wrap_gst_structure_set_value(PyObject *self, PyObject *args, PyObject *kwargs)
68 {
69         static char *kwlist[] = { "field", "value", "type_name", NULL };
70         char *field;
71         PyObject *py_value = NULL;
72         char *type_name = NULL;
73         GType type;
74         GValue value = { 0 };
75
76         if (!PyArg_ParseTupleAndKeywords(args, kwargs,
77                 "sO|s:GstStructure.set_value",
78                 kwlist, &field, &py_value,
79                 &type_name)) {
80             return NULL;
81         }
82
83         if (type_name) {
84             if (strcmp (type_name, "char") == 0) {
85                 type = G_TYPE_CHAR;
86             } else if (strcmp (type_name, "uchar") == 0) {
87                 type = G_TYPE_UCHAR;
88             } else if (strcmp (type_name, "boolean") == 0) {
89                 type = G_TYPE_BOOLEAN;
90             } else if (strcmp (type_name, "int") == 0) {
91                 type = G_TYPE_INT;
92             } else if (strcmp (type_name, "uint") == 0) {
93                 type = G_TYPE_UINT;
94             } else if (strcmp (type_name, "long") == 0) {
95                 type = G_TYPE_LONG;
96             } else if (strcmp (type_name, "ulong") == 0) {
97                 type = G_TYPE_ULONG;
98             } else if (strcmp (type_name, "int64") == 0) {
99                 type = G_TYPE_INT64;
100             } else if (strcmp (type_name, "uint64") == 0) {
101                 type = G_TYPE_UINT64;
102             } else if (strcmp (type_name, "float") == 0) {
103                 type = G_TYPE_FLOAT;
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;
108             } else {
109                 PyErr_SetString(PyExc_TypeError,
110                     "invalid type name");
111                 return NULL;
112             }
113         } else if (py_value == Py_None) {
114             PyErr_SetString(PyExc_TypeError, "value can't be None");
115             return NULL;
116         } else {
117             type = pyg_type_from_object((PyObject *) py_value->ob_type);
118         }
119
120         if (type != G_TYPE_INVALID) {
121             g_value_init(&value, type);
122         } else if (!pygst_value_init_for_pyobject(&value, py_value)) {
123             return NULL;
124         }
125
126         if (pygst_value_from_pyobject(&value, py_value) != 0) {
127             return NULL;
128         }
129         gst_structure_set_value(pyg_boxed_get(self, GstStructure), field,
130             &value);
131
132         Py_INCREF(Py_None);
133         return Py_None;
134 }
135 %%
136 define GstStructure.has_key args
137 static PyObject*
138 _wrap_gst_structure_has_key(PyGObject *self, PyObject *args)
139 {
140         gchar *key;
141         gboolean has_field;
142     
143         if (!PyArg_ParseTuple(args, "s:GstStructure.has_key", &key))
144                 return NULL;
145
146         has_field = gst_structure_has_field((GstStructure*)self->obj, key);
147             
148         return PyBool_FromLong(has_field);
149 }    
150
151 %%
152 override gst_structure_keys noargs
153 static PyObject *
154 _wrap_gst_structure_keys (PyObject *self)
155 {
156     GstStructure *s;
157     int i, n;
158     PyObject *ret;
159
160     s = pyg_boxed_get(self, GstStructure);
161     n = gst_structure_n_fields(s);
162     ret = PyList_New(n);
163
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));
167     }
168
169     return ret;
170 }
171
172
173 %%
174 override-slot GstStructure.tp_as_mapping
175 static Py_ssize_t
176 _wrap_gst_structure_length(PyObject *self)
177 {
178         PyGObject *gself = (PyGObject *)self;
179         return gst_structure_n_fields((GstStructure*)gself->obj);
180 }
181
182 static PyObject *
183 _wrap_gst_structure_subscript(PyGObject *self, PyObject *py_key)
184 {
185         PyObject *v = NULL;
186         const char *field = PyString_AsString(py_key);
187         
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);
193         } else {
194                 PyErr_SetString(PyExc_KeyError, field);
195         }
196                 
197         return v;
198 }
199
200 static int
201 _wrap_gst_structure_ass_subscript(PyGObject *self,
202                                   PyObject *py_key,
203                                   PyObject *py_value)
204 {
205         const char *key;
206         GstStructure* structure;
207
208         structure = (GstStructure*)self->obj;
209         key = PyString_AsString(py_key);
210         if (py_value != NULL) {
211             GValue v = { 0, };
212             if (!pygst_value_init_for_pyobject (&v, py_value))
213                 return -1;
214             if (pygst_value_from_pyobject(&v, py_value))
215                 return -1;
216             gst_structure_set_value(structure, key, &v);
217             g_value_unset(&v);
218         } else {
219             gst_structure_remove_field(structure, key);
220         }
221         
222         return 0;
223 }
224
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 */
229 };
230
231 %%
232 override gst_structure_foreach kwargs
233 static gboolean
234 pygst_structure_foreach_marshal(GQuark field_id,
235                                 const GValue *value,
236                                 gpointer user_data)
237 {
238         PyGstCustomNotify *cunote = user_data;
239         PyObject *py_field, *py_value, *retobj;
240         gboolean retval = TRUE;
241         PyGILState_STATE state;
242         
243         g_assert(cunote->func);
244
245         state = pyg_gil_state_ensure();
246
247         py_field = Py_BuildValue("s", g_quark_to_string(field_id));
248         py_value = pygst_value_as_pyobject(value, FALSE);
249         if (cunote->data)
250                 retobj = PyEval_CallFunction(cunote->func, "(NNO)",
251                                              py_field, py_value,
252                                              cunote->data);
253         else
254                 retobj = PyEval_CallFunction(cunote->func, "(NN)",
255                                              py_field, py_value);
256
257         if (PyErr_Occurred () || (retobj == NULL) || (retobj == Py_None)) {
258                 PyErr_Print ();
259                 retval = FALSE;
260         } else if (retobj != Py_None) {
261                 retval = PyInt_AsLong(retobj);
262         }
263
264         Py_XDECREF(retobj);
265
266         pyg_gil_state_release(state);
267         
268         return retval;
269 }
270
271 static PyObject *
272 _wrap_gst_structure_foreach (PyGObject *self,
273                              PyObject *args,
274                              PyObject *kwargs)
275 {
276         static char *kwlist[] = { "foreach_function", "args", NULL };
277         PyObject *pyfunc, *pyarg = NULL;
278         PyGstCustomNotify cunote;
279
280         if (!PyArg_ParseTupleAndKeywords(args, kwargs,
281                                          "O|O:GstStructure.foreach",
282                                          kwlist,
283                                          &pyfunc, &pyarg)) {
284                 return NULL;
285         }
286
287         if (!PyCallable_Check(pyfunc)) {
288                 PyErr_SetString(PyExc_TypeError, "foreach_function not callable");
289                 return NULL;
290         }
291
292         cunote.func = pyfunc;
293         cunote.data = pyarg;
294         gst_structure_foreach(pyg_boxed_get(self, GstStructure),
295                               pygst_structure_foreach_marshal,
296                               &cunote);
297
298         Py_INCREF(Py_None);
299         return Py_None;
300 }
301 %%
302 override-slot GstStructure.tp_repr
303 static PyObject *
304 _wrap_gst_structure_tp_repr (PyGObject *self)
305 {
306         char *buf;
307         PyObject *retval;
308         
309         buf = g_strdup_printf("<GstStructure (%s) at %lx>",
310                               gst_structure_get_name((GstStructure*)self->obj),
311                               (long)self->obj);
312
313         retval = PyString_FromString(buf);
314         g_free(buf);
315         return retval;
316 }
317 %%
318 override gst_structure_from_string kwargs
319 static PyObject *
320 _wrap_gst_structure_from_string(PyObject *self, PyObject *args, PyObject *kwargs)
321 {
322     static char *kwlist[] = { "string", NULL };
323     char *string;
324     GstStructure *ret;
325
326     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:structure_from_string", kwlist, &string))
327         return NULL;
328     
329     ret = gst_structure_from_string(string, NULL);
330     
331     /* pyg_boxed_new handles NULL checking */
332     return pyg_boxed_new(GST_TYPE_STRUCTURE, ret, FALSE, TRUE);
333 }
334 %%
335 override-slot GstStructure.tp_dealloc
336 static void
337 _wrap_gst_structure_tp_dealloc (PyObject *self)
338 {
339   PyGBoxed *boxed = (PyGBoxed *) self;
340
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);
345   }
346
347   self->ob_type->tp_free((PyObject *)self);
348 }