s/libvlc_media_list_view_release/libvlc_media_list_view_retain/ in previous commit.
[vlc.git] / 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_release_func_t pf_release,
108                             void * this_view_data,
109                             libvlc_exception_t * p_e )
110 {
111     libvlc_media_list_view_t * p_mlv;
112     p_mlv = calloc( 1, sizeof(libvlc_media_list_view_t) );
113     if( !p_mlv )
114         return NULL;
115
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 );
119
120     libvlc_media_list_retain( p_mlist );
121     p_mlv->p_mlist = p_mlist;
122
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;
126
127     p_mlv->p_this_view_data = this_view_data;
128
129     vlc_mutex_init( p_mlv->p_libvlc_instance->p_libvlc_int, &p_mlv->object_lock );
130     p_mlv->i_refcount = 1;
131
132     return p_mlv;
133 }
134
135
136 /*
137  * Public libvlc functions
138  */
139
140 /**************************************************************************
141  *       libvlc_media_list_view_retain (Public)
142  **************************************************************************/
143 void
144 libvlc_media_list_view_retain( libvlc_media_list_view_t * p_mlv )
145 {
146     vlc_mutex_lock( &p_mlv->object_lock );
147     p_mlv->i_refcount++;
148     vlc_mutex_unlock( &p_mlv->object_lock );
149 }
150
151 /**************************************************************************
152  *       libvlc_media_list_view_release (Public)
153  **************************************************************************/
154 void
155 libvlc_media_list_view_release( libvlc_media_list_view_t * p_mlv )
156 {
157     vlc_mutex_lock( &p_mlv->object_lock );
158     p_mlv->i_refcount--;
159     if( p_mlv->i_refcount > 0 )
160     {
161         vlc_mutex_unlock( &p_mlv->object_lock );
162         return;
163     }
164     vlc_mutex_unlock( &p_mlv->object_lock );
165
166     /* Refcount null, time to free */
167
168     if( p_mlv->pf_ml_item_added )
169     {
170         libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
171                             libvlc_MediaListItemAdded,
172                             p_mlv->pf_ml_item_added, p_mlv, NULL );
173         /* XXX: descend the whole tree and remove observer */
174     }
175     if( p_mlv->pf_ml_item_removed )
176     {
177         libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
178                             libvlc_MediaListItemDeleted,
179                             p_mlv->pf_ml_item_removed, p_mlv, NULL );
180         /* XXX: descend the whole tree and remove observer */
181     }
182
183     libvlc_event_manager_release( p_mlv->p_event_manager );
184
185     if( p_mlv->pf_release ) p_mlv->pf_release( p_mlv );
186     libvlc_media_list_release( p_mlv->p_mlist );
187     vlc_mutex_destroy( &p_mlv->object_lock );
188 }
189
190 /* Limited to four args, because it should be enough */
191
192 #define AN_SELECT( collapser, dec1, dec2, dec3, dec4, p, ...) p
193 #define ARGS(...) AN_SELECT( collapser, ##__VA_ARGS__, \
194                                               (p_mlv, arg1, arg2, arg3, arg4, p_e), \
195                                               (p_mlv, arg1, arg2, arg3, p_e), \
196                                               (p_mlv, arg1, arg2, p_e), \
197                                               (p_mlv, arg1, p_e), (p_mlv, p_e) )
198
199 #define MEDIA_LIST_VIEW_FUNCTION( name, ret_type, default_ret_value, /* Params */ ... ) \
200     ret_type \
201     libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
202                                   ##__VA_ARGS__, \
203                                   libvlc_exception_t * p_e ) \
204     { \
205         if( p_mlv->pf_##name ) \
206             return p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
207         libvlc_exception_raise( p_e, "No '" #name "' method in this media_list_view" ); \
208         return default_ret_value;\
209     }
210
211 #define MEDIA_LIST_VIEW_FUNCTION_VOID_RET( name, /* Params */ ... ) \
212     void \
213     libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
214                                   ##__VA_ARGS__, \
215                                   libvlc_exception_t * p_e ) \
216     { \
217         if( p_mlv->pf_##name ) \
218         { \
219             p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
220             return; \
221         } \
222         libvlc_exception_raise( p_e, "No '" #name "' method in this media_list_view" ); \
223     }
224
225
226 MEDIA_LIST_VIEW_FUNCTION( count, int, 0 )
227 MEDIA_LIST_VIEW_FUNCTION( item_at_index, libvlc_media_descriptor_t *, NULL, int arg1 )
228