blob: e94b495892f44a538f3fa6a0d49bb934d530d430 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanend581a8f2012-01-27 16:25:16 +02004 * Copyright © 2012 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberga9410222011-01-14 17:22:35 -050025#include "config.h"
26
Daniel Stoneb7452fe2012-06-01 12:14:06 +010027#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010032#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020034#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040035#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010036#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040037#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020038#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020039#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020040#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040041#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050042#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040044#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040045#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050046#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040047#include <sys/time.h>
48#include <time.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050049
Marcin Slusarz554a0da2013-02-18 13:27:22 -050050#ifdef HAVE_LIBUNWIND
51#define UNW_LOCAL_ONLY
52#include <libunwind.h>
53#endif
54
Pekka Paalanen50719bc2011-11-22 14:18:50 +020055#include <wayland-server.h>
Kristian Høgsberg82863022010-06-04 21:52:02 -040056#include "compositor.h"
U. Artie Eoffec08f332013-05-13 15:55:47 -070057#include "subsurface-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030058#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040059#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050060#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061
Kristian Høgsberg27da5382011-06-21 17:32:25 -040062static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040063static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040064
65static int
66sigchld_handler(int signal_number, void *data)
67{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050068 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040069 int status;
70 pid_t pid;
71
Bill Spitzak027b9622012-03-17 15:22:03 -070072 pid = waitpid(-1, &status, WNOHANG);
73 if (!pid)
74 return 1;
75
Kristian Høgsberg27da5382011-06-21 17:32:25 -040076 wl_list_for_each(p, &child_process_list, link) {
77 if (p->pid == pid)
78 break;
79 }
80
81 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020082 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040083 return 1;
84 }
85
86 wl_list_remove(&p->link);
87 p->cleanup(p, status);
88
89 return 1;
90}
91
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020092static void
Alexander Larsson0b135062013-05-28 16:23:36 +020093weston_output_transform_scale_init(struct weston_output *output,
94 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020095
Rob Bradford27b17932013-06-26 18:08:46 +010096static void
97weston_compositor_build_surface_list(struct weston_compositor *compositor);
98
Alex Wu2dda6042012-04-17 17:20:47 +080099WL_EXPORT int
Alexander Larsson355748e2013-05-28 16:23:38 +0200100weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode, int32_t scale)
Alex Wu2dda6042012-04-17 17:20:47 +0800101{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200102 struct weston_seat *seat;
103 pixman_region32_t old_output_region;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200104 int ret;
105
Alex Wu2dda6042012-04-17 17:20:47 +0800106 if (!output->switch_mode)
107 return -1;
108
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200109 ret = output->switch_mode(output, mode);
110 if (ret < 0)
111 return ret;
112
Alexander Larsson355748e2013-05-28 16:23:38 +0200113 output->scale = scale;
114
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200115 pixman_region32_init(&old_output_region);
116 pixman_region32_copy(&old_output_region, &output->region);
117
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200118 /* Update output region and transformation matrix */
Alexander Larsson0b135062013-05-28 16:23:36 +0200119 weston_output_transform_scale_init(output, output->transform, output->scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200120
121 pixman_region32_init(&output->previous_damage);
122 pixman_region32_init_rect(&output->region, output->x, output->y,
123 output->width, output->height);
124
125 weston_output_update_matrix(output);
126
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200127 /* If a pointer falls outside the outputs new geometry, move it to its
128 * lower-right corner */
129 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400130 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200131 int32_t x, y;
132
133 if (!pointer)
134 continue;
135
136 x = wl_fixed_to_int(pointer->x);
137 y = wl_fixed_to_int(pointer->y);
138
139 if (!pixman_region32_contains_point(&old_output_region,
140 x, y, NULL) ||
141 pixman_region32_contains_point(&output->region,
142 x, y, NULL))
143 continue;
144
145 if (x >= output->x + output->width)
146 x = output->x + output->width - 1;
147 if (y >= output->y + output->height)
148 y = output->y + output->height - 1;
149
150 pointer->x = wl_fixed_from_int(x);
151 pointer->y = wl_fixed_from_int(y);
152 }
153
154 pixman_region32_fini(&old_output_region);
155
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200156 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800157}
158
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400159WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500160weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400161{
162 wl_list_insert(&child_process_list, &process->link);
163}
164
Benjamin Franzke06286262011-05-06 19:12:33 +0200165static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200166child_client_exec(int sockfd, const char *path)
167{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500168 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200169 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200170 sigset_t allsigs;
171
172 /* do not give our signal mask to the new process */
173 sigfillset(&allsigs);
174 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200175
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500176 /* Launch clients as the user. */
177 seteuid(getuid());
178
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500179 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
180 * non-CLOEXEC fd to pass through exec. */
181 clientfd = dup(sockfd);
182 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200183 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500184 return;
185 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200186
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500187 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200188 setenv("WAYLAND_SOCKET", s, 1);
189
190 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200191 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200192 path);
193}
194
195WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500196weston_client_launch(struct weston_compositor *compositor,
197 struct weston_process *proc,
198 const char *path,
199 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200200{
201 int sv[2];
202 pid_t pid;
203 struct wl_client *client;
204
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300205 weston_log("launching '%s'\n", path);
206
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300207 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200208 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200209 "socketpair failed while launching '%s': %m\n",
210 path);
211 return NULL;
212 }
213
214 pid = fork();
215 if (pid == -1) {
216 close(sv[0]);
217 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200218 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200219 "fork failed while launching '%s': %m\n", path);
220 return NULL;
221 }
222
223 if (pid == 0) {
224 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700225 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200226 }
227
228 close(sv[1]);
229
230 client = wl_client_create(compositor->wl_display, sv[0]);
231 if (!client) {
232 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200233 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200234 "wl_client_create failed while launching '%s'.\n",
235 path);
236 return NULL;
237 }
238
239 proc->pid = pid;
240 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500241 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200242
243 return client;
244}
245
246static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300247surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
248{
249 struct weston_surface *surface =
250 container_of(listener, struct weston_surface,
251 pending.buffer_destroy_listener);
252
253 surface->pending.buffer = NULL;
254}
255
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200256static void
257empty_region(pixman_region32_t *region)
258{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300259 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200260 pixman_region32_init(region);
261}
262
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300263static void
264region_init_infinite(pixman_region32_t *region)
265{
266 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
267 UINT32_MAX, UINT32_MAX);
268}
269
Pekka Paalanene67858b2013-04-25 13:57:42 +0300270static struct weston_subsurface *
271weston_surface_to_subsurface(struct weston_surface *surface);
272
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500273WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500274weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500275{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500276 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400277
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200278 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400279 if (surface == NULL)
280 return NULL;
281
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500282 wl_signal_init(&surface->destroy_signal);
283
284 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500285
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500286 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500287 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500288
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500289 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400290 surface->alpha = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400291
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100292 if (compositor->renderer->create_surface(surface) < 0) {
293 free(surface);
294 return NULL;
295 }
296
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200297 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200298 surface->buffer_scale = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200299 surface->pending.buffer_transform = surface->buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200300 surface->pending.buffer_scale = surface->buffer_scale;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400301 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400302 surface->plane = &compositor->primary_plane;
Giulio Camuffo184df502013-02-21 11:29:21 +0100303 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200304
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400305 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500306 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500307 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300308 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200309 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500310 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400311
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200312 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200313 wl_list_insert(&surface->geometry.transformation_list,
314 &surface->transform.position.link);
315 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200316 wl_list_init(&surface->geometry.child_list);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200317 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200318 surface->transform.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500319
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300320 surface->pending.buffer_destroy_listener.notify =
321 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300322 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300323 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300324 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300325 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300326
Pekka Paalanene67858b2013-04-25 13:57:42 +0300327 wl_list_init(&surface->subsurface_list);
328 wl_list_init(&surface->subsurface_list_pending);
329
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400330 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500331}
332
Alex Wu8811bf92012-02-28 18:07:54 +0800333WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500334weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200335 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500336{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100337 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500338}
339
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400340WL_EXPORT void
341weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200342 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200343{
344 if (surface->transform.enabled) {
345 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
346
347 weston_matrix_transform(&surface->transform.matrix, &v);
348
349 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200350 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700351 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200352 v.f[3]);
353 *x = 0;
354 *y = 0;
355 return;
356 }
357
358 *x = v.f[0] / v.f[3];
359 *y = v.f[1] / v.f[3];
360 } else {
361 *x = sx + surface->geometry.x;
362 *y = sy + surface->geometry.y;
363 }
364}
365
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500366WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200367weston_transformed_coord(int width, int height,
368 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200369 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200370 float sx, float sy, float *bx, float *by)
371{
372 switch (transform) {
373 case WL_OUTPUT_TRANSFORM_NORMAL:
374 default:
375 *bx = sx;
376 *by = sy;
377 break;
378 case WL_OUTPUT_TRANSFORM_FLIPPED:
379 *bx = width - sx;
380 *by = sy;
381 break;
382 case WL_OUTPUT_TRANSFORM_90:
383 *bx = height - sy;
384 *by = sx;
385 break;
386 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
387 *bx = height - sy;
388 *by = width - sx;
389 break;
390 case WL_OUTPUT_TRANSFORM_180:
391 *bx = width - sx;
392 *by = height - sy;
393 break;
394 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
395 *bx = sx;
396 *by = height - sy;
397 break;
398 case WL_OUTPUT_TRANSFORM_270:
399 *bx = sy;
400 *by = width - sx;
401 break;
402 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
403 *bx = sy;
404 *by = sx;
405 break;
406 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200407
408 *bx *= scale;
409 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200410}
411
412WL_EXPORT pixman_box32_t
413weston_transformed_rect(int width, int height,
414 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200415 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200416 pixman_box32_t rect)
417{
418 float x1, x2, y1, y2;
419
420 pixman_box32_t ret;
421
Alexander Larsson4ea95522013-05-22 14:41:37 +0200422 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200423 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200424 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200425 rect.x2, rect.y2, &x2, &y2);
426
427 if (x1 <= x2) {
428 ret.x1 = x1;
429 ret.x2 = x2;
430 } else {
431 ret.x1 = x2;
432 ret.x2 = x1;
433 }
434
435 if (y1 <= y2) {
436 ret.y1 = y1;
437 ret.y2 = y2;
438 } else {
439 ret.y1 = y2;
440 ret.y2 = y1;
441 }
442
443 return ret;
444}
445
446WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200447weston_surface_to_buffer_float(struct weston_surface *surface,
448 float sx, float sy, float *bx, float *by)
449{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200450 weston_transformed_coord(surface->geometry.width,
451 surface->geometry.height,
452 surface->buffer_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200453 surface->buffer_scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200454 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200455}
456
Alexander Larsson4ea95522013-05-22 14:41:37 +0200457WL_EXPORT void
458weston_surface_to_buffer(struct weston_surface *surface,
459 int sx, int sy, int *bx, int *by)
460{
461 float bxf, byf;
462
463 weston_transformed_coord(surface->geometry.width,
464 surface->geometry.height,
465 surface->buffer_transform,
466 surface->buffer_scale,
467 sx, sy, &bxf, &byf);
468 *bx = floorf(bxf);
469 *by = floorf(byf);
470}
471
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200472WL_EXPORT pixman_box32_t
473weston_surface_to_buffer_rect(struct weston_surface *surface,
474 pixman_box32_t rect)
475{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200476 return weston_transformed_rect(surface->geometry.width,
477 surface->geometry.height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200478 surface->buffer_transform,
479 surface->buffer_scale,
480 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200481}
482
483WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400484weston_surface_move_to_plane(struct weston_surface *surface,
485 struct weston_plane *plane)
486{
487 if (surface->plane == plane)
488 return;
489
490 weston_surface_damage_below(surface);
491 surface->plane = plane;
492 weston_surface_damage(surface);
493}
494
495WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500496weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200497{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500498 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200499
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500500 pixman_region32_init(&damage);
501 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
502 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400503 pixman_region32_union(&surface->plane->damage,
504 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500505 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200506}
507
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400508static void
509weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
510{
511 uint32_t different = es->output_mask ^ mask;
512 uint32_t entered = mask & different;
513 uint32_t left = es->output_mask & different;
514 struct weston_output *output;
515 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500516 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400517
518 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500519 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400520 return;
521 if (different == 0)
522 return;
523
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500524 client = wl_resource_get_client(es->resource);
525
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400526 wl_list_for_each(output, &es->compositor->output_list, link) {
527 if (1 << output->id & different)
528 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500529 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400530 client);
531 if (resource == NULL)
532 continue;
533 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500534 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400535 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500536 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400537 }
538}
539
540static void
541weston_surface_assign_output(struct weston_surface *es)
542{
543 struct weston_compositor *ec = es->compositor;
544 struct weston_output *output, *new_output;
545 pixman_region32_t region;
546 uint32_t max, area, mask;
547 pixman_box32_t *e;
548
549 new_output = NULL;
550 max = 0;
551 mask = 0;
552 pixman_region32_init(&region);
553 wl_list_for_each(output, &ec->output_list, link) {
554 pixman_region32_intersect(&region, &es->transform.boundingbox,
555 &output->region);
556
557 e = pixman_region32_extents(&region);
558 area = (e->x2 - e->x1) * (e->y2 - e->y1);
559
560 if (area > 0)
561 mask |= 1 << output->id;
562
563 if (area >= max) {
564 new_output = output;
565 max = area;
566 }
567 }
568 pixman_region32_fini(&region);
569
570 es->output = new_output;
571 weston_surface_update_output_mask(es, mask);
572}
573
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200574static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200575surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
576 int32_t width, int32_t height,
577 pixman_region32_t *bbox)
578{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200579 float min_x = HUGE_VALF, min_y = HUGE_VALF;
580 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200581 int32_t s[4][2] = {
582 { sx, sy },
583 { sx, sy + height },
584 { sx + width, sy },
585 { sx + width, sy + height }
586 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200587 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200588 int i;
589
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300590 if (width == 0 || height == 0) {
591 /* avoid rounding empty bbox to 1x1 */
592 pixman_region32_init(bbox);
593 return;
594 }
595
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200596 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200597 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400598 weston_surface_to_global_float(surface,
599 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200600 if (x < min_x)
601 min_x = x;
602 if (x > max_x)
603 max_x = x;
604 if (y < min_y)
605 min_y = y;
606 if (y > max_y)
607 max_y = y;
608 }
609
Pekka Paalanen219b9822012-02-08 15:38:37 +0200610 int_x = floorf(min_x);
611 int_y = floorf(min_y);
612 pixman_region32_init_rect(bbox, int_x, int_y,
613 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200614}
615
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200616static void
617weston_surface_update_transform_disable(struct weston_surface *surface)
618{
619 surface->transform.enabled = 0;
620
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200621 /* round off fractions when not transformed */
622 surface->geometry.x = roundf(surface->geometry.x);
623 surface->geometry.y = roundf(surface->geometry.y);
624
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500625 /* Otherwise identity matrix, but with x and y translation. */
626 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
627 surface->transform.position.matrix.d[12] = surface->geometry.x;
628 surface->transform.position.matrix.d[13] = surface->geometry.y;
629
630 surface->transform.matrix = surface->transform.position.matrix;
631
Kristian Høgsberg9bcaaeb2013-02-28 14:56:43 -0500632 surface->transform.inverse = surface->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500633 surface->transform.inverse.d[12] = -surface->geometry.x;
634 surface->transform.inverse.d[13] = -surface->geometry.y;
635
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200636 pixman_region32_init_rect(&surface->transform.boundingbox,
637 surface->geometry.x,
638 surface->geometry.y,
639 surface->geometry.width,
640 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500641
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400642 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500643 pixman_region32_copy(&surface->transform.opaque,
644 &surface->opaque);
645 pixman_region32_translate(&surface->transform.opaque,
646 surface->geometry.x,
647 surface->geometry.y);
648 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200649}
650
651static int
652weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200653{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200654 struct weston_surface *parent = surface->geometry.parent;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200655 struct weston_matrix *matrix = &surface->transform.matrix;
656 struct weston_matrix *inverse = &surface->transform.inverse;
657 struct weston_transform *tform;
658
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200659 surface->transform.enabled = 1;
660
661 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300662 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200663 surface->transform.position.matrix.d[12] = surface->geometry.x;
664 surface->transform.position.matrix.d[13] = surface->geometry.y;
665
666 weston_matrix_init(matrix);
667 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
668 weston_matrix_multiply(matrix, &tform->matrix);
669
Pekka Paalanen483243f2013-03-08 14:56:50 +0200670 if (parent)
671 weston_matrix_multiply(matrix, &parent->transform.matrix);
672
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200673 if (weston_matrix_invert(inverse, matrix) < 0) {
674 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200675 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200676 " transformation not invertible.\n", surface);
677 return -1;
678 }
679
680 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
681 surface->geometry.height,
682 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500683
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200684 return 0;
685}
686
687WL_EXPORT void
688weston_surface_update_transform(struct weston_surface *surface)
689{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200690 struct weston_surface *parent = surface->geometry.parent;
691
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200692 if (!surface->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200693 return;
694
Pekka Paalanen483243f2013-03-08 14:56:50 +0200695 if (parent)
696 weston_surface_update_transform(parent);
697
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200698 surface->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200699
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500700 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200701
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200702 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500703 pixman_region32_fini(&surface->transform.opaque);
704 pixman_region32_init(&surface->transform.opaque);
705
Pekka Paalanencd403622012-01-25 13:37:39 +0200706 /* transform.position is always in transformation_list */
707 if (surface->geometry.transformation_list.next ==
708 &surface->transform.position.link &&
709 surface->geometry.transformation_list.prev ==
Pekka Paalanen483243f2013-03-08 14:56:50 +0200710 &surface->transform.position.link &&
711 !parent) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200712 weston_surface_update_transform_disable(surface);
713 } else {
714 if (weston_surface_update_transform_enable(surface) < 0)
715 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200716 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200717
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400718 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200719
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300720 weston_surface_assign_output(surface);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300721
722 wl_signal_emit(&surface->compositor->transform_signal, surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200723}
724
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200725WL_EXPORT void
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200726weston_surface_geometry_dirty(struct weston_surface *surface)
727{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200728 struct weston_surface *child;
729
730 /*
731 * The invariant: if surface->geometry.dirty, then all surfaces
732 * in surface->geometry.child_list have geometry.dirty too.
733 * Corollary: if not parent->geometry.dirty, then all ancestors
734 * are not dirty.
735 */
736
737 if (surface->transform.dirty)
738 return;
739
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200740 surface->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200741
742 wl_list_for_each(child, &surface->geometry.child_list,
743 geometry.parent_link)
744 weston_surface_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200745}
746
747WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100748weston_surface_to_global_fixed(struct weston_surface *surface,
749 wl_fixed_t sx, wl_fixed_t sy,
750 wl_fixed_t *x, wl_fixed_t *y)
751{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200752 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100753
754 weston_surface_to_global_float(surface,
755 wl_fixed_to_double(sx),
756 wl_fixed_to_double(sy),
757 &xf, &yf);
758 *x = wl_fixed_from_double(xf);
759 *y = wl_fixed_from_double(yf);
760}
761
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400762WL_EXPORT void
763weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200764 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200765{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200766 if (surface->transform.enabled) {
767 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
768
769 weston_matrix_transform(&surface->transform.inverse, &v);
770
771 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200772 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200773 "weston_surface_from_global(), divisor = %g\n",
774 v.f[3]);
775 *sx = 0;
776 *sy = 0;
777 return;
778 }
779
Pekka Paalanencd403622012-01-25 13:37:39 +0200780 *sx = v.f[0] / v.f[3];
781 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200782 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200783 *sx = x - surface->geometry.x;
784 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200785 }
786}
787
788WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100789weston_surface_from_global_fixed(struct weston_surface *surface,
790 wl_fixed_t x, wl_fixed_t y,
791 wl_fixed_t *sx, wl_fixed_t *sy)
792{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200793 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100794
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400795 weston_surface_from_global_float(surface,
796 wl_fixed_to_double(x),
797 wl_fixed_to_double(y),
798 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100799 *sx = wl_fixed_from_double(sxf);
800 *sy = wl_fixed_from_double(syf);
801}
802
803WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200804weston_surface_from_global(struct weston_surface *surface,
805 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
806{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200807 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200808
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400809 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200810 *sx = floorf(sxf);
811 *sy = floorf(syf);
812}
813
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400814WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400815weston_surface_schedule_repaint(struct weston_surface *surface)
816{
817 struct weston_output *output;
818
819 wl_list_for_each(output, &surface->compositor->output_list, link)
820 if (surface->output_mask & (1 << output->id))
821 weston_output_schedule_repaint(output);
822}
823
824WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500825weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500826{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400827 pixman_region32_union_rect(&surface->damage, &surface->damage,
828 0, 0, surface->geometry.width,
829 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200830
Kristian Høgsberg98238702012-08-03 16:29:12 -0400831 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500832}
833
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400834WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500835weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200836 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400837{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200838 surface->geometry.x = x;
839 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200840 surface->geometry.width = width;
841 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200842 weston_surface_geometry_dirty(surface);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400843}
844
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200845WL_EXPORT void
846weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200847 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200848{
849 surface->geometry.x = x;
850 surface->geometry.y = y;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200851 weston_surface_geometry_dirty(surface);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200852}
853
Pekka Paalanen483243f2013-03-08 14:56:50 +0200854static void
855transform_parent_handle_parent_destroy(struct wl_listener *listener,
856 void *data)
857{
858 struct weston_surface *surface =
859 container_of(listener, struct weston_surface,
860 geometry.parent_destroy_listener);
861
862 weston_surface_set_transform_parent(surface, NULL);
863}
864
865WL_EXPORT void
866weston_surface_set_transform_parent(struct weston_surface *surface,
867 struct weston_surface *parent)
868{
869 if (surface->geometry.parent) {
870 wl_list_remove(&surface->geometry.parent_destroy_listener.link);
871 wl_list_remove(&surface->geometry.parent_link);
872 }
873
874 surface->geometry.parent = parent;
875
876 surface->geometry.parent_destroy_listener.notify =
877 transform_parent_handle_parent_destroy;
878 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500879 wl_signal_add(&parent->destroy_signal,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200880 &surface->geometry.parent_destroy_listener);
881 wl_list_insert(&parent->geometry.child_list,
882 &surface->geometry.parent_link);
883 }
884
885 weston_surface_geometry_dirty(surface);
886}
887
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300888WL_EXPORT int
889weston_surface_is_mapped(struct weston_surface *surface)
890{
891 if (surface->output)
892 return 1;
893 else
894 return 0;
895}
896
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200897WL_EXPORT int32_t
898weston_surface_buffer_width(struct weston_surface *surface)
899{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200900 int32_t width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200901 switch (surface->buffer_transform) {
902 case WL_OUTPUT_TRANSFORM_90:
903 case WL_OUTPUT_TRANSFORM_270:
904 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
905 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200906 width = surface->buffer_ref.buffer->height;
907 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200908 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200909 width = surface->buffer_ref.buffer->width;
910 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200911 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200912 return width / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200913}
914
915WL_EXPORT int32_t
916weston_surface_buffer_height(struct weston_surface *surface)
917{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200918 int32_t height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200919 switch (surface->buffer_transform) {
920 case WL_OUTPUT_TRANSFORM_90:
921 case WL_OUTPUT_TRANSFORM_270:
922 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
923 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200924 height = surface->buffer_ref.buffer->width;
925 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200926 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200927 height = surface->buffer_ref.buffer->height;
928 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200929 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200930 return height / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200931}
932
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400933WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500934weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500935{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400936 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500937
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400938 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500939
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400940 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500941}
942
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400943WL_EXPORT struct weston_surface *
Tiago Vignatti9d393522012-02-10 16:26:19 +0200944weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100945 wl_fixed_t x, wl_fixed_t y,
946 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200947{
948 struct weston_surface *surface;
949
950 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100951 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500952 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100953 wl_fixed_to_int(*sx),
954 wl_fixed_to_int(*sy),
955 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200956 return surface;
957 }
958
959 return NULL;
960}
961
962static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500963weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400964{
Daniel Stone37816df2012-05-16 18:45:18 +0100965 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400966
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500967 if (!compositor->focus)
968 return;
969
Daniel Stone37816df2012-05-16 18:45:18 +0100970 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400971 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400972}
973
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400974WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500975weston_surface_unmap(struct weston_surface *surface)
976{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100977 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500978
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500979 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500980 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500981 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500982
Daniel Stone4dab5db2012-05-30 16:31:53 +0100983 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400984 if (seat->keyboard && seat->keyboard->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400985 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400986 if (seat->pointer && seat->pointer->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400987 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400988 NULL,
989 wl_fixed_from_int(0),
990 wl_fixed_from_int(0));
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{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001007 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1008
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001009 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001010 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001011
Pekka Paalanen483243f2013-03-08 14:56:50 +02001012 assert(wl_list_empty(&surface->geometry.child_list));
Pekka Paalanene67858b2013-04-25 13:57:42 +03001013 assert(wl_list_empty(&surface->subsurface_list_pending));
1014 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001015
Rob Bradford27b17932013-06-26 18:08:46 +01001016 if (weston_surface_is_mapped(surface)) {
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001017 weston_surface_unmap(surface);
Rob Bradford27b17932013-06-26 18:08:46 +01001018 weston_compositor_build_surface_list(compositor);
1019 }
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001020
Pekka Paalanenbc106382012-10-10 12:49:31 +03001021 wl_list_for_each_safe(cb, next,
1022 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001023 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001024
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001025 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001026 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001027 pixman_region32_fini(&surface->pending.damage);
1028
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001029 if (surface->pending.buffer)
1030 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1031
Pekka Paalanende685b82012-12-04 15:58:12 +02001032 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001033
Kristian Høgsberg42263852012-09-06 21:59:29 -04001034 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001035
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001036 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001037 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001038 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001039 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001040 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001041
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001042 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001043 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001044
Pekka Paalanen483243f2013-03-08 14:56:50 +02001045 weston_surface_set_transform_parent(surface, NULL);
1046
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001047 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001048}
1049
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001050static void
1051destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001052{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001053 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001054
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001055 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001056}
1057
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001058static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001059weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1060{
1061 struct weston_buffer *buffer =
1062 container_of(listener, struct weston_buffer, destroy_listener);
1063
1064 wl_signal_emit(&buffer->destroy_signal, buffer);
1065 free(buffer);
1066}
1067
1068struct weston_buffer *
1069weston_buffer_from_resource(struct wl_resource *resource)
1070{
1071 struct weston_buffer *buffer;
1072 struct wl_listener *listener;
1073
1074 listener = wl_resource_get_destroy_listener(resource,
1075 weston_buffer_destroy_handler);
1076
1077 if (listener) {
1078 buffer = container_of(listener, struct weston_buffer,
1079 destroy_listener);
1080 } else {
1081 buffer = malloc(sizeof *buffer);
1082 memset(buffer, 0, sizeof *buffer);
1083
1084 buffer->resource = resource;
1085 wl_signal_init(&buffer->destroy_signal);
1086 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
1087 wl_resource_add_destroy_listener(resource,
1088 &buffer->destroy_listener);
1089 }
1090
1091 return buffer;
1092}
1093
1094static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001095weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1096 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001097{
Pekka Paalanende685b82012-12-04 15:58:12 +02001098 struct weston_buffer_reference *ref =
1099 container_of(listener, struct weston_buffer_reference,
1100 destroy_listener);
1101
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001102 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001103 ref->buffer = NULL;
1104}
1105
1106WL_EXPORT void
1107weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001108 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001109{
1110 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001111 ref->buffer->busy_count--;
1112 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001113 assert(wl_resource_get_client(ref->buffer->resource));
1114 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001115 WL_BUFFER_RELEASE);
1116 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001117 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001118 }
1119
Pekka Paalanende685b82012-12-04 15:58:12 +02001120 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001121 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001122 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001123 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001124 }
1125
Pekka Paalanende685b82012-12-04 15:58:12 +02001126 ref->buffer = buffer;
1127 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1128}
1129
1130static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001131weston_surface_attach(struct weston_surface *surface,
1132 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001133{
1134 weston_buffer_reference(&surface->buffer_ref, buffer);
1135
Pekka Paalanena6421c42012-12-04 15:58:10 +02001136 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001137 if (weston_surface_is_mapped(surface))
1138 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001139 }
1140
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001141 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001142}
1143
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001144WL_EXPORT void
1145weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001146{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001147 wl_list_remove(&surface->layer_link);
1148 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001149 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001150 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001151}
1152
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001153WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001154weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001155{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001156 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001157
1158 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001159 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001160}
1161
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001162WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001163weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001164{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001165 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001166
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001167 pixman_region32_union(&compositor->primary_plane.damage,
1168 &compositor->primary_plane.damage,
1169 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001170 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001171}
1172
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001173static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001174surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001175 pixman_region32_t *opaque)
1176{
Pekka Paalanende685b82012-12-04 15:58:12 +02001177 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001178 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001179 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001180
1181 if (surface->transform.enabled) {
1182 pixman_box32_t *extents;
1183
1184 extents = pixman_region32_extents(&surface->damage);
1185 surface_compute_bbox(surface, extents->x1, extents->y1,
1186 extents->x2 - extents->x1,
1187 extents->y2 - extents->y1,
1188 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001189 pixman_region32_translate(&surface->damage,
1190 -surface->plane->x,
1191 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001192 } else {
1193 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001194 surface->geometry.x - surface->plane->x,
1195 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001196 }
1197
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001198 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001199 pixman_region32_union(&surface->plane->damage,
1200 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001201 empty_region(&surface->damage);
1202 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001203 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001204}
1205
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001206static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001207compositor_accumulate_damage(struct weston_compositor *ec)
1208{
1209 struct weston_plane *plane;
1210 struct weston_surface *es;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001211 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001212
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001213 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001214
1215 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001216 pixman_region32_copy(&plane->clip, &clip);
1217
1218 pixman_region32_init(&opaque);
1219
1220 wl_list_for_each(es, &ec->surface_list, link) {
1221 if (es->plane != plane)
1222 continue;
1223
1224 surface_accumulate_damage(es, &opaque);
1225 }
1226
1227 pixman_region32_union(&clip, &clip, &opaque);
1228 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001229 }
1230
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001231 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001232
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001233 wl_list_for_each(es, &ec->surface_list, link) {
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001234 /* Both the renderer and the backend have seen the buffer
1235 * by now. If renderer needs the buffer, it has its own
1236 * reference set. If the backend wants to keep the buffer
1237 * around for migrating the surface into a non-primary plane
1238 * later, keep_buffer is true. Otherwise, drop the core
1239 * reference now, and allow early buffer release. This enables
1240 * clients to use single-buffering.
1241 */
1242 if (!es->keep_buffer)
1243 weston_buffer_reference(&es->buffer_ref, NULL);
1244 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001245}
1246
1247static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001248surface_list_add(struct weston_compositor *compositor,
1249 struct weston_surface *surface)
1250{
1251 struct weston_subsurface *sub;
1252
1253 if (wl_list_empty(&surface->subsurface_list)) {
1254 weston_surface_update_transform(surface);
1255 wl_list_insert(compositor->surface_list.prev, &surface->link);
1256 return;
1257 }
1258
1259 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1260 if (!weston_surface_is_mapped(sub->surface))
1261 continue;
1262
1263 if (sub->surface == surface) {
1264 weston_surface_update_transform(sub->surface);
1265 wl_list_insert(compositor->surface_list.prev,
1266 &sub->surface->link);
1267 } else {
1268 surface_list_add(compositor, sub->surface);
1269 }
1270 }
1271}
1272
1273static void
1274weston_compositor_build_surface_list(struct weston_compositor *compositor)
1275{
1276 struct weston_surface *surface;
1277 struct weston_layer *layer;
1278
1279 wl_list_init(&compositor->surface_list);
1280 wl_list_for_each(layer, &compositor->layer_list, link) {
1281 wl_list_for_each(surface, &layer->surface_list, layer_link) {
1282 surface_list_add(compositor, surface);
1283 }
1284 }
1285}
1286
1287static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001288weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001289{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001290 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001291 struct weston_surface *es;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001292 struct weston_animation *animation, *next;
1293 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001294 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001295 pixman_region32_t output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001296
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001297 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001298 weston_compositor_build_surface_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001299
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001300 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001301 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001302 else
1303 wl_list_for_each(es, &ec->surface_list, link)
1304 weston_surface_move_to_plane(es, &ec->primary_plane);
1305
Pekka Paalanene67858b2013-04-25 13:57:42 +03001306 wl_list_init(&frame_callback_list);
1307 wl_list_for_each(es, &ec->surface_list, link) {
1308 if (es->output == output) {
1309 wl_list_insert_list(&frame_callback_list,
1310 &es->frame_callback_list);
1311 wl_list_init(&es->frame_callback_list);
1312 }
1313 }
1314
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001315 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001316
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001317 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001318 pixman_region32_intersect(&output_damage,
1319 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001320 pixman_region32_subtract(&output_damage,
1321 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001322
Scott Moreauccbf29d2012-02-22 14:21:41 -07001323 if (output->dirty)
1324 weston_output_update_matrix(output);
1325
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001326 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001327
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001328 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001329
Kristian Høgsbergef044142011-06-21 15:02:12 -04001330 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001331
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001332 weston_compositor_repick(ec);
1333 wl_event_loop_dispatch(ec->input_loop, 0);
1334
Jonas Ådahldb773762012-06-13 00:01:21 +02001335 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001336 wl_callback_send_done(cb->resource, msecs);
1337 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001338 }
1339
Scott Moreaud64cf212012-06-08 19:40:54 -06001340 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001341 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001342 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001343 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001344}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001345
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001346static int
1347weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001348{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001349 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001350
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001351 wl_event_loop_dispatch(compositor->input_loop, 0);
1352
1353 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001354}
1355
1356WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001357weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001358{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001359 struct weston_compositor *compositor = output->compositor;
1360 struct wl_event_loop *loop =
1361 wl_display_get_event_loop(compositor->wl_display);
1362 int fd;
1363
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001364 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001365 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001366 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001367 return;
1368 }
1369
1370 output->repaint_scheduled = 0;
1371 if (compositor->input_loop_source)
1372 return;
1373
1374 fd = wl_event_loop_get_fd(compositor->input_loop);
1375 compositor->input_loop_source =
1376 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1377 weston_compositor_read_input, compositor);
1378}
1379
1380static void
1381idle_repaint(void *data)
1382{
1383 struct weston_output *output = data;
1384
Jonas Ådahle5a12252013-04-05 23:07:11 +02001385 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001386}
1387
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001388WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001389weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1390{
1391 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001392 if (below != NULL)
1393 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001394}
1395
1396WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001397weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001398{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001399 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001400 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001401
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001402 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1403 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001404 return;
1405
Kristian Høgsbergef044142011-06-21 15:02:12 -04001406 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001407 output->repaint_needed = 1;
1408 if (output->repaint_scheduled)
1409 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001410
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001411 wl_event_loop_add_idle(loop, idle_repaint, output);
1412 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001413
1414 if (compositor->input_loop_source) {
1415 wl_event_source_remove(compositor->input_loop_source);
1416 compositor->input_loop_source = NULL;
1417 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001418}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001419
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001420WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001421weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1422{
1423 struct weston_output *output;
1424
1425 wl_list_for_each(output, &compositor->output_list, link)
1426 weston_output_schedule_repaint(output);
1427}
1428
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001429static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001430surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001431{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001432 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001433}
1434
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001435static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001436surface_attach(struct wl_client *client,
1437 struct wl_resource *resource,
1438 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1439{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001440 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001441 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001442
1443 if (buffer_resource)
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001444 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001445
Pekka Paalanende685b82012-12-04 15:58:12 +02001446 /* Attach, attach, without commit in between does not send
1447 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001448 if (surface->pending.buffer)
1449 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001450
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001451 surface->pending.sx = sx;
1452 surface->pending.sy = sy;
1453 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001454 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001455 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001456 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001457 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001458 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001459}
1460
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001461static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001462surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001463 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001464 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001465{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001466 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001467
Pekka Paalanen8e159182012-10-10 12:49:25 +03001468 pixman_region32_union_rect(&surface->pending.damage,
1469 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001470 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001471}
1472
Kristian Høgsberg33418202011-08-16 23:01:28 -04001473static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001474destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001475{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001476 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001477
1478 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001479 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001480}
1481
1482static void
1483surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001484 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001485{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001486 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001487 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001488
1489 cb = malloc(sizeof *cb);
1490 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001491 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001492 return;
1493 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001494
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001495 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1496 callback);
1497 if (cb->resource == NULL) {
1498 free(cb);
1499 wl_resource_post_no_memory(resource);
1500 return;
1501 }
1502
Jason Ekstranda85118c2013-06-27 20:17:02 -05001503 wl_resource_set_implementation(cb->resource, NULL, cb,
1504 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001505
Pekka Paalanenbc106382012-10-10 12:49:31 +03001506 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001507}
1508
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001509static void
1510surface_set_opaque_region(struct wl_client *client,
1511 struct wl_resource *resource,
1512 struct wl_resource *region_resource)
1513{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001514 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001515 struct weston_region *region;
1516
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001517 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001518 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001519 pixman_region32_copy(&surface->pending.opaque,
1520 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001521 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001522 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001523 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001524}
1525
1526static void
1527surface_set_input_region(struct wl_client *client,
1528 struct wl_resource *resource,
1529 struct wl_resource *region_resource)
1530{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001531 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001532 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001533
1534 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001535 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001536 pixman_region32_copy(&surface->pending.input,
1537 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001538 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001539 pixman_region32_fini(&surface->pending.input);
1540 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001541 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001542}
1543
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001544static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001545weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001546{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001547 struct weston_subsurface *sub;
1548
1549 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1550 parent_link_pending) {
1551 wl_list_remove(&sub->parent_link);
1552 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1553 }
1554}
1555
1556static void
1557weston_surface_commit(struct weston_surface *surface)
1558{
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001559 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001560 int surface_width = 0;
1561 int surface_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001562
Alexander Larsson4ea95522013-05-22 14:41:37 +02001563 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001564 surface->buffer_transform = surface->pending.buffer_transform;
1565
Alexander Larsson4ea95522013-05-22 14:41:37 +02001566 /* wl_surface.set_buffer_scale */
1567 surface->buffer_scale = surface->pending.buffer_scale;
1568
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001569 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001570 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001571 weston_surface_attach(surface, surface->pending.buffer);
1572
Giulio Camuffo184df502013-02-21 11:29:21 +01001573 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001574 surface_width = weston_surface_buffer_width(surface);
1575 surface_height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001576 }
1577
1578 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001579 surface->configure(surface,
1580 surface->pending.sx, surface->pending.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001581 surface_width, surface_height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001582
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001583 if (surface->pending.buffer)
1584 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1585 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001586 surface->pending.sx = 0;
1587 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001588 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001589
1590 /* wl_surface.damage */
1591 pixman_region32_union(&surface->damage, &surface->damage,
1592 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001593 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1594 0, 0,
1595 surface->geometry.width,
1596 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001597 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001598
1599 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001600 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001601 surface->geometry.width,
1602 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001603 pixman_region32_intersect(&opaque,
1604 &opaque, &surface->pending.opaque);
1605
1606 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1607 pixman_region32_copy(&surface->opaque, &opaque);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001608 weston_surface_geometry_dirty(surface);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001609 }
1610
1611 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001612
1613 /* wl_surface.set_input_region */
1614 pixman_region32_fini(&surface->input);
1615 pixman_region32_init_rect(&surface->input, 0, 0,
1616 surface->geometry.width,
1617 surface->geometry.height);
1618 pixman_region32_intersect(&surface->input,
1619 &surface->input, &surface->pending.input);
1620
Pekka Paalanenbc106382012-10-10 12:49:31 +03001621 /* wl_surface.frame */
1622 wl_list_insert_list(&surface->frame_callback_list,
1623 &surface->pending.frame_callback_list);
1624 wl_list_init(&surface->pending.frame_callback_list);
1625
Pekka Paalanene67858b2013-04-25 13:57:42 +03001626 weston_surface_commit_subsurface_order(surface);
1627
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001628 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001629}
1630
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001631static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001632weston_subsurface_commit(struct weston_subsurface *sub);
1633
1634static void
1635weston_subsurface_parent_commit(struct weston_subsurface *sub,
1636 int parent_is_synchronized);
1637
1638static void
1639surface_commit(struct wl_client *client, struct wl_resource *resource)
1640{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001641 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001642 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1643
1644 if (sub) {
1645 weston_subsurface_commit(sub);
1646 return;
1647 }
1648
1649 weston_surface_commit(surface);
1650
1651 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1652 if (sub->surface != surface)
1653 weston_subsurface_parent_commit(sub, 0);
1654 }
1655}
1656
1657static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001658surface_set_buffer_transform(struct wl_client *client,
1659 struct wl_resource *resource, int transform)
1660{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001661 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001662
1663 surface->pending.buffer_transform = transform;
1664}
1665
Alexander Larsson4ea95522013-05-22 14:41:37 +02001666static void
1667surface_set_buffer_scale(struct wl_client *client,
1668 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001669 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02001670{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001671 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02001672
1673 surface->pending.buffer_scale = scale;
1674}
1675
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001676static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001677 surface_destroy,
1678 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001679 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001680 surface_frame,
1681 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001682 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001683 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001684 surface_set_buffer_transform,
1685 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001686};
1687
1688static void
1689compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001690 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001691{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04001692 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001693 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001694
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001695 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001696 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001697 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001698 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001699 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001700
Jason Ekstranda85118c2013-06-27 20:17:02 -05001701 surface->resource =
1702 wl_resource_create(client, &wl_surface_interface,
1703 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001704 if (surface->resource == NULL) {
1705 weston_surface_destroy(surface);
1706 wl_resource_post_no_memory(resource);
1707 return;
1708 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05001709 wl_resource_set_implementation(surface->resource, &surface_interface,
1710 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001711}
1712
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001713static void
1714destroy_region(struct wl_resource *resource)
1715{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001716 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001717
1718 pixman_region32_fini(&region->region);
1719 free(region);
1720}
1721
1722static void
1723region_destroy(struct wl_client *client, struct wl_resource *resource)
1724{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001725 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001726}
1727
1728static void
1729region_add(struct wl_client *client, struct wl_resource *resource,
1730 int32_t x, int32_t y, int32_t width, int32_t height)
1731{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001732 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001733
1734 pixman_region32_union_rect(&region->region, &region->region,
1735 x, y, width, height);
1736}
1737
1738static void
1739region_subtract(struct wl_client *client, struct wl_resource *resource,
1740 int32_t x, int32_t y, int32_t width, int32_t height)
1741{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001742 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001743 pixman_region32_t rect;
1744
1745 pixman_region32_init_rect(&rect, x, y, width, height);
1746 pixman_region32_subtract(&region->region, &region->region, &rect);
1747 pixman_region32_fini(&rect);
1748}
1749
1750static const struct wl_region_interface region_interface = {
1751 region_destroy,
1752 region_add,
1753 region_subtract
1754};
1755
1756static void
1757compositor_create_region(struct wl_client *client,
1758 struct wl_resource *resource, uint32_t id)
1759{
1760 struct weston_region *region;
1761
1762 region = malloc(sizeof *region);
1763 if (region == NULL) {
1764 wl_resource_post_no_memory(resource);
1765 return;
1766 }
1767
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001768 pixman_region32_init(&region->region);
1769
Jason Ekstranda85118c2013-06-27 20:17:02 -05001770 region->resource =
1771 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001772 if (region->resource == NULL) {
1773 free(region);
1774 wl_resource_post_no_memory(resource);
1775 return;
1776 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05001777 wl_resource_set_implementation(region->resource, &region_interface,
1778 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001779}
1780
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001781static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001782 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001783 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001784};
1785
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001786static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001787weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
1788{
1789 struct weston_surface *surface = sub->surface;
1790 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001791 int surface_width = 0;
1792 int surface_height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001793
Alexander Larsson4ea95522013-05-22 14:41:37 +02001794 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001795 surface->buffer_transform = sub->cached.buffer_transform;
1796
Alexander Larsson4ea95522013-05-22 14:41:37 +02001797 /* wl_surface.set_buffer_scale */
1798 surface->buffer_scale = sub->cached.buffer_scale;
1799
Pekka Paalanene67858b2013-04-25 13:57:42 +03001800 /* wl_surface.attach */
1801 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
1802 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
1803 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
1804
1805 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001806 surface_width = weston_surface_buffer_width(surface);
1807 surface_height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001808 }
1809
1810 if (surface->configure && sub->cached.newly_attached)
1811 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001812 surface_width, surface_height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001813 sub->cached.sx = 0;
1814 sub->cached.sy = 0;
1815 sub->cached.newly_attached = 0;
1816
1817 /* wl_surface.damage */
1818 pixman_region32_union(&surface->damage, &surface->damage,
1819 &sub->cached.damage);
1820 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1821 0, 0,
1822 surface->geometry.width,
1823 surface->geometry.height);
1824 empty_region(&sub->cached.damage);
1825
1826 /* wl_surface.set_opaque_region */
1827 pixman_region32_init_rect(&opaque, 0, 0,
1828 surface->geometry.width,
1829 surface->geometry.height);
1830 pixman_region32_intersect(&opaque,
1831 &opaque, &sub->cached.opaque);
1832
1833 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1834 pixman_region32_copy(&surface->opaque, &opaque);
1835 weston_surface_geometry_dirty(surface);
1836 }
1837
1838 pixman_region32_fini(&opaque);
1839
1840 /* wl_surface.set_input_region */
1841 pixman_region32_fini(&surface->input);
1842 pixman_region32_init_rect(&surface->input, 0, 0,
1843 surface->geometry.width,
1844 surface->geometry.height);
1845 pixman_region32_intersect(&surface->input,
1846 &surface->input, &sub->cached.input);
1847
1848 /* wl_surface.frame */
1849 wl_list_insert_list(&surface->frame_callback_list,
1850 &sub->cached.frame_callback_list);
1851 wl_list_init(&sub->cached.frame_callback_list);
1852
1853 weston_surface_commit_subsurface_order(surface);
1854
1855 weston_surface_schedule_repaint(surface);
1856
1857 sub->cached.has_data = 0;
1858}
1859
1860static void
1861weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
1862{
1863 struct weston_surface *surface = sub->surface;
1864
1865 /*
1866 * If this commit would cause the surface to move by the
1867 * attach(dx, dy) parameters, the old damage region must be
1868 * translated to correspond to the new surface coordinate system
1869 * origin.
1870 */
1871 pixman_region32_translate(&sub->cached.damage,
1872 -surface->pending.sx, -surface->pending.sy);
1873 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
1874 &surface->pending.damage);
1875 empty_region(&surface->pending.damage);
1876
1877 if (surface->pending.newly_attached) {
1878 sub->cached.newly_attached = 1;
1879 weston_buffer_reference(&sub->cached.buffer_ref,
1880 surface->pending.buffer);
1881 }
1882 sub->cached.sx += surface->pending.sx;
1883 sub->cached.sy += surface->pending.sy;
1884 surface->pending.sx = 0;
1885 surface->pending.sy = 0;
1886 surface->pending.newly_attached = 0;
1887
1888 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001889 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001890
1891 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
1892
1893 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
1894
1895 wl_list_insert_list(&sub->cached.frame_callback_list,
1896 &surface->pending.frame_callback_list);
1897 wl_list_init(&surface->pending.frame_callback_list);
1898
1899 sub->cached.has_data = 1;
1900}
1901
1902static int
1903weston_subsurface_is_synchronized(struct weston_subsurface *sub)
1904{
1905 while (sub) {
1906 if (sub->synchronized)
1907 return 1;
1908
1909 if (!sub->parent)
1910 return 0;
1911
1912 sub = weston_surface_to_subsurface(sub->parent);
1913 }
1914
1915 return 0;
1916}
1917
1918static void
1919weston_subsurface_commit(struct weston_subsurface *sub)
1920{
1921 struct weston_surface *surface = sub->surface;
1922 struct weston_subsurface *tmp;
1923
1924 /* Recursive check for effectively synchronized. */
1925 if (weston_subsurface_is_synchronized(sub)) {
1926 weston_subsurface_commit_to_cache(sub);
1927 } else {
1928 if (sub->cached.has_data) {
1929 /* flush accumulated state from cache */
1930 weston_subsurface_commit_to_cache(sub);
1931 weston_subsurface_commit_from_cache(sub);
1932 } else {
1933 weston_surface_commit(surface);
1934 }
1935
1936 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1937 if (tmp->surface != surface)
1938 weston_subsurface_parent_commit(tmp, 0);
1939 }
1940 }
1941}
1942
1943static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001944weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001945{
1946 struct weston_surface *surface = sub->surface;
1947 struct weston_subsurface *tmp;
1948
Pekka Paalanene67858b2013-04-25 13:57:42 +03001949 /* From now on, commit_from_cache the whole sub-tree, regardless of
1950 * the synchronized mode of each child. This sub-surface or some
1951 * of its ancestors were synchronized, so we are synchronized
1952 * all the way down.
1953 */
1954
1955 if (sub->cached.has_data)
1956 weston_subsurface_commit_from_cache(sub);
1957
1958 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1959 if (tmp->surface != surface)
1960 weston_subsurface_parent_commit(tmp, 1);
1961 }
1962}
1963
1964static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001965weston_subsurface_parent_commit(struct weston_subsurface *sub,
1966 int parent_is_synchronized)
1967{
1968 if (sub->position.set) {
1969 weston_surface_set_position(sub->surface,
1970 sub->position.x, sub->position.y);
1971 sub->position.set = 0;
1972 }
1973
1974 if (parent_is_synchronized || sub->synchronized)
1975 weston_subsurface_synchronized_commit(sub);
1976}
1977
1978static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001979subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
1980 int32_t width, int32_t height)
1981{
1982 struct weston_compositor *compositor = surface->compositor;
1983
1984 weston_surface_configure(surface,
1985 surface->geometry.x + dx,
1986 surface->geometry.y + dy,
1987 width, height);
1988
1989 /* No need to check parent mappedness, because if parent is not
1990 * mapped, parent is not in a visible layer, so this sub-surface
1991 * will not be drawn either.
1992 */
1993 if (!weston_surface_is_mapped(surface)) {
1994 wl_list_init(&surface->layer_link);
1995
1996 /* Cannot call weston_surface_update_transform(),
1997 * because that would call it also for the parent surface,
1998 * which might not be mapped yet. That would lead to
1999 * inconsistent state, where the window could never be
2000 * mapped.
2001 *
2002 * Instead just assing any output, to make
2003 * weston_surface_is_mapped() return true, so that when the
2004 * parent surface does get mapped, this one will get
2005 * included, too. See surface_list_add().
2006 */
2007 assert(!wl_list_empty(&compositor->output_list));
2008 surface->output = container_of(compositor->output_list.next,
2009 struct weston_output, link);
2010 }
2011}
2012
2013static struct weston_subsurface *
2014weston_surface_to_subsurface(struct weston_surface *surface)
2015{
2016 if (surface->configure == subsurface_configure)
2017 return surface->configure_private;
2018
2019 return NULL;
2020}
2021
Pekka Paalanen01388e22013-04-25 13:57:44 +03002022WL_EXPORT struct weston_surface *
2023weston_surface_get_main_surface(struct weston_surface *surface)
2024{
2025 struct weston_subsurface *sub;
2026
2027 while (surface && (sub = weston_surface_to_subsurface(surface)))
2028 surface = sub->parent;
2029
2030 return surface;
2031}
2032
Pekka Paalanene67858b2013-04-25 13:57:42 +03002033static void
2034subsurface_set_position(struct wl_client *client,
2035 struct wl_resource *resource, int32_t x, int32_t y)
2036{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002037 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002038
2039 if (!sub)
2040 return;
2041
2042 sub->position.x = x;
2043 sub->position.y = y;
2044 sub->position.set = 1;
2045}
2046
2047static struct weston_subsurface *
2048subsurface_from_surface(struct weston_surface *surface)
2049{
2050 struct weston_subsurface *sub;
2051
2052 sub = weston_surface_to_subsurface(surface);
2053 if (sub)
2054 return sub;
2055
2056 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2057 if (sub->surface == surface)
2058 return sub;
2059
2060 return NULL;
2061}
2062
2063static struct weston_subsurface *
2064subsurface_sibling_check(struct weston_subsurface *sub,
2065 struct weston_surface *surface,
2066 const char *request)
2067{
2068 struct weston_subsurface *sibling;
2069
2070 sibling = subsurface_from_surface(surface);
2071
2072 if (!sibling) {
2073 wl_resource_post_error(sub->resource,
2074 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2075 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002076 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002077 return NULL;
2078 }
2079
2080 if (sibling->parent != sub->parent) {
2081 wl_resource_post_error(sub->resource,
2082 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2083 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002084 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002085 return NULL;
2086 }
2087
2088 return sibling;
2089}
2090
2091static void
2092subsurface_place_above(struct wl_client *client,
2093 struct wl_resource *resource,
2094 struct wl_resource *sibling_resource)
2095{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002096 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002097 struct weston_surface *surface =
2098 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002099 struct weston_subsurface *sibling;
2100
2101 if (!sub)
2102 return;
2103
2104 sibling = subsurface_sibling_check(sub, surface, "place_above");
2105 if (!sibling)
2106 return;
2107
2108 wl_list_remove(&sub->parent_link_pending);
2109 wl_list_insert(sibling->parent_link_pending.prev,
2110 &sub->parent_link_pending);
2111}
2112
2113static void
2114subsurface_place_below(struct wl_client *client,
2115 struct wl_resource *resource,
2116 struct wl_resource *sibling_resource)
2117{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002118 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002119 struct weston_surface *surface =
2120 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002121 struct weston_subsurface *sibling;
2122
2123 if (!sub)
2124 return;
2125
2126 sibling = subsurface_sibling_check(sub, surface, "place_below");
2127 if (!sibling)
2128 return;
2129
2130 wl_list_remove(&sub->parent_link_pending);
2131 wl_list_insert(&sibling->parent_link_pending,
2132 &sub->parent_link_pending);
2133}
2134
2135static void
2136subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2137{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002138 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002139
2140 if (sub)
2141 sub->synchronized = 1;
2142}
2143
2144static void
2145subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2146{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002147 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002148
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002149 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002150 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002151
2152 /* If sub became effectively desynchronized, flush. */
2153 if (!weston_subsurface_is_synchronized(sub))
2154 weston_subsurface_synchronized_commit(sub);
2155 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002156}
2157
2158static void
2159weston_subsurface_cache_init(struct weston_subsurface *sub)
2160{
2161 pixman_region32_init(&sub->cached.damage);
2162 pixman_region32_init(&sub->cached.opaque);
2163 pixman_region32_init(&sub->cached.input);
2164 wl_list_init(&sub->cached.frame_callback_list);
2165 sub->cached.buffer_ref.buffer = NULL;
2166}
2167
2168static void
2169weston_subsurface_cache_fini(struct weston_subsurface *sub)
2170{
2171 struct weston_frame_callback *cb, *tmp;
2172
2173 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002174 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002175
2176 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2177 pixman_region32_fini(&sub->cached.damage);
2178 pixman_region32_fini(&sub->cached.opaque);
2179 pixman_region32_fini(&sub->cached.input);
2180}
2181
2182static void
2183weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2184{
2185 wl_list_remove(&sub->parent_link);
2186 wl_list_remove(&sub->parent_link_pending);
2187 wl_list_remove(&sub->parent_destroy_listener.link);
2188 sub->parent = NULL;
2189}
2190
2191static void
2192weston_subsurface_destroy(struct weston_subsurface *sub);
2193
2194static void
2195subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2196{
2197 struct weston_subsurface *sub =
2198 container_of(listener, struct weston_subsurface,
2199 surface_destroy_listener);
2200 assert(data == &sub->surface->resource);
2201
2202 /* The protocol object (wl_resource) is left inert. */
2203 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002204 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002205
2206 weston_subsurface_destroy(sub);
2207}
2208
2209static void
2210subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2211{
2212 struct weston_subsurface *sub =
2213 container_of(listener, struct weston_subsurface,
2214 parent_destroy_listener);
2215 assert(data == &sub->parent->resource);
2216 assert(sub->surface != sub->parent);
2217
2218 if (weston_surface_is_mapped(sub->surface))
2219 weston_surface_unmap(sub->surface);
2220
2221 weston_subsurface_unlink_parent(sub);
2222}
2223
2224static void
2225subsurface_resource_destroy(struct wl_resource *resource)
2226{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002227 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002228
2229 if (sub)
2230 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002231}
2232
2233static void
2234subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2235{
2236 wl_resource_destroy(resource);
2237}
2238
2239static void
2240weston_subsurface_link_parent(struct weston_subsurface *sub,
2241 struct weston_surface *parent)
2242{
2243 sub->parent = parent;
2244 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002245 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002246 &sub->parent_destroy_listener);
2247
2248 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2249 wl_list_insert(&parent->subsurface_list_pending,
2250 &sub->parent_link_pending);
2251}
2252
2253static void
2254weston_subsurface_link_surface(struct weston_subsurface *sub,
2255 struct weston_surface *surface)
2256{
2257 sub->surface = surface;
2258 sub->surface_destroy_listener.notify =
2259 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002260 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002261 &sub->surface_destroy_listener);
2262}
2263
2264static void
2265weston_subsurface_destroy(struct weston_subsurface *sub)
2266{
2267 assert(sub->surface);
2268
2269 if (sub->resource) {
2270 assert(weston_surface_to_subsurface(sub->surface) == sub);
2271 assert(sub->parent_destroy_listener.notify ==
2272 subsurface_handle_parent_destroy);
2273
2274 weston_surface_set_transform_parent(sub->surface, NULL);
2275 if (sub->parent)
2276 weston_subsurface_unlink_parent(sub);
2277
2278 weston_subsurface_cache_fini(sub);
2279
2280 sub->surface->configure = NULL;
2281 sub->surface->configure_private = NULL;
2282 } else {
2283 /* the dummy weston_subsurface for the parent itself */
2284 assert(sub->parent_destroy_listener.notify == NULL);
2285 wl_list_remove(&sub->parent_link);
2286 wl_list_remove(&sub->parent_link_pending);
2287 }
2288
2289 wl_list_remove(&sub->surface_destroy_listener.link);
2290 free(sub);
2291}
2292
2293static const struct wl_subsurface_interface subsurface_implementation = {
2294 subsurface_destroy,
2295 subsurface_set_position,
2296 subsurface_place_above,
2297 subsurface_place_below,
2298 subsurface_set_sync,
2299 subsurface_set_desync
2300};
2301
2302static struct weston_subsurface *
2303weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2304 struct weston_surface *parent)
2305{
2306 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002307 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002308
2309 sub = calloc(1, sizeof *sub);
2310 if (!sub)
2311 return NULL;
2312
Jason Ekstranda85118c2013-06-27 20:17:02 -05002313 sub->resource =
2314 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002315 if (!sub->resource) {
2316 free(sub);
2317 return NULL;
2318 }
2319
Jason Ekstranda85118c2013-06-27 20:17:02 -05002320 wl_resource_set_implementation(sub->resource,
2321 &subsurface_implementation,
2322 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002323 weston_subsurface_link_surface(sub, surface);
2324 weston_subsurface_link_parent(sub, parent);
2325 weston_subsurface_cache_init(sub);
2326 sub->synchronized = 1;
2327 weston_surface_set_transform_parent(surface, parent);
2328
2329 return sub;
2330}
2331
2332/* Create a dummy subsurface for having the parent itself in its
2333 * sub-surface lists. Makes stacking order manipulation easy.
2334 */
2335static struct weston_subsurface *
2336weston_subsurface_create_for_parent(struct weston_surface *parent)
2337{
2338 struct weston_subsurface *sub;
2339
2340 sub = calloc(1, sizeof *sub);
2341 if (!sub)
2342 return NULL;
2343
2344 weston_subsurface_link_surface(sub, parent);
2345 sub->parent = parent;
2346 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2347 wl_list_insert(&parent->subsurface_list_pending,
2348 &sub->parent_link_pending);
2349
2350 return sub;
2351}
2352
2353static void
2354subcompositor_get_subsurface(struct wl_client *client,
2355 struct wl_resource *resource,
2356 uint32_t id,
2357 struct wl_resource *surface_resource,
2358 struct wl_resource *parent_resource)
2359{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002360 struct weston_surface *surface =
2361 wl_resource_get_user_data(surface_resource);
2362 struct weston_surface *parent =
2363 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002364 struct weston_subsurface *sub;
2365 static const char where[] = "get_subsurface: wl_subsurface@";
2366
2367 if (surface == parent) {
2368 wl_resource_post_error(resource,
2369 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2370 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002371 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002372 return;
2373 }
2374
2375 if (weston_surface_to_subsurface(surface)) {
2376 wl_resource_post_error(resource,
2377 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2378 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002379 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002380 return;
2381 }
2382
2383 if (surface->configure) {
2384 wl_resource_post_error(resource,
2385 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2386 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002387 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002388 return;
2389 }
2390
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002391 if (weston_surface_get_main_surface(parent) == surface) {
2392 wl_resource_post_error(resource,
2393 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2394 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002395 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002396 return;
2397 }
2398
Pekka Paalanene67858b2013-04-25 13:57:42 +03002399 /* make sure the parent is in its own list */
2400 if (wl_list_empty(&parent->subsurface_list)) {
2401 if (!weston_subsurface_create_for_parent(parent)) {
2402 wl_resource_post_no_memory(resource);
2403 return;
2404 }
2405 }
2406
2407 sub = weston_subsurface_create(id, surface, parent);
2408 if (!sub) {
2409 wl_resource_post_no_memory(resource);
2410 return;
2411 }
2412
2413 surface->configure = subsurface_configure;
2414 surface->configure_private = sub;
2415}
2416
2417static void
2418subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2419{
2420 wl_resource_destroy(resource);
2421}
2422
2423static const struct wl_subcompositor_interface subcompositor_interface = {
2424 subcompositor_destroy,
2425 subcompositor_get_subsurface
2426};
2427
2428static void
2429bind_subcompositor(struct wl_client *client,
2430 void *data, uint32_t version, uint32_t id)
2431{
2432 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002433 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002434
Jason Ekstranda85118c2013-06-27 20:17:02 -05002435 resource =
2436 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002437 if (resource == NULL) {
2438 wl_client_post_no_memory(client);
2439 return;
2440 }
2441 wl_resource_set_implementation(resource, &subcompositor_interface,
2442 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002443}
2444
2445static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002446weston_compositor_dpms(struct weston_compositor *compositor,
2447 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002448{
2449 struct weston_output *output;
2450
2451 wl_list_for_each(output, &compositor->output_list, link)
2452 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002453 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002454}
2455
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002456WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002457weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002458{
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002459 switch (compositor->state) {
2460 case WESTON_COMPOSITOR_SLEEPING:
2461 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2462 /* fall through */
2463 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002464 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002465 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002466 /* fall through */
2467 default:
2468 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2469 wl_event_source_timer_update(compositor->idle_source,
2470 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002471 }
2472}
2473
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002474WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002475weston_compositor_offscreen(struct weston_compositor *compositor)
2476{
2477 switch (compositor->state) {
2478 case WESTON_COMPOSITOR_OFFSCREEN:
2479 return;
2480 case WESTON_COMPOSITOR_SLEEPING:
2481 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2482 /* fall through */
2483 default:
2484 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2485 wl_event_source_timer_update(compositor->idle_source, 0);
2486 }
2487}
2488
2489WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002490weston_compositor_sleep(struct weston_compositor *compositor)
2491{
2492 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2493 return;
2494
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002495 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002496 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2497 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2498}
2499
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002500static int
2501idle_handler(void *data)
2502{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002503 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002504
2505 if (compositor->idle_inhibit)
2506 return 1;
2507
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002508 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002509 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002510
2511 return 1;
2512}
2513
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002514WL_EXPORT void
2515weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2516{
2517 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002518 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002519 plane->x = x;
2520 plane->y = y;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002521
2522 /* Init the link so that the call to wl_list_remove() when releasing
2523 * the plane without ever stacking doesn't lead to a crash */
2524 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002525}
2526
2527WL_EXPORT void
2528weston_plane_release(struct weston_plane *plane)
2529{
2530 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002531 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002532
2533 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002534}
2535
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002536WL_EXPORT void
2537weston_compositor_stack_plane(struct weston_compositor *ec,
2538 struct weston_plane *plane,
2539 struct weston_plane *above)
2540{
2541 if (above)
2542 wl_list_insert(above->link.prev, &plane->link);
2543 else
2544 wl_list_insert(&ec->plane_list, &plane->link);
2545}
2546
Casey Dahlin9074db52012-04-19 22:50:09 -04002547static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002548{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002549 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002550}
2551
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002552static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002553bind_output(struct wl_client *client,
2554 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002555{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002556 struct weston_output *output = data;
2557 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002558 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002559
Jason Ekstranda85118c2013-06-27 20:17:02 -05002560 resource = wl_resource_create(client, &wl_output_interface,
2561 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002562 if (resource == NULL) {
2563 wl_client_post_no_memory(client);
2564 return;
2565 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002566
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002567 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002568 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002569
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002570 wl_output_send_geometry(resource,
2571 output->x,
2572 output->y,
2573 output->mm_width,
2574 output->mm_height,
2575 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002576 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002577 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002578 if (version >= 2)
2579 wl_output_send_scale(resource,
2580 output->scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002581
2582 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002583 wl_output_send_mode(resource,
2584 mode->flags,
2585 mode->width,
2586 mode->height,
2587 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002588 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002589
2590 if (version >= 2)
2591 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002592}
2593
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002594WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002595weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002596{
Richard Hughes64ddde12013-05-01 21:52:10 +01002597 wl_signal_emit(&output->destroy_signal, output);
2598
Richard Hughesafe690c2013-05-02 10:10:04 +01002599 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002600 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002601 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002602 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002603
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002604 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002605}
2606
Scott Moreau1bad5db2012-08-18 01:04:05 -06002607static void
2608weston_output_compute_transform(struct weston_output *output)
2609{
2610 struct weston_matrix transform;
2611 int flip;
2612
2613 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002614 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002615
2616 switch(output->transform) {
2617 case WL_OUTPUT_TRANSFORM_FLIPPED:
2618 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2619 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2620 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002621 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002622 flip = -1;
2623 break;
2624 default:
2625 flip = 1;
2626 break;
2627 }
2628
2629 switch(output->transform) {
2630 case WL_OUTPUT_TRANSFORM_NORMAL:
2631 case WL_OUTPUT_TRANSFORM_FLIPPED:
2632 transform.d[0] = flip;
2633 transform.d[1] = 0;
2634 transform.d[4] = 0;
2635 transform.d[5] = 1;
2636 break;
2637 case WL_OUTPUT_TRANSFORM_90:
2638 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2639 transform.d[0] = 0;
2640 transform.d[1] = -flip;
2641 transform.d[4] = 1;
2642 transform.d[5] = 0;
2643 break;
2644 case WL_OUTPUT_TRANSFORM_180:
2645 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2646 transform.d[0] = -flip;
2647 transform.d[1] = 0;
2648 transform.d[4] = 0;
2649 transform.d[5] = -1;
2650 break;
2651 case WL_OUTPUT_TRANSFORM_270:
2652 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2653 transform.d[0] = 0;
2654 transform.d[1] = flip;
2655 transform.d[4] = -1;
2656 transform.d[5] = 0;
2657 break;
2658 default:
2659 break;
2660 }
2661
2662 weston_matrix_multiply(&output->matrix, &transform);
2663}
2664
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002665WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002666weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002667{
Scott Moreau850ca422012-05-21 15:21:25 -06002668 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002669 struct weston_matrix camera;
2670 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002671
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002672 weston_matrix_init(&output->matrix);
2673 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002674 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2675 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002676
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002677 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002678 2.0 / (output->width + output->border.left + output->border.right),
2679 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2680
2681 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002682
Scott Moreauccbf29d2012-02-22 14:21:41 -07002683 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002684 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002685 weston_matrix_init(&camera);
2686 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002687 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002688 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002689 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002690 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002691 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002692 weston_matrix_multiply(&output->matrix, &modelview);
2693 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002694
Scott Moreauccbf29d2012-02-22 14:21:41 -07002695 output->dirty = 0;
2696}
2697
Scott Moreau1bad5db2012-08-18 01:04:05 -06002698static void
Alexander Larsson0b135062013-05-28 16:23:36 +02002699weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002700{
2701 output->transform = transform;
2702
2703 switch (transform) {
2704 case WL_OUTPUT_TRANSFORM_90:
2705 case WL_OUTPUT_TRANSFORM_270:
2706 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2707 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2708 /* Swap width and height */
2709 output->width = output->current->height;
2710 output->height = output->current->width;
2711 break;
2712 case WL_OUTPUT_TRANSFORM_NORMAL:
2713 case WL_OUTPUT_TRANSFORM_180:
2714 case WL_OUTPUT_TRANSFORM_FLIPPED:
2715 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2716 output->width = output->current->width;
2717 output->height = output->current->height;
2718 break;
2719 default:
2720 break;
2721 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06002722
Alexander Larsson4ea95522013-05-22 14:41:37 +02002723 output->scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02002724 output->width /= scale;
2725 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002726}
2727
Scott Moreauccbf29d2012-02-22 14:21:41 -07002728WL_EXPORT void
2729weston_output_move(struct weston_output *output, int x, int y)
2730{
2731 output->x = x;
2732 output->y = y;
2733
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002734 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002735 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002736 output->width,
2737 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002738}
2739
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002740WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002741weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02002742 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002743 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002744{
2745 output->compositor = c;
2746 output->x = x;
2747 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002748 output->border.top = 0;
2749 output->border.bottom = 0;
2750 output->border.left = 0;
2751 output->border.right = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02002752 output->mm_width = mm_width;
2753 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002754 output->dirty = 1;
Alexander Larssone32c3762013-05-28 16:23:37 +02002755 output->origin_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002756
Alexander Larsson0b135062013-05-28 16:23:36 +02002757 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06002758 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002759
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002760 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002761 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002762
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002763 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01002764 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002765 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002766 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002767
Casey Dahlin58ba1372012-04-19 22:50:08 -04002768 output->id = ffs(~output->compositor->output_id_pool) - 1;
2769 output->compositor->output_id_pool |= 1 << output->id;
2770
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002771 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002772 wl_global_create(c->wl_display, &wl_output_interface, 2,
2773 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01002774 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002775}
2776
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002777WL_EXPORT void
2778weston_output_transform_coordinate(struct weston_output *output,
2779 int device_x, int device_y,
2780 wl_fixed_t *x, wl_fixed_t *y)
2781{
2782 wl_fixed_t tx, ty;
2783 wl_fixed_t width, height;
2784
2785 width = wl_fixed_from_int(output->width * output->scale - 1);
2786 height = wl_fixed_from_int(output->height * output->scale - 1);
2787
2788 switch(output->transform) {
2789 case WL_OUTPUT_TRANSFORM_NORMAL:
2790 default:
2791 tx = wl_fixed_from_int(device_x);
2792 ty = wl_fixed_from_int(device_y);
2793 break;
2794 case WL_OUTPUT_TRANSFORM_90:
2795 tx = wl_fixed_from_int(device_y);
2796 ty = height - wl_fixed_from_int(device_x);
2797 break;
2798 case WL_OUTPUT_TRANSFORM_180:
2799 tx = width - wl_fixed_from_int(device_x);
2800 ty = height - wl_fixed_from_int(device_y);
2801 break;
2802 case WL_OUTPUT_TRANSFORM_270:
2803 tx = width - wl_fixed_from_int(device_y);
2804 ty = wl_fixed_from_int(device_x);
2805 break;
2806 case WL_OUTPUT_TRANSFORM_FLIPPED:
2807 tx = width - wl_fixed_from_int(device_x);
2808 ty = wl_fixed_from_int(device_y);
2809 break;
2810 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2811 tx = width - wl_fixed_from_int(device_y);
2812 ty = height - wl_fixed_from_int(device_x);
2813 break;
2814 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2815 tx = wl_fixed_from_int(device_x);
2816 ty = height - wl_fixed_from_int(device_y);
2817 break;
2818 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2819 tx = wl_fixed_from_int(device_y);
2820 ty = wl_fixed_from_int(device_x);
2821 break;
2822 }
2823
2824 *x = tx / output->scale + wl_fixed_from_int(output->x);
2825 *y = ty / output->scale + wl_fixed_from_int(output->y);
2826}
2827
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002828static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002829compositor_bind(struct wl_client *client,
2830 void *data, uint32_t version, uint32_t id)
2831{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002832 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002833 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002834
Jason Ekstranda85118c2013-06-27 20:17:02 -05002835 resource = wl_resource_create(client, &wl_compositor_interface,
2836 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002837 if (resource == NULL) {
2838 wl_client_post_no_memory(client);
2839 return;
2840 }
2841
2842 wl_resource_set_implementation(resource, &compositor_interface,
2843 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05002844}
2845
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002846static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002847log_uname(void)
2848{
2849 struct utsname usys;
2850
2851 uname(&usys);
2852
2853 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2854 usys.version, usys.machine);
2855}
2856
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002857WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02002858weston_environment_get_fd(const char *env)
2859{
2860 char *e, *end;
2861 int fd, flags;
2862
2863 e = getenv(env);
2864 if (!e)
2865 return -1;
2866 fd = strtol(e, &end, 0);
2867 if (*end != '\0')
2868 return -1;
2869
2870 flags = fcntl(fd, F_GETFD);
2871 if (flags == -1)
2872 return -1;
2873
2874 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
2875 unsetenv(env);
2876
2877 return fd;
2878}
2879
2880WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002881weston_compositor_init(struct weston_compositor *ec,
2882 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002883 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002884 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002885{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002886 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002887 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002888 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002889
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002890 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002891 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002892 wl_signal_init(&ec->destroy_signal);
2893 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002894 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002895 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002896 wl_signal_init(&ec->idle_signal);
2897 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002898 wl_signal_init(&ec->show_input_panel_signal);
2899 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02002900 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002901 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01002902 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002903
Casey Dahlin58ba1372012-04-19 22:50:08 -04002904 ec->output_id_pool = 0;
2905
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002906 if (!wl_global_create(display, &wl_compositor_interface, 3,
2907 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05002908 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002909
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002910 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
2911 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03002912 return -1;
2913
Daniel Stone725c2c32012-06-22 14:04:36 +01002914 wl_list_init(&ec->surface_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002915 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002916 wl_list_init(&ec->layer_list);
2917 wl_list_init(&ec->seat_list);
2918 wl_list_init(&ec->output_list);
2919 wl_list_init(&ec->key_binding_list);
2920 wl_list_init(&ec->button_binding_list);
2921 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002922 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002923
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002924 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002925 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002926
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002927 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
2928 weston_config_section_get_string(s, "keymap_rules",
2929 (char **) &xkb_names.rules, NULL);
2930 weston_config_section_get_string(s, "keymap_model",
2931 (char **) &xkb_names.model, NULL);
2932 weston_config_section_get_string(s, "keymap_layout",
2933 (char **) &xkb_names.layout, NULL);
2934 weston_config_section_get_string(s, "keymap_variant",
2935 (char **) &xkb_names.variant, NULL);
2936 weston_config_section_get_string(s, "keymap_options",
2937 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01002938
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002939 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
2940 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01002941
2942 ec->ping_handler = NULL;
2943
2944 screenshooter_create(ec);
2945 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002946 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002947
2948 wl_data_device_manager_init(ec->wl_display);
2949
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002950 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002951
Daniel Stone725c2c32012-06-22 14:04:36 +01002952 loop = wl_display_get_event_loop(ec->wl_display);
2953 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2954 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2955
2956 ec->input_loop = wl_event_loop_create();
2957
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002958 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2959 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2960
2961 weston_compositor_schedule_repaint(ec);
2962
Daniel Stone725c2c32012-06-22 14:04:36 +01002963 return 0;
2964}
2965
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002966WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002967weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002968{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002969 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002970
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002971 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002972 if (ec->input_loop_source)
2973 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002974
Matt Roper361d2ad2011-08-29 13:52:23 -07002975 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002976 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002977 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002978
Daniel Stone325fc2d2012-05-30 16:31:58 +01002979 weston_binding_list_destroy_all(&ec->key_binding_list);
2980 weston_binding_list_destroy_all(&ec->button_binding_list);
2981 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002982 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002983
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002984 weston_plane_release(&ec->primary_plane);
2985
Jonas Ådahlc97af922012-03-28 22:36:09 +02002986 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07002987
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002988 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07002989}
2990
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05002991WL_EXPORT void
2992weston_version(int *major, int *minor, int *micro)
2993{
2994 *major = WESTON_VERSION_MAJOR;
2995 *minor = WESTON_VERSION_MINOR;
2996 *micro = WESTON_VERSION_MICRO;
2997}
2998
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002999static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003000 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003001 const char *desc;
3002} capability_strings[] = {
3003 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003004 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003005};
3006
3007static void
3008weston_compositor_log_capabilities(struct weston_compositor *compositor)
3009{
3010 unsigned i;
3011 int yes;
3012
3013 weston_log("Compositor capabilities:\n");
3014 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3015 yes = compositor->capabilities & capability_strings[i].bit;
3016 weston_log_continue(STAMP_SPACE "%s %s\n",
3017 capability_strings[i].desc,
3018 yes ? "yes" : "no");
3019 }
3020}
3021
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003022static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003023{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003024 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003025
Martin Minarik6d118362012-06-07 18:01:59 +02003026 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003027 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003028
3029 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003030}
3031
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003032#ifdef HAVE_LIBUNWIND
3033
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003034static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003035print_backtrace(void)
3036{
3037 unw_cursor_t cursor;
3038 unw_context_t context;
3039 unw_word_t off;
3040 unw_proc_info_t pip;
3041 int ret, i = 0;
3042 char procname[256];
3043 const char *filename;
3044 Dl_info dlinfo;
3045
3046 pip.unwind_info = NULL;
3047 ret = unw_getcontext(&context);
3048 if (ret) {
3049 weston_log("unw_getcontext: %d\n", ret);
3050 return;
3051 }
3052
3053 ret = unw_init_local(&cursor, &context);
3054 if (ret) {
3055 weston_log("unw_init_local: %d\n", ret);
3056 return;
3057 }
3058
3059 ret = unw_step(&cursor);
3060 while (ret > 0) {
3061 ret = unw_get_proc_info(&cursor, &pip);
3062 if (ret) {
3063 weston_log("unw_get_proc_info: %d\n", ret);
3064 break;
3065 }
3066
3067 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3068 if (ret && ret != -UNW_ENOMEM) {
3069 if (ret != -UNW_EUNSPEC)
3070 weston_log("unw_get_proc_name: %d\n", ret);
3071 procname[0] = '?';
3072 procname[1] = 0;
3073 }
3074
3075 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3076 *dlinfo.dli_fname)
3077 filename = dlinfo.dli_fname;
3078 else
3079 filename = "?";
3080
3081 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3082 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3083
3084 ret = unw_step(&cursor);
3085 if (ret < 0)
3086 weston_log("unw_step: %d\n", ret);
3087 }
3088}
3089
3090#else
3091
3092static void
3093print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003094{
3095 void *buffer[32];
3096 int i, count;
3097 Dl_info info;
3098
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003099 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3100 for (i = 0; i < count; i++) {
3101 dladdr(buffer[i], &info);
3102 weston_log(" [%016lx] %s (%s)\n",
3103 (long) buffer[i],
3104 info.dli_sname ? info.dli_sname : "--",
3105 info.dli_fname);
3106 }
3107}
3108
3109#endif
3110
3111static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003112on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003113{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003114 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003115 * then call the backend restore function, which will switch
3116 * back to the vt we launched from or ungrab X etc and then
3117 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003118 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003119 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003120 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003121
Peter Maatmane5b42e42013-03-27 22:38:53 +01003122 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003123
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003124 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003125
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003126 segv_compositor->restore(segv_compositor);
3127
3128 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003129}
3130
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003131static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003132load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003133{
3134 char path[PATH_MAX];
3135 void *module, *init;
3136
3137 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003138 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003139 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003140 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003141
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003142 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3143 if (module) {
3144 weston_log("Module '%s' already loaded\n", path);
3145 dlclose(module);
3146 return NULL;
3147 }
3148
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003149 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003150 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003151 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003152 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003153 return NULL;
3154 }
3155
3156 init = dlsym(module, entrypoint);
3157 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003158 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003159 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003160 return NULL;
3161 }
3162
3163 return init;
3164}
3165
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003166static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003167load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003168 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003169{
3170 const char *p, *end;
3171 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003172 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003173 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003174
3175 if (modules == NULL)
3176 return 0;
3177
3178 p = modules;
3179 while (*p) {
3180 end = strchrnul(p, ',');
3181 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3182 module_init = load_module(buffer, "module_init");
3183 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003184 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003185 p = end;
3186 while (*p == ',')
3187 p++;
3188
3189 }
3190
3191 return 0;
3192}
3193
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003194static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003195 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3196
3197static const char xdg_wrong_message[] =
3198 "fatal: environment variable XDG_RUNTIME_DIR\n"
3199 "is set to \"%s\", which is not a directory.\n";
3200
3201static const char xdg_wrong_mode_message[] =
3202 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3203 "correctly. Unix access mode must be 0700 but is %o,\n"
3204 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003205 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003206
3207static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003208 "Refer to your distribution on how to get it, or\n"
3209 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3210 "on how to implement it.\n";
3211
Martin Minarik37032f82012-06-18 20:15:18 +02003212static void
3213verify_xdg_runtime_dir(void)
3214{
3215 char *dir = getenv("XDG_RUNTIME_DIR");
3216 struct stat s;
3217
3218 if (!dir) {
3219 weston_log(xdg_error_message);
3220 weston_log_continue(xdg_detail_message);
3221 exit(EXIT_FAILURE);
3222 }
3223
3224 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3225 weston_log(xdg_wrong_message, dir);
3226 weston_log_continue(xdg_detail_message);
3227 exit(EXIT_FAILURE);
3228 }
3229
3230 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3231 weston_log(xdg_wrong_mode_message,
3232 dir, s.st_mode & 0777, s.st_uid);
3233 weston_log_continue(xdg_detail_message);
3234 }
3235}
3236
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003237static int
3238usage(int error_code)
3239{
3240 fprintf(stderr,
3241 "Usage: weston [OPTIONS]\n\n"
3242 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3243 "Weston supports multiple backends, and depending on which backend is in use\n"
3244 "different options will be accepted.\n\n"
3245
3246
3247 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003248 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003249 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003250 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3251 "\t\t\t\twayland-backend.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003252 " -S, --socket=NAME\tName of socket to listen on\n"
3253 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003254 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003255 " --log==FILE\t\tLog to the given file\n"
3256 " -h, --help\t\tThis help message\n\n");
3257
3258 fprintf(stderr,
3259 "Options for drm-backend.so:\n\n"
3260 " --connector=ID\tBring up only this connector\n"
3261 " --seat=SEAT\t\tThe seat that weston should run on\n"
3262 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003263 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003264 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3265
3266 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003267 "Options for fbdev-backend.so:\n\n"
3268 " --tty=TTY\t\tThe tty to use\n"
3269 " --device=DEVICE\tThe framebuffer device to use\n\n");
3270
3271 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003272 "Options for x11-backend.so:\n\n"
3273 " --width=WIDTH\t\tWidth of X window\n"
3274 " --height=HEIGHT\tHeight of X window\n"
3275 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003276 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003277 " --output-count=COUNT\tCreate multiple outputs\n"
3278 " --no-input\t\tDont create input devices\n\n");
3279
3280 fprintf(stderr,
3281 "Options for wayland-backend.so:\n\n"
3282 " --width=WIDTH\t\tWidth of Wayland surface\n"
3283 " --height=HEIGHT\tHeight of Wayland surface\n"
3284 " --display=DISPLAY\tWayland display to connect to\n\n");
3285
Pekka Paalanene31e0532013-05-22 18:03:07 +03003286#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3287 fprintf(stderr,
3288 "Options for rpi-backend.so:\n\n"
3289 " --tty=TTY\t\tThe tty to use\n"
3290 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3291 " --transform=TR\tThe output transformation, TR is one of:\n"
3292 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3293 "\n");
3294#endif
3295
Hardeningc077a842013-07-08 00:51:35 +02003296#if defined(BUILD_RDP_COMPOSITOR)
3297 fprintf(stderr,
3298 "Options for rdp-backend.so:\n\n"
3299 " --width=WIDTH\t\tWidth of desktop\n"
3300 " --height=HEIGHT\tHeight of desktop\n"
3301 " --extra-modes=MODES\t\n"
3302 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3303 " --address=ADDR\tThe address to bind\n"
3304 " --port=PORT\tThe port to listen on\n"
3305 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3306 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3307 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3308 "\n");
3309#endif
3310
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003311 exit(error_code);
3312}
3313
Peter Maatmane5b42e42013-03-27 22:38:53 +01003314static void
3315catch_signals(void)
3316{
3317 struct sigaction action;
3318
3319 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3320 action.sa_sigaction = on_caught_signal;
3321 sigemptyset(&action.sa_mask);
3322 sigaction(SIGSEGV, &action, NULL);
3323 sigaction(SIGABRT, &action, NULL);
3324}
3325
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003326int main(int argc, char *argv[])
3327{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003328 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003329 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003330 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003331 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003332 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003333 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003334 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003335 int *argc, char *argv[],
3336 struct weston_config *config);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003337 int i, config_fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003338 char *backend = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003339 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003340 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003341 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003342 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003343 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003344 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003345 struct weston_config *config;
3346 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003347
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003348 const struct weston_option core_options[] = {
3349 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3350 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3351 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003352 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003353 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003354 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003355 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003356 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003357
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003358 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003359
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003360 if (help)
3361 usage(EXIT_SUCCESS);
3362
Scott Moreau12245142012-08-29 15:15:58 -06003363 if (version) {
3364 printf(PACKAGE_STRING "\n");
3365 return EXIT_SUCCESS;
3366 }
3367
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003368 weston_log_file_open(log);
3369
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003370 weston_log("%s\n"
3371 STAMP_SPACE "%s\n"
3372 STAMP_SPACE "Bug reports to: %s\n"
3373 STAMP_SPACE "Build: %s\n",
3374 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003375 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003376 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003377
Martin Minarik37032f82012-06-18 20:15:18 +02003378 verify_xdg_runtime_dir();
3379
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003380 display = wl_display_create();
3381
Tiago Vignatti2116b892011-08-08 05:52:59 -07003382 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003383 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3384 display);
3385 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3386 display);
3387 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3388 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003389
3390 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003391 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3392 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003393
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003394 if (!backend) {
3395 if (getenv("WAYLAND_DISPLAY"))
3396 backend = "wayland-backend.so";
3397 else if (getenv("DISPLAY"))
3398 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003399 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003400 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003401 }
3402
Ossama Othmana50e6e42013-05-14 09:48:26 -07003403 config_fd = open_config_file("weston.ini");
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003404 config = weston_config_parse(config_fd);
3405 close(config_fd);
3406
3407 section = weston_config_get_section(config, "core", NULL, NULL);
3408 weston_config_section_get_string(section, "modules",
3409 &modules, "desktop-shell.so");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003410
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003411 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003412 if (!backend_init)
3413 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003414
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003415 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003416 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003417 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003418 exit(EXIT_FAILURE);
3419 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003420
Peter Maatmane5b42e42013-03-27 22:38:53 +01003421 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003422 segv_compositor = ec;
3423
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003424 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003425
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003426 setenv("WAYLAND_DISPLAY", socket_name, 1);
3427
Ossama Othmana50e6e42013-05-14 09:48:26 -07003428 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003429 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003430 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003431 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003432
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003433 for (i = 1; i < argc; i++)
3434 weston_log("fatal: unhandled option: %s\n", argv[i]);
3435 if (argc > 1) {
3436 ret = EXIT_FAILURE;
3437 goto out;
3438 }
3439
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003440 weston_compositor_log_capabilities(ec);
3441
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003442 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003443 weston_log("fatal: failed to add socket: %m\n");
3444 ret = EXIT_FAILURE;
3445 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003446 }
3447
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003448 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003449
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003450 wl_display_run(display);
3451
3452 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003453 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003454 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003455
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003456 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003457
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003458 for (i = ARRAY_LENGTH(signals); i;)
3459 wl_event_source_remove(signals[--i]);
3460
Daniel Stone855028f2012-05-01 20:37:10 +01003461 weston_compositor_xkb_destroy(ec);
3462
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003463 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003464 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003465
Martin Minarik19e6f262012-06-07 13:08:46 +02003466 weston_log_file_close();
3467
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003468 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003469}