1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
diff --git a/include/evdev-properties.h b/include/evdev-properties.h
index 8ae5ba3..d96a9e8 100644
--- a/include/evdev-properties.h
+++ b/include/evdev-properties.h
@@ -87,6 +87,9 @@
*/
#define EVDEV_PROP_FUNCTION_KEYS "Evdev Function Keys"
+/* CARD32 */
+#define EVDEV_PROP_DEBOUNCE_DELAY "Evdev Debounce Delay"
+
/* Smooth scroll */
/* INT32, 3 values (vertical, horizontal, dial) */
#define EVDEV_PROP_SCROLL_DISTANCE "Evdev Scrolling Distance"
diff --git a/src/Makefile.am b/src/Makefile.am
index 5e0c3b3..1ab5dd6 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -39,6 +39,7 @@ AM_CPPFLAGS =-I$(top_srcdir)/include $(LIBEVDEV_CFLAGS)
emuThird.c \
emuWheel.c \
draglock.c \
+ debounce.c \
apple.c \
axis_labels.h
diff --git a/src/debounce.c b/src/debounce.c
new file mode 100644
index 0000000..cda703d
--- /dev/null
+++ b/src/debounce.c
@@ -0,0 +1,198 @@
+/*
+ * Permission to use, copy, modify, distribute, and sell this software
+ * and its documentation for any purpose is hereby granted without
+ * fee, provided that the above copyright notice appear in all copies
+ * and that both that copyright notice and this permission notice
+ * appear in supporting documentation, and that the name of the authors
+ * not be used in advertising or publicity pertaining to distribution of the
+ * software without specific, written prior permission. The authors make no
+ * representations about the suitability of this software for any
+ * purpose. It is provided "as is" without express or implied
+ * warranty.
+ *
+ * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
+ * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
+ * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+ * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "evdev.h"
+
+#include <X11/Xatom.h>
+#include <xf86.h>
+#include <xf86Xinput.h>
+#include <exevents.h>
+
+#include <evdev-properties.h>
+
+/* how many milliseconds to wait for buttons to settle */
+static Atom prop_dbdelay;
+
+struct ButtonTime {
+ unsigned int button;
+ Time time;
+};
+
+static int
+EvdevDBCompareButtonTimes(const void *bt1, const void *bt2)
+{
+ return ((const struct ButtonTime *) bt1)->time -
+ ((const struct ButtonTime *) bt2)->time;
+}
+
+/**
+ * Timer callback for debouncing buttons.
+ */
+static CARD32
+EvdevDBTimer(OsTimerPtr timer, CARD32 time, pointer arg)
+{
+ InputInfoPtr pInfo = arg;
+ EvdevPtr pEvdev = pInfo->private;
+ struct debounce *db = &pEvdev->debounce;
+ struct ButtonTime release[EVDEV_MAXBUTTONS];
+ size_t nRelease = 0;
+ int sigstate = xf86BlockSIGIO();
+
+ if (db->bouncing) {
+ BOOL bouncing = FALSE;
+ Time next_expiry = ~0;
+ for (size_t button = 0; button < EVDEV_MAXBUTTONS; ++button) {
+ if (db->state[button].bouncing) {
+ Time expiry = db->state[button].expiry;
+ if (expiry <= time) {
+ db->state[button].pressed = FALSE;
+ db->state[button].bouncing = FALSE;
+ release[nRelease].button = button;
+ release[nRelease].time = expiry;
+ ++nRelease;
+ }
+ else if (expiry < next_expiry) {
+ bouncing = TRUE;
+ next_expiry = expiry;
+ }
+ }
+ }
+ if (nRelease > 0) {
+ if (nRelease > 1) {
+ qsort(release, nRelease, sizeof *release,
+ EvdevDBCompareButtonTimes);
+ }
+ for (size_t i = 0; i < nRelease; ++i) {
+ EvdevPostButtonEvent(pInfo, release[i].button, BUTTON_RELEASE);
+ }
+ }
+ if (bouncing) {
+ db->timer = TimerSet(db->timer, 0, next_expiry - GetTimeInMillis(),
+ EvdevDBTimer, pInfo);
+ }
+ else {
+ db->bouncing = FALSE;
+ }
+ }
+
+ xf86UnblockSIGIO(sigstate);
+ return 0;
+}
+
+/**
+ * Debounce button states.
+ *
+ * @return TRUE iff event was swallowed by debouncing.
+ */
+BOOL
+EvdevDBFilterEvent(InputInfoPtr pInfo, unsigned int button, BOOL pressed)
+{
+ EvdevPtr pEvdev = pInfo->private;
+ struct debounce *db = &pEvdev->debounce;
+
+ if (db->delay == 0) {
+ return FALSE;
+ }
+
+ if (pressed) {
+ if (db->state[button].pressed) {
+ db->state[button].bouncing = FALSE;
+ return TRUE;
+ }
+ db->state[button].pressed = TRUE;
+ return FALSE;
+ }
+
+ if (db->state[button].pressed) {
+ db->state[button].bouncing = TRUE;
+ db->state[button].expiry = GetTimeInMillis() + db->delay;
+ if (!db->bouncing) {
+ db->bouncing = TRUE;
+ db->timer = TimerSet(db->timer, 0, db->delay, EvdevDBTimer, pInfo);
+ }
+ }
+
+ return TRUE;
+}
+
+void
+EvdevDBPreInit(InputInfoPtr pInfo)
+{
+ EvdevPtr pEvdev = pInfo->private;
+ struct debounce *db = &pEvdev->debounce;
+
+ db->delay = xf86SetIntOption(pInfo->options, "DebounceDelay", 0);
+ db->timer = TimerSet(NULL, 0, 0, NULL, NULL);
+}
+
+void
+EvdevDBFinalize(InputInfoPtr pInfo)
+{
+ EvdevPtr pEvdev = pInfo->private;
+ struct debounce *db = &pEvdev->debounce;
+
+ TimerFree(db->timer), db->timer = NULL;
+}
+
+static int
+EvdevDBSetProperty(DeviceIntPtr dev, Atom atom, XIPropertyValuePtr val,
+ BOOL checkonly)
+{
+ InputInfoPtr pInfo = dev->public.devicePrivate;
+ EvdevPtr pEvdev = pInfo->private;
+ struct debounce *db = &pEvdev->debounce;
+
+ if (atom == prop_dbdelay) {
+ if (val->format != 32 || val->size != 1 || val->type != XA_INTEGER) {
+ return BadMatch;
+ }
+ if (!checkonly) {
+ db->delay = *((CARD32 *) val->data);
+ }
+ }
+
+ return Success;
+}
+
+void
+EvdevDBInitProperty(DeviceIntPtr dev)
+{
+ InputInfoPtr pInfo = dev->public.devicePrivate;
+ EvdevPtr pEvdev = pInfo->private;
+ struct debounce *db = &pEvdev->debounce;
+
+ if (dev->button) {
+ prop_dbdelay = MakeAtom(EVDEV_PROP_DEBOUNCE_DELAY,
+ strlen(EVDEV_PROP_DEBOUNCE_DELAY), TRUE);
+ if (XIChangeDeviceProperty(dev, prop_dbdelay, XA_INTEGER, 32,
+ PropModeReplace, 1, &db->delay, FALSE)
+ != Success) {
+ return;
+ }
+ XISetDevicePropertyDeletable(dev, prop_dbdelay, FALSE);
+ XIRegisterPropertyHandler(dev, EvdevDBSetProperty, NULL, NULL);
+ }
+}
diff --git a/src/evdev.c b/src/evdev.c
index 30f809b..47255d0 100644
--- a/src/evdev.c
+++ b/src/evdev.c
@@ -619,6 +619,9 @@ EvdevProcessButtonEvent(InputInfoPtr pInfo, struct input_event *ev)
if (EvdevDragLockFilterEvent(pInfo, button, value))
return;
+ if (EvdevDBFilterEvent(pInfo, button, value))
+ return;
+
if (EvdevWheelEmuFilterButton(pInfo, button, value))
return;
@@ -1908,6 +1911,7 @@ EvdevInit(DeviceIntPtr device)
Evdev3BEmuInitProperty(device);
EvdevWheelEmuInitProperty(device);
EvdevDragLockInitProperty(device);
+ EvdevDBInitProperty(device);
EvdevAppleInitProperty(device);
return Success;
@@ -1965,6 +1969,7 @@ EvdevProc(DeviceIntPtr device, int what)
{
EvdevMBEmuFinalize(pInfo);
Evdev3BEmuFinalize(pInfo);
+ EvdevDBFinalize(pInfo);
}
if (pInfo->fd != -1)
{
@@ -2619,6 +2624,7 @@ EvdevPreInit(InputDriverPtr drv, InputInfoPtr pInfo, int flags)
Evdev3BEmuPreInit(pInfo);
EvdevWheelEmuPreInit(pInfo);
EvdevDragLockPreInit(pInfo);
+ EvdevDBPreInit(pInfo);
}
return Success;
diff --git a/src/evdev.h b/src/evdev.h
index 520d017..a39e1ca 100644
--- a/src/evdev.h
+++ b/src/evdev.h
@@ -221,6 +221,17 @@ typedef struct {
Time expires; /* time of expiry */
Time timeout;
} emulateWheel;
+ /* Button debouncing */
+ struct debounce {
+ Time delay;
+ OsTimerPtr timer;
+ BOOL bouncing;
+ struct {
+ BOOL pressed;
+ BOOL bouncing;
+ Time expiry;
+ } state[EVDEV_MAXBUTTONS];
+ } debounce;
struct {
int vert_delta;
int horiz_delta;
@@ -295,6 +306,12 @@ BOOL EvdevWheelEmuFilterMotion(InputInfoPtr pInfo, struct input_event *pEv);
void EvdevDragLockPreInit(InputInfoPtr pInfo);
BOOL EvdevDragLockFilterEvent(InputInfoPtr pInfo, unsigned int button, int value);
+/* Button debouncing */
+BOOL EvdevDBFilterEvent(InputInfoPtr pInfo, unsigned int button, BOOL pressed);
+void EvdevDBPreInit(InputInfoPtr pInfo);
+void EvdevDBFinalize(InputInfoPtr pInfo);
+void EvdevDBInitProperty(DeviceIntPtr dev);
+
void EvdevMBEmuInitProperty(DeviceIntPtr);
void Evdev3BEmuInitProperty(DeviceIntPtr);
void EvdevWheelEmuInitProperty(DeviceIntPtr);
|