[mousetweaks] Migrate daemon to GDBus



commit 9020274f5fdd20aa2145e850efbe1bbe99ee29e1
Author: Gerd Kohlberger <gerdk src gnome org>
Date:   Sun Jun 20 18:35:57 2010 +0200

    Migrate daemon to GDBus

 src/mt-common.h  |   10 ++-
 src/mt-ctw.c     |    4 +-
 src/mt-main.c    |   17 ++--
 src/mt-service.c |  299 ++++++++++++++++++++++++++++++++++++-----------------
 src/mt-service.h |   29 +++---
 5 files changed, 235 insertions(+), 124 deletions(-)
---
diff --git a/src/mt-common.h b/src/mt-common.h
index b236aaf..f89496b 100644
--- a/src/mt-common.h
+++ b/src/mt-common.h
@@ -25,11 +25,16 @@
 #endif
 
 #include <glib/gi18n.h>
+#include <gdk/gdk.h>
 
 G_BEGIN_DECLS
 
 #define MT_ICON_NAME  "input-mouse"
 
+#define MOUSETWEAKS_DBUS_NAME  "org.gnome.Mousetweaks"
+#define MOUSETWEAKS_DBUS_IFACE "org.gnome.Mousetweaks"
+#define MOUSETWEAKS_DBUS_PATH  "/org/gnome/Mousetweaks"
+
 #define MT_GCONF_HOME "/desktop/gnome/accessibility/mouse"
 #define OPT_THRESHOLD MT_GCONF_HOME "/threshold"
 #define OPT_SSC       MT_GCONF_HOME "/delay_enable"
@@ -53,13 +58,14 @@ enum {
     DWELL_MODE_GESTURE
 };
 
-enum {
+typedef enum
+{
     DWELL_CLICK_TYPE_RIGHT = 0,
     DWELL_CLICK_TYPE_DRAG,
     DWELL_CLICK_TYPE_DOUBLE,
     DWELL_CLICK_TYPE_SINGLE,
     N_CLICK_TYPES
-};
+} MtClickType;
 
 enum {
     DIRECTION_LEFT = 0,
diff --git a/src/mt-ctw.c b/src/mt-ctw.c
index fbd657f..4fa7c61 100644
--- a/src/mt-ctw.c
+++ b/src/mt-ctw.c
@@ -119,9 +119,7 @@ ctw_button_cb (GtkToggleButton *button, gpointer data)
 	GSList *group;
 
 	group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (button));
-	mt_service_set_clicktype (mt->service, 
-				  g_slist_index (group, button),
-				  NULL);
+    mt_service_set_click_type (mt->service, g_slist_index (group, button));
     }
 }
 
