[vlc-commits] addons: simplify events handling

Rémi Denis-Courmont git at videolan.org
Sun May 14 21:29:30 CEST 2017


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun May 14 22:26:05 2017 +0300| [545f5b8a28085490ccfda75bb304d02daee55ed1] | committer: Rémi Denis-Courmont

addons: simplify events handling

Use fixed owner structure instead of deprecated VLC events.

Notes:
 - Outstanding memory leaks are not fixed.
 - Reference counting in MacOS UI is not obvious if it exists (??).

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=545f5b8a28085490ccfda75bb304d02daee55ed1
---

 include/vlc_addons.h                           | 17 +++++++--
 include/vlc_events.h                           | 11 ------
 modules/gui/macosx/VLCAddonsWindowController.m | 47 +++++++++++++++--------
 modules/gui/qt/managers/addons_manager.cpp     | 52 ++++++++++++++------------
 modules/gui/qt/managers/addons_manager.hpp     |  4 +-
 src/misc/addons.c                              | 44 ++++------------------
 6 files changed, 84 insertions(+), 91 deletions(-)

diff --git a/include/vlc_addons.h b/include/vlc_addons.h
index 92d5bc6e12..23cb134968 100644
--- a/include/vlc_addons.h
+++ b/include/vlc_addons.h
@@ -129,14 +129,22 @@ struct addons_storage_t
     addons_storage_sys_t *p_sys;
 };
 
+typedef struct addons_manager_t addons_manager_t;
+
+struct addons_manager_owner
+{
+    void *sys;
+    void (*addon_found)(struct addons_manager_t *, struct addon_entry_t *);
+    void (*discovery_ended)(struct addons_manager_t *);
+    void (*addon_changed)(struct addons_manager_t *, struct addon_entry_t *);
+};
+
 typedef struct addons_manager_private_t addons_manager_private_t;
 struct addons_manager_t
 {
-    vlc_event_manager_t * p_event_manager;
-
+    struct addons_manager_owner owner;
     addons_manager_private_t *p_priv;
 };
-typedef struct addons_manager_t addons_manager_t;
 
 /**
  *  addon entry lifecycle
@@ -148,7 +156,8 @@ VLC_API void addon_entry_Release(addon_entry_t *);
 /**
  * addons manager lifecycle
  */
-VLC_API addons_manager_t *addons_manager_New( vlc_object_t * );
+VLC_API addons_manager_t *addons_manager_New( vlc_object_t *,
+    const struct addons_manager_owner * );
 VLC_API void addons_manager_Delete( addons_manager_t * );
 
 /**
diff --git a/include/vlc_events.h b/include/vlc_events.h
index 82245cbbd0..aef8301cdc 100644
--- a/include/vlc_events.h
+++ b/include/vlc_events.h
@@ -124,11 +124,6 @@ typedef enum vlc_event_type_t {
     /* Renderer Discovery events */
     vlc_RendererDiscoveryItemAdded=vlc_InputItemPreparseEnded+6,
     vlc_RendererDiscoveryItemRemoved,
-
-    /* Addons Manager events */
-    vlc_AddonFound,
-    vlc_AddonsDiscoveryEnded,
-    vlc_AddonChanged
 } vlc_event_type_t;
 
 /* Event definition */
@@ -185,12 +180,6 @@ typedef struct vlc_event_t
         {
             vlc_renderer_item_t * p_item;
         } renderer_discovery_item_removed;
-
-        /* Addons */
-        struct vlc_addon_generic_event
-        {
-            addon_entry_t * p_entry;
-        } addon_generic_event;
     } u;
 } vlc_event_t;
 
diff --git a/modules/gui/macosx/VLCAddonsWindowController.m b/modules/gui/macosx/VLCAddonsWindowController.m
index a82e521ebf..b6b38ab045 100644
--- a/modules/gui/macosx/VLCAddonsWindowController.m
+++ b/modules/gui/macosx/VLCAddonsWindowController.m
@@ -20,7 +20,6 @@
  *****************************************************************************/
 
 #import <vlc_common.h>
-#import <vlc_events.h>
 #import <vlc_addons.h>
 
 #import "VLCAddonsWindowController.h"
@@ -42,17 +41,32 @@
 
 @end
 
