summaryrefslogtreecommitdiff
path: root/src/modules/gesture_recognition/e_mod_main.c
blob: 2ebbebecc10cbe9fbea9ecafad7ba632b9461187 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <e.h>
#include <Eina.h>
#include <libinput.h>
#include <grp.h>
#include <sys/types.h>
#include <pwd.h>

E_API E_Module_Api e_modapi =
   {
      E_MODULE_API_VERSION,
      "Gesture Recognition"
   };

static struct libinput *gesture_recognition_ctx;
static Ecore_Fd_Handler *fd_listener;

static int open_restricted(const char *path, int flags, void *user_data EINA_UNUSED)
{
        int fd = open(path, flags);
        return fd < 0 ? -errno : fd;
}

static void close_restricted(int fd, void *user_data EINA_UNUSED)
{
        close(fd);
}

static const struct libinput_interface interface = {
        .open_restricted = open_restricted,
        .close_restricted = close_restricted,
};

static void
_find_all_touch_input_devices(const char *path, struct libinput *li)
{
   Eina_File_Direct_Info *info;
   Eina_Iterator *input_devies = eina_file_direct_ls(path);

   EINA_ITERATOR_FOREACH(input_devies, info)
     {
        struct libinput_device *dev = libinput_path_add_device(li, info->path);

        if (!dev) continue;

        if (!libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_GESTURE))
          {
             libinput_path_remove_device(dev);
          }
     }
}

static Eina_Hash *active_gestures;

typedef struct {
   Eina_Vector2 pos;
   unsigned int fingers;
   struct {
     Evas_Object *visuals, *win;
   } visuals;
} Swipe_Stats;


static Swipe_Stats*
_find_swipe_gesture_recognizition(struct libinput_device *dev)
{
   Swipe_Stats *stats = eina_hash_find(active_gestures, dev);

   return stats;
}

static Swipe_Stats*
_start_swipe_gesture_recognizition(struct libinput_device *dev)
{
   Swipe_Stats *stats = _find_swipe_gesture_recognizition(dev);

   if (stats)
     eina_hash_del_by_key(active_gestures, dev);

   stats = calloc(1, sizeof(Swipe_Stats));
   EINA_SAFETY_ON_NULL_RETURN_VAL(stats, NULL);

   if (e_bindings_swipe_available())
     {
        stats->visuals.win =  elm_notify_add(e_comp->elm);
        elm_notify_align_set(stats->visuals.win, 0.5, 0.5);
        elm_object_tree_focus_allow_set(stats->visuals.win, EINA_FALSE);
        evas_object_layer_set(stats->visuals.win, E_LAYER_CLIENT_PRIO);
        evas_object_show(stats->visuals.win);

        stats->visuals.visuals = elm_progressbar_add(stats->visuals.win);
        elm_object_text_set(stats->visuals.visuals, "Progress of visuals");
        evas_object_size_hint_min_set(stats->visuals.visuals, 300, 50);
        evas_object_show(stats->visuals.visuals);
        elm_object_content_set(stats->visuals.win, stats->visuals.visuals);
     }


   eina_hash_add(active_gestures, dev, stats);

   return stats;
}

static void
_end_swipe_gesture_recognizition(struct libinput_device *dev)
{
   eina_hash_del_by_key(active_gestures, dev);
}

static double
_config_angle(Eina_Vector2 pos)
{
   double res = atan(pos.y/pos.x);

   if (res < 0) res += M_PI;
   if (pos.y < 0) res += M_PI;
   return res;
}