diff --git a/src/mt-main.c b/src/mt-main.c
index 0fc0824..bd19111 100644
--- a/src/mt-main.c
+++ b/src/mt-main.c
@@ -156,23 +156,24 @@ static void
 dwell_restore_single_click (MtData *mt)
 {
     if (mt->dwell_mode == DWELL_MODE_CTW)
-	mt_ctw_set_clicktype (mt, DWELL_CLICK_TYPE_SINGLE);
+        mt_ctw_set_clicktype (mt, DWELL_CLICK_TYPE_SINGLE);
 
-    mt_service_set_clicktype (mt->service, DWELL_CLICK_TYPE_SINGLE, NULL);
+    mt_service_set_click_type (mt->service, DWELL_CLICK_TYPE_SINGLE);
 }
 
 static void
 mt_main_do_dwell_click (MtData *mt)
 {
-    guint clicktype;
+    MtClickType click_type;
 
-    clicktype = mt_service_get_clicktype (mt->service);
+    click_type = mt_service_get_click_type (mt->service);
 
     if (mt->dwell_mode == DWELL_MODE_GESTURE && !mt->dwell_drag_started)
 	mt_main_generate_motion_event (mt_main_current_screen (mt),
 				       mt->pointer_x, mt->pointer_y);
 
-    switch (clicktype) {
+    switch (click_type)
+    {
 	case DWELL_CLICK_TYPE_SINGLE:
 	    mt_main_generate_button_event (mt, 1, CLICK, 60);
 	    break;
@@ -219,7 +220,7 @@ mt_main_analyze_gesture (MtData *mt)
 {
     gint x, y, gd, i, dx, dy;
 
-    if (mt_service_get_clicktype (mt->service) == DWELL_CLICK_TYPE_DRAG)
+    if (mt_service_get_click_type (mt->service) == DWELL_CLICK_TYPE_DRAG)
 	return TRUE;
 
     gdk_display_get_pointer (gdk_display_get_default (), NULL, &x, &y, NULL);
@@ -256,7 +257,7 @@ mt_main_analyze_gesture (MtData *mt)
     /* get click type for direction */
     for (i = 0; i < N_CLICK_TYPES; i++) {
 	if (mt->dwell_dirs[i] == gd) {
-	    mt_service_set_clicktype (mt->service, i, NULL);
+	    mt_service_set_click_type (mt->service, i);
 	    return TRUE;
 	}
     }
@@ -701,8 +702,6 @@ mt_data_init (void)
 		      G_CALLBACK (mt_main_timer_tick), mt);
 
     mt->service = mt_service_get_default ();
-    mt_service_set_clicktype (mt->service, DWELL_CLICK_TYPE_SINGLE, NULL);
-
     mt->n_screens = gdk_display_get_n_screens (gdk_display_get_default ());
 
     mt->x_old = -1;
diff --git a/src/mt-service.c b/src/mt-service.c
index e664cc8..4be8112 100644
--- a/src/mt-service.c
+++ b/src/mt-service.c
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2007-2009 Gerd Kohlberger <lowfi chello at>
+ * Copyright © 2007-2010 Gerd Kohlberger <gerdko gmail com>
  *
  * This file is part of Mousetweaks.
  *
@@ -17,123 +17,237 @@
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <dbus/dbus-glib.h>
-#include <dbus/dbus-glib-bindings.h>
+#include <gio/gio.h>
 
 #include "mt-service.h"
-#include "mt-service-glue.h"
 
-#define MOUSETWEAKS_DBUS_SERVICE "org.gnome.Mousetweaks"
-#define MOUSETWEAKS_DBUS_PATH    "/org/gnome/Mousetweaks"
+struct _MtServicePrivate
+{
+    guint          owner_id;
+    GDBusNodeInfo *ispec;
 
-struct _MtServicePrivate {
-    guint clicktype;
+    guint          click_type;
 };
 
-enum {
-    STATUS_CHANGED,
-    CLICKTYPE_CHANGED,
-    LAST_SIGNAL
+enum
+{
+    PROP_0,
+    PROP_CLICK_TYPE
 };
 
-static guint signals[LAST_SIGNAL] = { 0 };
+static const gchar introspection_xml[] =
+    "<node>"
+    "  <interface name='" MOUSETWEAKS_DBUS_IFACE "'>"
+    "    <property type='i' name='ClickType' access='readwrite'/>"
+    "  </interface>"
+    "</node>";
 
-G_DEFINE_TYPE (MtService, mt_service, G_TYPE_OBJECT)
+static void mt_service_bus_acquired (GDBusConnection *connection,
+                                     const gchar     *name,
+                                     gpointer         data);
 
-static void mt_service_dispose  (GObject   *object);
-static void mt_service_register (MtService *service);
+G_DEFINE_TYPE (MtService, mt_service, G_TYPE_OBJECT)
 
 static void
-mt_service_class_init (MtServiceClass *klass)
+mt_service_init (MtService *service)
 {
-    GObjectClass *object_class = G_OBJECT_CLASS (klass);
+    MtServicePrivate *priv;
+    GError *error = NULL;
 
-    object_class->dispose = mt_service_dispose;
+    service->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (service,
+                                                        MT_TYPE_SERVICE,
+                                                        MtServicePrivate);
 
-    signals[STATUS_CHANGED] =
-	g_signal_new (g_intern_static_string ("status_changed"),
-		      G_OBJECT_CLASS_TYPE (klass),
-		      G_SIGNAL_RUN_LAST,
-		      0, NULL, NULL,
-		      g_cclosure_marshal_VOID__BOOLEAN,
-		      G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
-    signals[CLICKTYPE_CHANGED] =
-	g_signal_new (g_intern_static_string ("clicktype_changed"),
-		      G_OBJECT_CLASS_TYPE (klass),
-		      G_SIGNAL_RUN_LAST,
-		      0, NULL, NULL,
-		      g_cclosure_marshal_VOID__UINT,
-		      G_TYPE_NONE, 1, G_TYPE_UINT);
+    priv->click_type = DWELL_CLICK_TYPE_SINGLE;
+    priv->owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
+                                     MOUSETWEAKS_DBUS_NAME,
+                                     G_BUS_NAME_OWNER_FLAGS_NONE,
+                                     mt_service_bus_acquired,
+                                     NULL, NULL,
+                                     service, NULL);
+    priv->ispec = g_dbus_node_info_new_for_xml (introspection_xml, &error);
+    if (error)
+    {
+        g_warning ("%s\n", error->message);
+        g_error_free (error);
+    }
+}
 
-    g_type_class_add_private (klass, sizeof (MtServicePrivate));
+static void
+mt_service_set_property (GObject      *object,
+                         guint         prop_id,
+                         const GValue *value,
+                         GParamSpec   *pspec)
+{
+    MtService *service = MT_SERVICE (object);
 
-    dbus_g_object_type_install_info (MT_TYPE_SERVICE,
-				     &dbus_glib_mt_service_object_info);
+    switch (prop_id)
+    {
+        case PROP_CLICK_TYPE:
+            service->priv->click_type = g_value_get_int (value);
+            break;
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
 }
 
 static void
-mt_service_init (MtService *service)
+mt_service_get_property (GObject    *object,
+                         guint       prop_id,
+                         GValue     *value,
+                         GParamSpec *pspec)
 {
-    service->priv = G_TYPE_INSTANCE_GET_PRIVATE (service,
-						 MT_TYPE_SERVICE,
-						 MtServicePrivate);
-    mt_service_register (service);
+    MtService *service = MT_SERVICE (object);
+
+    switch (prop_id)
+    {
+        case PROP_CLICK_TYPE:
+            g_value_set_int (value, service->priv->click_type);
+            break;
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
 }
 
 static void
 mt_service_dispose (GObject *object)
 {
-    g_signal_emit (object, signals[STATUS_CHANGED], 0, FALSE);
+    MtServicePrivate *priv = MT_SERVICE (object)->priv;
+
+    if (priv->owner_id)
+    {
+        g_bus_unown_name (priv->owner_id);
+        priv->owner_id = 0;
+    }
+
+    if (priv->ispec)
+    {
+        g_dbus_node_info_unref (priv->ispec);
+        priv->ispec = NULL;
+    }
 
     G_OBJECT_CLASS (mt_service_parent_class)->dispose (object);
 }
 
 static void
-mt_service_register (MtService *service)
+mt_service_class_init (MtServiceClass *klass)
 {
-    DBusGConnection *bus;
-    DBusGProxy *proxy;
-    GError *error = NULL;
-    guint result;
+    GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+    object_class->get_property = mt_service_get_property;
+    object_class->set_property = mt_service_set_property;
+    object_class->dispose = mt_service_dispose;
+
+    g_object_class_install_property (object_class, PROP_CLICK_TYPE,
+        g_param_spec_int ("click-type", "Click type",
+                          "The currently active click type",
+                          0, 3, DWELL_CLICK_TYPE_SINGLE,
+                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
-    bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
-    if (bus == NULL) {
-	g_warning ("Unable to connect to session bus: %s", error->message);
-	g_error_free (error);
-	return;
+    g_type_class_add_private (klass, sizeof (MtServicePrivate));
+}
+
+static GVariant *
+handle_get_property (GDBusConnection *connection,
+                     const gchar     *sender,
+                     const gchar     *path,
+                     const gchar     *interface,
+                     const gchar     *property,
+                     GError         **error,
+                     MtService       *service)
+{
+    GVariant *ret = NULL;
+
+    if (g_strcmp0 (property, "ClickType") == 0)
+    {
+        ret = g_variant_new_int32 (service->priv->click_type);
+    }
+    return ret;
+}
+
+static gboolean
+handle_set_property (GDBusConnection *connection,
+                     const gchar     *sender,
+                     const gchar     *path,
+                     const gchar     *interface,
+                     const gchar     *property,
+                     GVariant        *value,
+                     GError         **error,
+                     MtService       *service)
+{
+    if (g_strcmp0 (property, "ClickType") == 0)
+    {
+        mt_service_set_click_type (service, g_variant_get_int32 (value));
     }
+    return TRUE;
+}
+
+static const GDBusInterfaceVTable interface_vtable =
+{
+    (GDBusInterfaceMethodCallFunc) NULL,
+    (GDBusInterfaceGetPropertyFunc) handle_get_property,
+    (GDBusInterfaceSetPropertyFunc) handle_set_property
+};
 
-    proxy = dbus_g_proxy_new_for_name (bus,
-				       DBUS_SERVICE_DBUS,
-				       DBUS_PATH_DBUS,
-				       DBUS_INTERFACE_DBUS);
-
-    if (!dbus_g_proxy_call (proxy, "RequestName", &error,
-			    G_TYPE_STRING, MOUSETWEAKS_DBUS_SERVICE,
-			    G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
-			    G_TYPE_INVALID,
-			    G_TYPE_UINT, &result,
-			    G_TYPE_INVALID)) {
-	g_warning ("Unable to acquire name: %s", error->message);
-	g_error_free (error);
-	g_object_unref (proxy);
-	return;
+static void
+emit_property_changed (GObject         *object,
+                       GParamSpec      *pspec,
+                       GDBusConnection *connection)
+{
+    MtService *service = MT_SERVICE (object);
+    GError *error = NULL;
+    GVariantBuilder builder, inv_builder;
+    GVariant *prop_v;
+
+    g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
+    g_variant_builder_init (&inv_builder, G_VARIANT_TYPE ("as"));
+
+    if (g_strcmp0 (pspec->name, "click-type") == 0)
+    {
+        g_variant_builder_add (&builder, "{sv}", "ClickType",
+                               g_variant_new_int32 (service->priv->click_type));
     }
 
-    g_object_unref (proxy);
+    prop_v = g_variant_new ("(sa{sv}as)",
+                            MOUSETWEAKS_DBUS_IFACE,
+                            &builder, &inv_builder);
 
-    if (result == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
-	dbus_g_connection_register_g_object (bus,
-					     MOUSETWEAKS_DBUS_PATH,
-					     G_OBJECT (service));
-    else
-	g_warning ("DBus: Not primary name owner.");
+    if (!g_dbus_connection_emit_signal (connection, NULL,
+                                        MOUSETWEAKS_DBUS_PATH,
+                                        "org.freedesktop.DBus.Properties",
+                                        "PropertiesChanged",
+                                        prop_v, &error))
+    {
+        g_warning ("%s\n", error->message);
+        g_error_free (error);
+    }
 }
 
-static MtService *
-mt_service_new (void)
+static void
+mt_service_bus_acquired (GDBusConnection *connection,
+                         const gchar     *name,
+                         gpointer         data)
 {
-    return g_object_new (MT_TYPE_SERVICE, NULL);
+    MtService *service = data;
+
+    if (service->priv->ispec)
+    {
+        GError *error = NULL;
+
+        g_dbus_connection_register_object (connection,
+                                           MOUSETWEAKS_DBUS_PATH,
+                                           service->priv->ispec->interfaces[0],
+                                           &interface_vtable,
+                                           service, NULL, &error);
+        if (error)
+        {
+            g_warning ("%s", error->message);
+            g_error_free (error);
+        }
+
+        g_signal_connect (service, "notify",
+                          G_CALLBACK (emit_property_changed), connection);
+    }
 }
 
 MtService *
@@ -141,33 +255,26 @@ mt_service_get_default (void)
 {
     static MtService *service = NULL;
 
-    if (!service) {
-	service = mt_service_new ();
-	g_signal_emit (service, signals[STATUS_CHANGED], 0, TRUE);
-    }
+    if (!service)
+        service = g_object_new (MT_TYPE_SERVICE, NULL);
 
     return service;
 }
 
-gboolean
-mt_service_set_clicktype (MtService *service,
-			  guint      clicktype,
-			  GError   **error)
+void
+mt_service_set_click_type (MtService  *service,
+                           MtClickType type)
 {
-    g_return_val_if_fail (MT_IS_SERVICE (service), FALSE);
-
-    service->priv->clicktype = clicktype;
+    g_return_if_fail (MT_IS_SERVICE (service));
 
-    g_signal_emit (service,
-		   signals[CLICKTYPE_CHANGED],
-		   0, service->priv->clicktype);
-    return TRUE;
+    service->priv->click_type = type;
+    g_object_notify (G_OBJECT (service), "click-type");
 }
 
-guint
-mt_service_get_clicktype (MtService *service)
+MtClickType
+mt_service_get_click_type (MtService *service)
 {
-    g_return_val_if_fail (MT_IS_SERVICE (service), 0);
+    g_return_val_if_fail (MT_IS_SERVICE (service), -1);
 
-    return service->priv->clicktype;
+    return service->priv->click_type;
 }
diff --git a/src/mt-service.h b/src/mt-service.h
index 602824f..b67ea46 100644
--- a/src/mt-service.h
+++ b/src/mt-service.h
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2007-2009 Gerd Kohlberger <lowfi chello at>
+ * Copyright © 2007-2010 Gerd Kohlberger <gerdko gmail com>
  *
  * This file is part of Mousetweaks.
  *
@@ -22,30 +22,31 @@
 
 #include <glib-object.h>
 
+#include "mt-common.h"
+
 G_BEGIN_DECLS
 
-#define MT_TYPE_SERVICE         (mt_service_get_type ())
-#define MT_SERVICE(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), MT_TYPE_SERVICE, MtService))
-#define MT_SERVICE_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST ((k), MT_TYPE_SERVICE, MtServiceClass))
-#define MT_IS_SERVICE(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), MT_TYPE_SERVICE))
-#define MT_IS_SERVICE_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), MT_TYPE_SERVICE))
-#define MT_SERVICE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MT_TYPE_SERVICE, MtServiceClass))
+#define MT_TYPE_SERVICE  (mt_service_get_type ())
+#define MT_SERVICE(o)    (G_TYPE_CHECK_INSTANCE_CAST ((o), MT_TYPE_SERVICE, MtService))
+#define MT_IS_SERVICE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MT_TYPE_SERVICE))
 
 typedef struct _MtService        MtService;
 typedef GObjectClass             MtServiceClass;
 typedef struct _MtServicePrivate MtServicePrivate;
 
-struct _MtService {
+struct _MtService
+{
     GObject           parent;
     MtServicePrivate *priv;
 };
 
-GType       mt_service_get_type      (void) G_GNUC_CONST;
-MtService * mt_service_get_default   (void);
-gboolean    mt_service_set_clicktype (MtService *service,
-				      guint      clicktype,
-				      GError   **error);
-guint       mt_service_get_clicktype (MtService *service);
+GType             mt_service_get_type          (void) G_GNUC_CONST;
+
+MtService *       mt_service_get_default       (void);
+
+MtClickType       mt_service_get_click_type    (MtService  *service);
+void              mt_service_set_click_type    (MtService  *service,
+                                                MtClickType type);
 
 G_END_DECLS
 



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