[vlc-commits] Qt: add deduplication for some events.

Francois Cartegnie git at videolan.org
Thu Aug 16 15:30:02 CEST 2012


vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Thu Aug 16 15:06:25 2012 +0200| [541c1d97c0087c5de559b9793f42ea698f8c284e] | committer: Francois Cartegnie

Qt: add deduplication for some events.

Some events are fired on each meta type detection, while we just handle
those events on a global change basis.
Qt has some similar deduplication for repaint() or touch events, but
does not provide it to user.

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

 modules/gui/qt4/Modules.am           |    3 ++
 modules/gui/qt4/input_manager.cpp    |   18 ++++++++--
 modules/gui/qt4/input_manager.hpp    |   17 +++++++--
 modules/gui/qt4/util/uniqueevent.cpp |   65 ++++++++++++++++++++++++++++++++++
 modules/gui/qt4/util/uniqueevent.hpp |   55 ++++++++++++++++++++++++++++
 5 files changed, 153 insertions(+), 5 deletions(-)

diff --git a/modules/gui/qt4/Modules.am b/modules/gui/qt4/Modules.am
index 334c534..b88055b 100644
--- a/modules/gui/qt4/Modules.am
+++ b/modules/gui/qt4/Modules.am
@@ -73,6 +73,7 @@ nodist_SOURCES_qt4 = \
 		util/qmenuview.moc.cpp \
 		util/qvlcapp.moc.cpp \
 		util/pictureflow.moc.cpp \
+		util/uniqueevent.moc.cpp \
 		util/buttons/RoundButton.moc.cpp \
 		util/buttons/DeckButtonsLayout.moc.cpp \
 		util/buttons/BrowseButton.moc.cpp \
@@ -312,6 +313,7 @@ SOURCES_qt4 = 	qt4.cpp \
 		util/qmenuview.cpp \
 		util/qt_dirs.cpp \
 		util/pictureflow.cpp \
+		util/uniqueevent.cpp \
 		util/buttons/BrowseButton.cpp \
 		util/buttons/DeckButtonsLayout.cpp \
 		util/buttons/RoundButton.cpp \
@@ -391,6 +393,7 @@ noinst_HEADERS = \
 	util/qt_dirs.hpp \
 	util/registry.hpp \
 	util/pictureflow.hpp \
+	util/uniqueevent.hpp \
 	util/singleton.hpp \
 	util/buttons/RoundButton.hpp \
 	util/buttons/DeckButtonsLayout.hpp \
diff --git a/modules/gui/qt4/input_manager.cpp b/modules/gui/qt4/input_manager.cpp
index 6de8c16..e87eb1c 100644
--- a/modules/gui/qt4/input_manager.cpp
+++ b/modules/gui/qt4/input_manager.cpp
@@ -79,10 +79,12 @@ InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
     timeA        = 0;
     timeB        = 0;
     f_cache      = -1.; /* impossible initial value, different from all */
+    rateLimitedEventPoster = new RateLimitedEventPoster();
 }
 
 InputManager::~InputManager()
 {
+    delete rateLimitedEventPoster;
     delInput();
 }
 
@@ -167,6 +169,11 @@ void InputManager::delInput()
     emit cachingChanged( 1 );
 }
 
+void InputManager::postUniqueEvent( QObject *target, UniqueEvent *e )
+{
+    rateLimitedEventPoster->postEvent( e, target );
+}
+
 /* Convert the event from the callbacks in actions */
 void InputManager::customEvent( QEvent *event )
 {
@@ -282,7 +289,7 @@ static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
     input_item_t *p_item = static_cast<input_item_t *>(newval.p_address);
 
     IMEvent *event = new IMEvent( ItemChanged_Type, p_item );
-    QApplication::postEvent( im, event );
+    im->postUniqueEvent( im, event );
     return VLC_SUCCESS;
 }
 
