Initialize Tizen 2.3
[adaptation/xorg/driver/xserver-xorg-input-evdev.git] / src / emuThird.c
1 /*
2  * Copyright © 2011 Red Hat, Inc.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software
5  * and its documentation for any purpose is hereby granted without
6  * fee, provided that the above copyright notice appear in all copies
7  * and that both that copyright notice and this permission notice
8  * appear in supporting documentation, and that the name of the authors
9  * not be used in advertising or publicity pertaining to distribution of the
10  * software without specific, written prior permission.  The authors make no
11  * representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied
13  * warranty.
14  *
15  * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
17  * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
19  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
20  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  */
24
25 /* Right mouse button emulation code.
26  * Emulates a right button event if the first button is held down for a
27  * timeout. If the device moves more than a certain amount before the
28  * timeout is over, the emulation is cancelled and a normal button event is
29  * generated.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include "evdev.h"
37
38 #include <X11/Xatom.h>
39 #include <xf86.h>
40 #include <xf86Xinput.h>
41 #include <exevents.h>
42
43 #include <evdev-properties.h>
44
45 /* Threshold (in device coordinates) for devices to cancel emulation */
46 #define DEFAULT_MOVE_THRESHOLD 20
47
48 static Atom prop_3bemu;         /* Right button emulation on/off property   */
49 static Atom prop_3btimeout;     /* Right button timeout property            */
50 static Atom prop_3bbutton;      /* Right button target physical button      */
51 static Atom prop_3bthreshold;   /* Right button move cancellation threshold */
52
53 /* State machine for 3rd button emulation */
54 enum EmulationState {
55     EM3B_OFF,             /* no event      */
56     EM3B_PENDING,         /* timer pending */
57     EM3B_EMULATING        /* in emulation  */
58 };
59
60 static void
61 Evdev3BEmuPostButtonEvent(InputInfoPtr pInfo, int button, enum ButtonAction act)
62 {
63     EvdevPtr          pEvdev   = pInfo->private;
64     struct emulate3B *emu3B    = &pEvdev->emulate3B;
65     int               absolute = Relative;
66
67     /* if we cancel, emit the button down event at our start position,
68      * not at the current position. Only for absolute devices though. For
69      * relative events, this may be a bit iffy since pointer accel may shoot
70      * us back more than we moved and confuse the user.
71      */
72     if (emu3B->flags & EVDEV_ABSOLUTE_EVENTS)
73         absolute = Absolute;
74
75     xf86PostButtonEventP(pInfo->dev, absolute, button,
76                          (act == BUTTON_PRESS) ? 1 : 0, 0,
77                          (absolute ? 2 : 0), emu3B->startpos);
78 }
79
80
81 /**
82  * Timer function. Post a button down event to the server.
83  *
84  * @param arg The InputInfoPtr for this device.
85  */
86 CARD32
87 Evdev3BEmuTimer(OsTimerPtr timer, CARD32 time, pointer arg)
88 {
89     InputInfoPtr      pInfo    = (InputInfoPtr)arg;
90     EvdevPtr          pEvdev   = pInfo->private;
91     struct emulate3B *emu3B    = &pEvdev->emulate3B;
92     int               sigstate = 0;
93
94     sigstate = xf86BlockSIGIO ();
95     emu3B->state = EM3B_EMULATING;
96     Evdev3BEmuPostButtonEvent(pInfo, emu3B->button, BUTTON_PRESS);
97     xf86UnblockSIGIO (sigstate);
98     return 0;
99 }
100
101
102 /**
103  * Cancel all emulation, reset the timer and reset deltas.
104  */
105 static void
106 Evdev3BCancel(InputInfoPtr pInfo)
107 {
108     EvdevPtr          pEvdev = pInfo->private;
109     struct emulate3B *emu3B  = &pEvdev->emulate3B;
110
111     if (emu3B->state != EM3B_OFF)
112     {
113         TimerCancel(emu3B->timer);
114         emu3B->state = EM3B_OFF;
115         memset(emu3B->delta, 0, sizeof(emu3B->delta));
116     }
117
118     emu3B->flags = 0;
119 }
120
121 /**
122  * Emulate a third button on button press. Note that emulation only triggers
123  * on button 1.
124  *
125  * Return TRUE if event was swallowed by middle mouse button emulation,
126  * FALSE otherwise.
127  */
128 BOOL
129 Evdev3BEmuFilterEvent(InputInfoPtr pInfo, int button, BOOL press)
130 {
131     EvdevPtr          pEvdev = pInfo->private;
132     struct emulate3B *emu3B  = &pEvdev->emulate3B;
133     int               ret    = FALSE;
134
135     if (!emu3B->enabled)
136         goto out;
137
138     if (press)
139         emu3B->buttonstate |= button;
140     else
141         emu3B->buttonstate &= ~button;
142
143     /* Any other button pressed? Cancel timer */
144     if (button != 1)
145     {
146         switch (emu3B->state)
147         {
148             case EM3B_PENDING:
149                 Evdev3BEmuPostButtonEvent(pInfo, 1, BUTTON_PRESS);
150                 Evdev3BCancel(pInfo);
151                 break;
152             case EM3B_EMULATING:
153                 /* We're emulating and now the user pressed a different
154                  * button. Just release the emulating one, tell the user to
155                  * not do that and get on with life */
156                 Evdev3BEmuPostButtonEvent(pInfo, emu3B->button, BUTTON_RELEASE);
157                 Evdev3BCancel(pInfo);
158                 break;
159             default:
160                 break;
161         }
162         goto out;
163     }
164
165     /* Don't emulate if any other button is down */
166     if ((emu3B->buttonstate & ~0x1) != 0)
167         goto out;
168
169     /* Release event → cancel, send press and release now. */
170     if (!press)
171     {
172         switch(emu3B->state)
173         {
174             case EM3B_PENDING:
175                 Evdev3BEmuPostButtonEvent(pInfo, 1, BUTTON_PRESS);
176                 Evdev3BCancel(pInfo);
177                 break;
178             case EM3B_EMULATING:
179                 Evdev3BEmuPostButtonEvent(pInfo, emu3B->button, BUTTON_RELEASE);
180                 Evdev3BCancel(pInfo);
181                 ret = TRUE;
182                 break;
183             default:
184                 break;
185         }
186
187         goto out;
188     }
189
190     if (press && emu3B->state == EM3B_OFF)
191     {
192         emu3B->state = EM3B_PENDING;
193         emu3B->timer = TimerSet(emu3B->timer, 0, emu3B->timeout,
194                                 Evdev3BEmuTimer, pInfo);
195         ret = TRUE;
196         goto out;
197     }
198
199 out:
200     return ret;
201 }
202
203 /**
204  * Handle absolute x/y motion. If the motion is above the threshold, cancel
205  * emulation.
206  */
207 void
208 Evdev3BEmuProcessAbsMotion(InputInfoPtr pInfo, ValuatorMask *vals)
209 {
210     EvdevPtr          pEvdev = pInfo->private;
211     struct emulate3B *emu3B  = &pEvdev->emulate3B;
212     int               cancel = FALSE;
213     int               axis   = 0;
214
215     if (emu3B->state != EM3B_PENDING)
216     {
217         if (valuator_mask_isset(vals, 0))
218             emu3B->startpos[0] = valuator_mask_get(vals, 0);
219         if (valuator_mask_isset(vals, 1))
220             emu3B->startpos[1] = valuator_mask_get(vals, 1);
221
222         return;
223     }
224
225     if ((emu3B->flags & EVDEV_ABSOLUTE_EVENTS) == 0)
226         emu3B->flags |= EVDEV_ABSOLUTE_EVENTS;
227
228     while (axis <= 1 && !cancel)
229     {
230         if (valuator_mask_isset(vals, axis))
231         {
232             int delta = valuator_mask_get(vals, axis) - emu3B->startpos[axis];
233             if (abs(delta) > emu3B->threshold)
234                 cancel = TRUE;
235         }
236         axis++;
237     }
238
239     if (cancel)
240     {
241         Evdev3BEmuPostButtonEvent(pInfo, 1, BUTTON_PRESS);
242         Evdev3BCancel(pInfo);
243     }
244 }
245
246 /**
247  * Handle relative x/y motion. If the motion is above the threshold, cancel
248  * emulation.
249  */
250 void
251 Evdev3BEmuProcessRelMotion(InputInfoPtr pInfo, int dx, int dy)
252 {
253     EvdevPtr          pEvdev = pInfo->private;
254     struct emulate3B *emu3B  = &pEvdev->emulate3B;
255
256     if (emu3B->state != EM3B_PENDING)
257         return;
258
259     emu3B->delta[0] += dx;
260     emu3B->delta[1] += dy;
261     emu3B->flags |= EVDEV_RELATIVE_EVENTS;
262
263     if (abs(emu3B->delta[0]) > emu3B->threshold ||
264         abs(emu3B->delta[1]) > emu3B->threshold)
265     {
266         Evdev3BEmuPostButtonEvent(pInfo, 1, BUTTON_PRESS);
267         Evdev3BCancel(pInfo);
268     }
269 }
270
271 void
272 Evdev3BEmuPreInit(InputInfoPtr pInfo)
273 {
274     EvdevPtr          pEvdev = pInfo->private;
275     struct emulate3B *emu3B  = &pEvdev->emulate3B;
276
277     emu3B->enabled = xf86SetBoolOption(pInfo->options,
278                                        "EmulateThirdButton",
279                                        FALSE);
280     emu3B->timeout = xf86SetIntOption(pInfo->options,
281                                       "EmulateThirdButtonTimeout",
282                                       1000);
283     emu3B->button = xf86SetIntOption(pInfo->options,
284                                       "EmulateThirdButtonButton",
285                                       3);
286     /* FIXME: this should be auto-configured based on axis ranges */
287     emu3B->threshold = xf86SetIntOption(pInfo->options,
288                                          "EmulateThirdButtonMoveThreshold",
289                                          DEFAULT_MOVE_THRESHOLD);
290     /* allocate now so we don't allocate in the signal handler */
291     emu3B->timer = TimerSet(NULL, 0, 0, NULL, NULL);
292 }
293
294 void
295 Evdev3BEmuOn(InputInfoPtr pInfo)
296 {
297     /* This function just exists for symmetry in evdev.c */
298 }
299
300 void
301 Evdev3BEmuFinalize(InputInfoPtr pInfo)
302 {
303     EvdevPtr          pEvdev = pInfo->private;
304     struct emulate3B *emu3B  = &pEvdev->emulate3B;
305
306     TimerFree(emu3B->timer);
307     emu3B->timer = NULL;
308 }
309
310 static int
311 Evdev3BEmuSetProperty(DeviceIntPtr dev, Atom atom, XIPropertyValuePtr val,
312                       BOOL checkonly)
313 {
314     InputInfoPtr      pInfo  = dev->public.devicePrivate;
315     EvdevPtr          pEvdev = pInfo->private;
316     struct emulate3B *emu3B  = &pEvdev->emulate3B;
317
318     if (atom == prop_3bemu)
319     {
320         if (val->format != 8 || val->size != 1 || val->type != XA_INTEGER)
321             return BadMatch;
322
323         if (!checkonly)
324             emu3B->enabled = *((BOOL*)val->data);
325
326     } else if (atom == prop_3btimeout)
327     {
328         if (val->format != 32 || val->size != 1 || val->type != XA_INTEGER)
329             return BadMatch;
330
331         if (!checkonly)
332             emu3B->timeout = *((CARD32*)val->data);
333
334     } else if (atom == prop_3bbutton)
335     {
336         if (val->format != 8 || val->size != 1 || val->type != XA_INTEGER)
337             return BadMatch;
338
339         if (!checkonly)
340             emu3B->button = *((CARD8*)val->data);
341     } else if (atom == prop_3bthreshold)
342     {
343         if (val->format != 32 || val->size != 1 || val->type != XA_INTEGER)
344             return BadMatch;
345
346         if (!checkonly)
347             emu3B->threshold = *((CARD32*)val->data);
348     }
349
350
351     return Success;
352 }
353
354 /**
355  * Initialise properties for third button emulation
356  */
357 void
358 Evdev3BEmuInitProperty(DeviceIntPtr dev)
359 {
360     InputInfoPtr      pInfo  = dev->public.devicePrivate;
361     EvdevPtr          pEvdev = pInfo->private;
362     struct emulate3B *emu3B  = &pEvdev->emulate3B;
363     int               rc;
364
365     if (!dev->button) /* don't init prop for keyboards */
366         return;
367
368     /* third button emulation on/off */
369     prop_3bemu = MakeAtom(EVDEV_PROP_THIRDBUTTON, strlen(EVDEV_PROP_THIRDBUTTON), TRUE);
370     rc = XIChangeDeviceProperty(dev, prop_3bemu, XA_INTEGER, 8,
371                                 PropModeReplace, 1,
372                                 &emu3B->enabled,
373                                 FALSE);
374     if (rc != Success)
375         return;
376
377     XISetDevicePropertyDeletable(dev, prop_3bemu, FALSE);
378
379     /* third button emulation timeout */
380     prop_3btimeout = MakeAtom(EVDEV_PROP_THIRDBUTTON_TIMEOUT,
381                               strlen(EVDEV_PROP_THIRDBUTTON_TIMEOUT),
382                               TRUE);
383     rc = XIChangeDeviceProperty(dev, prop_3btimeout, XA_INTEGER, 32, PropModeReplace, 1,
384                                 &emu3B->timeout, FALSE);
385
386     if (rc != Success)
387         return;
388
389     XISetDevicePropertyDeletable(dev, prop_3btimeout, FALSE);
390
391     /* third button emulation button to be triggered  */
392     prop_3bbutton = MakeAtom(EVDEV_PROP_THIRDBUTTON_BUTTON,
393                              strlen(EVDEV_PROP_THIRDBUTTON_BUTTON),
394                              TRUE);
395     rc = XIChangeDeviceProperty(dev, prop_3bbutton, XA_INTEGER, 8, PropModeReplace, 1,
396                                 &emu3B->button, FALSE);
397
398     if (rc != Success)
399         return;
400
401     XISetDevicePropertyDeletable(dev, prop_3bbutton, FALSE);
402
403     /* third button emulation movement threshold */
404     prop_3bthreshold = MakeAtom(EVDEV_PROP_THIRDBUTTON_THRESHOLD,
405                                 strlen(EVDEV_PROP_THIRDBUTTON_THRESHOLD),
406                                 TRUE);
407     rc = XIChangeDeviceProperty(dev, prop_3bthreshold, XA_INTEGER, 32, PropModeReplace, 1,
408                                 &emu3B->threshold, FALSE);
409
410     if (rc != Success)
411         return;
412
413     XISetDevicePropertyDeletable(dev, prop_3bthreshold, FALSE);
414
415     XIRegisterPropertyHandler(dev, Evdev3BEmuSetProperty, NULL, NULL);
416 }