Blame SOURCES/0001-backends-x11-Support-synaptics-configuration.patch

657d8e
From 471174ba6cf517baf8ff73e903202e1c73b6ec74 Mon Sep 17 00:00:00 2001
657d8e
From: Carlos Garnacho <carlosg@gnome.org>
657d8e
Date: Thu, 19 Jan 2017 15:03:41 +0100
657d8e
Subject: [PATCH] backends/x11: Support synaptics configuration
657d8e
657d8e
The code is taken mostly as-is from g-s-d, so we can drag the
657d8e
dead horse a bit longer.
657d8e
---
657d8e
 src/backends/x11/meta-input-settings-x11.c | 268 +++++++++++++++++++++
657d8e
 1 file changed, 268 insertions(+)
657d8e
657d8e
diff --git a/src/backends/x11/meta-input-settings-x11.c b/src/backends/x11/meta-input-settings-x11.c
657d8e
index 89f07ee1f..051a1c605 100644
657d8e
--- a/src/backends/x11/meta-input-settings-x11.c
657d8e
+++ b/src/backends/x11/meta-input-settings-x11.c
657d8e
@@ -26,6 +26,7 @@
657d8e
 #include "backends/x11/meta-input-settings-x11.h"
657d8e
 
657d8e
 #include <gdk/gdkx.h>
657d8e
+#include <stdlib.h>
657d8e
 #include <string.h>
657d8e
 #include <X11/Xatom.h>
657d8e
 #include <X11/extensions/XInput2.h>
657d8e
@@ -162,6 +163,180 @@ change_property (ClutterInputDevice *device,
657d8e
   meta_XFree (data_ret);
657d8e
 }
657d8e
 
