[gnome-settings-daemon] common: Add GsdDeviceManager



commit 0d1a4550a55a9847782db925c7e40d251ce40f07
Author: Carlos Garnacho <carlosg gnome org>
Date:   Wed Dec 17 13:48:52 2014 +0100

    common: Add GsdDeviceManager
    
    This is a GUdev-based object that keeps track of the plugged in input
    devices. Meant to be used across g-s-d wherever some knowledge about these
    must remain.
    
    On X11, some API is still offered to access the gdk/X devices (which may not
    map 1:1 with the udev devices).
    
    For both platforms, the relevant /org/gnome/desktop/peripherals/... settings
    can be accessed, in order to interoperate with the settings that mutter
    applies on libinput devices.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=742593

 configure.ac                        |    4 +-
 plugins/common/Makefile.am          |   26 +++
 plugins/common/gsd-device-manager.c |  392 +++++++++++++++++++++++++++++++++++
 plugins/common/gsd-device-manager.h |  108 ++++++++++
 4 files changed, 528 insertions(+), 2 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 84b9266..9a90ed9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -56,7 +56,7 @@ LIBRSVG_REQUIRED_VERSION=2.36.2
 PANGO_REQUIRED_VERSION=1.20.0
 POLKIT_REQUIRED_VERSION=0.103
 UPOWER_REQUIRED_VERSION=0.99.0
-GSETTINGS_DESKTOP_SCHEMAS_REQUIRED_VERSION=3.9.91
+GSETTINGS_DESKTOP_SCHEMAS_REQUIRED_VERSION=3.15.4
 GEOCODE_GLIB_REQUIRED_VERSION=3.10.0
 GEOCLUE_REQUIRED_VERSION=2.1.2
 NM_REQUIRED_VERSION=0.9.9.1
@@ -164,7 +164,7 @@ dnl ---------------------------------------------------------------------------
 dnl - common
 dnl ---------------------------------------------------------------------------
 
-PKG_CHECK_MODULES(COMMON, x11 kbproto xi)
+PKG_CHECK_MODULES(COMMON, x11 kbproto xi $GUDEV_PKG)
 
 dnl ---------------------------------------------------------------------------
 dnl - XTest
diff --git a/plugins/common/Makefile.am b/plugins/common/Makefile.am
index fd2a7af..efa4cff 100644
--- a/plugins/common/Makefile.am
+++ b/plugins/common/Makefile.am
@@ -2,7 +2,30 @@ plugin_name = common
 
 noinst_LTLIBRARIES = libcommon.la
 
