Handle ds_seat 88/279088/1
authorSeunghun Lee <shiin.lee@samsung.com>
Wed, 29 Jun 2022 08:37:26 +0000 (17:37 +0900)
committerSeunghun Lee <shiin.lee@samsung.com>
Tue, 2 Aug 2022 01:18:11 +0000 (10:18 +0900)
Change-Id: I2ab777726cb7edef0e53261bfe292f7ed718fc31

src/headless_server.c
src/headless_server.h
src/input/input.c

index ef8a773..413546e 100644 (file)
@@ -119,6 +119,8 @@ int main(int argc, char *argv[])
 
        wl_list_init(&server.views);
 
+       wl_signal_init(&server.events.focus_change);
+
        server.compositor = ds_compositor_create(server.display);
        HEADLESS_CHECK(server.compositor, goto end, "Failed to create ds_compositor");
 
@@ -130,7 +132,7 @@ int main(int argc, char *argv[])
        HEADLESS_CHECK(server.debug, goto end, "headless_debug_create() failed\n");
 
        /* Init input for headless */
-       server.input = headless_input_create(server.display);
+       server.input = headless_input_create(&server);
        HEADLESS_CHECK(server.input, goto end, "headless_input_create() failed\n");
 
        /* Init Output */
@@ -227,10 +229,7 @@ view_handle_shell_surface_destroy(struct wl_listener *listener, void *data)
        server = view->server;
 
        if (server->top_view == view)
-               server_set_top_view(server, NULL);
-
-       if (server->focus_view == view)
-               server_set_focus_view(server, NULL);
+               server->top_view = NULL;
 
        server_schedule_idle_task(server);
 
@@ -383,7 +382,7 @@ server_set_focus_view(headless_server_t *server, headless_view_t *view)
 
        server->focus_view = view;
 
-       // TODO handle input and debug
+       wl_signal_emit(&server->events.focus_change, view);
 }
 
 static void
index 22205fa..de093b6 100644 (file)
@@ -94,6 +94,10 @@ struct headless_server
 
        struct wl_list views; // headless_view::link
 
+       struct {
+               struct wl_signal focus_change;
+       } events;
+
        bool boot_animating;
 };
 
@@ -157,7 +161,7 @@ headless_debug_t *headless_debug_create(headless_server_t *server);
 void headless_debug_destroy(headless_debug_t *hdebug);;
 
 /* APIs for headless_input */
-headless_input_t *headless_input_create(struct wl_display *display);
+headless_input_t *headless_input_create(headless_server_t *server);
 void headless_input_destroy(headless_input_t *input);
 
 #ifdef __cplusplus
index c4f97af..8153540 100644 (file)
 #include <libds/backend/libinput.h>
 #include <libds/input_device.h>
 #include <libds/keyboard.h>
+#include <libds/seat.h>
+#include <xkbcommon/xkbcommon.h>
+
+#include "headless_server.h"
 
 typedef struct headless_input headless_input_t;
 typedef struct headless_keyboard headless_keyboard_t;
 
 struct headless_input
 {
+       headless_server_t *server;
        headless_keyboard_t *keyboard;
 
-       struct wl_display *display;
        struct ds_backend *backend;
+       struct ds_seat *seat;
 
        struct wl_listener new_input;
+       struct wl_listener focus_change;
 };
 
 struct headless_keyboard
@@ -55,14 +61,16 @@ struct headless_keyboard
        struct wl_listener key;
 };
 
+static void handle_focus_change(struct wl_listener *listener, void *data);
 static void handle_new_input(struct wl_listener *listener, void *data);
-static void add_keyboard(headless_input_t *input, struct ds_input_device *dev);
+static headless_keyboard_t *create_keyboard(headless_input_t *input,
+               struct ds_input_device *dev);
 static void keyboard_destroy(headless_keyboard_t *keyboard);
 static void keyboard_handle_destroy(struct wl_listener *listener, void *data);
 static void keyboard_handle_key(struct wl_listener *listener, void *data);
 
 headless_input_t *
-headless_input_create(struct wl_display *display)
+headless_input_create(headless_server_t *server)
 {
        headless_input_t *input;
 
@@ -70,23 +78,38 @@ headless_input_create(struct wl_display *display)
        if (!input)
                return NULL;
 
-       input->display = display;
+       input->server = server;
 
-       input->backend = ds_libinput_backend_create(display);
+       input->backend = ds_libinput_backend_create(server->display);
        if (!input->backend) {
                ds_err("Could not create libinput backend");
-               free(input);
-               return NULL;
+               goto err_backend;
+       }
+
+       input->seat = ds_seat_create(server->display, "seat0");
+       if (!input->seat) {
+               ds_err("Could not create ds_seat");
+               goto err_seat;
        }
 
        input->new_input.notify = handle_new_input;
        ds_backend_add_new_input_listener(input->backend, &input->new_input);
 
+       input->focus_change.notify = handle_focus_change;
+       wl_signal_add(&server->events.focus_change, &input->focus_change);
+
        ds_backend_start(input->backend);
 
        ds_inf("Created Headless input(%p)", input);
 
        return input;
+
+err_seat:
+       ds_backend_destroy(input->backend);
+err_backend:
+       free(input);
+
+       return NULL;
 }
 
 void
