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