blob: 43d8965c8c3ea4627fbd0fe7c21fefdfe130e900 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanend581a8f2012-01-27 16:25:16 +02004 * Copyright © 2012 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberga9410222011-01-14 17:22:35 -050025#include "config.h"
26
Daniel Stoneb7452fe2012-06-01 12:14:06 +010027#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010032#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020034#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040035#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010036#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040037#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020038#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020039#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020040#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040041#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050042#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040044#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040045#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050046#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040047#include <sys/time.h>
48#include <time.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050049
Marcin Slusarz554a0da2013-02-18 13:27:22 -050050#ifdef HAVE_LIBUNWIND
51#define UNW_LOCAL_ONLY
52#include <libunwind.h>
53#endif
54
Pekka Paalanen50719bc2011-11-22 14:18:50 +020055#include <wayland-server.h>
Kristian Høgsberg82863022010-06-04 21:52:02 -040056#include "compositor.h"
U. Artie Eoffec08f332013-05-13 15:55:47 -070057#include "subsurface-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030058#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040059#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050060#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061
Kristian Høgsberg27da5382011-06-21 17:32:25 -040062static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040063static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040064
65static int
66sigchld_handler(int signal_number, void *data)
67{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050068 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040069 int status;
70 pid_t pid;
71
Bill Spitzak027b9622012-03-17 15:22:03 -070072 pid = waitpid(-1, &status, WNOHANG);
73 if (!pid)
74 return 1;
75
Kristian Høgsberg27da5382011-06-21 17:32:25 -040076 wl_list_for_each(p, &child_process_list, link) {
77 if (p->pid == pid)
78 break;
79 }
80
81 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020082 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040083 return 1;
84 }
85
86 wl_list_remove(&p->link);
87 p->cleanup(p, status);
88
89 return 1;
90}
91
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020092static void
Alexander Larsson0b135062013-05-28 16:23:36 +020093weston_output_transform_scale_init(struct weston_output *output,
94 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020095
Alex Wu2dda6042012-04-17 17:20:47 +080096WL_EXPORT int
Alexander Larsson355748e2013-05-28 16:23:38 +020097weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode, int32_t scale)
Alex Wu2dda6042012-04-17 17:20:47 +080098{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020099 struct weston_seat *seat;
100 pixman_region32_t old_output_region;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200101 int ret;
102
Alex Wu2dda6042012-04-17 17:20:47 +0800103 if (!output->switch_mode)
104 return -1;
105
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200106 ret = output->switch_mode(output, mode);
107 if (ret < 0)
108 return ret;
109
Alexander Larsson355748e2013-05-28 16:23:38 +0200110 output->scale = scale;
111
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200112 pixman_region32_init(&old_output_region);
113 pixman_region32_copy(&old_output_region, &output->region);
114
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200115 /* Update output region and transformation matrix */
Alexander Larsson0b135062013-05-28 16:23:36 +0200116 weston_output_transform_scale_init(output, output->transform, output->scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200117
118 pixman_region32_init(&output->previous_damage);
119 pixman_region32_init_rect(&output->region, output->x, output->y,
120 output->width, output->height);
121
122 weston_output_update_matrix(output);
123
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200124 /* If a pointer falls outside the outputs new geometry, move it to its
125 * lower-right corner */
126 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400127 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200128 int32_t x, y;
129
130 if (!pointer)
131 continue;
132
133 x = wl_fixed_to_int(pointer->x);
134 y = wl_fixed_to_int(pointer->y);
135
136 if (!pixman_region32_contains_point(&old_output_region,
137 x, y, NULL) ||
138 pixman_region32_contains_point(&output->region,
139 x, y, NULL))
140 continue;
141
142 if (x >= output->x + output->width)
143 x = output->x + output->width - 1;
144 if (y >= output->y + output->height)
145 y = output->y + output->height - 1;
146
147 pointer->x = wl_fixed_from_int(x);
148 pointer->y = wl_fixed_from_int(y);
149 }
150
151 pixman_region32_fini(&old_output_region);
152
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200153 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800154}
155
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400156WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500157weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400158{
159 wl_list_insert(&child_process_list, &process->link);
160}
161
Benjamin Franzke06286262011-05-06 19:12:33 +0200162static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200163child_client_exec(int sockfd, const char *path)
164{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500165 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200166 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200167 sigset_t allsigs;
168
169 /* do not give our signal mask to the new process */
170 sigfillset(&allsigs);
171 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200172
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500173 /* Launch clients as the user. */
174 seteuid(getuid());
175
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500176 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
177 * non-CLOEXEC fd to pass through exec. */
178 clientfd = dup(sockfd);
179 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200180 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500181 return;
182 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200183
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500184 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200185 setenv("WAYLAND_SOCKET", s, 1);
186
187 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200188 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200189 path);
190}
191
192WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500193weston_client_launch(struct weston_compositor *compositor,
194 struct weston_process *proc,
195 const char *path,
196 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200197{
198 int sv[2];
199 pid_t pid;
200 struct wl_client *client;
201
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300202 weston_log("launching '%s'\n", path);
203
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300204 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200205 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200206 "socketpair failed while launching '%s': %m\n",
207 path);
208 return NULL;
209 }
210
211 pid = fork();
212 if (pid == -1) {
213 close(sv[0]);
214 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200215 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200216 "fork failed while launching '%s': %m\n", path);
217 return NULL;
218 }
219
220 if (pid == 0) {
221 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700222 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200223 }
224
225 close(sv[1]);
226
227 client = wl_client_create(compositor->wl_display, sv[0]);
228 if (!client) {
229 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200230 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200231 "wl_client_create failed while launching '%s'.\n",
232 path);
233 return NULL;
234 }
235
236 proc->pid = pid;
237 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500238 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200239
240 return client;
241}
242
243static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300244surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
245{
246 struct weston_surface *surface =
247 container_of(listener, struct weston_surface,
248 pending.buffer_destroy_listener);
249
250 surface->pending.buffer = NULL;
251}
252
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200253static void
254empty_region(pixman_region32_t *region)
255{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300256 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200257 pixman_region32_init(region);
258}
259
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300260static void
261region_init_infinite(pixman_region32_t *region)
262{
263 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
264 UINT32_MAX, UINT32_MAX);
265}
266
Pekka Paalanene67858b2013-04-25 13:57:42 +0300267static struct weston_subsurface *
268weston_surface_to_subsurface(struct weston_surface *surface);
269
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500270WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500271weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500272{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500273 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400274
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200275 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400276 if (surface == NULL)
277 return NULL;
278
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500279 wl_signal_init(&surface->destroy_signal);
280
281 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500282
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500283 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500284 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500285
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500286 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400287 surface->alpha = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400288
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100289 if (compositor->renderer->create_surface(surface) < 0) {
290 free(surface);
291 return NULL;
292 }
293
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200294 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200295 surface->buffer_scale = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200296 surface->pending.buffer_transform = surface->buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200297 surface->pending.buffer_scale = surface->buffer_scale;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400298 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400299 surface->plane = &compositor->primary_plane;
Giulio Camuffo184df502013-02-21 11:29:21 +0100300 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200301
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400302 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500303 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500304 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300305 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200306 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500307 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400308
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200309 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200310 wl_list_insert(&surface->geometry.transformation_list,
311 &surface->transform.position.link);
312 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200313 wl_list_init(&surface->geometry.child_list);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200314 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200315 surface->transform.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500316
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300317 surface->pending.buffer_destroy_listener.notify =
318 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300319 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300320 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300321 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300322 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300323
Pekka Paalanene67858b2013-04-25 13:57:42 +0300324 wl_list_init(&surface->subsurface_list);
325 wl_list_init(&surface->subsurface_list_pending);
326
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400327 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500328}
329
Alex Wu8811bf92012-02-28 18:07:54 +0800330WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500331weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200332 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500333{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100334 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500335}
336
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400337WL_EXPORT void
338weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200339 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200340{
341 if (surface->transform.enabled) {
342 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
343
344 weston_matrix_transform(&surface->transform.matrix, &v);
345
346 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200347 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700348 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200349 v.f[3]);
350 *x = 0;
351 *y = 0;
352 return;
353 }
354
355 *x = v.f[0] / v.f[3];
356 *y = v.f[1] / v.f[3];
357 } else {
358 *x = sx + surface->geometry.x;
359 *y = sy + surface->geometry.y;
360 }
361}
362
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500363WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200364weston_transformed_coord(int width, int height,
365 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200366 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200367 float sx, float sy, float *bx, float *by)
368{
369 switch (transform) {
370 case WL_OUTPUT_TRANSFORM_NORMAL:
371 default:
372 *bx = sx;
373 *by = sy;
374 break;
375 case WL_OUTPUT_TRANSFORM_FLIPPED:
376 *bx = width - sx;
377 *by = sy;
378 break;
379 case WL_OUTPUT_TRANSFORM_90:
380 *bx = height - sy;
381 *by = sx;
382 break;
383 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
384 *bx = height - sy;
385 *by = width - sx;
386 break;
387 case WL_OUTPUT_TRANSFORM_180:
388 *bx = width - sx;
389 *by = height - sy;
390 break;
391 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
392 *bx = sx;
393 *by = height - sy;
394 break;
395 case WL_OUTPUT_TRANSFORM_270:
396 *bx = sy;
397 *by = width - sx;
398 break;
399 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
400 *bx = sy;
401 *by = sx;
402 break;
403 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200404
405 *bx *= scale;
406 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200407}
408
409WL_EXPORT pixman_box32_t
410weston_transformed_rect(int width, int height,
411 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200412 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200413 pixman_box32_t rect)
414{
415 float x1, x2, y1, y2;
416
417 pixman_box32_t ret;
418
Alexander Larsson4ea95522013-05-22 14:41:37 +0200419 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200420 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200421 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200422 rect.x2, rect.y2, &x2, &y2);
423
424 if (x1 <= x2) {
425 ret.x1 = x1;
426 ret.x2 = x2;
427 } else {
428 ret.x1 = x2;
429 ret.x2 = x1;
430 }
431
432 if (y1 <= y2) {
433 ret.y1 = y1;
434 ret.y2 = y2;
435 } else {
436 ret.y1 = y2;
437 ret.y2 = y1;
438 }
439
440 return ret;
441}
442
443WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200444weston_surface_to_buffer_float(struct weston_surface *surface,
445 float sx, float sy, float *bx, float *by)
446{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200447 weston_transformed_coord(surface->geometry.width,
448 surface->geometry.height,
449 surface->buffer_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200450 surface->buffer_scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200451 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200452}
453
Alexander Larsson4ea95522013-05-22 14:41:37 +0200454WL_EXPORT void
455weston_surface_to_buffer(struct weston_surface *surface,
456 int sx, int sy, int *bx, int *by)
457{
458 float bxf, byf;
459
460 weston_transformed_coord(surface->geometry.width,
461 surface->geometry.height,
462 surface->buffer_transform,
463 surface->buffer_scale,
464 sx, sy, &bxf, &byf);
465 *bx = floorf(bxf);
466 *by = floorf(byf);
467}
468
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200469WL_EXPORT pixman_box32_t
470weston_surface_to_buffer_rect(struct weston_surface *surface,
471 pixman_box32_t rect)
472{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200473 return weston_transformed_rect(surface->geometry.width,
474 surface->geometry.height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200475 surface->buffer_transform,
476 surface->buffer_scale,
477 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200478}
479
480WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400481weston_surface_move_to_plane(struct weston_surface *surface,
482 struct weston_plane *plane)
483{
484 if (surface->plane == plane)
485 return;
486
487 weston_surface_damage_below(surface);
488 surface->plane = plane;
489 weston_surface_damage(surface);
490}
491
492WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500493weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200494{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500495 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200496
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500497 pixman_region32_init(&damage);
498 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
499 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400500 pixman_region32_union(&surface->plane->damage,
501 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500502 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200503}
504
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400505static void
506weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
507{
508 uint32_t different = es->output_mask ^ mask;
509 uint32_t entered = mask & different;
510 uint32_t left = es->output_mask & different;
511 struct weston_output *output;
512 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500513 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400514
515 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500516 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400517 return;
518 if (different == 0)
519 return;
520
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500521 client = wl_resource_get_client(es->resource);
522
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400523 wl_list_for_each(output, &es->compositor->output_list, link) {
524 if (1 << output->id & different)
525 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500526 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400527 client);
528 if (resource == NULL)
529 continue;
530 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500531 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400532 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500533 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400534 }
535}
536
537static void
538weston_surface_assign_output(struct weston_surface *es)
539{
540 struct weston_compositor *ec = es->compositor;
541 struct weston_output *output, *new_output;
542 pixman_region32_t region;
543 uint32_t max, area, mask;
544 pixman_box32_t *e;
545
546 new_output = NULL;
547 max = 0;
548 mask = 0;
549 pixman_region32_init(&region);
550 wl_list_for_each(output, &ec->output_list, link) {
551 pixman_region32_intersect(&region, &es->transform.boundingbox,
552 &output->region);
553
554 e = pixman_region32_extents(&region);
555 area = (e->x2 - e->x1) * (e->y2 - e->y1);
556
557 if (area > 0)
558 mask |= 1 << output->id;
559
560 if (area >= max) {
561 new_output = output;
562 max = area;
563 }
564 }
565 pixman_region32_fini(&region);
566
567 es->output = new_output;
568 weston_surface_update_output_mask(es, mask);
569}
570
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200571static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200572surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
573 int32_t width, int32_t height,
574 pixman_region32_t *bbox)
575{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200576 float min_x = HUGE_VALF, min_y = HUGE_VALF;
577 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200578 int32_t s[4][2] = {
579 { sx, sy },
580 { sx, sy + height },
581 { sx + width, sy },
582 { sx + width, sy + height }
583 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200584 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200585 int i;
586
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300587 if (width == 0 || height == 0) {
588 /* avoid rounding empty bbox to 1x1 */
589 pixman_region32_init(bbox);
590 return;
591 }
592
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200593 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200594 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400595 weston_surface_to_global_float(surface,
596 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200597 if (x < min_x)
598 min_x = x;
599 if (x > max_x)
600 max_x = x;
601 if (y < min_y)
602 min_y = y;
603 if (y > max_y)
604 max_y = y;
605 }
606
Pekka Paalanen219b9822012-02-08 15:38:37 +0200607 int_x = floorf(min_x);
608 int_y = floorf(min_y);
609 pixman_region32_init_rect(bbox, int_x, int_y,
610 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200611}
612
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200613static void
614weston_surface_update_transform_disable(struct weston_surface *surface)
615{
616 surface->transform.enabled = 0;
617
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200618 /* round off fractions when not transformed */
619 surface->geometry.x = roundf(surface->geometry.x);
620 surface->geometry.y = roundf(surface->geometry.y);
621
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500622 /* Otherwise identity matrix, but with x and y translation. */
623 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
624 surface->transform.position.matrix.d[12] = surface->geometry.x;
625 surface->transform.position.matrix.d[13] = surface->geometry.y;
626
627 surface->transform.matrix = surface->transform.position.matrix;
628
Kristian Høgsberg9bcaaeb2013-02-28 14:56:43 -0500629 surface->transform.inverse = surface->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500630 surface->transform.inverse.d[12] = -surface->geometry.x;
631 surface->transform.inverse.d[13] = -surface->geometry.y;
632
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200633 pixman_region32_init_rect(&surface->transform.boundingbox,
634 surface->geometry.x,
635 surface->geometry.y,
636 surface->geometry.width,
637 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500638
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400639 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500640 pixman_region32_copy(&surface->transform.opaque,
641 &surface->opaque);
642 pixman_region32_translate(&surface->transform.opaque,
643 surface->geometry.x,
644 surface->geometry.y);
645 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200646}
647
648static int
649weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200650{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200651 struct weston_surface *parent = surface->geometry.parent;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200652 struct weston_matrix *matrix = &surface->transform.matrix;
653 struct weston_matrix *inverse = &surface->transform.inverse;
654 struct weston_transform *tform;
655
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200656 surface->transform.enabled = 1;
657
658 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300659 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200660 surface->transform.position.matrix.d[12] = surface->geometry.x;
661 surface->transform.position.matrix.d[13] = surface->geometry.y;
662
663 weston_matrix_init(matrix);
664 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
665 weston_matrix_multiply(matrix, &tform->matrix);
666
Pekka Paalanen483243f2013-03-08 14:56:50 +0200667 if (parent)
668 weston_matrix_multiply(matrix, &parent->transform.matrix);
669
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200670 if (weston_matrix_invert(inverse, matrix) < 0) {
671 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200672 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200673 " transformation not invertible.\n", surface);
674 return -1;
675 }
676
677 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
678 surface->geometry.height,
679 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500680
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200681 return 0;
682}
683
684WL_EXPORT void
685weston_surface_update_transform(struct weston_surface *surface)
686{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200687 struct weston_surface *parent = surface->geometry.parent;
688
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200689 if (!surface->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200690 return;
691
Pekka Paalanen483243f2013-03-08 14:56:50 +0200692 if (parent)
693 weston_surface_update_transform(parent);
694
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200695 surface->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200696
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500697 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200698
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200699 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500700 pixman_region32_fini(&surface->transform.opaque);
701 pixman_region32_init(&surface->transform.opaque);
702
Pekka Paalanencd403622012-01-25 13:37:39 +0200703 /* transform.position is always in transformation_list */
704 if (surface->geometry.transformation_list.next ==
705 &surface->transform.position.link &&
706 surface->geometry.transformation_list.prev ==
Pekka Paalanen483243f2013-03-08 14:56:50 +0200707 &surface->transform.position.link &&
708 !parent) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200709 weston_surface_update_transform_disable(surface);
710 } else {
711 if (weston_surface_update_transform_enable(surface) < 0)
712 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200713 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200714
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400715 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200716
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300717 weston_surface_assign_output(surface);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300718
719 wl_signal_emit(&surface->compositor->transform_signal, surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200720}
721
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200722WL_EXPORT void
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200723weston_surface_geometry_dirty(struct weston_surface *surface)
724{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200725 struct weston_surface *child;
726
727 /*
728 * The invariant: if surface->geometry.dirty, then all surfaces
729 * in surface->geometry.child_list have geometry.dirty too.
730 * Corollary: if not parent->geometry.dirty, then all ancestors
731 * are not dirty.
732 */
733
734 if (surface->transform.dirty)
735 return;
736
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200737 surface->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200738
739 wl_list_for_each(child, &surface->geometry.child_list,
740 geometry.parent_link)
741 weston_surface_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200742}
743
744WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100745weston_surface_to_global_fixed(struct weston_surface *surface,
746 wl_fixed_t sx, wl_fixed_t sy,
747 wl_fixed_t *x, wl_fixed_t *y)
748{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200749 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100750
751 weston_surface_to_global_float(surface,
752 wl_fixed_to_double(sx),
753 wl_fixed_to_double(sy),
754 &xf, &yf);
755 *x = wl_fixed_from_double(xf);
756 *y = wl_fixed_from_double(yf);
757}
758
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400759WL_EXPORT void
760weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200761 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200762{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200763 if (surface->transform.enabled) {
764 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
765
766 weston_matrix_transform(&surface->transform.inverse, &v);
767
768 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200769 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200770 "weston_surface_from_global(), divisor = %g\n",
771 v.f[3]);
772 *sx = 0;
773 *sy = 0;
774 return;
775 }
776
Pekka Paalanencd403622012-01-25 13:37:39 +0200777 *sx = v.f[0] / v.f[3];
778 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200779 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200780 *sx = x - surface->geometry.x;
781 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200782 }
783}
784
785WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100786weston_surface_from_global_fixed(struct weston_surface *surface,
787 wl_fixed_t x, wl_fixed_t y,
788 wl_fixed_t *sx, wl_fixed_t *sy)
789{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200790 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100791
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400792 weston_surface_from_global_float(surface,
793 wl_fixed_to_double(x),
794 wl_fixed_to_double(y),
795 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100796 *sx = wl_fixed_from_double(sxf);
797 *sy = wl_fixed_from_double(syf);
798}
799
800WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200801weston_surface_from_global(struct weston_surface *surface,
802 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
803{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200804 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200805
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400806 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200807 *sx = floorf(sxf);
808 *sy = floorf(syf);
809}
810
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400811WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400812weston_surface_schedule_repaint(struct weston_surface *surface)
813{
814 struct weston_output *output;
815
816 wl_list_for_each(output, &surface->compositor->output_list, link)
817 if (surface->output_mask & (1 << output->id))
818 weston_output_schedule_repaint(output);
819}
820
821WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500822weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500823{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400824 pixman_region32_union_rect(&surface->damage, &surface->damage,
825 0, 0, surface->geometry.width,
826 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200827
Kristian Høgsberg98238702012-08-03 16:29:12 -0400828 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500829}
830
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400831WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500832weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200833 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400834{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200835 surface->geometry.x = x;
836 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200837 surface->geometry.width = width;
838 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200839 weston_surface_geometry_dirty(surface);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400840}
841
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200842WL_EXPORT void
843weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200844 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200845{
846 surface->geometry.x = x;
847 surface->geometry.y = y;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200848 weston_surface_geometry_dirty(surface);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200849}
850
Pekka Paalanen483243f2013-03-08 14:56:50 +0200851static void
852transform_parent_handle_parent_destroy(struct wl_listener *listener,
853 void *data)
854{
855 struct weston_surface *surface =
856 container_of(listener, struct weston_surface,
857 geometry.parent_destroy_listener);
858
859 weston_surface_set_transform_parent(surface, NULL);
860}
861
862WL_EXPORT void
863weston_surface_set_transform_parent(struct weston_surface *surface,
864 struct weston_surface *parent)
865{
866 if (surface->geometry.parent) {
867 wl_list_remove(&surface->geometry.parent_destroy_listener.link);
868 wl_list_remove(&surface->geometry.parent_link);
869 }
870
871 surface->geometry.parent = parent;
872
873 surface->geometry.parent_destroy_listener.notify =
874 transform_parent_handle_parent_destroy;
875 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500876 wl_signal_add(&parent->destroy_signal,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200877 &surface->geometry.parent_destroy_listener);
878 wl_list_insert(&parent->geometry.child_list,
879 &surface->geometry.parent_link);
880 }
881
882 weston_surface_geometry_dirty(surface);
883}
884
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300885WL_EXPORT int
886weston_surface_is_mapped(struct weston_surface *surface)
887{
888 if (surface->output)
889 return 1;
890 else
891 return 0;
892}
893
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200894WL_EXPORT int32_t
895weston_surface_buffer_width(struct weston_surface *surface)
896{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200897 int32_t width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200898 switch (surface->buffer_transform) {
899 case WL_OUTPUT_TRANSFORM_90:
900 case WL_OUTPUT_TRANSFORM_270:
901 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
902 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200903 width = surface->buffer_ref.buffer->height;
904 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200905 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200906 width = surface->buffer_ref.buffer->width;
907 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200908 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200909 return width / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200910}
911
912WL_EXPORT int32_t
913weston_surface_buffer_height(struct weston_surface *surface)
914{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200915 int32_t height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200916 switch (surface->buffer_transform) {
917 case WL_OUTPUT_TRANSFORM_90:
918 case WL_OUTPUT_TRANSFORM_270:
919 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
920 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200921 height = surface->buffer_ref.buffer->width;
922 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200923 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200924 height = surface->buffer_ref.buffer->height;
925 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200926 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200927 return height / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200928}
929
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400930WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500931weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500932{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400933 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500934
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400935 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500936
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400937 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500938}
939
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400940WL_EXPORT struct weston_surface *
Tiago Vignatti9d393522012-02-10 16:26:19 +0200941weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100942 wl_fixed_t x, wl_fixed_t y,
943 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200944{
945 struct weston_surface *surface;
946
947 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100948 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500949 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100950 wl_fixed_to_int(*sx),
951 wl_fixed_to_int(*sy),
952 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200953 return surface;
954 }
955
956 return NULL;
957}
958
959static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500960weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400961{
Daniel Stone37816df2012-05-16 18:45:18 +0100962 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400963
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500964 if (!compositor->focus)
965 return;
966
Daniel Stone37816df2012-05-16 18:45:18 +0100967 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400968 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400969}
970
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400971WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500972weston_surface_unmap(struct weston_surface *surface)
973{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100974 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500975
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500976 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500977 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500978 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500979
Daniel Stone4dab5db2012-05-30 16:31:53 +0100980 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400981 if (seat->keyboard && seat->keyboard->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400982 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400983 if (seat->pointer && seat->pointer->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400984 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400985 NULL,
986 wl_fixed_from_int(0),
987 wl_fixed_from_int(0));
Daniel Stone4dab5db2012-05-30 16:31:53 +0100988 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500989
Kristian Høgsberg98238702012-08-03 16:29:12 -0400990 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500991}
992
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400993struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -0500994 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400995 struct wl_list link;
996};
997
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500998
999WL_EXPORT void
1000weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001001{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001002 /* Not a valid way to destroy a client surface */
1003 assert(surface->resource == NULL);
1004
1005 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1006
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001007 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001008 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001009
Pekka Paalanen483243f2013-03-08 14:56:50 +02001010 assert(wl_list_empty(&surface->geometry.child_list));
Pekka Paalanene67858b2013-04-25 13:57:42 +03001011 assert(wl_list_empty(&surface->subsurface_list_pending));
1012 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001013
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001014 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001015 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001016
Pekka Paalanenbc106382012-10-10 12:49:31 +03001017 wl_list_for_each_safe(cb, next,
1018 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001019 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001020
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001021 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001022 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001023 pixman_region32_fini(&surface->pending.damage);
1024
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001025 if (surface->pending.buffer)
1026 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1027
Pekka Paalanende685b82012-12-04 15:58:12 +02001028 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001029
Kristian Høgsberg42263852012-09-06 21:59:29 -04001030 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001031
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001032 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001033 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001034 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001035 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001036 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001037
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001038 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001039 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001040
Pekka Paalanen483243f2013-03-08 14:56:50 +02001041 weston_surface_set_transform_parent(surface, NULL);
1042
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001043 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001044}
1045
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001046static void
1047destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001048{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001049 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001050
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001051 surface->resource = NULL;
1052 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001053}
1054
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001055static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001056weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1057{
1058 struct weston_buffer *buffer =
1059 container_of(listener, struct weston_buffer, destroy_listener);
1060
1061 wl_signal_emit(&buffer->destroy_signal, buffer);
1062 free(buffer);
1063}
1064
1065struct weston_buffer *
1066weston_buffer_from_resource(struct wl_resource *resource)
1067{
1068 struct weston_buffer *buffer;
1069 struct wl_listener *listener;
1070
1071 listener = wl_resource_get_destroy_listener(resource,
1072 weston_buffer_destroy_handler);
1073
1074 if (listener) {
1075 buffer = container_of(listener, struct weston_buffer,
1076 destroy_listener);
1077 } else {
1078 buffer = malloc(sizeof *buffer);
1079 memset(buffer, 0, sizeof *buffer);
1080
1081 buffer->resource = resource;
1082 wl_signal_init(&buffer->destroy_signal);
1083 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
1084 wl_resource_add_destroy_listener(resource,
1085 &buffer->destroy_listener);
1086 }
1087
1088 return buffer;
1089}
1090
1091static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001092weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1093 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001094{
Pekka Paalanende685b82012-12-04 15:58:12 +02001095 struct weston_buffer_reference *ref =
1096 container_of(listener, struct weston_buffer_reference,
1097 destroy_listener);
1098
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001099 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001100 ref->buffer = NULL;
1101}
1102
1103WL_EXPORT void
1104weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001105 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001106{
1107 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001108 ref->buffer->busy_count--;
1109 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001110 assert(wl_resource_get_client(ref->buffer->resource));
1111 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001112 WL_BUFFER_RELEASE);
1113 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001114 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001115 }
1116
Pekka Paalanende685b82012-12-04 15:58:12 +02001117 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001118 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001119 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001120 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001121 }
1122
Pekka Paalanende685b82012-12-04 15:58:12 +02001123 ref->buffer = buffer;
1124 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1125}
1126
1127static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001128weston_surface_attach(struct weston_surface *surface,
1129 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001130{
1131 weston_buffer_reference(&surface->buffer_ref, buffer);
1132
Pekka Paalanena6421c42012-12-04 15:58:10 +02001133 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001134 if (weston_surface_is_mapped(surface))
1135 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001136 }
1137
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001138 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001139}
1140
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001141WL_EXPORT void
1142weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001143{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001144 wl_list_remove(&surface->layer_link);
1145 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001146 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001147 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001148}
1149
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001150WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001151weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001152{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001153 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001154
1155 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001156 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001157}
1158
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001159WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001160weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001161{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001162 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001163
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001164 pixman_region32_union(&compositor->primary_plane.damage,
1165 &compositor->primary_plane.damage,
1166 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001167 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001168}
1169
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001170static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001171surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001172 pixman_region32_t *opaque)
1173{
Pekka Paalanende685b82012-12-04 15:58:12 +02001174 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001175 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001176 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001177
1178 if (surface->transform.enabled) {
1179 pixman_box32_t *extents;
1180
1181 extents = pixman_region32_extents(&surface->damage);
1182 surface_compute_bbox(surface, extents->x1, extents->y1,
1183 extents->x2 - extents->x1,
1184 extents->y2 - extents->y1,
1185 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001186 pixman_region32_translate(&surface->damage,
1187 -surface->plane->x,
1188 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001189 } else {
1190 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001191 surface->geometry.x - surface->plane->x,
1192 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001193 }
1194
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001195 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001196 pixman_region32_union(&surface->plane->damage,
1197 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001198 empty_region(&surface->damage);
1199 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001200 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001201}
1202
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001203static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001204compositor_accumulate_damage(struct weston_compositor *ec)
1205{
1206 struct weston_plane *plane;
1207 struct weston_surface *es;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001208 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001209
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001210 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001211
1212 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001213 pixman_region32_copy(&plane->clip, &clip);
1214
1215 pixman_region32_init(&opaque);
1216
1217 wl_list_for_each(es, &ec->surface_list, link) {
1218 if (es->plane != plane)
1219 continue;
1220
1221 surface_accumulate_damage(es, &opaque);
1222 }
1223
1224 pixman_region32_union(&clip, &clip, &opaque);
1225 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001226 }
1227
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001228 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001229
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001230 wl_list_for_each(es, &ec->surface_list, link) {
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001231 /* Both the renderer and the backend have seen the buffer
1232 * by now. If renderer needs the buffer, it has its own
1233 * reference set. If the backend wants to keep the buffer
1234 * around for migrating the surface into a non-primary plane
1235 * later, keep_buffer is true. Otherwise, drop the core
1236 * reference now, and allow early buffer release. This enables
1237 * clients to use single-buffering.
1238 */
1239 if (!es->keep_buffer)
1240 weston_buffer_reference(&es->buffer_ref, NULL);
1241 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001242}
1243
1244static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001245surface_list_add(struct weston_compositor *compositor,
1246 struct weston_surface *surface)
1247{
1248 struct weston_subsurface *sub;
1249
1250 if (wl_list_empty(&surface->subsurface_list)) {
1251 weston_surface_update_transform(surface);
1252 wl_list_insert(compositor->surface_list.prev, &surface->link);
1253 return;
1254 }
1255
1256 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1257 if (!weston_surface_is_mapped(sub->surface))
1258 continue;
1259
1260 if (sub->surface == surface) {
1261 weston_surface_update_transform(sub->surface);
1262 wl_list_insert(compositor->surface_list.prev,
1263 &sub->surface->link);
1264 } else {
1265 surface_list_add(compositor, sub->surface);
1266 }
1267 }
1268}
1269
1270static void
1271weston_compositor_build_surface_list(struct weston_compositor *compositor)
1272{
1273 struct weston_surface *surface;
1274 struct weston_layer *layer;
1275
1276 wl_list_init(&compositor->surface_list);
1277 wl_list_for_each(layer, &compositor->layer_list, link) {
1278 wl_list_for_each(surface, &layer->surface_list, layer_link) {
1279 surface_list_add(compositor, surface);
1280 }
1281 }
1282}
1283
1284static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001285weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001286{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001287 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001288 struct weston_surface *es;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001289 struct weston_animation *animation, *next;
1290 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001291 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001292 pixman_region32_t output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001293
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001294 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001295 weston_compositor_build_surface_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001296
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001297 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001298 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001299 else
1300 wl_list_for_each(es, &ec->surface_list, link)
1301 weston_surface_move_to_plane(es, &ec->primary_plane);
1302
Pekka Paalanene67858b2013-04-25 13:57:42 +03001303 wl_list_init(&frame_callback_list);
1304 wl_list_for_each(es, &ec->surface_list, link) {
1305 if (es->output == output) {
1306 wl_list_insert_list(&frame_callback_list,
1307 &es->frame_callback_list);
1308 wl_list_init(&es->frame_callback_list);
1309 }
1310 }
1311
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001312 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001313
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001314 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001315 pixman_region32_intersect(&output_damage,
1316 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001317 pixman_region32_subtract(&output_damage,
1318 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001319
Scott Moreauccbf29d2012-02-22 14:21:41 -07001320 if (output->dirty)
1321 weston_output_update_matrix(output);
1322
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001323 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001324
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001325 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001326
Kristian Høgsbergef044142011-06-21 15:02:12 -04001327 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001328
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001329 weston_compositor_repick(ec);
1330 wl_event_loop_dispatch(ec->input_loop, 0);
1331
Jonas Ådahldb773762012-06-13 00:01:21 +02001332 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001333 wl_callback_send_done(cb->resource, msecs);
1334 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001335 }
1336
Scott Moreaud64cf212012-06-08 19:40:54 -06001337 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001338 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001339 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001340 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001341}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001342
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001343static int
1344weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001345{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001346 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001347
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001348 wl_event_loop_dispatch(compositor->input_loop, 0);
1349
1350 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001351}
1352
1353WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001354weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001355{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001356 struct weston_compositor *compositor = output->compositor;
1357 struct wl_event_loop *loop =
1358 wl_display_get_event_loop(compositor->wl_display);
1359 int fd;
1360
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001361 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001362 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001363 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001364 return;
1365 }
1366
1367 output->repaint_scheduled = 0;
1368 if (compositor->input_loop_source)
1369 return;
1370
1371 fd = wl_event_loop_get_fd(compositor->input_loop);
1372 compositor->input_loop_source =
1373 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1374 weston_compositor_read_input, compositor);
1375}
1376
1377static void
1378idle_repaint(void *data)
1379{
1380 struct weston_output *output = data;
1381
Jonas Ådahle5a12252013-04-05 23:07:11 +02001382 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001383}
1384
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001385WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001386weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1387{
1388 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001389 if (below != NULL)
1390 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001391}
1392
1393WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001394weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001395{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001396 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001397 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001398
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001399 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1400 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001401 return;
1402
Kristian Høgsbergef044142011-06-21 15:02:12 -04001403 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001404 output->repaint_needed = 1;
1405 if (output->repaint_scheduled)
1406 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001407
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001408 wl_event_loop_add_idle(loop, idle_repaint, output);
1409 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001410
1411 if (compositor->input_loop_source) {
1412 wl_event_source_remove(compositor->input_loop_source);
1413 compositor->input_loop_source = NULL;
1414 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001415}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001416
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001417WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001418weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1419{
1420 struct weston_output *output;
1421
1422 wl_list_for_each(output, &compositor->output_list, link)
1423 weston_output_schedule_repaint(output);
1424}
1425
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001426static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001427surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001428{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001429 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001430}
1431
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001432static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001433surface_attach(struct wl_client *client,
1434 struct wl_resource *resource,
1435 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1436{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001437 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001438 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001439
1440 if (buffer_resource)
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001441 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001442
Pekka Paalanende685b82012-12-04 15:58:12 +02001443 /* Attach, attach, without commit in between does not send
1444 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001445 if (surface->pending.buffer)
1446 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001447
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001448 surface->pending.sx = sx;
1449 surface->pending.sy = sy;
1450 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001451 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001452 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001453 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001454 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001455 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001456}
1457
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001458static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001459surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001460 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001461 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001462{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001463 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001464
Pekka Paalanen8e159182012-10-10 12:49:25 +03001465 pixman_region32_union_rect(&surface->pending.damage,
1466 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001467 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001468}
1469
Kristian Høgsberg33418202011-08-16 23:01:28 -04001470static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001471destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001472{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001473 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001474
1475 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001476 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001477}
1478
1479static void
1480surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001481 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001482{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001483 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001484 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001485
1486 cb = malloc(sizeof *cb);
1487 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001488 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001489 return;
1490 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001491
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001492 cb->resource = wl_client_add_object(client, &wl_callback_interface,
1493 NULL, callback, cb);
1494 wl_resource_set_destructor(cb->resource, destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001495
Pekka Paalanenbc106382012-10-10 12:49:31 +03001496 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001497}
1498
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001499static void
1500surface_set_opaque_region(struct wl_client *client,
1501 struct wl_resource *resource,
1502 struct wl_resource *region_resource)
1503{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001504 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001505 struct weston_region *region;
1506
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001507 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001508 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001509 pixman_region32_copy(&surface->pending.opaque,
1510 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001511 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001512 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001513 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001514}
1515
1516static void
1517surface_set_input_region(struct wl_client *client,
1518 struct wl_resource *resource,
1519 struct wl_resource *region_resource)
1520{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001521 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001522 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001523
1524 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001525 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001526 pixman_region32_copy(&surface->pending.input,
1527 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001528 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001529 pixman_region32_fini(&surface->pending.input);
1530 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001531 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001532}
1533
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001534static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001535weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001536{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001537 struct weston_subsurface *sub;
1538
1539 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1540 parent_link_pending) {
1541 wl_list_remove(&sub->parent_link);
1542 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1543 }
1544}
1545
1546static void
1547weston_surface_commit(struct weston_surface *surface)
1548{
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001549 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001550 int surface_width = 0;
1551 int surface_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001552
Alexander Larsson4ea95522013-05-22 14:41:37 +02001553 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001554 surface->buffer_transform = surface->pending.buffer_transform;
1555
Alexander Larsson4ea95522013-05-22 14:41:37 +02001556 /* wl_surface.set_buffer_scale */
1557 surface->buffer_scale = surface->pending.buffer_scale;
1558
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001559 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001560 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001561 weston_surface_attach(surface, surface->pending.buffer);
1562
Giulio Camuffo184df502013-02-21 11:29:21 +01001563 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001564 surface_width = weston_surface_buffer_width(surface);
1565 surface_height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001566 }
1567
1568 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001569 surface->configure(surface,
1570 surface->pending.sx, surface->pending.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001571 surface_width, surface_height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001572
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001573 if (surface->pending.buffer)
1574 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1575 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001576 surface->pending.sx = 0;
1577 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001578 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001579
1580 /* wl_surface.damage */
1581 pixman_region32_union(&surface->damage, &surface->damage,
1582 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001583 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1584 0, 0,
1585 surface->geometry.width,
1586 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001587 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001588
1589 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001590 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001591 surface->geometry.width,
1592 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001593 pixman_region32_intersect(&opaque,
1594 &opaque, &surface->pending.opaque);
1595
1596 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1597 pixman_region32_copy(&surface->opaque, &opaque);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001598 weston_surface_geometry_dirty(surface);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001599 }
1600
1601 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001602
1603 /* wl_surface.set_input_region */
1604 pixman_region32_fini(&surface->input);
1605 pixman_region32_init_rect(&surface->input, 0, 0,
1606 surface->geometry.width,
1607 surface->geometry.height);
1608 pixman_region32_intersect(&surface->input,
1609 &surface->input, &surface->pending.input);
1610
Pekka Paalanenbc106382012-10-10 12:49:31 +03001611 /* wl_surface.frame */
1612 wl_list_insert_list(&surface->frame_callback_list,
1613 &surface->pending.frame_callback_list);
1614 wl_list_init(&surface->pending.frame_callback_list);
1615
Pekka Paalanene67858b2013-04-25 13:57:42 +03001616 weston_surface_commit_subsurface_order(surface);
1617
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001618 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001619}
1620
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001621static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001622weston_subsurface_commit(struct weston_subsurface *sub);
1623
1624static void
1625weston_subsurface_parent_commit(struct weston_subsurface *sub,
1626 int parent_is_synchronized);
1627
1628static void
1629surface_commit(struct wl_client *client, struct wl_resource *resource)
1630{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001631 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001632 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1633
1634 if (sub) {
1635 weston_subsurface_commit(sub);
1636 return;
1637 }
1638
1639 weston_surface_commit(surface);
1640
1641 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1642 if (sub->surface != surface)
1643 weston_subsurface_parent_commit(sub, 0);
1644 }
1645}
1646
1647static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001648surface_set_buffer_transform(struct wl_client *client,
1649 struct wl_resource *resource, int transform)
1650{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001651 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001652
1653 surface->pending.buffer_transform = transform;
1654}
1655
Alexander Larsson4ea95522013-05-22 14:41:37 +02001656static void
1657surface_set_buffer_scale(struct wl_client *client,
1658 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001659 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02001660{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001661 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02001662
1663 surface->pending.buffer_scale = scale;
1664}
1665
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001666static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001667 surface_destroy,
1668 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001669 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001670 surface_frame,
1671 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001672 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001673 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001674 surface_set_buffer_transform,
1675 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001676};
1677
1678static void
1679compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001680 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001681{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04001682 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001683 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001684
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001685 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001686 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001687 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001688 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001689 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001690
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001691 surface->resource = wl_client_add_object(client, &wl_surface_interface,
1692 &surface_interface,
1693 id, surface);
1694 wl_resource_set_destructor(surface->resource, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001695}
1696
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001697static void
1698destroy_region(struct wl_resource *resource)
1699{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001700 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001701
1702 pixman_region32_fini(&region->region);
1703 free(region);
1704}
1705
1706static void
1707region_destroy(struct wl_client *client, struct wl_resource *resource)
1708{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001709 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001710}
1711
1712static void
1713region_add(struct wl_client *client, struct wl_resource *resource,
1714 int32_t x, int32_t y, int32_t width, int32_t height)
1715{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001716 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001717
1718 pixman_region32_union_rect(&region->region, &region->region,
1719 x, y, width, height);
1720}
1721
1722static void
1723region_subtract(struct wl_client *client, struct wl_resource *resource,
1724 int32_t x, int32_t y, int32_t width, int32_t height)
1725{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001726 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001727 pixman_region32_t rect;
1728
1729 pixman_region32_init_rect(&rect, x, y, width, height);
1730 pixman_region32_subtract(&region->region, &region->region, &rect);
1731 pixman_region32_fini(&rect);
1732}
1733
1734static const struct wl_region_interface region_interface = {
1735 region_destroy,
1736 region_add,
1737 region_subtract
1738};
1739
1740static void
1741compositor_create_region(struct wl_client *client,
1742 struct wl_resource *resource, uint32_t id)
1743{
1744 struct weston_region *region;
1745
1746 region = malloc(sizeof *region);
1747 if (region == NULL) {
1748 wl_resource_post_no_memory(resource);
1749 return;
1750 }
1751
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001752 pixman_region32_init(&region->region);
1753
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001754 region->resource = wl_client_add_object(client, &wl_region_interface,
1755 &region_interface, id, region);
1756 wl_resource_set_destructor(region->resource, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001757}
1758
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001759static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001760 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001761 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001762};
1763
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001764static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001765weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
1766{
1767 struct weston_surface *surface = sub->surface;
1768 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001769 int surface_width = 0;
1770 int surface_height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001771
Alexander Larsson4ea95522013-05-22 14:41:37 +02001772 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001773 surface->buffer_transform = sub->cached.buffer_transform;
1774
Alexander Larsson4ea95522013-05-22 14:41:37 +02001775 /* wl_surface.set_buffer_scale */
1776 surface->buffer_scale = sub->cached.buffer_scale;
1777
Pekka Paalanene67858b2013-04-25 13:57:42 +03001778 /* wl_surface.attach */
1779 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
1780 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
1781 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
1782
1783 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001784 surface_width = weston_surface_buffer_width(surface);
1785 surface_height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001786 }
1787
1788 if (surface->configure && sub->cached.newly_attached)
1789 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001790 surface_width, surface_height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001791 sub->cached.sx = 0;
1792 sub->cached.sy = 0;
1793 sub->cached.newly_attached = 0;
1794
1795 /* wl_surface.damage */
1796 pixman_region32_union(&surface->damage, &surface->damage,
1797 &sub->cached.damage);
1798 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1799 0, 0,
1800 surface->geometry.width,
1801 surface->geometry.height);
1802 empty_region(&sub->cached.damage);
1803
1804 /* wl_surface.set_opaque_region */
1805 pixman_region32_init_rect(&opaque, 0, 0,
1806 surface->geometry.width,
1807 surface->geometry.height);
1808 pixman_region32_intersect(&opaque,
1809 &opaque, &sub->cached.opaque);
1810
1811 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1812 pixman_region32_copy(&surface->opaque, &opaque);
1813 weston_surface_geometry_dirty(surface);
1814 }
1815
1816 pixman_region32_fini(&opaque);
1817
1818 /* wl_surface.set_input_region */
1819 pixman_region32_fini(&surface->input);
1820 pixman_region32_init_rect(&surface->input, 0, 0,
1821 surface->geometry.width,
1822 surface->geometry.height);
1823 pixman_region32_intersect(&surface->input,
1824 &surface->input, &sub->cached.input);
1825
1826 /* wl_surface.frame */
1827 wl_list_insert_list(&surface->frame_callback_list,
1828 &sub->cached.frame_callback_list);
1829 wl_list_init(&sub->cached.frame_callback_list);
1830
1831 weston_surface_commit_subsurface_order(surface);
1832
1833 weston_surface_schedule_repaint(surface);
1834
1835 sub->cached.has_data = 0;
1836}
1837
1838static void
1839weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
1840{
1841 struct weston_surface *surface = sub->surface;
1842
1843 /*
1844 * If this commit would cause the surface to move by the
1845 * attach(dx, dy) parameters, the old damage region must be
1846 * translated to correspond to the new surface coordinate system
1847 * origin.
1848 */
1849 pixman_region32_translate(&sub->cached.damage,
1850 -surface->pending.sx, -surface->pending.sy);
1851 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
1852 &surface->pending.damage);
1853 empty_region(&surface->pending.damage);
1854
1855 if (surface->pending.newly_attached) {
1856 sub->cached.newly_attached = 1;
1857 weston_buffer_reference(&sub->cached.buffer_ref,
1858 surface->pending.buffer);
1859 }
1860 sub->cached.sx += surface->pending.sx;
1861 sub->cached.sy += surface->pending.sy;
1862 surface->pending.sx = 0;
1863 surface->pending.sy = 0;
1864 surface->pending.newly_attached = 0;
1865
1866 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001867 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001868
1869 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
1870
1871 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
1872
1873 wl_list_insert_list(&sub->cached.frame_callback_list,
1874 &surface->pending.frame_callback_list);
1875 wl_list_init(&surface->pending.frame_callback_list);
1876
1877 sub->cached.has_data = 1;
1878}
1879
1880static int
1881weston_subsurface_is_synchronized(struct weston_subsurface *sub)
1882{
1883 while (sub) {
1884 if (sub->synchronized)
1885 return 1;
1886
1887 if (!sub->parent)
1888 return 0;
1889
1890 sub = weston_surface_to_subsurface(sub->parent);
1891 }
1892
1893 return 0;
1894}
1895
1896static void
1897weston_subsurface_commit(struct weston_subsurface *sub)
1898{
1899 struct weston_surface *surface = sub->surface;
1900 struct weston_subsurface *tmp;
1901
1902 /* Recursive check for effectively synchronized. */
1903 if (weston_subsurface_is_synchronized(sub)) {
1904 weston_subsurface_commit_to_cache(sub);
1905 } else {
1906 if (sub->cached.has_data) {
1907 /* flush accumulated state from cache */
1908 weston_subsurface_commit_to_cache(sub);
1909 weston_subsurface_commit_from_cache(sub);
1910 } else {
1911 weston_surface_commit(surface);
1912 }
1913
1914 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1915 if (tmp->surface != surface)
1916 weston_subsurface_parent_commit(tmp, 0);
1917 }
1918 }
1919}
1920
1921static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001922weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001923{
1924 struct weston_surface *surface = sub->surface;
1925 struct weston_subsurface *tmp;
1926
Pekka Paalanene67858b2013-04-25 13:57:42 +03001927 /* From now on, commit_from_cache the whole sub-tree, regardless of
1928 * the synchronized mode of each child. This sub-surface or some
1929 * of its ancestors were synchronized, so we are synchronized
1930 * all the way down.
1931 */
1932
1933 if (sub->cached.has_data)
1934 weston_subsurface_commit_from_cache(sub);
1935
1936 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1937 if (tmp->surface != surface)
1938 weston_subsurface_parent_commit(tmp, 1);
1939 }
1940}
1941
1942static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001943weston_subsurface_parent_commit(struct weston_subsurface *sub,
1944 int parent_is_synchronized)
1945{
1946 if (sub->position.set) {
1947 weston_surface_set_position(sub->surface,
1948 sub->position.x, sub->position.y);
1949 sub->position.set = 0;
1950 }
1951
1952 if (parent_is_synchronized || sub->synchronized)
1953 weston_subsurface_synchronized_commit(sub);
1954}
1955
1956static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001957subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
1958 int32_t width, int32_t height)
1959{
1960 struct weston_compositor *compositor = surface->compositor;
1961
1962 weston_surface_configure(surface,
1963 surface->geometry.x + dx,
1964 surface->geometry.y + dy,
1965 width, height);
1966
1967 /* No need to check parent mappedness, because if parent is not
1968 * mapped, parent is not in a visible layer, so this sub-surface
1969 * will not be drawn either.
1970 */
1971 if (!weston_surface_is_mapped(surface)) {
1972 wl_list_init(&surface->layer_link);
1973
1974 /* Cannot call weston_surface_update_transform(),
1975 * because that would call it also for the parent surface,
1976 * which might not be mapped yet. That would lead to
1977 * inconsistent state, where the window could never be
1978 * mapped.
1979 *
1980 * Instead just assing any output, to make
1981 * weston_surface_is_mapped() return true, so that when the
1982 * parent surface does get mapped, this one will get
1983 * included, too. See surface_list_add().
1984 */
1985 assert(!wl_list_empty(&compositor->output_list));
1986 surface->output = container_of(compositor->output_list.next,
1987 struct weston_output, link);
1988 }
1989}
1990
1991static struct weston_subsurface *
1992weston_surface_to_subsurface(struct weston_surface *surface)
1993{
1994 if (surface->configure == subsurface_configure)
1995 return surface->configure_private;
1996
1997 return NULL;
1998}
1999
Pekka Paalanen01388e22013-04-25 13:57:44 +03002000WL_EXPORT struct weston_surface *
2001weston_surface_get_main_surface(struct weston_surface *surface)
2002{
2003 struct weston_subsurface *sub;
2004
2005 while (surface && (sub = weston_surface_to_subsurface(surface)))
2006 surface = sub->parent;
2007
2008 return surface;
2009}
2010
Pekka Paalanene67858b2013-04-25 13:57:42 +03002011static void
2012subsurface_set_position(struct wl_client *client,
2013 struct wl_resource *resource, int32_t x, int32_t y)
2014{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002015 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002016
2017 if (!sub)
2018 return;
2019
2020 sub->position.x = x;
2021 sub->position.y = y;
2022 sub->position.set = 1;
2023}
2024
2025static struct weston_subsurface *
2026subsurface_from_surface(struct weston_surface *surface)
2027{
2028 struct weston_subsurface *sub;
2029
2030 sub = weston_surface_to_subsurface(surface);
2031 if (sub)
2032 return sub;
2033
2034 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2035 if (sub->surface == surface)
2036 return sub;
2037
2038 return NULL;
2039}
2040
2041static struct weston_subsurface *
2042subsurface_sibling_check(struct weston_subsurface *sub,
2043 struct weston_surface *surface,
2044 const char *request)
2045{
2046 struct weston_subsurface *sibling;
2047
2048 sibling = subsurface_from_surface(surface);
2049
2050 if (!sibling) {
2051 wl_resource_post_error(sub->resource,
2052 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2053 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002054 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002055 return NULL;
2056 }
2057
2058 if (sibling->parent != sub->parent) {
2059 wl_resource_post_error(sub->resource,
2060 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2061 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002062 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002063 return NULL;
2064 }
2065
2066 return sibling;
2067}
2068
2069static void
2070subsurface_place_above(struct wl_client *client,
2071 struct wl_resource *resource,
2072 struct wl_resource *sibling_resource)
2073{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002074 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002075 struct weston_surface *surface =
2076 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002077 struct weston_subsurface *sibling;
2078
2079 if (!sub)
2080 return;
2081
2082 sibling = subsurface_sibling_check(sub, surface, "place_above");
2083 if (!sibling)
2084 return;
2085
2086 wl_list_remove(&sub->parent_link_pending);
2087 wl_list_insert(sibling->parent_link_pending.prev,
2088 &sub->parent_link_pending);
2089}
2090
2091static void
2092subsurface_place_below(struct wl_client *client,
2093 struct wl_resource *resource,
2094 struct wl_resource *sibling_resource)
2095{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002096 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002097 struct weston_surface *surface =
2098 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002099 struct weston_subsurface *sibling;
2100
2101 if (!sub)
2102 return;
2103
2104 sibling = subsurface_sibling_check(sub, surface, "place_below");
2105 if (!sibling)
2106 return;
2107
2108 wl_list_remove(&sub->parent_link_pending);
2109 wl_list_insert(&sibling->parent_link_pending,
2110 &sub->parent_link_pending);
2111}
2112
2113static void
2114subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2115{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002116 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002117
2118 if (sub)
2119 sub->synchronized = 1;
2120}
2121
2122static void
2123subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2124{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002125 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002126
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002127 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002128 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002129
2130 /* If sub became effectively desynchronized, flush. */
2131 if (!weston_subsurface_is_synchronized(sub))
2132 weston_subsurface_synchronized_commit(sub);
2133 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002134}
2135
2136static void
2137weston_subsurface_cache_init(struct weston_subsurface *sub)
2138{
2139 pixman_region32_init(&sub->cached.damage);
2140 pixman_region32_init(&sub->cached.opaque);
2141 pixman_region32_init(&sub->cached.input);
2142 wl_list_init(&sub->cached.frame_callback_list);
2143 sub->cached.buffer_ref.buffer = NULL;
2144}
2145
2146static void
2147weston_subsurface_cache_fini(struct weston_subsurface *sub)
2148{
2149 struct weston_frame_callback *cb, *tmp;
2150
2151 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002152 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002153
2154 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2155 pixman_region32_fini(&sub->cached.damage);
2156 pixman_region32_fini(&sub->cached.opaque);
2157 pixman_region32_fini(&sub->cached.input);
2158}
2159
2160static void
2161weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2162{
2163 wl_list_remove(&sub->parent_link);
2164 wl_list_remove(&sub->parent_link_pending);
2165 wl_list_remove(&sub->parent_destroy_listener.link);
2166 sub->parent = NULL;
2167}
2168
2169static void
2170weston_subsurface_destroy(struct weston_subsurface *sub);
2171
2172static void
2173subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2174{
2175 struct weston_subsurface *sub =
2176 container_of(listener, struct weston_subsurface,
2177 surface_destroy_listener);
2178 assert(data == &sub->surface->resource);
2179
2180 /* The protocol object (wl_resource) is left inert. */
2181 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002182 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002183
2184 weston_subsurface_destroy(sub);
2185}
2186
2187static void
2188subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2189{
2190 struct weston_subsurface *sub =
2191 container_of(listener, struct weston_subsurface,
2192 parent_destroy_listener);
2193 assert(data == &sub->parent->resource);
2194 assert(sub->surface != sub->parent);
2195
2196 if (weston_surface_is_mapped(sub->surface))
2197 weston_surface_unmap(sub->surface);
2198
2199 weston_subsurface_unlink_parent(sub);
2200}
2201
2202static void
2203subsurface_resource_destroy(struct wl_resource *resource)
2204{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002205 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002206
2207 if (sub)
2208 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002209}
2210
2211static void
2212subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2213{
2214 wl_resource_destroy(resource);
2215}
2216
2217static void
2218weston_subsurface_link_parent(struct weston_subsurface *sub,
2219 struct weston_surface *parent)
2220{
2221 sub->parent = parent;
2222 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002223 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002224 &sub->parent_destroy_listener);
2225
2226 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2227 wl_list_insert(&parent->subsurface_list_pending,
2228 &sub->parent_link_pending);
2229}
2230
2231static void
2232weston_subsurface_link_surface(struct weston_subsurface *sub,
2233 struct weston_surface *surface)
2234{
2235 sub->surface = surface;
2236 sub->surface_destroy_listener.notify =
2237 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002238 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002239 &sub->surface_destroy_listener);
2240}
2241
2242static void
2243weston_subsurface_destroy(struct weston_subsurface *sub)
2244{
2245 assert(sub->surface);
2246
2247 if (sub->resource) {
2248 assert(weston_surface_to_subsurface(sub->surface) == sub);
2249 assert(sub->parent_destroy_listener.notify ==
2250 subsurface_handle_parent_destroy);
2251
2252 weston_surface_set_transform_parent(sub->surface, NULL);
2253 if (sub->parent)
2254 weston_subsurface_unlink_parent(sub);
2255
2256 weston_subsurface_cache_fini(sub);
2257
2258 sub->surface->configure = NULL;
2259 sub->surface->configure_private = NULL;
2260 } else {
2261 /* the dummy weston_subsurface for the parent itself */
2262 assert(sub->parent_destroy_listener.notify == NULL);
2263 wl_list_remove(&sub->parent_link);
2264 wl_list_remove(&sub->parent_link_pending);
2265 }
2266
2267 wl_list_remove(&sub->surface_destroy_listener.link);
2268 free(sub);
2269}
2270
2271static const struct wl_subsurface_interface subsurface_implementation = {
2272 subsurface_destroy,
2273 subsurface_set_position,
2274 subsurface_place_above,
2275 subsurface_place_below,
2276 subsurface_set_sync,
2277 subsurface_set_desync
2278};
2279
2280static struct weston_subsurface *
2281weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2282 struct weston_surface *parent)
2283{
2284 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002285 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002286
2287 sub = calloc(1, sizeof *sub);
2288 if (!sub)
2289 return NULL;
2290
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002291 sub->resource = wl_client_add_object(client,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002292 &wl_subsurface_interface,
2293 &subsurface_implementation,
2294 id, sub);
2295 if (!sub->resource) {
2296 free(sub);
2297 return NULL;
2298 }
2299
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002300 wl_resource_set_destructor(sub->resource, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002301 weston_subsurface_link_surface(sub, surface);
2302 weston_subsurface_link_parent(sub, parent);
2303 weston_subsurface_cache_init(sub);
2304 sub->synchronized = 1;
2305 weston_surface_set_transform_parent(surface, parent);
2306
2307 return sub;
2308}
2309
2310/* Create a dummy subsurface for having the parent itself in its
2311 * sub-surface lists. Makes stacking order manipulation easy.
2312 */
2313static struct weston_subsurface *
2314weston_subsurface_create_for_parent(struct weston_surface *parent)
2315{
2316 struct weston_subsurface *sub;
2317
2318 sub = calloc(1, sizeof *sub);
2319 if (!sub)
2320 return NULL;
2321
2322 weston_subsurface_link_surface(sub, parent);
2323 sub->parent = parent;
2324 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2325 wl_list_insert(&parent->subsurface_list_pending,
2326 &sub->parent_link_pending);
2327
2328 return sub;
2329}
2330
2331static void
2332subcompositor_get_subsurface(struct wl_client *client,
2333 struct wl_resource *resource,
2334 uint32_t id,
2335 struct wl_resource *surface_resource,
2336 struct wl_resource *parent_resource)
2337{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002338 struct weston_surface *surface =
2339 wl_resource_get_user_data(surface_resource);
2340 struct weston_surface *parent =
2341 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002342 struct weston_subsurface *sub;
2343 static const char where[] = "get_subsurface: wl_subsurface@";
2344
2345 if (surface == parent) {
2346 wl_resource_post_error(resource,
2347 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2348 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002349 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002350 return;
2351 }
2352
2353 if (weston_surface_to_subsurface(surface)) {
2354 wl_resource_post_error(resource,
2355 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2356 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002357 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002358 return;
2359 }
2360
2361 if (surface->configure) {
2362 wl_resource_post_error(resource,
2363 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2364 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002365 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002366 return;
2367 }
2368
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002369 if (weston_surface_get_main_surface(parent) == surface) {
2370 wl_resource_post_error(resource,
2371 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2372 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002373 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002374 return;
2375 }
2376
Pekka Paalanene67858b2013-04-25 13:57:42 +03002377 /* make sure the parent is in its own list */
2378 if (wl_list_empty(&parent->subsurface_list)) {
2379 if (!weston_subsurface_create_for_parent(parent)) {
2380 wl_resource_post_no_memory(resource);
2381 return;
2382 }
2383 }
2384
2385 sub = weston_subsurface_create(id, surface, parent);
2386 if (!sub) {
2387 wl_resource_post_no_memory(resource);
2388 return;
2389 }
2390
2391 surface->configure = subsurface_configure;
2392 surface->configure_private = sub;
2393}
2394
2395static void
2396subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2397{
2398 wl_resource_destroy(resource);
2399}
2400
2401static const struct wl_subcompositor_interface subcompositor_interface = {
2402 subcompositor_destroy,
2403 subcompositor_get_subsurface
2404};
2405
2406static void
2407bind_subcompositor(struct wl_client *client,
2408 void *data, uint32_t version, uint32_t id)
2409{
2410 struct weston_compositor *compositor = data;
2411
2412 wl_client_add_object(client, &wl_subcompositor_interface,
2413 &subcompositor_interface, id, compositor);
2414}
2415
2416static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002417weston_compositor_dpms(struct weston_compositor *compositor,
2418 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002419{
2420 struct weston_output *output;
2421
2422 wl_list_for_each(output, &compositor->output_list, link)
2423 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002424 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002425}
2426
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002427WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002428weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002429{
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002430 switch (compositor->state) {
2431 case WESTON_COMPOSITOR_SLEEPING:
2432 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2433 /* fall through */
2434 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002435 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002436 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002437 /* fall through */
2438 default:
2439 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2440 wl_event_source_timer_update(compositor->idle_source,
2441 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002442 }
2443}
2444
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002445WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002446weston_compositor_offscreen(struct weston_compositor *compositor)
2447{
2448 switch (compositor->state) {
2449 case WESTON_COMPOSITOR_OFFSCREEN:
2450 return;
2451 case WESTON_COMPOSITOR_SLEEPING:
2452 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2453 /* fall through */
2454 default:
2455 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2456 wl_event_source_timer_update(compositor->idle_source, 0);
2457 }
2458}
2459
2460WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002461weston_compositor_sleep(struct weston_compositor *compositor)
2462{
2463 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2464 return;
2465
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002466 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002467 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2468 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2469}
2470
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002471static int
2472idle_handler(void *data)
2473{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002474 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002475
2476 if (compositor->idle_inhibit)
2477 return 1;
2478
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002479 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002480 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002481
2482 return 1;
2483}
2484
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002485WL_EXPORT void
2486weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2487{
2488 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002489 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002490 plane->x = x;
2491 plane->y = y;
2492}
2493
2494WL_EXPORT void
2495weston_plane_release(struct weston_plane *plane)
2496{
2497 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002498 pixman_region32_fini(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002499}
2500
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002501WL_EXPORT void
2502weston_compositor_stack_plane(struct weston_compositor *ec,
2503 struct weston_plane *plane,
2504 struct weston_plane *above)
2505{
2506 if (above)
2507 wl_list_insert(above->link.prev, &plane->link);
2508 else
2509 wl_list_insert(&ec->plane_list, &plane->link);
2510}
2511
Casey Dahlin9074db52012-04-19 22:50:09 -04002512static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002513{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002514 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002515}
2516
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002517static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002518bind_output(struct wl_client *client,
2519 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002520{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002521 struct weston_output *output = data;
2522 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002523 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002524
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002525 resource = wl_client_add_object(client,
2526 &wl_output_interface, NULL, id, data);
2527
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002528 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
2529 wl_resource_set_destructor(resource, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002530
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002531 wl_output_send_geometry(resource,
2532 output->x,
2533 output->y,
2534 output->mm_width,
2535 output->mm_height,
2536 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002537 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002538 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002539 if (version >= 2)
2540 wl_output_send_scale(resource,
2541 output->scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002542
2543 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002544 wl_output_send_mode(resource,
2545 mode->flags,
2546 mode->width,
2547 mode->height,
2548 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002549 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002550
2551 if (version >= 2)
2552 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002553}
2554
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002555WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002556weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002557{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002558 struct weston_compositor *c = output->compositor;
2559
Richard Hughes64ddde12013-05-01 21:52:10 +01002560 wl_signal_emit(&output->destroy_signal, output);
2561
Richard Hughesafe690c2013-05-02 10:10:04 +01002562 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002563 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002564 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002565 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002566
2567 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002568}
2569
Scott Moreau1bad5db2012-08-18 01:04:05 -06002570static void
2571weston_output_compute_transform(struct weston_output *output)
2572{
2573 struct weston_matrix transform;
2574 int flip;
2575
2576 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002577 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002578
2579 switch(output->transform) {
2580 case WL_OUTPUT_TRANSFORM_FLIPPED:
2581 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2582 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2583 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002584 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002585 flip = -1;
2586 break;
2587 default:
2588 flip = 1;
2589 break;
2590 }
2591
2592 switch(output->transform) {
2593 case WL_OUTPUT_TRANSFORM_NORMAL:
2594 case WL_OUTPUT_TRANSFORM_FLIPPED:
2595 transform.d[0] = flip;
2596 transform.d[1] = 0;
2597 transform.d[4] = 0;
2598 transform.d[5] = 1;
2599 break;
2600 case WL_OUTPUT_TRANSFORM_90:
2601 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2602 transform.d[0] = 0;
2603 transform.d[1] = -flip;
2604 transform.d[4] = 1;
2605 transform.d[5] = 0;
2606 break;
2607 case WL_OUTPUT_TRANSFORM_180:
2608 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2609 transform.d[0] = -flip;
2610 transform.d[1] = 0;
2611 transform.d[4] = 0;
2612 transform.d[5] = -1;
2613 break;
2614 case WL_OUTPUT_TRANSFORM_270:
2615 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2616 transform.d[0] = 0;
2617 transform.d[1] = flip;
2618 transform.d[4] = -1;
2619 transform.d[5] = 0;
2620 break;
2621 default:
2622 break;
2623 }
2624
2625 weston_matrix_multiply(&output->matrix, &transform);
2626}
2627
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002628WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002629weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002630{
Scott Moreau850ca422012-05-21 15:21:25 -06002631 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002632 struct weston_matrix camera;
2633 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002634
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002635 weston_matrix_init(&output->matrix);
2636 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002637 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2638 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002639
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002640 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002641 2.0 / (output->width + output->border.left + output->border.right),
2642 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2643
2644 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002645
Scott Moreauccbf29d2012-02-22 14:21:41 -07002646 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002647 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002648 weston_matrix_init(&camera);
2649 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002650 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002651 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002652 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002653 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002654 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002655 weston_matrix_multiply(&output->matrix, &modelview);
2656 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002657
Scott Moreauccbf29d2012-02-22 14:21:41 -07002658 output->dirty = 0;
2659}
2660
Scott Moreau1bad5db2012-08-18 01:04:05 -06002661static void
Alexander Larsson0b135062013-05-28 16:23:36 +02002662weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002663{
2664 output->transform = transform;
2665
2666 switch (transform) {
2667 case WL_OUTPUT_TRANSFORM_90:
2668 case WL_OUTPUT_TRANSFORM_270:
2669 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2670 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2671 /* Swap width and height */
2672 output->width = output->current->height;
2673 output->height = output->current->width;
2674 break;
2675 case WL_OUTPUT_TRANSFORM_NORMAL:
2676 case WL_OUTPUT_TRANSFORM_180:
2677 case WL_OUTPUT_TRANSFORM_FLIPPED:
2678 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2679 output->width = output->current->width;
2680 output->height = output->current->height;
2681 break;
2682 default:
2683 break;
2684 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06002685
Alexander Larsson4ea95522013-05-22 14:41:37 +02002686 output->scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02002687 output->width /= scale;
2688 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002689}
2690
Scott Moreauccbf29d2012-02-22 14:21:41 -07002691WL_EXPORT void
2692weston_output_move(struct weston_output *output, int x, int y)
2693{
2694 output->x = x;
2695 output->y = y;
2696
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002697 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002698 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002699 output->width,
2700 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002701}
2702
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002703WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002704weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02002705 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002706 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002707{
2708 output->compositor = c;
2709 output->x = x;
2710 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002711 output->border.top = 0;
2712 output->border.bottom = 0;
2713 output->border.left = 0;
2714 output->border.right = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02002715 output->mm_width = mm_width;
2716 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002717 output->dirty = 1;
Alexander Larssone32c3762013-05-28 16:23:37 +02002718 output->origin_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002719
Alexander Larsson0b135062013-05-28 16:23:36 +02002720 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06002721 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002722
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002723 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002724 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002725
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002726 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01002727 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002728 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002729 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002730
Casey Dahlin58ba1372012-04-19 22:50:08 -04002731 output->id = ffs(~output->compositor->output_id_pool) - 1;
2732 output->compositor->output_id_pool |= 1 << output->id;
2733
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002734 output->global =
2735 wl_display_add_global(c->wl_display, &wl_output_interface,
2736 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01002737 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002738}
2739
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002740static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002741compositor_bind(struct wl_client *client,
2742 void *data, uint32_t version, uint32_t id)
2743{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002744 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002745
2746 wl_client_add_object(client, &wl_compositor_interface,
2747 &compositor_interface, id, compositor);
2748}
2749
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002750static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002751log_uname(void)
2752{
2753 struct utsname usys;
2754
2755 uname(&usys);
2756
2757 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2758 usys.version, usys.machine);
2759}
2760
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002761WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02002762weston_environment_get_fd(const char *env)
2763{
2764 char *e, *end;
2765 int fd, flags;
2766
2767 e = getenv(env);
2768 if (!e)
2769 return -1;
2770 fd = strtol(e, &end, 0);
2771 if (*end != '\0')
2772 return -1;
2773
2774 flags = fcntl(fd, F_GETFD);
2775 if (flags == -1)
2776 return -1;
2777
2778 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
2779 unsetenv(env);
2780
2781 return fd;
2782}
2783
2784WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002785weston_compositor_init(struct weston_compositor *ec,
2786 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002787 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002788 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002789{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002790 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002791 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002792 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002793
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002794 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002795 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002796 wl_signal_init(&ec->destroy_signal);
2797 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002798 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002799 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002800 wl_signal_init(&ec->idle_signal);
2801 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002802 wl_signal_init(&ec->show_input_panel_signal);
2803 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02002804 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002805 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01002806 wl_signal_init(&ec->output_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002807 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002808
Casey Dahlin58ba1372012-04-19 22:50:08 -04002809 ec->output_id_pool = 0;
2810
Kristian Høgsberga8873122011-11-23 10:39:34 -05002811 if (!wl_display_add_global(display, &wl_compositor_interface,
2812 ec, compositor_bind))
2813 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002814
Pekka Paalanene67858b2013-04-25 13:57:42 +03002815 if (!wl_display_add_global(display, &wl_subcompositor_interface,
2816 ec, bind_subcompositor))
2817 return -1;
2818
Daniel Stone725c2c32012-06-22 14:04:36 +01002819 wl_list_init(&ec->surface_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002820 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002821 wl_list_init(&ec->layer_list);
2822 wl_list_init(&ec->seat_list);
2823 wl_list_init(&ec->output_list);
2824 wl_list_init(&ec->key_binding_list);
2825 wl_list_init(&ec->button_binding_list);
2826 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002827 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002828
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002829 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002830 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002831
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002832 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
2833 weston_config_section_get_string(s, "keymap_rules",
2834 (char **) &xkb_names.rules, NULL);
2835 weston_config_section_get_string(s, "keymap_model",
2836 (char **) &xkb_names.model, NULL);
2837 weston_config_section_get_string(s, "keymap_layout",
2838 (char **) &xkb_names.layout, NULL);
2839 weston_config_section_get_string(s, "keymap_variant",
2840 (char **) &xkb_names.variant, NULL);
2841 weston_config_section_get_string(s, "keymap_options",
2842 (char **) &xkb_names.options, NULL);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002843 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
2844 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01002845
2846 ec->ping_handler = NULL;
2847
2848 screenshooter_create(ec);
2849 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002850 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002851
2852 wl_data_device_manager_init(ec->wl_display);
2853
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002854 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002855
Daniel Stone725c2c32012-06-22 14:04:36 +01002856 loop = wl_display_get_event_loop(ec->wl_display);
2857 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2858 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2859
2860 ec->input_loop = wl_event_loop_create();
2861
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002862 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2863 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2864
2865 weston_compositor_schedule_repaint(ec);
2866
Daniel Stone725c2c32012-06-22 14:04:36 +01002867 return 0;
2868}
2869
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002870WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002871weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002872{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002873 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002874
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002875 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002876 if (ec->input_loop_source)
2877 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002878
Matt Roper361d2ad2011-08-29 13:52:23 -07002879 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002880 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002881 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002882
Daniel Stone325fc2d2012-05-30 16:31:58 +01002883 weston_binding_list_destroy_all(&ec->key_binding_list);
2884 weston_binding_list_destroy_all(&ec->button_binding_list);
2885 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002886 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002887
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002888 weston_plane_release(&ec->primary_plane);
2889
Jonas Ådahlc97af922012-03-28 22:36:09 +02002890 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07002891
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002892 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07002893}
2894
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05002895WL_EXPORT void
2896weston_version(int *major, int *minor, int *micro)
2897{
2898 *major = WESTON_VERSION_MAJOR;
2899 *minor = WESTON_VERSION_MINOR;
2900 *micro = WESTON_VERSION_MICRO;
2901}
2902
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002903static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002904 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002905 const char *desc;
2906} capability_strings[] = {
2907 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002908 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002909};
2910
2911static void
2912weston_compositor_log_capabilities(struct weston_compositor *compositor)
2913{
2914 unsigned i;
2915 int yes;
2916
2917 weston_log("Compositor capabilities:\n");
2918 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
2919 yes = compositor->capabilities & capability_strings[i].bit;
2920 weston_log_continue(STAMP_SPACE "%s %s\n",
2921 capability_strings[i].desc,
2922 yes ? "yes" : "no");
2923 }
2924}
2925
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002926static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002927{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002928 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002929
Martin Minarik6d118362012-06-07 18:01:59 +02002930 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002931 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002932
2933 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002934}
2935
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002936#ifdef HAVE_LIBUNWIND
2937
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002938static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002939print_backtrace(void)
2940{
2941 unw_cursor_t cursor;
2942 unw_context_t context;
2943 unw_word_t off;
2944 unw_proc_info_t pip;
2945 int ret, i = 0;
2946 char procname[256];
2947 const char *filename;
2948 Dl_info dlinfo;
2949
2950 pip.unwind_info = NULL;
2951 ret = unw_getcontext(&context);
2952 if (ret) {
2953 weston_log("unw_getcontext: %d\n", ret);
2954 return;
2955 }
2956
2957 ret = unw_init_local(&cursor, &context);
2958 if (ret) {
2959 weston_log("unw_init_local: %d\n", ret);
2960 return;
2961 }
2962
2963 ret = unw_step(&cursor);
2964 while (ret > 0) {
2965 ret = unw_get_proc_info(&cursor, &pip);
2966 if (ret) {
2967 weston_log("unw_get_proc_info: %d\n", ret);
2968 break;
2969 }
2970
2971 ret = unw_get_proc_name(&cursor, procname, 256, &off);
2972 if (ret && ret != -UNW_ENOMEM) {
2973 if (ret != -UNW_EUNSPEC)
2974 weston_log("unw_get_proc_name: %d\n", ret);
2975 procname[0] = '?';
2976 procname[1] = 0;
2977 }
2978
2979 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
2980 *dlinfo.dli_fname)
2981 filename = dlinfo.dli_fname;
2982 else
2983 filename = "?";
2984
2985 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
2986 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
2987
2988 ret = unw_step(&cursor);
2989 if (ret < 0)
2990 weston_log("unw_step: %d\n", ret);
2991 }
2992}
2993
2994#else
2995
2996static void
2997print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002998{
2999 void *buffer[32];
3000 int i, count;
3001 Dl_info info;
3002
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003003 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3004 for (i = 0; i < count; i++) {
3005 dladdr(buffer[i], &info);
3006 weston_log(" [%016lx] %s (%s)\n",
3007 (long) buffer[i],
3008 info.dli_sname ? info.dli_sname : "--",
3009 info.dli_fname);
3010 }
3011}
3012
3013#endif
3014
3015static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003016on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003017{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003018 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003019 * then call the backend restore function, which will switch
3020 * back to the vt we launched from or ungrab X etc and then
3021 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003022 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003023 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003024 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003025
Peter Maatmane5b42e42013-03-27 22:38:53 +01003026 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003027
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003028 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003029
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003030 segv_compositor->restore(segv_compositor);
3031
3032 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003033}
3034
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003035static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003036load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003037{
3038 char path[PATH_MAX];
3039 void *module, *init;
3040
3041 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003042 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003043 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003044 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003045
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003046 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3047 if (module) {
3048 weston_log("Module '%s' already loaded\n", path);
3049 dlclose(module);
3050 return NULL;
3051 }
3052
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003053 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003054 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003055 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003056 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003057 return NULL;
3058 }
3059
3060 init = dlsym(module, entrypoint);
3061 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003062 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003063 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003064 return NULL;
3065 }
3066
3067 return init;
3068}
3069
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003070static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003071load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003072 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003073{
3074 const char *p, *end;
3075 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003076 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003077 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003078
3079 if (modules == NULL)
3080 return 0;
3081
3082 p = modules;
3083 while (*p) {
3084 end = strchrnul(p, ',');
3085 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3086 module_init = load_module(buffer, "module_init");
3087 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003088 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003089 p = end;
3090 while (*p == ',')
3091 p++;
3092
3093 }
3094
3095 return 0;
3096}
3097
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003098static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003099 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3100
3101static const char xdg_wrong_message[] =
3102 "fatal: environment variable XDG_RUNTIME_DIR\n"
3103 "is set to \"%s\", which is not a directory.\n";
3104
3105static const char xdg_wrong_mode_message[] =
3106 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3107 "correctly. Unix access mode must be 0700 but is %o,\n"
3108 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003109 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003110
3111static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003112 "Refer to your distribution on how to get it, or\n"
3113 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3114 "on how to implement it.\n";
3115
Martin Minarik37032f82012-06-18 20:15:18 +02003116static void
3117verify_xdg_runtime_dir(void)
3118{
3119 char *dir = getenv("XDG_RUNTIME_DIR");
3120 struct stat s;
3121
3122 if (!dir) {
3123 weston_log(xdg_error_message);
3124 weston_log_continue(xdg_detail_message);
3125 exit(EXIT_FAILURE);
3126 }
3127
3128 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3129 weston_log(xdg_wrong_message, dir);
3130 weston_log_continue(xdg_detail_message);
3131 exit(EXIT_FAILURE);
3132 }
3133
3134 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3135 weston_log(xdg_wrong_mode_message,
3136 dir, s.st_mode & 0777, s.st_uid);
3137 weston_log_continue(xdg_detail_message);
3138 }
3139}
3140
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003141static int
3142usage(int error_code)
3143{
3144 fprintf(stderr,
3145 "Usage: weston [OPTIONS]\n\n"
3146 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3147 "Weston supports multiple backends, and depending on which backend is in use\n"
3148 "different options will be accepted.\n\n"
3149
3150
3151 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003152 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003153 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003154 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3155 "\t\t\t\twayland-backend.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003156 " -S, --socket=NAME\tName of socket to listen on\n"
3157 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003158 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003159 " --log==FILE\t\tLog to the given file\n"
3160 " -h, --help\t\tThis help message\n\n");
3161
3162 fprintf(stderr,
3163 "Options for drm-backend.so:\n\n"
3164 " --connector=ID\tBring up only this connector\n"
3165 " --seat=SEAT\t\tThe seat that weston should run on\n"
3166 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003167 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003168 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3169
3170 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003171 "Options for fbdev-backend.so:\n\n"
3172 " --tty=TTY\t\tThe tty to use\n"
3173 " --device=DEVICE\tThe framebuffer device to use\n\n");
3174
3175 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003176 "Options for x11-backend.so:\n\n"
3177 " --width=WIDTH\t\tWidth of X window\n"
3178 " --height=HEIGHT\tHeight of X window\n"
3179 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003180 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003181 " --output-count=COUNT\tCreate multiple outputs\n"
3182 " --no-input\t\tDont create input devices\n\n");
3183
3184 fprintf(stderr,
3185 "Options for wayland-backend.so:\n\n"
3186 " --width=WIDTH\t\tWidth of Wayland surface\n"
3187 " --height=HEIGHT\tHeight of Wayland surface\n"
3188 " --display=DISPLAY\tWayland display to connect to\n\n");
3189
Pekka Paalanene31e0532013-05-22 18:03:07 +03003190#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3191 fprintf(stderr,
3192 "Options for rpi-backend.so:\n\n"
3193 " --tty=TTY\t\tThe tty to use\n"
3194 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3195 " --transform=TR\tThe output transformation, TR is one of:\n"
3196 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3197 "\n");
3198#endif
3199
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003200 exit(error_code);
3201}
3202
Peter Maatmane5b42e42013-03-27 22:38:53 +01003203static void
3204catch_signals(void)
3205{
3206 struct sigaction action;
3207
3208 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3209 action.sa_sigaction = on_caught_signal;
3210 sigemptyset(&action.sa_mask);
3211 sigaction(SIGSEGV, &action, NULL);
3212 sigaction(SIGABRT, &action, NULL);
3213}
3214
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003215int main(int argc, char *argv[])
3216{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003217 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003218 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003219 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003220 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003221 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003222 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003223 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003224 int *argc, char *argv[],
3225 struct weston_config *config);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003226 int i, config_fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003227 char *backend = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003228 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003229 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003230 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003231 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003232 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003233 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003234 struct weston_config *config;
3235 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003236
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003237 const struct weston_option core_options[] = {
3238 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3239 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3240 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003241 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003242 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003243 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003244 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003245 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003246
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003247 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003248
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003249 if (help)
3250 usage(EXIT_SUCCESS);
3251
Scott Moreau12245142012-08-29 15:15:58 -06003252 if (version) {
3253 printf(PACKAGE_STRING "\n");
3254 return EXIT_SUCCESS;
3255 }
3256
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003257 weston_log_file_open(log);
3258
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003259 weston_log("%s\n"
3260 STAMP_SPACE "%s\n"
3261 STAMP_SPACE "Bug reports to: %s\n"
3262 STAMP_SPACE "Build: %s\n",
3263 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003264 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003265 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003266
Martin Minarik37032f82012-06-18 20:15:18 +02003267 verify_xdg_runtime_dir();
3268
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003269 display = wl_display_create();
3270
Tiago Vignatti2116b892011-08-08 05:52:59 -07003271 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003272 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3273 display);
3274 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3275 display);
3276 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3277 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003278
3279 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003280 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3281 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003282
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003283 if (!backend) {
3284 if (getenv("WAYLAND_DISPLAY"))
3285 backend = "wayland-backend.so";
3286 else if (getenv("DISPLAY"))
3287 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003288 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003289 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003290 }
3291
Ossama Othmana50e6e42013-05-14 09:48:26 -07003292 config_fd = open_config_file("weston.ini");
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003293 config = weston_config_parse(config_fd);
3294 close(config_fd);
3295
3296 section = weston_config_get_section(config, "core", NULL, NULL);
3297 weston_config_section_get_string(section, "modules",
3298 &modules, "desktop-shell.so");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003299
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003300 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003301 if (!backend_init)
3302 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003303
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003304 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003305 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003306 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003307 exit(EXIT_FAILURE);
3308 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003309
Peter Maatmane5b42e42013-03-27 22:38:53 +01003310 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003311 segv_compositor = ec;
3312
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003313 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003314
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003315 setenv("WAYLAND_DISPLAY", socket_name, 1);
3316
Ossama Othmana50e6e42013-05-14 09:48:26 -07003317 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003318 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003319 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003320 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003321
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003322 for (i = 1; i < argc; i++)
3323 weston_log("fatal: unhandled option: %s\n", argv[i]);
3324 if (argc > 1) {
3325 ret = EXIT_FAILURE;
3326 goto out;
3327 }
3328
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003329 weston_compositor_log_capabilities(ec);
3330
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003331 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003332 weston_log("fatal: failed to add socket: %m\n");
3333 ret = EXIT_FAILURE;
3334 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003335 }
3336
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003337 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003338
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003339 wl_display_run(display);
3340
3341 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003342 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003343 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003344
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003345 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003346
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003347 for (i = ARRAY_LENGTH(signals); i;)
3348 wl_event_source_remove(signals[--i]);
3349
Daniel Stone855028f2012-05-01 20:37:10 +01003350 weston_compositor_xkb_destroy(ec);
3351
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003352 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003353 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003354
Martin Minarik19e6f262012-06-07 13:08:46 +02003355 weston_log_file_close();
3356
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003357 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003358}