Git init
[profile/ivi/xorg-x11-drv-evdev.git] / src / emuMB.c
1 /*
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
9  *
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
19  * warranty.
20  *
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.
28  *
29  */
30
31 /* Middle mouse button emulation code. */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <X11/Xatom.h>
38 #include <xf86.h>
39 #include <xf86Xinput.h>
40 #include <exevents.h>
41
42 #include <evdev-properties.h>
43 #include "evdev.h"
44
45 enum {
46     MBEMU_DISABLED = 0,
47     MBEMU_ENABLED,
48     MBEMU_AUTO
49 };
50
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 */
54 #endif
55 /*
56  * Lets create a simple finite-state machine for 3 button emulation:
57  *
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
70  *
71  * At each state, we need handlers for the following events
72  *   0: no buttons down
73  *   1: left button down
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
80  * handled.
81  *
82  * a handler consists of three values:
83  *   0: action1
84  *   1: action2
85  *   2: new emulation state
86  *
87  * action > 0: ButtonPress
88  * action = 0: nothing
89  * action < 0: ButtonRelease
90  *
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>.
97  */
98 static signed char stateTab[11][5][3] = {
99 /* 0 ground */
100   {
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 */
106   },
107 /* 1 delayed left */
108   {
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 */
114   },
115 /* 2 delayed right */
116   {
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 */
122   },
123 /* 3 pressed middle */
124   {
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 */
130   },
131 /* 4 pressed left */
132   {
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 */
138   },
139 /* 5 pressed right */
140   {
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 */
146   },
147 /* 6 released left */
148   {
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 */
154   },
155 /* 7 released right */
156   {
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 */
162   },
163 /* 8 repressed left */
164   {
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 */
170   },
171 /* 9 repressed right */
172   {
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 */
178   },
179 /* 10 pressed both */
180   {
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 */
186   },
187 };
188
189
190 int
191 EvdevMBEmuTimer(InputInfoPtr pInfo)
192 {
193     EvdevPtr pEvdev = pInfo->private;
194     int sigstate;
195     int id;
196
197     sigstate = xf86BlockSIGIO ();
198
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];
204     } else {
205         ErrorF("Got unexpected buttonTimer in state %d\n",
206                 pEvdev->emulateMB.state);
207     }
208
209     xf86UnblockSIGIO (sigstate);
210     return 0;
211 }
212
213
214 /**
215  * Emulate a middle button on button press.
216  *
217  * @param code button number (1 for left, 3 for right)
218  * @param press TRUE if press, FALSE if release.
219  *
220  * @return TRUE if event was swallowed by middle mouse button emulation, FALSE
221  * otherwise.
222  */
223 BOOL
224 EvdevMBEmuFilterEvent(InputInfoPtr pInfo, int button, BOOL press)
225 {
226     EvdevPtr pEvdev = pInfo->private;
227     int id;
228     int *btstate;
229     int ret = FALSE;
230
231     if (!pEvdev->emulateMB.enabled)
232         return ret;
233
234     if (button == 2) {
235         EvdevMBEmuEnable(pInfo, FALSE);
236         return ret;
237     }
238
239     /* don't care about other buttons */
240     if (button != 1 && button != 3)
241         return ret;
242
243     btstate = &pEvdev->emulateMB.buttonstate;
244     if (press)
245         *btstate |= (button == 1) ? 0x1 : 0x2;
246     else
247         *btstate &= (button == 1) ? ~0x1 : ~0x2;
248
249     if ((id = stateTab[pEvdev->emulateMB.state][*btstate][0]) != 0)
250     {
251         EvdevQueueButtonEvent(pInfo, abs(id), (id >= 0));
252         ret = TRUE;
253     }
254     if ((id = stateTab[pEvdev->emulateMB.state][*btstate][1]) != 0)
255     {
256         EvdevQueueButtonEvent(pInfo, abs(id), (id >= 0));
257         ret = TRUE;
258     }
259
260     pEvdev->emulateMB.state =
261         stateTab[pEvdev->emulateMB.state][*btstate][2];
262
263     if (stateTab[pEvdev->emulateMB.state][4][0] != 0) {
264         pEvdev->emulateMB.expires = GetTimeInMillis () + pEvdev->emulateMB.timeout;
265         pEvdev->emulateMB.pending = TRUE;
266         ret = TRUE;
267     } else {
268         pEvdev->emulateMB.pending = FALSE;
269     }
270
271     return ret;
272 }
273
274
275 void EvdevMBEmuWakeupHandler(pointer data,
276                              int i,
277                              pointer LastSelectMask)
278 {
279     InputInfoPtr pInfo = (InputInfoPtr)data;
280     EvdevPtr     pEvdev = (EvdevPtr)pInfo->private;
281     int ms;
282
283     if (pEvdev->emulateMB.pending)
284     {
285         ms = pEvdev->emulateMB.expires - GetTimeInMillis();
286         if (ms <= 0)
287             EvdevMBEmuTimer(pInfo);
288     }
289 }
290
291 void EvdevMBEmuBlockHandler(pointer data,
292                             struct timeval **waitTime,
293                             pointer LastSelectMask)
294 {
295     InputInfoPtr    pInfo = (InputInfoPtr) data;
296     EvdevPtr        pEvdev= (EvdevPtr) pInfo->private;
297     int             ms;
298
299     if (pEvdev->emulateMB.pending)
300     {
301         ms = pEvdev->emulateMB.expires - GetTimeInMillis ();
302         if (ms <= 0)
303             ms = 0;
304         AdjustWaitForDelay (waitTime, ms);
305     }
306 }
307
308 void
309 EvdevMBEmuPreInit(InputInfoPtr pInfo)
310 {
311     EvdevPtr pEvdev = (EvdevPtr)pInfo->private;
312     pEvdev->emulateMB.enabled = MBEMU_AUTO;
313
314     if (xf86FindOption(pInfo->options, "Emulate3Buttons"))
315     {
316         pEvdev->emulateMB.enabled = xf86SetBoolOption(pInfo->options,
317                                                       "Emulate3Buttons",
318                                                       MBEMU_ENABLED);
319         xf86Msg(X_INFO, "%s: Forcing middle mouse button emulation %s.\n",
320                 pInfo->name, (pEvdev->emulateMB.enabled) ? "on" : "off");
321     }
322
323     pEvdev->emulateMB.timeout = xf86SetIntOption(pInfo->options,
324                                                  "Emulate3Timeout", 50);
325 }
326
327 void
328 EvdevMBEmuOn(InputInfoPtr pInfo)
329 {
330     if (!pInfo->dev->button) /* don't init for keyboards */
331         return;
332
333     RegisterBlockAndWakeupHandlers (EvdevMBEmuBlockHandler,
334                                     EvdevMBEmuWakeupHandler,
335                                     (pointer)pInfo);
336 }
337
338 void
339 EvdevMBEmuFinalize(InputInfoPtr pInfo)
340 {
341     if (!pInfo->dev->button) /* don't cleanup for keyboards */
342         return;
343
344     RemoveBlockAndWakeupHandlers (EvdevMBEmuBlockHandler,
345                                   EvdevMBEmuWakeupHandler,
346                                   (pointer)pInfo);
347
348 }
349
350 /* Enable/disable middle mouse button emulation. */
351 void
352 EvdevMBEmuEnable(InputInfoPtr pInfo, BOOL enable)
353 {
354     EvdevPtr pEvdev = (EvdevPtr)pInfo->private;
355     if (pEvdev->emulateMB.enabled == MBEMU_AUTO)
356         pEvdev->emulateMB.enabled = enable;
357 }
358
359
360 #ifdef HAVE_PROPERTIES
361 static int
362 EvdevMBEmuSetProperty(DeviceIntPtr dev, Atom atom, XIPropertyValuePtr val,
363                       BOOL checkonly)
364 {
365     InputInfoPtr pInfo  = dev->public.devicePrivate;
366     EvdevPtr     pEvdev = pInfo->private;
367
368     if (atom == prop_mbemu)
369     {
370         if (val->format != 8 || val->size != 1 || val->type != XA_INTEGER)
371             return BadMatch;
372
373         if (!checkonly)
374             pEvdev->emulateMB.enabled = *((BOOL*)val->data);
375     } else if (atom == prop_mbtimeout)
376     {
377         if (val->format != 32 || val->size != 1 || val->type != XA_INTEGER)
378             return BadMatch;
379
380         if (!checkonly)
381             pEvdev->emulateMB.timeout = *((CARD32*)val->data);
382     }
383
384     return Success;
385 }
386
387 /**
388  * Initialise property for MB emulation on/off.
389  */
390 void
391 EvdevMBEmuInitProperty(DeviceIntPtr dev)
392 {
393     InputInfoPtr pInfo  = dev->public.devicePrivate;
394     EvdevPtr     pEvdev = pInfo->private;
395     int          rc;
396
397     if (!dev->button) /* don't init prop for keyboards */
398         return;
399
400     prop_mbemu = MakeAtom(EVDEV_PROP_MIDBUTTON, strlen(EVDEV_PROP_MIDBUTTON), TRUE);
401     rc = XIChangeDeviceProperty(dev, prop_mbemu, XA_INTEGER, 8,
402                                 PropModeReplace, 1,
403                                 &pEvdev->emulateMB.enabled,
404                                 FALSE);
405     if (rc != Success)
406         return;
407     XISetDevicePropertyDeletable(dev, prop_mbemu, FALSE);
408
409     prop_mbtimeout = MakeAtom(EVDEV_PROP_MIDBUTTON_TIMEOUT,
410                               strlen(EVDEV_PROP_MIDBUTTON_TIMEOUT),
411                               TRUE);
412     rc = XIChangeDeviceProperty(dev, prop_mbtimeout, XA_INTEGER, 32, PropModeReplace, 1,
413                                 &pEvdev->emulateMB.timeout, FALSE);
414
415     if (rc != Success)
416         return;
417     XISetDevicePropertyDeletable(dev, prop_mbtimeout, FALSE);
418
419     XIRegisterPropertyHandler(dev, EvdevMBEmuSetProperty, NULL, NULL);
420 }
421 #endif