kathenas / rpms / mutter

Forked from rpms/mutter 5 years ago
Clone

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

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