e9e17ff0895176dd04140b217fe9bf7bd76a9323
[adaptation/xorg/driver/xserver-xorg-input-gesture.git] / src / gesture.h
1 /**************************************************************************
2
3 xserver-xorg-input-gesture
4
5 Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
6
7 Contact: Sung-Jin Park <sj76.park@samsung.com>
8          Sangjin LEE <lsj119@samsung.com>
9
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sub license, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
17
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
20 of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 **************************************************************************/
31
32 #ifndef _GESTURE_H_
33 #define _GESTURE_H_
34
35 #include <xorg/events.h>
36 #include <xorg/gestureext.h>
37 #include <X11/extensions/gestureconst.h>
38 #include <pixman.h>
39
40 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 3
41 #define HAVE_PROPERTIES 1
42 #endif
43
44 #define SYSCALL(call) while (((call) == -1) && (errno == EINTR))
45 #define RootWindow(dev) dev->spriteInfo->sprite->spriteTrace[0]
46 #define CLIENT_BITS(id) ((id) & RESOURCE_CLIENT_MASK)
47 #define CLIENT_ID(id) ((int)(CLIENT_BITS(id) >> CLIENTOFFSET))
48
49 #define MAX_MT_DEVICES          3
50 #define GESTURE_EQ_SIZE 256
51
52 #define GESTURE_RECOGNIZER_ONOFF        "GESTURE_RECOGNIZER_ONOFF"
53
54 #define FINGER_WIDTH            10
55 #define FINGER_HEIGHT           10
56 #define FINGER_WIDTH_2T 20
57 #define FINGER_HEIGHT_2T        20
58 #define AREA_CENTER_X(extents)  ((extents)->x1 + (((extents)->x2-(extents)->x1)/2))
59 #define AREA_CENTER_Y(extents)  ((extents)->y1 + (((extents)->y2-(extents)->y1)/2))
60 #define AREA_SIZE(extents)              (ABS((extents)->x2-(extents)->x1)*ABS((extents)->y2-(extents)->y1))
61 #define INBOX(r,x,y)                            ( ((r)->x2 >  x) && ((r)->x1 <= x) && ((r)->y2 >  y) && ((r)->y1 <= y) )
62
63 //tap
64 #define TAP_THRESHOLD                   100//in pixel
65 #define SINGLE_TAP_TIMEOUT              100//in msec
66 #define DOUBLE_TAP_TIMEOUT      250//in msec
67
68 //pinch rotation
69 #define ZOOM_THRESHOLD                  0.05f
70 #define ANGLE_THRESHOLD         0.1f
71
72 typedef int XFixed;
73 typedef double XDouble;
74 #define XDoubleToFixed(f)    ((XFixed) ((f) * 65536))
75 #define XFixedToDouble(f)    (((XDouble) (f)) / 65536)
76
77 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
78 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
79 #define ABS(x) (((x) < 0) ? -(x) : (x))
80
81 enum
82 {
83         FLICK_NORTHWARD = 0,
84         FLICK_NORTHEASTWARD,
85         FLICK_EASTWARD,
86         FLICK_SOUTHEASTWARD,
87         FLICK_SOUTHWARD,
88         FLICK_SOUTHWESTWARD,
89         FLICK_WESTWARD,
90         FLICK_NORTHWESTWARD
91 };
92
93 #define PAN_AREA_THRESHOLD                      10000//=100pixel * 100pixel
94 #define PAN_MOVE_THRESHOLD                      5//pixel
95 #define PAN_UPDATE_MOVE_THRESHOLD       3//pixel
96 #define PAN_TIME_THRESHOLD                      300//ms
97
98 #define HOLD_AREA_THRESHOLD                     2500//=50pixel * 50pixel
99 #define HOLD_MOVE_THRESHOLD                     10//pixel
100 #define HOLD_TIME_THRESHOLD                     500//ms
101
102 #define TAP_AREA_THRESHOLD                      10000//= 100pixel * 100pixel
103 #define TAP_MOVE_THRESHOLD                      70//pixel
104 #define SGL_TAP_TIME_THRESHOLD          200//ms
105 #define DBL_TAP_TIME_THRESHOLD          200//ms
106 #define MAX_TAP_REPEATS                         3
107
108 #define TAPNHOLD_AREA_THRESHOLD                 4900//= 70pixel * 70pixel
109 #define TAPNHOLD_MOVE_THRESHOLD                 50//pixel
110 #define TAPNHOLD_TAP_TIME_THRESHOLD             200//ms
111 #define TAPNHOLD_INTV_TIME_THRESHOLD            200//ms
112 #define TAPNHOLD_HOLD_TIME_THRESHOLD    500//ms
113
114 #define FLICK_AREA_THRESHOLD                    22500//=150pixel * 150pixel
115 #define FLICK_AREA_TIMEOUT                              700//ms
116 #define FLICK_MOVE_THRESHOLD                    100//pixel
117 #define FLICK_MOVE_TIMEOUT                              1000//ms
118
119 typedef enum _MTSyncType
120 {
121         MTOUCH_FRAME_SYNC_END,
122         MTOUCH_FRAME_SYNC_BEGIN
123 } MTSyncType;
124
125 typedef enum _EventHandleType
126 {
127         KEEP_EVENTS,
128         PROPAGATE_EVENTS,
129         IGNORE_EVENTS
130 } EventHandleType;
131
132 typedef enum _ErrorStatus
133 {
134         ERROR_NONE,
135         ERROR_ABNORMAL,
136         ERROR_INVALPTR,
137         ERROR_EQFULL,
138         ERROR_ALLOCFAIL
139 } ErrorStatus;
140
141 enum EventType
142 {
143     ET_KeyPress = 2,
144     ET_KeyRelease,
145     ET_ButtonPress,
146     ET_ButtonRelease,
147     ET_Motion,
148     /*
149     ...
150     */
151     ET_MTSync = 0x7E,
152     ET_Internal = 0xFF /* First byte */
153 };
154
155 struct _DeviceEvent
156 {
157     unsigned char header; /**< Always ET_Internal */
158     enum EventType type;  /**< One of EventType */
159     int length;           /**< Length in bytes */
160     Time time;            /**< Time in ms */
161     int deviceid;         /**< Device to post this event for */
162     int sourceid;         /**< The physical source device */
163     union {
164         uint32_t button;  /**< Button number */
165         uint32_t key;     /**< Key code */
166     } detail;
167     int16_t root_x;       /**< Pos relative to root window in integral data */
168     float root_x_frac;    /**< Pos relative to root window in frac part */
169     int16_t root_y;       /**< Pos relative to root window in integral part */
170     float root_y_frac;    /**< Pos relative to root window in frac part */
171     uint8_t    buttons[(MAX_BUTTONS + 7)/8]; /**< Button mask */
172     struct {
173         uint8_t  mask[(MAX_VALUATORS + 7)/8]; /**< Valuator mask */
174         uint8_t  mode[(MAX_VALUATORS + 7)/8]; /**< Valuator mode (Abs or Rel)*/
175         int32_t  data[MAX_VALUATORS];         /**< Valuator data */
176         int32_t  data_frac[MAX_VALUATORS];    /**< Fractional part for data */
177     } valuators;
178     struct {
179         uint32_t base;    /**< XKB base modifiers */
180         uint32_t latched; /**< XKB latched modifiers */
181         uint32_t locked;  /**< XKB locked modifiers */
182         uint32_t effective;/**< XKB effective modifiers */
183     } mods;
184     struct {
185         uint8_t base;    /**< XKB base group */
186         uint8_t latched; /**< XKB latched group */
187         uint8_t locked;  /**< XKB locked group */
188         uint8_t effective;/**< XKB effective group */
189     } group;
190     Window      root; /**< Root window of the event */
191     int corestate;    /**< Core key/button state BEFORE the event */
192     int key_repeat;   /**< Internally-generated key repeat event */
193 };
194
195 typedef struct _AnyEvent AnyEvent;
196 struct _AnyEvent
197 {
198     unsigned char header; /**< Always ET_Internal */
199     enum EventType type;  /**< One of EventType */
200     int length;           /**< Length in bytes */
201     Time time;            /**< Time in ms */
202     int deviceid;
203     MTSyncType sync;
204     int x;
205     int y;
206 };
207
208 union _InternalEvent {
209         struct {
210             unsigned char header; /**< Always ET_Internal */
211             enum EventType type;  /**< One of ET_* */
212             int length;           /**< Length in bytes */
213             Time time;            /**< Time in ms. */
214         } any;
215         AnyEvent any_event;
216         DeviceEvent device_event;
217 };
218
219 #define wUseDefault(w,field,def)        ((w)->optional ? (w)->optional->field : def)
220 #define wBoundingShape(w)       wUseDefault(w, boundingShape, NULL)
221 #define wInputShape(w)          wUseDefault(w, inputShape, NULL)
222 #define wBorderWidth(w)         ((int) (w)->borderWidth)
223
224 /* used as NULL-terminated list */
225 typedef struct _DevCursorNode {
226     CursorPtr                   cursor;
227     DeviceIntPtr                dev;
228     struct _DevCursorNode*      next;
229 } DevCursNodeRec, *DevCursNodePtr, *DevCursorList;
230
231 typedef struct _WindowOpt {
232     VisualID            visual;            /* default: same as parent */
233     CursorPtr           cursor;            /* default: window.cursorNone */
234     Colormap            colormap;          /* default: same as parent */
235     Mask                dontPropagateMask; /* default: window.dontPropagate */
236     Mask                otherEventMasks;   /* default: 0 */
237     struct _OtherClients *otherClients;    /* default: NULL */
238     struct _GrabRec     *passiveGrabs;     /* default: NULL */
239     PropertyPtr         userProps;         /* default: NULL */
240     unsigned long       backingBitPlanes;  /* default: ~0L */
241     unsigned long       backingPixel;      /* default: 0 */
242     RegionPtr           boundingShape;     /* default: NULL */
243     RegionPtr           clipShape;         /* default: NULL */
244     RegionPtr           inputShape;        /* default: NULL */
245     struct _OtherInputMasks *inputMasks;   /* default: NULL */
246     DevCursorList       deviceCursors;     /* default: NULL */
247 } WindowOptRec, *WindowOptPtr;
248
249 typedef struct _Window {
250     DrawableRec         drawable;
251     PrivateRec          *devPrivates;
252     WindowPtr           parent;         /* ancestor chain */
253     WindowPtr           nextSib;        /* next lower sibling */
254     WindowPtr           prevSib;        /* next higher sibling */
255     WindowPtr           firstChild;     /* top-most child */
256     WindowPtr           lastChild;      /* bottom-most child */
257     RegionRec           clipList;       /* clipping rectangle for output */
258     RegionRec           borderClip;     /* NotClippedByChildren + border */
259     union _Validate     *valdata;
260     RegionRec           winSize;
261     RegionRec           borderSize;
262     DDXPointRec         origin;         /* position relative to parent */
263     unsigned short      borderWidth;
264     unsigned short      deliverableEvents; /* all masks from all clients */
265     Mask                eventMask;      /* mask from the creating client */
266     PixUnion            background;
267     PixUnion            border;
268     pointer             backStorage;    /* null when BS disabled */
269     WindowOptPtr        optional;
270     unsigned            backgroundState:2; /* None, Relative, Pixel, Pixmap */
271     unsigned            borderIsPixel:1;
272     unsigned            cursorIsNone:1; /* else real cursor (might inherit) */
273     unsigned            backingStore:2;
274     unsigned            saveUnder:1;
275     unsigned            DIXsaveUnder:1;
276     unsigned            bitGravity:4;
277     unsigned            winGravity:4;
278     unsigned            overrideRedirect:1;
279     unsigned            visibility:2;
280     unsigned            mapped:1;
281     unsigned            realized:1;     /* ancestors are all mapped */
282     unsigned            viewable:1;     /* realized && InputOutput */
283     unsigned            dontPropagate:3;/* index into DontPropagateMasks */
284     unsigned            forcedBS:1;     /* system-supplied backingStore */
285     unsigned            redirectDraw:2; /* COMPOSITE rendering redirect */
286     unsigned            forcedBG:1;     /* must have an opaque background */
287 #ifdef ROOTLESS
288     unsigned            rootlessUnhittable:1;   /* doesn't hit-test */
289 #endif
290 } WindowRec;
291
292 typedef struct _IEvent {
293         InternalEvent *event;
294         int screen_num;
295         DeviceIntPtr device;
296 } IEventRec, *IEventPtr;
297
298 enum
299 {
300         BTN_RELEASED,
301         BTN_PRESSED,
302         BTN_MOVING
303 };
304
305 #define PressFlagFlick                  0x01//(1 << 0)
306 #define PressFlagPan                            0x02//(1 << 1)
307 #define PressFlagPinchRotation  0x04//(1 << 2)
308 #define PressFlagTap                            0x08//(1 << 3)
309 #define PressFlagTapNHold               0x10//(1 << 4)
310 #define PressFlagHold                   0x20//(1 << 5)
311
312 #define FlickFilterMask                 0x01//(1 << 0)
313 #define PanFilterMask                   0x02//(1 << 1)
314 #define PinchRotationFilterMask 0x04//(1 << 2)
315 #define TapFilterMask                   0x08//(1 << 3)
316 #define TapNHoldFilterMask              0x10//(1 << 4)
317 #define HoldFilterMask                  0x20//(1 << 5)
318
319 #define GESTURE_FILTER_MASK_ALL 0x3f//(FlickFilterMask | PanFilterMask | PinchRotationFilterMask | TapFilterMask |TapNHoldFilterMask | HoldFilterMask)
320
321 typedef struct _tagTouchStatus
322 {
323         int status;//One of BTN_RELEASED, BTN_PRESSED and BTN_MOVING
324         uint32_t flags;
325
326         int px;         //press x
327         int py;         //press y
328         int mx;         //motion x
329         int my;         //motion y
330         int rx;         //release x
331         int ry;         //release y
332         Time ptime;     //press time
333         Time mtime;     //motion time
334         Time rtime;     //current/previous release time
335 } TouchStatus;
336
337 typedef struct _GestureDeviceRec
338 {
339         char *device;
340         int version;        /* Driver version */
341         OsTimerPtr device_setting_timer;
342
343         int is_active;
344
345         WindowPtr pRootWin;
346         Window gestureWin;
347         int num_mt_devices;
348
349         Mask grabMask;
350         Mask eventMask;
351         GestureGrabEventPtr GrabEvents;
352
353         EventHandleType ehtype;
354         IEventPtr       EQ;
355         int headEQ;
356         int tailEQ;
357
358         pixman_region16_t area;
359         pixman_region16_t finger_rects[MAX_MT_DEVICES];
360
361         WindowPtr pTempWin;
362         int inc_num_pressed;
363
364         int first_fingerid;
365         int num_pressed;
366         TouchStatus fingers[MAX_MT_DEVICES];
367
368         int event_sum[MAX_MT_DEVICES];
369         uint32_t recognized_gesture;
370         uint32_t filter_mask;
371
372         DeviceIntPtr this_device;
373         DeviceIntPtr mt_devices[MAX_MT_DEVICES];
374         DeviceIntPtr master_pointer;
375         DeviceIntPtr xtest_pointer;
376 } GestureDeviceRec, *GestureDevicePtr ;
377
378 #endif//_GESTURE_H_