[els_touch]emit tap after 10ms later
[framework/uifw/elementary.git] / src / lib / els_touch.c
1 /*
2  *
3  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
4  */
5 #include <Elementary.h>
6 #include <math.h>
7 #include "elm_priv.h"
8
9 #define SMART_NAME "elm_touch"
10 #define API_ENTRY Smart_Data *sd; sd = evas_object_smart_data_get(obj); if ((!obj) || (!sd) || (evas_object_type_get(obj) && strcmp(evas_object_type_get(obj), SMART_NAME)))
11 #define INTERNAL_ENTRY Smart_Data *sd; sd = evas_object_smart_data_get(obj); if (!sd) return;
12
13 #define PRESS_TIME          250   // ms
14 #define RELEASE_TIME         50   // ms
15 #define LONG_HOLD_TIME      500   // ms
16
17 #define N_FINGER             3
18 #define DEFAULT_FRAMERATE   60
19 #define DOUBLE_ERROR        0.00001
20
21 // for one finger
22 #define DBL_TAP_DISTANCE    30    // pixel
23 #define DRAG_THRESHOLD       3    // pixel
24 #define INIT_DRAG_THRESHOLD 15    // pixel
25 #define MOVE_HISTORY_SIZE    5
26 #define MAX_MOVE_DISTANCE   15
27 #define FLICK_THRESHOLD      5
28
29 // for two finger
30 #define MOVE_THRESHOLD      15
31 #define FINGER_DISTANCE     10
32
33 typedef struct _Mouse_Data Mouse_Data;
34
35 struct _Mouse_Data
36 {
37    Evas_Coord x;
38    Evas_Coord y;
39    int time;
40    int device;
41 };
42
43 typedef enum _Two_Drag_Mode
44 {
45    TWO_DRAG_NONE,
46    TWO_DRAG_PINCH,
47    TWO_DRAG_VERTICAL,
48    TWO_DRAG_HORIZONTAL,
49 } Two_Drag_Mode;
50
51 typedef struct _Two_Mouse_Data Two_Mouse_Data;
52
53 struct _Two_Mouse_Data
54 {
55    Evas_Point first;
56    Evas_Point second;
57    Two_Drag_Mode mode;
58 };
59
60 typedef struct _Three_Mouse_Data Three_Mouse_Data;
61
62 struct _Three_Mouse_Data
63 {
64    Evas_Point first;
65    Evas_Point second;
66    Evas_Point third;
67 };
68
69 typedef enum _Touch_State
70 {
71    TOUCH_STATE_NONE,
72    TOUCH_STATE_DOWN,
73    TOUCH_STATE_DOWN_DURING_DRAG,
74    TOUCH_STATE_DOWN_UP,
75    TOUCH_STATE_DOWN_UP_DOWN,
76    TOUCH_STATE_HOLD,
77    TOUCH_STATE_DRAG,
78    TOUCH_STATE_TWO_DOWN,
79    TOUCH_STATE_TWO_DOWN_DURING_DRAG,
80    TOUCH_STATE_TWO_DOWN_UP,
81    TOUCH_STATE_TWO_DOWN_UP_DOWN,
82    TOUCH_STATE_TWO_DRAG,
83    TOUCH_STATE_THREE_DOWN
84 } Touch_State;
85
86 typedef enum _One_Drag_Mode
87 {
88    ONE_DRAG_NONE,
89    ONE_DRAG_VERTICAL,
90    ONE_DRAG_HORIZONTAL
91 } One_Drag_Mode;
92
93 typedef struct _Flick_Data Flick_Data;
94
95 struct _Flick_Data
96 {
97    int flick_index;
98    Evas_Coord_Point last;
99    Evas_Coord_Point avg_distance;
100 };
101
102 typedef struct _Mouse_Diff_Data Mouse_Diff_Data;
103
104 struct _Mouse_Diff_Data
105 {
106    Evas_Coord dx, dy;
107    double time;
108 };
109
110 typedef struct _Smart_Data Smart_Data;
111
112 struct _Smart_Data
113 {
114    Evas_Object *smart_obj;
115    Evas_Object *child_obj;
116
117    Eina_Bool running;
118
119    int screen_angle;
120    Touch_State state;
121    One_Drag_Mode one_drag_mode;
122    Eina_Bool is_one_drag_mode;
123    Two_Drag_Mode two_drag_mode;
124    int numOfTouch;
125
126    // for flick
127    int last_move_history_index;
128    int move_history_count;
129    Flick_Data flick_data;
130    Mouse_Diff_Data move_history[MOVE_HISTORY_SIZE];
131
132    Mouse_Data first_down[N_FINGER];
133    Mouse_Data last_down[N_FINGER];
134    Mouse_Data last_drag[N_FINGER];
135
136    Ecore_Animator *animator_move;
137    Ecore_Animator *animator_flick;
138    Ecore_Animator *animator_two_move;
139
140    // one finger timers
141    Ecore_Timer *press_timer;
142    Ecore_Timer *long_press_timer;
143    Ecore_Timer *release_timer;
144    Ecore_Timer *press_release_timer;
145
146    // two finger timers
147    Ecore_Timer *two_press_timer;
148    Ecore_Timer *two_release_timer;
149    Ecore_Timer *two_press_release_timer;
150 };
151
152 /* local subsystem functions */
153 // mouse callbacks
154 static float _smart_velocity_easeinoutcubic(int index);
155 static void _smart_mouse_down(void *data, Evas *e, Evas_Object *obj, void *ev);
156 static void _smart_mouse_up(void *data, Evas *e, Evas_Object *obj, void *ev);
157 static void _smart_mouse_move(void *data, Evas *e, Evas_Object *obj, void *ev);
158 static void _smart_multi_down(void *data, Evas *e, Evas_Object *obj, void *ev);
159 static void _smart_multi_up(void *data, Evas *e, Evas_Object *obj, void *ev);
160 static void _smart_multi_move(void *data, Evas *e, Evas_Object *obj, void *ev);
161 // animator callbacks
162 static int _smart_animation_move(void *data);
163 static int _smart_animation_flick(void *data);
164 static int _smart_animation_two_move(void *data);
165 // enter mode functions
166 static void _smart_enter_none(Smart_Data *sd);
167 static void _smart_enter_down(Smart_Data *sd);
168 static void _smart_enter_down_during_drag(Smart_Data *sd);
169 static void _smart_enter_down_up(Smart_Data *sd, int downTime, int time);
170 static void _smart_enter_down_up_down(Smart_Data *sd);
171 static void _smart_enter_hold(Smart_Data *sd);
172 static void _smart_enter_drag(Smart_Data *sd);
173 static void _smart_enter_two_down(Smart_Data *sd);
174 static void _smart_enter_two_down_during_drag(Smart_Data *sd);
175 static void _smart_enter_two_down_up(Smart_Data *sd, int downTime, int time);
176 static void _smart_enter_two_down_up_down(Smart_Data *sd);
177 static void _smart_enter_two_drag(Smart_Data *sd);
178 static void _smart_enter_three_down(Smart_Data *sd);
179 // emit functions
180 static void _smart_emit_press(Smart_Data *sd);
181 static void _smart_emit_tap(Smart_Data *sd);
182 static void _smart_emit_double_tap(Smart_Data *sd);
183 static void _smart_emit_long_hold(Smart_Data *sd);
184 static void _smart_emit_release(Smart_Data *sd);
185 static void _smart_emit_two_press(Smart_Data *sd);
186 static void _smart_emit_two_tap(Smart_Data *sd);
187 static void _smart_emit_two_double_tap(Smart_Data *sd);
188 static void _smart_emit_two_move_start(Smart_Data *sd);
189 static void _smart_emit_two_move(Smart_Data *sd);
190 static void _smart_emit_two_move_end(Smart_Data *sd);
191 static void _smart_emit_three_press(Smart_Data *sd);
192 static void _smart_emit_three_tap(Smart_Data *sd);
193 // timer handlers
194 static int _smart_press_timer_handler(void *data);
195 static int _smart_long_press_timer_handler(void *data);
196 static int _smart_release_timer_handler(void *data);
197 static int _smart_press_release_timer_handler(void *data);
198 static int _tap_delay_timer_handler(void *data);
199 static int _smart_two_press_timer_handler(void *data);
200 static int _smart_two_release_timer_handler(void *data);
201 static int _smart_two_press_release_timer_handler(void *data);
202
203 static void _smart_save_move_history(Smart_Data *sd, int x, int y, int dx, int dy);
204 static void _smart_start_flick(Smart_Data *sd);
205 static void _smart_stop_animator_move(Smart_Data *sd);
206 static void _smart_stop_animator_flick(Smart_Data *sd);
207 static void _smart_stop_animator_two_move(Smart_Data *sd);
208 static Two_Drag_Mode _smart_check_two_drag_mode(Smart_Data *sd);
209 static void _smart_set_first_down(Smart_Data *sd, int index, Mouse_Data *data);
210 static void _smart_set_last_down(Smart_Data *sd, int index, Mouse_Data *data);
211 static void _smart_set_last_drag(Smart_Data *sd, int index, Mouse_Data *data);
212 static void _smart_stop_all_timers(Smart_Data *sd);
213 static void _smart_stop_all_one_timers(Smart_Data *sd);
214 static void _smart_stop_all_two_timers(Smart_Data *sd);
215 static void _smart_init(void);
216 static void _smart_del(Evas_Object *obj);
217 static void _smart_add(Evas_Object *obj);
218
219 /* local subsystem globals */
220 static Evas_Smart *_smart = NULL;
221
222 /* externally accessible functions */
223    Evas_Object *
224 _elm_smart_touch_add(Evas *evas)
225 {
226    _smart_init();
227    return evas_object_smart_add(evas, _smart);
228 }
229
230    void
231 _elm_smart_touch_child_set(Evas_Object *obj, Evas_Object *child)
232 {
233    API_ENTRY return;
234    if (child == sd->child_obj) return;
235
236    if (sd->child_obj) // delete callbacks of old object
237      {
238         evas_object_event_callback_del(sd->child_obj, EVAS_CALLBACK_MOUSE_DOWN, _smart_mouse_down);
239         evas_object_event_callback_del(sd->child_obj, EVAS_CALLBACK_MOUSE_UP, _smart_mouse_up);
240         evas_object_event_callback_del(sd->child_obj, EVAS_CALLBACK_MOUSE_MOVE, _smart_mouse_move);
241         evas_object_event_callback_del(sd->child_obj, EVAS_CALLBACK_MULTI_DOWN, _smart_multi_down);
242         evas_object_event_callback_del(sd->child_obj, EVAS_CALLBACK_MULTI_UP, _smart_multi_up);
243         evas_object_event_callback_del(sd->child_obj, EVAS_CALLBACK_MULTI_MOVE, _smart_multi_move);
244         _smart_stop_all_timers(sd);
245         _smart_stop_animator_move(sd);
246         _smart_stop_animator_flick(sd);
247         _smart_stop_animator_two_move(sd);
248
249         sd->child_obj = NULL;
250      }
251
252    if (child)
253      {
254         sd->child_obj = child;
255
256         // add callbacks
257         evas_object_event_callback_add(child, EVAS_CALLBACK_MOUSE_DOWN, _smart_mouse_down, sd);
258         evas_object_event_callback_add(child, EVAS_CALLBACK_MOUSE_UP, _smart_mouse_up, sd);
259         evas_object_event_callback_add(child, EVAS_CALLBACK_MOUSE_MOVE, _smart_mouse_move, sd);
260         evas_object_event_callback_add(child, EVAS_CALLBACK_MULTI_DOWN, _smart_multi_down, sd);
261         evas_object_event_callback_add(child, EVAS_CALLBACK_MULTI_UP, _smart_multi_up, sd);
262         evas_object_event_callback_add(child, EVAS_CALLBACK_MULTI_MOVE, _smart_multi_move, sd);
263
264         _smart_enter_none(sd);
265
266         sd->is_one_drag_mode = EINA_FALSE;
267      }
268
269    evas_object_smart_callback_call(sd->smart_obj, "changed", NULL);
270 }
271
272    void
273 _elm_smart_touch_start(Evas_Object *obj)
274 {
275    API_ENTRY return;
276    if (sd->running) return;
277
278    sd->running = EINA_TRUE;
279    _smart_enter_none(sd);
280 }
281
282    void
283 _elm_smart_touch_stop(Evas_Object *obj)
284 {
285    API_ENTRY return;
286    sd->running = EINA_FALSE;
287    _smart_stop_all_timers(sd);
288    _smart_stop_animator_move(sd);
289    _smart_stop_animator_flick(sd);
290    _smart_stop_animator_two_move(sd);
291    _smart_enter_none(sd);
292 }
293
294    void
295 _elm_smart_touch_reset(Evas_Object *obj)
296 {
297    API_ENTRY return;
298    _smart_stop_all_timers(sd);
299    _smart_stop_animator_move(sd);
300    _smart_stop_animator_flick(sd);
301    _smart_stop_animator_two_move(sd);
302    _smart_enter_none(sd);
303 }
304
305    void
306 _elm_smart_touch_screen_angle_update(Evas_Object *obj, int screen_angle)
307 {
308    API_ENTRY return;
309    sd->screen_angle = screen_angle;
310 }
311
312    void
313 _elm_smart_touch_is_one_drag_mode_enable(Evas_Object *obj, Eina_Bool is_one_drag_mode)
314 {
315    API_ENTRY return;
316    sd->is_one_drag_mode = is_one_drag_mode;
317 }
318
319 /* local subsystem functions */
320 /** reference from htsd://hosted.zeh.com.br/tweener/docs/en-us/misc/transitions.html
321  * Easing equation function for a cubic (t^3) easing in/out: acceleration until halfway, then deceleration
322  * @param                t   Number                Current time (in frames or seconds)
323  * @param                b   Number                Starting value
324  * @param                c   Number                Change needed in value
325  * @param                d   Number                Expected easing duration (in frames or seconds)
326  * @param                k1  Number                first sustain value
327  * @param                k2  Number                second sustain value
328  * @return                   Number                The correct value
329  public static function easeInOutCubic (t:Number, b:Number, c:Number, d:Number, p_params:Object):Number {
330  if ((t/=d/2) < 1) return c/2*t*t*t + b;
331  return c/2*((t-=2)*t*t + 2) + b;
332  }
333  */
334 static float
335 _smart_velocity_easeinoutcubic(int index)
336 {
337    float d = 60.0f;
338    float t = d - index; // we want to get reversed value
339    float c = 1.0f;
340    float k1 = 0.1f;
341    float k2 = 0.05f;
342    float velocity;
343    if ((t /= (d / 2)) < 1)
344      {
345         velocity = (c / 2) * t * t * t;
346      }
347    else
348      {
349         t -= 2;
350         velocity = (c / 2) * (t * t * t + 2);
351      }
352    if (velocity < k1 && velocity > k2) velocity = 0.1;
353    else if (velocity < k2) velocity = 0.05;
354    return velocity;
355 }
356
357 /* mouse callbacks */
358 static void
359 _smart_mouse_down(void *data, Evas *e, Evas_Object *obj, void *ev)
360 {
361    Smart_Data *sd;
362    Evas_Event_Mouse_Down *event;
363    Mouse_Data mouse_data;
364
365    sd = data;
366    if (!sd || sd->running == EINA_FALSE) return;
367
368    event = (Evas_Event_Mouse_Down*)ev;
369
370    switch (sd->state)
371      {
372       case TOUCH_STATE_NONE:
373          mouse_data.x = event->canvas.x;
374          mouse_data.y = event->canvas.y;
375          mouse_data.time = event->timestamp;
376          mouse_data.device = -1;
377          _smart_set_first_down(sd, 0, &mouse_data);
378          _smart_set_last_down(sd, 0, &mouse_data);
379          _smart_set_last_drag(sd, 0, &mouse_data);
380          _smart_enter_down(sd);
381          break;
382
383       case TOUCH_STATE_DRAG:
384          mouse_data.x = event->canvas.x;
385          mouse_data.y = event->canvas.y;
386          mouse_data.time = event->timestamp;
387          mouse_data.device = -1;
388          _smart_set_first_down(sd, 0, &mouse_data);
389          _smart_set_last_down(sd, 0, &mouse_data);
390          _smart_set_last_drag(sd, 0, &mouse_data);
391          if (sd->animator_move)
392            {
393               ecore_animator_del(sd->animator_move);
394               sd->animator_move = NULL;
395            }
396          if (sd->animator_flick)
397            {
398               ecore_animator_del(sd->animator_flick);
399               sd->animator_flick = NULL;
400            }
401          _smart_enter_down_during_drag(sd);
402          break;
403
404       case TOUCH_STATE_DOWN_UP:
405          if (event->flags == EVAS_BUTTON_DOUBLE_CLICK) {
406               mouse_data.x = event->canvas.x;
407               mouse_data.y = event->canvas.y;
408               mouse_data.time = event->timestamp;
409               mouse_data.device = -1;
410               _smart_set_last_down(sd, 0, &mouse_data);
411               _smart_set_last_drag(sd, 0, &mouse_data);
412               _smart_enter_down_up_down(sd);
413          }
414          break;
415
416       default:
417          break;
418      }
419 }
420
421 static void
422 _smart_mouse_up(void *data, Evas *e, Evas_Object *obj, void *ev)
423 {
424    Smart_Data *sd;
425    sd = data;
426    Evas_Event_Mouse_Up *event;
427
428    if (!sd || sd->running == EINA_FALSE) return;
429
430    event = (Evas_Event_Mouse_Up*)ev;
431
432    switch (sd->state)
433      {
434       case TOUCH_STATE_DOWN:
435          _smart_stop_animator_move(sd);
436          _smart_stop_animator_flick(sd);
437          _smart_enter_down_up(sd, (event->timestamp - sd->last_down[0].time), event->timestamp);
438          break;
439
440       case TOUCH_STATE_DOWN_DURING_DRAG:
441            {
442               Evas_Point point;
443               point.x = sd->last_drag[0].x;
444               point.y = sd->last_drag[0].y;
445               evas_object_smart_callback_call(sd->child_obj, "one,move,end", &point);
446               _smart_enter_none(sd);
447            } break;
448
449       case TOUCH_STATE_DOWN_UP_DOWN:
450            {
451               int dx = sd->last_down[0].x - sd->first_down[0].x;
452               int dy = sd->last_down[0].y - sd->first_down[0].y;
453               if ((dx * dx + dy * dy) <= (DBL_TAP_DISTANCE * DBL_TAP_DISTANCE))
454                 _smart_emit_double_tap(sd);
455               _smart_stop_all_timers(sd);
456               _smart_enter_none(sd);
457            } break;
458
459       case TOUCH_STATE_HOLD:
460          _smart_emit_release(sd);
461          _smart_stop_all_timers(sd);
462          _smart_enter_none(sd);
463          break;
464
465       case TOUCH_STATE_DRAG:
466          _smart_emit_release(sd);
467          _smart_start_flick(sd);
468          break;
469
470       case TOUCH_STATE_TWO_DOWN:
471          sd->numOfTouch = 0;
472          _smart_enter_two_down_up(sd, (event->timestamp - sd->last_down[1].time), event->timestamp);
473          break;
474
475       case TOUCH_STATE_TWO_DOWN_UP:
476          break;
477
478       case TOUCH_STATE_TWO_DOWN_UP_DOWN:
479            {
480               int dx = sd->last_down[0].x - sd->first_down[0].x;
481               int dy = sd->last_down[0].y - sd->first_down[0].y;
482               if ((dx * dx + dy * dy) <= (DBL_TAP_DISTANCE * DBL_TAP_DISTANCE))
483                 _smart_emit_two_double_tap(sd);
484               _smart_stop_all_timers(sd);
485               _smart_enter_none(sd);
486            } break;
487
488       case TOUCH_STATE_TWO_DRAG:
489          _smart_stop_animator_two_move(sd);
490          _smart_stop_all_timers(sd);
491          _smart_enter_none(sd);
492          break;
493
494       case TOUCH_STATE_THREE_DOWN:
495          _smart_emit_three_tap(sd);
496          _smart_stop_all_timers(sd);
497          _smart_enter_none(sd);
498          break;
499
500       default:
501          _smart_emit_release(sd);
502          _smart_stop_all_timers(sd);
503          _smart_enter_none(sd);
504          DBG("\nERROR: wrong state in mouse_up\n\n");
505          break;
506      }
507
508 }
509
510 static void
511 _smart_mouse_move(void *data, Evas *e, Evas_Object *obj, void *ev)
512 {
513    Smart_Data *sd;
514    sd = data;
515    if (!sd || sd->running == EINA_FALSE) return;
516
517    Evas_Event_Mouse_Move *event = (Evas_Event_Mouse_Move*)ev;
518    int dx = 0;
519    int dy = 0;
520
521    Mouse_Data mouse_data;
522    mouse_data.x = event->cur.canvas.x;
523    mouse_data.y = event->cur.canvas.y;
524    mouse_data.time = event->timestamp;
525    mouse_data.device = -1;
526
527    switch (sd->state)
528      {
529       case TOUCH_STATE_DOWN:
530       case TOUCH_STATE_DOWN_DURING_DRAG:
531          dx = mouse_data.x - sd->last_drag[0].x;
532          dy = mouse_data.y - sd->last_drag[0].y;
533
534          if ((abs(dx) > INIT_DRAG_THRESHOLD) || (abs(dy) > INIT_DRAG_THRESHOLD))
535            {
536               if (sd->animator_move)
537                 {
538                    ecore_animator_del(sd->animator_move);
539                    sd->animator_move = NULL;
540                 }
541               if (sd->animator_flick)
542                 {
543                    ecore_animator_del(sd->animator_flick);
544                    sd->animator_flick = NULL;
545                 }
546               _smart_set_last_drag(sd, 0, &mouse_data);
547               // Note:
548               // last_down - location where the drag starts
549               // (which is different than fisrtDown)
550               _smart_set_last_down(sd, 0, &mouse_data);
551               _smart_enter_drag(sd);
552            }
553          break;
554
555       case TOUCH_STATE_DRAG:
556          dx = mouse_data.x - sd->last_drag[0].x;
557          dy = mouse_data.y - sd->last_drag[0].y;
558
559          if ((abs(dx) > DRAG_THRESHOLD) || (abs(dy) > DRAG_THRESHOLD))
560            {
561               _smart_set_last_drag(sd, 0, &mouse_data);
562               _smart_save_move_history(sd, mouse_data.x, mouse_data.y, dx, dy);
563            }
564          break;
565
566       case TOUCH_STATE_TWO_DOWN:
567          dx = mouse_data.x - sd->last_drag[0].x;
568          dy = mouse_data.y - sd->last_drag[0].y;
569
570          if ((abs(dx) > INIT_DRAG_THRESHOLD) || (abs(dy) > INIT_DRAG_THRESHOLD))
571            {
572               _smart_set_last_drag(sd, 0, &mouse_data);
573
574               sd->two_drag_mode = _smart_check_two_drag_mode(sd);
575               if (sd->two_drag_mode != TWO_DRAG_NONE)
576                 {
577                    DBG("<< sd->two_drag_mode [%d] >>\n", sd->two_drag_mode);
578                    _smart_enter_two_drag(sd);
579                 }
580            }
581          break;
582
583       case TOUCH_STATE_TWO_DRAG:
584          _smart_set_last_drag(sd, 0, &mouse_data);
585          break;
586
587       case TOUCH_STATE_THREE_DOWN:
588          _smart_set_last_drag(sd, 0, &mouse_data);
589          break;
590
591       default:
592          break;
593      }
594 }
595
596 static void
597 _smart_multi_down(void *data, Evas *e, Evas_Object *obj, void *ev)
598 {
599    Smart_Data *sd;
600
601    sd = data;
602    if (!sd || sd->running == EINA_FALSE) return;
603
604    Evas_Event_Multi_Down *event = (Evas_Event_Multi_Down*)ev;
605    Mouse_Data mouse_data;
606
607    switch (sd->state)
608      {
609       case TOUCH_STATE_DOWN:
610          // add TOUCH_STATE_NONE,
611          // because sometimes multi down is faster then mouse down
612          // in that case, the two touch is recognized as one touch
613       case TOUCH_STATE_NONE:
614          sd->numOfTouch++;
615          if (sd->numOfTouch == 1)
616            {
617               mouse_data.x = event->output.x;
618               mouse_data.y = event->output.y;
619               mouse_data.time = event->timestamp;
620               mouse_data.device = event->device;
621               _smart_set_first_down(sd, 1, &mouse_data);
622               _smart_set_last_down(sd, 1, &mouse_data);
623               _smart_set_last_drag(sd, 1, &mouse_data);
624               _smart_stop_animator_move(sd);
625               _smart_stop_animator_flick(sd);
626               _smart_stop_animator_two_move(sd);
627               _smart_enter_two_down(sd);
628            }
629          break;
630
631       case TOUCH_STATE_TWO_DOWN_UP:
632          sd->numOfTouch++;
633          if (sd->numOfTouch == 1)
634            {
635               mouse_data.x = event->output.x;
636               mouse_data.y = event->output.y;
637               mouse_data.time = event->timestamp;
638               mouse_data.device = event->device;
639               _smart_set_last_down(sd, 1, &mouse_data);
640               _smart_set_last_drag(sd, 1, &mouse_data);
641               _smart_stop_animator_move(sd);
642               _smart_stop_animator_flick(sd);
643               _smart_stop_animator_two_move(sd);
644               _smart_enter_two_down_up_down(sd);
645            }
646          break;
647
648       case TOUCH_STATE_DOWN_DURING_DRAG:
649       case TOUCH_STATE_DRAG:
650          sd->numOfTouch++;
651          if (sd->numOfTouch == 1)
652            {
653               mouse_data.x = event->output.x;
654               mouse_data.y = event->output.y;
655               mouse_data.time = event->timestamp;
656               mouse_data.device = event->device;
657               _smart_set_first_down(sd, 1, &mouse_data);
658               _smart_set_last_down(sd, 1, &mouse_data);
659               _smart_set_last_drag(sd, 1, &mouse_data);
660               if (sd->animator_move)
661                 {
662                    ecore_animator_del(sd->animator_move);
663                    sd->animator_move = NULL;
664                 }
665               if (sd->animator_flick)
666                 {
667                    ecore_animator_del(sd->animator_flick);
668                    sd->animator_flick = NULL;
669                 }
670               if (sd->animator_two_move)
671                 {
672                    ecore_animator_del(sd->animator_two_move);
673                    sd->animator_two_move = NULL;
674                 }
675               _smart_enter_two_down_during_drag(sd);
676            }
677          break;
678
679       case TOUCH_STATE_TWO_DOWN:
680       case TOUCH_STATE_TWO_DRAG:
681          sd->numOfTouch++;
682          if (sd->numOfTouch == 2)
683            {
684               mouse_data.x = event->output.x;
685               mouse_data.y = event->output.y;
686               mouse_data.time = event->timestamp;
687               mouse_data.device = event->device;
688               _smart_set_first_down(sd, 2, &mouse_data);
689               _smart_set_last_down(sd, 2, &mouse_data);
690               _smart_set_last_drag(sd, 2, &mouse_data);
691               _smart_stop_animator_move(sd);
692               _smart_stop_animator_flick(sd);
693               _smart_stop_animator_two_move(sd);
694               _smart_enter_three_down(sd);
695            }
696          break;
697
698       default:
699          break;
700      }
701 }
702
703 static void
704 _smart_multi_up(void *data, Evas *e, Evas_Object *obj, void *ev)
705 {
706    Smart_Data *sd;
707    Evas_Event_Multi_Up *event;
708
709    sd = data;
710    if (!sd || sd->running == EINA_FALSE) return;
711
712    event = (Evas_Event_Multi_Up*)ev;
713
714    switch (sd->state)
715      {
716       case TOUCH_STATE_TWO_DOWN:
717          sd->numOfTouch = 0;
718          _smart_enter_two_down_up(sd, (event->timestamp - sd->last_down[1].time), event->timestamp);
719          break;
720
721       case TOUCH_STATE_TWO_DOWN_UP:
722       case TOUCH_STATE_TWO_DOWN_UP_DOWN:
723          break;
724
725       case TOUCH_STATE_TWO_DOWN_DURING_DRAG:
726            {
727               Evas_Point point;
728               point.x = sd->last_drag[0].x;
729               point.y = sd->last_drag[0].y;
730               evas_object_smart_callback_call(sd->child_obj, "one,move,end", &point);
731               _smart_emit_two_tap(sd);
732               _smart_stop_all_timers(sd);
733               _smart_enter_none(sd);
734            } break;
735
736       case TOUCH_STATE_TWO_DRAG:
737          _smart_stop_animator_two_move(sd);
738          _smart_stop_all_timers(sd);
739          _smart_enter_none(sd);
740          break;
741
742       case TOUCH_STATE_THREE_DOWN:
743          _smart_emit_three_tap(sd);
744          _smart_stop_all_timers(sd);
745          _smart_enter_none(sd);
746          break;
747
748       default:
749          _smart_stop_all_timers(sd);
750          _smart_enter_none(sd);
751          break;
752      }
753 }
754
755 static void
756 _smart_multi_move(void *data, Evas *e, Evas_Object *obj, void *ev)
757 {
758    Smart_Data *sd;
759    Evas_Event_Multi_Move *event;
760    Mouse_Data mouse_data;
761
762    sd = data;
763    if (!sd || sd->running == EINA_FALSE) return;
764
765    event = (Evas_Event_Multi_Move*)ev;
766    mouse_data.x = event->cur.output.x;
767    mouse_data.y = event->cur.output.y;
768    mouse_data.time = event->timestamp;
769    mouse_data.device = event->device;
770
771    switch (sd->state)
772      {
773       case TOUCH_STATE_TWO_DOWN:
774       case TOUCH_STATE_TWO_DOWN_DURING_DRAG:
775          if (sd->first_down[1].device == event->device)
776            {
777               int dx = mouse_data.x - sd->last_drag[0].x;
778               int dy = mouse_data.y - sd->last_drag[0].y;
779
780               if ((abs(dx) > INIT_DRAG_THRESHOLD) || (abs(dy) > INIT_DRAG_THRESHOLD))
781                 {
782                    _smart_set_last_drag(sd, 1, &mouse_data);
783                    sd->two_drag_mode = _smart_check_two_drag_mode(sd);
784                    if (sd->two_drag_mode != TWO_DRAG_NONE)
785                      {
786                         DBG("<< sd->two_drag_mode [%d] >>\n", sd->two_drag_mode);
787                         _smart_enter_two_drag(sd);
788                      }
789                 }
790            }
791          break;
792
793       case TOUCH_STATE_TWO_DRAG:
794          if (sd->first_down[1].device == event->device)
795            {
796               _smart_set_last_drag(sd, 1, &mouse_data);
797            }
798          break;
799
800       case TOUCH_STATE_THREE_DOWN:
801          if (sd->first_down[1].device == event->device)
802            {
803               _smart_set_last_drag(sd, 1, &mouse_data);
804            }
805          else if (sd->first_down[2].device == event->device)
806            {
807               _smart_set_last_drag(sd, 2, &mouse_data);
808            }
809          break;
810
811       default:
812          break;
813      }
814 }
815
816 /* animators */
817 static int
818 _smart_animation_move(void *data)
819 {
820    Smart_Data *sd;
821
822    sd = data;
823    if (sd->child_obj)
824      {
825         DBG("<< animation_move >>\n");
826         // get the position here instead of mouse_move event
827         Evas *evas = evas_object_evas_get(sd->child_obj);
828         Evas_Point point;
829         evas_pointer_canvas_xy_get(evas, &point.x, &point.y);
830         if (sd->is_one_drag_mode)
831           {
832              if (sd->one_drag_mode == ONE_DRAG_VERTICAL)
833                {
834                   // Note:
835                   // first_down - location of mouse down
836                   // last_down - location where the drag started
837                   point.x = sd->last_down[0].x;
838                }
839              else if (sd->one_drag_mode == ONE_DRAG_HORIZONTAL)
840                {
841                   point.y = sd->last_down[0].y;
842                }
843           }
844         evas_object_smart_callback_call(sd->child_obj, "one,move", &point);
845         return ECORE_CALLBACK_RENEW;
846      }
847    else
848      {
849         _smart_stop_animator_move(sd);
850         _smart_enter_none(sd);
851         return ECORE_CALLBACK_CANCEL;
852      }
853 }
854
855 static int
856 _smart_animation_flick(void *data)
857 {
858    Smart_Data *sd;
859    Flick_Data *flick_data;
860
861    sd = data;
862    flick_data = &(sd->flick_data);
863
864    if (flick_data && sd->child_obj)
865      {
866         // calculate dx, dy
867         float velocity = _smart_velocity_easeinoutcubic(flick_data->flick_index);
868         Evas_Coord dx = flick_data->avg_distance.x * velocity;
869         Evas_Coord dy = flick_data->avg_distance.y * velocity;
870         flick_data->flick_index++;
871         flick_data->last.x += dx;
872         flick_data->last.y += dy;
873         DBG("<< animation_flick |%d|%d|%f| >>\n", dx, dy, ecore_loop_time_get());
874
875         // stop flick animator
876         if (dx == 0 && dy == 0)
877           {
878              _smart_stop_animator_flick(sd);
879              if (sd->state == TOUCH_STATE_DRAG)
880                _smart_enter_none(sd);
881              return ECORE_CALLBACK_CANCEL;
882           }
883         else
884           {
885              Evas_Coord_Point point;
886              point = flick_data->last;
887              if (sd->is_one_drag_mode)
888                {
889                   if (sd->one_drag_mode == ONE_DRAG_VERTICAL)
890                     {
891                        point.x = sd->first_down[0].x;
892                     }
893                   else if (sd->one_drag_mode == ONE_DRAG_HORIZONTAL)
894                     {
895                        point.y = sd->first_down[0].y;
896                     }
897                }
898              evas_object_smart_callback_call(sd->child_obj, "one,move", &point);
899              return ECORE_CALLBACK_RENEW;
900           }
901      }
902    else
903      {
904         _smart_stop_animator_flick(sd);
905         _smart_enter_none(sd);
906         return ECORE_CALLBACK_CANCEL;
907      }
908 }
909
910 static int
911 _smart_animation_two_move(void *data)
912 {
913    Smart_Data *sd;
914
915    sd = data;
916
917    if (sd->child_obj)
918      {
919         _smart_emit_two_move(sd);
920         return ECORE_CALLBACK_RENEW;
921      }
922    else
923      {
924         _smart_stop_animator_two_move(sd);
925         _smart_enter_none(sd);
926         return ECORE_CALLBACK_CANCEL;
927      }
928
929 }
930
931 /* state switching */
932 static void
933 _smart_enter_none(Smart_Data *sd)
934 {
935    sd->numOfTouch = 0;
936    sd->two_drag_mode = TWO_DRAG_NONE;
937    sd->state = TOUCH_STATE_NONE;
938    DBG("\nTOUCH_STATE_NONE\n");
939 }
940
941 static void
942 _smart_enter_down(Smart_Data *sd)
943 {
944    // set press timer
945    sd->press_timer = ecore_timer_add(((double)PRESS_TIME)/1000.0, _smart_press_timer_handler, sd);
946
947    // set long press timer
948    sd->long_press_timer = ecore_timer_add(((double)LONG_HOLD_TIME)/1000.0, _smart_long_press_timer_handler, sd);
949
950    sd->state = TOUCH_STATE_DOWN;
951    DBG("\nTOUCH_STATE_DOWN\n");
952 }
953
954 static void
955 _smart_enter_down_during_drag(Smart_Data *sd)
956 {
957    // set press timer
958    sd->press_timer = ecore_timer_add(((double)PRESS_TIME)/1000.0, _smart_press_timer_handler, sd);
959
960    sd->state = TOUCH_STATE_DOWN_DURING_DRAG;
961    DBG("\nTOUCH_STATE_DOWN_DURING_DRAG\n");
962 }
963
964 static void
965 _smart_enter_down_up(Smart_Data *sd, int downTime, int time)
966 {
967    // remove sd->press_timer and set new timer
968    int timerTime = RELEASE_TIME - (downTime - PRESS_TIME);
969    if (sd->press_timer)
970      {
971         ecore_timer_del(sd->press_timer);
972         sd->press_timer = NULL;
973         sd->press_release_timer = ecore_timer_add(((double)timerTime)/1000.0, _smart_press_release_timer_handler, sd);
974
975      }
976    else
977      {
978         sd->release_timer = ecore_timer_add(((double)timerTime)/1000.0, _smart_release_timer_handler, sd);
979      }
980
981    if (sd->long_press_timer) // remove long press timer
982      {
983         ecore_timer_del(sd->long_press_timer);
984         sd->long_press_timer = NULL;
985      }
986
987    sd->state = TOUCH_STATE_DOWN_UP;
988    DBG("\nTOUCH_STATE_DOWN_UP\n");
989 }
990
991 static void
992 _smart_enter_down_up_down(Smart_Data *sd)
993 {
994    if (sd->press_release_timer) // remove press_release_timer
995      {
996         ecore_timer_del(sd->press_release_timer);
997         sd->press_release_timer = NULL;
998      }
999
1000    if (sd->release_timer) // remove ReleaseTimer
1001      {
1002         ecore_timer_del(sd->release_timer);
1003         sd->release_timer = NULL;
1004      }
1005
1006    sd->state = TOUCH_STATE_DOWN_UP_DOWN;
1007    DBG("\nTOUCH_STATE_DOWN_UP_DOWN\n");
1008 }
1009
1010 static void
1011 _smart_enter_hold(Smart_Data *sd)
1012 {
1013    sd->state = TOUCH_STATE_HOLD;
1014    DBG("\nTOUCH_STATE_HOLD\n");
1015 }
1016
1017 static void
1018 _smart_enter_drag(Smart_Data *sd)
1019 {
1020    if (sd->press_timer) // remove press_timer
1021      {
1022         ecore_timer_del(sd->press_timer);
1023         sd->press_timer = NULL;
1024      }
1025
1026    if (sd->press_release_timer) // remove press_release_timer
1027      {
1028         ecore_timer_del(sd->press_release_timer);
1029         sd->press_release_timer = NULL;
1030      }
1031
1032    if (sd->release_timer) // remove ReleaseTimer
1033      {
1034         ecore_timer_del(sd->release_timer);
1035         sd->release_timer = NULL;
1036      }
1037
1038    if (sd->long_press_timer) // remove long press timer
1039      {
1040         ecore_timer_del(sd->long_press_timer);
1041         sd->long_press_timer = NULL;
1042      }
1043
1044    if (sd->child_obj)
1045      {
1046         if (sd->is_one_drag_mode)
1047           {
1048              sd->one_drag_mode = ONE_DRAG_NONE;
1049              int abs_dx = abs(sd->first_down[0].x - sd->last_drag[0].x);
1050              int abs_dy = abs(sd->first_down[0].y - sd->last_drag[0].y);
1051              abs_dx = (abs_dx == 0) ? 1 : abs_dx;
1052              DBG("<< abs_dx[%d], abs_dy[%d] >>\n\n", abs_dx, abs_dy);
1053              float degree = (float)abs_dy / (float)abs_dx;
1054              // more than 70 degree
1055              if (degree > tan(70 * M_PI / 180))
1056                {
1057                   sd->one_drag_mode = ONE_DRAG_VERTICAL;
1058                }
1059              // less than 20 degree
1060              else if (degree < tan(20 * M_PI / 180))
1061                {
1062                   sd->one_drag_mode = ONE_DRAG_HORIZONTAL;
1063                }
1064           }
1065         Evas_Point point;
1066         point.x = sd->last_down[0].x;
1067         point.y = sd->last_down[0].y;
1068         evas_object_smart_callback_call(sd->child_obj, "one,move,start", &point);
1069
1070         // initialize flick variables
1071         sd->last_move_history_index = -1;
1072         sd->move_history_count = 0;
1073
1074         sd->animator_move = ecore_animator_add(_smart_animation_move, sd);
1075         DBG("<< sd->animator_move >>\n");
1076         sd->state = TOUCH_STATE_DRAG;
1077         DBG("\nTOUCH_STATE_DRAG\n");
1078      }
1079    else
1080      {
1081         sd->state = TOUCH_STATE_NONE;
1082      }
1083 }
1084
1085 static void
1086 _smart_enter_two_down(Smart_Data *sd)
1087 {
1088    _smart_stop_all_timers(sd);
1089
1090    // set two press timer
1091    sd->two_press_timer = ecore_timer_add(((double)PRESS_TIME)/1000.0, _smart_two_press_timer_handler, sd);
1092
1093    sd->state = TOUCH_STATE_TWO_DOWN;
1094    DBG("\nTOUCH_STATE_TWO_DOWN\n");
1095 }
1096
1097 static void
1098 _smart_enter_two_down_during_drag(Smart_Data *sd)
1099 {
1100    _smart_stop_all_timers(sd);
1101
1102    // set two press timer
1103    sd->two_press_timer = ecore_timer_add(((double)PRESS_TIME)/1000.0, _smart_two_press_timer_handler, sd);
1104
1105    sd->state = TOUCH_STATE_TWO_DOWN_DURING_DRAG;
1106    DBG("<< enter two down >>\n");
1107 }
1108
1109 static void
1110 _smart_enter_two_down_up(Smart_Data *sd, int downTime, int time)
1111 {
1112    // remove sd->press_timer and set new timer
1113    int timerTime = RELEASE_TIME - (downTime - PRESS_TIME);
1114    printf("<< time [%d] >>\n", timerTime);
1115    if (sd->two_press_timer)
1116      {
1117         ecore_timer_del(sd->two_press_timer);
1118         sd->two_press_timer = NULL;
1119         sd->two_press_release_timer =
1120            ecore_timer_add(((double)timerTime)/1000.0, _smart_two_press_release_timer_handler, sd);
1121
1122      }
1123    else
1124      {
1125         sd->two_release_timer = ecore_timer_add(((double)timerTime)/1000.0, _smart_two_release_timer_handler, sd);
1126      }
1127
1128    sd->state = TOUCH_STATE_TWO_DOWN_UP;
1129    DBG("\nTOUCH_STATE_TWO_DOWN_UP\n");
1130 }
1131
1132 static void
1133 _smart_enter_two_down_up_down(Smart_Data *sd)
1134 {
1135    if (sd->two_press_release_timer) // remove two_press_release_timer
1136      {
1137         ecore_timer_del(sd->two_press_release_timer);
1138         sd->two_press_release_timer = NULL;
1139      }
1140
1141    if (sd->two_release_timer) // remove two_release_timer
1142      {
1143         ecore_timer_del(sd->two_release_timer);
1144         sd->two_release_timer = NULL;
1145      }
1146
1147    sd->state = TOUCH_STATE_TWO_DOWN_UP_DOWN;
1148    DBG("\nTOUCH_STATE_TWO_DOWN_UP_DOWN\n");
1149 }
1150
1151 static void
1152 _smart_enter_two_drag(Smart_Data *sd)
1153 {
1154    if (sd->child_obj)
1155      {
1156         DBG("<< sd->animator_two_move >>\n");
1157         sd->state = TOUCH_STATE_TWO_DRAG;
1158         _smart_emit_two_move_start(sd);
1159         sd->animator_two_move = ecore_animator_add(_smart_animation_two_move, sd);
1160      }
1161    else
1162      {
1163         sd->state = TOUCH_STATE_NONE;
1164      }
1165 }
1166
1167 static void
1168 _smart_enter_three_down(Smart_Data *sd)
1169 {
1170    if (sd->child_obj)
1171      {
1172         sd->state = TOUCH_STATE_THREE_DOWN;
1173         _smart_emit_three_press(sd);
1174      }
1175 }
1176
1177 /* producing output events */
1178 static void
1179 _smart_emit_press(Smart_Data *sd)
1180 {
1181    if (sd->child_obj)
1182      {
1183         DBG("<< emit_press >>\n");
1184         Evas_Point point;
1185         point.x = sd->last_down[0].x;
1186         point.y = sd->last_down[0].y;
1187         evas_object_smart_callback_call(sd->child_obj, "one,press", &point);
1188      }
1189 }
1190
1191 static void
1192 _smart_emit_tap(Smart_Data *sd)
1193 {
1194    if (sd->child_obj)
1195      {
1196         DBG("<< emit_tap >>\n");
1197         Evas_Point point;
1198         point.x = sd->last_down[0].x;
1199         point.y = sd->last_down[0].y;
1200         evas_object_smart_callback_call(sd->child_obj, "one,single,tap", &point);
1201      }
1202 }
1203
1204 static void
1205 _smart_emit_double_tap(Smart_Data *sd)
1206 {
1207    if (sd->child_obj)
1208      {
1209         DBG("<< emit_double_tap >>\n");
1210         Evas_Point point;
1211         point.x = sd->last_down[0].x;
1212         point.y = sd->last_down[0].y;
1213         evas_object_smart_callback_call(sd->child_obj, "one,double,tap", &point);
1214      }
1215 }
1216
1217 static void
1218 _smart_emit_long_hold(Smart_Data *sd)
1219 {
1220    if (sd->child_obj)
1221      {
1222         DBG("<< emit_long_hold >>\n");
1223         Evas_Point point;
1224         point.x = sd->last_down[0].x;
1225         point.y = sd->last_down[0].y;
1226         evas_object_smart_callback_call(sd->child_obj, "one,long,press", &point);
1227      }
1228 }
1229
1230 static void
1231 _smart_emit_release(Smart_Data *sd)
1232 {
1233    if (sd->child_obj)
1234      {
1235         DBG("<< emit_release >>\n");
1236         Evas_Point point;
1237         point.x = sd->last_down[0].x;
1238         point.y = sd->last_down[0].y;
1239         evas_object_smart_callback_call(sd->child_obj, "one,release", &point);
1240      }
1241 }
1242
1243 static void
1244 _smart_emit_two_press(Smart_Data *sd)
1245 {
1246    if (sd->child_obj)
1247      {
1248         DBG("<< emit_two_press >>\n");
1249         Two_Mouse_Data two_mouse_data;
1250         two_mouse_data.first.x = sd->last_down[0].x;
1251         two_mouse_data.first.y = sd->last_down[0].y;
1252         two_mouse_data.second.x = sd->last_down[1].x;
1253         two_mouse_data.second.y = sd->last_down[1].y;
1254         two_mouse_data.mode = sd->two_drag_mode;
1255         evas_object_smart_callback_call(sd->child_obj, "two,press", &two_mouse_data);
1256      }
1257 }
1258
1259 static void
1260 _smart_emit_two_tap(Smart_Data *sd)
1261 {
1262    if (sd->child_obj)
1263      {
1264         DBG("<< emit_two_tap >>\n");
1265         Two_Mouse_Data two_mouse_data;
1266         two_mouse_data.first.x = sd->last_down[0].x;
1267         two_mouse_data.first.y = sd->last_down[0].y;
1268         two_mouse_data.second.x = sd->last_down[1].x;
1269         two_mouse_data.second.y = sd->last_down[1].y;
1270         two_mouse_data.mode = sd->two_drag_mode;
1271         evas_object_smart_callback_call(sd->child_obj, "two,tap", &two_mouse_data);
1272      }
1273 }
1274
1275 static void
1276 _smart_emit_two_double_tap(Smart_Data *sd)
1277 {
1278    if (sd->child_obj)
1279      {
1280         DBG("<< emit_two_double_tap >>\n");
1281         Two_Mouse_Data two_mouse_data;
1282         two_mouse_data.first.x = sd->last_down[0].x;
1283         two_mouse_data.first.y = sd->last_down[0].y;
1284         two_mouse_data.second.x = sd->last_down[1].x;
1285         two_mouse_data.second.y = sd->last_down[1].y;
1286         two_mouse_data.mode = sd->two_drag_mode;
1287         evas_object_smart_callback_call(sd->child_obj, "two,double,tap", &two_mouse_data);
1288      }
1289 }
1290
1291 static void
1292 _smart_emit_two_move_start(Smart_Data *sd)
1293 {
1294    if (sd->child_obj)
1295      {
1296         DBG("<< emit_two_move_start >>\n");
1297         Two_Mouse_Data two_mouse_data;
1298         two_mouse_data.first.x = sd->last_drag[0].x;
1299         two_mouse_data.first.y = sd->last_drag[0].y;
1300         two_mouse_data.second.x = sd->last_drag[1].x;
1301         two_mouse_data.second.y = sd->last_drag[1].y;
1302         two_mouse_data.mode = sd->two_drag_mode;
1303         evas_object_smart_callback_call(sd->child_obj, "two,move,start", &two_mouse_data);
1304      }
1305 }
1306
1307 static void
1308 _smart_emit_two_move(Smart_Data *sd)
1309 {
1310    if (sd->child_obj)
1311      {
1312         Two_Mouse_Data two_mouse_data;
1313         two_mouse_data.first.x = sd->last_drag[0].x;
1314         two_mouse_data.first.y = sd->last_drag[0].y;
1315         two_mouse_data.second.x = sd->last_drag[1].x;
1316         two_mouse_data.second.y = sd->last_drag[1].y;
1317         two_mouse_data.mode = sd->two_drag_mode;
1318         evas_object_smart_callback_call(sd->child_obj, "two,move", &two_mouse_data);
1319      }
1320 }
1321
1322 static void
1323 _smart_emit_two_move_end(Smart_Data *sd)
1324 {
1325    if (sd->child_obj)
1326      {
1327         DBG("<< emit_two_move_end >>\n");
1328         Two_Mouse_Data two_mouse_data;
1329         two_mouse_data.first.x = sd->last_drag[0].x;
1330         two_mouse_data.first.y = sd->last_drag[0].y;
1331         two_mouse_data.second.x = sd->last_drag[1].x;
1332         two_mouse_data.second.y = sd->last_drag[1].y;
1333         two_mouse_data.mode = sd->two_drag_mode;
1334         evas_object_smart_callback_call(sd->child_obj, "two,move,end", &two_mouse_data);
1335      }
1336 }
1337
1338 static void
1339 _smart_emit_three_press(Smart_Data *sd)
1340 {
1341    if (sd->child_obj)
1342      {
1343         DBG("<< emit_three_press >>\n");
1344         Three_Mouse_Data three_mouse_data;
1345         three_mouse_data.first.x = sd->last_drag[0].x;
1346         three_mouse_data.first.y = sd->last_drag[0].y;
1347         three_mouse_data.second.x = sd->last_drag[1].x;
1348         three_mouse_data.second.y = sd->last_drag[1].y;
1349         three_mouse_data.third.x = sd->last_drag[2].x;
1350         three_mouse_data.third.y = sd->last_drag[2].y;
1351         evas_object_smart_callback_call(sd->child_obj, "three,press", &three_mouse_data);
1352      }
1353 }
1354
1355 static void
1356 _smart_emit_three_tap(Smart_Data *sd)
1357 {
1358    if (sd->child_obj)
1359      {
1360         DBG("<< emit_three_tap >>\n");
1361         Three_Mouse_Data three_mouse_data;
1362         three_mouse_data.first.x = sd->last_drag[0].x;
1363         three_mouse_data.first.y = sd->last_drag[0].y;
1364         three_mouse_data.second.x = sd->last_drag[1].x;
1365         three_mouse_data.second.y = sd->last_drag[1].y;
1366         three_mouse_data.third.x = sd->last_drag[2].x;
1367         three_mouse_data.third.y = sd->last_drag[2].y;
1368         evas_object_smart_callback_call(sd->child_obj, "three,tap", &three_mouse_data);
1369      }
1370 }
1371
1372 /* timer event handling */
1373 static int
1374 _smart_press_timer_handler(void *data)
1375 {
1376    DBG("<< %s >>\n", __func__);
1377    Smart_Data *sd;
1378
1379    sd = data;
1380    _smart_emit_press(sd);
1381    sd->press_timer = NULL;
1382    return ECORE_CALLBACK_CANCEL;
1383 }
1384
1385 static int
1386 _smart_long_press_timer_handler(void *data)
1387 {
1388    DBG("<< %s >>\n", __func__);
1389    Smart_Data *sd;
1390
1391    sd = data;
1392    _smart_emit_long_hold(sd);
1393    _smart_enter_hold(sd);
1394    sd->long_press_timer = NULL;
1395    return ECORE_CALLBACK_CANCEL;
1396 }
1397
1398 static int
1399 _smart_release_timer_handler(void *data)
1400 {
1401    DBG("<< %s >>\n", __func__);
1402    Smart_Data *sd;
1403
1404    sd = data;
1405    _smart_emit_tap(sd);
1406    _smart_stop_all_timers(sd);
1407    _smart_enter_none(sd);
1408    sd->release_timer = NULL;
1409    return ECORE_CALLBACK_CANCEL;
1410 }
1411
1412 static int
1413 _smart_press_release_timer_handler(void *data)
1414 {
1415    DBG("<< %s >>\n", __func__);
1416    static int prevent_handler = 0;
1417    if (prevent_handler != 0) return ECORE_CALLBACK_CANCEL;
1418    prevent_handler = 1;
1419    Smart_Data *sd;
1420
1421    sd = data;
1422    _smart_emit_press(sd);
1423    // emit tap after 10ms later
1424    // because, webkit engine can not draw the focus ring,
1425    // if we emit press and release at the same time
1426    ecore_timer_add((double)10/1000.0, _tap_delay_timer_handler, sd);
1427    _smart_stop_all_timers(sd);
1428    _smart_enter_none(sd);
1429    sd->press_release_timer = NULL;
1430    prevent_handler = 0;
1431    return ECORE_CALLBACK_CANCEL;
1432 }
1433
1434 static int
1435 _tap_delay_timer_handler(void *data)
1436 {
1437    DBG("<< %s >>\n", __func__);
1438    Smart_Data *sd;
1439
1440    sd = data;
1441    _smart_emit_tap(sd);
1442    return ECORE_CALLBACK_CANCEL;
1443 }
1444
1445 static int
1446 _smart_two_press_timer_handler(void *data)
1447 {
1448    DBG("<< %s >>\n", __func__);
1449    Smart_Data *sd;
1450
1451    sd = data;
1452    _smart_emit_two_press(sd);
1453    _smart_stop_all_one_timers(sd);
1454    sd->two_press_timer = NULL;
1455    return ECORE_CALLBACK_CANCEL;
1456 }
1457
1458 static int
1459 _smart_two_release_timer_handler(void *data)
1460 {
1461    DBG("<< %s >>\n", __func__);
1462    Smart_Data *sd;
1463
1464    sd = data;
1465    _smart_emit_two_tap(sd);
1466    _smart_stop_all_timers(sd);
1467    _smart_enter_none(sd);
1468    sd->two_release_timer = NULL;
1469    return ECORE_CALLBACK_CANCEL;
1470 }
1471
1472 static int
1473 _smart_two_press_release_timer_handler(void *data)
1474 {
1475    DBG("<< %s >>\n", __func__);
1476    Smart_Data *sd;
1477
1478    sd = data;
1479    _smart_emit_two_press(sd);
1480    _smart_emit_two_tap(sd);
1481    _smart_stop_all_timers(sd);
1482    _smart_enter_none(sd);
1483    sd->two_press_release_timer = NULL;
1484    return ECORE_CALLBACK_CANCEL;
1485 }
1486
1487 /* other functions */
1488 static void
1489 _smart_save_move_history(Smart_Data *sd, int x, int y, int dx, int dy)
1490 {
1491    // save pan information to the pan history
1492    int index = (sd->last_move_history_index + 1) % MOVE_HISTORY_SIZE;
1493    sd->last_move_history_index = index;
1494    sd->move_history[index].dx = dx;
1495    sd->move_history[index].dy = dy;
1496    sd->move_history[index].time = ecore_time_get();
1497    sd->move_history_count++;
1498 }
1499
1500 static void
1501 _smart_start_flick(Smart_Data *sd)
1502 {
1503    if (sd->animator_move)
1504      {
1505         ecore_animator_del(sd->animator_move);
1506         DBG("<< stop_animator_move >>\n");
1507         sd->animator_move = NULL;
1508
1509         // start flick
1510         // accumulate sd->move_history data
1511         int nSamples = 0;
1512         int totalDx = 0;
1513         int totalDy = 0;
1514         int index = sd->last_move_history_index;
1515         int todo = sd->move_history_count > MOVE_HISTORY_SIZE ? MOVE_HISTORY_SIZE : sd->move_history_count;
1516         Mouse_Diff_Data *p;
1517         double endTime = ecore_time_get();
1518         double startTime = endTime;
1519         for( ; todo > 0; todo--) {
1520              p = sd->move_history + index; // get one sd->move_history
1521
1522              // get values
1523              startTime = p->time;
1524              totalDx += p->dx;
1525              totalDy += p->dy;
1526              nSamples++;
1527
1528              if ((endTime - startTime) > 0.2 && nSamples > 0)
1529                break;
1530
1531              index = (index > 0) ? (index - 1) : (MOVE_HISTORY_SIZE - 1); // set index
1532         }
1533         double totalTime = endTime - startTime;
1534         if (totalTime < DOUBLE_ERROR)
1535           totalTime = 0.001;
1536
1537         // calculate average pan_dx and pan_dy (per 1 / DEFAULT_FRAMERATE ms)
1538         double temp = totalTime * DEFAULT_FRAMERATE;
1539         if (temp <= 0)
1540           temp = 1;
1541
1542         Flick_Data *flick_data = &sd->flick_data;
1543         flick_data->avg_distance.x = totalDx / temp;
1544         flick_data->avg_distance.y = totalDy / temp;
1545
1546         // set max value for pan_dx and pan_dy
1547         int abs_pan_dx = abs(flick_data->avg_distance.x);
1548         int abs_pan_dy = abs(flick_data->avg_distance.y);
1549         if ((abs_pan_dx > MAX_MOVE_DISTANCE) && (abs_pan_dx > abs_pan_dy))
1550           {
1551              flick_data->avg_distance.x = (flick_data->avg_distance.x > 0) ? MAX_MOVE_DISTANCE : -MAX_MOVE_DISTANCE;
1552              flick_data->avg_distance.y = flick_data->avg_distance.y * MAX_MOVE_DISTANCE / abs_pan_dx;
1553
1554           }
1555         else if ((abs_pan_dy > MAX_MOVE_DISTANCE) && (abs_pan_dy > abs_pan_dx))
1556           {
1557              flick_data->avg_distance.y = (flick_data->avg_distance.y > 0) ? MAX_MOVE_DISTANCE : -MAX_MOVE_DISTANCE;
1558              flick_data->avg_distance.x = flick_data->avg_distance.x * MAX_MOVE_DISTANCE / abs_pan_dy;
1559           }
1560
1561         if (abs_pan_dx > FLICK_THRESHOLD || abs_pan_dy > FLICK_THRESHOLD)
1562           {
1563              // set flick_data and start flick
1564              flick_data->last.x = sd->last_drag[0].x;
1565              flick_data->last.y = sd->last_drag[0].y;
1566              flick_data->flick_index = 0;
1567              sd->animator_flick = ecore_animator_add(_smart_animation_flick, sd);
1568              DBG("<< sd->animator_flick >>\n");
1569           }
1570         else
1571           {
1572              Evas_Point point;
1573              point.x = sd->last_drag[0].x;
1574              point.y = sd->last_drag[0].y;
1575              evas_object_smart_callback_call(sd->child_obj, "one,move,end", &point);
1576              _smart_enter_none(sd);
1577           }
1578      }
1579    else
1580      {
1581         _smart_emit_release(sd);
1582         _smart_stop_all_timers(sd);
1583         _smart_enter_none(sd);
1584      }
1585 }
1586
1587 static void
1588 _smart_stop_animator_move(Smart_Data *sd)
1589 {
1590    if (sd->animator_move)
1591      {
1592         ecore_animator_del(sd->animator_move);
1593         DBG("<< stop_animator_move >>\n");
1594         sd->animator_move = NULL;
1595         Evas_Point point;
1596         point.x = sd->last_drag[0].x;
1597         point.y = sd->last_drag[0].y;
1598         evas_object_smart_callback_call(sd->child_obj, "one,move,end", &point);
1599      }
1600 }
1601
1602 static void
1603 _smart_stop_animator_flick(Smart_Data *sd)
1604 {
1605    if (sd->animator_flick)
1606      {
1607         ecore_animator_del(sd->animator_flick);
1608         DBG("<< stop_animator_flick >>\n");
1609         sd->animator_flick = NULL;
1610         Evas_Coord_Point point;
1611         point = sd->flick_data.last;
1612         evas_object_smart_callback_call(sd->child_obj, "one,move,end", &point);
1613      }
1614 }
1615
1616 static void
1617 _smart_stop_animator_two_move(Smart_Data *sd)
1618 {
1619    if (sd->animator_two_move)
1620      {
1621         ecore_animator_del(sd->animator_two_move);
1622         DBG("<< stop_animator_two_move >>\n");
1623         sd->animator_two_move = NULL;
1624         _smart_emit_two_move_end(sd);
1625      }
1626 }
1627
1628 static Two_Drag_Mode
1629 _smart_check_two_drag_mode(Smart_Data *sd)
1630 {
1631    // get distance from press to current position
1632    int dx0 = sd->last_drag[0].x - sd->first_down[0].x;
1633    int dy0 = sd->last_drag[0].y - sd->first_down[0].y;
1634    int dx1 = sd->last_drag[1].x - sd->first_down[1].x;
1635    int dy1 = sd->last_drag[1].y - sd->first_down[1].y;
1636    int dx = 0;
1637    int dy = 0;
1638
1639    // select dx and dy
1640    if ((abs(dx1) >= MOVE_THRESHOLD) || (abs(dy1) >= MOVE_THRESHOLD))
1641      {
1642         dx = dx1;
1643         dy = dy1;
1644      }
1645    else if ((abs(dx0) >= MOVE_THRESHOLD) || (abs(dy0) >= MOVE_THRESHOLD))
1646      {
1647         dx = dx0;
1648         dy = dy0;
1649      }
1650    else
1651      {
1652         return TWO_DRAG_NONE;
1653      }
1654
1655    // same x direction
1656    if ((abs(dx) > abs(dy)) && ((dx0 > 0 && dx1 > 0) || (dx0 < 0 && dx1 < 0)))
1657      {
1658         dy = (dy == 0) ? 1 : dy;
1659         // less than 30 degree (1024/root(3) = 591)
1660         if (((abs(dy) << 10) / abs(dx)) < 591)
1661           {
1662              return TWO_DRAG_HORIZONTAL;
1663           }
1664      }
1665
1666    // same y direction
1667    if ((abs(dy) > abs(dx)) && ((dy0 > 0 && dy1 > 0) || (dy0 < 0 && dy1 < 0)))
1668      {
1669         dx = (dx == 0) ? 1 : dx;
1670         // more than 60 degree (1024 * root(3)/1 = 1773)
1671         if (((abs(dy) << 10) / abs(dx)) > 1773)
1672           {
1673              return TWO_DRAG_VERTICAL;
1674           }
1675      }
1676
1677    // pinch direction
1678    int distanceX = abs(abs(sd->first_down[0].x - sd->first_down[1].x)
1679          - abs(sd->last_drag[0].x - sd->last_drag[1].x));
1680    int distanceY = abs(abs(sd->first_down[0].y - sd->first_down[1].y)
1681          - abs(sd->last_drag[0].y - sd->last_drag[1].y));
1682    if ((distanceX > FINGER_DISTANCE) || (distanceY > FINGER_DISTANCE))
1683      {
1684         return TWO_DRAG_PINCH;
1685      }
1686
1687    return TWO_DRAG_NONE;
1688 }
1689
1690 static void
1691 _smart_set_first_down(Smart_Data *sd, int index, Mouse_Data *data)
1692 {
1693    if (index > N_FINGER)
1694      {
1695         return;
1696      }
1697
1698    if ((sd->screen_angle == 270) && (index > 0))
1699      {
1700         sd->first_down[index].x = data->y;
1701         sd->first_down[index].y = data->x;
1702         sd->first_down[index].time = data->time;
1703         sd->first_down[index].device = data->device;
1704      }
1705    else
1706      {
1707         sd->first_down[index].x = data->x;
1708         sd->first_down[index].y = data->y;
1709         sd->first_down[index].time = data->time;
1710         sd->first_down[index].device = data->device;
1711      }
1712 }
1713
1714 static void
1715 _smart_set_last_down(Smart_Data *sd, int index, Mouse_Data *data)
1716 {
1717    if (index > N_FINGER)
1718      {
1719         return;
1720      }
1721
1722    if ((sd->screen_angle == 270) && (index > 0))
1723      {
1724         sd->last_down[index].x = data->y;
1725         sd->last_down[index].y = data->x;
1726         sd->last_down[index].time = data->time;
1727         sd->last_down[index].device = data->device;
1728      }
1729    else
1730      {
1731         sd->last_down[index].x = data->x;
1732         sd->last_down[index].y = data->y;
1733         sd->last_down[index].time = data->time;
1734         sd->last_down[index].device = data->device;
1735      }
1736 }
1737
1738 static void
1739 _smart_set_last_drag(Smart_Data *sd, int index, Mouse_Data *data)
1740 {
1741    if (index > N_FINGER)
1742      {
1743         return;
1744      }
1745
1746    if ((sd->screen_angle == 270) && (index > 0))
1747      {
1748         sd->last_drag[index].x = data->y;
1749         sd->last_drag[index].y = data->x;
1750         sd->last_drag[index].time = data->time;
1751         sd->last_drag[index].device = data->device;
1752      }
1753    else
1754      {
1755         sd->last_drag[index].x = data->x;
1756         sd->last_drag[index].y = data->y;
1757         sd->last_drag[index].time = data->time;
1758         sd->last_drag[index].device = data->device;
1759      }
1760 }
1761
1762 static void
1763 _smart_stop_all_timers(Smart_Data *sd)
1764 {
1765    _smart_stop_all_one_timers(sd);
1766    _smart_stop_all_two_timers(sd);
1767 }
1768
1769 static void
1770 _smart_stop_all_one_timers(Smart_Data *sd)
1771 {
1772    if (sd->press_timer) // remove sd->press_timer
1773      {
1774         ecore_timer_del(sd->press_timer);
1775         sd->press_timer = NULL;
1776      }
1777
1778    if (sd->long_press_timer) // remove long press timer
1779      {
1780         ecore_timer_del(sd->long_press_timer);
1781         sd->long_press_timer = NULL;
1782      }
1783
1784    if (sd->release_timer) // remove release timer
1785      {
1786         ecore_timer_del(sd->release_timer);
1787         sd->release_timer = NULL;
1788      }
1789
1790    if (sd->press_release_timer) // remove pressRelease timer
1791      {
1792         ecore_timer_del(sd->press_release_timer);
1793         sd->press_release_timer = NULL;
1794      }
1795 }
1796
1797 static void
1798 _smart_stop_all_two_timers(Smart_Data *sd)
1799 {
1800    if (sd->two_press_timer)
1801      {
1802         ecore_timer_del(sd->two_press_timer);
1803         sd->two_press_timer = NULL;
1804      }
1805
1806    if (sd->two_release_timer)
1807      {
1808         ecore_timer_del(sd->two_release_timer);
1809         sd->two_release_timer = NULL;
1810      }
1811
1812    if (sd->two_press_release_timer)
1813      {
1814         ecore_timer_del(sd->two_press_release_timer);
1815         sd->two_press_release_timer = NULL;
1816      }
1817 }
1818
1819 static void
1820 _smart_add(Evas_Object *obj)
1821 {
1822    Smart_Data *sd;
1823
1824    sd = calloc(1, sizeof(Smart_Data));
1825    if (!sd) return;
1826    memset((void *)sd, 0x00, sizeof(Smart_Data));
1827
1828    sd->smart_obj = obj;
1829
1830    // set default framerate
1831    ecore_animator_frametime_set(1.0 / DEFAULT_FRAMERATE);
1832
1833    evas_object_smart_data_set(obj, sd);
1834 }
1835
1836 static void
1837 _smart_del(Evas_Object *obj)
1838 {
1839    INTERNAL_ENTRY;
1840    if (sd)
1841      {
1842         if (sd->press_timer)
1843           ecore_timer_del(sd->press_timer);
1844
1845         if (sd->long_press_timer)
1846           ecore_timer_del(sd->long_press_timer);
1847
1848         if (sd->release_timer)
1849           ecore_timer_del(sd->release_timer);
1850
1851         if (sd->press_release_timer)
1852           ecore_timer_del(sd->press_release_timer);
1853
1854         if (sd->animator_move)
1855           ecore_animator_del(sd->animator_move);
1856
1857         if (sd->animator_flick)
1858           ecore_animator_del(sd->animator_flick);
1859
1860         if (sd->animator_two_move)
1861           ecore_animator_del(sd->animator_two_move);
1862
1863         if (sd->child_obj)
1864           {
1865              evas_object_event_callback_del(sd->child_obj, EVAS_CALLBACK_MOUSE_DOWN, _smart_mouse_down);
1866              evas_object_event_callback_del(sd->child_obj, EVAS_CALLBACK_MOUSE_UP, _smart_mouse_up);
1867              evas_object_event_callback_del(sd->child_obj, EVAS_CALLBACK_MOUSE_MOVE, _smart_mouse_move);
1868              evas_object_event_callback_del(sd->child_obj, EVAS_CALLBACK_MULTI_DOWN, _smart_multi_down);
1869              evas_object_event_callback_del(sd->child_obj, EVAS_CALLBACK_MULTI_UP, _smart_multi_up);
1870              evas_object_event_callback_del(sd->child_obj, EVAS_CALLBACK_MULTI_MOVE, _smart_multi_move);
1871           }
1872
1873         free(sd);
1874      }
1875 }
1876
1877 static void
1878 _smart_init(void)
1879 {
1880    if (_smart) return;
1881
1882    static const Evas_Smart_Class sc = 
1883      {
1884         SMART_NAME,
1885         EVAS_SMART_CLASS_VERSION,
1886         _smart_add,
1887         _smart_del,
1888         NULL,
1889         NULL,
1890         NULL,
1891         NULL,
1892         NULL,
1893         NULL,
1894         NULL,
1895         NULL,
1896         NULL,
1897         NULL,
1898         NULL,
1899         NULL,
1900         NULL,
1901         NULL
1902      };
1903    _smart = evas_smart_class_new(&sc);
1904 }