2 * Copyright (C) 2005 Stefan Kost <ensonic@users.sf.net>
4 * gstchildproxy.c: interface for multi child elements
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:gstchildproxy
24 * @short_description: Interface for multi child elements.
27 * This interface abstracts handling of property sets for elements with
28 * children. Imagine elements such as mixers or polyphonic generators. They all
29 * have multiple #GstPad or some kind of voice objects. Another use case are
30 * container elements like #GstBin.
31 * The element implementing the interface acts as a parent for those child
34 * By implementing this interface the child properties can be accessed from the
35 * parent element by using gst_child_proxy_get() and gst_child_proxy_set().
37 * Property names are written as "child-name::property-name". The whole naming
38 * scheme is recursive. Thus "child1::child2::property" is valid too, if
39 * "child1" and "child2" implement the #GstChildProxy interface.
42 * it would be nice to use GObject instead of GstObject.
43 * This change would allow to propose the interface for inclusion with
44 * glib/gobject. IMHO this is useful for GtkContainer and compound widgets too.
47 #include "gst_private.h"
49 #include "gstchildproxy.h"
50 #include <gobject/gvaluecollector.h>
60 static guint signals[LAST_SIGNAL] = { 0 };
63 gst_child_proxy_default_get_child_by_name (GstChildProxy * parent,
67 GstObject *object, *result;
70 g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), NULL);
71 g_return_val_if_fail (name != NULL, NULL);
75 count = gst_child_proxy_get_children_count (parent);
76 for (i = 0; i < count; i++) {
79 if (!(object = gst_child_proxy_get_child_by_index (parent, i)))
82 object_name = gst_object_get_name (object);
83 if (object_name == NULL) {
84 g_warning ("child %u of parent %s has no name", i,
85 GST_OBJECT_NAME (parent));
88 eq = g_str_equal (object_name, name);
96 gst_object_unref (object);
103 * gst_child_proxy_get_child_by_name:
104 * @parent: the parent object to get the child from
105 * @name: the childs name
107 * Looks up a child element by the given name.
109 * Implementors can use #GstObject together with gst_object_get_name()
111 * Returns: (transfer full): the child object or %NULL if not found. Unref
117 gst_child_proxy_get_child_by_name (GstChildProxy * parent, const gchar * name)
119 g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), 0);
121 return (GST_CHILD_PROXY_GET_INTERFACE (parent)->get_child_by_name (parent,
126 * gst_child_proxy_get_child_by_index:
127 * @parent: the parent object to get the child from
128 * @index: the childs position in the child list
130 * Fetches a child by its number.
132 * Returns: (transfer full): the child object or %NULL if not found (index
133 * too high). Unref after usage.
138 gst_child_proxy_get_child_by_index (GstChildProxy * parent, guint index)
140 g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), NULL);
142 return (GST_CHILD_PROXY_GET_INTERFACE (parent)->get_child_by_index (parent,
147 * gst_child_proxy_get_children_count:
148 * @parent: the parent object
150 * Gets the number of child objects this parent contains.
152 * Returns: the number of child objects
157 gst_child_proxy_get_children_count (GstChildProxy * parent)
159 g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), 0);
161 return (GST_CHILD_PROXY_GET_INTERFACE (parent)->get_children_count (parent));
165 * gst_child_proxy_lookup:
166 * @object: object to lookup the property in
167 * @name: name of the property to look up
168 * @target: (out) (allow-none) (transfer full): pointer to a #GstObject that
169 * takes the real object to set property on
170 * @pspec: (out) (allow-none) (transfer full): pointer to take the #GParamSpec
171 * describing the property
173 * Looks up which object and #GParamSpec would be effected by the given @name.
175 * Returns: TRUE if @target and @pspec could be found. FALSE otherwise. In that
176 * case the values for @pspec and @target are not modified. Unref @target after
182 gst_child_proxy_lookup (GstObject * object, const gchar * name,
183 GstObject ** target, GParamSpec ** pspec)
185 gboolean res = FALSE;
186 gchar **names, **current;
188 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
189 g_return_val_if_fail (name != NULL, FALSE);
191 gst_object_ref (object);
193 current = names = g_strsplit (name, "::", -1);
197 if (!GST_IS_CHILD_PROXY (object)) {
199 ("object %s is not a parent, so you cannot request a child by name %s",
200 GST_OBJECT_NAME (object), current[0]);
203 next = gst_child_proxy_get_child_by_name (GST_CHILD_PROXY (object),
206 GST_INFO ("no such object %s", current[0]);
209 gst_object_unref (object);
213 if (current[1] == NULL) {
215 g_object_class_find_property (G_OBJECT_GET_CLASS (object), current[0]);
217 GST_INFO ("no param spec named %s", current[0]);
222 gst_object_ref (object);
228 gst_object_unref (object);
234 * gst_child_proxy_get_property:
235 * @object: object to query
236 * @name: name of the property
237 * @value: (out caller-allocates): a #GValue that should take the result.
239 * Gets a single property using the GstChildProxy mechanism.
240 * You are responsible for freeing it by calling g_value_unset()
243 gst_child_proxy_get_property (GstObject * object, const gchar * name,
249 g_return_if_fail (GST_IS_OBJECT (object));
250 g_return_if_fail (name != NULL);
251 g_return_if_fail (G_IS_VALUE (value));
253 if (!gst_child_proxy_lookup (object, name, &target, &pspec))
256 g_object_get_property (G_OBJECT (target), pspec->name, value);
257 gst_object_unref (target);
263 g_warning ("no property %s in object %s", name, GST_OBJECT_NAME (object));
269 * gst_child_proxy_get_valist:
270 * @object: the object to query
271 * @first_property_name: name of the first property to get
272 * @var_args: return location for the first property, followed optionally by more name/return location pairs, followed by NULL
274 * Gets properties of the parent object and its children.
277 gst_child_proxy_get_valist (GstObject * object,
278 const gchar * first_property_name, va_list var_args)
282 GValue value = { 0, };
286 g_return_if_fail (G_IS_OBJECT (object));
288 name = first_property_name;
290 /* iterate over pairs */
292 if (!gst_child_proxy_lookup (object, name, &target, &pspec))
295 g_value_init (&value, pspec->value_type);
296 g_object_get_property (G_OBJECT (target), pspec->name, &value);
297 gst_object_unref (target);
299 G_VALUE_LCOPY (&value, var_args, 0, &error);
302 g_value_unset (&value);
303 name = va_arg (var_args, gchar *);
309 g_warning ("no property %s in object %s", name, GST_OBJECT_NAME (object));
314 g_warning ("error copying value %s in object %s: %s", pspec->name,
315 GST_OBJECT_NAME (object), error);
316 g_value_unset (&value);
322 * gst_child_proxy_get:
323 * @object: the parent object
324 * @first_property_name: name of the first property to get
325 * @...: return location for the first property, followed optionally by more name/return location pairs, followed by NULL
327 * Gets properties of the parent object and its children.
330 gst_child_proxy_get (GstObject * object, const gchar * first_property_name, ...)
334 g_return_if_fail (GST_IS_OBJECT (object));
336 va_start (var_args, first_property_name);
337 gst_child_proxy_get_valist (object, first_property_name, var_args);
342 * gst_child_proxy_set_property:
343 * @object: the parent object
344 * @name: name of the property to set
345 * @value: new #GValue for the property
347 * Sets a single property using the GstChildProxy mechanism.
350 gst_child_proxy_set_property (GstObject * object, const gchar * name,
351 const GValue * value)
356 g_return_if_fail (GST_IS_OBJECT (object));
357 g_return_if_fail (name != NULL);
358 g_return_if_fail (G_IS_VALUE (value));
360 if (!gst_child_proxy_lookup (object, name, &target, &pspec))
363 g_object_set_property (G_OBJECT (target), pspec->name, value);
364 gst_object_unref (target);
369 g_warning ("cannot set property %s on object %s", name,
370 GST_OBJECT_NAME (object));
376 * gst_child_proxy_set_valist:
377 * @object: the parent object
378 * @first_property_name: name of the first property to set
379 * @var_args: value for the first property, followed optionally by more name/value pairs, followed by NULL
381 * Sets properties of the parent object and its children.
384 gst_child_proxy_set_valist (GstObject * object,
385 const gchar * first_property_name, va_list var_args)
389 GValue value = { 0, };
393 g_return_if_fail (G_IS_OBJECT (object));
395 name = first_property_name;
397 /* iterate over pairs */
399 if (!gst_child_proxy_lookup (object, name, &target, &pspec))
402 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
403 G_VALUE_NOCOPY_CONTENTS, &error);
408 g_object_set_property (G_OBJECT (target), pspec->name, &value);
409 gst_object_unref (target);
411 g_value_unset (&value);
412 name = va_arg (var_args, gchar *);
418 g_warning ("no property %s in object %s", name, GST_OBJECT_NAME (object));
423 g_warning ("error copying value %s in object %s: %s", pspec->name,
424 GST_OBJECT_NAME (object), error);
425 g_value_unset (&value);
426 gst_object_unref (target);
432 * gst_child_proxy_set:
433 * @object: the parent object
434 * @first_property_name: name of the first property to set
435 * @...: value for the first property, followed optionally by more name/value pairs, followed by NULL
437 * Sets properties of the parent object and its children.
440 gst_child_proxy_set (GstObject * object, const gchar * first_property_name, ...)
444 g_return_if_fail (GST_IS_OBJECT (object));
446 va_start (var_args, first_property_name);
447 gst_child_proxy_set_valist (object, first_property_name, var_args);
452 * gst_child_proxy_child_added:
453 * @object: the parent object
454 * @child: the newly added child
456 * Emits the "child-added" signal.
459 gst_child_proxy_child_added (GstObject * object, GstObject * child)
461 g_signal_emit (G_OBJECT (object), signals[CHILD_ADDED], 0, child);
465 * gst_child_proxy_child_removed:
466 * @object: the parent object
467 * @child: the removed child
469 * Emits the "child-removed" signal.
472 gst_child_proxy_child_removed (GstObject * object, GstObject * child)
474 g_signal_emit (G_OBJECT (object), signals[CHILD_REMOVED], 0, child);
477 /* gobject methods */
480 gst_child_proxy_class_init (gpointer g_class, gpointer class_data)
482 GstChildProxyInterface *iface = (GstChildProxyInterface *) g_class;
484 iface->get_child_by_name = gst_child_proxy_default_get_child_by_name;
488 gst_child_proxy_base_init (gpointer g_class)
490 static gboolean initialized = FALSE;
493 /* create interface signals and properties here. */
495 * GstChildProxy::child-added:
496 * @child_proxy: the #GstChildProxy
497 * @object: the #GObject that was added
499 * Will be emitted after the @object was added to the @child_proxy.
501 signals[CHILD_ADDED] =
502 g_signal_new ("child-added", G_TYPE_FROM_CLASS (g_class),
503 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstChildProxyInterface,
504 child_added), NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
508 * GstChildProxy::child-removed:
509 * @child_proxy: the #GstChildProxy
510 * @object: the #GObject that was removed
512 * Will be emitted after the @object was removed from the @child_proxy.
514 signals[CHILD_REMOVED] =
515 g_signal_new ("child-removed", G_TYPE_FROM_CLASS (g_class),
516 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstChildProxyInterface,
517 child_removed), NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
525 gst_child_proxy_get_type (void)
527 static volatile gsize type = 0;
529 if (g_once_init_enter (&type)) {
531 static const GTypeInfo info = {
532 sizeof (GstChildProxyInterface),
533 gst_child_proxy_base_init, /* base_init */
534 NULL, /* base_finalize */
535 gst_child_proxy_class_init, /* class_init */
536 NULL, /* class_finalize */
537 NULL, /* class_data */
540 NULL /* instance_init */
544 g_type_register_static (G_TYPE_INTERFACE, "GstChildProxy", &info, 0);
546 g_type_interface_add_prerequisite (_type, GST_TYPE_OBJECT);
547 g_once_init_leave (&type, (gsize) _type);