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_release_func_t pf_release,
108 void * this_view_data,
109 libvlc_exception_t * p_e )
111 libvlc_media_list_view_t * p_mlv;
112 p_mlv = calloc( 1, sizeof(libvlc_media_list_view_t) );
116 p_mlv->p_libvlc_instance = p_mlist->p_libvlc_instance;
117 p_mlv->p_event_manager = libvlc_event_manager_new( p_mlist,
118 p_mlv->p_libvlc_instance, p_e );
120 libvlc_media_list_retain( p_mlist );
121 p_mlv->p_mlist = p_mlist;
123 p_mlv->pf_count = pf_count;
124 p_mlv->pf_item_at_index = pf_item_at_index;
125 p_mlv->pf_release = pf_release;
127 p_mlv->this_view_data = this_view_data;
129 vlc_mutex_init( p_mlv->p_libvlc_instance->p_libvlc_int, &p_mlv->object_lock );
130 p_mlv->i_refcount = 1;
137 * Public libvlc functions
140 /**************************************************************************
141 * libvlc_media_list_view_release (Public)
142 **************************************************************************/
144 libvlc_media_list_view_release( libvlc_media_list_view_t * p_mlv )
146 vlc_mutex_lock( &p_mlv->object_lock );
148 if( p_mlv->i_refcount > 0 )
150 vlc_mutex_unlock( &p_mlv->object_lock );
153 vlc_mutex_unlock( &p_mlv->object_lock );
155 /* Refcount null, time to free */
157 if( p_mlv->pf_ml_item_added )
159 libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
160 libvlc_MediaListItemAdded,
161 p_mlv->pf_ml_item_added, p_mlv, NULL );
162 /* XXX: descend the whole tree and remove observer */
164 if( p_mlv->pf_ml_item_removed )
166 libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
167 libvlc_MediaListItemDeleted,
168 p_mlv->pf_ml_item_removed, p_mlv, NULL );
169 /* XXX: descend the whole tree and remove observer */
172 libvlc_event_manager_release( p_mlv->p_event_manager );
174 if( p_mlv->pf_release ) p_mlv->pf_release( p_mlv );
175 libvlc_media_list_release( p_mlv->p_mlist );
176 vlc_mutex_destroy( &p_mlv->object_lock );
179 /* Limited to four args, because it should be enough */
181 #define AN_SELECT( collapser, dec1, dec2, dec3, dec4, p, ...) p
182 #define ARGS(...) AN_SELECT( collapser, ##__VA_ARGS__, \
183 (p_mlv, arg1, arg2, arg3, arg4, p_e), \
184 (p_mlv, arg1, arg2, arg3, p_e), \
185 (p_mlv, arg1, arg2, p_e), \
186 (p_mlv, arg1, p_e), (p_mlv, p_e) )
188 #define MEDIA_LIST_VIEW_FUNCTION( name, ret_type, default_ret_value, /* Params */ ... ) \
190 libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
192 libvlc_exception_t * p_e ) \
194 if( p_mlv->pf_##name ) \
195 return p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
196 libvlc_exception_raise( p_e, "No '" #name "' method in this media_list_view" ); \
197 return default_ret_value;\
200 #define MEDIA_LIST_VIEW_FUNCTION_VOID_RET( name, /* Params */ ... ) \
202 libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
204 libvlc_exception_t * p_e ) \
206 if( p_mlv->pf_##name ) \
208 p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
211 libvlc_exception_raise( p_e, "No '" #name "' method in this media_list_view" ); \
215 MEDIA_LIST_VIEW_FUNCTION( count, int, 0 )
216 MEDIA_LIST_VIEW_FUNCTION( item_at_index, libvlc_media_descriptor_t *, NULL, int arg1 )