-static void addonsEventsCallback( const vlc_event_t *event, void *data )
+static void addonFoundCallback( addons_manager_t *manager,
+                                addon_entry_t *entry )
 {
-    VLCAddonsWindowController *controller = (__bridge VLCAddonsWindowController *)data;
+    VLCAddonsWindowController *controller = (__bridge VLCAddonsWindowController *) manager->owner.sys;
 
     @autoreleasepool {
-        if (event->type == vlc_AddonFound)
-            [controller performSelectorOnMainThread:@selector(addAddon:) withObject:[NSValue valueWithPointer:event->u.addon_generic_event.p_entry] waitUntilDone:NO];
-        else if (event->type == vlc_AddonsDiscoveryEnded)
-            [controller performSelectorOnMainThread:@selector(discoveryEnded) withObject:nil waitUntilDone:NO];
-        else if (event->type == vlc_AddonChanged)
-            [controller performSelectorOnMainThread:@selector(addonChanged:) withObject:[NSValue valueWithPointer:event->u.addon_generic_event.p_entry] waitUntilDone:NO];
+        [controller performSelectorOnMainThread:@selector(addAddon:) withObject:[NSValue valueWithPointer:entry] waitUntilDone:NO];
+    }
+}
+
+static void addonsDiscoveryEndedCallback( addons_manager_t *manager )
+{
+    VLCAddonsWindowController *controller = (__bridge VLCAddonsWindowController *) manager->owner.sys;
+
+    @autoreleasepool {
+        [controller performSelectorOnMainThread:@selector(discoveryEnded) withObject:nil waitUntilDone:NO];
+    }
+}
+
+static void addonsChangedCallback( addons_manager_t *manager,
+                                   addon_entry_t *entry )
+{
+    VLCAddonsWindowController *controller = (__bridge VLCAddonsWindowController *) manager->owner.sys;
+
+    @autoreleasepool {
+        [controller performSelectorOnMainThread:@selector(addonChanged:) withObject:[NSValue valueWithPointer:entry] waitUntilDone:NO];
     }
 }
 
@@ -117,15 +131,18 @@ static void addonsEventsCallback( const vlc_event_t *event, void *data )
     [[[_addonsTable tableColumnWithIdentifier:@"author"] headerCell] setStringValue:_NS("Author")];
     [[[_addonsTable tableColumnWithIdentifier:@"type"] headerCell] setStringValue:_NS("Type")];
 
-    _manager = addons_manager_New((vlc_object_t *)getIntf());
+    struct addons_manager_owner owner =
+    {
+        (__bridge void *)self,
+        addonFoundCallback,
+        addonsDiscoveryEndedCallback,
+        addonChangedCallback,
+    };
+
+    _manager = addons_manager_New((vlc_object_t *)getIntf(), &owner);
     if (!_manager)
         return;
 
-    vlc_event_manager_t *p_em = _manager->p_event_manager;
-    vlc_event_attach(p_em, vlc_AddonFound, addonsEventsCallback, (__bridge void *)self);
-    vlc_event_attach(p_em, vlc_AddonsDiscoveryEnded, addonsEventsCallback, (__bridge void *)self);
-    vlc_event_attach(p_em, vlc_AddonChanged, addonsEventsCallback, (__bridge void *)self);
-
     [self _findInstalled];
 }
 
diff --git a/modules/gui/qt/managers/addons_manager.cpp b/modules/gui/qt/managers/addons_manager.cpp
index 6309ca319d..d4fc655d16 100644
--- a/modules/gui/qt/managers/addons_manager.cpp
+++ b/modules/gui/qt/managers/addons_manager.cpp
@@ -30,13 +30,15 @@ const QEvent::Type AddonManagerEvent::DiscoveryEndedEvent =
 
 AddonsManager::AddonsManager( intf_thread_t *p_intf )
 {
-    p_manager = addons_manager_New( VLC_OBJECT(p_intf) );
-    if ( !p_manager ) return;
+    struct addons_manager_owner owner =
+    {
+        this,
+        addonFoundCallback,
+        addonsDiscoveryEndedCallback,
+        addonChangedCallback,
+    };
 
-    vlc_event_manager_t *p_em = p_manager->p_event_manager;
-    vlc_event_attach( p_em, vlc_AddonFound, addonsEventsCallback, this );
-    vlc_event_attach( p_em, vlc_AddonsDiscoveryEnded, addonsEventsCallback, this );
-    vlc_event_attach( p_em, vlc_AddonChanged, addonsEventsCallback, this );
+    p_manager = addons_manager_New( VLC_OBJECT(p_intf), &owner );
 }
 
 AddonsManager::~AddonsManager()
