2 * Copyright © 2019 Samsung Electronics co., Ltd. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
29 #include <wayland-server.h>
30 #include <libds/log.h>
31 #include <libds/backend/libinput.h>
32 #include <libds/input_device.h>
33 #include <libds/keyboard.h>
34 #include <libds/seat.h>
35 #include <libds-tizen/keyrouter.h>
36 #include <libds-tizen/input_devicemgr.h>
37 #include <xkbcommon/xkbcommon.h>
39 #include "headless_server.h"
41 #define KEYNAME_XF86_VOLUME_RAISE "XF86VolumeRaise"
42 #define KEYNAME_XF86_VOLUME_LOWER "XF86VolumeLower"
43 #define KEYNAME_XF86_LIGHT_ON "XF86LightOn"
44 #define KEYNAME_XF86_LIGHT_OFF "XF86LightOff"
46 #define KEYCODE_XF86_VOLUME_RAISE 455
47 #define KEYCODE_XF86_VOLUME_LOWER 456
48 #define KEYCODE_XF86_LIGHT_ON 457
49 #define KEYCODE_XF86_LIGHT_OFF 458
51 typedef struct headless_input headless_input_t;
52 typedef struct headless_keyboard headless_keyboard_t;
56 headless_server_t *server;
57 headless_keyboard_t *keyboard;
58 headless_view_t *top_view;
60 struct ds_backend *backend;
62 struct ds_tizen_keyrouter *keyrouter;
63 struct ds_tizen_input_devicemgr *devicemgr;
65 struct wl_listener new_input;
66 struct wl_listener focus_change;
67 struct wl_listener top_change;
68 struct wl_listener top_view_destroy;
71 struct headless_keyboard
73 headless_input_t *input;
75 struct ds_input_device *dev;
77 struct wl_listener device_destroy;
78 struct wl_listener key;
81 static void handle_new_input(struct wl_listener *listener, void *data);
82 static void handle_focus_change(struct wl_listener *listener, void *data);
83 static void handle_top_change(struct wl_listener *listener, void *data);
84 static void handle_top_view_destroy(struct wl_listener *listener, void *data);
85 static headless_keyboard_t *create_keyboard(headless_input_t *input,
86 struct ds_input_device *dev);
87 static void keyboard_destroy(headless_keyboard_t *keyboard);
88 static void keyboard_handle_destroy(struct wl_listener *listener, void *data);
89 static void keyboard_handle_key(struct wl_listener *listener, void *data);
90 static void devicemgr_set_keymap(struct ds_tizen_input_devicemgr *devicemgr);
93 headless_input_create(headless_server_t *server)
95 headless_input_t *input;
97 input = calloc(1, sizeof *input);
101 input->server = server;
103 input->backend = ds_libinput_backend_create(server->display);
104 if (!input->backend) {
105 ds_err("Could not create libinput backend");
109 input->keyrouter = ds_tizen_keyrouter_create(server->display);
110 if (!input->keyrouter) {
111 ds_err("Could not create ds_tizen_keyrouter");
115 input->seat = ds_seat_create(server->display, "seat0");
117 ds_err("Could not create ds_seat");
121 input->devicemgr = ds_tizen_input_devicemgr_create(
122 input->backend, input->seat);
123 if (!input->devicemgr) {
124 ds_err("Could not create ds_tizen_input_devicemgr");
125 goto err_input_devicemgr;
128 devicemgr_set_keymap(input->devicemgr);
130 input->new_input.notify = handle_new_input;
131 ds_backend_add_new_input_listener(input->backend, &input->new_input);
133 input->focus_change.notify = handle_focus_change;
134 wl_signal_add(&server->events.focus_change, &input->focus_change);
136 input->top_change.notify = handle_top_change;
137 wl_signal_add(&server->events.top_change, &input->top_change);
139 ds_backend_start(input->backend);
141 ds_inf("Created Headless input(%p)", input);
148 ds_backend_destroy(input->backend);
156 headless_input_destroy(headless_input_t *input)
159 keyboard_destroy(input->keyboard);
161 if (input->top_view_destroy.notify)
162 wl_list_remove(&input->top_view_destroy.link);
164 wl_list_remove(&input->top_change.link);
165 wl_list_remove(&input->focus_change.link);
166 wl_list_remove(&input->new_input.link);
168 ds_backend_destroy(input->backend);
173 handle_focus_change(struct wl_listener *listener, void *data)
175 headless_input_t *input;
176 headless_view_t *view = data;
178 input = wl_container_of(listener, input, focus_change);
181 ds_seat_keyboard_notify_enter(input->seat, view->surface,
182 NULL, 0, NULL /* TODO */);
185 ds_seat_keyboard_notify_clear_focus(input->seat);
190 handle_top_change(struct wl_listener *listener, void *data)
192 headless_input_t *input;
193 headless_view_t *view = data;
195 input = wl_container_of(listener, input, top_change);
197 if (input->top_view_destroy.notify) {
198 wl_list_remove(&input->top_view_destroy.link);
199 input->top_view_destroy.notify = NULL;
203 input->top_view_destroy.notify = handle_top_view_destroy;
204 wl_signal_add(&view->events.destroy, &input->top_view_destroy);
207 input->top_view = view;
211 handle_top_view_destroy(struct wl_listener *listener, void *data)
213 headless_input_t *input;
215 input = wl_container_of(listener, input, top_view_destroy);
217 wl_list_remove(&input->top_view_destroy.link);
219 input->top_view_destroy.notify = NULL;
220 input->top_view = NULL;
224 handle_new_input(struct wl_listener *listener, void *data)
226 headless_input_t *input;
227 struct ds_input_device *dev = data;
228 enum ds_input_device_type dev_type;
230 input = wl_container_of(listener, input, new_input);
232 dev_type = ds_input_device_get_type(dev);
234 ds_inf("Input(%p): New input device(%p) type(%d)", input, dev, dev_type);
236 if (dev_type != DS_INPUT_DEVICE_KEYBOARD) {
237 ds_inf("Not supported device: type(%d)", dev_type);
241 if (input->keyboard) {
242 ds_inf("Already created keyboard device(%p)", input->keyboard);
246 input->keyboard = create_keyboard(input, dev);
247 if (!input->keyboard) {
248 ds_err("Could not create keyboard");
252 ds_seat_set_keyboard(input->seat, dev);
253 ds_seat_set_capabilities(input->seat, WL_SEAT_CAPABILITY_KEYBOARD);
256 static headless_keyboard_t *
257 create_keyboard(headless_input_t *input, struct ds_input_device *dev)
259 headless_keyboard_t *keyboard;
260 struct ds_keyboard *ds_keyboard;
261 struct xkb_context *context;
262 struct xkb_keymap *keymap;
264 keyboard = calloc(1, sizeof *keyboard);
266 ds_err("Could not allocate memory");
270 keyboard->input = input;
273 keyboard->device_destroy.notify = keyboard_handle_destroy;
274 ds_input_device_add_destroy_listener(dev, &keyboard->device_destroy);
276 ds_keyboard = ds_input_device_get_keyboard(dev);
278 keyboard->key.notify = keyboard_handle_key;
279 ds_keyboard_add_key_listener(ds_keyboard, &keyboard->key);
281 context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
282 keymap = xkb_keymap_new_from_names(context, NULL,
283 XKB_KEYMAP_COMPILE_NO_FLAGS);
285 ds_keyboard_set_keymap(ds_keyboard, keymap);
286 xkb_keymap_unref(keymap);
287 xkb_context_unref(context);
289 ds_inf("Input(%p): New keyboard(%p)", input, keyboard);
295 keyboard_destroy(headless_keyboard_t *keyboard)
297 ds_inf("Keyboard(%p) destroyed", keyboard);
299 wl_list_remove(&keyboard->key.link);
300 wl_list_remove(&keyboard->device_destroy.link);
305 keyboard_handle_destroy(struct wl_listener *listener, void *data)
307 headless_keyboard_t *keyboard;
309 keyboard = wl_container_of(listener, keyboard, device_destroy);
311 keyboard->input->keyboard = NULL;
313 keyboard_destroy(keyboard);
317 keyboard_handle_key(struct wl_listener *listener, void *data)
319 headless_input_t *input;
320 headless_keyboard_t *keyboard;
321 struct ds_event_keyboard_key *ev = data;
323 keyboard = wl_container_of(listener, keyboard, key);
324 input = keyboard->input;
326 ds_dbg("Keyboard(%p) KEY(%d) %s time_msec(%d) update_state(%d)", keyboard,
327 ev->keycode, (ev->state == 0) ? "RELEASED" : "PRESSED",
328 ev->time_msec, ev->update_state);
330 ds_tizen_keyrouter_notify_key(input->keyrouter, input->seat,
331 input->top_view ? input->top_view->surface : NULL,
332 ev->time_msec, ev->keycode, ev->state);
336 devicemgr_add_keymap_data(struct wl_list *list, const char *name, int keycode)
338 struct ds_tizen_input_devicemgr_keymap_data *data;
340 data = calloc(1, sizeof *data);
342 ds_err("Failed to alloc memory");
346 data->name = strdup(name);
347 data->keycode = keycode;
349 wl_list_insert(list, &data->link);
353 devicemgr_remove_keymap_data(struct wl_list *list, int keycode)
355 struct ds_tizen_input_devicemgr_keymap_data *data, *tmp;
357 wl_list_for_each_safe(data, tmp, list, link) {
358 if (data->keycode == keycode) {
359 wl_list_remove(&data->link);
367 devicemgr_set_keymap(struct ds_tizen_input_devicemgr *devicemgr)
369 struct wl_list keymap_list;
372 wl_list_init(&keymap_list);
374 devicemgr_add_keymap_data(&keymap_list, KEYNAME_XF86_VOLUME_RAISE, KEYCODE_XF86_VOLUME_RAISE);
375 devicemgr_add_keymap_data(&keymap_list, KEYNAME_XF86_VOLUME_LOWER, KEYCODE_XF86_VOLUME_LOWER);
376 devicemgr_add_keymap_data(&keymap_list, KEYNAME_XF86_LIGHT_ON, KEYCODE_XF86_LIGHT_ON);
377 devicemgr_add_keymap_data(&keymap_list, KEYNAME_XF86_LIGHT_OFF, KEYCODE_XF86_LIGHT_OFF);
379 res = ds_tizen_input_devicemgr_set_keymap_list(devicemgr, &keymap_list);
381 ds_inf("Failed to set keymap");
383 devicemgr_remove_keymap_data(&keymap_list, KEYCODE_XF86_VOLUME_RAISE);
384 devicemgr_remove_keymap_data(&keymap_list, KEYCODE_XF86_VOLUME_LOWER);
385 devicemgr_remove_keymap_data(&keymap_list, KEYCODE_XF86_LIGHT_ON);
386 devicemgr_remove_keymap_data(&keymap_list, KEYCODE_XF86_LIGHT_OFF);
390 #include <pepper-evdev.h>
391 #include <pepper-input-backend.h>
392 #include <pepper-keyrouter.h>
393 #include <pepper-devicemgr.h>
394 #include <pepper-xkb.h>
395 #include <pepper-inotify.h>
399 pepper_compositor_t *compositor;
401 pepper_evdev_t *evdev;
402 pepper_keyboard_t *keyboard;
403 pepper_pointer_t *pointer;
404 pepper_touch_t *touch;
405 pepper_input_device_t *default_device;
406 pepper_inotify_t *inotify;
408 pepper_view_t *focus_view;
409 pepper_view_t *top_view;
411 pepper_keyrouter_t *keyrouter;
412 pepper_devicemgr_t *devicemgr;
415 pepper_event_listener_t *listener_seat_keyboard_key;
416 pepper_event_listener_t *listener_seat_keyboard_add;
417 pepper_event_listener_t *listener_seat_pointer_event;
418 pepper_event_listener_t *listener_seat_pointer_add;
419 pepper_event_listener_t *listener_seat_touch_event;
420 pepper_event_listener_t *listener_seat_touch_add;
421 pepper_event_listener_t *listener_seat_add;
422 pepper_event_listener_t *listener_input_device_add;
427 const static int KEY_INPUT = 0xdeadbeaf;
429 static void headless_input_init_event_listeners(headless_input_t *hi);
430 static void headless_input_deinit_event_listeners(headless_input_t *hi);
432 /* pointer event handler */
434 _handle_pointer_event(pepper_event_listener_t *listener, pepper_object_t *object, uint32_t id, void *info, void *data)
436 pepper_input_event_t *event;
438 PEPPER_CHECK((id >= PEPPER_EVENT_POINTER_MOTION && id <= PEPPER_EVENT_POINTER_AXIS), return, "unknown event %d !\n", id);
439 PEPPER_CHECK(info, return, "Invalid event !\n");
440 PEPPER_CHECK(data, return, "Invalid data !\n");
442 event = (pepper_input_event_t *)info;
449 case PEPPER_EVENT_POINTER_MOTION:
451 PEPPER_TRACE("[%s] PEPPER_EVENT_POINTER_MOTION\n", __FUNCTION__);
453 case PEPPER_EVENT_POINTER_MOTION_ABSOLUTE:
455 PEPPER_TRACE("[%s] PEPPER_EVENT_POINTER_MOTION_ABSOLUTE\n", __FUNCTION__);
457 case PEPPER_EVENT_POINTER_BUTTON:
459 PEPPER_TRACE("[%s] PEPPER_EVENT_POINTER_BUTTON\n", __FUNCTION__);
461 case PEPPER_EVENT_POINTER_AXIS:
463 PEPPER_TRACE("[%s] PEPPER_EVENT_POINTER_AXIS\n", __FUNCTION__);
468 /* touch event handler */
470 _handle_touch_event(pepper_event_listener_t *listener, pepper_object_t *object, uint32_t id, void *info, void *data)
472 pepper_input_event_t *event;
474 PEPPER_CHECK((id >= PEPPER_EVENT_TOUCH_DOWN && id <= PEPPER_EVENT_TOUCH_CANCEL), return, "unknown event %d !\n", id);
475 PEPPER_CHECK(info, return, "Invalid event !\n");
476 PEPPER_CHECK(data, return, "Invalid data !\n");
478 event = (pepper_input_event_t *)info;
485 case PEPPER_EVENT_TOUCH_DOWN:
487 PEPPER_TRACE("[%s] PEPPER_EVENT_TOUCH_DOWN\n", __FUNCTION__);
489 case PEPPER_EVENT_TOUCH_UP:
491 PEPPER_TRACE("[%s] PEPPER_EVENT_TOUCH_UP\n", __FUNCTION__);
493 case PEPPER_EVENT_TOUCH_MOTION:
495 PEPPER_TRACE("[%s] PEPPER_EVENT_TOUCH_MOTION\n", __FUNCTION__);
497 case PEPPER_EVENT_TOUCH_FRAME:
499 PEPPER_TRACE("[%s] PEPPER_EVENT_TOUCH_FRAME\n", __FUNCTION__);
501 case PEPPER_EVENT_TOUCH_CANCEL:
503 PEPPER_TRACE("[%s] PEPPER_EVENT_TOUCH_CANCEL\n", __FUNCTION__);
508 /* seat keyboard add event handler */
510 _cb_handle_seat_keyboard_add(pepper_event_listener_t *listener, pepper_object_t *object, uint32_t id, void *info, void *data)
512 pepper_event_listener_t *h = NULL;
513 pepper_keyboard_t *keyboard = (pepper_keyboard_t *)info;
514 headless_input_t *hi = (headless_input_t *)data;
516 PEPPER_TRACE("[%s] keyboard added\n", __FUNCTION__);
518 /* FIXME: without a keymap, ecore wl2 based client must work properly. */
519 //pepper_keyboard_set_keymap_info(keyboard, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP, -1, 0);
520 pepper_xkb_keyboard_set_keymap(hi->xkb, keyboard, NULL);
522 pepper_keyrouter_set_keyboard(hi->keyrouter, keyboard);
523 h = pepper_object_add_event_listener((pepper_object_t *)keyboard, PEPPER_EVENT_KEYBOARD_KEY,
524 0, pepper_keyrouter_event_handler, hi->keyrouter);
525 PEPPER_CHECK(h, goto end, "Failed to add keyboard key listener.\n");
526 hi->listener_seat_keyboard_key = h;
527 hi->keyboard = keyboard;
532 headless_input_deinit_event_listeners(hi);
536 _cb_handle_seat_pointer_add(pepper_event_listener_t *listener, pepper_object_t *object, uint32_t id, void *info, void *data)
538 pepper_event_listener_t *h = NULL;
539 pepper_pointer_t *pointer = (pepper_pointer_t *)info;
540 headless_input_t *hi = (headless_input_t *)data;
542 PEPPER_TRACE("[%s] pointer added\n", __FUNCTION__);
544 h = pepper_object_add_event_listener((pepper_object_t *)pointer,
545 PEPPER_EVENT_POINTER_MOTION
546 | PEPPER_EVENT_POINTER_MOTION_ABSOLUTE
547 | PEPPER_EVENT_POINTER_BUTTON
548 | PEPPER_EVENT_POINTER_AXIS,
549 0, _handle_pointer_event, hi->compositor);
550 PEPPER_CHECK(h, goto end, "Failed to add pointer listener.\n");
551 hi->listener_seat_pointer_event = h;
552 hi->pointer = pointer;
557 headless_input_deinit_event_listeners(hi);
561 _cb_handle_seat_touch_add(pepper_event_listener_t *listener, pepper_object_t *object, uint32_t id, void *info, void *data)
563 pepper_event_listener_t *h = NULL;
564 pepper_touch_t *touch = (pepper_touch_t *)info;
565 headless_input_t *hi = (headless_input_t *)data;
567 PEPPER_TRACE("[%s] touch added\n", __FUNCTION__);
569 h = pepper_object_add_event_listener((pepper_object_t *)touch,
570 PEPPER_EVENT_TOUCH_DOWN
571 | PEPPER_EVENT_TOUCH_UP
572 | PEPPER_EVENT_TOUCH_MOTION
573 | PEPPER_EVENT_TOUCH_FRAME
574 | PEPPER_EVENT_TOUCH_CANCEL,
575 0, _handle_touch_event, hi->compositor);
576 PEPPER_CHECK(h, goto end, "Failed to add touch listener.\n");
577 hi->listener_seat_touch_event = h;
583 headless_input_deinit_event_listeners(hi);
586 /* compositor input device add event handler */
588 _cb_handle_input_device_add(pepper_event_listener_t *listener, pepper_object_t *object, uint32_t id, void *info, void *data)
590 pepper_input_device_t *device = (pepper_input_device_t *)info;
591 headless_input_t *hi = (headless_input_t *)data;
593 PEPPER_TRACE("[%s] input device added.\n", __FUNCTION__);
596 pepper_seat_add_input_device(hi->seat, device);
599 /* seat add event handler */
601 _cb_handle_seat_add(pepper_event_listener_t *listener, pepper_object_t *object, uint32_t id, void *info, void *data)
603 pepper_event_listener_t *key = NULL;
604 pepper_event_listener_t *pointer = NULL;
605 pepper_event_listener_t *touch = NULL;
606 pepper_seat_t *seat = (pepper_seat_t *)info;
607 headless_input_t *hi = (headless_input_t *)data;
609 PEPPER_TRACE("[%s] seat added. name:%s\n", __FUNCTION__, pepper_seat_get_name(seat));
611 key = pepper_object_add_event_listener((pepper_object_t *)seat, PEPPER_EVENT_SEAT_KEYBOARD_ADD,
612 0, _cb_handle_seat_keyboard_add, hi);
614 PEPPER_CHECK(key, goto end, "Failed to add seat keyboard add listener.\n");
615 hi->listener_seat_keyboard_add = key;
617 pointer = pepper_object_add_event_listener((pepper_object_t *)seat, PEPPER_EVENT_SEAT_POINTER_ADD,
618 0, _cb_handle_seat_pointer_add, hi);
619 PEPPER_CHECK(pointer, goto end, "Failed to add seat pointer add listener.\n");
620 hi->listener_seat_pointer_add = pointer;
622 touch = pepper_object_add_event_listener((pepper_object_t *)seat, PEPPER_EVENT_SEAT_TOUCH_ADD,
623 0, _cb_handle_seat_touch_add, hi);
624 PEPPER_CHECK(touch, goto end, "Failed to add seat touch add listener.\n");
625 hi->listener_seat_touch_add = touch;
630 headless_input_deinit_event_listeners(hi);
634 _cb_handle_inotify_event(uint32_t type, pepper_inotify_event_t *ev, void *data)
636 headless_input_t *hi = data;
638 PEPPER_CHECK(hi, return, "Invalid headless input\n");
642 case PEPPER_INOTIFY_EVENT_TYPE_CREATE:
643 pepper_evdev_device_path_add(hi->evdev, pepper_inotify_event_name_get(ev));
645 case PEPPER_INOTIFY_EVENT_TYPE_REMOVE:
646 pepper_evdev_device_path_remove(hi->evdev, pepper_inotify_event_name_get(ev));
648 case PEPPER_INOTIFY_EVENT_TYPE_MODIFY:
649 pepper_evdev_device_path_remove(hi->evdev, pepper_inotify_event_name_get(ev));
650 pepper_evdev_device_path_add(hi->evdev, pepper_inotify_event_name_get(ev));
658 headless_input_get_keyrouter(pepper_compositor_t *compositor)
660 headless_input_t *hi;
661 hi = pepper_object_get_user_data((pepper_object_t *)compositor, &KEY_INPUT);
662 PEPPER_CHECK(hi, return NULL, "input system is not initialized\n");
664 return hi->keyrouter;
668 headless_input_get_xkb(pepper_compositor_t *compositor)
670 headless_input_t *hi;
671 hi = pepper_object_get_user_data((pepper_object_t *)compositor, &KEY_INPUT);
672 PEPPER_CHECK(hi, return NULL, "input system is not initialized\n");
678 headless_input_set_focus_view(pepper_compositor_t *compositor, pepper_view_t *focus_view)
680 headless_input_t *hi;
682 hi = (headless_input_t *)pepper_object_get_user_data((pepper_object_t *) compositor, &KEY_INPUT);
683 PEPPER_CHECK(hi, return, "Invalid headless input.\n");
685 if (hi->focus_view != focus_view)
687 pepper_keyboard_send_leave(hi->keyboard, hi->focus_view);
688 pepper_keyboard_set_focus(hi->keyboard, focus_view);
689 pepper_keyboard_send_enter(hi->keyboard, focus_view);
691 hi->focus_view = focus_view;
695 pepper_keyrouter_set_focus_view(hi->keyrouter, focus_view);
699 headless_input_set_top_view(void *compositor, pepper_view_t *top_view)
701 headless_input_t *hi;
703 hi = (headless_input_t *)pepper_object_get_user_data((pepper_object_t *) compositor, &KEY_INPUT);
704 PEPPER_CHECK(hi, return, "Invalid headless input.\n");
706 if (hi->top_view == top_view) return;
708 hi->top_view = top_view;
711 pepper_keyrouter_set_top_view(hi->keyrouter, top_view);
715 headless_input_init_event_listeners(headless_input_t *hi)
717 pepper_event_listener_t *h = NULL;
718 pepper_object_t *compositor = (pepper_object_t *)hi->compositor;
720 /* register event listeners */
721 h = pepper_object_add_event_listener((pepper_object_t *)compositor,
722 PEPPER_EVENT_COMPOSITOR_SEAT_ADD, 0, _cb_handle_seat_add, hi);
723 PEPPER_CHECK(h, goto end, "Failed to add seat add listener.\n");
724 hi->listener_seat_add = h;
726 h = pepper_object_add_event_listener((pepper_object_t *)compositor,
727 PEPPER_EVENT_COMPOSITOR_INPUT_DEVICE_ADD, 0, _cb_handle_input_device_add, hi);
728 PEPPER_CHECK(h, goto end, "Failed to add input device add listener.\n");
729 hi->listener_input_device_add = h;
734 PEPPER_ERROR("[%s] Failed to init listeners", __FUNCTION__);
735 headless_input_deinit_event_listeners(hi);
739 headless_input_deinit_event_listeners(headless_input_t *hi)
741 if(hi->listener_seat_keyboard_key) pepper_event_listener_remove(hi->listener_seat_keyboard_key);
742 if(hi->listener_seat_keyboard_add) pepper_event_listener_remove(hi->listener_seat_keyboard_add);
743 if(hi->listener_seat_pointer_add) pepper_event_listener_remove(hi->listener_seat_pointer_add);
744 if(hi->listener_seat_pointer_event) pepper_event_listener_remove(hi->listener_seat_pointer_event);
745 if(hi->listener_seat_touch_add) pepper_event_listener_remove(hi->listener_seat_touch_add);
746 if(hi->listener_seat_touch_event) pepper_event_listener_remove(hi->listener_seat_touch_event);
747 pepper_event_listener_remove(hi->listener_seat_add);
748 pepper_event_listener_remove(hi->listener_input_device_add);
750 PEPPER_TRACE("[%s] event listeners have been removed.\n", __FUNCTION__);
754 headless_input_deinit_input(headless_input_t *hi)
758 pepper_inotify_destroy(hi->inotify);
762 if (hi->default_device)
764 pepper_input_device_destroy(hi->default_device);
765 hi->default_device = NULL;
768 pepper_evdev_destroy(hi->evdev);
771 pepper_seat_destroy(hi->seat);
779 headless_input_create_input_device(headless_input_t *hi, uint32_t caps)
781 pepper_input_device_t *input_device = NULL;
783 /* create a default pepper input device */
784 input_device = pepper_input_device_create(hi->compositor, caps, NULL, hi);
785 PEPPER_CHECK(input_device, return PEPPER_FALSE, "Failed to create a keyboard device !\n");
787 hi->default_device = input_device;
792 headless_input_init_input(headless_input_t *hi)
796 pepper_bool_t res = PEPPER_FALSE;
797 pepper_evdev_t *evdev = NULL;
798 pepper_inotify_t *inotify = NULL;
800 /* create pepper evdev */
801 evdev = pepper_evdev_create(hi->compositor);
802 PEPPER_CHECK(evdev, goto end, "Failed to create evdev !\n");
806 /* get capabilities for a default pepper input device*/
807 if (getenv("WAYLAND_INPUT_KEYBOARD"))
808 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
809 if (getenv("WAYLAND_INPUT_POINTER"))
810 caps |= WL_SEAT_CAPABILITY_POINTER;
811 if (getenv("WAYLAND_INPUT_TOUCH"))
812 caps |= WL_SEAT_CAPABILITY_TOUCH;
815 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
816 probed = pepper_evdev_device_probe(evdev, caps);
821 PEPPER_TRACE("No evdev device has been probed. A default key device will be created.\n");
823 res = headless_input_create_input_device(hi, caps);
824 PEPPER_CHECK(res, goto end, "Failed to create any input device(s) !\n");
829 hi->ndevices = probed;
831 PEPPER_TRACE("%d evdev device(s) has been found.\n", probed);
833 inotify = pepper_inotify_create(hi->compositor, _cb_handle_inotify_event, hi);
834 PEPPER_CHECK(inotify, goto end, "Failed to create inotify\n");
836 pepper_inotify_add(inotify, "/dev/input/");
838 hi->inotify = inotify;
843 pepper_evdev_destroy(evdev);
849 headless_input_init_modules(headless_input_t *hi)
851 const char *seat_name = NULL;
852 pepper_seat_t *seat = NULL;
854 pepper_keyrouter_t *keyrouter = NULL;
855 pepper_devicemgr_t *devicemgr = NULL;
856 pepper_xkb_t *xkb = NULL;
858 PEPPER_TRACE("[%s] ... begin\n", __FUNCTION__);
860 seat_name = getenv("XDG_SEAT");
865 /* create a default seat (seat0) */
866 seat = pepper_compositor_add_seat(hi->compositor, seat_name);
867 PEPPER_CHECK(seat, goto end, "Failed to add seat (%s)!\n", seat_name);
871 /* create pepper xkb */
872 xkb = pepper_xkb_create();
873 PEPPER_CHECK(xkb, goto end, "Failed to create pepper_xkb !\n");
877 /* create pepper keyrouter */
878 keyrouter = pepper_keyrouter_create(hi->compositor);
879 PEPPER_CHECK(keyrouter, goto end, "Failed to create keyrouter !\n");
881 hi->keyrouter = keyrouter;
883 /* create pepper devicemgr */
884 devicemgr = pepper_devicemgr_create(hi->compositor, hi->seat);
885 PEPPER_CHECK(devicemgr, goto end, "Failed to create devicemgr !\n");
886 pepper_devicemgr_xkb_enable(devicemgr);
888 hi->devicemgr = devicemgr;
890 PEPPER_TRACE("[%s] ... done\n", __FUNCTION__);
895 pepper_xkb_destroy(hi->xkb);
897 pepper_keyrouter_destroy(hi->keyrouter);
899 pepper_devicemgr_destroy(hi->devicemgr);
901 pepper_seat_destroy(hi->seat);
904 hi->keyrouter = NULL;
905 hi->devicemgr = NULL;
910 headless_input_deinit_modules(headless_input_t *hi)
913 pepper_xkb_destroy(hi->xkb);
915 pepper_keyrouter_destroy(hi->keyrouter);
917 pepper_devicemgr_destroy(hi->devicemgr);
920 hi->keyrouter = NULL;
921 hi->devicemgr = NULL;
925 headless_input_deinit(pepper_compositor_t * compositor)
927 headless_input_t *hi = NULL;
929 hi = (headless_input_t *)pepper_object_get_user_data((pepper_object_t *) compositor, &KEY_INPUT);
930 PEPPER_CHECK(hi, return, "Failed to get headless input instance.\n");
932 headless_input_deinit_event_listeners(hi);
933 headless_input_deinit_modules(hi);
934 headless_input_deinit_input(hi);
936 pepper_object_set_user_data((pepper_object_t *)hi->compositor, &KEY_INPUT, NULL, NULL);
941 headless_input_init(pepper_compositor_t *compositor)
943 headless_input_t *hi = NULL;
944 pepper_bool_t init = PEPPER_FALSE;
946 hi = (headless_input_t*)calloc(1, sizeof(headless_input_t));
947 PEPPER_CHECK(hi, goto error, "Failed to alloc for input\n");
948 hi->compositor = compositor;
950 headless_input_init_event_listeners(hi);
951 headless_input_init_modules(hi);
952 init = headless_input_init_input(hi);
953 PEPPER_CHECK(init, goto error, "headless_input_init_input() failed\n");
955 pepper_object_set_user_data((pepper_object_t *)compositor, &KEY_INPUT, hi, NULL);
960 headless_input_deinit(compositor);