1 /*****************************************************************************
2 * flat_media_list.c: libvlc flat media list functions. (extension to
4 *****************************************************************************
5 * Copyright (C) 2007 the VideoLAN team
6 * $Id: flat_media_list.c 21287 2007-08-20 01:28:12Z pdherbemont $
8 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include "libvlc_internal.h"
26 #include <vlc/libvlc.h>
28 #include "vlc_arrays.h"
30 //#define DEBUG_FLAT_LIST
32 #ifdef DEBUG_FLAT_LIST
33 # define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
42 media_list_item_removed( const libvlc_event_t * p_event, void * p_user_data );
45 media_list_item_added( const libvlc_event_t * p_event, void * p_user_data )
47 libvlc_media_list_view_t * p_mlv = p_user_data;
48 libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_added.item;
49 libvlc_media_list_t * p_mlist;
50 if((p_mlist = libvlc_media_descriptor_subitems( p_md, NULL )))
52 libvlc_event_attach( p_mlist->p_event_manager,
53 libvlc_MediaListItemAdded,
54 media_list_item_added, p_mlv, NULL );
55 libvlc_event_attach( p_mlist->p_event_manager,
56 libvlc_MediaListItemDeleted,
57 media_list_item_removed, p_mlv, NULL );
59 if( p_mlv->pf_ml_item_added ) p_mlv->pf_ml_item_added( p_event, p_user_data );
63 media_list_item_removed( const libvlc_event_t * p_event, void * p_user_data )
65 libvlc_media_list_view_t * p_mlv = p_user_data;
66 libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_added.item;
67 libvlc_media_list_t * p_mlist;
68 if((p_mlist = libvlc_media_descriptor_subitems( p_md, NULL )))
70 libvlc_event_attach( p_mlist->p_event_manager,
71 libvlc_MediaListItemAdded,
72 media_list_item_added, p_mlv, NULL );
73 libvlc_event_attach( p_mlist->p_event_manager,
74 libvlc_MediaListItemDeleted,
75 media_list_item_removed, p_mlv, NULL );
77 if( p_mlv->pf_ml_item_removed ) p_mlv->pf_ml_item_removed( p_event, p_user_data );
82 * LibVLC Internal functions
85 libvlc_media_list_view_set_ml_notification_callback(
86 libvlc_media_list_view_t * p_mlv,
87 void (*item_added)(const libvlc_event_t *, void *),
88 void (*item_removed)(const libvlc_event_t *, void *) )
90 p_mlv->pf_ml_item_added = item_added;
91 p_mlv->pf_ml_item_removed = item_removed;
92 libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
93 libvlc_MediaListItemAdded,
94 media_list_item_added, p_mlv, NULL );
95 libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
96 libvlc_MediaListItemDeleted,
97 media_list_item_removed, p_mlv, NULL );
100 /**************************************************************************
101 * libvlc_media_list_view_new (Internal)
102 **************************************************************************/
103 libvlc_media_list_view_t *
104 libvlc_media_list_view_new( libvlc_media_list_t * p_mlist,
105 libvlc_media_list_view_count_func_t pf_count,
106 libvlc_media_list_view_item_at_index_func_t pf_item_at_index,
107 libvlc_media_list_view_children_at_index_func_t pf_children_at_index,
108 libvlc_media_list_view_release_func_t pf_release,
109 void * this_view_data,
110 libvlc_exception_t * p_e )
112 libvlc_media_list_view_t * p_mlv;
113 p_mlv = calloc( 1, sizeof(libvlc_media_list_view_t) );
117 p_mlv->p_libvlc_instance = p_mlist->p_libvlc_instance;
118 p_mlv->p_event_manager = libvlc_event_manager_new( p_mlist,
119 p_mlv->p_libvlc_instance, p_e );
121 libvlc_media_list_retain( p_mlist );
122 p_mlv->p_mlist = p_mlist;
124 p_mlv->pf_count = pf_count;
125 p_mlv->pf_item_at_index = pf_item_at_index;
126 p_mlv->pf_children_at_index = pf_children_at_index;
127 p_mlv->pf_release = pf_release;
129 p_mlv->p_this_view_data = this_view_data;
131 vlc_mutex_init( p_mlv->p_libvlc_instance->p_libvlc_int, &p_mlv->object_lock );
132 p_mlv->i_refcount = 1;
139 * Public libvlc functions
142 /**************************************************************************
143 * libvlc_media_list_view_retain (Public)
144 **************************************************************************/
146 libvlc_media_list_view_retain( libvlc_media_list_view_t * p_mlv )
148 vlc_mutex_lock( &p_mlv->object_lock );
150 vlc_mutex_unlock( &p_mlv->object_lock );
153 /**************************************************************************
154 * libvlc_media_list_view_release (Public)
155 **************************************************************************/
157 libvlc_media_list_view_release( libvlc_media_list_view_t * p_mlv )
159 vlc_mutex_lock( &p_mlv->object_lock );
161 if( p_mlv->i_refcount > 0 )
163 vlc_mutex_unlock( &p_mlv->object_lock );
166 vlc_mutex_unlock( &p_mlv->object_lock );
168 /* Refcount null, time to free */
170 if( p_mlv->pf_ml_item_added )
172 libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
173 libvlc_MediaListItemAdded,
174 p_mlv->pf_ml_item_added, p_mlv, NULL );
175 /* XXX: descend the whole tree and remove observer */
177 if( p_mlv->pf_ml_item_removed )
179 libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
180 libvlc_MediaListItemDeleted,
181 p_mlv->pf_ml_item_removed, p_mlv, NULL );
182 /* XXX: descend the whole tree and remove observer */
185 libvlc_event_manager_release( p_mlv->p_event_manager );
187 if( p_mlv->pf_release ) p_mlv->pf_release( p_mlv );
188 libvlc_media_list_release( p_mlv->p_mlist );
189 vlc_mutex_destroy( &p_mlv->object_lock );
192 /* Limited to four args, because it should be enough */
194 #define AN_SELECT( collapser, dec1, dec2, dec3, dec4, p, ...) p
195 #define ARGS(...) AN_SELECT( collapser, ##__VA_ARGS__, \
196 (p_mlv, arg1, arg2, arg3, arg4, p_e), \
197 (p_mlv, arg1, arg2, arg3, p_e), \
198 (p_mlv, arg1, arg2, p_e), \
199 (p_mlv, arg1, p_e), (p_mlv, p_e) )
201 #define MEDIA_LIST_VIEW_FUNCTION( name, ret_type, default_ret_value, /* Params */ ... ) \
203 libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
205 libvlc_exception_t * p_e ) \
207 if( p_mlv->pf_##name ) \
208 return p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
209 libvlc_exception_raise( p_e, "No '" #name "' method in this media_list_view" ); \
210 return default_ret_value;\
213 #define MEDIA_LIST_VIEW_FUNCTION_VOID_RET( name, /* Params */ ... ) \
215 libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
217 libvlc_exception_t * p_e ) \
219 if( p_mlv->pf_##name ) \
221 p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
224 libvlc_exception_raise( p_e, "No '" #name "' method in this media_list_view" ); \
228 MEDIA_LIST_VIEW_FUNCTION( count, int, 0 )
229 MEDIA_LIST_VIEW_FUNCTION( item_at_index, libvlc_media_descriptor_t *, NULL, int arg1 )
230 MEDIA_LIST_VIEW_FUNCTION( children_at_index, libvlc_media_list_view_t *, NULL, int arg1 )