@@ -97,27 +99,29 @@ QString AddonsManager::getAddonType( int i_type )
     }
 }
 
-void AddonsManager::addonsEventsCallback( const vlc_event_t *event, void *data )
+void AddonsManager::addonFoundCallback( addons_manager_t *manager,
+                                        addon_entry_t *entry )
 {
-    AddonsManager *me = ( AddonsManager * ) data;
-    QEvent *ev = NULL;
+    AddonsManager *me = (AddonsManager *) manager->owner.sys;
+    QEvent *ev = new AddonManagerEvent( AddonManagerEvent::AddedEvent,
+                                        entry );
+    QApplication::postEvent( me, ev );
+}
 
-    if ( event->type == vlc_AddonFound )
-    {
-        ev = new AddonManagerEvent( AddonManagerEvent::AddedEvent,
-                                    event->u.addon_generic_event.p_entry );
-    }
-    else if ( event->type == vlc_AddonsDiscoveryEnded )
-    {
-        ev = new QEvent( AddonManagerEvent::DiscoveryEndedEvent );
-    }
-    else if ( event->type == vlc_AddonChanged )
-    {
-        ev = new AddonManagerEvent( AddonManagerEvent::ChangedEvent,
-                                    event->u.addon_generic_event.p_entry );
-    }
+void AddonsManager::addonsDiscoveryEndedCallback( addons_manager_t *manager )
+{
+    AddonsManager *me = (AddonsManager *) manager->owner.sys;
+    QEvent *ev = new QEvent( AddonManagerEvent::DiscoveryEndedEvent );
+    QApplication::postEvent( me, ev );
+}
 
-    if ( ev ) QApplication::postEvent( me, ev );
+void AddonsManager::addonChangedCallback( addons_manager_t *manager,
+                                          addon_entry_t *entry )
+{
+    AddonsManager *me = (AddonsManager *) manager->owner.sys;
+    QEvent *ev = new AddonManagerEvent( AddonManagerEvent::ChangedEvent,
+                                        entry );
+    QApplication::postEvent( me, ev );
 }
 
 void AddonsManager::customEvent( QEvent *event )
diff --git a/modules/gui/qt/managers/addons_manager.hpp b/modules/gui/qt/managers/addons_manager.hpp
index 6a221fdc7d..9ef3c6e035 100644
--- a/modules/gui/qt/managers/addons_manager.hpp
+++ b/modules/gui/qt/managers/addons_manager.hpp
@@ -66,7 +66,9 @@ class AddonsManager : public QObject, public Singleton<AddonsManager>
 public:
     AddonsManager( intf_thread_t * );
     virtual ~AddonsManager();
-    static void addonsEventsCallback( const vlc_event_t *, void * );
+    static void addonFoundCallback( addons_manager_t *, addon_entry_t * );
+    static void addonsDiscoveryEndedCallback( addons_manager_t * );
+    static void addonChangedCallback( addons_manager_t *, addon_entry_t * );
     void customEvent( QEvent * );
     void install( QByteArray id );
     void remove( QByteArray id );
diff --git a/src/misc/addons.c b/src/misc/addons.c
index c03505aecc..641ebd3b4a 100644
--- a/src/misc/addons.c
+++ b/src/misc/addons.c
@@ -26,7 +26,6 @@
 #include <vlc_atomic.h>
 #include <vlc_modules.h>
 #include <vlc_arrays.h>
-#include <vlc_events.h>
 #include "libvlc.h"
 
 #include <vlc_addons.h>
@@ -124,7 +123,8 @@ void addon_entry_Release( addon_entry_t * p_entry )
     free( owner );
 }
 
-addons_manager_t *addons_manager_New( vlc_object_t *p_this )
+addons_manager_t *addons_manager_New( vlc_object_t *p_this,
+    const struct addons_manager_owner *restrict owner )
 {
     addons_manager_t *p_manager = malloc( sizeof(addons_manager_t) );
     if ( !p_manager ) return NULL;
@@ -136,14 +136,7 @@ addons_manager_t *addons_manager_New( vlc_object_t *p_this )
         return NULL;
     }
 
