Elm glayer: Set mimimun momentum for ABORT
[framework/uifw/elementary.git] / src / lib / elm_gesture_layer.c
index bff601b..1cfd219 100644 (file)
@@ -8,6 +8,7 @@
 #define ELM_GESTURE_NEGATIVE_ANGLE (-1.0) /* Magic number */
 #define ELM_GESTURE_MOMENTUM_TIMEOUT 50
 #define ELM_GESTURE_MULTI_TIMEOUT 50
+#define ELM_GESTURE_MINIMUM_MOMENTUM 0.001
 
 /* Some Trigo values */
 #define RAD_90DEG  M_PI_2
@@ -174,7 +175,6 @@ struct _Momentum_Type
    unsigned int t_st_y;  /* Time start on Y */
    unsigned int t_end;   /* Time end        */
    unsigned int t_up; /* Recent up event time */
-   int n_fingers;
    int xdir, ydir;
 };
 typedef struct _Momentum_Type Momentum_Type;
@@ -251,7 +251,6 @@ struct _Widget_Data
    Ecore_Timer *dbl_timeout; /* When this expires, dbl click/taps ABORTed  */
    Eina_List *pending; /* List of devices need to refeed *UP event */
    Eina_List *touched;  /* Information  of touched devices   */
-   Eina_List *recent_device_event;  /* Information  of recent pe event of each device */
 
    Eina_Bool repeat_events : 1;
 };
@@ -330,69 +329,18 @@ _add_touched_device(Eina_List *list, Pointer_Event *pe)
         return list;
      }
 
