[gnome-settings-daemon/wip/settings-relocation: 2/12] common: Add udev-based GsdDeviceManager implementation
- From: Carlos Garnacho <carlosg src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-settings-daemon/wip/settings-relocation: 2/12] common: Add udev-based GsdDeviceManager implementation
- Date: Thu, 15 Jan 2015 19:15:51 +0000 (UTC)
commit 2d6870f73572aee17b508bcceaf0eb4d2b0a7174
Author: Carlos Garnacho <carlosg gnome org>
Date: Tue Jan 13 21:23:56 2015 +0100
common: Add udev-based GsdDeviceManager implementation
This will be used on wayland environments in order to figure out
the plugged devices and their capabilities.
https://bugzilla.gnome.org/show_bug.cgi?id=742593
plugins/common/Makefile.am | 2 +
plugins/common/gsd-device-manager-udev.c | 243 ++++++++++++++++++++++++++++++
plugins/common/gsd-device-manager-udev.h | 43 ++++++
plugins/common/gsd-device-manager.c | 10 ++
4 files changed, 298 insertions(+), 0 deletions(-)
---
diff --git a/plugins/common/Makefile.am b/plugins/common/Makefile.am
index efa4cff..ebb4f13 100644
--- a/plugins/common/Makefile.am
+++ b/plugins/common/Makefile.am
@@ -26,6 +26,8 @@ libcommon_la_SOURCES = \
gsd-common-enums.h \
gsd-device-manager.c \
gsd-device-manager.h \
+ gsd-device-manager-udev.c \
+ gsd-device-manager-udev.h \
gsd-device-mapper.c \
gsd-device-mapper.h \
gsd-keygrab.c \
diff --git a/plugins/common/gsd-device-manager-udev.c b/plugins/common/gsd-device-manager-udev.c
new file mode 100644
index 0000000..27a436a
--- /dev/null
+++ b/plugins/common/gsd-device-manager-udev.c
@@ -0,0 +1,243 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2015 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 <gudev/gudev.h>
+
+#include "gsd-device-manager-udev.h"
+
+struct _GsdUdevDeviceManager
+{
+ GsdDeviceManager parent_instance;
+ GHashTable *devices;
+ GUdevClient *udev_client;
+};
+
+struct _GsdUdevDeviceManagerClass
+{
+ GsdDeviceManagerClass parent_class;
+};
+
+G_DEFINE_TYPE (GsdUdevDeviceManager, gsd_udev_device_manager, GSD_TYPE_DEVICE_MANAGER)
+
+/* Index matches GsdDeviceType */
+const gchar *udev_ids[] = {
+ "ID_INPUT_MOUSE",
+ "ID_INPUT_KEYBOARD",
+ "ID_INPUT_TOUCHPAD",
+ "ID_INPUT_TABLET",
+ "ID_INPUT_TOUCHSCREEN"
+};
+
+static GsdDeviceType
+udev_device_get_device_type (GUdevDevice *device)
+{
+ GsdDeviceType type = 0;
+ gint i;
+
+ for (i = 0; i < G_N_ELEMENTS (udev_ids); i++) {
+ if (g_udev_device_get_property_as_boolean (device, udev_ids[i]))
+ type |= (1 << i);
+ }
+
+ return type;
+}
+
+static gboolean
+device_is_evdev (GUdevDevice *device)
+{
+ const gchar *device_file;
+
+ device_file = g_udev_device_get_device_file (device);
+
+ if (!device_file || strstr (device_file, "/event") == NULL)
+ return FALSE;
+
+ return g_udev_device_get_property_as_boolean (device, "ID_INPUT");
+}
+
+static GsdDevice *
+create_device (GUdevDevice *udev_device)
+{
+ const gchar *vendor, *product, *name;
+ guint width, height;
+ GUdevDevice *parent;
+ GsdDevice *device;
+
+ parent = g_udev_device_get_parent (udev_device);
+ g_assert (parent != NULL);
+
+ name = g_udev_device_get_sysfs_attr (parent, "name");
+ vendor = g_udev_device_get_property (udev_device, "ID_VENDOR_ID");
+ product = g_udev_device_get_property (udev_device, "ID_MODEL_ID");
+ width = g_udev_device_get_property_as_int (udev_device, "ID_INPUT_WIDTH_MM");
+ height = g_udev_device_get_property_as_int (udev_device, "ID_INPUT_WIDTH_MM");
+
+ device = g_object_new (GSD_TYPE_DEVICE,
+ "name", name,
+ "device-file", g_udev_device_get_device_file (udev_device),
+ "type", udev_device_get_device_type (udev_device),
+ "vendor-id", vendor,
+ "product-id", product,
+ "width", width,
+ "height", height,
+ NULL);
+
+ g_object_unref (parent);
+
+ return device;
+}
+
+static void
+add_device (GsdUdevDeviceManager *manager,
+ GUdevDevice *udev_device)
+{
+ GUdevDevice *parent;
+ GsdDevice *device;
+
+ parent = g_udev_device_get_parent (udev_device);
+
+ if (!parent)
+ return;
+
+ device = create_device (udev_device);
+ g_hash_table_insert (manager->devices, g_object_ref (udev_device), device);
+ g_signal_emit_by_name (manager, "device-added", device);
+}
+
+static void
+remove_device (GsdUdevDeviceManager *manager,
+ GUdevDevice *udev_device)
+{
+ GsdDevice *device;
+
+ device = g_hash_table_lookup (manager->devices, udev_device);
+
+ if (!device)
+ return;
+
+ g_hash_table_steal (manager->devices, udev_device);
+ g_signal_emit_by_name (manager, "device-removed", device);
+
+ g_object_unref (device);
+ g_object_unref (udev_device);
+}
+
+static void
+udev_event_cb (GUdevClient *client,
+ gchar *action,
+ GUdevDevice *device,
+ GsdUdevDeviceManager *manager)
+{
+ if (!device_is_evdev (device))
+ return;
+
+ if (g_strcmp0 (action, "add") == 0) {
+ add_device (manager, device);
+ } else if (g_strcmp0 (action, "remove") == 0) {
+ remove_device (manager, device);
+ }
+}
+
+static void
+gsd_udev_device_manager_init (GsdUdevDeviceManager *manager)
+{
+ const gchar *subsystems[] = { "input", NULL };
+ GList *devices, *l;
+
+ manager->devices = g_hash_table_new_full (NULL, NULL,
+ (GDestroyNotify) g_object_unref,
+ (GDestroyNotify) g_object_unref);
+
+ manager->udev_client = g_udev_client_new (subsystems);
+ g_signal_connect (manager->udev_client, "uevent",
+ G_CALLBACK (udev_event_cb), manager);
+
+ devices = g_udev_client_query_by_subsystem (manager->udev_client,
+ subsystems[0]);
+
+ for (l = devices; l; l = l->next) {
+ GUdevDevice *device = l->data;
+
+ if (device_is_evdev (device))
+ add_device (manager, device);
+
+ g_object_unref (device);
+ }
+
+ g_list_free (devices);
+}
+
+static void
+gsd_udev_device_manager_finalize (GObject *object)
+{
+ GsdUdevDeviceManager *manager = GSD_UDEV_DEVICE_MANAGER (object);
+
+ g_hash_table_destroy (manager->devices);
+ g_object_unref (manager->udev_client);
+
+ G_OBJECT_CLASS (gsd_udev_device_manager_parent_class)->finalize (object);
+}
+
+static GList *
+gsd_udev_device_manager_list_devices (GsdDeviceManager *manager,
+ GsdDeviceType type)
+{
+ GsdUdevDeviceManager *manager_udev = GSD_UDEV_DEVICE_MANAGER (manager);
+ GsdDeviceType device_type;
+ GList *devices = NULL;
+ GHashTableIter iter;
+ GsdDevice *device;
+
+ g_hash_table_iter_init (&iter, manager_udev->devices);
+
+ while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &device)) {
+ device_type = gsd_device_get_device_type (device);
+
+ {
+ const gchar *v, *p;
+ guint w, h;
+
+ gsd_device_get_dimensions (device, &w, &h);
+ gsd_device_get_device_ids (device, &v, &p);
+
+ g_print (" DEV: %s %d | %s %s | %d %d\n",
+ gsd_device_get_name (device), device_type,
+ v, p, w, h);
+ }
+ if ((device_type & type) == type)
+ devices = g_list_prepend (devices, device);
+ }
+
+ return devices;
+}
+
+static void
+gsd_udev_device_manager_class_init (GsdUdevDeviceManagerClass *klass)
+{
+ GsdDeviceManagerClass *manager_class = GSD_DEVICE_MANAGER_CLASS (klass);
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = gsd_udev_device_manager_finalize;
+ manager_class->list_devices = gsd_udev_device_manager_list_devices;
+}
diff --git a/plugins/common/gsd-device-manager-udev.h b/plugins/common/gsd-device-manager-udev.h
new file mode 100644
index 0000000..2004e97
--- /dev/null
+++ b/plugins/common/gsd-device-manager-udev.h
@@ -0,0 +1,43 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2015 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>
+ */
+
+#ifndef __GSD_UDEV_DEVICE_MANAGER_H__
+#define __GSD_UDEV_DEVICE_MANAGER_H__
+
+#include "gsd-device-manager.h"
+
+G_BEGIN_DECLS
+
+#define GSD_TYPE_UDEV_DEVICE_MANAGER (gsd_udev_device_manager_get_type ())
+#define GSD_UDEV_DEVICE_MANAGER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GSD_TYPE_UDEV_DEVICE_MANAGER,
GsdUdevDeviceManager))
+#define GSD_UDEV_DEVICE_MANAGER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), GSD_TYPE_UDEV_DEVICE_MANAGER,
GsdUdevDeviceManagerClass))
+#define GSD_IS_UDEV_DEVICE_MANAGER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GSD_TYPE_UDEV_DEVICE_MANAGER))
+#define GSD_IS_UDEV_DEVICE_MANAGER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GSD_TYPE_UDEV_DEVICE_MANAGER))
+#define GSD_UDEV_DEVICE_MANAGER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GSD_TYPE_UDEV_DEVICE_MANAGER,
GsdUdevDeviceManagerClass))
+
+typedef struct _GsdUdevDeviceManager GsdUdevDeviceManager;
+typedef struct _GsdUdevDeviceManagerClass GsdUdevDeviceManagerClass;
+
+GType gsd_udev_device_manager_get_type (void) G_GNUC_CONST;
+
+G_END_DECLS
+
+#endif /* __GSD_UDEV_DEVICE_MANAGER_H__ */
diff --git a/plugins/common/gsd-device-manager.c b/plugins/common/gsd-device-manager.c
index 6857670..bccd43f 100644
--- a/plugins/common/gsd-device-manager.c
+++ b/plugins/common/gsd-device-manager.c
@@ -23,6 +23,7 @@
#include <string.h>
+#include "gsd-device-manager-udev.h"
#include "gsd-common-enums.h"
#include "gnome-settings-bus.h"
@@ -262,6 +263,15 @@ gsd_device_manager_get (void)
manager = g_object_get_data (G_OBJECT (screen), "gsd-device-manager-data");
if (!manager) {
+#ifdef HAVE_WAYLAND
+ if (gnome_settings_is_wayland ()) {
+ manager = g_object_new (GSD_TYPE_UDEV_DEVICE_MANAGER,
+ NULL);
+ }
+#endif /* HAVE_WAYLAND */
+
+ g_object_set_data_full (G_OBJECT (screen), "gsd-device-manager-data",
+ manager, (GDestroyNotify) g_object_unref);
}
return manager;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]