-    p_manager->p_event_manager = malloc( sizeof(vlc_event_manager_t) );
-    if ( !p_manager->p_event_manager )
-    {
-        free( p_manager->p_priv );
-        free( p_manager );
-        return NULL;
-    }
-
+    p_manager->owner = *owner;
     p_manager->p_priv->p_parent = p_this;
 
 #define INIT_QUEUE( name ) \
@@ -156,12 +149,6 @@ addons_manager_t *addons_manager_New( vlc_object_t *p_this )
     INIT_QUEUE( installer )
     ARRAY_INIT( p_manager->p_priv->finder.uris );
 
-    vlc_event_manager_t *em = p_manager->p_event_manager;
-    vlc_event_manager_init( em, p_manager );
-    vlc_event_manager_register_event_type(em, vlc_AddonFound);
-    vlc_event_manager_register_event_type(em, vlc_AddonsDiscoveryEnded);
-    vlc_event_manager_register_event_type(em, vlc_AddonChanged);
-
     return p_manager;
 }
 
@@ -187,8 +174,6 @@ void addons_manager_Delete( addons_manager_t *p_manager )
         vlc_join( p_manager->p_priv->installer.thread, NULL );
     }
 
-    vlc_event_manager_fini( p_manager->p_event_manager );
-
 #define FREE_QUEUE( name ) \
     FOREACH_ARRAY( addon_entry_t *p_entry, p_manager->p_priv->name.entries )\
         addon_entry_Release( p_entry );\
@@ -205,7 +190,6 @@ void addons_manager_Delete( addons_manager_t *p_manager )
     ARRAY_RESET( p_manager->p_priv->finder.uris );
 
     free( p_manager->p_priv );
-    free( p_manager->p_event_manager );
     free( p_manager );
 }
 
@@ -273,10 +257,7 @@ static void MergeSources( addons_manager_t *p_manager,
         if ( !p_manager_entry )
         {
             ARRAY_APPEND( p_manager->p_priv->finder.entries, p_entry );
-            vlc_event_t event;
-            event.type = vlc_AddonFound;
-            event.u.addon_generic_event.p_entry = p_entry;
-            vlc_event_send( p_manager->p_event_manager, &event );
+            p_manager->owner.addon_found( p_manager, p_entry );
         }
         else
         {
@@ -361,11 +342,7 @@ static void *FinderThread( void *p_data )
             vlc_object_release( p_finder );
         }
 
-        vlc_event_t event;
-        event.type = vlc_AddonsDiscoveryEnded;
-        event.u.addon_generic_event.p_entry = NULL;
-        vlc_event_send( p_manager->p_event_manager, &event );
-
+        p_manager->owner.discovery_ended( p_manager );
         vlc_restorecancel( i_cancel );
         vlc_testcancel();
     }
@@ -437,8 +414,6 @@ static void *InstallerThread( void *p_data )
 {
     addons_manager_t *p_manager = p_data;
     int i_ret;
-    vlc_event_t event;
-    event.type = vlc_AddonChanged;
 
     for( ;; )
     {
@@ -466,8 +441,7 @@ static void *InstallerThread( void *p_data )
             vlc_mutex_unlock( &p_entry->lock );
 
             /* notify */
-            event.u.addon_generic_event.p_entry = p_entry;
-            vlc_event_send( p_manager->p_event_manager, &event );
+            p_manager->owner.addon_changed( p_manager, p_entry );
 
             i_ret = installOrRemoveAddon( p_manager, p_entry, false );
 
@@ -481,8 +455,7 @@ static void *InstallerThread( void *p_data )
             vlc_mutex_unlock( &p_entry->lock );
 
             /* notify */
-            event.u.addon_generic_event.p_entry = p_entry;
-            vlc_event_send( p_manager->p_event_manager, &event );
+            p_manager->owner.addon_changed( p_manager, p_entry );
 
             i_ret = installOrRemoveAddon( p_manager, p_entry, true );
 
@@ -493,8 +466,7 @@ static void *InstallerThread( void *p_data )
         vlc_mutex_unlock( &p_entry->lock );
         /* !DO WORK */
 
-        event.u.addon_generic_event.p_entry = p_entry;
-        vlc_event_send( p_manager->p_event_manager, &event );
+        p_manager->owner.addon_changed( p_manager, p_entry );
 
         addon_entry_Release( p_entry );
 



More information about the vlc-commits mailing list