[mousetweaks] Add private variable to cursor-manager and clean up



commit dc2bc22e8b62a1078dec3e9110f9fd598449a0a2
Author: Gerd Kohlberger <gerdk src gnome org>
Date:   Sat May 29 23:34:54 2010 +0200

    Add private variable to cursor-manager and clean up

 src/mt-cursor-manager.c |  317 +++++++++++++++++++++++------------------------
 src/mt-cursor-manager.h |   36 +++---
 2 files changed, 175 insertions(+), 178 deletions(-)
---
diff --git a/src/mt-cursor-manager.c b/src/mt-cursor-manager.c
index 92b58e8..95f5871 100644
--- a/src/mt-cursor-manager.c
+++ b/src/mt-cursor-manager.c
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2008-2009 Gerd Kohlberger <lowfi chello at>
+ * Copyright © 2008-2010 Gerd Kohlberger <gerdko gmail com>
  *
  * This file is part of Mousetweaks.
  *
@@ -25,215 +25,226 @@
 #include "mt-cursor.h"
 #include "mt-cursor-manager.h"
 
-#define MT_CURSOR_MANAGER_GET_PRIVATE(o) \
-    (G_TYPE_INSTANCE_GET_PRIVATE ((o), MT_TYPE_CURSOR_MANAGER, MtCursorManagerPrivate))
+static int xfixes_event = 0;
+static int xfixes_error = 0;
 
-static int fixes_event = 0;
-static int fixes_error = 0;
-
-typedef struct _MtCursorManagerPrivate MtCursorManagerPrivate;
-struct _MtCursorManagerPrivate {
+struct _MtCursorManagerPrivate
+{
     GHashTable *cache;
 };
 
-enum {
+enum
+{
     CURSOR_CHANGED,
     CACHE_CLEARED,
     LAST_SIGNAL
 };
 
-static guint signals[LAST_SIGNAL] = { 0 };
+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,
+                                                       gpointer         data);
 
 G_DEFINE_TYPE (MtCursorManager, mt_cursor_manager, G_TYPE_OBJECT)
 
-static void            mt_cursor_manager_finalize    (GObject         *object);
-static void            mt_cursor_manager_clear_cache (MtCursorManager *manager);
-static GdkFilterReturn xfixes_filter                 (GdkXEvent       *xevent,
-						      GdkEvent        *event,
-						      gpointer         data);
+static Display *
+mt_cursor_manager_display (void)
+{
+    return GDK_DISPLAY ();
+}
 
 static void
-mt_cursor_manager_class_init (MtCursorManagerClass *klass)
+mt_cursor_manager_init (MtCursorManager *manager)
 {
-    G_OBJECT_CLASS (klass)->finalize = mt_cursor_manager_finalize;
-
-    signals[CURSOR_CHANGED] =
-	g_signal_new (g_intern_static_string ("cursor_changed"),
-		      G_OBJECT_CLASS_TYPE (klass),
-		      G_SIGNAL_RUN_LAST,
-		      0, NULL, NULL,
-		      g_cclosure_marshal_VOID__STRING,
-		      G_TYPE_NONE, 1, G_TYPE_STRING);
+    GtkSettings *gs;
+
+    manager->priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
+                                                 MT_TYPE_CURSOR_MANAGER,
+                                                 MtCursorManagerPrivate);
+
+    manager->priv->cache = g_hash_table_new_full (g_str_hash,
+                                                  g_str_equal,
+                                                  g_free,
+                                                  g_object_unref);
+
+    /* init XFixes extension */
+    if (XFixesQueryExtension (mt_cursor_manager_display (),
+                              &xfixes_event, &xfixes_error))
+    {
+        XFixesSelectCursorInput (mt_cursor_manager_display (),
+                                 GDK_ROOT_WINDOW (),
+                                 XFixesDisplayCursorNotifyMask);
+    }
 
-    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);
+    gdk_window_add_filter (NULL, mt_cursor_manager_event_filter, manager);
 
