From 5e827398d077bd2c2a560f2be6bedeb7a868fe92 Mon Sep 17 00:00:00 2001 From: Pierre d'Herbemont Date: Sun, 19 Aug 2007 23:38:02 +0000 Subject: [PATCH] control/tree.c: Add a new structure to work with tree. --- include/vlc/libvlc.h | 51 +++++++ include/vlc/libvlc_structures.h | 16 +++ src/control/libvlc_internal.h | 9 ++ src/control/tree.c | 248 ++++++++++++++++++++++++++++++++ 4 files changed, 324 insertions(+) create mode 100644 src/control/tree.c diff --git a/include/vlc/libvlc.h b/include/vlc/libvlc.h index 12f7daf431..cf1bd8f35f 100644 --- a/include/vlc/libvlc.h +++ b/include/vlc/libvlc.h @@ -121,6 +121,57 @@ VLC_PUBLIC_API void libvlc_destroy( libvlc_instance_t *, libvlc_exception_t * ); /** @}*/ +/***************************************************************************** + * Tree + *****************************************************************************/ +/** defgroup libvlc_tree Tree + * \ingroup libvlc + * LibVLC Tree. A tree holds an item plus several subtrees. + * @{ + */ +VLC_PUBLIC_API libvlc_tree_t * + libvlc_tree_new_with_media_list_as_item( libvlc_media_list_t * p_mlist, + libvlc_exception_t * p_e ); + +VLC_PUBLIC_API libvlc_tree_t * + libvlc_tree_new_with_string_as_item( const char * psz, + libvlc_exception_t * p_e ); +VLC_PUBLIC_API void + libvlc_tree_release( libvlc_tree_t * p_tree ); + +VLC_PUBLIC_API void + libvlc_tree_retain( libvlc_tree_t * p_tree ); + +VLC_PUBLIC_API char * + libvlc_tree_item_as_string( libvlc_tree_t * p_tree, + libvlc_exception_t * p_e ); + +VLC_PUBLIC_API libvlc_media_list_t * + libvlc_tree_item_as_media_list( libvlc_tree_t * p_tree, + libvlc_exception_t * p_e ); + +VLC_PUBLIC_API int + libvlc_tree_subtree_count( libvlc_tree_t * p_tree, libvlc_exception_t * p_e ); + +VLC_PUBLIC_API libvlc_tree_t * + libvlc_tree_subtree_at_index( libvlc_tree_t * p_tree, + int index, + libvlc_exception_t * p_e ); + +VLC_PUBLIC_API void + libvlc_tree_insert_subtree_at_index( libvlc_tree_t * p_tree, + libvlc_tree_t * p_subtree, + int index, + libvlc_exception_t * p_e ); + +VLC_PUBLIC_API void + libvlc_tree_remove_subtree_at_index( libvlc_tree_t * p_tree, + int index, + libvlc_exception_t * p_e ); + +/**@} */ + + /***************************************************************************** * Media descriptor diff --git a/include/vlc/libvlc_structures.h b/include/vlc/libvlc_structures.h index 96fa3a6afd..01ade2fbaa 100644 --- a/include/vlc/libvlc_structures.h +++ b/include/vlc/libvlc_structures.h @@ -52,6 +52,22 @@ typedef struct libvlc_exception_t /**@} */ +/***************************************************************************** + * Tree + *****************************************************************************/ +/** defgroup libvlc_tree Tree + * \ingroup libvlc + * LibVLC Tree + * @{ + */ + +typedef void (*libvlc_retain_function)(void *); +typedef void (*libvlc_release_function)(void *); + +typedef struct libvlc_tree_t libvlc_tree_t; + +/**@} */ + /***************************************************************************** * Tag *****************************************************************************/ diff --git a/src/control/libvlc_internal.h b/src/control/libvlc_internal.h index 1e985e9bf1..4d74fc3c9d 100644 --- a/src/control/libvlc_internal.h +++ b/src/control/libvlc_internal.h @@ -80,6 +80,15 @@ struct libvlc_tag_query_t }; +struct libvlc_tree_t +{ + int i_refcount; + void * p_item; /* For dynamic sublist */ + libvlc_retain_function pf_item_retain; + libvlc_release_function pf_item_release; + DECL_ARRAY(struct libvlc_tree_t *) subtrees; /* For dynamic sublist */ +}; + struct libvlc_media_list_t { libvlc_event_manager_t * p_event_manager; diff --git a/src/control/tree.c b/src/control/tree.c new file mode 100644 index 0000000000..913b434aab --- /dev/null +++ b/src/control/tree.c @@ -0,0 +1,248 @@ +/***************************************************************************** + * media_list.c: libvlc tree functions + ***************************************************************************** + * Copyright (C) 2007 the VideoLAN team + * $Id$ + * + * Authors: Pierre d'Herbemont + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#include "libvlc_internal.h" +#include +#include +#include "vlc_arrays.h" + +/* + * Private libvlc functions + */ + +/************************************************************************** + * notify_subtree_addition (private) + * + * Do the appropriate action when a subtree is added. + **************************************************************************/ +static void +notify_subtree_addition( libvlc_tree_t * p_tree, + libvlc_tree_t * p_subtree, + int index ) +{ + +} + +/************************************************************************** + * notify_subtree_deletion (private) + * + * Do the appropriate action when a subtree is deleted. + **************************************************************************/ +static void +notify_subtree_deletion( libvlc_tree_t * p_tree, + libvlc_tree_t * p_subtree, + int index ) +{ + +} + +/************************************************************************** + * new (Private) + **************************************************************************/ +static libvlc_tree_t * +libvlc_tree_new( libvlc_retain_function item_retain, + libvlc_retain_function item_release, + void * item, + libvlc_exception_t * p_e ) +{ + (void)p_e; + libvlc_tree_t * p_tree; + + p_tree = malloc(sizeof(libvlc_tree_t)); + + if( !p_tree ) + return NULL; + + p_tree->i_refcount = 1; + p_tree->p_item = item; + ARRAY_INIT( p_tree->subtrees ); + return p_tree; +} + +/************************************************************************** + * item (Private) + **************************************************************************/ +static void * +libvlc_tree_item( libvlc_tree_t * p_tree, + libvlc_exception_t * p_e ) +{ + if( p_tree->pf_item_retain ) p_tree->pf_item_retain( p_tree->p_item ); + return p_tree->p_item; +} + + +/* + * Public libvlc functions + */ + + +/************************************************************************** + * new_with_media_list (Public) + **************************************************************************/ +libvlc_tree_t * +libvlc_tree_new_with_media_list_as_item( libvlc_media_list_t * p_mlist, + libvlc_exception_t * p_e ) +{ + (void)p_e; + libvlc_tree_t * p_tree = libvlc_tree_new( + (libvlc_retain_function)libvlc_media_list_retain, + (libvlc_release_function)libvlc_media_list_release, + p_mlist, + p_e ); + + return p_tree; +} + +/************************************************************************** + * new_with_string (Public) + **************************************************************************/ +libvlc_tree_t * +libvlc_tree_new_with_string_as_item( const char * psz, + libvlc_exception_t * p_e ) +{ + (void)p_e; + libvlc_tree_t * p_tree = libvlc_tree_new( NULL, + (libvlc_release_function)free, + psz ? strdup( psz ): NULL, + p_e ); + + return p_tree; +} + +/************************************************************************** + * release (Public) + **************************************************************************/ +void libvlc_tree_release( libvlc_tree_t * p_tree ) +{ + libvlc_tree_t * p_subtree; + + p_tree->i_refcount--; + + if( p_tree->i_refcount > 0 ) + return; + + if( p_tree->pf_item_release && p_tree->p_item ) + p_tree->pf_item_release( p_tree->p_item ); + + FOREACH_ARRAY( p_subtree, p_tree->subtrees ) + libvlc_tree_release( p_subtree ); + FOREACH_END() + + free( p_tree ); +} + +/************************************************************************** + * retain (Public) + **************************************************************************/ +void libvlc_tree_retain( libvlc_tree_t * p_tree ) +{ + p_tree->i_refcount++; +} + +/************************************************************************** + * item_as_string (Public) + **************************************************************************/ +char * +libvlc_tree_item_as_string( libvlc_tree_t * p_tree, + libvlc_exception_t * p_e ) +{ + (void)p_e; + return p_tree->p_item ? strdup( p_tree->p_item ) : NULL; +} + +/************************************************************************** + * item_as_media_list (Public) + **************************************************************************/ +libvlc_media_list_t * +libvlc_tree_item_as_media_list( libvlc_tree_t * p_tree, + libvlc_exception_t * p_e ) +{ + /* Automatically retained */ + return libvlc_tree_item( p_tree, p_e ); +} + +/************************************************************************** + * count (Public) + **************************************************************************/ +int +libvlc_tree_subtree_count( libvlc_tree_t * p_tree, libvlc_exception_t * p_e ) +{ + (void)p_e; + return p_tree->subtrees.i_size; +} + +/************************************************************************** + * subtree_at_index (Public) + * + * Note: The subtree won't be retained + **************************************************************************/ +libvlc_tree_t * +libvlc_tree_subtree_at_index( libvlc_tree_t * p_tree, + int index, + libvlc_exception_t * p_e ) +{ + (void)p_e; + libvlc_tree_t * p_subtree; + + p_subtree = ARRAY_VAL( p_tree->subtrees, index ); + libvlc_tree_retain( p_subtree ); + + return p_subtree; +} + +/************************************************************************** + * insert_subtree_at_index (Public) + * + * Note: The subtree won't be retained + **************************************************************************/ +void +libvlc_tree_insert_subtree_at_index( libvlc_tree_t * p_tree, + libvlc_tree_t * p_subtree, + int index, + libvlc_exception_t * p_e ) +{ + (void)p_e; + libvlc_tree_retain( p_tree ); + + ARRAY_INSERT( p_tree->subtrees, p_subtree, index); + notify_subtree_addition( p_tree, p_subtree, index ); +} + +/************************************************************************** + * remove_subtree_at_index (Public) + **************************************************************************/ +void +libvlc_tree_remove_subtree_at_index( libvlc_tree_t * p_tree, + int index, + libvlc_exception_t * p_e ) +{ + (void)p_e; + libvlc_tree_t * p_subtree; + + p_subtree = ARRAY_VAL( p_tree->subtrees, index ); + + ARRAY_REMOVE( p_tree->subtrees, index ); + notify_subtree_deletion( p_tree, p_subtree, index ); + + libvlc_tree_release( p_subtree ); +} \ No newline at end of file -- 2.20.1