87014a86d88665c38bee1247f30f2ec77e131097
[adaptation/xorg/driver/xserver-xorg-input-evdev-multitouch.git] / src / evdevmultitouch.h
1 /*
2  * xserver-xorg-input-evdev-multitouch
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Sung-Jin Park <sj76.park@samsung.com>
7  *          Sangjin LEE <lsj119@samsung.com>
8  *
9  * Permission to use, copy, modify, distribute, and sell this software
10  * and its documentation for any purpose is hereby granted without
11  * fee, provided that the above copyright notice appear in all copies
12  * and that both that copyright notice and this permission notice
13  * appear in supporting documentation, and that the name of Red Hat
14  * not be used in advertising or publicity pertaining to distribution
15  * of the software without specific, written prior permission.  Red
16  * Hat makes no representations about the suitability of this software
17  * for any purpose.  It is provided "as is" without express or implied
18  * warranty.
19  *
20  * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
21  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
22  * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
23  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
24  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
25  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
26  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27  *
28  * Copyright © 2004-2008 Red Hat, Inc.
29  * Copyright © 2008 University of South Australia
30  *
31  * Permission to use, copy, modify, distribute, and sell this software
32  * and its documentation for any purpose is hereby granted without
33  * fee, provided that the above copyright notice appear in all copies
34  * and that both that copyright notice and this permission notice
35  * appear in supporting documentation, and that the name of Red Hat
36  * not be used in advertising or publicity pertaining to distribution
37  * of the software without specific, written prior permission.  Red
38  * Hat makes no representations about the suitability of this software
39  * for any purpose.  It is provided "as is" without express or implied
40  * warranty.
41  *
42  * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
43  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
44  * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
45  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
46  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
47  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
48  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
49  *
50  * Authors:
51  *      Kristian Høgsberg (krh@redhat.com)
52  *      Adam Jackson (ajax@redhat.com)
53  *      Peter Hutterer (peter@cs.unisa.edu.au)
54  *      Oliver McFadden (oliver.mcfadden@nokia.com)
55  *      Benjamin Tissoires (tissoire@cena.fr)
56  */
57
58 #ifndef EVDEVMULTITOUCH_H
59 #define EVDEVMULTITOUCH_H
60
61 #include <linux/input.h>
62 #include <linux/types.h>
63
64 #include <xf86Xinput.h>
65 #include <xf86_OSproc.h>
66 #include <xkbstr.h>
67
68 #include <math.h>
69 #include <pixman.h>
70
71 #ifdef _F_GESTURE_EXTENSION_
72 typedef enum _MTSyncType
73 {
74         MTOUCH_FRAME_SYNC_END,
75         MTOUCH_FRAME_SYNC_BEGIN
76 } MTSyncType;
77
78 enum EventType
79 {
80     ET_MTSync = 0x7E,
81     ET_Internal = 0xFF /* First byte */
82 };
83
84 typedef struct _AnyEvent AnyEvent;
85 struct _AnyEvent
86 {
87     unsigned char header; /**< Always ET_Internal */
88     enum EventType type;  /**< One of EventType */
89     int length;           /**< Length in bytes */
90     Time time;            /**< Time in ms */
91     int deviceid;
92     MTSyncType sync;
93     int x;
94     int y;
95 };
96
97 union _InternalEvent {
98         struct {
99             unsigned char header; /**< Always ET_Internal */
100             enum EventType type;  /**< One of ET_* */
101             int length;           /**< Length in bytes */
102             Time time;            /**< Time in ms. */
103         } any;
104         AnyEvent any_event;
105 };
106 #endif//_F_GESTURE_EXTENSION_
107
108 #ifndef EV_CNT /* linux 2.4 kernels and earlier lack _CNT defines */
109 #define EV_CNT (EV_MAX+1)
110 #endif
111 #ifndef KEY_CNT
112 #define KEY_CNT (KEY_MAX+1)
113 #endif
114 #ifndef REL_CNT
115 #define REL_CNT (REL_MAX+1)
116 #endif
117 #ifndef ABS_CNT
118 #define ABS_CNT (ABS_MAX+1)
119 #endif
120 #ifndef LED_CNT
121 #define LED_CNT (LED_MAX+1)
122 #endif
123
124 #define EVDEVMULTITOUCH_MAXBUTTONS 32
125 #define EVDEVMULTITOUCH_MAXQUEUE 32
126
127 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 3
128 #define HAVE_PROPERTIES 1
129 #endif
130
131 #ifndef MAX_VALUATORS
132 #define MAX_VALUATORS 36
133 #endif
134
135 #define MAX_VALUATORS_MT 5 //currently if MAXDEVICES == 40
136
137 #define DEFAULT_TIMEOUT 100
138
139 #define EVDEVMULTITOUCH_PROP_TRACKING_ID "EvdevMultitouch Tracking ID"
140 #define EVDEVMULTITOUCH_PROP_MULTITOUCH_SUBDEVICES "EvdevMultitouch MultiTouch"
141 #define EVDEVMULTITOUCH_PROP_USE_POLL   "EvdevMultitouch Use Poll"
142 #define EVDEVMULTITOUCH_PROP_TRANSFORM  "EvdevMultitouch Transform Matrix"
143 #define EVDEVMULTITOUCH_PROP_GRABINFO   "EvdevMultitouch Grab Info"
144
145 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 5
146 typedef struct {
147     char *rules;
148     char *model;
149     char *layout;
150     char *variant;
151     char *options;
152 } XkbRMLVOSet;
153 #endif
154
155
156 #define LONG_BITS (sizeof(long) * 8)
157
158 /* Number of longs needed to hold the given number of bits */
159 #define NLONGS(x) (((x) + LONG_BITS - 1) / LONG_BITS)
160
161 /* axis specific data for wheel emulation */
162 typedef struct {
163     int up_button;
164     int down_button;
165     int traveled_distance;
166 } WheelAxis, *WheelAxisPtr;
167
168 /* Event queue used to defer keyboard/button events until EV_SYN time. */
169 typedef struct {
170     enum {
171         EV_QUEUE_KEY,   /* xf86PostKeyboardEvent() */
172         EV_QUEUE_BTN,   /* xf86PostButtonEvent() */
173     } type;
174     int key;            /* May be either a key code or button number. */
175     int val;            /* State of the key/button; pressed or released. */
176 } EventQueueRec, *EventQueuePtr;
177
178 typedef struct _EvdevMultitouchDataMTRec{
179     int id;
180     int slot;
181     int abs;
182    BOOL containsValues;
183     Time expires;
184     int vals[MAX_VALUATORS];
185     InputInfoPtr pInfo;
186 } EvdevMultitouchDataMTRec, *EvdevMultitouchDataMTPtr;
187
188
189 /**
190  * EvdevMultitouch device information, including list of current object
191  */
192 typedef struct {
193     const char *device;
194     int grabDevice;         /* grab the event device? */
195
196     int num_vals;           /* number of valuators */
197     int axis_map[max(ABS_CNT, REL_CNT)]; /* Map evdevmultitouch <axis> to index */
198     int vals[MAX_VALUATORS];
199     int old_vals[MAX_VALUATORS]; /* Translate absolute inputs to relative */
200     EvdevMultitouchDataMTRec vals_mt[MAX_VALUATORS_MT];
201
202     int flags;
203     int tool;
204     int num_buttons;            /* number of buttons */
205     BOOL swap_axes;
206     BOOL invert_x;
207     BOOL invert_y;
208     int num_multitouch;
209     OsTimerPtr multitouch_setting_timer;
210
211     int delta[REL_CNT];
212     unsigned int abs, rel, mt;
213
214     /* XKB stuff has to be per-device rather than per-driver */
215 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 5
216     XkbComponentNamesRec    xkbnames;
217 #endif
218     XkbRMLVOSet rmlvo;
219
220     /* Middle mouse button emulation */
221     struct {
222         BOOL                enabled;
223         BOOL                pending;     /* timer waiting? */
224         int                 buttonstate; /* phys. button state */
225         int                 state;       /* state machine (see bt3emu.c) */
226         Time                expires;     /* time of expiry */
227         Time                timeout;
228     } emulateMB;
229     struct {
230         int                 meta;           /* meta key to lock any button */
231         BOOL                meta_state;     /* meta_button state */
232         unsigned int        lock_pair[EVDEVMULTITOUCH_MAXBUTTONS];  /* specify a meta/lock pair */
233         BOOL                lock_state[EVDEVMULTITOUCH_MAXBUTTONS]; /* state of any locked buttons */
234     } dragLock;
235     struct {
236         BOOL                enabled;
237         int                 button;
238         int                 button_state;
239         int                 inertia;
240         WheelAxis           X;
241         WheelAxis           Y;
242         Time                expires;     /* time of expiry */
243         Time                timeout;
244     } emulateWheel;
245     /* run-time calibration */
246     struct {
247         int                 min_x;
248         int                 max_x;
249         int                 min_y;
250         int                 max_y;
251     } calibration;
252
253     struct {
254         int                 min_x;
255         int                 max_x;
256         int                 min_y;
257         int                 max_y;
258     } resolution;
259
260     unsigned char btnmap[32];           /* config-file specified button mapping */
261
262     int reopen_attempts; /* max attempts to re-open after read failure */
263     int reopen_left;     /* number of attempts left to re-open the device */
264     OsTimerPtr reopen_timer;
265
266     /* Cached info from device. */
267     char name[1024];
268     unsigned long bitmask[NLONGS(EV_CNT)];
269     unsigned long key_bitmask[NLONGS(KEY_CNT)];
270     unsigned long rel_bitmask[NLONGS(REL_CNT)];
271     unsigned long abs_bitmask[NLONGS(ABS_CNT)];
272     unsigned long led_bitmask[NLONGS(LED_CNT)];
273     struct input_absinfo absinfo[ABS_CNT];
274
275     /* minor/major number */
276     dev_t min_maj;
277
278     /* Event queue used to defer keyboard/button events until EV_SYN time. */
279     int                     num_queue;
280     EventQueueRec           queue[EVDEVMULTITOUCH_MAXQUEUE];
281
282     Time timeout; /* Maximum difference between consecutive fseq values
283                            that will allow a packet to be dropped */
284     
285     OsTimerPtr subdevice_timer;
286     int current_id;
287     int num_mt;
288     int id;
289     int last_slot;
290 #if 1//ABS_MT_SLOT
291     BOOL mt_slot_supported;
292 #endif
293     BOOL sync_mt;
294     BOOL associated;
295     BOOL use_poll;//polling_with_vsync
296
297     int touch_state;
298     Time evtime;
299     InputInfoPtr core_device;
300     
301 } EvdevMultitouchRec, *EvdevMultitouchPtr;
302
303 /* Event posting functions */
304 void EvdevMultitouchQueueKbdEvent(InputInfoPtr pInfo, struct input_event *ev, int value);
305 void EvdevMultitouchQueueButtonEvent(InputInfoPtr pInfo, int button, int value);
306 void EvdevMultitouchPostButtonEvent(InputInfoPtr pInfo, int button, int value);
307 void EvdevMultitouchQueueButtonClicks(InputInfoPtr pInfo, int button, int count);
308 void EvdevMultitouchPostRelativeMotionEvents(InputInfoPtr pInfo, int *num_v, int *first_v,
309                                    int v[MAX_VALUATORS]);
310 void EvdevMultitouchPostAbsoluteMotionEvents(InputInfoPtr pInfo, int *num_v, int *first_v,
311                                    int v[MAX_VALUATORS]);
312 unsigned int EvdevMultitouchUtilButtonEventToButtonNumber(EvdevMultitouchPtr pEvdevMultitouch, int code);
313
314 /* Middle Button emulation */
315 int  EvdevMultitouchMBEmuTimer(InputInfoPtr);
316 BOOL EvdevMultitouchMBEmuFilterEvent(InputInfoPtr, int, BOOL);
317 void EvdevMultitouchMBEmuWakeupHandler(pointer, int, pointer);
318 void EvdevMultitouchMBEmuBlockHandler(pointer, struct timeval**, pointer);
319 void EvdevMultitouchMBEmuPreInit(InputInfoPtr);
320 void EvdevMultitouchMBEmuOn(InputInfoPtr);
321 void EvdevMultitouchMBEmuFinalize(InputInfoPtr);
322 void EvdevMultitouchMBEmuEnable(InputInfoPtr, BOOL);
323
324 /* Mouse Wheel emulation */
325 void EvdevMultitouchWheelEmuPreInit(InputInfoPtr pInfo);
326 BOOL EvdevMultitouchWheelEmuFilterButton(InputInfoPtr pInfo, unsigned int button, int value);
327 BOOL EvdevMultitouchWheelEmuFilterMotion(InputInfoPtr pInfo, struct input_event *pEv);
328
329 /* Draglock code */
330 void EvdevMultitouchDragLockPreInit(InputInfoPtr pInfo);
331 BOOL EvdevMultitouchDragLockFilterEvent(InputInfoPtr pInfo, unsigned int button, int value);
332
333 #ifdef HAVE_PROPERTIES
334 void EvdevMultitouchMBEmuInitProperty(DeviceIntPtr);
335 void EvdevMultitouchWheelEmuInitProperty(DeviceIntPtr);
336 void EvdevMultitouchDragLockInitProperty(DeviceIntPtr);
337 #endif
338 #endif