@@ -95,11 +118,29 @@ headless_input_destroy(headless_input_t *input)
        if (input->keyboard)
                keyboard_destroy(input->keyboard);
 
+       wl_list_remove(&input->focus_change.link);
        wl_list_remove(&input->new_input.link);
+
        ds_backend_destroy(input->backend);
        free(input);
 }
 
+static void
+handle_focus_change(struct wl_listener *listener, void *data)
+{
+       headless_input_t *input;
+       headless_view_t *view = data;
+
+       input = wl_container_of(listener, input, focus_change);
+
+       if (view) {
+               ds_seat_keyboard_notify_enter(input->seat, view->surface,
+                               NULL, 0, NULL /* TODO */);
+       }
+       else {
+               ds_seat_keyboard_notify_clear_focus(input->seat);
+       }
+}
 
 static void
 handle_new_input(struct wl_listener *listener, void *data)
@@ -112,34 +153,40 @@ handle_new_input(struct wl_listener *listener, void *data)
 
        dev_type = ds_input_device_get_type(dev);
 
-       ds_inf("Input(%p): New input device(%p) type(%d)",
-                       input, dev, ds_input_device_get_type(dev));
+       ds_inf("Input(%p): New input device(%p) type(%d)", input, dev, dev_type);
 
-       switch (dev_type) {
-               case DS_INPUT_DEVICE_KEYBOARD:
-                       add_keyboard(input, dev);
-                       break;
-               default:
-                       ds_inf("Not supported device, type(%d)", dev_type);
-                       break;
+       if (dev_type != DS_INPUT_DEVICE_KEYBOARD) {
+               ds_inf("Not supported device: type(%d)", dev_type);
+               return;
        }
-}
-
-static void
-add_keyboard(headless_input_t *input, struct ds_input_device *dev)
-{
-       headless_keyboard_t *keyboard;
-       struct ds_keyboard *ds_keyboard;
 
        if (input->keyboard) {
                ds_inf("Already created keyboard device(%p)", input->keyboard);
                return;
        }
 
+       input->keyboard = create_keyboard(input, dev);
+       if (!input->keyboard) {
+               ds_err("Could not create keyboard");
+               return;
+       }
+
+    ds_seat_set_keyboard(input->seat, dev);
+       ds_seat_set_capabilities(input->seat, WL_SEAT_CAPABILITY_KEYBOARD);
+}
+
+static headless_keyboard_t *
+create_keyboard(headless_input_t *input, struct ds_input_device *dev)
+{
+       headless_keyboard_t *keyboard;
+       struct ds_keyboard *ds_keyboard;
+       struct xkb_context *context;
+       struct xkb_keymap *keymap;
+
        keyboard = calloc(1, sizeof *keyboard);
        if (!keyboard) {
                ds_err("Could not allocate memory");
-               return;
+               return false;
        }
 
        keyboard->input = input;
@@ -153,9 +200,16 @@ add_keyboard(headless_input_t *input, struct ds_input_device *dev)
        keyboard->key.notify = keyboard_handle_key;
        ds_keyboard_add_key_listener(ds_keyboard, &keyboard->key);
 
-       input->keyboard = keyboard;
+       context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
+       keymap = xkb_keymap_new_from_names(context, NULL,
+                       XKB_KEYMAP_COMPILE_NO_FLAGS);
+       ds_keyboard_set_keymap(ds_keyboard, keymap);
+       xkb_keymap_unref(keymap);
+       xkb_context_unref(context);
 
        ds_inf("Input(%p): New keyboard(%p)", input, keyboard);
+
+       return keyboard;
 }
 
 static void
@@ -191,6 +245,9 @@ keyboard_handle_key(struct wl_listener *listener, void *data)
        ds_inf("Keyboard(%p) key: keycode(%d), state(%d), time_msec(%d), "
                        "update_state(%d)", keyboard, event->keycode, event->state,
                        event->time_msec, event->update_state);
+
+       ds_seat_keyboard_notify_key(keyboard->input->seat, event->time_msec,
+                       event->keycode, event->state);
 }
 #else
 #include <pepper-evdev.h>