blob: c1f90caa3dda547944aec13d38b64c275e99c744 [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"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030059#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040060#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050061#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050062
Kristian Høgsberg27da5382011-06-21 17:32:25 -040063static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040064static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040065
66static int
67sigchld_handler(int signal_number, void *data)
68{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050069 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040070 int status;
71 pid_t pid;
72
Bill Spitzak027b9622012-03-17 15:22:03 -070073 pid = waitpid(-1, &status, WNOHANG);
74 if (!pid)
75 return 1;
76
Kristian Høgsberg27da5382011-06-21 17:32:25 -040077 wl_list_for_each(p, &child_process_list, link) {
78 if (p->pid == pid)
79 break;
80 }
81
82 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020083 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040084 return 1;
85 }
86
87 wl_list_remove(&p->link);
88 p->cleanup(p, status);
89
90 return 1;
91}
92
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020093static void
94weston_output_transform_init(struct weston_output *output, uint32_t transform);
95
Alex Wu2dda6042012-04-17 17:20:47 +080096WL_EXPORT int
97weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode)
98{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020099 struct weston_seat *seat;
100 pixman_region32_t old_output_region;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200101 int ret;
102
Alex Wu2dda6042012-04-17 17:20:47 +0800103 if (!output->switch_mode)
104 return -1;
105
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200106 ret = output->switch_mode(output, mode);
107 if (ret < 0)
108 return ret;
109
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200110 pixman_region32_init(&old_output_region);
111 pixman_region32_copy(&old_output_region, &output->region);
112
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200113 /* Update output region and transformation matrix */
114 weston_output_transform_init(output, output->transform);
115
116 pixman_region32_init(&output->previous_damage);
117 pixman_region32_init_rect(&output->region, output->x, output->y,
118 output->width, output->height);
119
120 weston_output_update_matrix(output);
121
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200122 /* If a pointer falls outside the outputs new geometry, move it to its
123 * lower-right corner */
124 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400125 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200126 int32_t x, y;
127
128 if (!pointer)
129 continue;
130
131 x = wl_fixed_to_int(pointer->x);
132 y = wl_fixed_to_int(pointer->y);
133
134 if (!pixman_region32_contains_point(&old_output_region,
135 x, y, NULL) ||
136 pixman_region32_contains_point(&output->region,
137 x, y, NULL))
138 continue;
139
140 if (x >= output->x + output->width)
141 x = output->x + output->width - 1;
142 if (y >= output->y + output->height)
143 y = output->y + output->height - 1;
144
145 pointer->x = wl_fixed_from_int(x);
146 pointer->y = wl_fixed_from_int(y);
147 }
148
149 pixman_region32_fini(&old_output_region);
150
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200151 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800152}
153
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400154WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500155weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400156{
157 wl_list_insert(&child_process_list, &process->link);
158}
159
Benjamin Franzke06286262011-05-06 19:12:33 +0200160static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200161child_client_exec(int sockfd, const char *path)
162{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500163 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200164 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200165 sigset_t allsigs;
166
167 /* do not give our signal mask to the new process */
168 sigfillset(&allsigs);
169 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200170
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500171 /* Launch clients as the user. */
172 seteuid(getuid());
173
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500174 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
175 * non-CLOEXEC fd to pass through exec. */
176 clientfd = dup(sockfd);
177 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200178 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500179 return;
180 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200181
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500182 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200183 setenv("WAYLAND_SOCKET", s, 1);
184
185 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200186 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200187 path);
188}
189
190WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500191weston_client_launch(struct weston_compositor *compositor,
192 struct weston_process *proc,
193 const char *path,
194 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200195{
196 int sv[2];
197 pid_t pid;
198 struct wl_client *client;
199
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300200 weston_log("launching '%s'\n", path);
201
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300202 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200203 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200204 "socketpair failed while launching '%s': %m\n",
205 path);
206 return NULL;
207 }
208
209 pid = fork();
210 if (pid == -1) {
211 close(sv[0]);
212 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200213 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200214 "fork failed while launching '%s': %m\n", path);
215 return NULL;
216 }
217
218 if (pid == 0) {
219 child_client_exec(sv[1], path);
220 exit(-1);
221 }
222
223 close(sv[1]);
224
225 client = wl_client_create(compositor->wl_display, sv[0]);
226 if (!client) {
227 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200228 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200229 "wl_client_create failed while launching '%s'.\n",
230 path);
231 return NULL;
232 }
233
234 proc->pid = pid;
235 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500236 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200237
238 return client;
239}
240
241static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300242surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
243{
244 struct weston_surface *surface =
245 container_of(listener, struct weston_surface,
246 pending.buffer_destroy_listener);
247
248 surface->pending.buffer = NULL;
249}
250
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200251static void
252empty_region(pixman_region32_t *region)
253{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300254 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200255 pixman_region32_init(region);
256}
257
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300258static void
259region_init_infinite(pixman_region32_t *region)
260{
261 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
262 UINT32_MAX, UINT32_MAX);
263}
264
Pekka Paalanene67858b2013-04-25 13:57:42 +0300265static struct weston_subsurface *
266weston_surface_to_subsurface(struct weston_surface *surface);
267
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500268WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500269weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500270{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500271 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400272
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200273 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400274 if (surface == NULL)
275 return NULL;
276
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400277 wl_signal_init(&surface->resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500278
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500279 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500280 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500281
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400282 surface->resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400283
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500284 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400285 surface->alpha = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400286
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100287 if (compositor->renderer->create_surface(surface) < 0) {
288 free(surface);
289 return NULL;
290 }
291
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200292 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
293 surface->pending.buffer_transform = surface->buffer_transform;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400294 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400295 surface->plane = &compositor->primary_plane;
Giulio Camuffo184df502013-02-21 11:29:21 +0100296 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200297
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400298 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500299 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500300 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300301 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200302 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500303 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400304
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200305 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200306 wl_list_insert(&surface->geometry.transformation_list,
307 &surface->transform.position.link);
308 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200309 wl_list_init(&surface->geometry.child_list);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200310 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200311 surface->transform.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500312
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300313 surface->pending.buffer_destroy_listener.notify =
314 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300315 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300316 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300317 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300318 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300319
Pekka Paalanene67858b2013-04-25 13:57:42 +0300320 wl_list_init(&surface->subsurface_list);
321 wl_list_init(&surface->subsurface_list_pending);
322
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400323 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500324}
325
Alex Wu8811bf92012-02-28 18:07:54 +0800326WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500327weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200328 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500329{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100330 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500331}
332
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400333WL_EXPORT void
334weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200335 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200336{
337 if (surface->transform.enabled) {
338 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
339
340 weston_matrix_transform(&surface->transform.matrix, &v);
341
342 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200343 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700344 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200345 v.f[3]);
346 *x = 0;
347 *y = 0;
348 return;
349 }
350
351 *x = v.f[0] / v.f[3];
352 *y = v.f[1] / v.f[3];
353 } else {
354 *x = sx + surface->geometry.x;
355 *y = sy + surface->geometry.y;
356 }
357}
358
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500359WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200360weston_transformed_coord(int width, int height,
361 enum wl_output_transform transform,
362 float sx, float sy, float *bx, float *by)
363{
364 switch (transform) {
365 case WL_OUTPUT_TRANSFORM_NORMAL:
366 default:
367 *bx = sx;
368 *by = sy;
369 break;
370 case WL_OUTPUT_TRANSFORM_FLIPPED:
371 *bx = width - sx;
372 *by = sy;
373 break;
374 case WL_OUTPUT_TRANSFORM_90:
375 *bx = height - sy;
376 *by = sx;
377 break;
378 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
379 *bx = height - sy;
380 *by = width - sx;
381 break;
382 case WL_OUTPUT_TRANSFORM_180:
383 *bx = width - sx;
384 *by = height - sy;
385 break;
386 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
387 *bx = sx;
388 *by = height - sy;
389 break;
390 case WL_OUTPUT_TRANSFORM_270:
391 *bx = sy;
392 *by = width - sx;
393 break;
394 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
395 *bx = sy;
396 *by = sx;
397 break;
398 }
399}
400
401WL_EXPORT pixman_box32_t
402weston_transformed_rect(int width, int height,
403 enum wl_output_transform transform,
404 pixman_box32_t rect)
405{
406 float x1, x2, y1, y2;
407
408 pixman_box32_t ret;
409
410 weston_transformed_coord(width, height, transform,
411 rect.x1, rect.y1, &x1, &y1);
412 weston_transformed_coord(width, height, transform,
413 rect.x2, rect.y2, &x2, &y2);
414
415 if (x1 <= x2) {
416 ret.x1 = x1;
417 ret.x2 = x2;
418 } else {
419 ret.x1 = x2;
420 ret.x2 = x1;
421 }
422
423 if (y1 <= y2) {
424 ret.y1 = y1;
425 ret.y2 = y2;
426 } else {
427 ret.y1 = y2;
428 ret.y2 = y1;
429 }
430
431 return ret;
432}
433
434WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200435weston_surface_to_buffer_float(struct weston_surface *surface,
436 float sx, float sy, float *bx, float *by)
437{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200438 weston_transformed_coord(surface->geometry.width,
439 surface->geometry.height,
440 surface->buffer_transform,
441 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200442}
443
444WL_EXPORT pixman_box32_t
445weston_surface_to_buffer_rect(struct weston_surface *surface,
446 pixman_box32_t rect)
447{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200448 return weston_transformed_rect(surface->geometry.width,
449 surface->geometry.height,
450 surface->buffer_transform, rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200451}
452
453WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400454weston_surface_move_to_plane(struct weston_surface *surface,
455 struct weston_plane *plane)
456{
457 if (surface->plane == plane)
458 return;
459
460 weston_surface_damage_below(surface);
461 surface->plane = plane;
462 weston_surface_damage(surface);
463}
464
465WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500466weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200467{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500468 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200469
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500470 pixman_region32_init(&damage);
471 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
472 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400473 pixman_region32_union(&surface->plane->damage,
474 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500475 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200476}
477
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400478static struct wl_resource *
479find_resource_for_client(struct wl_list *list, struct wl_client *client)
480{
481 struct wl_resource *r;
482
483 wl_list_for_each(r, list, link) {
484 if (r->client == client)
485 return r;
486 }
487
488 return NULL;
489}
490
491static void
492weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
493{
494 uint32_t different = es->output_mask ^ mask;
495 uint32_t entered = mask & different;
496 uint32_t left = es->output_mask & different;
497 struct weston_output *output;
498 struct wl_resource *resource = NULL;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400499 struct wl_client *client = es->resource.client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400500
501 es->output_mask = mask;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400502 if (es->resource.client == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400503 return;
504 if (different == 0)
505 return;
506
507 wl_list_for_each(output, &es->compositor->output_list, link) {
508 if (1 << output->id & different)
509 resource =
510 find_resource_for_client(&output->resource_list,
511 client);
512 if (resource == NULL)
513 continue;
514 if (1 << output->id & entered)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400515 wl_surface_send_enter(&es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400516 if (1 << output->id & left)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400517 wl_surface_send_leave(&es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400518 }
519}
520
521static void
522weston_surface_assign_output(struct weston_surface *es)
523{
524 struct weston_compositor *ec = es->compositor;
525 struct weston_output *output, *new_output;
526 pixman_region32_t region;
527 uint32_t max, area, mask;
528 pixman_box32_t *e;
529
530 new_output = NULL;
531 max = 0;
532 mask = 0;
533 pixman_region32_init(&region);
534 wl_list_for_each(output, &ec->output_list, link) {
535 pixman_region32_intersect(&region, &es->transform.boundingbox,
536 &output->region);
537
538 e = pixman_region32_extents(&region);
539 area = (e->x2 - e->x1) * (e->y2 - e->y1);
540
541 if (area > 0)
542 mask |= 1 << output->id;
543
544 if (area >= max) {
545 new_output = output;
546 max = area;
547 }
548 }
549 pixman_region32_fini(&region);
550
551 es->output = new_output;
552 weston_surface_update_output_mask(es, mask);
553}
554
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200555static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200556surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
557 int32_t width, int32_t height,
558 pixman_region32_t *bbox)
559{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200560 float min_x = HUGE_VALF, min_y = HUGE_VALF;
561 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200562 int32_t s[4][2] = {
563 { sx, sy },
564 { sx, sy + height },
565 { sx + width, sy },
566 { sx + width, sy + height }
567 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200568 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200569 int i;
570
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300571 if (width == 0 || height == 0) {
572 /* avoid rounding empty bbox to 1x1 */
573 pixman_region32_init(bbox);
574 return;
575 }
576
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200577 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200578 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400579 weston_surface_to_global_float(surface,
580 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200581 if (x < min_x)
582 min_x = x;
583 if (x > max_x)
584 max_x = x;
585 if (y < min_y)
586 min_y = y;
587 if (y > max_y)
588 max_y = y;
589 }
590
Pekka Paalanen219b9822012-02-08 15:38:37 +0200591 int_x = floorf(min_x);
592 int_y = floorf(min_y);
593 pixman_region32_init_rect(bbox, int_x, int_y,
594 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200595}
596
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200597static void
598weston_surface_update_transform_disable(struct weston_surface *surface)
599{
600 surface->transform.enabled = 0;
601
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200602 /* round off fractions when not transformed */
603 surface->geometry.x = roundf(surface->geometry.x);
604 surface->geometry.y = roundf(surface->geometry.y);
605
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500606 /* Otherwise identity matrix, but with x and y translation. */
607 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
608 surface->transform.position.matrix.d[12] = surface->geometry.x;
609 surface->transform.position.matrix.d[13] = surface->geometry.y;
610
611 surface->transform.matrix = surface->transform.position.matrix;
612
Kristian Høgsberg9bcaaeb2013-02-28 14:56:43 -0500613 surface->transform.inverse = surface->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500614 surface->transform.inverse.d[12] = -surface->geometry.x;
615 surface->transform.inverse.d[13] = -surface->geometry.y;
616
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200617 pixman_region32_init_rect(&surface->transform.boundingbox,
618 surface->geometry.x,
619 surface->geometry.y,
620 surface->geometry.width,
621 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500622
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400623 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500624 pixman_region32_copy(&surface->transform.opaque,
625 &surface->opaque);
626 pixman_region32_translate(&surface->transform.opaque,
627 surface->geometry.x,
628 surface->geometry.y);
629 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200630}
631
632static int
633weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200634{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200635 struct weston_surface *parent = surface->geometry.parent;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200636 struct weston_matrix *matrix = &surface->transform.matrix;
637 struct weston_matrix *inverse = &surface->transform.inverse;
638 struct weston_transform *tform;
639
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200640 surface->transform.enabled = 1;
641
642 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300643 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200644 surface->transform.position.matrix.d[12] = surface->geometry.x;
645 surface->transform.position.matrix.d[13] = surface->geometry.y;
646
647 weston_matrix_init(matrix);
648 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
649 weston_matrix_multiply(matrix, &tform->matrix);
650
Pekka Paalanen483243f2013-03-08 14:56:50 +0200651 if (parent)
652 weston_matrix_multiply(matrix, &parent->transform.matrix);
653
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200654 if (weston_matrix_invert(inverse, matrix) < 0) {
655 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200656 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200657 " transformation not invertible.\n", surface);
658 return -1;
659 }
660
661 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
662 surface->geometry.height,
663 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500664
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200665 return 0;
666}
667
668WL_EXPORT void
669weston_surface_update_transform(struct weston_surface *surface)
670{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200671 struct weston_surface *parent = surface->geometry.parent;
672
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200673 if (!surface->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200674 return;
675
Pekka Paalanen483243f2013-03-08 14:56:50 +0200676 if (parent)
677 weston_surface_update_transform(parent);
678
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200679 surface->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200680
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500681 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200682
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200683 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500684 pixman_region32_fini(&surface->transform.opaque);
685 pixman_region32_init(&surface->transform.opaque);
686
Pekka Paalanencd403622012-01-25 13:37:39 +0200687 /* transform.position is always in transformation_list */
688 if (surface->geometry.transformation_list.next ==
689 &surface->transform.position.link &&
690 surface->geometry.transformation_list.prev ==
Pekka Paalanen483243f2013-03-08 14:56:50 +0200691 &surface->transform.position.link &&
692 !parent) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200693 weston_surface_update_transform_disable(surface);
694 } else {
695 if (weston_surface_update_transform_enable(surface) < 0)
696 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200697 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200698
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400699 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200700
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300701 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200702}
703
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200704WL_EXPORT void
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200705weston_surface_geometry_dirty(struct weston_surface *surface)
706{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200707 struct weston_surface *child;
708
709 /*
710 * The invariant: if surface->geometry.dirty, then all surfaces
711 * in surface->geometry.child_list have geometry.dirty too.
712 * Corollary: if not parent->geometry.dirty, then all ancestors
713 * are not dirty.
714 */
715
716 if (surface->transform.dirty)
717 return;
718
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200719 surface->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200720
721 wl_list_for_each(child, &surface->geometry.child_list,
722 geometry.parent_link)
723 weston_surface_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200724}
725
726WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100727weston_surface_to_global_fixed(struct weston_surface *surface,
728 wl_fixed_t sx, wl_fixed_t sy,
729 wl_fixed_t *x, wl_fixed_t *y)
730{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200731 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100732
733 weston_surface_to_global_float(surface,
734 wl_fixed_to_double(sx),
735 wl_fixed_to_double(sy),
736 &xf, &yf);
737 *x = wl_fixed_from_double(xf);
738 *y = wl_fixed_from_double(yf);
739}
740
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400741WL_EXPORT void
742weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200743 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200744{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200745 if (surface->transform.enabled) {
746 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
747
748 weston_matrix_transform(&surface->transform.inverse, &v);
749
750 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200751 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200752 "weston_surface_from_global(), divisor = %g\n",
753 v.f[3]);
754 *sx = 0;
755 *sy = 0;
756 return;
757 }
758
Pekka Paalanencd403622012-01-25 13:37:39 +0200759 *sx = v.f[0] / v.f[3];
760 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200761 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200762 *sx = x - surface->geometry.x;
763 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200764 }
765}
766
767WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100768weston_surface_from_global_fixed(struct weston_surface *surface,
769 wl_fixed_t x, wl_fixed_t y,
770 wl_fixed_t *sx, wl_fixed_t *sy)
771{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200772 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100773
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400774 weston_surface_from_global_float(surface,
775 wl_fixed_to_double(x),
776 wl_fixed_to_double(y),
777 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100778 *sx = wl_fixed_from_double(sxf);
779 *sy = wl_fixed_from_double(syf);
780}
781
782WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200783weston_surface_from_global(struct weston_surface *surface,
784 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
785{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200786 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200787
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400788 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200789 *sx = floorf(sxf);
790 *sy = floorf(syf);
791}
792
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400793WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400794weston_surface_schedule_repaint(struct weston_surface *surface)
795{
796 struct weston_output *output;
797
798 wl_list_for_each(output, &surface->compositor->output_list, link)
799 if (surface->output_mask & (1 << output->id))
800 weston_output_schedule_repaint(output);
801}
802
803WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500804weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500805{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400806 pixman_region32_union_rect(&surface->damage, &surface->damage,
807 0, 0, surface->geometry.width,
808 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200809
Kristian Høgsberg98238702012-08-03 16:29:12 -0400810 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500811}
812
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400813WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500814weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200815 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400816{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200817 surface->geometry.x = x;
818 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200819 surface->geometry.width = width;
820 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200821 weston_surface_geometry_dirty(surface);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400822}
823
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200824WL_EXPORT void
825weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200826 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200827{
828 surface->geometry.x = x;
829 surface->geometry.y = y;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200830 weston_surface_geometry_dirty(surface);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200831}
832
Pekka Paalanen483243f2013-03-08 14:56:50 +0200833static void
834transform_parent_handle_parent_destroy(struct wl_listener *listener,
835 void *data)
836{
837 struct weston_surface *surface =
838 container_of(listener, struct weston_surface,
839 geometry.parent_destroy_listener);
840
841 weston_surface_set_transform_parent(surface, NULL);
842}
843
844WL_EXPORT void
845weston_surface_set_transform_parent(struct weston_surface *surface,
846 struct weston_surface *parent)
847{
848 if (surface->geometry.parent) {
849 wl_list_remove(&surface->geometry.parent_destroy_listener.link);
850 wl_list_remove(&surface->geometry.parent_link);
851 }
852
853 surface->geometry.parent = parent;
854
855 surface->geometry.parent_destroy_listener.notify =
856 transform_parent_handle_parent_destroy;
857 if (parent) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400858 wl_signal_add(&parent->resource.destroy_signal,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200859 &surface->geometry.parent_destroy_listener);
860 wl_list_insert(&parent->geometry.child_list,
861 &surface->geometry.parent_link);
862 }
863
864 weston_surface_geometry_dirty(surface);
865}
866
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300867WL_EXPORT int
868weston_surface_is_mapped(struct weston_surface *surface)
869{
870 if (surface->output)
871 return 1;
872 else
873 return 0;
874}
875
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200876WL_EXPORT int32_t
877weston_surface_buffer_width(struct weston_surface *surface)
878{
879 switch (surface->buffer_transform) {
880 case WL_OUTPUT_TRANSFORM_90:
881 case WL_OUTPUT_TRANSFORM_270:
882 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
883 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200884 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200885 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200886 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200887 }
888}
889
890WL_EXPORT int32_t
891weston_surface_buffer_height(struct weston_surface *surface)
892{
893 switch (surface->buffer_transform) {
894 case WL_OUTPUT_TRANSFORM_90:
895 case WL_OUTPUT_TRANSFORM_270:
896 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
897 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200898 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200899 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200900 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200901 }
902}
903
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400904WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500905weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500906{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400907 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500908
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400909 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500910
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400911 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500912}
913
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400914WL_EXPORT struct weston_surface *
Tiago Vignatti9d393522012-02-10 16:26:19 +0200915weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100916 wl_fixed_t x, wl_fixed_t y,
917 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200918{
919 struct weston_surface *surface;
920
921 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100922 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500923 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100924 wl_fixed_to_int(*sx),
925 wl_fixed_to_int(*sy),
926 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200927 return surface;
928 }
929
930 return NULL;
931}
932
933static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500934weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400935{
Daniel Stone37816df2012-05-16 18:45:18 +0100936 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400937
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500938 if (!compositor->focus)
939 return;
940
Daniel Stone37816df2012-05-16 18:45:18 +0100941 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400942 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400943}
944
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400945WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500946weston_surface_unmap(struct weston_surface *surface)
947{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100948 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500949
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500950 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500951 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500952 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500953
Daniel Stone4dab5db2012-05-30 16:31:53 +0100954 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400955 if (seat->keyboard && seat->keyboard->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400956 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400957 if (seat->pointer && seat->pointer->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400958 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400959 NULL,
960 wl_fixed_from_int(0),
961 wl_fixed_from_int(0));
Daniel Stone4dab5db2012-05-30 16:31:53 +0100962 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500963
Kristian Høgsberg98238702012-08-03 16:29:12 -0400964 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500965}
966
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400967struct weston_frame_callback {
968 struct wl_resource resource;
969 struct wl_list link;
970};
971
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500972static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400973destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500974{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500975 struct weston_surface *surface =
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400976 container_of(resource, struct weston_surface, resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500977 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400978 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400979
Pekka Paalanen483243f2013-03-08 14:56:50 +0200980 assert(wl_list_empty(&surface->geometry.child_list));
Pekka Paalanene67858b2013-04-25 13:57:42 +0300981 assert(wl_list_empty(&surface->subsurface_list_pending));
982 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +0200983
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300984 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500985 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400986
Pekka Paalanenbc106382012-10-10 12:49:31 +0300987 wl_list_for_each_safe(cb, next,
988 &surface->pending.frame_callback_list, link)
989 wl_resource_destroy(&cb->resource);
990
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300991 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300992 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300993 pixman_region32_fini(&surface->pending.damage);
994
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300995 if (surface->pending.buffer)
996 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
997
Pekka Paalanende685b82012-12-04 15:58:12 +0200998 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400999
Kristian Høgsberg42263852012-09-06 21:59:29 -04001000 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001001
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001002 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001003 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001004 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001005 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001006 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001007
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001008 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
1009 wl_resource_destroy(&cb->resource);
1010
Pekka Paalanen483243f2013-03-08 14:56:50 +02001011 weston_surface_set_transform_parent(surface, NULL);
1012
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001013 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001014}
1015
Alex Wu8811bf92012-02-28 18:07:54 +08001016WL_EXPORT void
1017weston_surface_destroy(struct weston_surface *surface)
1018{
1019 /* Not a valid way to destroy a client surface */
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001020 assert(surface->resource.client == NULL);
Alex Wu8811bf92012-02-28 18:07:54 +08001021
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001022 wl_signal_emit(&surface->resource.destroy_signal, &surface->resource);
1023 destroy_surface(&surface->resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001024}
1025
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001026static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001027weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1028 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001029{
Pekka Paalanende685b82012-12-04 15:58:12 +02001030 struct weston_buffer_reference *ref =
1031 container_of(listener, struct weston_buffer_reference,
1032 destroy_listener);
1033
1034 assert((struct wl_buffer *)data == ref->buffer);
1035 ref->buffer = NULL;
1036}
1037
1038WL_EXPORT void
1039weston_buffer_reference(struct weston_buffer_reference *ref,
1040 struct wl_buffer *buffer)
1041{
1042 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001043 ref->buffer->busy_count--;
1044 if (ref->buffer->busy_count == 0) {
1045 assert(ref->buffer->resource.client != NULL);
1046 wl_resource_queue_event(&ref->buffer->resource,
1047 WL_BUFFER_RELEASE);
1048 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001049 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001050 }
1051
Pekka Paalanende685b82012-12-04 15:58:12 +02001052 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001053 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001054 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001055 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001056 }
1057
Pekka Paalanende685b82012-12-04 15:58:12 +02001058 ref->buffer = buffer;
1059 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1060}
1061
1062static void
1063weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
1064{
1065 weston_buffer_reference(&surface->buffer_ref, buffer);
1066
Pekka Paalanena6421c42012-12-04 15:58:10 +02001067 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001068 if (weston_surface_is_mapped(surface))
1069 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001070 }
1071
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001072 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001073}
1074
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001075WL_EXPORT void
1076weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001077{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001078 wl_list_remove(&surface->layer_link);
1079 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001080 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001081 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001082}
1083
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001084WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001085weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001086{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001087 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001088
1089 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001090 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001091}
1092
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001093WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001094weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001095{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001096 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001097
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001098 pixman_region32_union(&compositor->primary_plane.damage,
1099 &compositor->primary_plane.damage,
1100 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001101 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001102}
1103
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001104static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001105surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001106 pixman_region32_t *opaque)
1107{
Pekka Paalanende685b82012-12-04 15:58:12 +02001108 if (surface->buffer_ref.buffer &&
1109 wl_buffer_is_shm(surface->buffer_ref.buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001110 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001111
1112 if (surface->transform.enabled) {
1113 pixman_box32_t *extents;
1114
1115 extents = pixman_region32_extents(&surface->damage);
1116 surface_compute_bbox(surface, extents->x1, extents->y1,
1117 extents->x2 - extents->x1,
1118 extents->y2 - extents->y1,
1119 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001120 pixman_region32_translate(&surface->damage,
1121 -surface->plane->x,
1122 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001123 } else {
1124 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001125 surface->geometry.x - surface->plane->x,
1126 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001127 }
1128
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001129 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001130 pixman_region32_union(&surface->plane->damage,
1131 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001132 empty_region(&surface->damage);
1133 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001134 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001135}
1136
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001137static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001138compositor_accumulate_damage(struct weston_compositor *ec)
1139{
1140 struct weston_plane *plane;
1141 struct weston_surface *es;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001142 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001143
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001144 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001145
1146 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001147 pixman_region32_copy(&plane->clip, &clip);
1148
1149 pixman_region32_init(&opaque);
1150
1151 wl_list_for_each(es, &ec->surface_list, link) {
1152 if (es->plane != plane)
1153 continue;
1154
1155 surface_accumulate_damage(es, &opaque);
1156 }
1157
1158 pixman_region32_union(&clip, &clip, &opaque);
1159 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001160 }
1161
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001162 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001163
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001164 wl_list_for_each(es, &ec->surface_list, link) {
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001165 /* Both the renderer and the backend have seen the buffer
1166 * by now. If renderer needs the buffer, it has its own
1167 * reference set. If the backend wants to keep the buffer
1168 * around for migrating the surface into a non-primary plane
1169 * later, keep_buffer is true. Otherwise, drop the core
1170 * reference now, and allow early buffer release. This enables
1171 * clients to use single-buffering.
1172 */
1173 if (!es->keep_buffer)
1174 weston_buffer_reference(&es->buffer_ref, NULL);
1175 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001176}
1177
1178static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001179surface_list_add(struct weston_compositor *compositor,
1180 struct weston_surface *surface)
1181{
1182 struct weston_subsurface *sub;
1183
1184 if (wl_list_empty(&surface->subsurface_list)) {
1185 weston_surface_update_transform(surface);
1186 wl_list_insert(compositor->surface_list.prev, &surface->link);
1187 return;
1188 }
1189
1190 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1191 if (!weston_surface_is_mapped(sub->surface))
1192 continue;
1193
1194 if (sub->surface == surface) {
1195 weston_surface_update_transform(sub->surface);
1196 wl_list_insert(compositor->surface_list.prev,
1197 &sub->surface->link);
1198 } else {
1199 surface_list_add(compositor, sub->surface);
1200 }
1201 }
1202}
1203
1204static void
1205weston_compositor_build_surface_list(struct weston_compositor *compositor)
1206{
1207 struct weston_surface *surface;
1208 struct weston_layer *layer;
1209
1210 wl_list_init(&compositor->surface_list);
1211 wl_list_for_each(layer, &compositor->layer_list, link) {
1212 wl_list_for_each(surface, &layer->surface_list, layer_link) {
1213 surface_list_add(compositor, surface);
1214 }
1215 }
1216}
1217
1218static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001219weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001220{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001221 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001222 struct weston_surface *es;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001223 struct weston_animation *animation, *next;
1224 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001225 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001226 pixman_region32_t output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001227
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001228 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001229 weston_compositor_build_surface_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001230
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001231 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001232 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001233 else
1234 wl_list_for_each(es, &ec->surface_list, link)
1235 weston_surface_move_to_plane(es, &ec->primary_plane);
1236
Pekka Paalanene67858b2013-04-25 13:57:42 +03001237 wl_list_init(&frame_callback_list);
1238 wl_list_for_each(es, &ec->surface_list, link) {
1239 if (es->output == output) {
1240 wl_list_insert_list(&frame_callback_list,
1241 &es->frame_callback_list);
1242 wl_list_init(&es->frame_callback_list);
1243 }
1244 }
1245
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001246 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001247
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001248 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001249 pixman_region32_intersect(&output_damage,
1250 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001251 pixman_region32_subtract(&output_damage,
1252 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001253
Scott Moreauccbf29d2012-02-22 14:21:41 -07001254 if (output->dirty)
1255 weston_output_update_matrix(output);
1256
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001257 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001258
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001259 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001260
Kristian Høgsbergef044142011-06-21 15:02:12 -04001261 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001262
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001263 weston_compositor_repick(ec);
1264 wl_event_loop_dispatch(ec->input_loop, 0);
1265
Jonas Ådahldb773762012-06-13 00:01:21 +02001266 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001267 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001268 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001269 }
1270
Scott Moreaud64cf212012-06-08 19:40:54 -06001271 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001272 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001273 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001274 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001275}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001276
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001277static int
1278weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001279{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001280 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001281
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001282 wl_event_loop_dispatch(compositor->input_loop, 0);
1283
1284 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001285}
1286
1287WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001288weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001289{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001290 struct weston_compositor *compositor = output->compositor;
1291 struct wl_event_loop *loop =
1292 wl_display_get_event_loop(compositor->wl_display);
1293 int fd;
1294
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001295 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001296 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001297 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001298 return;
1299 }
1300
1301 output->repaint_scheduled = 0;
1302 if (compositor->input_loop_source)
1303 return;
1304
1305 fd = wl_event_loop_get_fd(compositor->input_loop);
1306 compositor->input_loop_source =
1307 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1308 weston_compositor_read_input, compositor);
1309}
1310
1311static void
1312idle_repaint(void *data)
1313{
1314 struct weston_output *output = data;
1315
Jonas Ådahle5a12252013-04-05 23:07:11 +02001316 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001317}
1318
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001319WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001320weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1321{
1322 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001323 if (below != NULL)
1324 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001325}
1326
1327WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001328weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001329{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001330 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001331 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001332
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001333 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1334 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001335 return;
1336
Kristian Høgsbergef044142011-06-21 15:02:12 -04001337 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001338 output->repaint_needed = 1;
1339 if (output->repaint_scheduled)
1340 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001341
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001342 wl_event_loop_add_idle(loop, idle_repaint, output);
1343 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001344
1345 if (compositor->input_loop_source) {
1346 wl_event_source_remove(compositor->input_loop_source);
1347 compositor->input_loop_source = NULL;
1348 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001349}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001350
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001351WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001352weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1353{
1354 struct weston_output *output;
1355
1356 wl_list_for_each(output, &compositor->output_list, link)
1357 weston_output_schedule_repaint(output);
1358}
1359
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001360static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001361surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001362{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001363 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001364}
1365
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001366static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001367surface_attach(struct wl_client *client,
1368 struct wl_resource *resource,
1369 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1370{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001371 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001372 struct wl_buffer *buffer = NULL;
1373
1374 if (buffer_resource)
1375 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001376
Pekka Paalanende685b82012-12-04 15:58:12 +02001377 /* Attach, attach, without commit in between does not send
1378 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001379 if (surface->pending.buffer)
1380 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001381
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001382 surface->pending.sx = sx;
1383 surface->pending.sy = sy;
1384 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001385 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001386 if (buffer) {
1387 wl_signal_add(&buffer->resource.destroy_signal,
1388 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001389 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001390}
1391
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001392static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001393surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001394 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001395 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001396{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001397 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001398
Pekka Paalanen8e159182012-10-10 12:49:25 +03001399 pixman_region32_union_rect(&surface->pending.damage,
1400 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001401 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001402}
1403
Kristian Høgsberg33418202011-08-16 23:01:28 -04001404static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001405destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001406{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001407 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001408
1409 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001410 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001411}
1412
1413static void
1414surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001415 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001416{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001417 struct weston_frame_callback *cb;
Pekka Paalanenbc106382012-10-10 12:49:31 +03001418 struct weston_surface *surface = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001419
1420 cb = malloc(sizeof *cb);
1421 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001422 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001423 return;
1424 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001425
Kristian Høgsberg33418202011-08-16 23:01:28 -04001426 cb->resource.object.interface = &wl_callback_interface;
1427 cb->resource.object.id = callback;
1428 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001429 cb->resource.client = client;
1430 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001431
1432 wl_client_add_resource(client, &cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001433 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001434}
1435
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001436static void
1437surface_set_opaque_region(struct wl_client *client,
1438 struct wl_resource *resource,
1439 struct wl_resource *region_resource)
1440{
1441 struct weston_surface *surface = resource->data;
1442 struct weston_region *region;
1443
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001444 if (region_resource) {
1445 region = region_resource->data;
Pekka Paalanen512dde82012-10-10 12:49:27 +03001446 pixman_region32_copy(&surface->pending.opaque,
1447 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001448 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001449 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001450 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001451}
1452
1453static void
1454surface_set_input_region(struct wl_client *client,
1455 struct wl_resource *resource,
1456 struct wl_resource *region_resource)
1457{
1458 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001459 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001460
1461 if (region_resource) {
1462 region = region_resource->data;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001463 pixman_region32_copy(&surface->pending.input,
1464 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001465 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001466 pixman_region32_fini(&surface->pending.input);
1467 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001468 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001469}
1470
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001471static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001472weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001473{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001474 struct weston_subsurface *sub;
1475
1476 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1477 parent_link_pending) {
1478 wl_list_remove(&sub->parent_link);
1479 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1480 }
1481}
1482
1483static void
1484weston_surface_commit(struct weston_surface *surface)
1485{
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001486 pixman_region32_t opaque;
Giulio Camuffo184df502013-02-21 11:29:21 +01001487 int buffer_width = 0;
1488 int buffer_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001489
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001490 /* wl_surface.set_buffer_rotation */
1491 surface->buffer_transform = surface->pending.buffer_transform;
1492
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001493 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001494 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001495 weston_surface_attach(surface, surface->pending.buffer);
1496
Giulio Camuffo184df502013-02-21 11:29:21 +01001497 if (surface->buffer_ref.buffer) {
1498 buffer_width = weston_surface_buffer_width(surface);
1499 buffer_height = weston_surface_buffer_height(surface);
1500 }
1501
1502 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001503 surface->configure(surface,
1504 surface->pending.sx, surface->pending.sy,
1505 buffer_width, buffer_height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001506
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001507 if (surface->pending.buffer)
1508 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1509 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001510 surface->pending.sx = 0;
1511 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001512 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001513
1514 /* wl_surface.damage */
1515 pixman_region32_union(&surface->damage, &surface->damage,
1516 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001517 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1518 0, 0,
1519 surface->geometry.width,
1520 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001521 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001522
1523 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001524 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001525 surface->geometry.width,
1526 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001527 pixman_region32_intersect(&opaque,
1528 &opaque, &surface->pending.opaque);
1529
1530 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1531 pixman_region32_copy(&surface->opaque, &opaque);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001532 weston_surface_geometry_dirty(surface);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001533 }
1534
1535 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001536
1537 /* wl_surface.set_input_region */
1538 pixman_region32_fini(&surface->input);
1539 pixman_region32_init_rect(&surface->input, 0, 0,
1540 surface->geometry.width,
1541 surface->geometry.height);
1542 pixman_region32_intersect(&surface->input,
1543 &surface->input, &surface->pending.input);
1544
Pekka Paalanenbc106382012-10-10 12:49:31 +03001545 /* wl_surface.frame */
1546 wl_list_insert_list(&surface->frame_callback_list,
1547 &surface->pending.frame_callback_list);
1548 wl_list_init(&surface->pending.frame_callback_list);
1549
Pekka Paalanene67858b2013-04-25 13:57:42 +03001550 weston_surface_commit_subsurface_order(surface);
1551
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001552 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001553}
1554
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001555static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001556weston_subsurface_commit(struct weston_subsurface *sub);
1557
1558static void
1559weston_subsurface_parent_commit(struct weston_subsurface *sub,
1560 int parent_is_synchronized);
1561
1562static void
1563surface_commit(struct wl_client *client, struct wl_resource *resource)
1564{
1565 struct weston_surface *surface = resource->data;
1566 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1567
1568 if (sub) {
1569 weston_subsurface_commit(sub);
1570 return;
1571 }
1572
1573 weston_surface_commit(surface);
1574
1575 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1576 if (sub->surface != surface)
1577 weston_subsurface_parent_commit(sub, 0);
1578 }
1579}
1580
1581static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001582surface_set_buffer_transform(struct wl_client *client,
1583 struct wl_resource *resource, int transform)
1584{
1585 struct weston_surface *surface = resource->data;
1586
1587 surface->pending.buffer_transform = transform;
1588}
1589
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001590static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001591 surface_destroy,
1592 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001593 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001594 surface_frame,
1595 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001596 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001597 surface_commit,
1598 surface_set_buffer_transform
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001599};
1600
1601static void
1602compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001603 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001604{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001605 struct weston_compositor *ec = resource->data;
1606 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001607
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001608 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001609 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001610 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001611 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001612 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001613
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001614 surface->resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001615
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001616 surface->resource.object.id = id;
1617 surface->resource.object.interface = &wl_surface_interface;
1618 surface->resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001619 (void (**)(void)) &surface_interface;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001620 surface->resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001621
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001622 wl_client_add_resource(client, &surface->resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001623}
1624
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001625static void
1626destroy_region(struct wl_resource *resource)
1627{
1628 struct weston_region *region =
1629 container_of(resource, struct weston_region, resource);
1630
1631 pixman_region32_fini(&region->region);
1632 free(region);
1633}
1634
1635static void
1636region_destroy(struct wl_client *client, struct wl_resource *resource)
1637{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001638 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001639}
1640
1641static void
1642region_add(struct wl_client *client, struct wl_resource *resource,
1643 int32_t x, int32_t y, int32_t width, int32_t height)
1644{
1645 struct weston_region *region = resource->data;
1646
1647 pixman_region32_union_rect(&region->region, &region->region,
1648 x, y, width, height);
1649}
1650
1651static void
1652region_subtract(struct wl_client *client, struct wl_resource *resource,
1653 int32_t x, int32_t y, int32_t width, int32_t height)
1654{
1655 struct weston_region *region = resource->data;
1656 pixman_region32_t rect;
1657
1658 pixman_region32_init_rect(&rect, x, y, width, height);
1659 pixman_region32_subtract(&region->region, &region->region, &rect);
1660 pixman_region32_fini(&rect);
1661}
1662
1663static const struct wl_region_interface region_interface = {
1664 region_destroy,
1665 region_add,
1666 region_subtract
1667};
1668
1669static void
1670compositor_create_region(struct wl_client *client,
1671 struct wl_resource *resource, uint32_t id)
1672{
1673 struct weston_region *region;
1674
1675 region = malloc(sizeof *region);
1676 if (region == NULL) {
1677 wl_resource_post_no_memory(resource);
1678 return;
1679 }
1680
1681 region->resource.destroy = destroy_region;
1682
1683 region->resource.object.id = id;
1684 region->resource.object.interface = &wl_region_interface;
1685 region->resource.object.implementation =
1686 (void (**)(void)) &region_interface;
1687 region->resource.data = region;
1688
1689 pixman_region32_init(&region->region);
1690
1691 wl_client_add_resource(client, &region->resource);
1692}
1693
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001694static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001695 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001696 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001697};
1698
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001699static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001700weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
1701{
1702 struct weston_surface *surface = sub->surface;
1703 pixman_region32_t opaque;
1704 int buffer_width = 0;
1705 int buffer_height = 0;
1706
1707 /* wl_surface.set_buffer_rotation */
1708 surface->buffer_transform = sub->cached.buffer_transform;
1709
1710 /* wl_surface.attach */
1711 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
1712 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
1713 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
1714
1715 if (surface->buffer_ref.buffer) {
1716 buffer_width = weston_surface_buffer_width(surface);
1717 buffer_height = weston_surface_buffer_height(surface);
1718 }
1719
1720 if (surface->configure && sub->cached.newly_attached)
1721 surface->configure(surface, sub->cached.sx, sub->cached.sy,
1722 buffer_width, buffer_height);
1723 sub->cached.sx = 0;
1724 sub->cached.sy = 0;
1725 sub->cached.newly_attached = 0;
1726
1727 /* wl_surface.damage */
1728 pixman_region32_union(&surface->damage, &surface->damage,
1729 &sub->cached.damage);
1730 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1731 0, 0,
1732 surface->geometry.width,
1733 surface->geometry.height);
1734 empty_region(&sub->cached.damage);
1735
1736 /* wl_surface.set_opaque_region */
1737 pixman_region32_init_rect(&opaque, 0, 0,
1738 surface->geometry.width,
1739 surface->geometry.height);
1740 pixman_region32_intersect(&opaque,
1741 &opaque, &sub->cached.opaque);
1742
1743 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1744 pixman_region32_copy(&surface->opaque, &opaque);
1745 weston_surface_geometry_dirty(surface);
1746 }
1747
1748 pixman_region32_fini(&opaque);
1749
1750 /* wl_surface.set_input_region */
1751 pixman_region32_fini(&surface->input);
1752 pixman_region32_init_rect(&surface->input, 0, 0,
1753 surface->geometry.width,
1754 surface->geometry.height);
1755 pixman_region32_intersect(&surface->input,
1756 &surface->input, &sub->cached.input);
1757
1758 /* wl_surface.frame */
1759 wl_list_insert_list(&surface->frame_callback_list,
1760 &sub->cached.frame_callback_list);
1761 wl_list_init(&sub->cached.frame_callback_list);
1762
1763 weston_surface_commit_subsurface_order(surface);
1764
1765 weston_surface_schedule_repaint(surface);
1766
1767 sub->cached.has_data = 0;
1768}
1769
1770static void
1771weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
1772{
1773 struct weston_surface *surface = sub->surface;
1774
1775 /*
1776 * If this commit would cause the surface to move by the
1777 * attach(dx, dy) parameters, the old damage region must be
1778 * translated to correspond to the new surface coordinate system
1779 * origin.
1780 */
1781 pixman_region32_translate(&sub->cached.damage,
1782 -surface->pending.sx, -surface->pending.sy);
1783 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
1784 &surface->pending.damage);
1785 empty_region(&surface->pending.damage);
1786
1787 if (surface->pending.newly_attached) {
1788 sub->cached.newly_attached = 1;
1789 weston_buffer_reference(&sub->cached.buffer_ref,
1790 surface->pending.buffer);
1791 }
1792 sub->cached.sx += surface->pending.sx;
1793 sub->cached.sy += surface->pending.sy;
1794 surface->pending.sx = 0;
1795 surface->pending.sy = 0;
1796 surface->pending.newly_attached = 0;
1797
1798 sub->cached.buffer_transform = surface->pending.buffer_transform;
1799
1800 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
1801
1802 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
1803
1804 wl_list_insert_list(&sub->cached.frame_callback_list,
1805 &surface->pending.frame_callback_list);
1806 wl_list_init(&surface->pending.frame_callback_list);
1807
1808 sub->cached.has_data = 1;
1809}
1810
1811static int
1812weston_subsurface_is_synchronized(struct weston_subsurface *sub)
1813{
1814 while (sub) {
1815 if (sub->synchronized)
1816 return 1;
1817
1818 if (!sub->parent)
1819 return 0;
1820
1821 sub = weston_surface_to_subsurface(sub->parent);
1822 }
1823
1824 return 0;
1825}
1826
1827static void
1828weston_subsurface_commit(struct weston_subsurface *sub)
1829{
1830 struct weston_surface *surface = sub->surface;
1831 struct weston_subsurface *tmp;
1832
1833 /* Recursive check for effectively synchronized. */
1834 if (weston_subsurface_is_synchronized(sub)) {
1835 weston_subsurface_commit_to_cache(sub);
1836 } else {
1837 if (sub->cached.has_data) {
1838 /* flush accumulated state from cache */
1839 weston_subsurface_commit_to_cache(sub);
1840 weston_subsurface_commit_from_cache(sub);
1841 } else {
1842 weston_surface_commit(surface);
1843 }
1844
1845 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1846 if (tmp->surface != surface)
1847 weston_subsurface_parent_commit(tmp, 0);
1848 }
1849 }
1850}
1851
1852static void
1853weston_subsurface_parent_commit(struct weston_subsurface *sub,
1854 int parent_is_synchronized)
1855{
1856 struct weston_surface *surface = sub->surface;
1857 struct weston_subsurface *tmp;
1858
1859 if (sub->position.set) {
1860 weston_surface_set_position(sub->surface,
1861 sub->position.x, sub->position.y);
1862 sub->position.set = 0;
1863 }
1864
1865 if (!parent_is_synchronized && !sub->synchronized)
1866 return;
1867
1868 /* From now on, commit_from_cache the whole sub-tree, regardless of
1869 * the synchronized mode of each child. This sub-surface or some
1870 * of its ancestors were synchronized, so we are synchronized
1871 * all the way down.
1872 */
1873
1874 if (sub->cached.has_data)
1875 weston_subsurface_commit_from_cache(sub);
1876
1877 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1878 if (tmp->surface != surface)
1879 weston_subsurface_parent_commit(tmp, 1);
1880 }
1881}
1882
1883static void
1884subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
1885 int32_t width, int32_t height)
1886{
1887 struct weston_compositor *compositor = surface->compositor;
1888
1889 weston_surface_configure(surface,
1890 surface->geometry.x + dx,
1891 surface->geometry.y + dy,
1892 width, height);
1893
1894 /* No need to check parent mappedness, because if parent is not
1895 * mapped, parent is not in a visible layer, so this sub-surface
1896 * will not be drawn either.
1897 */
1898 if (!weston_surface_is_mapped(surface)) {
1899 wl_list_init(&surface->layer_link);
1900
1901 /* Cannot call weston_surface_update_transform(),
1902 * because that would call it also for the parent surface,
1903 * which might not be mapped yet. That would lead to
1904 * inconsistent state, where the window could never be
1905 * mapped.
1906 *
1907 * Instead just assing any output, to make
1908 * weston_surface_is_mapped() return true, so that when the
1909 * parent surface does get mapped, this one will get
1910 * included, too. See surface_list_add().
1911 */
1912 assert(!wl_list_empty(&compositor->output_list));
1913 surface->output = container_of(compositor->output_list.next,
1914 struct weston_output, link);
1915 }
1916}
1917
1918static struct weston_subsurface *
1919weston_surface_to_subsurface(struct weston_surface *surface)
1920{
1921 if (surface->configure == subsurface_configure)
1922 return surface->configure_private;
1923
1924 return NULL;
1925}
1926
Pekka Paalanen01388e22013-04-25 13:57:44 +03001927WL_EXPORT struct weston_surface *
1928weston_surface_get_main_surface(struct weston_surface *surface)
1929{
1930 struct weston_subsurface *sub;
1931
1932 while (surface && (sub = weston_surface_to_subsurface(surface)))
1933 surface = sub->parent;
1934
1935 return surface;
1936}
1937
Pekka Paalanene67858b2013-04-25 13:57:42 +03001938static void
1939subsurface_set_position(struct wl_client *client,
1940 struct wl_resource *resource, int32_t x, int32_t y)
1941{
1942 struct weston_subsurface *sub = resource->data;
1943
1944 if (!sub)
1945 return;
1946
1947 sub->position.x = x;
1948 sub->position.y = y;
1949 sub->position.set = 1;
1950}
1951
1952static struct weston_subsurface *
1953subsurface_from_surface(struct weston_surface *surface)
1954{
1955 struct weston_subsurface *sub;
1956
1957 sub = weston_surface_to_subsurface(surface);
1958 if (sub)
1959 return sub;
1960
1961 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
1962 if (sub->surface == surface)
1963 return sub;
1964
1965 return NULL;
1966}
1967
1968static struct weston_subsurface *
1969subsurface_sibling_check(struct weston_subsurface *sub,
1970 struct weston_surface *surface,
1971 const char *request)
1972{
1973 struct weston_subsurface *sibling;
1974
1975 sibling = subsurface_from_surface(surface);
1976
1977 if (!sibling) {
1978 wl_resource_post_error(sub->resource,
1979 WL_SUBSURFACE_ERROR_BAD_SURFACE,
1980 "%s: wl_surface@%d is not a parent or sibling",
1981 request, surface->resource.object.id);
1982 return NULL;
1983 }
1984
1985 if (sibling->parent != sub->parent) {
1986 wl_resource_post_error(sub->resource,
1987 WL_SUBSURFACE_ERROR_BAD_SURFACE,
1988 "%s: wl_surface@%d has a different parent",
1989 request, surface->resource.object.id);
1990 return NULL;
1991 }
1992
1993 return sibling;
1994}
1995
1996static void
1997subsurface_place_above(struct wl_client *client,
1998 struct wl_resource *resource,
1999 struct wl_resource *sibling_resource)
2000{
2001 struct weston_subsurface *sub = resource->data;
2002 struct weston_surface *surface = sibling_resource->data;
2003 struct weston_subsurface *sibling;
2004
2005 if (!sub)
2006 return;
2007
2008 sibling = subsurface_sibling_check(sub, surface, "place_above");
2009 if (!sibling)
2010 return;
2011
2012 wl_list_remove(&sub->parent_link_pending);
2013 wl_list_insert(sibling->parent_link_pending.prev,
2014 &sub->parent_link_pending);
2015}
2016
2017static void
2018subsurface_place_below(struct wl_client *client,
2019 struct wl_resource *resource,
2020 struct wl_resource *sibling_resource)
2021{
2022 struct weston_subsurface *sub = resource->data;
2023 struct weston_surface *surface = sibling_resource->data;
2024 struct weston_subsurface *sibling;
2025
2026 if (!sub)
2027 return;
2028
2029 sibling = subsurface_sibling_check(sub, surface, "place_below");
2030 if (!sibling)
2031 return;
2032
2033 wl_list_remove(&sub->parent_link_pending);
2034 wl_list_insert(&sibling->parent_link_pending,
2035 &sub->parent_link_pending);
2036}
2037
2038static void
2039subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2040{
2041 struct weston_subsurface *sub = resource->data;
2042
2043 if (sub)
2044 sub->synchronized = 1;
2045}
2046
2047static void
2048subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2049{
2050 struct weston_subsurface *sub = resource->data;
2051
2052 if (sub)
2053 sub->synchronized = 0;
2054}
2055
2056static void
2057weston_subsurface_cache_init(struct weston_subsurface *sub)
2058{
2059 pixman_region32_init(&sub->cached.damage);
2060 pixman_region32_init(&sub->cached.opaque);
2061 pixman_region32_init(&sub->cached.input);
2062 wl_list_init(&sub->cached.frame_callback_list);
2063 sub->cached.buffer_ref.buffer = NULL;
2064}
2065
2066static void
2067weston_subsurface_cache_fini(struct weston_subsurface *sub)
2068{
2069 struct weston_frame_callback *cb, *tmp;
2070
2071 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
2072 wl_resource_destroy(&cb->resource);
2073
2074 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2075 pixman_region32_fini(&sub->cached.damage);
2076 pixman_region32_fini(&sub->cached.opaque);
2077 pixman_region32_fini(&sub->cached.input);
2078}
2079
2080static void
2081weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2082{
2083 wl_list_remove(&sub->parent_link);
2084 wl_list_remove(&sub->parent_link_pending);
2085 wl_list_remove(&sub->parent_destroy_listener.link);
2086 sub->parent = NULL;
2087}
2088
2089static void
2090weston_subsurface_destroy(struct weston_subsurface *sub);
2091
2092static void
2093subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2094{
2095 struct weston_subsurface *sub =
2096 container_of(listener, struct weston_subsurface,
2097 surface_destroy_listener);
2098 assert(data == &sub->surface->resource);
2099
2100 /* The protocol object (wl_resource) is left inert. */
2101 if (sub->resource)
2102 sub->resource->data = NULL;
2103
2104 weston_subsurface_destroy(sub);
2105}
2106
2107static void
2108subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2109{
2110 struct weston_subsurface *sub =
2111 container_of(listener, struct weston_subsurface,
2112 parent_destroy_listener);
2113 assert(data == &sub->parent->resource);
2114 assert(sub->surface != sub->parent);
2115
2116 if (weston_surface_is_mapped(sub->surface))
2117 weston_surface_unmap(sub->surface);
2118
2119 weston_subsurface_unlink_parent(sub);
2120}
2121
2122static void
2123subsurface_resource_destroy(struct wl_resource *resource)
2124{
2125 struct weston_subsurface *sub = resource->data;
2126
2127 if (sub)
2128 weston_subsurface_destroy(sub);
2129
2130 free(resource);
2131}
2132
2133static void
2134subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2135{
2136 wl_resource_destroy(resource);
2137}
2138
2139static void
2140weston_subsurface_link_parent(struct weston_subsurface *sub,
2141 struct weston_surface *parent)
2142{
2143 sub->parent = parent;
2144 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
2145 wl_signal_add(&parent->resource.destroy_signal,
2146 &sub->parent_destroy_listener);
2147
2148 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2149 wl_list_insert(&parent->subsurface_list_pending,
2150 &sub->parent_link_pending);
2151}
2152
2153static void
2154weston_subsurface_link_surface(struct weston_subsurface *sub,
2155 struct weston_surface *surface)
2156{
2157 sub->surface = surface;
2158 sub->surface_destroy_listener.notify =
2159 subsurface_handle_surface_destroy;
2160 wl_signal_add(&surface->resource.destroy_signal,
2161 &sub->surface_destroy_listener);
2162}
2163
2164static void
2165weston_subsurface_destroy(struct weston_subsurface *sub)
2166{
2167 assert(sub->surface);
2168
2169 if (sub->resource) {
2170 assert(weston_surface_to_subsurface(sub->surface) == sub);
2171 assert(sub->parent_destroy_listener.notify ==
2172 subsurface_handle_parent_destroy);
2173
2174 weston_surface_set_transform_parent(sub->surface, NULL);
2175 if (sub->parent)
2176 weston_subsurface_unlink_parent(sub);
2177
2178 weston_subsurface_cache_fini(sub);
2179
2180 sub->surface->configure = NULL;
2181 sub->surface->configure_private = NULL;
2182 } else {
2183 /* the dummy weston_subsurface for the parent itself */
2184 assert(sub->parent_destroy_listener.notify == NULL);
2185 wl_list_remove(&sub->parent_link);
2186 wl_list_remove(&sub->parent_link_pending);
2187 }
2188
2189 wl_list_remove(&sub->surface_destroy_listener.link);
2190 free(sub);
2191}
2192
2193static const struct wl_subsurface_interface subsurface_implementation = {
2194 subsurface_destroy,
2195 subsurface_set_position,
2196 subsurface_place_above,
2197 subsurface_place_below,
2198 subsurface_set_sync,
2199 subsurface_set_desync
2200};
2201
2202static struct weston_subsurface *
2203weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2204 struct weston_surface *parent)
2205{
2206 struct weston_subsurface *sub;
2207
2208 sub = calloc(1, sizeof *sub);
2209 if (!sub)
2210 return NULL;
2211
2212 sub->resource = wl_client_add_object(surface->resource.client,
2213 &wl_subsurface_interface,
2214 &subsurface_implementation,
2215 id, sub);
2216 if (!sub->resource) {
2217 free(sub);
2218 return NULL;
2219 }
2220
2221 sub->resource->destroy = subsurface_resource_destroy;
2222 weston_subsurface_link_surface(sub, surface);
2223 weston_subsurface_link_parent(sub, parent);
2224 weston_subsurface_cache_init(sub);
2225 sub->synchronized = 1;
2226 weston_surface_set_transform_parent(surface, parent);
2227
2228 return sub;
2229}
2230
2231/* Create a dummy subsurface for having the parent itself in its
2232 * sub-surface lists. Makes stacking order manipulation easy.
2233 */
2234static struct weston_subsurface *
2235weston_subsurface_create_for_parent(struct weston_surface *parent)
2236{
2237 struct weston_subsurface *sub;
2238
2239 sub = calloc(1, sizeof *sub);
2240 if (!sub)
2241 return NULL;
2242
2243 weston_subsurface_link_surface(sub, parent);
2244 sub->parent = parent;
2245 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2246 wl_list_insert(&parent->subsurface_list_pending,
2247 &sub->parent_link_pending);
2248
2249 return sub;
2250}
2251
2252static void
2253subcompositor_get_subsurface(struct wl_client *client,
2254 struct wl_resource *resource,
2255 uint32_t id,
2256 struct wl_resource *surface_resource,
2257 struct wl_resource *parent_resource)
2258{
2259 struct weston_surface *surface = surface_resource->data;
2260 struct weston_surface *parent = parent_resource->data;
2261 struct weston_subsurface *sub;
2262 static const char where[] = "get_subsurface: wl_subsurface@";
2263
2264 if (surface == parent) {
2265 wl_resource_post_error(resource,
2266 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2267 "%s%d: wl_surface@%d cannot be its own parent",
2268 where, id, surface_resource->object.id);
2269 return;
2270 }
2271
2272 if (weston_surface_to_subsurface(surface)) {
2273 wl_resource_post_error(resource,
2274 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2275 "%s%d: wl_surface@%d is already a sub-surface",
2276 where, id, surface_resource->object.id);
2277 return;
2278 }
2279
2280 if (surface->configure) {
2281 wl_resource_post_error(resource,
2282 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2283 "%s%d: wl_surface@%d already has a role",
2284 where, id, surface_resource->object.id);
2285 return;
2286 }
2287
2288 /* make sure the parent is in its own list */
2289 if (wl_list_empty(&parent->subsurface_list)) {
2290 if (!weston_subsurface_create_for_parent(parent)) {
2291 wl_resource_post_no_memory(resource);
2292 return;
2293 }
2294 }
2295
2296 sub = weston_subsurface_create(id, surface, parent);
2297 if (!sub) {
2298 wl_resource_post_no_memory(resource);
2299 return;
2300 }
2301
2302 surface->configure = subsurface_configure;
2303 surface->configure_private = sub;
2304}
2305
2306static void
2307subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2308{
2309 wl_resource_destroy(resource);
2310}
2311
2312static const struct wl_subcompositor_interface subcompositor_interface = {
2313 subcompositor_destroy,
2314 subcompositor_get_subsurface
2315};
2316
2317static void
2318bind_subcompositor(struct wl_client *client,
2319 void *data, uint32_t version, uint32_t id)
2320{
2321 struct weston_compositor *compositor = data;
2322
2323 wl_client_add_object(client, &wl_subcompositor_interface,
2324 &subcompositor_interface, id, compositor);
2325}
2326
2327static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002328weston_compositor_dpms(struct weston_compositor *compositor,
2329 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002330{
2331 struct weston_output *output;
2332
2333 wl_list_for_each(output, &compositor->output_list, link)
2334 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002335 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002336}
2337
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002338WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002339weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002340{
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002341 switch (compositor->state) {
2342 case WESTON_COMPOSITOR_SLEEPING:
2343 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2344 /* fall through */
2345 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002346 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002347 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002348 /* fall through */
2349 default:
2350 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2351 wl_event_source_timer_update(compositor->idle_source,
2352 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002353 }
2354}
2355
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002356WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002357weston_compositor_offscreen(struct weston_compositor *compositor)
2358{
2359 switch (compositor->state) {
2360 case WESTON_COMPOSITOR_OFFSCREEN:
2361 return;
2362 case WESTON_COMPOSITOR_SLEEPING:
2363 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2364 /* fall through */
2365 default:
2366 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2367 wl_event_source_timer_update(compositor->idle_source, 0);
2368 }
2369}
2370
2371WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002372weston_compositor_sleep(struct weston_compositor *compositor)
2373{
2374 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2375 return;
2376
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002377 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002378 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2379 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2380}
2381
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002382static int
2383idle_handler(void *data)
2384{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002385 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002386
2387 if (compositor->idle_inhibit)
2388 return 1;
2389
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002390 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002391 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002392
2393 return 1;
2394}
2395
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002396WL_EXPORT void
2397weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2398{
2399 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002400 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002401 plane->x = x;
2402 plane->y = y;
2403}
2404
2405WL_EXPORT void
2406weston_plane_release(struct weston_plane *plane)
2407{
2408 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002409 pixman_region32_fini(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002410}
2411
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002412WL_EXPORT void
2413weston_compositor_stack_plane(struct weston_compositor *ec,
2414 struct weston_plane *plane,
2415 struct weston_plane *above)
2416{
2417 if (above)
2418 wl_list_insert(above->link.prev, &plane->link);
2419 else
2420 wl_list_insert(&ec->plane_list, &plane->link);
2421}
2422
Casey Dahlin9074db52012-04-19 22:50:09 -04002423static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002424{
2425 wl_list_remove(&resource->link);
2426 free(resource);
2427}
2428
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002429static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002430bind_output(struct wl_client *client,
2431 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002432{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002433 struct weston_output *output = data;
2434 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002435 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002436
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002437 resource = wl_client_add_object(client,
2438 &wl_output_interface, NULL, id, data);
2439
Casey Dahlin9074db52012-04-19 22:50:09 -04002440 wl_list_insert(&output->resource_list, &resource->link);
2441 resource->destroy = unbind_resource;
2442
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002443 wl_output_send_geometry(resource,
2444 output->x,
2445 output->y,
2446 output->mm_width,
2447 output->mm_height,
2448 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002449 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002450 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002451
2452 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002453 wl_output_send_mode(resource,
2454 mode->flags,
2455 mode->width,
2456 mode->height,
2457 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002458 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002459}
2460
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002461WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002462weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002463{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002464 struct weston_compositor *c = output->compositor;
2465
Richard Hughes64ddde12013-05-01 21:52:10 +01002466 wl_signal_emit(&output->destroy_signal, output);
2467
Richard Hughesafe690c2013-05-02 10:10:04 +01002468 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002469 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002470 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002471 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002472
2473 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002474}
2475
Scott Moreau1bad5db2012-08-18 01:04:05 -06002476static void
2477weston_output_compute_transform(struct weston_output *output)
2478{
2479 struct weston_matrix transform;
2480 int flip;
2481
2482 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002483 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002484
2485 switch(output->transform) {
2486 case WL_OUTPUT_TRANSFORM_FLIPPED:
2487 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2488 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2489 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002490 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002491 flip = -1;
2492 break;
2493 default:
2494 flip = 1;
2495 break;
2496 }
2497
2498 switch(output->transform) {
2499 case WL_OUTPUT_TRANSFORM_NORMAL:
2500 case WL_OUTPUT_TRANSFORM_FLIPPED:
2501 transform.d[0] = flip;
2502 transform.d[1] = 0;
2503 transform.d[4] = 0;
2504 transform.d[5] = 1;
2505 break;
2506 case WL_OUTPUT_TRANSFORM_90:
2507 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2508 transform.d[0] = 0;
2509 transform.d[1] = -flip;
2510 transform.d[4] = 1;
2511 transform.d[5] = 0;
2512 break;
2513 case WL_OUTPUT_TRANSFORM_180:
2514 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2515 transform.d[0] = -flip;
2516 transform.d[1] = 0;
2517 transform.d[4] = 0;
2518 transform.d[5] = -1;
2519 break;
2520 case WL_OUTPUT_TRANSFORM_270:
2521 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2522 transform.d[0] = 0;
2523 transform.d[1] = flip;
2524 transform.d[4] = -1;
2525 transform.d[5] = 0;
2526 break;
2527 default:
2528 break;
2529 }
2530
2531 weston_matrix_multiply(&output->matrix, &transform);
2532}
2533
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002534WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002535weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002536{
Scott Moreau850ca422012-05-21 15:21:25 -06002537 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002538 struct weston_matrix camera;
2539 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002540
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002541 weston_matrix_init(&output->matrix);
2542 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002543 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2544 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002545
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002546 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002547 2.0 / (output->width + output->border.left + output->border.right),
2548 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2549
2550 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002551
Scott Moreauccbf29d2012-02-22 14:21:41 -07002552 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002553 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002554 weston_matrix_init(&camera);
2555 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002556 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002557 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002558 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002559 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002560 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002561 weston_matrix_multiply(&output->matrix, &modelview);
2562 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002563
Scott Moreauccbf29d2012-02-22 14:21:41 -07002564 output->dirty = 0;
2565}
2566
Scott Moreau1bad5db2012-08-18 01:04:05 -06002567static void
2568weston_output_transform_init(struct weston_output *output, uint32_t transform)
2569{
2570 output->transform = transform;
2571
2572 switch (transform) {
2573 case WL_OUTPUT_TRANSFORM_90:
2574 case WL_OUTPUT_TRANSFORM_270:
2575 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2576 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2577 /* Swap width and height */
2578 output->width = output->current->height;
2579 output->height = output->current->width;
2580 break;
2581 case WL_OUTPUT_TRANSFORM_NORMAL:
2582 case WL_OUTPUT_TRANSFORM_180:
2583 case WL_OUTPUT_TRANSFORM_FLIPPED:
2584 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2585 output->width = output->current->width;
2586 output->height = output->current->height;
2587 break;
2588 default:
2589 break;
2590 }
2591}
2592
Scott Moreauccbf29d2012-02-22 14:21:41 -07002593WL_EXPORT void
2594weston_output_move(struct weston_output *output, int x, int y)
2595{
2596 output->x = x;
2597 output->y = y;
2598
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002599 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002600 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002601 output->width,
2602 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002603}
2604
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002605WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002606weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002607 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002608{
2609 output->compositor = c;
2610 output->x = x;
2611 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002612 output->border.top = 0;
2613 output->border.bottom = 0;
2614 output->border.left = 0;
2615 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002616 output->mm_width = width;
2617 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002618 output->dirty = 1;
2619
Scott Moreau1bad5db2012-08-18 01:04:05 -06002620 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002621 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002622
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002623 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002624 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002625
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002626 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01002627 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002628 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002629 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002630
Casey Dahlin58ba1372012-04-19 22:50:08 -04002631 output->id = ffs(~output->compositor->output_id_pool) - 1;
2632 output->compositor->output_id_pool |= 1 << output->id;
2633
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002634 output->global =
2635 wl_display_add_global(c->wl_display, &wl_output_interface,
2636 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01002637 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002638}
2639
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002640static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002641compositor_bind(struct wl_client *client,
2642 void *data, uint32_t version, uint32_t id)
2643{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002644 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002645
2646 wl_client_add_object(client, &wl_compositor_interface,
2647 &compositor_interface, id, compositor);
2648}
2649
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002650static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002651log_uname(void)
2652{
2653 struct utsname usys;
2654
2655 uname(&usys);
2656
2657 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2658 usys.version, usys.machine);
2659}
2660
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002661WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02002662weston_environment_get_fd(const char *env)
2663{
2664 char *e, *end;
2665 int fd, flags;
2666
2667 e = getenv(env);
2668 if (!e)
2669 return -1;
2670 fd = strtol(e, &end, 0);
2671 if (*end != '\0')
2672 return -1;
2673
2674 flags = fcntl(fd, F_GETFD);
2675 if (flags == -1)
2676 return -1;
2677
2678 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
2679 unsetenv(env);
2680
2681 return fd;
2682}
2683
2684WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002685weston_compositor_init(struct weston_compositor *ec,
2686 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002687 int *argc, char *argv[],
Daniel Stonec1be8e52012-06-01 11:14:02 -04002688 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002689{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002690 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002691 struct xkb_rule_names xkb_names;
2692 const struct config_key keyboard_config_keys[] = {
2693 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2694 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2695 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2696 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2697 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2698 };
2699 const struct config_section cs[] = {
2700 { "keyboard",
2701 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2702 };
2703
2704 memset(&xkb_names, 0, sizeof(xkb_names));
2705 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002706
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002707 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002708 wl_signal_init(&ec->destroy_signal);
2709 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002710 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002711 wl_signal_init(&ec->idle_signal);
2712 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002713 wl_signal_init(&ec->show_input_panel_signal);
2714 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02002715 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002716 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01002717 wl_signal_init(&ec->output_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002718 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002719
Casey Dahlin58ba1372012-04-19 22:50:08 -04002720 ec->output_id_pool = 0;
2721
Kristian Høgsberga8873122011-11-23 10:39:34 -05002722 if (!wl_display_add_global(display, &wl_compositor_interface,
2723 ec, compositor_bind))
2724 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002725
Pekka Paalanene67858b2013-04-25 13:57:42 +03002726 if (!wl_display_add_global(display, &wl_subcompositor_interface,
2727 ec, bind_subcompositor))
2728 return -1;
2729
Daniel Stone725c2c32012-06-22 14:04:36 +01002730 wl_list_init(&ec->surface_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002731 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002732 wl_list_init(&ec->layer_list);
2733 wl_list_init(&ec->seat_list);
2734 wl_list_init(&ec->output_list);
2735 wl_list_init(&ec->key_binding_list);
2736 wl_list_init(&ec->button_binding_list);
2737 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002738 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002739
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002740 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002741 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002742
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002743 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
2744 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01002745
2746 ec->ping_handler = NULL;
2747
2748 screenshooter_create(ec);
2749 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002750 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002751
2752 wl_data_device_manager_init(ec->wl_display);
2753
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002754 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002755
Daniel Stone725c2c32012-06-22 14:04:36 +01002756 loop = wl_display_get_event_loop(ec->wl_display);
2757 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2758 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2759
2760 ec->input_loop = wl_event_loop_create();
2761
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002762 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2763 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2764
2765 weston_compositor_schedule_repaint(ec);
2766
Daniel Stone725c2c32012-06-22 14:04:36 +01002767 return 0;
2768}
2769
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002770WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002771weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002772{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002773 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002774
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002775 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002776 if (ec->input_loop_source)
2777 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002778
Matt Roper361d2ad2011-08-29 13:52:23 -07002779 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002780 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002781 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002782
Daniel Stone325fc2d2012-05-30 16:31:58 +01002783 weston_binding_list_destroy_all(&ec->key_binding_list);
2784 weston_binding_list_destroy_all(&ec->button_binding_list);
2785 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002786 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002787
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002788 weston_plane_release(&ec->primary_plane);
2789
Jonas Ådahlc97af922012-03-28 22:36:09 +02002790 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07002791}
2792
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05002793WL_EXPORT void
2794weston_version(int *major, int *minor, int *micro)
2795{
2796 *major = WESTON_VERSION_MAJOR;
2797 *minor = WESTON_VERSION_MINOR;
2798 *micro = WESTON_VERSION_MICRO;
2799}
2800
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002801static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002802{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002803 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002804
Martin Minarik6d118362012-06-07 18:01:59 +02002805 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002806 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002807
2808 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002809}
2810
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002811#ifdef HAVE_LIBUNWIND
2812
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002813static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002814print_backtrace(void)
2815{
2816 unw_cursor_t cursor;
2817 unw_context_t context;
2818 unw_word_t off;
2819 unw_proc_info_t pip;
2820 int ret, i = 0;
2821 char procname[256];
2822 const char *filename;
2823 Dl_info dlinfo;
2824
2825 pip.unwind_info = NULL;
2826 ret = unw_getcontext(&context);
2827 if (ret) {
2828 weston_log("unw_getcontext: %d\n", ret);
2829 return;
2830 }
2831
2832 ret = unw_init_local(&cursor, &context);
2833 if (ret) {
2834 weston_log("unw_init_local: %d\n", ret);
2835 return;
2836 }
2837
2838 ret = unw_step(&cursor);
2839 while (ret > 0) {
2840 ret = unw_get_proc_info(&cursor, &pip);
2841 if (ret) {
2842 weston_log("unw_get_proc_info: %d\n", ret);
2843 break;
2844 }
2845
2846 ret = unw_get_proc_name(&cursor, procname, 256, &off);
2847 if (ret && ret != -UNW_ENOMEM) {
2848 if (ret != -UNW_EUNSPEC)
2849 weston_log("unw_get_proc_name: %d\n", ret);
2850 procname[0] = '?';
2851 procname[1] = 0;
2852 }
2853
2854 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
2855 *dlinfo.dli_fname)
2856 filename = dlinfo.dli_fname;
2857 else
2858 filename = "?";
2859
2860 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
2861 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
2862
2863 ret = unw_step(&cursor);
2864 if (ret < 0)
2865 weston_log("unw_step: %d\n", ret);
2866 }
2867}
2868
2869#else
2870
2871static void
2872print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002873{
2874 void *buffer[32];
2875 int i, count;
2876 Dl_info info;
2877
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002878 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2879 for (i = 0; i < count; i++) {
2880 dladdr(buffer[i], &info);
2881 weston_log(" [%016lx] %s (%s)\n",
2882 (long) buffer[i],
2883 info.dli_sname ? info.dli_sname : "--",
2884 info.dli_fname);
2885 }
2886}
2887
2888#endif
2889
2890static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01002891on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002892{
Peter Maatmane5b42e42013-03-27 22:38:53 +01002893 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002894 * then call the backend restore function, which will switch
2895 * back to the vt we launched from or ungrab X etc and then
2896 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01002897 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002898 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01002899 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002900
Peter Maatmane5b42e42013-03-27 22:38:53 +01002901 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002902
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002903 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002904
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002905 segv_compositor->restore(segv_compositor);
2906
2907 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002908}
2909
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002910static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04002911load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002912{
2913 char path[PATH_MAX];
2914 void *module, *init;
2915
2916 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07002917 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002918 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02002919 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002920
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002921 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
2922 if (module) {
2923 weston_log("Module '%s' already loaded\n", path);
2924 dlclose(module);
2925 return NULL;
2926 }
2927
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002928 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04002929 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002930 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002931 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002932 return NULL;
2933 }
2934
2935 init = dlsym(module, entrypoint);
2936 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03002937 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00002938 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002939 return NULL;
2940 }
2941
2942 return init;
2943}
2944
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002945static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05002946load_modules(struct weston_compositor *ec, const char *modules,
2947 int *argc, char *argv[], const char *config_file)
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002948{
2949 const char *p, *end;
2950 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05002951 int (*module_init)(struct weston_compositor *ec,
2952 int *argc, char *argv[], const char *config_file);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002953
2954 if (modules == NULL)
2955 return 0;
2956
2957 p = modules;
2958 while (*p) {
2959 end = strchrnul(p, ',');
2960 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
2961 module_init = load_module(buffer, "module_init");
2962 if (module_init)
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05002963 module_init(ec, argc, argv, config_file);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04002964 p = end;
2965 while (*p == ',')
2966 p++;
2967
2968 }
2969
2970 return 0;
2971}
2972
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002973static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02002974 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
2975
2976static const char xdg_wrong_message[] =
2977 "fatal: environment variable XDG_RUNTIME_DIR\n"
2978 "is set to \"%s\", which is not a directory.\n";
2979
2980static const char xdg_wrong_mode_message[] =
2981 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
2982 "correctly. Unix access mode must be 0700 but is %o,\n"
2983 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04002984 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02002985
2986static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03002987 "Refer to your distribution on how to get it, or\n"
2988 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
2989 "on how to implement it.\n";
2990
Martin Minarik37032f82012-06-18 20:15:18 +02002991static void
2992verify_xdg_runtime_dir(void)
2993{
2994 char *dir = getenv("XDG_RUNTIME_DIR");
2995 struct stat s;
2996
2997 if (!dir) {
2998 weston_log(xdg_error_message);
2999 weston_log_continue(xdg_detail_message);
3000 exit(EXIT_FAILURE);
3001 }
3002
3003 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3004 weston_log(xdg_wrong_message, dir);
3005 weston_log_continue(xdg_detail_message);
3006 exit(EXIT_FAILURE);
3007 }
3008
3009 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3010 weston_log(xdg_wrong_mode_message,
3011 dir, s.st_mode & 0777, s.st_uid);
3012 weston_log_continue(xdg_detail_message);
3013 }
3014}
3015
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003016static int
3017usage(int error_code)
3018{
3019 fprintf(stderr,
3020 "Usage: weston [OPTIONS]\n\n"
3021 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3022 "Weston supports multiple backends, and depending on which backend is in use\n"
3023 "different options will be accepted.\n\n"
3024
3025
3026 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003027 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003028 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003029 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3030 "\t\t\t\twayland-backend.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003031 " -S, --socket=NAME\tName of socket to listen on\n"
3032 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003033 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003034 " --log==FILE\t\tLog to the given file\n"
3035 " -h, --help\t\tThis help message\n\n");
3036
3037 fprintf(stderr,
3038 "Options for drm-backend.so:\n\n"
3039 " --connector=ID\tBring up only this connector\n"
3040 " --seat=SEAT\t\tThe seat that weston should run on\n"
3041 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003042 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003043 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3044
3045 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003046 "Options for fbdev-backend.so:\n\n"
3047 " --tty=TTY\t\tThe tty to use\n"
3048 " --device=DEVICE\tThe framebuffer device to use\n\n");
3049
3050 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003051 "Options for x11-backend.so:\n\n"
3052 " --width=WIDTH\t\tWidth of X window\n"
3053 " --height=HEIGHT\tHeight of X window\n"
3054 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003055 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003056 " --output-count=COUNT\tCreate multiple outputs\n"
3057 " --no-input\t\tDont create input devices\n\n");
3058
3059 fprintf(stderr,
3060 "Options for wayland-backend.so:\n\n"
3061 " --width=WIDTH\t\tWidth of Wayland surface\n"
3062 " --height=HEIGHT\tHeight of Wayland surface\n"
3063 " --display=DISPLAY\tWayland display to connect to\n\n");
3064
3065 exit(error_code);
3066}
3067
Peter Maatmane5b42e42013-03-27 22:38:53 +01003068static void
3069catch_signals(void)
3070{
3071 struct sigaction action;
3072
3073 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3074 action.sa_sigaction = on_caught_signal;
3075 sigemptyset(&action.sa_mask);
3076 sigaction(SIGSEGV, &action, NULL);
3077 sigaction(SIGABRT, &action, NULL);
3078}
3079
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003080int main(int argc, char *argv[])
3081{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003082 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003083 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003084 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003085 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003086 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003087 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003088 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003089 int *argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003090 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003091 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003092 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003093 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003094 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003095 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003096 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003097 int32_t version = 0;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003098 char *config_file;
3099
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003100 const struct config_key core_config_keys[] = {
3101 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003102 };
3103
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003104 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003105 { "core",
3106 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003107 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003108
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003109 const struct weston_option core_options[] = {
3110 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3111 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3112 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003113 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003114 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003115 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003116 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003117 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003118
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003119 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003120
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003121 if (help)
3122 usage(EXIT_SUCCESS);
3123
Scott Moreau12245142012-08-29 15:15:58 -06003124 if (version) {
3125 printf(PACKAGE_STRING "\n");
3126 return EXIT_SUCCESS;
3127 }
3128
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003129 weston_log_file_open(log);
3130
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003131 weston_log("%s\n"
3132 STAMP_SPACE "%s\n"
3133 STAMP_SPACE "Bug reports to: %s\n"
3134 STAMP_SPACE "Build: %s\n",
3135 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003136 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003137 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003138
Martin Minarik37032f82012-06-18 20:15:18 +02003139 verify_xdg_runtime_dir();
3140
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003141 display = wl_display_create();
3142
Tiago Vignatti2116b892011-08-08 05:52:59 -07003143 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003144 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3145 display);
3146 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3147 display);
3148 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3149 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003150
3151 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003152 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3153 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003154
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003155 if (!backend) {
3156 if (getenv("WAYLAND_DISPLAY"))
3157 backend = "wayland-backend.so";
3158 else if (getenv("DISPLAY"))
3159 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003160 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003161 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003162 }
3163
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003164 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003165 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003166
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003167 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003168 if (!backend_init)
3169 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003170
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003171 ec = backend_init(display, &argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003172 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003173 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003174 exit(EXIT_FAILURE);
3175 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003176
Peter Maatmane5b42e42013-03-27 22:38:53 +01003177 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003178 segv_compositor = ec;
3179
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003180 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003181
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003182 setenv("WAYLAND_DISPLAY", socket_name, 1);
3183
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003184 if (load_modules(ec, modules, &argc, argv, config_file) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003185 goto out;
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003186 if (load_modules(ec, option_modules, &argc, argv, config_file) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003187 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003188
Kristian Høgsberg142d5462013-02-21 21:10:36 -05003189 free(config_file);
3190
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003191 for (i = 1; i < argc; i++)
3192 weston_log("fatal: unhandled option: %s\n", argv[i]);
3193 if (argc > 1) {
3194 ret = EXIT_FAILURE;
3195 goto out;
3196 }
3197
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003198 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003199 weston_log("fatal: failed to add socket: %m\n");
3200 ret = EXIT_FAILURE;
3201 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003202 }
3203
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003204 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003205
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003206 wl_display_run(display);
3207
3208 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003209 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003210 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003211
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003212 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003213
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003214 for (i = ARRAY_LENGTH(signals); i;)
3215 wl_event_source_remove(signals[--i]);
3216
Daniel Stone855028f2012-05-01 20:37:10 +01003217 weston_compositor_xkb_destroy(ec);
3218
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003219 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003220 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003221
Martin Minarik19e6f262012-06-07 13:08:46 +02003222 weston_log_file_close();
3223
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003224 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003225}