1 /*
2     SPDX-FileCopyrightText: 2013 Alexander Mezin <mezin.alexander@gmail.com>
3     SPDX-FileContributor: 2002-2005, 2007 Peter Osterlund <petero2@telia.com>
4 
5     SPDX-License-Identifier: GPL-2.0-or-later AND LicenseRef-synaptics
6 */
7 
8 #include <QDebug>
9 #include <cmath>
10 
11 #include "synapticstouchpad.h"
12 
13 #include <limits.h>
14 #include <stddef.h>
15 #include <synaptics-properties.h>
16 
17 #define SYN_MAX_BUTTONS 12
18 
19 const struct Parameter synapticsProperties[] = {
20     {"LeftEdge", PT_INT, 0, 10000, SYNAPTICS_PROP_EDGES, 32, 0},
21     {"RightEdge", PT_INT, 0, 10000, SYNAPTICS_PROP_EDGES, 32, 1},
22     {"TopEdge", PT_INT, 0, 10000, SYNAPTICS_PROP_EDGES, 32, 2},
23     {"BottomEdge", PT_INT, 0, 10000, SYNAPTICS_PROP_EDGES, 32, 3},
24     {"FingerLow", PT_INT, 0, 255, SYNAPTICS_PROP_FINGER, 32, 0},
25     {"FingerHigh", PT_INT, 0, 255, SYNAPTICS_PROP_FINGER, 32, 1},
26     {"MaxTapTime", PT_INT, 0, 1000, SYNAPTICS_PROP_TAP_TIME, 32, 0},
27     {"MaxTapMove", PT_INT, 0, 2000, SYNAPTICS_PROP_TAP_MOVE, 32, 0},
28     {"MaxDoubleTapTime", PT_INT, 0, 1000, SYNAPTICS_PROP_TAP_DURATIONS, 32, 1},
29     {"SingleTapTimeout", PT_INT, 0, 1000, SYNAPTICS_PROP_TAP_DURATIONS, 32, 0},
30     {"ClickTime", PT_INT, 0, 1000, SYNAPTICS_PROP_TAP_DURATIONS, 32, 2},
31     {"FastTaps", PT_BOOL, 0, 1, SYNAPTICS_PROP_TAP_FAST, 8, 0},
32     {"EmulateMidButtonTime", PT_INT, 0, 1000, SYNAPTICS_PROP_MIDDLE_TIMEOUT, 32, 0},
33     {"EmulateTwoFingerMinZ", PT_INT, 0, 1000, SYNAPTICS_PROP_TWOFINGER_PRESSURE, 32, 0},
34     {"EmulateTwoFingerMinW", PT_INT, 0, 15, SYNAPTICS_PROP_TWOFINGER_WIDTH, 32, 0},
35     {"VertScrollDelta", PT_INT, -1000, 1000, SYNAPTICS_PROP_SCROLL_DISTANCE, 32, 0},
36     {"HorizScrollDelta", PT_INT, -1000, 1000, SYNAPTICS_PROP_SCROLL_DISTANCE, 32, 1},
37     {"VertEdgeScroll", PT_BOOL, 0, 1, SYNAPTICS_PROP_SCROLL_EDGE, 8, 0},
38     {"HorizEdgeScroll", PT_BOOL, 0, 1, SYNAPTICS_PROP_SCROLL_EDGE, 8, 1},
39     {"CornerCoasting", PT_BOOL, 0, 1, SYNAPTICS_PROP_SCROLL_EDGE, 8, 2},
40     {"VertTwoFingerScroll", PT_BOOL, 0, 1, SYNAPTICS_PROP_SCROLL_TWOFINGER, 8, 0},
41     {"HorizTwoFingerScroll", PT_BOOL, 0, 1, SYNAPTICS_PROP_SCROLL_TWOFINGER, 8, 1},
42     {"MinSpeed", PT_DOUBLE, 0, 255.0, SYNAPTICS_PROP_SPEED, 0, /*float */ 0},
43     {"MaxSpeed", PT_DOUBLE, 0, 255.0, SYNAPTICS_PROP_SPEED, 0, /*float */ 1},
44     {"AccelFactor", PT_DOUBLE, 0, 1.0, SYNAPTICS_PROP_SPEED, 0, /*float */ 2},
45     /*{"TouchpadOff",           PT_INT,    0, 2,     SYNAPTICS_PROP_OFF,        8,  0},*/
46     {"LockedDrags", PT_BOOL, 0, 1, SYNAPTICS_PROP_LOCKED_DRAGS, 8, 0},
47     {"LockedDragTimeout", PT_INT, 0, 30000, SYNAPTICS_PROP_LOCKED_DRAGS_TIMEOUT, 32, 0},
48     {"RTCornerButton", PT_INT, 0, SYN_MAX_BUTTONS, SYNAPTICS_PROP_TAP_ACTION, 8, 0},
49     {"RBCornerButton", PT_INT, 0, SYN_MAX_BUTTONS, SYNAPTICS_PROP_TAP_ACTION, 8, 1},
50     {"LTCornerButton", PT_INT, 0, SYN_MAX_BUTTONS, SYNAPTICS_PROP_TAP_ACTION, 8, 2},
51     {"LBCornerButton", PT_INT, 0, SYN_MAX_BUTTONS, SYNAPTICS_PROP_TAP_ACTION, 8, 3},
52     {"OneFingerTapButton", PT_INT, 0, SYN_MAX_BUTTONS, SYNAPTICS_PROP_TAP_ACTION, 8, 4},
53     {"TwoFingerTapButton", PT_INT, 0, SYN_MAX_BUTTONS, SYNAPTICS_PROP_TAP_ACTION, 8, 5},
54     {"ThreeFingerTapButton", PT_INT, 0, SYN_MAX_BUTTONS, SYNAPTICS_PROP_TAP_ACTION, 8, 6},
55     {"ClickFinger1", PT_INT, 0, SYN_MAX_BUTTONS, SYNAPTICS_PROP_CLICK_ACTION, 8, 0},
56     {"ClickFinger2", PT_INT, 0, SYN_MAX_BUTTONS, SYNAPTICS_PROP_CLICK_ACTION, 8, 1},
57     {"ClickFinger3", PT_INT, 0, SYN_MAX_BUTTONS, SYNAPTICS_PROP_CLICK_ACTION, 8, 2},
58     {"CircularScrolling", PT_BOOL, 0, 1, SYNAPTICS_PROP_CIRCULAR_SCROLLING, 8, 0},
59     {"CircScrollDelta", PT_DOUBLE, .01, 3, SYNAPTICS_PROP_CIRCULAR_SCROLLING_DIST, 0 /* float */, 0},
60     {"CircScrollTrigger", PT_INT, 0, 8, SYNAPTICS_PROP_CIRCULAR_SCROLLING_TRIGGER, 8, 0},
61     {"PalmDetect", PT_BOOL, 0, 1, SYNAPTICS_PROP_PALM_DETECT, 8, 0},
62     {"PalmMinWidth", PT_INT, 0, 15, SYNAPTICS_PROP_PALM_DIMENSIONS, 32, 0},
63     {"PalmMinZ", PT_INT, 0, 255, SYNAPTICS_PROP_PALM_DIMENSIONS, 32, 1},
64     {"CoastingSpeed", PT_DOUBLE, 0, 255, SYNAPTICS_PROP_COASTING_SPEED, 0 /* float*/, 0},
65     {"CoastingFriction", PT_DOUBLE, 0, 255, SYNAPTICS_PROP_COASTING_SPEED, 0 /* float*/, 1},
66     {"PressureMotionMinZ", PT_INT, 1, 255, SYNAPTICS_PROP_PRESSURE_MOTION, 32, 0},
67     {"PressureMotionMaxZ", PT_INT, 1, 255, SYNAPTICS_PROP_PRESSURE_MOTION, 32, 1},
68     {"PressureMotionMinFactor", PT_DOUBLE, 0, 10.0, SYNAPTICS_PROP_PRESSURE_MOTION_FACTOR, 0 /*float*/, 0},
69     {"PressureMotionMaxFactor", PT_DOUBLE, 0, 10.0, SYNAPTICS_PROP_PRESSURE_MOTION_FACTOR, 0 /*float*/, 1},
70     {"GrabEventDevice", PT_BOOL, 0, 1, SYNAPTICS_PROP_GRAB, 8, 0},
71     {"TapAndDragGesture", PT_BOOL, 0, 1, SYNAPTICS_PROP_GESTURES, 8, 0},
72     {"AreaLeftEdge", PT_INT, 0, 10000, SYNAPTICS_PROP_AREA, 32, 0},
73     {"AreaRightEdge", PT_INT, 0, 10000, SYNAPTICS_PROP_AREA, 32, 1},
74     {"AreaTopEdge", PT_INT, 0, 10000, SYNAPTICS_PROP_AREA, 32, 2},
75     {"AreaBottomEdge", PT_INT, 0, 10000, SYNAPTICS_PROP_AREA, 32, 3},
76     {"HorizHysteresis", PT_INT, 0, 10000, SYNAPTICS_PROP_NOISE_CANCELLATION, 32, 0},
77     {"VertHysteresis", PT_INT, 0, 10000, SYNAPTICS_PROP_NOISE_CANCELLATION, 32, 1},
78     {"ClickPad", PT_BOOL, 0, 1, SYNAPTICS_PROP_CLICKPAD, 8, 0},
79     {"RightButtonAreaLeft", PT_INT, INT_MIN, INT_MAX, SYNAPTICS_PROP_SOFTBUTTON_AREAS, 32, 0},
80     {"RightButtonAreaRight", PT_INT, INT_MIN, INT_MAX, SYNAPTICS_PROP_SOFTBUTTON_AREAS, 32, 1},
81     {"RightButtonAreaTop", PT_INT, INT_MIN, INT_MAX, SYNAPTICS_PROP_SOFTBUTTON_AREAS, 32, 2},
82     {"RightButtonAreaBottom", PT_INT, INT_MIN, INT_MAX, SYNAPTICS_PROP_SOFTBUTTON_AREAS, 32, 3},
83     {"MiddleButtonAreaLeft", PT_INT, INT_MIN, INT_MAX, SYNAPTICS_PROP_SOFTBUTTON_AREAS, 32, 4},
84     {"MiddleButtonAreaRight", PT_INT, INT_MIN, INT_MAX, SYNAPTICS_PROP_SOFTBUTTON_AREAS, 32, 5},
85     {"MiddleButtonAreaTop", PT_INT, INT_MIN, INT_MAX, SYNAPTICS_PROP_SOFTBUTTON_AREAS, 32, 6},
86     {"MiddleButtonAreaBottom", PT_INT, INT_MIN, INT_MAX, SYNAPTICS_PROP_SOFTBUTTON_AREAS, 32, 7},
87     {NULL, PT_INT, 0, 0, nullptr, 0, 0},
88 };
89 
SynapticsTouchpad(Display * display,int deviceId)90 SynapticsTouchpad::SynapticsTouchpad(Display *display, int deviceId)
91     : XlibTouchpad(display, deviceId)
92     , m_resX(1)
93     , m_resY(1)
94 {
95     m_capsAtom.intern(m_connection, SYNAPTICS_PROP_CAPABILITIES);
96     m_touchpadOffAtom.intern(m_connection, SYNAPTICS_PROP_OFF);
97     XcbAtom resolutionAtom(m_connection, SYNAPTICS_PROP_RESOLUTION);
98     XcbAtom edgesAtom(m_connection, SYNAPTICS_PROP_EDGES);
99 
100     loadSupportedProperties(synapticsProperties);
101 
102     m_toRadians.append("CircScrollDelta");
103 
104     PropertyInfo edges(m_display, m_deviceId, edgesAtom, 0);
105     if (edges.i && edges.nitems == 4) {
106         int w = qAbs(edges.i[1] - edges.i[0]);
107         int h = qAbs(edges.i[3] - edges.i[2]);
108         m_resX = w / 90;
109         m_resY = h / 50;
110         qDebug() << "Width: " << w << " height: " << h;
111         qDebug() << "Approx. resX: " << m_resX << " resY: " << m_resY;
112     }
113 
114     PropertyInfo resolution(m_display, m_deviceId, resolutionAtom, 0);
115     if (resolution.i && resolution.nitems == 2 && resolution.i[0] > 1 && resolution.i[1] > 1) {
116         m_resY = qMin(static_cast<unsigned long>(resolution.i[0]), static_cast<unsigned long>(INT_MAX));
117         m_resX = qMin(static_cast<unsigned long>(resolution.i[1]), static_cast<unsigned long>(INT_MAX));
118         qDebug() << "Touchpad resolution: x: " << m_resX << " y: " << m_resY;
119     }
120 
121     m_scaleByResX.append("HorizScrollDelta");
122     m_scaleByResY.append("VertScrollDelta");
123     m_scaleByResX.append("MaxTapMove");
124     m_scaleByResY.append("MaxTapMove");
125 
126     m_resX = qMax(10, m_resX);
127     m_resY = qMax(10, m_resY);
128     qDebug() << "Final resolution x:" << m_resX << " y:" << m_resY;
129     m_negate["HorizScrollDelta"] = "InvertHorizScroll";
130     m_negate["VertScrollDelta"] = "InvertVertScroll";
131     m_supported.append(m_negate.values());
132     m_supported.append("Coasting");
133 
134     PropertyInfo caps(m_display, m_deviceId, m_capsAtom.atom(), 0);
135     if (!caps.b) {
136         return;
137     }
138 
139     enum TouchpadCapabilitiy {
140         TouchpadHasLeftButton,
141         TouchpadHasMiddleButton,
142         TouchpadHasRightButton,
143         TouchpadTwoFingerDetect,
144         TouchpadThreeFingerDetect,
145         TouchpadPressureDetect,
146         TouchpadPalmDetect,
147         TouchpadCapsCount,
148     };
149 
150     QVector<bool> cap(TouchpadCapsCount, false);
151     std::copy(caps.b, caps.b + qMin(cap.size(), static_cast<int>(caps.nitems)), cap.begin());
152 
153     if (!cap[TouchpadTwoFingerDetect]) {
154         m_supported.removeAll("HorizTwoFingerScroll");
155         m_supported.removeAll("VertTwoFingerScroll");
156         m_supported.removeAll("TwoFingerTapButton");
157     }
158 
159     if (!cap[TouchpadThreeFingerDetect]) {
160         m_supported.removeAll("ThreeFingerTapButton");
161     }
162 
163     if (!cap[TouchpadPressureDetect]) {
164         m_supported.removeAll("FingerHigh");
165         m_supported.removeAll("FingerLow");
166 
167         m_supported.removeAll("PalmMinZ");
168         m_supported.removeAll("PressureMotionMinZ");
169         m_supported.removeAll("PressureMotionMinFactor");
170         m_supported.removeAll("PressureMotionMaxZ");
171         m_supported.removeAll("PressureMotionMaxFactor");
172         m_supported.removeAll("EmulateTwoFingerMinZ");
173     }
174 
175     if (!cap[TouchpadPalmDetect]) {
176         m_supported.removeAll("PalmDetect");
177         m_supported.removeAll("PalmMinWidth");
178         m_supported.removeAll("PalmMinZ");
179         m_supported.removeAll("EmulateTwoFingerMinW");
180     }
181 
182     for (QMap<QString, QString>::Iterator i = m_negate.begin(); i != m_negate.end(); ++i) {
183         if (!m_supported.contains(i.key())) {
184             m_supported.removeAll(i.value());
185         }
186     }
187 
188     m_paramList = synapticsProperties;
189 }
190 
setTouchpadOff(int touchpadOff)191 void SynapticsTouchpad::setTouchpadOff(int touchpadOff)
192 {
193     PropertyInfo off(m_display, m_deviceId, m_touchpadOffAtom.atom(), 0);
194     if (off.b && *(off.b) != touchpadOff) {
195         *(off.b) = touchpadOff;
196         off.set();
197     }
198 
199     flush();
200 }
201 
touchpadOff()202 int SynapticsTouchpad::touchpadOff()
203 {
204     PropertyInfo off(m_display, m_deviceId, m_touchpadOffAtom.atom(), 0);
205     return off.value(0).toInt();
206 }
207 
touchpadOffAtom()208 XcbAtom &SynapticsTouchpad::touchpadOffAtom()
209 {
210     return m_touchpadOffAtom;
211 }
212 
getPropertyScale(const QString & name) const213 double SynapticsTouchpad::getPropertyScale(const QString &name) const
214 {
215     if (m_scaleByResX.contains(name) && m_scaleByResY.contains(name)) {
216         return std::sqrt(static_cast<double>(m_resX) * m_resX + static_cast<double>(m_resY) * m_resY);
217     } else if (m_scaleByResX.contains(name)) {
218         return m_resX;
219     } else if (m_scaleByResY.contains(name)) {
220         return m_resY;
221     } else if (m_toRadians.contains(name)) {
222         return M_PI_4 / 45.0;
223     }
224     return 1.0;
225 }
226