summaryrefslogtreecommitdiff
path: root/src/lib/ecore_drm/ecore_drm_inputs.c
blob: 544c3b262cc1a5e170b3f6437248667af76915a7 (plain) (blame)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "ecore_drm_private.h"

typedef struct _Ecore_Drm_Device_Open_Data Ecore_Drm_Device_Open_Data;
struct _Ecore_Drm_Device_Open_Data
{
   Ecore_Drm_Seat *seat;
   const char *node;
};

/* local functions */
static int 
_device_flags_set(int fd)
{
   int ret, fl;
   /* char name[256] = "unknown"; */

   if (fd < 0)
     {
        ERR("Failed to take device");
        return -1;
     }

   if ((fl = fcntl(fd, F_GETFL)) < 0)
     {
        ERR("Failed to get file flags: %m");
        goto flag_err;
     }

   fl = (O_RDWR | O_NONBLOCK);

   if ((ret = fcntl(fd, F_SETFL, fl)) < 0)
     {
        ERR("Failed to set file flags: %m");
        goto flag_err;
     }

   if ((fl = fcntl(fd, F_GETFD)) < 0)
     {
        ERR("Failed to get file fd: %m");
        goto flag_err;
     }

   fl &= ~FD_CLOEXEC;

   if ((ret = fcntl(fd, F_SETFD, fl)) < 0)
     {
        ERR("Failed to set file fds: %m");
        goto flag_err;
     }

   /* if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) */
   /*   { */
   /*      ERR("Could not get device name: %m"); */
   /*      goto flag_err; */
   /*   } */
   /* else */
   /*   { */
   /*      name[sizeof(name) - 1] = '\0'; */
   /*      DBG("%s Opened", name); */
   /*   } */

   return fd;

flag_err:
   close(fd);
   return -1;
}

static void 
_cb_device_opened(void *data, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
   Ecore_Drm_Device_Open_Data *d;
   Ecore_Drm_Evdev *edev;
   Eina_Bool b = EINA_FALSE;
   const char *errname, *errmsg;
   int fd = -1;

   if (!(d = data)) return;

   if (eldbus_message_error_get(msg, &errname, &errmsg))
     {
        ERR("Eldbus Message Error: %s %s", errname, errmsg);
        ERR("\tFailed to open device: %s", d->node);
        return;
     }

   /* DBG("Input Device Opened: %s", d->node); */

   /* DBUS_TYPE_UNIX_FD == 'h' */
   if (!eldbus_message_arguments_get(msg, "hb", &fd, &b))
     {
        ERR("\tCould not get UNIX_FD from eldbus message: %d %d", fd, b);
        goto cleanup;
     }

   if (!(fd = _device_flags_set(fd)))
     {
        ERR("\tCould not set fd flags");
        goto release;
     }

   if (!(edev = _ecore_drm_evdev_device_create(d->seat, d->node, fd)))
     {
        ERR("\tCould not create evdev device: %s", d->node);
        goto release;
     }

   d->seat->devices = eina_list_append(d->seat->devices, edev);

   goto cleanup;

release:
   _ecore_drm_dbus_device_close(d->node);
cleanup:
   eina_stringshare_del(d->node);
   free(d);
}

static Ecore_Drm_Seat *
_seat_get(Ecore_Drm_Input *input, const char *seat)
{
   Ecore_Drm_Seat *s;
   Eina_List *l;

   EINA_LIST_FOREACH(input->dev->seats, l, s)
     {
        if (!strcmp(s->name, seat)) return s;
     }

   if (!(s = calloc(1, sizeof(Ecore_Drm_Seat))))
     return NULL;

   s->input = input;
   s->name = eina_stringshare_add(seat);

   input->dev->seats = eina_list_append(input->dev->seats, s);

   return s;
}

static Eina_Bool 
_device_add(Ecore_Drm_Input *input, const char *device)
{
   Ecore_Drm_Seat *seat;
   Ecore_Drm_Device_Open_Data *data;
   const char *devseat, *wlseat;

   DBG("Add Input Device: %s", device);

   if (!(devseat = eeze_udev_syspath_get_property(device, "ID_SEAT")))
     devseat = eina_stringshare_add("seat0");

   if (strcmp(devseat, input->seat)) goto seat_err;

   if (!(wlseat = eeze_udev_syspath_get_property(device, "WL_SEAT")))
     wlseat = eina_stringshare_add("seat0");

   if (!(seat = _seat_get(input, wlseat))) 
     {
        ERR("\tCould not get matching seat");
        goto seat_get_err;
     }

   if (!(data = calloc(1, sizeof(Ecore_Drm_Device_Open_Data))))
     goto seat_get_err;

   data->seat = seat;
   if (!(data->node = eeze_udev_syspath_get_devpath(device)))
     goto dev_err;

   DBG("\tDevice Path: %s", data->node);

   _ecore_drm_dbus_device_open(data->node, _cb_device_opened, data);

   return EINA_TRUE;

dev_err:
   free(data);
seat_get_err:
   eina_stringshare_del(wlseat);
seat_err:
   eina_stringshare_del(devseat);
   return EINA_FALSE;
}

