Tizen 2.0 Release
[profile/ivi/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 10
136 #define DEFAULT_TIMEOUT 100
137
138 #define EVDEVMULTITOUCH_PROP_TRACKING_ID "EvdevMultitouch Tracking ID"
139 #define EVDEVMULTITOUCH_PROP_MULTITOUCH_SUBDEVICES "EvdevMultitouch MultiTouch"
140 #define EVDEVMULTITOUCH_PROP_TRANSFORM  "EvdevMultitouch Transform Matrix"
141 #define EVDEVMULTITOUCH_PROP_GRABINFO   "EvdevMultitouch Grab Info"
142
143 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 5
144 typedef struct {
145     char *rules;
146     char *model;
147     char *layout;
148     char *variant;
149     char *options;
150 } XkbRMLVOSet;
151 #endif
152
153
154 #define LONG_BITS (sizeof(long) * 8)
155
156 /* Number of longs needed to hold the given number of bits */
157 #define NLONGS(x) (((x) + LONG_BITS - 1) / LONG_BITS)
158
159 /* axis specific data for wheel emulation */
160 typedef struct {
161     int up_button;
162     int down_button;
163     int traveled_distance;
164 } WheelAxis, *WheelAxisPtr;
165
166 /* Event queue used to defer keyboard/button events until EV_SYN time. */
167 typedef struct {
168     enum {
169         EV_QUEUE_KEY,   /* xf86PostKeyboardEvent() */
170         EV_QUEUE_BTN,   /* xf86PostButtonEvent() */
171     } type;
172     int key;            /* May be either a key code or button number. */
173     int val;            /* State of the key/button; pressed or released. */
174 } EventQueueRec, *EventQueuePtr;
175
176 typedef struct _EvdevMultitouchDataMTRec{
177     int id;
178     int slot;
179     int abs;
180    BOOL containsValues;
181     Time expires;
182     int vals[MAX_VALUATORS];
183     InputInfoPtr pInfo;
184 } EvdevMultitouchDataMTRec, *EvdevMultitouchDataMTPtr;
185
186
187 /**
188  * EvdevMultitouch device information, including list of current object
189  */
190 typedef struct {
191     const char *device;
192     int grabDevice;         /* grab the event device? */
193
194     int num_vals;           /* number of valuators */
195     int axis_map[max(ABS_CNT, REL_CNT)]; /* Map evdevmultitouch <axis> to index */
196     int vals[MAX_VALUATORS];
197     int old_vals[MAX_VALUATORS]; /* Translate absolute inputs to relative */
198     EvdevMultitouchDataMTRec vals_mt[MAX_VALUATORS_MT];
199
200     int flags;
201     int tool;
202     int num_buttons;            /* number of buttons */
203     BOOL swap_axes;
204     BOOL invert_x;
205     BOOL invert_y;
206     int num_multitouch;
207     OsTimerPtr multitouch_setting_timer;
208
209     int delta[REL_CNT];
210     unsigned int abs, rel, mt;
211
212     /* XKB stuff has to be per-device rather than per-driver */
213 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 5
214     XkbComponentNamesRec    xkbnames;
215 #endif
216     XkbRMLVOSet rmlvo;
217
218     /* Middle mouse button emulation */
219     struct {
220         BOOL                enabled;
221         BOOL                pending;     /* timer waiting? */
222         int                 buttonstate; /* phys. button state */
223         int                 state;       /* state machine (see bt3emu.c) */
224         Time                expires;     /* time of expiry */
225         Time                timeout;
226     } emulateMB;
227     struct {
228         int                 meta;           /* meta key to lock any button */
229         BOOL                meta_state;     /* meta_button state */
230         unsigned int        lock_pair[EVDEVMULTITOUCH_MAXBUTTONS];  /* specify a meta/lock pair */
231         BOOL                lock_state[EVDEVMULTITOUCH_MAXBUTTONS]; /* state of any locked buttons */
232     } dragLock;
233     struct {
234         BOOL                enabled;
235         int                 button;
236         int                 button_state;
237         int                 inertia;
238         WheelAxis           X;
239         WheelAxis           Y;
240         Time                expires;     /* time of expiry */
241         Time                timeout;
242     } emulateWheel;
243     /* run-time calibration */
244     struct {
245         int                 min_x;
246         int                 max_x;
247         int                 min_y;
248         int                 max_y;
249     } calibration;
250
251     struct {
252         int                 min_x;
253         int                 max_x;
254         int                 min_y;
255         int                 max_y;
256     } resolution;
257
258     unsigned char btnmap[32];           /* config-file specified button mapping */
259
260     int reopen_attempts; /* max attempts to re-open after read failure */
261     int reopen_left;     /* number of attempts left to re-open the device */
262     OsTimerPtr reopen_timer;
263
264     /* Cached info from device. */
265     char name[1024];
266     unsigned long bitmask[NLONGS(EV_CNT)];
267     unsigned long key_bitmask[NLONGS(KEY_CNT)];
268     unsigned long rel_bitmask[NLONGS(REL_CNT)];
269     unsigned long abs_bitmask[NLONGS(ABS_CNT)];
270     unsigned long led_bitmask[NLONGS(LED_CNT)];
271     struct input_absinfo absinfo[ABS_CNT];
272
273     /* minor/major number */
274     dev_t min_maj;
275
276     /* Event queue used to defer keyboard/button events until EV_SYN time. */
277     int                     num_queue;
278     EventQueueRec           queue[EVDEVMULTITOUCH_MAXQUEUE];
279
280     Time timeout; /* Maximum difference between consecutive fseq values
281                            that will allow a packet to be dropped */
282     
283     OsTimerPtr subdevice_timer;
284     int current_id;
285     int num_mt;
286     int id;
287     int last_slot;
288     BOOL mt_slot_supported;
289     BOOL sync_mt;
290     BOOL associated;
291
292     float transform[9];
293     BOOL use_transform;
294     struct pixman_transform inv_transform;
295
296     int touch_state;
297     Time evtime;
298     InputInfoPtr core_device;
299     
300 } EvdevMultitouchRec, *EvdevMultitouchPtr;
301
302 /* Event posting functions */
303 void EvdevMultitouchQueueKbdEvent(InputInfoPtr pInfo, struct input_event *ev, int value);
304 void EvdevMultitouchQueueButtonEvent(InputInfoPtr pInfo, int button, int value);
305 void EvdevMultitouchPostButtonEvent(InputInfoPtr pInfo, int button, int value);
306 void EvdevMultitouchQueueButtonClicks(InputInfoPtr pInfo, int button, int count);
307 void EvdevMultitouchPostRelativeMotionEvents(InputInfoPtr pInfo, int *num_v, int *first_v,
308                                    int v[MAX_VALUATORS]);
309 void EvdevMultitouchPostAbsoluteMotionEvents(InputInfoPtr pInfo, int *num_v, int *first_v,
310                                    int v[MAX_VALUATORS]);
311 unsigned int EvdevMultitouchUtilButtonEventToButtonNumber(EvdevMultitouchPtr pEvdevMultitouch, int code);
312
313 /* Middle Button emulation */
314 int  EvdevMultitouchMBEmuTimer(InputInfoPtr);
315 BOOL EvdevMultitouchMBEmuFilterEvent(InputInfoPtr, int, BOOL);
316 void EvdevMultitouchMBEmuWakeupHandler(pointer, int, pointer);
317 void EvdevMultitouchMBEmuBlockHandler(pointer, struct timeval**, pointer);
318 void EvdevMultitouchMBEmuPreInit(InputInfoPtr);
319 void EvdevMultitouchMBEmuOn(InputInfoPtr);
320 void EvdevMultitouchMBEmuFinalize(InputInfoPtr);
321 void EvdevMultitouchMBEmuEnable(InputInfoPtr, BOOL);
322
323 /* Mouse Wheel emulation */
324 void EvdevMultitouchWheelEmuPreInit(InputInfoPtr pInfo);
325 BOOL EvdevMultitouchWheelEmuFilterButton(InputInfoPtr pInfo, unsigned int button, int value);
326 BOOL EvdevMultitouchWheelEmuFilterMotion(InputInfoPtr pInfo, struct input_event *pEv);
327
328 /* Draglock code */
329 void EvdevMultitouchDragLockPreInit(InputInfoPtr pInfo);
330 BOOL EvdevMultitouchDragLockFilterEvent(InputInfoPtr pInfo, unsigned int button, int value);
331
332 #ifdef HAVE_PROPERTIES
333 void EvdevMultitouchMBEmuInitProperty(DeviceIntPtr);
334 void EvdevMultitouchWheelEmuInitProperty(DeviceIntPtr);
335 void EvdevMultitouchDragLockInitProperty(DeviceIntPtr);
336 #endif
337 #endif