Fixed typo into invmem error message
[vlc.git] / src / control / media_list_view.c
blobba8ea6490f4a1be5281e091711127b418e71b16a
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$
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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
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>
35 #include "libvlc_internal.h" // Abuse, could and should be removed
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"
41 //#define DEBUG_FLAT_LIST
43 #ifdef DEBUG_FLAT_LIST
44 # define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
45 #else
46 # define trace( ... )
47 #endif
50 * Private functions
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 );
59 static void
60 install_md_listener( libvlc_media_list_view_t * p_mlv,
61 libvlc_media_t * p_md)
63 libvlc_media_list_t * p_mlist;
64 if((p_mlist = libvlc_media_subitems( p_md, NULL )))
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++)
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 );
74 /* Install our listeners */
75 install_md_listener( p_mlv, p_submd );
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 );
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 );
92 else
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 );
101 static void
102 uninstall_md_listener( libvlc_media_list_view_t * p_mlv,
103 libvlc_media_t * p_md)
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 )))
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 );
123 int i, count = libvlc_media_list_count( p_mlist, NULL );
124 for( i = 0; i < count; i++)
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 );
131 libvlc_media_list_unlock( p_mlist );
132 libvlc_media_list_release( p_mlist );
136 static void
137 media_list_item_added( const libvlc_event_t * p_event, void * p_user_data )
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 );
145 static void
146 media_list_item_removed( const libvlc_event_t * p_event, void * p_user_data )
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 );
154 static void
155 media_list_subitem_added( const libvlc_event_t * p_event, void * p_user_data )
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;
163 if((p_mlist = libvlc_media_subitems( p_md, NULL )))
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 );
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 );
182 install_md_listener( p_mlv, p_submd );
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 );
190 * LibVLC Internal functions
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 *) )
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++)
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 );
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 )
229 libvlc_event_t event;
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;
236 /* Send the event */
237 libvlc_event_send( p_mlv->p_event_manager, &event );
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 )
249 libvlc_event_t event;
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;
256 /* Send the event */
257 libvlc_event_send( p_mlv->p_event_manager, &event );
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 )
269 libvlc_event_t event;
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;
276 /* Send the event */
277 libvlc_event_send( p_mlv->p_event_manager, &event );
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 )
289 libvlc_event_t event;
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;
296 /* Send the event */
297 libvlc_event_send( p_mlv->p_event_manager, &event );
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 )
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;
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 );
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 );
331 libvlc_media_list_retain( p_mlist );
332 p_mlv->p_mlist = p_mlist;
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;
340 p_mlv->p_this_view_data = this_view_data;
342 vlc_mutex_init( &p_mlv->object_lock );
343 p_mlv->i_refcount = 1;
345 return p_mlv;
350 * Public libvlc functions
353 /**************************************************************************
354 * libvlc_media_list_view_retain (Public)
355 **************************************************************************/
356 void
357 libvlc_media_list_view_retain( libvlc_media_list_view_t * p_mlv )
359 vlc_mutex_lock( &p_mlv->object_lock );
360 p_mlv->i_refcount++;
361 vlc_mutex_unlock( &p_mlv->object_lock );
364 /**************************************************************************
365 * libvlc_media_list_view_release (Public)
366 **************************************************************************/
367 void
368 libvlc_media_list_view_release( libvlc_media_list_view_t * p_mlv )
370 vlc_mutex_lock( &p_mlv->object_lock );
371 p_mlv->i_refcount--;
372 if( p_mlv->i_refcount > 0 )
374 vlc_mutex_unlock( &p_mlv->object_lock );
375 return;
377 vlc_mutex_unlock( &p_mlv->object_lock );
379 /* Refcount null, time to free */
380 libvlc_media_list_lock( p_mlv->p_mlist );
382 if( p_mlv->pf_ml_item_added )
384 libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
385 libvlc_MediaListItemAdded,
386 media_list_item_added, p_mlv, NULL );
388 if( p_mlv->pf_ml_item_removed )
390 libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
391 libvlc_MediaListItemDeleted,
392 media_list_item_removed, p_mlv, NULL );
394 int i, count = libvlc_media_list_count( p_mlv->p_mlist, NULL );
395 for( i = 0; i < count; i++)
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 );
402 libvlc_media_list_unlock( p_mlv->p_mlist );
404 libvlc_event_manager_release( p_mlv->p_event_manager );
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 );
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 )
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;
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)
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;
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)
448 (void)p_e;
449 libvlc_media_list_t * p_mlist;
450 libvlc_media_list_view_t * ret;
452 p_mlist = libvlc_media_subitems(p_md, p_e);
453 if(!p_mlist) return NULL;
455 ret = p_mlv->pf_constructor( p_mlist, p_e );
456 libvlc_media_list_release( p_mlist );
458 return ret;
461 /* Limited to four args, because it should be enough */
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) )
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 ) \
476 if( p_mlv->pf_##name ) \
477 return p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
478 libvlc_exception_raise( p_e ); \
479 libvlc_printerr( "No '" #name "' method in this media_list_view" ); \
480 return default_ret_value;\
483 #define MEDIA_LIST_VIEW_FUNCTION_VOID_RET( name, /* Params */ ... ) \
484 void \
485 libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
486 ##__VA_ARGS__, \
487 libvlc_exception_t * p_e ) \
489 if( p_mlv->pf_##name ) \
491 p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
492 return; \
494 libvlc_exception_raise( p_e ); \
495 libvlc_printerr( "No '" #name "' method in this media_list_view" ); \
499 MEDIA_LIST_VIEW_FUNCTION( count, int, 0 )
500 MEDIA_LIST_VIEW_FUNCTION( item_at_index, libvlc_media_t *, NULL, int arg1 )
501 MEDIA_LIST_VIEW_FUNCTION( children_at_index, libvlc_media_list_view_t *, NULL, int arg1 )