[mousetweaks] Don't emit cursor_changed signals if we have set the cursor



commit 2a18bba71ae288cbad11c1e11d60332d2bc1263c
Author: Gerd Kohlberger <gerdk src gnome org>
Date:   Sun Nov 7 20:24:15 2010 +0100

    Don't emit cursor_changed signals if we have set the cursor
    
    - Move code that tracks the current cursor into MtCursorManager.
    - Simplify cursor cache handling.

 src/mt-cursor-manager.c |   86 +++++++++++++++++++-------------------
 src/mt-cursor-manager.h |   18 ++++----
 src/mt-main.c           |  108 +++++++++++++++++++++--------------------------
 3 files changed, 100 insertions(+), 112 deletions(-)
---
diff --git a/src/mt-cursor-manager.c b/src/mt-cursor-manager.c
index 8ecef50..ad97350 100644
--- a/src/mt-cursor-manager.c
+++ b/src/mt-cursor-manager.c
@@ -30,23 +30,25 @@ static int xfixes_error = 0;
 struct _MtCursorManagerPrivate
 {
     GHashTable *cache;
+    MtCursor   *current;
 };
 
 enum
 {
     CURSOR_CHANGED,
-    CACHE_CLEARED,
     LAST_SIGNAL
 };
 
 static guint signals[LAST_SIGNAL] = { 0, };
 
-static void            mt_cursor_manager_clear_cache  (GObject         *settings,
-                                                       GParamSpec      *pspec,
-                                                       MtCursorManager *manager);
-static GdkFilterReturn mt_cursor_manager_event_filter (GdkXEvent       *gdk_xevent,
-                                                       GdkEvent        *gdk_event,
-                                                       MtCursorManager *manager);
+static void            mt_cursor_manager_clear_cache        (GObject         *settings,
+                                                             GParamSpec      *pspec,
+                                                             MtCursorManager *manager);
+static GdkFilterReturn mt_cursor_manager_event_filter       (GdkXEvent       *gdk_xevent,
+                                                             GdkEvent        *gdk_event,
+                                                             MtCursorManager *manager);
+static void            mt_cursor_manager_add_current_cursor (MtCursorManager *manager,
+                                                             Display         *display);
 
 G_DEFINE_TYPE (MtCursorManager, mt_cursor_manager, G_TYPE_OBJECT)
 
@@ -82,6 +84,8 @@ mt_cursor_manager_init (MtCursorManager *manager)
                       G_CALLBACK (mt_cursor_manager_clear_cache), manager);
     g_signal_connect (gs, "notify::gtk-cursor-theme-size",
                       G_CALLBACK (mt_cursor_manager_clear_cache), manager);
+
+    mt_cursor_manager_add_current_cursor (manager, dpy);
 }
 
 static void
@@ -110,14 +114,6 @@ mt_cursor_manager_class_init (MtCursorManagerClass *klass)
                       g_cclosure_marshal_VOID__STRING,
                       G_TYPE_NONE, 1, G_TYPE_STRING);
 
-    signals[CACHE_CLEARED] =
-        g_signal_new (g_intern_static_string ("cache_cleared"),
-                      G_OBJECT_CLASS_TYPE (klass),
-                      G_SIGNAL_RUN_LAST,
-                      0, NULL, NULL,
-                      g_cclosure_marshal_VOID__VOID,
-                      G_TYPE_NONE, 0);
-
     g_type_class_add_private (klass, sizeof (MtCursorManagerPrivate));
 }
 
@@ -169,7 +165,7 @@ mt_cursor_manager_restore_cursor (gpointer key,
     mt_cursor_manager_set_xcursor (value);
 }
 
-static void
+static MtCursor *
 mt_cursor_manager_add_cursor (MtCursorManager   *manager,
                               XFixesCursorImage *image)
 {
@@ -203,6 +199,22 @@ mt_cursor_manager_add_cursor (MtCursorManager   *manager,
                              g_strdup (image->name),
                              cursor);
     }
+
+    return cursor;
+}
+
+static void
+mt_cursor_manager_add_current_cursor (MtCursorManager *manager,
+                                      Display         *display)
+{
+    XFixesCursorImage *image;
+
+    image = XFixesGetCursorImage (display);
+    if (image)
+    {
+        manager->priv->current = mt_cursor_manager_add_cursor (manager, image);
+        XFree (image);
+    }
 }
 
 static GdkFilterReturn
