2 * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
3 * Copyright 1993 by David Dawes <dawes@xfree86.org>
4 * Copyright 2002 by SuSE Linux AG, Author: Egbert Eich
5 * Copyright 1994-2002 by The XFree86 Project, Inc.
6 * Copyright 2002 by Paul Elliott
7 * (Ported from xf86-input-mouse, above copyrights taken from there)
8 * Copyright © 2008 University of South Australia
10 * Permission to use, copy, modify, distribute, and sell this software
11 * and its documentation for any purpose is hereby granted without
12 * fee, provided that the above copyright notice appear in all copies
13 * and that both that copyright notice and this permission notice
14 * appear in supporting documentation, and that the name of the authors
15 * not be used in advertising or publicity pertaining to distribution of the
16 * software without specific, written prior permission. The authors make no
17 * representations about the suitability of this software for any
18 * purpose. It is provided "as is" without express or implied
21 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
22 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
23 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
24 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
25 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
26 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
27 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 /* Middle mouse button emulation code. */
37 #include <X11/Xatom.h>
39 #include <xf86Xinput.h>
42 #include <evdev-properties.h>
51 #ifdef HAVE_PROPERTIES
52 static Atom prop_mbemu = 0; /* Middle button emulation on/off property */
53 static Atom prop_mbtimeout = 0; /* Middle button timeout property */
56 * Lets create a simple finite-state machine for 3 button emulation:
58 * We track buttons 1 and 3 (left and right). There are 11 states:
59 * 0 ground - initial state
60 * 1 delayed left - left pressed, waiting for right
61 * 2 delayed right - right pressed, waiting for left
62 * 3 pressed middle - right and left pressed, emulated middle sent
63 * 4 pressed left - left pressed and sent
64 * 5 pressed right - right pressed and sent
65 * 6 released left - left released after emulated middle
66 * 7 released right - right released after emulated middle
67 * 8 repressed left - left pressed after released left
68 * 9 repressed right - right pressed after released right
69 * 10 pressed both - both pressed, not emulating middle
71 * At each state, we need handlers for the following events
74 * 2: right button down
75 * 3: both buttons down
76 * 4: emulate3Timeout passed without a button change
77 * Note that button events are not deltas, they are the set of buttons being
78 * pressed now. It's possible (ie, mouse hardware does it) to go from (eg)
79 * left down to right down without anything in between, so all cases must be
82 * a handler consists of three values:
85 * 2: new emulation state
87 * action > 0: ButtonPress
89 * action < 0: ButtonRelease
91 * The comment preceeding each section is the current emulation state.
92 * The comments to the right are of the form
93 * <button state> (<events>) -> <new emulation state>
94 * which should be read as
95 * If the buttons are in <button state>, generate <events> then go to
96 * <new emulation state>.
98 static signed char stateTab[11][5][3] = {
101 { 0, 0, 0 }, /* nothing -> ground (no change) */
102 { 0, 0, 1 }, /* left -> delayed left */
103 { 0, 0, 2 }, /* right -> delayed right */
104 { 2, 0, 3 }, /* left & right (middle press) -> pressed middle */
105 { 0, 0, -1 } /* timeout N/A */
109 { 1, -1, 0 }, /* nothing (left event) -> ground */
110 { 0, 0, 1 }, /* left -> delayed left (no change) */
111 { 1, -1, 2 }, /* right (left event) -> delayed right */
112 { 2, 0, 3 }, /* left & right (middle press) -> pressed middle */
113 { 1, 0, 4 }, /* timeout (left press) -> pressed left */
115 /* 2 delayed right */
117 { 3, -3, 0 }, /* nothing (right event) -> ground */
118 { 3, -3, 1 }, /* left (right event) -> delayed left (no change) */
119 { 0, 0, 2 }, /* right -> delayed right (no change) */
120 { 2, 0, 3 }, /* left & right (middle press) -> pressed middle */
121 { 3, 0, 5 }, /* timeout (right press) -> pressed right */
123 /* 3 pressed middle */
125 { -2, 0, 0 }, /* nothing (middle release) -> ground */
126 { 0, 0, 7 }, /* left -> released right */
127 { 0, 0, 6 }, /* right -> released left */
128 { 0, 0, 3 }, /* left & right -> pressed middle (no change) */
129 { 0, 0, -1 }, /* timeout N/A */
133 { -1, 0, 0 }, /* nothing (left release) -> ground */
134 { 0, 0, 4 }, /* left -> pressed left (no change) */
135 { -1, 0, 2 }, /* right (left release) -> delayed right */
136 { 3, 0, 10 }, /* left & right (right press) -> pressed both */
137 { 0, 0, -1 }, /* timeout N/A */
139 /* 5 pressed right */
141 { -3, 0, 0 }, /* nothing (right release) -> ground */
142 { -3, 0, 1 }, /* left (right release) -> delayed left */
143 { 0, 0, 5 }, /* right -> pressed right (no change) */
144 { 1, 0, 10 }, /* left & right (left press) -> pressed both */
145 { 0, 0, -1 }, /* timeout N/A */
147 /* 6 released left */
149 { -2, 0, 0 }, /* nothing (middle release) -> ground */
150 { -2, 0, 1 }, /* left (middle release) -> delayed left */
151 { 0, 0, 6 }, /* right -> released left (no change) */
152 { 1, 0, 8 }, /* left & right (left press) -> repressed left */
153 { 0, 0, -1 }, /* timeout N/A */
155 /* 7 released right */
157 { -2, 0, 0 }, /* nothing (middle release) -> ground */
158 { 0, 0, 7 }, /* left -> released right (no change) */
159 { -2, 0, 2 }, /* right (middle release) -> delayed right */
160 { 3, 0, 9 }, /* left & right (right press) -> repressed right */
161 { 0, 0, -1 }, /* timeout N/A */
163 /* 8 repressed left */
165 { -2, -1, 0 }, /* nothing (middle release, left release) -> ground */
166 { -2, 0, 4 }, /* left (middle release) -> pressed left */
167 { -1, 0, 6 }, /* right (left release) -> released left */
168 { 0, 0, 8 }, /* left & right -> repressed left (no change) */
169 { 0, 0, -1 }, /* timeout N/A */
171 /* 9 repressed right */
173 { -2, -3, 0 }, /* nothing (middle release, right release) -> ground */
174 { -3, 0, 7 }, /* left (right release) -> released right */
175 { -2, 0, 5 }, /* right (middle release) -> pressed right */
176 { 0, 0, 9 }, /* left & right -> repressed right (no change) */
177 { 0, 0, -1 }, /* timeout N/A */
179 /* 10 pressed both */
181 { -1, -3, 0 }, /* nothing (left release, right release) -> ground */
182 { -3, 0, 4 }, /* left (right release) -> pressed left */
183 { -1, 0, 5 }, /* right (left release) -> pressed right */
184 { 0, 0, 10 }, /* left & right -> pressed both (no change) */
185 { 0, 0, -1 }, /* timeout N/A */
191 EvdevMBEmuTimer(InputInfoPtr pInfo)
193 EvdevPtr pEvdev = pInfo->private;
197 sigstate = xf86BlockSIGIO ();
199 pEvdev->emulateMB.pending = FALSE;
200 if ((id = stateTab[pEvdev->emulateMB.state][4][0]) != 0) {
201 EvdevPostButtonEvent(pInfo, abs(id), (id >= 0));
202 pEvdev->emulateMB.state =
203 stateTab[pEvdev->emulateMB.state][4][2];
205 ErrorF("Got unexpected buttonTimer in state %d\n",
206 pEvdev->emulateMB.state);
209 xf86UnblockSIGIO (sigstate);
215 * Emulate a middle button on button press.
217 * @param code button number (1 for left, 3 for right)
218 * @param press TRUE if press, FALSE if release.
220 * @return TRUE if event was swallowed by middle mouse button emulation, FALSE
224 EvdevMBEmuFilterEvent(InputInfoPtr pInfo, int button, BOOL press)
226 EvdevPtr pEvdev = pInfo->private;
231 if (!pEvdev->emulateMB.enabled)
235 EvdevMBEmuEnable(pInfo, FALSE);
239 /* don't care about other buttons */
240 if (button != 1 && button != 3)
243 btstate = &pEvdev->emulateMB.buttonstate;
245 *btstate |= (button == 1) ? 0x1 : 0x2;
247 *btstate &= (button == 1) ? ~0x1 : ~0x2;
249 if ((id = stateTab[pEvdev->emulateMB.state][*btstate][0]) != 0)
251 EvdevQueueButtonEvent(pInfo, abs(id), (id >= 0));
254 if ((id = stateTab[pEvdev->emulateMB.state][*btstate][1]) != 0)
256 EvdevQueueButtonEvent(pInfo, abs(id), (id >= 0));
260 pEvdev->emulateMB.state =
261 stateTab[pEvdev->emulateMB.state][*btstate][2];
263 if (stateTab[pEvdev->emulateMB.state][4][0] != 0) {
264 pEvdev->emulateMB.expires = GetTimeInMillis () + pEvdev->emulateMB.timeout;
265 pEvdev->emulateMB.pending = TRUE;
268 pEvdev->emulateMB.pending = FALSE;
275 void EvdevMBEmuWakeupHandler(pointer data,
277 pointer LastSelectMask)
279 InputInfoPtr pInfo = (InputInfoPtr)data;
280 EvdevPtr pEvdev = (EvdevPtr)pInfo->private;
283 if (pEvdev->emulateMB.pending)
285 ms = pEvdev->emulateMB.expires - GetTimeInMillis();
287 EvdevMBEmuTimer(pInfo);
291 void EvdevMBEmuBlockHandler(pointer data,
292 struct timeval **waitTime,
293 pointer LastSelectMask)
295 InputInfoPtr pInfo = (InputInfoPtr) data;
296 EvdevPtr pEvdev= (EvdevPtr) pInfo->private;
299 if (pEvdev->emulateMB.pending)
301 ms = pEvdev->emulateMB.expires - GetTimeInMillis ();
304 AdjustWaitForDelay (waitTime, ms);
309 EvdevMBEmuPreInit(InputInfoPtr pInfo)
311 EvdevPtr pEvdev = (EvdevPtr)pInfo->private;
312 pEvdev->emulateMB.enabled = MBEMU_AUTO;
314 if (xf86FindOption(pInfo->options, "Emulate3Buttons"))
316 pEvdev->emulateMB.enabled = xf86SetBoolOption(pInfo->options,
319 xf86Msg(X_INFO, "%s: Forcing middle mouse button emulation %s.\n",
320 pInfo->name, (pEvdev->emulateMB.enabled) ? "on" : "off");
323 pEvdev->emulateMB.timeout = xf86SetIntOption(pInfo->options,
324 "Emulate3Timeout", 50);
328 EvdevMBEmuOn(InputInfoPtr pInfo)
330 if (!pInfo->dev->button) /* don't init for keyboards */
333 RegisterBlockAndWakeupHandlers (EvdevMBEmuBlockHandler,
334 EvdevMBEmuWakeupHandler,
339 EvdevMBEmuFinalize(InputInfoPtr pInfo)
341 if (!pInfo->dev->button) /* don't cleanup for keyboards */
344 RemoveBlockAndWakeupHandlers (EvdevMBEmuBlockHandler,
345 EvdevMBEmuWakeupHandler,
350 /* Enable/disable middle mouse button emulation. */
352 EvdevMBEmuEnable(InputInfoPtr pInfo, BOOL enable)
354 EvdevPtr pEvdev = (EvdevPtr)pInfo->private;
355 if (pEvdev->emulateMB.enabled == MBEMU_AUTO)
356 pEvdev->emulateMB.enabled = enable;
360 #ifdef HAVE_PROPERTIES
362 EvdevMBEmuSetProperty(DeviceIntPtr dev, Atom atom, XIPropertyValuePtr val,
365 InputInfoPtr pInfo = dev->public.devicePrivate;
366 EvdevPtr pEvdev = pInfo->private;
368 if (atom == prop_mbemu)
370 if (val->format != 8 || val->size != 1 || val->type != XA_INTEGER)
374 pEvdev->emulateMB.enabled = *((BOOL*)val->data);
375 } else if (atom == prop_mbtimeout)
377 if (val->format != 32 || val->size != 1 || val->type != XA_INTEGER)
381 pEvdev->emulateMB.timeout = *((CARD32*)val->data);
388 * Initialise property for MB emulation on/off.
391 EvdevMBEmuInitProperty(DeviceIntPtr dev)
393 InputInfoPtr pInfo = dev->public.devicePrivate;
394 EvdevPtr pEvdev = pInfo->private;
397 if (!dev->button) /* don't init prop for keyboards */
400 prop_mbemu = MakeAtom(EVDEV_PROP_MIDBUTTON, strlen(EVDEV_PROP_MIDBUTTON), TRUE);
401 rc = XIChangeDeviceProperty(dev, prop_mbemu, XA_INTEGER, 8,
403 &pEvdev->emulateMB.enabled,
407 XISetDevicePropertyDeletable(dev, prop_mbemu, FALSE);
409 prop_mbtimeout = MakeAtom(EVDEV_PROP_MIDBUTTON_TIMEOUT,
410 strlen(EVDEV_PROP_MIDBUTTON_TIMEOUT),
412 rc = XIChangeDeviceProperty(dev, prop_mbtimeout, XA_INTEGER, 32, PropModeReplace, 1,
413 &pEvdev->emulateMB.timeout, FALSE);
417 XISetDevicePropertyDeletable(dev, prop_mbtimeout, FALSE);
419 XIRegisterPropertyHandler(dev, EvdevMBEmuSetProperty, NULL, NULL);