[vlc-commits] npapi: gtk: Update position slider

Hugo Beauzée-Luyssen git at videolan.org
Tue Mar 31 10:51:55 CEST 2015


npapi-vlc | branch: cleanup | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Thu Mar 26 14:57:19 2015 +0100| [86e16b6825a680a109aef396470794a8f23d14bd] | committer: Hugo Beauzée-Luyssen

npapi: gtk: Update position slider

Simplify both slider handling

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

 npapi/vlcplugin_gtk.cpp |   62 ++++++++++++++++++-----------------------------
 npapi/vlcplugin_gtk.h   |    7 +++++-
 2 files changed, 29 insertions(+), 40 deletions(-)

diff --git a/npapi/vlcplugin_gtk.cpp b/npapi/vlcplugin_gtk.cpp
index fa8dd23..f58638e 100644
--- a/npapi/vlcplugin_gtk.cpp
+++ b/npapi/vlcplugin_gtk.cpp
@@ -43,8 +43,7 @@ VlcPluginGtk::VlcPluginGtk(NPP instance, NPuint16_t mode) :
     fullscreen_win(NULL),
     is_fullscreen(false),
     is_toolbar_visible(false),
-    time_slider_timeout_id(0),
-    vol_slider_timeout_id(0)
+    m_timer_update_timeout( 0 )
 {
     memset(&video_xwindow, 0, sizeof(Window));
     GtkIconTheme *icon_theme = gtk_icon_theme_get_default();
@@ -55,6 +54,15 @@ VlcPluginGtk::VlcPluginGtk(NPP instance, NPuint16_t mode) :
     }
 }
 
+VlcPluginGtk::~VlcPluginGtk()
+{
+    {
+        std::lock_guard<std::mutex> lock( m_timer_lock );
+        if ( m_timer_update_timeout != 0 )
+            g_source_remove( m_timer_update_timeout );
+    }
+}
+
 void VlcPluginGtk::set_player_window()
 {
     player().get_mp().setXwindow( video_xwindow);
@@ -298,53 +306,29 @@ static bool video_expose_handler(GtkWidget *widget, GdkEvent *event, gpointer us
     return true;
 }
 
-static gboolean do_time_slider_handler(gpointer user_data)
+gboolean VlcPluginGtk::update_time_slider(gpointer user_data)
 {
     VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
-    auto md = plugin->getMD();
-    if (md) {
-        gdouble value = gtk_range_get_value(GTK_RANGE(plugin->time_slider));
-        md.setPosition( value / 100.0 );
-    }
 
-    plugin->time_slider_timeout_id = 0;
+    std::lock_guard<std::mutex> lock( plugin->m_timer_lock );
+
+    auto pos = plugin->player().get_mp().position() * 100.0f;
+    gtk_range_set_value( GTK_RANGE( plugin->time_slider ), pos );
+    plugin->m_timer_update_timeout = 0;
     return FALSE;
 }
 
 static bool time_slider_handler(GtkRange *range, GtkScrollType scroll, gdouble value, gpointer user_data)
 {
     VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
-    if (plugin->time_slider_timeout_id != 0)
-        return false;
-
-    plugin->time_slider_timeout_id = g_timeout_add(500,
-                                                  do_time_slider_handler,
-                                                  user_data);
+    plugin->getMD().setPosition( value / 100.0 );
     return false;
 }
 
-static gboolean do_vol_slider_handler(gpointer user_data)
-{
-    VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
-    auto md = plugin->getMD();
-    if (md) {
-        gdouble value = gtk_range_get_value(GTK_RANGE(plugin->vol_slider));
-        md.setVolume( value );
-    }
-
-    plugin->vol_slider_timeout_id = 0;
-    return FALSE;
-}
-
 static bool vol_slider_handler(GtkRange *range, GtkScrollType scroll, gdouble value, gpointer user_data)
 {
     VlcPluginGtk *plugin = (VlcPluginGtk *) user_data;
-    if (plugin->vol_slider_timeout_id != 0)
-        return false;
-
-    plugin->vol_slider_timeout_id = g_timeout_add(100,
-                                                  do_vol_slider_handler,
-                                                  user_data);
+    plugin->getMD().setVolume( value );
     return false;
 }
 
@@ -403,11 +387,6 @@ void VlcPluginGtk::update_controls()
             gtk_range_set_value(GTK_RANGE(time_slider), 0);
         } else {
             gtk_widget_set_sensitive(time_slider, true);
-            gdouble timepos = 100.0 * player().get_mp().position();
-            if (time_slider_timeout_id == 0) {
-                /* only set the time if the user is not dragging the slider */
-                gtk_range_set_value(GTK_RANGE(time_slider), timepos);
-            }
         }
 
         gtk_widget_show_all(toolbar);
@@ -498,6 +477,11 @@ bool VlcPluginGtk::create_windows()
     gtk_container_add(GTK_CONTAINER(toolitem), time_slider);
     gtk_tool_item_set_expand(toolitem, true);
     gtk_toolbar_insert(GTK_TOOLBAR(toolbar), toolitem, -1);
+    m_player.get_mp().eventManager().onPositionChanged([this](float) {
+        std::lock_guard<std::mutex> lock( m_timer_lock );
+        if ( m_timer_update_timeout == 0 )
+            m_timer_update_timeout = g_timeout_add( 100, update_time_slider, (gpointer)this);
+    });
 
     /* volume slider */
     toolitem = gtk_tool_item_new();
diff --git a/npapi/vlcplugin_gtk.h b/npapi/vlcplugin_gtk.h
index 072bb7f..fbda220 100644
--- a/npapi/vlcplugin_gtk.h
+++ b/npapi/vlcplugin_gtk.h
@@ -27,6 +27,7 @@
 #include "vlcplugin_base.h"
 
 #include <gtk/gtk.h>
+#include <mutex>
 #include <X11/Xlib.h>
 
 #define VLCPLUGINGTK_MENU_TOOLBAR "Show toolbar"
@@ -35,6 +36,7 @@ class VlcPluginGtk : public VlcPluginBase
 {
 public:
     VlcPluginGtk(NPP, NPuint16_t);
+    virtual ~VlcPluginGtk();
 
     bool create_windows();
     bool resize_windows();
@@ -54,10 +56,11 @@ public:
 
     GdkPixbuf *cone_icon;
     GtkWidget *time_slider, *vol_slider;
-    guint time_slider_timeout_id, vol_slider_timeout_id;
+
 private:
     void set_player_window();
     Display *get_display() { return display; }
+    static gboolean update_time_slider(gpointer user_data);
 
     unsigned int     i_width, i_height;
     GtkWidget *parent, *parent_vbox, *video_container;
@@ -69,6 +72,8 @@ private:
     Window video_xwindow;
     XColor bg_color;
     bool is_fullscreen, is_toolbar_visible;
+    std::mutex m_timer_lock;
+    guint m_timer_update_timeout;
 };
 
 #endif /* __VLCPLUGIN_GTK_H__ */



More information about the vlc-commits mailing list