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 )
308 libvlc_media_list_view_t * p_mlv;
309 p_mlv = calloc( 1, sizeof(libvlc_media_list_view_t) );
310 if( unlikely(p_mlv == NULL) )
312 libvlc_printerr( "Not enough memory" );
316 p_mlv->p_libvlc_instance = p_mlist->p_libvlc_instance;
317 p_mlv->p_event_manager = libvlc_event_manager_new( p_mlist,
318 p_mlv->p_libvlc_instance );
319 if( unlikely(p_mlv->p_event_manager == NULL) )
325 libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
326 libvlc_MediaListViewItemAdded );
327 libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
328 libvlc_MediaListViewWillAddItem );
329 libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
330 libvlc_MediaListViewItemDeleted );
331 libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
332 libvlc_MediaListViewWillDeleteItem );
334 libvlc_media_list_retain( p_mlist );
335 p_mlv->p_mlist = p_mlist;
337 p_mlv->pf_count = pf_count;
338 p_mlv->pf_item_at_index = pf_item_at_index;
339 p_mlv->pf_children_at_index = pf_children_at_index;
340 p_mlv->pf_constructor = pf_constructor;
341 p_mlv->pf_release = pf_release;
343 p_mlv->p_this_view_data = this_view_data;
345 vlc_mutex_init( &p_mlv->object_lock );
346 p_mlv->i_refcount = 1;
353 * Public libvlc functions
356 /**************************************************************************
357 * libvlc_media_list_view_retain (Public)
358 **************************************************************************/
360 libvlc_media_list_view_retain( libvlc_media_list_view_t * p_mlv )
362 vlc_mutex_lock( &p_mlv->object_lock );
364 vlc_mutex_unlock( &p_mlv->object_lock );
367 /**************************************************************************
368 * libvlc_media_list_view_release (Public)
369 **************************************************************************/
371 libvlc_media_list_view_release( libvlc_media_list_view_t * p_mlv )
373 vlc_mutex_lock( &p_mlv->object_lock );
375 if( p_mlv->i_refcount > 0 )
377 vlc_mutex_unlock( &p_mlv->object_lock );
380 vlc_mutex_unlock( &p_mlv->object_lock );
382 /* Refcount null, time to free */
383 libvlc_media_list_lock( p_mlv->p_mlist );
385 if( p_mlv->pf_ml_item_added )
387 libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
388 libvlc_MediaListItemAdded,
389 media_list_item_added, p_mlv );
391 if( p_mlv->pf_ml_item_removed )
393 libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
394 libvlc_MediaListItemDeleted,
395 media_list_item_removed, p_mlv );
397 int i, count = libvlc_media_list_count( p_mlv->p_mlist );
398 for( i = 0; i < count; i++)
400 libvlc_media_t * p_md;
401 p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, NULL );
402 uninstall_md_listener( p_mlv, p_md );
403 libvlc_media_release( p_md );
405 libvlc_media_list_unlock( p_mlv->p_mlist );
407 libvlc_event_manager_release( p_mlv->p_event_manager );
409 if( p_mlv->pf_release ) p_mlv->pf_release( p_mlv );
410 libvlc_media_list_release( p_mlv->p_mlist );
411 vlc_mutex_destroy( &p_mlv->object_lock );
414 /**************************************************************************
415 * libvlc_media_list_view_event_manager (Public)
416 **************************************************************************/
417 libvlc_event_manager_t *
418 libvlc_media_list_view_event_manager( libvlc_media_list_view_t * p_mlv )
420 libvlc_event_manager_t * p_em;
421 vlc_mutex_lock( &p_mlv->object_lock );
422 p_em = p_mlv->p_event_manager;
423 vlc_mutex_unlock( &p_mlv->object_lock );
427 /**************************************************************************
428 * libvlc_media_list_view_parent_media_list (Public)
429 **************************************************************************/
430 libvlc_media_list_t *
431 libvlc_media_list_view_parent_media_list( libvlc_media_list_view_t * p_mlv,
432 libvlc_exception_t * p_e)
435 libvlc_media_list_t * p_mlist;
436 vlc_mutex_lock( &p_mlv->object_lock );
437 p_mlist = p_mlv->p_mlist;
438 libvlc_media_list_retain( p_mlv->p_mlist );
439 vlc_mutex_unlock( &p_mlv->object_lock );
443 /**************************************************************************
444 * libvlc_media_list_view_children_for_item (Public)
445 **************************************************************************/
446 libvlc_media_list_view_t *
447 libvlc_media_list_view_children_for_item( libvlc_media_list_view_t * p_mlv,
448 libvlc_media_t * p_md,
449 libvlc_exception_t * p_e)
452 libvlc_media_list_t * p_mlist;
453 libvlc_media_list_view_t * ret;
455 p_mlist = libvlc_media_subitems(p_md);
456 if(!p_mlist) return NULL;
458 ret = p_mlv->pf_constructor( p_mlist, p_e );
459 libvlc_media_list_release( p_mlist );
464 /* Limited to four args, because it should be enough */
466 #define AN_SELECT( collapser, dec1, dec2, dec3, dec4, p, ...) p
467 #define ARGS(...) AN_SELECT( collapser, ##__VA_ARGS__, \
468 (p_mlv, arg1, arg2, arg3, arg4, p_e), \
469 (p_mlv, arg1, arg2, arg3, p_e), \
470 (p_mlv, arg1, arg2, p_e), \
471 (p_mlv, arg1, p_e), (p_mlv, p_e) )
473 #define MEDIA_LIST_VIEW_FUNCTION( name, ret_type, default_ret_value, /* Params */ ... ) \
475 libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
477 libvlc_exception_t * p_e ) \
479 if( p_mlv->pf_##name ) \
480 return p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
481 libvlc_exception_raise( p_e ); \
482 libvlc_printerr( "No '" #name "' method in this media_list_view" ); \
483 return default_ret_value;\
486 #define MEDIA_LIST_VIEW_FUNCTION_VOID_RET( name, /* Params */ ... ) \
488 libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
490 libvlc_exception_t * p_e ) \
492 if( p_mlv->pf_##name ) \
494 p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
497 libvlc_exception_raise( p_e ); \
498 libvlc_printerr( "No '" #name "' method in this media_list_view" ); \
502 MEDIA_LIST_VIEW_FUNCTION( count, int, 0 )
503 MEDIA_LIST_VIEW_FUNCTION( item_at_index, libvlc_media_t *, NULL, int arg1 )
504 MEDIA_LIST_VIEW_FUNCTION( children_at_index, libvlc_media_list_view_t *, NULL, int arg1 )