2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstutils.c: Utility functions: gtk_get_property stuff, etc.
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.
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.
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.
27 #include "gst_private.h"
29 #include "gsturitype.h"
34 * @mem: a pointer to the memory to dump
35 * @size: the size of the memory block to dump
37 * Dumps the memory block into a hex representation. Useful for debugging.
40 gst_util_dump_mem (guchar *mem, guint size)
43 GString *string = g_string_sized_new (50);
44 GString *chars = g_string_sized_new (18);
48 if (g_ascii_isprint (mem[i]))
49 g_string_append_printf (chars, "%c", mem[i]);
51 g_string_append_printf (chars, ".");
53 g_string_append_printf (string, "%02x ", mem[i]);
58 if (j == 16 || i == size) {
59 g_print ("%08x (%p): %-48.48s %-16.16s\n", i-j, mem+i-j, string->str, chars->str);
60 g_string_set_size (string, 0);
61 g_string_set_size (chars, 0);
65 g_string_free (string, TRUE);
66 g_string_free (chars, TRUE);
71 * gst_util_set_value_from_string:
72 * @value: the value to set
73 * @value_str: the string to get the value from
75 * Converts the string to the type of the value and
76 * sets the value with it.
79 gst_util_set_value_from_string(GValue *value, const gchar *value_str)
82 g_return_if_fail(value != NULL);
83 g_return_if_fail(value_str != NULL);
85 GST_DEBUG(GST_CAT_PARAMS, "parsing '%s' to type %s", value_str, g_type_name(G_VALUE_TYPE(value)));
87 switch (G_VALUE_TYPE(value)) {
89 g_value_set_string(value, g_strdup(value_str));
94 sscanf (value_str, "%d", &i);
95 g_value_set_int(value, i);
100 sscanf (value_str, "%u", &i);
101 g_value_set_uint(value, i);
106 sscanf (value_str, "%ld", &i);
107 g_value_set_long(value, i);
112 sscanf (value_str, "%lu", &i);
113 g_value_set_ulong(value, i);
116 case G_TYPE_BOOLEAN: {
118 if (!strncmp ("true", value_str, 4)) i = TRUE;
119 g_value_set_boolean(value, i);
124 sscanf (value_str, "%c", &i);
125 g_value_set_char(value, i);
130 sscanf (value_str, "%c", &i);
131 g_value_set_uchar(value, i);
136 sscanf (value_str, "%f", &i);
137 g_value_set_float(value, i);
140 case G_TYPE_DOUBLE: {
142 sscanf (value_str, "%g", &i);
143 g_value_set_double(value, (gdouble)i);
152 * gst_util_set_object_arg:
153 * @object: the object to set the argument of
154 * @name: the name of the argument to set
155 * @value: the string value to set
157 * Convertes the string value to the type of the objects argument and
158 * sets the argument with it.
161 gst_util_set_object_arg (GObject * object, const gchar * name, const gchar * value)
164 GParamSpec *paramspec;
166 paramspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), name);
172 GST_DEBUG (0, "paramspec->flags is %d, paramspec->value_type is %d",
173 paramspec->flags, (gint) paramspec->value_type);
175 if (paramspec->flags & G_PARAM_WRITABLE) {
176 switch (paramspec->value_type) {
178 g_object_set (G_OBJECT (object), name, value, NULL);
184 sscanf (value, "%d", &i);
185 g_object_set (G_OBJECT (object), name, i, NULL);
191 sscanf (value, "%u", &i);
192 g_object_set (G_OBJECT (object), name, i, NULL);
198 sscanf (value, "%ld", &i);
199 g_object_set (G_OBJECT (object), name, i, NULL);
205 sscanf (value, "%lu", &i);
206 g_object_set (G_OBJECT (object), name, i, NULL);
209 case G_TYPE_BOOLEAN:{
212 if (!strncmp ("true", value, 4))
214 g_object_set (G_OBJECT (object), name, i, NULL);
220 sscanf (value, "%c", &i);
221 g_object_set (G_OBJECT (object), name, i, NULL);
227 sscanf (value, "%c", &i);
228 g_object_set (G_OBJECT (object), name, i, NULL);
234 sscanf (value, "%f", &i);
235 g_object_set (G_OBJECT (object), name, i, NULL);
241 sscanf (value, "%g", &i);
242 g_object_set (G_OBJECT (object), name, (gdouble) i, NULL);
246 if (G_IS_PARAM_SPEC_ENUM (paramspec)) {
249 sscanf (value, "%d", &i);
250 g_object_set (G_OBJECT (object), name, i, NULL);
252 else if (paramspec->value_type == GST_TYPE_URI) {
253 g_object_set (G_OBJECT (object), name, value, NULL);
261 /* -----------------------------------------------------
263 * The following code will be moved out of the main
264 * gstreamer library someday.
269 #include "gstprops.h"
272 string_append_indent (GString * str, gint count)
276 for (xx = 0; xx < count; xx++)
277 g_string_append_c (str, ' ');
281 gst_print_props (GString *buf, gint indent, GList *props, gboolean showname)
288 for (elem = props; elem; elem = g_list_next (elem)) {
289 GstPropsEntry *prop = elem->data;
290 const gchar *name = gst_props_entry_get_name (prop);
292 if (width < strlen (name))
293 width = strlen (name);
296 for (elem = props; elem; elem = g_list_next (elem)) {
297 GstPropsEntry *prop = elem->data;
299 string_append_indent (buf, indent);
301 const gchar *name = gst_props_entry_get_name (prop);
303 g_string_append (buf, name);
304 string_append_indent (buf, 2 + width - strlen (name));
307 type = gst_props_entry_get_type (prop);
309 case GST_PROPS_INT_TYPE:
312 gst_props_entry_get_int (prop, &val);
313 g_string_append_printf (buf, "%d (int)\n", val);
316 case GST_PROPS_INT_RANGE_TYPE:
319 gst_props_entry_get_int_range (prop, &min, &max);
320 g_string_append_printf (buf, "%d - %d (int)\n", min, max);
323 case GST_PROPS_FLOAT_TYPE:
326 gst_props_entry_get_float (prop, &val);
327 g_string_append_printf (buf, "%f (float)\n", val);
330 case GST_PROPS_FLOAT_RANGE_TYPE:
333 gst_props_entry_get_float_range (prop, &min, &max);
334 g_string_append_printf (buf, "%f - %f (float)\n", min, max);
337 case GST_PROPS_BOOLEAN_TYPE:
340 gst_props_entry_get_boolean (prop, &val);
341 g_string_append_printf (buf, "%s\n", val ? "TRUE" : "FALSE");
344 case GST_PROPS_STRING_TYPE:
347 gst_props_entry_get_string (prop, &val);
348 g_string_append_printf (buf, "\"%s\"\n", val);
351 case GST_PROPS_FOURCC_TYPE:
354 gst_props_entry_get_fourcc_int (prop, &val);
355 g_string_append_printf (buf, "'%c%c%c%c' (fourcc)\n",
356 (gchar)( val & 0xff),
357 (gchar)((val >> 8) & 0xff),
358 (gchar)((val >> 16) & 0xff),
359 (gchar)((val >> 24) & 0xff));
362 case GST_PROPS_LIST_TYPE:
365 gst_props_entry_get_list (prop, &list);
366 gst_print_props (buf, indent + 2, (GList *)list, FALSE);
370 g_string_append_printf (buf, "unknown proptype %d\n", type);
377 * gst_print_pad_caps:
378 * @buf: the buffer to print the caps in
379 * @indent: initial indentation
380 * @pad: the pad to print the caps from
382 * Write the pad capabilities in a human readable format into
386 gst_print_pad_caps (GString * buf, gint indent, GstPad * pad)
391 realpad = GST_PAD_REALIZE (pad);
392 caps = realpad->caps;
395 string_append_indent (buf, indent);
396 g_string_printf (buf, "%s:%s has no capabilities", GST_DEBUG_PAD_NAME (pad));
404 string_append_indent (buf, indent);
405 g_string_append_printf (buf, "Cap[%d]: %s\n", capx++, caps->name);
407 type = gst_type_find_by_id (caps->id);
408 string_append_indent (buf, indent + 2);
409 g_string_append_printf (buf, "MIME type: %s\n", type->mime ? type->mime : "unknown/unknown");
411 if (caps->properties)
412 gst_print_props (buf, indent + 4, caps->properties->properties, TRUE);
420 * gst_print_element_args:
421 * @buf: the buffer to print the args in
422 * @indent: initial indentation
423 * @element: the element to print the args of
425 * Print the element argument in a human readable format in the given
429 gst_print_element_args (GString * buf, gint indent, GstElement * element)
432 GValue value = { 0, }; /* the important thing is that value.type = 0 */
434 GParamSpec *spec, **specs, **walk;
436 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (element), NULL);
439 for (walk = specs; *walk; walk++) {
441 if (width < strlen (spec->name))
442 width = strlen (spec->name);
445 for (walk = specs; *walk; walk++) {
448 if (spec->flags & G_PARAM_READABLE) {
449 g_value_init(&value, G_PARAM_SPEC_VALUE_TYPE (spec));
450 g_object_get_property (G_OBJECT (element), spec->name, &value);
451 str = g_strdup_value_contents (&value);
452 g_value_unset(&value);
454 str = g_strdup ("Parameter not readable.");
457 string_append_indent (buf, indent);
458 g_string_append (buf, spec->name);
459 string_append_indent (buf, 2 + width - strlen (spec->name));
460 g_string_append (buf, str);
461 g_string_append_c (buf, '\n');