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_added( const libvlc_event_t * p_event, void * p_user_data );
44 media_list_item_removed( const libvlc_event_t * p_event, void * p_user_data );
46 media_list_subitem_added( const libvlc_event_t * p_event, void * p_user_data );
49 install_md_listener( libvlc_media_list_view_t * p_mlv,
50 libvlc_media_descriptor_t * p_md)
52 libvlc_media_list_t * p_mlist;
53 if((p_mlist = libvlc_media_descriptor_subitems( p_md, NULL )))
55 libvlc_media_list_lock( p_mlist );
56 int i, count = libvlc_media_list_count( p_mlist, NULL );
57 for( i = 0; i < count; i++)
59 libvlc_media_descriptor_t * p_submd;
60 p_submd = libvlc_media_list_item_at_index( p_mlist, i, NULL );
61 install_md_listener( p_mlv, p_submd );
62 libvlc_media_descriptor_release( p_submd );
64 libvlc_media_list_unlock( p_mlist );
65 libvlc_media_list_release( p_mlist );
67 libvlc_event_attach( p_md->p_event_manager,
68 libvlc_MediaDescriptorSubItemAdded,
69 media_list_subitem_added, p_mlv, NULL );
73 uninstall_md_listener( libvlc_media_list_view_t * p_mlv,
74 libvlc_media_descriptor_t * p_md)
76 libvlc_media_list_t * p_mlist;
77 libvlc_exception_t ignored_exception;
78 libvlc_exception_init( &ignored_exception );
79 libvlc_event_detach( p_md->p_event_manager,
80 libvlc_MediaDescriptorSubItemAdded,
81 media_list_subitem_added, p_mlv, &ignored_exception );
82 if( libvlc_exception_raised( &ignored_exception ) )
83 libvlc_exception_clear( &ignored_exception ); /* We don't care if we encounter an exception */
84 if((p_mlist = libvlc_media_descriptor_subitems( p_md, NULL )))
86 libvlc_media_list_lock( p_mlist );
87 int i, count = libvlc_media_list_count( p_mlist, NULL );
88 for( i = 0; i < count; i++)
90 libvlc_media_descriptor_t * p_submd;
91 p_submd = libvlc_media_list_item_at_index( p_mlist,i, NULL );
92 uninstall_md_listener( p_mlv, p_submd );
93 libvlc_media_descriptor_release( p_submd );
95 libvlc_media_list_unlock( p_mlist );
96 libvlc_media_list_release( p_mlist );
101 media_list_item_added( const libvlc_event_t * p_event, void * p_user_data )
103 libvlc_media_list_view_t * p_mlv = p_user_data;
104 libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_added.item;
105 install_md_listener( p_mlv, p_md );
106 if( p_mlv->pf_ml_item_added ) p_mlv->pf_ml_item_added( p_event, p_mlv );
110 media_list_item_removed( const libvlc_event_t * p_event, void * p_user_data )
112 libvlc_media_list_view_t * p_mlv = p_user_data;
113 libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_added.item;
114 uninstall_md_listener( p_mlv, p_md );
115 if( p_mlv->pf_ml_item_removed ) p_mlv->pf_ml_item_removed( p_event, p_mlv );
119 media_list_subitem_added( const libvlc_event_t * p_event, void * p_user_data )
121 libvlc_event_t added_event;
122 libvlc_media_list_view_t * p_mlv = p_user_data;
123 libvlc_media_descriptor_t * p_md = p_event->u.media_descriptor_subitem_added.new_child;
124 install_md_listener( p_mlv, p_md );
126 added_event.u.media_list_item_added.item = p_md;
127 added_event.u.media_list_item_added.index = 0;
128 if( p_mlv->pf_ml_item_added ) p_mlv->pf_ml_item_added( &added_event, p_mlv );
132 * LibVLC Internal functions
134 /**************************************************************************
135 * libvlc_media_list_view_set_ml_notification_callback (Internal)
136 * The mlist lock should be held when entered
137 **************************************************************************/
139 libvlc_media_list_view_set_ml_notification_callback(
140 libvlc_media_list_view_t * p_mlv,
141 void (*item_added)(const libvlc_event_t *, libvlc_media_list_view_t *),
142 void (*item_removed)(const libvlc_event_t *, libvlc_media_list_view_t *) )
144 p_mlv->pf_ml_item_added = item_added;
145 p_mlv->pf_ml_item_removed = item_removed;
146 libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
147 libvlc_MediaListItemAdded,
148 media_list_item_added, p_mlv, NULL );
149 libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
150 libvlc_MediaListItemDeleted,
151 media_list_item_removed, p_mlv, NULL );
152 int i, count = libvlc_media_list_count( p_mlv->p_mlist, NULL );
153 for( i = 0; i < count; i++)
155 libvlc_media_descriptor_t * p_md;
156 p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, NULL );
157 install_md_listener( p_mlv, p_md );
158 libvlc_media_descriptor_release( p_md );
162 /**************************************************************************
163 * libvlc_media_list_view_notify_deletion (Internal)
164 **************************************************************************/
166 libvlc_media_list_view_will_delete_item(
167 libvlc_media_list_view_t * p_mlv,
168 libvlc_media_descriptor_t * p_item,
171 libvlc_event_t event;
173 /* Construct the event */
174 event.type = libvlc_MediaListViewWillDeleteItem;
175 event.u.media_list_view_will_delete_item.item = p_item;
176 event.u.media_list_view_will_delete_item.index = index;
179 libvlc_event_send( p_mlv->p_event_manager, &event );
182 /**************************************************************************
183 * libvlc_media_list_view_item_deleted (Internal)
184 **************************************************************************/
186 libvlc_media_list_view_item_deleted(
187 libvlc_media_list_view_t * p_mlv,
188 libvlc_media_descriptor_t * p_item,
191 libvlc_event_t event;
193 /* Construct the event */
194 event.type = libvlc_MediaListViewItemDeleted;
195 event.u.media_list_view_item_deleted.item = p_item;
196 event.u.media_list_view_item_deleted.index = index;
199 libvlc_event_send( p_mlv->p_event_manager, &event );
202 /**************************************************************************
203 * libvlc_media_list_view_will_add_item (Internal)
204 **************************************************************************/
206 libvlc_media_list_view_will_add_item(
207 libvlc_media_list_view_t * p_mlv,
208 libvlc_media_descriptor_t * p_item,
211 libvlc_event_t event;
213 /* Construct the event */
214 event.type = libvlc_MediaListViewWillAddItem;
215 event.u.media_list_view_will_add_item.item = p_item;
216 event.u.media_list_view_will_add_item.index = index;
219 libvlc_event_send( p_mlv->p_event_manager, &event );
222 /**************************************************************************
223 * libvlc_media_list_view_item_added (Internal)
224 **************************************************************************/
226 libvlc_media_list_view_item_added(
227 libvlc_media_list_view_t * p_mlv,
228 libvlc_media_descriptor_t * p_item,
231 libvlc_event_t event;
233 /* Construct the event */
234 event.type = libvlc_MediaListViewItemAdded;
235 event.u.media_list_view_item_added.item = p_item;
236 event.u.media_list_view_item_added.index = index;
239 libvlc_event_send( p_mlv->p_event_manager, &event );
242 /**************************************************************************
243 * libvlc_media_list_view_new (Internal)
244 **************************************************************************/
245 libvlc_media_list_view_t *
246 libvlc_media_list_view_new( libvlc_media_list_t * p_mlist,
247 libvlc_media_list_view_count_func_t pf_count,
248 libvlc_media_list_view_item_at_index_func_t pf_item_at_index,
249 libvlc_media_list_view_children_at_index_func_t pf_children_at_index,
250 libvlc_media_list_view_release_func_t pf_release,
251 void * this_view_data,
252 libvlc_exception_t * p_e )
254 libvlc_media_list_view_t * p_mlv;
255 p_mlv = calloc( 1, sizeof(libvlc_media_list_view_t) );
259 p_mlv->p_libvlc_instance = p_mlist->p_libvlc_instance;
260 p_mlv->p_event_manager = libvlc_event_manager_new( p_mlist,
261 p_mlv->p_libvlc_instance, p_e );
263 libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
264 libvlc_MediaListViewItemAdded, p_e );
265 libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
266 libvlc_MediaListViewWillAddItem, p_e );
267 libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
268 libvlc_MediaListViewItemDeleted, p_e );
269 libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
270 libvlc_MediaListViewWillDeleteItem, p_e );
272 libvlc_media_list_retain( p_mlist );
273 p_mlv->p_mlist = p_mlist;
275 p_mlv->pf_count = pf_count;
276 p_mlv->pf_item_at_index = pf_item_at_index;
277 p_mlv->pf_children_at_index = pf_children_at_index;
278 p_mlv->pf_release = pf_release;
280 p_mlv->p_this_view_data = this_view_data;
282 vlc_mutex_init( p_mlv->p_libvlc_instance->p_libvlc_int, &p_mlv->object_lock );
283 p_mlv->i_refcount = 1;
290 * Public libvlc functions
293 /**************************************************************************
294 * libvlc_media_list_view_retain (Public)
295 **************************************************************************/
297 libvlc_media_list_view_retain( libvlc_media_list_view_t * p_mlv )
299 vlc_mutex_lock( &p_mlv->object_lock );
301 vlc_mutex_unlock( &p_mlv->object_lock );
304 /**************************************************************************
305 * libvlc_media_list_view_release (Public)
306 **************************************************************************/
308 libvlc_media_list_view_release( libvlc_media_list_view_t * p_mlv )
310 vlc_mutex_lock( &p_mlv->object_lock );
312 if( p_mlv->i_refcount > 0 )
314 vlc_mutex_unlock( &p_mlv->object_lock );
317 vlc_mutex_unlock( &p_mlv->object_lock );
319 /* Refcount null, time to free */
320 libvlc_media_list_lock( p_mlv->p_mlist );
322 if( p_mlv->pf_ml_item_added )
324 libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
325 libvlc_MediaListItemAdded,
326 media_list_item_added, p_mlv, NULL );
328 if( p_mlv->pf_ml_item_removed )
330 libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
331 libvlc_MediaListItemDeleted,
332 media_list_item_removed, p_mlv, NULL );
334 int i, count = libvlc_media_list_count( p_mlv->p_mlist, NULL );
335 for( i = 0; i < count; i++)
337 libvlc_media_descriptor_t * p_md;
338 p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, NULL );
339 uninstall_md_listener( p_mlv, p_md );
340 libvlc_media_descriptor_release( p_md );
342 libvlc_media_list_unlock( p_mlv->p_mlist );
344 libvlc_event_manager_release( p_mlv->p_event_manager );
346 if( p_mlv->pf_release ) p_mlv->pf_release( p_mlv );
347 libvlc_media_list_release( p_mlv->p_mlist );
348 vlc_mutex_destroy( &p_mlv->object_lock );
351 /**************************************************************************
352 * libvlc_media_list_view_event_manager (Public)
353 **************************************************************************/
354 libvlc_event_manager_t *
355 libvlc_media_list_view_event_manager( libvlc_media_list_view_t * p_mlv )
357 libvlc_event_manager_t * p_em;
358 vlc_mutex_lock( &p_mlv->object_lock );
359 p_em = p_mlv->p_event_manager;
360 vlc_mutex_unlock( &p_mlv->object_lock );
364 /**************************************************************************
365 * libvlc_media_list_view_parent_media_list (Public)
366 **************************************************************************/
367 libvlc_media_list_t *
368 libvlc_media_list_view_parent_media_list( libvlc_media_list_view_t * p_mlv,
369 libvlc_exception_t * p_e)
372 libvlc_media_list_t * p_mlist;
373 vlc_mutex_lock( &p_mlv->object_lock );
374 p_mlist = p_mlv->p_mlist;
375 libvlc_media_list_retain( p_mlv->p_mlist );
376 vlc_mutex_unlock( &p_mlv->object_lock );
380 /* Limited to four args, because it should be enough */
382 #define AN_SELECT( collapser, dec1, dec2, dec3, dec4, p, ...) p
383 #define ARGS(...) AN_SELECT( collapser, ##__VA_ARGS__, \
384 (p_mlv, arg1, arg2, arg3, arg4, p_e), \
385 (p_mlv, arg1, arg2, arg3, p_e), \
386 (p_mlv, arg1, arg2, p_e), \
387 (p_mlv, arg1, p_e), (p_mlv, p_e) )
389 #define MEDIA_LIST_VIEW_FUNCTION( name, ret_type, default_ret_value, /* Params */ ... ) \
391 libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
393 libvlc_exception_t * p_e ) \
395 if( p_mlv->pf_##name ) \
396 return p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
397 libvlc_exception_raise( p_e, "No '" #name "' method in this media_list_view" ); \
398 return default_ret_value;\
401 #define MEDIA_LIST_VIEW_FUNCTION_VOID_RET( name, /* Params */ ... ) \
403 libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
405 libvlc_exception_t * p_e ) \
407 if( p_mlv->pf_##name ) \
409 p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
412 libvlc_exception_raise( p_e, "No '" #name "' method in this media_list_view" ); \
416 MEDIA_LIST_VIEW_FUNCTION( count, int, 0 )
417 MEDIA_LIST_VIEW_FUNCTION( item_at_index, libvlc_media_descriptor_t *, NULL, int arg1 )
418 MEDIA_LIST_VIEW_FUNCTION( children_at_index, libvlc_media_list_view_t *, NULL, int arg1 )