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