]> git.sesse.net Git - vlc/blob - src/control/media_list_view.c
c562a151e71aca87ea398a629b1f3733448c2d6b
[vlc] / src / control / media_list_view.c
1 /*****************************************************************************
2  * flat_media_list.c: libvlc flat media list functions. (extension to
3  * media_list.c).
4  *****************************************************************************
5  * Copyright (C) 2007 the VideoLAN team
6  * $Id: flat_media_list.c 21287 2007-08-20 01:28:12Z pdherbemont $
7  *
8  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #include "libvlc_internal.h"
26 #include <vlc/libvlc.h>
27 #include <assert.h>
28 #include "vlc_arrays.h"
29
30 //#define DEBUG_FLAT_LIST
31
32 #ifdef DEBUG_FLAT_LIST
33 # define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
34 #else
35 # define trace( ... )
36 #endif
37
38 /*
39  * Private functions
40  */
41 static void
42 media_list_item_removed( const libvlc_event_t * p_event, void * p_user_data );
43
44 static void
45 media_list_item_added( const libvlc_event_t * p_event, void * p_user_data )
46 {
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 )))
51     {
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 );
58     }
59     if( p_mlv->pf_ml_item_added ) p_mlv->pf_ml_item_added( p_event, p_user_data );
60 }
61
62 static void
63 media_list_item_removed( const libvlc_event_t * p_event, void * p_user_data )
64 {
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 )))
69     {
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 );
76     }
77     if( p_mlv->pf_ml_item_removed ) p_mlv->pf_ml_item_removed( p_event, p_user_data );
78 }
79
80
81 /*
82  * LibVLC Internal functions
83  */
84 void
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 *) )
89 {
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 );
98 }
99
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_children_at_index_func_t pf_children_at_index,
108                             libvlc_media_list_view_release_func_t pf_release,
109                             void * this_view_data,
110                             libvlc_exception_t * p_e )
111 {
112     libvlc_media_list_view_t * p_mlv;
113     p_mlv = calloc( 1, sizeof(libvlc_media_list_view_t) );
114     if( !p_mlv )
115         return NULL;
116
117     p_mlv->p_libvlc_instance = p_mlist->p_libvlc_instance;
118     p_mlv->p_event_manager = libvlc_event_manager_new( p_mlist,
119                                     p_mlv->p_libvlc_instance, p_e );
120
121     libvlc_media_list_retain( p_mlist );
122     p_mlv->p_mlist = p_mlist;
123
124     p_mlv->pf_count             = pf_count;
125     p_mlv->pf_item_at_index     = pf_item_at_index;
126     p_mlv->pf_children_at_index = pf_children_at_index;
127     p_mlv->pf_release           = pf_release;
128
129     p_mlv->p_this_view_data = this_view_data;
130
131     vlc_mutex_init( p_mlv->p_libvlc_instance->p_libvlc_int, &p_mlv->object_lock );
132     p_mlv->i_refcount = 1;
133
134     return p_mlv;
135 }
136
137
138 /*
139  * Public libvlc functions
140  */
141
142 /**************************************************************************
143  *       libvlc_media_list_view_retain (Public)
144  **************************************************************************/
145 void
146 libvlc_media_list_view_retain( libvlc_media_list_view_t * p_mlv )
147 {
148     vlc_mutex_lock( &p_mlv->object_lock );
149     p_mlv->i_refcount++;
150     vlc_mutex_unlock( &p_mlv->object_lock );
151 }
152
153 /**************************************************************************
154  *       libvlc_media_list_view_release (Public)
155  **************************************************************************/
156 void
157 libvlc_media_list_view_release( libvlc_media_list_view_t * p_mlv )
158 {
159     vlc_mutex_lock( &p_mlv->object_lock );
160     p_mlv->i_refcount--;
161     if( p_mlv->i_refcount > 0 )
162     {
163         vlc_mutex_unlock( &p_mlv->object_lock );
164         return;
165     }
166     vlc_mutex_unlock( &p_mlv->object_lock );
167
168     /* Refcount null, time to free */
169
170     if( p_mlv->pf_ml_item_added )
171     {
172         libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
173                             libvlc_MediaListItemAdded,
174                             p_mlv->pf_ml_item_added, p_mlv, NULL );
175         /* XXX: descend the whole tree and remove observer */
176     }
177     if( p_mlv->pf_ml_item_removed )
178     {
179         libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
180                             libvlc_MediaListItemDeleted,
181                             p_mlv->pf_ml_item_removed, p_mlv, NULL );
182         /* XXX: descend the whole tree and remove observer */
183     }
184
185     libvlc_event_manager_release( p_mlv->p_event_manager );
186
187     if( p_mlv->pf_release ) p_mlv->pf_release( p_mlv );
188     libvlc_media_list_release( p_mlv->p_mlist );
189     vlc_mutex_destroy( &p_mlv->object_lock );
190 }
191
192 /* Limited to four args, because it should be enough */
193
194 #define AN_SELECT( collapser, dec1, dec2, dec3, dec4, p, ...) p
195 #define ARGS(...) AN_SELECT( collapser, ##__VA_ARGS__, \
196                                               (p_mlv, arg1, arg2, arg3, arg4, p_e), \
197                                               (p_mlv, arg1, arg2, arg3, p_e), \
198                                               (p_mlv, arg1, arg2, p_e), \
199                                               (p_mlv, arg1, p_e), (p_mlv, p_e) )
200
201 #define MEDIA_LIST_VIEW_FUNCTION( name, ret_type, default_ret_value, /* Params */ ... ) \
202     ret_type \
203     libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
204                                   ##__VA_ARGS__, \
205                                   libvlc_exception_t * p_e ) \
206     { \
207         if( p_mlv->pf_##name ) \
208             return p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
209         libvlc_exception_raise( p_e, "No '" #name "' method in this media_list_view" ); \
210         return default_ret_value;\
211     }
212
213 #define MEDIA_LIST_VIEW_FUNCTION_VOID_RET( name, /* Params */ ... ) \
214     void \
215     libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
216                                   ##__VA_ARGS__, \
217                                   libvlc_exception_t * p_e ) \
218     { \
219         if( p_mlv->pf_##name ) \
220         { \
221             p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
222             return; \
223         } \
224         libvlc_exception_raise( p_e, "No '" #name "' method in this media_list_view" ); \
225     }
226
227
228 MEDIA_LIST_VIEW_FUNCTION( count, int, 0 )
229 MEDIA_LIST_VIEW_FUNCTION( item_at_index, libvlc_media_descriptor_t *, NULL, int arg1 )
230 MEDIA_LIST_VIEW_FUNCTION( children_at_index, libvlc_media_list_view_t *, NULL, int arg1 )
231