media_list_view.c 18.6 KB
Newer Older
1 2 3 4 5
/*****************************************************************************
 * flat_media_list.c: libvlc flat media list functions. (extension to
 * media_list.c).
 *****************************************************************************
 * Copyright (C) 2007 the VideoLAN team
6
 * $Id$
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include "libvlc_internal.h"
#include <vlc/libvlc.h>
#include <assert.h>
#include "vlc_arrays.h"

//#define DEBUG_FLAT_LIST

#ifdef DEBUG_FLAT_LIST
# define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
#else
# define trace( ... )
#endif

/*
 * Private functions
 */
41
static void
42 43
media_list_item_added( const libvlc_event_t * p_event, void * p_user_data );
static void
44
media_list_item_removed( const libvlc_event_t * p_event, void * p_user_data );
45 46
static void
media_list_subitem_added( const libvlc_event_t * p_event, void * p_user_data );
47 48

static void
49 50
install_md_listener( libvlc_media_list_view_t * p_mlv,
                     libvlc_media_descriptor_t * p_md)
51 52 53 54
{
    libvlc_media_list_t * p_mlist;
    if((p_mlist = libvlc_media_descriptor_subitems( p_md, NULL )))
    {
55 56 57 58
        libvlc_media_list_lock( p_mlist );
        int i, count = libvlc_media_list_count( p_mlist, NULL );
        for( i = 0; i < count; i++)
        {
59
            libvlc_event_t added_event;
60 61
            libvlc_media_descriptor_t * p_submd;
            p_submd = libvlc_media_list_item_at_index( p_mlist, i, NULL );
62 63

            /* Install our listeners */
64
            install_md_listener( p_mlv, p_submd );
65 66 67 68 69

            /* For each item, send a notification to the mlv subclasses */
            added_event.u.media_list_item_added.item = p_submd;
            added_event.u.media_list_item_added.index = 0;
            if( p_mlv->pf_ml_item_added ) p_mlv->pf_ml_item_added( &added_event, p_mlv );
70 71
            libvlc_media_descriptor_release( p_submd );
        }
72 73 74 75 76 77
        libvlc_event_attach( p_mlist->p_event_manager,
                             libvlc_MediaListItemAdded,
                             media_list_item_added, p_mlv, NULL );
        libvlc_event_attach( p_mlist->p_event_manager,
                             libvlc_MediaListItemDeleted,
                             media_list_item_removed, p_mlv, NULL );
78 79
        libvlc_media_list_unlock( p_mlist );
        libvlc_media_list_release( p_mlist );
80
    }
81 82 83 84 85 86 87
    else
    {
        /* No mlist, wait for a subitem added event */
        libvlc_event_attach( p_md->p_event_manager,
                            libvlc_MediaDescriptorSubItemAdded,
                            media_list_subitem_added, p_mlv, NULL );
    }
88 89 90
}

static void
91 92
uninstall_md_listener( libvlc_media_list_view_t * p_mlv,
                       libvlc_media_descriptor_t * p_md)
93 94
{
    libvlc_media_list_t * p_mlist;
95 96
    libvlc_exception_t ignored_exception;
    libvlc_exception_init( &ignored_exception );
97 98
    libvlc_event_detach( p_md->p_event_manager,
                         libvlc_MediaDescriptorSubItemAdded,
99 100 101
                         media_list_subitem_added, p_mlv, &ignored_exception );
    if( libvlc_exception_raised( &ignored_exception ) )
        libvlc_exception_clear( &ignored_exception ); /* We don't care if we encounter an exception */
102 103
    if((p_mlist = libvlc_media_descriptor_subitems( p_md, NULL )))
    {
104
        libvlc_media_list_lock( p_mlist );
105 106 107 108 109 110 111
        libvlc_event_detach( p_mlist->p_event_manager,
                             libvlc_MediaListItemAdded,
                             media_list_item_added, p_mlv, NULL );
        libvlc_event_detach( p_mlist->p_event_manager,
                             libvlc_MediaListItemDeleted,
                             media_list_item_removed, p_mlv, NULL );

112 113 114 115 116 117 118 119 120 121
        int i, count = libvlc_media_list_count( p_mlist, NULL );
        for( i = 0; i < count; i++)
        {
            libvlc_media_descriptor_t * p_submd;
            p_submd = libvlc_media_list_item_at_index( p_mlist,i, NULL );
            uninstall_md_listener( p_mlv, p_submd );
            libvlc_media_descriptor_release( p_submd );
        }
        libvlc_media_list_unlock( p_mlist );
        libvlc_media_list_release( p_mlist );
122
    }
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
}

