1
/* this file contains code copied from weston; the copyright notice is below */
3
* Copyright © 2013 Intel Corporation
5
* Permission is hereby granted, free of charge, to any person obtaining
6
* a copy of this software and associated documentation files (the
7
* "Software"), to deal in the Software without restriction, including
8
* without limitation the rights to use, copy, modify, merge, publish,
9
* distribute, sublicense, and/or sell copies of the Software, and to
10
* permit persons to whom the Software is furnished to do so, subject to
11
* the following conditions:
13
* The above copyright notice and this permission notice (including the
14
* next paragraph) shall be included in all copies or substantial
15
* portions of the Software.
17
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
#include "elput_private.h"
31
_cb_open_restricted(const char *path, int flags, void *data)
33
Elput_Manager *em = data;
38
if (!em->input.thread)
39
return em->interface->open(em, path, flags);
40
if (!em->interface->open_async) return ret;
41
if (ecore_thread_check(em->input.thread)) return ret;
42
ao = calloc(1, sizeof(Elput_Async_Open));
44
if (pipe2(p, O_CLOEXEC) < 0)
50
ao->path = strdup(path);
52
em->input.pipe = p[1];
53
ecore_thread_feedback(em->input.thread, ao);
54
while (!ecore_thread_check(em->input.thread))
57
fd_set rfds, wfds, exfds;
58
struct timeval tv, *t;
67
avail = select(p[0] + 1, &rfds, &wfds, &exfds, t);
70
if (read(p[0], &fd, sizeof(int)) < 1)
83
_cb_close_restricted(int fd, void *data)
88
elput_manager_close(em, fd);
91
const struct libinput_interface _input_interface =
98
_udev_seat_create(Elput_Manager *em, const char *name)
102
eseat = calloc(1, sizeof(Elput_Seat));
103
if (!eseat) return NULL;
108
eseat->name = eina_stringshare_add(name);
109
em->input.seats = eina_list_append(em->input.seats, eseat);
115
_udev_seat_destroy(Elput_Seat *eseat)
120
if (eseat->refs) return;
122
EINA_LIST_FREE(eseat->devices, edev)
123
_evdev_device_destroy(edev);
125
if (eseat->kbd) _evdev_keyboard_destroy(eseat->kbd);
127
if (eseat->ptr) _evdev_pointer_destroy(eseat->ptr);
129
if (eseat->touch) _evdev_touch_destroy(eseat->touch);
131
if (eseat->manager->input.seats)
132
eseat->manager->input.seats = eina_list_remove(eseat->manager->input.seats, eseat);
133
if (eseat->refs) return;
135
eina_stringshare_del(eseat->name);
140
_udev_seat_named_get(Elput_Manager *em, const char *name)
145
if (!name) name = "seat0";
147
EINA_LIST_FOREACH(em->input.seats, l, eseat)
148
if (!strcmp(eseat->name, name)) return eseat;
150
eseat = _udev_seat_create(em, name);
151
if (!eseat) return NULL;
157
_udev_seat_get(Elput_Manager *em, struct libinput_device *device)
159
struct libinput_seat *lseat;
162
lseat = libinput_device_get_seat(device);
163
name = libinput_seat_get_physical_name(lseat);
165
return _udev_seat_named_get(em, name);
169
_device_event_cb_free(void *data EINA_UNUSED, void *event)
171
Elput_Event_Device_Change *ev;
176
if (ev->type == ELPUT_DEVICE_REMOVED)
180
seat = ev->device->seat;
182
seat->devices = eina_list_remove(seat->devices, ev->device);
184
_evdev_device_destroy(ev->device);
191
_device_event_send(Elput_Device *edev, Elput_Device_Change_Type type)
193
Elput_Event_Device_Change *ev;
195
ev = calloc(1, sizeof(Elput_Event_Device_Change));
202
ecore_event_add(ELPUT_EVENT_DEVICE_CHANGE, ev, _device_event_cb_free, NULL);
206
_device_add(Elput_Manager *em, struct libinput_device *dev)
211
eseat = _udev_seat_get(em, dev);
214
edev = _evdev_device_create(eseat, dev);
217
eseat->devices = eina_list_append(eseat->devices, edev);
219
DBG("Input Device Added: %s", libinput_device_get_name(dev));
221
if (edev->caps & ELPUT_DEVICE_CAPS_KEYBOARD)
222
DBG("\tDevice added as Keyboard device");
224
if (edev->caps & ELPUT_DEVICE_CAPS_POINTER)
226
DBG("\tDevice added as Pointer device");
227
switch (em->input.rotation)
230
edev->swap = EINA_FALSE;
231
edev->invert_x = EINA_FALSE;
232
edev->invert_y = EINA_FALSE;
235
edev->swap = EINA_TRUE;
236
edev->invert_x = EINA_FALSE;
237
edev->invert_y = EINA_TRUE;
240
edev->swap = EINA_FALSE;
241
edev->invert_x = EINA_TRUE;
242
edev->invert_y = EINA_TRUE;
245
edev->swap = EINA_TRUE;
246
edev->invert_x = EINA_TRUE;
247
edev->invert_y = EINA_FALSE;
254
if (edev->caps & ELPUT_DEVICE_CAPS_TOUCH)
256
DBG("\tDevice added as Touch device");
259
_device_event_send(edev, ELPUT_DEVICE_ADDED);
263
_device_remove(Elput_Manager *em EINA_UNUSED, struct libinput_device *device)
267
edev = libinput_device_get_user_data(device);
270
DBG("Input Device Removed: %s", libinput_device_get_name(device));
272
_device_event_send(edev, ELPUT_DEVICE_REMOVED);
276
_udev_process_event(struct libinput_event *event)
279
struct libinput *lib;
280
struct libinput_device *dev;
283
lib = libinput_event_get_context(event);
284
dev = libinput_event_get_device(event);
285
em = libinput_get_user_data(lib);
287
switch (libinput_event_get_type(event))
289
case LIBINPUT_EVENT_DEVICE_ADDED:
290
_device_add(em, dev);
292
case LIBINPUT_EVENT_DEVICE_REMOVED:
293
_device_remove(em, dev);
304
_process_event(Elput_Manager *em, struct libinput_event *event)
306
if (_udev_process_event(event)) return;
307
if (!em->only_gesture_events)
309
if (_evdev_event_process(event)) return;
311
if (_gesture_event_process(event)) return;
315
_process_events(Elput_Manager *em)
317
struct libinput_event *event;
321
while ((ei->lib) && (event = libinput_get_event(ei->lib)))
323
_process_event(em, event);
324
libinput_event_destroy(event);
329
_cb_input_dispatch(void *data, Ecore_Fd_Handler *hdlr EINA_UNUSED)
335
if ((em->input.lib) && (libinput_dispatch(em->input.lib) != 0))
336
WRN("libinput failed to dispatch events");
344
_elput_input_init_cancel(void *data, Ecore_Thread *eth EINA_UNUSED)
346
Elput_Manager *manager;
349
manager->input.thread = NULL;
350
if (manager->input.current_pending)
352
eldbus_pending_cancel(manager->input.current_pending);
353
if (manager->input.pipe >= 0)
354
close(manager->input.pipe);
357
elput_manager_disconnect(manager);
361
_elput_input_init_end(void *data, Ecore_Thread *eth EINA_UNUSED)
363
Elput_Manager *manager;
366
manager->input.thread = NULL;
367
if (!manager->input.lib) return;
369
manager->input.hdlr =
370
ecore_main_fd_handler_add(libinput_get_fd(manager->input.lib),
371
ECORE_FD_READ, _cb_input_dispatch,
372
manager, NULL, NULL);
374
if (manager->input.hdlr)
375
_process_events(manager);
378
ERR("Could not create input fd handler");
379
libinput_unref(manager->input.lib);
380
manager->input.lib = NULL;
383
if ((manager->pending_ptr_x) || (manager->pending_ptr_y))
385
elput_input_pointer_xy_set(manager, NULL, manager->pending_ptr_x,
386
manager->pending_ptr_y);
387
manager->pending_ptr_x = 0;
388
manager->pending_ptr_y = 0;
393
_elput_input_init_notify(void *data EINA_UNUSED, Ecore_Thread *eth EINA_UNUSED, void *msg_data)
395
Elput_Async_Open *ao;
398
ao->manager->interface->open_async(ao->manager, ao->path, ao->flags);
404
_elput_input_init_thread(void *data, Ecore_Thread *eth EINA_UNUSED)
406
Elput_Manager *manager;
413
libinput_udev_create_context(&_input_interface, manager, udev);
414
if (!manager->input.lib)
416
ERR("libinput could not create udev context");
421
if (libinput_udev_assign_seat(manager->input.lib, manager->seat))
423
ERR("libinput could not assign udev seat");
424
libinput_unref(manager->input.lib);
425
manager->input.lib = NULL;
430
_elput_input_enable(Elput_Manager *manager)
432
if (!manager->input.hdlr)
434
manager->input.hdlr =
435
ecore_main_fd_handler_add(libinput_get_fd(manager->input.lib),
436
ECORE_FD_READ, _cb_input_dispatch,
437
&manager->input, NULL, NULL);
440
if (manager->input.suspended)
442
if (libinput_resume(manager->input.lib) != 0) return;
443
manager->input.suspended = EINA_FALSE;
444
_process_events(manager);
449
_elput_input_disable(Elput_Manager *manager)
454
EINA_LIST_FOREACH(manager->input.seats, l, seat)
455
seat->pending_motion = 1;
456
if (manager->input.lib) libinput_suspend(manager->input.lib);
457
_process_events(manager);
458
manager->input.suspended = EINA_TRUE;
462
elput_input_init(Elput_Manager *manager)
464
EINA_SAFETY_ON_NULL_RETURN_VAL(manager, EINA_FALSE);
465
EINA_SAFETY_ON_TRUE_RETURN_VAL(!!manager->input.hdlr, EINA_TRUE);
467
memset(&manager->input, 0, sizeof(Elput_Input));
468
manager->input.thread =
469
ecore_thread_feedback_run(_elput_input_init_thread,
470
_elput_input_init_notify,
471
_elput_input_init_end,
472
_elput_input_init_cancel, manager, 1);
473
return !!manager->input.thread;
477
elput_input_shutdown(Elput_Manager *manager)
482
EINA_SAFETY_ON_NULL_RETURN(manager);
484
ecore_main_fd_handler_del(manager->input.hdlr);
486
EINA_LIST_FOREACH_SAFE(manager->input.seats, l, ll, seat)
487
_udev_seat_destroy(seat);
489
if (manager->input.thread)
490
ecore_thread_cancel(manager->input.thread);
493
libinput_unref(manager->input.lib);
494
manager->input.lib = NULL;
499
elput_input_pointer_xy_get(Elput_Manager *manager, const char *seat, int *x, int *y)
507
EINA_SAFETY_ON_NULL_RETURN(manager);
509
/* if no seat name is passed in, just use default seat name */
510
if (!seat) seat = "seat0";
512
EINA_LIST_FOREACH(manager->input.seats, l, eseat)
514
if (!eina_streq(eseat->name, seat)) continue;
515
if (x) *x = eseat->pointer.x;
516
if (y) *y = eseat->pointer.y;
522
elput_input_pointer_xy_set(Elput_Manager *manager, const char *seat, int x, int y)
528
EINA_SAFETY_ON_NULL_RETURN(manager);
530
/* if no seat name is passed in, just use default seat name */
531
if (!seat) seat = "seat0";
533
if (eina_list_count(manager->input.seats) < 1)
535
manager->pending_ptr_x = x;
536
manager->pending_ptr_y = y;
540
EINA_LIST_FOREACH(manager->input.seats, l, eseat)
542
if (!eseat->ptr) continue;
543
if ((eseat->name) && (strcmp(eseat->name, seat)))
546
eseat->pointer.x = x;
547
eseat->pointer.y = y;
548
eseat->ptr->timestamp = ecore_loop_time_get();
550
EINA_LIST_FOREACH(eseat->devices, ll, edev)
552
if (!libinput_device_has_capability(edev->device,
553
LIBINPUT_DEVICE_CAP_POINTER))
556
_evdev_pointer_motion_send(edev);
563
elput_input_pointer_left_handed_set(Elput_Manager *manager, const char *seat, Eina_Bool left)
569
EINA_SAFETY_ON_NULL_RETURN_VAL(manager, EINA_FALSE);
571
/* if no seat name is passed in, just use default seat name */
572
if (!seat) seat = "seat0";
574
EINA_LIST_FOREACH(manager->input.seats, l, eseat)
576
if ((eseat->name) && (strcmp(eseat->name, seat)))
579
EINA_LIST_FOREACH(eseat->devices, ll, edev)
581
if (!libinput_device_has_capability(edev->device,
582
LIBINPUT_DEVICE_CAP_POINTER))
585
if (edev->left_handed == left) continue;
587
if (libinput_device_config_left_handed_set(edev->device,
589
LIBINPUT_CONFIG_STATUS_SUCCESS)
591
WRN("Failed to set left handed mode for device: %s",
592
libinput_device_get_name(edev->device));
596
edev->left_handed = !!left;
604
elput_input_pointer_max_set(Elput_Manager *manager, int maxw, int maxh)
606
EINA_SAFETY_ON_NULL_RETURN(manager);
607
manager->input.pointer_w = maxw;
608
manager->input.pointer_h = maxh;
612
elput_input_pointer_rotation_set(Elput_Manager *manager, int rotation)
618
EINA_SAFETY_ON_NULL_RETURN_VAL(manager, EINA_FALSE);
620
if ((rotation % 90 != 0) || (rotation / 90 > 3) || (rotation < 0))
623
manager->input.rotation = rotation;
625
EINA_LIST_FOREACH(manager->input.seats, l, eseat)
627
EINA_LIST_FOREACH(eseat->devices, ll, edev)
629
if (!(edev->caps & ELPUT_DEVICE_CAPS_POINTER)) continue;
634
edev->swap = EINA_FALSE;
635
edev->invert_x = EINA_FALSE;
636
edev->invert_y = EINA_FALSE;
639
edev->swap = EINA_TRUE;
640
edev->invert_x = EINA_FALSE;
641
edev->invert_y = EINA_TRUE;
644
edev->swap = EINA_FALSE;
645
edev->invert_x = EINA_TRUE;
646
edev->invert_y = EINA_TRUE;
649
edev->swap = EINA_TRUE;
650
edev->invert_x = EINA_TRUE;
651
edev->invert_y = EINA_FALSE;
663
elput_input_devices_calibrate(Elput_Manager *manager, int w, int h)
669
EINA_SAFETY_ON_NULL_RETURN(manager);
671
manager->output_w = w;
672
manager->output_h = h;
674
EINA_LIST_FOREACH(manager->input.seats, l, eseat)
676
EINA_LIST_FOREACH(eseat->devices, ll, edev)
680
_evdev_device_calibrate(edev);
686
elput_input_key_remap_enable(Elput_Manager *manager, Eina_Bool enable)
692
EINA_SAFETY_ON_NULL_RETURN_VAL(manager, EINA_FALSE);
694
EINA_LIST_FOREACH(manager->input.seats, l, eseat)
696
EINA_LIST_FOREACH(eseat->devices, ll, edev)
698
if (!(edev->caps & ELPUT_DEVICE_CAPS_KEYBOARD)) continue;
700
edev->key_remap = enable;
701
if ((!enable) && (edev->key_remap_hash))
703
eina_hash_free(edev->key_remap_hash);
704
edev->key_remap_hash = NULL;
713
elput_input_key_remap_set(Elput_Manager *manager, int *from_keys, int *to_keys, int num)
720
EINA_SAFETY_ON_NULL_RETURN_VAL(manager, EINA_FALSE);
721
EINA_SAFETY_ON_NULL_RETURN_VAL(from_keys, EINA_FALSE);
722
EINA_SAFETY_ON_NULL_RETURN_VAL(to_keys, EINA_FALSE);
723
EINA_SAFETY_ON_TRUE_RETURN_VAL((num <= 0), EINA_FALSE);
725
EINA_LIST_FOREACH(manager->input.seats, l, eseat)
727
EINA_LIST_FOREACH(eseat->devices, ll, edev)
729
if (!(edev->caps & ELPUT_DEVICE_CAPS_KEYBOARD)) continue;
731
if (!edev->key_remap) continue;
732
if (!edev->key_remap_hash)
733
edev->key_remap_hash = eina_hash_int32_new(NULL);
734
if (!edev->key_remap_hash) continue;
736
for (i = 0; i < num; i++)
738
if ((!from_keys[i]) || (!to_keys[i]))
742
for (i = 0; i < num; i++)
743
eina_hash_add(edev->key_remap_hash, &from_keys[i],
744
(void *)(intptr_t)to_keys[i]);
752
elput_input_keyboard_info_set(Elput_Manager *manager, void *context, void *keymap, int group)
757
EINA_SAFETY_ON_NULL_RETURN(manager);
758
EINA_SAFETY_ON_FALSE_RETURN((!!context) == (!!keymap));
760
if ((manager->cached.context == context) &&
761
(manager->cached.keymap == keymap))
763
if (context) xkb_context_ref(context);
764
if (keymap) xkb_keymap_ref(keymap);
765
if (manager->cached.context) xkb_context_unref(manager->cached.context);
766
if (manager->cached.keymap) xkb_keymap_unref(manager->cached.keymap);
767
manager->cached.context = context;
768
manager->cached.keymap = keymap;
769
manager->cached.group = group;
770
EINA_LIST_FOREACH(manager->input.seats, l, seat)
771
_keyboard_keymap_update(seat);
775
elput_input_keyboard_group_set(Elput_Manager *manager, int group)
779
EINA_SAFETY_ON_NULL_RETURN(manager);
781
if (manager->cached.group == group) return;
782
manager->cached.group = group;
783
EINA_LIST_FOREACH(manager->input.seats, l, seat)
784
_keyboard_group_update(seat);
788
elput_input_pointer_accel_profile_set(Elput_Manager *manager, const char *seat, uint32_t profile)
794
EINA_SAFETY_ON_NULL_RETURN(manager);
796
/* if no seat name is passed in, just use default seat name */
797
if (!seat) seat = "seat0";
799
EINA_LIST_FOREACH(manager->input.seats, l, eseat)
801
if ((eseat->name) && (strcmp(eseat->name, seat)))
804
EINA_LIST_FOREACH(eseat->devices, ll, edev)
806
if (!libinput_device_has_capability(edev->device,
807
LIBINPUT_DEVICE_CAP_POINTER))
810
if (libinput_device_config_accel_set_profile(edev->device,
812
LIBINPUT_CONFIG_STATUS_SUCCESS)
814
WRN("Failed to set acceleration profile for device: %s",
815
libinput_device_get_name(edev->device));
823
elput_input_pointer_accel_speed_set(Elput_Manager *manager, const char *seat, double speed)
829
EINA_SAFETY_ON_NULL_RETURN(manager);
831
/* if no seat name is passed in, just use default seat name */
832
if (!seat) seat = "seat0";
834
EINA_LIST_FOREACH(manager->input.seats, l, eseat)
836
if ((eseat->name) && (strcmp(eseat->name, seat)))
839
EINA_LIST_FOREACH(eseat->devices, ll, edev)
841
if (!libinput_device_has_capability(edev->device,
842
LIBINPUT_DEVICE_CAP_POINTER))
845
if (!libinput_device_config_accel_is_available(edev->device))
848
if (libinput_device_config_accel_set_speed(edev->device,
850
LIBINPUT_CONFIG_STATUS_SUCCESS)
852
WRN("Failed to set acceleration speed for device: %s",
853
libinput_device_get_name(edev->device));
861
elput_input_touch_tap_to_click_enabled_set(Elput_Manager *manager, const char *seat, Eina_Bool enabled)
866
enum libinput_config_tap_state state;
868
EINA_SAFETY_ON_NULL_RETURN(manager);
870
state = enabled ? LIBINPUT_CONFIG_TAP_ENABLED : LIBINPUT_CONFIG_TAP_DISABLED;
872
/* if no seat name is passed in, just use default seat name */
873
if (!seat) seat = "seat0";
875
EINA_LIST_FOREACH(manager->input.seats, l, eseat)
877
if ((eseat->name) && (strcmp(eseat->name, seat)))
880
EINA_LIST_FOREACH(eseat->devices, ll, edev)
882
if (!libinput_device_has_capability(edev->device,
883
LIBINPUT_DEVICE_CAP_POINTER))
886
if (libinput_device_config_tap_set_enabled(edev->device, state)
887
!= LIBINPUT_CONFIG_STATUS_SUCCESS)
889
WRN("Failed to %s tap-to-click on device: %s",
890
enabled ? "enable" : "disable",
891
libinput_device_get_name(edev->device));
899
elput_device_seat_get(const Elput_Device *dev)
901
EINA_SAFETY_ON_NULL_RETURN_VAL(dev, NULL);
905
EAPI Elput_Device_Caps
906
elput_device_caps_get(const Elput_Device *dev)
908
EINA_SAFETY_ON_NULL_RETURN_VAL(dev, 0);
912
EAPI Eina_Stringshare *
913
elput_device_output_name_get(Elput_Device *device)
915
EINA_SAFETY_ON_NULL_RETURN_VAL(device, NULL);
917
return device->output_name;
920
EAPI const Eina_List *
921
elput_seat_devices_get(const Elput_Seat *seat)
923
EINA_SAFETY_ON_NULL_RETURN_VAL(seat, NULL);
924
return seat->devices;
927
EAPI Eina_Stringshare *
928
elput_seat_name_get(const Elput_Seat *seat)
930
EINA_SAFETY_ON_NULL_RETURN_VAL(seat, NULL);
935
elput_seat_manager_get(const Elput_Seat *seat)
937
EINA_SAFETY_ON_NULL_RETURN_VAL(seat, NULL);
938
return seat->manager;