static void 
_device_remove(Ecore_Drm_Input *input, const char *device)
{
   Ecore_Drm_Seat *seat;
   Eina_List *l;

   if (!input) return;

   EINA_LIST_FOREACH(input->dev->seats, l, seat)
     {
        Ecore_Drm_Evdev *edev;
        Eina_List *ll;

        EINA_LIST_FOREACH(seat->devices, ll, edev)
          {
             if (!strcmp(edev->path, device))
               {
                  seat->devices = eina_list_remove(seat->devices, edev);
                  _ecore_drm_evdev_device_destroy(edev);
                  break;
               }
          }
     }
}

static void 
_cb_input_event(const char *device, Eeze_Udev_Event event, void *data, Eeze_Udev_Watch *watch EINA_UNUSED)
{
   Ecore_Drm_Input *input;

   if (!(input = data)) return;

   switch (event)
     {
      case EEZE_UDEV_EVENT_ADD:
        _device_add(input, device);
        break;
      case EEZE_UDEV_EVENT_REMOVE:
          {
             const char *node;

             node = eeze_udev_syspath_get_devpath(device);
             _device_remove(input, node);
             eina_stringshare_del(node);
          }
        break;
      default:
        break;
     }
}

static Eina_Bool 
_devices_add(Ecore_Drm_Input *input)
{
   Eina_List *devices;
   Eina_Bool found = EINA_FALSE;
   const char *device;

   /* NB: This really sucks !! We cannot 'OR' diferent device types 
    * together for eeze_udev_find_by_type
    * 
    * For now, just find by 'NONE" and we'll filter for input devices by 
    * running tests */
   devices = eeze_udev_find_by_type(EEZE_UDEV_TYPE_NONE, NULL);
   EINA_LIST_FREE(devices, device)
     {
        if ((eeze_udev_syspath_is_mouse(device)) || 
            (eeze_udev_syspath_is_kbd(device)) || 
            (eeze_udev_syspath_is_touchpad(device)) || 
            (eeze_udev_syspath_is_joystick(device)))
          {
             if (!_device_add(input, device))
               ERR("\tFailed to add device");
             else
               found = EINA_TRUE;
          }

        eina_stringshare_del(device);
     }

   if (!found)
     {
        ERR("No Input Devices Found");
        return EINA_FALSE;
     }

   return EINA_TRUE;
}

/* public functions */
EAPI Eina_Bool 
ecore_drm_inputs_create(Ecore_Drm_Device *dev)
{
   Ecore_Drm_Input *input;

   /* check for valid device */
   if (!dev) return EINA_FALSE;

   /* try to allocate space for input structure */
   if (!(input = calloc(1, sizeof(Ecore_Drm_Input))))
     return EINA_FALSE;

   /* FIXME: Hardcoded seat name */
   input->seat = eina_stringshare_add("seat0");
   input->dev = dev;

   /* try to enable this input */
   if (!ecore_drm_inputs_enable(input))
     {
        ERR("Could not enable input");
        if (input->seat) eina_stringshare_del(input->seat);
        free(input);
        return EINA_FALSE;
     }

   /* add this input to dev */
   dev->inputs = eina_list_append(dev->inputs, input);

   return EINA_TRUE;
}

EAPI void 
ecore_drm_inputs_destroy(Ecore_Drm_Device *dev)
{
   Ecore_Drm_Seat *seat;
   Eina_List *l;

   if (!dev) return;

   EINA_LIST_FOREACH(dev->seats, l, seat)
     {
        Ecore_Drm_Evdev *edev;

        EINA_LIST_FREE(seat->devices, edev)
          {
             _ecore_drm_dbus_device_close(edev->path);
             _ecore_drm_evdev_device_destroy(edev);
          }
     }
}

EAPI Eina_Bool 
ecore_drm_inputs_enable(Ecore_Drm_Input *input)
{
   /* check for valid input */
   if (!input) return EINA_FALSE;

   if (!input->watch)
     {
        int events = 0;

        events = (EEZE_UDEV_EVENT_ADD | EEZE_UDEV_EVENT_REMOVE);
        /* NB: This really sucks !! We cannot 'OR' diferent device types 
         * together for eeze_udev_watch_add :(
         * 
         * For now, just put a 'watch' on mouse type as this (in effect) does 
         * what we need by internally by adding a subsystem match for 'input' */
        if (!(input->watch = 
              eeze_udev_watch_add(EEZE_UDEV_TYPE_MOUSE, events, 
                                  _cb_input_event, input)))
          {
             ERR("Could not create Eeze_Udev_Watch for input");
             return EINA_FALSE;
          }
     }

   /* try to add devices */
   if (!_devices_add(input))
     {
        ERR("Could not add input devices");
        eeze_udev_watch_del(input->watch);
        input->watch = NULL;
        return EINA_FALSE;
     }

   input->enabled = EINA_TRUE;
   input->suspended = EINA_FALSE;

   return EINA_TRUE;
}

EAPI void 
ecore_drm_inputs_disable(Ecore_Drm_Input *input)
{
   if (!input) return;

   if (input->watch) eeze_udev_watch_del(input->watch);
   input->watch = NULL;

   input->enabled = EINA_FALSE;
   input->suspended = EINA_TRUE;

   ecore_drm_inputs_destroy(input->dev);
}