blob: e8f1e2b41eeb8e2006e81158baf2cba79de6c457 [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"
Marius Vlad2be09372021-03-08 22:45:52 +020029#include <libweston-desktop/libweston-desktop.h>
Marius Vladd98c7e82021-03-08 18:02:32 +020030
31struct weston_output *
32get_default_output(struct weston_compositor *compositor)
33{
34 if (wl_list_empty(&compositor->output_list))
35 return NULL;
36
37 return container_of(compositor->output_list.next,
38 struct weston_output, link);
39}
40
41struct weston_output *
42get_focused_output(struct weston_compositor *compositor)
43{
44 struct weston_seat *seat;
45 struct weston_output *output = NULL;
46
47 wl_list_for_each(seat, &compositor->seat_list, link) {
48 struct weston_touch *touch = weston_seat_get_touch(seat);
49 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
50 struct weston_keyboard *keyboard =
51 weston_seat_get_keyboard(seat);
52
53 /* Priority has touch focus, then pointer and
54 * then keyboard focus. We should probably have
55 * three for loops and check first for touch,
56 * then for pointer, etc. but unless somebody has some
57 * objections, I think this is sufficient. */
58 if (touch && touch->focus)
59 output = touch->focus->output;
60 else if (pointer && pointer->focus)
61 output = pointer->focus->output;
62 else if (keyboard && keyboard->focus)
63 output = keyboard->focus->output;
64
65 if (output)
66 break;
67 }
68
69 return output;
70}
71
72/* TODO: Fix this function to take into account nested subsurfaces. */
73void
74surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
75 int32_t *y, int32_t *w, int32_t *h)
76{
77 pixman_region32_t region;
78 pixman_box32_t *box;
79 struct weston_subsurface *subsurface;
80
81 pixman_region32_init_rect(&region, 0, 0,
82 surface->width,
83 surface->height);
84
85 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
86 pixman_region32_union_rect(&region, &region,
87 subsurface->position.x,
88 subsurface->position.y,
89 subsurface->surface->width,
90 subsurface->surface->height);
91 }
92
93 box = pixman_region32_extents(&region);
94 if (x)
95 *x = box->x1;
96 if (y)
97 *y = box->y1;
98 if (w)
99 *w = box->x2 - box->x1;
100 if (h)
101 *h = box->y2 - box->y1;
102
103 pixman_region32_fini(&region);
104}
105
106void
107center_on_output(struct weston_view *view, struct weston_output *output)
108{
109 int32_t surf_x, surf_y, width, height;
110 float x, y;
111
112 if (!output) {
113 weston_view_set_position(view, 0, 0);
114 return;
115 }
116
117 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
118
119 x = output->x + (output->width - width) / 2 - surf_x / 2;
120 y = output->y + (output->height - height) / 2 - surf_y / 2;
121
122 weston_view_set_position(view, x, y);
123}
Marius Vlad2be09372021-03-08 22:45:52 +0200124
125int
126surface_get_label(struct weston_surface *surface, char *buf, size_t len)
127{
128 const char *t, *c;
129 struct weston_desktop_surface *desktop_surface =
130 weston_surface_get_desktop_surface(surface);
131
132 t = weston_desktop_surface_get_title(desktop_surface);
133 c = weston_desktop_surface_get_app_id(desktop_surface);
134
135 return snprintf(buf, len, "%s window%s%s%s%s%s",
136 "top-level",
137 t ? " '" : "", t ?: "", t ? "'" : "",
138 c ? " of " : "", c ?: "");
139}
140
141struct weston_view *
142create_solid_color_surface(struct weston_compositor *compositor,
143 struct weston_solid_color_surface *ss,
144 float x, float y, int w, int h)
145{
146 struct weston_surface *surface = NULL;
147 struct weston_view *view;
148
149 surface = weston_surface_create(compositor);
150 if (surface == NULL) {
151 weston_log("no memory\n");
152 return NULL;
153 }
154 view = weston_view_create(surface);
leng.fang32af9fc2024-06-13 11:22:15 +0800155 weston_view_opacity(view, 0.0);
156
Marius Vlad2be09372021-03-08 22:45:52 +0200157 if (view == NULL) {
158 weston_log("no memory\n");
159 weston_surface_destroy(surface);
160 return NULL;
161 }
162
163 surface->committed = ss->surface_committed;
164 surface->committed_private = ss->surface_private;
165
166 weston_surface_set_color(surface, ss->r, ss->g, ss->b, 1.0);
167 weston_surface_set_label_func(surface, ss->get_label);
168 pixman_region32_fini(&surface->opaque);
169 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
170 pixman_region32_fini(&surface->input);
171 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
172
173 weston_surface_set_size(surface, w, h);
174 weston_view_set_position(view, x, y);
175
176 return view;
177}