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.
26 #include "gst_private.h"
29 #include "gstextratypes.h"
31 #define ZERO(mem) memset(&mem, 0, sizeof(mem))
34 * gst_util_get_int_arg:
35 * @object: the object to query
36 * @argname: the name of the argument
38 * Retrieves a property of an object as an integer.
40 * Returns: the property of the object
43 gst_util_get_int_arg (GObject * object, const gchar * argname)
48 g_value_init (&value, G_TYPE_INT);
49 g_object_get_property (G_OBJECT (object), argname, &value);
51 return g_value_get_int (&value);
55 * gst_util_get_bool_arg:
56 * @object: the object to query
57 * @argname: the name of the argument
59 * Retrieves a property of an object as a boolean.
61 * Returns: the property of the object
64 gst_util_get_bool_arg (GObject * object, const gchar * argname)
69 g_value_init (&value, G_TYPE_BOOLEAN);
70 g_object_get_property (G_OBJECT (object), argname, &value);
72 return g_value_get_boolean (&value);
76 * gst_util_get_long_arg:
77 * @object: the object to query
78 * @argname: the name of the argument
80 * Retrieves a property of an object as a long.
82 * Returns: the property of the object
85 gst_util_get_long_arg (GObject * object, const gchar * argname)
90 g_value_init (&value, G_TYPE_LONG);
91 g_object_get_property (G_OBJECT (object), argname, &value);
93 return g_value_get_long (&value);
97 * gst_util_get_int64_arg:
98 * @object: the object to query
99 * @argname: the name of the argument
101 * Retrieves a property of an object as an int64.
103 * Returns: the property of the object
106 gst_util_get_int64_arg (GObject *object, const gchar *argname)
111 g_value_init (&value, G_TYPE_INT64);
112 g_object_get_property (G_OBJECT (object), argname, &value);
114 return g_value_get_int64 (&value);
118 * gst_util_get_float_arg:
119 * @object: the object to query
120 * @argname: the name of the argument
122 * Retrieves a property of an object as a float.
124 * Returns: the property of the object
127 gst_util_get_float_arg (GObject * object, const gchar * argname)
132 g_value_init (&value, G_TYPE_FLOAT);
133 g_object_get_property (G_OBJECT (object), argname, &value);
135 return g_value_get_float (&value);
139 * gst_util_get_double_arg:
140 * @object: the object to query
141 * @argname: the name of the argument
143 * Retrieves a property of an object as a double.
145 * Returns: the property of the object
148 gst_util_get_double_arg (GObject * object, const gchar * argname)
153 g_value_init (&value, G_TYPE_DOUBLE);
154 g_object_get_property (G_OBJECT (object), argname, &value);
156 return g_value_get_double (&value);
160 * gst_util_get_string_arg:
161 * @object: the object to query
162 * @argname: the name of the argument
164 * Retrieves a property of an object as a string.
166 * Returns: the property of the object
169 gst_util_get_string_arg (GObject * object, const gchar * argname)
174 g_value_init (&value, G_TYPE_STRING);
175 g_object_get_property (G_OBJECT (object), argname, &value);
177 return g_value_get_string (&value); /* memleak? */
181 * gst_util_get_pointer_arg:
182 * @object: the object to query
183 * @argname: the name of the argument
185 * Retrieves a property of an object as a pointer.
187 * Returns: the property of the object
190 gst_util_get_pointer_arg (GObject * object, const gchar * argname)
195 g_value_init (&value, G_TYPE_POINTER);
196 g_object_get_property (G_OBJECT (object), argname, &value);
198 return g_value_get_pointer (&value);
202 * gst_util_get_widget_property:
203 * @object: the object to query
204 * @argname: the name of the argument
206 * Retrieves a property of an object as a widget.
208 * Returns: the property of the object
210 /* COMMENTED OUT BECAUSE WE HAVE NO MORE gtk.h
212 gst_util_get_widget_property (GObject *object, const gchar *argname)
217 gtk_object_getv(G_OBJECT(object),1,&arg);
219 return GTK_WIDGET(G_VALUE_OBJECT(arg));
225 * @mem: a pointer to the memory to dump
226 * @size: the size of the memory block to dump
228 * Dumps the memory block into a hex representation. Useful for debugging.
231 gst_util_dump_mem (guchar * mem, guint size)
238 g_print ("\n%08x : ", i);
244 g_print ("%02x ", mem[i]);
251 * gst_util_set_object_arg:
252 * @object: the object to set the argument of
253 * @name: the name of the argument to set
254 * @value: the string value to set
256 * Convertes the string value to the type of the objects argument and
257 * sets the argument with it.
260 gst_util_set_object_arg (GObject * object, const gchar * name, const gchar * value)
263 GParamSpec *paramspec;
265 paramspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), name);
271 GST_DEBUG (0, "paramspec->flags is %d, paramspec->value_type is %d\n",
272 paramspec->flags, paramspec->value_type);
274 if (paramspec->flags & G_PARAM_WRITABLE) {
275 switch (paramspec->value_type) {
277 g_object_set (G_OBJECT (object), name, value, NULL);
283 sscanf (value, "%d", &i);
284 g_object_set (G_OBJECT (object), name, i, NULL);
290 sscanf (value, "%u", &i);
291 g_object_set (G_OBJECT (object), name, i, NULL);
297 sscanf (value, "%ld", &i);
298 g_object_set (G_OBJECT (object), name, i, NULL);
304 sscanf (value, "%lu", &i);
305 g_object_set (G_OBJECT (object), name, i, NULL);
308 case G_TYPE_BOOLEAN:{
311 if (!strncmp ("true", value, 4))
313 g_object_set (G_OBJECT (object), name, i, NULL);
319 sscanf (value, "%c", &i);
320 g_object_set (G_OBJECT (object), name, i, NULL);
326 sscanf (value, "%c", &i);
327 g_object_set (G_OBJECT (object), name, i, NULL);
333 sscanf (value, "%f", &i);
334 g_object_set (G_OBJECT (object), name, i, NULL);
340 sscanf (value, "%g", &i);
341 g_object_set (G_OBJECT (object), name, (gdouble) i, NULL);
345 if (G_IS_PARAM_SPEC_ENUM (paramspec)) {
348 sscanf (value, "%d", &i);
349 g_object_set (G_OBJECT (object), name, i, NULL);
351 else if (paramspec->value_type == GST_TYPE_FILENAME) {
352 g_object_set (G_OBJECT (object), name, value, NULL);
360 /* -----------------------------------------------------
362 * The following code will be moved out of the main
363 * gstreamer library someday.
368 #include "gstprops.h"
369 #include "gstpropsprivate.h"
372 string_append_indent (GString * str, gint count)
376 for (xx = 0; xx < count; xx++)
377 g_string_append_c (str, ' ');
381 gst_print_props (GString * buf, gint indent, GList * props, gboolean showname)
387 for (elem = props; elem; elem = g_list_next (elem)) {
388 GstPropsEntry *prop = elem->data;
389 const gchar *name = g_quark_to_string (prop->propid);
391 if (width < strlen (name))
392 width = strlen (name);
395 for (elem = props; elem; elem = g_list_next (elem)) {
396 GstPropsEntry *prop = elem->data;
398 string_append_indent (buf, indent);
400 const gchar *name = g_quark_to_string (prop->propid);
402 g_string_append (buf, name);
403 string_append_indent (buf, 2 + width - strlen (name));
406 switch (prop->propstype) {
407 case GST_PROPS_INT_ID:
408 g_string_append_printf (buf, "%d (int)\n", prop->data.int_data);
410 case GST_PROPS_INT_RANGE_ID:
411 g_string_append_printf (buf, "%d - %d (int)\n",
412 prop->data.int_range_data.min, prop->data.int_range_data.max);
414 case GST_PROPS_FLOAT_ID:
415 g_string_append_printf (buf, "%f (float)\n", prop->data.float_data);
417 case GST_PROPS_FLOAT_RANGE_ID:
418 g_string_append_printf (buf, "%f - %f (float)\n",
419 prop->data.float_range_data.min, prop->data.float_range_data.max);
421 case GST_PROPS_BOOL_ID:
422 g_string_append_printf (buf, "%s\n", prop->data.bool_data ? "TRUE" : "FALSE");
424 case GST_PROPS_STRING_ID:
425 g_string_append_printf (buf, "\"%s\"\n", prop->data.string_data.string);
427 case GST_PROPS_FOURCC_ID:
428 g_string_append_printf (buf, "'%c%c%c%c' (fourcc)\n",
429 prop->data.fourcc_data & 0xff,
430 prop->data.fourcc_data >> 8 & 0xff,
431 prop->data.fourcc_data >> 16 & 0xff,
432 prop->data.fourcc_data >> 24 & 0xff);
434 case GST_PROPS_LIST_ID:
435 gst_print_props (buf, indent + 2, prop->data.list_data.entries, FALSE);
438 g_string_append_printf (buf, "unknown proptype %d\n", prop->propstype);
445 * gst_print_pad_caps:
446 * @buf: the buffer to print the caps in
447 * @indent: initial indentation
448 * @pad: the pad to print the caps from
450 * Write the pad capabilities in a human readable format into
454 gst_print_pad_caps (GString * buf, gint indent, GstPad * pad)
459 realpad = GST_PAD_REALIZE (pad);
460 caps = realpad->caps;
463 string_append_indent (buf, indent);
464 g_string_printf (buf, "%s:%s has no capabilities", GST_DEBUG_PAD_NAME (pad));
472 string_append_indent (buf, indent);
473 g_string_append_printf (buf, "Cap[%d]: %s\n", capx++, caps->name);
475 type = gst_type_find_by_id (caps->id);
476 string_append_indent (buf, indent + 2);
477 g_string_append_printf (buf, "MIME type: %s\n", type->mime ? type->mime : "unknown/unknown");
479 if (caps->properties)
480 gst_print_props (buf, indent + 4, caps->properties->properties, TRUE);
488 * gst_print_element_args:
489 * @buf: the buffer to print the args in
490 * @indent: initial indentation
491 * @element: the element to print the args of
493 * Print the element argument in a human readable format in the given
497 gst_print_element_args (GString * buf, gint indent, GstElement * element)
503 GParamSpec **property_specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (element),
507 for (px = 0; px < num_properties; px++) {
508 GParamSpec *param = property_specs[px];
510 if (width < strlen (param->name))
511 width = strlen (param->name);
514 for (px = 0; px < num_properties; px++) {
515 GParamSpec *param = property_specs[px];
520 g_value_init (&value, param->value_type);
521 g_object_get_property (G_OBJECT (element), param->name, &value);
523 string_append_indent (buf, indent);
524 g_string_append (buf, param->name);
525 string_append_indent (buf, 2 + width - strlen (param->name));
527 if (G_IS_PARAM_SPEC_ENUM (param)) {
531 values = G_ENUM_CLASS (g_type_class_ref (param->value_type))->values;
533 values = gtk_type_enum_get_values (param->value_type);
536 g_string_append_printf (buf, "%s (%s)",
537 values[g_value_get_enum (&value)].value_nick,
538 g_type_name (G_VALUE_TYPE (&value)));
541 switch (G_VALUE_TYPE (&value)) {
543 g_string_append_printf (buf, "\"%s\"", g_value_get_string (&value));
546 g_string_append (buf, g_value_get_boolean (&value) ? "TRUE" : "FALSE");
549 gulong val = g_value_get_ulong (&value);
551 g_string_append_printf (buf, "%lu (0x%lx)", val, val);
555 glong val = g_value_get_long (&value);
557 g_string_append_printf (buf, "%ld (0x%lx)", val, val);
561 guint val = g_value_get_uint (&value);
563 g_string_append_printf (buf, "%u (0x%x)", val, val);
567 gint val = g_value_get_int (&value);
569 g_string_append_printf (buf, "%d (0x%x)", val, val);
573 g_string_append_printf (buf, "%f", g_value_get_float (&value));
576 g_string_append_printf (buf, "%f", g_value_get_double (&value));
579 g_string_append_printf (buf, "unknown value_type %d", G_VALUE_TYPE (&value));
584 g_string_append_c (buf, '\n');
586 if (G_VALUE_TYPE (&value)) {
587 g_value_unset (&value);
590 g_free (property_specs);
596 * @argname: a property it might have
597 * @arg_type: the type of the argument it should have
599 * Determines whether this @object has a property of name
600 * @argname and of type @arg_type
602 * Return value: TRUE if it has the prop, else FALSE
605 gst_util_has_arg (GObject *object, const gchar *argname, GType arg_type)
609 pspec = g_object_class_find_property (
610 G_OBJECT_GET_CLASS (object), argname);
615 if (pspec->value_type != arg_type)