static void
media_list_item_added( const libvlc_event_t * p_event, void * p_user_data )
{
    libvlc_media_list_view_t * p_mlv = p_user_data;
    libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_added.item;
    install_md_listener( p_mlv, p_md );
    if( p_mlv->pf_ml_item_added ) p_mlv->pf_ml_item_added( p_event, p_mlv );
}

static void
media_list_item_removed( const libvlc_event_t * p_event, void * p_user_data )
{
    libvlc_media_list_view_t * p_mlv = p_user_data;
    libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_added.item;
    uninstall_md_listener( p_mlv, p_md );
140
    if( p_mlv->pf_ml_item_removed ) p_mlv->pf_ml_item_removed( p_event, p_mlv );
141 142
}

143 144 145
static void
media_list_subitem_added( const libvlc_event_t * p_event, void * p_user_data )
{
146
    libvlc_media_list_t * p_mlist;
147 148
    libvlc_event_t added_event;
    libvlc_media_list_view_t * p_mlv = p_user_data;
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    libvlc_media_descriptor_t * p_submd = p_event->u.media_descriptor_subitem_added.new_child;
    libvlc_media_descriptor_t * p_md = p_event->p_obj;

    if((p_mlist = libvlc_media_descriptor_subitems( p_md, NULL )))
    {
        /* We have a mlist to which we're going to listen to events
         * thus, no need to wait for SubItemAdded events */
        libvlc_event_detach( p_md->p_event_manager,
                             libvlc_MediaDescriptorSubItemAdded,
                             media_list_subitem_added, p_mlv, NULL );
        libvlc_media_list_lock( p_mlist );

        libvlc_event_attach( p_mlist->p_event_manager,
                             libvlc_MediaListItemAdded,
                             media_list_item_added, p_mlv, NULL );
        libvlc_event_attach( p_mlist->p_event_manager,
                             libvlc_MediaListItemDeleted,
                             media_list_item_removed, p_mlv, NULL );
        libvlc_media_list_unlock( p_mlist );
        libvlc_media_list_release( p_mlist );
    }

    install_md_listener( p_mlv, p_submd );
172

173
    added_event.u.media_list_item_added.item = p_submd;
174 175 176
    added_event.u.media_list_item_added.index = 0;
    if( p_mlv->pf_ml_item_added ) p_mlv->pf_ml_item_added( &added_event, p_mlv );
}
177 178 179 180

/*
 * LibVLC Internal functions
 */
181 182
/**************************************************************************
 *       libvlc_media_list_view_set_ml_notification_callback (Internal)
183
 * The mlist lock should be held when entered
184
 **************************************************************************/
185 186 187
void
libvlc_media_list_view_set_ml_notification_callback(
                libvlc_media_list_view_t * p_mlv,
188 189
                void (*item_added)(const libvlc_event_t *, libvlc_media_list_view_t *),
                void (*item_removed)(const libvlc_event_t *, libvlc_media_list_view_t *) )
190 191 192 193 194 195 196 197 198
{
    p_mlv->pf_ml_item_added = item_added;
    p_mlv->pf_ml_item_removed = item_removed;
    libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
                         libvlc_MediaListItemAdded,
                         media_list_item_added, p_mlv, NULL );
    libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
                         libvlc_MediaListItemDeleted,
                         media_list_item_removed, p_mlv, NULL );
199 200 201 202 203 204 205 206
    int i, count = libvlc_media_list_count( p_mlv->p_mlist, NULL );
    for( i = 0; i < count; i++)
    {
        libvlc_media_descriptor_t * p_md;
        p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, NULL );
        install_md_listener( p_mlv, p_md );
        libvlc_media_descriptor_release( p_md );
    }
207 208
}

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
/**************************************************************************
 *       libvlc_media_list_view_notify_deletion (Internal)
 **************************************************************************/
void
libvlc_media_list_view_will_delete_item(
                libvlc_media_list_view_t * p_mlv,
                libvlc_media_descriptor_t * p_item,
                int index )
{
    libvlc_event_t event;

    /* Construct the event */
    event.type = libvlc_MediaListViewWillDeleteItem;
    event.u.media_list_view_will_delete_item.item = p_item;
    event.u.media_list_view_will_delete_item.index = index;

    /* Send the event */
    libvlc_event_send( p_mlv->p_event_manager, &event );
}

/**************************************************************************
 *       libvlc_media_list_view_item_deleted (Internal)
 **************************************************************************/
void
libvlc_media_list_view_item_deleted(
                libvlc_media_list_view_t * p_mlv,
                libvlc_media_descriptor_t * p_item,
                int index )
{
    libvlc_event_t event;

    /* Construct the event */
    event.type = libvlc_MediaListViewItemDeleted;
    event.u.media_list_view_item_deleted.item = p_item;
    event.u.media_list_view_item_deleted.index = index;

    /* Send the event */
    libvlc_event_send( p_mlv->p_event_manager, &event );
}

