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 make gst_child_proxy_get_child_by_name virtual too and
43 * use GObject instead of GstObject. We could eventually provide the current
44 * implementation as a default if children are GstObjects.
45 * This change would allow to propose the interface for inclusion with
46 * glib/gobject. IMHO this is useful for GtkContainer and compound widgets too.
49 #include "gst_private.h"
51 #include "gstchildproxy.h"
52 #include "gstmarshal.h"
53 #include <gobject/gvaluecollector.h>
63 static guint signals[LAST_SIGNAL] = { 0 };
66 * gst_child_proxy_get_child_by_name:
67 * @parent: the parent object to get the child from
68 * @name: the childs name
70 * Looks up a child element by the given name.
72 * Implementors can use #GstObject together with gst_object_get_name()
74 * Returns: (transfer full): the child object or %NULL if not found. Unref
80 gst_child_proxy_get_child_by_name (GstChildProxy * parent, const gchar * name)
83 GstObject *object, *result;
86 g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), NULL);
87 g_return_val_if_fail (name != NULL, NULL);
91 count = gst_child_proxy_get_children_count (parent);
92 for (i = 0; i < count; i++) {
95 if (!(object = gst_child_proxy_get_child_by_index (parent, i)))
98 object_name = gst_object_get_name (object);
99 if (object_name == NULL) {
100 g_warning ("child %u of parent %s has no name", i,
101 GST_OBJECT_NAME (parent));
104 eq = g_str_equal (object_name, name);
105 g_free (object_name);
112 gst_object_unref (object);
118 * gst_child_proxy_get_child_by_index:
119 * @parent: the parent object to get the child from
120 * @index: the childs position in the child list
122 * Fetches a child by its number.
124 * Returns: (transfer full): the child object or %NULL if not found (index
125 * too high). Unref after usage.
130 gst_child_proxy_get_child_by_index (GstChildProxy * parent, guint index)
132 g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), NULL);
134 return (GST_CHILD_PROXY_GET_INTERFACE (parent)->get_child_by_index (parent,
139 * gst_child_proxy_get_children_count:
140 * @parent: the parent object
142 * Gets the number of child objects this parent contains.
144 * Returns: the number of child objects
149 gst_child_proxy_get_children_count (GstChildProxy * parent)
151 g_return_val_if_fail (GST_IS_CHILD_PROXY (parent), 0);
153 return (GST_CHILD_PROXY_GET_INTERFACE (parent)->get_children_count (parent));
157 * gst_child_proxy_lookup:
158 * @object: object to lookup the property in
159 * @name: name of the property to look up
160 * @target: (out) (allow-none) (transfer full): pointer to a #GstObject that
161 * takes the real object to set property on
162 * @pspec: (out) (allow-none) (transfer full): pointer to take the #GParamSpec
163 * describing the property
165 * Looks up which object and #GParamSpec would be effected by the given @name.
167 * Returns: TRUE if @target and @pspec could be found. FALSE otherwise. In that
168 * case the values for @pspec and @target are not modified. Unref @target after
174 gst_child_proxy_lookup (GstObject * object, const gchar * name,
175 GstObject ** target, GParamSpec ** pspec)
177 gboolean res = FALSE;
178 gchar **names, **current;
180 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
181 g_return_val_if_fail (name != NULL, FALSE);
183 gst_object_ref (object);
185 current = names = g_strsplit (name, "::", -1);
189 if (!GST_IS_CHILD_PROXY (object)) {
191 ("object %s is not a parent, so you cannot request a child by name %s",
192 GST_OBJECT_NAME (object), current[0]);
195 next = gst_child_proxy_get_child_by_name (GST_CHILD_PROXY (object),
198 GST_INFO ("no such object %s", current[0]);
201 gst_object_unref (object);
205 if (current[1] == NULL) {
207 g_object_class_find_property (G_OBJECT_GET_CLASS (object), current[0]);
209 GST_INFO ("no param spec named %s", current[0]);
214 gst_object_ref (object);
220 gst_object_unref (object);
226 * gst_child_proxy_get_property:
227 * @object: object to query
228 * @name: name of the property
229 * @value: (out caller-allocates): a #GValue that should take the result.
231 * Gets a single property using the GstChildProxy mechanism.
232 * You are responsible for freeing it by calling g_value_unset()
235 gst_child_proxy_get_property (GstObject * object, const gchar * name,
241 g_return_if_fail (GST_IS_OBJECT (object));
242 g_return_if_fail (name != NULL);
243 g_return_if_fail (G_IS_VALUE (value));
245 if (!gst_child_proxy_lookup (object, name, &target, &pspec))
248 g_object_get_property (G_OBJECT (target), pspec->name, value);
249 gst_object_unref (target);
255 g_warning ("no property %s in object %s", name, GST_OBJECT_NAME (object));
261 * gst_child_proxy_get_valist:
262 * @object: the object to query
263 * @first_property_name: name of the first property to get
264 * @var_args: return location for the first property, followed optionally by more name/return location pairs, followed by NULL
266 * Gets properties of the parent object and its children.
269 gst_child_proxy_get_valist (GstObject * object,
270 const gchar * first_property_name, va_list var_args)
274 GValue value = { 0, };
278 g_return_if_fail (G_IS_OBJECT (object));
280 name = first_property_name;
282 /* iterate over pairs */
284 if (!gst_child_proxy_lookup (object, name, &target, &pspec))
287 g_value_init (&value, pspec->value_type);
288 g_object_get_property (G_OBJECT (target), pspec->name, &value);
289 gst_object_unref (target);
291 G_VALUE_LCOPY (&value, var_args, 0, &error);
294 g_value_unset (&value);
295 name = va_arg (var_args, gchar *);
301 g_warning ("no property %s in object %s", name, GST_OBJECT_NAME (object));
306 g_warning ("error copying value %s in object %s: %s", pspec->name,
307 GST_OBJECT_NAME (object), error);
308 g_value_unset (&value);
314 * gst_child_proxy_get:
315 * @object: the parent object
316 * @first_property_name: name of the first property to get
317 * @...: return location for the first property, followed optionally by more name/return location pairs, followed by NULL
319 * Gets properties of the parent object and its children.
322 gst_child_proxy_get (GstObject * object, const gchar * first_property_name, ...)
326 g_return_if_fail (GST_IS_OBJECT (object));
328 va_start (var_args, first_property_name);
329 gst_child_proxy_get_valist (object, first_property_name, var_args);
334 * gst_child_proxy_set_property:
335 * @object: the parent object
336 * @name: name of the property to set
337 * @value: new #GValue for the property
339 * Sets a single property using the GstChildProxy mechanism.
342 gst_child_proxy_set_property (GstObject * object, const gchar * name,
343 const GValue * value)
348 g_return_if_fail (GST_IS_OBJECT (object));
349 g_return_if_fail (name != NULL);
350 g_return_if_fail (G_IS_VALUE (value));
352 if (!gst_child_proxy_lookup (object, name, &target, &pspec))
355 g_object_set_property (G_OBJECT (target), pspec->name, value);
356 gst_object_unref (target);
361 g_warning ("cannot set property %s on object %s", name,
362 GST_OBJECT_NAME (object));
368 * gst_child_proxy_set_valist:
369 * @object: the parent object
370 * @first_property_name: name of the first property to set
371 * @var_args: value for the first property, followed optionally by more name/value pairs, followed by NULL
373 * Sets properties of the parent object and its children.
376 gst_child_proxy_set_valist (GstObject * object,
377 const gchar * first_property_name, va_list var_args)
381 GValue value = { 0, };
385 g_return_if_fail (G_IS_OBJECT (object));
387 name = first_property_name;
389 /* iterate over pairs */
391 if (!gst_child_proxy_lookup (object, name, &target, &pspec))
394 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
395 G_VALUE_NOCOPY_CONTENTS, &error);
400 g_object_set_property (G_OBJECT (target), pspec->name, &value);
401 gst_object_unref (target);
403 g_value_unset (&value);
404 name = va_arg (var_args, gchar *);
410 g_warning ("no property %s in object %s", name, GST_OBJECT_NAME (object));
415 g_warning ("error copying value %s in object %s: %s", pspec->name,
416 GST_OBJECT_NAME (object), error);
417 g_value_unset (&value);
418 gst_object_unref (target);
424 * gst_child_proxy_set:
425 * @object: the parent object
426 * @first_property_name: name of the first property to set
427 * @...: value for the first property, followed optionally by more name/value pairs, followed by NULL
429 * Sets properties of the parent object and its children.
432 gst_child_proxy_set (GstObject * object, const gchar * first_property_name, ...)
436 g_return_if_fail (GST_IS_OBJECT (object));
438 va_start (var_args, first_property_name);
439 gst_child_proxy_set_valist (object, first_property_name, var_args);
444 * gst_child_proxy_child_added:
445 * @object: the parent object
446 * @child: the newly added child
448 * Emits the "child-added" signal.
451 gst_child_proxy_child_added (GstObject * object, GstObject * child)
453 g_signal_emit (G_OBJECT (object), signals[CHILD_ADDED], 0, child);
457 * gst_child_proxy_child_removed:
458 * @object: the parent object
459 * @child: the removed child
461 * Emits the "child-removed" signal.
464 gst_child_proxy_child_removed (GstObject * object, GstObject * child)
466 g_signal_emit (G_OBJECT (object), signals[CHILD_REMOVED], 0, child);
469 /* gobject methods */
472 gst_child_proxy_base_init (gpointer g_class)
474 static gboolean initialized = FALSE;
477 /* create interface signals and properties here. */
479 * GstChildProxy::child-added:
480 * @child_proxy: the #GstChildProxy
481 * @object: the #GObject that was added
483 * Will be emitted after the @object was added to the @child_proxy.
485 /* FIXME 0.11: use GST_TYPE_OBJECT as GstChildProxy only
486 * supports GstObjects */
487 signals[CHILD_ADDED] =
488 g_signal_new ("child-added", G_TYPE_FROM_CLASS (g_class),
489 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstChildProxyInterface,
490 child_added), NULL, NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
494 * GstChildProxy::child-removed:
495 * @child_proxy: the #GstChildProxy
496 * @object: the #GObject that was removed
498 * Will be emitted after the @object was removed from the @child_proxy.
500 /* FIXME 0.11: use GST_TYPE_OBJECT as GstChildProxy only
501 * supports GstObjects */
502 signals[CHILD_REMOVED] =
503 g_signal_new ("child-removed", G_TYPE_FROM_CLASS (g_class),
504 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstChildProxyInterface,
505 child_removed), NULL, NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE,
513 gst_child_proxy_get_type (void)
515 static volatile gsize type = 0;
517 if (g_once_init_enter (&type)) {
519 static const GTypeInfo info = {
520 sizeof (GstChildProxyInterface),
521 gst_child_proxy_base_init, /* base_init */
522 NULL, /* base_finalize */
523 NULL, /* class_init */
524 NULL, /* class_finalize */
525 NULL, /* class_data */
528 NULL /* instance_init */
532 g_type_register_static (G_TYPE_INTERFACE, "GstChildProxy", &info, 0);
534 g_type_interface_add_prerequisite (_type, GST_TYPE_OBJECT);
535 g_once_init_leave (&type, (gsize) _type);