[vlc-commits] Simplify events classes

Jean-Baptiste Kempf git at videolan.org
Wed Apr 23 17:13:35 CEST 2014


npapi-vlc | branch: master | Jean-Baptiste Kempf <jb at videolan.org> | Wed Apr 23 17:12:34 2014 +0200| [728a715bc2e53a7c8a39bc80f5c8b364be786c78] | committer: Jean-Baptiste Kempf

Simplify events classes

> http://git.videolan.org/gitweb.cgi/npapi-vlc.git/?a=commit;h=728a715bc2e53a7c8a39bc80f5c8b364be786c78
---

 npapi/events.cpp         |   61 ++++++++++++++++++++--------------------------
 npapi/events.h           |   35 ++++++++++++--------------
 npapi/vlcplugin_base.cpp |    3 ---
 3 files changed, 41 insertions(+), 58 deletions(-)

diff --git a/npapi/events.cpp b/npapi/events.cpp
index 1bd5f44..7476406 100644
--- a/npapi/events.cpp
+++ b/npapi/events.cpp
@@ -110,10 +110,9 @@ static vlcplugin_event_t vlcevents[] = {
     { "MediaPlayerLengthChanged", libvlc_MediaPlayerLengthChanged, handle_changed_event },
 };
 
-bool EventObj::init()
+EventObj::EventObj() : _em(NULL), _already_in_deliver(false)
 {
     plugin_lock_init(&lock);
-    return true;
 }
 
 EventObj::~EventObj()
