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

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