From d98c7e86fb75bfa12ace99cfe25048f49851a90b Mon Sep 17 00:00:00 2001 From: Marius Vlad Date: Mon, 8 Mar 2021 18:02:32 +0200 Subject: [PATCH] shared/shell-utils: Create common helpers functions for shells Group common functions found in both kiosk-shell and deskop-shell to a shared utils file. Signed-off-by: Marius Vlad --- desktop-shell/meson.build | 1 + desktop-shell/shell.c | 99 +------------------------------ kiosk-shell/kiosk-shell.c | 1 + kiosk-shell/meson.build | 1 + kiosk-shell/util.c | 98 ------------------------------ kiosk-shell/util.h | 12 ---- shared/shell-utils.c | 122 ++++++++++++++++++++++++++++++++++++++ shared/shell-utils.h | 40 +++++++++++++ 8 files changed, 166 insertions(+), 208 deletions(-) create mode 100644 shared/shell-utils.c create mode 100644 shared/shell-utils.h diff --git a/desktop-shell/meson.build b/desktop-shell/meson.build index c67bfd60..cc7215cb 100644 --- a/desktop-shell/meson.build +++ b/desktop-shell/meson.build @@ -5,6 +5,7 @@ if get_option('shell-desktop') 'shell.c', 'exposay.c', 'input-panel.c', + '../shared/shell-utils.c', weston_desktop_shell_server_protocol_h, weston_desktop_shell_protocol_c, input_method_unstable_v1_server_protocol_h, diff --git a/desktop-shell/shell.c b/desktop-shell/shell.c index 46c0b446..90caf0fd 100644 --- a/desktop-shell/shell.c +++ b/desktop-shell/shell.c @@ -41,6 +41,7 @@ #include "weston-desktop-shell-server-protocol.h" #include #include "shared/helpers.h" +#include "shared/shell-utils.h" #include "shared/timespec-util.h" #include @@ -426,10 +427,6 @@ shell_touch_grab_end(struct shell_touch_grab *grab) weston_touch_end_grab(grab->touch); } -static void -center_on_output(struct weston_view *view, - struct weston_output *output); - static enum weston_keyboard_modifier get_modifier(char *modifier) { @@ -512,16 +509,6 @@ shell_configuration(struct desktop_shell *shell) DEFAULT_NUM_WORKSPACES); } -struct weston_output * -get_default_output(struct weston_compositor *compositor) -{ - if (wl_list_empty(&compositor->output_list)) - return NULL; - - return container_of(compositor->output_list.next, - struct weston_output, link); -} - static int focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len) { @@ -1705,40 +1692,6 @@ static const struct weston_pointer_grab_interface resize_grab_interface = { resize_grab_cancel, }; -/* - * Returns the bounding box of a surface and all its sub-surfaces, - * in surface-local coordinates. */ -static void -surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x, - int32_t *y, int32_t *w, int32_t *h) { - pixman_region32_t region; - pixman_box32_t *box; - struct weston_subsurface *subsurface; - - pixman_region32_init_rect(®ion, 0, 0, - surface->width, - surface->height); - - wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) { - pixman_region32_union_rect(®ion, ®ion, - subsurface->position.x, - subsurface->position.y, - subsurface->surface->width, - subsurface->surface->height); - } - - box = pixman_region32_extents(®ion); - if (x) - *x = box->x1; - if (y) - *y = box->y1; - if (w) - *w = box->x2 - box->x1; - if (h) - *h = box->y2 - box->y1; - - pixman_region32_fini(®ion); -} static int surface_resize(struct shell_surface *shsurf, @@ -2201,37 +2154,6 @@ shell_map_fullscreen(struct shell_surface *shsurf) shell_configure_fullscreen(shsurf); } -static struct weston_output * -get_focused_output(struct weston_compositor *compositor) -{ - struct weston_seat *seat; - struct weston_output *output = NULL; - - wl_list_for_each(seat, &compositor->seat_list, link) { - struct weston_touch *touch = weston_seat_get_touch(seat); - struct weston_pointer *pointer = weston_seat_get_pointer(seat); - struct weston_keyboard *keyboard = - weston_seat_get_keyboard(seat); - - /* Priority has touch focus, then pointer and - * then keyboard focus. We should probably have - * three for loops and check first for touch, - * then for pointer, etc. but unless somebody has some - * objections, I think this is sufficient. */ - if (touch && touch->focus) - output = touch->focus->output; - else if (pointer && pointer->focus) - output = pointer->focus->output; - else if (keyboard && keyboard->focus) - output = keyboard->focus->output; - - if (output) - break; - } - - return output; -} - static void desktop_shell_destroy_seat(struct shell_seat *shseat) { @@ -4283,25 +4205,6 @@ transform_handler(struct wl_listener *listener, void *data) api->send_position(surface, x, y); } -static void -center_on_output(struct weston_view *view, struct weston_output *output) -{ - int32_t surf_x, surf_y, width, height; - float x, y; - - if (!output) { - weston_view_set_position(view, 0, 0); - return; - } - - surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height); - - x = output->x + (output->width - width) / 2 - surf_x / 2; - y = output->y + (output->height - height) / 2 - surf_y / 2; - - weston_view_set_position(view, x, y); -} - static void weston_view_set_initial_position(struct weston_view *view, struct desktop_shell *shell) diff --git a/kiosk-shell/kiosk-shell.c b/kiosk-shell/kiosk-shell.c index cfa0a95f..0648ed58 100644 --- a/kiosk-shell/kiosk-shell.c +++ b/kiosk-shell/kiosk-shell.c @@ -33,6 +33,7 @@ #include "kiosk-shell-grab.h" #include "compositor/weston.h" #include "shared/helpers.h" +#include "shared/shell-utils.h" #include "util.h" #include diff --git a/kiosk-shell/meson.build b/kiosk-shell/meson.build index e838614e..f9468687 100644 --- a/kiosk-shell/meson.build +++ b/kiosk-shell/meson.build @@ -3,6 +3,7 @@ if get_option('shell-kiosk') 'kiosk-shell.c', 'kiosk-shell-grab.c', 'util.c', + '../shared/shell-utils.c', weston_desktop_shell_server_protocol_h, weston_desktop_shell_protocol_c, input_method_unstable_v1_server_protocol_h, diff --git a/kiosk-shell/util.c b/kiosk-shell/util.c index ad3e0d9b..b411b504 100644 --- a/kiosk-shell/util.c +++ b/kiosk-shell/util.c @@ -25,108 +25,10 @@ /* Helper functions for kiosk-shell */ -/* TODO: These functions are useful to many shells, and, in fact, - * much of content in this file was copied from desktop-shell. We should - * create a shared shell utility collection to deduplicate this code. */ - #include "util.h" #include "shared/helpers.h" #include -struct weston_output * -get_default_output(struct weston_compositor *compositor) -{ - if (wl_list_empty(&compositor->output_list)) - return NULL; - - return container_of(compositor->output_list.next, - struct weston_output, link); -} - -struct weston_output * -get_focused_output(struct weston_compositor *compositor) -{ - struct weston_seat *seat; - struct weston_output *output = NULL; - - wl_list_for_each(seat, &compositor->seat_list, link) { - struct weston_touch *touch = weston_seat_get_touch(seat); - struct weston_pointer *pointer = weston_seat_get_pointer(seat); - struct weston_keyboard *keyboard = - weston_seat_get_keyboard(seat); - - /* Priority has touch focus, then pointer and - * then keyboard focus. We should probably have - * three for loops and check first for touch, - * then for pointer, etc. but unless somebody has some - * objections, I think this is sufficient. */ - if (touch && touch->focus) - output = touch->focus->output; - else if (pointer && pointer->focus) - output = pointer->focus->output; - else if (keyboard && keyboard->focus) - output = keyboard->focus->output; - - if (output) - break; - } - - return output; -} - -/* This is a copy of the same function from desktop-shell. - * TODO: Fix this function to take into account nested subsurfaces. */ -static void -surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x, - int32_t *y, int32_t *w, int32_t *h) { - pixman_region32_t region; - pixman_box32_t *box; - struct weston_subsurface *subsurface; - - pixman_region32_init_rect(®ion, 0, 0, - surface->width, - surface->height); - - wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) { - pixman_region32_union_rect(®ion, ®ion, - subsurface->position.x, - subsurface->position.y, - subsurface->surface->width, - subsurface->surface->height); - } - - box = pixman_region32_extents(®ion); - if (x) - *x = box->x1; - if (y) - *y = box->y1; - if (w) - *w = box->x2 - box->x1; - if (h) - *h = box->y2 - box->y1; - - pixman_region32_fini(®ion); -} - -void -center_on_output(struct weston_view *view, struct weston_output *output) -{ - int32_t surf_x, surf_y, width, height; - float x, y; - - if (!output) { - weston_view_set_position(view, 0, 0); - return; - } - - surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height); - - x = output->x + (output->width - width) / 2 - surf_x / 2; - y = output->y + (output->height - height) / 2 - surf_y / 2; - - weston_view_set_position(view, x, y); -} - static void colored_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy) { diff --git a/kiosk-shell/util.h b/kiosk-shell/util.h index e60aa3b5..a6647be6 100644 --- a/kiosk-shell/util.h +++ b/kiosk-shell/util.h @@ -28,19 +28,7 @@ #include struct weston_compositor; -struct weston_layer; -struct weston_output; struct weston_surface; -struct weston_view; - -struct weston_output * -get_default_output(struct weston_compositor *compositor); - -struct weston_output * -get_focused_output(struct weston_compositor *compositor); - -void -center_on_output(struct weston_view *view, struct weston_output *output); struct weston_view * create_colored_surface(struct weston_compositor *compositor, diff --git a/shared/shell-utils.c b/shared/shell-utils.c new file mode 100644 index 00000000..7f9ec042 --- /dev/null +++ b/shared/shell-utils.c @@ -0,0 +1,122 @@ +/* + * Copyright 2010-2012 Intel Corporation + * Copyright 2013 Raspberry Pi Foundation + * Copyright 2011-2012,2021 Collabora, Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ + +#include "config.h" +#include "shared/shell-utils.h" + +struct weston_output * +get_default_output(struct weston_compositor *compositor) +{ + if (wl_list_empty(&compositor->output_list)) + return NULL; + + return container_of(compositor->output_list.next, + struct weston_output, link); +} + +struct weston_output * +get_focused_output(struct weston_compositor *compositor) +{ + struct weston_seat *seat; + struct weston_output *output = NULL; + + wl_list_for_each(seat, &compositor->seat_list, link) { + struct weston_touch *touch = weston_seat_get_touch(seat); + struct weston_pointer *pointer = weston_seat_get_pointer(seat); + struct weston_keyboard *keyboard = + weston_seat_get_keyboard(seat); + + /* Priority has touch focus, then pointer and + * then keyboard focus. We should probably have + * three for loops and check first for touch, + * then for pointer, etc. but unless somebody has some + * objections, I think this is sufficient. */ + if (touch && touch->focus) + output = touch->focus->output; + else if (pointer && pointer->focus) + output = pointer->focus->output; + else if (keyboard && keyboard->focus) + output = keyboard->focus->output; + + if (output) + break; + } + + return output; +} + +/* TODO: Fix this function to take into account nested subsurfaces. */ +void +surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x, + int32_t *y, int32_t *w, int32_t *h) +{ + pixman_region32_t region; + pixman_box32_t *box; + struct weston_subsurface *subsurface; + + pixman_region32_init_rect(®ion, 0, 0, + surface->width, + surface->height); + + wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) { + pixman_region32_union_rect(®ion, ®ion, + subsurface->position.x, + subsurface->position.y, + subsurface->surface->width, + subsurface->surface->height); + } + + box = pixman_region32_extents(®ion); + if (x) + *x = box->x1; + if (y) + *y = box->y1; + if (w) + *w = box->x2 - box->x1; + if (h) + *h = box->y2 - box->y1; + + pixman_region32_fini(®ion); +} + +void +center_on_output(struct weston_view *view, struct weston_output *output) +{ + int32_t surf_x, surf_y, width, height; + float x, y; + + if (!output) { + weston_view_set_position(view, 0, 0); + return; + } + + surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height); + + x = output->x + (output->width - width) / 2 - surf_x / 2; + y = output->y + (output->height - height) / 2 - surf_y / 2; + + weston_view_set_position(view, x, y); +} diff --git a/shared/shell-utils.h b/shared/shell-utils.h new file mode 100644 index 00000000..c1732ef5 --- /dev/null +++ b/shared/shell-utils.h @@ -0,0 +1,40 @@ +/* + * Copyright 2021 Collabora, Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ + +#include "config.h" +#include "shared/helpers.h" +#include + +struct weston_output * +get_default_output(struct weston_compositor *compositor); + +struct weston_output * +get_focused_output(struct weston_compositor *compositor); + +void +center_on_output(struct weston_view *view, struct weston_output *output); + +void +surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x, + int32_t *y, int32_t *w, int32_t *h);