-    g_type_class_add_private (klass, sizeof (MtCursorManagerPrivate));
+    /* listen for cursor theme changes */
+    gs = gtk_settings_get_default ();
+    g_signal_connect (gs, "notify::gtk-cursor-theme-name",
+                      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);
 }
 
 static void
-mt_cursor_manager_init (MtCursorManager *manager)
+mt_cursor_manager_finalize (GObject *object)
 {
-    MtCursorManagerPrivate *priv = MT_CURSOR_MANAGER_GET_PRIVATE (manager);
+    MtCursorManagerPrivate *priv = MT_CURSOR_MANAGER (object)->priv;
 
-    priv->cache = NULL;
+    gdk_window_remove_filter (NULL, mt_cursor_manager_event_filter, object);
+    g_hash_table_destroy (priv->cache);
+
+    G_OBJECT_CLASS (mt_cursor_manager_parent_class)->finalize (object);
 }
 
 static void
-mt_cursor_manager_finalize (GObject *object)
+mt_cursor_manager_class_init (MtCursorManagerClass *klass)
 {
-    MtCursorManagerPrivate *priv = MT_CURSOR_MANAGER_GET_PRIVATE (object);
+    GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-    gdk_window_remove_filter (gdk_get_default_root_window (),
-			      xfixes_filter, object);
+    object_class->finalize = mt_cursor_manager_finalize;
 
-    if (priv->cache)
-	g_hash_table_destroy (priv->cache);
+    signals[CURSOR_CHANGED] =
+        g_signal_new (g_intern_static_string ("cursor_changed"),
+                      G_OBJECT_CLASS_TYPE (klass),
+                      G_SIGNAL_RUN_LAST,
+                      0, NULL, NULL,
+                      g_cclosure_marshal_VOID__STRING,
+                      G_TYPE_NONE, 1, G_TYPE_STRING);
 
-    G_OBJECT_CLASS (mt_cursor_manager_parent_class)->finalize (object);
+    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));
 }
 
 static void
 mt_cursor_manager_set_xcursor (MtCursor *cursor)
 {
+    Display *dpy;
     XcursorImage *ximage;
-    const gchar  *name;
-    Cursor        xcursor;
-    gushort       width, height;
-    gushort       xhot, yhot;
+    Cursor xcursor;
+    const gchar *name;
+    gushort width, height;
+    gushort xhot, yhot;
 
+    dpy = mt_cursor_manager_display ();
     mt_cursor_get_dimension (cursor, &width, &height);
     ximage = XcursorImageCreate (width, height);
     xcursor = 0;
 
-    if (ximage) {
-	mt_cursor_get_hotspot (cursor, &xhot, &yhot);
+    if (ximage)
+    {
+        mt_cursor_get_hotspot (cursor, &xhot, &yhot);
 
-	ximage->xhot = xhot;
-	ximage->yhot = yhot;
-	ximage->delay = 0;
-	ximage->pixels = (XcursorPixel *) mt_cursor_get_image (cursor);
+        ximage->xhot = xhot;
+        ximage->yhot = yhot;
+        ximage->delay = 0;
+        ximage->pixels = (XcursorPixel *) mt_cursor_get_image (cursor);
 
-	xcursor = XcursorImageLoadCursor (GDK_DISPLAY (), ximage);
-	XcursorImageDestroy (ximage);
+        xcursor = XcursorImageLoadCursor (dpy, ximage);
+        XcursorImageDestroy (ximage);
     }
 
-    if (xcursor) {
-	name = mt_cursor_get_name (cursor);
+    if (xcursor)
+    {
+        name = mt_cursor_get_name (cursor);
 
-	XFixesSetCursorName (GDK_DISPLAY (), xcursor, name);
-	XFixesChangeCursorByName (GDK_DISPLAY (), xcursor, name);
-	XFreeCursor (GDK_DISPLAY (), xcursor);
+        XFixesSetCursorName (dpy, xcursor, name);
+        XFixesChangeCursorByName (dpy, xcursor, name);
+        XFreeCursor (dpy, xcursor);
 
-	gdk_flush ();
+        gdk_flush ();
     }
 }
 
 static void
 mt_cursor_manager_restore_cursor (gpointer key,
-				  gpointer value,
-				  gpointer data)
+                                  gpointer value,
+                                  gpointer data)
 {
-    mt_cursor_manager_set_xcursor (MT_CURSOR (value));
+    mt_cursor_manager_set_xcursor (value);
 }
 
 static void
 mt_cursor_manager_add_cursor (MtCursorManager   *manager,
-			      XFixesCursorImage *image)
+                              XFixesCursorImage *image)
 {
     MtCursor *cursor;
-    MtCursorManagerPrivate *priv;
     guint32 *copy;
     guchar *pixels;
     guint i, n_pixels;
 
     /* convert cursor image on x64 arch */
-    if (sizeof (unsigned long) != sizeof (guint32)) {
-	n_pixels = image->width * image->height;
-	copy = g_new (guint32, n_pixels);
+    if (sizeof (unsigned long) != sizeof (guint32))
+    {
+        n_pixels = image->width * image->height;
+        copy = g_new (guint32, n_pixels);
 
-	for (i = 0; i < n_pixels; i++)
-	    copy[i] = image->pixels[i];
+        for (i = 0; i < n_pixels; i++)
+            copy[i] = image->pixels[i];
 
-	pixels = (guchar *) copy;
+        pixels = (guchar *) copy;
     }
     else
-	pixels = (guchar *) image->pixels;
-
+    {
+        pixels = (guchar *) image->pixels;
+    }
 
     cursor = mt_cursor_new (image->name, pixels,
-			    image->width, image->height,
-			    image->xhot, image->yhot);
-
-    if (cursor) {
-	priv = MT_CURSOR_MANAGER_GET_PRIVATE (manager);
-	g_hash_table_insert (priv->cache, g_strdup (image->name), cursor);
+                            image->width, image->height,
+                            image->xhot, image->yhot);
+    if (cursor)
+    {
+        g_hash_table_insert (manager->priv->cache,
+                             g_strdup (image->name),
+                             cursor);
     }
 }
 
 static GdkFilterReturn
-xfixes_filter (GdkXEvent *xevent, GdkEvent *event, gpointer data)
+mt_cursor_manager_event_filter (GdkXEvent *gdk_xevent,
+                                GdkEvent  *gdk_event,
+                                gpointer   data)
 {
-    XEvent *xev = (XEvent *) xevent;
+    XEvent *xev = gdk_xevent;
 
-    if (xev->type == fixes_event + XFixesCursorNotify) {
-	XFixesCursorNotifyEvent *cn = (XFixesCursorNotifyEvent *) xev;
+    if (xev->type == xfixes_event + XFixesCursorNotify)
+    {
+        XFixesCursorNotifyEvent *cn = (XFixesCursorNotifyEvent *) xev;
 
-	if (cn->cursor_name != None) {
-	    MtCursorManager *manager = data;
-	    XFixesCursorImage *image;
+        if (cn->cursor_name != None)
+        {
+            MtCursorManager *manager = data;
+            XFixesCursorImage *image;
 
-	    image = XFixesGetCursorImage (GDK_DISPLAY ());
-	    if (!mt_cursor_manager_lookup_cursor (manager, image->name))
-		mt_cursor_manager_add_cursor (manager, image);
+            image = XFixesGetCursorImage (cn->display);
+            if (!mt_cursor_manager_lookup_cursor (manager, image->name))
+                mt_cursor_manager_add_cursor (manager, image);
 
-	    g_signal_emit (manager, signals[CURSOR_CHANGED], 0, image->name);
-	    XFree (image);
-	}
+            g_signal_emit (manager, signals[CURSOR_CHANGED], 0, image->name);
+            XFree (image);
+        }
     }
     return GDK_FILTER_CONTINUE;
 }
 
 static void
-_clear_cursor_cache (GObject    *settings,
-		     GParamSpec *pspec,
-		     gpointer    data)
-{
-    mt_cursor_manager_clear_cache (data);
-    g_signal_emit (data, signals[CACHE_CLEARED], 0);
-}
-
-static MtCursorManager *
-mt_cursor_manager_new (void)
+mt_cursor_manager_clear_cache (GObject         *settings,
+                               GParamSpec      *pspec,
+                               MtCursorManager *manager)
 {
-    MtCursorManager *manager;
-    MtCursorManagerPrivate *priv;
-    GtkSettings *settings;
-
-    manager = g_object_new (MT_TYPE_CURSOR_MANAGER, NULL);
-    priv = MT_CURSOR_MANAGER_GET_PRIVATE (manager);
-    priv->cache = g_hash_table_new_full (g_str_hash, g_str_equal,
-					 g_free, g_object_unref);
-
-    XFixesQueryExtension (GDK_DISPLAY (), &fixes_event, &fixes_error);
-    XFixesSelectCursorInput (GDK_DISPLAY (), GDK_ROOT_WINDOW (),
-			     XFixesDisplayCursorNotifyMask);
-
-    gdk_window_add_filter (gdk_get_default_root_window (),
-			   xfixes_filter, manager);
-
-    settings = gtk_settings_get_default ();
-    g_signal_connect (settings, "notify::gtk-cursor-theme-name",
-		      G_CALLBACK (_clear_cursor_cache), manager);
-    g_signal_connect (settings, "notify::gtk-cursor-theme-size",
-		      G_CALLBACK (_clear_cursor_cache), manager);
-
-    return manager;
+    g_hash_table_remove_all (manager->priv->cache);
+    g_signal_emit (manager, signals[CACHE_CLEARED], 0);
 }
 
 MtCursorManager *
@@ -241,23 +252,12 @@ mt_cursor_manager_get_default (void)
 {
     static MtCursorManager *manager = NULL;
 
-    if (manager == NULL)
-	manager = mt_cursor_manager_new ();
+    if (!manager)
+        manager = g_object_new (MT_TYPE_CURSOR_MANAGER, NULL);
 
     return manager;
 }
 
-static void
-mt_cursor_manager_clear_cache (MtCursorManager *manager)
-{
-    MtCursorManagerPrivate *priv;
-
-    g_return_if_fail (MT_IS_CURSOR_MANAGER (manager));
-
-    priv = MT_CURSOR_MANAGER_GET_PRIVATE (manager);
-    g_hash_table_remove_all (priv->cache);
-}
-
 MtCursor *
 mt_cursor_manager_current_cursor (MtCursorManager *manager)
 {
@@ -266,18 +266,18 @@ mt_cursor_manager_current_cursor (MtCursorManager *manager)
 
     g_return_val_if_fail (MT_IS_CURSOR_MANAGER (manager), NULL);
 
-    image = XFixesGetCursorImage (GDK_DISPLAY ());
+    image = XFixesGetCursorImage (mt_cursor_manager_display ());
     cursor = NULL;
 
-    if (image->name && image->name[0] != '\0') {
-	cursor = mt_cursor_manager_lookup_cursor (manager, image->name);
-
-	if (cursor == NULL) {
-	    mt_cursor_manager_add_cursor (manager, image);
-	    cursor = mt_cursor_manager_lookup_cursor (manager, image->name);
-	}
+    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;
@@ -285,23 +285,19 @@ mt_cursor_manager_current_cursor (MtCursorManager *manager)
 
 MtCursor *
 mt_cursor_manager_lookup_cursor (MtCursorManager *manager,
-				 const gchar     *name)
+                                 const gchar     *name)
 {
-    MtCursorManagerPrivate *priv;
-
     g_return_val_if_fail (MT_IS_CURSOR_MANAGER (manager), NULL);
 
     if (name == NULL || name[0] == '\0')
-	return NULL;
-
-    priv = MT_CURSOR_MANAGER_GET_PRIVATE (manager);
+        return NULL;
 
-    return g_hash_table_lookup (priv->cache, name);
+    return g_hash_table_lookup (manager->priv->cache, name);
 }
 
 void
 mt_cursor_manager_set_cursor (MtCursorManager *manager,
-			      MtCursor        *cursor)
+                              MtCursor        *cursor)
 {
     g_return_if_fail (MT_IS_CURSOR_MANAGER (manager));
     g_return_if_fail (MT_IS_CURSOR (cursor));
@@ -312,12 +308,9 @@ mt_cursor_manager_set_cursor (MtCursorManager *manager,
 void
 mt_cursor_manager_restore_all (MtCursorManager *manager)
 {
-    MtCursorManagerPrivate *priv;
-
     g_return_if_fail (MT_IS_CURSOR_MANAGER (manager));
 
-    priv = MT_CURSOR_MANAGER_GET_PRIVATE (manager);
-    g_hash_table_foreach (priv->cache,
-			  mt_cursor_manager_restore_cursor,
-			  manager);
+    g_hash_table_foreach (manager->priv->cache,
+                          mt_cursor_manager_restore_cursor,
+                          manager);
 }
diff --git a/src/mt-cursor-manager.h b/src/mt-cursor-manager.h
index aad3d68..e68f214 100644
--- a/src/mt-cursor-manager.h
+++ b/src/mt-cursor-manager.h
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2008-2009 Gerd Kohlberger <lowfi chello at>
+ * Copyright © 2008-2010 Gerd Kohlberger <gerdko gmail com>
  *
  * This file is part of Mousetweaks.
  *
@@ -26,21 +26,25 @@
 
 G_BEGIN_DECLS
 
-#define MT_TYPE_CURSOR_MANAGER		  (mt_cursor_manager_get_type ())
-#define MT_CURSOR_MANAGER(obj)		  (G_TYPE_CHECK_INSTANCE_CAST ((obj), MT_TYPE_CURSOR_MANAGER, MtCursorManager))
-#define MT_CURSOR_MANAGER_CLASS(klass)	  (G_TYPE_CHECK_CLASS_CAST ((klass), MT_TYPE_CURSOR_MANAGER, MtCursorManagerClass))
-#define MT_IS_CURSOR_MANAGER(obj)	  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MT_TYPE_CURSOR_MANAGER))
-#define MT_IS_CURSOR_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MT_TYPE_CURSOR_MANAGER))
-#define MT_CURSOR_MANAGER_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), MT_TYPE_CURSOR_MANAGER, MtCursorManagerClass))
-
-typedef struct _MtCursorManager MtCursorManager;
-typedef struct _MtCursorManagerClass MtCursorManagerClass;
-
-struct _MtCursorManager {
-    GObject parent;
+#define MT_TYPE_CURSOR_MANAGER         (mt_cursor_manager_get_type ())
+#define MT_CURSOR_MANAGER(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), MT_TYPE_CURSOR_MANAGER, MtCursorManager))
+#define MT_CURSOR_MANAGER_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST ((k), MT_TYPE_CURSOR_MANAGER, MtCursorManagerClass))
+#define MT_IS_CURSOR_MANAGER(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), MT_TYPE_CURSOR_MANAGER))
+#define MT_IS_CURSOR_MANAGER_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), MT_TYPE_CURSOR_MANAGER))
+#define MT_CURSOR_MANAGER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MT_TYPE_CURSOR_MANAGER, MtCursorManagerClass))
+
+typedef struct _MtCursorManager        MtCursorManager;
+typedef struct _MtCursorManagerClass   MtCursorManagerClass;
+typedef struct _MtCursorManagerPrivate MtCursorManagerPrivate;
+
+struct _MtCursorManager
+{
+    GObject                 parent;
+    MtCursorManagerPrivate *priv;
 };
 
-struct _MtCursorManagerClass {
+struct _MtCursorManagerClass
+{
     GObjectClass parent;
 };
 
@@ -48,9 +52,9 @@ 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);
+                                                    const gchar     *name);
 void              mt_cursor_manager_set_cursor     (MtCursorManager *manager,
-						    MtCursor        *cursor);
+                                                    MtCursor        *cursor);
 void              mt_cursor_manager_restore_all    (MtCursorManager *manager);
 
 G_END_DECLS



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