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_event_detach( p_md->p_event_manager,
78 libvlc_MediaDescriptorSubItemAdded,
79 media_list_subitem_added, p_mlv, NULL );
80 if((p_mlist = libvlc_media_descriptor_subitems( p_md, NULL )))
82 libvlc_media_list_lock( p_mlist );
83 int i, count = libvlc_media_list_count( p_mlist, NULL );
84 for( i = 0; i < count; i++)
86 libvlc_media_descriptor_t * p_submd;
87 p_submd = libvlc_media_list_item_at_index( p_mlist,i, NULL );
88 uninstall_md_listener( p_mlv, p_submd );
89 libvlc_media_descriptor_release( p_submd );
91 libvlc_media_list_unlock( p_mlist );
92 libvlc_media_list_release( p_mlist );
97 media_list_item_added( const libvlc_event_t * p_event, void * p_user_data )
99 libvlc_media_list_view_t * p_mlv = p_user_data;
100 libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_added.item;
101 install_md_listener( p_mlv, p_md );
102 if( p_mlv->pf_ml_item_added ) p_mlv->pf_ml_item_added( p_event, p_mlv );
106 media_list_item_removed( const libvlc_event_t * p_event, void * p_user_data )
108 libvlc_media_list_view_t * p_mlv = p_user_data;
109 libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_added.item;
110 uninstall_md_listener( p_mlv, p_md );
111 if( p_mlv->pf_ml_item_removed ) p_mlv->pf_ml_item_removed( p_event, p_mlv );
115 media_list_subitem_added( const libvlc_event_t * p_event, void * p_user_data )
117 libvlc_event_t added_event;
118 libvlc_media_list_view_t * p_mlv = p_user_data;
119 libvlc_media_descriptor_t * p_md = p_event->u.media_descriptor_subitem_added.new_child;
120 install_md_listener( p_mlv, p_md );
122 added_event.u.media_list_item_added.item = p_md;
123 added_event.u.media_list_item_added.index = 0;
124 if( p_mlv->pf_ml_item_added ) p_mlv->pf_ml_item_added( &added_event, p_mlv );
128 * LibVLC Internal functions
130 /**************************************************************************
131 * libvlc_media_list_view_set_ml_notification_callback (Internal)
132 * The mlist lock should be held when entered
133 **************************************************************************/
135 libvlc_media_list_view_set_ml_notification_callback(
136 libvlc_media_list_view_t * p_mlv,
137 void (*item_added)(const libvlc_event_t *, libvlc_media_list_view_t *),
138 void (*item_removed)(const libvlc_event_t *, libvlc_media_list_view_t *) )
140 p_mlv->pf_ml_item_added = item_added;
141 p_mlv->pf_ml_item_removed = item_removed;
142 libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
143 libvlc_MediaListItemAdded,
144 media_list_item_added, p_mlv, NULL );
145 libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
146 libvlc_MediaListItemDeleted,
147 media_list_item_removed, p_mlv, NULL );
148 int i, count = libvlc_media_list_count( p_mlv->p_mlist, NULL );
149 for( i = 0; i < count; i++)
151 libvlc_media_descriptor_t * p_md;
152 p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, NULL );
153 install_md_listener( p_mlv, p_md );
154 libvlc_media_descriptor_release( p_md );
158 /**************************************************************************
159 * libvlc_media_list_view_notify_deletion (Internal)
160 **************************************************************************/
162 libvlc_media_list_view_will_delete_item(
163 libvlc_media_list_view_t * p_mlv,
164 libvlc_media_descriptor_t * p_item,
167 libvlc_event_t event;
169 /* Construct the event */
170 event.type = libvlc_MediaListViewWillDeleteItem;
171 event.u.media_list_view_will_delete_item.item = p_item;
172 event.u.media_list_view_will_delete_item.index = index;
175 libvlc_event_send( p_mlv->p_event_manager, &event );
178 /**************************************************************************
179 * libvlc_media_list_view_item_deleted (Internal)
180 **************************************************************************/
182 libvlc_media_list_view_item_deleted(
183 libvlc_media_list_view_t * p_mlv,
184 libvlc_media_descriptor_t * p_item,
187 libvlc_event_t event;
189 /* Construct the event */
190 event.type = libvlc_MediaListViewItemDeleted;
191 event.u.media_list_view_item_deleted.item = p_item;
192 event.u.media_list_view_item_deleted.index = index;
195 libvlc_event_send( p_mlv->p_event_manager, &event );
198 /**************************************************************************
199 * libvlc_media_list_view_will_add_item (Internal)
200 **************************************************************************/
202 libvlc_media_list_view_will_add_item(
203 libvlc_media_list_view_t * p_mlv,
204 libvlc_media_descriptor_t * p_item,
207 libvlc_event_t event;
209 /* Construct the event */
210 event.type = libvlc_MediaListViewWillAddItem;
211 event.u.media_list_view_will_add_item.item = p_item;
212 event.u.media_list_view_will_add_item.index = index;
215 libvlc_event_send( p_mlv->p_event_manager, &event );
218 /**************************************************************************
219 * libvlc_media_list_view_item_added (Internal)
220 **************************************************************************/
222 libvlc_media_list_view_item_added(
223 libvlc_media_list_view_t * p_mlv,
224 libvlc_media_descriptor_t * p_item,
227 libvlc_event_t event;
229 /* Construct the event */
230 event.type = libvlc_MediaListViewItemAdded;
231 event.u.media_list_view_item_added.item = p_item;
232 event.u.media_list_view_item_added.index = index;
235 libvlc_event_send( p_mlv->p_event_manager, &event );
238 /**************************************************************************
239 * libvlc_media_list_view_new (Internal)
240 **************************************************************************/
241 libvlc_media_list_view_t *
242 libvlc_media_list_view_new( libvlc_media_list_t * p_mlist,
243 libvlc_media_list_view_count_func_t pf_count,
244 libvlc_media_list_view_item_at_index_func_t pf_item_at_index,
245 libvlc_media_list_view_children_at_index_func_t pf_children_at_index,
246 libvlc_media_list_view_release_func_t pf_release,
247 void * this_view_data,
248 libvlc_exception_t * p_e )
250 libvlc_media_list_view_t * p_mlv;
251 p_mlv = calloc( 1, sizeof(libvlc_media_list_view_t) );
255 p_mlv->p_libvlc_instance = p_mlist->p_libvlc_instance;
256 p_mlv->p_event_manager = libvlc_event_manager_new( p_mlist,
257 p_mlv->p_libvlc_instance, p_e );
259 libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
260 libvlc_MediaListViewItemAdded, p_e );
261 libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
262 libvlc_MediaListViewWillAddItem, p_e );
263 libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
264 libvlc_MediaListViewItemDeleted, p_e );
265 libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
266 libvlc_MediaListViewWillDeleteItem, p_e );
268 libvlc_media_list_retain( p_mlist );
269 p_mlv->p_mlist = p_mlist;
271 p_mlv->pf_count = pf_count;
272 p_mlv->pf_item_at_index = pf_item_at_index;
273 p_mlv->pf_children_at_index = pf_children_at_index;
274 p_mlv->pf_release = pf_release;
276 p_mlv->p_this_view_data = this_view_data;
278 vlc_mutex_init( p_mlv->p_libvlc_instance->p_libvlc_int, &p_mlv->object_lock );
279 p_mlv->i_refcount = 1;
286 * Public libvlc functions
289 /**************************************************************************
290 * libvlc_media_list_view_retain (Public)
291 **************************************************************************/
293 libvlc_media_list_view_retain( libvlc_media_list_view_t * p_mlv )
295 vlc_mutex_lock( &p_mlv->object_lock );
297 vlc_mutex_unlock( &p_mlv->object_lock );
300 /**************************************************************************
301 * libvlc_media_list_view_release (Public)
302 **************************************************************************/
304 libvlc_media_list_view_release( libvlc_media_list_view_t * p_mlv )
306 vlc_mutex_lock( &p_mlv->object_lock );
308 if( p_mlv->i_refcount > 0 )
310 vlc_mutex_unlock( &p_mlv->object_lock );
313 vlc_mutex_unlock( &p_mlv->object_lock );
315 /* Refcount null, time to free */
316 libvlc_media_list_lock( p_mlv->p_mlist );
318 if( p_mlv->pf_ml_item_added )
320 libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
321 libvlc_MediaListItemAdded,
322 media_list_item_added, p_mlv, NULL );
324 if( p_mlv->pf_ml_item_removed )
326 libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
327 libvlc_MediaListItemDeleted,
328 media_list_item_removed, p_mlv, NULL );
330 int i, count = libvlc_media_list_count( p_mlv->p_mlist, NULL );
331 for( i = 0; i < count; i++)
333 libvlc_media_descriptor_t * p_md;
334 p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, NULL );
335 uninstall_md_listener( p_mlv, p_md );
336 libvlc_media_descriptor_release( p_md );
338 libvlc_media_list_unlock( p_mlv->p_mlist );
340 libvlc_event_manager_release( p_mlv->p_event_manager );
342 if( p_mlv->pf_release ) p_mlv->pf_release( p_mlv );
343 libvlc_media_list_release( p_mlv->p_mlist );
344 vlc_mutex_destroy( &p_mlv->object_lock );
347 /**************************************************************************
348 * libvlc_media_list_view_event_manager (Public)
349 **************************************************************************/
350 libvlc_event_manager_t *
351 libvlc_media_list_view_event_manager( libvlc_media_list_view_t * p_mlv )
353 libvlc_event_manager_t * p_em;
354 vlc_mutex_lock( &p_mlv->object_lock );
355 p_em = p_mlv->p_event_manager;
356 vlc_mutex_unlock( &p_mlv->object_lock );
360 /* Limited to four args, because it should be enough */
362 #define AN_SELECT( collapser, dec1, dec2, dec3, dec4, p, ...) p
363 #define ARGS(...) AN_SELECT( collapser, ##__VA_ARGS__, \
364 (p_mlv, arg1, arg2, arg3, arg4, p_e), \
365 (p_mlv, arg1, arg2, arg3, p_e), \
366 (p_mlv, arg1, arg2, p_e), \
367 (p_mlv, arg1, p_e), (p_mlv, p_e) )
369 #define MEDIA_LIST_VIEW_FUNCTION( name, ret_type, default_ret_value, /* Params */ ... ) \
371 libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
373 libvlc_exception_t * p_e ) \
375 if( p_mlv->pf_##name ) \
376 return p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
377 libvlc_exception_raise( p_e, "No '" #name "' method in this media_list_view" ); \
378 return default_ret_value;\
381 #define MEDIA_LIST_VIEW_FUNCTION_VOID_RET( name, /* Params */ ... ) \
383 libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
385 libvlc_exception_t * p_e ) \
387 if( p_mlv->pf_##name ) \
389 p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
392 libvlc_exception_raise( p_e, "No '" #name "' method in this media_list_view" ); \
396 MEDIA_LIST_VIEW_FUNCTION( count, int, 0 )
397 MEDIA_LIST_VIEW_FUNCTION( item_at_index, libvlc_media_descriptor_t *, NULL, int arg1 )
398 MEDIA_LIST_VIEW_FUNCTION( children_at_index, libvlc_media_list_view_t *, NULL, int arg1 )