From 50d573b2ca68013a32828ae061ac2c242db333c6 Mon Sep 17 00:00:00 2001 From: Markus Ongyerth Date: Wed, 7 Feb 2018 11:10:49 +0100 Subject: [PATCH] implements the idle_inhibit protocol type This adds the types/wlr_idle_inhibit_v1 implementation. --- include/rootston/desktop.h | 5 + include/wlr/types/wlr_idle_inhibit_v1.h | 27 ++++ protocol/meson.build | 5 + rootston/desktop.c | 2 + types/meson.build | 1 + types/wlr_idle_inhibit_v1.c | 191 ++++++++++++++++++++++++ 6 files changed, 231 insertions(+) create mode 100644 include/wlr/types/wlr_idle_inhibit_v1.h create mode 100644 types/wlr_idle_inhibit_v1.c diff --git a/include/rootston/desktop.h b/include/rootston/desktop.h index 0132a7e8..de9f117a 100644 --- a/include/rootston/desktop.h +++ b/include/rootston/desktop.h @@ -16,6 +16,10 @@ #include #include #include +#include +#include +#include +#include "rootston/view.h" #include "rootston/config.h" #include "rootston/output.h" #include "rootston/view.h" @@ -41,6 +45,7 @@ struct roots_desktop { struct wlr_server_decoration_manager *server_decoration_manager; struct wlr_primary_selection_device_manager *primary_selection_device_manager; struct wlr_idle *idle; + struct wlr_idle_inhibit_v1 *idle_inhibit; struct wl_listener new_output; struct wl_listener layout_change; diff --git a/include/wlr/types/wlr_idle_inhibit_v1.h b/include/wlr/types/wlr_idle_inhibit_v1.h new file mode 100644 index 00000000..e8be43c9 --- /dev/null +++ b/include/wlr/types/wlr_idle_inhibit_v1.h @@ -0,0 +1,27 @@ +#ifndef WLR_TYPES_WLR_IDLE_INHIBIT_V1_H +#define WLR_TYPES_WLR_IDLE_INHIBIT_V1_H + +#include + +struct wlr_idle_inhibit_v1 { + struct wl_list clients; + struct wl_global *global; + + struct wl_listener display_destroy; + struct wl_signal new_inhibitor; +}; + +struct wlr_idle_inhibit_inhibitor_v1 { + struct wlr_surface *surface; + struct wl_resource *resource; + struct wl_listener surface_destroy; + + struct wl_list link; // wlr_idle_inhibit_manager::inhibitors; + + struct wl_signal destroy; +}; + +struct wlr_idle_inhibit_v1 *wlr_idle_inhibit_v1_create(struct wl_display *display); +void wlr_idle_inhibit_v1_destroy(struct wlr_idle_inhibit_v1 *idle_inhibit); + +#endif diff --git a/protocol/meson.build b/protocol/meson.build index 2853971f..7e111613 100644 --- a/protocol/meson.build +++ b/protocol/meson.build @@ -23,6 +23,7 @@ wayland_scanner_client = generator( protocols = [ [wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml'], [wl_protocol_dir, 'stable/xdg-shell/xdg-shell.xml'], + [wl_protocol_dir, 'unstable/idle-inhibit/idle-inhibit-unstable-v1.xml'], 'gamma-control.xml', 'gtk-primary-selection.xml', 'idle.xml', @@ -32,8 +33,12 @@ protocols = [ client_protocols = [ [wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml'], + [wl_protocol_dir, 'unstable/idle-inhibit/idle-inhibit-unstable-v1.xml'], + 'gamma-control.xml', + 'gtk-primary-selection.xml', 'idle.xml', 'screenshooter.xml', + 'server-decoration.xml', ] wl_protos_src = [] diff --git a/rootston/desktop.c b/rootston/desktop.c index 6b28a41c..8ac741bb 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -703,6 +704,7 @@ struct roots_desktop *desktop_create(struct roots_server *server, desktop->primary_selection_device_manager = wlr_primary_selection_device_manager_create(server->wl_display); desktop->idle = wlr_idle_create(server->wl_display); + desktop->idle_inhibit = wlr_idle_inhibit_v1_create(server->wl_display); return desktop; } diff --git a/types/meson.build b/types/meson.build index 3e40be02..703b06ca 100644 --- a/types/meson.build +++ b/types/meson.build @@ -27,6 +27,7 @@ lib_wlr_types = static_library( 'wlr_xcursor_manager.c', 'wlr_xdg_shell_v6.c', 'wlr_xdg_shell.c', + 'wlr_idle_inhibit_v1.c', ), include_directories: wlr_inc, dependencies: [pixman, xkbcommon, wayland_server, wlr_protos], diff --git a/types/wlr_idle_inhibit_v1.c b/types/wlr_idle_inhibit_v1.c new file mode 100644 index 00000000..1d47da90 --- /dev/null +++ b/types/wlr_idle_inhibit_v1.c @@ -0,0 +1,191 @@ +#include +#include +#include +#include +#include +#include "wayland-util.h" +#include "wayland-server.h" +#include "idle-inhibit-unstable-v1-protocol.h" + +struct wlr_idle_inhibit_manager { + struct wlr_idle_inhibit_v1 *wlr_idle_inhibit; + struct wl_resource *resource; + + struct wl_list link; // wlr_idle_inhibit_v1::clients + + struct wl_list inhibitors; +}; + +static void idle_inhibit_inhibitor_destroy(struct wl_resource *resource) { + struct wlr_idle_inhibit_inhibitor_v1 *inhibitor = wl_resource_get_user_data(resource); + assert(inhibitor); + + wl_signal_emit(&inhibitor->destroy, inhibitor->surface); + + wl_list_remove(&inhibitor->link); + wl_list_remove(&inhibitor->surface_destroy.link); + free(inhibitor); +} + +static void idle_inhibit_inhibitor_handle_surface_destroy(struct wl_listener *listener, + void *data) { + assert(listener); + struct wlr_idle_inhibit_inhibitor_v1 *inhibitor = + wl_container_of(listener, inhibitor, surface_destroy); + + wl_resource_destroy(inhibitor->resource); +} + +static void idle_inhibit_inhibitor_v1_handle_destroy(struct wl_client *client, + struct wl_resource *manager_resource) { + wl_resource_destroy(manager_resource); +} + +static struct zwp_idle_inhibitor_v1_interface idle_inhibitor_impl = { + .destroy = idle_inhibit_inhibitor_v1_handle_destroy, +}; + +static void wlr_create_inhibitor(struct wl_client *client, + struct wl_resource *resource, uint32_t id, + struct wl_resource *surface_resource) { + struct wlr_surface *surface = wl_resource_get_user_data(surface_resource); + struct wlr_idle_inhibit_manager *manager = wl_resource_get_user_data(resource); + assert(surface && manager); + + struct wlr_idle_inhibit_inhibitor_v1 *inhibitor = calloc(1, sizeof(struct wlr_idle_inhibit_inhibitor_v1)); + if (!inhibitor) { + wl_client_post_no_memory(client); + return; + } + + struct wl_resource *wl_resource = wl_resource_create(client, + &zwp_idle_inhibitor_v1_interface, 1, id); + if (!wl_resource) { + wl_client_post_no_memory(client); + free(inhibitor); + return; + } + + inhibitor->resource = wl_resource; + inhibitor->surface = surface; + wl_signal_init(&inhibitor->destroy); + + inhibitor->surface_destroy.notify = idle_inhibit_inhibitor_handle_surface_destroy; + wl_signal_add(&surface->events.destroy, &inhibitor->surface_destroy); + + + wl_resource_set_implementation(wl_resource, &idle_inhibitor_impl, + inhibitor, idle_inhibit_inhibitor_destroy); + + wl_list_insert(&manager->inhibitors, &inhibitor->link); + wl_signal_emit(&manager->wlr_idle_inhibit->new_inhibitor, inhibitor); +} + + +static void idle_inhibit_manager_destroy(struct wl_resource *resource) { + struct wlr_idle_inhibit_manager *manager = wl_resource_get_user_data(resource); + assert(manager); + + wl_list_remove(&manager->link); + + struct wlr_idle_inhibit_inhibitor_v1 *inhibitor; + struct wlr_idle_inhibit_inhibitor_v1 *tmp; + wl_list_for_each_safe(inhibitor, tmp, &manager->inhibitors, link) { + wl_resource_destroy(inhibitor->resource); + } +} + +static void idle_inhibit_manager_handle_destroy(struct wl_client *client, + struct wl_resource *manager_resource) { + wl_resource_destroy(manager_resource); +} + +static struct zwp_idle_inhibit_manager_v1_interface idle_inhibit_impl = { + .destroy = idle_inhibit_manager_handle_destroy, + .create_inhibitor = wlr_create_inhibitor, +}; + +void wlr_idle_inhibit_v1_destroy(struct wlr_idle_inhibit_v1 *idle_inhibit); + + +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_idle_inhibit_v1 *idle_inhibit = + wl_container_of(listener, idle_inhibit, display_destroy); + + wlr_idle_inhibit_v1_destroy(idle_inhibit); +} + +static void idle_inhibit_bind(struct wl_client *wl_client, void *data, + uint32_t version, uint32_t id) { + struct wlr_idle_inhibit_v1 *idle_inhibit = data; + assert(wl_client && idle_inhibit); + + struct wlr_idle_inhibit_manager *manager = calloc(1, sizeof(struct wlr_idle_inhibit_manager)); + if (!manager) { + wl_client_post_no_memory(wl_client); + return; + } + + struct wl_resource *wl_resource = wl_resource_create(wl_client, + &zwp_idle_inhibit_manager_v1_interface, version, id); + + if (!wl_resource) { + wl_client_post_no_memory(wl_client); + free(manager); + return; + } + + manager->resource = wl_resource; + wl_list_init(&manager->inhibitors); + wl_list_insert(&idle_inhibit->clients, &manager->link); + manager->wlr_idle_inhibit = idle_inhibit; + + + wl_resource_set_implementation(wl_resource, &idle_inhibit_impl, + manager, idle_inhibit_manager_destroy); + wlr_log(L_DEBUG, "idle_inhibit bound"); +} + +void wlr_idle_inhibit_v1_destroy(struct wlr_idle_inhibit_v1 *idle_inhibit) { + if (!idle_inhibit) { + return; + } + + wl_list_remove(&idle_inhibit->display_destroy.link); + + struct wlr_idle_inhibit_manager *manager; + struct wlr_idle_inhibit_manager *tmp; + wl_list_for_each_safe(manager, tmp, &idle_inhibit->clients, link) { + wl_resource_destroy(manager->resource); + } + + wl_global_destroy(idle_inhibit->global); + free(idle_inhibit); +} + +struct wlr_idle_inhibit_v1 *wlr_idle_inhibit_v1_create(struct wl_display *display) { + struct wlr_idle_inhibit_v1 *idle_inhibit = calloc(1, sizeof(struct wlr_idle_inhibit_v1)); + + if (!idle_inhibit) { + return NULL; + } + + wl_list_init(&idle_inhibit->clients); + idle_inhibit->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &idle_inhibit->display_destroy); + wl_signal_init(&idle_inhibit->new_inhibitor); + + idle_inhibit->global = wl_global_create(display, + &zwp_idle_inhibit_manager_v1_interface, 1, + idle_inhibit, idle_inhibit_bind); + + if (!idle_inhibit->global) { + wl_list_remove(&idle_inhibit->display_destroy.link); + free(idle_inhibit); + return NULL; + } + + wlr_log(L_DEBUG, "idle_inhibit manager created"); + + return idle_inhibit; +}