]> git.sesse.net Git - vlc/blob - src/control/media_list_view.c
libvlc: include config.h when needed
[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$
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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
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>
34
35 #include "libvlc_internal.h"  // Abuse, could and should be removed
36
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"
40
41 //#define DEBUG_FLAT_LIST
42
43 #ifdef DEBUG_FLAT_LIST
44 # define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
45 #else
46 # define trace( ... )
47 #endif
48
49 /*
50  * Private functions
51  */
52 static void
53 media_list_item_added( const libvlc_event_t * p_event, void * p_user_data );
54 static void
55 media_list_item_removed( const libvlc_event_t * p_event, void * p_user_data );
56 static void
57 media_list_subitem_added( const libvlc_event_t * p_event, void * p_user_data );
58
59 static void
60 install_md_listener( libvlc_media_list_view_t * p_mlv,
61                      libvlc_media_t * p_md)
62 {
63     libvlc_media_list_t * p_mlist;
64     if((p_mlist = libvlc_media_subitems( p_md, NULL )))
65     {
66         libvlc_media_list_lock( p_mlist );
67         int i, count = libvlc_media_list_count( p_mlist, NULL );
68         for( i = 0; i < count; i++)
69         {
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 );
73
74             /* Install our listeners */
75             install_md_listener( p_mlv, p_submd );
76
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 );
82         }
83         libvlc_event_attach( p_mlist->p_event_manager,
84                              libvlc_MediaListItemAdded,
85                              media_list_item_added, p_mlv, NULL );
86         libvlc_event_attach( p_mlist->p_event_manager,
87                              libvlc_MediaListItemDeleted,
88                              media_list_item_removed, p_mlv, NULL );
89         libvlc_media_list_unlock( p_mlist );
90         libvlc_media_list_release( p_mlist );
91     }
92     else
93     {
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, NULL );
98     }
99 }
100
101 static void
102 uninstall_md_listener( libvlc_media_list_view_t * p_mlv,
103                        libvlc_media_t * p_md)
104 {
105     libvlc_media_list_t * p_mlist;
106     libvlc_exception_t ignored_exception;
107     libvlc_exception_init( &ignored_exception );
108     libvlc_event_detach( p_md->p_event_manager,
109                          libvlc_MediaSubItemAdded,
110                          media_list_subitem_added, p_mlv, &ignored_exception );
111     if( libvlc_exception_raised( &ignored_exception ) )
112         libvlc_exception_clear( &ignored_exception ); /* We don't care if we encounter an exception */
113     if((p_mlist = libvlc_media_subitems( p_md, NULL )))
114     {
115         libvlc_media_list_lock( p_mlist );
116         libvlc_event_detach( p_mlist->p_event_manager,
117                              libvlc_MediaListItemAdded,
118                              media_list_item_added, p_mlv, NULL );
119         libvlc_event_detach( p_mlist->p_event_manager,
120                              libvlc_MediaListItemDeleted,
121                              media_list_item_removed, p_mlv, NULL );
122
123         int i, count = libvlc_media_list_count( p_mlist, NULL );
124         for( i = 0; i < count; i++)
125         {
126             libvlc_media_t * p_submd;
127             p_submd = libvlc_media_list_item_at_index( p_mlist,i, NULL );
128             uninstall_md_listener( p_mlv, p_submd );
129             libvlc_media_release( p_submd );
130         }
131         libvlc_media_list_unlock( p_mlist );
132         libvlc_media_list_release( p_mlist );
133     }
134 }
135
136 static void
137 media_list_item_added( const libvlc_event_t * p_event, void * p_user_data )
138 {
139     libvlc_media_list_view_t * p_mlv = p_user_data;
140     libvlc_media_t * p_md = p_event->u.media_list_item_added.item;
141     install_md_listener( p_mlv, p_md );
142     if( p_mlv->pf_ml_item_added ) p_mlv->pf_ml_item_added( p_event, p_mlv );
143 }
144
145 static void
146 media_list_item_removed( const libvlc_event_t * p_event, void * p_user_data )
147 {
148     libvlc_media_list_view_t * p_mlv = p_user_data;
149     libvlc_media_t * p_md = p_event->u.media_list_item_added.item;
150     uninstall_md_listener( p_mlv, p_md );
151     if( p_mlv->pf_ml_item_removed ) p_mlv->pf_ml_item_removed( p_event, p_mlv );
152 }
153
154 static void
155 media_list_subitem_added( const libvlc_event_t * p_event, void * p_user_data )
156 {
157     libvlc_media_list_t * p_mlist;
158     libvlc_event_t added_event;
159     libvlc_media_list_view_t * p_mlv = p_user_data;
160     libvlc_media_t * p_submd = p_event->u.media_subitem_added.new_child;
161     libvlc_media_t * p_md = p_event->p_obj;
162
163     if((p_mlist = libvlc_media_subitems( p_md, NULL )))
164     {
165         /* We have a mlist to which we're going to listen to events
166          * thus, no need to wait for SubItemAdded events */
167         libvlc_event_detach( p_md->p_event_manager,
168                              libvlc_MediaSubItemAdded,
169                              media_list_subitem_added, p_mlv, NULL );
170         libvlc_media_list_lock( p_mlist );
171
172         libvlc_event_attach( p_mlist->p_event_manager,
173                              libvlc_MediaListItemAdded,
174                              media_list_item_added, p_mlv, NULL );
175         libvlc_event_attach( p_mlist->p_event_manager,
176                              libvlc_MediaListItemDeleted,
177                              media_list_item_removed, p_mlv, NULL );
178         libvlc_media_list_unlock( p_mlist );
179         libvlc_media_list_release( p_mlist );
180     }
181
182     install_md_listener( p_mlv, p_submd );
183
184     added_event.u.media_list_item_added.item = p_submd;
185     added_event.u.media_list_item_added.index = 0;
186     if( p_mlv->pf_ml_item_added ) p_mlv->pf_ml_item_added( &added_event, p_mlv );
187 }
188
189 /*
190  * LibVLC Internal functions
191  */
192 /**************************************************************************
193  *       libvlc_media_list_view_set_ml_notification_callback (Internal)
194  * The mlist lock should be held when entered
195  **************************************************************************/
196 void
197 libvlc_media_list_view_set_ml_notification_callback(
198                 libvlc_media_list_view_t * p_mlv,
199                 void (*item_added)(const libvlc_event_t *, libvlc_media_list_view_t *),
200                 void (*item_removed)(const libvlc_event_t *, libvlc_media_list_view_t *) )
201 {
202     p_mlv->pf_ml_item_added = item_added;
203     p_mlv->pf_ml_item_removed = item_removed;
204     libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
205                          libvlc_MediaListItemAdded,
206                          media_list_item_added, p_mlv, NULL );
207     libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
208                          libvlc_MediaListItemDeleted,
209                          media_list_item_removed, p_mlv, NULL );
210     int i, count = libvlc_media_list_count( p_mlv->p_mlist, NULL );
211     for( i = 0; i < count; i++)
212     {
213         libvlc_media_t * p_md;
214         p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, NULL );
215         install_md_listener( p_mlv, p_md );
216         libvlc_media_release( p_md );
217     }
218 }
219
220 /**************************************************************************
221  *       libvlc_media_list_view_notify_deletion (Internal)
222  **************************************************************************/
223 void
224 libvlc_media_list_view_will_delete_item(
225                 libvlc_media_list_view_t * p_mlv,
226                 libvlc_media_t * p_item,
227                 int index )
228 {
229     libvlc_event_t event;
230
231     /* Construct the event */
232     event.type = libvlc_MediaListViewWillDeleteItem;
233     event.u.media_list_view_will_delete_item.item = p_item;
234     event.u.media_list_view_will_delete_item.index = index;
235
236     /* Send the event */
237     libvlc_event_send( p_mlv->p_event_manager, &event );
238 }
239
240 /**************************************************************************
241  *       libvlc_media_list_view_item_deleted (Internal)
242  **************************************************************************/
243 void
244 libvlc_media_list_view_item_deleted(
245                 libvlc_media_list_view_t * p_mlv,
246                 libvlc_media_t * p_item,
247                 int index )
248 {
249     libvlc_event_t event;
250
251     /* Construct the event */
252     event.type = libvlc_MediaListViewItemDeleted;
253     event.u.media_list_view_item_deleted.item = p_item;
254     event.u.media_list_view_item_deleted.index = index;
255
256     /* Send the event */
257     libvlc_event_send( p_mlv->p_event_manager, &event );
258 }
259
260 /**************************************************************************
261  *       libvlc_media_list_view_will_add_item (Internal)
262  **************************************************************************/
263 void
264 libvlc_media_list_view_will_add_item(
265                 libvlc_media_list_view_t * p_mlv,
266                 libvlc_media_t * p_item,
267                 int index )
268 {
269     libvlc_event_t event;
270
271     /* Construct the event */
272     event.type = libvlc_MediaListViewWillAddItem;
273     event.u.media_list_view_will_add_item.item = p_item;
274     event.u.media_list_view_will_add_item.index = index;
275
276     /* Send the event */
277     libvlc_event_send( p_mlv->p_event_manager, &event );
278 }
279
280 /**************************************************************************
281  *       libvlc_media_list_view_item_added (Internal)
282  **************************************************************************/
283 void
284 libvlc_media_list_view_item_added(
285                 libvlc_media_list_view_t * p_mlv,
286                 libvlc_media_t * p_item,
287                 int index )
288 {
289     libvlc_event_t event;
290
291     /* Construct the event */
292     event.type = libvlc_MediaListViewItemAdded;
293     event.u.media_list_view_item_added.item = p_item;
294     event.u.media_list_view_item_added.index = index;
295
296     /* Send the event */
297     libvlc_event_send( p_mlv->p_event_manager, &event );
298 }
299
300 /**************************************************************************
301  *       libvlc_media_list_view_new (Internal)
302  **************************************************************************/
303 libvlc_media_list_view_t *
304 libvlc_media_list_view_new( libvlc_media_list_t * p_mlist,
305                             libvlc_media_list_view_count_func_t pf_count,
306                             libvlc_media_list_view_item_at_index_func_t pf_item_at_index,
307                             libvlc_media_list_view_children_at_index_func_t pf_children_at_index,
308                             libvlc_media_list_view_constructor_func_t pf_constructor,
309                             libvlc_media_list_view_release_func_t pf_release,
310                             void * this_view_data,
311                             libvlc_exception_t * p_e )
312 {
313     libvlc_media_list_view_t * p_mlv;
314     p_mlv = calloc( 1, sizeof(libvlc_media_list_view_t) );
315     if( !p_mlv )
316         return NULL;
317
318     p_mlv->p_libvlc_instance = p_mlist->p_libvlc_instance;
319     p_mlv->p_event_manager = libvlc_event_manager_new( p_mlist,
320                                     p_mlv->p_libvlc_instance, p_e );
321
322     libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
323             libvlc_MediaListViewItemAdded, p_e );
324     libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
325             libvlc_MediaListViewWillAddItem, p_e );
326     libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
327             libvlc_MediaListViewItemDeleted, p_e );
328     libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
329             libvlc_MediaListViewWillDeleteItem, p_e );
330
331     libvlc_media_list_retain( p_mlist );
332     p_mlv->p_mlist = p_mlist;
333
334     p_mlv->pf_count             = pf_count;
335     p_mlv->pf_item_at_index     = pf_item_at_index;
336     p_mlv->pf_children_at_index = pf_children_at_index;
337     p_mlv->pf_constructor       = pf_constructor;
338     p_mlv->pf_release           = pf_release;
339
340     p_mlv->p_this_view_data = this_view_data;
341
342     vlc_mutex_init( &p_mlv->object_lock );
343     p_mlv->i_refcount = 1;
344
345     return p_mlv;
346 }
347
348
349 /*
350  * Public libvlc functions
351  */
352
353 /**************************************************************************
354  *       libvlc_media_list_view_retain (Public)
355  **************************************************************************/
356 void
357 libvlc_media_list_view_retain( libvlc_media_list_view_t * p_mlv )
358 {
359     vlc_mutex_lock( &p_mlv->object_lock );
360     p_mlv->i_refcount++;
361     vlc_mutex_unlock( &p_mlv->object_lock );
362 }
363
364 /**************************************************************************
365  *       libvlc_media_list_view_release (Public)
366  **************************************************************************/
367 void
368 libvlc_media_list_view_release( libvlc_media_list_view_t * p_mlv )
369 {
370     vlc_mutex_lock( &p_mlv->object_lock );
371     p_mlv->i_refcount--;
372     if( p_mlv->i_refcount > 0 )
373     {
374         vlc_mutex_unlock( &p_mlv->object_lock );
375         return;
376     }
377     vlc_mutex_unlock( &p_mlv->object_lock );
378
379     /* Refcount null, time to free */
380     libvlc_media_list_lock( p_mlv->p_mlist );
381
382     if( p_mlv->pf_ml_item_added )
383     {
384         libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
385                             libvlc_MediaListItemAdded,
386                             media_list_item_added, p_mlv, NULL );
387     }
388     if( p_mlv->pf_ml_item_removed )
389     {
390         libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
391                             libvlc_MediaListItemDeleted,
392                             media_list_item_removed, p_mlv, NULL );
393     }
394     int i, count = libvlc_media_list_count( p_mlv->p_mlist, NULL );
395     for( i = 0; i < count; i++)
396     {
397         libvlc_media_t * p_md;
398         p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, NULL );
399         uninstall_md_listener( p_mlv, p_md );
400         libvlc_media_release( p_md );
401     }
402     libvlc_media_list_unlock( p_mlv->p_mlist );
403
404     libvlc_event_manager_release( p_mlv->p_event_manager );
405
406     if( p_mlv->pf_release ) p_mlv->pf_release( p_mlv );
407     libvlc_media_list_release( p_mlv->p_mlist );
408     vlc_mutex_destroy( &p_mlv->object_lock );
409 }
410
411 /**************************************************************************
412  *       libvlc_media_list_view_event_manager (Public)
413  **************************************************************************/
414 libvlc_event_manager_t *
415 libvlc_media_list_view_event_manager( libvlc_media_list_view_t * p_mlv )
416 {
417     libvlc_event_manager_t * p_em;
418     vlc_mutex_lock( &p_mlv->object_lock );
419     p_em = p_mlv->p_event_manager;
420     vlc_mutex_unlock( &p_mlv->object_lock );
421     return p_em;
422 }
423
424 /**************************************************************************
425  *       libvlc_media_list_view_parent_media_list (Public)
426  **************************************************************************/
427 libvlc_media_list_t *
428 libvlc_media_list_view_parent_media_list( libvlc_media_list_view_t * p_mlv,
429                                          libvlc_exception_t * p_e)
430 {
431     (void)p_e;
432     libvlc_media_list_t * p_mlist;
433     vlc_mutex_lock( &p_mlv->object_lock );
434     p_mlist = p_mlv->p_mlist;
435     libvlc_media_list_retain( p_mlv->p_mlist );
436     vlc_mutex_unlock( &p_mlv->object_lock );
437     return p_mlist;
438 }
439
440 /**************************************************************************
441  *       libvlc_media_list_view_children_for_item (Public)
442  **************************************************************************/
443 libvlc_media_list_view_t *
444 libvlc_media_list_view_children_for_item( libvlc_media_list_view_t * p_mlv,
445                                           libvlc_media_t * p_md,
446                                           libvlc_exception_t * p_e)
447 {
448     (void)p_e;
449     libvlc_media_list_t * p_mlist;
450     libvlc_media_list_view_t * ret;
451
452     p_mlist = libvlc_media_subitems(p_md, p_e);
453     if(!p_mlist) return NULL;
454
455     ret = p_mlv->pf_constructor( p_mlist, p_e );
456     libvlc_media_list_release( p_mlist );
457
458     return ret;
459 }
460
461 /* Limited to four args, because it should be enough */
462
463 #define AN_SELECT( collapser, dec1, dec2, dec3, dec4, p, ...) p
464 #define ARGS(...) AN_SELECT( collapser, ##__VA_ARGS__, \
465                                               (p_mlv, arg1, arg2, arg3, arg4, p_e), \
466                                               (p_mlv, arg1, arg2, arg3, p_e), \
467                                               (p_mlv, arg1, arg2, p_e), \
468                                               (p_mlv, arg1, p_e), (p_mlv, p_e) )
469
470 #define MEDIA_LIST_VIEW_FUNCTION( name, ret_type, default_ret_value, /* Params */ ... ) \
471     ret_type \
472     libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
473                                   ##__VA_ARGS__, \
474                                   libvlc_exception_t * p_e ) \
475     { \
476         if( p_mlv->pf_##name ) \
477             return p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
478         libvlc_exception_raise( p_e, "No '" #name "' method in this media_list_view" ); \
479         return default_ret_value;\
480     }
481
482 #define MEDIA_LIST_VIEW_FUNCTION_VOID_RET( name, /* Params */ ... ) \
483     void \
484     libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
485                                   ##__VA_ARGS__, \
486                                   libvlc_exception_t * p_e ) \
487     { \
488         if( p_mlv->pf_##name ) \
489         { \
490             p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
491             return; \
492         } \
493         libvlc_exception_raise( p_e, "No '" #name "' method in this media_list_view" ); \
494     }
495
496
497 MEDIA_LIST_VIEW_FUNCTION( count, int, 0 )
498 MEDIA_LIST_VIEW_FUNCTION( item_at_index, libvlc_media_t *, NULL, int arg1 )
499 MEDIA_LIST_VIEW_FUNCTION( children_at_index, libvlc_media_list_view_t *, NULL, int arg1 )
500