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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * event.c: New libvlc event control API
  3.  *****************************************************************************
  4.  * Copyright (C) 2007 the VideoLAN team
  5.  * $Id $
  6.  *
  7.  * Authors: Filippo Carone <filippo@carone.org>
  8.  *          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 "libvlc_internal.h"
  26. #include "event_internal.h"
  27. typedef struct libvlc_event_listeners_group_t
  28. {
  29.     libvlc_event_type_t event_type;
  30.     vlc_array_t listeners;
  31.     bool b_sublistener_removed;
  32. } libvlc_event_listeners_group_t;
  33. /*
  34.  * Private functions
  35.  */
  36. static bool
  37. group_contains_listener( libvlc_event_listeners_group_t * group,
  38.                          libvlc_event_listener_t * searched_listener )
  39. {
  40.     int i;
  41.     for( i = 0; i < vlc_array_count(&group->listeners); i++ )
  42.     {
  43.         if( listeners_are_equal(searched_listener, vlc_array_item_at_index(&group->listeners, i)) )
  44.             return true;
  45.     }
  46.     return false;
  47. }
  48. /*
  49.  * Internal libvlc functions
  50.  */
  51. /**************************************************************************
  52.  *       libvlc_event_manager_new (internal) :
  53.  *
  54.  * Init an object's event manager.
  55.  **************************************************************************/
  56. libvlc_event_manager_t *
  57. libvlc_event_manager_new( void * p_obj, libvlc_instance_t * p_libvlc_inst,
  58.                            libvlc_exception_t *p_e )
  59. {
  60.     libvlc_event_manager_t * p_em;
  61.     p_em = malloc(sizeof( libvlc_event_manager_t ));
  62.     if( !p_em )
  63.     {
  64.         libvlc_exception_raise( p_e, "No Memory left" );
  65.         return NULL;
  66.     }
  67.     p_em->p_obj = p_obj;
  68.     p_em->p_obj = p_obj;
  69.     p_em->async_event_queue = NULL;
  70.     p_em->p_libvlc_instance = p_libvlc_inst;
  71.     libvlc_retain( p_libvlc_inst );
  72.     vlc_array_init( &p_em->listeners_groups );
  73.     vlc_mutex_init( &p_em->object_lock );
  74.     vlc_mutex_init_recursive( &p_em->event_sending_lock );
  75.     return p_em;
  76. }
  77. /**************************************************************************
  78.  *       libvlc_event_manager_release (internal) :
  79.  *
  80.  * Init an object's event manager.
  81.  **************************************************************************/
  82. void libvlc_event_manager_release( libvlc_event_manager_t * p_em )
  83. {
  84.     libvlc_event_listeners_group_t * p_lg;
  85.     int i,j ;
  86.     libvlc_event_async_fini(p_em);
  87.     vlc_mutex_destroy( &p_em->event_sending_lock );
  88.     vlc_mutex_destroy( &p_em->object_lock );
  89.     for( i = 0; i < vlc_array_count(&p_em->listeners_groups); i++)
  90.     {
  91.         p_lg = vlc_array_item_at_index( &p_em->listeners_groups, i );
  92.         for( j = 0; j < vlc_array_count(&p_lg->listeners); j++)
  93.             free( vlc_array_item_at_index( &p_lg->listeners, j ) );
  94.         vlc_array_clear( &p_lg->listeners );
  95.         free( p_lg );
  96.     }
  97.     vlc_array_clear( &p_em->listeners_groups );
  98.     libvlc_release( p_em->p_libvlc_instance );
  99.     free( p_em );
  100. }
  101. /**************************************************************************
  102.  *       libvlc_event_manager_register_event_type (internal) :
  103.  *
  104.  * Init an object's event manager.
  105.  **************************************************************************/
  106. void libvlc_event_manager_register_event_type(
  107.         libvlc_event_manager_t * p_em,
  108.         libvlc_event_type_t event_type,
  109.         libvlc_exception_t * p_e )
  110. {
  111.     libvlc_event_listeners_group_t * listeners_group;
  112.     listeners_group = malloc(sizeof(libvlc_event_listeners_group_t));
  113.     if( !listeners_group )
  114.     {
  115.         libvlc_exception_raise( p_e, "No Memory left" );
  116.         return;
  117.     }
  118.     listeners_group->event_type = event_type;
  119.     vlc_array_init( &listeners_group->listeners );
  120.     vlc_mutex_lock( &p_em->object_lock );
  121.     vlc_array_append( &p_em->listeners_groups, listeners_group );
  122.     vlc_mutex_unlock( &p_em->object_lock );
  123. }
  124. /**************************************************************************
  125.  *       libvlc_event_send (internal) :
  126.  *
  127.  * Send a callback.
  128.  **************************************************************************/
  129. void libvlc_event_send( libvlc_event_manager_t * p_em,
  130.                         libvlc_event_t * p_event )
  131. {
  132.     libvlc_event_listeners_group_t * listeners_group = NULL;
  133.     libvlc_event_listener_t * listener_cached;
  134.     libvlc_event_listener_t * listener;
  135.     libvlc_event_listener_t * array_listeners_cached = NULL;
  136.     int i, i_cached_listeners = 0;
  137.     /* Fill event with the sending object now */
  138.     p_event->p_obj = p_em->p_obj;
  139.     /* Here a read/write lock would be nice */
  140.     vlc_mutex_lock( &p_em->object_lock );
  141.     for( i = 0; i < vlc_array_count(&p_em->listeners_groups); i++)
  142.     {
  143.         listeners_group = vlc_array_item_at_index(&p_em->listeners_groups, i);
  144.         if( listeners_group->event_type == p_event->type )
  145.         {
  146.             if( vlc_array_count( &listeners_group->listeners ) <= 0 )
  147.                 break;
  148.             /* Cache a copy of the listener to avoid locking issues,
  149.              * and allow that edition of listeners during callbacks will garantee immediate effect. */
  150.             i_cached_listeners = vlc_array_count(&listeners_group->listeners);
  151.             array_listeners_cached = malloc(sizeof(libvlc_event_listener_t)*(i_cached_listeners));
  152.             if( !array_listeners_cached )
  153.             {
  154.                 vlc_mutex_unlock( &p_em->object_lock );
  155.                 fprintf(stderr, "Can't alloc memory in libvlc_event_send" );
  156.                 return;
  157.             }
  158.             listener_cached = array_listeners_cached;
  159.             for( i = 0; i < vlc_array_count(&listeners_group->listeners); i++)
  160.             {
  161.                 listener = vlc_array_item_at_index(&listeners_group->listeners, i);
  162.                 memcpy( listener_cached, listener, sizeof(libvlc_event_listener_t) );
  163.                 listener_cached++;
  164.             }
  165.             break;
  166.         }
  167.     }
  168.     if( !listeners_group )
  169.     {
  170.         free( array_listeners_cached );
  171.         vlc_mutex_unlock( &p_em->object_lock );
  172.         return;
  173.     }
  174.     vlc_mutex_unlock( &p_em->object_lock );
  175.     vlc_mutex_lock( &p_em->event_sending_lock );
  176.     listener_cached = array_listeners_cached;
  177.     listeners_group->b_sublistener_removed = false;
  178.     for( i = 0; i < i_cached_listeners; i++ )
  179.     {
  180.         if(listener_cached->is_asynchronous)
  181.         {
  182.             /* The listener wants not to block the emitter during event callback */
  183.             libvlc_event_async_dispatch(p_em, listener_cached, p_event);
  184.         }
  185.         else
  186.         {
  187.             /* The listener wants to block the emitter during event callback */
  188.             
  189.             listener_cached->pf_callback( p_event, listener_cached->p_user_data );
  190.             listener_cached++;
  191.             
  192.             if( listeners_group->b_sublistener_removed )
  193.             {
  194.                 /* If a callback was removed, this gets called */
  195.                 bool valid_listener;
  196.                 vlc_mutex_lock( &p_em->object_lock );
  197.                 valid_listener = group_contains_listener( listeners_group, listener_cached );
  198.                 vlc_mutex_unlock( &p_em->object_lock );
  199.                 if( !valid_listener )
  200.                 {
  201.                     listener_cached++;
  202.                     continue;
  203.                 }
  204.             }            
  205.         }
  206.     }
  207.     vlc_mutex_unlock( &p_em->event_sending_lock );
  208.     free( array_listeners_cached );
  209. }
  210. /*
  211.  * Public libvlc functions
  212.  */
  213. /**************************************************************************
  214.  *       libvlc_event_type_name (public) :
  215.  *
  216.  * Get the char * name of an event type.
  217.  **************************************************************************/
  218. static const char event_type_to_name[][35] =
  219. {
  220. #define EVENT(a) [a]=#a
  221.     EVENT(libvlc_MediaMetaChanged),
  222.     EVENT(libvlc_MediaSubItemAdded),
  223.     EVENT(libvlc_MediaDurationChanged),
  224.     EVENT(libvlc_MediaPreparsedChanged),
  225.     EVENT(libvlc_MediaFreed),
  226.     EVENT(libvlc_MediaStateChanged),
  227.     EVENT(libvlc_MediaPlayerNothingSpecial),
  228.     EVENT(libvlc_MediaPlayerOpening),
  229.     EVENT(libvlc_MediaPlayerBuffering),
  230.     EVENT(libvlc_MediaPlayerPlaying),
  231.     EVENT(libvlc_MediaPlayerPaused),
  232.     EVENT(libvlc_MediaPlayerStopped),
  233.     EVENT(libvlc_MediaPlayerForward),
  234.     EVENT(libvlc_MediaPlayerBackward),
  235.     EVENT(libvlc_MediaPlayerEndReached),
  236.     EVENT(libvlc_MediaPlayerTimeChanged),
  237.     EVENT(libvlc_MediaPlayerTitleChanged),
  238.     EVENT(libvlc_MediaPlayerPositionChanged),
  239.     EVENT(libvlc_MediaPlayerSeekableChanged),
  240.     EVENT(libvlc_MediaPlayerPausableChanged),
  241.     EVENT(libvlc_MediaListItemAdded),
  242.     EVENT(libvlc_MediaListWillAddItem),
  243.     EVENT(libvlc_MediaListItemDeleted),
  244.     EVENT(libvlc_MediaListWillDeleteItem),
  245.     EVENT(libvlc_MediaListViewItemAdded),
  246.     EVENT(libvlc_MediaListViewWillAddItem),
  247.     EVENT(libvlc_MediaListViewItemDeleted),
  248.     EVENT(libvlc_MediaListViewWillDeleteItem),
  249.     EVENT(libvlc_MediaListPlayerPlayed),
  250.     EVENT(libvlc_MediaListPlayerNextItemSet),
  251.     EVENT(libvlc_MediaListPlayerStopped),
  252.     EVENT(libvlc_MediaDiscovererStarted),
  253.     EVENT(libvlc_MediaDiscovererEnded),
  254.     EVENT(libvlc_MediaPlayerSnapshotTaken),
  255. #undef EVENT
  256. };
  257. static const char unkwown_event_name[] = "Unknown Event";
  258. const char * libvlc_event_type_name( libvlc_event_type_t event_type )
  259. {
  260.     if( event_type >= sizeof(event_type_to_name)/sizeof(event_type_to_name[0]))
  261.         return unkwown_event_name;
  262.     return event_type_to_name[event_type];
  263. }
  264. /**************************************************************************
  265.  *       event_attach (internal) :
  266.  *
  267.  * Add a callback for an event.
  268.  **************************************************************************/
  269. static
  270. void event_attach( libvlc_event_manager_t * p_event_manager,
  271.                          libvlc_event_type_t event_type,
  272.                          libvlc_callback_t pf_callback,
  273.                          void *p_user_data,
  274.                          bool is_asynchronous,
  275.                          libvlc_exception_t *p_e )
  276. {
  277.     libvlc_event_listeners_group_t * listeners_group;
  278.     libvlc_event_listener_t * listener;
  279.     int i;
  280.     
  281.     listener = malloc(sizeof(libvlc_event_listener_t));
  282.     if( !listener )
  283.     {
  284.         libvlc_exception_raise( p_e, "No Memory left" );
  285.         return;
  286.     }
  287.     
  288.     listener->event_type = event_type;
  289.     listener->p_user_data = p_user_data;
  290.     listener->pf_callback = pf_callback;
  291.     listener->is_asynchronous = is_asynchronous;
  292.     
  293.     vlc_mutex_lock( &p_event_manager->object_lock );
  294.     for( i = 0; i < vlc_array_count(&p_event_manager->listeners_groups); i++ )
  295.     {
  296.         listeners_group = vlc_array_item_at_index(&p_event_manager->listeners_groups, i);
  297.         if( listeners_group->event_type == listener->event_type )
  298.         {
  299.             vlc_array_append( &listeners_group->listeners, listener );
  300.             vlc_mutex_unlock( &p_event_manager->object_lock );
  301.             return;
  302.         }
  303.     }
  304.     vlc_mutex_unlock( &p_event_manager->object_lock );
  305.     
  306.     free(listener);
  307.     libvlc_exception_raise( p_e,
  308.                            "This object event manager doesn't know about '%s' events",
  309.                            libvlc_event_type_name(event_type));
  310. }
  311. /**************************************************************************
  312.  *       libvlc_event_attach (public) :
  313.  *
  314.  * Add a callback for an event.
  315.  **************************************************************************/
  316. void libvlc_event_attach( libvlc_event_manager_t * p_event_manager,
  317.                          libvlc_event_type_t event_type,
  318.                          libvlc_callback_t pf_callback,
  319.                          void *p_user_data,
  320.                          libvlc_exception_t *p_e )
  321. {
  322.     event_attach(p_event_manager, event_type, pf_callback, p_user_data, false /* synchronous */, p_e);
  323. }
  324. /**************************************************************************
  325.  *       libvlc_event_attach (public) :
  326.  *
  327.  * Add a callback for an event.
  328.  **************************************************************************/
  329. void libvlc_event_attach_async( libvlc_event_manager_t * p_event_manager,
  330.                          libvlc_event_type_t event_type,
  331.                          libvlc_callback_t pf_callback,
  332.                          void *p_user_data,
  333.                          libvlc_exception_t *p_e )
  334. {
  335.     event_attach(p_event_manager, event_type, pf_callback, p_user_data, true /* asynchronous */, p_e);
  336. }
  337. /**************************************************************************
  338.  *       libvlc_event_detach (public) :
  339.  *
  340.  * Remove a callback for an event.
  341.  **************************************************************************/
  342. void libvlc_event_detach( libvlc_event_manager_t *p_event_manager,
  343.                                      libvlc_event_type_t event_type,
  344.                                      libvlc_callback_t pf_callback,
  345.                                      void *p_user_data,
  346.                                      libvlc_exception_t *p_e )
  347. {
  348.     libvlc_event_listeners_group_t * listeners_group;
  349.     libvlc_event_listener_t * listener;
  350.     int i, j;
  351.     bool found = false;
  352.     
  353.     vlc_mutex_lock( &p_event_manager->event_sending_lock );
  354.     vlc_mutex_lock( &p_event_manager->object_lock );
  355.     for( i = 0; i < vlc_array_count(&p_event_manager->listeners_groups); i++)
  356.     {
  357.         listeners_group = vlc_array_item_at_index(&p_event_manager->listeners_groups, i);
  358.         if( listeners_group->event_type == event_type )
  359.         {
  360.             for( j = 0; j < vlc_array_count(&listeners_group->listeners); j++)
  361.             {
  362.                 listener = vlc_array_item_at_index(&listeners_group->listeners, j);
  363.                 if( listener->event_type == event_type &&
  364.                     listener->pf_callback == pf_callback &&
  365.                     listener->p_user_data == p_user_data )
  366.                 {
  367.                     /* that's our listener */
  368.                     /* Mark this group as edited so that libvlc_event_send
  369.                      * will recheck what listener to call */
  370.                     listeners_group->b_sublistener_removed = false;
  371.                     free( listener );
  372.                     vlc_array_remove( &listeners_group->listeners, j );
  373.                     found = true;
  374.                     break;
  375.                 }
  376.             }
  377.         }
  378.     }
  379.     vlc_mutex_unlock( &p_event_manager->object_lock );
  380.     vlc_mutex_unlock( &p_event_manager->event_sending_lock );
  381.     /* Now make sure any pending async event won't get fired after that point */
  382.     libvlc_event_listener_t listener_to_remove;
  383.     listener_to_remove.event_type  = event_type;
  384.     listener_to_remove.pf_callback = pf_callback;
  385.     listener_to_remove.p_user_data = p_user_data;
  386.     listener_to_remove.is_asynchronous = true;
  387.     libvlc_event_async_ensure_listener_removal(p_event_manager, &listener_to_remove);
  388.     if(!found)
  389.     {
  390.         libvlc_exception_raise( p_e,
  391.                                "This object event manager doesn't know about '%s,%p,%p' event observer",
  392.                                libvlc_event_type_name(event_type), pf_callback, p_user_data );        
  393.     }
  394. }