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

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