]> git.sesse.net Git - vlc/blob - src/control/media_list_view.c
control/media_discoverer.c: Forward started and ended event.
[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_added( const libvlc_event_t * p_event, void * p_user_data );
43 static void
44 media_list_item_removed( const libvlc_event_t * p_event, void * p_user_data );
45 static void
46 media_list_subitem_added( const libvlc_event_t * p_event, void * p_user_data );
47
48 static void
49 install_md_listener( libvlc_media_list_view_t * p_mlv,
50                      libvlc_media_descriptor_t * p_md)
51 {
52     libvlc_media_list_t * p_mlist;
53     if((p_mlist = libvlc_media_descriptor_subitems( p_md, NULL )))
54     {
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++)
58         {
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 );
63         }
64         libvlc_media_list_unlock( p_mlist );
65         libvlc_media_list_release( p_mlist );
66     }
67     libvlc_event_attach( p_md->p_event_manager,
68                          libvlc_MediaDescriptorSubItemAdded,
69                          media_list_subitem_added, p_mlv, NULL );
70 }
71
72 static void
73 uninstall_md_listener( libvlc_media_list_view_t * p_mlv,
74                        libvlc_media_descriptor_t * p_md)
75 {
76     libvlc_media_list_t * p_mlist;
77     libvlc_exception_t ignored_exception;
78     libvlc_exception_init( &ignored_exception );
79     libvlc_event_detach( p_md->p_event_manager,
80                          libvlc_MediaDescriptorSubItemAdded,
81                          media_list_subitem_added, p_mlv, &ignored_exception );
82     if( libvlc_exception_raised( &ignored_exception ) )
83         libvlc_exception_clear( &ignored_exception ); /* We don't care if we encounter an exception */
84     if((p_mlist = libvlc_media_descriptor_subitems( p_md, NULL )))
85     {
86         libvlc_media_list_lock( p_mlist );
87         int i, count = libvlc_media_list_count( p_mlist, NULL );
88         for( i = 0; i < count; i++)
89         {
90             libvlc_media_descriptor_t * p_submd;
91             p_submd = libvlc_media_list_item_at_index( p_mlist,i, NULL );
92             uninstall_md_listener( p_mlv, p_submd );
93             libvlc_media_descriptor_release( p_submd );
94         }
95         libvlc_media_list_unlock( p_mlist );
96         libvlc_media_list_release( p_mlist );
97     }
98 }
99
100 static void
101 media_list_item_added( const libvlc_event_t * p_event, void * p_user_data )
102 {
103     libvlc_media_list_view_t * p_mlv = p_user_data;
104     libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_added.item;
105     install_md_listener( p_mlv, p_md );
106     if( p_mlv->pf_ml_item_added ) p_mlv->pf_ml_item_added( p_event, p_mlv );
107 }
108
109 static void
110 media_list_item_removed( const libvlc_event_t * p_event, void * p_user_data )
111 {
112     libvlc_media_list_view_t * p_mlv = p_user_data;
113     libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_added.item;
114     uninstall_md_listener( p_mlv, p_md );
115     if( p_mlv->pf_ml_item_removed ) p_mlv->pf_ml_item_removed( p_event, p_mlv );
116 }
117
118 static void
119 media_list_subitem_added( const libvlc_event_t * p_event, void * p_user_data )
120 {
121     libvlc_event_t added_event;
122     libvlc_media_list_view_t * p_mlv = p_user_data;
123     libvlc_media_descriptor_t * p_md = p_event->u.media_descriptor_subitem_added.new_child;
124     install_md_listener( p_mlv, p_md );
125
126     added_event.u.media_list_item_added.item = p_md;
127     added_event.u.media_list_item_added.index = 0;
128     if( p_mlv->pf_ml_item_added ) p_mlv->pf_ml_item_added( &added_event, p_mlv );
129 }
130
131 /*
132  * LibVLC Internal functions
133  */
134 /**************************************************************************
135  *       libvlc_media_list_view_set_ml_notification_callback (Internal)
136  * The mlist lock should be held when entered
137  **************************************************************************/
138 void
139 libvlc_media_list_view_set_ml_notification_callback(
140                 libvlc_media_list_view_t * p_mlv,
141                 void (*item_added)(const libvlc_event_t *, libvlc_media_list_view_t *),
142                 void (*item_removed)(const libvlc_event_t *, libvlc_media_list_view_t *) )
143 {
144     p_mlv->pf_ml_item_added = item_added;
145     p_mlv->pf_ml_item_removed = item_removed;
146     libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
147                          libvlc_MediaListItemAdded,
148                          media_list_item_added, p_mlv, NULL );
149     libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
150                          libvlc_MediaListItemDeleted,
151                          media_list_item_removed, p_mlv, NULL );
152     int i, count = libvlc_media_list_count( p_mlv->p_mlist, NULL );
153     for( i = 0; i < count; i++)
154     {
155         libvlc_media_descriptor_t * p_md;
156         p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, NULL );
157         install_md_listener( p_mlv, p_md );
158         libvlc_media_descriptor_release( p_md );
159     }
160 }
161
162 /**************************************************************************
163  *       libvlc_media_list_view_notify_deletion (Internal)
164  **************************************************************************/
165 void
166 libvlc_media_list_view_will_delete_item(
167                 libvlc_media_list_view_t * p_mlv,
168                 libvlc_media_descriptor_t * p_item,
169                 int index )
170 {
171     libvlc_event_t event;
172
173     /* Construct the event */
174     event.type = libvlc_MediaListViewWillDeleteItem;
175     event.u.media_list_view_will_delete_item.item = p_item;
176     event.u.media_list_view_will_delete_item.index = index;
177
178     /* Send the event */
179     libvlc_event_send( p_mlv->p_event_manager, &event );
180 }
181
182 /**************************************************************************
183  *       libvlc_media_list_view_item_deleted (Internal)
184  **************************************************************************/
185 void
186 libvlc_media_list_view_item_deleted(
187                 libvlc_media_list_view_t * p_mlv,
188                 libvlc_media_descriptor_t * p_item,
189                 int index )
190 {
191     libvlc_event_t event;
192
193     /* Construct the event */
194     event.type = libvlc_MediaListViewItemDeleted;
195     event.u.media_list_view_item_deleted.item = p_item;
196     event.u.media_list_view_item_deleted.index = index;
197
198     /* Send the event */
199     libvlc_event_send( p_mlv->p_event_manager, &event );
200 }
201
202 /**************************************************************************
203  *       libvlc_media_list_view_will_add_item (Internal)
204  **************************************************************************/
205 void
206 libvlc_media_list_view_will_add_item(
207                 libvlc_media_list_view_t * p_mlv,
208                 libvlc_media_descriptor_t * p_item,
209                 int index )
210 {
211     libvlc_event_t event;
212
213     /* Construct the event */
214     event.type = libvlc_MediaListViewWillAddItem;
215     event.u.media_list_view_will_add_item.item = p_item;
216     event.u.media_list_view_will_add_item.index = index;
217
218     /* Send the event */
219     libvlc_event_send( p_mlv->p_event_manager, &event );
220 }
221
222 /**************************************************************************
223  *       libvlc_media_list_view_item_added (Internal)
224  **************************************************************************/
225 void
226 libvlc_media_list_view_item_added(
227                 libvlc_media_list_view_t * p_mlv,
228                 libvlc_media_descriptor_t * p_item,
229                 int index )
230 {
231     libvlc_event_t event;
232
233     /* Construct the event */
234     event.type = libvlc_MediaListViewItemAdded;
235     event.u.media_list_view_item_added.item = p_item;
236     event.u.media_list_view_item_added.index = index;
237
238     /* Send the event */
239     libvlc_event_send( p_mlv->p_event_manager, &event );
240 }
241
242 /**************************************************************************
243  *       libvlc_media_list_view_new (Internal)
244  **************************************************************************/
245 libvlc_media_list_view_t *
246 libvlc_media_list_view_new( libvlc_media_list_t * p_mlist,
247                             libvlc_media_list_view_count_func_t pf_count,
248                             libvlc_media_list_view_item_at_index_func_t pf_item_at_index,
249                             libvlc_media_list_view_children_at_index_func_t pf_children_at_index,
250                             libvlc_media_list_view_release_func_t pf_release,
251                             void * this_view_data,
252                             libvlc_exception_t * p_e )
253 {
254     libvlc_media_list_view_t * p_mlv;
255     p_mlv = calloc( 1, sizeof(libvlc_media_list_view_t) );
256     if( !p_mlv )
257         return NULL;
258
259     p_mlv->p_libvlc_instance = p_mlist->p_libvlc_instance;
260     p_mlv->p_event_manager = libvlc_event_manager_new( p_mlist,
261                                     p_mlv->p_libvlc_instance, p_e );
262
263     libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
264             libvlc_MediaListViewItemAdded, p_e );
265     libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
266             libvlc_MediaListViewWillAddItem, p_e );
267     libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
268             libvlc_MediaListViewItemDeleted, p_e );
269     libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
270             libvlc_MediaListViewWillDeleteItem, p_e );
271
272     libvlc_media_list_retain( p_mlist );
273     p_mlv->p_mlist = p_mlist;
274
275     p_mlv->pf_count             = pf_count;
276     p_mlv->pf_item_at_index     = pf_item_at_index;
277     p_mlv->pf_children_at_index = pf_children_at_index;
278     p_mlv->pf_release           = pf_release;
279
280     p_mlv->p_this_view_data = this_view_data;
281
282     vlc_mutex_init( p_mlv->p_libvlc_instance->p_libvlc_int, &p_mlv->object_lock );
283     p_mlv->i_refcount = 1;
284
285     return p_mlv;
286 }
287
288
289 /*
290  * Public libvlc functions
291  */
292
293 /**************************************************************************
294  *       libvlc_media_list_view_retain (Public)
295  **************************************************************************/
296 void
297 libvlc_media_list_view_retain( libvlc_media_list_view_t * p_mlv )
298 {
299     vlc_mutex_lock( &p_mlv->object_lock );
300     p_mlv->i_refcount++;
301     vlc_mutex_unlock( &p_mlv->object_lock );
302 }
303
304 /**************************************************************************
305  *       libvlc_media_list_view_release (Public)
306  **************************************************************************/
307 void
308 libvlc_media_list_view_release( libvlc_media_list_view_t * p_mlv )
309 {
310     vlc_mutex_lock( &p_mlv->object_lock );
311     p_mlv->i_refcount--;
312     if( p_mlv->i_refcount > 0 )
313     {
314         vlc_mutex_unlock( &p_mlv->object_lock );
315         return;
316     }
317     vlc_mutex_unlock( &p_mlv->object_lock );
318
319     /* Refcount null, time to free */
320     libvlc_media_list_lock( p_mlv->p_mlist );
321
322     if( p_mlv->pf_ml_item_added )
323     {
324         libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
325                             libvlc_MediaListItemAdded,
326                             media_list_item_added, p_mlv, NULL );
327     }
328     if( p_mlv->pf_ml_item_removed )
329     {
330         libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
331                             libvlc_MediaListItemDeleted,
332                             media_list_item_removed, p_mlv, NULL );
333     }
334     int i, count = libvlc_media_list_count( p_mlv->p_mlist, NULL );
335     for( i = 0; i < count; i++)
336     {
337         libvlc_media_descriptor_t * p_md;
338         p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, NULL );
339         uninstall_md_listener( p_mlv, p_md );
340         libvlc_media_descriptor_release( p_md );
341     }
342     libvlc_media_list_unlock( p_mlv->p_mlist );
343
344     libvlc_event_manager_release( p_mlv->p_event_manager );
345
346     if( p_mlv->pf_release ) p_mlv->pf_release( p_mlv );
347     libvlc_media_list_release( p_mlv->p_mlist );
348     vlc_mutex_destroy( &p_mlv->object_lock );
349 }
350
351 /**************************************************************************
352  *       libvlc_media_list_view_event_manager (Public)
353  **************************************************************************/
354 libvlc_event_manager_t *
355 libvlc_media_list_view_event_manager( libvlc_media_list_view_t * p_mlv )
356 {
357     libvlc_event_manager_t * p_em;
358     vlc_mutex_lock( &p_mlv->object_lock );
359     p_em = p_mlv->p_event_manager;
360     vlc_mutex_unlock( &p_mlv->object_lock );
361     return p_em;
362 }
363
364 /**************************************************************************
365  *       libvlc_media_list_view_parent_media_list (Public)
366  **************************************************************************/
367 libvlc_media_list_t *
368 libvlc_media_list_view_parent_media_list( libvlc_media_list_view_t * p_mlv,
369                                          libvlc_exception_t * p_e)
370 {
371     (void)p_e;
372     libvlc_media_list_t * p_mlist;
373     vlc_mutex_lock( &p_mlv->object_lock );
374     p_mlist = p_mlv->p_mlist;
375     libvlc_media_list_retain( p_mlv->p_mlist );
376     vlc_mutex_unlock( &p_mlv->object_lock );
377     return p_mlist;
378 }
379
380 /* Limited to four args, because it should be enough */
381
382 #define AN_SELECT( collapser, dec1, dec2, dec3, dec4, p, ...) p
383 #define ARGS(...) AN_SELECT( collapser, ##__VA_ARGS__, \
384                                               (p_mlv, arg1, arg2, arg3, arg4, p_e), \
385                                               (p_mlv, arg1, arg2, arg3, p_e), \
386                                               (p_mlv, arg1, arg2, p_e), \
387                                               (p_mlv, arg1, p_e), (p_mlv, p_e) )
388
389 #define MEDIA_LIST_VIEW_FUNCTION( name, ret_type, default_ret_value, /* Params */ ... ) \
390     ret_type \
391     libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
392                                   ##__VA_ARGS__, \
393                                   libvlc_exception_t * p_e ) \
394     { \
395         if( p_mlv->pf_##name ) \
396             return p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
397         libvlc_exception_raise( p_e, "No '" #name "' method in this media_list_view" ); \
398         return default_ret_value;\
399     }
400
401 #define MEDIA_LIST_VIEW_FUNCTION_VOID_RET( name, /* Params */ ... ) \
402     void \
403     libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
404                                   ##__VA_ARGS__, \
405                                   libvlc_exception_t * p_e ) \
406     { \
407         if( p_mlv->pf_##name ) \
408         { \
409             p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
410             return; \
411         } \
412         libvlc_exception_raise( p_e, "No '" #name "' method in this media_list_view" ); \
413     }
414
415
416 MEDIA_LIST_VIEW_FUNCTION( count, int, 0 )
417 MEDIA_LIST_VIEW_FUNCTION( item_at_index, libvlc_media_descriptor_t *, NULL, int arg1 )
418 MEDIA_LIST_VIEW_FUNCTION( children_at_index, libvlc_media_list_view_t *, NULL, int arg1 )
419