657d8e
+static gboolean
657d8e
+is_device_synaptics (ClutterInputDevice *device)
657d8e
+{
657d8e
+  guchar *has_setting;
657d8e
+
657d8e
+  /* We just need looking for a synaptics-specific property */
657d8e
+  has_setting = get_property (device, "Synaptics Off", XA_INTEGER, 8, 1);
657d8e
+  if (!has_setting)
657d8e
+    return FALSE;
657d8e
+
657d8e
+  meta_XFree (has_setting);
657d8e
+  return TRUE;
657d8e
+}
657d8e
+
657d8e
+static void
657d8e
+change_synaptics_tap_left_handed (ClutterInputDevice *device,
657d8e
+                                  gboolean            tap_enabled,
657d8e
+                                  gboolean            left_handed)
657d8e
+{
657d8e
+  MetaDisplay *display = meta_get_display ();
657d8e
+  MetaX11Display *x11_display = display ? display->x11_display : NULL;
657d8e
+  MetaBackend *backend = meta_get_backend ();
657d8e
+  Display *xdisplay = meta_backend_x11_get_xdisplay (META_BACKEND_X11 (backend));
657d8e
+  XDevice *xdevice;
657d8e
+  guchar *tap_action, *buttons;
657d8e
+  guint buttons_capacity = 16, n_buttons;
657d8e
+
657d8e
+  xdevice = XOpenDevice(xdisplay, clutter_input_device_get_device_id (device));
657d8e
+  if (!xdevice)
657d8e
+    return;
657d8e
+
657d8e
+  tap_action = get_property (device, "Synaptics Tap Action",
657d8e
+                             XA_INTEGER, 8, 7);
657d8e
+  if (!tap_action)
657d8e
+    goto out;
657d8e
+
657d8e
+  tap_action[4] = tap_enabled ? (left_handed ? 3 : 1) : 0;
657d8e
+  tap_action[5] = tap_enabled ? (left_handed ? 1 : 3) : 0;
657d8e
+  tap_action[6] = tap_enabled ? 2 : 0;
657d8e
+
657d8e
+  change_property (device, "Synaptics Tap Action",
657d8e
+                   XA_INTEGER, 8, tap_action, 7);
657d8e
+  meta_XFree (tap_action);
657d8e
+
657d8e
+  if (x11_display)
657d8e
+    meta_x11_error_trap_push (x11_display);
657d8e
+  buttons = g_new (guchar, buttons_capacity);
657d8e
+  n_buttons = XGetDeviceButtonMapping (xdisplay, xdevice,
657d8e
+                                       buttons, buttons_capacity);
657d8e
+
657d8e
+  while (n_buttons > buttons_capacity)
657d8e
+    {
657d8e
+      buttons_capacity = n_buttons;
657d8e
+      buttons = (guchar *) g_realloc (buttons,
657d8e
+                                      buttons_capacity * sizeof (guchar));
657d8e
+
657d8e
+      n_buttons = XGetDeviceButtonMapping (xdisplay, xdevice,
657d8e
+                                           buttons, buttons_capacity);
657d8e
+    }
657d8e
+
657d8e
+  buttons[0] = left_handed ? 3 : 1;
657d8e
+  buttons[2] = left_handed ? 1 : 3;
657d8e
+  XSetDeviceButtonMapping (xdisplay, xdevice, buttons, n_buttons);
657d8e
+  g_free (buttons);
657d8e
+
657d8e
+  if (x11_display && meta_x11_error_trap_pop_with_return (x11_display))
657d8e
+    {
657d8e
+      g_warning ("Could not set synaptics touchpad left-handed for %s",
657d8e
+                 clutter_input_device_get_device_name (device));
657d8e
+    }
657d8e
+
657d8e
+ out:
657d8e
+  XCloseDevice (xdisplay, xdevice);
657d8e
+}
657d8e
+
657d8e
+static void
657d8e
+change_synaptics_speed (ClutterInputDevice *device,
657d8e
+                        gdouble             speed)
657d8e
+{
657d8e
+  MetaDisplay *display = meta_get_display ();
657d8e
+  MetaX11Display *x11_display = display ? display->x11_display : NULL;
657d8e
+  MetaBackend *backend = meta_get_backend ();
657d8e
+  Display *xdisplay = meta_backend_x11_get_xdisplay (META_BACKEND_X11 (backend));
657d8e
+  XDevice *xdevice;
657d8e
+  XPtrFeedbackControl feedback;
657d8e
+  XFeedbackState *states, *state;
657d8e
+  int i, num_feedbacks, motion_threshold, numerator, denominator;
657d8e
+  gfloat motion_acceleration;
657d8e
+
657d8e
+  xdevice = XOpenDevice(xdisplay, clutter_input_device_get_device_id (device));
657d8e
+  if (!xdevice)
657d8e
+    return;
657d8e
+  /* Get the list of feedbacks for the device */
657d8e
+  states = XGetFeedbackControl (xdisplay, xdevice, &num_feedbacks);
657d8e
+  if (!states)
657d8e
+    return;
657d8e
+
657d8e
+  /* Calculate acceleration and threshold */
657d8e
+  motion_acceleration = (speed + 1) * 5; /* speed is [-1..1], map to [0..10] */
657d8e
+  motion_threshold = CLAMP (10 - floor (motion_acceleration), 1, 10);
657d8e
+
657d8e
+  if (motion_acceleration >= 1.0)
657d8e
+    {
657d8e
+      /* we want to get the acceleration, with a resolution of 0.5
657d8e
+       */
657d8e
+      if ((motion_acceleration - floor (motion_acceleration)) < 0.25)
657d8e
+        {
657d8e
+          numerator = floor (motion_acceleration);
657d8e
+          denominator = 1;
657d8e
+        }
657d8e
+      else if ((motion_acceleration - floor (motion_acceleration)) < 0.5)
657d8e
+        {
657d8e
+          numerator = ceil (2.0 * motion_acceleration);
657d8e
+          denominator = 2;
657d8e
+        }
657d8e
+      else if ((motion_acceleration - floor (motion_acceleration)) < 0.75)
657d8e
+        {
657d8e
+          numerator = floor (2.0 *motion_acceleration);
657d8e
+          denominator = 2;
657d8e
+        }
657d8e
+      else
657d8e
+        {
657d8e
+          numerator = ceil (motion_acceleration);
657d8e
+          denominator = 1;
657d8e
+        }
657d8e
+    }
657d8e
+  else if (motion_acceleration < 1.0 && motion_acceleration > 0)
657d8e
+    {
657d8e
+      /* This we do to 1/10ths */
657d8e
+      numerator = floor (motion_acceleration * 10) + 1;
657d8e
+      denominator= 10;
657d8e
+    }
657d8e
+  else
657d8e
+    {
657d8e
+      numerator = -1;
657d8e
+      denominator = -1;
657d8e
+    }
657d8e
+
657d8e
+  if (x11_display)
657d8e
+    meta_x11_error_trap_push (x11_display);
657d8e
+
657d8e
+  state = (XFeedbackState *) states;
657d8e
+
657d8e
+  for (i = 0; i < num_feedbacks; i++)
657d8e
+    {
657d8e
+      if (state->class == PtrFeedbackClass)
657d8e
+        {
657d8e
+          /* And tell the device */
657d8e
+          feedback.class      = PtrFeedbackClass;
657d8e
+          feedback.length     = sizeof (XPtrFeedbackControl);
657d8e
+          feedback.id         = state->id;
657d8e
+          feedback.threshold  = motion_threshold;
657d8e
+          feedback.accelNum   = numerator;
657d8e
+          feedback.accelDenom = denominator;
657d8e
+
657d8e
+          XChangeFeedbackControl (xdisplay, xdevice,
657d8e
+                                  DvAccelNum | DvAccelDenom | DvThreshold,
657d8e
+                                  (XFeedbackControl *) &feedback);
657d8e
+          break;
657d8e
+        }
657d8e
+
657d8e
+      state = (XFeedbackState *) ((char *) state + state->length);
657d8e
+    }
657d8e
+
657d8e
+  if (x11_display && meta_x11_error_trap_pop_with_return (x11_display))
657d8e
+    {
657d8e
+      g_warning ("Could not set synaptics touchpad acceleration for %s",
657d8e
+                 clutter_input_device_get_device_name (device));
657d8e
+    }
657d8e
+
657d8e
+  XFreeFeedbackList (states);
657d8e
+  XCloseDevice (xdisplay, xdevice);
657d8e
+}
657d8e
+
657d8e
 static void