@@ -131,14 +130,14 @@ void EventObj::deliver(NPP browser)
 
     for( ev_l::iterator iter = _elist.begin(); iter != _elist.end(); ++iter )
     {
+        NPVariant *params = iter->params();
+        uint32_t   count  = iter->count();
+
         for( lr_l::iterator j = _llist.begin(); j != _llist.end(); ++j )
         {
             if( j->event_type() == iter->event_type() )
             {
                 NPVariant result;
-                NPVariant *params = iter->params();
-                uint32_t   count  = iter->count();
-
                 NPObject *listener = j->listener();
                 assert( listener );
 
@@ -148,7 +147,9 @@ void EventObj::deliver(NPP browser)
                 for( uint32_t n = 0; n < count; n++ )
                 {
                     if( NPVARIANT_IS_STRING(params[n]) )
+                    {
                         NPN_MemFree( (void*) NPVARIANT_TO_STRING(params[n]).UTF8Characters );
+                    }
                     else if( NPVARIANT_IS_OBJECT(params[n]) )
                     {
                         NPN_ReleaseObject( NPVARIANT_TO_OBJECT(params[n]) );
@@ -173,29 +174,19 @@ void EventObj::callback(const libvlc_event_t* event,
     plugin_unlock(&lock);
 }
 
-vlcplugin_event_t *EventObj::find_event(const char *s) const
+vlcplugin_event_t *EventObj::find_event(const NPString &name) const
 {
     for( size_t i = 0; i < ARRAY_SIZE(vlcevents); i++ )
     {
-        if( strncmp(vlcevents[i].name, s, strlen(vlcevents[i].name)) == 0 )
+        if( strncmp(vlcevents[i].name, name.UTF8Characters, strlen(vlcevents[i].name)) == 0 )
             return &vlcevents[i];
     }
     return NULL;
 }
 
-const char *EventObj::find_name(const libvlc_event_t *event)
-{
-    for( size_t i = 0; i < ARRAY_SIZE(vlcevents); i++ )
-    {
-        if( vlcevents[i].libvlc_type == event->type )
-            return vlcevents[i].name;
-    }
-    return NULL;
-}
-
 bool EventObj::insert(const NPString &name, NPObject *listener, bool bubble)
 {
-    vlcplugin_event_t *event = find_event(name.UTF8Characters);
+    vlcplugin_event_t *event = find_event(name);
     if( !event )
         return false;
 
@@ -215,7 +206,7 @@ bool EventObj::insert(const NPString &name, NPObject *listener, bool bubble)
 
 bool EventObj::remove(const NPString &name, NPObject *listener, bool bubble)
 {
-    vlcplugin_event_t *event = find_event(name.UTF8Characters);
+    vlcplugin_event_t *event = find_event(name);
     if( !event )
         return false;
 
@@ -235,31 +226,31 @@ bool EventObj::remove(const NPString &name, NPObject *listener, bool bubble)
 
 void EventObj::hook_manager( libvlc_event_manager_t *em, void *userdata )
 {
+    if( !_em )
+        return;
+
     _em = em;
 
-    if( _em )
+    /* attach all libvlc events we need */
+    for( size_t i = 0; i < ARRAY_SIZE(vlcevents); i++ )
     {
-        /* attach all libvlc events we care about */
-        for( size_t i = 0; i < ARRAY_SIZE(vlcevents); i++ )
-        {
-            libvlc_event_attach( _em, vlcevents[i].libvlc_type,
-                                      vlcevents[i].libvlc_callback,
-                                      userdata );
-        }
+        libvlc_event_attach( _em, vlcevents[i].libvlc_type,
+                vlcevents[i].libvlc_callback,
+                userdata );
     }
 }
 
 void EventObj::unhook_manager( void *userdata )
 {
-    if( _em )
+    if( !_em )
+        return;
+
+    /* detach all libvlc events */
+    for( size_t i = 0; i < ARRAY_SIZE(vlcevents); i++ )
     {
-        /* detach all libvlc events we cared about */
-        for( size_t i = 0; i < ARRAY_SIZE(vlcevents); i++ )
-        {
-            libvlc_event_detach( _em, vlcevents[i].libvlc_type,
-                                      vlcevents[i].libvlc_callback,
-                                      userdata );
-        }
+        libvlc_event_detach( _em, vlcevents[i].libvlc_type,
+                vlcevents[i].libvlc_callback,
+                userdata );
     }
 }
 
diff --git a/npapi/events.h b/npapi/events.h
index 75c1897..d36b1ff 100644
--- a/npapi/events.h
+++ b/npapi/events.h
@@ -49,14 +49,11 @@ private:
     public:
         Listener(vlcplugin_event_t *event, NPObject *p_object, bool b_bubble):
             _event(event), _listener(p_object), _bubble(b_bubble)
-            {
+        {
                 assert(event);
                 assert(p_object);
-            }
-        Listener(): _event(NULL), _listener(NULL), _bubble(false) { }
-        ~Listener()
-            {
-            }
+        }
+
         libvlc_event_type_t event_type() const { return _event->libvlc_type; }
         NPObject *listener() const { return _listener; }
         bool bubble() const { return _bubble; }
@@ -71,12 +68,8 @@ private:
     public:
         VLCEvent(libvlc_event_type_t libvlc_event_type, NPVariant *npparams, uint32_t npcount):
             _libvlc_event_type(libvlc_event_type), _npparams(npparams), _npcount(npcount)
-            {
-            }
-        VLCEvent(): _libvlc_event_type(0), _npparams(NULL), _npcount(0) { }
-        ~VLCEvent()
-            {
-            }
+         {}
+
         libvlc_event_type_t event_type() const { return _libvlc_event_type; }
         NPVariant *params() const { return _npparams; }
         uint32_t count() const { return _npcount; }
@@ -85,21 +78,23 @@ private:
         NPVariant *_npparams;
         uint32_t _npcount;
     };
-    libvlc_event_manager_t *_em; /* libvlc media_player event manager */
+
 public:
-    EventObj(): _em(NULL), _already_in_deliver(false) { /* deferred to init() */ }
-    bool init();
-    ~EventObj();
+    EventObj();
+    virtual ~EventObj();
+
+    void unhook_manager(void *);
+    void hook_manager(libvlc_event_manager_t *, void *);
 
     void deliver(NPP browser);
     void callback(const libvlc_event_t *event, NPVariant *npparams, uint32_t count);
     bool insert(const NPString &name, NPObject *listener, bool bubble);
     bool remove(const NPString &name, NPObject *listener, bool bubble);
-    void unhook_manager(void *);
-    void hook_manager(libvlc_event_manager_t *, void *);
+
 private:
-    vlcplugin_event_t *find_event(const char *s) const;
-    const char *find_name(const libvlc_event_t *event);
+    libvlc_event_manager_t *_em; /* libvlc media_player event manager */
+    vlcplugin_event_t *find_event(const NPString &name) const;
+
     typedef std::vector<Listener> lr_l;
     typedef std::vector<VLCEvent> ev_l;
     lr_l _llist; /* list of registered listeners with 'addEventListener' method */
diff --git a/npapi/vlcplugin_base.cpp b/npapi/vlcplugin_base.cpp
index 0ec2225..de8b473 100644
--- a/npapi/vlcplugin_base.cpp
+++ b/npapi/vlcplugin_base.cpp
@@ -255,9 +255,6 @@ NPError VlcPluginBase::init(int argc, char* const argn[], char* const argv[])
     /* new APIs */
     p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
 
-    if( !events.init() )
-        return NPERR_GENERIC_ERROR;
-
     libvlc_media_player_t *p_md = getMD();
     if( p_md ) {
       libvlc_event_manager_t *p_em;



More information about the vlc-commits mailing list