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