blob: 89b9cefb0fbe04f2c8b738b493f574317728602a [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øgsberg06bc2642010-12-01 09:50:16 -050025#define _GNU_SOURCE
26
Kristian Høgsberga9410222011-01-14 17:22:35 -050027#include "config.h"
28
Daniel Stoneb7452fe2012-06-01 12:14:06 +010029#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040030#include <stdio.h>
31#include <string.h>
32#include <stdlib.h>
33#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010034#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050035#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020036#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040037#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010038#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040039#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020040#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020041#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020042#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040043#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050044#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040045#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040046#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040047#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050048#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040049#include <sys/time.h>
50#include <time.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050051
Marcin Slusarz554a0da2013-02-18 13:27:22 -050052#ifdef HAVE_LIBUNWIND
53#define UNW_LOCAL_ONLY
54#include <libunwind.h>
55#endif
56
Pekka Paalanen50719bc2011-11-22 14:18:50 +020057#include <wayland-server.h>
Kristian Høgsberg82863022010-06-04 21:52:02 -040058#include "compositor.h"
U. Artie Eoffec08f332013-05-13 15:55:47 -070059#include "subsurface-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030060#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040061#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050062#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050063
Kristian Høgsberg27da5382011-06-21 17:32:25 -040064static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040065static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040066
67static int
68sigchld_handler(int signal_number, void *data)
69{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050070 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040071 int status;
72 pid_t pid;
73
Bill Spitzak027b9622012-03-17 15:22:03 -070074 pid = waitpid(-1, &status, WNOHANG);
75 if (!pid)
76 return 1;
77
Kristian Høgsberg27da5382011-06-21 17:32:25 -040078 wl_list_for_each(p, &child_process_list, link) {
79 if (p->pid == pid)
80 break;
81 }
82
83 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020084 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040085 return 1;
86 }
87
88 wl_list_remove(&p->link);
89 p->cleanup(p, status);
90
91 return 1;
92}
93
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020094static void
95weston_output_transform_init(struct weston_output *output, uint32_t transform);
96
Alex Wu2dda6042012-04-17 17:20:47 +080097WL_EXPORT int
98weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode)
99{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200100 struct weston_seat *seat;
101 pixman_region32_t old_output_region;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200102 int ret;
103
Alex Wu2dda6042012-04-17 17:20:47 +0800104 if (!output->switch_mode)
105 return -1;
106
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200107 ret = output->switch_mode(output, mode);
108 if (ret < 0)
109 return ret;
110
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200111 pixman_region32_init(&old_output_region);
112 pixman_region32_copy(&old_output_region, &output->region);
113
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200114 /* Update output region and transformation matrix */
115 weston_output_transform_init(output, output->transform);
116
117 pixman_region32_init(&output->previous_damage);
118 pixman_region32_init_rect(&output->region, output->x, output->y,
119 output->width, output->height);
120
121 weston_output_update_matrix(output);
122
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200123 /* If a pointer falls outside the outputs new geometry, move it to its
124 * lower-right corner */
125 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400126 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200127 int32_t x, y;
128
129 if (!pointer)
130 continue;
131
132 x = wl_fixed_to_int(pointer->x);
133 y = wl_fixed_to_int(pointer->y);
134
135 if (!pixman_region32_contains_point(&old_output_region,
136 x, y, NULL) ||
137 pixman_region32_contains_point(&output->region,
138 x, y, NULL))
139 continue;
140
141 if (x >= output->x + output->width)
142 x = output->x + output->width - 1;
143 if (y >= output->y + output->height)
144 y = output->y + output->height - 1;
145
146 pointer->x = wl_fixed_from_int(x);
147 pointer->y = wl_fixed_from_int(y);
148 }
149
150 pixman_region32_fini(&old_output_region);
151
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200152 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800153}
154
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400155WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500156weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400157{
158 wl_list_insert(&child_process_list, &process->link);
159}
160
Benjamin Franzke06286262011-05-06 19:12:33 +0200161static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200162child_client_exec(int sockfd, const char *path)
163{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500164 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200165 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200166 sigset_t allsigs;
167
168 /* do not give our signal mask to the new process */
169 sigfillset(&allsigs);
170 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200171
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500172 /* Launch clients as the user. */
173 seteuid(getuid());
174
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500175 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
176 * non-CLOEXEC fd to pass through exec. */
177 clientfd = dup(sockfd);
178 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200179 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500180 return;
181 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200182
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500183 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200184 setenv("WAYLAND_SOCKET", s, 1);
185
186 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200187 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200188 path);
189}
190
191WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500192weston_client_launch(struct weston_compositor *compositor,
193 struct weston_process *proc,
194 const char *path,
195 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200196{
197 int sv[2];
198 pid_t pid;
199 struct wl_client *client;
200
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300201 weston_log("launching '%s'\n", path);
202
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300203 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200204 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200205 "socketpair failed while launching '%s': %m\n",
206 path);
207 return NULL;
208 }
209
210 pid = fork();
211 if (pid == -1) {
212 close(sv[0]);
213 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200214 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200215 "fork failed while launching '%s': %m\n", path);
216 return NULL;
217 }
218
219 if (pid == 0) {
220 child_client_exec(sv[1], path);
221 exit(-1);
222 }
223
224 close(sv[1]);
225
226 client = wl_client_create(compositor->wl_display, sv[0]);
227 if (!client) {
228 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200229 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200230 "wl_client_create failed while launching '%s'.\n",
231 path);
232 return NULL;
233 }
234
235 proc->pid = pid;
236 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500237 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200238
239 return client;
240}
241
242static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300243surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
244{
245 struct weston_surface *surface =
246 container_of(listener, struct weston_surface,
247 pending.buffer_destroy_listener);
248
249 surface->pending.buffer = NULL;
250}
251
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200252static void
253empty_region(pixman_region32_t *region)
254{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300255 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200256 pixman_region32_init(region);
257}
258
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300259static void
260region_init_infinite(pixman_region32_t *region)
261{
262 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
263 UINT32_MAX, UINT32_MAX);
264}
265
Pekka Paalanene67858b2013-04-25 13:57:42 +0300266static struct weston_subsurface *
267weston_surface_to_subsurface(struct weston_surface *surface);
268
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500269WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500270weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500271{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500272 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400273
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200274 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400275 if (surface == NULL)
276 return NULL;
277
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400278 wl_signal_init(&surface->resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500279
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500280 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500281 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500282
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400283 surface->resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400284
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500285 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400286 surface->alpha = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400287
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100288 if (compositor->renderer->create_surface(surface) < 0) {
289 free(surface);
290 return NULL;
291 }
292
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200293 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
294 surface->pending.buffer_transform = surface->buffer_transform;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400295 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400296 surface->plane = &compositor->primary_plane;
Giulio Camuffo184df502013-02-21 11:29:21 +0100297 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200298
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400299 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500300 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500301 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300302 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200303 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500304 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400305
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200306 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200307 wl_list_insert(&surface->geometry.transformation_list,
308 &surface->transform.position.link);
309 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200310 wl_list_init(&surface->geometry.child_list);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200311 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200312 surface->transform.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500313
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300314 surface->pending.buffer_destroy_listener.notify =
315 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300316 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300317 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300318 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300319 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300320
Pekka Paalanene67858b2013-04-25 13:57:42 +0300321 wl_list_init(&surface->subsurface_list);
322 wl_list_init(&surface->subsurface_list_pending);
323
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400324 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500325}
326
Alex Wu8811bf92012-02-28 18:07:54 +0800327WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500328weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200329 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500330{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100331 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500332}
333
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400334WL_EXPORT void
335weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200336 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200337{
338 if (surface->transform.enabled) {
339 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
340
341 weston_matrix_transform(&surface->transform.matrix, &v);
342
343 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200344 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700345 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200346 v.f[3]);
347 *x = 0;
348 *y = 0;
349 return;
350 }
351
352 *x = v.f[0] / v.f[3];
353 *y = v.f[1] / v.f[3];
354 } else {
355 *x = sx + surface->geometry.x;
356 *y = sy + surface->geometry.y;
357 }
358}
359
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500360WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200361weston_transformed_coord(int width, int height,
362 enum wl_output_transform transform,
363 float sx, float sy, float *bx, float *by)
364{
365 switch (transform) {
366 case WL_OUTPUT_TRANSFORM_NORMAL:
367 default:
368 *bx = sx;
369 *by = sy;
370 break;
371 case WL_OUTPUT_TRANSFORM_FLIPPED:
372 *bx = width - sx;
373 *by = sy;
374 break;
375 case WL_OUTPUT_TRANSFORM_90:
376 *bx = height - sy;
377 *by = sx;
378 break;
379 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
380 *bx = height - sy;
381 *by = width - sx;
382 break;
383 case WL_OUTPUT_TRANSFORM_180:
384 *bx = width - sx;
385 *by = height - sy;
386 break;
387 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
388 *bx = sx;
389 *by = height - sy;
390 break;
391 case WL_OUTPUT_TRANSFORM_270:
392 *bx = sy;
393 *by = width - sx;
394 break;
395 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
396 *bx = sy;
397 *by = sx;
398 break;
399 }
400}
401
402WL_EXPORT pixman_box32_t
403weston_transformed_rect(int width, int height,
404 enum wl_output_transform transform,
405 pixman_box32_t rect)
406{
407 float x1, x2, y1, y2;
408
409 pixman_box32_t ret;
410
411 weston_transformed_coord(width, height, transform,
412 rect.x1, rect.y1, &x1, &y1);
413 weston_transformed_coord(width, height, transform,
414 rect.x2, rect.y2, &x2, &y2);
415
416 if (x1 <= x2) {
417 ret.x1 = x1;
418 ret.x2 = x2;
419 } else {
420 ret.x1 = x2;
421 ret.x2 = x1;
422 }
423
424 if (y1 <= y2) {
425 ret.y1 = y1;
426 ret.y2 = y2;
427 } else {
428 ret.y1 = y2;
429 ret.y2 = y1;
430 }
431
432 return ret;
433}
434
435WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200436weston_surface_to_buffer_float(struct weston_surface *surface,
437 float sx, float sy, float *bx, float *by)
438{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200439 weston_transformed_coord(surface->geometry.width,
440 surface->geometry.height,
441 surface->buffer_transform,
442 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200443}
444
445WL_EXPORT pixman_box32_t
446weston_surface_to_buffer_rect(struct weston_surface *surface,
447 pixman_box32_t rect)
448{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200449 return weston_transformed_rect(surface->geometry.width,
450 surface->geometry.height,
451 surface->buffer_transform, rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200452}
453
454WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400455weston_surface_move_to_plane(struct weston_surface *surface,
456 struct weston_plane *plane)
457{
458 if (surface->plane == plane)
459 return;
460
461 weston_surface_damage_below(surface);
462 surface->plane = plane;
463 weston_surface_damage(surface);
464}
465
466WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500467weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200468{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500469 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200470
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500471 pixman_region32_init(&damage);
472 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
473 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400474 pixman_region32_union(&surface->plane->damage,
475 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500476 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200477}
478
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400479static struct wl_resource *
480find_resource_for_client(struct wl_list *list, struct wl_client *client)
481{
482 struct wl_resource *r;
483
484 wl_list_for_each(r, list, link) {
485 if (r->client == client)
486 return r;
487 }
488
489 return NULL;
490}
491
492static void
493weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
494{
495 uint32_t different = es->output_mask ^ mask;
496 uint32_t entered = mask & different;
497 uint32_t left = es->output_mask & different;
498 struct weston_output *output;
499 struct wl_resource *resource = NULL;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400500 struct wl_client *client = es->resource.client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400501
502 es->output_mask = mask;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400503 if (es->resource.client == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400504 return;
505 if (different == 0)
506 return;
507
508 wl_list_for_each(output, &es->compositor->output_list, link) {
509 if (1 << output->id & different)
510 resource =
511 find_resource_for_client(&output->resource_list,
512 client);
513 if (resource == NULL)
514 continue;
515 if (1 << output->id & entered)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400516 wl_surface_send_enter(&es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400517 if (1 << output->id & left)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400518 wl_surface_send_leave(&es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400519 }
520}
521
522static void
523weston_surface_assign_output(struct weston_surface *es)
524{
525 struct weston_compositor *ec = es->compositor;
526 struct weston_output *output, *new_output;
527 pixman_region32_t region;
528 uint32_t max, area, mask;
529 pixman_box32_t *e;
530
531 new_output = NULL;
532 max = 0;
533 mask = 0;
534 pixman_region32_init(&region);
535 wl_list_for_each(output, &ec->output_list, link) {
536 pixman_region32_intersect(&region, &es->transform.boundingbox,
537 &output->region);
538
539 e = pixman_region32_extents(&region);
540 area = (e->x2 - e->x1) * (e->y2 - e->y1);
541
542 if (area > 0)
543 mask |= 1 << output->id;
544
545 if (area >= max) {
546 new_output = output;
547 max = area;
548 }
549 }
550 pixman_region32_fini(&region);
551
552 es->output = new_output;
553 weston_surface_update_output_mask(es, mask);
554}
555
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200556static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200557surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
558 int32_t width, int32_t height,
559 pixman_region32_t *bbox)
560{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200561 float min_x = HUGE_VALF, min_y = HUGE_VALF;
562 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200563 int32_t s[4][2] = {
564 { sx, sy },
565 { sx, sy + height },
566 { sx + width, sy },
567 { sx + width, sy + height }
568 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200569 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200570 int i;
571
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300572 if (width == 0 || height == 0) {
573 /* avoid rounding empty bbox to 1x1 */
574 pixman_region32_init(bbox);
575 return;
576 }
577
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200578 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200579 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400580 weston_surface_to_global_float(surface,
581 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200582 if (x < min_x)
583 min_x = x;
584 if (x > max_x)
585 max_x = x;
586 if (y < min_y)
587 min_y = y;
588 if (y > max_y)
589 max_y = y;
590 }
591
Pekka Paalanen219b9822012-02-08 15:38:37 +0200592 int_x = floorf(min_x);
593 int_y = floorf(min_y);
594 pixman_region32_init_rect(bbox, int_x, int_y,
595 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200596}
597
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200598static void
599weston_surface_update_transform_disable(struct weston_surface *surface)
600{
601 surface->transform.enabled = 0;
602
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200603 /* round off fractions when not transformed */
604 surface->geometry.x = roundf(surface->geometry.x);
605 surface->geometry.y = roundf(surface->geometry.y);
606
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500607 /* Otherwise identity matrix, but with x and y translation. */
608 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
609 surface->transform.position.matrix.d[12] = surface->geometry.x;
610 surface->transform.position.matrix.d[13] = surface->geometry.y;
611
612 surface->transform.matrix = surface->transform.position.matrix;
613
Kristian Høgsberg9bcaaeb2013-02-28 14:56:43 -0500614 surface->transform.inverse = surface->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500615 surface->transform.inverse.d[12] = -surface->geometry.x;
616 surface->transform.inverse.d[13] = -surface->geometry.y;
617
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200618 pixman_region32_init_rect(&surface->transform.boundingbox,
619 surface->geometry.x,
620 surface->geometry.y,
621 surface->geometry.width,
622 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500623
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400624 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500625 pixman_region32_copy(&surface->transform.opaque,
626 &surface->opaque);
627 pixman_region32_translate(&surface->transform.opaque,
628 surface->geometry.x,
629 surface->geometry.y);
630 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200631}
632
633static int
634weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200635{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200636 struct weston_surface *parent = surface->geometry.parent;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200637 struct weston_matrix *matrix = &surface->transform.matrix;
638 struct weston_matrix *inverse = &surface->transform.inverse;
639 struct weston_transform *tform;
640
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200641 surface->transform.enabled = 1;
642
643 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300644 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200645 surface->transform.position.matrix.d[12] = surface->geometry.x;
646 surface->transform.position.matrix.d[13] = surface->geometry.y;
647
648 weston_matrix_init(matrix);
649 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
650 weston_matrix_multiply(matrix, &tform->matrix);
651
Pekka Paalanen483243f2013-03-08 14:56:50 +0200652 if (parent)
653 weston_matrix_multiply(matrix, &parent->transform.matrix);
654
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200655 if (weston_matrix_invert(inverse, matrix) < 0) {
656 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200657 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200658 " transformation not invertible.\n", surface);
659 return -1;
660 }
661
662 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
663 surface->geometry.height,
664 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500665
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200666 return 0;
667}
668
669WL_EXPORT void
670weston_surface_update_transform(struct weston_surface *surface)
671{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200672 struct weston_surface *parent = surface->geometry.parent;
673
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200674 if (!surface->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200675 return;
676
Pekka Paalanen483243f2013-03-08 14:56:50 +0200677 if (parent)
678 weston_surface_update_transform(parent);
679
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200680 surface->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200681
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500682 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200683
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200684 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500685 pixman_region32_fini(&surface->transform.opaque);
686 pixman_region32_init(&surface->transform.opaque);
687
Pekka Paalanencd403622012-01-25 13:37:39 +0200688 /* transform.position is always in transformation_list */
689 if (surface->geometry.transformation_list.next ==
690 &surface->transform.position.link &&
691 surface->geometry.transformation_list.prev ==
Pekka Paalanen483243f2013-03-08 14:56:50 +0200692 &surface->transform.position.link &&
693 !parent) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200694 weston_surface_update_transform_disable(surface);
695 } else {
696 if (weston_surface_update_transform_enable(surface) < 0)
697 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200698 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200699
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400700 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200701
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300702 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200703}
704
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200705WL_EXPORT void
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200706weston_surface_geometry_dirty(struct weston_surface *surface)
707{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200708 struct weston_surface *child;
709
710 /*
711 * The invariant: if surface->geometry.dirty, then all surfaces
712 * in surface->geometry.child_list have geometry.dirty too.
713 * Corollary: if not parent->geometry.dirty, then all ancestors
714 * are not dirty.
715 */
716
717 if (surface->transform.dirty)
718 return;
719
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200720 surface->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200721
722 wl_list_for_each(child, &surface->geometry.child_list,
723 geometry.parent_link)
724 weston_surface_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200725}
726
727WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100728weston_surface_to_global_fixed(struct weston_surface *surface,
729 wl_fixed_t sx, wl_fixed_t sy,
730 wl_fixed_t *x, wl_fixed_t *y)
731{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200732 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100733
734 weston_surface_to_global_float(surface,
735 wl_fixed_to_double(sx),
736 wl_fixed_to_double(sy),
737 &xf, &yf);
738 *x = wl_fixed_from_double(xf);
739 *y = wl_fixed_from_double(yf);
740}
741
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400742WL_EXPORT void
743weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200744 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200745{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200746 if (surface->transform.enabled) {
747 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
748
749 weston_matrix_transform(&surface->transform.inverse, &v);
750
751 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200752 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200753 "weston_surface_from_global(), divisor = %g\n",
754 v.f[3]);
755 *sx = 0;
756 *sy = 0;
757 return;
758 }
759
Pekka Paalanencd403622012-01-25 13:37:39 +0200760 *sx = v.f[0] / v.f[3];
761 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200762 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200763 *sx = x - surface->geometry.x;
764 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200765 }
766}
767
768WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100769weston_surface_from_global_fixed(struct weston_surface *surface,
770 wl_fixed_t x, wl_fixed_t y,
771 wl_fixed_t *sx, wl_fixed_t *sy)
772{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200773 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100774
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400775 weston_surface_from_global_float(surface,
776 wl_fixed_to_double(x),
777 wl_fixed_to_double(y),
778 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100779 *sx = wl_fixed_from_double(sxf);
780 *sy = wl_fixed_from_double(syf);
781}
782
783WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200784weston_surface_from_global(struct weston_surface *surface,
785 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
786{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200787 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200788
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400789 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200790 *sx = floorf(sxf);
791 *sy = floorf(syf);
792}
793
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400794WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400795weston_surface_schedule_repaint(struct weston_surface *surface)
796{
797 struct weston_output *output;
798
799 wl_list_for_each(output, &surface->compositor->output_list, link)
800 if (surface->output_mask & (1 << output->id))
801 weston_output_schedule_repaint(output);
802}
803
804WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500805weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500806{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400807 pixman_region32_union_rect(&surface->damage, &surface->damage,
808 0, 0, surface->geometry.width,
809 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200810
Kristian Høgsberg98238702012-08-03 16:29:12 -0400811 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500812}
813
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400814WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500815weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200816 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400817{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200818 surface->geometry.x = x;
819 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200820 surface->geometry.width = width;
821 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200822 weston_surface_geometry_dirty(surface);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400823}
824
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200825WL_EXPORT void
826weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200827 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200828{
829 surface->geometry.x = x;
830 surface->geometry.y = y;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200831 weston_surface_geometry_dirty(surface);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200832}
833
Pekka Paalanen483243f2013-03-08 14:56:50 +0200834static void
835transform_parent_handle_parent_destroy(struct wl_listener *listener,
836 void *data)
837{
838 struct weston_surface *surface =
839 container_of(listener, struct weston_surface,
840 geometry.parent_destroy_listener);
841
842 weston_surface_set_transform_parent(surface, NULL);
843}
844
845WL_EXPORT void
846weston_surface_set_transform_parent(struct weston_surface *surface,
847 struct weston_surface *parent)
848{
849 if (surface->geometry.parent) {
850 wl_list_remove(&surface->geometry.parent_destroy_listener.link);
851 wl_list_remove(&surface->geometry.parent_link);
852 }
853
854 surface->geometry.parent = parent;
855
856 surface->geometry.parent_destroy_listener.notify =
857 transform_parent_handle_parent_destroy;
858 if (parent) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400859 wl_signal_add(&parent->resource.destroy_signal,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200860 &surface->geometry.parent_destroy_listener);
861 wl_list_insert(&parent->geometry.child_list,
862 &surface->geometry.parent_link);
863 }
864
865 weston_surface_geometry_dirty(surface);
866}
867
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300868WL_EXPORT int
869weston_surface_is_mapped(struct weston_surface *surface)
870{
871 if (surface->output)
872 return 1;
873 else
874 return 0;
875}
876
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200877WL_EXPORT int32_t
878weston_surface_buffer_width(struct weston_surface *surface)
879{
880 switch (surface->buffer_transform) {
881 case WL_OUTPUT_TRANSFORM_90:
882 case WL_OUTPUT_TRANSFORM_270:
883 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
884 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200885 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200886 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200887 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200888 }
889}
890
891WL_EXPORT int32_t
892weston_surface_buffer_height(struct weston_surface *surface)
893{
894 switch (surface->buffer_transform) {
895 case WL_OUTPUT_TRANSFORM_90:
896 case WL_OUTPUT_TRANSFORM_270:
897 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
898 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200899 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200900 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200901 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200902 }
903}
904
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400905WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500906weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500907{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400908 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500909
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400910 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500911
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400912 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500913}
914
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400915WL_EXPORT struct weston_surface *
Tiago Vignatti9d393522012-02-10 16:26:19 +0200916weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100917 wl_fixed_t x, wl_fixed_t y,
918 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200919{
920 struct weston_surface *surface;
921
922 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100923 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500924 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100925 wl_fixed_to_int(*sx),
926 wl_fixed_to_int(*sy),
927 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200928 return surface;
929 }
930
931 return NULL;
932}
933
934static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500935weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400936{
Daniel Stone37816df2012-05-16 18:45:18 +0100937 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400938
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500939 if (!compositor->focus)
940 return;
941
Daniel Stone37816df2012-05-16 18:45:18 +0100942 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400943 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400944}
945
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400946WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500947weston_surface_unmap(struct weston_surface *surface)
948{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100949 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500950
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500951 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500952 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500953 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500954
Daniel Stone4dab5db2012-05-30 16:31:53 +0100955 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400956 if (seat->keyboard && seat->keyboard->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400957 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400958 if (seat->pointer && seat->pointer->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400959 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400960 NULL,
961 wl_fixed_from_int(0),
962 wl_fixed_from_int(0));
Daniel Stone4dab5db2012-05-30 16:31:53 +0100963 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500964
Kristian Høgsberg98238702012-08-03 16:29:12 -0400965 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500966}
967
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400968struct weston_frame_callback {
969 struct wl_resource resource;
970 struct wl_list link;
971};
972
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500973static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400974destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500975{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500976 struct weston_surface *surface =
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400977 container_of(resource, struct weston_surface, resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500978 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400979 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400980
Pekka Paalanen483243f2013-03-08 14:56:50 +0200981 assert(wl_list_empty(&surface->geometry.child_list));
Pekka Paalanene67858b2013-04-25 13:57:42 +0300982 assert(wl_list_empty(&surface->subsurface_list_pending));
983 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +0200984
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300985 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500986 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400987
Pekka Paalanenbc106382012-10-10 12:49:31 +0300988 wl_list_for_each_safe(cb, next,
989 &surface->pending.frame_callback_list, link)
990 wl_resource_destroy(&cb->resource);
991
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300992 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300993 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300994 pixman_region32_fini(&surface->pending.damage);
995
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300996 if (surface->pending.buffer)
997 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
998
Pekka Paalanende685b82012-12-04 15:58:12 +0200999 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001000
Kristian Høgsberg42263852012-09-06 21:59:29 -04001001 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001002
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001003 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001004 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001005 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001006 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001007 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001008
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001009 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
1010 wl_resource_destroy(&cb->resource);
1011
Pekka Paalanen483243f2013-03-08 14:56:50 +02001012 weston_surface_set_transform_parent(surface, NULL);
1013
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001014 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001015}
1016
Alex Wu8811bf92012-02-28 18:07:54 +08001017WL_EXPORT void
1018weston_surface_destroy(struct weston_surface *surface)
1019{
1020 /* Not a valid way to destroy a client surface */
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001021 assert(surface->resource.client == NULL);
Alex Wu8811bf92012-02-28 18:07:54 +08001022
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001023 wl_signal_emit(&surface->resource.destroy_signal, &surface->resource);
1024 destroy_surface(&surface->resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001025}
1026
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001027static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001028weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1029 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001030{
Pekka Paalanende685b82012-12-04 15:58:12 +02001031 struct weston_buffer_reference *ref =
1032 container_of(listener, struct weston_buffer_reference,
1033 destroy_listener);
1034
1035 assert((struct wl_buffer *)data == ref->buffer);
1036 ref->buffer = NULL;
1037}
1038
1039WL_EXPORT void
1040weston_buffer_reference(struct weston_buffer_reference *ref,
1041 struct wl_buffer *buffer)
1042{
1043 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001044 ref->buffer->busy_count--;
1045 if (ref->buffer->busy_count == 0) {
1046 assert(ref->buffer->resource.client != NULL);
1047 wl_resource_queue_event(&ref->buffer->resource,
1048 WL_BUFFER_RELEASE);
1049 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001050 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001051 }
1052
Pekka Paalanende685b82012-12-04 15:58:12 +02001053 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001054 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001055 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001056 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001057 }
1058
Pekka Paalanende685b82012-12-04 15:58:12 +02001059 ref->buffer = buffer;
1060 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1061}
1062
1063static void
1064weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
1065{
1066 weston_buffer_reference(&surface->buffer_ref, buffer);
1067
Pekka Paalanena6421c42012-12-04 15:58:10 +02001068 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001069 if (weston_surface_is_mapped(surface))
1070 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001071 }
1072
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001073 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001074}
1075
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001076WL_EXPORT void
1077weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001078{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001079 wl_list_remove(&surface->layer_link);
1080 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001081 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001082 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001083}
1084
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001085WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001086weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001087{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001088 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001089
1090 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001091 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001092}
1093
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001094WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001095weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001096{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001097 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001098
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001099 pixman_region32_union(&compositor->primary_plane.damage,
1100 &compositor->primary_plane.damage,
1101 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001102 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001103}
1104
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001105static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001106surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001107 pixman_region32_t *opaque)
1108{
Pekka Paalanende685b82012-12-04 15:58:12 +02001109 if (surface->buffer_ref.buffer &&
1110 wl_buffer_is_shm(surface->buffer_ref.buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001111 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001112
1113 if (surface->transform.enabled) {
1114 pixman_box32_t *extents;
1115
1116 extents = pixman_region32_extents(&surface->damage);
1117 surface_compute_bbox(surface, extents->x1, extents->y1,
1118 extents->x2 - extents->x1,
1119 extents->y2 - extents->y1,
1120 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001121 pixman_region32_translate(&surface->damage,
1122 -surface->plane->x,
1123 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001124 } else {
1125 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001126 surface->geometry.x - surface->plane->x,
1127 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001128 }
1129
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001130 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001131 pixman_region32_union(&surface->plane->damage,
1132 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001133 empty_region(&surface->damage);
1134 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001135 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001136}
1137
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001138static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001139compositor_accumulate_damage(struct weston_compositor *ec)
1140{
1141 struct weston_plane *plane;
1142 struct weston_surface *es;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001143 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001144
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001145 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001146
1147 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001148 pixman_region32_copy(&plane->clip, &clip);
1149
1150 pixman_region32_init(&opaque);
1151
1152 wl_list_for_each(es, &ec->surface_list, link) {
1153 if (es->plane != plane)
1154 continue;
1155
1156 surface_accumulate_damage(es, &opaque);
1157 }
1158
1159 pixman_region32_union(&clip, &clip, &opaque);
1160 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001161 }
1162
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001163 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001164
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001165 wl_list_for_each(es, &ec->surface_list, link) {
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001166 /* Both the renderer and the backend have seen the buffer
1167 * by now. If renderer needs the buffer, it has its own
1168 * reference set. If the backend wants to keep the buffer
1169 * around for migrating the surface into a non-primary plane
1170 * later, keep_buffer is true. Otherwise, drop the core
1171 * reference now, and allow early buffer release. This enables
1172 * clients to use single-buffering.
1173 */
1174 if (!es->keep_buffer)
1175 weston_buffer_reference(&es->buffer_ref, NULL);
1176 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001177}
1178
1179static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001180surface_list_add(struct weston_compositor *compositor,
1181 struct weston_surface *surface)
1182{
1183 struct weston_subsurface *sub;
1184
1185 if (wl_list_empty(&surface->subsurface_list)) {
1186 weston_surface_update_transform(surface);
1187 wl_list_insert(compositor->surface_list.prev, &surface->link);
1188 return;
1189 }
1190
1191 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1192 if (!weston_surface_is_mapped(sub->surface))
1193 continue;
1194
1195 if (sub->surface == surface) {
1196 weston_surface_update_transform(sub->surface);
1197 wl_list_insert(compositor->surface_list.prev,
1198 &sub->surface->link);
1199 } else {
1200 surface_list_add(compositor, sub->surface);
1201 }
1202 }
1203}
1204
1205static void
1206weston_compositor_build_surface_list(struct weston_compositor *compositor)
1207{
1208 struct weston_surface *surface;
1209 struct weston_layer *layer;
1210
1211 wl_list_init(&compositor->surface_list);
1212 wl_list_for_each(layer, &compositor->layer_list, link) {
1213 wl_list_for_each(surface, &layer->surface_list, layer_link) {
1214 surface_list_add(compositor, surface);
1215 }
1216 }
1217}
1218
1219static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001220weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001221{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001222 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001223 struct weston_surface *es;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001224 struct weston_animation *animation, *next;
1225 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001226 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001227 pixman_region32_t output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001228
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001229 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001230 weston_compositor_build_surface_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001231
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001232 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001233 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001234 else
1235 wl_list_for_each(es, &ec->surface_list, link)
1236 weston_surface_move_to_plane(es, &ec->primary_plane);
1237
Pekka Paalanene67858b2013-04-25 13:57:42 +03001238 wl_list_init(&frame_callback_list);
1239 wl_list_for_each(es, &ec->surface_list, link) {
1240 if (es->output == output) {
1241 wl_list_insert_list(&frame_callback_list,
1242 &es->frame_callback_list);
1243 wl_list_init(&es->frame_callback_list);
1244 }
1245 }
1246
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001247 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001248
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001249 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001250 pixman_region32_intersect(&output_damage,
1251 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001252 pixman_region32_subtract(&output_damage,
1253 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001254
Scott Moreauccbf29d2012-02-22 14:21:41 -07001255 if (output->dirty)
1256 weston_output_update_matrix(output);
1257
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001258 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001259
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001260 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001261
Kristian Høgsbergef044142011-06-21 15:02:12 -04001262 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001263
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001264 weston_compositor_repick(ec);
1265 wl_event_loop_dispatch(ec->input_loop, 0);
1266
Jonas Ådahldb773762012-06-13 00:01:21 +02001267 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001268 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001269 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001270 }
1271
Scott Moreaud64cf212012-06-08 19:40:54 -06001272 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001273 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001274 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001275 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001276}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001277
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001278static int
1279weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001280{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001281 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001282
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001283 wl_event_loop_dispatch(compositor->input_loop, 0);
1284
1285 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001286}
1287
1288WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001289weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001290{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001291 struct weston_compositor *compositor = output->compositor;
1292 struct wl_event_loop *loop =
1293 wl_display_get_event_loop(compositor->wl_display);
1294 int fd;
1295
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001296 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001297 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001298 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001299 return;
1300 }
1301
1302 output->repaint_scheduled = 0;
1303 if (compositor->input_loop_source)
1304 return;
1305
1306 fd = wl_event_loop_get_fd(compositor->input_loop);
1307 compositor->input_loop_source =
1308 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1309 weston_compositor_read_input, compositor);
1310}
1311
1312static void
1313idle_repaint(void *data)
1314{
1315 struct weston_output *output = data;
1316
Jonas Ådahle5a12252013-04-05 23:07:11 +02001317 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001318}
1319
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001320WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001321weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1322{
1323 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001324 if (below != NULL)
1325 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001326}
1327
1328WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001329weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001330{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001331 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001332 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001333
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001334 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1335 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001336 return;
1337
Kristian Høgsbergef044142011-06-21 15:02:12 -04001338 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001339 output->repaint_needed = 1;
1340 if (output->repaint_scheduled)
1341 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001342
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001343 wl_event_loop_add_idle(loop, idle_repaint, output);
1344 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001345
1346 if (compositor->input_loop_source) {
1347 wl_event_source_remove(compositor->input_loop_source);
1348 compositor->input_loop_source = NULL;
1349 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001350}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001351
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001352WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001353weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1354{
1355 struct weston_output *output;
1356
1357 wl_list_for_each(output, &compositor->output_list, link)
1358 weston_output_schedule_repaint(output);
1359}
1360
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001361static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001362surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001363{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001364 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001365}
1366
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001367static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001368surface_attach(struct wl_client *client,
1369 struct wl_resource *resource,
1370 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1371{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001372 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001373 struct wl_buffer *buffer = NULL;
1374
1375 if (buffer_resource)
1376 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001377
Pekka Paalanende685b82012-12-04 15:58:12 +02001378 /* Attach, attach, without commit in between does not send
1379 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001380 if (surface->pending.buffer)
1381 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001382
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001383 surface->pending.sx = sx;
1384 surface->pending.sy = sy;
1385 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001386 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001387 if (buffer) {
1388 wl_signal_add(&buffer->resource.destroy_signal,
1389 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001390 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001391}
1392
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001393static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001394surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001395 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001396 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001397{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001398 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001399
Pekka Paalanen8e159182012-10-10 12:49:25 +03001400 pixman_region32_union_rect(&surface->pending.damage,
1401 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001402 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001403}
1404
Kristian Høgsberg33418202011-08-16 23:01:28 -04001405static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001406destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001407{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001408 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001409
1410 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001411 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001412}
1413
1414static void
1415surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001416 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001417{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001418 struct weston_frame_callback *cb;
Pekka Paalanenbc106382012-10-10 12:49:31 +03001419 struct weston_surface *surface = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001420
1421 cb = malloc(sizeof *cb);
1422 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001423 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001424 return;
1425 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001426
Kristian Høgsberg33418202011-08-16 23:01:28 -04001427 cb->resource.object.interface = &wl_callback_interface;
1428 cb->resource.object.id = callback;
1429 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001430 cb->resource.client = client;
1431 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001432
1433 wl_client_add_resource(client, &cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001434 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001435}
1436
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001437static void
1438surface_set_opaque_region(struct wl_client *client,
1439 struct wl_resource *resource,
1440 struct wl_resource *region_resource)
1441{
1442 struct weston_surface *surface = resource->data;
1443 struct weston_region *region;
1444
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001445 if (region_resource) {
1446 region = region_resource->data;
Pekka Paalanen512dde82012-10-10 12:49:27 +03001447 pixman_region32_copy(&surface->pending.opaque,
1448 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001449 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001450 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001451 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001452}
1453
1454static void
1455surface_set_input_region(struct wl_client *client,
1456 struct wl_resource *resource,
1457 struct wl_resource *region_resource)
1458{
1459 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001460 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001461
1462 if (region_resource) {
1463 region = region_resource->data;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001464 pixman_region32_copy(&surface->pending.input,
1465 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001466 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001467 pixman_region32_fini(&surface->pending.input);
1468 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001469 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001470}
1471
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001472static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001473weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001474{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001475 struct weston_subsurface *sub;
1476
1477 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1478 parent_link_pending) {
1479 wl_list_remove(&sub->parent_link);
1480 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1481 }
1482}
1483
1484static void
1485weston_surface_commit(struct weston_surface *surface)
1486{
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001487 pixman_region32_t opaque;
Giulio Camuffo184df502013-02-21 11:29:21 +01001488 int buffer_width = 0;
1489 int buffer_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001490
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001491 /* wl_surface.set_buffer_rotation */
1492 surface->buffer_transform = surface->pending.buffer_transform;
1493
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001494 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001495 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001496 weston_surface_attach(surface, surface->pending.buffer);
1497
Giulio Camuffo184df502013-02-21 11:29:21 +01001498 if (surface->buffer_ref.buffer) {
1499 buffer_width = weston_surface_buffer_width(surface);
1500 buffer_height = weston_surface_buffer_height(surface);
1501 }
1502
1503 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001504 surface->configure(surface,
1505 surface->pending.sx, surface->pending.sy,
1506 buffer_width, buffer_height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001507
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001508 if (surface->pending.buffer)
1509 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1510 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001511 surface->pending.sx = 0;
1512 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001513 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001514
1515 /* wl_surface.damage */
1516 pixman_region32_union(&surface->damage, &surface->damage,
1517 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001518 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1519 0, 0,
1520 surface->geometry.width,
1521 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001522 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001523
1524 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001525 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001526 surface->geometry.width,
1527 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001528 pixman_region32_intersect(&opaque,
1529 &opaque, &surface->pending.opaque);
1530
1531 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1532 pixman_region32_copy(&surface->opaque, &opaque);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001533 weston_surface_geometry_dirty(surface);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001534 }
1535
1536 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001537
1538 /* wl_surface.set_input_region */
1539 pixman_region32_fini(&surface->input);
1540 pixman_region32_init_rect(&surface->input, 0, 0,
1541 surface->geometry.width,
1542 surface->geometry.height);
1543 pixman_region32_intersect(&surface->input,
1544 &surface->input, &surface->pending.input);
1545
Pekka Paalanenbc106382012-10-10 12:49:31 +03001546 /* wl_surface.frame */
1547 wl_list_insert_list(&surface->frame_callback_list,
1548 &surface->pending.frame_callback_list);
1549 wl_list_init(&surface->pending.frame_callback_list);
1550
Pekka Paalanene67858b2013-04-25 13:57:42 +03001551 weston_surface_commit_subsurface_order(surface);
1552
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001553 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001554}
1555
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001556static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001557weston_subsurface_commit(struct weston_subsurface *sub);
1558
1559static void
1560weston_subsurface_parent_commit(struct weston_subsurface *sub,
1561 int parent_is_synchronized);
1562
1563static void
1564surface_commit(struct wl_client *client, struct wl_resource *resource)
1565{
1566 struct weston_surface *surface = resource->data;
1567 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1568
1569 if (sub) {
1570 weston_subsurface_commit(sub);
1571 return;
1572 }
1573
1574 weston_surface_commit(surface);
1575
1576 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1577 if (sub->surface != surface)
1578 weston_subsurface_parent_commit(sub, 0);
1579 }
1580}
1581
1582static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001583surface_set_buffer_transform(struct wl_client *client,
1584 struct wl_resource *resource, int transform)
1585{
1586 struct weston_surface *surface = resource->data;
1587
1588 surface->pending.buffer_transform = transform;
1589}
1590
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001591static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001592 surface_destroy,
1593 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001594 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001595 surface_frame,
1596 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001597 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001598 surface_commit,
1599 surface_set_buffer_transform
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001600};
1601
1602static void
1603compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001604 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001605{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001606 struct weston_compositor *ec = resource->data;
1607 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001608
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001609 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001610 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001611 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001612 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001613 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001614
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001615 surface->resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001616
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001617 surface->resource.object.id = id;
1618 surface->resource.object.interface = &wl_surface_interface;
1619 surface->resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001620 (void (**)(void)) &surface_interface;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001621 surface->resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001622
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001623 wl_client_add_resource(client, &surface->resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001624}
1625
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001626static void
1627destroy_region(struct wl_resource *resource)
1628{
1629 struct weston_region *region =
1630 container_of(resource, struct weston_region, resource);
1631
1632 pixman_region32_fini(&region->region);
1633 free(region);
1634}
1635
1636static void
1637region_destroy(struct wl_client *client, struct wl_resource *resource)
1638{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001639 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001640}
1641
1642static void
1643region_add(struct wl_client *client, struct wl_resource *resource,
1644 int32_t x, int32_t y, int32_t width, int32_t height)
1645{
1646 struct weston_region *region = resource->data;
1647
1648 pixman_region32_union_rect(&region->region, &region->region,
1649 x, y, width, height);
1650}
1651
1652static void
1653region_subtract(struct wl_client *client, struct wl_resource *resource,
1654 int32_t x, int32_t y, int32_t width, int32_t height)
1655{
1656 struct weston_region *region = resource->data;
1657 pixman_region32_t rect;
1658
1659 pixman_region32_init_rect(&rect, x, y, width, height);
1660 pixman_region32_subtract(&region->region, &region->region, &rect);
1661 pixman_region32_fini(&rect);
1662}
1663
1664static const struct wl_region_interface region_interface = {
1665 region_destroy,
1666 region_add,
1667 region_subtract
1668};
1669
1670static void
1671compositor_create_region(struct wl_client *client,
1672 struct wl_resource *resource, uint32_t id)
1673{
1674 struct weston_region *region;
1675
1676 region = malloc(sizeof *region);
1677 if (region == NULL) {
1678 wl_resource_post_no_memory(resource);
1679 return;
1680 }
1681
1682 region->resource.destroy = destroy_region;
1683
1684 region->resource.object.id = id;
1685 region->resource.object.interface = &wl_region_interface;
1686 region->resource.object.implementation =
1687 (void (**)(void)) &region_interface;
1688 region->resource.data = region;
1689
1690 pixman_region32_init(&region->region);
1691
1692 wl_client_add_resource(client, &region->resource);
1693}
1694
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001695static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001696 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001697 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001698};
1699
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001700static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001701weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
1702{
1703 struct weston_surface *surface = sub->surface;
1704 pixman_region32_t opaque;
1705 int buffer_width = 0;
1706 int buffer_height = 0;
1707
1708 /* wl_surface.set_buffer_rotation */
1709 surface->buffer_transform = sub->cached.buffer_transform;
1710
1711 /* wl_surface.attach */
1712 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
1713 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
1714 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
1715
1716 if (surface->buffer_ref.buffer) {
1717 buffer_width = weston_surface_buffer_width(surface);
1718 buffer_height = weston_surface_buffer_height(surface);
1719 }
1720
1721 if (surface->configure && sub->cached.newly_attached)
1722 surface->configure(surface, sub->cached.sx, sub->cached.sy,
1723 buffer_width, buffer_height);
1724 sub->cached.sx = 0;
1725 sub->cached.sy = 0;
1726 sub->cached.newly_attached = 0;
1727
1728 /* wl_surface.damage */
1729 pixman_region32_union(&surface->damage, &surface->damage,
1730 &sub->cached.damage);
1731 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1732 0, 0,
1733 surface->geometry.width,
1734 surface->geometry.height);
1735 empty_region(&sub->cached.damage);
1736
1737 /* wl_surface.set_opaque_region */
1738 pixman_region32_init_rect(&opaque, 0, 0,
1739 surface->geometry.width,
1740 surface->geometry.height);
1741 pixman_region32_intersect(&opaque,
1742 &opaque, &sub->cached.opaque);
1743
1744 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1745 pixman_region32_copy(&surface->opaque, &opaque);
1746 weston_surface_geometry_dirty(surface);
1747 }
1748
1749 pixman_region32_fini(&opaque);
1750
1751 /* wl_surface.set_input_region */
1752 pixman_region32_fini(&surface->input);
1753 pixman_region32_init_rect(&surface->input, 0, 0,
1754 surface->geometry.width,
1755 surface->geometry.height);
1756 pixman_region32_intersect(&surface->input,
1757 &surface->input, &sub->cached.input);
1758
1759 /* wl_surface.frame */
1760 wl_list_insert_list(&surface->frame_callback_list,
1761 &sub->cached.frame_callback_list);
1762 wl_list_init(&sub->cached.frame_callback_list);
1763
1764 weston_surface_commit_subsurface_order(surface);
1765
1766 weston_surface_schedule_repaint(surface);
1767
1768 sub->cached.has_data = 0;
1769}
1770
1771static void
1772weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
1773{
1774 struct weston_surface *surface = sub->surface;
1775
1776 /*
1777 * If this commit would cause the surface to move by the
1778 * attach(dx, dy) parameters, the old damage region must be
1779 * translated to correspond to the new surface coordinate system
1780 * origin.
1781 */
1782 pixman_region32_translate(&sub->cached.damage,
1783 -surface->pending.sx, -surface->pending.sy);
1784 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
1785 &surface->pending.damage);
1786 empty_region(&surface->pending.damage);
1787
1788 if (surface->pending.newly_attached) {
1789 sub->cached.newly_attached = 1;
1790 weston_buffer_reference(&sub->cached.buffer_ref,
1791 surface->pending.buffer);
1792 }
1793 sub->cached.sx += surface->pending.sx;
1794 sub->cached.sy += surface->pending.sy;
1795 surface->pending.sx = 0;
1796 surface->pending.sy = 0;
1797 surface->pending.newly_attached = 0;
1798
1799 sub->cached.buffer_transform = surface->pending.buffer_transform;
1800
1801 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
1802
1803 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
1804
1805 wl_list_insert_list(&sub->cached.frame_callback_list,
1806 &surface->pending.frame_callback_list);
1807 wl_list_init(&surface->pending.frame_callback_list);
1808
1809 sub->cached.has_data = 1;
1810}
1811
1812static int
1813weston_subsurface_is_synchronized(struct weston_subsurface *sub)
1814{
1815 while (sub) {
1816 if (sub->synchronized)
1817 return 1;
1818
1819 if (!sub->parent)
1820 return 0;
1821
1822 sub = weston_surface_to_subsurface(sub->parent);
1823 }
1824
1825 return 0;
1826}
1827
1828static void
1829weston_subsurface_commit(struct weston_subsurface *sub)
1830{
1831 struct weston_surface *surface = sub->surface;
1832 struct weston_subsurface *tmp;
1833
1834 /* Recursive check for effectively synchronized. */
1835 if (weston_subsurface_is_synchronized(sub)) {
1836 weston_subsurface_commit_to_cache(sub);
1837 } else {
1838 if (sub->cached.has_data) {
1839 /* flush accumulated state from cache */
1840 weston_subsurface_commit_to_cache(sub);
1841 weston_subsurface_commit_from_cache(sub);
1842 } else {
1843 weston_surface_commit(surface);
1844 }
1845
1846 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1847 if (tmp->surface != surface)
1848 weston_subsurface_parent_commit(tmp, 0);
1849 }
1850 }
1851}
1852
1853static void
1854weston_subsurface_parent_commit(struct weston_subsurface *sub,
1855 int parent_is_synchronized)
1856{
1857 struct weston_surface *surface = sub->surface;
1858 struct weston_subsurface *tmp;
1859
1860 if (sub->position.set) {
1861 weston_surface_set_position(sub->surface,
1862 sub->position.x, sub->position.y);
1863 sub->position.set = 0;
1864 }
1865
1866 if (!parent_is_synchronized && !sub->synchronized)
1867 return;
1868
1869 /* From now on, commit_from_cache the whole sub-tree, regardless of
1870 * the synchronized mode of each child. This sub-surface or some
1871 * of its ancestors were synchronized, so we are synchronized
1872 * all the way down.
1873 */
1874
1875 if (sub->cached.has_data)
1876 weston_subsurface_commit_from_cache(sub);
1877
1878 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1879 if (tmp->surface != surface)
1880 weston_subsurface_parent_commit(tmp, 1);
1881 }
1882}
1883
1884static void
1885subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
1886 int32_t width, int32_t height)
1887{
1888 struct weston_compositor *compositor = surface->compositor;
1889
1890 weston_surface_configure(surface,
1891 surface->geometry.x + dx,
1892 surface->geometry.y + dy,
1893 width, height);
1894
1895 /* No need to check parent mappedness, because if parent is not
1896 * mapped, parent is not in a visible layer, so this sub-surface
1897 * will not be drawn either.
1898 */
1899 if (!weston_surface_is_mapped(surface)) {
1900 wl_list_init(&surface->layer_link);
1901
1902 /* Cannot call weston_surface_update_transform(),
1903 * because that would call it also for the parent surface,
1904 * which might not be mapped yet. That would lead to
1905 * inconsistent state, where the window could never be
1906 * mapped.
1907 *
1908 * Instead just assing any output, to make
1909 * weston_surface_is_mapped() return true, so that when the
1910 * parent surface does get mapped, this one will get
1911 * included, too. See surface_list_add().
1912 */
1913 assert(!wl_list_empty(&compositor->output_list));
1914 surface->output = container_of(compositor->output_list.next,
1915 struct weston_output, link);
1916 }
1917}
1918
1919static struct weston_subsurface *
1920weston_surface_to_subsurface(struct weston_surface *surface)
1921{
1922 if (surface->configure == subsurface_configure)
1923 return surface->configure_private;
1924
1925 return NULL;
1926}
1927
Pekka Paalanen01388e22013-04-25 13:57:44 +03001928WL_EXPORT struct weston_surface *
1929weston_surface_get_main_surface(struct weston_surface *surface)
1930{
1931 struct weston_subsurface *sub;
1932
1933 while (surface && (sub = weston_surface_to_subsurface(surface)))
1934 surface = sub->parent;
1935
1936 return surface;
1937}
1938
Pekka Paalanene67858b2013-04-25 13:57:42 +03001939static void
1940subsurface_set_position(struct wl_client *client,
1941 struct wl_resource *resource, int32_t x, int32_t y)
1942{
1943 struct weston_subsurface *sub = resource->data;
1944
1945 if (!sub)
1946 return;
1947
1948 sub->position.x = x;
1949 sub->position.y = y;
1950 sub->position.set = 1;
1951}
1952
1953static struct weston_subsurface *
1954subsurface_from_surface(struct weston_surface *surface)
1955{
1956 struct weston_subsurface *sub;
1957
1958 sub = weston_surface_to_subsurface(surface);
1959 if (sub)
1960 return sub;
1961
1962 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
1963 if (sub->surface == surface)
1964 return sub;
1965
1966 return NULL;
1967}
1968
1969static struct weston_subsurface *
1970subsurface_sibling_check(struct weston_subsurface *sub,
1971 struct weston_surface *surface,
1972 const char *request)
1973{
1974 struct weston_subsurface *sibling;
1975
1976 sibling = subsurface_from_surface(surface);
1977
1978 if (!sibling) {
1979 wl_resource_post_error(sub->resource,
1980 WL_SUBSURFACE_ERROR_BAD_SURFACE,
1981 "%s: wl_surface@%d is not a parent or sibling",
1982 request, surface->resource.object.id);
1983 return NULL;
1984 }
1985
1986 if (sibling->parent != sub->parent) {
1987 wl_resource_post_error(sub->resource,
1988 WL_SUBSURFACE_ERROR_BAD_SURFACE,
1989 "%s: wl_surface@%d has a different parent",
1990 request, surface->resource.object.id);
1991 return NULL;
1992 }
1993
1994 return sibling;
1995}
1996
1997static void
1998subsurface_place_above(struct wl_client *client,
1999 struct wl_resource *resource,
2000 struct wl_resource *sibling_resource)
2001{
2002 struct weston_subsurface *sub = resource->data;
2003 struct weston_surface *surface = sibling_resource->data;
2004 struct weston_subsurface *sibling;
2005
2006 if (!sub)
2007 return;
2008
2009 sibling = subsurface_sibling_check(sub, surface, "place_above");
2010 if (!sibling)
2011 return;
2012
2013 wl_list_remove(&sub->parent_link_pending);
2014 wl_list_insert(sibling->parent_link_pending.prev,
2015 &sub->parent_link_pending);
2016}
2017
2018static void
2019subsurface_place_below(struct wl_client *client,
2020 struct wl_resource *resource,
2021 struct wl_resource *sibling_resource)
2022{
2023 struct weston_subsurface *sub = resource->data;
2024 struct weston_surface *surface = sibling_resource->data;
2025 struct weston_subsurface *sibling;
2026
2027 if (!sub)
2028 return;
2029
2030 sibling = subsurface_sibling_check(sub, surface, "place_below");
2031 if (!sibling)
2032 return;
2033
2034 wl_list_remove(&sub->parent_link_pending);
2035 wl_list_insert(&sibling->parent_link_pending,
2036 &sub->parent_link_pending);
2037}
2038
2039static void
2040subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2041{
2042 struct weston_subsurface *sub = resource->data;
2043
2044 if (sub)
2045 sub->synchronized = 1;
2046}
2047
2048static void
2049subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2050{
2051 struct weston_subsurface *sub = resource->data;
2052
2053 if (sub)
2054 sub->synchronized = 0;
2055}
2056
2057static void
2058weston_subsurface_cache_init(struct weston_subsurface *sub)
2059{
2060 pixman_region32_init(&sub->cached.damage);
2061 pixman_region32_init(&sub->cached.opaque);
2062 pixman_region32_init(&sub->cached.input);
2063 wl_list_init(&sub->cached.frame_callback_list);
2064 sub->cached.buffer_ref.buffer = NULL;
2065}
2066
2067static void
2068weston_subsurface_cache_fini(struct weston_subsurface *sub)
2069{
2070 struct weston_frame_callback *cb, *tmp;
2071
2072 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
2073 wl_resource_destroy(&cb->resource);
2074
2075 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2076 pixman_region32_fini(&sub->cached.damage);
2077 pixman_region32_fini(&sub->cached.opaque);
2078 pixman_region32_fini(&sub->cached.input);
2079}
2080
2081static void
2082weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2083{
2084 wl_list_remove(&sub->parent_link);
2085 wl_list_remove(&sub->parent_link_pending);
2086 wl_list_remove(&sub->parent_destroy_listener.link);
2087 sub->parent = NULL;
2088}
2089
2090static void
2091weston_subsurface_destroy(struct weston_subsurface *sub);
2092
2093static void
2094subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2095{
2096 struct weston_subsurface *sub =
2097 container_of(listener, struct weston_subsurface,
2098 surface_destroy_listener);
2099 assert(data == &sub->surface->resource);
2100
2101 /* The protocol object (wl_resource) is left inert. */
2102 if (sub->resource)
2103 sub->resource->data = NULL;
2104
2105 weston_subsurface_destroy(sub);
2106}
2107
2108static void
2109subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2110{
2111 struct weston_subsurface *sub =
2112 container_of(listener, struct weston_subsurface,
2113 parent_destroy_listener);
2114 assert(data == &sub->parent->resource);
2115 assert(sub->surface != sub->parent);
2116
2117 if (weston_surface_is_mapped(sub->surface))
2118 weston_surface_unmap(sub->surface);
2119
2120 weston_subsurface_unlink_parent(sub);
2121}
2122
2123static void
2124subsurface_resource_destroy(struct wl_resource *resource)
2125{
2126 struct weston_subsurface *sub = resource->data;
2127
2128 if (sub)
2129 weston_subsurface_destroy(sub);
2130
2131 free(resource);
2132}
2133
2134static void
2135subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2136{
2137 wl_resource_destroy(resource);
2138}
2139
2140static void
2141weston_subsurface_link_parent(struct weston_subsurface *sub,
2142 struct weston_surface *parent)
2143{
2144 sub->parent = parent;
2145 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
2146 wl_signal_add(&parent->resource.destroy_signal,
2147 &sub->parent_destroy_listener);
2148
2149 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2150 wl_list_insert(&parent->subsurface_list_pending,
2151 &sub->parent_link_pending);
2152}
2153
2154static void
2155weston_subsurface_link_surface(struct weston_subsurface *sub,
2156 struct weston_surface *surface)
2157{
2158 sub->surface = surface;
2159 sub->surface_destroy_listener.notify =
2160 subsurface_handle_surface_destroy;
2161 wl_signal_add(&surface->resource.destroy_signal,
2162 &sub->surface_destroy_listener);
2163}
2164
2165static void
2166weston_subsurface_destroy(struct weston_subsurface *sub)
2167{
2168 assert(sub->surface);
2169
2170 if (sub->resource) {
2171 assert(weston_surface_to_subsurface(sub->surface) == sub);
2172 assert(sub->parent_destroy_listener.notify ==
2173 subsurface_handle_parent_destroy);
2174
2175 weston_surface_set_transform_parent(sub->surface, NULL);
2176 if (sub->parent)
2177 weston_subsurface_unlink_parent(sub);
2178
2179 weston_subsurface_cache_fini(sub);
2180
2181 sub->surface->configure = NULL;
2182 sub->surface->configure_private = NULL;
2183 } else {
2184 /* the dummy weston_subsurface for the parent itself */
2185 assert(sub->parent_destroy_listener.notify == NULL);
2186 wl_list_remove(&sub->parent_link);
2187 wl_list_remove(&sub->parent_link_pending);
2188 }
2189
2190 wl_list_remove(&sub->surface_destroy_listener.link);
2191 free(sub);
2192}
2193
2194static const struct wl_subsurface_interface subsurface_implementation = {
2195 subsurface_destroy,
2196 subsurface_set_position,
2197 subsurface_place_above,
2198 subsurface_place_below,
2199 subsurface_set_sync,
2200 subsurface_set_desync
2201};
2202
2203static struct weston_subsurface *
2204weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2205 struct weston_surface *parent)
2206{
2207 struct weston_subsurface *sub;
2208
2209 sub = calloc(1, sizeof *sub);
2210 if (!sub)
2211 return NULL;
2212
2213 sub->resource = wl_client_add_object(surface->resource.client,
2214 &wl_subsurface_interface,
2215 &subsurface_implementation,
2216 id, sub);
2217 if (!sub->resource) {
2218 free(sub);
2219 return NULL;
2220 }
2221
2222 sub->resource->destroy = subsurface_resource_destroy;
2223 weston_subsurface_link_surface(sub, surface);
2224 weston_subsurface_link_parent(sub, parent);
2225 weston_subsurface_cache_init(sub);
2226 sub->synchronized = 1;
2227 weston_surface_set_transform_parent(surface, parent);
2228
2229 return sub;
2230}
2231
2232/* Create a dummy subsurface for having the parent itself in its
2233 * sub-surface lists. Makes stacking order manipulation easy.
2234 */
2235static struct weston_subsurface *
2236weston_subsurface_create_for_parent(struct weston_surface *parent)
2237{
2238 struct weston_subsurface *sub;
2239
2240 sub = calloc(1, sizeof *sub);
2241 if (!sub)
2242 return NULL;
2243
2244 weston_subsurface_link_surface(sub, parent);
2245 sub->parent = parent;
2246 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2247 wl_list_insert(&parent->subsurface_list_pending,
2248 &sub->parent_link_pending);
2249
2250 return sub;
2251}
2252
2253static void
2254subcompositor_get_subsurface(struct wl_client *client,
2255 struct wl_resource *resource,
2256 uint32_t id,
2257 struct wl_resource *surface_resource,
2258 struct wl_resource *parent_resource)
2259{
2260 struct weston_surface *surface = surface_resource->data;
2261 struct weston_surface *parent = parent_resource->data;
2262 struct weston_subsurface *sub;
2263 static const char where[] = "get_subsurface: wl_subsurface@";
2264
2265 if (surface == parent) {
2266 wl_resource_post_error(resource,
2267 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2268 "%s%d: wl_surface@%d cannot be its own parent",
2269 where, id, surface_resource->object.id);
2270 return;
2271 }
2272
2273 if (weston_surface_to_subsurface(surface)) {
2274 wl_resource_post_error(resource,
2275 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2276 "%s%d: wl_surface@%d is already a sub-surface",
2277 where, id, surface_resource->object.id);
2278 return;
2279 }
2280
2281 if (surface->configure) {
2282 wl_resource_post_error(resource,
2283 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2284 "%s%d: wl_surface@%d already has a role",
2285 where, id, surface_resource->object.id);
2286 return;
2287 }
2288
2289 /* make sure the parent is in its own list */
2290 if (wl_list_empty(&parent->subsurface_list)) {
2291 if (!weston_subsurface_create_for_parent(parent)) {
2292 wl_resource_post_no_memory(resource);
2293 return;
2294 }
2295 }
2296
2297 sub = weston_subsurface_create(id, surface, parent);
2298 if (!sub) {
2299 wl_resource_post_no_memory(resource);
2300 return;
2301 }
2302
2303 surface->configure = subsurface_configure;
2304 surface->configure_private = sub;
2305}
2306
2307static void
2308subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2309{
2310 wl_resource_destroy(resource);
2311}
2312
2313static const struct wl_subcompositor_interface subcompositor_interface = {
2314 subcompositor_destroy,
2315 subcompositor_get_subsurface
2316};
2317
2318static void
2319bind_subcompositor(struct wl_client *client,
2320 void *data, uint32_t version, uint32_t id)
2321{
2322 struct weston_compositor *compositor = data;
2323
2324 wl_client_add_object(client, &wl_subcompositor_interface,
2325 &subcompositor_interface, id, compositor);
2326}
2327
2328static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002329weston_compositor_dpms(struct weston_compositor *compositor,
2330 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002331{
2332 struct weston_output *output;
2333
2334 wl_list_for_each(output, &compositor->output_list, link)
2335 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002336 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002337}
2338
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002339WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002340weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002341{
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002342 switch (compositor->state) {
2343 case WESTON_COMPOSITOR_SLEEPING:
2344 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2345 /* fall through */
2346 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002347 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002348 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002349 /* fall through */
2350 default:
2351 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2352 wl_event_source_timer_update(compositor->idle_source,
2353 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002354 }
2355}
2356
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002357WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002358weston_compositor_offscreen(struct weston_compositor *compositor)
2359{
2360 switch (compositor->state) {
2361 case WESTON_COMPOSITOR_OFFSCREEN:
2362 return;
2363 case WESTON_COMPOSITOR_SLEEPING:
2364 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2365 /* fall through */
2366 default:
2367 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2368 wl_event_source_timer_update(compositor->idle_source, 0);
2369 }
2370}
2371
2372WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002373weston_compositor_sleep(struct weston_compositor *compositor)
2374{
2375 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2376 return;
2377
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002378 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002379 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2380 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2381}
2382
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002383static int
2384idle_handler(void *data)
2385{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002386 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002387
2388 if (compositor->idle_inhibit)
2389 return 1;
2390
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002391 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002392 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002393
2394 return 1;
2395}
2396
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002397WL_EXPORT void
2398weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2399{
2400 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002401 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002402 plane->x = x;
2403 plane->y = y;
2404}
2405
2406WL_EXPORT void
2407weston_plane_release(struct weston_plane *plane)
2408{
2409 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002410 pixman_region32_fini(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002411}
2412
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002413WL_EXPORT void
2414weston_compositor_stack_plane(struct weston_compositor *ec,
2415 struct weston_plane *plane,
2416 struct weston_plane *above)
2417{
2418 if (above)
2419 wl_list_insert(above->link.prev, &plane->link);
2420 else
2421 wl_list_insert(&ec->plane_list, &plane->link);
2422}
2423
Casey Dahlin9074db52012-04-19 22:50:09 -04002424static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002425{
2426 wl_list_remove(&resource->link);
2427 free(resource);
2428}
2429
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002430static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002431bind_output(struct wl_client *client,
2432 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002433{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002434 struct weston_output *output = data;
2435 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002436 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002437
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002438 resource = wl_client_add_object(client,
2439 &wl_output_interface, NULL, id, data);
2440
Casey Dahlin9074db52012-04-19 22:50:09 -04002441 wl_list_insert(&output->resource_list, &resource->link);
2442 resource->destroy = unbind_resource;
2443
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002444 wl_output_send_geometry(resource,
2445 output->x,
2446 output->y,
2447 output->mm_width,
2448 output->mm_height,
2449 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002450 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002451 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002452
2453 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002454 wl_output_send_mode(resource,
2455 mode->flags,
2456 mode->width,
2457 mode->height,
2458 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002459 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002460}
2461
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002462WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002463weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002464{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002465 struct weston_compositor *c = output->compositor;
2466
Richard Hughes64ddde12013-05-01 21:52:10 +01002467 wl_signal_emit(&output->destroy_signal, output);
2468
Richard Hughesafe690c2013-05-02 10:10:04 +01002469 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002470 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002471 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002472 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002473
2474 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002475}
2476
Scott Moreau1bad5db2012-08-18 01:04:05 -06002477static void
2478weston_output_compute_transform(struct weston_output *output)
2479{
2480 struct weston_matrix transform;
2481 int flip;
2482
2483 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002484 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002485
2486 switch(output->transform) {
2487 case WL_OUTPUT_TRANSFORM_FLIPPED:
2488 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2489 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2490 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002491 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002492 flip = -1;
2493 break;
2494 default:
2495 flip = 1;
2496 break;
2497 }
2498
2499 switch(output->transform) {
2500 case WL_OUTPUT_TRANSFORM_NORMAL:
2501 case WL_OUTPUT_TRANSFORM_FLIPPED:
2502 transform.d[0] = flip;
2503 transform.d[1] = 0;
2504 transform.d[4] = 0;
2505 transform.d[5] = 1;
2506 break;
2507 case WL_OUTPUT_TRANSFORM_90:
2508 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2509 transform.d[0] = 0;
2510 transform.d[1] = -flip;
2511 transform.d[4] = 1;
2512 transform.d[5] = 0;
2513 break;
2514 case WL_OUTPUT_TRANSFORM_180:
2515 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2516 transform.d[0] = -flip;
2517 transform.d[1] = 0;
2518 transform.d[4] = 0;
2519 transform.d[5] = -1;
2520 break;
2521 case WL_OUTPUT_TRANSFORM_270:
2522 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2523 transform.d[0] = 0;
2524 transform.d[1] = flip;
2525 transform.d[4] = -1;
2526 transform.d[5] = 0;
2527 break;
2528 default:
2529 break;
2530 }
2531
2532 weston_matrix_multiply(&output->matrix, &transform);
2533}
2534
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002535WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002536weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002537{
Scott Moreau850ca422012-05-21 15:21:25 -06002538 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002539 struct weston_matrix camera;
2540 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002541
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002542 weston_matrix_init(&output->matrix);
2543 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002544 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2545 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002546
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002547 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002548 2.0 / (output->width + output->border.left + output->border.right),
2549 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2550
2551 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002552
Scott Moreauccbf29d2012-02-22 14:21:41 -07002553 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002554 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002555 weston_matrix_init(&camera);
2556 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002557 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002558 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002559 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002560 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002561 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002562 weston_matrix_multiply(&output->matrix, &modelview);
2563 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002564
Scott Moreauccbf29d2012-02-22 14:21:41 -07002565 output->dirty = 0;
2566}
2567
Scott Moreau1bad5db2012-08-18 01:04:05 -06002568static void
2569weston_output_transform_init(struct weston_output *output, uint32_t transform)
2570{
2571 output->transform = transform;
2572
2573 switch (transform) {
2574 case WL_OUTPUT_TRANSFORM_90:
2575 case WL_OUTPUT_TRANSFORM_270:
2576 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2577 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2578 /* Swap width and height */
2579 output->width = output->current->height;
2580 output->height = output->current->width;
2581 break;
2582 case WL_OUTPUT_TRANSFORM_NORMAL:
2583 case WL_OUTPUT_TRANSFORM_180:
2584 case WL_OUTPUT_TRANSFORM_FLIPPED:
2585 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2586 output->width = output->current->width;
2587 output->height = output->current->height;
2588 break;
2589 default:
2590 break;
2591 }
2592}
2593
Scott Moreauccbf29d2012-02-22 14:21:41 -07002594WL_EXPORT void
2595weston_output_move(struct weston_output *output, int x, int y)
2596{
2597 output->x = x;
2598 output->y = y;
2599
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002600 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002601 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002602 output->width,
2603 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002604}
2605
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002606WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002607weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002608 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002609{
2610 output->compositor = c;
2611 output->x = x;
2612 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002613 output->border.top = 0;
2614 output->border.bottom = 0;
2615 output->border.left = 0;
2616 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002617 output->mm_width = width;
2618 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002619 output->dirty = 1;
2620
Scott Moreau1bad5db2012-08-18 01:04:05 -06002621 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002622 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002623
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002624 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002625 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002626
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002627 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01002628 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002629 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002630 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002631
Casey Dahlin58ba1372012-04-19 22:50:08 -04002632 output->id = ffs(~output->compositor->output_id_pool) - 1;
2633 output->compositor->output_id_pool |= 1 << output->id;
2634
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002635 output->global =
2636 wl_display_add_global(c->wl_display, &wl_output_interface,
2637 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01002638 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002639}
2640
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002641static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002642compositor_bind(struct wl_client *client,
2643 void *data, uint32_t version, uint32_t id)
2644{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002645 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002646
2647 wl_client_add_object(client, &wl_compositor_interface,
2648 &compositor_interface, id, compositor);
2649}
2650
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002651static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002652log_uname(void)
2653{
2654 struct utsname usys;
2655
2656 uname(&usys);
2657
2658 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2659 usys.version, usys.machine);
2660}
2661
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002662WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02002663weston_environment_get_fd(const char *env)
2664{
2665 char *e, *end;
2666 int fd, flags;
2667
2668 e = getenv(env);
2669 if (!e)
2670 return -1;
2671 fd = strtol(e, &end, 0);
2672 if (*end != '\0')
2673 return -1;
2674
2675 flags = fcntl(fd, F_GETFD);
2676 if (flags == -1)
2677 return -1;
2678
2679 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
2680 unsetenv(env);
2681
2682 return fd;
2683}
2684
2685WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002686weston_compositor_init(struct weston_compositor *ec,
2687 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002688 int *argc, char *argv[],
Ossama Othmana50e6e42013-05-14 09:48:26 -07002689 int config_fd)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002690{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002691 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002692 struct xkb_rule_names xkb_names;
2693 const struct config_key keyboard_config_keys[] = {
2694 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2695 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2696 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2697 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2698 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2699 };
2700 const struct config_section cs[] = {
2701 { "keyboard",
2702 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2703 };
2704
2705 memset(&xkb_names, 0, sizeof(xkb_names));
Ossama Othmana50e6e42013-05-14 09:48:26 -07002706
2707 ec->config_fd = config_fd;
2708 parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002709
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002710 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002711 wl_signal_init(&ec->destroy_signal);
2712 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002713 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002714 wl_signal_init(&ec->idle_signal);
2715 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002716 wl_signal_init(&ec->show_input_panel_signal);
2717 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02002718 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002719 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01002720 wl_signal_init(&ec->output_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002721 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002722
Casey Dahlin58ba1372012-04-19 22:50:08 -04002723 ec->output_id_pool = 0;
2724
Kristian Høgsberga8873122011-11-23 10:39:34 -05002725 if (!wl_display_add_global(display, &wl_compositor_interface,
2726 ec, compositor_bind))
2727 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002728
Pekka Paalanene67858b2013-04-25 13:57:42 +03002729 if (!wl_display_add_global(display, &wl_subcompositor_interface,
2730 ec, bind_subcompositor))
2731 return -1;
2732
Daniel Stone725c2c32012-06-22 14:04:36 +01002733 wl_list_init(&ec->surface_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002734 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002735 wl_list_init(&ec->layer_list);
2736 wl_list_init(&ec->seat_list);
2737 wl_list_init(&ec->output_list);
2738 wl_list_init(&ec->key_binding_list);
2739 wl_list_init(&ec->button_binding_list);
2740 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002741 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002742
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002743 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002744 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002745
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002746 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
2747 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01002748
2749 ec->ping_handler = NULL;
2750
2751 screenshooter_create(ec);
2752 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002753 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002754
2755 wl_data_device_manager_init(ec->wl_display);
2756
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002757 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002758
Daniel Stone725c2c32012-06-22 14:04:36 +01002759 loop = wl_display_get_event_loop(ec->wl_display);
2760 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2761 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2762
2763 ec->input_loop = wl_event_loop_create();
2764
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002765 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2766 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2767
2768 weston_compositor_schedule_repaint(ec);
2769
Daniel Stone725c2c32012-06-22 14:04:36 +01002770 return 0;
2771}
2772
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002773WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002774weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002775{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002776 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002777
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002778 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002779 if (ec->input_loop_source)
2780 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002781
Matt Roper361d2ad2011-08-29 13:52:23 -07002782 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002783 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002784 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002785
Daniel Stone325fc2d2012-05-30 16:31:58 +01002786 weston_binding_list_destroy_all(&ec->key_binding_list);
2787 weston_binding_list_destroy_all(&ec->button_binding_list);
2788 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002789 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002790
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002791 weston_plane_release(&ec->primary_plane);
2792
Jonas Ådahlc97af922012-03-28 22:36:09 +02002793 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07002794
2795 close(ec->config_fd);
Matt Roper361d2ad2011-08-29 13:52:23 -07002796}
2797
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05002798WL_EXPORT void
2799weston_version(int *major, int *minor, int *micro)
2800{
2801 *major = WESTON_VERSION_MAJOR;
2802 *minor = WESTON_VERSION_MINOR;
2803 *micro = WESTON_VERSION_MICRO;
2804}
2805
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002806static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002807{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002808 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002809
Martin Minarik6d118362012-06-07 18:01:59 +02002810 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002811 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002812
2813 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002814}
2815
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002816#ifdef HAVE_LIBUNWIND
2817
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002818static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002819print_backtrace(void)
2820{
2821 unw_cursor_t cursor;
2822 unw_context_t context;
2823 unw_word_t off;
2824 unw_proc_info_t pip;
2825 int ret, i = 0;
2826 char procname[256];
2827 const char *filename;
2828 Dl_info dlinfo;
2829
2830 pip.unwind_info = NULL;
2831 ret = unw_getcontext(&context);
2832 if (ret) {
2833 weston_log("unw_getcontext: %d\n", ret);
2834 return;
2835 }
2836
2837 ret = unw_init_local(&cursor, &context);
2838 if (ret) {
2839 weston_log("unw_init_local: %d\n", ret);
2840 return;
2841 }
2842
2843 ret = unw_step(&cursor);
2844 while (ret > 0) {
2845 ret = unw_get_proc_info(&cursor, &pip);
2846 if (ret) {
2847 weston_log("unw_get_proc_info: %d\n", ret);
2848 break;
2849 }
2850
2851 ret = unw_get_proc_name(&cursor, procname, 256, &off);
2852 if (ret && ret != -UNW_ENOMEM) {
2853 if (ret != -UNW_EUNSPEC)
2854 weston_log("unw_get_proc_name: %d\n", ret);
2855 procname[0] = '?';
2856 procname[1] = 0;
2857 }
2858
2859 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
2860 *dlinfo.dli_fname)
2861 filename = dlinfo.dli_fname;
2862 else
2863 filename = "?";
2864
2865 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
2866 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
2867
2868 ret = unw_step(&cursor);
2869 if (ret < 0)
2870 weston_log("unw_step: %d\n", ret);
2871 }
2872}
2873
2874#else
2875
2876static void
2877print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002878{
2879 void *buffer[32];
2880 int i, count;
2881 Dl_info info;
2882
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002883 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2884 for (i = 0; i < count; i++) {
2885 dladdr(buffer[i], &info);
2886 weston_log(" [%016lx] %s (%s)\n",
2887 (long) buffer[i],
2888 info.dli_sname ? info.dli_sname : "--",
2889 info.dli_fname);
2890 }
2891}
2892
2893#endif
2894
2895static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01002896on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002897{
Peter Maatmane5b42e42013-03-27 22:38:53 +01002898 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002899 * then call the backend restore function, which will switch
2900 * back to the vt we launched from or ungrab X etc and then
2901 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01002902 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002903 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01002904 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002905
Peter Maatmane5b42e42013-03-27 22:38:53 +01002906 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002907
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002908 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002909
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002910 segv_compositor->restore(segv_compositor);
2911
2912 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002913}
2914
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002915static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04002916load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002917{
2918 char path[PATH_MAX];
2919 void *module, *init;
2920
2921 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07002922 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002923 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02002924 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002925
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002926 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
2927 if (module) {
2928 weston_log("Module '%s' already loaded\n", path);
2929 dlclose(module);
2930 return NULL;
2931 }
2932
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002933 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04002934 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002935 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002936 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002937 return NULL;
2938 }
2939
2940 init = dlsym(module, entrypoint);
2941 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002942 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00002943 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002944 return NULL;
2945 }
2946
2947 return init;
2948}
2949
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002950static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05002951load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07002952 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002953{
2954 const char *p, *end;
2955 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05002956 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07002957 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002958
2959 if (modules == NULL)
2960 return 0;
2961
2962 p = modules;
2963 while (*p) {
2964 end = strchrnul(p, ',');
2965 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
2966 module_init = load_module(buffer, "module_init");
2967 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07002968 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002969 p = end;
2970 while (*p == ',')
2971 p++;
2972
2973 }
2974
2975 return 0;
2976}
2977
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002978static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02002979 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
2980
2981static const char xdg_wrong_message[] =
2982 "fatal: environment variable XDG_RUNTIME_DIR\n"
2983 "is set to \"%s\", which is not a directory.\n";
2984
2985static const char xdg_wrong_mode_message[] =
2986 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
2987 "correctly. Unix access mode must be 0700 but is %o,\n"
2988 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04002989 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02002990
2991static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002992 "Refer to your distribution on how to get it, or\n"
2993 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
2994 "on how to implement it.\n";
2995
Martin Minarik37032f82012-06-18 20:15:18 +02002996static void
2997verify_xdg_runtime_dir(void)
2998{
2999 char *dir = getenv("XDG_RUNTIME_DIR");
3000 struct stat s;
3001
3002 if (!dir) {
3003 weston_log(xdg_error_message);
3004 weston_log_continue(xdg_detail_message);
3005 exit(EXIT_FAILURE);
3006 }
3007
3008 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3009 weston_log(xdg_wrong_message, dir);
3010 weston_log_continue(xdg_detail_message);
3011 exit(EXIT_FAILURE);
3012 }
3013
3014 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3015 weston_log(xdg_wrong_mode_message,
3016 dir, s.st_mode & 0777, s.st_uid);
3017 weston_log_continue(xdg_detail_message);
3018 }
3019}
3020
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003021static int
3022usage(int error_code)
3023{
3024 fprintf(stderr,
3025 "Usage: weston [OPTIONS]\n\n"
3026 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3027 "Weston supports multiple backends, and depending on which backend is in use\n"
3028 "different options will be accepted.\n\n"
3029
3030
3031 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003032 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003033 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003034 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3035 "\t\t\t\twayland-backend.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003036 " -S, --socket=NAME\tName of socket to listen on\n"
3037 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003038 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003039 " --log==FILE\t\tLog to the given file\n"
3040 " -h, --help\t\tThis help message\n\n");
3041
3042 fprintf(stderr,
3043 "Options for drm-backend.so:\n\n"
3044 " --connector=ID\tBring up only this connector\n"
3045 " --seat=SEAT\t\tThe seat that weston should run on\n"
3046 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003047 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003048 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3049
3050 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003051 "Options for fbdev-backend.so:\n\n"
3052 " --tty=TTY\t\tThe tty to use\n"
3053 " --device=DEVICE\tThe framebuffer device to use\n\n");
3054
3055 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003056 "Options for x11-backend.so:\n\n"
3057 " --width=WIDTH\t\tWidth of X window\n"
3058 " --height=HEIGHT\tHeight of X window\n"
3059 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003060 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003061 " --output-count=COUNT\tCreate multiple outputs\n"
3062 " --no-input\t\tDont create input devices\n\n");
3063
3064 fprintf(stderr,
3065 "Options for wayland-backend.so:\n\n"
3066 " --width=WIDTH\t\tWidth of Wayland surface\n"
3067 " --height=HEIGHT\tHeight of Wayland surface\n"
3068 " --display=DISPLAY\tWayland display to connect to\n\n");
3069
3070 exit(error_code);
3071}
3072
Peter Maatmane5b42e42013-03-27 22:38:53 +01003073static void
3074catch_signals(void)
3075{
3076 struct sigaction action;
3077
3078 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3079 action.sa_sigaction = on_caught_signal;
3080 sigemptyset(&action.sa_mask);
3081 sigaction(SIGSEGV, &action, NULL);
3082 sigaction(SIGABRT, &action, NULL);
3083}
3084
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003085int main(int argc, char *argv[])
3086{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003087 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003088 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003089 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003090 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003091 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003092 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003093 *(*backend_init)(struct wl_display *display,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003094 int *argc, char *argv[], int config_fd);
3095 int i, config_fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003096 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003097 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003098 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003099 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003100 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003101 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003102 int32_t version = 0;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003103
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003104 const struct config_key core_config_keys[] = {
3105 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003106 };
3107
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003108 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003109 { "core",
3110 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003111 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003112
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003113 const struct weston_option core_options[] = {
3114 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3115 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3116 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003117 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003118 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003119 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003120 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003121 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003122
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003123 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003124
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003125 if (help)
3126 usage(EXIT_SUCCESS);
3127
Scott Moreau12245142012-08-29 15:15:58 -06003128 if (version) {
3129 printf(PACKAGE_STRING "\n");
3130 return EXIT_SUCCESS;
3131 }
3132
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003133 weston_log_file_open(log);
3134
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003135 weston_log("%s\n"
3136 STAMP_SPACE "%s\n"
3137 STAMP_SPACE "Bug reports to: %s\n"
3138 STAMP_SPACE "Build: %s\n",
3139 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003140 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003141 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003142
Martin Minarik37032f82012-06-18 20:15:18 +02003143 verify_xdg_runtime_dir();
3144
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003145 display = wl_display_create();
3146
Tiago Vignatti2116b892011-08-08 05:52:59 -07003147 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003148 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3149 display);
3150 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3151 display);
3152 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3153 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003154
3155 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003156 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3157 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003158
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003159 if (!backend) {
3160 if (getenv("WAYLAND_DISPLAY"))
3161 backend = "wayland-backend.so";
3162 else if (getenv("DISPLAY"))
3163 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003164 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003165 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003166 }
3167
Ossama Othmana50e6e42013-05-14 09:48:26 -07003168 config_fd = open_config_file("weston.ini");
3169 parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003170
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003171 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003172 if (!backend_init)
3173 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003174
Ossama Othmana50e6e42013-05-14 09:48:26 -07003175 ec = backend_init(display, &argc, argv, config_fd);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003176 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003177 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003178 exit(EXIT_FAILURE);
3179 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003180
Peter Maatmane5b42e42013-03-27 22:38:53 +01003181 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003182 segv_compositor = ec;
3183
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003184 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003185
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003186 setenv("WAYLAND_DISPLAY", socket_name, 1);
3187
Ossama Othmana50e6e42013-05-14 09:48:26 -07003188 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003189 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003190 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003191 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003192
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003193 for (i = 1; i < argc; i++)
3194 weston_log("fatal: unhandled option: %s\n", argv[i]);
3195 if (argc > 1) {
3196 ret = EXIT_FAILURE;
3197 goto out;
3198 }
3199
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003200 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003201 weston_log("fatal: failed to add socket: %m\n");
3202 ret = EXIT_FAILURE;
3203 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003204 }
3205
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003206 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003207
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003208 wl_display_run(display);
3209
3210 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003211 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003212 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003213
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003214 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003215
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003216 for (i = ARRAY_LENGTH(signals); i;)
3217 wl_event_source_remove(signals[--i]);
3218
Daniel Stone855028f2012-05-01 20:37:10 +01003219 weston_compositor_xkb_destroy(ec);
3220
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003221 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003222 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003223
Martin Minarik19e6f262012-06-07 13:08:46 +02003224 weston_log_file_close();
3225
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003226 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003227}