3 * xserver-xorg-input-evdev-multitouch
5 * Contact: Sung-Jin Park <sj76.park@samsung.com>
6 * Sangjin LEE <lsj119@samsung.com>
8 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
10 * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
11 * Copyright 1993 by David Dawes <dawes@xfree86.org>
12 * Copyright 2002 by SuSE Linux AG, Author: Egbert Eich
13 * Copyright 1994-2002 by The XFree86 Project, Inc.
14 * Copyright 2002 by Paul Elliott
15 * (Ported from xf86-input-mouse, above copyrights taken from there)
16 * Copyright © 2008 University of South Australia
18 * Permission to use, copy, modify, distribute, and sell this software
19 * and its documentation for any purpose is hereby granted without
20 * fee, provided that the above copyright notice appear in all copies
21 * and that both that copyright notice and this permission notice
22 * appear in supporting documentation, and that the name of the authors
23 * not be used in advertising or publicity pertaining to distribution of the
24 * software without specific, written prior permission. The authors make no
25 * representations about the suitability of this software for any
26 * purpose. It is provided "as is" without express or implied
29 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
30 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
31 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
32 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
33 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
34 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
35 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
39 /* Middle mouse button emulation code. */
45 #include <X11/Xatom.h>
47 #include <xf86Xinput.h>
50 #include <evdevmultitouch-properties.h>
51 #include "evdevmultitouch.h"
59 #ifdef HAVE_PROPERTIES
60 static Atom prop_mbemu = 0; /* Middle button emulation on/off property */
61 static Atom prop_mbtimeout = 0; /* Middle button timeout property */
64 * Lets create a simple finite-state machine for 3 button emulation:
66 * We track buttons 1 and 3 (left and right). There are 11 states:
67 * 0 ground - initial state
68 * 1 delayed left - left pressed, waiting for right
69 * 2 delayed right - right pressed, waiting for left
70 * 3 pressed middle - right and left pressed, emulated middle sent
71 * 4 pressed left - left pressed and sent
72 * 5 pressed right - right pressed and sent
73 * 6 released left - left released after emulated middle
74 * 7 released right - right released after emulated middle
75 * 8 repressed left - left pressed after released left
76 * 9 repressed right - right pressed after released right
77 * 10 pressed both - both pressed, not emulating middle
79 * At each state, we need handlers for the following events
82 * 2: right button down
83 * 3: both buttons down
84 * 4: emulate3Timeout passed without a button change
85 * Note that button events are not deltas, they are the set of buttons being
86 * pressed now. It's possible (ie, mouse hardware does it) to go from (eg)
87 * left down to right down without anything in between, so all cases must be
90 * a handler consists of three values:
93 * 2: new emulation state
95 * action > 0: ButtonPress
97 * action < 0: ButtonRelease
99 * The comment preceeding each section is the current emulation state.
100 * The comments to the right are of the form
101 * <button state> (<events>) -> <new emulation state>
102 * which should be read as
103 * If the buttons are in <button state>, generate <events> then go to
104 * <new emulation state>.
106 static signed char stateTab[11][5][3] = {
109 { 0, 0, 0 }, /* nothing -> ground (no change) */
110 { 0, 0, 1 }, /* left -> delayed left */
111 { 0, 0, 2 }, /* right -> delayed right */
112 { 2, 0, 3 }, /* left & right (middle press) -> pressed middle */
113 { 0, 0, -1 } /* timeout N/A */
117 { 1, -1, 0 }, /* nothing (left event) -> ground */
118 { 0, 0, 1 }, /* left -> delayed left (no change) */
119 { 1, -1, 2 }, /* right (left event) -> delayed right */
120 { 2, 0, 3 }, /* left & right (middle press) -> pressed middle */
121 { 1, 0, 4 }, /* timeout (left press) -> pressed left */
123 /* 2 delayed right */
125 { 3, -3, 0 }, /* nothing (right event) -> ground */
126 { 3, -3, 1 }, /* left (right event) -> delayed left (no change) */
127 { 0, 0, 2 }, /* right -> delayed right (no change) */
128 { 2, 0, 3 }, /* left & right (middle press) -> pressed middle */
129 { 3, 0, 5 }, /* timeout (right press) -> pressed right */
131 /* 3 pressed middle */
133 { -2, 0, 0 }, /* nothing (middle release) -> ground */
134 { 0, 0, 7 }, /* left -> released right */
135 { 0, 0, 6 }, /* right -> released left */
136 { 0, 0, 3 }, /* left & right -> pressed middle (no change) */
137 { 0, 0, -1 }, /* timeout N/A */
141 { -1, 0, 0 }, /* nothing (left release) -> ground */
142 { 0, 0, 4 }, /* left -> pressed left (no change) */
143 { -1, 0, 2 }, /* right (left release) -> delayed right */
144 { 3, 0, 10 }, /* left & right (right press) -> pressed both */
145 { 0, 0, -1 }, /* timeout N/A */
147 /* 5 pressed right */
149 { -3, 0, 0 }, /* nothing (right release) -> ground */
150 { -3, 0, 1 }, /* left (right release) -> delayed left */
151 { 0, 0, 5 }, /* right -> pressed right (no change) */
152 { 1, 0, 10 }, /* left & right (left press) -> pressed both */
153 { 0, 0, -1 }, /* timeout N/A */
155 /* 6 released left */
157 { -2, 0, 0 }, /* nothing (middle release) -> ground */
158 { -2, 0, 1 }, /* left (middle release) -> delayed left */
159 { 0, 0, 6 }, /* right -> released left (no change) */
160 { 1, 0, 8 }, /* left & right (left press) -> repressed left */
161 { 0, 0, -1 }, /* timeout N/A */
163 /* 7 released right */
165 { -2, 0, 0 }, /* nothing (middle release) -> ground */
166 { 0, 0, 7 }, /* left -> released right (no change) */
167 { -2, 0, 2 }, /* right (middle release) -> delayed right */
168 { 3, 0, 9 }, /* left & right (right press) -> repressed right */
169 { 0, 0, -1 }, /* timeout N/A */
171 /* 8 repressed left */
173 { -2, -1, 0 }, /* nothing (middle release, left release) -> ground */
174 { -2, 0, 4 }, /* left (middle release) -> pressed left */
175 { -1, 0, 6 }, /* right (left release) -> released left */
176 { 0, 0, 8 }, /* left & right -> repressed left (no change) */
177 { 0, 0, -1 }, /* timeout N/A */
179 /* 9 repressed right */
181 { -2, -3, 0 }, /* nothing (middle release, right release) -> ground */
182 { -3, 0, 7 }, /* left (right release) -> released right */
183 { -2, 0, 5 }, /* right (middle release) -> pressed right */
184 { 0, 0, 9 }, /* left & right -> repressed right (no change) */
185 { 0, 0, -1 }, /* timeout N/A */
187 /* 10 pressed both */
189 { -1, -3, 0 }, /* nothing (left release, right release) -> ground */
190 { -3, 0, 4 }, /* left (right release) -> pressed left */
191 { -1, 0, 5 }, /* right (left release) -> pressed right */
192 { 0, 0, 10 }, /* left & right -> pressed both (no change) */
193 { 0, 0, -1 }, /* timeout N/A */
199 EvdevMultitouchMBEmuTimer(InputInfoPtr pInfo)
201 EvdevMultitouchPtr pEvdevMultitouch = pInfo->private;
205 sigstate = xf86BlockSIGIO ();
207 pEvdevMultitouch->emulateMB.pending = FALSE;
208 if ((id = stateTab[pEvdevMultitouch->emulateMB.state][4][0]) != 0) {
209 EvdevMultitouchPostButtonEvent(pInfo, abs(id), (id >= 0));
210 pEvdevMultitouch->emulateMB.state =
211 stateTab[pEvdevMultitouch->emulateMB.state][4][2];
213 ErrorF("Got unexpected buttonTimer in state %d\n",
214 pEvdevMultitouch->emulateMB.state);
217 xf86UnblockSIGIO (sigstate);
223 * Emulate a middle button on button press.
225 * @param code button number (1 for left, 3 for right)
226 * @param press TRUE if press, FALSE if release.
228 * @return TRUE if event was swallowed by middle mouse button emulation, FALSE
232 EvdevMultitouchMBEmuFilterEvent(InputInfoPtr pInfo, int button, BOOL press)
234 EvdevMultitouchPtr pEvdevMultitouch = pInfo->private;
239 if (!pEvdevMultitouch->emulateMB.enabled)
243 EvdevMultitouchMBEmuEnable(pInfo, FALSE);
247 /* don't care about other buttons */
248 if (button != 1 && button != 3)
251 btstate = &pEvdevMultitouch->emulateMB.buttonstate;
253 *btstate |= (button == 1) ? 0x1 : 0x2;
255 *btstate &= (button == 1) ? ~0x1 : ~0x2;
257 if ((id = stateTab[pEvdevMultitouch->emulateMB.state][*btstate][0]) != 0)
259 EvdevMultitouchQueueButtonEvent(pInfo, abs(id), (id >= 0));
262 if ((id = stateTab[pEvdevMultitouch->emulateMB.state][*btstate][1]) != 0)
264 EvdevMultitouchQueueButtonEvent(pInfo, abs(id), (id >= 0));
268 pEvdevMultitouch->emulateMB.state =
269 stateTab[pEvdevMultitouch->emulateMB.state][*btstate][2];
271 if (stateTab[pEvdevMultitouch->emulateMB.state][4][0] != 0) {
272 pEvdevMultitouch->emulateMB.expires = GetTimeInMillis () + pEvdevMultitouch->emulateMB.timeout;
273 pEvdevMultitouch->emulateMB.pending = TRUE;
276 pEvdevMultitouch->emulateMB.pending = FALSE;
283 void EvdevMultitouchMBEmuWakeupHandler(pointer data,
285 pointer LastSelectMask)
287 InputInfoPtr pInfo = (InputInfoPtr)data;
288 EvdevMultitouchPtr pEvdevMultitouch = (EvdevMultitouchPtr)pInfo->private;
291 if (pEvdevMultitouch->emulateMB.pending)
293 ms = pEvdevMultitouch->emulateMB.expires - GetTimeInMillis();
295 EvdevMultitouchMBEmuTimer(pInfo);
299 void EvdevMultitouchMBEmuBlockHandler(pointer data,
300 struct timeval **waitTime,
301 pointer LastSelectMask)
303 InputInfoPtr pInfo = (InputInfoPtr) data;
304 EvdevMultitouchPtr pEvdevMultitouch= (EvdevMultitouchPtr) pInfo->private;
307 if (pEvdevMultitouch->emulateMB.pending)
309 ms = pEvdevMultitouch->emulateMB.expires - GetTimeInMillis ();
312 AdjustWaitForDelay (waitTime, ms);
317 EvdevMultitouchMBEmuPreInit(InputInfoPtr pInfo)
319 EvdevMultitouchPtr pEvdevMultitouch = (EvdevMultitouchPtr)pInfo->private;
320 pEvdevMultitouch->emulateMB.enabled = MBEMU_AUTO;
322 if (xf86FindOption(pInfo->options, "Emulate3Buttons"))
324 pEvdevMultitouch->emulateMB.enabled = xf86SetBoolOption(pInfo->options,
327 xf86Msg(X_INFO, "%s: Forcing middle mouse button emulation %s.\n",
328 pInfo->name, (pEvdevMultitouch->emulateMB.enabled) ? "on" : "off");
331 pEvdevMultitouch->emulateMB.timeout = xf86SetIntOption(pInfo->options,
332 "Emulate3Timeout", 50);
336 EvdevMultitouchMBEmuOn(InputInfoPtr pInfo)
338 if (!pInfo->dev->button) /* don't init for keyboards */
341 RegisterBlockAndWakeupHandlers (EvdevMultitouchMBEmuBlockHandler,
342 EvdevMultitouchMBEmuWakeupHandler,
347 EvdevMultitouchMBEmuFinalize(InputInfoPtr pInfo)
349 if (!pInfo->dev->button) /* don't cleanup for keyboards */
352 RemoveBlockAndWakeupHandlers (EvdevMultitouchMBEmuBlockHandler,
353 EvdevMultitouchMBEmuWakeupHandler,
358 /* Enable/disable middle mouse button emulation. */
360 EvdevMultitouchMBEmuEnable(InputInfoPtr pInfo, BOOL enable)
362 EvdevMultitouchPtr pEvdevMultitouch = (EvdevMultitouchPtr)pInfo->private;
363 if (pEvdevMultitouch->emulateMB.enabled == MBEMU_AUTO)
364 pEvdevMultitouch->emulateMB.enabled = enable;
368 #ifdef HAVE_PROPERTIES
370 EvdevMultitouchMBEmuSetProperty(DeviceIntPtr dev, Atom atom, XIPropertyValuePtr val,
373 InputInfoPtr pInfo = dev->public.devicePrivate;
374 EvdevMultitouchPtr pEvdevMultitouch = pInfo->private;
376 if (atom == prop_mbemu)
378 if (val->format != 8 || val->size != 1 || val->type != XA_INTEGER)
382 pEvdevMultitouch->emulateMB.enabled = *((BOOL*)val->data);
383 } else if (atom == prop_mbtimeout)
385 if (val->format != 32 || val->size != 1 || val->type != XA_INTEGER)
389 pEvdevMultitouch->emulateMB.timeout = *((CARD32*)val->data);
396 * Initialise property for MB emulation on/off.
399 EvdevMultitouchMBEmuInitProperty(DeviceIntPtr dev)
401 InputInfoPtr pInfo = dev->public.devicePrivate;
402 EvdevMultitouchPtr pEvdevMultitouch = pInfo->private;
405 if (!dev->button) /* don't init prop for keyboards */
408 prop_mbemu = MakeAtom(EVDEVMULTITOUCH_PROP_MIDBUTTON, strlen(EVDEVMULTITOUCH_PROP_MIDBUTTON), TRUE);
409 rc = XIChangeDeviceProperty(dev, prop_mbemu, XA_INTEGER, 8,
411 &pEvdevMultitouch->emulateMB.enabled,
415 XISetDevicePropertyDeletable(dev, prop_mbemu, FALSE);
417 prop_mbtimeout = MakeAtom(EVDEVMULTITOUCH_PROP_MIDBUTTON_TIMEOUT,
418 strlen(EVDEVMULTITOUCH_PROP_MIDBUTTON_TIMEOUT),
420 rc = XIChangeDeviceProperty(dev, prop_mbtimeout, XA_INTEGER, 32, PropModeReplace, 1,
421 &pEvdevMultitouch->emulateMB.timeout, FALSE);
425 XISetDevicePropertyDeletable(dev, prop_mbtimeout, FALSE);
427 XIRegisterPropertyHandler(dev, EvdevMultitouchMBEmuSetProperty, NULL, NULL);