[gnome-settings-daemon] common: Make GsdDeviceMapper use devices from GsdDeviceManager



commit 9e17dced137ff2fd05ed97c82e90a7725e9f82d7
Author: Carlos Garnacho <carlosg gnome org>
Date:   Wed Dec 17 14:19:52 2014 +0100

    common: Make GsdDeviceMapper use devices from GsdDeviceManager
    
    This has brought some changes in the xrandr and wacom modules, which now
    use GsdDevice for device mapping.
    
    The use of GdkDevices and XInput properties remain for these settings not
    yet in the gsettings-desktop-schemas peripherals settings in the wacom case,
    and those bits have been made to run only on X11 as expected.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=742593

 plugins/common/gsd-device-mapper.c  |  315 +++++------------------------------
 plugins/common/gsd-device-mapper.h  |   15 +-
 plugins/wacom/gsd-wacom-device.c    |   20 ++-
 plugins/wacom/gsd-wacom-manager.c   |  160 +++++++++++-------
 plugins/xrandr/gsd-xrandr-manager.c |   33 ++--
 5 files changed, 177 insertions(+), 366 deletions(-)
---
diff --git a/plugins/common/gsd-device-mapper.c b/plugins/common/gsd-device-mapper.c
index 41cd0f5..d5ba6ec 100644
--- a/plugins/common/gsd-device-mapper.c
+++ b/plugins/common/gsd-device-mapper.c
@@ -27,6 +27,7 @@
 #include <libwacom/libwacom.h>
 #endif
 
+#include "gsd-device-manager.h"
 #include "gsd-device-mapper.h"
 #include "gsd-input-helper.h"
 #include "gsd-enums.h"
@@ -36,7 +37,6 @@ typedef struct _GsdOutputInfo GsdOutputInfo;
 typedef struct _MappingHelper MappingHelper;
 typedef struct _DeviceMapHelper DeviceMapHelper;
 
-#define NUM_ELEMS_MATRIX 9
 #define KEY_DISPLAY  "display"
 #define KEY_ROTATION "rotation"
 
