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