summaryrefslogtreecommitdiff
path: root/src/lib/elementary/elm_gesture_layer_extra_gestures.c
blob: b7bce1926cfefc28440e4d35a614245e7dc25c76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#include <Eina.h>
#include <Elementary.h>

//#define DEBUGON
#ifdef DEBUGON
# define gl_debug(x...) fprintf(stderr, __FILE__": " x)
#else
# define gl_debug(x...) do { } while (0)
#endif

struct _Func_Data
{
   EINA_INLIST;
   void                 *user_data; /**< Holds user data to CB (like sd) */
   Elm_Gesture_Event_Cb cb;
};
typedef struct _Func_Data Func_Data;

struct _Tap_Longpress_Info
{
   Evas_Object          *obj;
   Eina_Inlist          *cbs[ELM_GESTURE_STATE_ABORT + 1]; /**< Callback info (Func_Data) for states */
   void                 *data;

   Ecore_Timer          *timer_between_taps;
   unsigned int         nb_taps_on_single : 7;
   Eina_Bool            long_tap_started  : 1;
};

typedef struct _Tap_Longpress_Info Tap_Longpress_Info;

static Evas_Event_Flags
_cb_call(Tap_Longpress_Info *info, Elm_Gesture_State state, void *event_info)
{
   Evas_Event_Flags flags = EVAS_EVENT_FLAG_NONE;
   Func_Data *cb_info;
   EINA_INLIST_FOREACH(info->cbs[state], cb_info)
      flags |= cb_info->cb(cb_info->user_data, event_info);
   return flags;
}

static Evas_Event_Flags
_tap_long_single_tap_start_cb(void *data, void *event_info)
{
   Tap_Longpress_Info *info = data;
   Evas_Event_Flags flags = EVAS_EVENT_FLAG_NONE;
   if (!info->nb_taps_on_single)
     {
        gl_debug("\n%s\n", __FUNCTION__);
        _cb_call(info, ELM_GESTURE_STATE_START, event_info);
     }
   return flags;
}

static Evas_Event_Flags
_tap_long_single_tap_abort_cb(void *data, void *event_info EINA_UNUSED)
{
   Tap_Longpress_Info *info = data;
   Evas_Event_Flags flags = EVAS_EVENT_FLAG_NONE;
   if (!info->long_tap_started)
     {
        gl_debug("%s\n", __FUNCTION__);
        _cb_call(info, ELM_GESTURE_STATE_ABORT, NULL);
        info->nb_taps_on_single = 0;
     }
   return flags;
}

static Eina_Bool
_tap_long_timeout(void *data)
{
   gl_debug("%s\n", __FUNCTION__);
   Tap_Longpress_Info *info = data;
   _tap_long_single_tap_abort_cb(info, NULL);
   info->timer_between_taps = NULL;
   return ECORE_CALLBACK_CANCEL;
}

static Evas_Event_Flags
_tap_long_single_tap_end_cb(void *data, void *event_info)
{
   gl_debug("%s\n", __FUNCTION__);
   Tap_Longpress_Info *info = data;
   Evas_Event_Flags flags = EVAS_EVENT_FLAG_NONE;
   double timeout_between_taps = elm_gesture_layer_double_tap_timeout_get(info->obj);
   info->timer_between_taps = ecore_timer_add(timeout_between_taps,
                            _tap_long_timeout, info);
   info->nb_taps_on_single = ((Elm_Gesture_Taps_Info *)event_info)->n;
   return flags;
}

static Evas_Event_Flags
_tap_long_long_tap_start_cb(void *data, void *event_info EINA_UNUSED)
{
   Tap_Longpress_Info *info = data;
   Evas_Event_Flags flags = EVAS_EVENT_FLAG_NONE;
   if (info->nb_taps_on_single && info->timer_between_taps)
     {
        gl_debug("%s\n", __FUNCTION__);
        info->long_tap_started = EINA_TRUE;
        ecore_timer_del(info->timer_between_taps);
        info->timer_between_taps = NULL;
     }
   return flags;
}

static Evas_Event_Flags
_tap_long_long_tap_abort_cb(void *data, void *event_info EINA_UNUSED)
{
   Tap_Longpress_Info *info = data;
   Evas_Event_Flags flags = EVAS_EVENT_FLAG_NONE;
   if (info->long_tap_started)
     {
        gl_debug("%s\n", __FUNCTION__);
        _cb_call(info, ELM_GESTURE_STATE_ABORT, NULL);
        info->nb_taps_on_single = 0;
        info->long_tap_started = EINA_FALSE;
     }
   return flags;
}

static Evas_Event_Flags
_tap_long_long_tap_move_cb(void *data, void *event_info)
{
   Tap_Longpress_Info *info = data;
   Evas_Event_Flags flags = EVAS_EVENT_FLAG_NONE;
   if (info->long_tap_started)
     {
        if (((Elm_Gesture_Taps_Info *)event_info)->n != info->nb_taps_on_single)
          {
             _tap_long_long_tap_abort_cb(info, NULL);
          }
        else
          {
             gl_debug("%s\n", __FUNCTION__);
             _cb_call(info, ELM_GESTURE_STATE_MOVE, event_info);
          }
     }
   return flags;
}

static Evas_Event_Flags
_tap_long_long_tap_end_cb(void *data, void *event_info)
{
   Tap_Longpress_Info *info = data;
   Evas_Event_Flags flags = EVAS_EVENT_FLAG_NONE;
   if (info->long_tap_started)
     {
        gl_debug("%s\n", __FUNCTION__);
        _cb_call(info, ELM_GESTURE_STATE_END, event_info);
        info->long_tap_started = EINA_FALSE;
        info->nb_taps_on_single = 0;
     }
   return flags;
}