@@ -44,10 +44,9 @@ typedef enum {
        GSD_INPUT_IS_SYSTEM_INTEGRATED = 1 << 0, /* eg. laptop tablets/touchscreens */
        GSD_INPUT_IS_SCREEN_INTEGRATED = 1 << 1, /* eg. Wacom Cintiq devices */
        GSD_INPUT_IS_TOUCH             = 1 << 2, /* touch device, either touchscreen or tablet */
-       GSD_INPUT_IS_PEN               = 1 << 3, /* pen device, either touchscreen or tablet */
-       GSD_INPUT_IS_ERASER            = 1 << 4, /* eraser device, either touchscreen or tablet */
-       GSD_INPUT_IS_PAD               = 1 << 5, /* pad device, most usually in tablets */
-       GSD_INPUT_IS_CURSOR            = 1 << 6  /* pointer-like device in tablets */
+       GSD_INPUT_IS_PEN               = 1 << 3, /* tablet pen */
+       GSD_INPUT_IS_PAD               = 1 << 4, /* pad device, most usually in tablets */
+       GSD_INPUT_IS_CURSOR            = 1 << 5  /* pointer-like device in tablets */
 } GsdInputCapabilityFlags;
 
 typedef enum {
@@ -60,7 +59,7 @@ typedef enum {
 } GsdOutputPriority;
 
 struct _GsdInputInfo {
-       GdkDevice *device;
+       GsdDevice *device;
        GSettings *settings;
        GsdDeviceMapper *mapper;
        GsdOutputInfo *output;
@@ -89,7 +88,7 @@ struct _GsdDeviceMapper {
        GObject parent_instance;
        GdkScreen *screen;
        GnomeRRScreen *rr_screen;
-       GHashTable *input_devices; /* GdkDevice -> GsdInputInfo */
+       GHashTable *input_devices; /* GsdDevice -> GsdInputInfo */
        GHashTable *output_devices; /* GnomeRROutput -> GsdOutputInfo */
 #if HAVE_WACOM
        WacomDeviceDatabase *wacom_db;
@@ -100,18 +99,6 @@ struct _GsdDeviceMapperClass {
        GObjectClass parent_class;
 };
 
-/* Array order matches GsdWacomRotation */
-struct {
-       GnomeRRRotation rotation;
-       /* Coordinate Transformation Matrix */
-       gfloat matrix[NUM_ELEMS_MATRIX];
-} rotation_matrices[] = {
-       { GNOME_RR_ROTATION_0, { 1, 0, 0, 0, 1, 0, 0, 0, 1 } },
-       { GNOME_RR_ROTATION_90, { 0, -1, 1, 1, 0, 0, 0, 0, 1 } },
-       { GNOME_RR_ROTATION_270, { 0, 1, 0, -1, 0, 1, 0,  0, 1 } },
-       { GNOME_RR_ROTATION_180, { -1, 0, 1, 0, -1, 1, 0, 0, 1 } }
-};
-
 enum {
        PROP_0,
        PROP_SCREEN
@@ -131,60 +118,6 @@ static guint signals[N_SIGNALS] = { 0 };
 
 G_DEFINE_TYPE (GsdDeviceMapper, gsd_device_mapper, G_TYPE_OBJECT)
 
-static XDevice *
-open_device (GdkDevice *device)
-{
-       XDevice *xdev;
-       int id;
-
-       id = gdk_x11_device_get_id (device);
-
-       gdk_error_trap_push ();
-       xdev = XOpenDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), id);
-       if (gdk_error_trap_pop () || (xdev == NULL))
-               return NULL;
-
-       return xdev;
-}
-
-static gboolean
-device_apply_property (GdkDevice      *device,
-                      PropertyHelper *property)
-{
-       gboolean retval;
-       XDevice *xdev;
-
-       xdev = open_device (device);
-
-       if (!xdev)
-               return FALSE;
-
-       retval = device_set_property (xdev, gdk_device_get_name (device), property);
-       xdevice_close (xdev);
-       return retval;
-}
-
-static gboolean
-device_set_matrix (GdkDevice *device,
-                  gfloat     matrix[NUM_ELEMS_MATRIX])
-{
-       PropertyHelper property = {
-               .name = "Coordinate Transformation Matrix",
-               .nitems = 9,
-               .format = 32,
-               .type = gdk_x11_get_xatom_by_name ("FLOAT"),
-               .data.i = (int *) matrix,
-       };
-
-       g_debug ("Setting '%s' matrix to:\n %f,%f,%f,\n %f,%f,%f,\n %f,%f,%f",
-                gdk_device_get_name (device),
-                matrix[0], matrix[1], matrix[2],
-                matrix[3], matrix[4], matrix[5],
-                matrix[6], matrix[7], matrix[8]);
-
-       return device_apply_property (device, &property);
-}
-
 /* Finds an output which matches the given EDID information. Any NULL
  * parameter will be interpreted to match any value. */
 static GnomeRROutput *
@@ -343,7 +276,7 @@ input_info_guess_candidates (GsdInputInfo  *input,
        gchar **split;
        gint i;
 
-       name = gdk_device_get_name (input->device);
+       name = gsd_device_get_name (input->device);
 
        if (input->capabilities & GSD_INPUT_IS_SCREEN_INTEGRATED) {
                outputs[GSD_PRIO_MATCH_SIZE] = input_info_find_size_match (input, input->mapper->rr_screen);
@@ -525,75 +458,6 @@ input_info_get_output (GsdInputInfo *input)
        return NULL;
 }
 
-static void
-init_device_rotation_matrix (GsdWacomRotation rotation,
-                            float            matrix[NUM_ELEMS_MATRIX])
-{
-        memcpy (matrix, rotation_matrices[rotation].matrix,
-                sizeof (rotation_matrices[rotation].matrix));
-}
-
-static void
-init_output_rotation_matrix (GnomeRRRotation rotation,
-                            float           matrix[NUM_ELEMS_MATRIX])
-{
-       guint i;
-
-       for (i = 0; i < G_N_ELEMENTS (rotation_matrices); i++) {
-               if (rotation_matrices[i].rotation != rotation)
-                       continue;
-
-               memcpy (matrix, rotation_matrices[i].matrix, sizeof (rotation_matrices[i].matrix));
-               return;
-       }
-
-       /* We know nothing about this rotation */
-       init_device_rotation_matrix (GSD_WACOM_ROTATION_NONE, matrix);
-}
-
-static void
-multiply_matrix (float a[NUM_ELEMS_MATRIX],
-                float b[NUM_ELEMS_MATRIX],
-                float res[NUM_ELEMS_MATRIX])
-{
-       float result[NUM_ELEMS_MATRIX];
-
-       result[0] = a[0] * b[0] + a[1] * b[3] + a[2] * b[6];
-       result[1] = a[0] * b[1] + a[1] * b[4] + a[2] * b[7];
-       result[2] = a[0] * b[2] + a[1] * b[5] + a[2] * b[8];
-       result[3] = a[3] * b[0] + a[4] * b[3] + a[5] * b[6];
-       result[4] = a[3] * b[1] + a[4] * b[4] + a[5] * b[7];
-       result[5] = a[3] * b[2] + a[4] * b[5] + a[5] * b[8];
-       result[6] = a[6] * b[0] + a[7] * b[3] + a[8] * b[6];
-       result[7] = a[6] * b[1] + a[7] * b[4] + a[8] * b[7];
-       result[8] = a[6] * b[2] + a[7] * b[5] + a[8] * b[8];
-
-       memcpy (res, result, sizeof (result));
-}
-
-static void
-calculate_viewport_matrix (const GdkRectangle mapped,
-                          const GdkRectangle desktop,
-                          float              viewport[NUM_ELEMS_MATRIX])
-{
-       float x_scale = (float) mapped.x / desktop.width;
-       float y_scale = (float) mapped.y / desktop.height;
-       float width_scale  = (float) mapped.width / desktop.width;
-       float height_scale = (float) mapped.height / desktop.height;
-
-       viewport[0] = width_scale;
-       viewport[1] = 0.0f;
-       viewport[2] = x_scale;
-
-       viewport[3] = 0.0f;
-       viewport[4] = height_scale;
-       viewport[5] = y_scale;
-
-       viewport[6] = 0.0f;
-       viewport[7] = 0.0f;
-       viewport[8] = 1.0f;
-}
-
 static gint
 monitor_for_output (GnomeRROutput *output)
 {
@@ -637,15 +501,15 @@ input_info_find_size_match (GsdInputInfo  *input,
 
        g_return_val_if_fail (rr_screen != NULL, NULL);
 
-       if (!xdevice_get_dimensions (gdk_x11_device_get_id (input->device),
-                                    &input_width, &input_height))
+       if (!gsd_device_get_dimensions (input->device,
+                                       &input_width, &input_height))
                return NULL;
 
        /* Restrict the matches to be below a narrow percentage */
        min_width_diff = min_height_diff = 0.05;
 
        g_debug ("Input device '%s' has %dx%d mm",
-                gdk_device_get_name (input->device), input_width, input_height);
+                gsd_device_get_name (input->device), input_width, input_height);
 
        outputs = gnome_rr_screen_list_outputs (rr_screen);
 
@@ -682,81 +546,16 @@ input_info_find_size_match (GsdInputInfo  *input,
 }
 
 static void
-input_info_get_matrix (GsdInputInfo *input,
-                      float         matrix[NUM_ELEMS_MATRIX])
-{
-       GsdOutputInfo *output;
-       GnomeRRCrtc *crtc;
-
-       output = input_info_get_output (input);
-       if (output)
-               crtc = gnome_rr_output_get_crtc (output->output);
-
-       if (!output || !crtc) {
-               init_output_rotation_matrix (GNOME_RR_ROTATION_0, matrix);
-       } else {
-               GdkScreen *screen = gdk_screen_get_default ();
-               float viewport[NUM_ELEMS_MATRIX];
-               float output_rot[NUM_ELEMS_MATRIX];
-               GdkRectangle display, desktop = { 0 };
-               GnomeRRRotation rotation;
-               int monitor;
-
-               g_debug ("Mapping '%s' to output '%s'",
-                        gdk_device_get_name (input->device),
-                        gnome_rr_output_get_name (output->output));
-
-               rotation = gnome_rr_crtc_get_current_rotation (crtc);
-               init_output_rotation_matrix (rotation, output_rot);
-
-               desktop.width = gdk_screen_get_width (screen);
-               desktop.height = gdk_screen_get_height (screen);
-
-               monitor = monitor_for_output (output->output);
-               gdk_screen_get_monitor_geometry (screen, monitor, &display);
-               calculate_viewport_matrix (display, desktop, viewport);
-
-               multiply_matrix (viewport, output_rot, matrix);
-       }
-
-       /* Apply device rotation after output rotation */
-       if (input->settings &&
-           (input->capabilities &
-            (GSD_INPUT_IS_SYSTEM_INTEGRATED | GSD_INPUT_IS_SCREEN_INTEGRATED)) == 0) {
-               gint rotation;
-
-               rotation = g_settings_get_enum (input->settings, KEY_ROTATION);
-
-               if (rotation > 0) {
-                       float device_rot[NUM_ELEMS_MATRIX];
-
-                       g_debug ("Applying device rotation %d to '%s'",
-                                rotation, gdk_device_get_name (input->device));
-
-                       init_device_rotation_matrix (rotation, device_rot);
-                       multiply_matrix (matrix, device_rot, matrix);
-               }
-       }
-}
-
-static void
 input_info_remap (GsdInputInfo *input)
 {
-       float matrix[NUM_ELEMS_MATRIX] = { 0 };
+       GsdOutputInfo *output;
 
        if (input->capabilities & GSD_INPUT_IS_PAD)
                return;
 
-       input_info_get_matrix (input, matrix);
-
-       g_debug ("About to remap device '%s'",
-                gdk_device_get_name (input->device));
-
-       if (!device_set_matrix (input->device, matrix)) {
-               g_warning ("Failed to map device '%s'",
-                          gdk_device_get_name (input->device));
-       }
-
+       output = input_info_get_output (input);
+       settings_set_display (gsd_device_get_settings (input->device),
+                             output ? output->output : NULL);
        g_signal_emit (input->mapper, signals[DEVICE_CHANGED], 0, input->device);
 }
 
@@ -852,41 +651,16 @@ mapper_recalculate_input (GsdDeviceMapper *mapper,
        mapping_helper_free (helper);
 }
 
-static gboolean
-input_info_update_capabilities_from_tool_type (GsdInputInfo *info)
-{
-       const char *tool_type;
-       int deviceid;
-
-       deviceid = gdk_x11_device_get_id (info->device);
-       tool_type = xdevice_get_wacom_tool_type (deviceid);
-
-       if (!tool_type)
-               return FALSE;
-
-       if (g_str_equal (tool_type, "STYLUS"))
-               info->capabilities |= GSD_INPUT_IS_PEN;
-       else if (g_str_equal (tool_type, "ERASER"))
-               info->capabilities |= GSD_INPUT_IS_ERASER;
-       else if (g_str_equal (tool_type, "PAD"))
-               info->capabilities |= GSD_INPUT_IS_PAD;
-       else if (g_str_equal (tool_type, "CURSOR"))
-               info->capabilities |= GSD_INPUT_IS_CURSOR;
-       else
-               return FALSE;
-
-       return TRUE;
-}
-
 static void
 input_info_update_capabilities (GsdInputInfo *info)
 {
 #if HAVE_WACOM
        WacomDevice *wacom_device;
-       gchar *devpath;
+       const gchar *devpath;
+       GsdDeviceType type;
 
        info->capabilities = 0;
-       devpath = xdevice_get_device_node (gdk_x11_device_get_id (info->device));
+       devpath = gsd_device_get_device_file (info->device);
        wacom_device = libwacom_new_from_path (info->mapper->wacom_db, devpath,
                                               WFALLBACK_GENERIC, NULL);
 
@@ -903,25 +677,17 @@ input_info_update_capabilities (GsdInputInfo *info)
 
                libwacom_destroy (wacom_device);
        }
-
-       g_free (devpath);
 #else
        info->capabilities = 0;
 #endif /* HAVE_WACOM */
 
-       if (!input_info_update_capabilities_from_tool_type (info)) {
-               GdkInputSource source;
-
-               /* Fallback to GdkInputSource */
-               source = gdk_device_get_source (info->device);
+       /* Fallback to GsdDeviceType */
+       type = gsd_device_get_device_type (info->device);
 
-               if (source == GDK_SOURCE_TOUCHSCREEN)
-                       info->capabilities |= GSD_INPUT_IS_TOUCH | GSD_INPUT_IS_SCREEN_INTEGRATED;
-               else if (source == GDK_SOURCE_PEN)
-                       info->capabilities |= GSD_INPUT_IS_PEN;
-               else if (source == GDK_SOURCE_ERASER)
-                       info->capabilities |= GSD_INPUT_IS_ERASER;
-       }
+       if (type & GSD_DEVICE_TYPE_TOUCHSCREEN)
+               info->capabilities |= GSD_INPUT_IS_TOUCH | GSD_INPUT_IS_SCREEN_INTEGRATED;
+       else if (type & GSD_DEVICE_TYPE_TABLET)
+               info->capabilities |= GSD_INPUT_IS_PEN;
 }
 
 static void
@@ -966,15 +732,14 @@ device_settings_changed_cb (GSettings      *settings,
 }
 
 static GsdInputInfo *
-input_info_new (GdkDevice      *device,
-               GSettings       *settings,
+input_info_new (GsdDevice      *device,
                GsdDeviceMapper *mapper)
 {
        GsdInputInfo *info;
 
        info = g_new0 (GsdInputInfo, 1);
        info->device = device;
-       info->settings = (settings) ? g_object_ref (settings) : NULL;
+       info->settings = gsd_device_get_settings (device);;
        info->mapper = mapper;
 
        if (info->settings) {
@@ -1182,7 +947,7 @@ gsd_device_mapper_class_init (GsdDeviceMapperClass *klass)
                              GSD_TYPE_DEVICE_MAPPER,
                              G_SIGNAL_RUN_LAST, 0,
                              NULL, NULL, NULL,
-                             G_TYPE_NONE, 1, GDK_TYPE_DEVICE);
+                             G_TYPE_NONE, 1, GSD_TYPE_DEVICE);
 }
 
 static void
@@ -1217,41 +982,39 @@ gsd_device_mapper_get (void)
 
 void
 gsd_device_mapper_add_input (GsdDeviceMapper *mapper,
-                            GdkDevice       *device,
-                            GSettings       *settings)
+                            GsdDevice       *device)
 {
        GsdInputInfo *info;
 
        g_return_if_fail (mapper != NULL);
-       g_return_if_fail (GDK_IS_DEVICE (device));
-       g_return_if_fail (!settings || G_IS_SETTINGS (settings));
+       g_return_if_fail (device != NULL);
 
        if (g_hash_table_contains (mapper->input_devices, device))
                return;
 
-       info = input_info_new (device, settings, mapper);
+       info = input_info_new (device, mapper);
        g_hash_table_insert (mapper->input_devices, device, info);
 }
 
 void
 gsd_device_mapper_remove_input (GsdDeviceMapper *mapper,
-                               GdkDevice       *device)
+                               GsdDevice       *device)
 {
        g_return_if_fail (mapper != NULL);
-       g_return_if_fail (GDK_IS_DEVICE (device));
+       g_return_if_fail (device != NULL);
 
        g_hash_table_remove (mapper->input_devices, device);
 }
 
 GnomeRROutput *
 gsd_device_mapper_get_device_output (GsdDeviceMapper *mapper,
-                                    GdkDevice       *device)
+                                    GsdDevice       *device)
 {
        GsdOutputInfo *output;
        GsdInputInfo *input;
 
        g_return_val_if_fail (mapper != NULL, NULL);
-       g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
+       g_return_val_if_fail (device != NULL, NULL);
 
        input = g_hash_table_lookup (mapper->input_devices, device);
        output = input_info_get_output (input);
@@ -1264,13 +1027,13 @@ gsd_device_mapper_get_device_output (GsdDeviceMapper *mapper,
 
 gint
 gsd_device_mapper_get_device_monitor (GsdDeviceMapper *mapper,
-                                     GdkDevice       *device)
+                                     GsdDevice       *device)
 {
        GsdOutputInfo *output;
        GsdInputInfo *input;
 
        g_return_val_if_fail (GSD_IS_DEVICE_MAPPER (mapper), -1);
-       g_return_val_if_fail (GDK_IS_DEVICE (device), -1);
+       g_return_val_if_fail (GSD_IS_DEVICE (device), -1);
 
        input = g_hash_table_lookup (mapper->input_devices, device);
 
@@ -1287,14 +1050,14 @@ gsd_device_mapper_get_device_monitor (GsdDeviceMapper *mapper,
 
 void
 gsd_device_mapper_set_device_output (GsdDeviceMapper *mapper,
-                                    GdkDevice       *device,
+                                    GsdDevice       *device,
                                     GnomeRROutput   *output)
 {
        GsdInputInfo *input_info;
        GsdOutputInfo *output_info;
 
        g_return_if_fail (mapper != NULL);
-       g_return_if_fail (GDK_IS_DEVICE (device));
+       g_return_if_fail (GSD_IS_DEVICE (device));
 
        input_info = g_hash_table_lookup (mapper->input_devices, device);
        output_info = g_hash_table_lookup (mapper->output_devices, output);
@@ -1308,13 +1071,13 @@ gsd_device_mapper_set_device_output (GsdDeviceMapper *mapper,
 
 void
 gsd_device_mapper_set_device_monitor (GsdDeviceMapper *mapper,
-                                     GdkDevice       *device,
+                                     GsdDevice       *device,
                                      gint             monitor_num)
 {
        GnomeRROutput *output;
 
        g_return_if_fail (GSD_IS_DEVICE_MAPPER (mapper));
-       g_return_if_fail (GDK_IS_DEVICE (device));
+       g_return_if_fail (GSD_IS_DEVICE (device));
 
        output = monitor_to_output (mapper, monitor_num);
        gsd_device_mapper_set_device_output (mapper, device, output);
diff --git a/plugins/common/gsd-device-mapper.h b/plugins/common/gsd-device-mapper.h
index cce4b60..a5d1e56 100644
--- a/plugins/common/gsd-device-mapper.h
+++ b/plugins/common/gsd-device-mapper.h
@@ -25,6 +25,8 @@
 #undef GNOME_DESKTOP_USE_UNSTABLE_API
 #include <gdk/gdk.h>
 
+#include "gsd-device-manager.h"
+
 G_BEGIN_DECLS
 
 #define GSD_TYPE_DEVICE_MAPPER        (gsd_device_mapper_get_type ())
@@ -41,26 +43,25 @@ GType                 gsd_device_mapper_get_type          (void) G_GNUC_CONST;
 GsdDeviceMapper * gsd_device_mapper_get                      (void);
 
 void             gsd_device_mapper_add_input         (GsdDeviceMapper *mapper,
-                                                      GdkDevice       *device,
-                                                      GSettings       *settings);
+                                                      GsdDevice       *device);
 void             gsd_device_mapper_remove_input      (GsdDeviceMapper *mapper,
-                                                      GdkDevice       *device);
+                                                      GsdDevice       *device);
 void             gsd_device_mapper_add_output        (GsdDeviceMapper *mapper,
                                                       GnomeRROutput   *output);
 void             gsd_device_mapper_remove_output     (GsdDeviceMapper *mapper,
                                                       GnomeRROutput   *output);
 
 GnomeRROutput  * gsd_device_mapper_get_device_output (GsdDeviceMapper *mapper,
-                                                      GdkDevice       *device);
+                                                      GsdDevice       *device);
 
 void             gsd_device_mapper_set_device_output (GsdDeviceMapper *mapper,
-                                                      GdkDevice       *device,
+                                                      GsdDevice       *device,
                                                       GnomeRROutput   *output);
 
 gint             gsd_device_mapper_get_device_monitor (GsdDeviceMapper *mapper,
-                                                       GdkDevice       *device);
+                                                       GsdDevice       *device);
 void             gsd_device_mapper_set_device_monitor (GsdDeviceMapper *mapper,
-                                                       GdkDevice       *device,
+                                                       GsdDevice       *device,
                                                        gint             monitor_num);
 
 G_END_DECLS
diff --git a/plugins/wacom/gsd-wacom-device.c b/plugins/wacom/gsd-wacom-device.c
index 1b18143..8df9c64 100644
--- a/plugins/wacom/gsd-wacom-device.c
+++ b/plugins/wacom/gsd-wacom-device.c
@@ -34,9 +34,12 @@
 #include <X11/extensions/XInput2.h>
 
 #include "gsd-input-helper.h"
+#include "gsd-device-mapper.h"
 
 #include "gsd-enums.h"
 #include "gsd-wacom-device.h"
+#include "gsd-device-manager.h"
+#include "gsd-device-manager-x11.h"
 
 #define GSD_WACOM_STYLUS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GSD_TYPE_WACOM_STYLUS, 
GsdWacomStylusPrivate))
 