-   p = malloc(sizeof(Pointer_Event));
-   memcpy(p, pe, sizeof(Pointer_Event)); /* Freed in _remove_touched_device() */
-   return eina_list_append(list, p);
-}
-/* END   - Functions to manage touched-device list */
-
-/* START - Functions to manage recent device event list */
-/* This list holds the recent-event for each device     */
-/* it will hold a single recent-event for each device   */
-/* We are using those PE events of this list to         */
-/* send them later to test funcs to restart gestures    */
-/* We only keep DOWN, MOVE events in this list.         */
-/* So when no touch this list is empty.                 */
-/**
- * @internal
- *
- * Recoed Pointer Event in touched device list
- * Note: This fuction allocates memory for PE event
- * This memory is released here when device untouched
- * or in cleanup.
- * @param list Pointer to touched device list.
- * @param Pointer_Event Pointer to PE.
- *
- * @ingroup Elm_Gesture_Layer
- */
-static Eina_List *
-_add_recent_device_event(Eina_List *list, Pointer_Event *pe)
-{
-   void *data = eina_list_search_sorted(list, compare_device, pe);
-   Eina_List *l = list;
-   Pointer_Event *p = NULL;
-   if(data)   /* First remove recent event for this device */
-     l = eina_list_remove(list, data);
-
-
-   switch(pe->event_type)
-     {
-      case EVAS_CALLBACK_MOUSE_DOWN:
-      case EVAS_CALLBACK_MOUSE_MOVE:
-      case EVAS_CALLBACK_MULTI_DOWN:
-      case EVAS_CALLBACK_MULTI_MOVE:
-         p = malloc(sizeof(Pointer_Event));
-         memcpy(p, pe, sizeof(Pointer_Event)); /* Freed in here or on cleanup */
-         l =  eina_list_sorted_insert(l, compare_device, p);
-         break;
-
-      /* Kept those cases for referance */
-      case EVAS_CALLBACK_MOUSE_IN:
-      case EVAS_CALLBACK_MOUSE_OUT:
-      case EVAS_CALLBACK_MOUSE_WHEEL:
-      case EVAS_CALLBACK_MOUSE_UP:
-      case EVAS_CALLBACK_MULTI_UP:
-      case EVAS_CALLBACK_KEY_DOWN:
-      case EVAS_CALLBACK_KEY_UP:
-         break;
-
-      default:
-         return l;
+   if ((pe->event_type == EVAS_CALLBACK_MOUSE_DOWN) ||
+         (pe->event_type == EVAS_CALLBACK_MULTI_DOWN))
+     {  /* Add touched device on DOWN event only */
+        p = malloc(sizeof(Pointer_Event));
+        /* Freed in _remove_touched_device()    */
+        memcpy(p, pe, sizeof(Pointer_Event));
+        return eina_list_append(list, p);
      }
 
-   return l;
+   return list;
 }
-/* END   - Functions to manage recent device event list */
+/* END   - Functions to manage touched-device list */
 
 /**
  * @internal
@@ -1016,16 +964,14 @@ _event_history_clear(Evas_Object *obj)
 
              if (pending)
                {
-               wd->pending = eina_list_remove_list(wd->pending, pending);
-               int device = ELM_MOUSE_DEVICE;
-               if (wd->event_history_list->event_type == EVAS_CALLBACK_MULTI_UP)
-                 device = ((Evas_Event_Multi_Up *)
-                       (wd->event_history_list->event))->device;
+                  wd->pending = eina_list_remove_list(wd->pending, pending);
                }
              else
-               wd->pending = _add_device_pending(wd->pending,
-                     wd->event_history_list->event,
-                     wd->event_history_list->event_type);
+               {
+                  wd->pending = _add_device_pending(wd->pending,
+                        wd->event_history_list->event,
+                        wd->event_history_list->event_type);
+               }
           }
 
         free(wd->event_history_list->event);
@@ -1117,9 +1063,6 @@ _del_hook(Evas_Object *obj)
    EINA_LIST_FREE(wd->touched, data)
       free(data);
 
-   EINA_LIST_FREE(wd->recent_device_event, data)
-      free(data);
-
    if (!elm_widget_disabled_get(obj))
      _unregister_callbacks(obj);
 
@@ -1158,8 +1101,8 @@ compare_match_fingers(const void *data1, const void *data2)
 
 static int
 compare_pe_device(const void *data1, const void *data2)
-{  /* Compare coords of first item in list to cur coords */
-   const Pointer_Event *pe1 = eina_list_data_get(eina_list_last(data1));
+{  /* Compare device of first item in list to our pe device */
+   const Pointer_Event *pe1 = eina_list_data_get(data1);
    const Pointer_Event *pe2 = data2;
 
    /* Only match if last was a down event */
@@ -1328,7 +1271,19 @@ _tap_gesture_start(Widget_Data *wd, Pointer_Event *pe,
      {
       case EVAS_CALLBACK_MULTI_DOWN:
       case EVAS_CALLBACK_MOUSE_DOWN:
+         /* Check if got tap on same cord was tapped before */
          pe_list = eina_list_search_unsorted(st->l, compare_match_fingers, pe);
+
+         if ((!pe_list) &&
+               eina_list_search_unsorted(st->l, compare_pe_device, pe))
+           {  /* This device was touched in other cord before completion */
+              ev_flag = _set_state(gesture, ELM_GESTURE_STATE_ABORT,
+                    &st->info, EINA_FALSE);
+              consume_event(wd, event_info, event_type, ev_flag);
+
+              return EINA_FALSE;
+           }
+
          pe_list = _record_pointer_event(st, pe_list, pe, wd, event_info, event_type);
          if ((pe->device == 0) && (eina_list_count(pe_list) == 1))
            {  /* This is the first mouse down we got */
@@ -1473,8 +1428,8 @@ _compute_taps_center(Long_Tap_Type *st,
  */
 static void
 _n_long_tap_test(Evas_Object *obj, Pointer_Event *pe,
-      void *event_info, Evas_Callback_Type event_type,
-      Elm_Gesture_Types g_type)
+                 void *event_info, Evas_Callback_Type event_type,
+                 Elm_Gesture_Types g_type)
 {  /* Here we fill Recent_Taps struct and fire-up click/tap timers */
    Widget_Data *wd = elm_widget_data_get(obj);
    if (!wd) return;
@@ -1497,91 +1452,91 @@ _n_long_tap_test(Evas_Object *obj, Pointer_Event *pe,
      {
       case EVAS_CALLBACK_MULTI_DOWN:
       case EVAS_CALLBACK_MOUSE_DOWN:
-         st->touched = _add_touched_device(st->touched, pe);
-         st->info.n = eina_list_count(st->touched);
-         if (st->info.n > st->max_touched)
-           st->max_touched = st->info.n;
-         else
-           {  /* User removed finger from touch, then put back - ABORT */
-              if ((gesture->state == ELM_GESTURE_STATE_START) ||
-                    (gesture->state == ELM_GESTURE_STATE_MOVE))
-                {
-                   ev_flag =_set_state(gesture, ELM_GESTURE_STATE_ABORT,
-                         &st->info, EINA_FALSE);
-                   consume_event(wd, event_info, event_type, ev_flag);
-                }
-           }
+        st->touched = _add_touched_device(st->touched, pe);
+        st->info.n = eina_list_count(st->touched);
+        if (st->info.n > st->max_touched)
+          st->max_touched = st->info.n;
+        else
+          {  /* User removed finger from touch, then put back - ABORT */
+             if ((gesture->state == ELM_GESTURE_STATE_START) ||
+                 (gesture->state == ELM_GESTURE_STATE_MOVE))
+               {
+                  ev_flag =_set_state(gesture, ELM_GESTURE_STATE_ABORT,
+                                      &st->info, EINA_FALSE);
+                  consume_event(wd, event_info, event_type, ev_flag);
+               }
+          }
 
-         if ((pe->device == 0) && (eina_list_count(st->touched) == 1))
-           {  /* This is the first mouse down we got */
-              st->info.timestamp = pe->timestamp;
+        if ((pe->device == 0) && (eina_list_count(st->touched) == 1))
+          {  /* This is the first mouse down we got */
+             st->info.timestamp = pe->timestamp;
 
-              /* To test long tap */
-              /* When this timer expires, gesture STARTED */
-              if (!st->timeout)
-                st->timeout = ecore_timer_add(wd->long_tap_start_timeout,
-                      _long_tap_timeout, gesture);
-           }
+             /* To test long tap */
+             /* When this timer expires, gesture STARTED */
+             if (!st->timeout)
+               st->timeout = ecore_timer_add(wd->long_tap_start_timeout,
+                                             _long_tap_timeout, gesture);
+          }
 
-         consume_event(wd, event_info, event_type, ev_flag);
-         _compute_taps_center(st, &st->info.x, &st->info.y, pe);
-         st->center_x = st->info.x;
-         st->center_y = st->info.y;
-         break;
+        consume_event(wd, event_info, event_type, ev_flag);
+        _compute_taps_center(st, &st->info.x, &st->info.y, pe);
+        st->center_x = st->info.x;
+        st->center_y = st->info.y;
+        break;
 
       case EVAS_CALLBACK_MULTI_UP:
       case EVAS_CALLBACK_MOUSE_UP:
-         st->touched = _remove_touched_device(st->touched, pe);
-         _compute_taps_center(st, &st->center_x, &st->center_y, pe);
-         if (st->info.n &&
-               ((gesture->state == ELM_GESTURE_STATE_START) ||
+        st->touched = _remove_touched_device(st->touched, pe);
+        _compute_taps_center(st, &st->center_x, &st->center_y, pe);
+        if (st->info.n &&
+            ((gesture->state == ELM_GESTURE_STATE_START) ||
                 (gesture->state == ELM_GESTURE_STATE_MOVE)))
-           {  /* Report END only for gesture that STARTed */
-              if (eina_list_count(st->touched) == 0)
-                {  /* Report END only at last release event */
-                   ev_flag =_set_state(gesture, ELM_GESTURE_STATE_END,
-                         &st->info, EINA_FALSE);
-                   consume_event(wd, event_info, event_type, ev_flag);
-                }
-           }
-         else
-           {  /* Stop test, user lifts finger before long-start */
-              if (st->timeout) ecore_timer_del(st->timeout);
-              st->timeout = NULL;
-              ev_flag =_set_state(gesture, ELM_GESTURE_STATE_ABORT,
-                    &st->info, EINA_FALSE);
-              consume_event(wd, event_info, event_type, ev_flag);
-           }
+          {  /* Report END only for gesture that STARTed */
+             if (eina_list_count(st->touched) == 0)
+               {  /* Report END only at last release event */
+                  ev_flag =_set_state(gesture, ELM_GESTURE_STATE_END,
+                                      &st->info, EINA_FALSE);
+                  consume_event(wd, event_info, event_type, ev_flag);
+               }
+          }
+        else
+          {  /* Stop test, user lifts finger before long-start */
+             if (st->timeout) ecore_timer_del(st->timeout);
+             st->timeout = NULL;
+             ev_flag =_set_state(gesture, ELM_GESTURE_STATE_ABORT,
+                                 &st->info, EINA_FALSE);
+             consume_event(wd, event_info, event_type, ev_flag);
+          }
 
-         break;
+        break;
 
       case EVAS_CALLBACK_MULTI_MOVE:
       case EVAS_CALLBACK_MOUSE_MOVE:
-         if(st->info.n &&
-               ((gesture->state == ELM_GESTURE_STATE_START) ||
-                (gesture->state == ELM_GESTURE_STATE_MOVE)))
-           {  /* Report MOVE only if STARTED */
-              Evas_Coord x;
-              Evas_Coord y;
-              Elm_Gesture_State state_to_report = ELM_GESTURE_STATE_MOVE;
-
-              _compute_taps_center(st, &x, &y, pe);
-              /* ABORT if user moved fingers out of tap area */
+        if(st->info.n &&
+           ((gesture->state == ELM_GESTURE_STATE_START) ||
+               (gesture->state == ELM_GESTURE_STATE_MOVE)))
+          {  /* Report MOVE only if STARTED */
+             Evas_Coord x = 0;
+             Evas_Coord y = 0;
+             Elm_Gesture_State state_to_report = ELM_GESTURE_STATE_MOVE;
+
+             _compute_taps_center(st, &x, &y, pe);
+             /* ABORT if user moved fingers out of tap area */
 #if defined(DEBUG_GESTURE_LAYER)
-              printf("%s x,y=(%d,%d) st->info.x,st->info.y=(%d,%d)\n",__func__,x,y,st->info.x,st->info.y);
+             printf("%s x,y=(%d,%d) st->info.x,st->info.y=(%d,%d)\n",__func__,x,y,st->info.x,st->info.y);
 #endif
-              if (!_inside(x, y, st->center_x, st->center_y))
-                state_to_report = ELM_GESTURE_STATE_ABORT;
+             if (!_inside(x, y, st->center_x, st->center_y))
+               state_to_report = ELM_GESTURE_STATE_ABORT;
 
-              /* Report MOVE if gesture started */
-              ev_flag = _set_state(gesture, state_to_report,
-                    &st->info, EINA_TRUE);
-              consume_event(wd, event_info, event_type, ev_flag);
-           }
-         break;
+             /* Report MOVE if gesture started */
+             ev_flag = _set_state(gesture, state_to_report,
+                                  &st->info, EINA_TRUE);
+             consume_event(wd, event_info, event_type, ev_flag);
+          }
+        break;
 
       default:
-         return;
+        return;
      }
 }
 
@@ -1762,6 +1717,15 @@ _momentum_test(Evas_Object *obj, Pointer_Event *pe,
    Gesture_Info *gesture = wd->gesture[g_type];
    if (!gesture ) return;
 
+   /* When continues enable = TRUE a gesture may START on MOVE event */
+   /* We don't allow this to happen with the if-statement below.     */
+   /* When continues enable = FALSE a gesture may START on DOWN only */
+   /* Therefor it would NOT start on MOVE event.                     */
+   /* NOTE that touched list is updated AFTER this function returns  */
+   /* so (count == 0) when we get here on first touch on surface.    */
+   if ((wd->glayer_continues_enable) && (!eina_list_count(wd->touched)))
+     return; /* Got move on mouse-over move */
+
    Momentum_Type *st = gesture->data;
    Elm_Gesture_State state_to_report = ELM_GESTURE_STATE_MOVE;
    if (!st)
@@ -1777,8 +1741,8 @@ _momentum_test(Evas_Object *obj, Pointer_Event *pe,
    /* First make avarage of all touched devices to determine center point */
    Eina_List *l;
    Pointer_Event *p;
-   Pointer_Event pe_local = *pe;  /* Copy pe event info to local */
-   int cnt = 1;    /* We start counter counting current pe event */
+   Pointer_Event pe_local = *pe;           /* Copy pe event info to local */
+   unsigned int cnt = 1;    /* We start counter counting current pe event */
    EINA_LIST_FOREACH(wd->touched, l, p)
       if (p->device != pe_local.device)
         {
@@ -1793,10 +1757,11 @@ _momentum_test(Evas_Object *obj, Pointer_Event *pe,
    pe_local.y /= cnt;
 
    /* If user added finger - reset gesture */
-   if ((st->n_fingers) && (st->n_fingers < cnt))
+   if ((st->info.n) && (st->info.n < cnt))
      state_to_report = ELM_GESTURE_STATE_ABORT;
 
-   st->n_fingers = cnt;
+   if (st->info.n < cnt)
+     st->info.n = cnt;
 
    Evas_Event_Flags ev_flag = EVAS_EVENT_FLAG_NONE;
    switch (event_type)
@@ -1896,7 +1861,13 @@ _momentum_test(Evas_Object *obj, Pointer_Event *pe,
          _set_momentum(&st->info, st->line_st.x, st->line_st.y, pe_local.x, pe_local.y,
                st->t_st_x, st->t_st_y, pe_local.timestamp);
 
-         ev_flag = _set_state(gesture, ELM_GESTURE_STATE_END, &st->info,
+         if ((fabs(st->info.mx) > ELM_GESTURE_MINIMUM_MOMENTUM) ||
+               (fabs(st->info.my) > ELM_GESTURE_MINIMUM_MOMENTUM))
+           state_to_report = ELM_GESTURE_STATE_END;
+         else
+           state_to_report = ELM_GESTURE_STATE_ABORT;
+
+         ev_flag = _set_state(gesture, state_to_report, &st->info,
                EINA_FALSE);
          consume_event(wd, event_info, event_type, ev_flag);
          return;
@@ -2009,6 +1980,15 @@ _n_line_test(Evas_Object *obj, Pointer_Event *pe, void *event_info,
    Gesture_Info *gesture = wd->gesture[g_type];
    if (!gesture ) return;
 
+   /* When continues enable = TRUE a gesture may START on MOVE event */
+   /* We don't allow this to happen with the if-statement below.     */
+   /* When continues enable = FALSE a gesture may START on DOWN only */
+   /* Therefor it would NOT start on MOVE event.                     */
+   /* NOTE that touched list is updated AFTER this function returns  */
+   /* so (count == 0) when we get here on first touch on surface.    */
+   if ((wd->glayer_continues_enable) && (!eina_list_count(wd->touched)))
+     return; /* Got move on mouse-over move */
+
    Line_Type *st = gesture->data;
    if (!st)
      {
@@ -2153,7 +2133,7 @@ _n_line_test(Evas_Object *obj, Pointer_Event *pe, void *event_info,
           }
      }
 
-   st->info.n = started;
+   st->info.momentum.n = started;
 
 
    if (ended &&
@@ -2389,7 +2369,6 @@ get_finger_gap_length(Evas_Coord x1, Evas_Coord y1, Evas_Coord x2,
  *
  * @ingroup Elm_Gesture_Layer
  */
-/* FIXME change float to double */
 static double
 compute_zoom(Zoom_Type *st, Evas_Coord x1, Evas_Coord y1, unsigned int tm1,
       Evas_Coord x2, Evas_Coord y2, unsigned int tm2, double zoom_finger_factor)
@@ -3011,8 +2990,18 @@ void continues_gestures_restart(void *data, Eina_Bool states_reset)
 
    /* Run through events to restart gestures */
    Gesture_Info *g;
-   Eina_Bool n_lines, n_flicks, zoom, rotate;
+   Eina_Bool n_momentum, n_lines, n_flicks, zoom, rotate;
    /* We turn-on flag for finished, aborted, not-started gestures */
+   g = wd->gesture[ELM_GESTURE_MOMENTUM];
+   n_momentum = (g) ? ((states_reset) | ((g->state != ELM_GESTURE_STATE_START)
+         && (g->state != ELM_GESTURE_STATE_MOVE))) : EINA_FALSE;
+   if (n_momentum)
+     {
+        _momentum_test_reset(wd->gesture[ELM_GESTURE_MOMENTUM]);
+        _set_state(g, ELM_GESTURE_STATE_UNDEFINED, NULL, EINA_FALSE);
+        SET_TEST_BIT(g);
+     }
+
    g = wd->gesture[ELM_GESTURE_N_LINES];
    n_lines = (g) ? ((states_reset) | ((g->state != ELM_GESTURE_STATE_START)
          && (g->state != ELM_GESTURE_STATE_MOVE))) : EINA_FALSE;
@@ -3141,9 +3130,6 @@ _event_process(void *data, Evas_Object *obj __UNUSED__,
           }
      }
 
-   /* Log event to restart gestures */
-   wd->recent_device_event = _add_recent_device_event(wd->recent_device_event, &_pe);
-
    /* we maintain list of touched devices              */
    /* We also use move to track current device x.y pos */
    if ((event_type == EVAS_CALLBACK_MOUSE_DOWN) ||