static void
_object_delete(void *data EINA_UNUSED, Evas *e EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   Tap_Longpress_Info *info = evas_object_data_get(obj, "Tap-Longpress");
   if (info)
     {
        Eina_Inlist *itr;
        Func_Data *cb_info;
        int state;
        for (state = ELM_GESTURE_STATE_START; state <= ELM_GESTURE_STATE_ABORT; state++)
          {
             EINA_INLIST_FOREACH_SAFE(info->cbs[state], itr, cb_info)
               {
                  info->cbs[state] = eina_inlist_remove(
                        info->cbs[state], EINA_INLIST_GET(cb_info));
                  free(cb_info);
               }
          }
        elm_gesture_layer_cb_del(obj, ELM_GESTURE_N_TAPS, ELM_GESTURE_STATE_START, _tap_long_single_tap_start_cb, info);
        elm_gesture_layer_cb_del(obj, ELM_GESTURE_N_TAPS, ELM_GESTURE_STATE_ABORT, _tap_long_single_tap_abort_cb, info);
        elm_gesture_layer_cb_del(obj, ELM_GESTURE_N_TAPS, ELM_GESTURE_STATE_END, _tap_long_single_tap_end_cb, info);
        elm_gesture_layer_cb_del(obj, ELM_GESTURE_N_LONG_TAPS, ELM_GESTURE_STATE_START, _tap_long_long_tap_start_cb, info);
        elm_gesture_layer_cb_del(obj, ELM_GESTURE_N_LONG_TAPS, ELM_GESTURE_STATE_MOVE, _tap_long_long_tap_move_cb, info);
        elm_gesture_layer_cb_del(obj, ELM_GESTURE_N_LONG_TAPS, ELM_GESTURE_STATE_ABORT, _tap_long_long_tap_abort_cb, info);
        elm_gesture_layer_cb_del(obj, ELM_GESTURE_N_LONG_TAPS, ELM_GESTURE_STATE_END, _tap_long_long_tap_end_cb, info);
        evas_object_data_del(obj, "Tap-Longpress");
        free(info);
     }
}

EAPI void elm_gesture_layer_tap_longpress_cb_add(Evas_Object *obj, Elm_Gesture_State state, Elm_Gesture_Event_Cb cb, void *data)
{
   Tap_Longpress_Info *info = evas_object_data_get(obj, "Tap-Longpress");
   if (!info)
     {
        info = calloc(1, sizeof(*info));
        if (!info) return;

        info->obj = obj;
        elm_gesture_layer_cb_add(obj, ELM_GESTURE_N_TAPS, ELM_GESTURE_STATE_START, _tap_long_single_tap_start_cb, info);
        elm_gesture_layer_cb_add(obj, ELM_GESTURE_N_TAPS, ELM_GESTURE_STATE_ABORT, _tap_long_single_tap_abort_cb, info);
        elm_gesture_layer_cb_add(obj, ELM_GESTURE_N_TAPS, ELM_GESTURE_STATE_END, _tap_long_single_tap_end_cb, info);
        elm_gesture_layer_cb_add(obj, ELM_GESTURE_N_LONG_TAPS, ELM_GESTURE_STATE_START, _tap_long_long_tap_start_cb, info);
        elm_gesture_layer_cb_add(obj, ELM_GESTURE_N_LONG_TAPS, ELM_GESTURE_STATE_MOVE, _tap_long_long_tap_move_cb, info);
        elm_gesture_layer_cb_add(obj, ELM_GESTURE_N_LONG_TAPS, ELM_GESTURE_STATE_ABORT, _tap_long_long_tap_abort_cb, info);
        elm_gesture_layer_cb_add(obj, ELM_GESTURE_N_LONG_TAPS, ELM_GESTURE_STATE_END, _tap_long_long_tap_end_cb, info);
        evas_object_data_set(obj, "Tap-Longpress", info);
        evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL, _object_delete, NULL);
     }

   Func_Data *cb_info = calloc(1, sizeof(*cb_info));
   if (!cb_info) return;
   cb_info->cb = cb;
   cb_info->user_data = data;
   info->cbs[state] = eina_inlist_append(info->cbs[state],
         EINA_INLIST_GET(cb_info));
}

EAPI void elm_gesture_layer_tap_longpress_cb_del(Evas_Object *obj, Elm_Gesture_State state, Elm_Gesture_Event_Cb cb, void *data)
{
   Tap_Longpress_Info *info = evas_object_data_get(obj, "Tap-Longpress");
   if (!info) return;

   Eina_Inlist *itr;
   Func_Data *cb_info;
   EINA_INLIST_FOREACH_SAFE(info->cbs[state], itr, cb_info)
     {
        if (cb_info->cb == cb && cb_info->user_data == data)
          {
             info->cbs[state] = eina_inlist_remove(
                   info->cbs[state], EINA_INLIST_GET(cb_info));
             free(cb_info);
             break;
          }
     }
   if (!info->cbs[ELM_GESTURE_STATE_START] &&
         !info->cbs[ELM_GESTURE_STATE_MOVE] &&
         !info->cbs[ELM_GESTURE_STATE_END] &&
         !info->cbs[ELM_GESTURE_STATE_ABORT])
     {
        _object_delete(NULL, NULL, obj, NULL);
     }
}