@@ -890,25 +893,24 @@ gsd_wacom_device_get_display_matrix (GsdWacomDevice *device, float matrix[NUM_EL
 GsdWacomRotation
 gsd_wacom_device_get_display_rotation (GsdWacomDevice *device)
 {
-       GError *error = NULL;
-       GnomeRRScreen *rr_screen;
        GnomeRROutput *rr_output;
        GnomeRRRotation rotation = GNOME_RR_ROTATION_0;
+       GsdDevice *gsd_device;
 
-       rr_screen = gnome_rr_screen_new (gdk_screen_get_default (), &error);
-       if (rr_screen == NULL) {
-               g_warning ("Failed to create GnomeRRScreen: %s", error->message);
-               g_error_free (error);
+       gsd_device = gsd_x11_device_manager_lookup_gdk_device (GSD_X11_DEVICE_MANAGER (gsd_device_manager_get 
()),
+                                                              device->priv->gdk_device);
+
+       if (!gsd_device)
                return GSD_WACOM_ROTATION_NONE;
-       }
 
-       rr_output = find_output (rr_screen, device);
+       rr_output = gsd_device_mapper_get_device_output (gsd_device_mapper_get (),
+                                                        gsd_device);
+
        if (rr_output) {
                GnomeRRCrtc *crtc = gnome_rr_output_get_crtc (rr_output);
                if (crtc)
                        rotation = gnome_rr_crtc_get_current_rotation (crtc);
        }
-       g_object_unref (rr_screen);
 
        return get_rotation_wacom (rotation);
 }
diff --git a/plugins/wacom/gsd-wacom-manager.c b/plugins/wacom/gsd-wacom-manager.c
index 786a601..458afbb 100644
--- a/plugins/wacom/gsd-wacom-manager.c
+++ b/plugins/wacom/gsd-wacom-manager.c
@@ -54,6 +54,8 @@
 #include "gsd-wacom-osd-window.h"
 #include "gsd-shell-helper.h"
 #include "gsd-device-mapper.h"
+#include "gsd-device-manager.h"
+#include "gsd-device-manager-x11.h"
 
 #define GSD_WACOM_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GSD_TYPE_WACOM_MANAGER, 
GsdWacomManagerPrivate))
 
