fix warnings
[platform/upstream/gstreamer.git] / gst / gstutils.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstutils.c: Utility functions: gtk_get_property stuff, etc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "gst_private.h"
27 #include "gstutils.h"
28
29 #include "gstextratypes.h"
30
31 #define ZERO(mem) memset(&mem, 0, sizeof(mem))
32
33 /**
34  * gst_util_get_int_arg:
35  * @object: the object to query
36  * @argname: the name of the argument
37  *
38  * Retrieves a property of an object as an integer.
39  *
40  * Returns: the property of the object
41  */
42 gint
43 gst_util_get_int_arg (GObject *object, const gchar *argname) 
44 {
45   GValue value;
46
47   ZERO (value);
48   g_value_init (&value, G_TYPE_INT);
49   g_object_get_property(G_OBJECT(object),argname,&value);
50
51   return g_value_get_int(&value);
52 }
53
54 /**
55  * gst_util_get_bool_arg:
56  * @object: the object to query
57  * @argname: the name of the argument
58  *
59  * Retrieves a property of an object as a boolean.
60  *
61  * Returns: the property of the object
62  */
63 gint
64 gst_util_get_bool_arg (GObject *object, const gchar *argname) 
65 {
66   GValue value;
67
68   ZERO (value);
69   g_value_init (&value, G_TYPE_BOOLEAN);
70   g_object_get_property(G_OBJECT(object),argname,&value);
71
72   return g_value_get_boolean(&value);
73 }
74
75 /**
76  * gst_util_get_long_arg:
77  * @object: the object to query
78  * @argname: the name of the argument
79  *
80  * Retrieves a property of an object as a long.
81  *
82  * Returns: the property of the object
83  */
84 glong
85 gst_util_get_long_arg (GObject *object, const gchar *argname) 
86 {
87   GValue value;
88
89   ZERO (value);
90   g_value_init (&value, G_TYPE_LONG);
91   g_object_get_property(G_OBJECT(object),argname,&value);
92
93   return g_value_get_long(&value);
94 }
95
96 /**
97  * gst_util_get_float_arg:
98  * @object: the object to query
99  * @argname: the name of the argument
100  *
101  * Retrieves a property of an object as a float.
102  *
103  * Returns: the property of the object
104  */
105 gfloat
106 gst_util_get_float_arg (GObject *object, const gchar *argname) 
107 {
108   GValue value;
109
110   ZERO (value);
111   g_value_init (&value, G_TYPE_FLOAT);
112   g_object_get_property(G_OBJECT(object),argname,&value);
113
114   return g_value_get_float(&value);
115 }
116
117 /**
118  * gst_util_get_double_arg:
119  * @object: the object to query
120  * @argname: the name of the argument
121  *
122  * Retrieves a property of an object as a double.
123  *
124  * Returns: the property of the object
125  */
126 gdouble 
127 gst_util_get_double_arg (GObject *object, const gchar *argname) 
128 {
129   GValue value;
130
131   ZERO (value);
132   g_value_init (&value, G_TYPE_DOUBLE);
133   g_object_get_property(G_OBJECT(object),argname,&value);
134
135   return g_value_get_double(&value);
136 }
137
138 /**
139  * gst_util_get_string_arg:
140  * @object: the object to query
141  * @argname: the name of the argument
142  *
143  * Retrieves a property of an object as a string.
144  *
145  * Returns: the property of the object
146  */
147  const gchar*
148 gst_util_get_string_arg (GObject *object, const gchar *argname) 
149 {
150   GValue value;
151
152   ZERO (value);
153   g_value_init (&value, G_TYPE_STRING);
154   g_object_get_property(G_OBJECT(object),argname,&value);
155
156   return g_value_get_string(&value);  // memleak?
157 }
158
159 /**
160  * gst_util_get_pointer_arg:
161  * @object: the object to query
162  * @argname: the name of the argument
163  *
164  * Retrieves a property of an object as a pointer.
165  *
166  * Returns: the property of the object
167  */
168 gpointer
169 gst_util_get_pointer_arg (GObject *object, const gchar *argname) 
170 {
171   GValue value;
172
173   ZERO (value);
174   g_value_init (&value, G_TYPE_POINTER);
175   g_object_get_property(G_OBJECT(object),argname,&value);
176
177   return g_value_get_pointer(&value);
178 }
179
180 /**
181  * gst_util_get_widget_property:
182  * @object: the object to query
183  * @argname: the name of the argument
184  *
185  * Retrieves a property of an object as a widget.
186  *
187  * Returns: the property of the object
188  */
189 /* COMMENTED OUT BECAUSE WE HAVE NO MORE gtk.h
190 GtkWidget*
191 gst_util_get_widget_property (GObject *object, const gchar *argname) 
192 {
193   GtkArg arg;
194
195   arg.name = argname;
196   gtk_object_getv(G_OBJECT(object),1,&arg);
197   
198   return GTK_WIDGET(G_VALUE_OBJECT(arg));
199 }
200 */
201
202 /**
203  * gst_util_dump_mem:
204  * @mem: a pointer to the memory to dump
205  * @size: the size of the memory block to dump
206  *
207  * Dumps the memory block into a hex representation. Useful for debugging.
208  */
209 void 
210 gst_util_dump_mem (guchar *mem, guint size) 
211 {
212   guint i, j;
213
214   i = j =0;
215   while (i<size) {
216     if (j == 0) {
217       g_print("\n%08x : ", i);
218       j = 15;
219     }
220     else {
221       j--;
222     }
223     g_print("%02x ", mem[i]);
224     i++;
225   }
226   g_print("\n");
227 }
228
229 /**
230  * gst_util_set_object_arg:
231  * @object: the object to set the argument of
232  * @name: the name of the argument to set
233  * @value: the string value to set
234  *
235  * Convertes the string value to the type of the objects argument and
236  * sets the argument with it.
237  */
238 void
239 gst_util_set_object_arg (GObject *object,  const gchar *name,  const gchar *value) 
240 {
241   if (name && value) {
242     GParamSpec *paramspec;
243
244     paramspec = g_object_class_find_property(G_OBJECT_GET_CLASS(object),name);
245
246     if (!paramspec) {
247       return;
248     }
249
250     GST_DEBUG(0,"paramspec->flags is %d, paramspec->value_type is %d\n",
251               paramspec->flags,paramspec->value_type);
252
253     if (paramspec->flags & G_PARAM_WRITABLE) {
254       switch (paramspec->value_type) {
255         case G_TYPE_STRING:
256           g_object_set (G_OBJECT (object), name, value, NULL);
257           break;
258         case G_TYPE_ENUM: 
259         case G_TYPE_INT: {
260           gint i;
261           sscanf (value, "%d", &i);
262           g_object_set (G_OBJECT (object), name, i, NULL);
263           break;
264         }
265         case G_TYPE_UINT: {
266           guint i;
267           sscanf (value, "%u", &i);
268           g_object_set (G_OBJECT (object), name, i, NULL);
269           break;
270         }
271         case G_TYPE_LONG: {
272           glong i;
273           sscanf (value, "%ld", &i);
274           g_object_set (G_OBJECT (object), name, i, NULL);
275           break;
276         }
277         case G_TYPE_ULONG: {
278           gulong i;
279           sscanf (value, "%lu", &i);
280           g_object_set (G_OBJECT (object), name, i, NULL);
281           break;
282         }
283         case G_TYPE_BOOLEAN: {
284           gboolean i = FALSE;
285           if (!strncmp ("true", value, 4)) i = TRUE;
286           g_object_set (G_OBJECT (object), name, i, NULL);
287           break;
288         }
289         case G_TYPE_CHAR: {
290           gchar i;
291           sscanf (value, "%c", &i);
292           g_object_set (G_OBJECT (object), name, i, NULL);
293           break;
294         }
295         case G_TYPE_UCHAR: {
296           guchar i;
297           sscanf (value, "%c", &i);
298           g_object_set (G_OBJECT (object), name, i, NULL);
299           break;
300         }
301         case G_TYPE_FLOAT: {
302           gfloat i;
303           sscanf (value, "%f", &i);
304           g_object_set (G_OBJECT (object), name, i, NULL);
305           break;
306         }
307         case G_TYPE_DOUBLE: {
308           gfloat i;
309           sscanf (value, "%g", &i);
310           g_object_set (G_OBJECT (object), name, (gdouble)i, NULL);
311           break;
312         }
313         default:
314           if (G_IS_PARAM_SPEC_ENUM(paramspec)) {
315             gint i;
316             sscanf (value, "%d", &i);
317             g_object_set (G_OBJECT (object), name, i, NULL);
318           }
319           else if (paramspec->value_type == GST_TYPE_FILENAME) {
320             g_object_set (G_OBJECT (object), name, value, NULL);
321           }
322           break;
323       }
324     }
325   }
326 }
327
328 // -----------------------------------------------------
329 //
330 // The following code will be moved out of the main
331 // gstreamer library someday.
332 //
333
334 #include "gstpad.h"
335 #include "gsttype.h"
336 #include "gstprops.h"
337 #include "gstpropsprivate.h"
338
339 static void string_append_indent (GString *str, gint count)
340 {
341   gint xx;
342   for (xx=0; xx < count; xx++)
343     g_string_append_c (str, ' ');
344 }
345
346 static void 
347 gst_print_props (GString *buf, gint indent,
348                  GList *props, gboolean showname)
349 {
350   GList *elem;
351             
352   for (elem = props; elem; elem = g_list_next (elem))
353     {
354       GstPropsEntry *prop = elem->data;
355
356       string_append_indent (buf, indent);
357       if (showname)
358         g_string_append (buf, g_quark_to_string (prop->propid));
359
360       switch (prop->propstype) {
361       case GST_PROPS_INT_ID:
362         g_string_printfa (buf, "%d (int)\n", prop->data.int_data);
363         break;
364       case GST_PROPS_INT_RANGE_ID:
365         g_string_printfa (buf, "%d - %d (int)\n",
366                           prop->data.int_range_data.min,
367                           prop->data.int_range_data.max);
368         break;
369       case GST_PROPS_FLOAT_ID:
370         g_string_printfa (buf, "%f (float)\n", prop->data.float_data);
371       break;
372       case GST_PROPS_FLOAT_RANGE_ID:
373         g_string_printfa (buf, "%f - %f (float)\n",
374                           prop->data.float_range_data.min,
375                           prop->data.float_range_data.max);
376         break;
377       case GST_PROPS_BOOL_ID:
378         g_string_printfa (buf, "%s\n",
379                           prop->data.bool_data ? "TRUE" : "FALSE");
380         break;
381       case GST_PROPS_STRING_ID:
382         g_string_printfa (buf, "\"%s\"\n", prop->data.string_data.string);
383         break;
384       case GST_PROPS_FOURCC_ID:
385         g_string_printfa (buf, "'%c%c%c%c' (fourcc)\n",
386                           prop->data.fourcc_data & 0xff,
387                           prop->data.fourcc_data>>8 & 0xff,
388                           prop->data.fourcc_data>>16 & 0xff,
389                           prop->data.fourcc_data>>24 & 0xff);
390         break;
391       case GST_PROPS_LIST_ID:
392         gst_print_props (buf, indent+2, prop->data.list_data.entries, FALSE);
393         break;
394       default:
395         g_string_printfa (buf, "unknown proptype %d\n", prop->propstype);
396         break;
397       }
398   }
399 }
400
401 void gst_print_pad_caps (GString *buf, gint indent, GstPad *pad)
402 {
403   GstRealPad *realpad;
404   GstCaps *caps;
405
406   realpad = GST_PAD_REALIZE(pad);
407   caps = realpad->caps;
408
409   if (!caps)
410     {
411       string_append_indent (buf, indent);
412       g_string_printf (buf, "%s:%s has no capabilities",
413                        GST_DEBUG_PAD_NAME (pad));
414     }
415   else
416     {
417       gint capx = 0;
418
419       while (caps) {
420         GstType *type;
421
422         string_append_indent (buf, indent);
423         g_string_printfa (buf, "Cap[%d]: %s\n", capx++, caps->name);
424
425         type = gst_type_find_by_id (caps->id);
426         string_append_indent (buf, indent+2);
427         g_string_printfa (buf, "MIME type: %s\n",
428                           type->mime? type->mime : "unknown/unknown");
429
430         if (caps->properties)
431           gst_print_props (buf, indent + 2,
432                            caps->properties->properties, TRUE);
433
434         caps = caps->next;
435       }
436     }
437 }