657d8e
 meta_input_settings_x11_set_send_events (MetaInputSettings        *settings,
657d8e
                                          ClutterInputDevice       *device,
657d8e
@@ -170,6 +345,13 @@ meta_input_settings_x11_set_send_events (MetaInputSettings        *settings,
657d8e
   guchar values[2] = { 0 }; /* disabled, disabled-on-external-mouse */
657d8e
   guchar *available;
657d8e
 
657d8e
+  if (is_device_synaptics (device))
657d8e
+    {
657d8e
+      values[0] = mode != G_DESKTOP_DEVICE_SEND_EVENTS_ENABLED;
657d8e
+      change_property (device, "Synaptics Off", XA_INTEGER, 8, &values, 1);
657d8e
+      return;
657d8e
+    }
657d8e
+
657d8e
   available = get_property (device, "libinput Send Events Modes Available",
657d8e
                             XA_INTEGER, 8, 2);
657d8e
   if (!available)
657d8e
@@ -222,6 +404,12 @@ meta_input_settings_x11_set_speed (MetaInputSettings  *settings,
657d8e
   Display *xdisplay = meta_backend_x11_get_xdisplay (META_BACKEND_X11 (backend));
657d8e
   gfloat value = speed;
657d8e
 
657d8e
+  if (is_device_synaptics (device))
657d8e
+    {
657d8e
+      change_synaptics_speed (device, speed);
657d8e
+      return;
657d8e
+    }
657d8e
+
657d8e
   change_property (device, "libinput Accel Speed",
657d8e
                    XInternAtom (xdisplay, "FLOAT", False),
657d8e
                    32, &value, 1);
657d8e
@@ -248,6 +436,19 @@ meta_input_settings_x11_set_left_handed (MetaInputSettings  *settings,
657d8e
   else
657d8e
     {
657d8e
       value = enabled ? 1 : 0;
657d8e
+
657d8e
+      if (is_device_synaptics (device))
657d8e
+        {
657d8e
+          GSettings *settings;
657d8e
+
657d8e
+          settings = g_settings_new ("org.gnome.desktop.peripherals.touchpad");
657d8e
+          change_synaptics_tap_left_handed (device,
657d8e
+                                            g_settings_get_boolean (settings, "tap-to-click"),
657d8e
+                                            enabled);
657d8e
+          g_object_unref (settings);
657d8e
+          return;
657d8e
+        }
657d8e
+
657d8e
       change_property (device, "libinput Left Handed Enabled",
657d8e
                        XA_INTEGER, 8, &value, 1);
657d8e
     }
657d8e
@@ -271,6 +472,20 @@ meta_input_settings_x11_set_tap_enabled (MetaInputSettings  *settings,
657d8e
 {
657d8e
   guchar value = (enabled) ? 1 : 0;
657d8e
 
657d8e
+  if (is_device_synaptics (device))
657d8e
+    {
657d8e
+      GDesktopTouchpadHandedness handedness;
657d8e
+      GSettings *settings;
657d8e
+
657d8e
+      settings = g_settings_new ("org.gnome.desktop.peripherals.touchpad");
657d8e
+      handedness = g_settings_get_enum (settings, "left-handed");
657d8e
+      g_object_unref (settings);
657d8e
+
657d8e
+      change_synaptics_tap_left_handed (device, enabled,
657d8e
+                                        handedness == G_DESKTOP_TOUCHPAD_HANDEDNESS_LEFT);
657d8e
+      return;
657d8e
+    }
657d8e
+
657d8e
   change_property (device, "libinput Tapping Enabled",
657d8e
                    XA_INTEGER, 8, &value, 1);
657d8e
 }
657d8e
@@ -293,6 +508,27 @@ meta_input_settings_x11_set_invert_scroll (MetaInputSettings  *settings,
657d8e
 {
657d8e
   guchar value = (inverted) ? 1 : 0;
657d8e
 
657d8e
+  if (is_device_synaptics (device))
657d8e
+    {
657d8e
+      gint32 *scrolling_distance;
657d8e
+
657d8e
+      scrolling_distance = get_property (device, "Synaptics Scrolling Distance",
657d8e
+                                         XA_INTEGER, 32, 2);
657d8e
+      if (scrolling_distance)
657d8e
+        {
657d8e
+          scrolling_distance[0] = inverted ?
657d8e
+            -abs (scrolling_distance[0]) : abs (scrolling_distance[0]);
657d8e
+          scrolling_distance[1] = inverted ?
657d8e
+            -abs (scrolling_distance[1]) : abs (scrolling_distance[1]);
657d8e
+
657d8e
+          change_property (device, "Synaptics Scrolling Distance",
657d8e
+                           XA_INTEGER, 32, scrolling_distance, 2);
657d8e
+          meta_XFree (scrolling_distance);
657d8e
+        }
657d8e
+
657d8e
+      return;
657d8e
+    }
657d8e
+
657d8e
   change_property (device, "libinput Natural Scrolling Enabled",
657d8e
                    XA_INTEGER, 8, &value, 1);
657d8e
 }
657d8e
@@ -306,6 +542,22 @@ meta_input_settings_x11_set_edge_scroll (MetaInputSettings            *settings,
657d8e
   guchar *current = NULL;
657d8e
   guchar *available = NULL;
657d8e
 
657d8e
+  if (is_device_synaptics (device))
657d8e
+    {
657d8e
+      current = get_property (device, "Synaptics Edge Scrolling",
657d8e
+                              XA_INTEGER, 8, 3);
657d8e
+      if (current)
657d8e
+        {
657d8e
+          current[0] = !!edge_scroll_enabled;
657d8e
+          current[1] = !!edge_scroll_enabled;
657d8e
+          change_property (device, "Synaptics Edge Scrolling",
657d8e
+                           XA_INTEGER, 8, current, 3);
657d8e
+          meta_XFree (current);
657d8e
+        }
657d8e
+
657d8e
+      return;
657d8e
+    }
657d8e
+
657d8e
   available = get_property (device, "libinput Scroll Methods Available",
657d8e
                             XA_INTEGER, 8, SCROLL_METHOD_NUM_FIELDS);
657d8e
   if (!available || !available[SCROLL_METHOD_FIELD_EDGE])
657d8e
@@ -335,6 +587,22 @@ meta_input_settings_x11_set_two_finger_scroll (MetaInputSettings            *set
657d8e
   guchar *current = NULL;
657d8e
   guchar *available = NULL;
657d8e
 
657d8e
+  if (is_device_synaptics (device))
657d8e
+    {
657d8e
+      current = get_property (device, "Synaptics Two-Finger Scrolling",
657d8e
+                              XA_INTEGER, 8, 2);
657d8e
+      if (current)
657d8e
+        {
657d8e
+          current[0] = !!two_finger_scroll_enabled;
657d8e
+          current[1] = !!two_finger_scroll_enabled;
657d8e
+          change_property (device, "Synaptics Two-Finger Scrolling",
657d8e
+                           XA_INTEGER, 8, current, 2);
657d8e
+          meta_XFree (current);
657d8e
+        }
657d8e
+
657d8e
+      return;
657d8e
+    }
657d8e
+
657d8e
   available = get_property (device, "libinput Scroll Methods Available",
657d8e
                             XA_INTEGER, 8, SCROLL_METHOD_NUM_FIELDS);
657d8e
   if (!available || !available[SCROLL_METHOD_FIELD_2FG])
657d8e
-- 
657d8e
2.21.0
657d8e