Merge "Elm Glayer: Fixed all tap, long tap, gestures bugs"
[framework/uifw/elementary.git] / src / lib / elm_gesture_layer.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3 /** @defgroup Elm_Gesture_Layer Gesture Layer */
4
5 /* Some defaults */
6 #define ELM_MOUSE_DEVICE 0
7 /* ELM_GESTURE_NEGATIVE_ANGLE - magic number says we didn't compute this yet */
8 #define ELM_GESTURE_NEGATIVE_ANGLE (-1.0) /* Magic number */
9 #define ELM_GESTURE_MOMENTUM_TIMEOUT 50
10
11 /* Some Trigo values */
12 #define RAD_90DEG  M_PI_2
13 #define RAD_180DEG M_PI
14 #define RAD_270DEG (M_PI_2 * 3)
15 #define RAD_360DEG (M_PI * 2)
16 /* #define DEBUG_GESTURE_LAYER 1 */
17
18 static void *
19 _glayer_bufdup(void *buf, size_t size)
20 {
21    void *p;
22    p = malloc(size);
23    memcpy(p, buf, size);
24    return p;
25 }
26 #define COPY_EVENT_INFO(EV) _glayer_bufdup(EV, sizeof(*EV))
27
28
29 #define SET_TEST_BIT(P) do { \
30    P->test = P->fn[ELM_GESTURE_STATE_START].cb || P->fn[ELM_GESTURE_STATE_MOVE].cb || P->fn[ELM_GESTURE_STATE_END].cb || P->fn[ELM_GESTURE_STATE_ABORT].cb; \
31 } while (0)
32
33 #define IS_TESTED(T) ((wd->gesture[T]) ? wd->gesture[T]->test : EINA_FALSE)
34
35 /**
36  * @internal
37  *
38  * @struct _Func_Data
39  * Struct holds callback information.
40  *
41  * @ingroup Elm_Gesture_Layer
42  */
43 struct _Func_Data
44 {
45    void *user_data; /**< Holds user data to CB (like sd) */
46    Elm_Gesture_Event_Cb cb;
47 };
48
49 /**
50  * @internal
51  *
52  * @typedef Func_Data
53  * type for callback information
54  *
55  * @ingroup Elm_Gesture_Layer
56  */
57 typedef struct _Func_Data Func_Data;
58
59 /**
60  * @internal
61  *
62  * @struct _Gesture_Info
63  * Struct holds gesture info
64  *
65  * @ingroup Elm_Gesture_Layer
66  */
67 struct _Gesture_Info
68 {
69   Evas_Object *obj;
70   void *data; /**< Holds gesture intemidiate processing data */
71   Func_Data fn[ELM_GESTURE_STATE_ABORT + 1]; /**< Callback info for states */
72   Elm_Gesture_Types g_type;  /**< gesture type */
73   Elm_Gesture_State state;  /**< gesture state */
74   void *info;                        /**< Data for the state callback */
75   Eina_Bool test; /**< if true this gesture should be tested on input */
76 };
77
78 /**
79  * @internal
80  *
81  * @typedef Gesture_Info
82  * Type for _Gesture_Info
83  *
84  * @ingroup Elm_Gesture_Layer
85  */
86 typedef struct _Gesture_Info Gesture_Info;
87
88 /**
89  * @internal
90  *
91  * @struct _Event_History
92  * Struct holds event history.
93  * These events are repeated if no gesture found.
94  *
95  * @ingroup Elm_Gesture_Layer
96  */
97 struct _Event_History
98 {
99    EINA_INLIST;
100    void *event;
101    Evas_Callback_Type event_type;
102 };
103
104 /**
105  * @internal
106  *
107  * @typedef Event_History
108  * Type for _Event_History
109  *
110  * @ingroup Elm_Gesture_Layer
111  */
112 typedef struct _Event_History Event_History;
113
114 /**
115  * @internal
116  *
117  * @struct _Pointer_Event
118  * Struct holds pointer-event info
119  * This is a generic pointer event structure
120  *
121  * @ingroup Elm_Gesture_Layer
122  */
123 struct _Pointer_Event
124 {
125    Evas_Coord x, y;
126    unsigned int timestamp;
127    int device;
128    Evas_Callback_Type event_type;
129 };
130
131 /**
132  * @internal
133  *
134  * @typedef Pointer_Event
135  * Type for generic pointer event structure
136  *
137  * @ingroup Elm_Gesture_Layer
138  */
139 typedef struct _Pointer_Event Pointer_Event;
140
141 /* All *Type structs hold result for the user in 'info' field
142  * The rest is gesture processing intermediate data.
143  * NOTE: info field must be FIRST in the struct.
144  * This is used when reporting ABORT in event_history_clear() */
145 struct _Taps_Type
146 {
147    Elm_Gesture_Taps_Info info;
148    unsigned int sum_x;
149    unsigned int sum_y;
150    unsigned int n_taps_needed;
151    unsigned int n_taps;
152    Eina_List *l;
153 };
154 typedef struct _Taps_Type Taps_Type;
155
156 struct _Long_Tap_Type
157 {
158    Elm_Gesture_Taps_Info info;
159    unsigned int center_x;
160    unsigned int center_y;
161    unsigned int n_taps;
162    Ecore_Timer *timeout; /* When this expires, long tap STARTed */
163    Eina_List *touched;
164 };
165 typedef struct _Long_Tap_Type Long_Tap_Type;
166
167 struct _Momentum_Type
168 {  /* Fields used by _line_test() */
169    Elm_Gesture_Momentum_Info info;
170    Evas_Coord_Point line_st;
171    Evas_Coord_Point line_end;
172    unsigned int t_st_x;  /* Time start on X */
173    unsigned int t_st_y;  /* Time start on Y */
174    unsigned int t_end;   /* Time end        */
175    int xdir, ydir;
176 };
177 typedef struct _Momentum_Type Momentum_Type;
178
179 struct _Line_Data
180 {
181    Evas_Coord_Point line_st;
182    Evas_Coord_Point line_end;
183    Evas_Coord line_length;
184    unsigned int t_st;  /* Time start */
185    unsigned int t_end; /* Time end   */
186    int device;
187    double line_angle;  /* Current angle of line */
188 };
189 typedef struct _Line_Data Line_Data;
190
191 struct _Line_Type
192 {  /* Fields used by _line_test() */
193    Elm_Gesture_Line_Info info;
194    Eina_List *list; /* List of Line_Data */
195 };
196 typedef struct _Line_Type Line_Type;
197
198 struct _Zoom_Type
199 {  /* Fields used by _zoom_test() */
200    Elm_Gesture_Zoom_Info info;
201    Pointer_Event zoom_st;
202    Pointer_Event zoom_mv;
203    Pointer_Event zoom_st1;
204    Pointer_Event zoom_mv1;
205    Evas_Event_Mouse_Wheel *zoom_wheel;
206    Evas_Coord zoom_base;  /* Holds gap between fingers on zoom-start  */
207    Evas_Coord zoom_distance_tolerance;
208    Elm_Gesture_Momentum_Info momentum1;      /* For continues gesture */
209    Elm_Gesture_Momentum_Info momentum2;      /* For continues gesture */
210    double next_step;
211 };
212 typedef struct _Zoom_Type Zoom_Type;
213
214 struct _Rotate_Type
215 {  /* Fields used by _rotation_test() */
216    Elm_Gesture_Rotate_Info info;
217    Pointer_Event rotate_st;
218    Pointer_Event rotate_mv;
219    Pointer_Event rotate_st1;
220    Pointer_Event rotate_mv1;
221    double rotate_angular_tolerance;
222    double next_step;
223    Elm_Gesture_Momentum_Info momentum1;      /* For continues gesture */
224    Elm_Gesture_Momentum_Info momentum2;      /* For continues gesture */
225 };
226 typedef struct _Rotate_Type Rotate_Type;
227
228 struct _Widget_Data
229 {
230    Evas_Object *target;  /* Target Widget */
231    Event_History *event_history_list;
232
233    int line_min_length;
234    Evas_Coord zoom_distance_tolerance;
235    Evas_Coord line_distance_tolerance;
236    double line_angular_tolerance;
237    double zoom_wheel_factor; /* mouse wheel zoom steps */
238    double zoom_finger_factor; /* used for zoom factor */
239    double rotate_angular_tolerance;
240    unsigned int flick_time_limit_ms;
241    double long_tap_start_timeout;
242    Eina_Bool glayer_continues_enable;
243
244    double zoom_step;
245    double rotate_step;
246
247    Gesture_Info *gesture[ELM_GESTURE_LAST];
248    Ecore_Timer *dbl_timeout; /* When this expires, dbl click/taps ABORTed  */
249    Eina_List *pending; /* List of devices need to refeed *UP event */
250    Eina_List *touched;  /* Information  of touched devices   */
251    Eina_List *recent_device_event;  /* Information  of recent pe event of each device */
252
253    Eina_Bool repeat_events : 1;
254 };
255 typedef struct _Widget_Data Widget_Data;
256
257 static const char *widtype = NULL;
258 static void _del_hook(Evas_Object *obj);
259
260 static Eina_Bool _event_history_clear(Evas_Object *obj);
261 static void _reset_states(Widget_Data *wd);
262 static void _key_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info);
263 static void _key_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info);
264 static void _zoom_with_wheel_test(Evas_Object *obj, void *event_info, Evas_Callback_Type event_type, Elm_Gesture_Types g_type);
265 static void _mouse_wheel(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info);
266 static void _mouse_down(void *data, Evas *e, Evas_Object *obj, void *event_info);
267 static void _mouse_move(void *data, Evas *e, Evas_Object *obj, void *event_info);
268 static void _mouse_up(void *data, Evas *e, Evas_Object *obj, void *event_info);
269
270 static void _multi_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info);
271 static void _multi_move(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info);
272 static void _multi_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info);
273
274 /* START - Functions to manage touched-device list */
275 /**
276  * @internal
277  * This function is used to find if device is touched
278  *
279  * @ingroup Elm_Gesture_Layer
280  */
281 static int
282 compare_device(const void *data1, const void *data2)
283 {  /* Compare the two device numbers */
284    return (((Pointer_Event *) data1)->device -((Pointer_Event *) data2)->device);
285 }
286
287 /**
288  * @internal
289  *
290  * Remove Pointer Event from touched device list
291  * @param list Pointer to touched device list.
292  * @param Pointer_Event Pointer to PE.
293  *
294  * @ingroup Elm_Gesture_Layer
295  */
296 static Eina_List *
297 _remove_touched_device(Eina_List *list, Pointer_Event *pe)
298 {
299    Pointer_Event *p = eina_list_search_unsorted(list, compare_device, pe);
300    if (p)
301      {
302         free(p);
303         return eina_list_remove(list, p);
304      }
305
306    return list;
307 }
308
309 /**
310  * @internal
311  *
312  * Recoed Pointer Event in touched device list
313  * Note: This fuction allocates memory for PE event
314  * This memory is released in _remove_touched_device()
315  * @param list Pointer to touched device list.
316  * @param Pointer_Event Pointer to PE.
317  *
318  * @ingroup Elm_Gesture_Layer
319  */
320 static Eina_List *
321 _add_touched_device(Eina_List *list, Pointer_Event *pe)
322 {
323    Pointer_Event *p = eina_list_search_unsorted(list, compare_device, pe);
324    if (p)
325      {  /* We like to track device touch-position, overwrite info */
326         memcpy(p, pe, sizeof(Pointer_Event));
327         return list;
328      }
329
330    p = malloc(sizeof(Pointer_Event));
331    memcpy(p, pe, sizeof(Pointer_Event)); /* Freed in _remove_touched_device() */
332    return eina_list_append(list, p);
333 }
334 /* END   - Functions to manage touched-device list */
335
336 /* START - Functions to manage recent device event list */
337 /* This list holds the recent-event for each device     */
338 /* it will hold a single recent-event for each device   */
339 /* We are using those PE events of this list to         */
340 /* send them later to test funcs to restart gestures    */
341 /* We only keep DOWN, MOVE events in this list.         */
342 /* So when no touch this list is empty.                 */
343 /**
344  * @internal
345  *
346  * Recoed Pointer Event in touched device list
347  * Note: This fuction allocates memory for PE event
348  * This memory is released here when device untouched
349  * or in cleanup.
350  * @param list Pointer to touched device list.
351  * @param Pointer_Event Pointer to PE.
352  *
353  * @ingroup Elm_Gesture_Layer
354  */
355 static Eina_List *
356 _add_recent_device_event(Eina_List *list, Pointer_Event *pe)
357 {
358    void *data = eina_list_search_sorted(list, compare_device, pe);
359    Eina_List *l = list;
360    Pointer_Event *p = NULL;
361    if(data)   /* First remove recent event for this device */
362      l = eina_list_remove(list, data);
363
364
365    switch(pe->event_type)
366      {
367       case EVAS_CALLBACK_MOUSE_DOWN:
368       case EVAS_CALLBACK_MOUSE_MOVE:
369       case EVAS_CALLBACK_MULTI_DOWN:
370       case EVAS_CALLBACK_MULTI_MOVE:
371          p = malloc(sizeof(Pointer_Event));
372          memcpy(p, pe, sizeof(Pointer_Event)); /* Freed in here or on cleanup */
373          l =  eina_list_sorted_insert(l, compare_device, p);
374          break;
375
376       /* Kept those cases for referance */
377       case EVAS_CALLBACK_MOUSE_IN:
378       case EVAS_CALLBACK_MOUSE_OUT:
379       case EVAS_CALLBACK_MOUSE_WHEEL:
380       case EVAS_CALLBACK_MOUSE_UP:
381       case EVAS_CALLBACK_MULTI_UP:
382       case EVAS_CALLBACK_KEY_DOWN:
383       case EVAS_CALLBACK_KEY_UP:
384          break;
385
386       default:
387          return l;
388      }
389
390    return l;
391 }
392 /* END   - Functions to manage recent device event list */
393
394 /**
395  * @internal
396  *
397  * Get event flag
398  * @param event_info pointer to event.
399  *
400  * @ingroup Elm_Gesture_Layer
401  */
402 static Evas_Event_Flags
403 _get_event_flag(void *event_info, Evas_Callback_Type event_type)
404 {
405    switch(event_type)
406      {
407       case EVAS_CALLBACK_MOUSE_IN:
408          return ((Evas_Event_Mouse_In *) event_info)->event_flags;
409       case EVAS_CALLBACK_MOUSE_OUT:
410          return ((Evas_Event_Mouse_Out *) event_info)->event_flags;
411       case EVAS_CALLBACK_MOUSE_DOWN:
412          return ((Evas_Event_Mouse_Down *) event_info)->event_flags;
413       case EVAS_CALLBACK_MOUSE_MOVE:
414          return ((Evas_Event_Mouse_Move *) event_info)->event_flags;
415       case EVAS_CALLBACK_MOUSE_UP:
416          return ((Evas_Event_Mouse_Up *) event_info)->event_flags;
417       case EVAS_CALLBACK_MOUSE_WHEEL:
418          return ((Evas_Event_Mouse_Wheel *) event_info)->event_flags;
419       case EVAS_CALLBACK_MULTI_DOWN:
420          return ((Evas_Event_Multi_Down *) event_info)->event_flags;
421       case EVAS_CALLBACK_MULTI_MOVE:
422          return ((Evas_Event_Multi_Move *) event_info)->event_flags;
423       case EVAS_CALLBACK_MULTI_UP:
424          return ((Evas_Event_Multi_Up *) event_info)->event_flags;
425       case EVAS_CALLBACK_KEY_DOWN:
426          return ((Evas_Event_Key_Down *) event_info)->event_flags;
427       case EVAS_CALLBACK_KEY_UP:
428          return ((Evas_Event_Key_Up *) event_info)->event_flags;
429       default:
430          return EVAS_EVENT_FLAG_NONE;
431      }
432 }
433
434 /**
435  * @internal
436  *
437  * Sets event flag to value returned from user callback
438  * @param wd Widget Data
439  * @param event_info pointer to event.
440  * @param event_type what type was ev (mouse down, etc...)
441  * @param ev_flags event flags
442  *
443  * @ingroup Elm_Gesture_Layer
444  */
445 static void
446 consume_event(Widget_Data *wd, void *event_info,
447       Evas_Callback_Type event_type, Evas_Event_Flags ev_flags)
448 {  /* Mark EVAS_EVENT_FLAG_ON_HOLD on events that are used by gesture layer */
449    /* ev_flags != EVAS_EVENT_FLAG_NONE means target used event and g-layer  */
450    /* should not refeed this event.                                         */
451    if(!event_info)
452      return;  /* This happens when restarting gestures  */
453
454    if ((ev_flags) || (!wd->repeat_events))
455      {
456         switch(event_type)
457           {
458            case EVAS_CALLBACK_MOUSE_DOWN:
459               ((Evas_Event_Mouse_Down *) event_info)->event_flags |= ev_flags;
460               break;
461            case EVAS_CALLBACK_MOUSE_MOVE:
462               ((Evas_Event_Mouse_Move *) event_info)->event_flags |= ev_flags;
463               break;
464            case EVAS_CALLBACK_MOUSE_UP:
465               ((Evas_Event_Mouse_Up *) event_info)->event_flags |= ev_flags;
466               break;
467            case EVAS_CALLBACK_MOUSE_WHEEL:
468               ((Evas_Event_Mouse_Wheel *) event_info)->event_flags |= ev_flags;
469               break;
470            case EVAS_CALLBACK_MULTI_DOWN:
471               ((Evas_Event_Multi_Down *) event_info)->event_flags |= ev_flags;
472               break;
473            case EVAS_CALLBACK_MULTI_MOVE:
474               ((Evas_Event_Multi_Move *) event_info)->event_flags |= ev_flags;
475               break;
476            case EVAS_CALLBACK_MULTI_UP:
477               ((Evas_Event_Multi_Up *) event_info)->event_flags |= ev_flags;
478               break;
479            case EVAS_CALLBACK_KEY_DOWN:
480               ((Evas_Event_Key_Down *) event_info)->event_flags |= ev_flags;
481               break;
482            case EVAS_CALLBACK_KEY_UP:
483               ((Evas_Event_Key_Up *) event_info)->event_flags |= ev_flags;
484               break;
485            default:
486               return;
487           }
488      }
489 }
490
491 /**
492  * @internal
493  *
494  * Report current state of a gesture by calling user callback.
495  * @param gesture what gesture state we report.
496  * @param info inforamtion for user callback
497  *
498  * @ingroup Elm_Gesture_Layer
499  */
500 static Evas_Event_Flags
501 _report_state(Gesture_Info *gesture, void *info)
502 {  /* We report current state (START, MOVE, END, ABORT), once */
503 #if defined(DEBUG_GESTURE_LAYER)
504    printf("%s reporting gesture=<%d> state=<%d>\n" , __func__, gesture->g_type,
505          gesture->state);
506 #endif
507    if ((gesture->state != ELM_GESTURE_STATE_UNDEFINED) &&
508          (gesture->fn[gesture->state].cb))
509      {  /* Fill state-info struct and send ptr to user callback */
510         return gesture->fn[gesture->state].cb(
511               gesture->fn[gesture->state].user_data, info);
512      }
513
514    return EVAS_EVENT_FLAG_NONE;
515 }
516
517 /**
518  * @internal
519  *
520  * Update state for a given gesture.
521  * We may update gesture state to:
522  * UNDEFINED - current input did not start gesure yet.
523  * START - gesture started according to input.
524  * MOVE - gusture in progress.
525  * END - gesture completed according to input.
526  * ABORT - input does not matches gesure.
527  * note that we may move from UNDEFINED to ABORT
528  * because we may detect that gesture will not START
529  * with a given input.
530  *
531  * @param g given gesture to change state.
532  * @param s gesure new state.
533  * @param info buffer to be sent to user callback on report_state.
534  * @param force makes report_state to report the new-state even
535  * if its same as current state. Works for MOVE - gesture in progress.
536  *
537  * @ingroup Elm_Gesture_Layer
538  */
539 static Evas_Event_Flags
540 _set_state(Gesture_Info *g, Elm_Gesture_State s,
541       void *info, Eina_Bool force)
542 {
543    Elm_Gesture_State old_state;
544    if ((g->state == s) && (!force))
545      return EVAS_EVENT_FLAG_NONE;
546
547    old_state = g->state;
548
549    g->state = s;
550    g->info = info;  /* Information for user callback */
551    if ((g->state == ELM_GESTURE_STATE_ABORT) ||
552          (g->state == ELM_GESTURE_STATE_END))
553      g->test = EINA_FALSE;
554
555    if ((g->state != ELM_GESTURE_STATE_UNDEFINED) &&
556          (!((old_state == ELM_GESTURE_STATE_UNDEFINED) &&
557             (s == ELM_GESTURE_STATE_ABORT))))
558      return _report_state(g, g->info);
559
560    return EVAS_EVENT_FLAG_NONE;
561 }
562
563 /**
564  * @internal
565  *
566  * This resets all gesture states and sets test-bit.
567  * this is used for restarting gestures to listen to input.
568  * happens after we complete a gesture or no gesture was detected.
569  * @param wd Widget data of the gesture-layer object.
570  *
571  * @ingroup Elm_Gesture_Layer
572  */
573 static void
574 _reset_states(Widget_Data *wd)
575 {
576    int i;
577    Gesture_Info *p;
578    for (i = ELM_GESTURE_FIRST; i < ELM_GESTURE_LAST; i++)
579      {
580         p = wd->gesture[i];
581         if (p)
582           {
583              _set_state(p, ELM_GESTURE_STATE_UNDEFINED, NULL, EINA_FALSE);
584              SET_TEST_BIT(p);
585           }
586      }
587 }
588
589 /**
590  * @internal
591  *
592  * if gesture was NOT detected AND we only have gestures in ABORT state
593  * we clear history immediately to be ready for input.
594  *
595  * @param obj The gesture-layer object.
596  * @return TRUE on event history_clear
597  *
598  * @ingroup Elm_Gesture_Layer
599  */
600 static Eina_Bool
601 _clear_if_finished(Evas_Object *obj)
602 {
603    Widget_Data *wd = elm_widget_data_get(obj);
604    if (!wd) return EINA_FALSE;
605    int i;
606
607    /* Clear history if all we have aborted gestures */
608    Eina_Bool reset_s = EINA_TRUE, all_undefined = EINA_TRUE;
609    for (i = ELM_GESTURE_FIRST; i < ELM_GESTURE_LAST; i++)
610      {  /* If no gesture started and all we have aborted gestures, reset all */
611         Gesture_Info *p = wd->gesture[i];
612         if ((p) && (p->state != ELM_GESTURE_STATE_UNDEFINED))
613           {
614              if ((p->state == ELM_GESTURE_STATE_START) ||
615                    (p->state == ELM_GESTURE_STATE_MOVE))
616                reset_s = EINA_FALSE;
617
618              all_undefined = EINA_FALSE;
619           }
620      }
621
622    if (reset_s && (!all_undefined))
623      return _event_history_clear(obj);
624
625    return EINA_FALSE;
626 }
627
628 static Eina_Bool
629 _inside(Evas_Coord x1, Evas_Coord y1, Evas_Coord x2, Evas_Coord y2)
630 {
631    int w = elm_finger_size_get() >> 1; /* Finger size devided by 2 */
632    if (x1 < (x2 - w))
633      return EINA_FALSE;
634
635    if (x1 > (x2 + w))
636      return EINA_FALSE;
637
638    if (y1 < (y2 - w))
639      return EINA_FALSE;
640
641    if (y1 > (y2 + w))
642      return EINA_FALSE;
643
644    return EINA_TRUE;
645 }
646
647 /* All *test_reset() funcs are called to clear
648  * gesture intermediate data.
649  * This happens when we need to reset our tests.
650  * for example when gesture is detected or all ABORTed. */
651 static void
652 _tap_gestures_test_reset(Gesture_Info *gesture)
653 {
654    if (!gesture)
655      return;
656
657    Widget_Data *wd = elm_widget_data_get(gesture->obj);
658    wd->dbl_timeout = NULL;
659    Eina_List *data;
660    Pointer_Event *pe;
661
662    if (!gesture->data)
663      return;
664
665    EINA_LIST_FREE(((Taps_Type *) gesture->data)->l, data)
666       EINA_LIST_FREE(data, pe)
667          free(pe);
668
669   memset(gesture->data, 0, sizeof(Taps_Type));
670 }
671
672 /* All *test_reset() funcs are called to clear
673  * gesture intermediate data.
674  * This happens when we need to reset our tests.
675  * for example when gesture is detected or all ABORTed. */
676 static void
677 _n_long_tap_test_reset(Gesture_Info *gesture)
678 {
679    if (!gesture)
680      return;
681
682    if (!gesture->data)
683      return;
684
685    Long_Tap_Type *st = gesture->data;
686    Eina_List *l;
687    Pointer_Event *p;
688    EINA_LIST_FOREACH(st->touched, l, p)
689       free(p);
690
691    eina_list_free(st->touched);
692    if (st->timeout) ecore_timer_del(st->timeout);
693    memset(gesture->data, 0, sizeof(Long_Tap_Type));
694 }
695
696 static void
697 _momentum_test_reset(Gesture_Info *gesture)
698 {
699    if (!gesture)
700      return;
701
702    if (!gesture->data)
703      return;
704
705    memset(gesture->data, 0, sizeof(Momentum_Type));
706 }
707
708 static void
709 _line_data_reset(Line_Data *st)
710 {
711    if (!st)
712      return;
713
714    memset(st, 0, sizeof(Line_Data));
715    st->line_angle = ELM_GESTURE_NEGATIVE_ANGLE;
716 }
717
718 static void
719 _line_test_reset(Gesture_Info *gesture)
720 {
721    if (!gesture)
722      return;
723
724    if (!gesture->data)
725      return;
726
727    Line_Type *st = gesture->data;
728    Eina_List *list = st->list;
729    Eina_List *l;
730    Line_Data *t_line;
731    EINA_LIST_FOREACH(list, l, t_line)
732       free(t_line);
733
734    eina_list_free(list);
735    st->list = NULL;
736 }
737
738 static void
739 _zoom_test_reset(Gesture_Info *gesture)
740 {
741    if (!gesture)
742      return;
743
744    if (!gesture->data)
745      return;
746
747    Widget_Data *wd = elm_widget_data_get(gesture->obj);
748    Zoom_Type *st = gesture->data;
749    Evas_Modifier_Mask mask = evas_key_modifier_mask_get(
750          evas_object_evas_get(wd->target), "Control");
751    evas_object_key_ungrab(wd->target, "Control_L", mask, 0);
752    evas_object_key_ungrab(wd->target, "Control_R", mask, 0);
753
754    memset(st, 0, sizeof(Zoom_Type));
755    st->zoom_distance_tolerance = wd->zoom_distance_tolerance;
756    st->info.zoom = 1.0;
757 }
758
759 static void
760 _rotate_test_reset(Gesture_Info *gesture)
761 {
762    if (!gesture)
763      return;
764
765    if (!gesture->data)
766      return;
767
768    Widget_Data *wd = elm_widget_data_get(gesture->obj);
769    Rotate_Type *st = gesture->data;
770
771    memset(st, 0, sizeof(Rotate_Type));
772    st->info.base_angle = ELM_GESTURE_NEGATIVE_ANGLE;
773    st->rotate_angular_tolerance = wd->rotate_angular_tolerance;
774 }
775
776
777 /**
778  * @internal
779  *
780  * We register callbacks when gesture layer is attached to an object
781  * or when its enabled after disable.
782  *
783  * @param obj The gesture-layer object.
784  *
785  * @ingroup Elm_Gesture_Layer
786  */
787 static void
788 _register_callbacks(Evas_Object *obj)
789 {
790    Widget_Data *wd = elm_widget_data_get(obj);
791    if (!wd) return;
792
793    if (wd->target)
794      {
795         evas_object_event_callback_add(wd->target, EVAS_CALLBACK_MOUSE_DOWN,
796               _mouse_down, obj);
797         evas_object_event_callback_add(wd->target, EVAS_CALLBACK_MOUSE_MOVE,
798               _mouse_move, obj);
799         evas_object_event_callback_add(wd->target, EVAS_CALLBACK_MOUSE_UP,
800               _mouse_up, obj);
801
802         evas_object_event_callback_add(wd->target, EVAS_CALLBACK_MOUSE_WHEEL,
803               _mouse_wheel, obj);
804
805         evas_object_event_callback_add(wd->target, EVAS_CALLBACK_MULTI_DOWN,
806               _multi_down, obj);
807         evas_object_event_callback_add(wd->target, EVAS_CALLBACK_MULTI_MOVE,
808               _multi_move, obj);
809         evas_object_event_callback_add(wd->target, EVAS_CALLBACK_MULTI_UP,
810               _multi_up, obj);
811
812         evas_object_event_callback_add(wd->target, EVAS_CALLBACK_KEY_DOWN,
813               _key_down_cb, obj);
814         evas_object_event_callback_add(wd->target, EVAS_CALLBACK_KEY_UP,
815               _key_up_cb, obj);
816      }
817 }
818
819 /**
820  * @internal
821  *
822  * We unregister callbacks when gesture layer is disabled.
823  *
824  * @param obj The gesture-layer object.
825  *
826  * @ingroup Elm_Gesture_Layer
827  */
828 static void
829 _unregister_callbacks(Evas_Object *obj)
830 {
831    Widget_Data *wd = elm_widget_data_get(obj);
832    if (!wd) return;
833
834    if (wd->target)
835      {
836         evas_object_event_callback_del(wd->target, EVAS_CALLBACK_MOUSE_DOWN,
837               _mouse_down);
838         evas_object_event_callback_del(wd->target, EVAS_CALLBACK_MOUSE_MOVE,
839               _mouse_move);
840         evas_object_event_callback_del(wd->target, EVAS_CALLBACK_MOUSE_UP,
841               _mouse_up);
842
843         evas_object_event_callback_del(wd->target, EVAS_CALLBACK_MOUSE_WHEEL,
844               _mouse_wheel);
845
846         evas_object_event_callback_del(wd->target, EVAS_CALLBACK_MULTI_DOWN,
847               _multi_down);
848
849         evas_object_event_callback_del(wd->target, EVAS_CALLBACK_MULTI_MOVE,
850               _multi_move);
851
852         evas_object_event_callback_del(wd->target, EVAS_CALLBACK_MULTI_UP,
853               _multi_up);
854
855         evas_object_event_callback_del(wd->target, EVAS_CALLBACK_KEY_DOWN,
856               _key_down_cb);
857         evas_object_event_callback_del(wd->target, EVAS_CALLBACK_KEY_UP,
858               _key_up_cb);
859      }
860 }
861
862 /* START - Event history list handling functions */
863 /**
864  * @internal
865  * This function is used to find if device number
866  * is found in a list of devices.
867  * The list contains devices for refeeding *UP event
868  *
869  * @ingroup Elm_Gesture_Layer
870  */
871 static int
872 device_in_pending_list(const void *data1, const void *data2)
873 {  /* Compare the two device numbers */
874    return (((intptr_t) data1) - ((intptr_t) data2));
875 }
876
877 /**
878  * @internal
879  *
880  * This functions adds device to refeed-pending device list
881  * @ingroup Elm_Gesture_Layer
882  */
883 static Eina_List *
884 _add_device_pending(Eina_List *list, void *event, Evas_Callback_Type event_type)
885 {
886    int device = ELM_MOUSE_DEVICE;
887    switch(event_type)
888      {
889       case EVAS_CALLBACK_MOUSE_DOWN:
890          break;
891       case EVAS_CALLBACK_MULTI_DOWN:
892          device = ((Evas_Event_Multi_Down *) event)->device;
893          break;
894       default:
895          return list;
896      }
897
898    if (!eina_list_search_unsorted_list(list, device_in_pending_list,
899             (intptr_t*) device))
900      {
901         return eina_list_append(list, (intptr_t*) device);
902      }
903
904    return list;
905 }
906
907 /**
908  * @internal
909  *
910  * This functions returns pending-device node
911  * @ingroup Elm_Gesture_Layer
912  */
913 static Eina_List *
914 _device_is_pending(Eina_List *list, void *event, Evas_Callback_Type event_type)
915 {
916    int device = ELM_MOUSE_DEVICE;
917    switch(event_type)
918      {
919       case EVAS_CALLBACK_MOUSE_UP:
920          break;
921       case EVAS_CALLBACK_MULTI_UP:
922          device = ((Evas_Event_Multi_Up *) event)->device;
923          break;
924       default:
925         return NULL;
926      }
927
928    return eina_list_search_unsorted_list(list, device_in_pending_list,
929          (intptr_t *) device);
930 }
931
932 /**
933  * @internal
934  *
935  * This function reports ABORT to all none-detected gestures
936  * Then resets test bits for all desired gesures
937  * and clears input-events history.
938  * note: if no gesture was detected, events from history list
939  * are streamed to the widget because it's unused by layer.
940  * user may cancel refeed of events by setting repeat events.
941  *
942  * @param obj The gesture-layer object.
943  *
944  * @ingroup Elm_Gesture_Layer
945  */
946 static Eina_Bool
947 _event_history_clear(Evas_Object *obj)
948 {
949    Widget_Data *wd = elm_widget_data_get(obj);
950    if (!wd) return EINA_FALSE;
951
952    int i;
953    Gesture_Info *p;
954    Evas *e = evas_object_evas_get(obj);
955    Eina_Bool gesture_found = EINA_FALSE;
956    for (i = ELM_GESTURE_FIRST ; i < ELM_GESTURE_LAST; i++)
957      {
958         p = wd->gesture[i];
959         if (p)
960           {
961              if (p->state == ELM_GESTURE_STATE_END)
962                gesture_found = EINA_TRUE;
963              else
964                {  /* Report ABORT to all gestures that still not finished */
965                   _set_state(p, ELM_GESTURE_STATE_ABORT, wd->gesture[i]->info,
966                         EINA_FALSE);
967                }
968           }
969      }
970
971    _reset_states(wd); /* we are ready to start testing for gestures again */
972
973    /* Clear all gestures intermediate data */
974    if (IS_TESTED(ELM_GESTURE_N_LONG_TAPS))
975      {  /* We do not clear a long-tap gesture if fingers still on surface */
976         /* and gesture timer still pending to test gesture state          */
977         Long_Tap_Type *st = wd->gesture[ELM_GESTURE_N_LONG_TAPS]->data;
978         if((!eina_list_count(st->touched)) || (!st->timeout))
979           _n_long_tap_test_reset(wd->gesture[ELM_GESTURE_N_LONG_TAPS]);
980      }
981
982    if (wd->dbl_timeout)
983      {
984         ecore_timer_del(wd->dbl_timeout);
985         wd->dbl_timeout = NULL;
986      }
987
988    _tap_gestures_test_reset(wd->gesture[ELM_GESTURE_N_TAPS]);
989    _tap_gestures_test_reset(wd->gesture[ELM_GESTURE_N_DOUBLE_TAPS]);
990    _tap_gestures_test_reset(wd->gesture[ELM_GESTURE_N_TRIPLE_TAPS]);
991    _momentum_test_reset(wd->gesture[ELM_GESTURE_MOMENTUM]);
992    _line_test_reset(wd->gesture[ELM_GESTURE_N_LINES]);
993    _line_test_reset(wd->gesture[ELM_GESTURE_N_FLICKS]);
994    _zoom_test_reset(wd->gesture[ELM_GESTURE_ZOOM]);
995    _rotate_test_reset(wd->gesture[ELM_GESTURE_ROTATE]);
996
997    /* Disable gesture layer so refeeded events won't be consumed by it */
998    _unregister_callbacks(obj);
999    while (wd->event_history_list)
1000      {
1001         Event_History *t;
1002         t = wd->event_history_list;
1003         Eina_List *pending = _device_is_pending(wd->pending,
1004               wd->event_history_list->event,
1005               wd->event_history_list->event_type);
1006
1007         /* Refeed events if no gesture matched input */
1008         if (pending || ((!gesture_found) && (!wd->repeat_events)))
1009           {
1010              evas_event_refeed_event(e, wd->event_history_list->event,
1011                    wd->event_history_list->event_type);
1012
1013              if (pending)
1014                {
1015                wd->pending = eina_list_remove_list(wd->pending, pending);
1016                int device = ELM_MOUSE_DEVICE;
1017                if (wd->event_history_list->event_type == EVAS_CALLBACK_MULTI_UP)
1018                  device = ((Evas_Event_Multi_Up *)
1019                        (wd->event_history_list->event))->device;
1020                }
1021              else
1022                wd->pending = _add_device_pending(wd->pending,
1023                      wd->event_history_list->event,
1024                      wd->event_history_list->event_type);
1025           }
1026
1027         free(wd->event_history_list->event);
1028         wd->event_history_list = (Event_History *) eina_inlist_remove(
1029               EINA_INLIST_GET(wd->event_history_list),
1030               EINA_INLIST_GET(wd->event_history_list));
1031         free(t);
1032      }
1033    _register_callbacks(obj);
1034    return EINA_TRUE;
1035 }
1036
1037 /**
1038  * @internal
1039  *
1040  * This function copies input events.
1041  * We copy event info before adding it to history.
1042  * The memory is freed when we clear history.
1043  *
1044  * @param event the event to copy
1045  * @param event_type event type to copy
1046  *
1047  * @ingroup Elm_Gesture_Layer
1048  */
1049 static void *
1050 _copy_event_info(void *event, Evas_Callback_Type event_type)
1051 {
1052    switch(event_type)
1053      {
1054       case EVAS_CALLBACK_MOUSE_DOWN:
1055          return COPY_EVENT_INFO((Evas_Event_Mouse_Down *) event);
1056          break;
1057       case EVAS_CALLBACK_MOUSE_MOVE:
1058          return COPY_EVENT_INFO((Evas_Event_Mouse_Move *) event);
1059          break;
1060       case EVAS_CALLBACK_MOUSE_UP:
1061          return COPY_EVENT_INFO((Evas_Event_Mouse_Up *) event);
1062          break;
1063       case EVAS_CALLBACK_MOUSE_WHEEL:
1064          return COPY_EVENT_INFO((Evas_Event_Mouse_Wheel *) event);
1065          break;
1066       case EVAS_CALLBACK_MULTI_DOWN:
1067          return COPY_EVENT_INFO((Evas_Event_Multi_Down *) event);
1068          break;
1069       case EVAS_CALLBACK_MULTI_MOVE:
1070          return COPY_EVENT_INFO((Evas_Event_Multi_Move *) event);
1071          break;
1072       case EVAS_CALLBACK_MULTI_UP:
1073          return COPY_EVENT_INFO((Evas_Event_Multi_Up *) event);
1074          break;
1075       case EVAS_CALLBACK_KEY_DOWN:
1076          return COPY_EVENT_INFO((Evas_Event_Key_Down *) event);
1077          break;
1078       case EVAS_CALLBACK_KEY_UP:
1079          return COPY_EVENT_INFO((Evas_Event_Key_Up *) event);
1080          break;
1081       default:
1082          return NULL;
1083      }
1084 }
1085
1086 static Eina_Bool
1087 _event_history_add(Evas_Object *obj, void *event, Evas_Callback_Type event_type)
1088 {
1089    Widget_Data *wd = elm_widget_data_get(obj);
1090    Event_History *ev;
1091    if (!wd) return EINA_FALSE;
1092
1093    ev = malloc(sizeof(Event_History));
1094    ev->event = _copy_event_info(event, event_type);  /* Freed on event_history_clear */
1095    ev->event_type = event_type;
1096    wd->event_history_list = (Event_History *) eina_inlist_append(
1097          EINA_INLIST_GET(wd->event_history_list), EINA_INLIST_GET(ev));
1098
1099    return EINA_TRUE;
1100 }
1101 /* END - Event history list handling functions */
1102
1103 static void
1104 _del_hook(Evas_Object *obj)
1105 {
1106    Widget_Data *wd = elm_widget_data_get(obj);
1107    if (!wd) return;
1108
1109    _event_history_clear(obj);
1110    eina_list_free(wd->pending);
1111
1112    Pointer_Event *data;
1113    EINA_LIST_FREE(wd->touched, data)
1114       free(data);
1115
1116    EINA_LIST_FREE(wd->recent_device_event, data)
1117       free(data);
1118
1119    if (!elm_widget_disabled_get(obj))
1120      _unregister_callbacks(obj);
1121
1122    /* Free all gestures internal data structures */
1123    int i;
1124    for (i = 0; i < ELM_GESTURE_LAST; i++)
1125      if (wd->gesture[i])
1126        {
1127           if (wd->gesture[i]->data)
1128             free(wd->gesture[i]->data);
1129
1130           free(wd->gesture[i]);
1131        }
1132
1133    free(wd);
1134 }
1135
1136 static int
1137 compare_match_fingers(const void *data1, const void *data2)
1138 {  /* Compare coords of first item in list to cur coords */
1139    const Pointer_Event *pe1 = eina_list_data_get(data1);
1140    const Pointer_Event *pe2 = data2;
1141
1142    if (_inside(pe1->x, pe1->y, pe2->x, pe2->y))
1143      return 0;
1144    else if (pe1->x < pe2->x)
1145      return -1;
1146    else
1147      {
1148         if (pe1->x == pe2->x)
1149           return pe1->y - pe2->y;
1150         else
1151           return 1;
1152      }
1153 }
1154
1155 static int
1156 compare_pe_device(const void *data1, const void *data2)
1157 {  /* Compare coords of first item in list to cur coords */
1158    const Pointer_Event *pe1 = eina_list_data_get(eina_list_last(data1));
1159    const Pointer_Event *pe2 = data2;
1160
1161    /* Only match if last was a down event */
1162    if ((pe1->event_type != EVAS_CALLBACK_MULTI_DOWN) &&
1163          (pe1->event_type != EVAS_CALLBACK_MOUSE_DOWN))
1164      return 1;
1165
1166
1167    if (pe1->device == pe2->device)
1168      return 0;
1169    else if (pe1->device < pe2->device)
1170      return -1;
1171    else
1172      return 1;
1173 }
1174
1175 static Eina_List*
1176 _record_pointer_event(Taps_Type *st, Eina_List *pe_list, Pointer_Event *pe,
1177       Widget_Data *wd, void *event_info, Evas_Callback_Type event_type)
1178 {  /* Keep copy of pe and record it in list */
1179    Pointer_Event *p = malloc(sizeof(Pointer_Event));
1180    memcpy(p, pe, sizeof(Pointer_Event));
1181    consume_event(wd, event_info, event_type, EVAS_EVENT_FLAG_NONE);
1182
1183    st->sum_x += pe->x;
1184    st->sum_y += pe->y;
1185    st->n_taps++;
1186
1187    /* This will also update middle-point to report to user later */
1188    st->info.x = st->sum_x / st->n_taps;
1189    st->info.y = st->sum_y / st->n_taps;
1190    st->info.timestamp = pe->timestamp;
1191
1192    if (!pe_list)
1193      {
1194         pe_list = eina_list_append(pe_list, p);
1195         st->l = eina_list_append(st->l, pe_list);
1196      }
1197    else
1198      pe_list = eina_list_append(pe_list, p);
1199
1200    return pe_list;
1201 }
1202
1203 /**
1204  * @internal
1205  *
1206  * This function sets state a tap-gesture to END or ABORT
1207  *
1208  * @param data gesture info pointer
1209  *
1210  * @ingroup Elm_Gesture_Layer
1211  */
1212 static void
1213 _tap_gesture_finish(void *data)
1214 {  /* This function will test each tap gesture when timer expires */
1215    Gesture_Info *gesture = data;
1216    Elm_Gesture_State s = ELM_GESTURE_STATE_END;
1217    /* Here we check if taps-gesture was completed successfuly */
1218    /* Count how many taps were recieved on each device then   */
1219    /* determine if it matches n_taps_needed defined on START  */
1220    Taps_Type *st = gesture->data;
1221    Eina_List *l;
1222    Eina_List *pe_list;
1223    EINA_LIST_FOREACH(st->l, l, pe_list)
1224      {
1225         if (eina_list_count(pe_list) != st->n_taps_needed)
1226           {  /* No match taps number on device, ABORT */
1227              s = ELM_GESTURE_STATE_ABORT;
1228              break;
1229           }
1230      }
1231
1232    st->info.n = eina_list_count(st->l);
1233    _set_state(gesture, s, gesture->info, EINA_FALSE);
1234    _tap_gestures_test_reset(gesture);
1235 }
1236
1237 /**
1238  * @internal
1239  *
1240  * when this timer expires we finish tap gestures.
1241  *
1242  * @param data The gesture-layer object.
1243  * @return cancles callback for this timer.
1244  *
1245  * @ingroup Elm_Gesture_Layer
1246  */
1247 static Eina_Bool
1248 _multi_tap_timeout(void *data)
1249 {
1250    Widget_Data *wd = elm_widget_data_get(data);
1251    if (!wd) return EINA_FALSE;
1252
1253    if (IS_TESTED(ELM_GESTURE_N_TAPS))
1254      _tap_gesture_finish(wd->gesture[ELM_GESTURE_N_TAPS]);
1255
1256    if (IS_TESTED(ELM_GESTURE_N_DOUBLE_TAPS))
1257    _tap_gesture_finish(wd->gesture[ELM_GESTURE_N_DOUBLE_TAPS]);
1258
1259    if (IS_TESTED(ELM_GESTURE_N_TRIPLE_TAPS))
1260    _tap_gesture_finish(wd->gesture[ELM_GESTURE_N_TRIPLE_TAPS]);
1261
1262    _clear_if_finished(data);
1263    wd->dbl_timeout = NULL;
1264    return ECORE_CALLBACK_CANCEL;
1265 }
1266
1267 /**
1268  * @internal
1269  *
1270  * when this timer expires we START long tap gesture
1271  *
1272  * @param data The gesture-layer object.
1273  * @return cancles callback for this timer.
1274  *
1275  * @ingroup Elm_Gesture_Layer
1276  */
1277 static Eina_Bool
1278 _long_tap_timeout(void *data)
1279 {
1280    Gesture_Info *gesture = data;
1281    Long_Tap_Type *st = gesture->data;
1282    st->timeout = NULL;
1283
1284    _set_state(gesture, ELM_GESTURE_STATE_START,
1285          gesture->data, EINA_FALSE);
1286
1287    return ECORE_CALLBACK_CANCEL;
1288 }
1289
1290
1291 /**
1292  * @internal
1293  *
1294  * This function checks if a tap gesture should start
1295  *
1296  * @param wd Gesture Layer Widget Data.
1297  * @param pe The recent input event as stored in pe struct.
1298  * @param event_info Original input event pointer.
1299  * @param event_type Type of original input event.
1300  * @param gesture what gesture is tested
1301  * @param how many taps for this gesture (1, 2 or 3)
1302  *
1303  * @return Flag to determine if we need to set a timer for finish
1304  *
1305  * @ingroup Elm_Gesture_Layer
1306  */
1307 static Eina_Bool
1308 _tap_gesture_start(Widget_Data *wd, Pointer_Event *pe,
1309       void *event_info, Evas_Callback_Type event_type,
1310       Gesture_Info *gesture, int taps)
1311 {  /* Here we fill Tap struct */
1312    Taps_Type *st = gesture->data;
1313    if (!st)
1314      {  /* Allocated once on first time */
1315         st = calloc(1, sizeof(Taps_Type));
1316         gesture->data = st;
1317         _tap_gestures_test_reset(gesture);
1318      }
1319
1320    Eina_List *pe_list = NULL;
1321    Pointer_Event *pe_down = NULL;
1322    Evas_Event_Flags ev_flag = EVAS_EVENT_FLAG_NONE;
1323    switch (pe->event_type)
1324      {
1325       case EVAS_CALLBACK_MULTI_DOWN:
1326       case EVAS_CALLBACK_MOUSE_DOWN:
1327          pe_list = eina_list_search_unsorted(st->l, compare_match_fingers, pe);
1328          pe_list = _record_pointer_event(st, pe_list, pe, wd, event_info, event_type);
1329          if ((pe->device == 0) && (eina_list_count(pe_list) == 1))
1330            {  /* This is the first mouse down we got */
1331               ev_flag = _set_state(gesture, ELM_GESTURE_STATE_START,
1332                     &st->info, EINA_FALSE);
1333               consume_event(wd, event_info, event_type, ev_flag);
1334
1335               st->n_taps_needed = taps * 2; /* count DOWN and UP */
1336
1337               return EINA_TRUE;
1338            }
1339
1340          break;
1341
1342       case EVAS_CALLBACK_MULTI_UP:
1343       case EVAS_CALLBACK_MOUSE_UP:
1344          pe_list = eina_list_search_unsorted(st->l, compare_pe_device, pe);
1345          if (!pe_list)
1346            return EINA_FALSE;
1347
1348          pe_list = _record_pointer_event(st, pe_list, pe, wd, event_info, event_type);
1349          break;
1350
1351       case EVAS_CALLBACK_MULTI_MOVE:
1352       case EVAS_CALLBACK_MOUSE_MOVE:
1353          /* Get first event in first list, this has to be a Mouse Down event  */
1354          /* and verify that user didn't move out of this area before next tap */
1355          pe_list = eina_list_search_unsorted(st->l, compare_pe_device, pe);
1356          if (pe_list)
1357            {
1358               pe_down = eina_list_data_get(pe_list);
1359               if (!_inside(pe_down->x, pe_down->y, pe->x, pe->y))
1360                 {
1361                    ev_flag = _set_state(gesture, ELM_GESTURE_STATE_ABORT,
1362                          &st->info, EINA_FALSE);
1363                    consume_event(wd, event_info, event_type, ev_flag);
1364                 }
1365            }
1366          break;
1367
1368       default:
1369          return EINA_FALSE;
1370      }
1371
1372    return EINA_FALSE;
1373 }
1374
1375
1376 /**
1377  * @internal
1378  *
1379  * This function checks all click/tap and double/triple taps
1380  *
1381  * @param obj The gesture-layer object.
1382  * @param pe The recent input event as stored in pe struct.
1383  * @param event_info Original input event pointer.
1384  * @param event_type Type of original input event.
1385  *
1386  * @ingroup Elm_Gesture_Layer
1387  */
1388 static void
1389 _tap_gestures_test(Evas_Object *obj, Pointer_Event *pe,
1390       void *event_info, Evas_Callback_Type event_type)
1391 {  /* Here we fill Recent_Taps struct and fire-up click/tap timers */
1392    Eina_Bool need_timer = EINA_FALSE;
1393    Widget_Data *wd = elm_widget_data_get(obj);
1394    if (!wd) return;
1395
1396    if (!pe)   /* this happens when unhandled event arrived */
1397      return;  /* see _make_pointer_event function */
1398
1399    if (IS_TESTED(ELM_GESTURE_N_TAPS))
1400      need_timer |= _tap_gesture_start(wd, pe, event_info, event_type,
1401            wd->gesture[ELM_GESTURE_N_TAPS], 1);
1402
1403    if (IS_TESTED(ELM_GESTURE_N_DOUBLE_TAPS))
1404      need_timer |= _tap_gesture_start(wd, pe, event_info, event_type,
1405            wd->gesture[ELM_GESTURE_N_DOUBLE_TAPS], 2);
1406
1407    if (IS_TESTED(ELM_GESTURE_N_TRIPLE_TAPS))
1408      need_timer |= _tap_gesture_start(wd, pe, event_info, event_type,
1409            wd->gesture[ELM_GESTURE_N_TRIPLE_TAPS], 3);
1410
1411    if ((need_timer) && (!wd->dbl_timeout))
1412      {  /* Set a timer to finish these gestures */
1413         wd->dbl_timeout = ecore_timer_add(0.4, _multi_tap_timeout,
1414               obj);
1415      }
1416 }
1417
1418 /**
1419  * @internal
1420  *
1421  * This function computes center-point for  long-tap gesture
1422  *
1423  * @param st Long Tap gesture info pointer
1424  * @param pe The recent input event as stored in pe struct.
1425  *
1426  * @ingroup Elm_Gesture_Layer
1427  */
1428 static void
1429 _compute_taps_center(Long_Tap_Type *st, Pointer_Event *pe)
1430 {
1431    if(!eina_list_count(st->touched))
1432      return;
1433
1434    Eina_List *l;
1435    Pointer_Event *p;
1436    Evas_Coord x = 0, y = 0;
1437    EINA_LIST_FOREACH(st->touched, l, p)
1438      {  /* Accumulate all then take avarage */
1439         if (p->device == pe->device)
1440           {  /* This will take care of values coming from MOVE event */
1441              x += pe->x;
1442              y += pe->y;
1443           }
1444         else
1445           {
1446              x += p->x;
1447              y += p->y;
1448           }
1449      }
1450
1451    st->info.x = x / eina_list_count(st->touched);
1452    st->info.y = y / eina_list_count(st->touched);
1453 }
1454
1455 /**
1456  * @internal
1457  *
1458  * This function checks N long-tap gesture.
1459  *
1460  * @param obj The gesture-layer object.
1461  * @param pe The recent input event as stored in pe struct.
1462  * @param event_info Original input event pointer.
1463  * @param event_type Type of original input event.
1464  * @param g_type what Gesture we are testing.
1465  * @param taps How many click/taps we test for.
1466  *
1467  * @ingroup Elm_Gesture_Layer
1468  */
1469 static void
1470 _n_long_tap_test(Evas_Object *obj, Pointer_Event *pe,
1471       void *event_info, Evas_Callback_Type event_type,
1472       Elm_Gesture_Types g_type)
1473 {  /* Here we fill Recent_Taps struct and fire-up click/tap timers */
1474    Widget_Data *wd = elm_widget_data_get(obj);
1475    if (!wd) return;
1476
1477    if (!pe)   /* this happens when unhandled event arrived */
1478      return; /* see _make_pointer_event function */
1479
1480    Gesture_Info *gesture = wd->gesture[g_type];
1481    if (!gesture ) return;
1482
1483    Long_Tap_Type *st = gesture->data;
1484    if (!st)
1485      {  /* Allocated once on first time */
1486         st = calloc(1, sizeof(Long_Tap_Type));
1487         gesture->data = st;
1488         _n_long_tap_test_reset(gesture);
1489      }
1490
1491    Evas_Event_Flags ev_flag = EVAS_EVENT_FLAG_NONE;
1492    switch (pe->event_type)
1493      {
1494       case EVAS_CALLBACK_MULTI_DOWN:
1495       case EVAS_CALLBACK_MOUSE_DOWN:
1496          st->touched = _add_touched_device(st->touched, pe);
1497          st->info.n = eina_list_count(st->touched);
1498          if ((pe->device == 0) && (eina_list_count(st->touched) == 1))
1499            {  /* This is the first mouse down we got */
1500               st->info.timestamp = pe->timestamp;
1501
1502               /* To test long tap */
1503               /* When this timer expires, gesture STARTED */
1504               if (!st->timeout)
1505                 st->timeout = ecore_timer_add(wd->long_tap_start_timeout,
1506                       _long_tap_timeout, gesture);
1507            }
1508
1509          consume_event(wd, event_info, event_type, ev_flag);
1510          _compute_taps_center(st, pe);
1511          break;
1512
1513       case EVAS_CALLBACK_MULTI_UP:
1514       case EVAS_CALLBACK_MOUSE_UP:
1515          st->touched = _remove_touched_device(st->touched, pe);
1516          if (st->info.n &&
1517                ((gesture->state == ELM_GESTURE_STATE_START) ||
1518                 (gesture->state == ELM_GESTURE_STATE_MOVE)))
1519            {  /* Report END only for gesture that STARTed */
1520               if (eina_list_count(st->touched) == 0)
1521                 {  /* Report END only at last release event */
1522                    ev_flag =_set_state(gesture, ELM_GESTURE_STATE_END,
1523                          &st->info, EINA_FALSE);
1524                    consume_event(wd, event_info, event_type, ev_flag);
1525                 }
1526            }
1527          else
1528            {  /* Stop test, user lifts finger before long-start */
1529               if (st->timeout) ecore_timer_del(st->timeout);
1530               st->timeout = NULL;
1531               ev_flag =_set_state(gesture, ELM_GESTURE_STATE_ABORT,
1532                     &st->info, EINA_FALSE);
1533               consume_event(wd, event_info, event_type, ev_flag);
1534            }
1535
1536          break;
1537
1538       case EVAS_CALLBACK_MULTI_MOVE:
1539       case EVAS_CALLBACK_MOUSE_MOVE:
1540          if(st->info.n &&
1541                ((gesture->state == ELM_GESTURE_STATE_START) ||
1542                 (gesture->state == ELM_GESTURE_STATE_MOVE)))
1543            {  /* Report MOVE only if STARTED */
1544               _compute_taps_center(st, pe);
1545               /* Report MOVE if gesture started */
1546               ev_flag = _set_state(gesture, ELM_GESTURE_STATE_MOVE,
1547                     &st->info, EINA_TRUE);
1548               consume_event(wd, event_info, event_type, ev_flag);
1549            }
1550          break;
1551
1552       default:
1553          return;
1554      }
1555 }
1556
1557 /**
1558  * @internal
1559  *
1560  * This function computes momentum for MOMENTUM, LINE and FLICK gestures
1561  * This momentum value will be sent to widget when gesture is completed.
1562  *
1563  * @param momentum pointer to buffer where we record momentum value.
1564  * @param x1 x coord where user started gesture.
1565  * @param y1 y coord where user started gesture.
1566  * @param x2 x coord where user completed gesture.
1567  * @param y2 y coord where user completed gesture.
1568  * @param t1x timestamp for X, when user started gesture.
1569  * @param t1y timestamp for Y, when user started gesture.
1570  * @param t2  timestamp when user completed gesture.
1571  *
1572  * @ingroup Elm_Gesture_Layer
1573  */
1574 static void
1575 _set_momentum(Elm_Gesture_Momentum_Info *momentum, Evas_Coord x1, Evas_Coord y1,
1576       Evas_Coord x2, Evas_Coord y2, unsigned int t1x, unsigned int t1y,
1577       unsigned int t2)
1578 {
1579    Evas_Coord velx = 0, vely = 0, vel;
1580    Evas_Coord dx = x2 - x1;
1581    Evas_Coord dy = y2 - y1;
1582    int dtx = t2 - t1x;
1583    int dty = t2 - t1y;
1584    if (dtx > 0)
1585      velx = (dx * 1000) / dtx;
1586
1587    if (dty > 0)
1588      vely = (dy * 1000) / dty;
1589
1590    vel = sqrt((velx * velx) + (vely * vely));
1591
1592    if ((_elm_config->thumbscroll_friction > 0.0) &&
1593          (vel > _elm_config->thumbscroll_momentum_threshold))
1594      {  /* report momentum */
1595         momentum->mx = velx;
1596         momentum->my = vely;
1597      }
1598    else
1599      {
1600         momentum->mx = 0;
1601         momentum->my = 0;
1602      }
1603 }
1604
1605 /**
1606  * @internal
1607  *
1608  * This function is used for computing rotation angle (DEG).
1609  *
1610  * @param x1 first finger x location.
1611  * @param y1 first finger y location.
1612  * @param x2 second finger x location.
1613  * @param y2 second finger y location.
1614  *
1615  * @return angle of the line between (x1,y1), (x2,y2) in Radians.
1616  *
1617  * @ingroup Elm_Gesture_Layer
1618  */
1619 static double
1620 get_angle(Evas_Coord x1, Evas_Coord y1, Evas_Coord x2, Evas_Coord y2)
1621 {
1622    double a, xx, yy;
1623    xx = fabs(x2 - x1);
1624    yy = fabs(y2 - y1);
1625
1626    if (((int) xx) && ((int) yy))
1627      {
1628         a = atan(yy / xx);
1629         if (x1 < x2)
1630           {
1631              if (y1 < y2)
1632                {
1633                   return RAD_360DEG - a;
1634                }
1635              else
1636                {
1637                   return (a);
1638                }
1639           }
1640         else
1641           {
1642              if (y1 < y2)
1643                {
1644                   return RAD_180DEG + a;
1645                }
1646              else
1647                {
1648                   return RAD_180DEG - a;
1649                }
1650           }
1651      }
1652
1653    if (((int) xx))
1654      {  /* Horizontal line */
1655         if (x2 < x1)
1656           {
1657              return RAD_180DEG;
1658           }
1659         else
1660           {
1661              return 0.0;
1662           }
1663      }
1664
1665    /* Vertical line */
1666    if (y2 < y1)
1667      {
1668         return RAD_90DEG;
1669      }
1670    else
1671      {
1672         return RAD_270DEG;
1673      }
1674 }
1675
1676 /**
1677  * @internal
1678  *
1679  * This function is used for computing the magnitude and direction
1680  * of vector between two points.
1681  *
1682  * @param x1 first finger x location.
1683  * @param y1 first finger y location.
1684  * @param x2 second finger x location.
1685  * @param y2 second finger y location.
1686  * @param l length computed (output)
1687  * @param a angle computed (output)
1688  *
1689  * @ingroup Elm_Gesture_Layer
1690  */
1691 static void
1692 get_vector(Evas_Coord x1, Evas_Coord y1, Evas_Coord x2, Evas_Coord y2,
1693       Evas_Coord *l, double *a)
1694 {
1695    Evas_Coord xx, yy;
1696    xx = x2 - x1;
1697    yy = y2 - y1;
1698    *l = (Evas_Coord) sqrt(xx*xx + yy*yy);
1699    *a = get_angle(x1, y1, x2, y2);
1700 }
1701
1702 static int
1703 _get_direction(Evas_Coord x1, Evas_Coord x2)
1704 {
1705    if (x1 == x2)
1706      return 0;
1707    else if (x2 < x1)
1708      return -1;
1709    else
1710      return 1;
1711 }
1712 /**
1713  * @internal
1714  *
1715  * This function tests momentum gesture.
1716  * @param obj The gesture-layer object.
1717  * @param pe The recent input event as stored in pe struct.
1718  * @param event_info recent input event.
1719  * @param event_type recent event type.
1720  * @param g_type what Gesture we are testing.
1721  *
1722  * @ingroup Elm_Gesture_Layer
1723  */
1724 static void
1725 _momentum_test(Evas_Object *obj, Pointer_Event *pe,
1726       void *event_info, Evas_Callback_Type event_type,
1727       Elm_Gesture_Types g_type)
1728 {
1729    Widget_Data *wd = elm_widget_data_get(obj);
1730    if (!wd) return;
1731    Gesture_Info *gesture = wd->gesture[g_type];
1732    if (!gesture ) return;
1733
1734    Momentum_Type *st = gesture->data;
1735    Elm_Gesture_State state_to_report;
1736    if (!st)
1737      {  /* Allocated once on first time */
1738         st = calloc(1, sizeof(Momentum_Type));
1739         gesture->data = st;
1740         _momentum_test_reset(gesture);
1741      }
1742
1743    if (!pe)
1744      return;
1745
1746    Evas_Event_Flags ev_flag = EVAS_EVENT_FLAG_NONE;
1747    switch (event_type)
1748      {
1749       case EVAS_CALLBACK_MOUSE_DOWN:
1750       case EVAS_CALLBACK_MOUSE_MOVE:
1751          if (!st->t_st_x)
1752            {
1753               if ((event_type == EVAS_CALLBACK_MOUSE_DOWN) ||
1754                     (wd->glayer_continues_enable)) /* start also on MOVE */
1755                 {  /* We start on MOVE when cont-enabled only */
1756                    st->line_st.x = st->line_end.x = pe->x;
1757                    st->line_st.y = st->line_end.y = pe->y;
1758                    st->t_st_x = st->t_st_y = st->t_end = pe->timestamp;
1759                    st->xdir = st->ydir = 0;
1760                    st->info.x2 = st->info.x1 = pe->x;
1761                    st->info.y2 = st->info.y1 = pe->y;
1762                    st->info.tx = st->info.ty = pe->timestamp;
1763                    ev_flag = _set_state(gesture, ELM_GESTURE_STATE_START,
1764                          &st->info, EINA_FALSE);
1765                    consume_event(wd, event_info, event_type, ev_flag);
1766
1767                 }
1768
1769               return;
1770            }
1771
1772          state_to_report = ELM_GESTURE_STATE_MOVE;
1773          if ((pe->timestamp - ELM_GESTURE_MOMENTUM_TIMEOUT) > st->t_end)
1774            {  /*  Too long of a wait, reset all values */
1775               st->line_st.x = pe->x;
1776               st->line_st.y = pe->y;
1777               st->t_st_y = st->t_st_x = pe->timestamp;
1778               st->info.tx = st->t_st_x;
1779               st->info.ty = st->t_st_y;
1780               st->xdir = st->ydir = 0;
1781            }
1782          else
1783            {
1784               int xdir, ydir;
1785               xdir = _get_direction(st->line_st.x, pe->x);
1786               ydir = _get_direction(st->line_st.y, pe->y);
1787               if (!xdir || (xdir == (-st->xdir)))
1788                 {
1789                    st->line_st.x = st->line_end.x;
1790                    st->info.tx = st->t_st_x = st->t_end;
1791                    st->xdir = xdir;
1792                 }
1793
1794               if (!ydir || (ydir == (-st->ydir)))
1795                 {
1796                    st->line_st.y = st->line_end.y;
1797                    st->info.ty = st->t_st_y = st->t_end;
1798                    st->ydir = ydir;
1799                 }
1800            }
1801
1802          st->info.x2 = st->line_end.x = pe->x;
1803          st->info.y2 = st->line_end.y = pe->y;
1804          st->t_end = pe->timestamp;
1805          _set_momentum(&st->info, st->line_st.x, st->line_st.y, pe->x, pe->y,
1806                st->t_st_x, st->t_st_y, pe->timestamp);
1807          ev_flag = _set_state(gesture, state_to_report, &st->info,
1808                EINA_TRUE);
1809          consume_event(wd, event_info, event_type, ev_flag);
1810          break;
1811
1812
1813       case EVAS_CALLBACK_MOUSE_UP:
1814          /* IGNORE if line info was cleared, like long press, move */
1815          if (!st->t_st_x)
1816            return;
1817          state_to_report = ELM_GESTURE_STATE_END;
1818
1819          if ((pe->timestamp - ELM_GESTURE_MOMENTUM_TIMEOUT) > st->t_end)
1820            {  /* Too long of a wait, reset all values */
1821               st->line_st.x = pe->x;
1822               st->line_st.y = pe->y;
1823               st->t_st_y = st->t_st_x = pe->timestamp;
1824               st->xdir = st->ydir = 0;
1825            }
1826
1827          st->info.x2 = pe->x;
1828          st->info.y2 = pe->y;
1829          st->line_end.x = pe->x;
1830          st->line_end.y = pe->y;
1831          st->t_end = pe->timestamp;
1832
1833          _set_momentum(&st->info, st->line_st.x, st->line_st.y, pe->x, pe->y,
1834                st->t_st_x, st->t_st_y, pe->timestamp);
1835
1836          ev_flag = _set_state(gesture, state_to_report, &st->info,
1837                EINA_FALSE);
1838          consume_event(wd, event_info, event_type, ev_flag);
1839
1840          return;
1841
1842       case EVAS_CALLBACK_MULTI_UP:
1843          ev_flag = _set_state(gesture, ELM_GESTURE_STATE_ABORT, &st->info,
1844                EINA_FALSE);
1845          consume_event(wd, event_info, event_type, ev_flag);
1846          return;
1847
1848       default:
1849          return;
1850      }
1851 }
1852
1853 static int
1854 compare_line_device(const void *data1, const void *data2)
1855 {  /* Compare device component of line struct */
1856    const Line_Data *ln1 = data1;
1857    const int *device = data2;
1858
1859    if (ln1->t_st) /* Compare only with lines that started */
1860      return (ln1->device - (*device));
1861
1862    return (-1);
1863 }
1864
1865 /**
1866  * @internal
1867  *
1868  * This function construct line struct from input.
1869  * @param info pointer to store line momentum.
1870  * @param st line info to store input data.
1871  * @param pe The recent input event as stored in pe struct.
1872  *
1873  * @ingroup Elm_Gesture_Layer
1874  */
1875 static Eina_Bool
1876 _single_line_process(Elm_Gesture_Line_Info *info, Line_Data *st,
1877       Pointer_Event *pe, Evas_Callback_Type event_type)
1878 {  /* Record events and set momentum for line pointed by st */
1879    if (!pe)
1880      return EINA_FALSE;
1881
1882    switch (event_type)
1883      {
1884       case EVAS_CALLBACK_MOUSE_DOWN:
1885       case EVAS_CALLBACK_MOUSE_MOVE:
1886       case EVAS_CALLBACK_MULTI_DOWN:
1887       case EVAS_CALLBACK_MULTI_MOVE:
1888          if (!st->t_st)
1889            {  /* This happens only when line starts */
1890               st->line_st.x = pe->x;
1891               st->line_st.y = pe->y;
1892               st->t_st = pe->timestamp;
1893               st->device = pe->device;
1894               info->momentum.x1 = pe->x;
1895               info->momentum.y1 = pe->y;
1896               info->momentum.tx = pe->timestamp;
1897               info->momentum.ty = pe->timestamp;
1898
1899               return EINA_TRUE;
1900            }
1901
1902          break;
1903
1904       case EVAS_CALLBACK_MOUSE_UP:
1905       case EVAS_CALLBACK_MULTI_UP:
1906          /* IGNORE if line info was cleared, like long press, move */
1907          if (!st->t_st)
1908            return EINA_FALSE;
1909
1910          st->line_end.x = pe->x;
1911          st->line_end.y = pe->y;
1912          st->t_end = pe->timestamp;
1913          break;
1914
1915       default:
1916          return EINA_FALSE;
1917      }
1918
1919    if (!st->t_st)
1920      {
1921         _line_data_reset(st);
1922         return EINA_FALSE;
1923      }
1924
1925    info->momentum.x2 = pe->x;
1926    info->momentum.y2 = pe->y;
1927    _set_momentum(&info->momentum, st->line_st.x, st->line_st.y, pe->x, pe->y,
1928          st->t_st, st->t_st, pe->timestamp);
1929
1930    return EINA_TRUE;
1931 }
1932
1933 /**
1934  * @internal
1935  *
1936  * This function test for (n) line gesture.
1937  * @param obj The gesture-layer object.
1938  * @param pe The recent input event as stored in pe struct.
1939  * @param event_info Original input event pointer.
1940  * @param event_type Type of original input event.
1941  * @param g_type what Gesture we are testing.
1942  *
1943  * @ingroup Elm_Gesture_Layer
1944  */
1945 static void
1946 _n_line_test(Evas_Object *obj, Pointer_Event *pe, void *event_info,
1947       Evas_Callback_Type event_type, Elm_Gesture_Types g_type)
1948 {
1949    if (!pe)
1950      return;
1951    Widget_Data *wd = elm_widget_data_get(obj);
1952    if (!wd) return;
1953    Gesture_Info *gesture = wd->gesture[g_type];
1954    if (!gesture ) return;
1955
1956    Line_Type *st = gesture->data;
1957    if (!st)
1958      {
1959         st = calloc(1, sizeof(Line_Type));
1960         gesture->data = st;
1961      }
1962
1963    Line_Data *line = NULL;
1964    Eina_List *list = st->list;
1965    unsigned cnt = eina_list_count(list);
1966
1967    if (cnt)
1968      {  /* list is not empty, locate this device on list */
1969         line = (Line_Data *) eina_list_search_unsorted(st->list,
1970               compare_line_device, &pe->device);
1971      }
1972
1973    if (!line)
1974      {  /* List is empty or device not found, new line-struct on START only */
1975         if ((event_type == EVAS_CALLBACK_MOUSE_DOWN) ||
1976               (event_type == EVAS_CALLBACK_MULTI_DOWN) ||
1977               ((wd->glayer_continues_enable) && /* START on MOVE also */
1978                ((event_type == EVAS_CALLBACK_MOUSE_MOVE) ||
1979                 (event_type == EVAS_CALLBACK_MULTI_MOVE))))
1980           {  /* Allocate new item on START only */
1981              line = calloc(1, sizeof(Line_Data));
1982              _line_data_reset(line);
1983              list = eina_list_append(list, line);
1984              st->list = list;
1985           }
1986      }
1987
1988    if (!line)  /* This may happen on MOVE that comes before DOWN      */
1989      return;   /* No line-struct to work with, can't continue testing */
1990
1991    if (_single_line_process(&st->info, line, pe, event_type)) /* update st with input */
1992      consume_event(wd, event_info, event_type, EVAS_EVENT_FLAG_NONE);
1993
1994    /* Get direction and magnitude of the line */
1995    double angle;
1996    get_vector(line->line_st.x, line->line_st.y, pe->x, pe->y,
1997          &line->line_length, &angle);
1998
1999    /* These are used later to compare lines length */
2000    Evas_Coord shortest_line_len = line->line_length;
2001    Evas_Coord longest_line_len = line->line_length;
2002    Evas_Event_Flags ev_flag = EVAS_EVENT_FLAG_NONE;
2003
2004    /* Now update line-state */
2005    if (line->t_st)
2006      {  /* Analyze line only if line started */
2007         if (line->line_angle >= 0.0)
2008           {  /* if line direction was set, we test if broke tolerance */
2009              double a = fabs(angle - line->line_angle);
2010
2011              double d = (tan(a)) * line->line_length; /* Distance from line */
2012 #if defined(DEBUG_GESTURE_LAYER)
2013              printf("%s a=<%f> d=<%f>\n", __func__, (a * 57.295779513), d);
2014 #endif
2015              if ((d > wd->line_distance_tolerance) || (a > wd->line_angular_tolerance))
2016                {  /* Broke tolerance: abort line and start a new one */
2017                   ev_flag = _set_state(gesture, ELM_GESTURE_STATE_ABORT,
2018                         &st->info, EINA_FALSE);
2019                   consume_event(wd, event_info, event_type, ev_flag);
2020                   return;
2021                }
2022
2023              if (wd->glayer_continues_enable)
2024                {  /* We may finish line if momentum is zero */
2025                   /* This is for continues-gesture */
2026                   if ((!st->info.momentum.mx) && (!st->info.momentum.my))
2027                     {  /* Finish line on zero momentum for continues gesture */
2028                        line->line_end.x = pe->x;
2029                        line->line_end.y = pe->y;
2030                        line->t_end = pe->timestamp;
2031                     }
2032                }
2033           }
2034         else
2035           {  /* Record the line angle as it broke minimum length for line */
2036              if (line->line_length >= wd->line_min_length)
2037                st->info.angle = line->line_angle = angle;
2038           }
2039
2040
2041         if (line->t_end)
2042           {
2043              if (line->line_angle < 0.0)
2044                { /* it's not a line, too short more close to a tap */
2045                   ev_flag = _set_state(gesture, ELM_GESTURE_STATE_ABORT,
2046                         &st->info, EINA_FALSE);
2047                   consume_event(wd, event_info, event_type, ev_flag);
2048                   return;
2049                }
2050           }
2051      }
2052
2053    /* Count how many lines already started / ended */
2054    int started = 0;
2055    int ended = 0;
2056    unsigned int tm_start = pe->timestamp;
2057    unsigned int tm_end = pe->timestamp;
2058    Eina_List *l;
2059    Line_Data *t_line;
2060    double base_angle = ELM_GESTURE_NEGATIVE_ANGLE;
2061    Eina_Bool lines_parallel = EINA_TRUE;
2062    EINA_LIST_FOREACH(list, l, t_line)
2063      {
2064         if (base_angle < 0)
2065           base_angle = t_line->line_angle;
2066         else
2067           {
2068              if (t_line->line_angle >= 0)
2069                {  /* Compare angle only with lines with direction defined */
2070                   if (fabs(base_angle - t_line->line_angle) >
2071                         wd->line_angular_tolerance)
2072                     lines_parallel = EINA_FALSE;
2073                }
2074           }
2075
2076         if (t_line->line_length)
2077           {  /* update only if this line is used */
2078              if (shortest_line_len > t_line->line_length)
2079                shortest_line_len = t_line->line_length;
2080
2081              if (longest_line_len < t_line->line_length)
2082                longest_line_len = t_line->line_length;
2083           }
2084
2085         if (t_line->t_st)
2086           {
2087              started++;
2088              if (t_line->t_st < tm_start)
2089                tm_start = t_line->t_st;
2090           }
2091
2092         if (t_line->t_end)
2093           {
2094              ended++;
2095              if (t_line->t_end < tm_end)
2096                tm_end = t_line->t_end;
2097           }
2098      }
2099
2100    st->info.n = started;
2101
2102
2103    if (ended &&
2104          ((event_type == EVAS_CALLBACK_MOUSE_DOWN) ||
2105           (event_type == EVAS_CALLBACK_MULTI_DOWN)))
2106      {  /* user lift one finger then starts again without line-end - ABORT */
2107         ev_flag = _set_state(gesture, ELM_GESTURE_STATE_ABORT, &st->info,
2108               EINA_FALSE);
2109         consume_event(wd, event_info, event_type, ev_flag);
2110         return;
2111      }
2112
2113    if (!lines_parallel)
2114      { /* Lines are NOT at same direction, abort this gesture */
2115         ev_flag = _set_state(gesture, ELM_GESTURE_STATE_ABORT, &st->info,
2116               EINA_FALSE);
2117         consume_event(wd, event_info, event_type, ev_flag);
2118         return;
2119      }
2120
2121
2122    /* We report ABORT if lines length are NOT matching when fingers are up */
2123    if ((longest_line_len - shortest_line_len) > (elm_finger_size_get()*2))
2124      {
2125         ev_flag = _set_state(gesture, ELM_GESTURE_STATE_ABORT, &st->info,
2126               EINA_FALSE);
2127         consume_event(wd, event_info, event_type, ev_flag);
2128         return;
2129      }
2130
2131    if ((g_type == ELM_GESTURE_N_FLICKS) && ((tm_end - tm_start) > wd->flick_time_limit_ms))
2132      {  /* We consider FLICK as a fast line.ABORT if take too long to finish */
2133         ev_flag = _set_state(gesture, ELM_GESTURE_STATE_ABORT, &st->info,
2134               EINA_FALSE);
2135         consume_event(wd, event_info, event_type, ev_flag);
2136         return;
2137      }
2138
2139    switch (event_type)
2140      {
2141       case EVAS_CALLBACK_MOUSE_UP:
2142       case EVAS_CALLBACK_MULTI_UP:
2143          if ((started) && (started == ended))
2144            {
2145               ev_flag = _set_state(gesture, ELM_GESTURE_STATE_END,
2146                     &st->info, EINA_FALSE);
2147               consume_event(wd, event_info, event_type, ev_flag);
2148            }
2149
2150          return;
2151
2152       case EVAS_CALLBACK_MOUSE_DOWN:
2153       case EVAS_CALLBACK_MULTI_DOWN:
2154       case EVAS_CALLBACK_MOUSE_MOVE:
2155       case EVAS_CALLBACK_MULTI_MOVE:
2156          if (started)
2157            {
2158               if (wd->glayer_continues_enable && (started == ended))
2159                 {  /* For continues gesture */
2160                    ev_flag = _set_state(gesture, ELM_GESTURE_STATE_END,
2161                          &st->info, EINA_FALSE);
2162                    consume_event(wd, event_info, event_type, ev_flag);
2163                 }
2164               else
2165                 {  /* When continues, may START on MOVE event too */
2166                    Elm_Gesture_State s = ELM_GESTURE_STATE_MOVE;
2167
2168                    /* This happens when: on n > 1 lines then one finger up */
2169                    /* caused abort, then put finger down.                  */
2170                    /* This will stop line from starting again.             */
2171                    /* Number of lines, MUST match touched-device in list   */
2172                    if ((!wd->glayer_continues_enable) &&
2173                          (eina_list_count(st->list) < eina_list_count(wd->touched)))
2174                      s = ELM_GESTURE_STATE_ABORT;
2175
2176                    if (gesture->state == ELM_GESTURE_STATE_UNDEFINED)
2177                      s = ELM_GESTURE_STATE_START;
2178
2179                    ev_flag = _set_state(gesture, s, &st->info, EINA_TRUE);
2180                    consume_event(wd, event_info, event_type, ev_flag);
2181                 }
2182            }
2183          break;
2184
2185       default:
2186          return;  /* Unhandeld event type */
2187      }
2188 }
2189
2190 /**
2191  * @internal
2192  *
2193  * This function is used to check if rotation gesture started.
2194  * @param st Contains current rotation values from user input.
2195  * @return TRUE/FALSE if we need to set rotation START.
2196  *
2197  * @ingroup Elm_Gesture_Layer
2198  */
2199 static Eina_Bool
2200 rotation_broke_tolerance(Rotate_Type *st)
2201 {
2202    if (st->info.base_angle < 0)
2203      return EINA_FALSE; /* Angle has to be computed first */
2204
2205    if (st->rotate_angular_tolerance < 0)
2206      return EINA_TRUE;
2207
2208    double low  = st->info.base_angle - st->rotate_angular_tolerance;
2209    double high = st->info.base_angle + st->rotate_angular_tolerance;
2210    double t = st->info.angle;
2211
2212    if (low < 0)
2213      {
2214         low += RAD_180DEG;
2215         high += RAD_180DEG;
2216
2217         if (t < RAD_180DEG)
2218           t += RAD_180DEG;
2219         else
2220           t -= RAD_180DEG;
2221      }
2222
2223    if (high > RAD_360DEG)
2224      {
2225         low -= RAD_180DEG;
2226         high -= RAD_180DEG;
2227
2228         if (t < RAD_180DEG)
2229           t += RAD_180DEG;
2230         else
2231           t -= RAD_180DEG;
2232      }
2233
2234 #if defined(DEBUG_GESTURE_LAYER)
2235    printf("%s angle=<%f> low=<%f> high=<%f>\n", __func__, t, low, high);
2236 #endif
2237    if ((t < low) || (t > high))
2238      {  /* This marks that roation action has started */
2239         st->rotate_angular_tolerance = ELM_GESTURE_NEGATIVE_ANGLE;
2240         st->info.base_angle = st->info.angle; /* Avoid jump in angle value */
2241         return EINA_TRUE;
2242      }
2243
2244    return EINA_FALSE;
2245 }
2246
2247 /**
2248  * @internal
2249  *
2250  * This function is used for computing the gap between fingers.
2251  * It returns the length and center point between fingers.
2252  *
2253  * @param x1 first finger x location.
2254  * @param y1 first finger y location.
2255  * @param x2 second finger x location.
2256  * @param y2 second finger y location.
2257  * @param x  Gets center point x cord (output)
2258  * @param y  Gets center point y cord (output)
2259  *
2260  * @return length of the line between (x1,y1), (x2,y2) in pixels.
2261  *
2262  * @ingroup Elm_Gesture_Layer
2263  */
2264 static Evas_Coord
2265 get_finger_gap_length(Evas_Coord x1, Evas_Coord y1, Evas_Coord x2,
2266       Evas_Coord y2, Evas_Coord *x, Evas_Coord *y)
2267 {
2268    double a, b, xx, yy, gap;
2269    xx = fabs(x2 - x1);
2270    yy = fabs(y2 - y1);
2271    gap = sqrt(xx*xx + yy*yy);
2272
2273    /* START - Compute zoom center point */
2274    /* The triangle defined as follows:
2275     *             B
2276     *           / |
2277     *          /  |
2278     *     gap /   | a
2279     *        /    |
2280     *       A-----C
2281     *          b
2282     * http://en.wikipedia.org/wiki/Trigonometric_functions
2283     *************************************/
2284    if (((int) xx) && ((int) yy))
2285      {
2286         double A = atan((yy / xx));
2287 #if defined(DEBUG_GESTURE_LAYER)
2288         printf("xx=<%f> yy=<%f> A=<%f>\n", xx, yy, A);
2289 #endif
2290         a = (Evas_Coord) ((gap / 2) * sin(A));
2291         b = (Evas_Coord) ((gap / 2) * cos(A));
2292         *x = (Evas_Coord) ((x2 > x1) ? (x1 + b) : (x2 + b));
2293         *y = (Evas_Coord) ((y2 > y1) ? (y1 + a) : (y2 + a));
2294      }
2295    else
2296      {
2297         if ((int) xx)
2298           {  /* horiz line, take half width */
2299 #if defined(DEBUG_GESTURE_LAYER)
2300              printf("==== HORIZ ====\n");
2301 #endif
2302              *x = (Evas_Coord) (xx / 2);
2303              *y = (Evas_Coord) (y1);
2304           }
2305
2306         if ((int) yy)
2307           {  /* vert line, take half width */
2308 #if defined(DEBUG_GESTURE_LAYER)
2309              printf("==== VERT ====\n");
2310 #endif
2311              *x = (Evas_Coord) (x1);
2312              *y = (Evas_Coord) (yy / 2);
2313           }
2314      }
2315    /* END   - Compute zoom center point */
2316
2317    return (Evas_Coord) gap;
2318 }
2319
2320 /**
2321  * @internal
2322  *
2323  * This function is used for computing zoom value.
2324  *
2325  * @param st Pointer to zoom data based on user input.
2326  * @param x1 first finger x location.
2327  * @param y1 first finger y location.
2328  * @param x2 second finger x location.
2329  * @param y2 second finger y location.
2330  * @param factor zoom-factor, used to determine how fast zoom works.
2331  *
2332  * @return zoom value, when 1.0 means no zoom, 0.5 half size...
2333  *
2334  * @ingroup Elm_Gesture_Layer
2335  */
2336 /* FIXME change float to double */
2337 static double
2338 compute_zoom(Zoom_Type *st, Evas_Coord x1, Evas_Coord y1, unsigned int tm1,
2339       Evas_Coord x2, Evas_Coord y2, unsigned int tm2, double zoom_finger_factor)
2340 {
2341    double rt = 1.0;
2342    Evas_Coord diam = get_finger_gap_length(x1, y1, x2, y2,
2343          &st->info.x, &st->info.y);
2344
2345    st->info.radius = diam / 2;
2346
2347    if (!st->zoom_base)
2348      {
2349         st->zoom_base = diam;
2350         return st->info.zoom;
2351      }
2352
2353    if (st->zoom_distance_tolerance)
2354      {  /* zoom tolerance <> ZERO, means zoom action NOT started yet */
2355         if (diam < (st->zoom_base - st->zoom_distance_tolerance))
2356           {  /* avoid jump with zoom value when break tolerance */
2357              st->zoom_base -= st->zoom_distance_tolerance;
2358              st->zoom_distance_tolerance = 0;
2359           }
2360
2361         if (diam > (st->zoom_base + st->zoom_distance_tolerance))
2362           {  /* avoid jump with zoom value when break tolerance */
2363              st->zoom_base += st->zoom_distance_tolerance;
2364              st->zoom_distance_tolerance = 0;
2365           }
2366
2367         return rt;
2368      }
2369
2370    /* We use factor only on the difference between gap-base   */
2371    /* if gap=120, base=100, we get ((120-100)/100)=0.2*factor */
2372    rt = ((1.0) + ((((float) diam - (float) st->zoom_base) /
2373                (float) st->zoom_base) * zoom_finger_factor));
2374
2375 #if 0
2376    /* Momentum: zoom per second: (NOT YET SUPPORTED) */
2377    st->info.momentum = (((rt - 1.0) * 1000) / (tm2 - tm1));
2378 #else
2379    (void) tm1;
2380    (void) tm2;
2381 #endif
2382    return rt;
2383 }
2384
2385 /**
2386  * @internal
2387  *
2388  * This function handles zoom with mouse wheel.
2389  * thats a combination of wheel + CTRL key.
2390  * @param obj The gesture-layer object.
2391  * @param event_info Original input event pointer.
2392  * @param event_type Type of original input event.
2393  * @param g_type what Gesture we are testing.
2394  *
2395  * @ingroup Elm_Gesture_Layer
2396  */
2397 static void
2398 _zoom_with_wheel_test(Evas_Object *obj, void *event_info,
2399       Evas_Callback_Type event_type, Elm_Gesture_Types g_type)
2400 {
2401    Widget_Data *wd = elm_widget_data_get(obj);
2402    if (!wd) return;
2403    if (!wd->gesture[g_type]) return;
2404
2405    Gesture_Info *gesture_zoom = wd->gesture[g_type];
2406    Zoom_Type *st = gesture_zoom->data;
2407    Evas_Event_Flags ev_flag = EVAS_EVENT_FLAG_NONE;
2408    if (!st)
2409      {  /* Allocated once on first time, used for zoom intermediate data */
2410         st = calloc(1, sizeof(Zoom_Type));
2411         gesture_zoom->data = st;
2412         _zoom_test_reset(gesture_zoom);
2413      }
2414
2415    switch (event_type)
2416      {
2417       case EVAS_CALLBACK_KEY_UP:
2418            {
2419               Evas_Event_Key_Up *p = event_info;
2420               if ((!strcmp(p->keyname, "Control_L")) ||
2421                     (!strcmp(p->keyname, "Control_R")))
2422                 {  /* Test if we ended a zoom gesture when releasing CTRL */
2423                    if ((st->zoom_wheel) &&
2424                          ((gesture_zoom->state == ELM_GESTURE_STATE_START) ||
2425                           (gesture_zoom->state == ELM_GESTURE_STATE_MOVE)))
2426                      {  /* User released CTRL after zooming */
2427                         ev_flag = _set_state(gesture_zoom,
2428                               ELM_GESTURE_STATE_END, &st->info, EINA_FALSE);
2429                         consume_event(wd, event_info, event_type, ev_flag);
2430
2431                         return;
2432                      }
2433                 }
2434               break;
2435            }
2436
2437       case EVAS_CALLBACK_MOUSE_WHEEL:
2438            {
2439               Eina_Bool force;
2440               Elm_Gesture_State s;
2441               if (!evas_key_modifier_is_set(
2442                        ((Evas_Event_Mouse_Wheel *) event_info)->modifiers,
2443                        "Control"))
2444                 {  /* if using wheel witout CTRL after starting zoom */
2445                    if ((st->zoom_wheel) &&
2446                          ((gesture_zoom->state == ELM_GESTURE_STATE_START) ||
2447                           (gesture_zoom->state == ELM_GESTURE_STATE_MOVE)))
2448                      {
2449                         ev_flag = _set_state(gesture_zoom,
2450                               ELM_GESTURE_STATE_END, &st->info, EINA_FALSE);
2451                         consume_event(wd, event_info, event_type, ev_flag);
2452
2453                         return;
2454                      }
2455                    else
2456                      return; /* Ignore mouse-wheel without control */
2457                 }
2458
2459               /* Using mouse wheel with CTRL for zoom */
2460               if (st->zoom_wheel || (st->zoom_distance_tolerance == 0))
2461                 {  /* when (zoom_wheel == NULL) and (zoom_distance_tolerance == 0)
2462                       we continue a zoom gesture */
2463                    force = EINA_TRUE;
2464                    s = ELM_GESTURE_STATE_MOVE;
2465                 }
2466               else
2467                 {  /* On first wheel event, report START */
2468                    Evas_Modifier_Mask mask = evas_key_modifier_mask_get(
2469                             evas_object_evas_get(wd->target), "Control");
2470                    force = EINA_FALSE;
2471                    s = ELM_GESTURE_STATE_START;
2472                    if (!evas_object_key_grab(wd->target, "Control_L", mask, 0, EINA_FALSE))
2473                      ERR("Failed to Grabbed CTRL_L");
2474                    if (!evas_object_key_grab(wd->target, "Control_R", mask, 0, EINA_FALSE))
2475                      ERR("Failed to Grabbed CTRL_R");
2476                 }
2477
2478               st->zoom_distance_tolerance = 0; /* Cancel tolerance */
2479               st->zoom_wheel = (Evas_Event_Mouse_Wheel *) event_info;
2480               st->info.x  = st->zoom_wheel->canvas.x;
2481               st->info.y  = st->zoom_wheel->canvas.y;
2482
2483               if (st->zoom_wheel->z < 0) /* zoom in */
2484                 st->info.zoom += (wd->zoom_finger_factor * wd->zoom_wheel_factor);
2485
2486               if (st->zoom_wheel->z > 0) /* zoom out */
2487                 st->info.zoom -= (wd->zoom_finger_factor * wd->zoom_wheel_factor);
2488
2489               if (st->info.zoom < 0.0)
2490                 st->info.zoom = 0.0;
2491
2492               ev_flag = _set_state(gesture_zoom, s, &st->info, force);
2493               consume_event(wd, event_info, event_type, ev_flag);
2494               break;
2495            }
2496
2497       default:
2498            return;
2499      }
2500 }
2501
2502 /**
2503  * @internal
2504  *
2505  * This function is used to test zoom gesture.
2506  * user may combine zoom, rotation together.
2507  * so its possible that both will be detected from input.
2508  * (both are two-finger movement-oriented gestures)
2509  *
2510  * @param obj The gesture-layer object.
2511  * @param event_info Pointer to recent input event.
2512  * @param event_type Recent input event type.
2513  * @param g_type what Gesture we are testing.
2514  *
2515  * @ingroup Elm_Gesture_Layer
2516  */
2517 static void
2518 _zoom_test(Evas_Object *obj, Pointer_Event *pe, void *event_info,
2519       Evas_Callback_Type event_type, Elm_Gesture_Types g_type)
2520 {
2521    if (!pe)
2522      return;
2523    Widget_Data *wd = elm_widget_data_get(obj);
2524    if (!wd) return;
2525    if (!wd->gesture[g_type]) return;
2526
2527    Gesture_Info *gesture_zoom = wd->gesture[g_type];
2528    Zoom_Type *st = gesture_zoom->data;
2529
2530    if (!st)
2531      {  /* Allocated once on first time, used for zoom data */
2532         st = calloc(1, sizeof(Zoom_Type));
2533         gesture_zoom->data = st;
2534         _zoom_test_reset(gesture_zoom);
2535      }
2536
2537
2538    /* Start - new zoom testing, letting all fingers start */
2539    Evas_Event_Flags ev_flag = EVAS_EVENT_FLAG_NONE;
2540    switch (event_type)
2541      {
2542       case EVAS_CALLBACK_MOUSE_MOVE:
2543       case EVAS_CALLBACK_MULTI_MOVE:
2544          /* if non-continues mode and gesture NOT started, ignore MOVE */
2545          if ((!wd->glayer_continues_enable) &&
2546                (!st->zoom_st.timestamp))
2547            return;
2548
2549       case EVAS_CALLBACK_MOUSE_DOWN:
2550       case EVAS_CALLBACK_MULTI_DOWN:
2551            {  /* Here we take care of zoom-start and zoom move */
2552               Eina_List *l;
2553               Pointer_Event *p;
2554
2555               if(eina_list_count(wd->touched) > 2)
2556                 {  /* Process zoom only when 2 fingers on surface */
2557                    ev_flag = _set_state(gesture_zoom,
2558                          ELM_GESTURE_STATE_ABORT, &st->info, EINA_FALSE);
2559                    consume_event(wd, event_info, event_type, ev_flag);
2560
2561                    return;
2562                 }
2563
2564               if (!st->zoom_st.timestamp)
2565                 {  /* Now scan touched-devices list and find other finger */
2566                    EINA_LIST_FOREACH(wd->touched, l, p)
2567                      {  /* Device of other finger <> pe device */
2568                         if (p->device != pe->device)
2569                           break;
2570                      }
2571
2572                    if (!p)  /* Single finger on touch */
2573                         return;
2574
2575                    /* Record down fingers */
2576                    consume_event(wd, event_info, event_type, ev_flag);
2577                    memcpy(&st->zoom_st, pe, sizeof(Pointer_Event));
2578                    memcpy(&st->zoom_st1, p, sizeof(Pointer_Event));
2579
2580                    /* Set mv field as well to be ready for MOVE events  */
2581                    memcpy(&st->zoom_mv, pe, sizeof(Pointer_Event));
2582                    memcpy(&st->zoom_mv1, p, sizeof(Pointer_Event));
2583
2584                    /* Here we have zoom_st, zoom_st1 set, report START  */
2585                    /* Set zoom-base after BOTH down events  recorded    */
2586                    /* Compute length of line between fingers zoom start */
2587                    st->info.zoom = 1.0;
2588                    st->zoom_base = get_finger_gap_length(st->zoom_st1.x,
2589                          st->zoom_st1.y, st->zoom_st.x,  st->zoom_st.y,
2590                          &st->info.x, &st->info.y);
2591
2592                    st->info.radius = st->zoom_base / 2;
2593
2594                    if ((gesture_zoom->state != ELM_GESTURE_STATE_START) &&
2595                          (gesture_zoom->state != ELM_GESTURE_STATE_MOVE))
2596                      {  /* zoom started with mouse-wheel, don't report twice */
2597                         ev_flag = _set_state(gesture_zoom,
2598                               ELM_GESTURE_STATE_START, &st->info, EINA_FALSE);
2599                         consume_event(wd, event_info, event_type, ev_flag);
2600                      }
2601
2602                    return;  /* Zoom started */
2603                 }  /* End of ZOOM_START handling */
2604
2605
2606               /* if we got here, we have (exacally) two fingers on surfce */
2607               /* we also after START, report MOVE */
2608               /* First detect which finger moved  */
2609               if (pe->device == st->zoom_mv.device)
2610                 memcpy(&st->zoom_mv, pe, sizeof(Pointer_Event));
2611               else if (pe->device == st->zoom_mv1.device)
2612                 memcpy(&st->zoom_mv1, pe, sizeof(Pointer_Event));
2613
2614               /* Compute change in zoom as fingers move */
2615                 st->info.zoom = compute_zoom(st,
2616                       st->zoom_mv.x, st->zoom_mv.y, st->zoom_mv.timestamp,
2617                       st->zoom_mv1.x, st->zoom_mv1.y, st->zoom_mv1.timestamp,
2618                       wd->zoom_finger_factor);
2619
2620               if (!st->zoom_distance_tolerance)
2621                 {  /* Zoom broke tolerance, report move */
2622                    double d = st->info.zoom - st->next_step;
2623                    if (d < 0.0)
2624                      d = (-d);
2625
2626                    if (d >= wd->zoom_step)
2627                      {  /* Report move in steps */
2628                         st->next_step = st->info.zoom;
2629
2630                         ev_flag = _set_state(gesture_zoom,
2631                               ELM_GESTURE_STATE_MOVE,
2632                               &st->info, EINA_TRUE);
2633                         consume_event(wd, event_info, event_type, ev_flag);
2634                      }
2635                 }  /* End of ZOOM_MOVE handling */
2636
2637               return;
2638            }
2639
2640       case EVAS_CALLBACK_MOUSE_UP:
2641       case EVAS_CALLBACK_MULTI_UP:
2642          /* Reset timestamp of finger-up.This is used later
2643             by _zoom_test_reset() to retain finger-down data */
2644          consume_event(wd, event_info, event_type, ev_flag);
2645          if (((st->zoom_wheel) || (st->zoom_base)) &&
2646                (st->zoom_distance_tolerance == 0))
2647            {
2648               ev_flag = _set_state(gesture_zoom, ELM_GESTURE_STATE_END,
2649                     &st->info, EINA_FALSE);
2650               consume_event(wd, event_info, event_type, ev_flag);
2651
2652               return;
2653            }
2654
2655          /* if we got here not a ZOOM */
2656          if (gesture_zoom->state != ELM_GESTURE_STATE_UNDEFINED)
2657            {  /* Must be != undefined, if gesture started */
2658               ev_flag = _set_state(gesture_zoom,
2659                     ELM_GESTURE_STATE_ABORT, &st->info, EINA_FALSE);
2660               consume_event(wd, event_info, event_type, ev_flag);
2661            }
2662
2663          _zoom_test_reset(gesture_zoom);
2664
2665          return;
2666
2667       default:
2668          return;
2669        }
2670 }
2671
2672 static void
2673 _get_rotate_properties(Rotate_Type *st,
2674       Evas_Coord x1, Evas_Coord y1, unsigned int tm1,
2675       Evas_Coord x2, Evas_Coord y2, unsigned int tm2,
2676       double *angle)
2677 {
2678    st->info.radius = get_finger_gap_length(x1, y1, x2, y2,
2679          &st->info.x, &st->info.y) / 2;
2680
2681    *angle = get_angle(x1, y1, x2, y2);
2682 #if 0 /* (NOT YET SUPPORTED) */
2683    if (angle == &st->info.angle)
2684      {  /* Compute momentum: TODO: bug when breaking 0, 360 values */
2685         st->info.momentum = (((*angle) - st->info.base_angle) /
2686            (fabs(tm2 - tm1))) * 1000;
2687      }
2688    else
2689      st->info.momentum = 0;
2690 #else
2691    (void) tm1;
2692    (void) tm2;
2693 #endif
2694 }
2695
2696 /**
2697  * @internal
2698  *
2699  * This function is used to test rotation gesture.
2700  * user may combine zoom, rotation together.
2701  * so its possible that both will be detected from input.
2702  * (both are two-finger movement-oriented gestures)
2703  *
2704  * @param obj The gesture-layer object.
2705  * @param event_info Pointer to recent input event.
2706  * @param event_type Recent input event type.
2707  * @param g_type what Gesture we are testing.
2708  *
2709  * @ingroup Elm_Gesture_Layer
2710  */
2711 static void
2712 _rotate_test(Evas_Object *obj, Pointer_Event *pe, void *event_info,
2713       Evas_Callback_Type event_type, Elm_Gesture_Types g_type)
2714 {
2715    if (!pe)
2716      return;
2717
2718    Widget_Data *wd = elm_widget_data_get(obj);
2719    if (!wd) return;
2720    if (!wd->gesture[g_type]) return;
2721
2722    Gesture_Info *gesture = wd->gesture[g_type];
2723    Rotate_Type *st = gesture->data;
2724    if (gesture)
2725    {
2726       st = gesture->data;
2727       if (!st)
2728         {  /* Allocated once on first time */
2729            st = calloc(1, sizeof(Rotate_Type));
2730            gesture->data = st;
2731            _rotate_test_reset(gesture);
2732         }
2733    }
2734
2735    Evas_Event_Flags ev_flag = EVAS_EVENT_FLAG_NONE;
2736    switch (event_type)
2737      {
2738       case EVAS_CALLBACK_MOUSE_MOVE:
2739       case EVAS_CALLBACK_MULTI_MOVE:
2740          /* if non-continues mode and gesture NOT started, ignore MOVE */
2741          if ((!wd->glayer_continues_enable) &&
2742                (!st->rotate_st.timestamp))
2743            return;
2744
2745       case EVAS_CALLBACK_MOUSE_DOWN:
2746       case EVAS_CALLBACK_MULTI_DOWN:
2747            {  /* Here we take care of rotate-start and rotate move */
2748               Eina_List *l;
2749               Pointer_Event *p;
2750
2751               if(eina_list_count(wd->touched) > 2)
2752                 {  /* Process rotate only when 2 fingers on surface */
2753                    ev_flag = _set_state(gesture,
2754                          ELM_GESTURE_STATE_ABORT, &st->info, EINA_FALSE);
2755                    consume_event(wd, event_info, event_type, ev_flag);
2756
2757                    return;
2758                 }
2759
2760               if (!st->rotate_st.timestamp)
2761                 {  /* Now scan touched-devices list and find other finger */
2762                    EINA_LIST_FOREACH(wd->touched, l, p)
2763                      {  /* Device of other finger <> pe device */
2764                         if (p->device != pe->device)
2765                           break;
2766                      }
2767
2768                    if (!p)
2769                         return;  /* Single finger on touch */
2770
2771                    /* Record down fingers */
2772                    consume_event(wd, event_info, event_type, ev_flag);
2773                    memcpy(&st->rotate_st, pe, sizeof(Pointer_Event));
2774                    memcpy(&st->rotate_st1, p, sizeof(Pointer_Event));
2775
2776                    /* Set mv field as well to be ready for MOVE events  */
2777                    memcpy(&st->rotate_mv, pe, sizeof(Pointer_Event));
2778                    memcpy(&st->rotate_mv1, p, sizeof(Pointer_Event));
2779
2780                    /* Here we have rotate_st, rotate_st1 set, report START  */
2781                    /* Set rotate-base after BOTH down events  recorded    */
2782                    /* Compute length of line between fingers rotate start */
2783                    _get_rotate_properties(st,
2784                          st->rotate_st.x, st->rotate_st.y,
2785                          st->rotate_st.timestamp,
2786                          st->rotate_st1.x, st->rotate_st1.y,
2787                          st->rotate_st1.timestamp, &st->info.base_angle);
2788
2789                    ev_flag = _set_state(gesture, ELM_GESTURE_STATE_START,
2790                          &st->info, EINA_FALSE);
2791                    consume_event(wd, event_info, event_type, ev_flag);
2792
2793                    return;  /* Rotate started */
2794                 }  /* End of ROTATE_START handling */
2795
2796
2797               /* if we got here, we have (exacally) two fingers on surfce */
2798               /* we also after START, report MOVE */
2799               /* First detect which finger moved  */
2800               if (pe->device == st->rotate_mv.device)
2801                 memcpy(&st->rotate_mv, pe, sizeof(Pointer_Event));
2802               else if (pe->device == st->rotate_mv1.device)
2803                 memcpy(&st->rotate_mv1, pe, sizeof(Pointer_Event));
2804
2805               /* Compute change in rotate as fingers move */
2806               _get_rotate_properties(st,
2807                     st->rotate_mv.x, st->rotate_mv.y,
2808                     st->rotate_mv.timestamp,
2809                     st->rotate_mv1.x, st->rotate_mv1.y,
2810                     st->rotate_mv1.timestamp, &st->info.angle);
2811
2812               if (rotation_broke_tolerance(st))
2813                 {  /* Rotation broke tolerance, report move */
2814                    double d = st->info.angle - st->next_step;
2815                    if (d < 0.0)
2816                      d = (-d);
2817
2818                    if (d >= wd->rotate_step)
2819                      {  /* Report move in steps */
2820                         st->next_step = st->info.angle;
2821
2822                         ev_flag = _set_state(gesture,
2823                               ELM_GESTURE_STATE_MOVE, &st->info, EINA_TRUE);
2824                         consume_event(wd, event_info, event_type, ev_flag);
2825                      }
2826                 }  /* End of ROTATE_MOVE handling */
2827
2828               return;
2829            }
2830
2831       case EVAS_CALLBACK_MOUSE_UP:
2832       case EVAS_CALLBACK_MULTI_UP:
2833            consume_event(wd, event_info, event_type, ev_flag);
2834            /* Reset timestamp of finger-up.This is used later
2835               by rotate_test_reset() to retain finger-down data */
2836            if (st->rotate_angular_tolerance < 0)
2837              {
2838                 ev_flag = _set_state(gesture, ELM_GESTURE_STATE_END,
2839                       &st->info, EINA_FALSE);
2840                 consume_event(wd, event_info, event_type, ev_flag);
2841
2842                 return;
2843              }
2844
2845            if (gesture->state != ELM_GESTURE_STATE_UNDEFINED)
2846              {  /* Must be != undefined, if gesture started */
2847                 ev_flag = _set_state(gesture, ELM_GESTURE_STATE_ABORT,
2848                       &st->info, EINA_FALSE);
2849                 consume_event(wd, event_info, event_type, ev_flag);
2850              }
2851
2852            _rotate_test_reset(gesture);
2853            return;
2854
2855       default:
2856          return;
2857        }
2858 }
2859
2860 /**
2861  * @internal
2862  *
2863  * This function is used to save input events in an abstract struct
2864  * to be used later by getsure-testing functions.
2865  *
2866  * @param data The gesture-layer object.
2867  * @param event_info Pointer to recent input event.
2868  * @param event_type Recent input event type.
2869  * @param pe The abstract data-struct (output).
2870  *
2871  * @ingroup Elm_Gesture_Layer
2872  */
2873 static Eina_Bool
2874 _make_pointer_event(void *data, void *event_info,
2875       Evas_Callback_Type event_type, Pointer_Event *pe)
2876 {
2877    Widget_Data *wd = elm_widget_data_get(data);
2878    if (!wd) return EINA_FALSE;
2879
2880    switch (event_type)
2881      {
2882       case EVAS_CALLBACK_MOUSE_DOWN:
2883            pe->x = ((Evas_Event_Mouse_Down *) event_info)->canvas.x;
2884            pe->y = ((Evas_Event_Mouse_Down *) event_info)->canvas.y;
2885            pe->timestamp = ((Evas_Event_Mouse_Down *) event_info)->timestamp;
2886            pe->device = ELM_MOUSE_DEVICE;
2887            break;
2888
2889       case EVAS_CALLBACK_MOUSE_UP:
2890            pe->x = ((Evas_Event_Mouse_Up *) event_info)->canvas.x;
2891            pe->y = ((Evas_Event_Mouse_Up *) event_info)->canvas.y;
2892            pe->timestamp = ((Evas_Event_Mouse_Up *) event_info)->timestamp;
2893            pe->device = ELM_MOUSE_DEVICE;
2894            break;
2895
2896       case EVAS_CALLBACK_MOUSE_MOVE:
2897            pe->x = ((Evas_Event_Mouse_Move *) event_info)->cur.canvas.x;
2898            pe->y = ((Evas_Event_Mouse_Move *) event_info)->cur.canvas.y;
2899            pe->timestamp = ((Evas_Event_Mouse_Move *) event_info)->timestamp;
2900            pe->device = ELM_MOUSE_DEVICE;
2901            break;
2902
2903       case EVAS_CALLBACK_MULTI_DOWN:
2904            pe->x = ((Evas_Event_Multi_Down *) event_info)->canvas.x;
2905            pe->y = ((Evas_Event_Multi_Down *) event_info)->canvas.y;
2906            pe->timestamp = ((Evas_Event_Multi_Down *) event_info)->timestamp;
2907            pe->device = ((Evas_Event_Multi_Down *) event_info)->device;
2908            break;
2909
2910       case EVAS_CALLBACK_MULTI_UP:
2911            pe->x = ((Evas_Event_Multi_Up *) event_info)->canvas.x;
2912            pe->y = ((Evas_Event_Multi_Up *) event_info)->canvas.y;
2913            pe->timestamp = ((Evas_Event_Multi_Up *) event_info)->timestamp;
2914            pe->device = ((Evas_Event_Multi_Up *) event_info)->device;
2915            break;
2916
2917       case EVAS_CALLBACK_MULTI_MOVE:
2918            pe->x = ((Evas_Event_Multi_Move *) event_info)->cur.canvas.x;
2919            pe->y = ((Evas_Event_Multi_Move *) event_info)->cur.canvas.y;
2920            pe->timestamp = ((Evas_Event_Multi_Move *) event_info)->timestamp;
2921            pe->device = ((Evas_Event_Multi_Move *) event_info)->device;
2922            break;
2923
2924       default:
2925            return EINA_FALSE;
2926      }
2927
2928    pe->event_type = event_type;
2929    return EINA_TRUE;
2930 }
2931
2932 /**
2933  * @internal
2934  *
2935  * This function restartes line, flick, zoom and rotate gestures
2936  * when gesture-layer continues-gestures enabled.
2937  * Example of continues-gesture:
2938  * When doing a line, user stops moving finger but keeps fingers on touch.
2939  * This will cause line-end, then as user continues moving his finger
2940  * it re-starts line gesture.
2941  * When continue mode is disabled, user has to lift finger from touch
2942  * to end a gesture. Them touch-again to start a new one.
2943  *
2944  * @param data The gesture-layer object.
2945  * @param wd gesture layer widget data.
2946  * @param states_reset flag that marks gestures were reset in history clear.
2947  *
2948  * @ingroup Elm_Gesture_Layer
2949  */
2950 void continues_gestures_restart(void *data, Eina_Bool states_reset)
2951 {
2952    Widget_Data *wd = elm_widget_data_get(data);
2953    if (!wd) return;
2954
2955    /* Run through events to restart gestures */
2956    Gesture_Info *g;
2957    Eina_Bool n_lines, n_flicks, zoom, rotate;
2958    /* We turn-on flag for finished, aborted, not-started gestures */
2959    g = wd->gesture[ELM_GESTURE_N_LINES];
2960    n_lines = (g) ? ((states_reset) | ((g->state != ELM_GESTURE_STATE_START)
2961          && (g->state != ELM_GESTURE_STATE_MOVE))) : EINA_FALSE;
2962    if (n_lines)
2963      {
2964         _line_test_reset(wd->gesture[ELM_GESTURE_N_LINES]);
2965         _set_state(g, ELM_GESTURE_STATE_UNDEFINED, NULL, EINA_FALSE);
2966         SET_TEST_BIT(g);
2967      }
2968
2969    g = wd->gesture[ELM_GESTURE_N_FLICKS];
2970    n_flicks = (g) ? ((states_reset) | ((g->state != ELM_GESTURE_STATE_START)
2971          && (g->state != ELM_GESTURE_STATE_MOVE))) : EINA_FALSE;
2972    if (n_flicks)
2973      {
2974         _line_test_reset(wd->gesture[ELM_GESTURE_N_FLICKS]);
2975         _set_state(g, ELM_GESTURE_STATE_UNDEFINED, NULL, EINA_FALSE);
2976         SET_TEST_BIT(g);
2977      }
2978
2979    g = wd->gesture[ELM_GESTURE_ZOOM];
2980    zoom = (g) ? ((states_reset) | ((g->state != ELM_GESTURE_STATE_START)
2981          && (g->state != ELM_GESTURE_STATE_MOVE))) : EINA_FALSE;
2982    if (zoom)
2983      {
2984         _zoom_test_reset(wd->gesture[ELM_GESTURE_ZOOM]);
2985         _set_state(g, ELM_GESTURE_STATE_UNDEFINED, NULL, EINA_FALSE);
2986         SET_TEST_BIT(g);
2987      }
2988
2989    g = wd->gesture[ELM_GESTURE_ROTATE];
2990    rotate = (g) ? ((states_reset) | ((g->state != ELM_GESTURE_STATE_START)
2991          && (g->state != ELM_GESTURE_STATE_MOVE))) : EINA_FALSE;
2992    if (rotate)
2993      {
2994         _rotate_test_reset(wd->gesture[ELM_GESTURE_ROTATE]);
2995         _set_state(g, ELM_GESTURE_STATE_UNDEFINED, NULL, EINA_FALSE);
2996         SET_TEST_BIT(g);
2997      }
2998 }
2999
3000 /**
3001  * @internal
3002  *
3003  * This function the core-function where input handling is done.
3004  * Here we get user input and stream it to gesture testing.
3005  * We notify user about any gestures with new state:
3006  * Valid states are:
3007  * START - gesture started.
3008  * MOVE - gesture is ongoing.
3009  * END - gesture was completed.
3010  * ABORT - gesture was aborted after START, MOVE (will NOT be completed)
3011  *
3012  * We also check if a gesture was detected, then reset event history
3013  * If no gestures were found we reset gesture test flag
3014  * after streaming event-history to widget.
3015  * (stream to the widget all events not consumed as a gesture)
3016  *
3017  * @param data The gesture-layer object.
3018  * @param event_info Pointer to recent input event.
3019  * @param event_type Recent input event type.
3020  *
3021  * @ingroup Elm_Gesture_Layer
3022  */
3023 static void
3024 _event_process(void *data, Evas_Object *obj __UNUSED__,
3025       void *event_info, Evas_Callback_Type event_type)
3026 {
3027    Pointer_Event _pe;
3028    Pointer_Event *pe = NULL;
3029    Widget_Data *wd = elm_widget_data_get(data);
3030    if (!wd) return;
3031
3032 #if defined(DEBUG_GESTURE_LAYER)
3033    int i;
3034    Gesture_Info *g;
3035    printf("Gesture | State | is tested\n");
3036    for(i = ELM_GESTURE_N_TAPS; i < ELM_GESTURE_LAST; i++)
3037      {
3038         g = wd->gesture[i];
3039         if(g)
3040           printf("   %d       %d       %d\n", i, g->state, g->test);
3041      }
3042 #endif
3043
3044    /* Start testing candidate gesture from here */
3045    if (_make_pointer_event(data, event_info, event_type, &_pe))
3046      pe = &_pe;
3047
3048    if (IS_TESTED(ELM_GESTURE_N_LONG_TAPS))
3049      _n_long_tap_test(data, pe, event_info, event_type,
3050            ELM_GESTURE_N_LONG_TAPS);
3051
3052    /* This takes care of single, double and tripple tap */
3053    _tap_gestures_test(data, pe, event_info, event_type);
3054
3055    if (IS_TESTED(ELM_GESTURE_MOMENTUM))
3056      _momentum_test(data, pe, event_info, event_type,
3057            ELM_GESTURE_MOMENTUM);
3058
3059    if (IS_TESTED(ELM_GESTURE_N_LINES))
3060      _n_line_test(data, pe, event_info, event_type, ELM_GESTURE_N_LINES);
3061
3062    if (IS_TESTED(ELM_GESTURE_N_FLICKS))
3063      _n_line_test(data, pe, event_info, event_type, ELM_GESTURE_N_FLICKS);
3064
3065    if (IS_TESTED(ELM_GESTURE_ZOOM))
3066      _zoom_test(data, pe, event_info, event_type, ELM_GESTURE_ZOOM);
3067
3068    if (IS_TESTED(ELM_GESTURE_ZOOM))
3069      _zoom_with_wheel_test(data, event_info, event_type, ELM_GESTURE_ZOOM);
3070
3071    if (IS_TESTED(ELM_GESTURE_ROTATE))
3072      _rotate_test(data, pe, event_info, event_type, ELM_GESTURE_ROTATE);
3073
3074    if (_get_event_flag(event_info, event_type) & EVAS_EVENT_FLAG_ON_HOLD)
3075      _event_history_add(data, event_info, event_type);
3076    else if ((event_type == EVAS_CALLBACK_MOUSE_UP) ||
3077          (event_type == EVAS_CALLBACK_MULTI_UP))
3078      {
3079         Eina_List *pending = _device_is_pending(wd->pending, event_info, event_type);
3080         if (pending)
3081           {
3082              consume_event(wd, event_info, event_type, EVAS_EVENT_FLAG_ON_HOLD);
3083              _event_history_add(data, event_info, event_type);
3084           }
3085      }
3086
3087    /* Log event to restart gestures */
3088    wd->recent_device_event = _add_recent_device_event(wd->recent_device_event, &_pe);
3089
3090    /* we maintain list of touched devices              */
3091    /* We also use move to track current device x.y pos */
3092    if ((event_type == EVAS_CALLBACK_MOUSE_DOWN) ||
3093          (event_type == EVAS_CALLBACK_MULTI_DOWN) ||
3094          (event_type == EVAS_CALLBACK_MOUSE_MOVE) ||
3095          (event_type == EVAS_CALLBACK_MULTI_MOVE))
3096      {
3097         wd->touched = _add_touched_device(wd->touched, pe);
3098      }
3099    else if ((event_type == EVAS_CALLBACK_MOUSE_UP) ||
3100          (event_type == EVAS_CALLBACK_MULTI_UP))
3101      {
3102         wd->touched = _remove_touched_device(wd->touched, pe);
3103      }
3104
3105    /* Report current states and clear history if needed */
3106    Eina_Bool states_reset = _clear_if_finished(data);
3107    if (wd->glayer_continues_enable)
3108      continues_gestures_restart(data, states_reset);
3109 }
3110
3111
3112 /**
3113  * For all _mouse_* / multi_* functions wethen send this event to
3114  * _event_process function.
3115  *
3116  * @param data The gesture-layer object.
3117  * @param event_info Pointer to recent input event.
3118  *
3119  * @ingroup Elm_Gesture_Layer
3120  */
3121 static void
3122 _mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
3123       void *event_info)
3124 {
3125    Widget_Data *wd = elm_widget_data_get(data);
3126    if (!wd) return;
3127    if (((Evas_Event_Mouse_Down *) event_info)->button != 1)
3128      return; /* We only process left-click at the moment */
3129
3130    _event_process(data, obj, event_info, EVAS_CALLBACK_MOUSE_DOWN);
3131 }
3132
3133 static void
3134 _mouse_move(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
3135       void *event_info)
3136 {
3137    Widget_Data *wd = elm_widget_data_get(data);
3138    if (!wd) return;
3139
3140    _event_process(data, obj, event_info, EVAS_CALLBACK_MOUSE_MOVE);
3141 }
3142
3143 static void
3144 _key_down_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
3145       void *event_info)
3146 {
3147    Widget_Data *wd = elm_widget_data_get(data);
3148    if (!wd) return;
3149
3150    _event_process(data, obj, event_info, EVAS_CALLBACK_KEY_DOWN);
3151 }
3152
3153 static void
3154 _key_up_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
3155       void *event_info)
3156 {
3157    Widget_Data *wd = elm_widget_data_get(data);
3158    if (!wd) return;
3159
3160    _event_process(data, obj, event_info, EVAS_CALLBACK_KEY_UP);
3161 }
3162
3163 static void
3164 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
3165       void *event_info)
3166 {
3167    Widget_Data *wd = elm_widget_data_get(data);
3168    if (!wd) return;
3169
3170    if (((Evas_Event_Mouse_Up *) event_info)->button != 1)
3171      return; /* We only process left-click at the moment */
3172
3173    _event_process(data, obj, event_info, EVAS_CALLBACK_MOUSE_UP);
3174 }
3175
3176 static void
3177 _mouse_wheel(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
3178       void *event_info)
3179 {
3180    Widget_Data *wd = elm_widget_data_get(data);
3181    if (!wd) return;
3182
3183    _event_process(data, obj, event_info, EVAS_CALLBACK_MOUSE_WHEEL);
3184 }
3185
3186 static void
3187 _multi_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
3188       void *event_info)
3189 {
3190    Widget_Data *wd = elm_widget_data_get(data);
3191    if (!wd) return;
3192
3193    _event_process(data, obj, event_info, EVAS_CALLBACK_MULTI_DOWN);
3194 }
3195
3196 static void
3197 _multi_move(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
3198       void *event_info)
3199 {
3200    Widget_Data *wd = elm_widget_data_get(data);
3201    if (!wd) return;
3202
3203    _event_process(data, obj, event_info, EVAS_CALLBACK_MULTI_MOVE);
3204 }
3205
3206 static void
3207 _multi_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__,
3208       void *event_info)
3209 {
3210    Widget_Data *wd = elm_widget_data_get(data);
3211    if (!wd) return;
3212
3213    _event_process(data, obj, event_info, EVAS_CALLBACK_MULTI_UP);
3214 }
3215
3216 EAPI Eina_Bool
3217 elm_gesture_layer_hold_events_get(Evas_Object *obj)
3218 {
3219    Widget_Data *wd = elm_widget_data_get(obj);
3220    if (!wd) return EINA_FALSE;
3221
3222    return !wd->repeat_events;
3223 }
3224
3225 EAPI void
3226 elm_gesture_layer_hold_events_set(Evas_Object *obj, Eina_Bool r)
3227 {
3228    Widget_Data *wd = elm_widget_data_get(obj);
3229    if (!wd) return;
3230
3231    wd->repeat_events = !r;
3232 }
3233
3234 EAPI void
3235 elm_gesture_layer_zoom_step_set(Evas_Object *obj, double s)
3236 {
3237    Widget_Data *wd = elm_widget_data_get(obj);
3238    if (!wd) return;
3239
3240    if (s < 0.0)
3241      return;
3242
3243    wd->zoom_step = s;
3244 }
3245
3246 EAPI void
3247 elm_gesture_layer_rotate_step_set(Evas_Object *obj, double s)
3248 {
3249    Widget_Data *wd = elm_widget_data_get(obj);
3250    if (!wd) return;
3251
3252    if (s < 0.0)
3253      return;
3254
3255    wd->rotate_step = s;
3256 }
3257
3258 EAPI Eina_Bool
3259 elm_gesture_layer_attach(Evas_Object *obj, Evas_Object *t)
3260 {
3261    Widget_Data *wd = elm_widget_data_get(obj);
3262    if (!wd) return EINA_FALSE;
3263
3264    if (!t)
3265      return EINA_FALSE;
3266
3267    /* if was attached before, unregister callbacks first */
3268    if (wd->target)
3269      _unregister_callbacks(obj);
3270
3271    wd->target = t;
3272
3273    _register_callbacks(obj);
3274    return EINA_TRUE;
3275 }
3276
3277 EAPI void
3278 elm_gesture_layer_cb_set(Evas_Object *obj, Elm_Gesture_Types idx,
3279       Elm_Gesture_State cb_type, Elm_Gesture_Event_Cb cb, void *data)
3280 {
3281    Widget_Data *wd = elm_widget_data_get(obj);
3282    Gesture_Info *p;
3283    if (!wd) return;
3284
3285    if (!wd->gesture[idx])
3286      wd->gesture[idx] = calloc(1, sizeof(Gesture_Info));
3287    if (!wd->gesture[idx]) return;
3288
3289    p = wd->gesture[idx];
3290    p->obj = obj;
3291    p->g_type = idx;
3292    p->fn[cb_type].cb = cb;
3293    p->fn[cb_type].user_data = data;
3294    p->state = ELM_GESTURE_STATE_UNDEFINED;
3295    SET_TEST_BIT(p);
3296 }
3297
3298 static void
3299 _disable_hook(Evas_Object *obj)
3300 {
3301    if (elm_widget_disabled_get(obj))
3302      _unregister_callbacks(obj);
3303    else
3304      _register_callbacks(obj);
3305 }
3306
3307 EAPI Evas_Object *
3308 elm_gesture_layer_add(Evas_Object *parent)
3309 {
3310    Evas_Object *obj;
3311    Evas *e;
3312    Widget_Data *wd;
3313
3314    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
3315
3316    wd = ELM_NEW(Widget_Data);
3317    e = evas_object_evas_get(parent);
3318    if (!e) return NULL;
3319    obj = elm_widget_add(e);
3320    ELM_SET_WIDTYPE(widtype, "gesture_layer");
3321    elm_widget_type_set(obj, "gesture_layer");
3322    elm_widget_sub_object_add(parent, obj);
3323    elm_widget_data_set(obj, wd);
3324    elm_widget_del_hook_set(obj, _del_hook);
3325    elm_widget_disable_hook_set(obj, _disable_hook);
3326
3327    wd->target = NULL;
3328    wd->line_min_length =_elm_config->glayer_line_min_length * elm_finger_size_get();
3329    wd->zoom_distance_tolerance = _elm_config->glayer_zoom_distance_tolerance * elm_finger_size_get();
3330    wd->line_distance_tolerance = _elm_config->glayer_line_distance_tolerance * elm_finger_size_get();
3331    wd->zoom_finger_factor = _elm_config->glayer_zoom_finger_factor;
3332    wd->zoom_wheel_factor = _elm_config->glayer_zoom_wheel_factor; /* mouse wheel zoom steps */
3333    wd->rotate_angular_tolerance = _elm_config->glayer_rotate_angular_tolerance;
3334    wd->line_angular_tolerance = _elm_config->glayer_line_angular_tolerance;
3335    wd->flick_time_limit_ms = _elm_config->glayer_flick_time_limit_ms;
3336    wd->long_tap_start_timeout = _elm_config->glayer_long_tap_start_timeout;
3337    wd->repeat_events = EINA_TRUE;
3338    wd->glayer_continues_enable = EINA_FALSE;//_elm_config->glayer_continues_enable;
3339
3340 #if defined(DEBUG_GESTURE_LAYER)
3341    printf("size of Gestures = <%d>\n", sizeof(wd->gesture));
3342    printf("initial values:\n\tzoom_finger_factor=<%f>\n\tzoom_distance_tolerance=<%d>\n\tline_min_length=<%d>\n\tline_distance_tolerance=<%d>\n\tzoom_wheel_factor=<%f>\n\trotate_angular_tolerance=<%f>\n\twd->line_angular_tolerance=<%f>\n\twd->flick_time_limit_ms=<%d>\n\twd->long_tap_start_timeout=<%f>\n\twd->zoom_step=<%f>\n\twd->rotate_step=<%f>\n\twd->glayer_continues_enable=<%d>\n ", wd->zoom_finger_factor, wd->zoom_distance_tolerance, wd->line_min_length, wd->line_distance_tolerance, wd->zoom_wheel_factor, wd->rotate_angular_tolerance, wd->line_angular_tolerance, wd->flick_time_limit_ms, wd->long_tap_start_timeout, wd->zoom_step, wd->rotate_step, wd->glayer_continues_enable);
3343 #endif
3344    memset(wd->gesture, 0, sizeof(wd->gesture));
3345
3346    return obj;
3347 }