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