88781c478f3fe40d2327682b9cf200fdf49f3418
[platform/upstream/enlightenment.git] / src / bin / e_input_device.c
1 #include "e.h"
2 #include "e_input_private.h"
3
4 /* e_input_device private variable */
5 static Eina_List *einput_devices;
6 static E_Input_Device *e_input_device_default = NULL;
7
8 struct xkb_keymap *cached_keymap;
9 struct xkb_context *cached_context;
10
11 static int
12 _device_open_no_pending(const char *device, int flags)
13 {
14    int fd = -1;
15    struct stat s;
16
17    fd = open(device, flags | O_CLOEXEC);
18
19    if (fd < 0) return fd;
20    if (fstat(fd, &s) == -1)
21      {
22         close(fd);
23         return -1;
24      }
25
26    return fd;
27 }
28
29 /* local functions */
30 static int
31 _e_input_device_cb_open_restricted(const char *path, int flags, void *data)
32 {
33    E_Input_Backend *input = (E_Input_Backend *)data;
34    int fd = -1;
35
36    EINA_SAFETY_ON_NULL_RETURN_VAL(input, -1);
37
38    /* try to open the device */
39    fd = _device_open_no_pending(path, flags);
40
41    if (fd < 0)
42      {
43         ERR("Could not open device");
44         return -1;
45      }
46
47    return fd;
48 }
49
50 static void
51 _e_input_device_cb_close_restricted(int fd, void *data)
52 {
53    if (fd >= 0) close(fd);
54 }
55
56 const struct libinput_interface _input_interface =
57 {
58    _e_input_device_cb_open_restricted,
59    _e_input_device_cb_close_restricted,
60 };
61
62 static E_Input_Device *
63 _e_input_device_default_get(void)
64 {
65    return e_input_device_default;
66 }
67
68 struct xkb_context *
69 _e_input_device_cached_context_get(enum xkb_context_flags flags)
70 {
71    if (!cached_context)
72      return xkb_context_new(flags);
73    else
74      return xkb_context_ref(cached_context);
75 }
76
77 struct xkb_keymap *
78 _e_input_device_cached_keymap_get(struct xkb_context *ctx,
79                        const struct xkb_rule_names *names,
80                        enum xkb_keymap_compile_flags flags)
81 {
82    EINA_SAFETY_ON_NULL_RETURN_VAL(ctx, NULL);
83
84    if (!cached_keymap)
85      return xkb_map_new_from_names(ctx, names, flags);
86    else
87      return xkb_map_ref(cached_keymap);
88 }
89
90 void
91 _e_input_device_cached_context_update(struct xkb_context *ctx)
92 {
93    Eina_List *l;
94    E_Input_Device *dev;
95
96    EINA_LIST_FOREACH(einput_devices, l, dev)
97      {
98         xkb_context_unref(dev->xkb_ctx);
99         dev->xkb_ctx = xkb_context_ref(ctx);
100      }
101 }
102
103 void
104 _e_input_device_cached_keymap_update(struct xkb_keymap *map)
105 {
106    Eina_List *l, *l2, *l3;
107    E_Input_Device *dev;
108    E_Input_Seat *seat;
109    E_Input_Evdev *edev;
110
111    EINA_LIST_FOREACH(einput_devices, l, dev)
112      EINA_LIST_FOREACH(dev->seats, l2, seat)
113        EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l3, edev)
114          {
115             g_mutex_lock(&edev->xkb.keymap_mutex);
116             xkb_keymap_unref(edev->xkb.keymap);
117             edev->xkb.keymap = xkb_keymap_ref(map);
118             g_mutex_unlock(&edev->xkb.keymap_mutex);
119
120             g_mutex_lock(&edev->xkb.state_mutex);
121             xkb_state_unref(edev->xkb.state);
122             edev->xkb.state = xkb_state_new(map);
123             g_mutex_unlock(&edev->xkb.state_mutex);
124          }
125 }
126
127 EINTERN void
128 e_input_device_keyboard_cached_context_set(struct xkb_context *ctx)
129 {
130    EINA_SAFETY_ON_NULL_RETURN(ctx);
131
132    if (cached_context == ctx) return;
133
134    if (cached_context)
135      _e_input_device_cached_context_update(ctx);
136
137    cached_context = ctx;
138 }
139
140 EINTERN void
141 e_input_device_keyboard_cached_keymap_set(struct xkb_keymap *map)
142 {
143    EINA_SAFETY_ON_NULL_RETURN(map);
144
145    if (cached_keymap == map) return;
146
147    if (cached_keymap)
148       _e_input_device_cached_keymap_update(map);
149
150    cached_keymap = map;
151 }
152
153 static void
154 e_input_device_destroy(E_Input_Device *dev)
155 {
156    E_Input_Backend *input;
157    E_Input_Seat *seat;
158    E_Input_Evdev *edev;
159
160    EINA_SAFETY_ON_NULL_RETURN(dev);
161
162    EINA_LIST_FREE(dev->seats, seat)
163      {
164         EINA_LIST_FREE(seat->devices, edev)
165           {
166              libinput_device_config_send_events_set_mode(edev->device,
167                                                          LIBINPUT_CONFIG_SEND_EVENTS_DISABLED);
168              _e_input_evdev_device_destroy(edev);
169           }
170
171         if (seat->name)
172           eina_stringshare_del(seat->name);
173         free(seat);
174      }
175
176    EINA_LIST_FREE(dev->inputs, input)
177      {
178         if (input->hdlr)
179           ecore_main_fd_handler_del(input->hdlr);
180         if (input->libinput)
181           libinput_unref(input->libinput);
182         free(input);
183      }
184
185    eina_stringshare_del(dev->seat);
186    xkb_context_unref(dev->xkb_ctx);
187    eina_hash_free(dev->fd_hash);
188    dev->fd_hash = NULL;
189
190    if (dev == e_input_device_default)
191      e_input_device_default = NULL;
192
193    free(dev);
194 }
195
196 static void
197 _e_input_device_add_list(E_Input_Device *dev)
198 {
199    Eina_List *l;
200    E_Input_Device *dev_data;
201
202    EINA_LIST_FOREACH(einput_devices, l, dev_data)
203      {
204         if (dev_data == dev) return;
205      }
206
207    einput_devices = eina_list_append(einput_devices, dev);
208 }
209
210 static void
211 _e_input_device_remove_list(E_Input_Device *dev)
212 {
213    Eina_List *l, *l_next;
214    E_Input_Device *dev_data;
215
216    EINA_LIST_FOREACH_SAFE(einput_devices, l, l_next, dev_data)
217      {
218         if (dev == dev_data)
219           einput_devices = eina_list_remove_list(einput_devices, l);
220      }
221 }
222
223 EINTERN E_Input_Device *
224 e_input_device_open(void)
225 {
226    E_Input_Device *dev = NULL;
227
228    dev = (E_Input_Device *)calloc(1, sizeof(E_Input_Device));
229
230    if (!dev)
231      {
232         EINA_LOG_ERR("Failed to alloc memory for E_Input_Device\n");
233         return NULL;
234      }
235
236    dev->seat = eina_stringshare_add("seat0");
237    dev->fd_hash = eina_hash_string_superfast_new(NULL);
238
239    /* try to create xkb context */
240    if (!(dev->xkb_ctx = _e_input_device_cached_context_get(0)))
241      {
242         ERR("Failed to create xkb context: %m");
243         goto err;
244      }
245
246    if (!e_input_device_default)
247      e_input_device_default = dev;
248
249    _e_input_device_add_list(dev);
250
251    return dev;
252
253 err:
254    if (dev)
255      {
256         eina_stringshare_del(dev->seat);
257         eina_hash_free(dev->fd_hash);
258         xkb_context_unref(dev->xkb_ctx);
259         free(dev);
260      }
261
262    return NULL;
263 }
264
265 EINTERN Eina_Bool
266 e_input_device_close(E_Input_Device *dev)
267 {
268    /* check for valid device */
269    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
270
271    _e_input_device_remove_list(dev);
272    e_input_device_destroy(dev);
273
274    return EINA_TRUE;
275 }
276
277 EINTERN void
278 e_input_device_window_set(E_Input_Device *dev, Ecore_Window window)
279 {
280    /* check for valid device */
281    EINA_SAFETY_ON_TRUE_RETURN(!dev);
282
283    /* TODO : Must update window of ecore/evas device when the given window */
284    /*        is not equal to the existing window. */
285
286    dev->window = window;
287 }
288
289 EINTERN void
290 e_input_device_pointer_xy_get(E_Input_Device *dev, int *x, int *y)
291 {
292    E_Input_Seat *seat;
293    E_Input_Evdev *edev;
294    Eina_List *l, *ll;
295
296    if (x) *x = 0;
297    if (y) *y = 0;
298
299    if (!dev)
300      dev = _e_input_device_default_get();
301
302    /* check for valid device */
303    EINA_SAFETY_ON_TRUE_RETURN(!dev);
304    EINA_LIST_FOREACH(dev->seats, l, seat)
305      {
306         EINA_LIST_FOREACH(seat->devices, ll, edev)
307           {
308              if (!(edev->caps & E_INPUT_SEAT_POINTER ||
309                    edev->caps & E_INPUT_SEAT_TOUCH))
310                continue;
311
312              if (x) *x = seat->ptr.dx;
313              if (y) *y = seat->ptr.dy;
314
315              return;
316           }
317      }
318 }
319
320 E_API Eina_Bool
321 e_input_device_pointer_warp(E_Input_Device *dev, int x, int y)
322 {
323    E_Input_Seat *seat;
324    E_Input_Evdev *edev, *warp_dev;
325    Eina_List *l, *ll;
326    Eina_Bool found = EINA_FALSE;
327    char *device_name = NULL, *device_path = NULL;
328
329    if (!dev)
330      dev = _e_input_device_default_get();
331
332    if (e_devicemgr && e_devicemgr->last_device_ptr)
333      {
334         device_name = (char *)e_devicemgr->last_device_ptr->name;
335         device_path = (char *)e_devicemgr->last_device_ptr->identifier;
336      }
337
338    /* check for valid device */
339    EINA_SAFETY_ON_TRUE_RETURN_VAL(!dev, EINA_FALSE);
340    EINA_LIST_FOREACH(dev->seats, l, seat)
341      {
342         warp_dev = NULL;
343         EINA_LIST_FOREACH(seat->devices, ll, edev)
344           {
345              if (!libinput_device_has_capability(edev->device,
346                                                  LIBINPUT_DEVICE_CAP_POINTER))
347                continue;
348
349              if (!e_util_strcmp(libinput_device_get_name(edev->device), device_name) &&
350                  !e_util_strcmp(edev->path, device_path))
351                {
352                   warp_dev = edev;
353                   break;
354                }
355
356              if (!warp_dev) warp_dev = edev;
357           }
358
359         if (warp_dev)
360           {
361              _e_input_hook_call(E_INPUT_HOOK_POINTER_WARP, libinput_device_get_name(warp_dev->device));
362
363              seat->ptr.dx = seat->ptr.ix = x;
364              seat->ptr.dy = seat->ptr.iy = y;
365              _e_input_pointer_motion_post(warp_dev);
366
367              found = EINA_TRUE;
368           }
369      }
370
371    if (found)
372      return EINA_TRUE;
373
374    return EINA_FALSE;
375 }
376
377 EINTERN Eina_Bool
378 e_input_device_pointer_left_handed_set(E_Input_Device *dev, Eina_Bool left_handed)
379 {
380    E_Input_Seat *seat = NULL;
381    E_Input_Evdev *edev = NULL;
382    Eina_List *l = NULL, *l2 = NULL;
383
384    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
385    EINA_SAFETY_ON_NULL_RETURN_VAL(dev->seats, EINA_FALSE);
386
387    if (dev->left_handed == left_handed)
388      return EINA_TRUE;
389    dev->left_handed = left_handed;
390
391    EINA_LIST_FOREACH(dev->seats, l, seat)
392      {
393         EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l2, edev)
394           {
395              if (libinput_device_has_capability(edev->device,
396                                                 LIBINPUT_DEVICE_CAP_POINTER))
397                {
398                   if (libinput_device_config_left_handed_set(edev->device, (int)left_handed) !=
399                       LIBINPUT_CONFIG_STATUS_SUCCESS)
400                     {
401                        WRN("Failed to set left hand mode about device: %s\n",
402                            libinput_device_get_name(edev->device));
403                        continue;
404                     }
405                }
406           }
407      }
408    return EINA_TRUE;
409 }
410
411
412 EINTERN Eina_Bool
413 e_input_device_pointer_rotation_set(E_Input_Device *dev, int rotation)
414 {
415    E_Input_Seat *seat = NULL;
416    Eina_List *l = NULL;
417
418    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
419    EINA_SAFETY_ON_NULL_RETURN_VAL(dev->seats, EINA_FALSE);
420
421    if ((rotation % 90 != 0) || (rotation / 90 > 3) || (rotation < 0)) return EINA_FALSE;
422
423    EINA_LIST_FOREACH(dev->seats, l, seat)
424      {
425         switch (rotation)
426           {
427            case 90:
428               seat->ptr.swap = EINA_TRUE;
429               seat->ptr.invert_x = EINA_FALSE;
430               seat->ptr.invert_y = EINA_TRUE;
431               break;
432            case 180:
433               seat->ptr.swap = EINA_FALSE;
434               seat->ptr.invert_x = EINA_TRUE;
435               seat->ptr.invert_y = EINA_TRUE;
436               break;
437            case 270:
438               seat->ptr.swap = EINA_TRUE;
439               seat->ptr.invert_x = EINA_TRUE;
440               seat->ptr.invert_y = EINA_FALSE;
441                 break;
442            case 0:
443               seat->ptr.swap = EINA_FALSE;
444               seat->ptr.invert_x = EINA_FALSE;
445               seat->ptr.invert_y = EINA_FALSE;
446               break;
447            default:
448               break;
449           }
450      }
451    return EINA_TRUE;
452 }
453
454 EINTERN void
455 e_input_device_rotation_set(E_Input_Device *dev, unsigned int rotation)
456 {
457    E_Input_Seat *seat = NULL;
458    E_Input_Evdev *edev = NULL;
459    Eina_List *l = NULL, *l2 = NULL;
460    int temp;
461
462    EINA_SAFETY_ON_NULL_RETURN(dev);
463    EINA_SAFETY_ON_NULL_RETURN(dev->seats);
464
465    EINA_LIST_FOREACH(dev->seats, l, seat)
466      {
467         EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l2, edev)
468           {
469              if (libinput_device_has_capability(edev->device,
470                                       LIBINPUT_DEVICE_CAP_POINTER))
471                {
472                   edev->mouse.minx = edev->mouse.miny = 0;
473                   e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen),
474                                        &edev->mouse.maxw, &edev->mouse.maxh);
475
476                   if (rotation == 90 || rotation == 270)
477                     {
478                        temp = edev->mouse.minx;
479                        edev->mouse.minx = edev->mouse.miny;
480                        edev->mouse.miny = temp;
481
482                        temp = edev->mouse.maxw;
483                        edev->mouse.maxw = edev->mouse.maxh;
484                        edev->mouse.maxh = temp;
485                     }
486                }
487           }
488      }
489 }
490
491 static void
492 _e_input_device_touch_matrix_identify(float result[6])
493 {
494    result[0] = 1.0;
495    result[1] = 0.0;
496    result[2] = 0.0;
497    result[3] = 0.0;
498    result[4] = 1.0;
499    result[5] = 0.0;
500 }
501
502 static void
503 _e_input_device_touch_matrix_multiply(float result[6], float m1[6], float m2[6])
504 {
505    result[0] = m1[0] * m2 [0] + m1[1] * m2[3];
506    result[1] = m1[0] * m2 [1] + m1[1] * m2[4];
507    result[2] = m1[0] * m2 [2] + m1[1] * m2[5] + m1[2];
508    result[3] = m1[3] * m2 [0] + m1[4] * m2[3];
509    result[4] = m1[3] * m2 [1] + m1[4] * m2[4];
510    result[5] = m1[3] * m2 [2] + m1[4] * m2[5] + m1[5];
511 }
512
513 static void
514 _e_input_device_touch_matrix_rotation_get(float result[6], int degree, float w, float h)
515 {
516    if (w == 0.0) w = 1.0;
517    if (h == 0.0) h = 1.0;
518
519    switch (degree)
520      {
521         case 90:
522           result[0] = 0.0;
523           result[1] = -h/w;
524           result[2] = h/w;
525           result[3] = w/h;
526           result[4] = 0.0;
527           result[5] = 0.0;
528           break;
529         case 180:
530           result[0] = -1.0;
531           result[1] = 0.0;
532           result[2] = 1.0;
533           result[3] = 0.0;
534           result[4] = -1.0;
535           result[5] = 1.0;
536           break;
537         case 270:
538           result[0] = 0.0;
539           result[1] = h/w;
540           result[2] = 0.0;
541           result[3] = -w/h;
542           result[4] = 0.0;
543           result[5] = w/h;
544           break;
545         case 0:
546           _e_input_device_touch_matrix_identify(result);
547           break;
548         default:
549           WRN("Please input valid angle(%d)\n", degree);
550      }
551 }
552
553 static void
554 _e_input_device_touch_matrix_translate_get(float result[6], float x, float y, float w, float h, float default_w, float default_h)
555 {
556    if (default_w == 0.0) default_w = 1.0;
557    if (default_h == 0.0) default_h = 1.0;
558
559    result[0] = w / default_w;
560    result[4] = h / default_h;
561    result[2] = x / default_w;
562    result[5] = y / default_h;
563 }
564
565 EINTERN Eina_Bool
566 e_input_device_touch_rotation_set(E_Input_Device *dev, unsigned int rotation)
567 {
568    E_Input_Seat *seat = NULL;
569    E_Input_Evdev *edev = NULL;
570    Eina_List *l = NULL, *l2 = NULL;
571    float mat_translate[6] = {0.0, }, mat_rotation[6] = {0.0, }, result[6] = {0.0, };
572    float default_w = 0.0, default_h = 0.0;
573    Eina_Bool res = EINA_TRUE;
574    int output_w = 0, output_h = 0;
575
576    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
577    EINA_SAFETY_ON_NULL_RETURN_VAL(dev->seats, EINA_FALSE);
578
579    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &output_w, &output_h);
580    default_w = (float)output_w;
581    default_h = (float)output_h;
582
583    EINA_LIST_FOREACH(dev->seats, l, seat)
584      {
585         EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l2, edev)
586           {
587              if (edev->caps & E_INPUT_SEAT_TOUCH)
588                {
589                   _e_input_device_touch_matrix_identify(mat_translate);
590                   _e_input_device_touch_matrix_identify(mat_rotation);
591                   _e_input_device_touch_matrix_identify(result);
592
593                   if (edev->touch.transform.x || edev->touch.transform.y ||
594                       edev->touch.transform.w || edev->touch.transform.h)
595                     {
596                        _e_input_device_touch_matrix_translate_get(mat_translate,
597                                                                     (float)edev->touch.transform.x,
598                                                                     (float)edev->touch.transform.y,
599                                                                     (float)edev->touch.transform.w,
600                                                                     (float)edev->touch.transform.h,
601                                                                     default_w, default_h);
602
603                     }
604
605                   _e_input_device_touch_matrix_rotation_get(mat_rotation, rotation, default_w, default_h);
606
607                   _e_input_device_touch_matrix_multiply(result, mat_translate, mat_rotation);
608
609                   if (!e_input_evdev_touch_calibration_set(edev, result))
610                     {
611                        res = EINA_FALSE;
612                        continue;
613                     }
614                   else
615                     {
616                        edev->touch.transform.rotation = rotation;
617                     }
618                }
619           }
620      }
621
622    return res;
623 }
624
625 EINTERN Eina_Bool
626 e_input_device_touch_transformation_set(E_Input_Device *dev, int offset_x, int offset_y, int w, int h)
627 {
628    E_Input_Seat *seat = NULL;
629    E_Input_Evdev *edev = NULL;
630    Eina_List *l = NULL, *l2 = NULL;
631    float mat_translate[6] = {0.0, }, mat_rotation[6] = {0.0 }, result[6] = {0.0, };
632    float default_w = 0.0, default_h = 0.0;
633    Eina_Bool res = EINA_TRUE;
634    int output_w = 0, output_h = 0;
635
636    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
637    EINA_SAFETY_ON_NULL_RETURN_VAL(dev->seats, EINA_FALSE);
638    EINA_SAFETY_ON_TRUE_RETURN_VAL((w == 0) || (h == 0), EINA_FALSE);
639
640    e_output_size_get(e_comp_screen_primary_output_get(e_comp->e_comp_screen), &output_w, &output_h);
641    default_w = (float)output_w;
642    default_h = (float)output_h;
643
644    EINA_LIST_FOREACH(dev->seats, l, seat)
645      {
646         EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l2, edev)
647           {
648              if (edev->caps & E_INPUT_SEAT_TOUCH)
649                {
650                   _e_input_device_touch_matrix_identify(mat_translate);
651                   _e_input_device_touch_matrix_identify(mat_rotation);
652                   _e_input_device_touch_matrix_identify(result);
653
654                   _e_input_device_touch_matrix_translate_get(mat_translate,
655                                                                (float)offset_x, (float)offset_y,
656                                                                (float)w, (float)h, default_w, default_h);
657
658                   if (edev->touch.transform.rotation)
659                     {
660                        _e_input_device_touch_matrix_rotation_get(mat_rotation,
661                                                                    edev->touch.transform.rotation,
662                                                                    default_w, default_h);
663                     }
664
665                   _e_input_device_touch_matrix_multiply(result, mat_translate, mat_rotation);
666
667                   if (!e_input_evdev_touch_calibration_set(edev, result))
668                     {
669                        res = EINA_FALSE;
670                        continue;
671                     }
672                   else
673                     {
674                        edev->touch.transform.x = offset_x;
675                        edev->touch.transform.y = offset_y;
676                        edev->touch.transform.w = w;
677                        edev->touch.transform.h = h;
678                     }
679                }
680           }
681      }
682    return res;
683 }
684
685 static void
686 _libinput_log_handler(struct libinput *libinput EINA_UNUSED,
687                                enum libinput_log_priority priority,
688                                const char *format, va_list args)
689 {
690    char buf[1024] = {0,};
691
692    vsnprintf(buf, 1024, format, args);
693    switch (priority)
694      {
695         case LIBINPUT_LOG_PRIORITY_DEBUG:
696            EIDBG("%s", buf);
697            break;
698         case LIBINPUT_LOG_PRIORITY_INFO:
699            EIINF("%s", buf);
700            break;
701         case LIBINPUT_LOG_PRIORITY_ERROR:
702            EIERR("%s", buf);
703            break;
704         default:
705            break;
706      }
707 }
708
709 EINTERN Eina_Bool
710 e_input_device_input_backend_create(E_Input_Device *dev, E_Input_Libinput_Backend backend)
711 {
712    Eina_Bool res = EINA_FALSE;
713
714    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
715
716    if (backend == E_INPUT_LIBINPUT_BACKEND_UDEV)
717      {
718         TRACE_INPUT_BEGIN(e_input_device_input_create_libinput_udev);
719         res = e_input_device_input_create_libinput_udev(dev);
720         TRACE_INPUT_END();
721      }
722    else if (backend == E_INPUT_LIBINPUT_BACKEND_PATH)
723      {
724         TRACE_INPUT_BEGIN(e_input_device_input_create_libinput_path);
725         res = e_input_device_input_create_libinput_path(dev);
726         TRACE_INPUT_END();
727      }
728
729    return res;
730 }
731
732 static void
733 _e_input_device_input_thread_name_set(void)
734 {
735    eina_thread_name_set(eina_thread_self(), "input-device-thread");
736 }
737
738 static void
739 _einput_device_input_thread_udev_backend_heavy(void *data, Ecore_Thread *th, void *msg_data)
740 {
741    E_Input_Backend *input = (E_Input_Backend *)data;
742
743    EINA_SAFETY_ON_NULL_RETURN(input);
744    EINA_SAFETY_ON_NULL_RETURN(input->dev);
745    EINA_SAFETY_ON_NULL_RETURN(input->dev->seat);
746
747    /* try to create libinput context */
748    input->libinput =
749      libinput_udev_create_context(&_input_interface, input, eeze_udev_get());
750
751    if (!input->libinput)
752      {
753         free(input);
754
755         ERR("Could not create libinput context: %m");
756         return;
757      }
758
759    _e_input_device_input_thread_name_set();
760
761    if (input->log_disable)
762      libinput_log_set_handler(input->libinput, NULL);
763    else
764      {
765         if (input->log_use_eina)
766           libinput_log_set_handler(input->libinput, _libinput_log_handler);
767         libinput_log_set_priority(input->libinput, LIBINPUT_LOG_PRIORITY_INFO);
768      }
769
770    TRACE_INPUT_BEGIN(libinput_udev_assign_seat);
771    /* assign udev seat */
772    if (libinput_udev_assign_seat(input->libinput, input->dev->seat) != 0)
773      {
774         ERR("Failed to assign seat: %m");
775         TRACE_INPUT_END();
776         return;
777      }
778    TRACE_INPUT_END();
779
780    return;
781 }
782
783 static void
784 _einput_device_input_thread_udev_backend_notify(void *data, Ecore_Thread *th, void *msg_data)
785 {
786    //TODO : do if there is something to do in main thread
787 }
788
789 static void
790 _einput_device_input_thread_udev_backend_end(void *data, Ecore_Thread *th, void *msg_data)
791 {
792    E_Input_Backend *input = (E_Input_Backend *)data;
793    E_Input_Device *dev = NULL;
794
795    EINA_SAFETY_ON_NULL_RETURN(input);
796    EINA_SAFETY_ON_NULL_RETURN(input->dev);
797
798    input->thread = NULL;
799
800    /* enable this input */
801    if (!e_input_enable_input(input))
802      {
803         ERR("Failed to enable input");
804         return;
805      }
806
807    /* append this input */
808    dev = input->dev;
809    dev->inputs = eina_list_append(dev->inputs, input);
810
811    /* process pending events */
812    _input_events_process(input);
813 }
814
815 static void
816 _einput_device_input_thread_udev_backend_cancel(void *data, Ecore_Thread *th, void *msg_data)
817 {
818    E_Input_Backend *input = (E_Input_Backend *)data;
819
820    EINA_SAFETY_ON_NULL_RETURN(input);
821
822    input->thread = NULL;
823 }
824
825 static Eina_Bool
826 _e_input_device_input_thread_init_udev_backend(E_Input_Backend *input)
827 {
828    EINA_SAFETY_ON_NULL_RETURN_VAL(input, EINA_FALSE);
829
830    input->thread = ecore_thread_feedback_run((Ecore_Thread_Cb)_einput_device_input_thread_udev_backend_heavy,
831                                              (Ecore_Thread_Notify_Cb)_einput_device_input_thread_udev_backend_notify,
832                                              (Ecore_Thread_Cb)_einput_device_input_thread_udev_backend_end,
833                                              (Ecore_Thread_Cb)_einput_device_input_thread_udev_backend_cancel, input, 1);
834    return !!(input->thread);
835 }
836
837 /* public functions */
838 EINTERN Eina_Bool
839 e_input_device_input_create_libinput_udev(E_Input_Device *dev)
840 {
841    E_Input_Backend *input;
842    char *env = NULL;
843    int buf_size = 0;
844    int res = 0;
845
846    /* check for valid device */
847    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
848
849    /* try to allocate space for new input structure */
850    if (!(input = calloc(1, sizeof(E_Input_Backend))))
851      {
852         return EINA_FALSE;
853      }
854
855    /* set reference for parent device */
856    input->dev = dev;
857
858    input->backend = E_INPUT_LIBINPUT_BACKEND_UDEV;
859    input->log_disable = EINA_FALSE;
860    input->log_use_eina = EINA_FALSE;
861
862    env = e_util_env_get(E_INPUT_ENV_LIBINPUT_LOG_DISABLE);
863    if ((env) && (atoi(env) == 1))
864      input->log_disable = EINA_TRUE;
865    else
866      {
867         if (env) E_FREE(env);
868
869         env = e_util_env_get(E_INPUT_ENV_LIBINPUT_LOG_EINA_LOG);
870         if ((env) && (atoi(env) == 1))
871           input->log_use_eina = EINA_TRUE;
872      }
873    E_FREE(env);
874
875    env = e_util_env_get("UDEV_MONITOR_EVENT_SOURCE");
876
877    if (env)
878      {
879         libinput_udev_set_udev_monitor_event_source(env);
880      }
881    E_FREE(env);
882
883    env = e_util_env_get("UDEV_MONITOR_BUFFER_SIZE");
884
885    if ((env) && (buf_size = atoi(env)))
886      {
887         res = libinput_udev_set_udev_monitor_buffer_size(buf_size);
888         if (res)
889           ERR("Wrong buffer size for udev monitor : %d\n", buf_size);
890      }
891    E_FREE(env);
892
893    if (e_input_thread_enabled_get())
894      {
895         /* initialize libinput udev backend within an ecore thread */
896         if (!_e_input_device_input_thread_init_udev_backend(input))
897           {
898              ERR("Failed to initialize e_input backend (libinput udev backend) !");
899              goto err;
900         }
901
902         return EINA_TRUE;
903      }
904
905    /* try to create libinput context */
906    input->libinput =
907      libinput_udev_create_context(&_input_interface, input, eeze_udev_get());
908    if (!input->libinput)
909      {
910         ERR("Could not create libinput context: %m");
911         goto err;
912      }
913
914    if (input->log_disable)
915      libinput_log_set_handler(input->libinput, NULL);
916    else
917      {
918         if (input->log_use_eina)
919           libinput_log_set_handler(input->libinput, _libinput_log_handler);
920         libinput_log_set_priority(input->libinput, LIBINPUT_LOG_PRIORITY_INFO);
921      }
922
923    /* assign udev seat */
924    TRACE_INPUT_BEGIN(libinput_udev_assign_seat);
925    if (libinput_udev_assign_seat(input->libinput, dev->seat) != 0)
926      {
927         ERR("Failed to assign seat: %m");
928         TRACE_INPUT_END();
929         goto err;
930      }
931    TRACE_INPUT_END();
932
933    /* enable this input */
934    if (!e_input_enable_input(input))
935      {
936         ERR("Failed to enable input");
937         goto err;
938      }
939
940    /* append this input */
941    dev->inputs = eina_list_append(dev->inputs, input);
942
943    /* process pending events */
944    _input_events_process(input);
945
946    return EINA_TRUE;
947
948 err:
949    if (input->libinput) libinput_unref(input->libinput);
950    free(input);
951
952    return EINA_FALSE;
953 }
954
955 static void
956 _einput_device_input_thread_path_backend_heavy(void *data, Ecore_Thread *th, void *msg_data)
957 {
958    char *env = NULL;
959    struct libinput_device *device;
960    E_Input_Backend *input = (E_Input_Backend *)data;
961
962    EINA_SAFETY_ON_NULL_RETURN(input);
963    EINA_SAFETY_ON_NULL_RETURN(input->dev);
964    EINA_SAFETY_ON_NULL_RETURN(input->dev->seat);
965
966    /* try to create libinput context */
967    input->libinput =
968      libinput_path_create_context(&_input_interface, input);
969    if (!input->libinput)
970      {
971         free(input);
972
973         ERR("Could not create libinput path context: %m");
974         return;
975      }
976
977    _e_input_device_input_thread_name_set();
978
979    if (input->log_disable)
980      libinput_log_set_handler(input->libinput, NULL);
981    else
982      {
983         if (input->log_use_eina)
984           libinput_log_set_handler(input->libinput, _libinput_log_handler);
985         libinput_log_set_priority(input->libinput, LIBINPUT_LOG_PRIORITY_INFO);
986      }
987
988    TRACE_INPUT_BEGIN(libinput_path_add_device_loop);
989    for (int i = 0; i < input->path_ndevices; i++)
990      {
991         char buf[1024] = "PATH_DEVICE_";
992         eina_convert_itoa(i + 1, buf + 12);
993         env = e_util_env_get(buf);
994
995         if (env)
996           {
997              device = libinput_path_add_device(input->libinput, env);
998              if (!device)
999                ERR("Failed to initialized device %s", env);
1000              else
1001                INF("libinput_path created input device %s", env);
1002              E_FREE(env);
1003           }
1004      }
1005    TRACE_INPUT_END();
1006
1007    return;
1008 }
1009
1010 static void
1011 _einput_device_input_thread_path_backend_notify(void *data, Ecore_Thread *th, void *msg_data)
1012 {
1013    //TODO : do if there is something to do in main thread
1014 }
1015
1016 static void
1017 _einput_device_input_thread_path_backend_end(void *data, Ecore_Thread *th, void *msg_data)
1018 {
1019    E_Input_Backend *input = (E_Input_Backend *)data;
1020    E_Input_Device *dev = NULL;
1021
1022    EINA_SAFETY_ON_NULL_RETURN(input);
1023    EINA_SAFETY_ON_NULL_RETURN(input->dev);
1024
1025    input->thread = NULL;
1026
1027    /* enable this input */
1028    if (!e_input_enable_input(input))
1029      {
1030         ERR("Failed to enable input");
1031         return;
1032      }
1033
1034    /* append this input */
1035    dev = input->dev;
1036    dev->inputs = eina_list_append(dev->inputs, input);
1037
1038    /* process pending events */
1039    _input_events_process(input);
1040 }
1041
1042 static void
1043 _einput_device_input_thread_path_backend_cancel(void *data, Ecore_Thread *th, void *msg_data)
1044 {
1045    E_Input_Backend *input = (E_Input_Backend *)data;
1046
1047    EINA_SAFETY_ON_NULL_RETURN(input);
1048
1049    input->thread = NULL;
1050 }
1051
1052 static Eina_Bool
1053 _e_input_device_input_thread_init_path_backend(E_Input_Backend *input)
1054 {
1055    EINA_SAFETY_ON_NULL_RETURN_VAL(input, EINA_FALSE);
1056
1057    input->thread = ecore_thread_feedback_run((Ecore_Thread_Cb)_einput_device_input_thread_path_backend_heavy,
1058                                              (Ecore_Thread_Notify_Cb)_einput_device_input_thread_path_backend_notify,
1059                                              (Ecore_Thread_Cb)_einput_device_input_thread_path_backend_end,
1060                                              (Ecore_Thread_Cb)_einput_device_input_thread_path_backend_cancel, input, 1);
1061    return !!(input->thread);
1062 }
1063
1064 EINTERN Eina_Bool
1065 e_input_device_input_create_libinput_path(E_Input_Device *dev)
1066 {
1067    E_Input_Backend *input;
1068    struct libinput_device *device;
1069    int ndevices = 0;
1070    char *env;
1071
1072    /* check for valid device */
1073    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
1074
1075    env = e_util_env_get("PATH_DEVICES_NUM");
1076    if (env)
1077      {
1078         ndevices = atoi(env);
1079         E_FREE(env);
1080      }
1081
1082    if (ndevices <= 0 || ndevices >= INT_MAX)
1083      {
1084         return EINA_TRUE;
1085      }
1086
1087    INF("PATH_DEVICES_NUM : %d", ndevices);
1088
1089    /* try to allocate space for new input structure */
1090    if (!(input = calloc(1, sizeof(E_Input_Backend))))
1091      {
1092         return EINA_FALSE;
1093      }
1094
1095    /* set reference for parent device */
1096    input->dev = dev;
1097
1098    input->backend = E_INPUT_LIBINPUT_BACKEND_PATH;
1099    input->path_ndevices = ndevices;
1100    input->log_disable = EINA_FALSE;
1101    input->log_use_eina = EINA_FALSE;
1102
1103    env = e_util_env_get(E_INPUT_ENV_LIBINPUT_LOG_DISABLE);
1104    if ((env) && (atoi(env) == 1))
1105      input->log_disable = EINA_TRUE;
1106    else
1107      {
1108         if (env) E_FREE(env);
1109
1110         env = e_util_env_get(E_INPUT_ENV_LIBINPUT_LOG_EINA_LOG);
1111         if ((env) && (atoi(env) == 1))
1112           input->log_use_eina = EINA_TRUE;
1113      }
1114    E_FREE(env);
1115
1116    if (e_input_thread_enabled_get())
1117      {
1118         /* initialize libinput path backend within an ecore thread */
1119         if (!_e_input_device_input_thread_init_path_backend(input))
1120           {
1121              ERR("Failed to initialize e_input backend (libinput path backend) !");
1122              goto err;
1123         }
1124
1125         return EINA_TRUE;
1126      }
1127
1128    /* try to create libinput context */
1129    input->libinput =
1130      libinput_path_create_context(&_input_interface, input);
1131    if (!input->libinput)
1132      {
1133         ERR("Could not create libinput path context: %m");
1134         goto err;
1135      }
1136
1137    if (input->log_disable)
1138      libinput_log_set_handler(input->libinput, NULL);
1139    else
1140      {
1141         if (input->log_use_eina)
1142           libinput_log_set_handler(input->libinput, _libinput_log_handler);
1143         libinput_log_set_priority(input->libinput, LIBINPUT_LOG_PRIORITY_INFO);
1144      }
1145
1146    TRACE_INPUT_BEGIN(libinput_path_add_device_loop);
1147    for (int i = 0; i < ndevices; i++)
1148      {
1149         char buf[1024] = "PATH_DEVICE_";
1150         eina_convert_itoa(i + 1, buf + 12);
1151         env = e_util_env_get(buf);
1152         if (env)
1153           {
1154              device = libinput_path_add_device(input->libinput, env);
1155              if (!device)
1156                ERR("Failed to initialized device %s", env);
1157              else
1158                INF("libinput_path created input device %s", env);
1159              E_FREE(env);
1160           }
1161      }
1162    TRACE_INPUT_END();
1163
1164    /* enable this input */
1165    if (!e_input_enable_input(input))
1166      {
1167         ERR("Failed to enable input");
1168         goto err;
1169      }
1170
1171    /* append this input */
1172    dev->inputs = eina_list_append(dev->inputs, input);
1173
1174    /* process pending events */
1175    _input_events_process(input);
1176
1177    return EINA_TRUE;
1178
1179 err:
1180    if (input->libinput) libinput_unref(input->libinput);
1181    free(input);
1182
1183    return EINA_FALSE;
1184 }
1185
1186 void
1187 e_input_device_output_changed(E_Input_Device *dev)
1188 {
1189    E_Input_Seat *seat = NULL;
1190    E_Input_Evdev *edev = NULL;
1191    Eina_List *l = NULL, *l2 = NULL;
1192
1193    EINA_SAFETY_ON_NULL_RETURN(dev);
1194    EINA_SAFETY_ON_NULL_RETURN(dev->seats);
1195
1196    EINA_LIST_FOREACH(dev->seats, l, seat)
1197      {
1198         EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l2, edev)
1199           {
1200              _device_calibration_set(edev);
1201           }
1202      }
1203 }
1204
1205 E_API const Eina_List *
1206 e_input_devices_get(void)
1207 {
1208    return einput_devices;
1209 }
1210
1211 E_API Eina_Bool
1212 e_input_device_mouse_accel_speed_set(E_Input_Device *dev, double speed)
1213 {
1214    E_Input_Seat *seat = NULL;
1215    E_Input_Evdev *edev = NULL;
1216    Eina_List *l = NULL, *l2 = NULL;
1217    Eina_Bool res = EINA_TRUE, ret = EINA_TRUE;
1218
1219    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
1220    EINA_SAFETY_ON_NULL_RETURN_VAL(dev->seats, EINA_FALSE);
1221
1222    EINA_LIST_FOREACH(dev->seats, l, seat)
1223      {
1224         EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l2, edev)
1225           {
1226              if (edev->caps & E_INPUT_SEAT_POINTER)
1227                res = e_input_evdev_mouse_accel_speed_set(edev, speed);
1228              if (!res) ret = EINA_FALSE;
1229           }
1230      }
1231
1232    return ret;
1233 }
1234
1235 EINTERN unsigned int
1236 e_input_device_touch_pressed_get(E_Input_Device *dev)
1237 {
1238    E_Input_Seat *seat = NULL;
1239    E_Input_Evdev *edev = NULL;
1240    Eina_List *l = NULL, *l2 = NULL;
1241    unsigned int pressed = 0x0;
1242
1243    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
1244
1245    EINA_LIST_FOREACH(dev->seats, l, seat)
1246      {
1247         EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l2, edev)
1248           {
1249              if (edev->caps & E_INPUT_SEAT_TOUCH)
1250                pressed |= e_input_evdev_touch_pressed_get(edev);
1251           }
1252      }
1253
1254    return pressed;
1255 }
1256
1257 EINTERN Eina_Bool
1258 e_input_device_keyboard_remap_set(E_Input_Device *dev, int *from_keys, int *to_keys, int num)
1259 {
1260    E_Input_Seat *seat = NULL;
1261    E_Input_Evdev *edev = NULL;
1262    Eina_List *l = NULL, *l2 = NULL;
1263    Eina_Bool res = EINA_TRUE, ret = EINA_TRUE;
1264
1265    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
1266    EINA_SAFETY_ON_NULL_RETURN_VAL(dev->seats, EINA_FALSE);
1267    EINA_SAFETY_ON_NULL_RETURN_VAL(from_keys, EINA_FALSE);
1268    EINA_SAFETY_ON_NULL_RETURN_VAL(to_keys, EINA_FALSE);
1269    EINA_SAFETY_ON_TRUE_RETURN_VAL(num <= 0, EINA_FALSE);
1270
1271    EINA_LIST_FOREACH(dev->seats, l, seat)
1272      {
1273         EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l2, edev)
1274           {
1275              if (edev->caps & E_INPUT_SEAT_KEYBOARD)
1276                {
1277                   res = e_input_evdev_key_remap_enable(edev, EINA_TRUE);
1278                   if (res)
1279                      res = e_input_evdev_key_remap_set(edev, from_keys, to_keys, num);
1280                }
1281              if (!res) ret = EINA_FALSE;
1282           }
1283      }
1284
1285    return ret;
1286 }
1287
1288 EINTERN Eina_Bool
1289 e_input_device_block(E_Input_Device *dev, unsigned int type, void *client)
1290 {
1291    E_Input_Seat *seat = NULL;
1292    E_Input_Evdev *edev = NULL;
1293    Eina_List *l = NULL, *l2 = NULL;
1294
1295    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
1296
1297    if (client && client != E_INPUT_REQUEST_SERVER)
1298      {
1299         if (dev->blocked_client && (dev->blocked_client != client))
1300           {
1301              WRN("Already blocked by client: %p (type: 0x%x)\n", dev->blocked_client, dev->blocked);
1302              return EINA_FALSE;
1303           }
1304
1305         dev->blocked |= type;
1306         dev->blocked_client = client;
1307      }
1308    else if (client == E_INPUT_REQUEST_SERVER)
1309      {
1310         dev->server_blocked = type;
1311      }
1312
1313    if (type & E_INPUT_SEAT_TOUCH)
1314      {
1315         EINA_LIST_FOREACH(dev->seats, l, seat)
1316           {
1317              EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l2, edev)
1318                {
1319                   edev->touch.blocked = EINA_TRUE;
1320                }
1321           }
1322      }
1323
1324    return EINA_TRUE;
1325 }
1326
1327 EINTERN Eina_Bool
1328 e_input_device_unblock(E_Input_Device *dev, void *client)
1329 {
1330    E_Input_Seat *seat = NULL;
1331    E_Input_Evdev *edev = NULL;
1332    Eina_List *l = NULL, *l2 = NULL;
1333
1334    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
1335
1336    if (client != E_INPUT_REQUEST_SERVER && dev->blocked_client != client)
1337      {
1338         WRN("Currently %p client has already bloked (type: 0x%x)\n", dev->blocked_client, dev->blocked);
1339         return EINA_FALSE;
1340      }
1341
1342    if ((dev->server_blocked & E_INPUT_SEAT_TOUCH) ||
1343        (dev->blocked & E_INPUT_SEAT_TOUCH))
1344      {
1345         EINA_LIST_FOREACH(dev->seats, l, seat)
1346           {
1347              EINA_LIST_FOREACH(e_input_seat_evdev_list_get(seat), l2, edev)
1348                {
1349                   if (edev->touch.raw_pressed == 0x0)
1350                     edev->touch.blocked = EINA_FALSE;
1351                }
1352           }
1353      }
1354
1355    if (client == E_INPUT_REQUEST_SERVER)
1356      {
1357         dev->server_blocked = 0x0;
1358      }
1359    else
1360      {
1361         dev->blocked = 0x0;
1362         dev->blocked_client = NULL;
1363      }
1364
1365    return EINA_TRUE;
1366 }
1367
1368 EINTERN Eina_Bool
1369 e_input_device_output_name_set(E_Input_Device *dev, const char *input, const char *output)
1370 {
1371    E_Input_Seat *seat;
1372    E_Input_Evdev *edev = NULL;
1373    Eina_List *l, *ll;
1374    Eina_Bool found = EINA_FALSE;
1375
1376    if (!dev)
1377      dev = _e_input_device_default_get();
1378
1379    EINA_SAFETY_ON_TRUE_RETURN_VAL(!dev, EINA_FALSE);
1380    EINA_SAFETY_ON_TRUE_RETURN_VAL(!input, EINA_FALSE);
1381    EINA_SAFETY_ON_TRUE_RETURN_VAL(!output, EINA_FALSE);
1382    EINA_LIST_FOREACH(dev->seats, l, seat)
1383      {
1384         EINA_LIST_FOREACH(seat->devices, ll, edev)
1385           {
1386              if (!e_util_strcmp(edev->path, input))
1387                {
1388                   found = EINA_TRUE;
1389                   break;
1390                }
1391           }
1392         if (found) break;
1393      }
1394
1395    if (!found || !edev)
1396      {
1397         ERR("Failed to find input device: %s", input);
1398         return EINA_FALSE;
1399      }
1400
1401    if (e_output_find(output))
1402      {
1403         INF("output device found: %s", output);
1404         eina_stringshare_replace(&edev->output_name, output);
1405         edev->output_configured = EINA_FALSE;
1406         return EINA_TRUE;
1407      }
1408    else
1409      ERR("Failed to find output device: %s", output);
1410
1411    return EINA_FALSE;
1412 }
1413
1414 EINTERN const char *
1415 e_input_device_output_name_get(E_Input_Device *dev, const char *input)
1416 {
1417    E_Input_Seat *seat;
1418    E_Input_Evdev *edev = NULL;
1419    Eina_List *l, *ll;
1420    Eina_Bool found = EINA_FALSE;
1421
1422    if (!dev)
1423      dev = _e_input_device_default_get();
1424
1425    EINA_SAFETY_ON_TRUE_RETURN_VAL(!dev, NULL);
1426    EINA_SAFETY_ON_TRUE_RETURN_VAL(!input, NULL);
1427    EINA_LIST_FOREACH(dev->seats, l, seat)
1428      {
1429         EINA_LIST_FOREACH(seat->devices, ll, edev)
1430           {
1431              if (!e_util_strcmp(edev->path, input))
1432                {
1433                   found = EINA_TRUE;
1434                   break;
1435                }
1436           }
1437         if (found) break;
1438      }
1439    if (!found || !edev)
1440      {
1441         ERR("Failed to find input device: %s", input);
1442         return NULL;
1443      }
1444    return edev->output_name;
1445 }
1446
1447 EINTERN Eina_Bool
1448 e_input_device_seat_name_set(E_Input_Device *dev, const char *input, const char *seat_name)
1449 {
1450    E_Input_Seat *seat;
1451    E_Input_Evdev *edev = NULL;
1452    Eina_List *l, *ll;
1453    Eina_Bool found = EINA_FALSE;
1454
1455    if (!dev)
1456      dev = _e_input_device_default_get();
1457
1458    EINA_SAFETY_ON_TRUE_RETURN_VAL(!dev, EINA_FALSE);
1459    EINA_SAFETY_ON_TRUE_RETURN_VAL(!input, EINA_FALSE);
1460    EINA_SAFETY_ON_TRUE_RETURN_VAL(!seat_name, EINA_FALSE);
1461    EINA_LIST_FOREACH(dev->seats, l, seat)
1462      {
1463         EINA_LIST_FOREACH(seat->devices, ll, edev)
1464           {
1465              if (!e_util_strcmp(edev->path, input))
1466                {
1467                   found = EINA_TRUE;
1468                   break;
1469                }
1470           }
1471         if (found) break;
1472      }
1473
1474    if (!found || !edev)
1475      {
1476         ERR("Failed to find input device: %s", input);
1477         return EINA_FALSE;
1478      }
1479
1480    INF("Current seatname:%s", e_input_evdev_seatname_get(edev));
1481    if (e_input_evdev_seatname_set(edev, seat_name))
1482      {
1483         INF("New seatname is now set: %s", seat_name);
1484         return EINA_TRUE;
1485      }
1486    else
1487      ERR("Failed to set new seatname: %s", seat_name);
1488
1489    return EINA_FALSE;
1490 }
1491
1492 EINTERN Eina_Bool
1493 e_input_device_subtype_set(E_Input_Device *dev, const char *input, const char *subtype_name)
1494 {
1495    Ecore_Device *ecore_dev = NULL;
1496    E_Device *e_dev = NULL;
1497    Ecore_Device_Subclass device_subclas = ECORE_DEVICE_SUBCLASS_NONE;
1498
1499    if (!dev)
1500      dev = _e_input_device_default_get();
1501
1502    EINA_SAFETY_ON_TRUE_RETURN_VAL(!dev, EINA_FALSE);
1503    EINA_SAFETY_ON_TRUE_RETURN_VAL(!input, EINA_FALSE);
1504    EINA_SAFETY_ON_TRUE_RETURN_VAL(!subtype_name, EINA_FALSE);
1505
1506    //TODO : find device other than keyboard type.
1507    //right now, subclas is only used with keyboard type.
1508    if (!e_input_thread_mode_get())
1509      {
1510         ecore_dev = e_input_evdev_get_ecore_device(input, ECORE_DEVICE_CLASS_KEYBOARD);
1511         if (!ecore_dev)
1512           {
1513              ERR("Failed to find input device: %s", input);
1514              return EINA_FALSE;
1515           }
1516
1517         device_subclas = ecore_device_subclass_get(ecore_dev);
1518
1519         if (!e_util_strcmp(subtype_name, "Remocon") &&
1520             device_subclas != ECORE_DEVICE_SUBCLASS_REMOCON)
1521           {
1522              ecore_device_subclass_set(ecore_dev, ECORE_DEVICE_SUBCLASS_REMOCON);
1523              INF("device(%s) subtype is changed to Remocon(%d->%d)", input,
1524                  device_subclas, ECORE_DEVICE_SUBCLASS_REMOCON);
1525           }
1526         else if (!e_util_strcmp(subtype_name, "None") &&
1527                  device_subclas != ECORE_DEVICE_SUBCLASS_NONE)
1528           {
1529              ecore_device_subclass_set(ecore_dev, ECORE_DEVICE_SUBCLASS_NONE);
1530              INF("device(%s) subtype is changed to None(%d->%d)", input,
1531                  device_subclas, ECORE_DEVICE_SUBCLASS_NONE);
1532           }
1533         else
1534           {
1535              ERR("Failed to set new subtype: %s (current:%d)", subtype_name, device_subclas);
1536              return EINA_FALSE;
1537           }
1538      }
1539    else
1540      {
1541         e_dev = e_input_evdev_get_e_device(input, ECORE_DEVICE_CLASS_KEYBOARD);
1542         if (!e_dev)
1543           {
1544              ERR("Failed to find input device: %s", input);
1545              return EINA_FALSE;
1546           }
1547
1548         device_subclas = e_device_subclass_get(e_dev);
1549
1550         if (!e_util_strcmp(subtype_name, "Remocon") &&
1551             device_subclas != ECORE_DEVICE_SUBCLASS_REMOCON)
1552           {
1553              e_device_subclass_set(e_dev, ECORE_DEVICE_SUBCLASS_REMOCON);
1554              INF("device(%s) subtype is changed to Remocon(%d->%d)", input,
1555                  device_subclas, ECORE_DEVICE_SUBCLASS_REMOCON);
1556           }
1557         else if (!e_util_strcmp(subtype_name, "None") &&
1558                  device_subclas != ECORE_DEVICE_SUBCLASS_NONE)
1559           {
1560              e_device_subclass_set(e_dev, ECORE_DEVICE_SUBCLASS_NONE);
1561              INF("device(%s) subtype is changed to None(%d->%d)", input,
1562                  device_subclas, ECORE_DEVICE_SUBCLASS_NONE);
1563           }
1564         else
1565           {
1566              ERR("Failed to set new subtype: %s (current:%d)", subtype_name, device_subclas);
1567              return EINA_FALSE;
1568           }
1569      }
1570
1571    return EINA_TRUE;
1572 }