diff --git a/backend/backend.c b/backend/backend.c index 93d7e0df..23e212b6 100644 --- a/backend/backend.c +++ b/backend/backend.c @@ -182,7 +182,3 @@ struct wlr_backend *wlr_backend_autocreate(struct wl_display *display) { return backend; } - -uint32_t usec_to_msec(uint64_t usec) { - return (uint32_t)(usec / 1000); -} diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c index 1e7c1ad4..ef33df0a 100644 --- a/backend/libinput/backend.c +++ b/backend/libinput/backend.c @@ -191,3 +191,7 @@ struct libinput_device *wlr_libinput_get_device_handle(struct wlr_input_device * struct wlr_libinput_input_device *dev = (struct wlr_libinput_input_device *)_dev; return dev->handle; } + +uint32_t usec_to_msec(uint64_t usec) { + return (uint32_t)(usec / 1000); +} diff --git a/examples/idle-inhibit.c b/examples/idle-inhibit.c index c09e6507..348892ab 100644 --- a/examples/idle-inhibit.c +++ b/examples/idle-inhibit.c @@ -55,10 +55,8 @@ static void pointer_handle_button(void *data, struct wl_pointer *pointer, uint32 zwp_idle_inhibitor_v1_destroy(idle_inhibitor); idle_inhibitor = NULL; } else { - idle_inhibitor = - zwp_idle_inhibit_manager_v1_create_inhibitor( - idle_inhibit_manager, - surface); + idle_inhibitor = zwp_idle_inhibit_manager_v1_create_inhibitor( + idle_inhibit_manager, surface); } } @@ -140,7 +138,6 @@ static void xdg_toplevel_handle_close(void *data, exit(EXIT_SUCCESS); } - static const struct xdg_toplevel_listener xdg_toplevel_listener = { .configure = xdg_toplevel_handle_configure, .close = xdg_toplevel_handle_close, diff --git a/examples/input-inhibitor.c b/examples/input-inhibitor.c new file mode 100644 index 00000000..25e46c73 --- /dev/null +++ b/examples/input-inhibitor.c @@ -0,0 +1,189 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "wlr-input-inhibitor-unstable-v1-client-protocol.h" +#include "xdg-shell-client-protocol.h" + +static int width = 500, height = 300; +static int keys = 0; + +static struct wl_compositor *compositor = NULL; +static struct wl_seat *seat = NULL; +static struct xdg_wm_base *wm_base = NULL; +static struct zwlr_input_inhibit_manager_v1 *input_inhibit_manager = NULL; +static struct zwlr_input_inhibitor_v1 *input_inhibitor = NULL; + +struct wlr_egl egl; +struct wl_egl_window *egl_window; +struct wlr_egl_surface *egl_surface; + +static void render_frame(void) { + eglMakeCurrent(egl.display, egl_surface, egl_surface, egl.context); + + glViewport(0, 0, width, height); + if (keys) { + glClearColor(1.0, 1.0, 1.0, 1.0); + } else { + glClearColor(0.8, 0.4, 1.0, 1.0); + } + glClear(GL_COLOR_BUFFER_BIT); + + eglSwapBuffers(egl.display, egl_surface); +} + +static void xdg_surface_handle_configure(void *data, + struct xdg_surface *xdg_surface, uint32_t serial) { + xdg_surface_ack_configure(xdg_surface, serial); + wl_egl_window_resize(egl_window, width, height, 0, 0); + render_frame(); +} + +static const struct xdg_surface_listener xdg_surface_listener = { + .configure = xdg_surface_handle_configure, +}; + +static void xdg_toplevel_handle_configure(void *data, + struct xdg_toplevel *xdg_toplevel, int32_t w, int32_t h, + struct wl_array *states) { + width = w; + height = h; +} + +static void xdg_toplevel_handle_close(void *data, + struct xdg_toplevel *xdg_toplevel) { + exit(0); +} + +static const struct xdg_toplevel_listener xdg_toplevel_listener = { + .configure = xdg_toplevel_handle_configure, + .close = xdg_toplevel_handle_close, +}; + +static void wl_keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard, + uint32_t format, int32_t fd, uint32_t size) { +} +static void wl_keyboard_enter(void *data, struct wl_keyboard *wl_keyboard, + uint32_t serial, struct wl_surface *surface, struct wl_array *keys) { +} +static void wl_keyboard_leave(void *data, struct wl_keyboard *wl_keyboard, + uint32_t serial, struct wl_surface *surface) { +} +static void wl_keyboard_modifiers(void *data, struct wl_keyboard *wl_keyboard, + uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, + uint32_t mods_locked, uint32_t group) { +} +static void wl_keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard, + int32_t rate, int32_t delay) { +} + +static void wl_keyboard_key(void *data, struct wl_keyboard *wl_keyboard, + uint32_t serial, uint32_t time, uint32_t key, uint32_t state) { + if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { + ++keys; + } else { + --keys; + } + render_frame(); +} + +static struct wl_keyboard_listener keyboard_listener = { + .keymap = wl_keyboard_keymap, + .enter = wl_keyboard_enter, + .leave = wl_keyboard_leave, + .key = wl_keyboard_key, + .modifiers = wl_keyboard_modifiers, + .repeat_info = wl_keyboard_repeat_info, +}; + +static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, + enum wl_seat_capability caps) { + if ((caps & WL_SEAT_CAPABILITY_KEYBOARD)) { + struct wl_keyboard *keyboard = wl_seat_get_keyboard(wl_seat); + wl_keyboard_add_listener(keyboard, &keyboard_listener, NULL); + } +} + +static void seat_handle_name(void *data, struct wl_seat *wl_seat, + const char *name) { + // Who cares +} + +const struct wl_seat_listener seat_listener = { + .capabilities = seat_handle_capabilities, + .name = seat_handle_name, +}; + +static void handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version) { + if (strcmp(interface, wl_compositor_interface.name) == 0) { + compositor = wl_registry_bind(registry, name, + &wl_compositor_interface, 1); + } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) { + wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1); + } else if (strcmp(interface, zwlr_input_inhibit_manager_v1_interface.name) == 0) { + input_inhibit_manager = wl_registry_bind(registry, name, + &zwlr_input_inhibit_manager_v1_interface, 1); + } else if (strcmp(interface, wl_seat_interface.name) == 0) { + seat = wl_registry_bind(registry, name, &wl_seat_interface, version); + wl_seat_add_listener(seat, &seat_listener, seat); + } +} + +static void handle_global_remove(void *data, struct wl_registry *registry, + uint32_t name) { + // who cares +} + +static const struct wl_registry_listener registry_listener = { + .global = handle_global, + .global_remove = handle_global_remove, +}; + +int main(int argc, char **argv) { + struct wl_display *display = wl_display_connect(NULL); + assert(display); + struct wl_registry *registry = wl_display_get_registry(display); + assert(registry); + wl_registry_add_listener(registry, ®istry_listener, NULL); + wl_display_dispatch(display); + wl_display_roundtrip(display); + assert(compositor && seat && wm_base && input_inhibit_manager); + + input_inhibitor = zwlr_input_inhibit_manager_v1_get_inhibitor( + input_inhibit_manager); + assert(input_inhibitor); + + wlr_egl_init(&egl, EGL_PLATFORM_WAYLAND_EXT, display, NULL, + WL_SHM_FORMAT_ARGB8888); + + struct wl_surface *surface = wl_compositor_create_surface(compositor); + assert(surface); + struct xdg_surface *xdg_surface = + xdg_wm_base_get_xdg_surface(wm_base, surface); + assert(xdg_surface); + struct xdg_toplevel *xdg_toplevel = xdg_surface_get_toplevel(xdg_surface); + assert(xdg_toplevel); + + xdg_surface_add_listener(xdg_surface, &xdg_surface_listener, NULL); + xdg_toplevel_add_listener(xdg_toplevel, &xdg_toplevel_listener, NULL); + + wl_surface_commit(surface); + + egl_window = wl_egl_window_create(surface, width, height); + egl_surface = wlr_egl_create_surface(&egl, egl_window); + + wl_display_roundtrip(display); + + render_frame(); + + while (wl_display_dispatch(display) != -1) { + // This space intentionally left blank + } + + return 0; +} diff --git a/examples/meson.build b/examples/meson.build index 9e60c37a..6b9a1f46 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -55,3 +55,9 @@ executable( 'layer-shell.c', dependencies: [wayland_cursor, wayland_client, wlr_protos, wlroots] ) + +executable( + 'input-inhibitor', + 'input-inhibitor.c', + dependencies: [wayland_cursor, wayland_client, wlr_protos, wlroots] +) diff --git a/include/backend/libinput.h b/include/backend/libinput.h index f828c310..4eee7eb8 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -29,6 +29,8 @@ struct wlr_libinput_input_device { struct libinput_device *handle; }; +uint32_t usec_to_msec(uint64_t usec); + void wlr_libinput_event(struct wlr_libinput_backend *state, struct libinput_event *event); diff --git a/include/rootston/desktop.h b/include/rootston/desktop.h index c1dcad56..f150147b 100644 --- a/include/rootston/desktop.h +++ b/include/rootston/desktop.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -47,6 +48,7 @@ struct roots_desktop { struct wlr_primary_selection_device_manager *primary_selection_device_manager; struct wlr_idle *idle; struct wlr_idle_inhibit_manager_v1 *idle_inhibit; + struct wlr_input_inhibit_manager *input_inhibit; struct wlr_linux_dmabuf *linux_dmabuf; struct wlr_layer_shell *layer_shell; @@ -57,6 +59,8 @@ struct roots_desktop { struct wl_listener wl_shell_surface; struct wl_listener layer_shell_surface; struct wl_listener decoration_new; + struct wl_listener input_inhibit_activate; + struct wl_listener input_inhibit_deactivate; #ifdef WLR_HAS_XWAYLAND struct wlr_xwayland *xwayland; diff --git a/include/rootston/seat.h b/include/rootston/seat.h index 6f482723..d2ef90f3 100644 --- a/include/rootston/seat.h +++ b/include/rootston/seat.h @@ -19,6 +19,9 @@ struct roots_seat { // If the focused layer is set, views cannot receive keyboard focus struct wlr_layer_surface *focused_layer; + // If non-null, only this client can receive input events + struct wl_client *exclusive_client; + struct wl_list views; // roots_seat_view::link bool has_focus; @@ -125,4 +128,10 @@ void roots_drag_icon_update_position(struct roots_drag_icon *icon); void roots_drag_icon_damage_whole(struct roots_drag_icon *icon); +void roots_seat_set_exclusive_client(struct roots_seat *seat, + struct wl_client *client); + +bool roots_seat_allow_input(struct roots_seat *seat, + struct wl_resource *resource); + #endif diff --git a/include/wlr/backend.h b/include/wlr/backend.h index e3b14add..f5482e04 100644 --- a/include/wlr/backend.h +++ b/include/wlr/backend.h @@ -46,6 +46,4 @@ struct wlr_egl *wlr_backend_get_egl(struct wlr_backend *backend); */ struct wlr_renderer *wlr_backend_get_renderer(struct wlr_backend *backend); -uint32_t usec_to_msec(uint64_t usec); - #endif diff --git a/include/wlr/types/wlr_input_inhibitor.h b/include/wlr/types/wlr_input_inhibitor.h new file mode 100644 index 00000000..4416c18f --- /dev/null +++ b/include/wlr/types/wlr_input_inhibitor.h @@ -0,0 +1,25 @@ +#ifndef WLR_TYPES_INPUT_INHIBITOR_H +#define WLR_TYPES_INPUT_INHIBITOR_H +#include + +struct wlr_input_inhibit_manager { + struct wl_global *wl_global; + struct wl_client *active_client; + struct wl_resource *active_inhibitor; + + struct wl_listener display_destroy; + + struct { + struct wl_signal activate; // struct wlr_input_inhibit_manager * + struct wl_signal deactivate; // struct wlr_input_inhibit_manager * + } events; + + void *data; +}; + +struct wlr_input_inhibit_manager *wlr_input_inhibit_manager_create( + struct wl_display *display); +void wlr_input_inhibit_manager_destroy( + struct wlr_input_inhibit_manager *manager); + +#endif diff --git a/include/wlr/types/wlr_seat.h b/include/wlr/types/wlr_seat.h index 124c1cb8..4c7e34b9 100644 --- a/include/wlr/types/wlr_seat.h +++ b/include/wlr/types/wlr_seat.h @@ -530,6 +530,7 @@ bool wlr_seat_touch_has_grab(struct wlr_seat *seat); */ bool wlr_seat_validate_grab_serial(struct wlr_seat *seat, uint32_t serial); -struct wlr_seat_client *wlr_seat_client_from_resource(struct wl_resource *resource); +struct wlr_seat_client *wlr_seat_client_from_resource( + struct wl_resource *resource); #endif diff --git a/protocol/meson.build b/protocol/meson.build index a41fdec3..b88b4b77 100644 --- a/protocol/meson.build +++ b/protocol/meson.build @@ -31,6 +31,7 @@ protocols = [ 'screenshooter.xml', 'server-decoration.xml', 'wlr-layer-shell-unstable-v1.xml', + 'wlr-input-inhibitor-unstable-v1.xml', ] client_protocols = [ @@ -40,6 +41,7 @@ client_protocols = [ 'idle.xml', 'screenshooter.xml', 'wlr-layer-shell-unstable-v1.xml', + 'wlr-input-inhibitor-unstable-v1.xml', ] wl_protos_src = [] diff --git a/protocol/wlr-input-inhibitor-unstable-v1.xml b/protocol/wlr-input-inhibitor-unstable-v1.xml new file mode 100644 index 00000000..b62d1bb4 --- /dev/null +++ b/protocol/wlr-input-inhibitor-unstable-v1.xml @@ -0,0 +1,67 @@ + + + + Copyright © 2018 Drew DeVault + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that copyright notice and this permission + notice appear in supporting documentation, and that the name of + the copyright holders not be used in advertising or publicity + pertaining to distribution of the software without specific, + written prior permission. The copyright holders make no + representations about the suitability of this software for any + purpose. It is provided "as is" without express or implied + warranty. + + THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN + AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF + THIS SOFTWARE. + + + + + Clients can use this interface to prevent input events from being sent to + any surfaces but its own, which is useful for example in lock screen + software. It is assumed that access to this interface will be locked down + to whitelisted clients by the compositor. + + + + + Activates the input inhibitor. As long as the inhibitor is active, the + compositor will not send input events to other clients. + + + + + + + + + + + + While this resource exists, input to clients other than the owner of the + inhibitor resource will not receive input events. The client that owns + this resource will receive all input events normally. The compositor will + also disable all of its own input processing (such as keyboard shortcuts) + while the inhibitor is active. + + The compositor may continue to send input events to selected clients, + such as an on-screen keyboard (via the input-method protocol). + + + + + Destroy the inhibitor and allow other clients to receive input. + + + + diff --git a/rootston/cursor.c b/rootston/cursor.c index ac46ff5d..21e32a09 100644 --- a/rootston/cursor.c +++ b/rootston/cursor.c @@ -111,6 +111,9 @@ static void roots_passthrough_cursor(struct roots_cursor *cursor, if (surface) { client = wl_resource_get_client(surface->resource); } + if (surface && !roots_seat_allow_input(cursor->seat, surface->resource)) { + return; + } if (cursor->cursor_client != client) { wlr_xcursor_manager_set_cursor_image(cursor->xcursor_manager, @@ -327,7 +330,7 @@ void roots_cursor_handle_touch_down(struct roots_cursor *cursor, desktop, lx, ly, &sx, &sy, NULL); uint32_t serial = 0; - if (surface) { + if (surface && roots_seat_allow_input(cursor->seat, surface->resource)) { serial = wlr_seat_touch_notify_down(cursor->seat->seat, surface, event->time_msec, event->touch_id, sx, sy); } @@ -378,7 +381,7 @@ void roots_cursor_handle_touch_motion(struct roots_cursor *cursor, struct wlr_surface *surface = desktop_surface_at( desktop, lx, ly, &sx, &sy, NULL); - if (surface) { + if (surface && roots_seat_allow_input(cursor->seat, surface->resource)) { wlr_seat_touch_point_focus(cursor->seat->seat, surface, event->time_msec, event->touch_id, sx, sy); wlr_seat_touch_notify_motion(cursor->seat->seat, event->time_msec, diff --git a/rootston/desktop.c b/rootston/desktop.c index ab16ed3d..f6801332 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -755,6 +756,25 @@ static void handle_layout_change(struct wl_listener *listener, void *data) { } } +static void input_inhibit_activate(struct wl_listener *listener, void *data) { + struct roots_desktop *desktop = wl_container_of( + listener, desktop, input_inhibit_activate); + struct roots_seat *seat; + wl_list_for_each(seat, &desktop->server->input->seats, link) { + roots_seat_set_exclusive_client(seat, + desktop->input_inhibit->active_client); + } +} + +static void input_inhibit_deactivate(struct wl_listener *listener, void *data) { + struct roots_desktop *desktop = wl_container_of( + listener, desktop, input_inhibit_deactivate); + struct roots_seat *seat; + wl_list_for_each(seat, &desktop->server->input->seats, link) { + roots_seat_set_exclusive_client(seat, NULL); + } +} + struct roots_desktop *desktop_create(struct roots_server *server, struct roots_config *config) { wlr_log(L_DEBUG, "Initializing roots desktop"); @@ -855,6 +875,15 @@ struct roots_desktop *desktop_create(struct roots_server *server, desktop->idle = wlr_idle_create(server->wl_display); desktop->idle_inhibit = wlr_idle_inhibit_v1_create(server->wl_display); + desktop->input_inhibit = + wlr_input_inhibit_manager_create(server->wl_display); + desktop->input_inhibit_activate.notify = input_inhibit_activate; + wl_signal_add(&desktop->input_inhibit->events.activate, + &desktop->input_inhibit_activate); + desktop->input_inhibit_deactivate.notify = input_inhibit_deactivate; + wl_signal_add(&desktop->input_inhibit->events.deactivate, + &desktop->input_inhibit_deactivate); + struct wlr_egl *egl = wlr_backend_get_egl(server->backend); desktop->linux_dmabuf = wlr_linux_dmabuf_create(server->wl_display, egl); return desktop; diff --git a/rootston/seat.c b/rootston/seat.c index cfdeab00..e12df7d6 100644 --- a/rootston/seat.c +++ b/rootston/seat.c @@ -1,6 +1,8 @@ +#define _POSIX_C_SOURCE 199309L #include #include #include +#include #include #include #include @@ -716,7 +718,17 @@ struct roots_seat_view *roots_seat_view_from_view( return seat_view; } +bool roots_seat_allow_input(struct roots_seat *seat, + struct wl_resource *resource) { + return !seat->exclusive_client || + wl_resource_get_client(resource) == seat->exclusive_client; +} + void roots_seat_set_focus(struct roots_seat *seat, struct roots_view *view) { + if (view && !roots_seat_allow_input(seat, view->wlr_surface->resource)) { + return; + } + // Make sure the view will be rendered on top of others, even if it's // already focused in this seat if (view != NULL) { @@ -811,11 +823,14 @@ void roots_seat_set_focus(struct roots_seat *seat, struct roots_view *view) { */ void roots_seat_set_focus_layer(struct roots_seat *seat, struct wlr_layer_surface *layer) { - struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat->seat); if (!layer) { seat->focused_layer = NULL; return; } + struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat->seat); + if (!roots_seat_allow_input(seat, layer->resource)) { + return; + } if (seat->has_focus) { struct roots_view *prev_focus = roots_seat_get_focus(seat); wlr_seat_keyboard_clear_focus(seat->seat); @@ -835,6 +850,46 @@ void roots_seat_set_focus_layer(struct roots_seat *seat, } } +void roots_seat_set_exclusive_client(struct roots_seat *seat, + struct wl_client *client) { + if (!client) { + seat->exclusive_client = client; + // Triggers a refocus of the topmost surface layer if necessary + // TODO: Make layer surface focus per-output based on cursor position + struct roots_output *output; + wl_list_for_each(output, &seat->input->server->desktop->outputs, link) { + arrange_layers(output); + } + return; + } + if (seat->focused_layer) { + if (wl_resource_get_client(seat->focused_layer->resource) != client) { + roots_seat_set_focus_layer(seat, NULL); + } + } + if (seat->has_focus) { + struct roots_view *focus = roots_seat_get_focus(seat); + if (wl_resource_get_client(focus->wlr_surface->resource) != client) { + roots_seat_set_focus(seat, NULL); + } + } + if (seat->seat->pointer_state.focused_client) { + if (seat->seat->pointer_state.focused_client->client != client) { + wlr_seat_pointer_clear_focus(seat->seat); + } + } + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + struct wlr_touch_point *point; + wl_list_for_each(point, &seat->seat->touch_state.touch_points, link) { + if (point->client->client != client) { + wlr_seat_touch_point_clear_focus(seat->seat, + now.tv_nsec / 1000, point->touch_id); + } + } + seat->exclusive_client = client; +} + void roots_seat_cycle_focus(struct roots_seat *seat) { if (wl_list_empty(&seat->views)) { return; diff --git a/types/meson.build b/types/meson.build index 198563b1..eda5f4e4 100644 --- a/types/meson.build +++ b/types/meson.build @@ -10,6 +10,7 @@ lib_wlr_types = static_library( 'wlr_idle.c', 'wlr_idle_inhibit_v1.c', 'wlr_input_device.c', + 'wlr_input_inhibitor.c', 'wlr_keyboard.c', 'wlr_layer_shell.c', 'wlr_linux_dmabuf.c', diff --git a/types/wlr_input_inhibitor.c b/types/wlr_input_inhibitor.c new file mode 100644 index 00000000..d42a5c0c --- /dev/null +++ b/types/wlr_input_inhibitor.c @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include "wlr/types/wlr_input_inhibitor.h" +#include "wlr-input-inhibitor-unstable-v1-protocol.h" + +static const struct zwlr_input_inhibit_manager_v1_interface inhibit_manager_implementation; + +static struct wlr_input_inhibit_manager *input_inhibit_manager_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, &zwlr_input_inhibit_manager_v1_interface, + &inhibit_manager_implementation)); + return wl_resource_get_user_data(resource); +} + +static void input_inhibitor_destroy(struct wl_client *client, + struct wl_resource *resource) { + struct wlr_input_inhibit_manager *manager = + input_inhibit_manager_from_resource(resource); + manager->active_client = NULL; + manager->active_inhibitor = NULL; + wl_resource_destroy(resource); + wl_signal_emit(&manager->events.deactivate, manager); +} + +static struct zwlr_input_inhibitor_v1_interface input_inhibitor_implementation = { + .destroy = input_inhibitor_destroy, +}; + +static void inhibit_manager_get_inhibitor(struct wl_client *client, + struct wl_resource *resource, uint32_t id) { + struct wlr_input_inhibit_manager *manager = + input_inhibit_manager_from_resource(resource); + if (manager->active_client || manager->active_inhibitor) { + wl_resource_post_error(resource, + ZWLR_INPUT_INHIBIT_MANAGER_V1_ERROR_ALREADY_INHIBITED, + "this compositor already has input inhibited"); + return; + } + + struct wl_resource *wl_resource = wl_resource_create(client, + &zwlr_input_inhibitor_v1_interface, + wl_resource_get_version(resource), id); + if (!wl_resource) { + wl_client_post_no_memory(client); + } + wl_resource_set_implementation(wl_resource, &input_inhibitor_implementation, + manager, NULL); + + manager->active_client = client; + manager->active_inhibitor = wl_resource; + + wl_signal_emit(&manager->events.activate, manager); +} + +static const struct zwlr_input_inhibit_manager_v1_interface inhibit_manager_implementation = { + .get_inhibitor = inhibit_manager_get_inhibitor +}; + +static void input_manager_client_destroy(struct wl_resource *resource) { + struct wlr_input_inhibit_manager *manager = + input_inhibit_manager_from_resource(resource); + if (manager->active_client == wl_resource_get_client(resource)) { + input_inhibitor_destroy(manager->active_client, resource); + } +} + +static void inhibit_manager_bind(struct wl_client *wl_client, void *data, + uint32_t version, uint32_t id) { + struct wlr_input_inhibit_manager *manager = data; + assert(wl_client && manager); + + struct wl_resource *wl_resource = wl_resource_create(wl_client, + &zwlr_input_inhibit_manager_v1_interface, version, id); + if (wl_resource == NULL) { + wl_client_post_no_memory(wl_client); + return; + } + wl_resource_set_implementation(wl_resource, + &inhibit_manager_implementation, manager, + input_manager_client_destroy); +} + +void wlr_input_inhibit_manager_destroy( + struct wlr_input_inhibit_manager *manager) { + if (!manager) { + return; + } + if (manager->active_client) { + input_inhibitor_destroy(manager->active_client, + manager->active_inhibitor); + } + wl_list_remove(&manager->display_destroy.link); + wl_global_destroy(manager->wl_global); + free(manager); +} + +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_input_inhibit_manager *manager = + wl_container_of(listener, manager, display_destroy); + wlr_input_inhibit_manager_destroy(manager); +} + +struct wlr_input_inhibit_manager *wlr_input_inhibit_manager_create( + struct wl_display *display) { + // TODO: Client destroy + struct wlr_input_inhibit_manager *manager = + calloc(1, sizeof(struct wlr_input_inhibit_manager)); + if (!manager) { + return NULL; + } + + manager->wl_global = wl_global_create(display, + &zwlr_input_inhibit_manager_v1_interface, + 1, manager, inhibit_manager_bind); + if (manager->wl_global == NULL){ + wl_list_remove(&manager->display_destroy.link); + free(manager); + return NULL; + } + + wl_signal_init(&manager->events.activate); + wl_signal_init(&manager->events.deactivate); + + manager->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &manager->display_destroy); + + return manager; +}