media_list_view.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:19k
源码类别:

midi

开发平台:

Unix_Linux

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