static Eina_Bool
_cb_input_dispatch(void *data, Ecore_Fd_Handler *hdlr EINA_UNUSED)
{
   struct libinput *li = data;
   struct libinput_event *event;

   if (libinput_dispatch(li) != 0)
     printf("Failed to dispatch libinput events");

   while((event = libinput_get_event(li)))
     {
        E_Bindings_Swipe_Live_Update live_update = e_bindings_swipe_live_update_hook_get();

        enum libinput_event_type type = libinput_event_get_type(event);
             struct libinput_device *dev = libinput_event_get_device(event);
        if (type == LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN)
          {
             struct libinput_event_gesture *gesture = libinput_event_get_gesture_event(event);

             Swipe_Stats *stats = _start_swipe_gesture_recognizition(dev);
             stats->fingers = libinput_event_gesture_get_finger_count(gesture);
             stats->pos.x = stats->pos.y = 0;
          }
        else if (type == LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE)
          {
             struct libinput_event_gesture *gesture = libinput_event_get_gesture_event(event);
             Swipe_Stats *stats = _find_swipe_gesture_recognizition(dev);

             stats->pos.x += libinput_event_gesture_get_dx(gesture);
             stats->pos.y += libinput_event_gesture_get_dy(gesture);
             if (live_update)
               {
                  live_update(e_bindings_swipe_live_update_hook_data_get(), EINA_FALSE, _config_angle(stats->pos), eina_vector2_length_get(&stats->pos), 0.8, stats->fingers);
               }
             else if (stats->visuals.win)
               {
                  Eina_Inarray *res = e_bindings_swipe_find_candidates(E_BINDING_CONTEXT_NONE, _config_angle (stats->pos), eina_vector2_length_get(&stats->pos), stats->fingers);
                  E_Binding_Swipe_Candidate *itr;
                  double total = 0.0f;
                  unsigned int len = 0;

                  EINA_INARRAY_FOREACH(res, itr)
                    {
                       total += itr->acceptance;
                       len ++;
                    }

                  if (len > 0)
                    {
                       char text_buffer[1000];

                       snprintf(text_buffer, sizeof(text_buffer), "%d gestures possible", len);
                       elm_progressbar_value_set(stats->visuals.visuals, total/len);
                       elm_object_text_set(stats->visuals.visuals, text_buffer);
                    }
                  else
                    {
                       elm_progressbar_value_set(stats->visuals.visuals, 0.0f);
                       elm_object_text_set(stats->visuals.visuals, "No gesture found");
                    }

                  eina_inarray_free(res);
               }
          }
        else if (type == LIBINPUT_EVENT_GESTURE_SWIPE_END)
          {
             Swipe_Stats *stats = _find_swipe_gesture_recognizition(dev);

             if (live_update)
               live_update(e_bindings_swipe_live_update_hook_data_get(), EINA_TRUE, _config_angle(stats->pos), eina_vector2_length_get(&stats->pos), 0.8, stats->fingers);
             else
               e_bindings_swipe_handle(E_BINDING_CONTEXT_NONE, NULL, _config_angle(stats->pos), eina_vector2_length_get(&stats->pos), stats->fingers);

             _end_swipe_gesture_recognizition(dev);
          }
        libinput_event_destroy(event);
     }
   return EINA_TRUE;
}

static void
_setup_libinput(void)
{
   gesture_recognition_ctx = libinput_path_create_context(&interface, NULL);

   _find_all_touch_input_devices("/dev/input/", gesture_recognition_ctx);

   fd_listener = ecore_main_fd_handler_add(libinput_get_fd(gesture_recognition_ctx), ECORE_FD_READ, _cb_input_dispatch, gesture_recognition_ctx, NULL, NULL);
}


static void
_tear_down_libinput(void)
{
   ecore_main_fd_handler_del(fd_listener);
   libinput_unref(gesture_recognition_ctx);
}

static Eina_Bool
_user_part_of_input(void)
{
   uid_t user = getuid();
   struct passwd *user_pw = getpwuid(user);
   gid_t *gids = NULL;
   int number_of_groups = 0;
   struct group *input_group = getgrnam("input");

   EINA_SAFETY_ON_NULL_RETURN_VAL(user_pw, EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(input_group, EINA_FALSE);

   if (getgrouplist(user_pw->pw_name, getgid(), NULL, &number_of_groups) != -1)
     {
        ERR("Failed to enumerate groups of user");
        return EINA_FALSE;
     }
   number_of_groups ++;
   gids = alloca((number_of_groups) * sizeof(gid_t));
   if (getgrouplist(user_pw->pw_name, getgid(), gids, &number_of_groups) == -1)
     {
        ERR("Failed to get groups of user");
        return EINA_FALSE;
     }

   for (int i = 0; i < number_of_groups; ++i)
     {
        if (gids[i] == input_group->gr_gid)
          return EINA_TRUE;
     }
   return EINA_FALSE;
}

static void
_stats_free(void *ptr)
{
   Swipe_Stats *stats = ptr;

   evas_object_del(stats->visuals.win);
   free(stats);
}

E_API int
e_modapi_init(E_Module *m EINA_UNUSED)
{
   if (!_user_part_of_input())
     {
        e_module_dialog_show(m, "Gesture Recognition", "Your user is not part of the input group, libinput cannot be used.");

        return 0;
     }
   active_gestures = eina_hash_pointer_new(_stats_free);
   _setup_libinput();

   return 1;
}

E_API int
e_modapi_shutdown(E_Module *m EINA_UNUSED)
{
   _tear_down_libinput();
   return 1;
}

E_API int
e_modapi_save(E_Module *m EINA_UNUSED)
{

   return 1;
}