input: Use ds_tizen_input_devicemgr
[platform/core/uifw/headless-server.git] / src / input / input.c
1 /*
2 * Copyright © 2019 Samsung Electronics co., Ltd. All Rights Reserved.
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 #define USE_LIBDS
25
26 #ifdef USE_LIBDS
27 #include <stdlib.h>
28
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>
38
39 #include "headless_server.h"
40
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"
45
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
50
51 typedef struct headless_input headless_input_t;
52 typedef struct headless_keyboard headless_keyboard_t;
53
54 struct headless_input
55 {
56         headless_server_t *server;
57         headless_keyboard_t *keyboard;
58         headless_view_t *top_view;
59
60         struct ds_backend *backend;
61         struct ds_seat *seat;
62         struct ds_tizen_keyrouter *keyrouter;
63         struct ds_tizen_input_devicemgr *devicemgr;
64
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;
69 };
70
71 struct headless_keyboard
72 {
73         headless_input_t *input;
74
75         struct ds_input_device *dev;
76
77         struct wl_listener device_destroy;
78         struct wl_listener key;
79 };
80
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);
91
92 headless_input_t *
93 headless_input_create(headless_server_t *server)
94 {
95         headless_input_t *input;
96
97         input = calloc(1, sizeof *input);
98         if (!input)
99                 return NULL;
100
101         input->server = server;
102
103         input->backend = ds_libinput_backend_create(server->display);
104         if (!input->backend) {
105                 ds_err("Could not create libinput backend");
106                 goto err_backend;
107         }
108
109         input->keyrouter = ds_tizen_keyrouter_create(server->display);
110         if (!input->keyrouter) {
111                 ds_err("Could not create ds_tizen_keyrouter");
112                 goto err_keyrouter;
113         }
114
115         input->seat = ds_seat_create(server->display, "seat0");
116         if (!input->seat) {
117                 ds_err("Could not create ds_seat");
118                 goto err_seat;
119         }
120
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;
126         }
127
128         devicemgr_set_keymap(input->devicemgr);
129
130         input->new_input.notify = handle_new_input;
131         ds_backend_add_new_input_listener(input->backend, &input->new_input);
132
133         input->focus_change.notify = handle_focus_change;
134         wl_signal_add(&server->events.focus_change, &input->focus_change);
135
136         input->top_change.notify = handle_top_change;
137         wl_signal_add(&server->events.top_change, &input->top_change);
138
139         ds_backend_start(input->backend);
140
141         ds_inf("Created Headless input(%p)", input);
142
143         return input;
144
145 err_input_devicemgr:
146 err_seat:
147 err_keyrouter:
148         ds_backend_destroy(input->backend);
149 err_backend:
150         free(input);
151
152         return NULL;
153 }
154
155 void
156 headless_input_destroy(headless_input_t *input)
157 {
158         if (input->keyboard)
159                 keyboard_destroy(input->keyboard);
160
161         if (input->top_view_destroy.notify)
162                 wl_list_remove(&input->top_view_destroy.link);
163
164         wl_list_remove(&input->top_change.link);
165         wl_list_remove(&input->focus_change.link);
166         wl_list_remove(&input->new_input.link);
167
168         ds_backend_destroy(input->backend);
169         free(input);
170 }
171
172 static void
173 handle_focus_change(struct wl_listener *listener, void *data)
174 {
175         headless_input_t *input;
176         headless_view_t *view = data;
177
178         input = wl_container_of(listener, input, focus_change);
179
180         if (view) {
181                 ds_seat_keyboard_notify_enter(input->seat, view->surface,
182                                 NULL, 0, NULL /* TODO */);
183         }
184         else {
185                 ds_seat_keyboard_notify_clear_focus(input->seat);
186         }
187 }
188
189 static void
190 handle_top_change(struct wl_listener *listener, void *data)
191 {
192         headless_input_t *input;
193         headless_view_t *view = data;
194
195         input = wl_container_of(listener, input, top_change);
196
197         if (input->top_view_destroy.notify) {
198                 wl_list_remove(&input->top_view_destroy.link);
199                 input->top_view_destroy.notify = NULL;
200         }
201
202         if (view) {
203                 input->top_view_destroy.notify = handle_top_view_destroy;
204                 wl_signal_add(&view->events.destroy, &input->top_view_destroy);
205         }
206
207         input->top_view = view;
208 }
209
210 static void
211 handle_top_view_destroy(struct wl_listener *listener, void *data)
212 {
213         headless_input_t *input;
214
215         input = wl_container_of(listener, input, top_view_destroy);
216
217         wl_list_remove(&input->top_view_destroy.link);
218
219         input->top_view_destroy.notify = NULL;
220         input->top_view = NULL;
221 }
222
223 static void
224 handle_new_input(struct wl_listener *listener, void *data)
225 {
226         headless_input_t *input;
227         struct ds_input_device *dev = data;
228         enum ds_input_device_type dev_type;
229
230         input = wl_container_of(listener, input, new_input);
231
232         dev_type = ds_input_device_get_type(dev);
233
234         ds_inf("Input(%p): New input device(%p) type(%d)", input, dev, dev_type);
235
236         if (dev_type != DS_INPUT_DEVICE_KEYBOARD) {
237                 ds_inf("Not supported device: type(%d)", dev_type);
238                 return;
239         }
240
241         if (input->keyboard) {
242                 ds_inf("Already created keyboard device(%p)", input->keyboard);
243                 return;
244         }
245
246         input->keyboard = create_keyboard(input, dev);
247         if (!input->keyboard) {
248                 ds_err("Could not create keyboard");
249                 return;
250         }
251
252     ds_seat_set_keyboard(input->seat, dev);
253         ds_seat_set_capabilities(input->seat, WL_SEAT_CAPABILITY_KEYBOARD);
254 }
255
256 static headless_keyboard_t *
257 create_keyboard(headless_input_t *input, struct ds_input_device *dev)
258 {
259         headless_keyboard_t *keyboard;
260         struct ds_keyboard *ds_keyboard;
261         struct xkb_context *context;
262         struct xkb_keymap *keymap;
263
264         keyboard = calloc(1, sizeof *keyboard);
265         if (!keyboard) {
266                 ds_err("Could not allocate memory");
267                 return false;
268         }
269
270         keyboard->input = input;
271         keyboard->dev = dev;
272
273         keyboard->device_destroy.notify = keyboard_handle_destroy;
274         ds_input_device_add_destroy_listener(dev, &keyboard->device_destroy);
275
276         ds_keyboard = ds_input_device_get_keyboard(dev);
277
278         keyboard->key.notify = keyboard_handle_key;
279         ds_keyboard_add_key_listener(ds_keyboard, &keyboard->key);
280
281         context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
282         keymap = xkb_keymap_new_from_names(context, NULL,
283                         XKB_KEYMAP_COMPILE_NO_FLAGS);
284
285         ds_keyboard_set_keymap(ds_keyboard, keymap);
286         xkb_keymap_unref(keymap);
287         xkb_context_unref(context);
288
289         ds_inf("Input(%p): New keyboard(%p)", input, keyboard);
290
291         return keyboard;
292 }
293
294 static void
295 keyboard_destroy(headless_keyboard_t *keyboard)
296 {
297         ds_inf("Keyboard(%p) destroyed", keyboard);
298
299         wl_list_remove(&keyboard->key.link);
300         wl_list_remove(&keyboard->device_destroy.link);
301         free(keyboard);
302 }
303
304 static void
305 keyboard_handle_destroy(struct wl_listener *listener, void *data)
306 {
307         headless_keyboard_t *keyboard;
308
309         keyboard = wl_container_of(listener, keyboard, device_destroy);
310
311         keyboard->input->keyboard = NULL;
312
313         keyboard_destroy(keyboard);
314 }
315
316 static void
317 keyboard_handle_key(struct wl_listener *listener, void *data)
318 {
319         headless_input_t *input;
320         headless_keyboard_t *keyboard;
321         struct ds_event_keyboard_key *ev = data;
322
323         keyboard = wl_container_of(listener, keyboard, key);
324         input = keyboard->input;
325
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);
329
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);
333 }
334
335 static void
336 devicemgr_add_keymap_data(struct wl_list *list, const char *name, int keycode)
337 {
338         struct ds_tizen_input_devicemgr_keymap_data *data;
339
340         data = calloc(1, sizeof *data);
341         if (!data) {
342                 ds_err("Failed to alloc memory");
343                 return;
344         }
345
346         data->name = strdup(name);
347         data->keycode = keycode;
348
349         wl_list_insert(list, &data->link);
350 }
351
352 static void
353 devicemgr_remove_keymap_data(struct wl_list *list, int keycode)
354 {
355         struct ds_tizen_input_devicemgr_keymap_data *data, *tmp;
356
357         wl_list_for_each_safe(data, tmp, list, link) {
358                 if (data->keycode == keycode) {
359                         wl_list_remove(&data->link);
360                         free(data->name);
361                         free(data);
362                 }
363         }
364 }
365
366 static void
367 devicemgr_set_keymap(struct ds_tizen_input_devicemgr *devicemgr)
368 {
369         struct wl_list keymap_list;
370         bool res;
371
372         wl_list_init(&keymap_list);
373
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);
378
379         res = ds_tizen_input_devicemgr_set_keymap_list(devicemgr, &keymap_list);
380         if (!res)
381                 ds_inf("Failed to set keymap");
382
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);
387 }
388
389 #else
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>
396
397 typedef struct
398 {
399         pepper_compositor_t *compositor;
400         pepper_seat_t *seat;
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;
407
408         pepper_view_t *focus_view;
409         pepper_view_t *top_view;
410
411         pepper_keyrouter_t *keyrouter;
412         pepper_devicemgr_t *devicemgr;
413         pepper_xkb_t *xkb;
414
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;
423
424         uint32_t ndevices;
425 } headless_input_t;
426
427 const static int KEY_INPUT = 0xdeadbeaf;
428
429 static void headless_input_init_event_listeners(headless_input_t *hi);
430 static void headless_input_deinit_event_listeners(headless_input_t *hi);
431
432 /* pointer event handler */
433 static void
434 _handle_pointer_event(pepper_event_listener_t *listener, pepper_object_t *object, uint32_t id, void *info, void *data)
435 {
436         pepper_input_event_t *event;
437
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");
441
442         event = (pepper_input_event_t *)info;
443
444         /* TODO: */
445         (void) event;
446
447         switch (id)
448         {
449                 case PEPPER_EVENT_POINTER_MOTION:
450                         /* TODO: */
451                         PEPPER_TRACE("[%s] PEPPER_EVENT_POINTER_MOTION\n", __FUNCTION__);
452                         break;
453                 case PEPPER_EVENT_POINTER_MOTION_ABSOLUTE:
454                         /* TODO: */
455                         PEPPER_TRACE("[%s] PEPPER_EVENT_POINTER_MOTION_ABSOLUTE\n", __FUNCTION__);
456                         break;
457                 case PEPPER_EVENT_POINTER_BUTTON:
458                         /* TODO: */
459                         PEPPER_TRACE("[%s] PEPPER_EVENT_POINTER_BUTTON\n", __FUNCTION__);
460                         break;
461                 case PEPPER_EVENT_POINTER_AXIS:
462                         /* TODO: */
463                         PEPPER_TRACE("[%s] PEPPER_EVENT_POINTER_AXIS\n", __FUNCTION__);
464                         break;
465         }
466 }
467
468 /* touch event handler */
469 static void
470 _handle_touch_event(pepper_event_listener_t *listener, pepper_object_t *object, uint32_t id, void *info, void *data)
471 {
472         pepper_input_event_t *event;
473
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");
477
478         event = (pepper_input_event_t *)info;
479
480         /* TODO: */
481         (void) event;
482
483         switch (id)
484         {
485                 case PEPPER_EVENT_TOUCH_DOWN:
486                         /* TODO: */
487                         PEPPER_TRACE("[%s] PEPPER_EVENT_TOUCH_DOWN\n", __FUNCTION__);
488                         break;
489                 case PEPPER_EVENT_TOUCH_UP:
490                         /* TODO: */
491                         PEPPER_TRACE("[%s] PEPPER_EVENT_TOUCH_UP\n", __FUNCTION__);
492                         break;
493                 case PEPPER_EVENT_TOUCH_MOTION:
494                         /* TODO: */
495                         PEPPER_TRACE("[%s] PEPPER_EVENT_TOUCH_MOTION\n", __FUNCTION__);
496                         break;
497                 case PEPPER_EVENT_TOUCH_FRAME:
498                         /* TODO: */
499                         PEPPER_TRACE("[%s] PEPPER_EVENT_TOUCH_FRAME\n", __FUNCTION__);
500                         break;
501                 case PEPPER_EVENT_TOUCH_CANCEL:
502                         /* TODO: */
503                         PEPPER_TRACE("[%s] PEPPER_EVENT_TOUCH_CANCEL\n", __FUNCTION__);
504                         break;
505         }
506 }
507
508 /* seat keyboard add event handler */
509 static void
510 _cb_handle_seat_keyboard_add(pepper_event_listener_t *listener, pepper_object_t *object, uint32_t id, void *info, void *data)
511 {
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;
515
516         PEPPER_TRACE("[%s] keyboard added\n", __FUNCTION__);
517
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);
521
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;
528
529         return;
530
531 end:
532         headless_input_deinit_event_listeners(hi);
533 }
534
535 static void
536 _cb_handle_seat_pointer_add(pepper_event_listener_t *listener, pepper_object_t *object, uint32_t id, void *info, void *data)
537 {
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;
541
542         PEPPER_TRACE("[%s] pointer added\n", __FUNCTION__);
543
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;
553
554         return;
555
556 end:
557         headless_input_deinit_event_listeners(hi);
558 }
559
560 static void
561 _cb_handle_seat_touch_add(pepper_event_listener_t *listener, pepper_object_t *object, uint32_t id, void *info, void *data)
562 {
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;
566
567         PEPPER_TRACE("[%s] touch added\n", __FUNCTION__);
568
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;
578         hi->touch = touch;
579
580         return;
581
582 end:
583         headless_input_deinit_event_listeners(hi);
584 }
585
586 /* compositor input device add event handler */
587 static void
588 _cb_handle_input_device_add(pepper_event_listener_t *listener, pepper_object_t *object, uint32_t id, void *info, void *data)
589 {
590         pepper_input_device_t *device = (pepper_input_device_t *)info;
591         headless_input_t *hi = (headless_input_t *)data;
592
593         PEPPER_TRACE("[%s] input device added.\n", __FUNCTION__);
594
595         if (hi->seat)
596                 pepper_seat_add_input_device(hi->seat, device);
597 }
598
599 /* seat add event handler */
600 static void
601 _cb_handle_seat_add(pepper_event_listener_t *listener, pepper_object_t *object, uint32_t id, void *info, void *data)
602 {
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;
608
609         PEPPER_TRACE("[%s] seat added. name:%s\n", __FUNCTION__, pepper_seat_get_name(seat));
610
611         key = pepper_object_add_event_listener((pepper_object_t *)seat, PEPPER_EVENT_SEAT_KEYBOARD_ADD,
612                                                                 0, _cb_handle_seat_keyboard_add, hi);
613
614         PEPPER_CHECK(key, goto end, "Failed to add seat keyboard add listener.\n");
615         hi->listener_seat_keyboard_add = key;
616
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;
621
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;
626
627         return;
628
629 end:
630         headless_input_deinit_event_listeners(hi);
631 }
632
633 static void
634 _cb_handle_inotify_event(uint32_t type, pepper_inotify_event_t *ev, void *data)
635 {
636         headless_input_t *hi = data;
637
638         PEPPER_CHECK(hi, return, "Invalid headless input\n");
639
640         switch (type)
641         {
642                 case PEPPER_INOTIFY_EVENT_TYPE_CREATE:
643                         pepper_evdev_device_path_add(hi->evdev, pepper_inotify_event_name_get(ev));
644                         break;
645                 case PEPPER_INOTIFY_EVENT_TYPE_REMOVE:
646                         pepper_evdev_device_path_remove(hi->evdev, pepper_inotify_event_name_get(ev));
647                         break;
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));
651                         break;
652                 default:
653                         break;
654         }
655 }
656
657 PEPPER_API void *
658 headless_input_get_keyrouter(pepper_compositor_t *compositor)
659 {
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");
663
664         return hi->keyrouter;
665 }
666
667 PEPPER_API void *
668 headless_input_get_xkb(pepper_compositor_t *compositor)
669 {
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");
673
674         return hi->xkb;
675 }
676
677 void
678 headless_input_set_focus_view(pepper_compositor_t *compositor, pepper_view_t *focus_view)
679 {
680         headless_input_t *hi;
681
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");
684
685         if (hi->focus_view != focus_view)
686         {
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);
690
691                 hi->focus_view = focus_view;
692         }
693
694         if (hi->keyrouter)
695                 pepper_keyrouter_set_focus_view(hi->keyrouter, focus_view);
696 }
697
698 void
699 headless_input_set_top_view(void *compositor, pepper_view_t *top_view)
700 {
701         headless_input_t *hi;
702
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");
705
706         if (hi->top_view == top_view) return;
707
708         hi->top_view = top_view;
709
710         if (hi->keyrouter)
711                 pepper_keyrouter_set_top_view(hi->keyrouter, top_view);
712 }
713
714 static void
715 headless_input_init_event_listeners(headless_input_t *hi)
716 {
717         pepper_event_listener_t *h = NULL;
718         pepper_object_t *compositor = (pepper_object_t *)hi->compositor;
719
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;
725
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;
730
731         return;
732
733 end:
734         PEPPER_ERROR("[%s] Failed to init listeners", __FUNCTION__);
735         headless_input_deinit_event_listeners(hi);
736 }
737
738 static void
739 headless_input_deinit_event_listeners(headless_input_t *hi)
740 {
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);
749
750         PEPPER_TRACE("[%s] event listeners have been removed.\n", __FUNCTION__);
751 }
752
753 static void
754 headless_input_deinit_input(headless_input_t *hi)
755 {
756         if (hi->inotify)
757         {
758                 pepper_inotify_destroy(hi->inotify);
759                 hi->inotify = NULL;
760         }
761
762         if (hi->default_device)
763         {
764                 pepper_input_device_destroy(hi->default_device);
765                 hi->default_device = NULL;
766         }
767
768         pepper_evdev_destroy(hi->evdev);
769
770         if (hi->seat)
771                 pepper_seat_destroy(hi->seat);
772
773         hi->seat = NULL;
774         hi->evdev = NULL;
775         hi->ndevices = 0;
776 }
777
778 static pepper_bool_t
779 headless_input_create_input_device(headless_input_t *hi, uint32_t caps)
780 {
781         pepper_input_device_t *input_device = NULL;
782
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");
786
787         hi->default_device = input_device;
788         return PEPPER_TRUE;
789 }
790
791 static pepper_bool_t
792 headless_input_init_input(headless_input_t *hi)
793 {
794         uint32_t caps = 0;
795         uint32_t probed = 0;
796         pepper_bool_t res = PEPPER_FALSE;
797         pepper_evdev_t *evdev = NULL;
798         pepper_inotify_t *inotify = NULL;
799
800         /* create pepper evdev */
801         evdev = pepper_evdev_create(hi->compositor);
802         PEPPER_CHECK(evdev, goto end, "Failed to create evdev !\n");
803
804         hi->evdev = evdev;
805
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;
813
814         if(!caps)
815                 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
816         probed = pepper_evdev_device_probe(evdev, caps);
817
818
819         if (!probed)
820         {
821                 PEPPER_TRACE("No evdev device has been probed. A default key device will be created.\n");
822
823                 res = headless_input_create_input_device(hi, caps);
824                 PEPPER_CHECK(res,  goto end, "Failed to create any input device(s) !\n");
825
826                 probed++;
827         }
828
829         hi->ndevices = probed;
830
831         PEPPER_TRACE("%d evdev device(s) has been found.\n", probed);
832
833         inotify = pepper_inotify_create(hi->compositor, _cb_handle_inotify_event, hi);
834         PEPPER_CHECK(inotify, goto end, "Failed to create inotify\n");
835
836         pepper_inotify_add(inotify, "/dev/input/");
837
838         hi->inotify = inotify;
839
840         return PEPPER_TRUE;
841
842 end:
843         pepper_evdev_destroy(evdev);
844
845         return PEPPER_FALSE;
846 }
847
848 static void
849 headless_input_init_modules(headless_input_t *hi)
850 {
851         const char *seat_name = NULL;
852         pepper_seat_t *seat = NULL;
853
854         pepper_keyrouter_t *keyrouter = NULL;
855         pepper_devicemgr_t *devicemgr = NULL;
856         pepper_xkb_t *xkb = NULL;
857
858         PEPPER_TRACE("[%s] ... begin\n", __FUNCTION__);
859
860         seat_name = getenv("XDG_SEAT");
861
862         if (!seat_name)
863                 seat_name = "seat0";
864
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);
868
869         hi->seat = seat;
870
871         /* create pepper xkb */
872         xkb = pepper_xkb_create();
873         PEPPER_CHECK(xkb, goto end, "Failed to create pepper_xkb !\n");
874
875         hi->xkb = xkb;
876
877         /* create pepper keyrouter */
878         keyrouter = pepper_keyrouter_create(hi->compositor);
879         PEPPER_CHECK(keyrouter, goto end, "Failed to create keyrouter !\n");
880
881         hi->keyrouter = keyrouter;
882
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);
887
888         hi->devicemgr = devicemgr;
889
890         PEPPER_TRACE("[%s] ... done\n", __FUNCTION__);
891
892         return;
893 end:
894         if (hi->xkb)
895                 pepper_xkb_destroy(hi->xkb);
896         if (hi->keyrouter)
897                 pepper_keyrouter_destroy(hi->keyrouter);
898         if (hi->devicemgr)
899                 pepper_devicemgr_destroy(hi->devicemgr);
900         if (hi->seat)
901                 pepper_seat_destroy(hi->seat);
902
903         hi->xkb = NULL;
904         hi->keyrouter = NULL;
905         hi->devicemgr = NULL;
906         hi->seat = NULL;
907 }
908
909 static void
910 headless_input_deinit_modules(headless_input_t *hi)
911 {
912         if (hi->xkb)
913                 pepper_xkb_destroy(hi->xkb);
914         if (hi->keyrouter)
915                 pepper_keyrouter_destroy(hi->keyrouter);
916         if (hi->devicemgr)
917                 pepper_devicemgr_destroy(hi->devicemgr);
918
919         hi->xkb = NULL;
920         hi->keyrouter = NULL;
921         hi->devicemgr = NULL;
922 }
923
924 PEPPER_API void
925 headless_input_deinit(pepper_compositor_t * compositor)
926 {
927         headless_input_t *hi = NULL;
928
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");
931
932         headless_input_deinit_event_listeners(hi);
933         headless_input_deinit_modules(hi);
934         headless_input_deinit_input(hi);
935
936         pepper_object_set_user_data((pepper_object_t *)hi->compositor, &KEY_INPUT, NULL, NULL);
937         free(hi);
938 }
939
940 pepper_bool_t
941 headless_input_init(pepper_compositor_t *compositor)
942 {
943         headless_input_t *hi = NULL;
944         pepper_bool_t init = PEPPER_FALSE;
945
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;
949
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");
954
955         pepper_object_set_user_data((pepper_object_t *)compositor, &KEY_INPUT, hi, NULL);
956
957         return PEPPER_TRUE;
958
959 error:
960         headless_input_deinit(compositor);
961
962         return PEPPER_FALSE;
963 }
964 #endif