@@ -218,18 +230,23 @@ mt_cursor_manager_event_filter (GdkXEvent       *gdk_xevent,
 
         if (cn->cursor_name != None)
         {
-            XFixesCursorImage *image;
+            MtCursorManagerPrivate *priv = manager->priv;
+            char *name;
 
-            image = XFixesGetCursorImage (cn->display);
-            if (image)
+            name = XGetAtomName (cn->display, cn->cursor_name);
+
+            if (!priv->current || !g_str_equal (name, mt_cursor_get_name (priv->current)))
             {
-                if (!mt_cursor_manager_lookup_cursor (manager, image->name))
-                    mt_cursor_manager_add_cursor (manager, image);
+                priv->current = mt_cursor_manager_lookup_cursor (manager, name);
 
-                g_signal_emit (manager, signals[CURSOR_CHANGED], 0, image->name);
+                if (!priv->current)
+                {
+                    mt_cursor_manager_add_current_cursor (manager, cn->display);
+                }
 
-                XFree (image);
+                g_signal_emit (manager, signals[CURSOR_CHANGED], 0, name);
             }
+            XFree (name);
         }
     }
     return GDK_FILTER_CONTINUE;
@@ -241,7 +258,7 @@ mt_cursor_manager_clear_cache (GObject         *settings,
                                MtCursorManager *manager)
 {
     g_hash_table_remove_all (manager->priv->cache);
-    g_signal_emit (manager, signals[CACHE_CLEARED], 0);
+    manager->priv->current = NULL;
 }
 
 MtCursorManager *
@@ -258,28 +275,11 @@ mt_cursor_manager_get_default (void)
 }
 
 MtCursor *
-mt_cursor_manager_current_cursor (MtCursorManager *manager)
+mt_cursor_manager_get_current_cursor (MtCursorManager *manager)
 {
-    XFixesCursorImage *image;
-    MtCursor *cursor;
-
     g_return_val_if_fail (MT_IS_CURSOR_MANAGER (manager), NULL);
 
-    image = XFixesGetCursorImage (mt_common_get_xdisplay ());
-    cursor = NULL;
-
-    if (image->name && image->name[0] != '\0')
-    {
-        cursor = mt_cursor_manager_lookup_cursor (manager, image->name);
-        if (!cursor)
-        {
-            mt_cursor_manager_add_cursor (manager, image);
-            cursor = mt_cursor_manager_lookup_cursor (manager, image->name);
-        }
-    }
-    XFree (image);
-
-    return cursor;
+    return g_object_ref (manager->priv->current);
 }
 
 MtCursor *
diff --git a/src/mt-cursor-manager.h b/src/mt-cursor-manager.h
index 95f62dc..7372588 100644
--- a/src/mt-cursor-manager.h
+++ b/src/mt-cursor-manager.h
@@ -40,15 +40,15 @@ struct _MtCursorManager
     MtCursorManagerPrivate *priv;
 };
 
-GType             mt_cursor_manager_get_type       (void) G_GNUC_CONST;
-
-MtCursorManager * mt_cursor_manager_get_default    (void);
-MtCursor *        mt_cursor_manager_current_cursor (MtCursorManager *manager);
-MtCursor *        mt_cursor_manager_lookup_cursor  (MtCursorManager *manager,
-                                                    const gchar     *name);
-void              mt_cursor_manager_set_cursor     (MtCursorManager *manager,
-                                                    MtCursor        *cursor);
-void              mt_cursor_manager_restore_all    (MtCursorManager *manager);
+GType             mt_cursor_manager_get_type           (void) G_GNUC_CONST;
+
+MtCursorManager * mt_cursor_manager_get_default        (void);
+MtCursor *        mt_cursor_manager_get_current_cursor (MtCursorManager *manager);
+MtCursor *        mt_cursor_manager_lookup_cursor      (MtCursorManager *manager,
+                                                        const gchar     *name);
+void              mt_cursor_manager_set_cursor         (MtCursorManager *manager,
+                                                        MtCursor        *cursor);
+void              mt_cursor_manager_restore_all        (MtCursorManager *manager);
 
 G_END_DECLS
 
diff --git a/src/mt-main.c b/src/mt-main.c
index fc32bcb..33bec1a 100644
--- a/src/mt-main.c
+++ b/src/mt-main.c
@@ -64,7 +64,6 @@ typedef struct _MtData
     MtService   *service;
     MtTimer     *ssc_timer;
     MtTimer     *dwell_timer;
-    MtCursor    *cursor;
     gint         direction;
     gint         pointer_x;
     gint         pointer_y;
@@ -439,35 +438,31 @@ global_button_event (MtListener *listener,
 }
 
 static void