/**************************************************************************
 *       libvlc_media_list_view_will_add_item (Internal)
 **************************************************************************/
void
libvlc_media_list_view_will_add_item(
                libvlc_media_list_view_t * p_mlv,
                libvlc_media_descriptor_t * p_item,
                int index )
{
    libvlc_event_t event;

    /* Construct the event */
    event.type = libvlc_MediaListViewWillAddItem;
    event.u.media_list_view_will_add_item.item = p_item;
    event.u.media_list_view_will_add_item.index = index;

    /* Send the event */
    libvlc_event_send( p_mlv->p_event_manager, &event );
}

/**************************************************************************
 *       libvlc_media_list_view_item_added (Internal)
 **************************************************************************/
void
libvlc_media_list_view_item_added(
                libvlc_media_list_view_t * p_mlv,
                libvlc_media_descriptor_t * p_item,
                int index )
{
    libvlc_event_t event;

    /* Construct the event */
    event.type = libvlc_MediaListViewItemAdded;
    event.u.media_list_view_item_added.item = p_item;
    event.u.media_list_view_item_added.index = index;

    /* Send the event */
    libvlc_event_send( p_mlv->p_event_manager, &event );
}

289 290 291 292 293 294 295
/**************************************************************************
 *       libvlc_media_list_view_new (Internal)
 **************************************************************************/
libvlc_media_list_view_t *
libvlc_media_list_view_new( libvlc_media_list_t * p_mlist,
                            libvlc_media_list_view_count_func_t pf_count,
                            libvlc_media_list_view_item_at_index_func_t pf_item_at_index,
296
                            libvlc_media_list_view_children_at_index_func_t pf_children_at_index,
297
                            libvlc_media_list_view_constructor_func_t pf_constructor,
298 299 300 301 302 303 304 305 306 307 308 309 310
                            libvlc_media_list_view_release_func_t pf_release,
                            void * this_view_data,
                            libvlc_exception_t * p_e )
{
    libvlc_media_list_view_t * p_mlv;
    p_mlv = calloc( 1, sizeof(libvlc_media_list_view_t) );
    if( !p_mlv )
        return NULL;

    p_mlv->p_libvlc_instance = p_mlist->p_libvlc_instance;
    p_mlv->p_event_manager = libvlc_event_manager_new( p_mlist,
                                    p_mlv->p_libvlc_instance, p_e );

311 312 313 314 315 316 317 318 319
    libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
            libvlc_MediaListViewItemAdded, p_e );
    libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
            libvlc_MediaListViewWillAddItem, p_e );
    libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
            libvlc_MediaListViewItemDeleted, p_e );
    libvlc_event_manager_register_event_type( p_mlv->p_event_manager,
            libvlc_MediaListViewWillDeleteItem, p_e );

320 321 322
    libvlc_media_list_retain( p_mlist );
    p_mlv->p_mlist = p_mlist;

323 324 325
    p_mlv->pf_count             = pf_count;
    p_mlv->pf_item_at_index     = pf_item_at_index;
    p_mlv->pf_children_at_index = pf_children_at_index;
326
    p_mlv->pf_constructor       = pf_constructor;
327
    p_mlv->pf_release           = pf_release;
328

329
    p_mlv->p_this_view_data = this_view_data;
330 331 332 333 334 335 336

    vlc_mutex_init( p_mlv->p_libvlc_instance->p_libvlc_int, &p_mlv->object_lock );
    p_mlv->i_refcount = 1;

    return p_mlv;
}

337 338 339 340 341

/*
 * Public libvlc functions
 */

342 343 344 345
/**************************************************************************
 *       libvlc_media_list_view_retain (Public)
 **************************************************************************/
void
346
libvlc_media_list_view_retain( libvlc_media_list_view_t * p_mlv )
347 348 349 350 351 352
{
    vlc_mutex_lock( &p_mlv->object_lock );
    p_mlv->i_refcount++;
    vlc_mutex_unlock( &p_mlv->object_lock );
}

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
/**************************************************************************
 *       libvlc_media_list_view_release (Public)
 **************************************************************************/
void
libvlc_media_list_view_release( libvlc_media_list_view_t * p_mlv )
{
    vlc_mutex_lock( &p_mlv->object_lock );
    p_mlv->i_refcount--;
    if( p_mlv->i_refcount > 0 )
    {
        vlc_mutex_unlock( &p_mlv->object_lock );
        return;
    }
    vlc_mutex_unlock( &p_mlv->object_lock );

    /* Refcount null, time to free */
369
    libvlc_media_list_lock( p_mlv->p_mlist );
370 371 372 373 374

    if( p_mlv->pf_ml_item_added )
    {
        libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
                            libvlc_MediaListItemAdded,
375
                            media_list_item_added, p_mlv, NULL );
376 377 378 379 380
    }
    if( p_mlv->pf_ml_item_removed )
    {
        libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
                            libvlc_MediaListItemDeleted,
381 382 383 384 385 386 387 388 389
                            media_list_item_removed, p_mlv, NULL );
    }
    int i, count = libvlc_media_list_count( p_mlv->p_mlist, NULL );
    for( i = 0; i < count; i++)
    {
        libvlc_media_descriptor_t * p_md;
        p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, NULL );
        uninstall_md_listener( p_mlv, p_md );
        libvlc_media_descriptor_release( p_md );