@@ -107,7 +109,7 @@ typedef struct
 struct GsdWacomManagerPrivate
 {
         guint start_idle_id;
-        GdkDeviceManager *device_manager;
+        GsdDeviceManager *device_manager;
         guint device_added_id;
         guint device_removed_id;
         GHashTable *devices; /* key = GdkDevice, value = GsdWacomDevice */
@@ -430,6 +432,8 @@ set_keep_aspect (GsdWacomDevice *device,
        guint i;
        GdkDevice *gdk_device;
        GsdDeviceMapper *mapper;
+        GsdDeviceManager *device_manager;
+        GsdDevice *gsd_device;
        gint *area;
        gint monitor = GSD_WACOM_SET_ALL_MONITORS;
        GsdWacomRotation rotation;
@@ -466,8 +470,12 @@ set_keep_aspect (GsdWacomDevice *device,
 
        /* Get corresponding monitor size */
        mapper = gsd_device_mapper_get ();
+       device_manager = gsd_device_manager_get ();
        g_object_get (device, "gdk-device", &gdk_device, NULL);
-       monitor = gsd_device_mapper_get_device_monitor (mapper, gdk_device);
+       gsd_device = gsd_x11_device_manager_lookup_gdk_device (GSD_X11_DEVICE_MANAGER (device_manager),
+                                                              gdk_device);
+       monitor = gsd_device_mapper_get_device_monitor (mapper, gsd_device);
+       g_object_unref (gdk_device);
 
        /* Adjust area to match the monitor aspect ratio */
        g_debug ("Initial device area: (%d,%d) (%d,%d)", area[0], area[1], area[2], area[3]);
@@ -1026,9 +1034,8 @@ notify_unknown_device (GsdWacomManager *manager, const gchar *device_name)
 }
 
 static void
-device_added_cb (GdkDeviceManager *device_manager,
-                 GdkDevice        *gdk_device,
-                 GsdWacomManager  *manager)
+gsd_wacom_manager_add_gdk_device (GsdWacomManager *manager,
+                                  GdkDevice       *gdk_device)
 {
        GsdWacomDevice *device;
        GSettings *settings;
@@ -1066,16 +1073,6 @@ device_added_cb (GdkDeviceManager *device_manager,
        g_signal_connect (G_OBJECT (settings), "changed",
                          G_CALLBACK (wacom_settings_changed), device);
 
-       /* Map devices, the xrandr module handles touchscreens in general, so bypass these here */
-       if (type == WACOM_TYPE_PAD ||
-           type == WACOM_TYPE_CURSOR ||
-           type == WACOM_TYPE_STYLUS ||
-           type == WACOM_TYPE_ERASER ||
-           (type == WACOM_TYPE_TOUCH && !gsd_wacom_device_is_screen_tablet (device))) {
-               gsd_device_mapper_add_input (manager->priv->device_mapper,
-                                            gdk_device, settings);
-       }
-
        if (type == WACOM_TYPE_STYLUS || type == WACOM_TYPE_ERASER) {
                GList *styli, *l;
 
@@ -1101,17 +1098,39 @@ device_added_cb (GdkDeviceManager *device_manager,
 }
 
 static void
-device_removed_cb (GdkDeviceManager *device_manager,
-                   GdkDevice        *gdk_device,
-                   GsdWacomManager  *manager)
+device_added_cb (GsdDeviceManager *device_manager,
+                 GsdDevice        *gsd_device,
+                 GsdWacomManager  *manager)
+{
+       GdkDevice **devices;
+       GsdDeviceType device_type;
+       guint i, n_gdk_devices;
+
+       device_type = gsd_device_get_device_type (gsd_device);
+
+       if (device_type & GSD_DEVICE_TYPE_TABLET) {
+               gsd_device_mapper_add_input (manager->priv->device_mapper,
+                                            gsd_device);
+       }
+
+       if (gnome_settings_is_wayland ())
+           return;
+
+       devices = gsd_x11_device_manager_get_gdk_devices (GSD_X11_DEVICE_MANAGER (device_manager),
+                                                         gsd_device, &n_gdk_devices);
+
+       for (i = 0; i < n_gdk_devices; i++)
+               gsd_wacom_manager_add_gdk_device (manager, devices[i]);
+}
+
+static void
+gsd_wacom_manager_remove_gdk_device (GsdWacomManager *manager,
+                                     GdkDevice       *gdk_device)
 {
        g_debug ("Removing device '%s' from known devices list",
                 gdk_device_get_name (gdk_device));
        g_hash_table_remove (manager->priv->devices, gdk_device);
 
-        gsd_device_mapper_remove_input (manager->priv->device_mapper,
-                                        gdk_device);
-
        /* Enable this chunk of code if you want to valgrind
         * test-wacom. It will exit when there are no Wacom devices left */
 #if 0
@@ -1120,6 +1139,24 @@ device_removed_cb (GdkDeviceManager *device_manager,
 #endif
 }
 
+static void
+device_removed_cb (GsdDeviceManager *device_manager,
+                   GsdDevice        *gsd_device,
+                   GsdWacomManager  *manager)
+{
+       GdkDevice **devices;
+       guint i, n_gdk_devices;
+
+       gsd_device_mapper_remove_input (manager->priv->device_mapper,
+                                       gsd_device);
+
+       devices = gsd_x11_device_manager_get_gdk_devices (GSD_X11_DEVICE_MANAGER (device_manager),
+                                                         gsd_device, &n_gdk_devices);
+
+       for (i = 0; i < n_gdk_devices; i++)
+               gsd_wacom_manager_remove_gdk_device (manager, devices[i]);
+}
+
 static GsdWacomDevice *
 device_id_to_device (GsdWacomManager *manager,
                     int              deviceid)
@@ -1285,6 +1322,7 @@ switch_monitor (GsdWacomManager *manager,
 {
        gint current_monitor, n_monitors;
         GdkDevice *gdk_device;
+        GsdDevice *gsd_device;
 
        /* We dont; do that for screen tablets, sorry... */
        if (gsd_wacom_device_is_screen_tablet (device))
@@ -1297,9 +1335,12 @@ switch_monitor (GsdWacomManager *manager,
                return;
 
         g_object_get (device, "gdk-device", &gdk_device, NULL);
+        gsd_device = gsd_x11_device_manager_lookup_gdk_device (GSD_X11_DEVICE_MANAGER 
(gsd_device_manager_get ()),
+                                                               gdk_device);
+        g_object_unref (gdk_device);
         current_monitor =
                 gsd_device_mapper_get_device_monitor (manager->priv->device_mapper,
-                                                      gdk_device);
+                                                      gsd_device);
 
        /* Select next monitor */
        current_monitor++;
@@ -1308,7 +1349,7 @@ switch_monitor (GsdWacomManager *manager,
                current_monitor = 0;
 
         gsd_device_mapper_set_device_monitor (manager->priv->device_mapper,
-                                              gdk_device, current_monitor);
+                                              gsd_device, current_monitor);
 }
 
 static void
@@ -1316,12 +1357,15 @@ notify_osd_for_device (GsdWacomManager *manager,
                        GsdWacomDevice  *device)
 {
         GdkDevice *gdk_device;
+        GsdDevice *gsd_device;
         GdkScreen *screen;
         gint monitor_num;
 
         g_object_get (device, "gdk-device", &gdk_device, NULL);
+        gsd_device = gsd_x11_device_manager_lookup_gdk_device (GSD_X11_DEVICE_MANAGER 
(gsd_device_manager_get ()),
+                                                               gdk_device);
         monitor_num = gsd_device_mapper_get_device_monitor (manager->priv->device_mapper,
-                                                            gdk_device);
+                                                            gsd_device);
 
         if (monitor_num == GSD_WACOM_SET_ALL_MONITORS)
                 return;
@@ -1477,12 +1521,9 @@ filter_button_events (XEvent          *xevent,
 static void
 set_devicepresence_handler (GsdWacomManager *manager)
 {
-        GdkDeviceManager *device_manager;
-
-        device_manager = gdk_display_get_device_manager (gdk_display_get_default ());
-        if (device_manager == NULL)
-                return;
+        GsdDeviceManager *device_manager;
 
+        device_manager = gsd_device_manager_get ();
         manager->priv->device_added_id = g_signal_connect (G_OBJECT (device_manager), "device-added",
                                                            G_CALLBACK (device_added_cb), manager);
         manager->priv->device_removed_id = g_signal_connect (G_OBJECT (device_manager), "device-removed",
@@ -1511,15 +1552,18 @@ gsd_wacom_manager_idle_cb (GsdWacomManager *manager)
 
         set_devicepresence_handler (manager);
 
-        devices = gdk_device_manager_list_devices (manager->priv->device_manager, GDK_DEVICE_TYPE_SLAVE);
+        devices = gsd_device_manager_list_devices (manager->priv->device_manager,
+                                                   GSD_DEVICE_TYPE_TABLET);
         for (l = devices; l ; l = l->next)
                device_added_cb (manager->priv->device_manager, l->data, manager);
         g_list_free (devices);
 
-        /* Start filtering the button events */
-        gdk_window_add_filter (gdk_screen_get_root_window (manager->priv->screen),
-                               (GdkFilterFunc) filter_button_events,
-                               manager);
+        if (!gnome_settings_is_wayland ()) {
+                /* Start filtering the button events */
+                gdk_window_add_filter (gdk_screen_get_root_window (manager->priv->screen),
+                                       (GdkFilterFunc) filter_button_events,
+                                       manager);
+        }
 
         gnome_settings_profile_end (NULL);
 
@@ -1536,6 +1580,7 @@ check_need_for_calibration (GsdWacomDevice  *device)
         gint width, height;
         GdkScreen *screen;
         GdkDevice *gdk_device;
+        GsdDevice *gsd_device;
         GsdDeviceMapper *mapper;
         gint monitor;
         GdkRectangle geometry;
@@ -1548,7 +1593,10 @@ check_need_for_calibration (GsdWacomDevice  *device)
         screen = gdk_screen_get_default ();
         mapper = gsd_device_mapper_get ();
         g_object_get (device, "gdk-device", &gdk_device, NULL);
-        monitor = gsd_device_mapper_get_device_monitor (mapper, gdk_device);
+        gsd_device = gsd_x11_device_manager_lookup_gdk_device (GSD_X11_DEVICE_MANAGER 
(gsd_device_manager_get ()),
+                                                               gdk_device);
+        monitor = gsd_device_mapper_get_device_monitor (mapper, gsd_device);
+        g_object_unref (gdk_device);
 
        if (monitor < 0) {
                geometry.width = gdk_screen_get_width (screen);
@@ -1884,7 +1932,9 @@ void
 gsd_wacom_manager_stop (GsdWacomManager *manager)
 {
         GsdWacomManagerPrivate *p = manager->priv;
-        GList *l;
+        GsdWacomDeviceType type;
+        GsdWacomDevice *device;
+        GHashTableIter iter;
 
         g_debug ("Stopping wacom manager");
 
@@ -1895,37 +1945,31 @@ gsd_wacom_manager_stop (GsdWacomManager *manager)
         }
 
         if (p->device_manager != NULL) {
-                GList *devices;
-
                 g_signal_handler_disconnect (p->device_manager, p->device_added_id);
                 g_signal_handler_disconnect (p->device_manager, p->device_removed_id);
+                p->device_manager = NULL;
+        }
 
-                devices = gdk_device_manager_list_devices (p->device_manager, GDK_DEVICE_TYPE_SLAVE);
-                for (l = devices; l != NULL; l = l->next) {
-                        GsdWacomDeviceType type;
-                        GsdWacomDevice *device;
-                        int id;
-
-                        id = gdk_x11_device_get_id (l->data);
-                        device = device_id_to_device (manager, id);
-
-                        if (!device)
-                                continue;
+        if (!gnome_settings_is_wayland ()) {
+                g_hash_table_iter_init (&iter, manager->priv->devices);
 
+                while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &device)) {
                         type = gsd_wacom_device_get_device_type (device);
-
-                        if (type == WACOM_TYPE_PAD)
-                                grab_button (id, FALSE, manager->priv->screen);
+                        if (type == WACOM_TYPE_PAD) {
+                                GdkDevice *gdk_device;
+
+                                g_object_get (device, "gdk-device", &gdk_device, NULL);
+                                grab_button (gdk_x11_device_get_id (gdk_device),
+                                             FALSE, manager->priv->screen);
+                                g_object_unref (gdk_device);
+                        }
                 }
-                g_list_free (devices);
 
-                p->device_manager = NULL;
+                gdk_window_remove_filter (gdk_screen_get_root_window (p->screen),
+                                          (GdkFilterFunc) filter_button_events,
+                                          manager);
         }
 
-        gdk_window_remove_filter (gdk_screen_get_root_window (p->screen),
-                                  (GdkFilterFunc) filter_button_events,
-                                  manager);
-
         g_signal_handlers_disconnect_by_func (p->rr_screen, on_screen_changed_cb, manager);
 
         g_clear_pointer (&p->osd_window, gtk_widget_destroy);
diff --git a/plugins/xrandr/gsd-xrandr-manager.c b/plugins/xrandr/gsd-xrandr-manager.c
index 67b3ed1..33d1ed1 100644
--- a/plugins/xrandr/gsd-xrandr-manager.c
+++ b/plugins/xrandr/gsd-xrandr-manager.c
@@ -101,7 +101,7 @@ struct GsdXrandrManagerPrivate {
         GCancellable    *bus_cancellable;
 
         GsdDeviceMapper  *device_mapper;
-        GdkDeviceManager *device_manager;
+        GsdDeviceManager *device_manager;
         guint             device_added_id;
         guint             device_removed_id;
 
@@ -1158,21 +1158,27 @@ get_allowed_rotations_for_output (GnomeRRConfig *config,
 
 static void
 manager_device_added (GsdXrandrManager *manager,
-                      GdkDevice        *device)
+                      GsdDevice        *device)
 {
-        if (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER ||
-            gdk_device_get_source (device) != GDK_SOURCE_TOUCHSCREEN)
+        GsdDeviceType type;
+
+        type = gsd_device_get_device_type (device);
+
+        if ((type & GSD_DEVICE_TYPE_TOUCHSCREEN) == 0)
                 return;
 
-        gsd_device_mapper_add_input (manager->priv->device_mapper, device, NULL);
+        gsd_device_mapper_add_input (manager->priv->device_mapper, device);
 }
 
 static void
 manager_device_removed (GsdXrandrManager *manager,
-                        GdkDevice        *device)
+                        GsdDevice        *device)
 {
-        if (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER ||
-            gdk_device_get_source (device) != GDK_SOURCE_TOUCHSCREEN)
+        GsdDeviceType type;
+
+        type = gsd_device_get_device_type (device);
+
+        if ((type & GSD_DEVICE_TYPE_TOUCHSCREEN) == 0)
                 return;
 
         gsd_device_mapper_remove_input (manager->priv->device_mapper, device);
@@ -1181,15 +1187,10 @@ manager_device_removed (GsdXrandrManager *manager,
 static void
 manager_init_devices (GsdXrandrManager *manager)
 {
-        GdkDisplay *display;
         GList *devices, *d;
-        GdkScreen *screen;
-
-        screen = gdk_screen_get_default ();
-        display = gdk_screen_get_display (screen);
 
         manager->priv->device_mapper = gsd_device_mapper_get ();
-        manager->priv->device_manager = gdk_display_get_device_manager (display);
+        manager->priv->device_manager = gsd_device_manager_get ();
         manager->priv->device_added_id =
                 g_signal_connect_swapped (manager->priv->device_manager, "device-added",
                                           G_CALLBACK (manager_device_added), manager);
@@ -1197,8 +1198,8 @@ manager_init_devices (GsdXrandrManager *manager)
                 g_signal_connect_swapped (manager->priv->device_manager, "device-removed",
                                   G_CALLBACK (manager_device_removed), manager);
 
-        devices = gdk_device_manager_list_devices (manager->priv->device_manager,
-                                                   GDK_DEVICE_TYPE_SLAVE);
+        devices = gsd_device_manager_list_devices (manager->priv->device_manager,
+                                                   GSD_DEVICE_TYPE_TOUCHSCREEN);
 
         for (d = devices; d; d = d->next)
                 manager_device_added (manager, d->data);


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