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. */
39 #include <X11/Xatom.h>
41 #include <xf86Xinput.h>
44 #include <evdev-properties.h>
46 static Atom prop_mbemu = 0; /* Middle button emulation on/off property */
47 static Atom prop_mbtimeout = 0; /* Middle button timeout property */
49 * Lets create a simple finite-state machine for 3 button emulation:
51 * We track buttons 1 and 3 (left and right). There are 11 states:
52 * 0 ground - initial state
53 * 1 delayed left - left pressed, waiting for right
54 * 2 delayed right - right pressed, waiting for left
55 * 3 pressed middle - right and left pressed, emulated middle sent
56 * 4 pressed left - left pressed and sent
57 * 5 pressed right - right pressed and sent
58 * 6 released left - left released after emulated middle
59 * 7 released right - right released after emulated middle
60 * 8 repressed left - left pressed after released left
61 * 9 repressed right - right pressed after released right
62 * 10 pressed both - both pressed, not emulating middle
64 * At each state, we need handlers for the following events
67 * 2: right button down
68 * 3: both buttons down
69 * 4: emulate3Timeout passed without a button change
70 * Note that button events are not deltas, they are the set of buttons being
71 * pressed now. It's possible (ie, mouse hardware does it) to go from (eg)
72 * left down to right down without anything in between, so all cases must be
75 * a handler consists of three values:
78 * 2: new emulation state
80 * action > 0: ButtonPress
82 * action < 0: ButtonRelease
84 * The comment preceeding each section is the current emulation state.
85 * The comments to the right are of the form
86 * <button state> (<events>) -> <new emulation state>
87 * which should be read as
88 * If the buttons are in <button state>, generate <events> then go to
89 * <new emulation state>.
91 static signed char stateTab[11][5][3] = {
94 { 0, 0, 0 }, /* nothing -> ground (no change) */
95 { 0, 0, 1 }, /* left -> delayed left */
96 { 0, 0, 2 }, /* right -> delayed right */
97 { 2, 0, 3 }, /* left & right (middle press) -> pressed middle */
98 { 0, 0, -1 } /* timeout N/A */
102 { 1, -1, 0 }, /* nothing (left event) -> ground */
103 { 0, 0, 1 }, /* left -> delayed left (no change) */
104 { 1, -1, 2 }, /* right (left event) -> delayed right */
105 { 2, 0, 3 }, /* left & right (middle press) -> pressed middle */
106 { 1, 0, 4 }, /* timeout (left press) -> pressed left */
108 /* 2 delayed right */
110 { 3, -3, 0 }, /* nothing (right event) -> ground */
111 { 3, -3, 1 }, /* left (right event) -> delayed left (no change) */
112 { 0, 0, 2 }, /* right -> delayed right (no change) */
113 { 2, 0, 3 }, /* left & right (middle press) -> pressed middle */
114 { 3, 0, 5 }, /* timeout (right press) -> pressed right */
116 /* 3 pressed middle */
118 { -2, 0, 0 }, /* nothing (middle release) -> ground */
119 { 0, 0, 7 }, /* left -> released right */
120 { 0, 0, 6 }, /* right -> released left */
121 { 0, 0, 3 }, /* left & right -> pressed middle (no change) */
122 { 0, 0, -1 }, /* timeout N/A */
126 { -1, 0, 0 }, /* nothing (left release) -> ground */
127 { 0, 0, 4 }, /* left -> pressed left (no change) */
128 { -1, 0, 2 }, /* right (left release) -> delayed right */
129 { 3, 0, 10 }, /* left & right (right press) -> pressed both */
130 { 0, 0, -1 }, /* timeout N/A */
132 /* 5 pressed right */
134 { -3, 0, 0 }, /* nothing (right release) -> ground */
135 { -3, 0, 1 }, /* left (right release) -> delayed left */
136 { 0, 0, 5 }, /* right -> pressed right (no change) */
137 { 1, 0, 10 }, /* left & right (left press) -> pressed both */
138 { 0, 0, -1 }, /* timeout N/A */
140 /* 6 released left */
142 { -2, 0, 0 }, /* nothing (middle release) -> ground */
143 { -2, 0, 1 }, /* left (middle release) -> delayed left */
144 { 0, 0, 6 }, /* right -> released left (no change) */
145 { 1, 0, 8 }, /* left & right (left press) -> repressed left */
146 { 0, 0, -1 }, /* timeout N/A */
148 /* 7 released right */
150 { -2, 0, 0 }, /* nothing (middle release) -> ground */
151 { 0, 0, 7 }, /* left -> released right (no change) */
152 { -2, 0, 2 }, /* right (middle release) -> delayed right */
153 { 3, 0, 9 }, /* left & right (right press) -> repressed right */
154 { 0, 0, -1 }, /* timeout N/A */
156 /* 8 repressed left */
158 { -2, -1, 0 }, /* nothing (middle release, left release) -> ground */
159 { -2, 0, 4 }, /* left (middle release) -> pressed left */
160 { -1, 0, 6 }, /* right (left release) -> released left */
161 { 0, 0, 8 }, /* left & right -> repressed left (no change) */
162 { 0, 0, -1 }, /* timeout N/A */
164 /* 9 repressed right */
166 { -2, -3, 0 }, /* nothing (middle release, right release) -> ground */
167 { -3, 0, 7 }, /* left (right release) -> released right */
168 { -2, 0, 5 }, /* right (middle release) -> pressed right */
169 { 0, 0, 9 }, /* left & right -> repressed right (no change) */
170 { 0, 0, -1 }, /* timeout N/A */
172 /* 10 pressed both */
174 { -1, -3, 0 }, /* nothing (left release, right release) -> ground */
175 { -3, 0, 4 }, /* left (right release) -> pressed left */
176 { -1, 0, 5 }, /* right (left release) -> pressed right */
177 { 0, 0, 10 }, /* left & right -> pressed both (no change) */
178 { 0, 0, -1 }, /* timeout N/A */
184 EvdevMBEmuTimer(InputInfoPtr pInfo)
186 EvdevPtr pEvdev = pInfo->private;
190 sigstate = xf86BlockSIGIO ();
192 pEvdev->emulateMB.pending = FALSE;
193 if ((id = stateTab[pEvdev->emulateMB.state][4][0]) != 0) {
194 EvdevPostButtonEvent(pInfo, abs(id),
195 (id >= 0) ? BUTTON_PRESS : BUTTON_RELEASE);
196 pEvdev->emulateMB.state =
197 stateTab[pEvdev->emulateMB.state][4][2];
199 ErrorF("Got unexpected buttonTimer in state %d\n",
200 pEvdev->emulateMB.state);
203 xf86UnblockSIGIO (sigstate);
209 * Emulate a middle button on button press.
211 * @param code button number (1 for left, 3 for right)
212 * @param press TRUE if press, FALSE if release.
214 * @return TRUE if event was swallowed by middle mouse button emulation, FALSE
218 EvdevMBEmuFilterEvent(InputInfoPtr pInfo, int button, BOOL press)
220 EvdevPtr pEvdev = pInfo->private;
225 if (!pEvdev->emulateMB.enabled)
228 /* don't care about other buttons */
229 if (button != 1 && button != 3)
232 btstate = &pEvdev->emulateMB.buttonstate;
234 *btstate |= (button == 1) ? 0x1 : 0x2;
236 *btstate &= (button == 1) ? ~0x1 : ~0x2;
238 if ((id = stateTab[pEvdev->emulateMB.state][*btstate][0]) != 0)
240 EvdevQueueButtonEvent(pInfo, abs(id), (id >= 0));
243 if ((id = stateTab[pEvdev->emulateMB.state][*btstate][1]) != 0)
245 EvdevQueueButtonEvent(pInfo, abs(id), (id >= 0));
249 pEvdev->emulateMB.state =
250 stateTab[pEvdev->emulateMB.state][*btstate][2];
252 if (stateTab[pEvdev->emulateMB.state][4][0] != 0) {
253 pEvdev->emulateMB.expires = GetTimeInMillis () + pEvdev->emulateMB.timeout;
254 pEvdev->emulateMB.pending = TRUE;
257 pEvdev->emulateMB.pending = FALSE;
264 void EvdevMBEmuWakeupHandler(pointer data,
266 pointer LastSelectMask)
268 InputInfoPtr pInfo = (InputInfoPtr)data;
269 EvdevPtr pEvdev = (EvdevPtr)pInfo->private;
272 if (pEvdev->emulateMB.pending)
274 ms = pEvdev->emulateMB.expires - GetTimeInMillis();
276 EvdevMBEmuTimer(pInfo);
280 void EvdevMBEmuBlockHandler(pointer data,
281 struct timeval **waitTime,
282 pointer LastSelectMask)
284 InputInfoPtr pInfo = (InputInfoPtr) data;
285 EvdevPtr pEvdev= (EvdevPtr) pInfo->private;
288 if (pEvdev->emulateMB.pending)
290 ms = pEvdev->emulateMB.expires - GetTimeInMillis ();
293 AdjustWaitForDelay (waitTime, ms);
298 EvdevMBEmuPreInit(InputInfoPtr pInfo)
300 EvdevPtr pEvdev = (EvdevPtr)pInfo->private;
302 pEvdev->emulateMB.enabled = xf86SetBoolOption(pInfo->options,
305 pEvdev->emulateMB.timeout = xf86SetIntOption(pInfo->options,
306 "Emulate3Timeout", 50);
310 EvdevMBEmuOn(InputInfoPtr pInfo)
312 if (!pInfo->dev->button) /* don't init for keyboards */
315 RegisterBlockAndWakeupHandlers (EvdevMBEmuBlockHandler,
316 EvdevMBEmuWakeupHandler,
321 EvdevMBEmuFinalize(InputInfoPtr pInfo)
323 if (!pInfo->dev->button) /* don't cleanup for keyboards */
326 RemoveBlockAndWakeupHandlers (EvdevMBEmuBlockHandler,
327 EvdevMBEmuWakeupHandler,
333 EvdevMBEmuSetProperty(DeviceIntPtr dev, Atom atom, XIPropertyValuePtr val,
336 InputInfoPtr pInfo = dev->public.devicePrivate;
337 EvdevPtr pEvdev = pInfo->private;
339 if (atom == prop_mbemu)
341 if (val->format != 8 || val->size != 1 || val->type != XA_INTEGER)
345 pEvdev->emulateMB.enabled = *((BOOL*)val->data);
346 } else if (atom == prop_mbtimeout)
348 if (val->format != 32 || val->size != 1 || val->type != XA_INTEGER)
352 pEvdev->emulateMB.timeout = *((CARD32*)val->data);
359 * Initialise property for MB emulation on/off.
362 EvdevMBEmuInitProperty(DeviceIntPtr dev)
364 InputInfoPtr pInfo = dev->public.devicePrivate;
365 EvdevPtr pEvdev = pInfo->private;
368 if (!dev->button) /* don't init prop for keyboards */
371 prop_mbemu = MakeAtom(EVDEV_PROP_MIDBUTTON, strlen(EVDEV_PROP_MIDBUTTON), TRUE);
372 rc = XIChangeDeviceProperty(dev, prop_mbemu, XA_INTEGER, 8,
374 &pEvdev->emulateMB.enabled,
378 XISetDevicePropertyDeletable(dev, prop_mbemu, FALSE);
380 prop_mbtimeout = MakeAtom(EVDEV_PROP_MIDBUTTON_TIMEOUT,
381 strlen(EVDEV_PROP_MIDBUTTON_TIMEOUT),
383 rc = XIChangeDeviceProperty(dev, prop_mbtimeout, XA_INTEGER, 32, PropModeReplace, 1,
384 &pEvdev->emulateMB.timeout, FALSE);
388 XISetDevicePropertyDeletable(dev, prop_mbtimeout, FALSE);
390 XIRegisterPropertyHandler(dev, EvdevMBEmuSetProperty, NULL, NULL);