1 /*****************************************************************************
2 * flat_media_list.c: libvlc flat media list functions. (extension to
4 *****************************************************************************
5 * Copyright (C) 2007 the VideoLAN team
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 *****************************************************************************/
29 #include <vlc/libvlc.h>
30 #include <vlc/libvlc_media.h>
31 #include <vlc/libvlc_media_list.h>
32 #include <vlc/libvlc_media_list_view.h>
33 #include <vlc/libvlc_events.h>
35 #include "libvlc_internal.h" // Abuse, could and should be removed
37 #include "media_internal.h" // Abuse, could and should be removed
38 #include "media_list_internal.h" // Abuse, could and should be removed
39 #include "media_list_view_internal.h"
41 //#define DEBUG_FLAT_LIST
43 #ifdef DEBUG_FLAT_LIST
44 # define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
53 media_list_item_added( const libvlc_event_t * p_event, void * p_user_data );
55 media_list_item_removed( const libvlc_event_t * p_event, void * p_user_data );
57 media_list_subitem_added( const libvlc_event_t * p_event, void * p_user_data );
60 install_md_listener( libvlc_media_list_view_t * p_mlv,
61 libvlc_media_t * p_md)
63 libvlc_media_list_t * p_mlist;
64 if((p_mlist = libvlc_media_subitems( p_md )))
66 libvlc_media_list_lock( p_mlist );
67 int i, count = libvlc_media_list_count( p_mlist );
68 for( i = 0; i < count; i++)
70 libvlc_event_t added_event;
71 libvlc_media_t * p_submd;
72 p_submd = libvlc_media_list_item_at_index( p_mlist, i, NULL );
74 /* Install our listeners */
75 install_md_listener( p_mlv, p_submd );
77 /* For each item, send a notification to the mlv subclasses */
78 added_event.u.media_list_item_added.item = p_submd;
79 added_event.u.media_list_item_added.index = 0;
80 if( p_mlv->pf_ml_item_added ) p_mlv->pf_ml_item_added( &added_event, p_mlv );
81 libvlc_media_release( p_submd );
83 libvlc_event_attach( p_mlist->p_event_manager,
84 libvlc_MediaListItemAdded,
85 media_list_item_added, p_mlv );
86 libvlc_event_attach( p_mlist->p_event_manager,
87 libvlc_MediaListItemDeleted,
88 media_list_item_removed, p_mlv );
89 libvlc_media_list_unlock( p_mlist );
90 libvlc_media_list_release( p_mlist );
94 /* No mlist, wait for a subitem added event */
95 libvlc_event_attach( p_md->p_event_manager,
96 libvlc_MediaSubItemAdded,
97 media_list_subitem_added, p_mlv );
102 uninstall_md_listener( libvlc_media_list_view_t * p_mlv,
103 libvlc_media_t * p_md)
105 libvlc_media_list_t * p_mlist;
106 libvlc_event_detach( p_md->p_event_manager,
107 libvlc_MediaSubItemAdded,
108 media_list_subitem_added, p_mlv );
109 if((p_mlist = libvlc_media_subitems( p_md )))
111 libvlc_media_list_lock( p_mlist );
112 libvlc_event_detach( p_mlist->p_event_manager,
113 libvlc_MediaListItemAdded,
114 media_list_item_added, p_mlv );
115 libvlc_event_detach( p_mlist->p_event_manager,
116 libvlc_MediaListItemDeleted,
117 media_list_item_removed, p_mlv );
119 int i, count = libvlc_media_list_count( p_mlist );
120 for( i = 0; i < count; i++)
122 libvlc_media_t * p_submd;
123 p_submd = libvlc_media_list_item_at_index( p_mlist,i, NULL );
124 uninstall_md_listener( p_mlv, p_submd );
125 libvlc_media_release( p_submd );
127 libvlc_media_list_unlock( p_mlist );
128 libvlc_media_list_release( p_mlist );
133 media_list_item_added( const libvlc_event_t * p_event, void * p_user_data )
135 libvlc_media_list_view_t * p_mlv = p_user_data;
136 libvlc_media_t * p_md = p_event->u.media_list_item_added.item;
137 install_md_listener( p_mlv, p_md );
138 if( p_mlv->pf_ml_item_added ) p_mlv->pf_ml_item_added( p_event, p_mlv );
142 media_list_item_removed( const libvlc_event_t * p_event, void * p_user_data )
144 libvlc_media_list_view_t * p_mlv = p_user_data;
145 libvlc_media_t * p_md = p_event->u.media_list_item_added.item;
146 uninstall_md_listener( p_mlv, p_md );
147 if( p_mlv->pf_ml_item_removed ) p_mlv->pf_ml_item_removed( p_event, p_mlv );
151 media_list_subitem_added( const libvlc_event_t * p_event, void * p_user_data )
153 libvlc_media_list_t * p_mlist;
154 libvlc_event_t added_event;
155 libvlc_media_list_view_t * p_mlv = p_user_data;
156 libvlc_media_t * p_submd = p_event->u.media_subitem_added.new_child;
157 libvlc_media_t * p_md = p_event->p_obj;
159 if((p_mlist = libvlc_media_subitems( p_md )))
161 /* We have a mlist to which we're going to listen to events
162 * thus, no need to wait for SubItemAdded events */
163 libvlc_event_detach( p_md->p_event_manager,
164 libvlc_MediaSubItemAdded,
165 media_list_subitem_added, p_mlv );
166 libvlc_media_list_lock( p_mlist );
168 libvlc_event_attach( p_mlist->p_event_manager,
169 libvlc_MediaListItemAdded,
170 media_list_item_added, p_mlv );
171 libvlc_event_attach( p_mlist->p_event_manager,
172 libvlc_MediaListItemDeleted,
173 media_list_item_removed, p_mlv );
174 libvlc_media_list_unlock( p_mlist );
175 libvlc_media_list_release( p_mlist );
178 install_md_listener( p_mlv, p_submd );
180 added_event.u.media_list_item_added.item = p_submd;
181 added_event.u.media_list_item_added.index = 0;
182 if( p_mlv->pf_ml_item_added ) p_mlv->pf_ml_item_added( &added_event, p_mlv );
186 * LibVLC Internal functions
188 /**************************************************************************
189 * libvlc_media_list_view_set_ml_notification_callback (Internal)
190 * The mlist lock should be held when entered
191 **************************************************************************/
193 libvlc_media_list_view_set_ml_notification_callback(
194 libvlc_media_list_view_t * p_mlv,
195 void (*item_added)(const libvlc_event_t *, libvlc_media_list_view_t *),
196 void (*item_removed)(const libvlc_event_t *, libvlc_media_list_view_t *) )
198 p_mlv->pf_ml_item_added = item_added;
199 p_mlv->pf_ml_item_removed = item_removed;
200 libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
201 libvlc_MediaListItemAdded,
202 media_list_item_added, p_mlv );
203 libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
204 libvlc_MediaListItemDeleted,
205 media_list_item_removed, p_mlv );
206 int i, count = libvlc_media_list_count( p_mlv->p_mlist );
207 for( i = 0; i < count; i++)
209 libvlc_media_t * p_md;
210 p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, NULL );
211 install_md_listener( p_mlv, p_md );
212 libvlc_media_release( p_md );
216 /**************************************************************************
217 * libvlc_media_list_view_notify_deletion (Internal)
218 **************************************************************************/
220 libvlc_media_list_view_will_delete_item(
221 libvlc_media_list_view_t * p_mlv,
222 libvlc_media_t * p_item,
225 libvlc_event_t event;
227 /* Construct the event */
228 event.type = libvlc_MediaListViewWillDeleteItem;
229 event.u.media_list_view_will_delete_item.item = p_item;
230 event.u.media_list_view_will_delete_item.index = index;
233 libvlc_event_send( p_mlv->p_event_manager, &event );
236 /**************************************************************************
237 * libvlc_media_list_view_item_deleted (Internal)
238 **************************************************************************/
240 libvlc_media_list_view_item_deleted(
241 libvlc_media_list_view_t * p_mlv,
242 libvlc_media_t * p_item,
245 libvlc_event_t event;
247 /* Construct the event */
248 event.type = libvlc_MediaListViewItemDeleted;
249 event.u.media_list_view_item_deleted.item = p_item;
250 event.u.media_list_view_item_deleted.index = index;
253 libvlc_event_send( p_mlv->p_event_manager, &event );
256 /**************************************************************************
257 * libvlc_media_list_view_will_add_item (Internal)
258 **************************************************************************/
260 libvlc_media_list_view_will_add_item(
261 libvlc_media_list_view_t * p_mlv,
262 libvlc_media_t * p_item,
265 libvlc_event_t event;
267 /* Construct the event */
268 event.type = libvlc_MediaListViewWillAddItem;
269 event.u.media_list_view_will_add_item.item = p_item;
270 event.u.media_list_view_will_add_item.index = index;
273 libvlc_event_send( p_mlv->p_event_manager, &event );
276 /**************************************************************************
277 * libvlc_media_list_view_item_added (Internal)
278 **************************************************************************/
280 libvlc_media_list_view_item_added(
281 libvlc_media_list_view_t * p_mlv,
282 libvlc_media_t * p_item,
285 libvlc_event_t event;
287 /* Construct the event */
288 event.type = libvlc_MediaListViewItemAdded;
289 event.u.media_list_view_item_added.item = p_item;
290 event.u.media_list_view_item_added.index = index;
293 libvlc_event_send( p_mlv->p_event_manager, &event );
296 /**************************************************************************
297 * libvlc_media_list_view_new (Internal)
298 **************************************************************************/
299 libvlc_media_list_view_t *
300 libvlc_media_list_view_new( libvlc_media_list_t * p_mlist,
301 libvlc_media_list_view_count_func_t pf_count,
302 libvlc_media_list_view_item_at_index_func_t pf_item_at_index,
303 libvlc_media_list_view_children_at_index_func_t pf_children_at_index,
304 libvlc_media_list_view_constructor_func_t pf_constructor,
305 libvlc_media_list_view_release_func_t pf_release,
306 void * this_view_data,
307 libvlc_exception_t * p_e )
309 libvlc_media_list_view_t * p_mlv;
310 p_mlv = calloc( 1, sizeof(libvlc_media_list_view_t) );
311 if( unlikely(p_mlv == NULL) )
313 libvlc_printerr( "Not enough memory" );
317 p_mlv->p_libvlc_instance = p_mlist->p_libvlc_instance;
318 p_mlv->p_event_manager = libvlc_event_manager_new( p_mlist,
319 p_mlv->p_libvlc_instance );
320 if( unlikely(p_mlv->p_event_manager == NULL) )
326 libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
327 libvlc_MediaListViewItemAdded );
328 libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
329 libvlc_MediaListViewWillAddItem );
330 libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
331 libvlc_MediaListViewItemDeleted );
332 libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
333 libvlc_MediaListViewWillDeleteItem );
335 libvlc_media_list_retain( p_mlist );
336 p_mlv->p_mlist = p_mlist;
338 p_mlv->pf_count = pf_count;
339 p_mlv->pf_item_at_index = pf_item_at_index;
340 p_mlv->pf_children_at_index = pf_children_at_index;
341 p_mlv->pf_constructor = pf_constructor;
342 p_mlv->pf_release = pf_release;
344 p_mlv->p_this_view_data = this_view_data;
346 vlc_mutex_init( &p_mlv->object_lock );
347 p_mlv->i_refcount = 1;
354 * Public libvlc functions
357 /**************************************************************************
358 * libvlc_media_list_view_retain (Public)
359 **************************************************************************/
361 libvlc_media_list_view_retain( libvlc_media_list_view_t * p_mlv )
363 vlc_mutex_lock( &p_mlv->object_lock );
365 vlc_mutex_unlock( &p_mlv->object_lock );
368 /**************************************************************************
369 * libvlc_media_list_view_release (Public)
370 **************************************************************************/
372 libvlc_media_list_view_release( libvlc_media_list_view_t * p_mlv )
374 vlc_mutex_lock( &p_mlv->object_lock );
376 if( p_mlv->i_refcount > 0 )
378 vlc_mutex_unlock( &p_mlv->object_lock );
381 vlc_mutex_unlock( &p_mlv->object_lock );
383 /* Refcount null, time to free */
384 libvlc_media_list_lock( p_mlv->p_mlist );
386 if( p_mlv->pf_ml_item_added )
388 libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
389 libvlc_MediaListItemAdded,
390 media_list_item_added, p_mlv );
392 if( p_mlv->pf_ml_item_removed )
394 libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
395 libvlc_MediaListItemDeleted,
396 media_list_item_removed, p_mlv );
398 int i, count = libvlc_media_list_count( p_mlv->p_mlist );
399 for( i = 0; i < count; i++)
401 libvlc_media_t * p_md;
402 p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, NULL );
403 uninstall_md_listener( p_mlv, p_md );
404 libvlc_media_release( p_md );
406 libvlc_media_list_unlock( p_mlv->p_mlist );
408 libvlc_event_manager_release( p_mlv->p_event_manager );
410 if( p_mlv->pf_release ) p_mlv->pf_release( p_mlv );
411 libvlc_media_list_release( p_mlv->p_mlist );
412 vlc_mutex_destroy( &p_mlv->object_lock );
415 /**************************************************************************
416 * libvlc_media_list_view_event_manager (Public)
417 **************************************************************************/
418 libvlc_event_manager_t *
419 libvlc_media_list_view_event_manager( libvlc_media_list_view_t * p_mlv )
421 libvlc_event_manager_t * p_em;
422 vlc_mutex_lock( &p_mlv->object_lock );
423 p_em = p_mlv->p_event_manager;
424 vlc_mutex_unlock( &p_mlv->object_lock );
428 /**************************************************************************
429 * libvlc_media_list_view_parent_media_list (Public)
430 **************************************************************************/
431 libvlc_media_list_t *
432 libvlc_media_list_view_parent_media_list( libvlc_media_list_view_t * p_mlv,
433 libvlc_exception_t * p_e)
436 libvlc_media_list_t * p_mlist;
437 vlc_mutex_lock( &p_mlv->object_lock );
438 p_mlist = p_mlv->p_mlist;
439 libvlc_media_list_retain( p_mlv->p_mlist );
440 vlc_mutex_unlock( &p_mlv->object_lock );
444 /**************************************************************************
445 * libvlc_media_list_view_children_for_item (Public)
446 **************************************************************************/
447 libvlc_media_list_view_t *
448 libvlc_media_list_view_children_for_item( libvlc_media_list_view_t * p_mlv,
449 libvlc_media_t * p_md,
450 libvlc_exception_t * p_e)
453 libvlc_media_list_t * p_mlist;
454 libvlc_media_list_view_t * ret;
456 p_mlist = libvlc_media_subitems(p_md);
457 if(!p_mlist) return NULL;
459 ret = p_mlv->pf_constructor( p_mlist, p_e );
460 libvlc_media_list_release( p_mlist );
465 /* Limited to four args, because it should be enough */
467 #define AN_SELECT( collapser, dec1, dec2, dec3, dec4, p, ...) p
468 #define ARGS(...) AN_SELECT( collapser, ##__VA_ARGS__, \
469 (p_mlv, arg1, arg2, arg3, arg4, p_e), \
470 (p_mlv, arg1, arg2, arg3, p_e), \
471 (p_mlv, arg1, arg2, p_e), \
472 (p_mlv, arg1, p_e), (p_mlv, p_e) )
474 #define MEDIA_LIST_VIEW_FUNCTION( name, ret_type, default_ret_value, /* Params */ ... ) \
476 libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
478 libvlc_exception_t * p_e ) \
480 if( p_mlv->pf_##name ) \
481 return p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
482 libvlc_exception_raise( p_e ); \
483 libvlc_printerr( "No '" #name "' method in this media_list_view" ); \
484 return default_ret_value;\
487 #define MEDIA_LIST_VIEW_FUNCTION_VOID_RET( name, /* Params */ ... ) \
489 libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
491 libvlc_exception_t * p_e ) \
493 if( p_mlv->pf_##name ) \
495 p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
498 libvlc_exception_raise( p_e ); \
499 libvlc_printerr( "No '" #name "' method in this media_list_view" ); \
503 MEDIA_LIST_VIEW_FUNCTION( count, int, 0 )
504 MEDIA_LIST_VIEW_FUNCTION( item_at_index, libvlc_media_t *, NULL, int arg1 )
505 MEDIA_LIST_VIEW_FUNCTION( children_at_index, libvlc_media_list_view_t *, NULL, int arg1 )