blob: 78351eb1e962c179b07f504a490874d90c85eeb8 [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
Kristian Høgsberg82863022010-06-04 21:52:02 -040055#include "compositor.h"
U. Artie Eoffec08f332013-05-13 15:55:47 -070056#include "subsurface-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030057#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040058#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050059#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050060
Kristian Høgsberg27da5382011-06-21 17:32:25 -040061static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040062static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040063
64static int
65sigchld_handler(int signal_number, void *data)
66{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050067 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040068 int status;
69 pid_t pid;
70
Bill Spitzak027b9622012-03-17 15:22:03 -070071 pid = waitpid(-1, &status, WNOHANG);
72 if (!pid)
73 return 1;
74
Kristian Høgsberg27da5382011-06-21 17:32:25 -040075 wl_list_for_each(p, &child_process_list, link) {
76 if (p->pid == pid)
77 break;
78 }
79
80 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020081 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040082 return 1;
83 }
84
85 wl_list_remove(&p->link);
86 p->cleanup(p, status);
87
88 return 1;
89}
90
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020091static void
Alexander Larsson0b135062013-05-28 16:23:36 +020092weston_output_transform_scale_init(struct weston_output *output,
93 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020094
Rob Bradford27b17932013-06-26 18:08:46 +010095static void
96weston_compositor_build_surface_list(struct weston_compositor *compositor);
97
Alex Wu2dda6042012-04-17 17:20:47 +080098WL_EXPORT int
Alexander Larsson355748e2013-05-28 16:23:38 +020099weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode, int32_t scale)
Alex Wu2dda6042012-04-17 17:20:47 +0800100{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200101 struct weston_seat *seat;
102 pixman_region32_t old_output_region;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200103 int ret;
104
Alex Wu2dda6042012-04-17 17:20:47 +0800105 if (!output->switch_mode)
106 return -1;
107
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200108 ret = output->switch_mode(output, mode);
109 if (ret < 0)
110 return ret;
111
Alexander Larsson355748e2013-05-28 16:23:38 +0200112 output->scale = scale;
113
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200114 pixman_region32_init(&old_output_region);
115 pixman_region32_copy(&old_output_region, &output->region);
116
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200117 /* Update output region and transformation matrix */
Alexander Larsson0b135062013-05-28 16:23:36 +0200118 weston_output_transform_scale_init(output, output->transform, output->scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200119
120 pixman_region32_init(&output->previous_damage);
121 pixman_region32_init_rect(&output->region, output->x, output->y,
122 output->width, output->height);
123
124 weston_output_update_matrix(output);
125
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200126 /* If a pointer falls outside the outputs new geometry, move it to its
127 * lower-right corner */
128 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400129 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200130 int32_t x, y;
131
132 if (!pointer)
133 continue;
134
135 x = wl_fixed_to_int(pointer->x);
136 y = wl_fixed_to_int(pointer->y);
137
138 if (!pixman_region32_contains_point(&old_output_region,
139 x, y, NULL) ||
140 pixman_region32_contains_point(&output->region,
141 x, y, NULL))
142 continue;
143
144 if (x >= output->x + output->width)
145 x = output->x + output->width - 1;
146 if (y >= output->y + output->height)
147 y = output->y + output->height - 1;
148
149 pointer->x = wl_fixed_from_int(x);
150 pointer->y = wl_fixed_from_int(y);
151 }
152
153 pixman_region32_fini(&old_output_region);
154
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200155 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800156}
157
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400158WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500159weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400160{
161 wl_list_insert(&child_process_list, &process->link);
162}
163
Benjamin Franzke06286262011-05-06 19:12:33 +0200164static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200165child_client_exec(int sockfd, const char *path)
166{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500167 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200168 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200169 sigset_t allsigs;
170
171 /* do not give our signal mask to the new process */
172 sigfillset(&allsigs);
173 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200174
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500175 /* Launch clients as the user. */
176 seteuid(getuid());
177
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500178 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
179 * non-CLOEXEC fd to pass through exec. */
180 clientfd = dup(sockfd);
181 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200182 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500183 return;
184 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200185
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500186 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200187 setenv("WAYLAND_SOCKET", s, 1);
188
189 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200190 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200191 path);
192}
193
194WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500195weston_client_launch(struct weston_compositor *compositor,
196 struct weston_process *proc,
197 const char *path,
198 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200199{
200 int sv[2];
201 pid_t pid;
202 struct wl_client *client;
203
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300204 weston_log("launching '%s'\n", path);
205
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300206 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200207 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200208 "socketpair failed while launching '%s': %m\n",
209 path);
210 return NULL;
211 }
212
213 pid = fork();
214 if (pid == -1) {
215 close(sv[0]);
216 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200217 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200218 "fork failed while launching '%s': %m\n", path);
219 return NULL;
220 }
221
222 if (pid == 0) {
223 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700224 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200225 }
226
227 close(sv[1]);
228
229 client = wl_client_create(compositor->wl_display, sv[0]);
230 if (!client) {
231 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200232 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200233 "wl_client_create failed while launching '%s'.\n",
234 path);
235 return NULL;
236 }
237
238 proc->pid = pid;
239 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500240 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200241
242 return client;
243}
244
245static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300246surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
247{
248 struct weston_surface *surface =
249 container_of(listener, struct weston_surface,
250 pending.buffer_destroy_listener);
251
252 surface->pending.buffer = NULL;
253}
254
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200255static void
256empty_region(pixman_region32_t *region)
257{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300258 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200259 pixman_region32_init(region);
260}
261
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300262static void
263region_init_infinite(pixman_region32_t *region)
264{
265 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
266 UINT32_MAX, UINT32_MAX);
267}
268
Pekka Paalanene67858b2013-04-25 13:57:42 +0300269static struct weston_subsurface *
270weston_surface_to_subsurface(struct weston_surface *surface);
271
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500272WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500273weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500274{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500275 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400276
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200277 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400278 if (surface == NULL)
279 return NULL;
280
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500281 wl_signal_init(&surface->destroy_signal);
282
283 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500284
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500285 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500286 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500287
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500288 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400289 surface->alpha = 1.0;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200290 surface->ref_count = 1;
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));
Michael Fua2bb7912013-07-23 15:51:06 +0800991 if (seat->touch && seat->touch->focus == surface)
992 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +0100993 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500994
Kristian Høgsberg98238702012-08-03 16:29:12 -0400995 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500996}
997
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400998struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -0500999 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001000 struct wl_list link;
1001};
1002
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001003
1004WL_EXPORT void
1005weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001006{
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
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001010 if (--surface->ref_count > 0)
1011 return;
1012
1013 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1014
Pekka Paalanen483243f2013-03-08 14:56:50 +02001015 assert(wl_list_empty(&surface->geometry.child_list));
Pekka Paalanene67858b2013-04-25 13:57:42 +03001016 assert(wl_list_empty(&surface->subsurface_list_pending));
1017 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001018
Rob Bradford27b17932013-06-26 18:08:46 +01001019 if (weston_surface_is_mapped(surface)) {
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001020 weston_surface_unmap(surface);
Rob Bradford27b17932013-06-26 18:08:46 +01001021 weston_compositor_build_surface_list(compositor);
1022 }
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001023
Pekka Paalanenbc106382012-10-10 12:49:31 +03001024 wl_list_for_each_safe(cb, next,
1025 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001026 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001027
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001028 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001029 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001030 pixman_region32_fini(&surface->pending.damage);
1031
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001032 if (surface->pending.buffer)
1033 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1034
Pekka Paalanende685b82012-12-04 15:58:12 +02001035 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001036
Kristian Høgsberg42263852012-09-06 21:59:29 -04001037 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001038
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001039 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001040 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001041 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001042 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001043 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001044
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001045 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001046 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001047
Pekka Paalanen483243f2013-03-08 14:56:50 +02001048 weston_surface_set_transform_parent(surface, NULL);
1049
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001050 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001051}
1052
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001053static void
1054destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001055{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001056 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001057
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001058 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001059}
1060
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001061static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001062weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1063{
1064 struct weston_buffer *buffer =
1065 container_of(listener, struct weston_buffer, destroy_listener);
1066
1067 wl_signal_emit(&buffer->destroy_signal, buffer);
1068 free(buffer);
1069}
1070
1071struct weston_buffer *
1072weston_buffer_from_resource(struct wl_resource *resource)
1073{
1074 struct weston_buffer *buffer;
1075 struct wl_listener *listener;
1076
1077 listener = wl_resource_get_destroy_listener(resource,
1078 weston_buffer_destroy_handler);
1079
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001080 if (listener)
1081 return container_of(listener, struct weston_buffer,
1082 destroy_listener);
1083
1084 buffer = zalloc(sizeof *buffer);
1085 if (buffer == NULL)
1086 return NULL;
1087
1088 buffer->resource = resource;
1089 wl_signal_init(&buffer->destroy_signal);
1090 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
1091 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001092
1093 return buffer;
1094}
1095
1096static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001097weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1098 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001099{
Pekka Paalanende685b82012-12-04 15:58:12 +02001100 struct weston_buffer_reference *ref =
1101 container_of(listener, struct weston_buffer_reference,
1102 destroy_listener);
1103
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001104 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001105 ref->buffer = NULL;
1106}
1107
1108WL_EXPORT void
1109weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001110 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001111{
1112 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001113 ref->buffer->busy_count--;
1114 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001115 assert(wl_resource_get_client(ref->buffer->resource));
1116 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001117 WL_BUFFER_RELEASE);
1118 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001119 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001120 }
1121
Pekka Paalanende685b82012-12-04 15:58:12 +02001122 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001123 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001124 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001125 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001126 }
1127
Pekka Paalanende685b82012-12-04 15:58:12 +02001128 ref->buffer = buffer;
1129 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1130}
1131
1132static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001133weston_surface_attach(struct weston_surface *surface,
1134 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001135{
1136 weston_buffer_reference(&surface->buffer_ref, buffer);
1137
Pekka Paalanena6421c42012-12-04 15:58:10 +02001138 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001139 if (weston_surface_is_mapped(surface))
1140 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001141 }
1142
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001143 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001144}
1145
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001146WL_EXPORT void
1147weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001148{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001149 wl_list_remove(&surface->layer_link);
1150 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001151 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001152 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001153}
1154
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001155WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001156weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001157{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001158 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001159
1160 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001161 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001162}
1163
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001164WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001165weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001166{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001167 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001168
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001169 pixman_region32_union(&compositor->primary_plane.damage,
1170 &compositor->primary_plane.damage,
1171 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001172 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001173}
1174
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001175static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001176surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001177 pixman_region32_t *opaque)
1178{
Pekka Paalanende685b82012-12-04 15:58:12 +02001179 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001180 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001181 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001182
1183 if (surface->transform.enabled) {
1184 pixman_box32_t *extents;
1185
1186 extents = pixman_region32_extents(&surface->damage);
1187 surface_compute_bbox(surface, extents->x1, extents->y1,
1188 extents->x2 - extents->x1,
1189 extents->y2 - extents->y1,
1190 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001191 pixman_region32_translate(&surface->damage,
1192 -surface->plane->x,
1193 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001194 } else {
1195 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001196 surface->geometry.x - surface->plane->x,
1197 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001198 }
1199
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001200 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001201 pixman_region32_union(&surface->plane->damage,
1202 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001203 empty_region(&surface->damage);
1204 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001205 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001206}
1207
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001208static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001209compositor_accumulate_damage(struct weston_compositor *ec)
1210{
1211 struct weston_plane *plane;
1212 struct weston_surface *es;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001213 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001214
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001215 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001216
1217 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001218 pixman_region32_copy(&plane->clip, &clip);
1219
1220 pixman_region32_init(&opaque);
1221
1222 wl_list_for_each(es, &ec->surface_list, link) {
1223 if (es->plane != plane)
1224 continue;
1225
1226 surface_accumulate_damage(es, &opaque);
1227 }
1228
1229 pixman_region32_union(&clip, &clip, &opaque);
1230 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001231 }
1232
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001233 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001234
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001235 wl_list_for_each(es, &ec->surface_list, link) {
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001236 /* Both the renderer and the backend have seen the buffer
1237 * by now. If renderer needs the buffer, it has its own
1238 * reference set. If the backend wants to keep the buffer
1239 * around for migrating the surface into a non-primary plane
1240 * later, keep_buffer is true. Otherwise, drop the core
1241 * reference now, and allow early buffer release. This enables
1242 * clients to use single-buffering.
1243 */
1244 if (!es->keep_buffer)
1245 weston_buffer_reference(&es->buffer_ref, NULL);
1246 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001247}
1248
1249static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001250surface_list_add(struct weston_compositor *compositor,
1251 struct weston_surface *surface)
1252{
1253 struct weston_subsurface *sub;
1254
1255 if (wl_list_empty(&surface->subsurface_list)) {
1256 weston_surface_update_transform(surface);
1257 wl_list_insert(compositor->surface_list.prev, &surface->link);
1258 return;
1259 }
1260
1261 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1262 if (!weston_surface_is_mapped(sub->surface))
1263 continue;
1264
1265 if (sub->surface == surface) {
1266 weston_surface_update_transform(sub->surface);
1267 wl_list_insert(compositor->surface_list.prev,
1268 &sub->surface->link);
1269 } else {
1270 surface_list_add(compositor, sub->surface);
1271 }
1272 }
1273}
1274
1275static void
1276weston_compositor_build_surface_list(struct weston_compositor *compositor)
1277{
1278 struct weston_surface *surface;
1279 struct weston_layer *layer;
1280
1281 wl_list_init(&compositor->surface_list);
1282 wl_list_for_each(layer, &compositor->layer_list, link) {
1283 wl_list_for_each(surface, &layer->surface_list, layer_link) {
1284 surface_list_add(compositor, surface);
1285 }
1286 }
1287}
1288
1289static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001290weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001291{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001292 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001293 struct weston_surface *es;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001294 struct weston_animation *animation, *next;
1295 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001296 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001297 pixman_region32_t output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001298
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001299 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001300 weston_compositor_build_surface_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001301
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001302 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001303 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001304 else
1305 wl_list_for_each(es, &ec->surface_list, link)
1306 weston_surface_move_to_plane(es, &ec->primary_plane);
1307
Pekka Paalanene67858b2013-04-25 13:57:42 +03001308 wl_list_init(&frame_callback_list);
1309 wl_list_for_each(es, &ec->surface_list, link) {
1310 if (es->output == output) {
1311 wl_list_insert_list(&frame_callback_list,
1312 &es->frame_callback_list);
1313 wl_list_init(&es->frame_callback_list);
1314 }
1315 }
1316
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001317 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001318
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001319 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001320 pixman_region32_intersect(&output_damage,
1321 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001322 pixman_region32_subtract(&output_damage,
1323 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001324
Scott Moreauccbf29d2012-02-22 14:21:41 -07001325 if (output->dirty)
1326 weston_output_update_matrix(output);
1327
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001328 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001329
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001330 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001331
Kristian Høgsbergef044142011-06-21 15:02:12 -04001332 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001333
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001334 weston_compositor_repick(ec);
1335 wl_event_loop_dispatch(ec->input_loop, 0);
1336
Jonas Ådahldb773762012-06-13 00:01:21 +02001337 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001338 wl_callback_send_done(cb->resource, msecs);
1339 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001340 }
1341
Scott Moreaud64cf212012-06-08 19:40:54 -06001342 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001343 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001344 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001345 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001346}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001347
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001348static int
1349weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001350{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001351 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001352
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001353 wl_event_loop_dispatch(compositor->input_loop, 0);
1354
1355 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001356}
1357
1358WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001359weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001360{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001361 struct weston_compositor *compositor = output->compositor;
1362 struct wl_event_loop *loop =
1363 wl_display_get_event_loop(compositor->wl_display);
1364 int fd;
1365
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001366 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001367 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001368 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001369 return;
1370 }
1371
1372 output->repaint_scheduled = 0;
1373 if (compositor->input_loop_source)
1374 return;
1375
1376 fd = wl_event_loop_get_fd(compositor->input_loop);
1377 compositor->input_loop_source =
1378 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1379 weston_compositor_read_input, compositor);
1380}
1381
1382static void
1383idle_repaint(void *data)
1384{
1385 struct weston_output *output = data;
1386
Jonas Ådahle5a12252013-04-05 23:07:11 +02001387 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001388}
1389
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001390WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001391weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1392{
1393 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001394 if (below != NULL)
1395 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001396}
1397
1398WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001399weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001400{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001401 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001402 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001403
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001404 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1405 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001406 return;
1407
Kristian Høgsbergef044142011-06-21 15:02:12 -04001408 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001409 output->repaint_needed = 1;
1410 if (output->repaint_scheduled)
1411 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001412
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001413 wl_event_loop_add_idle(loop, idle_repaint, output);
1414 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001415
1416 if (compositor->input_loop_source) {
1417 wl_event_source_remove(compositor->input_loop_source);
1418 compositor->input_loop_source = NULL;
1419 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001420}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001421
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001422WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001423weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1424{
1425 struct weston_output *output;
1426
1427 wl_list_for_each(output, &compositor->output_list, link)
1428 weston_output_schedule_repaint(output);
1429}
1430
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001431static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001432surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001433{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001434 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001435}
1436
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001437static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001438surface_attach(struct wl_client *client,
1439 struct wl_resource *resource,
1440 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1441{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001442 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001443 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001444
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001445 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001446 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001447 if (buffer == NULL) {
1448 wl_client_post_no_memory(client);
1449 return;
1450 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001451 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001452
Pekka Paalanende685b82012-12-04 15:58:12 +02001453 /* Attach, attach, without commit in between does not send
1454 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001455 if (surface->pending.buffer)
1456 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001457
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001458 surface->pending.sx = sx;
1459 surface->pending.sy = sy;
1460 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001461 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001462 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001463 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001464 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001465 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001466}
1467
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001468static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001469surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001470 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001471 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001472{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001473 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001474
Pekka Paalanen8e159182012-10-10 12:49:25 +03001475 pixman_region32_union_rect(&surface->pending.damage,
1476 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001477 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001478}
1479
Kristian Høgsberg33418202011-08-16 23:01:28 -04001480static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001481destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001482{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001483 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001484
1485 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001486 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001487}
1488
1489static void
1490surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001491 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001492{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001493 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001494 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001495
1496 cb = malloc(sizeof *cb);
1497 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001498 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001499 return;
1500 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001501
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001502 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1503 callback);
1504 if (cb->resource == NULL) {
1505 free(cb);
1506 wl_resource_post_no_memory(resource);
1507 return;
1508 }
1509
Jason Ekstranda85118c2013-06-27 20:17:02 -05001510 wl_resource_set_implementation(cb->resource, NULL, cb,
1511 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001512
Pekka Paalanenbc106382012-10-10 12:49:31 +03001513 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001514}
1515
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001516static void
1517surface_set_opaque_region(struct wl_client *client,
1518 struct wl_resource *resource,
1519 struct wl_resource *region_resource)
1520{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001521 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001522 struct weston_region *region;
1523
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001524 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001525 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001526 pixman_region32_copy(&surface->pending.opaque,
1527 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001528 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001529 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001530 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001531}
1532
1533static void
1534surface_set_input_region(struct wl_client *client,
1535 struct wl_resource *resource,
1536 struct wl_resource *region_resource)
1537{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001538 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001539 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001540
1541 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001542 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001543 pixman_region32_copy(&surface->pending.input,
1544 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001545 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001546 pixman_region32_fini(&surface->pending.input);
1547 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001548 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001549}
1550
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001551static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001552weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001553{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001554 struct weston_subsurface *sub;
1555
1556 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1557 parent_link_pending) {
1558 wl_list_remove(&sub->parent_link);
1559 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1560 }
1561}
1562
1563static void
1564weston_surface_commit(struct weston_surface *surface)
1565{
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001566 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001567 int surface_width = 0;
1568 int surface_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001569
Alexander Larsson4ea95522013-05-22 14:41:37 +02001570 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001571 surface->buffer_transform = surface->pending.buffer_transform;
1572
Alexander Larsson4ea95522013-05-22 14:41:37 +02001573 /* wl_surface.set_buffer_scale */
1574 surface->buffer_scale = surface->pending.buffer_scale;
1575
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001576 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001577 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001578 weston_surface_attach(surface, surface->pending.buffer);
1579
Giulio Camuffo184df502013-02-21 11:29:21 +01001580 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001581 surface_width = weston_surface_buffer_width(surface);
1582 surface_height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001583 }
1584
1585 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001586 surface->configure(surface,
1587 surface->pending.sx, surface->pending.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001588 surface_width, surface_height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001589
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001590 if (surface->pending.buffer)
1591 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1592 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001593 surface->pending.sx = 0;
1594 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001595 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001596
1597 /* wl_surface.damage */
1598 pixman_region32_union(&surface->damage, &surface->damage,
1599 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001600 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1601 0, 0,
1602 surface->geometry.width,
1603 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001604 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001605
1606 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001607 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001608 surface->geometry.width,
1609 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001610 pixman_region32_intersect(&opaque,
1611 &opaque, &surface->pending.opaque);
1612
1613 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1614 pixman_region32_copy(&surface->opaque, &opaque);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001615 weston_surface_geometry_dirty(surface);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001616 }
1617
1618 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001619
1620 /* wl_surface.set_input_region */
1621 pixman_region32_fini(&surface->input);
1622 pixman_region32_init_rect(&surface->input, 0, 0,
1623 surface->geometry.width,
1624 surface->geometry.height);
1625 pixman_region32_intersect(&surface->input,
1626 &surface->input, &surface->pending.input);
1627
Pekka Paalanenbc106382012-10-10 12:49:31 +03001628 /* wl_surface.frame */
1629 wl_list_insert_list(&surface->frame_callback_list,
1630 &surface->pending.frame_callback_list);
1631 wl_list_init(&surface->pending.frame_callback_list);
1632
Pekka Paalanene67858b2013-04-25 13:57:42 +03001633 weston_surface_commit_subsurface_order(surface);
1634
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001635 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001636}
1637
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001638static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001639weston_subsurface_commit(struct weston_subsurface *sub);
1640
1641static void
1642weston_subsurface_parent_commit(struct weston_subsurface *sub,
1643 int parent_is_synchronized);
1644
1645static void
1646surface_commit(struct wl_client *client, struct wl_resource *resource)
1647{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001648 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001649 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1650
1651 if (sub) {
1652 weston_subsurface_commit(sub);
1653 return;
1654 }
1655
1656 weston_surface_commit(surface);
1657
1658 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1659 if (sub->surface != surface)
1660 weston_subsurface_parent_commit(sub, 0);
1661 }
1662}
1663
1664static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001665surface_set_buffer_transform(struct wl_client *client,
1666 struct wl_resource *resource, int transform)
1667{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001668 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001669
1670 surface->pending.buffer_transform = transform;
1671}
1672
Alexander Larsson4ea95522013-05-22 14:41:37 +02001673static void
1674surface_set_buffer_scale(struct wl_client *client,
1675 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001676 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02001677{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001678 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02001679
1680 surface->pending.buffer_scale = scale;
1681}
1682
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001683static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001684 surface_destroy,
1685 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001686 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001687 surface_frame,
1688 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001689 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001690 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001691 surface_set_buffer_transform,
1692 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001693};
1694
1695static void
1696compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001697 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001698{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04001699 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001700 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001701
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001702 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001703 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001704 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001705 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001706 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001707
Jason Ekstranda85118c2013-06-27 20:17:02 -05001708 surface->resource =
1709 wl_resource_create(client, &wl_surface_interface,
1710 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001711 if (surface->resource == NULL) {
1712 weston_surface_destroy(surface);
1713 wl_resource_post_no_memory(resource);
1714 return;
1715 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05001716 wl_resource_set_implementation(surface->resource, &surface_interface,
1717 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001718}
1719
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001720static void
1721destroy_region(struct wl_resource *resource)
1722{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001723 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001724
1725 pixman_region32_fini(&region->region);
1726 free(region);
1727}
1728
1729static void
1730region_destroy(struct wl_client *client, struct wl_resource *resource)
1731{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001732 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001733}
1734
1735static void
1736region_add(struct wl_client *client, struct wl_resource *resource,
1737 int32_t x, int32_t y, int32_t width, int32_t height)
1738{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001739 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001740
1741 pixman_region32_union_rect(&region->region, &region->region,
1742 x, y, width, height);
1743}
1744
1745static void
1746region_subtract(struct wl_client *client, struct wl_resource *resource,
1747 int32_t x, int32_t y, int32_t width, int32_t height)
1748{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001749 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001750 pixman_region32_t rect;
1751
1752 pixman_region32_init_rect(&rect, x, y, width, height);
1753 pixman_region32_subtract(&region->region, &region->region, &rect);
1754 pixman_region32_fini(&rect);
1755}
1756
1757static const struct wl_region_interface region_interface = {
1758 region_destroy,
1759 region_add,
1760 region_subtract
1761};
1762
1763static void
1764compositor_create_region(struct wl_client *client,
1765 struct wl_resource *resource, uint32_t id)
1766{
1767 struct weston_region *region;
1768
1769 region = malloc(sizeof *region);
1770 if (region == NULL) {
1771 wl_resource_post_no_memory(resource);
1772 return;
1773 }
1774
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001775 pixman_region32_init(&region->region);
1776
Jason Ekstranda85118c2013-06-27 20:17:02 -05001777 region->resource =
1778 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001779 if (region->resource == NULL) {
1780 free(region);
1781 wl_resource_post_no_memory(resource);
1782 return;
1783 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05001784 wl_resource_set_implementation(region->resource, &region_interface,
1785 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001786}
1787
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001788static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001789 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001790 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001791};
1792
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001793static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001794weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
1795{
1796 struct weston_surface *surface = sub->surface;
1797 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001798 int surface_width = 0;
1799 int surface_height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001800
Alexander Larsson4ea95522013-05-22 14:41:37 +02001801 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001802 surface->buffer_transform = sub->cached.buffer_transform;
1803
Alexander Larsson4ea95522013-05-22 14:41:37 +02001804 /* wl_surface.set_buffer_scale */
1805 surface->buffer_scale = sub->cached.buffer_scale;
1806
Pekka Paalanene67858b2013-04-25 13:57:42 +03001807 /* wl_surface.attach */
1808 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
1809 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
1810 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
1811
1812 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001813 surface_width = weston_surface_buffer_width(surface);
1814 surface_height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001815 }
1816
1817 if (surface->configure && sub->cached.newly_attached)
1818 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001819 surface_width, surface_height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001820 sub->cached.sx = 0;
1821 sub->cached.sy = 0;
1822 sub->cached.newly_attached = 0;
1823
1824 /* wl_surface.damage */
1825 pixman_region32_union(&surface->damage, &surface->damage,
1826 &sub->cached.damage);
1827 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1828 0, 0,
1829 surface->geometry.width,
1830 surface->geometry.height);
1831 empty_region(&sub->cached.damage);
1832
1833 /* wl_surface.set_opaque_region */
1834 pixman_region32_init_rect(&opaque, 0, 0,
1835 surface->geometry.width,
1836 surface->geometry.height);
1837 pixman_region32_intersect(&opaque,
1838 &opaque, &sub->cached.opaque);
1839
1840 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1841 pixman_region32_copy(&surface->opaque, &opaque);
1842 weston_surface_geometry_dirty(surface);
1843 }
1844
1845 pixman_region32_fini(&opaque);
1846
1847 /* wl_surface.set_input_region */
1848 pixman_region32_fini(&surface->input);
1849 pixman_region32_init_rect(&surface->input, 0, 0,
1850 surface->geometry.width,
1851 surface->geometry.height);
1852 pixman_region32_intersect(&surface->input,
1853 &surface->input, &sub->cached.input);
1854
1855 /* wl_surface.frame */
1856 wl_list_insert_list(&surface->frame_callback_list,
1857 &sub->cached.frame_callback_list);
1858 wl_list_init(&sub->cached.frame_callback_list);
1859
1860 weston_surface_commit_subsurface_order(surface);
1861
1862 weston_surface_schedule_repaint(surface);
1863
1864 sub->cached.has_data = 0;
1865}
1866
1867static void
1868weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
1869{
1870 struct weston_surface *surface = sub->surface;
1871
1872 /*
1873 * If this commit would cause the surface to move by the
1874 * attach(dx, dy) parameters, the old damage region must be
1875 * translated to correspond to the new surface coordinate system
1876 * origin.
1877 */
1878 pixman_region32_translate(&sub->cached.damage,
1879 -surface->pending.sx, -surface->pending.sy);
1880 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
1881 &surface->pending.damage);
1882 empty_region(&surface->pending.damage);
1883
1884 if (surface->pending.newly_attached) {
1885 sub->cached.newly_attached = 1;
1886 weston_buffer_reference(&sub->cached.buffer_ref,
1887 surface->pending.buffer);
1888 }
1889 sub->cached.sx += surface->pending.sx;
1890 sub->cached.sy += surface->pending.sy;
1891 surface->pending.sx = 0;
1892 surface->pending.sy = 0;
1893 surface->pending.newly_attached = 0;
1894
1895 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001896 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001897
1898 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
1899
1900 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
1901
1902 wl_list_insert_list(&sub->cached.frame_callback_list,
1903 &surface->pending.frame_callback_list);
1904 wl_list_init(&surface->pending.frame_callback_list);
1905
1906 sub->cached.has_data = 1;
1907}
1908
1909static int
1910weston_subsurface_is_synchronized(struct weston_subsurface *sub)
1911{
1912 while (sub) {
1913 if (sub->synchronized)
1914 return 1;
1915
1916 if (!sub->parent)
1917 return 0;
1918
1919 sub = weston_surface_to_subsurface(sub->parent);
1920 }
1921
1922 return 0;
1923}
1924
1925static void
1926weston_subsurface_commit(struct weston_subsurface *sub)
1927{
1928 struct weston_surface *surface = sub->surface;
1929 struct weston_subsurface *tmp;
1930
1931 /* Recursive check for effectively synchronized. */
1932 if (weston_subsurface_is_synchronized(sub)) {
1933 weston_subsurface_commit_to_cache(sub);
1934 } else {
1935 if (sub->cached.has_data) {
1936 /* flush accumulated state from cache */
1937 weston_subsurface_commit_to_cache(sub);
1938 weston_subsurface_commit_from_cache(sub);
1939 } else {
1940 weston_surface_commit(surface);
1941 }
1942
1943 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1944 if (tmp->surface != surface)
1945 weston_subsurface_parent_commit(tmp, 0);
1946 }
1947 }
1948}
1949
1950static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001951weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001952{
1953 struct weston_surface *surface = sub->surface;
1954 struct weston_subsurface *tmp;
1955
Pekka Paalanene67858b2013-04-25 13:57:42 +03001956 /* From now on, commit_from_cache the whole sub-tree, regardless of
1957 * the synchronized mode of each child. This sub-surface or some
1958 * of its ancestors were synchronized, so we are synchronized
1959 * all the way down.
1960 */
1961
1962 if (sub->cached.has_data)
1963 weston_subsurface_commit_from_cache(sub);
1964
1965 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1966 if (tmp->surface != surface)
1967 weston_subsurface_parent_commit(tmp, 1);
1968 }
1969}
1970
1971static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001972weston_subsurface_parent_commit(struct weston_subsurface *sub,
1973 int parent_is_synchronized)
1974{
1975 if (sub->position.set) {
1976 weston_surface_set_position(sub->surface,
1977 sub->position.x, sub->position.y);
1978 sub->position.set = 0;
1979 }
1980
1981 if (parent_is_synchronized || sub->synchronized)
1982 weston_subsurface_synchronized_commit(sub);
1983}
1984
1985static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001986subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
1987 int32_t width, int32_t height)
1988{
1989 struct weston_compositor *compositor = surface->compositor;
1990
1991 weston_surface_configure(surface,
1992 surface->geometry.x + dx,
1993 surface->geometry.y + dy,
1994 width, height);
1995
1996 /* No need to check parent mappedness, because if parent is not
1997 * mapped, parent is not in a visible layer, so this sub-surface
1998 * will not be drawn either.
1999 */
2000 if (!weston_surface_is_mapped(surface)) {
2001 wl_list_init(&surface->layer_link);
2002
2003 /* Cannot call weston_surface_update_transform(),
2004 * because that would call it also for the parent surface,
2005 * which might not be mapped yet. That would lead to
2006 * inconsistent state, where the window could never be
2007 * mapped.
2008 *
2009 * Instead just assing any output, to make
2010 * weston_surface_is_mapped() return true, so that when the
2011 * parent surface does get mapped, this one will get
2012 * included, too. See surface_list_add().
2013 */
2014 assert(!wl_list_empty(&compositor->output_list));
2015 surface->output = container_of(compositor->output_list.next,
2016 struct weston_output, link);
2017 }
2018}
2019
2020static struct weston_subsurface *
2021weston_surface_to_subsurface(struct weston_surface *surface)
2022{
2023 if (surface->configure == subsurface_configure)
2024 return surface->configure_private;
2025
2026 return NULL;
2027}
2028
Pekka Paalanen01388e22013-04-25 13:57:44 +03002029WL_EXPORT struct weston_surface *
2030weston_surface_get_main_surface(struct weston_surface *surface)
2031{
2032 struct weston_subsurface *sub;
2033
2034 while (surface && (sub = weston_surface_to_subsurface(surface)))
2035 surface = sub->parent;
2036
2037 return surface;
2038}
2039
Pekka Paalanene67858b2013-04-25 13:57:42 +03002040static void
2041subsurface_set_position(struct wl_client *client,
2042 struct wl_resource *resource, int32_t x, int32_t y)
2043{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002044 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002045
2046 if (!sub)
2047 return;
2048
2049 sub->position.x = x;
2050 sub->position.y = y;
2051 sub->position.set = 1;
2052}
2053
2054static struct weston_subsurface *
2055subsurface_from_surface(struct weston_surface *surface)
2056{
2057 struct weston_subsurface *sub;
2058
2059 sub = weston_surface_to_subsurface(surface);
2060 if (sub)
2061 return sub;
2062
2063 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2064 if (sub->surface == surface)
2065 return sub;
2066
2067 return NULL;
2068}
2069
2070static struct weston_subsurface *
2071subsurface_sibling_check(struct weston_subsurface *sub,
2072 struct weston_surface *surface,
2073 const char *request)
2074{
2075 struct weston_subsurface *sibling;
2076
2077 sibling = subsurface_from_surface(surface);
2078
2079 if (!sibling) {
2080 wl_resource_post_error(sub->resource,
2081 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2082 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002083 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002084 return NULL;
2085 }
2086
2087 if (sibling->parent != sub->parent) {
2088 wl_resource_post_error(sub->resource,
2089 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2090 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002091 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002092 return NULL;
2093 }
2094
2095 return sibling;
2096}
2097
2098static void
2099subsurface_place_above(struct wl_client *client,
2100 struct wl_resource *resource,
2101 struct wl_resource *sibling_resource)
2102{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002103 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002104 struct weston_surface *surface =
2105 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002106 struct weston_subsurface *sibling;
2107
2108 if (!sub)
2109 return;
2110
2111 sibling = subsurface_sibling_check(sub, surface, "place_above");
2112 if (!sibling)
2113 return;
2114
2115 wl_list_remove(&sub->parent_link_pending);
2116 wl_list_insert(sibling->parent_link_pending.prev,
2117 &sub->parent_link_pending);
2118}
2119
2120static void
2121subsurface_place_below(struct wl_client *client,
2122 struct wl_resource *resource,
2123 struct wl_resource *sibling_resource)
2124{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002125 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002126 struct weston_surface *surface =
2127 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002128 struct weston_subsurface *sibling;
2129
2130 if (!sub)
2131 return;
2132
2133 sibling = subsurface_sibling_check(sub, surface, "place_below");
2134 if (!sibling)
2135 return;
2136
2137 wl_list_remove(&sub->parent_link_pending);
2138 wl_list_insert(&sibling->parent_link_pending,
2139 &sub->parent_link_pending);
2140}
2141
2142static void
2143subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2144{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002145 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002146
2147 if (sub)
2148 sub->synchronized = 1;
2149}
2150
2151static void
2152subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2153{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002154 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002155
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002156 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002157 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002158
2159 /* If sub became effectively desynchronized, flush. */
2160 if (!weston_subsurface_is_synchronized(sub))
2161 weston_subsurface_synchronized_commit(sub);
2162 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002163}
2164
2165static void
2166weston_subsurface_cache_init(struct weston_subsurface *sub)
2167{
2168 pixman_region32_init(&sub->cached.damage);
2169 pixman_region32_init(&sub->cached.opaque);
2170 pixman_region32_init(&sub->cached.input);
2171 wl_list_init(&sub->cached.frame_callback_list);
2172 sub->cached.buffer_ref.buffer = NULL;
2173}
2174
2175static void
2176weston_subsurface_cache_fini(struct weston_subsurface *sub)
2177{
2178 struct weston_frame_callback *cb, *tmp;
2179
2180 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002181 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002182
2183 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2184 pixman_region32_fini(&sub->cached.damage);
2185 pixman_region32_fini(&sub->cached.opaque);
2186 pixman_region32_fini(&sub->cached.input);
2187}
2188
2189static void
2190weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2191{
2192 wl_list_remove(&sub->parent_link);
2193 wl_list_remove(&sub->parent_link_pending);
2194 wl_list_remove(&sub->parent_destroy_listener.link);
2195 sub->parent = NULL;
2196}
2197
2198static void
2199weston_subsurface_destroy(struct weston_subsurface *sub);
2200
2201static void
2202subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2203{
2204 struct weston_subsurface *sub =
2205 container_of(listener, struct weston_subsurface,
2206 surface_destroy_listener);
2207 assert(data == &sub->surface->resource);
2208
2209 /* The protocol object (wl_resource) is left inert. */
2210 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002211 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002212
2213 weston_subsurface_destroy(sub);
2214}
2215
2216static void
2217subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2218{
2219 struct weston_subsurface *sub =
2220 container_of(listener, struct weston_subsurface,
2221 parent_destroy_listener);
2222 assert(data == &sub->parent->resource);
2223 assert(sub->surface != sub->parent);
2224
2225 if (weston_surface_is_mapped(sub->surface))
2226 weston_surface_unmap(sub->surface);
2227
2228 weston_subsurface_unlink_parent(sub);
2229}
2230
2231static void
2232subsurface_resource_destroy(struct wl_resource *resource)
2233{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002234 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002235
2236 if (sub)
2237 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002238}
2239
2240static void
2241subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2242{
2243 wl_resource_destroy(resource);
2244}
2245
2246static void
2247weston_subsurface_link_parent(struct weston_subsurface *sub,
2248 struct weston_surface *parent)
2249{
2250 sub->parent = parent;
2251 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002252 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002253 &sub->parent_destroy_listener);
2254
2255 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2256 wl_list_insert(&parent->subsurface_list_pending,
2257 &sub->parent_link_pending);
2258}
2259
2260static void
2261weston_subsurface_link_surface(struct weston_subsurface *sub,
2262 struct weston_surface *surface)
2263{
2264 sub->surface = surface;
2265 sub->surface_destroy_listener.notify =
2266 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002267 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002268 &sub->surface_destroy_listener);
2269}
2270
2271static void
2272weston_subsurface_destroy(struct weston_subsurface *sub)
2273{
2274 assert(sub->surface);
2275
2276 if (sub->resource) {
2277 assert(weston_surface_to_subsurface(sub->surface) == sub);
2278 assert(sub->parent_destroy_listener.notify ==
2279 subsurface_handle_parent_destroy);
2280
2281 weston_surface_set_transform_parent(sub->surface, NULL);
2282 if (sub->parent)
2283 weston_subsurface_unlink_parent(sub);
2284
2285 weston_subsurface_cache_fini(sub);
2286
2287 sub->surface->configure = NULL;
2288 sub->surface->configure_private = NULL;
2289 } else {
2290 /* the dummy weston_subsurface for the parent itself */
2291 assert(sub->parent_destroy_listener.notify == NULL);
2292 wl_list_remove(&sub->parent_link);
2293 wl_list_remove(&sub->parent_link_pending);
2294 }
2295
2296 wl_list_remove(&sub->surface_destroy_listener.link);
2297 free(sub);
2298}
2299
2300static const struct wl_subsurface_interface subsurface_implementation = {
2301 subsurface_destroy,
2302 subsurface_set_position,
2303 subsurface_place_above,
2304 subsurface_place_below,
2305 subsurface_set_sync,
2306 subsurface_set_desync
2307};
2308
2309static struct weston_subsurface *
2310weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2311 struct weston_surface *parent)
2312{
2313 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002314 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002315
2316 sub = calloc(1, sizeof *sub);
2317 if (!sub)
2318 return NULL;
2319
Jason Ekstranda85118c2013-06-27 20:17:02 -05002320 sub->resource =
2321 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002322 if (!sub->resource) {
2323 free(sub);
2324 return NULL;
2325 }
2326
Jason Ekstranda85118c2013-06-27 20:17:02 -05002327 wl_resource_set_implementation(sub->resource,
2328 &subsurface_implementation,
2329 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002330 weston_subsurface_link_surface(sub, surface);
2331 weston_subsurface_link_parent(sub, parent);
2332 weston_subsurface_cache_init(sub);
2333 sub->synchronized = 1;
2334 weston_surface_set_transform_parent(surface, parent);
2335
2336 return sub;
2337}
2338
2339/* Create a dummy subsurface for having the parent itself in its
2340 * sub-surface lists. Makes stacking order manipulation easy.
2341 */
2342static struct weston_subsurface *
2343weston_subsurface_create_for_parent(struct weston_surface *parent)
2344{
2345 struct weston_subsurface *sub;
2346
2347 sub = calloc(1, sizeof *sub);
2348 if (!sub)
2349 return NULL;
2350
2351 weston_subsurface_link_surface(sub, parent);
2352 sub->parent = parent;
2353 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2354 wl_list_insert(&parent->subsurface_list_pending,
2355 &sub->parent_link_pending);
2356
2357 return sub;
2358}
2359
2360static void
2361subcompositor_get_subsurface(struct wl_client *client,
2362 struct wl_resource *resource,
2363 uint32_t id,
2364 struct wl_resource *surface_resource,
2365 struct wl_resource *parent_resource)
2366{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002367 struct weston_surface *surface =
2368 wl_resource_get_user_data(surface_resource);
2369 struct weston_surface *parent =
2370 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002371 struct weston_subsurface *sub;
2372 static const char where[] = "get_subsurface: wl_subsurface@";
2373
2374 if (surface == parent) {
2375 wl_resource_post_error(resource,
2376 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2377 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002378 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002379 return;
2380 }
2381
2382 if (weston_surface_to_subsurface(surface)) {
2383 wl_resource_post_error(resource,
2384 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2385 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002386 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002387 return;
2388 }
2389
2390 if (surface->configure) {
2391 wl_resource_post_error(resource,
2392 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2393 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002394 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002395 return;
2396 }
2397
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002398 if (weston_surface_get_main_surface(parent) == surface) {
2399 wl_resource_post_error(resource,
2400 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2401 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002402 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002403 return;
2404 }
2405
Pekka Paalanene67858b2013-04-25 13:57:42 +03002406 /* make sure the parent is in its own list */
2407 if (wl_list_empty(&parent->subsurface_list)) {
2408 if (!weston_subsurface_create_for_parent(parent)) {
2409 wl_resource_post_no_memory(resource);
2410 return;
2411 }
2412 }
2413
2414 sub = weston_subsurface_create(id, surface, parent);
2415 if (!sub) {
2416 wl_resource_post_no_memory(resource);
2417 return;
2418 }
2419
2420 surface->configure = subsurface_configure;
2421 surface->configure_private = sub;
2422}
2423
2424static void
2425subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2426{
2427 wl_resource_destroy(resource);
2428}
2429
2430static const struct wl_subcompositor_interface subcompositor_interface = {
2431 subcompositor_destroy,
2432 subcompositor_get_subsurface
2433};
2434
2435static void
2436bind_subcompositor(struct wl_client *client,
2437 void *data, uint32_t version, uint32_t id)
2438{
2439 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002440 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002441
Jason Ekstranda85118c2013-06-27 20:17:02 -05002442 resource =
2443 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002444 if (resource == NULL) {
2445 wl_client_post_no_memory(client);
2446 return;
2447 }
2448 wl_resource_set_implementation(resource, &subcompositor_interface,
2449 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002450}
2451
2452static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002453weston_compositor_dpms(struct weston_compositor *compositor,
2454 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002455{
2456 struct weston_output *output;
2457
2458 wl_list_for_each(output, &compositor->output_list, link)
2459 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002460 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002461}
2462
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002463WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002464weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002465{
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002466 switch (compositor->state) {
2467 case WESTON_COMPOSITOR_SLEEPING:
2468 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2469 /* fall through */
2470 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002471 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002472 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002473 /* fall through */
2474 default:
2475 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2476 wl_event_source_timer_update(compositor->idle_source,
2477 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002478 }
2479}
2480
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002481WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002482weston_compositor_offscreen(struct weston_compositor *compositor)
2483{
2484 switch (compositor->state) {
2485 case WESTON_COMPOSITOR_OFFSCREEN:
2486 return;
2487 case WESTON_COMPOSITOR_SLEEPING:
2488 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2489 /* fall through */
2490 default:
2491 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2492 wl_event_source_timer_update(compositor->idle_source, 0);
2493 }
2494}
2495
2496WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002497weston_compositor_sleep(struct weston_compositor *compositor)
2498{
2499 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2500 return;
2501
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002502 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002503 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2504 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2505}
2506
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002507static int
2508idle_handler(void *data)
2509{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002510 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002511
2512 if (compositor->idle_inhibit)
2513 return 1;
2514
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002515 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002516 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002517
2518 return 1;
2519}
2520
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002521WL_EXPORT void
2522weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2523{
2524 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002525 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002526 plane->x = x;
2527 plane->y = y;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002528
2529 /* Init the link so that the call to wl_list_remove() when releasing
2530 * the plane without ever stacking doesn't lead to a crash */
2531 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002532}
2533
2534WL_EXPORT void
2535weston_plane_release(struct weston_plane *plane)
2536{
2537 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002538 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002539
2540 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002541}
2542
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002543WL_EXPORT void
2544weston_compositor_stack_plane(struct weston_compositor *ec,
2545 struct weston_plane *plane,
2546 struct weston_plane *above)
2547{
2548 if (above)
2549 wl_list_insert(above->link.prev, &plane->link);
2550 else
2551 wl_list_insert(&ec->plane_list, &plane->link);
2552}
2553
Casey Dahlin9074db52012-04-19 22:50:09 -04002554static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002555{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002556 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002557}
2558
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002559static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002560bind_output(struct wl_client *client,
2561 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002562{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002563 struct weston_output *output = data;
2564 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002565 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002566
Jason Ekstranda85118c2013-06-27 20:17:02 -05002567 resource = wl_resource_create(client, &wl_output_interface,
2568 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002569 if (resource == NULL) {
2570 wl_client_post_no_memory(client);
2571 return;
2572 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002573
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002574 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002575 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002576
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002577 wl_output_send_geometry(resource,
2578 output->x,
2579 output->y,
2580 output->mm_width,
2581 output->mm_height,
2582 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002583 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002584 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002585 if (version >= 2)
2586 wl_output_send_scale(resource,
2587 output->scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002588
2589 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002590 wl_output_send_mode(resource,
2591 mode->flags,
2592 mode->width,
2593 mode->height,
2594 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002595 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002596
2597 if (version >= 2)
2598 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002599}
2600
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002601WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002602weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002603{
Richard Hughes64ddde12013-05-01 21:52:10 +01002604 wl_signal_emit(&output->destroy_signal, output);
2605
Richard Hughesafe690c2013-05-02 10:10:04 +01002606 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002607 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002608 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002609 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002610
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002611 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002612}
2613
Scott Moreau1bad5db2012-08-18 01:04:05 -06002614static void
2615weston_output_compute_transform(struct weston_output *output)
2616{
2617 struct weston_matrix transform;
2618 int flip;
2619
2620 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002621 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002622
2623 switch(output->transform) {
2624 case WL_OUTPUT_TRANSFORM_FLIPPED:
2625 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2626 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2627 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002628 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002629 flip = -1;
2630 break;
2631 default:
2632 flip = 1;
2633 break;
2634 }
2635
2636 switch(output->transform) {
2637 case WL_OUTPUT_TRANSFORM_NORMAL:
2638 case WL_OUTPUT_TRANSFORM_FLIPPED:
2639 transform.d[0] = flip;
2640 transform.d[1] = 0;
2641 transform.d[4] = 0;
2642 transform.d[5] = 1;
2643 break;
2644 case WL_OUTPUT_TRANSFORM_90:
2645 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2646 transform.d[0] = 0;
2647 transform.d[1] = -flip;
2648 transform.d[4] = 1;
2649 transform.d[5] = 0;
2650 break;
2651 case WL_OUTPUT_TRANSFORM_180:
2652 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2653 transform.d[0] = -flip;
2654 transform.d[1] = 0;
2655 transform.d[4] = 0;
2656 transform.d[5] = -1;
2657 break;
2658 case WL_OUTPUT_TRANSFORM_270:
2659 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2660 transform.d[0] = 0;
2661 transform.d[1] = flip;
2662 transform.d[4] = -1;
2663 transform.d[5] = 0;
2664 break;
2665 default:
2666 break;
2667 }
2668
2669 weston_matrix_multiply(&output->matrix, &transform);
2670}
2671
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002672WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002673weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002674{
Scott Moreau850ca422012-05-21 15:21:25 -06002675 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002676 struct weston_matrix camera;
2677 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002678
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002679 weston_matrix_init(&output->matrix);
2680 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002681 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2682 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002683
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002684 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002685 2.0 / (output->width + output->border.left + output->border.right),
2686 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2687
2688 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002689
Scott Moreauccbf29d2012-02-22 14:21:41 -07002690 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002691 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002692 weston_matrix_init(&camera);
2693 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002694 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002695 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002696 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002697 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002698 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002699 weston_matrix_multiply(&output->matrix, &modelview);
2700 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002701
Scott Moreauccbf29d2012-02-22 14:21:41 -07002702 output->dirty = 0;
2703}
2704
Scott Moreau1bad5db2012-08-18 01:04:05 -06002705static void
Alexander Larsson0b135062013-05-28 16:23:36 +02002706weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002707{
2708 output->transform = transform;
2709
2710 switch (transform) {
2711 case WL_OUTPUT_TRANSFORM_90:
2712 case WL_OUTPUT_TRANSFORM_270:
2713 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2714 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2715 /* Swap width and height */
2716 output->width = output->current->height;
2717 output->height = output->current->width;
2718 break;
2719 case WL_OUTPUT_TRANSFORM_NORMAL:
2720 case WL_OUTPUT_TRANSFORM_180:
2721 case WL_OUTPUT_TRANSFORM_FLIPPED:
2722 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2723 output->width = output->current->width;
2724 output->height = output->current->height;
2725 break;
2726 default:
2727 break;
2728 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06002729
Alexander Larsson4ea95522013-05-22 14:41:37 +02002730 output->scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02002731 output->width /= scale;
2732 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002733}
2734
Scott Moreauccbf29d2012-02-22 14:21:41 -07002735WL_EXPORT void
2736weston_output_move(struct weston_output *output, int x, int y)
2737{
2738 output->x = x;
2739 output->y = y;
2740
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002741 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002742 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002743 output->width,
2744 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002745}
2746
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002747WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002748weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02002749 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002750 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002751{
2752 output->compositor = c;
2753 output->x = x;
2754 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002755 output->border.top = 0;
2756 output->border.bottom = 0;
2757 output->border.left = 0;
2758 output->border.right = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02002759 output->mm_width = mm_width;
2760 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002761 output->dirty = 1;
Alexander Larssone32c3762013-05-28 16:23:37 +02002762 output->origin_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002763
Alexander Larsson0b135062013-05-28 16:23:36 +02002764 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06002765 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002766
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002767 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002768 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002769
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002770 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01002771 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002772 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002773 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002774
Casey Dahlin58ba1372012-04-19 22:50:08 -04002775 output->id = ffs(~output->compositor->output_id_pool) - 1;
2776 output->compositor->output_id_pool |= 1 << output->id;
2777
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002778 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002779 wl_global_create(c->wl_display, &wl_output_interface, 2,
2780 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01002781 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002782}
2783
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002784WL_EXPORT void
2785weston_output_transform_coordinate(struct weston_output *output,
2786 int device_x, int device_y,
2787 wl_fixed_t *x, wl_fixed_t *y)
2788{
2789 wl_fixed_t tx, ty;
2790 wl_fixed_t width, height;
2791
2792 width = wl_fixed_from_int(output->width * output->scale - 1);
2793 height = wl_fixed_from_int(output->height * output->scale - 1);
2794
2795 switch(output->transform) {
2796 case WL_OUTPUT_TRANSFORM_NORMAL:
2797 default:
2798 tx = wl_fixed_from_int(device_x);
2799 ty = wl_fixed_from_int(device_y);
2800 break;
2801 case WL_OUTPUT_TRANSFORM_90:
2802 tx = wl_fixed_from_int(device_y);
2803 ty = height - wl_fixed_from_int(device_x);
2804 break;
2805 case WL_OUTPUT_TRANSFORM_180:
2806 tx = width - wl_fixed_from_int(device_x);
2807 ty = height - wl_fixed_from_int(device_y);
2808 break;
2809 case WL_OUTPUT_TRANSFORM_270:
2810 tx = width - wl_fixed_from_int(device_y);
2811 ty = wl_fixed_from_int(device_x);
2812 break;
2813 case WL_OUTPUT_TRANSFORM_FLIPPED:
2814 tx = width - wl_fixed_from_int(device_x);
2815 ty = wl_fixed_from_int(device_y);
2816 break;
2817 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2818 tx = width - wl_fixed_from_int(device_y);
2819 ty = height - wl_fixed_from_int(device_x);
2820 break;
2821 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2822 tx = wl_fixed_from_int(device_x);
2823 ty = height - wl_fixed_from_int(device_y);
2824 break;
2825 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2826 tx = wl_fixed_from_int(device_y);
2827 ty = wl_fixed_from_int(device_x);
2828 break;
2829 }
2830
2831 *x = tx / output->scale + wl_fixed_from_int(output->x);
2832 *y = ty / output->scale + wl_fixed_from_int(output->y);
2833}
2834
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002835static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002836compositor_bind(struct wl_client *client,
2837 void *data, uint32_t version, uint32_t id)
2838{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002839 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002840 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002841
Jason Ekstranda85118c2013-06-27 20:17:02 -05002842 resource = wl_resource_create(client, &wl_compositor_interface,
2843 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002844 if (resource == NULL) {
2845 wl_client_post_no_memory(client);
2846 return;
2847 }
2848
2849 wl_resource_set_implementation(resource, &compositor_interface,
2850 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05002851}
2852
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002853static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002854log_uname(void)
2855{
2856 struct utsname usys;
2857
2858 uname(&usys);
2859
2860 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2861 usys.version, usys.machine);
2862}
2863
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002864WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02002865weston_environment_get_fd(const char *env)
2866{
2867 char *e, *end;
2868 int fd, flags;
2869
2870 e = getenv(env);
2871 if (!e)
2872 return -1;
2873 fd = strtol(e, &end, 0);
2874 if (*end != '\0')
2875 return -1;
2876
2877 flags = fcntl(fd, F_GETFD);
2878 if (flags == -1)
2879 return -1;
2880
2881 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
2882 unsetenv(env);
2883
2884 return fd;
2885}
2886
2887WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002888weston_compositor_init(struct weston_compositor *ec,
2889 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002890 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002891 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002892{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002893 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002894 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002895 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002896
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002897 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002898 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002899 wl_signal_init(&ec->destroy_signal);
2900 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002901 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002902 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002903 wl_signal_init(&ec->idle_signal);
2904 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002905 wl_signal_init(&ec->show_input_panel_signal);
2906 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02002907 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002908 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01002909 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002910
Casey Dahlin58ba1372012-04-19 22:50:08 -04002911 ec->output_id_pool = 0;
2912
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002913 if (!wl_global_create(display, &wl_compositor_interface, 3,
2914 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05002915 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002916
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002917 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
2918 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03002919 return -1;
2920
Daniel Stone725c2c32012-06-22 14:04:36 +01002921 wl_list_init(&ec->surface_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002922 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002923 wl_list_init(&ec->layer_list);
2924 wl_list_init(&ec->seat_list);
2925 wl_list_init(&ec->output_list);
2926 wl_list_init(&ec->key_binding_list);
2927 wl_list_init(&ec->button_binding_list);
2928 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002929 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002930
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002931 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002932 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002933
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002934 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
2935 weston_config_section_get_string(s, "keymap_rules",
2936 (char **) &xkb_names.rules, NULL);
2937 weston_config_section_get_string(s, "keymap_model",
2938 (char **) &xkb_names.model, NULL);
2939 weston_config_section_get_string(s, "keymap_layout",
2940 (char **) &xkb_names.layout, NULL);
2941 weston_config_section_get_string(s, "keymap_variant",
2942 (char **) &xkb_names.variant, NULL);
2943 weston_config_section_get_string(s, "keymap_options",
2944 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01002945
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002946 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
2947 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01002948
2949 ec->ping_handler = NULL;
2950
2951 screenshooter_create(ec);
2952 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002953 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002954
2955 wl_data_device_manager_init(ec->wl_display);
2956
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002957 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002958
Daniel Stone725c2c32012-06-22 14:04:36 +01002959 loop = wl_display_get_event_loop(ec->wl_display);
2960 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2961 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2962
2963 ec->input_loop = wl_event_loop_create();
2964
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002965 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2966 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2967
2968 weston_compositor_schedule_repaint(ec);
2969
Daniel Stone725c2c32012-06-22 14:04:36 +01002970 return 0;
2971}
2972
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002973WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002974weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002975{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002976 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002977
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002978 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002979 if (ec->input_loop_source)
2980 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002981
Matt Roper361d2ad2011-08-29 13:52:23 -07002982 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002983 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002984 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002985
Daniel Stone325fc2d2012-05-30 16:31:58 +01002986 weston_binding_list_destroy_all(&ec->key_binding_list);
2987 weston_binding_list_destroy_all(&ec->button_binding_list);
2988 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002989 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002990
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002991 weston_plane_release(&ec->primary_plane);
2992
Jonas Ådahlc97af922012-03-28 22:36:09 +02002993 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07002994
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002995 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07002996}
2997
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05002998WL_EXPORT void
2999weston_version(int *major, int *minor, int *micro)
3000{
3001 *major = WESTON_VERSION_MAJOR;
3002 *minor = WESTON_VERSION_MINOR;
3003 *micro = WESTON_VERSION_MICRO;
3004}
3005
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003006static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003007 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003008 const char *desc;
3009} capability_strings[] = {
3010 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003011 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003012};
3013
3014static void
3015weston_compositor_log_capabilities(struct weston_compositor *compositor)
3016{
3017 unsigned i;
3018 int yes;
3019
3020 weston_log("Compositor capabilities:\n");
3021 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3022 yes = compositor->capabilities & capability_strings[i].bit;
3023 weston_log_continue(STAMP_SPACE "%s %s\n",
3024 capability_strings[i].desc,
3025 yes ? "yes" : "no");
3026 }
3027}
3028
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003029static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003030{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003031 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003032
Martin Minarik6d118362012-06-07 18:01:59 +02003033 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003034 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003035
3036 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003037}
3038
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003039#ifdef HAVE_LIBUNWIND
3040
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003041static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003042print_backtrace(void)
3043{
3044 unw_cursor_t cursor;
3045 unw_context_t context;
3046 unw_word_t off;
3047 unw_proc_info_t pip;
3048 int ret, i = 0;
3049 char procname[256];
3050 const char *filename;
3051 Dl_info dlinfo;
3052
3053 pip.unwind_info = NULL;
3054 ret = unw_getcontext(&context);
3055 if (ret) {
3056 weston_log("unw_getcontext: %d\n", ret);
3057 return;
3058 }
3059
3060 ret = unw_init_local(&cursor, &context);
3061 if (ret) {
3062 weston_log("unw_init_local: %d\n", ret);
3063 return;
3064 }
3065
3066 ret = unw_step(&cursor);
3067 while (ret > 0) {
3068 ret = unw_get_proc_info(&cursor, &pip);
3069 if (ret) {
3070 weston_log("unw_get_proc_info: %d\n", ret);
3071 break;
3072 }
3073
3074 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3075 if (ret && ret != -UNW_ENOMEM) {
3076 if (ret != -UNW_EUNSPEC)
3077 weston_log("unw_get_proc_name: %d\n", ret);
3078 procname[0] = '?';
3079 procname[1] = 0;
3080 }
3081
3082 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3083 *dlinfo.dli_fname)
3084 filename = dlinfo.dli_fname;
3085 else
3086 filename = "?";
3087
3088 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3089 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3090
3091 ret = unw_step(&cursor);
3092 if (ret < 0)
3093 weston_log("unw_step: %d\n", ret);
3094 }
3095}
3096
3097#else
3098
3099static void
3100print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003101{
3102 void *buffer[32];
3103 int i, count;
3104 Dl_info info;
3105
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003106 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3107 for (i = 0; i < count; i++) {
3108 dladdr(buffer[i], &info);
3109 weston_log(" [%016lx] %s (%s)\n",
3110 (long) buffer[i],
3111 info.dli_sname ? info.dli_sname : "--",
3112 info.dli_fname);
3113 }
3114}
3115
3116#endif
3117
3118static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003119on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003120{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003121 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003122 * then call the backend restore function, which will switch
3123 * back to the vt we launched from or ungrab X etc and then
3124 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003125 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003126 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003127 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003128
Peter Maatmane5b42e42013-03-27 22:38:53 +01003129 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003130
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003131 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003132
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003133 segv_compositor->restore(segv_compositor);
3134
3135 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003136}
3137
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003138static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003139load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003140{
3141 char path[PATH_MAX];
3142 void *module, *init;
3143
3144 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003145 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003146 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003147 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003148
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003149 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3150 if (module) {
3151 weston_log("Module '%s' already loaded\n", path);
3152 dlclose(module);
3153 return NULL;
3154 }
3155
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003156 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003157 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003158 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003159 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003160 return NULL;
3161 }
3162
3163 init = dlsym(module, entrypoint);
3164 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003165 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003166 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003167 return NULL;
3168 }
3169
3170 return init;
3171}
3172
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003173static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003174load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003175 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003176{
3177 const char *p, *end;
3178 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003179 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003180 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003181
3182 if (modules == NULL)
3183 return 0;
3184
3185 p = modules;
3186 while (*p) {
3187 end = strchrnul(p, ',');
3188 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3189 module_init = load_module(buffer, "module_init");
3190 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003191 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003192 p = end;
3193 while (*p == ',')
3194 p++;
3195
3196 }
3197
3198 return 0;
3199}
3200
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003201static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003202 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3203
3204static const char xdg_wrong_message[] =
3205 "fatal: environment variable XDG_RUNTIME_DIR\n"
3206 "is set to \"%s\", which is not a directory.\n";
3207
3208static const char xdg_wrong_mode_message[] =
3209 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3210 "correctly. Unix access mode must be 0700 but is %o,\n"
3211 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003212 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003213
3214static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003215 "Refer to your distribution on how to get it, or\n"
3216 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3217 "on how to implement it.\n";
3218
Martin Minarik37032f82012-06-18 20:15:18 +02003219static void
3220verify_xdg_runtime_dir(void)
3221{
3222 char *dir = getenv("XDG_RUNTIME_DIR");
3223 struct stat s;
3224
3225 if (!dir) {
3226 weston_log(xdg_error_message);
3227 weston_log_continue(xdg_detail_message);
3228 exit(EXIT_FAILURE);
3229 }
3230
3231 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3232 weston_log(xdg_wrong_message, dir);
3233 weston_log_continue(xdg_detail_message);
3234 exit(EXIT_FAILURE);
3235 }
3236
3237 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3238 weston_log(xdg_wrong_mode_message,
3239 dir, s.st_mode & 0777, s.st_uid);
3240 weston_log_continue(xdg_detail_message);
3241 }
3242}
3243
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003244static int
3245usage(int error_code)
3246{
3247 fprintf(stderr,
3248 "Usage: weston [OPTIONS]\n\n"
3249 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3250 "Weston supports multiple backends, and depending on which backend is in use\n"
3251 "different options will be accepted.\n\n"
3252
3253
3254 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003255 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003256 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003257 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3258 "\t\t\t\twayland-backend.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003259 " -S, --socket=NAME\tName of socket to listen on\n"
3260 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003261 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003262 " --log==FILE\t\tLog to the given file\n"
3263 " -h, --help\t\tThis help message\n\n");
3264
3265 fprintf(stderr,
3266 "Options for drm-backend.so:\n\n"
3267 " --connector=ID\tBring up only this connector\n"
3268 " --seat=SEAT\t\tThe seat that weston should run on\n"
3269 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003270 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003271 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3272
3273 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003274 "Options for fbdev-backend.so:\n\n"
3275 " --tty=TTY\t\tThe tty to use\n"
3276 " --device=DEVICE\tThe framebuffer device to use\n\n");
3277
3278 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003279 "Options for x11-backend.so:\n\n"
3280 " --width=WIDTH\t\tWidth of X window\n"
3281 " --height=HEIGHT\tHeight of X window\n"
3282 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003283 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003284 " --output-count=COUNT\tCreate multiple outputs\n"
3285 " --no-input\t\tDont create input devices\n\n");
3286
3287 fprintf(stderr,
3288 "Options for wayland-backend.so:\n\n"
3289 " --width=WIDTH\t\tWidth of Wayland surface\n"
3290 " --height=HEIGHT\tHeight of Wayland surface\n"
3291 " --display=DISPLAY\tWayland display to connect to\n\n");
3292
Pekka Paalanene31e0532013-05-22 18:03:07 +03003293#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3294 fprintf(stderr,
3295 "Options for rpi-backend.so:\n\n"
3296 " --tty=TTY\t\tThe tty to use\n"
3297 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3298 " --transform=TR\tThe output transformation, TR is one of:\n"
3299 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3300 "\n");
3301#endif
3302
Hardeningc077a842013-07-08 00:51:35 +02003303#if defined(BUILD_RDP_COMPOSITOR)
3304 fprintf(stderr,
3305 "Options for rdp-backend.so:\n\n"
3306 " --width=WIDTH\t\tWidth of desktop\n"
3307 " --height=HEIGHT\tHeight of desktop\n"
3308 " --extra-modes=MODES\t\n"
3309 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3310 " --address=ADDR\tThe address to bind\n"
3311 " --port=PORT\tThe port to listen on\n"
3312 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3313 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3314 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3315 "\n");
3316#endif
3317
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003318 exit(error_code);
3319}
3320
Peter Maatmane5b42e42013-03-27 22:38:53 +01003321static void
3322catch_signals(void)
3323{
3324 struct sigaction action;
3325
3326 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3327 action.sa_sigaction = on_caught_signal;
3328 sigemptyset(&action.sa_mask);
3329 sigaction(SIGSEGV, &action, NULL);
3330 sigaction(SIGABRT, &action, NULL);
3331}
3332
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003333int main(int argc, char *argv[])
3334{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003335 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003336 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003337 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003338 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003339 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003340 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003341 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003342 int *argc, char *argv[],
3343 struct weston_config *config);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003344 int i, config_fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003345 char *backend = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003346 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003347 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003348 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003349 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003350 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003351 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003352 struct weston_config *config;
3353 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003354
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003355 const struct weston_option core_options[] = {
3356 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3357 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3358 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003359 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003360 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003361 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003362 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003363 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003364
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003365 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003366
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003367 if (help)
3368 usage(EXIT_SUCCESS);
3369
Scott Moreau12245142012-08-29 15:15:58 -06003370 if (version) {
3371 printf(PACKAGE_STRING "\n");
3372 return EXIT_SUCCESS;
3373 }
3374
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003375 weston_log_file_open(log);
3376
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003377 weston_log("%s\n"
3378 STAMP_SPACE "%s\n"
3379 STAMP_SPACE "Bug reports to: %s\n"
3380 STAMP_SPACE "Build: %s\n",
3381 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003382 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003383 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003384
Martin Minarik37032f82012-06-18 20:15:18 +02003385 verify_xdg_runtime_dir();
3386
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003387 display = wl_display_create();
3388
Tiago Vignatti2116b892011-08-08 05:52:59 -07003389 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003390 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3391 display);
3392 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3393 display);
3394 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3395 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003396
3397 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003398 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3399 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003400
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003401 if (!backend) {
3402 if (getenv("WAYLAND_DISPLAY"))
3403 backend = "wayland-backend.so";
3404 else if (getenv("DISPLAY"))
3405 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003406 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003407 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003408 }
3409
Ossama Othmana50e6e42013-05-14 09:48:26 -07003410 config_fd = open_config_file("weston.ini");
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003411 config = weston_config_parse(config_fd);
3412 close(config_fd);
3413
3414 section = weston_config_get_section(config, "core", NULL, NULL);
3415 weston_config_section_get_string(section, "modules",
3416 &modules, "desktop-shell.so");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003417
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003418 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003419 if (!backend_init)
3420 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003421
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003422 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003423 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003424 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003425 exit(EXIT_FAILURE);
3426 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003427
Peter Maatmane5b42e42013-03-27 22:38:53 +01003428 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003429 segv_compositor = ec;
3430
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003431 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003432
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003433 setenv("WAYLAND_DISPLAY", socket_name, 1);
3434
Ossama Othmana50e6e42013-05-14 09:48:26 -07003435 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003436 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003437 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003438 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003439
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003440 for (i = 1; i < argc; i++)
3441 weston_log("fatal: unhandled option: %s\n", argv[i]);
3442 if (argc > 1) {
3443 ret = EXIT_FAILURE;
3444 goto out;
3445 }
3446
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003447 weston_compositor_log_capabilities(ec);
3448
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003449 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003450 weston_log("fatal: failed to add socket: %m\n");
3451 ret = EXIT_FAILURE;
3452 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003453 }
3454
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003455 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003456
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003457 wl_display_run(display);
3458
3459 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003460 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003461 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003462
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003463 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003464
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003465 for (i = ARRAY_LENGTH(signals); i;)
3466 wl_event_source_remove(signals[--i]);
3467
Daniel Stone855028f2012-05-01 20:37:10 +01003468 weston_compositor_xkb_destroy(ec);
3469
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003470 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003471 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003472
Martin Minarik19e6f262012-06-07 13:08:46 +02003473 weston_log_file_close();
3474
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003475 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003476}