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

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