390
    }
391
    libvlc_media_list_unlock( p_mlv->p_mlist );
392 393 394 395 396 397 398 399

    libvlc_event_manager_release( p_mlv->p_event_manager );

    if( p_mlv->pf_release ) p_mlv->pf_release( p_mlv );
    libvlc_media_list_release( p_mlv->p_mlist );
    vlc_mutex_destroy( &p_mlv->object_lock );
}

400 401 402 403 404 405 406 407 408 409 410 411 412
/**************************************************************************
 *       libvlc_media_list_view_event_manager (Public)
 **************************************************************************/
libvlc_event_manager_t *
libvlc_media_list_view_event_manager( libvlc_media_list_view_t * p_mlv )
{
    libvlc_event_manager_t * p_em;
    vlc_mutex_lock( &p_mlv->object_lock );
    p_em = p_mlv->p_event_manager;
    vlc_mutex_unlock( &p_mlv->object_lock );
    return p_em;
}

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
/**************************************************************************
 *       libvlc_media_list_view_parent_media_list (Public)
 **************************************************************************/
libvlc_media_list_t *
libvlc_media_list_view_parent_media_list( libvlc_media_list_view_t * p_mlv,
                                         libvlc_exception_t * p_e)
{
    (void)p_e;
    libvlc_media_list_t * p_mlist;
    vlc_mutex_lock( &p_mlv->object_lock );
    p_mlist = p_mlv->p_mlist;
    libvlc_media_list_retain( p_mlv->p_mlist );
    vlc_mutex_unlock( &p_mlv->object_lock );
    return p_mlist;
}

429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
/**************************************************************************
 *       libvlc_media_list_view_children_for_item (Public)
 **************************************************************************/
libvlc_media_list_view_t *
libvlc_media_list_view_children_for_item( libvlc_media_list_view_t * p_mlv,
                                          libvlc_media_descriptor_t * p_md,
                                          libvlc_exception_t * p_e)
{
    (void)p_e;
    libvlc_media_list_t * p_mlist;
    libvlc_media_list_view_t * ret;

    p_mlist = libvlc_media_descriptor_subitems(p_md, p_e);
    if(!p_mlist) return NULL;

    ret = p_mlv->pf_constructor( p_mlist, p_e );
    libvlc_media_list_release( p_mlist );

    return ret;
}

450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
/* Limited to four args, because it should be enough */

#define AN_SELECT( collapser, dec1, dec2, dec3, dec4, p, ...) p
#define ARGS(...) AN_SELECT( collapser, ##__VA_ARGS__, \
                                              (p_mlv, arg1, arg2, arg3, arg4, p_e), \
                                              (p_mlv, arg1, arg2, arg3, p_e), \
                                              (p_mlv, arg1, arg2, p_e), \
                                              (p_mlv, arg1, p_e), (p_mlv, p_e) )

#define MEDIA_LIST_VIEW_FUNCTION( name, ret_type, default_ret_value, /* Params */ ... ) \
    ret_type \
    libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
                                  ##__VA_ARGS__, \
                                  libvlc_exception_t * p_e ) \
    { \
        if( p_mlv->pf_##name ) \
            return p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
        libvlc_exception_raise( p_e, "No '" #name "' method in this media_list_view" ); \
        return default_ret_value;\
    }

#define MEDIA_LIST_VIEW_FUNCTION_VOID_RET( name, /* Params */ ... ) \
    void \
    libvlc_media_list_view_##name( libvlc_media_list_view_t * p_mlv, \
                                  ##__VA_ARGS__, \
                                  libvlc_exception_t * p_e ) \
    { \
        if( p_mlv->pf_##name ) \
        { \
            p_mlv->pf_##name ARGS(__VA_ARGS__) ; \
            return; \
        } \
        libvlc_exception_raise( p_e, "No '" #name "' method in this media_list_view" ); \
    }


MEDIA_LIST_VIEW_FUNCTION( count, int, 0 )
MEDIA_LIST_VIEW_FUNCTION( item_at_index, libvlc_media_descriptor_t *, NULL, int arg1 )
488
MEDIA_LIST_VIEW_FUNCTION( children_at_index, libvlc_media_list_view_t *, NULL, int arg1 )
489