@@ -293,6 +300,7 @@ static int InputEvent( vlc_object_t *p_this, const char *,
 
     InputManager *im = (InputManager*)param;
     IMEvent *event;
+    bool b_unified = false;
 
     switch( newval.i_int )
     {
@@ -331,6 +339,7 @@ static int InputEvent( vlc_object_t *p_this, const char *,
         break;
 
     case INPUT_EVENT_ITEM_META: /* Codec MetaData + Art */
+        b_unified = true;
         event = new IMEvent( MetaChanged_Type );
         break;
     case INPUT_EVENT_ITEM_INFO: /* Codec Info */
@@ -377,7 +386,12 @@ static int InputEvent( vlc_object_t *p_this, const char *,
     }
 
     if( event )
-        QApplication::postEvent( im, event );
+    {
+        if ( b_unified )
+            im->postUniqueEvent( im, event );
+        else
+            QApplication::postEvent( im, event );
+    }
     return VLC_SUCCESS;
 }
 
diff --git a/modules/gui/qt4/input_manager.hpp b/modules/gui/qt4/input_manager.hpp
index f234d48..b2d63cf 100644
--- a/modules/gui/qt4/input_manager.hpp
+++ b/modules/gui/qt4/input_manager.hpp
@@ -33,6 +33,7 @@
 
 #include "qt4.hpp"
 #include "util/singleton.hpp"
+#include "util/uniqueevent.hpp"
 #include "variables.hpp"
 
 #include <QObject>
@@ -74,13 +75,13 @@ enum { NORMAL,    /* loop: 0, repeat: 0 */
        REPEAT_ALL,/* loop: 1, repeat: 0 */
 };
 
-class IMEvent : public QEvent
+class IMEvent : public UniqueEvent
 {
 friend class InputManager;
 friend class MainInputManager;
     public:
     IMEvent( int type, input_item_t *p_input = NULL )
-        : QEvent( (QEvent::Type)(type) )
+        : UniqueEvent( (QEvent::Type)(type) )
     {
         if( (p_item = p_input) != NULL )
             vlc_gc_incref( p_item );
@@ -90,7 +91,15 @@ friend class MainInputManager;
         if( p_item )
             vlc_gc_decref( p_item );
     }
-
+    bool itemEquals( input_item_t *p_item_ ) const
+    {
+        return p_item_ == p_item;
+    };
+    virtual bool equals(UniqueEvent *e) const
+    {
+        IMEvent *ev = static_cast<IMEvent *>(e);
+        return ( ev->itemEquals( p_item ) && ev->type() == type() );
+    }
 private:
     input_item_t *p_item;
 };
@@ -141,6 +150,7 @@ public:
 
     QString getName() { return oldName; }
     static const QString decodeArtURL( input_item_t *p_item );
+    void postUniqueEvent( QObject *, UniqueEvent * );
 
 private:
     intf_thread_t  *p_intf;
@@ -156,6 +166,7 @@ private:
     mtime_t         timeA, timeB;
 
     void customEvent( QEvent * );
+    RateLimitedEventPoster *rateLimitedEventPoster;
 
     void addCallbacks();
     void delCallbacks();
diff --git a/modules/gui/qt4/util/uniqueevent.cpp b/modules/gui/qt4/util/uniqueevent.cpp
new file mode 100644
index 0000000..e116bb3
--- /dev/null
+++ b/modules/gui/qt4/util/uniqueevent.cpp
@@ -0,0 +1,65 @@
+/*****************************************************************************
+ * Copyright © 2012 VideoLAN
+ *
+ * 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 "uniqueevent.hpp"
+#include "qt4.hpp"
+
+#include <QTimer>
+#include <QApplication>
+
+RateLimitedEventPoster::RateLimitedEventPoster( int i_millisec_interval )
+{
+    timer = new QTimer();
+    timer->setSingleShot( true );
+    /* Assuming a 24fps event loop, delays at least events to the next frame */
+    if ( i_millisec_interval < 1 )
+        i_millisec_interval = 1000 / 48;
+    timer->setInterval( i_millisec_interval );
+    CONNECT( timer, timeout(), this, commit() );
+}
+
+RateLimitedEventPoster::~RateLimitedEventPoster()
+{
+    timer->stop();
+    commit();
+    delete timer;
+}
+
+void RateLimitedEventPoster::postEvent( UniqueEvent *e, QObject *target )
+{
+    event_tuple newtuple = { target, e };
+    foreach( event_tuple tuple, eventsList )
+    {
+        if ( target == tuple.target && tuple.event->equals( e ) )
+        {
+            delete e;
+            return;
+        }
+    }
+    eventsList << newtuple;
+    if ( !timer->isActive() ) timer->start();
+}
+
+void RateLimitedEventPoster::commit()
+{
+    foreach( event_tuple tuple, eventsList )
+    {
+        QApplication::postEvent( tuple.target, tuple.event );
+    }
+    eventsList.clear();
+}
diff --git a/modules/gui/qt4/util/uniqueevent.hpp b/modules/gui/qt4/util/uniqueevent.hpp
new file mode 100644
index 0000000..0eb2bd3
--- /dev/null
+++ b/modules/gui/qt4/util/uniqueevent.hpp
@@ -0,0 +1,55 @@
+/*****************************************************************************
+ * Copyright © 2012 VideoLAN
+ *
+ * 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.
+ *****************************************************************************/
+#ifndef UNIQUEEVENT_HPP
+#define UNIQUEEVENT_HPP
+
+#include <QObject>
+#include <QEvent>
+#include <QList>
+class QTimer;
+
+class UniqueEvent : public QEvent
+{
+public:
+    UniqueEvent( QEvent::Type type ) : QEvent( type ) {};
+    virtual bool equals( UniqueEvent *e ) const = 0;
+};
+
+class RateLimitedEventPoster : public QObject
+{
+    Q_OBJECT
+
+public:
+    RateLimitedEventPoster( int i_millisec_interval = -1 );
+    ~RateLimitedEventPoster();
+    void postEvent( UniqueEvent *e, QObject *target );
+
+private slots:
+    void commit();
+
+private:
+    struct event_tuple
+    {
+        QObject *target;
+        UniqueEvent *event;
+    };
+    QList<event_tuple> eventsList;
+    QTimer *timer;
+};
+
+#endif // UNIQUEEVENT_HPP



More information about the vlc-commits mailing list