-cursor_overlay_time (MtData  *mt,
-                     guchar  *image,
-                     gint     width,
-                     gint     height,
-                     MtTimer *timer,
-                     gdouble  time)
+cursor_overlay_time (guchar *image,
+                     gint    width,
+                     gint    height,
+                     gdouble target,
+                     gdouble elapsed)
 {
-    GdkColor c;
+    GdkColor color;
     GtkStyle *style;
     cairo_surface_t *surface;
     cairo_t *cr;
-    gdouble target;
+
+    style = gtk_widget_get_style (mt_ctw_get_window ());
+    color = style->bg[GTK_STATE_SELECTED];
 
     surface = cairo_image_surface_create_for_data (image,
                                                    CAIRO_FORMAT_ARGB32,
                                                    width, height,
                                                    width * 4);
     cr = cairo_create (surface);
-
-    style = gtk_widget_get_style (mt_ctw_get_window ());
-    c = style->bg[GTK_STATE_SELECTED];
-    target = mt_timer_get_target (timer);
-
     cairo_set_operator (cr, CAIRO_OPERATOR_ATOP);
-    cairo_rectangle (cr, 0, 0, width, height / (target / time));
+    cairo_rectangle (cr, 0, 0, width, (height * elapsed) / target);
     cairo_set_source_rgba (cr,
-                           c.red   / 65535.,
-                           c.green / 65535.,
-                           c.blue  / 65535.,
+                           color.red   / 65535.,
+                           color.green / 65535.,
+                           color.blue  / 65535.,
                            0.60);
     cairo_fill (cr);
     cairo_destroy (cr);
@@ -475,58 +470,54 @@ cursor_overlay_time (MtData  *mt,
 }
 
 static void
-mt_main_update_cursor (MtData  *mt,
-                       MtTimer *timer,
-                       gdouble  time)
+mt_main_timer_tick (MtTimer *timer, gdouble elapsed, gpointer data)
 {
-    MtCursor *new_cursor;
-    const gchar *name;
-    gushort xhot, yhot;
-    guchar *image;
-    gushort width, height;
+    MtCursorManager *manager;
+    MtCursor *current_cursor, *new_cursor;
 
-    image = mt_cursor_get_image_copy (mt->cursor);
-    if (image)
+    manager = mt_cursor_manager_get_default ();
+    current_cursor = mt_cursor_manager_get_current_cursor (manager);
+
+    if (current_cursor)
     {
-        mt_cursor_get_dimension (mt->cursor, &width, &height);
-        cursor_overlay_time (mt, image, width, height, timer, time);
+        const gchar *name;
+        gushort width, height, xhot, yhot;
+        gdouble target;
+        guchar *image;
 
-        name = mt_cursor_get_name (mt->cursor);
-        mt_cursor_get_hotspot (mt->cursor, &xhot, &yhot);
-        new_cursor = mt_cursor_new (name, image, width, height, xhot, yhot);
-        mt_cursor_manager_set_cursor (mt_cursor_manager_get_default (), new_cursor);
+        /* get cursor info */
+        name = mt_cursor_get_name (current_cursor);
+        image = mt_cursor_get_image_copy (current_cursor);
+        mt_cursor_get_dimension (current_cursor, &width, &height);
+        mt_cursor_get_hotspot (current_cursor, &xhot, &yhot);
 
-        g_object_unref (new_cursor);
-    }
-}
+        g_object_unref (current_cursor);
 
-static void
-mt_main_timer_tick (MtTimer *timer,
-                    gdouble  time,
-                    MtData  *mt)
-{
-    if (mt->cursor)
-    {
-        mt_main_update_cursor (mt, timer, time);
-    }
-}
+        target = mt_timer_get_target (timer);
 
-static void
-cursor_cache_cleared (MtCursorManager *manager, MtData *mt)
-{
-    mt->cursor = mt_cursor_manager_current_cursor (manager);
+        /* paint overlay */
+        cursor_overlay_time (image, width, height, target, elapsed);
+
+        /* create and set new cursor */
+        new_cursor = mt_cursor_new (name, image, width, height, xhot, yhot);
+        if (new_cursor)
+        {
+            mt_cursor_manager_set_cursor (manager, new_cursor);
+            g_object_unref (new_cursor);
+        }
+    }
 }
 
 static void
-cursor_changed (MtCursorManager *manager,
-                const gchar     *name,
-                MtData          *mt)
+mt_main_cursor_changed (MtCursorManager *manager,
+                        const gchar     *name,
+                        MtData          *mt)
 {
     if (!mt->dwell_gesture_started)
     {
+        /* Remove me, I'm weird */
         mt->override_cursor = !g_str_equal (name, "left_ptr");
     }
-    mt->cursor = mt_cursor_manager_lookup_cursor (manager, name);
 }
 
 static void
@@ -732,11 +723,8 @@ mt_main (int argc, char **argv, MtCliArgs cli_args)
 
     /* init cursor animation */
     manager = mt_cursor_manager_get_default ();
-    mt->cursor = mt_cursor_manager_current_cursor (manager);
     g_signal_connect (manager, "cursor_changed",
-                      G_CALLBACK (cursor_changed), mt);
-    g_signal_connect (manager, "cache_cleared",
-                      G_CALLBACK (cursor_cache_cleared), mt);
+                      G_CALLBACK (mt_main_cursor_changed), mt);
 
     /* init mouse listener */
     listener = mt_listener_get_default ();



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]