blob: 2cadb45d0485ecccbf3d615d9acdd273f4d31430 [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
Rob Bradford27b17932013-06-26 18:08:46 +010096static void
97weston_compositor_build_surface_list(struct weston_compositor *compositor);
98
Alex Wu2dda6042012-04-17 17:20:47 +080099WL_EXPORT int
Alexander Larsson355748e2013-05-28 16:23:38 +0200100weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode, int32_t scale)
Alex Wu2dda6042012-04-17 17:20:47 +0800101{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200102 struct weston_seat *seat;
103 pixman_region32_t old_output_region;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200104 int ret;
105
Alex Wu2dda6042012-04-17 17:20:47 +0800106 if (!output->switch_mode)
107 return -1;
108
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200109 ret = output->switch_mode(output, mode);
110 if (ret < 0)
111 return ret;
112
Alexander Larsson355748e2013-05-28 16:23:38 +0200113 output->scale = scale;
114
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200115 pixman_region32_init(&old_output_region);
116 pixman_region32_copy(&old_output_region, &output->region);
117
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200118 /* Update output region and transformation matrix */
Alexander Larsson0b135062013-05-28 16:23:36 +0200119 weston_output_transform_scale_init(output, output->transform, output->scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200120
121 pixman_region32_init(&output->previous_damage);
122 pixman_region32_init_rect(&output->region, output->x, output->y,
123 output->width, output->height);
124
125 weston_output_update_matrix(output);
126
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200127 /* If a pointer falls outside the outputs new geometry, move it to its
128 * lower-right corner */
129 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400130 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200131 int32_t x, y;
132
133 if (!pointer)
134 continue;
135
136 x = wl_fixed_to_int(pointer->x);
137 y = wl_fixed_to_int(pointer->y);
138
139 if (!pixman_region32_contains_point(&old_output_region,
140 x, y, NULL) ||
141 pixman_region32_contains_point(&output->region,
142 x, y, NULL))
143 continue;
144
145 if (x >= output->x + output->width)
146 x = output->x + output->width - 1;
147 if (y >= output->y + output->height)
148 y = output->y + output->height - 1;
149
150 pointer->x = wl_fixed_from_int(x);
151 pointer->y = wl_fixed_from_int(y);
152 }
153
154 pixman_region32_fini(&old_output_region);
155
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200156 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800157}
158
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400159WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500160weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400161{
162 wl_list_insert(&child_process_list, &process->link);
163}
164
Benjamin Franzke06286262011-05-06 19:12:33 +0200165static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200166child_client_exec(int sockfd, const char *path)
167{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500168 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200169 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200170 sigset_t allsigs;
171
172 /* do not give our signal mask to the new process */
173 sigfillset(&allsigs);
174 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200175
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500176 /* Launch clients as the user. */
177 seteuid(getuid());
178
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500179 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
180 * non-CLOEXEC fd to pass through exec. */
181 clientfd = dup(sockfd);
182 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200183 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500184 return;
185 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200186
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500187 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200188 setenv("WAYLAND_SOCKET", s, 1);
189
190 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200191 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200192 path);
193}
194
195WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500196weston_client_launch(struct weston_compositor *compositor,
197 struct weston_process *proc,
198 const char *path,
199 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200200{
201 int sv[2];
202 pid_t pid;
203 struct wl_client *client;
204
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300205 weston_log("launching '%s'\n", path);
206
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300207 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200208 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200209 "socketpair failed while launching '%s': %m\n",
210 path);
211 return NULL;
212 }
213
214 pid = fork();
215 if (pid == -1) {
216 close(sv[0]);
217 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200218 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200219 "fork failed while launching '%s': %m\n", path);
220 return NULL;
221 }
222
223 if (pid == 0) {
224 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700225 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200226 }
227
228 close(sv[1]);
229
230 client = wl_client_create(compositor->wl_display, sv[0]);
231 if (!client) {
232 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200233 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200234 "wl_client_create failed while launching '%s'.\n",
235 path);
236 return NULL;
237 }
238
239 proc->pid = pid;
240 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500241 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200242
243 return client;
244}
245
246static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300247surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
248{
249 struct weston_surface *surface =
250 container_of(listener, struct weston_surface,
251 pending.buffer_destroy_listener);
252
253 surface->pending.buffer = NULL;
254}
255
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200256static void
257empty_region(pixman_region32_t *region)
258{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300259 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200260 pixman_region32_init(region);
261}
262
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300263static void
264region_init_infinite(pixman_region32_t *region)
265{
266 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
267 UINT32_MAX, UINT32_MAX);
268}
269
Pekka Paalanene67858b2013-04-25 13:57:42 +0300270static struct weston_subsurface *
271weston_surface_to_subsurface(struct weston_surface *surface);
272
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500273WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500274weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500275{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500276 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400277
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200278 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400279 if (surface == NULL)
280 return NULL;
281
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500282 wl_signal_init(&surface->destroy_signal);
283
284 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500285
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500286 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500287 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500288
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500289 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400290 surface->alpha = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400291
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100292 if (compositor->renderer->create_surface(surface) < 0) {
293 free(surface);
294 return NULL;
295 }
296
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200297 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200298 surface->buffer_scale = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200299 surface->pending.buffer_transform = surface->buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200300 surface->pending.buffer_scale = surface->buffer_scale;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400301 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400302 surface->plane = &compositor->primary_plane;
Giulio Camuffo184df502013-02-21 11:29:21 +0100303 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200304
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400305 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500306 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500307 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300308 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200309 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500310 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400311
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200312 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200313 wl_list_insert(&surface->geometry.transformation_list,
314 &surface->transform.position.link);
315 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200316 wl_list_init(&surface->geometry.child_list);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200317 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200318 surface->transform.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500319
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300320 surface->pending.buffer_destroy_listener.notify =
321 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300322 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300323 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300324 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300325 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300326
Pekka Paalanene67858b2013-04-25 13:57:42 +0300327 wl_list_init(&surface->subsurface_list);
328 wl_list_init(&surface->subsurface_list_pending);
329
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400330 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500331}
332
Alex Wu8811bf92012-02-28 18:07:54 +0800333WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500334weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200335 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500336{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100337 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500338}
339
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400340WL_EXPORT void
341weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200342 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200343{
344 if (surface->transform.enabled) {
345 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
346
347 weston_matrix_transform(&surface->transform.matrix, &v);
348
349 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200350 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700351 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200352 v.f[3]);
353 *x = 0;
354 *y = 0;
355 return;
356 }
357
358 *x = v.f[0] / v.f[3];
359 *y = v.f[1] / v.f[3];
360 } else {
361 *x = sx + surface->geometry.x;
362 *y = sy + surface->geometry.y;
363 }
364}
365
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500366WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200367weston_transformed_coord(int width, int height,
368 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200369 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200370 float sx, float sy, float *bx, float *by)
371{
372 switch (transform) {
373 case WL_OUTPUT_TRANSFORM_NORMAL:
374 default:
375 *bx = sx;
376 *by = sy;
377 break;
378 case WL_OUTPUT_TRANSFORM_FLIPPED:
379 *bx = width - sx;
380 *by = sy;
381 break;
382 case WL_OUTPUT_TRANSFORM_90:
383 *bx = height - sy;
384 *by = sx;
385 break;
386 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
387 *bx = height - sy;
388 *by = width - sx;
389 break;
390 case WL_OUTPUT_TRANSFORM_180:
391 *bx = width - sx;
392 *by = height - sy;
393 break;
394 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
395 *bx = sx;
396 *by = height - sy;
397 break;
398 case WL_OUTPUT_TRANSFORM_270:
399 *bx = sy;
400 *by = width - sx;
401 break;
402 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
403 *bx = sy;
404 *by = sx;
405 break;
406 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200407
408 *bx *= scale;
409 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200410}
411
412WL_EXPORT pixman_box32_t
413weston_transformed_rect(int width, int height,
414 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200415 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200416 pixman_box32_t rect)
417{
418 float x1, x2, y1, y2;
419
420 pixman_box32_t ret;
421
Alexander Larsson4ea95522013-05-22 14:41:37 +0200422 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200423 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200424 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200425 rect.x2, rect.y2, &x2, &y2);
426
427 if (x1 <= x2) {
428 ret.x1 = x1;
429 ret.x2 = x2;
430 } else {
431 ret.x1 = x2;
432 ret.x2 = x1;
433 }
434
435 if (y1 <= y2) {
436 ret.y1 = y1;
437 ret.y2 = y2;
438 } else {
439 ret.y1 = y2;
440 ret.y2 = y1;
441 }
442
443 return ret;
444}
445
446WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200447weston_surface_to_buffer_float(struct weston_surface *surface,
448 float sx, float sy, float *bx, float *by)
449{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200450 weston_transformed_coord(surface->geometry.width,
451 surface->geometry.height,
452 surface->buffer_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200453 surface->buffer_scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200454 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200455}
456
Alexander Larsson4ea95522013-05-22 14:41:37 +0200457WL_EXPORT void
458weston_surface_to_buffer(struct weston_surface *surface,
459 int sx, int sy, int *bx, int *by)
460{
461 float bxf, byf;
462
463 weston_transformed_coord(surface->geometry.width,
464 surface->geometry.height,
465 surface->buffer_transform,
466 surface->buffer_scale,
467 sx, sy, &bxf, &byf);
468 *bx = floorf(bxf);
469 *by = floorf(byf);
470}
471
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200472WL_EXPORT pixman_box32_t
473weston_surface_to_buffer_rect(struct weston_surface *surface,
474 pixman_box32_t rect)
475{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200476 return weston_transformed_rect(surface->geometry.width,
477 surface->geometry.height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200478 surface->buffer_transform,
479 surface->buffer_scale,
480 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200481}
482
483WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400484weston_surface_move_to_plane(struct weston_surface *surface,
485 struct weston_plane *plane)
486{
487 if (surface->plane == plane)
488 return;
489
490 weston_surface_damage_below(surface);
491 surface->plane = plane;
492 weston_surface_damage(surface);
493}
494
495WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500496weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200497{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500498 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200499
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500500 pixman_region32_init(&damage);
501 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
502 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400503 pixman_region32_union(&surface->plane->damage,
504 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500505 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200506}
507
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400508static void
509weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
510{
511 uint32_t different = es->output_mask ^ mask;
512 uint32_t entered = mask & different;
513 uint32_t left = es->output_mask & different;
514 struct weston_output *output;
515 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500516 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400517
518 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500519 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400520 return;
521 if (different == 0)
522 return;
523
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500524 client = wl_resource_get_client(es->resource);
525
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400526 wl_list_for_each(output, &es->compositor->output_list, link) {
527 if (1 << output->id & different)
528 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500529 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400530 client);
531 if (resource == NULL)
532 continue;
533 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500534 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400535 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500536 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400537 }
538}
539
540static void
541weston_surface_assign_output(struct weston_surface *es)
542{
543 struct weston_compositor *ec = es->compositor;
544 struct weston_output *output, *new_output;
545 pixman_region32_t region;
546 uint32_t max, area, mask;
547 pixman_box32_t *e;
548
549 new_output = NULL;
550 max = 0;
551 mask = 0;
552 pixman_region32_init(&region);
553 wl_list_for_each(output, &ec->output_list, link) {
554 pixman_region32_intersect(&region, &es->transform.boundingbox,
555 &output->region);
556
557 e = pixman_region32_extents(&region);
558 area = (e->x2 - e->x1) * (e->y2 - e->y1);
559
560 if (area > 0)
561 mask |= 1 << output->id;
562
563 if (area >= max) {
564 new_output = output;
565 max = area;
566 }
567 }
568 pixman_region32_fini(&region);
569
570 es->output = new_output;
571 weston_surface_update_output_mask(es, mask);
572}
573
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200574static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200575surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
576 int32_t width, int32_t height,
577 pixman_region32_t *bbox)
578{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200579 float min_x = HUGE_VALF, min_y = HUGE_VALF;
580 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200581 int32_t s[4][2] = {
582 { sx, sy },
583 { sx, sy + height },
584 { sx + width, sy },
585 { sx + width, sy + height }
586 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200587 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200588 int i;
589
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300590 if (width == 0 || height == 0) {
591 /* avoid rounding empty bbox to 1x1 */
592 pixman_region32_init(bbox);
593 return;
594 }
595
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200596 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200597 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400598 weston_surface_to_global_float(surface,
599 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200600 if (x < min_x)
601 min_x = x;
602 if (x > max_x)
603 max_x = x;
604 if (y < min_y)
605 min_y = y;
606 if (y > max_y)
607 max_y = y;
608 }
609
Pekka Paalanen219b9822012-02-08 15:38:37 +0200610 int_x = floorf(min_x);
611 int_y = floorf(min_y);
612 pixman_region32_init_rect(bbox, int_x, int_y,
613 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200614}
615
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200616static void
617weston_surface_update_transform_disable(struct weston_surface *surface)
618{
619 surface->transform.enabled = 0;
620
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200621 /* round off fractions when not transformed */
622 surface->geometry.x = roundf(surface->geometry.x);
623 surface->geometry.y = roundf(surface->geometry.y);
624
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500625 /* Otherwise identity matrix, but with x and y translation. */
626 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
627 surface->transform.position.matrix.d[12] = surface->geometry.x;
628 surface->transform.position.matrix.d[13] = surface->geometry.y;
629
630 surface->transform.matrix = surface->transform.position.matrix;
631
Kristian Høgsberg9bcaaeb2013-02-28 14:56:43 -0500632 surface->transform.inverse = surface->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500633 surface->transform.inverse.d[12] = -surface->geometry.x;
634 surface->transform.inverse.d[13] = -surface->geometry.y;
635
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200636 pixman_region32_init_rect(&surface->transform.boundingbox,
637 surface->geometry.x,
638 surface->geometry.y,
639 surface->geometry.width,
640 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500641
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400642 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500643 pixman_region32_copy(&surface->transform.opaque,
644 &surface->opaque);
645 pixman_region32_translate(&surface->transform.opaque,
646 surface->geometry.x,
647 surface->geometry.y);
648 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200649}
650
651static int
652weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200653{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200654 struct weston_surface *parent = surface->geometry.parent;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200655 struct weston_matrix *matrix = &surface->transform.matrix;
656 struct weston_matrix *inverse = &surface->transform.inverse;
657 struct weston_transform *tform;
658
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200659 surface->transform.enabled = 1;
660
661 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300662 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200663 surface->transform.position.matrix.d[12] = surface->geometry.x;
664 surface->transform.position.matrix.d[13] = surface->geometry.y;
665
666 weston_matrix_init(matrix);
667 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
668 weston_matrix_multiply(matrix, &tform->matrix);
669
Pekka Paalanen483243f2013-03-08 14:56:50 +0200670 if (parent)
671 weston_matrix_multiply(matrix, &parent->transform.matrix);
672
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200673 if (weston_matrix_invert(inverse, matrix) < 0) {
674 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200675 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200676 " transformation not invertible.\n", surface);
677 return -1;
678 }
679
680 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
681 surface->geometry.height,
682 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500683
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200684 return 0;
685}
686
687WL_EXPORT void
688weston_surface_update_transform(struct weston_surface *surface)
689{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200690 struct weston_surface *parent = surface->geometry.parent;
691
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200692 if (!surface->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200693 return;
694
Pekka Paalanen483243f2013-03-08 14:56:50 +0200695 if (parent)
696 weston_surface_update_transform(parent);
697
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200698 surface->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200699
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500700 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200701
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200702 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500703 pixman_region32_fini(&surface->transform.opaque);
704 pixman_region32_init(&surface->transform.opaque);
705
Pekka Paalanencd403622012-01-25 13:37:39 +0200706 /* transform.position is always in transformation_list */
707 if (surface->geometry.transformation_list.next ==
708 &surface->transform.position.link &&
709 surface->geometry.transformation_list.prev ==
Pekka Paalanen483243f2013-03-08 14:56:50 +0200710 &surface->transform.position.link &&
711 !parent) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200712 weston_surface_update_transform_disable(surface);
713 } else {
714 if (weston_surface_update_transform_enable(surface) < 0)
715 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200716 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200717
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400718 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200719
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300720 weston_surface_assign_output(surface);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300721
722 wl_signal_emit(&surface->compositor->transform_signal, surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200723}
724
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200725WL_EXPORT void
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200726weston_surface_geometry_dirty(struct weston_surface *surface)
727{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200728 struct weston_surface *child;
729
730 /*
731 * The invariant: if surface->geometry.dirty, then all surfaces
732 * in surface->geometry.child_list have geometry.dirty too.
733 * Corollary: if not parent->geometry.dirty, then all ancestors
734 * are not dirty.
735 */
736
737 if (surface->transform.dirty)
738 return;
739
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200740 surface->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200741
742 wl_list_for_each(child, &surface->geometry.child_list,
743 geometry.parent_link)
744 weston_surface_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200745}
746
747WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100748weston_surface_to_global_fixed(struct weston_surface *surface,
749 wl_fixed_t sx, wl_fixed_t sy,
750 wl_fixed_t *x, wl_fixed_t *y)
751{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200752 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100753
754 weston_surface_to_global_float(surface,
755 wl_fixed_to_double(sx),
756 wl_fixed_to_double(sy),
757 &xf, &yf);
758 *x = wl_fixed_from_double(xf);
759 *y = wl_fixed_from_double(yf);
760}
761
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400762WL_EXPORT void
763weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200764 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200765{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200766 if (surface->transform.enabled) {
767 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
768
769 weston_matrix_transform(&surface->transform.inverse, &v);
770
771 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200772 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200773 "weston_surface_from_global(), divisor = %g\n",
774 v.f[3]);
775 *sx = 0;
776 *sy = 0;
777 return;
778 }
779
Pekka Paalanencd403622012-01-25 13:37:39 +0200780 *sx = v.f[0] / v.f[3];
781 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200782 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200783 *sx = x - surface->geometry.x;
784 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200785 }
786}
787
788WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100789weston_surface_from_global_fixed(struct weston_surface *surface,
790 wl_fixed_t x, wl_fixed_t y,
791 wl_fixed_t *sx, wl_fixed_t *sy)
792{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200793 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100794
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400795 weston_surface_from_global_float(surface,
796 wl_fixed_to_double(x),
797 wl_fixed_to_double(y),
798 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100799 *sx = wl_fixed_from_double(sxf);
800 *sy = wl_fixed_from_double(syf);
801}
802
803WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200804weston_surface_from_global(struct weston_surface *surface,
805 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
806{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200807 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200808
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400809 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200810 *sx = floorf(sxf);
811 *sy = floorf(syf);
812}
813
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400814WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400815weston_surface_schedule_repaint(struct weston_surface *surface)
816{
817 struct weston_output *output;
818
819 wl_list_for_each(output, &surface->compositor->output_list, link)
820 if (surface->output_mask & (1 << output->id))
821 weston_output_schedule_repaint(output);
822}
823
824WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500825weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500826{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400827 pixman_region32_union_rect(&surface->damage, &surface->damage,
828 0, 0, surface->geometry.width,
829 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200830
Kristian Høgsberg98238702012-08-03 16:29:12 -0400831 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500832}
833
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400834WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500835weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200836 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400837{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200838 surface->geometry.x = x;
839 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200840 surface->geometry.width = width;
841 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200842 weston_surface_geometry_dirty(surface);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400843}
844
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200845WL_EXPORT void
846weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200847 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200848{
849 surface->geometry.x = x;
850 surface->geometry.y = y;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200851 weston_surface_geometry_dirty(surface);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200852}
853
Pekka Paalanen483243f2013-03-08 14:56:50 +0200854static void
855transform_parent_handle_parent_destroy(struct wl_listener *listener,
856 void *data)
857{
858 struct weston_surface *surface =
859 container_of(listener, struct weston_surface,
860 geometry.parent_destroy_listener);
861
862 weston_surface_set_transform_parent(surface, NULL);
863}
864
865WL_EXPORT void
866weston_surface_set_transform_parent(struct weston_surface *surface,
867 struct weston_surface *parent)
868{
869 if (surface->geometry.parent) {
870 wl_list_remove(&surface->geometry.parent_destroy_listener.link);
871 wl_list_remove(&surface->geometry.parent_link);
872 }
873
874 surface->geometry.parent = parent;
875
876 surface->geometry.parent_destroy_listener.notify =
877 transform_parent_handle_parent_destroy;
878 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500879 wl_signal_add(&parent->destroy_signal,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200880 &surface->geometry.parent_destroy_listener);
881 wl_list_insert(&parent->geometry.child_list,
882 &surface->geometry.parent_link);
883 }
884
885 weston_surface_geometry_dirty(surface);
886}
887
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300888WL_EXPORT int
889weston_surface_is_mapped(struct weston_surface *surface)
890{
891 if (surface->output)
892 return 1;
893 else
894 return 0;
895}
896
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200897WL_EXPORT int32_t
898weston_surface_buffer_width(struct weston_surface *surface)
899{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200900 int32_t width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200901 switch (surface->buffer_transform) {
902 case WL_OUTPUT_TRANSFORM_90:
903 case WL_OUTPUT_TRANSFORM_270:
904 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
905 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200906 width = surface->buffer_ref.buffer->height;
907 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200908 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200909 width = surface->buffer_ref.buffer->width;
910 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200911 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200912 return width / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200913}
914
915WL_EXPORT int32_t
916weston_surface_buffer_height(struct weston_surface *surface)
917{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200918 int32_t height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200919 switch (surface->buffer_transform) {
920 case WL_OUTPUT_TRANSFORM_90:
921 case WL_OUTPUT_TRANSFORM_270:
922 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
923 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200924 height = surface->buffer_ref.buffer->width;
925 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200926 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200927 height = surface->buffer_ref.buffer->height;
928 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200929 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200930 return height / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200931}
932
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400933WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500934weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500935{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400936 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500937
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400938 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500939
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400940 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500941}
942
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400943WL_EXPORT struct weston_surface *
Tiago Vignatti9d393522012-02-10 16:26:19 +0200944weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100945 wl_fixed_t x, wl_fixed_t y,
946 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200947{
948 struct weston_surface *surface;
949
950 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100951 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500952 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100953 wl_fixed_to_int(*sx),
954 wl_fixed_to_int(*sy),
955 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200956 return surface;
957 }
958
959 return NULL;
960}
961
962static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500963weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400964{
Daniel Stone37816df2012-05-16 18:45:18 +0100965 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400966
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500967 if (!compositor->focus)
968 return;
969
Daniel Stone37816df2012-05-16 18:45:18 +0100970 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400971 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400972}
973
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400974WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500975weston_surface_unmap(struct weston_surface *surface)
976{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100977 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500978
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500979 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500980 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500981 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500982
Daniel Stone4dab5db2012-05-30 16:31:53 +0100983 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400984 if (seat->keyboard && seat->keyboard->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400985 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400986 if (seat->pointer && seat->pointer->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400987 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400988 NULL,
989 wl_fixed_from_int(0),
990 wl_fixed_from_int(0));
Daniel Stone4dab5db2012-05-30 16:31:53 +0100991 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500992
Kristian Høgsberg98238702012-08-03 16:29:12 -0400993 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500994}
995
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400996struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -0500997 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400998 struct wl_list link;
999};
1000
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001001
1002WL_EXPORT void
1003weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001004{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001005 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
Rob Bradford27b17932013-06-26 18:08:46 +01001014 if (weston_surface_is_mapped(surface)) {
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001015 weston_surface_unmap(surface);
Rob Bradford27b17932013-06-26 18:08:46 +01001016 weston_compositor_build_surface_list(compositor);
1017 }
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001018
Pekka Paalanenbc106382012-10-10 12:49:31 +03001019 wl_list_for_each_safe(cb, next,
1020 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001021 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001022
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001023 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001024 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001025 pixman_region32_fini(&surface->pending.damage);
1026
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001027 if (surface->pending.buffer)
1028 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1029
Pekka Paalanende685b82012-12-04 15:58:12 +02001030 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001031
Kristian Høgsberg42263852012-09-06 21:59:29 -04001032 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001033
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001034 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001035 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001036 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001037 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001038 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001039
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001040 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001041 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001042
Pekka Paalanen483243f2013-03-08 14:56:50 +02001043 weston_surface_set_transform_parent(surface, NULL);
1044
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001045 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001046}
1047
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001048static void
1049destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001050{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001051 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001052
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001053 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001054}
1055
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001056static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001057weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1058{
1059 struct weston_buffer *buffer =
1060 container_of(listener, struct weston_buffer, destroy_listener);
1061
1062 wl_signal_emit(&buffer->destroy_signal, buffer);
1063 free(buffer);
1064}
1065
1066struct weston_buffer *
1067weston_buffer_from_resource(struct wl_resource *resource)
1068{
1069 struct weston_buffer *buffer;
1070 struct wl_listener *listener;
1071
1072 listener = wl_resource_get_destroy_listener(resource,
1073 weston_buffer_destroy_handler);
1074
1075 if (listener) {
1076 buffer = container_of(listener, struct weston_buffer,
1077 destroy_listener);
1078 } else {
1079 buffer = malloc(sizeof *buffer);
1080 memset(buffer, 0, sizeof *buffer);
1081
1082 buffer->resource = resource;
1083 wl_signal_init(&buffer->destroy_signal);
1084 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
1085 wl_resource_add_destroy_listener(resource,
1086 &buffer->destroy_listener);
1087 }
1088
1089 return buffer;
1090}
1091
1092static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001093weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1094 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001095{
Pekka Paalanende685b82012-12-04 15:58:12 +02001096 struct weston_buffer_reference *ref =
1097 container_of(listener, struct weston_buffer_reference,
1098 destroy_listener);
1099
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001100 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001101 ref->buffer = NULL;
1102}
1103
1104WL_EXPORT void
1105weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001106 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001107{
1108 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001109 ref->buffer->busy_count--;
1110 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001111 assert(wl_resource_get_client(ref->buffer->resource));
1112 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001113 WL_BUFFER_RELEASE);
1114 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001115 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001116 }
1117
Pekka Paalanende685b82012-12-04 15:58:12 +02001118 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001119 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001120 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001121 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001122 }
1123
Pekka Paalanende685b82012-12-04 15:58:12 +02001124 ref->buffer = buffer;
1125 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1126}
1127
1128static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001129weston_surface_attach(struct weston_surface *surface,
1130 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001131{
1132 weston_buffer_reference(&surface->buffer_ref, buffer);
1133
Pekka Paalanena6421c42012-12-04 15:58:10 +02001134 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001135 if (weston_surface_is_mapped(surface))
1136 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001137 }
1138
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001139 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001140}
1141
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001142WL_EXPORT void
1143weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001144{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001145 wl_list_remove(&surface->layer_link);
1146 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001147 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001148 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001149}
1150
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001151WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001152weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001153{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001154 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001155
1156 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001157 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001158}
1159
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001160WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001161weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001162{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001163 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001164
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001165 pixman_region32_union(&compositor->primary_plane.damage,
1166 &compositor->primary_plane.damage,
1167 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001168 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001169}
1170
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001171static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001172surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001173 pixman_region32_t *opaque)
1174{
Pekka Paalanende685b82012-12-04 15:58:12 +02001175 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001176 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001177 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001178
1179 if (surface->transform.enabled) {
1180 pixman_box32_t *extents;
1181
1182 extents = pixman_region32_extents(&surface->damage);
1183 surface_compute_bbox(surface, extents->x1, extents->y1,
1184 extents->x2 - extents->x1,
1185 extents->y2 - extents->y1,
1186 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001187 pixman_region32_translate(&surface->damage,
1188 -surface->plane->x,
1189 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001190 } else {
1191 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001192 surface->geometry.x - surface->plane->x,
1193 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001194 }
1195
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001196 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001197 pixman_region32_union(&surface->plane->damage,
1198 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001199 empty_region(&surface->damage);
1200 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001201 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001202}
1203
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001204static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001205compositor_accumulate_damage(struct weston_compositor *ec)
1206{
1207 struct weston_plane *plane;
1208 struct weston_surface *es;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001209 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001210
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001211 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001212
1213 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001214 pixman_region32_copy(&plane->clip, &clip);
1215
1216 pixman_region32_init(&opaque);
1217
1218 wl_list_for_each(es, &ec->surface_list, link) {
1219 if (es->plane != plane)
1220 continue;
1221
1222 surface_accumulate_damage(es, &opaque);
1223 }
1224
1225 pixman_region32_union(&clip, &clip, &opaque);
1226 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001227 }
1228
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001229 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001230
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001231 wl_list_for_each(es, &ec->surface_list, link) {
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001232 /* Both the renderer and the backend have seen the buffer
1233 * by now. If renderer needs the buffer, it has its own
1234 * reference set. If the backend wants to keep the buffer
1235 * around for migrating the surface into a non-primary plane
1236 * later, keep_buffer is true. Otherwise, drop the core
1237 * reference now, and allow early buffer release. This enables
1238 * clients to use single-buffering.
1239 */
1240 if (!es->keep_buffer)
1241 weston_buffer_reference(&es->buffer_ref, NULL);
1242 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001243}
1244
1245static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001246surface_list_add(struct weston_compositor *compositor,
1247 struct weston_surface *surface)
1248{
1249 struct weston_subsurface *sub;
1250
1251 if (wl_list_empty(&surface->subsurface_list)) {
1252 weston_surface_update_transform(surface);
1253 wl_list_insert(compositor->surface_list.prev, &surface->link);
1254 return;
1255 }
1256
1257 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1258 if (!weston_surface_is_mapped(sub->surface))
1259 continue;
1260
1261 if (sub->surface == surface) {
1262 weston_surface_update_transform(sub->surface);
1263 wl_list_insert(compositor->surface_list.prev,
1264 &sub->surface->link);
1265 } else {
1266 surface_list_add(compositor, sub->surface);
1267 }
1268 }
1269}
1270
1271static void
1272weston_compositor_build_surface_list(struct weston_compositor *compositor)
1273{
1274 struct weston_surface *surface;
1275 struct weston_layer *layer;
1276
1277 wl_list_init(&compositor->surface_list);
1278 wl_list_for_each(layer, &compositor->layer_list, link) {
1279 wl_list_for_each(surface, &layer->surface_list, layer_link) {
1280 surface_list_add(compositor, surface);
1281 }
1282 }
1283}
1284
1285static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001286weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001287{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001288 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001289 struct weston_surface *es;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001290 struct weston_animation *animation, *next;
1291 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001292 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001293 pixman_region32_t output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001294
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001295 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001296 weston_compositor_build_surface_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001297
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001298 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001299 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001300 else
1301 wl_list_for_each(es, &ec->surface_list, link)
1302 weston_surface_move_to_plane(es, &ec->primary_plane);
1303
Pekka Paalanene67858b2013-04-25 13:57:42 +03001304 wl_list_init(&frame_callback_list);
1305 wl_list_for_each(es, &ec->surface_list, link) {
1306 if (es->output == output) {
1307 wl_list_insert_list(&frame_callback_list,
1308 &es->frame_callback_list);
1309 wl_list_init(&es->frame_callback_list);
1310 }
1311 }
1312
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001313 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001314
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001315 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001316 pixman_region32_intersect(&output_damage,
1317 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001318 pixman_region32_subtract(&output_damage,
1319 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001320
Scott Moreauccbf29d2012-02-22 14:21:41 -07001321 if (output->dirty)
1322 weston_output_update_matrix(output);
1323
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001324 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001325
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001326 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001327
Kristian Høgsbergef044142011-06-21 15:02:12 -04001328 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001329
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001330 weston_compositor_repick(ec);
1331 wl_event_loop_dispatch(ec->input_loop, 0);
1332
Jonas Ådahldb773762012-06-13 00:01:21 +02001333 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001334 wl_callback_send_done(cb->resource, msecs);
1335 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001336 }
1337
Scott Moreaud64cf212012-06-08 19:40:54 -06001338 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001339 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001340 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001341 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001342}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001343
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001344static int
1345weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001346{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001347 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001348
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001349 wl_event_loop_dispatch(compositor->input_loop, 0);
1350
1351 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001352}
1353
1354WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001355weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001356{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001357 struct weston_compositor *compositor = output->compositor;
1358 struct wl_event_loop *loop =
1359 wl_display_get_event_loop(compositor->wl_display);
1360 int fd;
1361
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001362 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001363 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001364 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001365 return;
1366 }
1367
1368 output->repaint_scheduled = 0;
1369 if (compositor->input_loop_source)
1370 return;
1371
1372 fd = wl_event_loop_get_fd(compositor->input_loop);
1373 compositor->input_loop_source =
1374 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1375 weston_compositor_read_input, compositor);
1376}
1377
1378static void
1379idle_repaint(void *data)
1380{
1381 struct weston_output *output = data;
1382
Jonas Ådahle5a12252013-04-05 23:07:11 +02001383 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001384}
1385
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001386WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001387weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1388{
1389 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001390 if (below != NULL)
1391 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001392}
1393
1394WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001395weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001396{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001397 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001398 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001399
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001400 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1401 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001402 return;
1403
Kristian Høgsbergef044142011-06-21 15:02:12 -04001404 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001405 output->repaint_needed = 1;
1406 if (output->repaint_scheduled)
1407 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001408
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001409 wl_event_loop_add_idle(loop, idle_repaint, output);
1410 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001411
1412 if (compositor->input_loop_source) {
1413 wl_event_source_remove(compositor->input_loop_source);
1414 compositor->input_loop_source = NULL;
1415 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001416}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001417
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001418WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001419weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1420{
1421 struct weston_output *output;
1422
1423 wl_list_for_each(output, &compositor->output_list, link)
1424 weston_output_schedule_repaint(output);
1425}
1426
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001427static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001428surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001429{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001430 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001431}
1432
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001433static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001434surface_attach(struct wl_client *client,
1435 struct wl_resource *resource,
1436 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1437{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001438 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001439 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001440
1441 if (buffer_resource)
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001442 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001443
Pekka Paalanende685b82012-12-04 15:58:12 +02001444 /* Attach, attach, without commit in between does not send
1445 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001446 if (surface->pending.buffer)
1447 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001448
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001449 surface->pending.sx = sx;
1450 surface->pending.sy = sy;
1451 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001452 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001453 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001454 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001455 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001456 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001457}
1458
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001459static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001460surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001461 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001462 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001463{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001464 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001465
Pekka Paalanen8e159182012-10-10 12:49:25 +03001466 pixman_region32_union_rect(&surface->pending.damage,
1467 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001468 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001469}
1470
Kristian Høgsberg33418202011-08-16 23:01:28 -04001471static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001472destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001473{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001474 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001475
1476 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001477 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001478}
1479
1480static void
1481surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001482 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001483{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001484 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001485 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001486
1487 cb = malloc(sizeof *cb);
1488 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001489 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001490 return;
1491 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001492
Jason Ekstranda85118c2013-06-27 20:17:02 -05001493 cb->resource = wl_resource_create(client,
1494 &wl_callback_interface,
1495 1, callback);
1496 wl_resource_set_implementation(cb->resource, NULL, cb,
1497 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001498
Pekka Paalanenbc106382012-10-10 12:49:31 +03001499 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001500}
1501
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001502static void
1503surface_set_opaque_region(struct wl_client *client,
1504 struct wl_resource *resource,
1505 struct wl_resource *region_resource)
1506{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001507 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001508 struct weston_region *region;
1509
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001510 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001511 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001512 pixman_region32_copy(&surface->pending.opaque,
1513 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001514 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001515 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001516 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001517}
1518
1519static void
1520surface_set_input_region(struct wl_client *client,
1521 struct wl_resource *resource,
1522 struct wl_resource *region_resource)
1523{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001524 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001525 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001526
1527 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001528 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001529 pixman_region32_copy(&surface->pending.input,
1530 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001531 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001532 pixman_region32_fini(&surface->pending.input);
1533 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001534 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001535}
1536
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001537static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001538weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001539{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001540 struct weston_subsurface *sub;
1541
1542 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1543 parent_link_pending) {
1544 wl_list_remove(&sub->parent_link);
1545 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1546 }
1547}
1548
1549static void
1550weston_surface_commit(struct weston_surface *surface)
1551{
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001552 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001553 int surface_width = 0;
1554 int surface_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001555
Alexander Larsson4ea95522013-05-22 14:41:37 +02001556 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001557 surface->buffer_transform = surface->pending.buffer_transform;
1558
Alexander Larsson4ea95522013-05-22 14:41:37 +02001559 /* wl_surface.set_buffer_scale */
1560 surface->buffer_scale = surface->pending.buffer_scale;
1561
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001562 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001563 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001564 weston_surface_attach(surface, surface->pending.buffer);
1565
Giulio Camuffo184df502013-02-21 11:29:21 +01001566 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001567 surface_width = weston_surface_buffer_width(surface);
1568 surface_height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001569 }
1570
1571 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001572 surface->configure(surface,
1573 surface->pending.sx, surface->pending.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001574 surface_width, surface_height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001575
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001576 if (surface->pending.buffer)
1577 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1578 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001579 surface->pending.sx = 0;
1580 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001581 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001582
1583 /* wl_surface.damage */
1584 pixman_region32_union(&surface->damage, &surface->damage,
1585 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001586 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1587 0, 0,
1588 surface->geometry.width,
1589 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001590 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001591
1592 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001593 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001594 surface->geometry.width,
1595 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001596 pixman_region32_intersect(&opaque,
1597 &opaque, &surface->pending.opaque);
1598
1599 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1600 pixman_region32_copy(&surface->opaque, &opaque);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001601 weston_surface_geometry_dirty(surface);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001602 }
1603
1604 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001605
1606 /* wl_surface.set_input_region */
1607 pixman_region32_fini(&surface->input);
1608 pixman_region32_init_rect(&surface->input, 0, 0,
1609 surface->geometry.width,
1610 surface->geometry.height);
1611 pixman_region32_intersect(&surface->input,
1612 &surface->input, &surface->pending.input);
1613
Pekka Paalanenbc106382012-10-10 12:49:31 +03001614 /* wl_surface.frame */
1615 wl_list_insert_list(&surface->frame_callback_list,
1616 &surface->pending.frame_callback_list);
1617 wl_list_init(&surface->pending.frame_callback_list);
1618
Pekka Paalanene67858b2013-04-25 13:57:42 +03001619 weston_surface_commit_subsurface_order(surface);
1620
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001621 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001622}
1623
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001624static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001625weston_subsurface_commit(struct weston_subsurface *sub);
1626
1627static void
1628weston_subsurface_parent_commit(struct weston_subsurface *sub,
1629 int parent_is_synchronized);
1630
1631static void
1632surface_commit(struct wl_client *client, struct wl_resource *resource)
1633{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001634 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001635 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1636
1637 if (sub) {
1638 weston_subsurface_commit(sub);
1639 return;
1640 }
1641
1642 weston_surface_commit(surface);
1643
1644 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1645 if (sub->surface != surface)
1646 weston_subsurface_parent_commit(sub, 0);
1647 }
1648}
1649
1650static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001651surface_set_buffer_transform(struct wl_client *client,
1652 struct wl_resource *resource, int transform)
1653{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001654 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001655
1656 surface->pending.buffer_transform = transform;
1657}
1658
Alexander Larsson4ea95522013-05-22 14:41:37 +02001659static void
1660surface_set_buffer_scale(struct wl_client *client,
1661 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001662 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02001663{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001664 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02001665
1666 surface->pending.buffer_scale = scale;
1667}
1668
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001669static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001670 surface_destroy,
1671 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001672 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001673 surface_frame,
1674 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001675 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001676 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001677 surface_set_buffer_transform,
1678 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001679};
1680
1681static void
1682compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001683 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001684{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04001685 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001686 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001687
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001688 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001689 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001690 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001691 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001692 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001693
Jason Ekstranda85118c2013-06-27 20:17:02 -05001694 surface->resource =
1695 wl_resource_create(client, &wl_surface_interface,
1696 wl_resource_get_version(resource), id);
1697 wl_resource_set_implementation(surface->resource, &surface_interface,
1698 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001699}
1700
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001701static void
1702destroy_region(struct wl_resource *resource)
1703{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001704 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001705
1706 pixman_region32_fini(&region->region);
1707 free(region);
1708}
1709
1710static void
1711region_destroy(struct wl_client *client, struct wl_resource *resource)
1712{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001713 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001714}
1715
1716static void
1717region_add(struct wl_client *client, struct wl_resource *resource,
1718 int32_t x, int32_t y, int32_t width, int32_t height)
1719{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001720 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001721
1722 pixman_region32_union_rect(&region->region, &region->region,
1723 x, y, width, height);
1724}
1725
1726static void
1727region_subtract(struct wl_client *client, struct wl_resource *resource,
1728 int32_t x, int32_t y, int32_t width, int32_t height)
1729{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001730 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001731 pixman_region32_t rect;
1732
1733 pixman_region32_init_rect(&rect, x, y, width, height);
1734 pixman_region32_subtract(&region->region, &region->region, &rect);
1735 pixman_region32_fini(&rect);
1736}
1737
1738static const struct wl_region_interface region_interface = {
1739 region_destroy,
1740 region_add,
1741 region_subtract
1742};
1743
1744static void
1745compositor_create_region(struct wl_client *client,
1746 struct wl_resource *resource, uint32_t id)
1747{
1748 struct weston_region *region;
1749
1750 region = malloc(sizeof *region);
1751 if (region == NULL) {
1752 wl_resource_post_no_memory(resource);
1753 return;
1754 }
1755
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001756 pixman_region32_init(&region->region);
1757
Jason Ekstranda85118c2013-06-27 20:17:02 -05001758 region->resource =
1759 wl_resource_create(client, &wl_region_interface, 1, id);
1760 wl_resource_set_implementation(region->resource, &region_interface,
1761 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001762}
1763
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001764static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001765 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001766 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001767};
1768
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001769static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001770weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
1771{
1772 struct weston_surface *surface = sub->surface;
1773 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001774 int surface_width = 0;
1775 int surface_height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001776
Alexander Larsson4ea95522013-05-22 14:41:37 +02001777 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001778 surface->buffer_transform = sub->cached.buffer_transform;
1779
Alexander Larsson4ea95522013-05-22 14:41:37 +02001780 /* wl_surface.set_buffer_scale */
1781 surface->buffer_scale = sub->cached.buffer_scale;
1782
Pekka Paalanene67858b2013-04-25 13:57:42 +03001783 /* wl_surface.attach */
1784 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
1785 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
1786 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
1787
1788 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001789 surface_width = weston_surface_buffer_width(surface);
1790 surface_height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001791 }
1792
1793 if (surface->configure && sub->cached.newly_attached)
1794 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001795 surface_width, surface_height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001796 sub->cached.sx = 0;
1797 sub->cached.sy = 0;
1798 sub->cached.newly_attached = 0;
1799
1800 /* wl_surface.damage */
1801 pixman_region32_union(&surface->damage, &surface->damage,
1802 &sub->cached.damage);
1803 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1804 0, 0,
1805 surface->geometry.width,
1806 surface->geometry.height);
1807 empty_region(&sub->cached.damage);
1808
1809 /* wl_surface.set_opaque_region */
1810 pixman_region32_init_rect(&opaque, 0, 0,
1811 surface->geometry.width,
1812 surface->geometry.height);
1813 pixman_region32_intersect(&opaque,
1814 &opaque, &sub->cached.opaque);
1815
1816 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1817 pixman_region32_copy(&surface->opaque, &opaque);
1818 weston_surface_geometry_dirty(surface);
1819 }
1820
1821 pixman_region32_fini(&opaque);
1822
1823 /* wl_surface.set_input_region */
1824 pixman_region32_fini(&surface->input);
1825 pixman_region32_init_rect(&surface->input, 0, 0,
1826 surface->geometry.width,
1827 surface->geometry.height);
1828 pixman_region32_intersect(&surface->input,
1829 &surface->input, &sub->cached.input);
1830
1831 /* wl_surface.frame */
1832 wl_list_insert_list(&surface->frame_callback_list,
1833 &sub->cached.frame_callback_list);
1834 wl_list_init(&sub->cached.frame_callback_list);
1835
1836 weston_surface_commit_subsurface_order(surface);
1837
1838 weston_surface_schedule_repaint(surface);
1839
1840 sub->cached.has_data = 0;
1841}
1842
1843static void
1844weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
1845{
1846 struct weston_surface *surface = sub->surface;
1847
1848 /*
1849 * If this commit would cause the surface to move by the
1850 * attach(dx, dy) parameters, the old damage region must be
1851 * translated to correspond to the new surface coordinate system
1852 * origin.
1853 */
1854 pixman_region32_translate(&sub->cached.damage,
1855 -surface->pending.sx, -surface->pending.sy);
1856 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
1857 &surface->pending.damage);
1858 empty_region(&surface->pending.damage);
1859
1860 if (surface->pending.newly_attached) {
1861 sub->cached.newly_attached = 1;
1862 weston_buffer_reference(&sub->cached.buffer_ref,
1863 surface->pending.buffer);
1864 }
1865 sub->cached.sx += surface->pending.sx;
1866 sub->cached.sy += surface->pending.sy;
1867 surface->pending.sx = 0;
1868 surface->pending.sy = 0;
1869 surface->pending.newly_attached = 0;
1870
1871 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001872 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001873
1874 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
1875
1876 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
1877
1878 wl_list_insert_list(&sub->cached.frame_callback_list,
1879 &surface->pending.frame_callback_list);
1880 wl_list_init(&surface->pending.frame_callback_list);
1881
1882 sub->cached.has_data = 1;
1883}
1884
1885static int
1886weston_subsurface_is_synchronized(struct weston_subsurface *sub)
1887{
1888 while (sub) {
1889 if (sub->synchronized)
1890 return 1;
1891
1892 if (!sub->parent)
1893 return 0;
1894
1895 sub = weston_surface_to_subsurface(sub->parent);
1896 }
1897
1898 return 0;
1899}
1900
1901static void
1902weston_subsurface_commit(struct weston_subsurface *sub)
1903{
1904 struct weston_surface *surface = sub->surface;
1905 struct weston_subsurface *tmp;
1906
1907 /* Recursive check for effectively synchronized. */
1908 if (weston_subsurface_is_synchronized(sub)) {
1909 weston_subsurface_commit_to_cache(sub);
1910 } else {
1911 if (sub->cached.has_data) {
1912 /* flush accumulated state from cache */
1913 weston_subsurface_commit_to_cache(sub);
1914 weston_subsurface_commit_from_cache(sub);
1915 } else {
1916 weston_surface_commit(surface);
1917 }
1918
1919 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1920 if (tmp->surface != surface)
1921 weston_subsurface_parent_commit(tmp, 0);
1922 }
1923 }
1924}
1925
1926static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001927weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001928{
1929 struct weston_surface *surface = sub->surface;
1930 struct weston_subsurface *tmp;
1931
Pekka Paalanene67858b2013-04-25 13:57:42 +03001932 /* From now on, commit_from_cache the whole sub-tree, regardless of
1933 * the synchronized mode of each child. This sub-surface or some
1934 * of its ancestors were synchronized, so we are synchronized
1935 * all the way down.
1936 */
1937
1938 if (sub->cached.has_data)
1939 weston_subsurface_commit_from_cache(sub);
1940
1941 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1942 if (tmp->surface != surface)
1943 weston_subsurface_parent_commit(tmp, 1);
1944 }
1945}
1946
1947static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001948weston_subsurface_parent_commit(struct weston_subsurface *sub,
1949 int parent_is_synchronized)
1950{
1951 if (sub->position.set) {
1952 weston_surface_set_position(sub->surface,
1953 sub->position.x, sub->position.y);
1954 sub->position.set = 0;
1955 }
1956
1957 if (parent_is_synchronized || sub->synchronized)
1958 weston_subsurface_synchronized_commit(sub);
1959}
1960
1961static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001962subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
1963 int32_t width, int32_t height)
1964{
1965 struct weston_compositor *compositor = surface->compositor;
1966
1967 weston_surface_configure(surface,
1968 surface->geometry.x + dx,
1969 surface->geometry.y + dy,
1970 width, height);
1971
1972 /* No need to check parent mappedness, because if parent is not
1973 * mapped, parent is not in a visible layer, so this sub-surface
1974 * will not be drawn either.
1975 */
1976 if (!weston_surface_is_mapped(surface)) {
1977 wl_list_init(&surface->layer_link);
1978
1979 /* Cannot call weston_surface_update_transform(),
1980 * because that would call it also for the parent surface,
1981 * which might not be mapped yet. That would lead to
1982 * inconsistent state, where the window could never be
1983 * mapped.
1984 *
1985 * Instead just assing any output, to make
1986 * weston_surface_is_mapped() return true, so that when the
1987 * parent surface does get mapped, this one will get
1988 * included, too. See surface_list_add().
1989 */
1990 assert(!wl_list_empty(&compositor->output_list));
1991 surface->output = container_of(compositor->output_list.next,
1992 struct weston_output, link);
1993 }
1994}
1995
1996static struct weston_subsurface *
1997weston_surface_to_subsurface(struct weston_surface *surface)
1998{
1999 if (surface->configure == subsurface_configure)
2000 return surface->configure_private;
2001
2002 return NULL;
2003}
2004
Pekka Paalanen01388e22013-04-25 13:57:44 +03002005WL_EXPORT struct weston_surface *
2006weston_surface_get_main_surface(struct weston_surface *surface)
2007{
2008 struct weston_subsurface *sub;
2009
2010 while (surface && (sub = weston_surface_to_subsurface(surface)))
2011 surface = sub->parent;
2012
2013 return surface;
2014}
2015
Pekka Paalanene67858b2013-04-25 13:57:42 +03002016static void
2017subsurface_set_position(struct wl_client *client,
2018 struct wl_resource *resource, int32_t x, int32_t y)
2019{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002020 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002021
2022 if (!sub)
2023 return;
2024
2025 sub->position.x = x;
2026 sub->position.y = y;
2027 sub->position.set = 1;
2028}
2029
2030static struct weston_subsurface *
2031subsurface_from_surface(struct weston_surface *surface)
2032{
2033 struct weston_subsurface *sub;
2034
2035 sub = weston_surface_to_subsurface(surface);
2036 if (sub)
2037 return sub;
2038
2039 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2040 if (sub->surface == surface)
2041 return sub;
2042
2043 return NULL;
2044}
2045
2046static struct weston_subsurface *
2047subsurface_sibling_check(struct weston_subsurface *sub,
2048 struct weston_surface *surface,
2049 const char *request)
2050{
2051 struct weston_subsurface *sibling;
2052
2053 sibling = subsurface_from_surface(surface);
2054
2055 if (!sibling) {
2056 wl_resource_post_error(sub->resource,
2057 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2058 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002059 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002060 return NULL;
2061 }
2062
2063 if (sibling->parent != sub->parent) {
2064 wl_resource_post_error(sub->resource,
2065 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2066 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002067 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002068 return NULL;
2069 }
2070
2071 return sibling;
2072}
2073
2074static void
2075subsurface_place_above(struct wl_client *client,
2076 struct wl_resource *resource,
2077 struct wl_resource *sibling_resource)
2078{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002079 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002080 struct weston_surface *surface =
2081 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002082 struct weston_subsurface *sibling;
2083
2084 if (!sub)
2085 return;
2086
2087 sibling = subsurface_sibling_check(sub, surface, "place_above");
2088 if (!sibling)
2089 return;
2090
2091 wl_list_remove(&sub->parent_link_pending);
2092 wl_list_insert(sibling->parent_link_pending.prev,
2093 &sub->parent_link_pending);
2094}
2095
2096static void
2097subsurface_place_below(struct wl_client *client,
2098 struct wl_resource *resource,
2099 struct wl_resource *sibling_resource)
2100{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002101 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002102 struct weston_surface *surface =
2103 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002104 struct weston_subsurface *sibling;
2105
2106 if (!sub)
2107 return;
2108
2109 sibling = subsurface_sibling_check(sub, surface, "place_below");
2110 if (!sibling)
2111 return;
2112
2113 wl_list_remove(&sub->parent_link_pending);
2114 wl_list_insert(&sibling->parent_link_pending,
2115 &sub->parent_link_pending);
2116}
2117
2118static void
2119subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2120{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002121 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002122
2123 if (sub)
2124 sub->synchronized = 1;
2125}
2126
2127static void
2128subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2129{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002130 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002131
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002132 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002133 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002134
2135 /* If sub became effectively desynchronized, flush. */
2136 if (!weston_subsurface_is_synchronized(sub))
2137 weston_subsurface_synchronized_commit(sub);
2138 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002139}
2140
2141static void
2142weston_subsurface_cache_init(struct weston_subsurface *sub)
2143{
2144 pixman_region32_init(&sub->cached.damage);
2145 pixman_region32_init(&sub->cached.opaque);
2146 pixman_region32_init(&sub->cached.input);
2147 wl_list_init(&sub->cached.frame_callback_list);
2148 sub->cached.buffer_ref.buffer = NULL;
2149}
2150
2151static void
2152weston_subsurface_cache_fini(struct weston_subsurface *sub)
2153{
2154 struct weston_frame_callback *cb, *tmp;
2155
2156 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002157 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002158
2159 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2160 pixman_region32_fini(&sub->cached.damage);
2161 pixman_region32_fini(&sub->cached.opaque);
2162 pixman_region32_fini(&sub->cached.input);
2163}
2164
2165static void
2166weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2167{
2168 wl_list_remove(&sub->parent_link);
2169 wl_list_remove(&sub->parent_link_pending);
2170 wl_list_remove(&sub->parent_destroy_listener.link);
2171 sub->parent = NULL;
2172}
2173
2174static void
2175weston_subsurface_destroy(struct weston_subsurface *sub);
2176
2177static void
2178subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2179{
2180 struct weston_subsurface *sub =
2181 container_of(listener, struct weston_subsurface,
2182 surface_destroy_listener);
2183 assert(data == &sub->surface->resource);
2184
2185 /* The protocol object (wl_resource) is left inert. */
2186 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002187 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002188
2189 weston_subsurface_destroy(sub);
2190}
2191
2192static void
2193subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2194{
2195 struct weston_subsurface *sub =
2196 container_of(listener, struct weston_subsurface,
2197 parent_destroy_listener);
2198 assert(data == &sub->parent->resource);
2199 assert(sub->surface != sub->parent);
2200
2201 if (weston_surface_is_mapped(sub->surface))
2202 weston_surface_unmap(sub->surface);
2203
2204 weston_subsurface_unlink_parent(sub);
2205}
2206
2207static void
2208subsurface_resource_destroy(struct wl_resource *resource)
2209{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002210 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002211
2212 if (sub)
2213 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002214}
2215
2216static void
2217subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2218{
2219 wl_resource_destroy(resource);
2220}
2221
2222static void
2223weston_subsurface_link_parent(struct weston_subsurface *sub,
2224 struct weston_surface *parent)
2225{
2226 sub->parent = parent;
2227 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002228 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002229 &sub->parent_destroy_listener);
2230
2231 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2232 wl_list_insert(&parent->subsurface_list_pending,
2233 &sub->parent_link_pending);
2234}
2235
2236static void
2237weston_subsurface_link_surface(struct weston_subsurface *sub,
2238 struct weston_surface *surface)
2239{
2240 sub->surface = surface;
2241 sub->surface_destroy_listener.notify =
2242 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002243 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002244 &sub->surface_destroy_listener);
2245}
2246
2247static void
2248weston_subsurface_destroy(struct weston_subsurface *sub)
2249{
2250 assert(sub->surface);
2251
2252 if (sub->resource) {
2253 assert(weston_surface_to_subsurface(sub->surface) == sub);
2254 assert(sub->parent_destroy_listener.notify ==
2255 subsurface_handle_parent_destroy);
2256
2257 weston_surface_set_transform_parent(sub->surface, NULL);
2258 if (sub->parent)
2259 weston_subsurface_unlink_parent(sub);
2260
2261 weston_subsurface_cache_fini(sub);
2262
2263 sub->surface->configure = NULL;
2264 sub->surface->configure_private = NULL;
2265 } else {
2266 /* the dummy weston_subsurface for the parent itself */
2267 assert(sub->parent_destroy_listener.notify == NULL);
2268 wl_list_remove(&sub->parent_link);
2269 wl_list_remove(&sub->parent_link_pending);
2270 }
2271
2272 wl_list_remove(&sub->surface_destroy_listener.link);
2273 free(sub);
2274}
2275
2276static const struct wl_subsurface_interface subsurface_implementation = {
2277 subsurface_destroy,
2278 subsurface_set_position,
2279 subsurface_place_above,
2280 subsurface_place_below,
2281 subsurface_set_sync,
2282 subsurface_set_desync
2283};
2284
2285static struct weston_subsurface *
2286weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2287 struct weston_surface *parent)
2288{
2289 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002290 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002291
2292 sub = calloc(1, sizeof *sub);
2293 if (!sub)
2294 return NULL;
2295
Jason Ekstranda85118c2013-06-27 20:17:02 -05002296 sub->resource =
2297 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002298 if (!sub->resource) {
2299 free(sub);
2300 return NULL;
2301 }
2302
Jason Ekstranda85118c2013-06-27 20:17:02 -05002303 wl_resource_set_implementation(sub->resource,
2304 &subsurface_implementation,
2305 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002306 weston_subsurface_link_surface(sub, surface);
2307 weston_subsurface_link_parent(sub, parent);
2308 weston_subsurface_cache_init(sub);
2309 sub->synchronized = 1;
2310 weston_surface_set_transform_parent(surface, parent);
2311
2312 return sub;
2313}
2314
2315/* Create a dummy subsurface for having the parent itself in its
2316 * sub-surface lists. Makes stacking order manipulation easy.
2317 */
2318static struct weston_subsurface *
2319weston_subsurface_create_for_parent(struct weston_surface *parent)
2320{
2321 struct weston_subsurface *sub;
2322
2323 sub = calloc(1, sizeof *sub);
2324 if (!sub)
2325 return NULL;
2326
2327 weston_subsurface_link_surface(sub, parent);
2328 sub->parent = parent;
2329 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2330 wl_list_insert(&parent->subsurface_list_pending,
2331 &sub->parent_link_pending);
2332
2333 return sub;
2334}
2335
2336static void
2337subcompositor_get_subsurface(struct wl_client *client,
2338 struct wl_resource *resource,
2339 uint32_t id,
2340 struct wl_resource *surface_resource,
2341 struct wl_resource *parent_resource)
2342{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002343 struct weston_surface *surface =
2344 wl_resource_get_user_data(surface_resource);
2345 struct weston_surface *parent =
2346 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002347 struct weston_subsurface *sub;
2348 static const char where[] = "get_subsurface: wl_subsurface@";
2349
2350 if (surface == parent) {
2351 wl_resource_post_error(resource,
2352 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2353 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002354 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002355 return;
2356 }
2357
2358 if (weston_surface_to_subsurface(surface)) {
2359 wl_resource_post_error(resource,
2360 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2361 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002362 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002363 return;
2364 }
2365
2366 if (surface->configure) {
2367 wl_resource_post_error(resource,
2368 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2369 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002370 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002371 return;
2372 }
2373
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002374 if (weston_surface_get_main_surface(parent) == surface) {
2375 wl_resource_post_error(resource,
2376 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2377 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002378 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002379 return;
2380 }
2381
Pekka Paalanene67858b2013-04-25 13:57:42 +03002382 /* make sure the parent is in its own list */
2383 if (wl_list_empty(&parent->subsurface_list)) {
2384 if (!weston_subsurface_create_for_parent(parent)) {
2385 wl_resource_post_no_memory(resource);
2386 return;
2387 }
2388 }
2389
2390 sub = weston_subsurface_create(id, surface, parent);
2391 if (!sub) {
2392 wl_resource_post_no_memory(resource);
2393 return;
2394 }
2395
2396 surface->configure = subsurface_configure;
2397 surface->configure_private = sub;
2398}
2399
2400static void
2401subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2402{
2403 wl_resource_destroy(resource);
2404}
2405
2406static const struct wl_subcompositor_interface subcompositor_interface = {
2407 subcompositor_destroy,
2408 subcompositor_get_subsurface
2409};
2410
2411static void
2412bind_subcompositor(struct wl_client *client,
2413 void *data, uint32_t version, uint32_t id)
2414{
2415 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002416 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002417
Jason Ekstranda85118c2013-06-27 20:17:02 -05002418 resource =
2419 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
2420 if (resource)
2421 wl_resource_set_implementation(resource,
2422 &subcompositor_interface,
2423 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002424}
2425
2426static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002427weston_compositor_dpms(struct weston_compositor *compositor,
2428 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002429{
2430 struct weston_output *output;
2431
2432 wl_list_for_each(output, &compositor->output_list, link)
2433 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002434 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002435}
2436
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002437WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002438weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002439{
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002440 switch (compositor->state) {
2441 case WESTON_COMPOSITOR_SLEEPING:
2442 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2443 /* fall through */
2444 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002445 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002446 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002447 /* fall through */
2448 default:
2449 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2450 wl_event_source_timer_update(compositor->idle_source,
2451 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002452 }
2453}
2454
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002455WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002456weston_compositor_offscreen(struct weston_compositor *compositor)
2457{
2458 switch (compositor->state) {
2459 case WESTON_COMPOSITOR_OFFSCREEN:
2460 return;
2461 case WESTON_COMPOSITOR_SLEEPING:
2462 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2463 /* fall through */
2464 default:
2465 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2466 wl_event_source_timer_update(compositor->idle_source, 0);
2467 }
2468}
2469
2470WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002471weston_compositor_sleep(struct weston_compositor *compositor)
2472{
2473 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2474 return;
2475
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002476 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002477 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2478 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2479}
2480
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002481static int
2482idle_handler(void *data)
2483{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002484 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002485
2486 if (compositor->idle_inhibit)
2487 return 1;
2488
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002489 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002490 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002491
2492 return 1;
2493}
2494
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002495WL_EXPORT void
2496weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2497{
2498 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002499 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002500 plane->x = x;
2501 plane->y = y;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002502
2503 /* Init the link so that the call to wl_list_remove() when releasing
2504 * the plane without ever stacking doesn't lead to a crash */
2505 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002506}
2507
2508WL_EXPORT void
2509weston_plane_release(struct weston_plane *plane)
2510{
2511 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002512 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002513
2514 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002515}
2516
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002517WL_EXPORT void
2518weston_compositor_stack_plane(struct weston_compositor *ec,
2519 struct weston_plane *plane,
2520 struct weston_plane *above)
2521{
2522 if (above)
2523 wl_list_insert(above->link.prev, &plane->link);
2524 else
2525 wl_list_insert(&ec->plane_list, &plane->link);
2526}
2527
Casey Dahlin9074db52012-04-19 22:50:09 -04002528static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002529{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002530 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002531}
2532
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002533static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002534bind_output(struct wl_client *client,
2535 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002536{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002537 struct weston_output *output = data;
2538 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002539 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002540
Jason Ekstranda85118c2013-06-27 20:17:02 -05002541 resource = wl_resource_create(client, &wl_output_interface,
2542 MIN(version, 2), id);
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002543
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002544 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002545 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002546
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002547 wl_output_send_geometry(resource,
2548 output->x,
2549 output->y,
2550 output->mm_width,
2551 output->mm_height,
2552 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002553 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002554 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002555 if (version >= 2)
2556 wl_output_send_scale(resource,
2557 output->scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002558
2559 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002560 wl_output_send_mode(resource,
2561 mode->flags,
2562 mode->width,
2563 mode->height,
2564 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002565 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002566
2567 if (version >= 2)
2568 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002569}
2570
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002571WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002572weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002573{
Richard Hughes64ddde12013-05-01 21:52:10 +01002574 wl_signal_emit(&output->destroy_signal, output);
2575
Richard Hughesafe690c2013-05-02 10:10:04 +01002576 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002577 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002578 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002579 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002580
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002581 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002582}
2583
Scott Moreau1bad5db2012-08-18 01:04:05 -06002584static void
2585weston_output_compute_transform(struct weston_output *output)
2586{
2587 struct weston_matrix transform;
2588 int flip;
2589
2590 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002591 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002592
2593 switch(output->transform) {
2594 case WL_OUTPUT_TRANSFORM_FLIPPED:
2595 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2596 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2597 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002598 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002599 flip = -1;
2600 break;
2601 default:
2602 flip = 1;
2603 break;
2604 }
2605
2606 switch(output->transform) {
2607 case WL_OUTPUT_TRANSFORM_NORMAL:
2608 case WL_OUTPUT_TRANSFORM_FLIPPED:
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_90:
2615 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2616 transform.d[0] = 0;
2617 transform.d[1] = -flip;
2618 transform.d[4] = 1;
2619 transform.d[5] = 0;
2620 break;
2621 case WL_OUTPUT_TRANSFORM_180:
2622 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2623 transform.d[0] = -flip;
2624 transform.d[1] = 0;
2625 transform.d[4] = 0;
2626 transform.d[5] = -1;
2627 break;
2628 case WL_OUTPUT_TRANSFORM_270:
2629 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2630 transform.d[0] = 0;
2631 transform.d[1] = flip;
2632 transform.d[4] = -1;
2633 transform.d[5] = 0;
2634 break;
2635 default:
2636 break;
2637 }
2638
2639 weston_matrix_multiply(&output->matrix, &transform);
2640}
2641
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002642WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002643weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002644{
Scott Moreau850ca422012-05-21 15:21:25 -06002645 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002646 struct weston_matrix camera;
2647 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002648
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002649 weston_matrix_init(&output->matrix);
2650 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002651 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2652 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002653
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002654 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002655 2.0 / (output->width + output->border.left + output->border.right),
2656 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2657
2658 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002659
Scott Moreauccbf29d2012-02-22 14:21:41 -07002660 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002661 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002662 weston_matrix_init(&camera);
2663 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002664 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002665 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002666 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002667 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002668 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002669 weston_matrix_multiply(&output->matrix, &modelview);
2670 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002671
Scott Moreauccbf29d2012-02-22 14:21:41 -07002672 output->dirty = 0;
2673}
2674
Scott Moreau1bad5db2012-08-18 01:04:05 -06002675static void
Alexander Larsson0b135062013-05-28 16:23:36 +02002676weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002677{
2678 output->transform = transform;
2679
2680 switch (transform) {
2681 case WL_OUTPUT_TRANSFORM_90:
2682 case WL_OUTPUT_TRANSFORM_270:
2683 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2684 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2685 /* Swap width and height */
2686 output->width = output->current->height;
2687 output->height = output->current->width;
2688 break;
2689 case WL_OUTPUT_TRANSFORM_NORMAL:
2690 case WL_OUTPUT_TRANSFORM_180:
2691 case WL_OUTPUT_TRANSFORM_FLIPPED:
2692 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2693 output->width = output->current->width;
2694 output->height = output->current->height;
2695 break;
2696 default:
2697 break;
2698 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06002699
Alexander Larsson4ea95522013-05-22 14:41:37 +02002700 output->scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02002701 output->width /= scale;
2702 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002703}
2704
Scott Moreauccbf29d2012-02-22 14:21:41 -07002705WL_EXPORT void
2706weston_output_move(struct weston_output *output, int x, int y)
2707{
2708 output->x = x;
2709 output->y = y;
2710
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002711 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002712 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002713 output->width,
2714 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002715}
2716
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002717WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002718weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02002719 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002720 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002721{
2722 output->compositor = c;
2723 output->x = x;
2724 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002725 output->border.top = 0;
2726 output->border.bottom = 0;
2727 output->border.left = 0;
2728 output->border.right = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02002729 output->mm_width = mm_width;
2730 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002731 output->dirty = 1;
Alexander Larssone32c3762013-05-28 16:23:37 +02002732 output->origin_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002733
Alexander Larsson0b135062013-05-28 16:23:36 +02002734 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06002735 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002736
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002737 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002738 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002739
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002740 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01002741 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002742 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002743 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002744
Casey Dahlin58ba1372012-04-19 22:50:08 -04002745 output->id = ffs(~output->compositor->output_id_pool) - 1;
2746 output->compositor->output_id_pool |= 1 << output->id;
2747
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002748 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002749 wl_global_create(c->wl_display, &wl_output_interface, 2,
2750 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01002751 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002752}
2753
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002754WL_EXPORT void
2755weston_output_transform_coordinate(struct weston_output *output,
2756 int device_x, int device_y,
2757 wl_fixed_t *x, wl_fixed_t *y)
2758{
2759 wl_fixed_t tx, ty;
2760 wl_fixed_t width, height;
2761
2762 width = wl_fixed_from_int(output->width * output->scale - 1);
2763 height = wl_fixed_from_int(output->height * output->scale - 1);
2764
2765 switch(output->transform) {
2766 case WL_OUTPUT_TRANSFORM_NORMAL:
2767 default:
2768 tx = wl_fixed_from_int(device_x);
2769 ty = wl_fixed_from_int(device_y);
2770 break;
2771 case WL_OUTPUT_TRANSFORM_90:
2772 tx = wl_fixed_from_int(device_y);
2773 ty = height - wl_fixed_from_int(device_x);
2774 break;
2775 case WL_OUTPUT_TRANSFORM_180:
2776 tx = width - wl_fixed_from_int(device_x);
2777 ty = height - wl_fixed_from_int(device_y);
2778 break;
2779 case WL_OUTPUT_TRANSFORM_270:
2780 tx = width - wl_fixed_from_int(device_y);
2781 ty = wl_fixed_from_int(device_x);
2782 break;
2783 case WL_OUTPUT_TRANSFORM_FLIPPED:
2784 tx = width - wl_fixed_from_int(device_x);
2785 ty = wl_fixed_from_int(device_y);
2786 break;
2787 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2788 tx = width - wl_fixed_from_int(device_y);
2789 ty = height - wl_fixed_from_int(device_x);
2790 break;
2791 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2792 tx = wl_fixed_from_int(device_x);
2793 ty = height - wl_fixed_from_int(device_y);
2794 break;
2795 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2796 tx = wl_fixed_from_int(device_y);
2797 ty = wl_fixed_from_int(device_x);
2798 break;
2799 }
2800
2801 *x = tx / output->scale + wl_fixed_from_int(output->x);
2802 *y = ty / output->scale + wl_fixed_from_int(output->y);
2803}
2804
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002805static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002806compositor_bind(struct wl_client *client,
2807 void *data, uint32_t version, uint32_t id)
2808{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002809 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002810 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002811
Jason Ekstranda85118c2013-06-27 20:17:02 -05002812 resource = wl_resource_create(client, &wl_compositor_interface,
2813 MIN(version, 3), id);
2814 if (resource)
2815 wl_resource_set_implementation(resource, &compositor_interface,
2816 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05002817}
2818
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002819static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002820log_uname(void)
2821{
2822 struct utsname usys;
2823
2824 uname(&usys);
2825
2826 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2827 usys.version, usys.machine);
2828}
2829
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002830WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02002831weston_environment_get_fd(const char *env)
2832{
2833 char *e, *end;
2834 int fd, flags;
2835
2836 e = getenv(env);
2837 if (!e)
2838 return -1;
2839 fd = strtol(e, &end, 0);
2840 if (*end != '\0')
2841 return -1;
2842
2843 flags = fcntl(fd, F_GETFD);
2844 if (flags == -1)
2845 return -1;
2846
2847 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
2848 unsetenv(env);
2849
2850 return fd;
2851}
2852
2853WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002854weston_compositor_init(struct weston_compositor *ec,
2855 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002856 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002857 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002858{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002859 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002860 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002861 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002862
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002863 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002864 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002865 wl_signal_init(&ec->destroy_signal);
2866 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002867 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002868 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002869 wl_signal_init(&ec->idle_signal);
2870 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002871 wl_signal_init(&ec->show_input_panel_signal);
2872 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02002873 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002874 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01002875 wl_signal_init(&ec->output_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002876 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002877
Casey Dahlin58ba1372012-04-19 22:50:08 -04002878 ec->output_id_pool = 0;
2879
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002880 if (!wl_global_create(display, &wl_compositor_interface, 3,
2881 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05002882 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002883
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002884 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
2885 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03002886 return -1;
2887
Daniel Stone725c2c32012-06-22 14:04:36 +01002888 wl_list_init(&ec->surface_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002889 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002890 wl_list_init(&ec->layer_list);
2891 wl_list_init(&ec->seat_list);
2892 wl_list_init(&ec->output_list);
2893 wl_list_init(&ec->key_binding_list);
2894 wl_list_init(&ec->button_binding_list);
2895 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002896 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002897
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002898 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002899 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002900
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002901 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
2902 weston_config_section_get_string(s, "keymap_rules",
2903 (char **) &xkb_names.rules, NULL);
2904 weston_config_section_get_string(s, "keymap_model",
2905 (char **) &xkb_names.model, NULL);
2906 weston_config_section_get_string(s, "keymap_layout",
2907 (char **) &xkb_names.layout, NULL);
2908 weston_config_section_get_string(s, "keymap_variant",
2909 (char **) &xkb_names.variant, NULL);
2910 weston_config_section_get_string(s, "keymap_options",
2911 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01002912
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002913 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
2914 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01002915
2916 ec->ping_handler = NULL;
2917
2918 screenshooter_create(ec);
2919 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002920 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002921
2922 wl_data_device_manager_init(ec->wl_display);
2923
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002924 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002925
Daniel Stone725c2c32012-06-22 14:04:36 +01002926 loop = wl_display_get_event_loop(ec->wl_display);
2927 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2928 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2929
2930 ec->input_loop = wl_event_loop_create();
2931
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002932 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2933 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2934
2935 weston_compositor_schedule_repaint(ec);
2936
Daniel Stone725c2c32012-06-22 14:04:36 +01002937 return 0;
2938}
2939
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002940WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002941weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002942{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002943 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002944
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002945 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002946 if (ec->input_loop_source)
2947 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002948
Matt Roper361d2ad2011-08-29 13:52:23 -07002949 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002950 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002951 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002952
Daniel Stone325fc2d2012-05-30 16:31:58 +01002953 weston_binding_list_destroy_all(&ec->key_binding_list);
2954 weston_binding_list_destroy_all(&ec->button_binding_list);
2955 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002956 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002957
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002958 weston_plane_release(&ec->primary_plane);
2959
Jonas Ådahlc97af922012-03-28 22:36:09 +02002960 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07002961
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002962 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07002963}
2964
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05002965WL_EXPORT void
2966weston_version(int *major, int *minor, int *micro)
2967{
2968 *major = WESTON_VERSION_MAJOR;
2969 *minor = WESTON_VERSION_MINOR;
2970 *micro = WESTON_VERSION_MICRO;
2971}
2972
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002973static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002974 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002975 const char *desc;
2976} capability_strings[] = {
2977 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002978 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002979};
2980
2981static void
2982weston_compositor_log_capabilities(struct weston_compositor *compositor)
2983{
2984 unsigned i;
2985 int yes;
2986
2987 weston_log("Compositor capabilities:\n");
2988 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
2989 yes = compositor->capabilities & capability_strings[i].bit;
2990 weston_log_continue(STAMP_SPACE "%s %s\n",
2991 capability_strings[i].desc,
2992 yes ? "yes" : "no");
2993 }
2994}
2995
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002996static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002997{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002998 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002999
Martin Minarik6d118362012-06-07 18:01:59 +02003000 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003001 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003002
3003 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003004}
3005
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003006#ifdef HAVE_LIBUNWIND
3007
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003008static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003009print_backtrace(void)
3010{
3011 unw_cursor_t cursor;
3012 unw_context_t context;
3013 unw_word_t off;
3014 unw_proc_info_t pip;
3015 int ret, i = 0;
3016 char procname[256];
3017 const char *filename;
3018 Dl_info dlinfo;
3019
3020 pip.unwind_info = NULL;
3021 ret = unw_getcontext(&context);
3022 if (ret) {
3023 weston_log("unw_getcontext: %d\n", ret);
3024 return;
3025 }
3026
3027 ret = unw_init_local(&cursor, &context);
3028 if (ret) {
3029 weston_log("unw_init_local: %d\n", ret);
3030 return;
3031 }
3032
3033 ret = unw_step(&cursor);
3034 while (ret > 0) {
3035 ret = unw_get_proc_info(&cursor, &pip);
3036 if (ret) {
3037 weston_log("unw_get_proc_info: %d\n", ret);
3038 break;
3039 }
3040
3041 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3042 if (ret && ret != -UNW_ENOMEM) {
3043 if (ret != -UNW_EUNSPEC)
3044 weston_log("unw_get_proc_name: %d\n", ret);
3045 procname[0] = '?';
3046 procname[1] = 0;
3047 }
3048
3049 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3050 *dlinfo.dli_fname)
3051 filename = dlinfo.dli_fname;
3052 else
3053 filename = "?";
3054
3055 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3056 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3057
3058 ret = unw_step(&cursor);
3059 if (ret < 0)
3060 weston_log("unw_step: %d\n", ret);
3061 }
3062}
3063
3064#else
3065
3066static void
3067print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003068{
3069 void *buffer[32];
3070 int i, count;
3071 Dl_info info;
3072
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003073 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3074 for (i = 0; i < count; i++) {
3075 dladdr(buffer[i], &info);
3076 weston_log(" [%016lx] %s (%s)\n",
3077 (long) buffer[i],
3078 info.dli_sname ? info.dli_sname : "--",
3079 info.dli_fname);
3080 }
3081}
3082
3083#endif
3084
3085static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003086on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003087{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003088 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003089 * then call the backend restore function, which will switch
3090 * back to the vt we launched from or ungrab X etc and then
3091 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003092 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003093 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003094 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003095
Peter Maatmane5b42e42013-03-27 22:38:53 +01003096 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003097
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003098 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003099
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003100 segv_compositor->restore(segv_compositor);
3101
3102 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003103}
3104
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003105static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003106load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003107{
3108 char path[PATH_MAX];
3109 void *module, *init;
3110
3111 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003112 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003113 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003114 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003115
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003116 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3117 if (module) {
3118 weston_log("Module '%s' already loaded\n", path);
3119 dlclose(module);
3120 return NULL;
3121 }
3122
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003123 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003124 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003125 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003126 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003127 return NULL;
3128 }
3129
3130 init = dlsym(module, entrypoint);
3131 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003132 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003133 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003134 return NULL;
3135 }
3136
3137 return init;
3138}
3139
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003140static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003141load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003142 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003143{
3144 const char *p, *end;
3145 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003146 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003147 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003148
3149 if (modules == NULL)
3150 return 0;
3151
3152 p = modules;
3153 while (*p) {
3154 end = strchrnul(p, ',');
3155 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3156 module_init = load_module(buffer, "module_init");
3157 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003158 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003159 p = end;
3160 while (*p == ',')
3161 p++;
3162
3163 }
3164
3165 return 0;
3166}
3167
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003168static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003169 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3170
3171static const char xdg_wrong_message[] =
3172 "fatal: environment variable XDG_RUNTIME_DIR\n"
3173 "is set to \"%s\", which is not a directory.\n";
3174
3175static const char xdg_wrong_mode_message[] =
3176 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3177 "correctly. Unix access mode must be 0700 but is %o,\n"
3178 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003179 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003180
3181static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003182 "Refer to your distribution on how to get it, or\n"
3183 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3184 "on how to implement it.\n";
3185
Martin Minarik37032f82012-06-18 20:15:18 +02003186static void
3187verify_xdg_runtime_dir(void)
3188{
3189 char *dir = getenv("XDG_RUNTIME_DIR");
3190 struct stat s;
3191
3192 if (!dir) {
3193 weston_log(xdg_error_message);
3194 weston_log_continue(xdg_detail_message);
3195 exit(EXIT_FAILURE);
3196 }
3197
3198 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3199 weston_log(xdg_wrong_message, dir);
3200 weston_log_continue(xdg_detail_message);
3201 exit(EXIT_FAILURE);
3202 }
3203
3204 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3205 weston_log(xdg_wrong_mode_message,
3206 dir, s.st_mode & 0777, s.st_uid);
3207 weston_log_continue(xdg_detail_message);
3208 }
3209}
3210
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003211static int
3212usage(int error_code)
3213{
3214 fprintf(stderr,
3215 "Usage: weston [OPTIONS]\n\n"
3216 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3217 "Weston supports multiple backends, and depending on which backend is in use\n"
3218 "different options will be accepted.\n\n"
3219
3220
3221 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003222 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003223 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003224 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3225 "\t\t\t\twayland-backend.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003226 " -S, --socket=NAME\tName of socket to listen on\n"
3227 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003228 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003229 " --log==FILE\t\tLog to the given file\n"
3230 " -h, --help\t\tThis help message\n\n");
3231
3232 fprintf(stderr,
3233 "Options for drm-backend.so:\n\n"
3234 " --connector=ID\tBring up only this connector\n"
3235 " --seat=SEAT\t\tThe seat that weston should run on\n"
3236 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003237 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003238 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3239
3240 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003241 "Options for fbdev-backend.so:\n\n"
3242 " --tty=TTY\t\tThe tty to use\n"
3243 " --device=DEVICE\tThe framebuffer device to use\n\n");
3244
3245 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003246 "Options for x11-backend.so:\n\n"
3247 " --width=WIDTH\t\tWidth of X window\n"
3248 " --height=HEIGHT\tHeight of X window\n"
3249 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003250 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003251 " --output-count=COUNT\tCreate multiple outputs\n"
3252 " --no-input\t\tDont create input devices\n\n");
3253
3254 fprintf(stderr,
3255 "Options for wayland-backend.so:\n\n"
3256 " --width=WIDTH\t\tWidth of Wayland surface\n"
3257 " --height=HEIGHT\tHeight of Wayland surface\n"
3258 " --display=DISPLAY\tWayland display to connect to\n\n");
3259
Pekka Paalanene31e0532013-05-22 18:03:07 +03003260#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3261 fprintf(stderr,
3262 "Options for rpi-backend.so:\n\n"
3263 " --tty=TTY\t\tThe tty to use\n"
3264 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3265 " --transform=TR\tThe output transformation, TR is one of:\n"
3266 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3267 "\n");
3268#endif
3269
Hardeningc077a842013-07-08 00:51:35 +02003270#if defined(BUILD_RDP_COMPOSITOR)
3271 fprintf(stderr,
3272 "Options for rdp-backend.so:\n\n"
3273 " --width=WIDTH\t\tWidth of desktop\n"
3274 " --height=HEIGHT\tHeight of desktop\n"
3275 " --extra-modes=MODES\t\n"
3276 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3277 " --address=ADDR\tThe address to bind\n"
3278 " --port=PORT\tThe port to listen on\n"
3279 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3280 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3281 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3282 "\n");
3283#endif
3284
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003285 exit(error_code);
3286}
3287
Peter Maatmane5b42e42013-03-27 22:38:53 +01003288static void
3289catch_signals(void)
3290{
3291 struct sigaction action;
3292
3293 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3294 action.sa_sigaction = on_caught_signal;
3295 sigemptyset(&action.sa_mask);
3296 sigaction(SIGSEGV, &action, NULL);
3297 sigaction(SIGABRT, &action, NULL);
3298}
3299
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003300int main(int argc, char *argv[])
3301{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003302 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003303 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003304 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003305 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003306 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003307 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003308 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003309 int *argc, char *argv[],
3310 struct weston_config *config);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003311 int i, config_fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003312 char *backend = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003313 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003314 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003315 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003316 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003317 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003318 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003319 struct weston_config *config;
3320 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003321
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003322 const struct weston_option core_options[] = {
3323 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3324 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3325 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003326 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003327 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003328 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003329 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003330 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003331
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003332 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003333
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003334 if (help)
3335 usage(EXIT_SUCCESS);
3336
Scott Moreau12245142012-08-29 15:15:58 -06003337 if (version) {
3338 printf(PACKAGE_STRING "\n");
3339 return EXIT_SUCCESS;
3340 }
3341
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003342 weston_log_file_open(log);
3343
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003344 weston_log("%s\n"
3345 STAMP_SPACE "%s\n"
3346 STAMP_SPACE "Bug reports to: %s\n"
3347 STAMP_SPACE "Build: %s\n",
3348 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003349 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003350 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003351
Martin Minarik37032f82012-06-18 20:15:18 +02003352 verify_xdg_runtime_dir();
3353
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003354 display = wl_display_create();
3355
Tiago Vignatti2116b892011-08-08 05:52:59 -07003356 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003357 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3358 display);
3359 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3360 display);
3361 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3362 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003363
3364 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003365 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3366 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003367
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003368 if (!backend) {
3369 if (getenv("WAYLAND_DISPLAY"))
3370 backend = "wayland-backend.so";
3371 else if (getenv("DISPLAY"))
3372 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003373 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003374 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003375 }
3376
Ossama Othmana50e6e42013-05-14 09:48:26 -07003377 config_fd = open_config_file("weston.ini");
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003378 config = weston_config_parse(config_fd);
3379 close(config_fd);
3380
3381 section = weston_config_get_section(config, "core", NULL, NULL);
3382 weston_config_section_get_string(section, "modules",
3383 &modules, "desktop-shell.so");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003384
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003385 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003386 if (!backend_init)
3387 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003388
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003389 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003390 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003391 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003392 exit(EXIT_FAILURE);
3393 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003394
Peter Maatmane5b42e42013-03-27 22:38:53 +01003395 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003396 segv_compositor = ec;
3397
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003398 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003399
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003400 setenv("WAYLAND_DISPLAY", socket_name, 1);
3401
Ossama Othmana50e6e42013-05-14 09:48:26 -07003402 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003403 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003404 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003405 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003406
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003407 for (i = 1; i < argc; i++)
3408 weston_log("fatal: unhandled option: %s\n", argv[i]);
3409 if (argc > 1) {
3410 ret = EXIT_FAILURE;
3411 goto out;
3412 }
3413
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003414 weston_compositor_log_capabilities(ec);
3415
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003416 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003417 weston_log("fatal: failed to add socket: %m\n");
3418 ret = EXIT_FAILURE;
3419 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003420 }
3421
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003422 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003423
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003424 wl_display_run(display);
3425
3426 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003427 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003428 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003429
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003430 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003431
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003432 for (i = ARRAY_LENGTH(signals); i;)
3433 wl_event_source_remove(signals[--i]);
3434
Daniel Stone855028f2012-05-01 20:37:10 +01003435 weston_compositor_xkb_destroy(ec);
3436
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003437 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003438 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003439
Martin Minarik19e6f262012-06-07 13:08:46 +02003440 weston_log_file_close();
3441
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003442 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003443}