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