+GSD_COMMON_ENUM_FILES = gsd-common-enums.c gsd-common-enums.h
+
+gsd-common-enums.h: gsd-device-manager.h Makefile
+       $(AM_V_GEN)($(GLIB_MKENUMS) \
+                       --fhead "#ifndef GSD_COMMON_ENUMS_H\n#define GSD_COMMON_ENUMS_H\n\n#include 
<glib-object.h>\n\nG_BEGIN_DECLS\n" \
+                       --fprod "/* enumerations from \"@filename \" */\n" \
+                       --vhead "GType @enum_name _get_type (void) G_GNUC_CONST;\n#define GSD_TYPE_ 
ENUMSHORT@ (@enum_name _get_type())\n" \
+                       --ftail "G_END_DECLS\n\n#endif /* !GSD_COMMON_ENUMS_H */" \
+               $(srcdir)/gsd-device-manager.h > $@)
+
+gsd-common-enums.c: gsd-device-manager.h Makefile gsd-common-enums.h
+       $(AM_V_GEN)($(GLIB_MKENUMS) \
+                       --fhead "#include \"gsd-device-manager.h\"\n#include \"gsd-common-enums.h\"\n" \
+                       --fprod "\n/* enumerations from \"@filename \" */" \
+                       --vhead "GType\n enum_name@_get_type (void)\n{\n  static GType etype = 0;\n  if 
(etype == 0) {\n    static const G Type@Value values[] = {" \
+                       --vprod "      { @VALUENAME@, \"@VALUENAME \", \"@valuenick \" }," \
+                       --vtail "      { 0, NULL, NULL }\n    };\n    etype = g_ type@_register_static 
(\"@EnumName \", values);\n  }\n  return etype;\n}\n" \
+               $(srcdir)/gsd-device-manager.h > $@)
+
 libcommon_la_SOURCES = \
+       gsd-common-enums.c      \
+       gsd-common-enums.h      \
+       gsd-device-manager.c    \
+       gsd-device-manager.h    \
        gsd-device-mapper.c     \
        gsd-device-mapper.h     \
        gsd-keygrab.c           \
@@ -51,3 +74,6 @@ scriptsdir = $(datadir)/gnome-settings-daemon- GSD_API_VERSION@
 scripts_DATA = input-device-example.sh
 
 EXTRA_DIST = $(scripts_DATA) test-plugin.h
+
+CLEANFILES = \
+       $(GSD_COMMON_ENUM_FILES)
diff --git a/plugins/common/gsd-device-manager.c b/plugins/common/gsd-device-manager.c
new file mode 100644
index 0000000..0732419
--- /dev/null
+++ b/plugins/common/gsd-device-manager.c
@@ -0,0 +1,392 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2014 Red Hat
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Author: Carlos Garnacho <carlosg gnome org>
+ */
+
+#include "config.h"
+
+#include <string.h>
+
+#include "gsd-common-enums.h"
+#include "gnome-settings-bus.h"
+
+typedef struct _GsdDevicePrivate GsdDevicePrivate;
+typedef struct _GsdDeviceManagerPrivate GsdDeviceManagerPrivate;
+
+struct _GsdDevicePrivate
+{
+       gchar *name;
+       gchar *device_file;
+       gchar *vendor_id;
+       gchar *product_id;
+       GsdDeviceType type;
+       guint width;
+       guint height;
+};
+
+enum {
+       PROP_NAME = 1,
+       PROP_DEVICE_FILE,
+       PROP_VENDOR_ID,
+       PROP_PRODUCT_ID,
+       PROP_TYPE,
+       PROP_WIDTH,
+       PROP_HEIGHT
+};
+
+enum {
+       DEVICE_ADDED,
+       DEVICE_REMOVED,
+       N_SIGNALS
+};
+
+static guint signals[N_SIGNALS] = { 0 };
+
+G_DEFINE_TYPE_WITH_PRIVATE (GsdDevice, gsd_device, G_TYPE_OBJECT)
+G_DEFINE_TYPE (GsdDeviceManager, gsd_device_manager, G_TYPE_OBJECT)
+
+static void
+gsd_device_init (GsdDevice *device)
+{
+}
+
+static void
+gsd_device_set_property (GObject      *object,
+                        guint         prop_id,
+                        const GValue *value,
+                        GParamSpec   *pspec)
+{
+       GsdDevicePrivate *priv;
+
+       priv = gsd_device_get_instance_private (GSD_DEVICE (object));
+
+       switch (prop_id) {
+       case PROP_NAME:
+               priv->name = g_value_dup_string (value);
+               break;
+       case PROP_DEVICE_FILE:
+               priv->device_file = g_value_dup_string (value);
+               break;
+       case PROP_VENDOR_ID:
+               priv->vendor_id = g_value_dup_string (value);
+               break;
+       case PROP_PRODUCT_ID:
+               priv->product_id = g_value_dup_string (value);
+               break;
+       case PROP_TYPE:
+               priv->type = g_value_get_flags (value);
+               break;
+       case PROP_WIDTH:
+               priv->width = g_value_get_uint (value);
+               break;
+       case PROP_HEIGHT:
+               priv->height = g_value_get_uint (value);
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+               break;
+       }
+}
+
+static void
+gsd_device_get_property (GObject    *object,
+                        guint       prop_id,
+                        GValue     *value,
+                        GParamSpec *pspec)
+{
+       GsdDevicePrivate *priv;
+
+       priv = gsd_device_get_instance_private (GSD_DEVICE (object));
+
+       switch (prop_id) {
+       case PROP_NAME:
+               g_value_set_string (value, priv->name);
+               break;
+       case PROP_DEVICE_FILE:
+               g_value_set_string (value, priv->device_file);
+               break;
+       case PROP_VENDOR_ID:
+               g_value_set_string (value, priv->vendor_id);
+               break;
+       case PROP_PRODUCT_ID:
+               g_value_set_string (value, priv->product_id);
+               break;
+       case PROP_TYPE:
+               g_value_set_flags (value, priv->type);
+               break;
+       case PROP_WIDTH:
+               g_value_set_uint (value, priv->width);
+               break;
+       case PROP_HEIGHT:
+               g_value_set_uint (value, priv->height);
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+               break;
+       }
+}
+
+static void
+gsd_device_finalize (GObject *object)
+{
+       GsdDevicePrivate *priv;
+
+       priv = gsd_device_get_instance_private (GSD_DEVICE (object));
+
+       g_free (priv->name);
+       g_free (priv->vendor_id);
+       g_free (priv->product_id);
+       g_free (priv->device_file);
+
+       G_OBJECT_CLASS (gsd_device_parent_class)->finalize (object);
+}
+
+static void
+gsd_device_class_init (GsdDeviceClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       object_class->set_property = gsd_device_set_property;
+       object_class->get_property = gsd_device_get_property;
+       object_class->finalize = gsd_device_finalize;
+
+       g_object_class_install_property (object_class,
+                                        PROP_NAME,
+                                        g_param_spec_string ("name",
+                                                             "Name",
+                                                             "Name",
+                                                             NULL,
+                                                             G_PARAM_READWRITE |
+                                                             G_PARAM_CONSTRUCT_ONLY));
+       g_object_class_install_property (object_class,
+                                        PROP_DEVICE_FILE,
+                                        g_param_spec_string ("device-file",
+                                                             "Device file",
+                                                             "Device file",
+                                                             NULL,
+                                                             G_PARAM_READWRITE |
+                                                             G_PARAM_CONSTRUCT_ONLY));
+       g_object_class_install_property (object_class,
+                                        PROP_VENDOR_ID,
+                                        g_param_spec_string ("vendor-id",
+                                                             "Vendor ID",
+                                                             "Vendor ID",
+                                                             NULL,
+                                                             G_PARAM_READWRITE |
+                                                             G_PARAM_CONSTRUCT_ONLY));
+       g_object_class_install_property (object_class,
+                                        PROP_PRODUCT_ID,
+                                        g_param_spec_string ("product-id",
+                                                             "Product ID",
+                                                             "Product ID",
+                                                             NULL,
+                                                             G_PARAM_READWRITE |
+                                                             G_PARAM_CONSTRUCT_ONLY));
+       g_object_class_install_property (object_class,
+                                        PROP_TYPE,
+                                        g_param_spec_flags ("type",
+                                                            "Device type",
+                                                            "Device type",
+                                                            GSD_TYPE_DEVICE_TYPE, 0,
+                                                            G_PARAM_READWRITE |
+                                                            G_PARAM_CONSTRUCT_ONLY));
+       g_object_class_install_property (object_class,
+                                        PROP_WIDTH,
+                                        g_param_spec_uint ("width",
+                                                           "Width",
+                                                           "Width",
+                                                           0, G_MAXUINT, 0,
+                                                           G_PARAM_READWRITE |
+                                                           G_PARAM_CONSTRUCT_ONLY));
+       g_object_class_install_property (object_class,
+                                        PROP_HEIGHT,
+                                        g_param_spec_uint ("height",
+                                                           "Height",
+                                                           "Height",
+                                                           0, G_MAXUINT, 0,
+                                                           G_PARAM_READWRITE |
+                                                           G_PARAM_CONSTRUCT_ONLY));
+}
+
+static void
+gsd_device_manager_class_init (GsdDeviceManagerClass *klass)
+{
+       signals[DEVICE_ADDED] =
+               g_signal_new ("device-added",
+                             GSD_TYPE_DEVICE_MANAGER,
+                             G_SIGNAL_RUN_LAST,
+                             G_STRUCT_OFFSET (GsdDeviceManagerClass, device_added),
+                             NULL, NULL, NULL,
+                             G_TYPE_NONE, 1,
+                             GSD_TYPE_DEVICE | G_SIGNAL_TYPE_STATIC_SCOPE);
+
+       signals[DEVICE_REMOVED] =
+               g_signal_new ("device-removed",
+                             GSD_TYPE_DEVICE_MANAGER,
+                             G_SIGNAL_RUN_LAST,
+                             G_STRUCT_OFFSET (GsdDeviceManagerClass, device_removed),
+                             NULL, NULL, NULL,
+                             G_TYPE_NONE, 1,
+                             GSD_TYPE_DEVICE | G_SIGNAL_TYPE_STATIC_SCOPE);
+}
+
+static void
+gsd_device_manager_init (GsdDeviceManager *manager)
+{
+}
+
+GsdDeviceManager *
+gsd_device_manager_get (void)
+{
+       GsdDeviceManager *manager;
+       GdkScreen *screen;
+
+       screen = gdk_screen_get_default ();
+       g_return_val_if_fail (screen != NULL, NULL);
+
+       manager = g_object_get_data (G_OBJECT (screen), "gsd-device-manager-data");
+
+       if (!manager) {
+       }
+
+       return manager;
+}
+
+GList *
+gsd_device_manager_list_devices (GsdDeviceManager *manager,
+                                GsdDeviceType     type)
+{
+       g_return_val_if_fail (GSD_IS_DEVICE_MANAGER (manager), NULL);
+
+       return GSD_DEVICE_MANAGER_GET_CLASS (manager)->list_devices (manager, type);
+}
+
+GsdDeviceType
+gsd_device_get_device_type (GsdDevice *device)
+{
+       GsdDevicePrivate *priv;
+
+       g_return_val_if_fail (GSD_IS_DEVICE (device), 0);
+
+       priv = gsd_device_get_instance_private (device);
+
+       return priv->type;
+}
+
+void
+gsd_device_get_device_ids (GsdDevice   *device,
+                          const gchar **vendor,
+                          const gchar **product)
+{
+       GsdDevicePrivate *priv;
+
+       g_return_if_fail (GSD_IS_DEVICE (device));
+
+       priv = gsd_device_get_instance_private (device);
+
+       if (vendor)
+               *vendor = priv->vendor_id;
+       if (product)
+               *product = priv->product_id;
+}
+
+GSettings *
+gsd_device_get_settings (GsdDevice *device)
+{
+       const gchar *schema = NULL, *vendor, *product;
+       GSettings *settings;
+       GsdDeviceType type;
+       gchar *path = NULL;
+
+       g_return_val_if_fail (GSD_IS_DEVICE (device), NULL);
+
+       type = gsd_device_get_device_type (device);
+
+       if (type & (GSD_DEVICE_TYPE_TOUCHSCREEN | GSD_DEVICE_TYPE_TABLET)) {
+               gsd_device_get_device_ids (device, &vendor, &product);
+
+               if (type & GSD_DEVICE_TYPE_TOUCHSCREEN) {
+                       schema = "org.gnome.desktop.peripherals.touchscreen";
+                       path = g_strdup_printf ("/org/gnome/desktop/peripherals/touchscreens/%s:%s/",
+                                               vendor, product);
+               } else if (type & GSD_DEVICE_TYPE_TABLET) {
+                       schema = "org.gnome.desktop.peripherals.tablet";
+                       path = g_strdup_printf ("/org/gnome/desktop/peripherals/tablets/%s:%s/",
+                                               vendor, product);
+               }
+       } else if (type & (GSD_DEVICE_TYPE_MOUSE | GSD_DEVICE_TYPE_TOUCHPAD)) {
+               schema = "org.gnome.desktop.peripherals.mouse";
+       } else if (type & GSD_DEVICE_TYPE_KEYBOARD) {
+               schema = "org.gnome.desktop.peripherals.keyboard";
+       } else {
+               return NULL;
+       }
+
+       if (path) {
+               settings = g_settings_new_with_path (schema, path);
+               g_free (path);
+       } else {
+               settings = g_settings_new (schema);
+       }
+
+       return settings;
+}
+
+const gchar *
+gsd_device_get_name (GsdDevice *device)
+{
+       GsdDevicePrivate *priv;
+
+       g_return_val_if_fail (GSD_IS_DEVICE (device), NULL);
+
+       priv = gsd_device_get_instance_private (device);
+
+       return priv->name;
+}
+
+const gchar *
+gsd_device_get_device_file (GsdDevice *device)
+{
+       GsdDevicePrivate *priv;
+
+       g_return_val_if_fail (GSD_IS_DEVICE (device), NULL);
+
+       priv = gsd_device_get_instance_private (device);
+
+       return priv->device_file;
+}
+
+gboolean
+gsd_device_get_dimensions (GsdDevice *device,
+                          guint     *width,
+                          guint     *height)
+{
+       GsdDevicePrivate *priv;
+
+       g_return_val_if_fail (GSD_IS_DEVICE (device), FALSE);
+
+       priv = gsd_device_get_instance_private (device);
+
+       if (width)
+               *width = priv->width;
+       if (height)
+               *height = priv->height;
+
+       return priv->width > 0 && priv->height > 0;
+}
diff --git a/plugins/common/gsd-device-manager.h b/plugins/common/gsd-device-manager.h
new file mode 100644
index 0000000..61e5727
--- /dev/null
+++ b/plugins/common/gsd-device-manager.h
@@ -0,0 +1,108 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2014 Carlos Garnacho <carlosg gnome org>
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GSD_DEVICE_MANAGER_H__
+#define __GSD_DEVICE_MANAGER_H__
+
+#include <glib-object.h>
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define GSD_TYPE_DEVICE                (gsd_device_get_type ())
+#define GSD_DEVICE(o)          (G_TYPE_CHECK_INSTANCE_CAST ((o), GSD_TYPE_DEVICE, GsdDevice))
+#define GSD_DEVICE_CLASS(k)    (G_TYPE_CHECK_CLASS_CAST((k), GSD_TYPE_DEVICE, GsdDeviceClass))
+#define GSD_IS_DEVICE(o)       (G_TYPE_CHECK_INSTANCE_TYPE ((o), GSD_TYPE_DEVICE))
+#define GSD_IS_DEVICE_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GSD_TYPE_DEVICE))
+#define GSD_DEVICE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GSD_TYPE_DEVICE, GsdDeviceClass))
+
+#define GSD_TYPE_DEVICE_MANAGER                (gsd_device_manager_get_type ())
+#define GSD_DEVICE_MANAGER(o)          (G_TYPE_CHECK_INSTANCE_CAST ((o), GSD_TYPE_DEVICE_MANAGER, 
GsdDeviceManager))
+#define GSD_DEVICE_MANAGER_CLASS(k)    (G_TYPE_CHECK_CLASS_CAST((k), GSD_TYPE_DEVICE_MANAGER, 
GsdDeviceManagerClass))
+#define GSD_IS_DEVICE_MANAGER(o)       (G_TYPE_CHECK_INSTANCE_TYPE ((o), GSD_TYPE_DEVICE_MANAGER))
+#define GSD_IS_DEVICE_MANAGER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GSD_TYPE_DEVICE_MANAGER))
+#define GSD_DEVICE_MANAGER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GSD_TYPE_DEVICE_MANAGER, 
GsdDeviceManagerClass))
+
+typedef struct _GsdDevice GsdDevice;
+typedef struct _GsdDeviceClass GsdDeviceClass;
+typedef struct _GsdDeviceManager GsdDeviceManager;
+typedef struct _GsdDeviceManagerClass GsdDeviceManagerClass;
+
+typedef enum {
+       GSD_DEVICE_TYPE_MOUSE        = 1 << 0,
+       GSD_DEVICE_TYPE_KEYBOARD     = 1 << 1,
+       GSD_DEVICE_TYPE_TOUCHPAD     = 1 << 2,
+       GSD_DEVICE_TYPE_TABLET       = 1 << 3,
+       GSD_DEVICE_TYPE_TOUCHSCREEN  = 1 << 4
+} GsdDeviceType;
+
+struct _GsdDevice {
+       GObject parent_instance;
+};
+
+struct _GsdDeviceClass {
+       GObjectClass parent_class;
+};
+
+struct _GsdDeviceManager
+{
+       GObject parent_instance;
+};
+
+struct _GsdDeviceManagerClass
+{
+       GObjectClass parent_class;
+
+       GList * (* list_devices) (GsdDeviceManager *manager,
+                                 GsdDeviceType     type);
+
+       void (* device_added)   (GsdDeviceManager *manager,
+                                GsdDevice        *device);
+       void (* device_removed) (GsdDeviceManager *manager,
+                                GsdDevice        *device);
+};
+
+GType             gsd_device_get_type                (void) G_GNUC_CONST;
+GType             gsd_device_manager_get_type        (void) G_GNUC_CONST;
+GsdDeviceManager * gsd_device_manager_get            (void);
+GList *                   gsd_device_manager_list_devices    (GsdDeviceManager *manager,
+                                                      GsdDeviceType     type);
+
+const gchar *     gsd_device_get_name        (GsdDevice  *device);
+GsdDeviceType     gsd_device_get_device_type (GsdDevice  *device);
+void              gsd_device_get_device_ids  (GsdDevice    *device,
+                                              const gchar **vendor,
+                                              const gchar **product);
+GSettings *       gsd_device_get_settings    (GsdDevice  *device);
+
+const gchar *     gsd_device_get_device_file (GsdDevice  *device);
+gboolean          gsd_device_get_dimensions  (GsdDevice  *device,
+                                              guint      *width,
+                                              guint      *height);
+
+#ifdef GDK_WINDOWING_X11
+GdkDevice **      gsd_device_get_gdk_devices (GsdDevice *device,
+                                              guint     *n_gdk_devices);
+GsdDevice *       gsd_device_manager_lookup_gdk_device (GsdDeviceManager *manager,
+                                                        GdkDevice        *gdk_device);
+#endif
+
+G_END_DECLS
+
+#endif /* __GSD_DEVICE_MANAGER_H__ */


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