Initialize Tizen 2.3
[adaptation/xorg/driver/xserver-xorg-input-gesture.git] / wearable / 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          10
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 #define AREA_HEIGHT(extents)    (((extents)->y2)-((extents)->y1))
63 #define AREA_WIDTH(extents)     (((extents)->x2)-((extents)->x1))
64 #define AREA_DIAG_LEN(extents)  sqrt((AREA_WIDTH(extents)*AREA_WIDTH(extents))+(AREA_HEIGHT(extents)*AREA_HEIGHT(extents)))
65
66 //tap
67 #define TAP_THRESHOLD                   100//in pixel
68 #define SINGLE_TAP_TIMEOUT              100//in msec
69 #define DOUBLE_TAP_TIMEOUT      250//in msec
70
71 typedef int XFixed;
72 typedef double XDouble;
73 #define XDoubleToFixed(f)    ((XFixed) ((f) * 65536))
74 #define XFixedToDouble(f)    (((XDouble) (f)) / 65536)
75
76 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
77 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
78 #define ABS(x) (((x) < 0) ? -(x) : (x))
79
80 enum
81 {
82         FLICK_NORTHWARD = 0,
83         FLICK_NORTHEASTWARD,
84         FLICK_EASTWARD,
85         FLICK_SOUTHEASTWARD,
86         FLICK_SOUTHWARD,
87         FLICK_SOUTHWESTWARD,
88         FLICK_WESTWARD,
89         FLICK_NORTHWESTWARD
90 };
91
92 enum
93 {
94         FLICK_POINT_NONE = 0,
95         FLICK_POINT_UP,
96         FLICK_POINT_RIGHT,
97         FLICK_POINT_DOWN,
98         FLICK_POINT_LEFT,
99         FLICK_POINT_MAX
100 };
101
102 #define TAP_AREA_THRESHOLD                      10000//= 100pixel * 100pixel
103 #define TAP_MOVE_THRESHOLD                      70//pixel
104 #define SGL_TAP_TIME_THRESHOLD          300//ms
105 #define DBL_TAP_TIME_THRESHOLD          200//ms
106 #define MAX_TAP_REPEATS             2
107
108 #define FLICK_AREA_THRESHOLD                    22500//=150pixel * 150pixel
109 #define FLICK_AREA_TIMEOUT                              700//ms
110 #define FLICK_MOVE_THRESHOLD                    100//pixel
111 #define FLICK_MOVE_TIMEOUT                              1000//ms
112 #define FLICK_FALSE_Y_DIFF_COUNT                7
113 #define FLICK_FALSE_X_DIFF_COUNT                5
114
115
116 typedef enum _MTSyncType
117 {
118         MTOUCH_FRAME_SYNC_END,
119         MTOUCH_FRAME_SYNC_BEGIN
120 } MTSyncType;
121
122 typedef enum _EventHandleType
123 {
124         PROPAGATE_EVENTS,
125         KEEP_EVENTS,
126         IGNORE_EVENTS
127 } EventHandleType;
128
129 typedef enum _ErrorStatus
130 {
131         ERROR_NONE,
132         ERROR_ABNORMAL,
133         ERROR_INVALPTR,
134         ERROR_EQFULL,
135         ERROR_ALLOCFAIL
136 } ErrorStatus;
137
138 enum EventType
139 {
140     ET_KeyPress = 2,
141     ET_KeyRelease,
142     ET_ButtonPress,
143     ET_ButtonRelease,
144     ET_Motion,
145     /*
146     ...
147     */
148     ET_MTSync = 0x7E,
149     ET_Internal = 0xFF /* First byte */
150 };
151
152 struct _DeviceEvent {
153     unsigned char header; /**< Always ET_Internal */
154     enum EventType type;  /**< One of EventType */
155     int length;           /**< Length in bytes */
156     Time time;            /**< Time in ms */
157     int deviceid;         /**< Device to post this event for */
158     int sourceid;         /**< The physical source device */
159     union {
160         uint32_t button;  /**< Button number (also used in pointer emulating
161                                touch events) */
162         uint32_t key;     /**< Key code */
163     } detail;
164     uint32_t touchid;     /**< Touch ID (client_id) */
165     int16_t root_x;       /**< Pos relative to root window in integral data */
166     float root_x_frac;    /**< Pos relative to root window in frac part */
167     int16_t root_y;       /**< Pos relative to root window in integral part */
168     float root_y_frac;    /**< Pos relative to root window in frac part */
169     uint8_t buttons[(MAX_BUTTONS + 7) / 8];  /**< Button mask */
170     struct {
171         uint8_t mask[(MAX_VALUATORS + 7) / 8];/**< Valuator mask */
172         uint8_t mode[(MAX_VALUATORS + 7) / 8];/**< Valuator mode (Abs or Rel)*/
173         double data[MAX_VALUATORS];           /**< Valuator data */
174     } valuators;
175     struct {
176         uint32_t base;    /**< XKB base modifiers */
177         uint32_t latched; /**< XKB latched modifiers */
178         uint32_t locked;  /**< XKB locked modifiers */
179         uint32_t effective;/**< XKB effective modifiers */
180     } mods;
181     struct {
182         uint8_t base;    /**< XKB base group */
183         uint8_t latched; /**< XKB latched group */
184         uint8_t locked;  /**< XKB locked group */
185         uint8_t effective;/**< XKB effective group */
186     } group;
187     Window root;      /**< Root window of the event */
188     int corestate;    /**< Core key/button state BEFORE the event */
189     int key_repeat;   /**< Internally-generated key repeat event */
190     uint32_t flags;   /**< Flags to be copied into the generated event */
191 };
192
193 typedef struct _AnyEvent AnyEvent;
194 struct _AnyEvent
195 {
196     unsigned char header; /**< Always ET_Internal */
197     enum EventType type;  /**< One of EventType */
198     int length;           /**< Length in bytes */
199     Time time;            /**< Time in ms */
200     int deviceid;
201     MTSyncType sync;
202     int x;
203     int y;
204 };
205
206 union _InternalEvent {
207         struct {
208             unsigned char header; /**< Always ET_Internal */
209             enum EventType type;  /**< One of ET_* */
210             int length;           /**< Length in bytes */
211             Time time;            /**< Time in ms. */
212         } any;
213         AnyEvent any_event;
214         DeviceEvent device_event;
215 };
216
217 #define wUseDefault(w,field,def)        ((w)->optional ? (w)->optional->field : def)
218 #define wBoundingShape(w)       wUseDefault(w, boundingShape, NULL)
219 #define wInputShape(w)          wUseDefault(w, inputShape, NULL)
220 #define wBorderWidth(w)         ((int) (w)->borderWidth)
221
222 /* used as NULL-terminated list */
223 typedef struct _DevCursorNode {
224     CursorPtr                   cursor;
225     DeviceIntPtr                dev;
226     struct _DevCursorNode*      next;
227 } DevCursNodeRec, *DevCursNodePtr, *DevCursorList;
228
229 typedef struct _WindowOpt {
230     VisualID            visual;            /* default: same as parent */
231     CursorPtr           cursor;            /* default: window.cursorNone */
232     Colormap            colormap;          /* default: same as parent */
233     Mask                dontPropagateMask; /* default: window.dontPropagate */
234     Mask                otherEventMasks;   /* default: 0 */
235     struct _OtherClients *otherClients;    /* default: NULL */
236     struct _GrabRec     *passiveGrabs;     /* default: NULL */
237     PropertyPtr         userProps;         /* default: NULL */
238     unsigned long       backingBitPlanes;  /* default: ~0L */
239     unsigned long       backingPixel;      /* default: 0 */
240     RegionPtr           boundingShape;     /* default: NULL */
241     RegionPtr           clipShape;         /* default: NULL */
242     RegionPtr           inputShape;        /* default: NULL */
243     struct _OtherInputMasks *inputMasks;   /* default: NULL */
244     DevCursorList       deviceCursors;     /* default: NULL */
245 } WindowOptRec, *WindowOptPtr;
246
247 typedef struct _Window {
248     DrawableRec         drawable;
249     PrivateRec          *devPrivates;
250     WindowPtr           parent;         /* ancestor chain */
251     WindowPtr           nextSib;        /* next lower sibling */
252     WindowPtr           prevSib;        /* next higher sibling */
253     WindowPtr           firstChild;     /* top-most child */
254     WindowPtr           lastChild;      /* bottom-most child */
255     RegionRec           clipList;       /* clipping rectangle for output */
256     RegionRec           borderClip;     /* NotClippedByChildren + border */
257     union _Validate     *valdata;
258     RegionRec           winSize;
259     RegionRec           borderSize;
260     DDXPointRec         origin;         /* position relative to parent */
261     unsigned short      borderWidth;
262     unsigned short      deliverableEvents; /* all masks from all clients */
263     Mask                eventMask;      /* mask from the creating client */
264     PixUnion            background;
265     PixUnion            border;
266     pointer             backStorage;    /* null when BS disabled */
267     WindowOptPtr        optional;
268     unsigned            backgroundState:2; /* None, Relative, Pixel, Pixmap */
269     unsigned            borderIsPixel:1;
270     unsigned            cursorIsNone:1; /* else real cursor (might inherit) */
271     unsigned            backingStore:2;
272     unsigned            saveUnder:1;
273     unsigned            DIXsaveUnder:1;
274     unsigned            bitGravity:4;
275     unsigned            winGravity:4;
276     unsigned            overrideRedirect:1;
277     unsigned            visibility:2;
278     unsigned            mapped:1;
279     unsigned            realized:1;     /* ancestors are all mapped */
280     unsigned            viewable:1;     /* realized && InputOutput */
281     unsigned            dontPropagate:3;/* index into DontPropagateMasks */
282     unsigned            forcedBS:1;     /* system-supplied backingStore */
283     unsigned            redirectDraw:2; /* COMPOSITE rendering redirect */
284     unsigned            forcedBG:1;     /* must have an opaque background */
285 #ifdef ROOTLESS
286     unsigned            rootlessUnhittable:1;   /* doesn't hit-test */
287 #endif
288 } WindowRec;
289
290 typedef struct _IEvent {
291         InternalEvent *event;
292         int screen_num;
293         DeviceIntPtr device;
294 } IEventRec, *IEventPtr;
295
296 enum
297 {
298         BTN_RELEASED,
299         BTN_PRESSED,
300         BTN_MOVING
301 };
302
303 #define PressFlagFlick                  0x01//(1 << 0)
304 #define PressFlagPan                            0x02//(1 << 1)
305 #define PressFlagPinchRotation  0x04//(1 << 2)
306 #define PressFlagTap                            0x08//(1 << 3)
307 #define PressFlagTapNHold               0x10//(1 << 4)
308 #define PressFlagHold                   0x20//(1 << 5)
309
310 #define FlickFilterMask                 0x01//(1 << 0)
311 #define PanFilterMask                   0x02//(1 << 1)
312 #define PinchRotationFilterMask 0x04//(1 << 2)
313 #define TapFilterMask                   0x08//(1 << 3)
314 #define TapNHoldFilterMask              0x10//(1 << 4)
315 #define HoldFilterMask                  0x20//(1 << 5)
316
317 #define GESTURE_FILTER_MASK_ALL 0x3f//(FlickFilterMask | PanFilterMask | PinchRotationFilterMask | TapFilterMask |TapNHoldFilterMask | HoldFilterMask)
318
319 #define WFlickFilterMask                                0x01//(1 << 0)
320 #define WTapFilterMask                          0x02//(1 << 1)
321 #define WHoldFilterMask                         0x04//(1 << 2)
322
323 #define GESTURE_WATCH_FILTER_MASK_ALL   0x07//(WFlickFilterMask | WTapFilterMask | WHoldFilterMask)
324
325 typedef struct _tagTouchStatus
326 {
327         int status;//One of BTN_RELEASED, BTN_PRESSED and BTN_MOVING
328         uint32_t flags;
329
330         int px;         //press x
331         int py;         //press y
332         int mx;         //motion x
333         int my;         //motion y
334         int rx;         //release x
335         int ry;         //release y
336         Time ptime;     //press time
337         Time mtime;     //motion time
338         Time rtime;     //current/previous release time
339 } TouchStatus;
340
341 typedef struct _GestureDeviceRec
342 {
343         char *device;
344         int version;        /* Driver version */
345         OsTimerPtr device_setting_timer;
346
347         int is_active;
348         int power_pressed;
349         int flick_pressed_point;
350         int flick_direction;
351
352         int hwkeycode_flick_down;
353         int hwkeycode_flick_up;
354         int shutdown_keycode;
355         int flick_press_area;
356         int flick_press_area_left;
357         int flick_press_area_left_right;
358         int flick_minimum_height;
359         int activate_flick_down;//up to down
360         int activate_flick_up;//down to up
361         int activate_flick_right;//left to right
362         int screen_width;
363         int screen_height;
364         int singletap_threshold;
365         int doubletap_threshold;
366         double hold_area_threshold;
367         int hold_move_threshold;
368         int hold_time_threshold;
369
370         int hwkey_id;
371         char *hwkey_name;
372         DeviceIntPtr hwkey_dev;
373         MTSyncType mtsync_status;
374
375         DeviceIntPtr power_device;
376
377         WindowPtr pRootWin;
378         Window gestureWin;
379         int num_mt_devices;
380
381         Mask grabMask;
382         Mask eventMask;
383         GestureGrabEventPtr GrabEvents;
384         Mask lastSelectedMask;
385         Window lastSelectedWin;
386
387         EventHandleType ehtype;
388         IEventPtr       EQ;
389         int headEQ;
390         int tailEQ;
391
392         pixman_region16_t area;
393         pixman_region16_t finger_rects[MAX_MT_DEVICES];
394
395         WindowPtr pTempWin;
396         int inc_num_pressed;
397
398         int first_fingerid;
399         int num_pressed;
400         TouchStatus fingers[MAX_MT_DEVICES];
401
402         int event_sum[MAX_MT_DEVICES];
403         uint32_t recognized_gesture;
404         uint32_t filter_mask;
405
406         DeviceIntPtr this_device;
407         DeviceIntPtr mt_devices[MAX_MT_DEVICES];
408         DeviceIntPtr master_pointer;
409         DeviceIntPtr xtest_pointer;
410 } GestureDeviceRec, *GestureDevicePtr ;
411
412 #endif//_GESTURE_H_