blob: 7f9ec042d2093f7a1d5145d5b83668ceabff3ab3 [file] [log] [blame]
Marius Vladd98c7e82021-03-08 18:02:32 +02001/*
2 * Copyright 2010-2012 Intel Corporation
3 * Copyright 2013 Raspberry Pi Foundation
4 * Copyright 2011-2012,2021 Collabora, Ltd.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27#include "config.h"
28#include "shared/shell-utils.h"
29
30struct weston_output *
31get_default_output(struct weston_compositor *compositor)
32{
33 if (wl_list_empty(&compositor->output_list))
34 return NULL;
35
36 return container_of(compositor->output_list.next,
37 struct weston_output, link);
38}
39
40struct weston_output *
41get_focused_output(struct weston_compositor *compositor)
42{
43 struct weston_seat *seat;
44 struct weston_output *output = NULL;
45
46 wl_list_for_each(seat, &compositor->seat_list, link) {
47 struct weston_touch *touch = weston_seat_get_touch(seat);
48 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
49 struct weston_keyboard *keyboard =
50 weston_seat_get_keyboard(seat);
51
52 /* Priority has touch focus, then pointer and
53 * then keyboard focus. We should probably have
54 * three for loops and check first for touch,
55 * then for pointer, etc. but unless somebody has some
56 * objections, I think this is sufficient. */
57 if (touch && touch->focus)
58 output = touch->focus->output;
59 else if (pointer && pointer->focus)
60 output = pointer->focus->output;
61 else if (keyboard && keyboard->focus)
62 output = keyboard->focus->output;
63
64 if (output)
65 break;
66 }
67
68 return output;
69}
70
71/* TODO: Fix this function to take into account nested subsurfaces. */
72void
73surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
74 int32_t *y, int32_t *w, int32_t *h)
75{
76 pixman_region32_t region;
77 pixman_box32_t *box;
78 struct weston_subsurface *subsurface;
79
80 pixman_region32_init_rect(&region, 0, 0,
81 surface->width,
82 surface->height);
83
84 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
85 pixman_region32_union_rect(&region, &region,
86 subsurface->position.x,
87 subsurface->position.y,
88 subsurface->surface->width,
89 subsurface->surface->height);
90 }
91
92 box = pixman_region32_extents(&region);
93 if (x)
94 *x = box->x1;
95 if (y)
96 *y = box->y1;
97 if (w)
98 *w = box->x2 - box->x1;
99 if (h)
100 *h = box->y2 - box->y1;
101
102 pixman_region32_fini(&region);
103}
104
105void
106center_on_output(struct weston_view *view, struct weston_output *output)
107{
108 int32_t surf_x, surf_y, width, height;
109 float x, y;
110
111 if (!output) {
112 weston_view_set_position(view, 0, 0);
113 return;
114 }
115
116 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
117
118 x = output->x + (output->width - width) / 2 - surf_x / 2;
119 y = output->y + (output->height - height) / 2 - surf_y / 2;
120
121 weston_view_set_position(view, x, y);
122}