blob: 3213a7b4deb22ccdb5ecac281cca11a4b0951343 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanend581a8f2012-01-27 16:25:16 +02004 * Copyright © 2012 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberga9410222011-01-14 17:22:35 -050025#include "config.h"
26
Daniel Stoneb7452fe2012-06-01 12:14:06 +010027#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010032#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020034#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040035#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010036#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040037#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020038#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020039#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020040#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040041#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050042#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040044#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040045#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050046#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040047#include <sys/time.h>
48#include <time.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050049
Marcin Slusarz554a0da2013-02-18 13:27:22 -050050#ifdef HAVE_LIBUNWIND
51#define UNW_LOCAL_ONLY
52#include <libunwind.h>
53#endif
54
Kristian Høgsberg82863022010-06-04 21:52:02 -040055#include "compositor.h"
U. Artie Eoffec08f332013-05-13 15:55:47 -070056#include "subsurface-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030057#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040058#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050059#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050060
Kristian Høgsberg27da5382011-06-21 17:32:25 -040061static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040062static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040063
64static int
65sigchld_handler(int signal_number, void *data)
66{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050067 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040068 int status;
69 pid_t pid;
70
Bill Spitzak027b9622012-03-17 15:22:03 -070071 pid = waitpid(-1, &status, WNOHANG);
72 if (!pid)
73 return 1;
74
Kristian Høgsberg27da5382011-06-21 17:32:25 -040075 wl_list_for_each(p, &child_process_list, link) {
76 if (p->pid == pid)
77 break;
78 }
79
80 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020081 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040082 return 1;
83 }
84
85 wl_list_remove(&p->link);
86 p->cleanup(p, status);
87
88 return 1;
89}
90
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020091static void
Alexander Larsson0b135062013-05-28 16:23:36 +020092weston_output_transform_scale_init(struct weston_output *output,
93 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020094
Rob Bradford27b17932013-06-26 18:08:46 +010095static void
96weston_compositor_build_surface_list(struct weston_compositor *compositor);
97
Alex Wu2dda6042012-04-17 17:20:47 +080098WL_EXPORT int
Alexander Larsson355748e2013-05-28 16:23:38 +020099weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode, int32_t scale)
Alex Wu2dda6042012-04-17 17:20:47 +0800100{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200101 struct weston_seat *seat;
102 pixman_region32_t old_output_region;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200103 int ret;
104
Alex Wu2dda6042012-04-17 17:20:47 +0800105 if (!output->switch_mode)
106 return -1;
107
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200108 ret = output->switch_mode(output, mode);
109 if (ret < 0)
110 return ret;
111
Alexander Larsson355748e2013-05-28 16:23:38 +0200112 output->scale = scale;
113
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200114 pixman_region32_init(&old_output_region);
115 pixman_region32_copy(&old_output_region, &output->region);
116
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200117 /* Update output region and transformation matrix */
Alexander Larsson0b135062013-05-28 16:23:36 +0200118 weston_output_transform_scale_init(output, output->transform, output->scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200119
120 pixman_region32_init(&output->previous_damage);
121 pixman_region32_init_rect(&output->region, output->x, output->y,
122 output->width, output->height);
123
124 weston_output_update_matrix(output);
125
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200126 /* If a pointer falls outside the outputs new geometry, move it to its
127 * lower-right corner */
128 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400129 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200130 int32_t x, y;
131
132 if (!pointer)
133 continue;
134
135 x = wl_fixed_to_int(pointer->x);
136 y = wl_fixed_to_int(pointer->y);
137
138 if (!pixman_region32_contains_point(&old_output_region,
139 x, y, NULL) ||
140 pixman_region32_contains_point(&output->region,
141 x, y, NULL))
142 continue;
143
144 if (x >= output->x + output->width)
145 x = output->x + output->width - 1;
146 if (y >= output->y + output->height)
147 y = output->y + output->height - 1;
148
149 pointer->x = wl_fixed_from_int(x);
150 pointer->y = wl_fixed_from_int(y);
151 }
152
153 pixman_region32_fini(&old_output_region);
154
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200155 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800156}
157
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400158WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500159weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400160{
161 wl_list_insert(&child_process_list, &process->link);
162}
163
Benjamin Franzke06286262011-05-06 19:12:33 +0200164static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200165child_client_exec(int sockfd, const char *path)
166{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500167 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200168 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200169 sigset_t allsigs;
170
171 /* do not give our signal mask to the new process */
172 sigfillset(&allsigs);
173 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200174
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500175 /* Launch clients as the user. */
176 seteuid(getuid());
177
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500178 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
179 * non-CLOEXEC fd to pass through exec. */
180 clientfd = dup(sockfd);
181 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200182 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500183 return;
184 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200185
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500186 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200187 setenv("WAYLAND_SOCKET", s, 1);
188
189 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200190 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200191 path);
192}
193
194WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500195weston_client_launch(struct weston_compositor *compositor,
196 struct weston_process *proc,
197 const char *path,
198 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200199{
200 int sv[2];
201 pid_t pid;
202 struct wl_client *client;
203
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300204 weston_log("launching '%s'\n", path);
205
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300206 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200207 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200208 "socketpair failed while launching '%s': %m\n",
209 path);
210 return NULL;
211 }
212
213 pid = fork();
214 if (pid == -1) {
215 close(sv[0]);
216 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200217 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200218 "fork failed while launching '%s': %m\n", path);
219 return NULL;
220 }
221
222 if (pid == 0) {
223 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700224 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200225 }
226
227 close(sv[1]);
228
229 client = wl_client_create(compositor->wl_display, sv[0]);
230 if (!client) {
231 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200232 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200233 "wl_client_create failed while launching '%s'.\n",
234 path);
235 return NULL;
236 }
237
238 proc->pid = pid;
239 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500240 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200241
242 return client;
243}
244
245static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300246surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
247{
248 struct weston_surface *surface =
249 container_of(listener, struct weston_surface,
250 pending.buffer_destroy_listener);
251
252 surface->pending.buffer = NULL;
253}
254
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200255static void
256empty_region(pixman_region32_t *region)
257{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300258 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200259 pixman_region32_init(region);
260}
261
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300262static void
263region_init_infinite(pixman_region32_t *region)
264{
265 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
266 UINT32_MAX, UINT32_MAX);
267}
268
Pekka Paalanene67858b2013-04-25 13:57:42 +0300269static struct weston_subsurface *
270weston_surface_to_subsurface(struct weston_surface *surface);
271
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500272WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500273weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500274{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500275 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400276
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200277 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400278 if (surface == NULL)
279 return NULL;
280
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500281 wl_signal_init(&surface->destroy_signal);
282
283 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500284
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500285 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500286 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500287
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500288 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400289 surface->alpha = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400290
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100291 if (compositor->renderer->create_surface(surface) < 0) {
292 free(surface);
293 return NULL;
294 }
295
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200296 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200297 surface->buffer_scale = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200298 surface->pending.buffer_transform = surface->buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200299 surface->pending.buffer_scale = surface->buffer_scale;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400300 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400301 surface->plane = &compositor->primary_plane;
Giulio Camuffo184df502013-02-21 11:29:21 +0100302 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200303
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400304 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500305 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500306 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300307 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200308 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500309 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400310
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200311 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200312 wl_list_insert(&surface->geometry.transformation_list,
313 &surface->transform.position.link);
314 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200315 wl_list_init(&surface->geometry.child_list);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200316 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200317 surface->transform.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500318
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300319 surface->pending.buffer_destroy_listener.notify =
320 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300321 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300322 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300323 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300324 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300325
Pekka Paalanene67858b2013-04-25 13:57:42 +0300326 wl_list_init(&surface->subsurface_list);
327 wl_list_init(&surface->subsurface_list_pending);
328
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400329 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500330}
331
Alex Wu8811bf92012-02-28 18:07:54 +0800332WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500333weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200334 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500335{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100336 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500337}
338
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400339WL_EXPORT void
340weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200341 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200342{
343 if (surface->transform.enabled) {
344 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
345
346 weston_matrix_transform(&surface->transform.matrix, &v);
347
348 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200349 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700350 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200351 v.f[3]);
352 *x = 0;
353 *y = 0;
354 return;
355 }
356
357 *x = v.f[0] / v.f[3];
358 *y = v.f[1] / v.f[3];
359 } else {
360 *x = sx + surface->geometry.x;
361 *y = sy + surface->geometry.y;
362 }
363}
364
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500365WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200366weston_transformed_coord(int width, int height,
367 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200368 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200369 float sx, float sy, float *bx, float *by)
370{
371 switch (transform) {
372 case WL_OUTPUT_TRANSFORM_NORMAL:
373 default:
374 *bx = sx;
375 *by = sy;
376 break;
377 case WL_OUTPUT_TRANSFORM_FLIPPED:
378 *bx = width - sx;
379 *by = sy;
380 break;
381 case WL_OUTPUT_TRANSFORM_90:
382 *bx = height - sy;
383 *by = sx;
384 break;
385 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
386 *bx = height - sy;
387 *by = width - sx;
388 break;
389 case WL_OUTPUT_TRANSFORM_180:
390 *bx = width - sx;
391 *by = height - sy;
392 break;
393 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
394 *bx = sx;
395 *by = height - sy;
396 break;
397 case WL_OUTPUT_TRANSFORM_270:
398 *bx = sy;
399 *by = width - sx;
400 break;
401 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
402 *bx = sy;
403 *by = sx;
404 break;
405 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200406
407 *bx *= scale;
408 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200409}
410
411WL_EXPORT pixman_box32_t
412weston_transformed_rect(int width, int height,
413 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200414 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200415 pixman_box32_t rect)
416{
417 float x1, x2, y1, y2;
418
419 pixman_box32_t ret;
420
Alexander Larsson4ea95522013-05-22 14:41:37 +0200421 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200422 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200423 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200424 rect.x2, rect.y2, &x2, &y2);
425
426 if (x1 <= x2) {
427 ret.x1 = x1;
428 ret.x2 = x2;
429 } else {
430 ret.x1 = x2;
431 ret.x2 = x1;
432 }
433
434 if (y1 <= y2) {
435 ret.y1 = y1;
436 ret.y2 = y2;
437 } else {
438 ret.y1 = y2;
439 ret.y2 = y1;
440 }
441
442 return ret;
443}
444
445WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200446weston_surface_to_buffer_float(struct weston_surface *surface,
447 float sx, float sy, float *bx, float *by)
448{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200449 weston_transformed_coord(surface->geometry.width,
450 surface->geometry.height,
451 surface->buffer_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200452 surface->buffer_scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200453 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200454}
455
Alexander Larsson4ea95522013-05-22 14:41:37 +0200456WL_EXPORT void
457weston_surface_to_buffer(struct weston_surface *surface,
458 int sx, int sy, int *bx, int *by)
459{
460 float bxf, byf;
461
462 weston_transformed_coord(surface->geometry.width,
463 surface->geometry.height,
464 surface->buffer_transform,
465 surface->buffer_scale,
466 sx, sy, &bxf, &byf);
467 *bx = floorf(bxf);
468 *by = floorf(byf);
469}
470
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200471WL_EXPORT pixman_box32_t
472weston_surface_to_buffer_rect(struct weston_surface *surface,
473 pixman_box32_t rect)
474{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200475 return weston_transformed_rect(surface->geometry.width,
476 surface->geometry.height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200477 surface->buffer_transform,
478 surface->buffer_scale,
479 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200480}
481
482WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400483weston_surface_move_to_plane(struct weston_surface *surface,
484 struct weston_plane *plane)
485{
486 if (surface->plane == plane)
487 return;
488
489 weston_surface_damage_below(surface);
490 surface->plane = plane;
491 weston_surface_damage(surface);
492}
493
494WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500495weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200496{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500497 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200498
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500499 pixman_region32_init(&damage);
500 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
501 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400502 pixman_region32_union(&surface->plane->damage,
503 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500504 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200505}
506
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400507static void
508weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
509{
510 uint32_t different = es->output_mask ^ mask;
511 uint32_t entered = mask & different;
512 uint32_t left = es->output_mask & different;
513 struct weston_output *output;
514 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500515 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400516
517 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500518 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400519 return;
520 if (different == 0)
521 return;
522
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500523 client = wl_resource_get_client(es->resource);
524
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400525 wl_list_for_each(output, &es->compositor->output_list, link) {
526 if (1 << output->id & different)
527 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500528 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400529 client);
530 if (resource == NULL)
531 continue;
532 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500533 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400534 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500535 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400536 }
537}
538
539static void
540weston_surface_assign_output(struct weston_surface *es)
541{
542 struct weston_compositor *ec = es->compositor;
543 struct weston_output *output, *new_output;
544 pixman_region32_t region;
545 uint32_t max, area, mask;
546 pixman_box32_t *e;
547
548 new_output = NULL;
549 max = 0;
550 mask = 0;
551 pixman_region32_init(&region);
552 wl_list_for_each(output, &ec->output_list, link) {
553 pixman_region32_intersect(&region, &es->transform.boundingbox,
554 &output->region);
555
556 e = pixman_region32_extents(&region);
557 area = (e->x2 - e->x1) * (e->y2 - e->y1);
558
559 if (area > 0)
560 mask |= 1 << output->id;
561
562 if (area >= max) {
563 new_output = output;
564 max = area;
565 }
566 }
567 pixman_region32_fini(&region);
568
569 es->output = new_output;
570 weston_surface_update_output_mask(es, mask);
571}
572
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200573static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200574surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
575 int32_t width, int32_t height,
576 pixman_region32_t *bbox)
577{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200578 float min_x = HUGE_VALF, min_y = HUGE_VALF;
579 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200580 int32_t s[4][2] = {
581 { sx, sy },
582 { sx, sy + height },
583 { sx + width, sy },
584 { sx + width, sy + height }
585 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200586 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200587 int i;
588
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300589 if (width == 0 || height == 0) {
590 /* avoid rounding empty bbox to 1x1 */
591 pixman_region32_init(bbox);
592 return;
593 }
594
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200595 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200596 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400597 weston_surface_to_global_float(surface,
598 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200599 if (x < min_x)
600 min_x = x;
601 if (x > max_x)
602 max_x = x;
603 if (y < min_y)
604 min_y = y;
605 if (y > max_y)
606 max_y = y;
607 }
608
Pekka Paalanen219b9822012-02-08 15:38:37 +0200609 int_x = floorf(min_x);
610 int_y = floorf(min_y);
611 pixman_region32_init_rect(bbox, int_x, int_y,
612 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200613}
614
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200615static void
616weston_surface_update_transform_disable(struct weston_surface *surface)
617{
618 surface->transform.enabled = 0;
619
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200620 /* round off fractions when not transformed */
621 surface->geometry.x = roundf(surface->geometry.x);
622 surface->geometry.y = roundf(surface->geometry.y);
623
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500624 /* Otherwise identity matrix, but with x and y translation. */
625 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
626 surface->transform.position.matrix.d[12] = surface->geometry.x;
627 surface->transform.position.matrix.d[13] = surface->geometry.y;
628
629 surface->transform.matrix = surface->transform.position.matrix;
630
Kristian Høgsberg9bcaaeb2013-02-28 14:56:43 -0500631 surface->transform.inverse = surface->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500632 surface->transform.inverse.d[12] = -surface->geometry.x;
633 surface->transform.inverse.d[13] = -surface->geometry.y;
634
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200635 pixman_region32_init_rect(&surface->transform.boundingbox,
636 surface->geometry.x,
637 surface->geometry.y,
638 surface->geometry.width,
639 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500640
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400641 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500642 pixman_region32_copy(&surface->transform.opaque,
643 &surface->opaque);
644 pixman_region32_translate(&surface->transform.opaque,
645 surface->geometry.x,
646 surface->geometry.y);
647 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200648}
649
650static int
651weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200652{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200653 struct weston_surface *parent = surface->geometry.parent;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200654 struct weston_matrix *matrix = &surface->transform.matrix;
655 struct weston_matrix *inverse = &surface->transform.inverse;
656 struct weston_transform *tform;
657
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200658 surface->transform.enabled = 1;
659
660 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300661 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200662 surface->transform.position.matrix.d[12] = surface->geometry.x;
663 surface->transform.position.matrix.d[13] = surface->geometry.y;
664
665 weston_matrix_init(matrix);
666 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
667 weston_matrix_multiply(matrix, &tform->matrix);
668
Pekka Paalanen483243f2013-03-08 14:56:50 +0200669 if (parent)
670 weston_matrix_multiply(matrix, &parent->transform.matrix);
671
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200672 if (weston_matrix_invert(inverse, matrix) < 0) {
673 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200674 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200675 " transformation not invertible.\n", surface);
676 return -1;
677 }
678
679 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
680 surface->geometry.height,
681 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500682
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200683 return 0;
684}
685
686WL_EXPORT void
687weston_surface_update_transform(struct weston_surface *surface)
688{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200689 struct weston_surface *parent = surface->geometry.parent;
690
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200691 if (!surface->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200692 return;
693
Pekka Paalanen483243f2013-03-08 14:56:50 +0200694 if (parent)
695 weston_surface_update_transform(parent);
696
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200697 surface->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200698
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500699 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200700
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200701 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500702 pixman_region32_fini(&surface->transform.opaque);
703 pixman_region32_init(&surface->transform.opaque);
704
Pekka Paalanencd403622012-01-25 13:37:39 +0200705 /* transform.position is always in transformation_list */
706 if (surface->geometry.transformation_list.next ==
707 &surface->transform.position.link &&
708 surface->geometry.transformation_list.prev ==
Pekka Paalanen483243f2013-03-08 14:56:50 +0200709 &surface->transform.position.link &&
710 !parent) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200711 weston_surface_update_transform_disable(surface);
712 } else {
713 if (weston_surface_update_transform_enable(surface) < 0)
714 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200715 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200716
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400717 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200718
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300719 weston_surface_assign_output(surface);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300720
721 wl_signal_emit(&surface->compositor->transform_signal, surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200722}
723
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200724WL_EXPORT void
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200725weston_surface_geometry_dirty(struct weston_surface *surface)
726{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200727 struct weston_surface *child;
728
729 /*
730 * The invariant: if surface->geometry.dirty, then all surfaces
731 * in surface->geometry.child_list have geometry.dirty too.
732 * Corollary: if not parent->geometry.dirty, then all ancestors
733 * are not dirty.
734 */
735
736 if (surface->transform.dirty)
737 return;
738
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200739 surface->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200740
741 wl_list_for_each(child, &surface->geometry.child_list,
742 geometry.parent_link)
743 weston_surface_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200744}
745
746WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100747weston_surface_to_global_fixed(struct weston_surface *surface,
748 wl_fixed_t sx, wl_fixed_t sy,
749 wl_fixed_t *x, wl_fixed_t *y)
750{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200751 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100752
753 weston_surface_to_global_float(surface,
754 wl_fixed_to_double(sx),
755 wl_fixed_to_double(sy),
756 &xf, &yf);
757 *x = wl_fixed_from_double(xf);
758 *y = wl_fixed_from_double(yf);
759}
760
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400761WL_EXPORT void
762weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200763 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200764{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200765 if (surface->transform.enabled) {
766 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
767
768 weston_matrix_transform(&surface->transform.inverse, &v);
769
770 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200771 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200772 "weston_surface_from_global(), divisor = %g\n",
773 v.f[3]);
774 *sx = 0;
775 *sy = 0;
776 return;
777 }
778
Pekka Paalanencd403622012-01-25 13:37:39 +0200779 *sx = v.f[0] / v.f[3];
780 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200781 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200782 *sx = x - surface->geometry.x;
783 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200784 }
785}
786
787WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100788weston_surface_from_global_fixed(struct weston_surface *surface,
789 wl_fixed_t x, wl_fixed_t y,
790 wl_fixed_t *sx, wl_fixed_t *sy)
791{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200792 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100793
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400794 weston_surface_from_global_float(surface,
795 wl_fixed_to_double(x),
796 wl_fixed_to_double(y),
797 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100798 *sx = wl_fixed_from_double(sxf);
799 *sy = wl_fixed_from_double(syf);
800}
801
802WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200803weston_surface_from_global(struct weston_surface *surface,
804 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
805{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200806 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200807
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400808 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200809 *sx = floorf(sxf);
810 *sy = floorf(syf);
811}
812
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400813WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400814weston_surface_schedule_repaint(struct weston_surface *surface)
815{
816 struct weston_output *output;
817
818 wl_list_for_each(output, &surface->compositor->output_list, link)
819 if (surface->output_mask & (1 << output->id))
820 weston_output_schedule_repaint(output);
821}
822
823WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500824weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500825{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400826 pixman_region32_union_rect(&surface->damage, &surface->damage,
827 0, 0, surface->geometry.width,
828 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200829
Kristian Høgsberg98238702012-08-03 16:29:12 -0400830 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500831}
832
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400833WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500834weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200835 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400836{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200837 surface->geometry.x = x;
838 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200839 surface->geometry.width = width;
840 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200841 weston_surface_geometry_dirty(surface);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400842}
843
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200844WL_EXPORT void
845weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200846 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200847{
848 surface->geometry.x = x;
849 surface->geometry.y = y;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200850 weston_surface_geometry_dirty(surface);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200851}
852
Pekka Paalanen483243f2013-03-08 14:56:50 +0200853static void
854transform_parent_handle_parent_destroy(struct wl_listener *listener,
855 void *data)
856{
857 struct weston_surface *surface =
858 container_of(listener, struct weston_surface,
859 geometry.parent_destroy_listener);
860
861 weston_surface_set_transform_parent(surface, NULL);
862}
863
864WL_EXPORT void
865weston_surface_set_transform_parent(struct weston_surface *surface,
866 struct weston_surface *parent)
867{
868 if (surface->geometry.parent) {
869 wl_list_remove(&surface->geometry.parent_destroy_listener.link);
870 wl_list_remove(&surface->geometry.parent_link);
871 }
872
873 surface->geometry.parent = parent;
874
875 surface->geometry.parent_destroy_listener.notify =
876 transform_parent_handle_parent_destroy;
877 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500878 wl_signal_add(&parent->destroy_signal,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200879 &surface->geometry.parent_destroy_listener);
880 wl_list_insert(&parent->geometry.child_list,
881 &surface->geometry.parent_link);
882 }
883
884 weston_surface_geometry_dirty(surface);
885}
886
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300887WL_EXPORT int
888weston_surface_is_mapped(struct weston_surface *surface)
889{
890 if (surface->output)
891 return 1;
892 else
893 return 0;
894}
895
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200896WL_EXPORT int32_t
897weston_surface_buffer_width(struct weston_surface *surface)
898{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200899 int32_t width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200900 switch (surface->buffer_transform) {
901 case WL_OUTPUT_TRANSFORM_90:
902 case WL_OUTPUT_TRANSFORM_270:
903 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
904 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200905 width = surface->buffer_ref.buffer->height;
906 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200907 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200908 width = surface->buffer_ref.buffer->width;
909 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200910 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200911 return width / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200912}
913
914WL_EXPORT int32_t
915weston_surface_buffer_height(struct weston_surface *surface)
916{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200917 int32_t height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200918 switch (surface->buffer_transform) {
919 case WL_OUTPUT_TRANSFORM_90:
920 case WL_OUTPUT_TRANSFORM_270:
921 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
922 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200923 height = surface->buffer_ref.buffer->width;
924 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200925 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200926 height = surface->buffer_ref.buffer->height;
927 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200928 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200929 return height / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200930}
931
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400932WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500933weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500934{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400935 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500936
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400937 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500938
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400939 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500940}
941
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400942WL_EXPORT struct weston_surface *
Tiago Vignatti9d393522012-02-10 16:26:19 +0200943weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100944 wl_fixed_t x, wl_fixed_t y,
945 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200946{
947 struct weston_surface *surface;
948
949 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100950 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500951 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100952 wl_fixed_to_int(*sx),
953 wl_fixed_to_int(*sy),
954 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200955 return surface;
956 }
957
958 return NULL;
959}
960
961static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500962weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400963{
Daniel Stone37816df2012-05-16 18:45:18 +0100964 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400965
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500966 if (!compositor->focus)
967 return;
968
Daniel Stone37816df2012-05-16 18:45:18 +0100969 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400970 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400971}
972
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400973WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500974weston_surface_unmap(struct weston_surface *surface)
975{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100976 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500977
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500978 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500979 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500980 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500981
Daniel Stone4dab5db2012-05-30 16:31:53 +0100982 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400983 if (seat->keyboard && seat->keyboard->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400984 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400985 if (seat->pointer && seat->pointer->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400986 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400987 NULL,
988 wl_fixed_from_int(0),
989 wl_fixed_from_int(0));
Michael Fua2bb7912013-07-23 15:51:06 +0800990 if (seat->touch && seat->touch->focus == surface)
991 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +0100992 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500993
Kristian Høgsberg98238702012-08-03 16:29:12 -0400994 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500995}
996
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400997struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -0500998 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400999 struct wl_list link;
1000};
1001
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001002
1003WL_EXPORT void
1004weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001005{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001006 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1007
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001008 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001009 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001010
Pekka Paalanen483243f2013-03-08 14:56:50 +02001011 assert(wl_list_empty(&surface->geometry.child_list));
Pekka Paalanene67858b2013-04-25 13:57:42 +03001012 assert(wl_list_empty(&surface->subsurface_list_pending));
1013 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001014
Rob Bradford27b17932013-06-26 18:08:46 +01001015 if (weston_surface_is_mapped(surface)) {
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001016 weston_surface_unmap(surface);
Rob Bradford27b17932013-06-26 18:08:46 +01001017 weston_compositor_build_surface_list(compositor);
1018 }
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001019
Pekka Paalanenbc106382012-10-10 12:49:31 +03001020 wl_list_for_each_safe(cb, next,
1021 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001022 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001023
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001024 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001025 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001026 pixman_region32_fini(&surface->pending.damage);
1027
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001028 if (surface->pending.buffer)
1029 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1030
Pekka Paalanende685b82012-12-04 15:58:12 +02001031 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001032
Kristian Høgsberg42263852012-09-06 21:59:29 -04001033 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001034
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001035 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001036 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001037 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001038 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001039 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001040
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001041 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001042 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001043
Pekka Paalanen483243f2013-03-08 14:56:50 +02001044 weston_surface_set_transform_parent(surface, NULL);
1045
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001046 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001047}
1048
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001049static void
1050destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001051{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001052 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001053
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001054 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001055}
1056
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001057static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001058weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1059{
1060 struct weston_buffer *buffer =
1061 container_of(listener, struct weston_buffer, destroy_listener);
1062
1063 wl_signal_emit(&buffer->destroy_signal, buffer);
1064 free(buffer);
1065}
1066
1067struct weston_buffer *
1068weston_buffer_from_resource(struct wl_resource *resource)
1069{
1070 struct weston_buffer *buffer;
1071 struct wl_listener *listener;
1072
1073 listener = wl_resource_get_destroy_listener(resource,
1074 weston_buffer_destroy_handler);
1075
1076 if (listener) {
1077 buffer = container_of(listener, struct weston_buffer,
1078 destroy_listener);
1079 } else {
Peter Huttererf3d62272013-08-08 11:57:05 +10001080 buffer = zalloc(sizeof *buffer);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001081 buffer->resource = resource;
1082 wl_signal_init(&buffer->destroy_signal);
1083 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
1084 wl_resource_add_destroy_listener(resource,
1085 &buffer->destroy_listener);
1086 }
1087
1088 return buffer;
1089}
1090
1091static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001092weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1093 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001094{
Pekka Paalanende685b82012-12-04 15:58:12 +02001095 struct weston_buffer_reference *ref =
1096 container_of(listener, struct weston_buffer_reference,
1097 destroy_listener);
1098
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001099 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001100 ref->buffer = NULL;
1101}
1102
1103WL_EXPORT void
1104weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001105 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001106{
1107 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001108 ref->buffer->busy_count--;
1109 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001110 assert(wl_resource_get_client(ref->buffer->resource));
1111 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001112 WL_BUFFER_RELEASE);
1113 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001114 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001115 }
1116
Pekka Paalanende685b82012-12-04 15:58:12 +02001117 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001118 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001119 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001120 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001121 }
1122
Pekka Paalanende685b82012-12-04 15:58:12 +02001123 ref->buffer = buffer;
1124 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1125}
1126
1127static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001128weston_surface_attach(struct weston_surface *surface,
1129 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001130{
1131 weston_buffer_reference(&surface->buffer_ref, buffer);
1132
Pekka Paalanena6421c42012-12-04 15:58:10 +02001133 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001134 if (weston_surface_is_mapped(surface))
1135 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001136 }
1137
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001138 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001139}
1140
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001141WL_EXPORT void
1142weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001143{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001144 wl_list_remove(&surface->layer_link);
1145 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001146 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001147 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001148}
1149
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001150WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001151weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001152{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001153 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001154
1155 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001156 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001157}
1158
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001159WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001160weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001161{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001162 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001163
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001164 pixman_region32_union(&compositor->primary_plane.damage,
1165 &compositor->primary_plane.damage,
1166 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001167 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001168}
1169
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001170static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001171surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001172 pixman_region32_t *opaque)
1173{
Pekka Paalanende685b82012-12-04 15:58:12 +02001174 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001175 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001176 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001177
1178 if (surface->transform.enabled) {
1179 pixman_box32_t *extents;
1180
1181 extents = pixman_region32_extents(&surface->damage);
1182 surface_compute_bbox(surface, extents->x1, extents->y1,
1183 extents->x2 - extents->x1,
1184 extents->y2 - extents->y1,
1185 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001186 pixman_region32_translate(&surface->damage,
1187 -surface->plane->x,
1188 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001189 } else {
1190 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001191 surface->geometry.x - surface->plane->x,
1192 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001193 }
1194
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001195 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001196 pixman_region32_union(&surface->plane->damage,
1197 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001198 empty_region(&surface->damage);
1199 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001200 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001201}
1202
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001203static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001204compositor_accumulate_damage(struct weston_compositor *ec)
1205{
1206 struct weston_plane *plane;
1207 struct weston_surface *es;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001208 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001209
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001210 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001211
1212 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001213 pixman_region32_copy(&plane->clip, &clip);
1214
1215 pixman_region32_init(&opaque);
1216
1217 wl_list_for_each(es, &ec->surface_list, link) {
1218 if (es->plane != plane)
1219 continue;
1220
1221 surface_accumulate_damage(es, &opaque);
1222 }
1223
1224 pixman_region32_union(&clip, &clip, &opaque);
1225 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001226 }
1227
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001228 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001229
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001230 wl_list_for_each(es, &ec->surface_list, link) {
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001231 /* Both the renderer and the backend have seen the buffer
1232 * by now. If renderer needs the buffer, it has its own
1233 * reference set. If the backend wants to keep the buffer
1234 * around for migrating the surface into a non-primary plane
1235 * later, keep_buffer is true. Otherwise, drop the core
1236 * reference now, and allow early buffer release. This enables
1237 * clients to use single-buffering.
1238 */
1239 if (!es->keep_buffer)
1240 weston_buffer_reference(&es->buffer_ref, NULL);
1241 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001242}
1243
1244static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001245surface_list_add(struct weston_compositor *compositor,
1246 struct weston_surface *surface)
1247{
1248 struct weston_subsurface *sub;
1249
1250 if (wl_list_empty(&surface->subsurface_list)) {
1251 weston_surface_update_transform(surface);
1252 wl_list_insert(compositor->surface_list.prev, &surface->link);
1253 return;
1254 }
1255
1256 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1257 if (!weston_surface_is_mapped(sub->surface))
1258 continue;
1259
1260 if (sub->surface == surface) {
1261 weston_surface_update_transform(sub->surface);
1262 wl_list_insert(compositor->surface_list.prev,
1263 &sub->surface->link);
1264 } else {
1265 surface_list_add(compositor, sub->surface);
1266 }
1267 }
1268}
1269
1270static void
1271weston_compositor_build_surface_list(struct weston_compositor *compositor)
1272{
1273 struct weston_surface *surface;
1274 struct weston_layer *layer;
1275
1276 wl_list_init(&compositor->surface_list);
1277 wl_list_for_each(layer, &compositor->layer_list, link) {
1278 wl_list_for_each(surface, &layer->surface_list, layer_link) {
1279 surface_list_add(compositor, surface);
1280 }
1281 }
1282}
1283
1284static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001285weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001286{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001287 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001288 struct weston_surface *es;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001289 struct weston_animation *animation, *next;
1290 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001291 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001292 pixman_region32_t output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001293
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001294 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001295 weston_compositor_build_surface_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001296
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001297 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001298 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001299 else
1300 wl_list_for_each(es, &ec->surface_list, link)
1301 weston_surface_move_to_plane(es, &ec->primary_plane);
1302
Pekka Paalanene67858b2013-04-25 13:57:42 +03001303 wl_list_init(&frame_callback_list);
1304 wl_list_for_each(es, &ec->surface_list, link) {
1305 if (es->output == output) {
1306 wl_list_insert_list(&frame_callback_list,
1307 &es->frame_callback_list);
1308 wl_list_init(&es->frame_callback_list);
1309 }
1310 }
1311
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001312 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001313
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001314 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001315 pixman_region32_intersect(&output_damage,
1316 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001317 pixman_region32_subtract(&output_damage,
1318 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001319
Scott Moreauccbf29d2012-02-22 14:21:41 -07001320 if (output->dirty)
1321 weston_output_update_matrix(output);
1322
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001323 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001324
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001325 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001326
Kristian Høgsbergef044142011-06-21 15:02:12 -04001327 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001328
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001329 weston_compositor_repick(ec);
1330 wl_event_loop_dispatch(ec->input_loop, 0);
1331
Jonas Ådahldb773762012-06-13 00:01:21 +02001332 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001333 wl_callback_send_done(cb->resource, msecs);
1334 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001335 }
1336
Scott Moreaud64cf212012-06-08 19:40:54 -06001337 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001338 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001339 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001340 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001341}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001342
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001343static int
1344weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001345{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001346 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001347
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001348 wl_event_loop_dispatch(compositor->input_loop, 0);
1349
1350 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001351}
1352
1353WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001354weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001355{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001356 struct weston_compositor *compositor = output->compositor;
1357 struct wl_event_loop *loop =
1358 wl_display_get_event_loop(compositor->wl_display);
1359 int fd;
1360
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001361 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001362 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001363 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001364 return;
1365 }
1366
1367 output->repaint_scheduled = 0;
1368 if (compositor->input_loop_source)
1369 return;
1370
1371 fd = wl_event_loop_get_fd(compositor->input_loop);
1372 compositor->input_loop_source =
1373 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1374 weston_compositor_read_input, compositor);
1375}
1376
1377static void
1378idle_repaint(void *data)
1379{
1380 struct weston_output *output = data;
1381
Jonas Ådahle5a12252013-04-05 23:07:11 +02001382 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001383}
1384
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001385WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001386weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1387{
1388 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001389 if (below != NULL)
1390 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001391}
1392
1393WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001394weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001395{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001396 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001397 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001398
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001399 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1400 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001401 return;
1402
Kristian Høgsbergef044142011-06-21 15:02:12 -04001403 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001404 output->repaint_needed = 1;
1405 if (output->repaint_scheduled)
1406 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001407
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001408 wl_event_loop_add_idle(loop, idle_repaint, output);
1409 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001410
1411 if (compositor->input_loop_source) {
1412 wl_event_source_remove(compositor->input_loop_source);
1413 compositor->input_loop_source = NULL;
1414 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001415}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001416
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001417WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001418weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1419{
1420 struct weston_output *output;
1421
1422 wl_list_for_each(output, &compositor->output_list, link)
1423 weston_output_schedule_repaint(output);
1424}
1425
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001426static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001427surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001428{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001429 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001430}
1431
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001432static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001433surface_attach(struct wl_client *client,
1434 struct wl_resource *resource,
1435 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1436{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001437 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001438 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001439
1440 if (buffer_resource)
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001441 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001442
Pekka Paalanende685b82012-12-04 15:58:12 +02001443 /* Attach, attach, without commit in between does not send
1444 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001445 if (surface->pending.buffer)
1446 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001447
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001448 surface->pending.sx = sx;
1449 surface->pending.sy = sy;
1450 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001451 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001452 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001453 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001454 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001455 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001456}
1457
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001458static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001459surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001460 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001461 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001462{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001463 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001464
Pekka Paalanen8e159182012-10-10 12:49:25 +03001465 pixman_region32_union_rect(&surface->pending.damage,
1466 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001467 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001468}
1469
Kristian Høgsberg33418202011-08-16 23:01:28 -04001470static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001471destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001472{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001473 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001474
1475 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001476 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001477}
1478
1479static void
1480surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001481 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001482{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001483 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001484 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001485
1486 cb = malloc(sizeof *cb);
1487 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001488 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001489 return;
1490 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001491
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001492 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1493 callback);
1494 if (cb->resource == NULL) {
1495 free(cb);
1496 wl_resource_post_no_memory(resource);
1497 return;
1498 }
1499
Jason Ekstranda85118c2013-06-27 20:17:02 -05001500 wl_resource_set_implementation(cb->resource, NULL, cb,
1501 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001502
Pekka Paalanenbc106382012-10-10 12:49:31 +03001503 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001504}
1505
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001506static void
1507surface_set_opaque_region(struct wl_client *client,
1508 struct wl_resource *resource,
1509 struct wl_resource *region_resource)
1510{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001511 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001512 struct weston_region *region;
1513
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001514 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001515 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001516 pixman_region32_copy(&surface->pending.opaque,
1517 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001518 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001519 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001520 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001521}
1522
1523static void
1524surface_set_input_region(struct wl_client *client,
1525 struct wl_resource *resource,
1526 struct wl_resource *region_resource)
1527{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001528 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001529 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001530
1531 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001532 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001533 pixman_region32_copy(&surface->pending.input,
1534 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001535 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001536 pixman_region32_fini(&surface->pending.input);
1537 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001538 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001539}
1540
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001541static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001542weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001543{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001544 struct weston_subsurface *sub;
1545
1546 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1547 parent_link_pending) {
1548 wl_list_remove(&sub->parent_link);
1549 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1550 }
1551}
1552
1553static void
1554weston_surface_commit(struct weston_surface *surface)
1555{
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001556 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001557 int surface_width = 0;
1558 int surface_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001559
Alexander Larsson4ea95522013-05-22 14:41:37 +02001560 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001561 surface->buffer_transform = surface->pending.buffer_transform;
1562
Alexander Larsson4ea95522013-05-22 14:41:37 +02001563 /* wl_surface.set_buffer_scale */
1564 surface->buffer_scale = surface->pending.buffer_scale;
1565
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001566 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001567 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001568 weston_surface_attach(surface, surface->pending.buffer);
1569
Giulio Camuffo184df502013-02-21 11:29:21 +01001570 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001571 surface_width = weston_surface_buffer_width(surface);
1572 surface_height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001573 }
1574
1575 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001576 surface->configure(surface,
1577 surface->pending.sx, surface->pending.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001578 surface_width, surface_height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001579
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001580 if (surface->pending.buffer)
1581 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1582 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001583 surface->pending.sx = 0;
1584 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001585 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001586
1587 /* wl_surface.damage */
1588 pixman_region32_union(&surface->damage, &surface->damage,
1589 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001590 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1591 0, 0,
1592 surface->geometry.width,
1593 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001594 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001595
1596 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001597 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001598 surface->geometry.width,
1599 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001600 pixman_region32_intersect(&opaque,
1601 &opaque, &surface->pending.opaque);
1602
1603 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1604 pixman_region32_copy(&surface->opaque, &opaque);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001605 weston_surface_geometry_dirty(surface);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001606 }
1607
1608 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001609
1610 /* wl_surface.set_input_region */
1611 pixman_region32_fini(&surface->input);
1612 pixman_region32_init_rect(&surface->input, 0, 0,
1613 surface->geometry.width,
1614 surface->geometry.height);
1615 pixman_region32_intersect(&surface->input,
1616 &surface->input, &surface->pending.input);
1617
Pekka Paalanenbc106382012-10-10 12:49:31 +03001618 /* wl_surface.frame */
1619 wl_list_insert_list(&surface->frame_callback_list,
1620 &surface->pending.frame_callback_list);
1621 wl_list_init(&surface->pending.frame_callback_list);
1622
Pekka Paalanene67858b2013-04-25 13:57:42 +03001623 weston_surface_commit_subsurface_order(surface);
1624
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001625 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001626}
1627
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001628static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001629weston_subsurface_commit(struct weston_subsurface *sub);
1630
1631static void
1632weston_subsurface_parent_commit(struct weston_subsurface *sub,
1633 int parent_is_synchronized);
1634
1635static void
1636surface_commit(struct wl_client *client, struct wl_resource *resource)
1637{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001638 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001639 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1640
1641 if (sub) {
1642 weston_subsurface_commit(sub);
1643 return;
1644 }
1645
1646 weston_surface_commit(surface);
1647
1648 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1649 if (sub->surface != surface)
1650 weston_subsurface_parent_commit(sub, 0);
1651 }
1652}
1653
1654static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001655surface_set_buffer_transform(struct wl_client *client,
1656 struct wl_resource *resource, int transform)
1657{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001658 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001659
1660 surface->pending.buffer_transform = transform;
1661}
1662
Alexander Larsson4ea95522013-05-22 14:41:37 +02001663static void
1664surface_set_buffer_scale(struct wl_client *client,
1665 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001666 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02001667{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001668 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02001669
1670 surface->pending.buffer_scale = scale;
1671}
1672
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001673static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001674 surface_destroy,
1675 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001676 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001677 surface_frame,
1678 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001679 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001680 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001681 surface_set_buffer_transform,
1682 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001683};
1684
1685static void
1686compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001687 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001688{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04001689 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001690 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001691
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001692 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001693 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001694 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001695 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001696 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001697
Jason Ekstranda85118c2013-06-27 20:17:02 -05001698 surface->resource =
1699 wl_resource_create(client, &wl_surface_interface,
1700 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001701 if (surface->resource == NULL) {
1702 weston_surface_destroy(surface);
1703 wl_resource_post_no_memory(resource);
1704 return;
1705 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05001706 wl_resource_set_implementation(surface->resource, &surface_interface,
1707 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001708}
1709
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001710static void
1711destroy_region(struct wl_resource *resource)
1712{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001713 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001714
1715 pixman_region32_fini(&region->region);
1716 free(region);
1717}
1718
1719static void
1720region_destroy(struct wl_client *client, struct wl_resource *resource)
1721{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001722 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001723}
1724
1725static void
1726region_add(struct wl_client *client, struct wl_resource *resource,
1727 int32_t x, int32_t y, int32_t width, int32_t height)
1728{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001729 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001730
1731 pixman_region32_union_rect(&region->region, &region->region,
1732 x, y, width, height);
1733}
1734
1735static void
1736region_subtract(struct wl_client *client, struct wl_resource *resource,
1737 int32_t x, int32_t y, int32_t width, int32_t height)
1738{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001739 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001740 pixman_region32_t rect;
1741
1742 pixman_region32_init_rect(&rect, x, y, width, height);
1743 pixman_region32_subtract(&region->region, &region->region, &rect);
1744 pixman_region32_fini(&rect);
1745}
1746
1747static const struct wl_region_interface region_interface = {
1748 region_destroy,
1749 region_add,
1750 region_subtract
1751};
1752
1753static void
1754compositor_create_region(struct wl_client *client,
1755 struct wl_resource *resource, uint32_t id)
1756{
1757 struct weston_region *region;
1758
1759 region = malloc(sizeof *region);
1760 if (region == NULL) {
1761 wl_resource_post_no_memory(resource);
1762 return;
1763 }
1764
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001765 pixman_region32_init(&region->region);
1766
Jason Ekstranda85118c2013-06-27 20:17:02 -05001767 region->resource =
1768 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001769 if (region->resource == NULL) {
1770 free(region);
1771 wl_resource_post_no_memory(resource);
1772 return;
1773 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05001774 wl_resource_set_implementation(region->resource, &region_interface,
1775 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001776}
1777
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001778static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001779 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001780 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001781};
1782
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001783static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001784weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
1785{
1786 struct weston_surface *surface = sub->surface;
1787 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001788 int surface_width = 0;
1789 int surface_height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001790
Alexander Larsson4ea95522013-05-22 14:41:37 +02001791 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001792 surface->buffer_transform = sub->cached.buffer_transform;
1793
Alexander Larsson4ea95522013-05-22 14:41:37 +02001794 /* wl_surface.set_buffer_scale */
1795 surface->buffer_scale = sub->cached.buffer_scale;
1796
Pekka Paalanene67858b2013-04-25 13:57:42 +03001797 /* wl_surface.attach */
1798 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
1799 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
1800 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
1801
1802 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001803 surface_width = weston_surface_buffer_width(surface);
1804 surface_height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001805 }
1806
1807 if (surface->configure && sub->cached.newly_attached)
1808 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001809 surface_width, surface_height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001810 sub->cached.sx = 0;
1811 sub->cached.sy = 0;
1812 sub->cached.newly_attached = 0;
1813
1814 /* wl_surface.damage */
1815 pixman_region32_union(&surface->damage, &surface->damage,
1816 &sub->cached.damage);
1817 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1818 0, 0,
1819 surface->geometry.width,
1820 surface->geometry.height);
1821 empty_region(&sub->cached.damage);
1822
1823 /* wl_surface.set_opaque_region */
1824 pixman_region32_init_rect(&opaque, 0, 0,
1825 surface->geometry.width,
1826 surface->geometry.height);
1827 pixman_region32_intersect(&opaque,
1828 &opaque, &sub->cached.opaque);
1829
1830 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1831 pixman_region32_copy(&surface->opaque, &opaque);
1832 weston_surface_geometry_dirty(surface);
1833 }
1834
1835 pixman_region32_fini(&opaque);
1836
1837 /* wl_surface.set_input_region */
1838 pixman_region32_fini(&surface->input);
1839 pixman_region32_init_rect(&surface->input, 0, 0,
1840 surface->geometry.width,
1841 surface->geometry.height);
1842 pixman_region32_intersect(&surface->input,
1843 &surface->input, &sub->cached.input);
1844
1845 /* wl_surface.frame */
1846 wl_list_insert_list(&surface->frame_callback_list,
1847 &sub->cached.frame_callback_list);
1848 wl_list_init(&sub->cached.frame_callback_list);
1849
1850 weston_surface_commit_subsurface_order(surface);
1851
1852 weston_surface_schedule_repaint(surface);
1853
1854 sub->cached.has_data = 0;
1855}
1856
1857static void
1858weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
1859{
1860 struct weston_surface *surface = sub->surface;
1861
1862 /*
1863 * If this commit would cause the surface to move by the
1864 * attach(dx, dy) parameters, the old damage region must be
1865 * translated to correspond to the new surface coordinate system
1866 * origin.
1867 */
1868 pixman_region32_translate(&sub->cached.damage,
1869 -surface->pending.sx, -surface->pending.sy);
1870 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
1871 &surface->pending.damage);
1872 empty_region(&surface->pending.damage);
1873
1874 if (surface->pending.newly_attached) {
1875 sub->cached.newly_attached = 1;
1876 weston_buffer_reference(&sub->cached.buffer_ref,
1877 surface->pending.buffer);
1878 }
1879 sub->cached.sx += surface->pending.sx;
1880 sub->cached.sy += surface->pending.sy;
1881 surface->pending.sx = 0;
1882 surface->pending.sy = 0;
1883 surface->pending.newly_attached = 0;
1884
1885 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001886 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001887
1888 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
1889
1890 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
1891
1892 wl_list_insert_list(&sub->cached.frame_callback_list,
1893 &surface->pending.frame_callback_list);
1894 wl_list_init(&surface->pending.frame_callback_list);
1895
1896 sub->cached.has_data = 1;
1897}
1898
1899static int
1900weston_subsurface_is_synchronized(struct weston_subsurface *sub)
1901{
1902 while (sub) {
1903 if (sub->synchronized)
1904 return 1;
1905
1906 if (!sub->parent)
1907 return 0;
1908
1909 sub = weston_surface_to_subsurface(sub->parent);
1910 }
1911
1912 return 0;
1913}
1914
1915static void
1916weston_subsurface_commit(struct weston_subsurface *sub)
1917{
1918 struct weston_surface *surface = sub->surface;
1919 struct weston_subsurface *tmp;
1920
1921 /* Recursive check for effectively synchronized. */
1922 if (weston_subsurface_is_synchronized(sub)) {
1923 weston_subsurface_commit_to_cache(sub);
1924 } else {
1925 if (sub->cached.has_data) {
1926 /* flush accumulated state from cache */
1927 weston_subsurface_commit_to_cache(sub);
1928 weston_subsurface_commit_from_cache(sub);
1929 } else {
1930 weston_surface_commit(surface);
1931 }
1932
1933 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1934 if (tmp->surface != surface)
1935 weston_subsurface_parent_commit(tmp, 0);
1936 }
1937 }
1938}
1939
1940static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001941weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001942{
1943 struct weston_surface *surface = sub->surface;
1944 struct weston_subsurface *tmp;
1945
Pekka Paalanene67858b2013-04-25 13:57:42 +03001946 /* From now on, commit_from_cache the whole sub-tree, regardless of
1947 * the synchronized mode of each child. This sub-surface or some
1948 * of its ancestors were synchronized, so we are synchronized
1949 * all the way down.
1950 */
1951
1952 if (sub->cached.has_data)
1953 weston_subsurface_commit_from_cache(sub);
1954
1955 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1956 if (tmp->surface != surface)
1957 weston_subsurface_parent_commit(tmp, 1);
1958 }
1959}
1960
1961static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001962weston_subsurface_parent_commit(struct weston_subsurface *sub,
1963 int parent_is_synchronized)
1964{
1965 if (sub->position.set) {
1966 weston_surface_set_position(sub->surface,
1967 sub->position.x, sub->position.y);
1968 sub->position.set = 0;
1969 }
1970
1971 if (parent_is_synchronized || sub->synchronized)
1972 weston_subsurface_synchronized_commit(sub);
1973}
1974
1975static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001976subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
1977 int32_t width, int32_t height)
1978{
1979 struct weston_compositor *compositor = surface->compositor;
1980
1981 weston_surface_configure(surface,
1982 surface->geometry.x + dx,
1983 surface->geometry.y + dy,
1984 width, height);
1985
1986 /* No need to check parent mappedness, because if parent is not
1987 * mapped, parent is not in a visible layer, so this sub-surface
1988 * will not be drawn either.
1989 */
1990 if (!weston_surface_is_mapped(surface)) {
1991 wl_list_init(&surface->layer_link);
1992
1993 /* Cannot call weston_surface_update_transform(),
1994 * because that would call it also for the parent surface,
1995 * which might not be mapped yet. That would lead to
1996 * inconsistent state, where the window could never be
1997 * mapped.
1998 *
1999 * Instead just assing any output, to make
2000 * weston_surface_is_mapped() return true, so that when the
2001 * parent surface does get mapped, this one will get
2002 * included, too. See surface_list_add().
2003 */
2004 assert(!wl_list_empty(&compositor->output_list));
2005 surface->output = container_of(compositor->output_list.next,
2006 struct weston_output, link);
2007 }
2008}
2009
2010static struct weston_subsurface *
2011weston_surface_to_subsurface(struct weston_surface *surface)
2012{
2013 if (surface->configure == subsurface_configure)
2014 return surface->configure_private;
2015
2016 return NULL;
2017}
2018
Pekka Paalanen01388e22013-04-25 13:57:44 +03002019WL_EXPORT struct weston_surface *
2020weston_surface_get_main_surface(struct weston_surface *surface)
2021{
2022 struct weston_subsurface *sub;
2023
2024 while (surface && (sub = weston_surface_to_subsurface(surface)))
2025 surface = sub->parent;
2026
2027 return surface;
2028}
2029
Pekka Paalanene67858b2013-04-25 13:57:42 +03002030static void
2031subsurface_set_position(struct wl_client *client,
2032 struct wl_resource *resource, int32_t x, int32_t y)
2033{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002034 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002035
2036 if (!sub)
2037 return;
2038
2039 sub->position.x = x;
2040 sub->position.y = y;
2041 sub->position.set = 1;
2042}
2043
2044static struct weston_subsurface *
2045subsurface_from_surface(struct weston_surface *surface)
2046{
2047 struct weston_subsurface *sub;
2048
2049 sub = weston_surface_to_subsurface(surface);
2050 if (sub)
2051 return sub;
2052
2053 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2054 if (sub->surface == surface)
2055 return sub;
2056
2057 return NULL;
2058}
2059
2060static struct weston_subsurface *
2061subsurface_sibling_check(struct weston_subsurface *sub,
2062 struct weston_surface *surface,
2063 const char *request)
2064{
2065 struct weston_subsurface *sibling;
2066
2067 sibling = subsurface_from_surface(surface);
2068
2069 if (!sibling) {
2070 wl_resource_post_error(sub->resource,
2071 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2072 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002073 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002074 return NULL;
2075 }
2076
2077 if (sibling->parent != sub->parent) {
2078 wl_resource_post_error(sub->resource,
2079 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2080 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002081 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002082 return NULL;
2083 }
2084
2085 return sibling;
2086}
2087
2088static void
2089subsurface_place_above(struct wl_client *client,
2090 struct wl_resource *resource,
2091 struct wl_resource *sibling_resource)
2092{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002093 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002094 struct weston_surface *surface =
2095 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002096 struct weston_subsurface *sibling;
2097
2098 if (!sub)
2099 return;
2100
2101 sibling = subsurface_sibling_check(sub, surface, "place_above");
2102 if (!sibling)
2103 return;
2104
2105 wl_list_remove(&sub->parent_link_pending);
2106 wl_list_insert(sibling->parent_link_pending.prev,
2107 &sub->parent_link_pending);
2108}
2109
2110static void
2111subsurface_place_below(struct wl_client *client,
2112 struct wl_resource *resource,
2113 struct wl_resource *sibling_resource)
2114{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002115 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002116 struct weston_surface *surface =
2117 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002118 struct weston_subsurface *sibling;
2119
2120 if (!sub)
2121 return;
2122
2123 sibling = subsurface_sibling_check(sub, surface, "place_below");
2124 if (!sibling)
2125 return;
2126
2127 wl_list_remove(&sub->parent_link_pending);
2128 wl_list_insert(&sibling->parent_link_pending,
2129 &sub->parent_link_pending);
2130}
2131
2132static void
2133subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2134{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002135 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002136
2137 if (sub)
2138 sub->synchronized = 1;
2139}
2140
2141static void
2142subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2143{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002144 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002145
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002146 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002147 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002148
2149 /* If sub became effectively desynchronized, flush. */
2150 if (!weston_subsurface_is_synchronized(sub))
2151 weston_subsurface_synchronized_commit(sub);
2152 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002153}
2154
2155static void
2156weston_subsurface_cache_init(struct weston_subsurface *sub)
2157{
2158 pixman_region32_init(&sub->cached.damage);
2159 pixman_region32_init(&sub->cached.opaque);
2160 pixman_region32_init(&sub->cached.input);
2161 wl_list_init(&sub->cached.frame_callback_list);
2162 sub->cached.buffer_ref.buffer = NULL;
2163}
2164
2165static void
2166weston_subsurface_cache_fini(struct weston_subsurface *sub)
2167{
2168 struct weston_frame_callback *cb, *tmp;
2169
2170 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002171 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002172
2173 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2174 pixman_region32_fini(&sub->cached.damage);
2175 pixman_region32_fini(&sub->cached.opaque);
2176 pixman_region32_fini(&sub->cached.input);
2177}
2178
2179static void
2180weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2181{
2182 wl_list_remove(&sub->parent_link);
2183 wl_list_remove(&sub->parent_link_pending);
2184 wl_list_remove(&sub->parent_destroy_listener.link);
2185 sub->parent = NULL;
2186}
2187
2188static void
2189weston_subsurface_destroy(struct weston_subsurface *sub);
2190
2191static void
2192subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2193{
2194 struct weston_subsurface *sub =
2195 container_of(listener, struct weston_subsurface,
2196 surface_destroy_listener);
2197 assert(data == &sub->surface->resource);
2198
2199 /* The protocol object (wl_resource) is left inert. */
2200 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002201 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002202
2203 weston_subsurface_destroy(sub);
2204}
2205
2206static void
2207subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2208{
2209 struct weston_subsurface *sub =
2210 container_of(listener, struct weston_subsurface,
2211 parent_destroy_listener);
2212 assert(data == &sub->parent->resource);
2213 assert(sub->surface != sub->parent);
2214
2215 if (weston_surface_is_mapped(sub->surface))
2216 weston_surface_unmap(sub->surface);
2217
2218 weston_subsurface_unlink_parent(sub);
2219}
2220
2221static void
2222subsurface_resource_destroy(struct wl_resource *resource)
2223{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002224 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002225
2226 if (sub)
2227 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002228}
2229
2230static void
2231subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2232{
2233 wl_resource_destroy(resource);
2234}
2235
2236static void
2237weston_subsurface_link_parent(struct weston_subsurface *sub,
2238 struct weston_surface *parent)
2239{
2240 sub->parent = parent;
2241 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002242 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002243 &sub->parent_destroy_listener);
2244
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
2250static void
2251weston_subsurface_link_surface(struct weston_subsurface *sub,
2252 struct weston_surface *surface)
2253{
2254 sub->surface = surface;
2255 sub->surface_destroy_listener.notify =
2256 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002257 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002258 &sub->surface_destroy_listener);
2259}
2260
2261static void
2262weston_subsurface_destroy(struct weston_subsurface *sub)
2263{
2264 assert(sub->surface);
2265
2266 if (sub->resource) {
2267 assert(weston_surface_to_subsurface(sub->surface) == sub);
2268 assert(sub->parent_destroy_listener.notify ==
2269 subsurface_handle_parent_destroy);
2270
2271 weston_surface_set_transform_parent(sub->surface, NULL);
2272 if (sub->parent)
2273 weston_subsurface_unlink_parent(sub);
2274
2275 weston_subsurface_cache_fini(sub);
2276
2277 sub->surface->configure = NULL;
2278 sub->surface->configure_private = NULL;
2279 } else {
2280 /* the dummy weston_subsurface for the parent itself */
2281 assert(sub->parent_destroy_listener.notify == NULL);
2282 wl_list_remove(&sub->parent_link);
2283 wl_list_remove(&sub->parent_link_pending);
2284 }
2285
2286 wl_list_remove(&sub->surface_destroy_listener.link);
2287 free(sub);
2288}
2289
2290static const struct wl_subsurface_interface subsurface_implementation = {
2291 subsurface_destroy,
2292 subsurface_set_position,
2293 subsurface_place_above,
2294 subsurface_place_below,
2295 subsurface_set_sync,
2296 subsurface_set_desync
2297};
2298
2299static struct weston_subsurface *
2300weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2301 struct weston_surface *parent)
2302{
2303 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002304 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002305
2306 sub = calloc(1, sizeof *sub);
2307 if (!sub)
2308 return NULL;
2309
Jason Ekstranda85118c2013-06-27 20:17:02 -05002310 sub->resource =
2311 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002312 if (!sub->resource) {
2313 free(sub);
2314 return NULL;
2315 }
2316
Jason Ekstranda85118c2013-06-27 20:17:02 -05002317 wl_resource_set_implementation(sub->resource,
2318 &subsurface_implementation,
2319 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002320 weston_subsurface_link_surface(sub, surface);
2321 weston_subsurface_link_parent(sub, parent);
2322 weston_subsurface_cache_init(sub);
2323 sub->synchronized = 1;
2324 weston_surface_set_transform_parent(surface, parent);
2325
2326 return sub;
2327}
2328
2329/* Create a dummy subsurface for having the parent itself in its
2330 * sub-surface lists. Makes stacking order manipulation easy.
2331 */
2332static struct weston_subsurface *
2333weston_subsurface_create_for_parent(struct weston_surface *parent)
2334{
2335 struct weston_subsurface *sub;
2336
2337 sub = calloc(1, sizeof *sub);
2338 if (!sub)
2339 return NULL;
2340
2341 weston_subsurface_link_surface(sub, parent);
2342 sub->parent = parent;
2343 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2344 wl_list_insert(&parent->subsurface_list_pending,
2345 &sub->parent_link_pending);
2346
2347 return sub;
2348}
2349
2350static void
2351subcompositor_get_subsurface(struct wl_client *client,
2352 struct wl_resource *resource,
2353 uint32_t id,
2354 struct wl_resource *surface_resource,
2355 struct wl_resource *parent_resource)
2356{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002357 struct weston_surface *surface =
2358 wl_resource_get_user_data(surface_resource);
2359 struct weston_surface *parent =
2360 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002361 struct weston_subsurface *sub;
2362 static const char where[] = "get_subsurface: wl_subsurface@";
2363
2364 if (surface == parent) {
2365 wl_resource_post_error(resource,
2366 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2367 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002368 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002369 return;
2370 }
2371
2372 if (weston_surface_to_subsurface(surface)) {
2373 wl_resource_post_error(resource,
2374 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2375 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002376 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002377 return;
2378 }
2379
2380 if (surface->configure) {
2381 wl_resource_post_error(resource,
2382 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2383 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002384 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002385 return;
2386 }
2387
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002388 if (weston_surface_get_main_surface(parent) == surface) {
2389 wl_resource_post_error(resource,
2390 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2391 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002392 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002393 return;
2394 }
2395
Pekka Paalanene67858b2013-04-25 13:57:42 +03002396 /* make sure the parent is in its own list */
2397 if (wl_list_empty(&parent->subsurface_list)) {
2398 if (!weston_subsurface_create_for_parent(parent)) {
2399 wl_resource_post_no_memory(resource);
2400 return;
2401 }
2402 }
2403
2404 sub = weston_subsurface_create(id, surface, parent);
2405 if (!sub) {
2406 wl_resource_post_no_memory(resource);
2407 return;
2408 }
2409
2410 surface->configure = subsurface_configure;
2411 surface->configure_private = sub;
2412}
2413
2414static void
2415subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2416{
2417 wl_resource_destroy(resource);
2418}
2419
2420static const struct wl_subcompositor_interface subcompositor_interface = {
2421 subcompositor_destroy,
2422 subcompositor_get_subsurface
2423};
2424
2425static void
2426bind_subcompositor(struct wl_client *client,
2427 void *data, uint32_t version, uint32_t id)
2428{
2429 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002430 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002431
Jason Ekstranda85118c2013-06-27 20:17:02 -05002432 resource =
2433 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002434 if (resource == NULL) {
2435 wl_client_post_no_memory(client);
2436 return;
2437 }
2438 wl_resource_set_implementation(resource, &subcompositor_interface,
2439 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002440}
2441
2442static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002443weston_compositor_dpms(struct weston_compositor *compositor,
2444 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002445{
2446 struct weston_output *output;
2447
2448 wl_list_for_each(output, &compositor->output_list, link)
2449 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002450 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002451}
2452
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002453WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002454weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002455{
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002456 switch (compositor->state) {
2457 case WESTON_COMPOSITOR_SLEEPING:
2458 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2459 /* fall through */
2460 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002461 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002462 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002463 /* fall through */
2464 default:
2465 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2466 wl_event_source_timer_update(compositor->idle_source,
2467 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002468 }
2469}
2470
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002471WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002472weston_compositor_offscreen(struct weston_compositor *compositor)
2473{
2474 switch (compositor->state) {
2475 case WESTON_COMPOSITOR_OFFSCREEN:
2476 return;
2477 case WESTON_COMPOSITOR_SLEEPING:
2478 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2479 /* fall through */
2480 default:
2481 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2482 wl_event_source_timer_update(compositor->idle_source, 0);
2483 }
2484}
2485
2486WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002487weston_compositor_sleep(struct weston_compositor *compositor)
2488{
2489 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2490 return;
2491
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002492 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002493 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2494 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2495}
2496
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002497static int
2498idle_handler(void *data)
2499{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002500 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002501
2502 if (compositor->idle_inhibit)
2503 return 1;
2504
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002505 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002506 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002507
2508 return 1;
2509}
2510
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002511WL_EXPORT void
2512weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2513{
2514 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002515 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002516 plane->x = x;
2517 plane->y = y;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002518
2519 /* Init the link so that the call to wl_list_remove() when releasing
2520 * the plane without ever stacking doesn't lead to a crash */
2521 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002522}
2523
2524WL_EXPORT void
2525weston_plane_release(struct weston_plane *plane)
2526{
2527 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002528 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002529
2530 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002531}
2532
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002533WL_EXPORT void
2534weston_compositor_stack_plane(struct weston_compositor *ec,
2535 struct weston_plane *plane,
2536 struct weston_plane *above)
2537{
2538 if (above)
2539 wl_list_insert(above->link.prev, &plane->link);
2540 else
2541 wl_list_insert(&ec->plane_list, &plane->link);
2542}
2543
Casey Dahlin9074db52012-04-19 22:50:09 -04002544static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002545{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002546 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002547}
2548
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002549static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002550bind_output(struct wl_client *client,
2551 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002552{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002553 struct weston_output *output = data;
2554 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002555 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002556
Jason Ekstranda85118c2013-06-27 20:17:02 -05002557 resource = wl_resource_create(client, &wl_output_interface,
2558 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002559 if (resource == NULL) {
2560 wl_client_post_no_memory(client);
2561 return;
2562 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002563
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002564 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002565 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002566
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002567 wl_output_send_geometry(resource,
2568 output->x,
2569 output->y,
2570 output->mm_width,
2571 output->mm_height,
2572 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002573 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002574 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002575 if (version >= 2)
2576 wl_output_send_scale(resource,
2577 output->scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002578
2579 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002580 wl_output_send_mode(resource,
2581 mode->flags,
2582 mode->width,
2583 mode->height,
2584 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002585 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002586
2587 if (version >= 2)
2588 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002589}
2590
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002591WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002592weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002593{
Richard Hughes64ddde12013-05-01 21:52:10 +01002594 wl_signal_emit(&output->destroy_signal, output);
2595
Richard Hughesafe690c2013-05-02 10:10:04 +01002596 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002597 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002598 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002599 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002600
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002601 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002602}
2603
Scott Moreau1bad5db2012-08-18 01:04:05 -06002604static void
2605weston_output_compute_transform(struct weston_output *output)
2606{
2607 struct weston_matrix transform;
2608 int flip;
2609
2610 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002611 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002612
2613 switch(output->transform) {
2614 case WL_OUTPUT_TRANSFORM_FLIPPED:
2615 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2616 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2617 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002618 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002619 flip = -1;
2620 break;
2621 default:
2622 flip = 1;
2623 break;
2624 }
2625
2626 switch(output->transform) {
2627 case WL_OUTPUT_TRANSFORM_NORMAL:
2628 case WL_OUTPUT_TRANSFORM_FLIPPED:
2629 transform.d[0] = flip;
2630 transform.d[1] = 0;
2631 transform.d[4] = 0;
2632 transform.d[5] = 1;
2633 break;
2634 case WL_OUTPUT_TRANSFORM_90:
2635 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2636 transform.d[0] = 0;
2637 transform.d[1] = -flip;
2638 transform.d[4] = 1;
2639 transform.d[5] = 0;
2640 break;
2641 case WL_OUTPUT_TRANSFORM_180:
2642 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2643 transform.d[0] = -flip;
2644 transform.d[1] = 0;
2645 transform.d[4] = 0;
2646 transform.d[5] = -1;
2647 break;
2648 case WL_OUTPUT_TRANSFORM_270:
2649 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2650 transform.d[0] = 0;
2651 transform.d[1] = flip;
2652 transform.d[4] = -1;
2653 transform.d[5] = 0;
2654 break;
2655 default:
2656 break;
2657 }
2658
2659 weston_matrix_multiply(&output->matrix, &transform);
2660}
2661
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002662WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002663weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002664{
Scott Moreau850ca422012-05-21 15:21:25 -06002665 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002666 struct weston_matrix camera;
2667 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002668
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002669 weston_matrix_init(&output->matrix);
2670 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002671 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2672 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002673
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002674 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002675 2.0 / (output->width + output->border.left + output->border.right),
2676 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2677
2678 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002679
Scott Moreauccbf29d2012-02-22 14:21:41 -07002680 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002681 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002682 weston_matrix_init(&camera);
2683 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002684 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002685 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002686 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002687 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002688 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002689 weston_matrix_multiply(&output->matrix, &modelview);
2690 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002691
Scott Moreauccbf29d2012-02-22 14:21:41 -07002692 output->dirty = 0;
2693}
2694
Scott Moreau1bad5db2012-08-18 01:04:05 -06002695static void
Alexander Larsson0b135062013-05-28 16:23:36 +02002696weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002697{
2698 output->transform = transform;
2699
2700 switch (transform) {
2701 case WL_OUTPUT_TRANSFORM_90:
2702 case WL_OUTPUT_TRANSFORM_270:
2703 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2704 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2705 /* Swap width and height */
2706 output->width = output->current->height;
2707 output->height = output->current->width;
2708 break;
2709 case WL_OUTPUT_TRANSFORM_NORMAL:
2710 case WL_OUTPUT_TRANSFORM_180:
2711 case WL_OUTPUT_TRANSFORM_FLIPPED:
2712 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2713 output->width = output->current->width;
2714 output->height = output->current->height;
2715 break;
2716 default:
2717 break;
2718 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06002719
Alexander Larsson4ea95522013-05-22 14:41:37 +02002720 output->scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02002721 output->width /= scale;
2722 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002723}
2724
Scott Moreauccbf29d2012-02-22 14:21:41 -07002725WL_EXPORT void
2726weston_output_move(struct weston_output *output, int x, int y)
2727{
2728 output->x = x;
2729 output->y = y;
2730
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002731 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002732 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002733 output->width,
2734 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002735}
2736
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002737WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002738weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02002739 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002740 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002741{
2742 output->compositor = c;
2743 output->x = x;
2744 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002745 output->border.top = 0;
2746 output->border.bottom = 0;
2747 output->border.left = 0;
2748 output->border.right = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02002749 output->mm_width = mm_width;
2750 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002751 output->dirty = 1;
Alexander Larssone32c3762013-05-28 16:23:37 +02002752 output->origin_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002753
Alexander Larsson0b135062013-05-28 16:23:36 +02002754 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06002755 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002756
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002757 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002758 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002759
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002760 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01002761 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002762 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002763 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002764
Casey Dahlin58ba1372012-04-19 22:50:08 -04002765 output->id = ffs(~output->compositor->output_id_pool) - 1;
2766 output->compositor->output_id_pool |= 1 << output->id;
2767
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002768 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002769 wl_global_create(c->wl_display, &wl_output_interface, 2,
2770 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01002771 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002772}
2773
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002774WL_EXPORT void
2775weston_output_transform_coordinate(struct weston_output *output,
2776 int device_x, int device_y,
2777 wl_fixed_t *x, wl_fixed_t *y)
2778{
2779 wl_fixed_t tx, ty;
2780 wl_fixed_t width, height;
2781
2782 width = wl_fixed_from_int(output->width * output->scale - 1);
2783 height = wl_fixed_from_int(output->height * output->scale - 1);
2784
2785 switch(output->transform) {
2786 case WL_OUTPUT_TRANSFORM_NORMAL:
2787 default:
2788 tx = wl_fixed_from_int(device_x);
2789 ty = wl_fixed_from_int(device_y);
2790 break;
2791 case WL_OUTPUT_TRANSFORM_90:
2792 tx = wl_fixed_from_int(device_y);
2793 ty = height - wl_fixed_from_int(device_x);
2794 break;
2795 case WL_OUTPUT_TRANSFORM_180:
2796 tx = width - wl_fixed_from_int(device_x);
2797 ty = height - wl_fixed_from_int(device_y);
2798 break;
2799 case WL_OUTPUT_TRANSFORM_270:
2800 tx = width - wl_fixed_from_int(device_y);
2801 ty = wl_fixed_from_int(device_x);
2802 break;
2803 case WL_OUTPUT_TRANSFORM_FLIPPED:
2804 tx = width - wl_fixed_from_int(device_x);
2805 ty = wl_fixed_from_int(device_y);
2806 break;
2807 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2808 tx = width - wl_fixed_from_int(device_y);
2809 ty = height - wl_fixed_from_int(device_x);
2810 break;
2811 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2812 tx = wl_fixed_from_int(device_x);
2813 ty = height - wl_fixed_from_int(device_y);
2814 break;
2815 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2816 tx = wl_fixed_from_int(device_y);
2817 ty = wl_fixed_from_int(device_x);
2818 break;
2819 }
2820
2821 *x = tx / output->scale + wl_fixed_from_int(output->x);
2822 *y = ty / output->scale + wl_fixed_from_int(output->y);
2823}
2824
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002825static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002826compositor_bind(struct wl_client *client,
2827 void *data, uint32_t version, uint32_t id)
2828{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002829 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002830 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002831
Jason Ekstranda85118c2013-06-27 20:17:02 -05002832 resource = wl_resource_create(client, &wl_compositor_interface,
2833 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002834 if (resource == NULL) {
2835 wl_client_post_no_memory(client);
2836 return;
2837 }
2838
2839 wl_resource_set_implementation(resource, &compositor_interface,
2840 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05002841}
2842
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002843static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002844log_uname(void)
2845{
2846 struct utsname usys;
2847
2848 uname(&usys);
2849
2850 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2851 usys.version, usys.machine);
2852}
2853
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002854WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02002855weston_environment_get_fd(const char *env)
2856{
2857 char *e, *end;
2858 int fd, flags;
2859
2860 e = getenv(env);
2861 if (!e)
2862 return -1;
2863 fd = strtol(e, &end, 0);
2864 if (*end != '\0')
2865 return -1;
2866
2867 flags = fcntl(fd, F_GETFD);
2868 if (flags == -1)
2869 return -1;
2870
2871 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
2872 unsetenv(env);
2873
2874 return fd;
2875}
2876
2877WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002878weston_compositor_init(struct weston_compositor *ec,
2879 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002880 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002881 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002882{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002883 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002884 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002885 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002886
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002887 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002888 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002889 wl_signal_init(&ec->destroy_signal);
2890 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002891 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002892 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002893 wl_signal_init(&ec->idle_signal);
2894 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002895 wl_signal_init(&ec->show_input_panel_signal);
2896 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02002897 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002898 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01002899 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002900
Casey Dahlin58ba1372012-04-19 22:50:08 -04002901 ec->output_id_pool = 0;
2902
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002903 if (!wl_global_create(display, &wl_compositor_interface, 3,
2904 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05002905 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002906
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002907 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
2908 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03002909 return -1;
2910
Daniel Stone725c2c32012-06-22 14:04:36 +01002911 wl_list_init(&ec->surface_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002912 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002913 wl_list_init(&ec->layer_list);
2914 wl_list_init(&ec->seat_list);
2915 wl_list_init(&ec->output_list);
2916 wl_list_init(&ec->key_binding_list);
2917 wl_list_init(&ec->button_binding_list);
2918 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002919 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002920
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002921 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002922 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002923
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002924 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
2925 weston_config_section_get_string(s, "keymap_rules",
2926 (char **) &xkb_names.rules, NULL);
2927 weston_config_section_get_string(s, "keymap_model",
2928 (char **) &xkb_names.model, NULL);
2929 weston_config_section_get_string(s, "keymap_layout",
2930 (char **) &xkb_names.layout, NULL);
2931 weston_config_section_get_string(s, "keymap_variant",
2932 (char **) &xkb_names.variant, NULL);
2933 weston_config_section_get_string(s, "keymap_options",
2934 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01002935
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002936 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
2937 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01002938
2939 ec->ping_handler = NULL;
2940
2941 screenshooter_create(ec);
2942 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002943 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002944
2945 wl_data_device_manager_init(ec->wl_display);
2946
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002947 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002948
Daniel Stone725c2c32012-06-22 14:04:36 +01002949 loop = wl_display_get_event_loop(ec->wl_display);
2950 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2951 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2952
2953 ec->input_loop = wl_event_loop_create();
2954
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002955 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2956 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2957
2958 weston_compositor_schedule_repaint(ec);
2959
Daniel Stone725c2c32012-06-22 14:04:36 +01002960 return 0;
2961}
2962
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002963WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002964weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002965{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002966 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002967
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002968 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002969 if (ec->input_loop_source)
2970 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002971
Matt Roper361d2ad2011-08-29 13:52:23 -07002972 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002973 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002974 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002975
Daniel Stone325fc2d2012-05-30 16:31:58 +01002976 weston_binding_list_destroy_all(&ec->key_binding_list);
2977 weston_binding_list_destroy_all(&ec->button_binding_list);
2978 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002979 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002980
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002981 weston_plane_release(&ec->primary_plane);
2982
Jonas Ådahlc97af922012-03-28 22:36:09 +02002983 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07002984
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002985 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07002986}
2987
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05002988WL_EXPORT void
2989weston_version(int *major, int *minor, int *micro)
2990{
2991 *major = WESTON_VERSION_MAJOR;
2992 *minor = WESTON_VERSION_MINOR;
2993 *micro = WESTON_VERSION_MICRO;
2994}
2995
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002996static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002997 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002998 const char *desc;
2999} capability_strings[] = {
3000 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003001 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003002};
3003
3004static void
3005weston_compositor_log_capabilities(struct weston_compositor *compositor)
3006{
3007 unsigned i;
3008 int yes;
3009
3010 weston_log("Compositor capabilities:\n");
3011 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3012 yes = compositor->capabilities & capability_strings[i].bit;
3013 weston_log_continue(STAMP_SPACE "%s %s\n",
3014 capability_strings[i].desc,
3015 yes ? "yes" : "no");
3016 }
3017}
3018
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003019static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003020{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003021 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003022
Martin Minarik6d118362012-06-07 18:01:59 +02003023 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003024 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003025
3026 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003027}
3028
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003029#ifdef HAVE_LIBUNWIND
3030
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003031static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003032print_backtrace(void)
3033{
3034 unw_cursor_t cursor;
3035 unw_context_t context;
3036 unw_word_t off;
3037 unw_proc_info_t pip;
3038 int ret, i = 0;
3039 char procname[256];
3040 const char *filename;
3041 Dl_info dlinfo;
3042
3043 pip.unwind_info = NULL;
3044 ret = unw_getcontext(&context);
3045 if (ret) {
3046 weston_log("unw_getcontext: %d\n", ret);
3047 return;
3048 }
3049
3050 ret = unw_init_local(&cursor, &context);
3051 if (ret) {
3052 weston_log("unw_init_local: %d\n", ret);
3053 return;
3054 }
3055
3056 ret = unw_step(&cursor);
3057 while (ret > 0) {
3058 ret = unw_get_proc_info(&cursor, &pip);
3059 if (ret) {
3060 weston_log("unw_get_proc_info: %d\n", ret);
3061 break;
3062 }
3063
3064 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3065 if (ret && ret != -UNW_ENOMEM) {
3066 if (ret != -UNW_EUNSPEC)
3067 weston_log("unw_get_proc_name: %d\n", ret);
3068 procname[0] = '?';
3069 procname[1] = 0;
3070 }
3071
3072 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3073 *dlinfo.dli_fname)
3074 filename = dlinfo.dli_fname;
3075 else
3076 filename = "?";
3077
3078 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3079 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3080
3081 ret = unw_step(&cursor);
3082 if (ret < 0)
3083 weston_log("unw_step: %d\n", ret);
3084 }
3085}
3086
3087#else
3088
3089static void
3090print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003091{
3092 void *buffer[32];
3093 int i, count;
3094 Dl_info info;
3095
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003096 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3097 for (i = 0; i < count; i++) {
3098 dladdr(buffer[i], &info);
3099 weston_log(" [%016lx] %s (%s)\n",
3100 (long) buffer[i],
3101 info.dli_sname ? info.dli_sname : "--",
3102 info.dli_fname);
3103 }
3104}
3105
3106#endif
3107
3108static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003109on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003110{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003111 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003112 * then call the backend restore function, which will switch
3113 * back to the vt we launched from or ungrab X etc and then
3114 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003115 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003116 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003117 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003118
Peter Maatmane5b42e42013-03-27 22:38:53 +01003119 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003120
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003121 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003122
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003123 segv_compositor->restore(segv_compositor);
3124
3125 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003126}
3127
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003128static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003129load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003130{
3131 char path[PATH_MAX];
3132 void *module, *init;
3133
3134 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003135 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003136 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003137 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003138
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003139 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3140 if (module) {
3141 weston_log("Module '%s' already loaded\n", path);
3142 dlclose(module);
3143 return NULL;
3144 }
3145
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003146 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003147 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003148 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003149 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003150 return NULL;
3151 }
3152
3153 init = dlsym(module, entrypoint);
3154 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003155 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003156 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003157 return NULL;
3158 }
3159
3160 return init;
3161}
3162
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003163static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003164load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003165 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003166{
3167 const char *p, *end;
3168 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003169 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003170 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003171
3172 if (modules == NULL)
3173 return 0;
3174
3175 p = modules;
3176 while (*p) {
3177 end = strchrnul(p, ',');
3178 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3179 module_init = load_module(buffer, "module_init");
3180 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003181 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003182 p = end;
3183 while (*p == ',')
3184 p++;
3185
3186 }
3187
3188 return 0;
3189}
3190
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003191static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003192 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3193
3194static const char xdg_wrong_message[] =
3195 "fatal: environment variable XDG_RUNTIME_DIR\n"
3196 "is set to \"%s\", which is not a directory.\n";
3197
3198static const char xdg_wrong_mode_message[] =
3199 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3200 "correctly. Unix access mode must be 0700 but is %o,\n"
3201 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003202 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003203
3204static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003205 "Refer to your distribution on how to get it, or\n"
3206 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3207 "on how to implement it.\n";
3208
Martin Minarik37032f82012-06-18 20:15:18 +02003209static void
3210verify_xdg_runtime_dir(void)
3211{
3212 char *dir = getenv("XDG_RUNTIME_DIR");
3213 struct stat s;
3214
3215 if (!dir) {
3216 weston_log(xdg_error_message);
3217 weston_log_continue(xdg_detail_message);
3218 exit(EXIT_FAILURE);
3219 }
3220
3221 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3222 weston_log(xdg_wrong_message, dir);
3223 weston_log_continue(xdg_detail_message);
3224 exit(EXIT_FAILURE);
3225 }
3226
3227 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3228 weston_log(xdg_wrong_mode_message,
3229 dir, s.st_mode & 0777, s.st_uid);
3230 weston_log_continue(xdg_detail_message);
3231 }
3232}
3233
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003234static int
3235usage(int error_code)
3236{
3237 fprintf(stderr,
3238 "Usage: weston [OPTIONS]\n\n"
3239 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3240 "Weston supports multiple backends, and depending on which backend is in use\n"
3241 "different options will be accepted.\n\n"
3242
3243
3244 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003245 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003246 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003247 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3248 "\t\t\t\twayland-backend.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003249 " -S, --socket=NAME\tName of socket to listen on\n"
3250 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003251 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003252 " --log==FILE\t\tLog to the given file\n"
3253 " -h, --help\t\tThis help message\n\n");
3254
3255 fprintf(stderr,
3256 "Options for drm-backend.so:\n\n"
3257 " --connector=ID\tBring up only this connector\n"
3258 " --seat=SEAT\t\tThe seat that weston should run on\n"
3259 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003260 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003261 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3262
3263 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003264 "Options for fbdev-backend.so:\n\n"
3265 " --tty=TTY\t\tThe tty to use\n"
3266 " --device=DEVICE\tThe framebuffer device to use\n\n");
3267
3268 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003269 "Options for x11-backend.so:\n\n"
3270 " --width=WIDTH\t\tWidth of X window\n"
3271 " --height=HEIGHT\tHeight of X window\n"
3272 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003273 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003274 " --output-count=COUNT\tCreate multiple outputs\n"
3275 " --no-input\t\tDont create input devices\n\n");
3276
3277 fprintf(stderr,
3278 "Options for wayland-backend.so:\n\n"
3279 " --width=WIDTH\t\tWidth of Wayland surface\n"
3280 " --height=HEIGHT\tHeight of Wayland surface\n"
3281 " --display=DISPLAY\tWayland display to connect to\n\n");
3282
Pekka Paalanene31e0532013-05-22 18:03:07 +03003283#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3284 fprintf(stderr,
3285 "Options for rpi-backend.so:\n\n"
3286 " --tty=TTY\t\tThe tty to use\n"
3287 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3288 " --transform=TR\tThe output transformation, TR is one of:\n"
3289 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3290 "\n");
3291#endif
3292
Hardeningc077a842013-07-08 00:51:35 +02003293#if defined(BUILD_RDP_COMPOSITOR)
3294 fprintf(stderr,
3295 "Options for rdp-backend.so:\n\n"
3296 " --width=WIDTH\t\tWidth of desktop\n"
3297 " --height=HEIGHT\tHeight of desktop\n"
3298 " --extra-modes=MODES\t\n"
3299 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3300 " --address=ADDR\tThe address to bind\n"
3301 " --port=PORT\tThe port to listen on\n"
3302 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3303 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3304 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3305 "\n");
3306#endif
3307
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003308 exit(error_code);
3309}
3310
Peter Maatmane5b42e42013-03-27 22:38:53 +01003311static void
3312catch_signals(void)
3313{
3314 struct sigaction action;
3315
3316 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3317 action.sa_sigaction = on_caught_signal;
3318 sigemptyset(&action.sa_mask);
3319 sigaction(SIGSEGV, &action, NULL);
3320 sigaction(SIGABRT, &action, NULL);
3321}
3322
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003323int main(int argc, char *argv[])
3324{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003325 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003326 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003327 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003328 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003329 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003330 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003331 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003332 int *argc, char *argv[],
3333 struct weston_config *config);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003334 int i, config_fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003335 char *backend = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003336 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003337 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003338 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003339 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003340 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003341 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003342 struct weston_config *config;
3343 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003344
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003345 const struct weston_option core_options[] = {
3346 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3347 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3348 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003349 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003350 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003351 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003352 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003353 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003354
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003355 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003356
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003357 if (help)
3358 usage(EXIT_SUCCESS);
3359
Scott Moreau12245142012-08-29 15:15:58 -06003360 if (version) {
3361 printf(PACKAGE_STRING "\n");
3362 return EXIT_SUCCESS;
3363 }
3364
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003365 weston_log_file_open(log);
3366
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003367 weston_log("%s\n"
3368 STAMP_SPACE "%s\n"
3369 STAMP_SPACE "Bug reports to: %s\n"
3370 STAMP_SPACE "Build: %s\n",
3371 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003372 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003373 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003374
Martin Minarik37032f82012-06-18 20:15:18 +02003375 verify_xdg_runtime_dir();
3376
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003377 display = wl_display_create();
3378
Tiago Vignatti2116b892011-08-08 05:52:59 -07003379 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003380 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3381 display);
3382 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3383 display);
3384 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3385 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003386
3387 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003388 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3389 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003390
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003391 if (!backend) {
3392 if (getenv("WAYLAND_DISPLAY"))
3393 backend = "wayland-backend.so";
3394 else if (getenv("DISPLAY"))
3395 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003396 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003397 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003398 }
3399
Ossama Othmana50e6e42013-05-14 09:48:26 -07003400 config_fd = open_config_file("weston.ini");
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003401 config = weston_config_parse(config_fd);
3402 close(config_fd);
3403
3404 section = weston_config_get_section(config, "core", NULL, NULL);
3405 weston_config_section_get_string(section, "modules",
3406 &modules, "desktop-shell.so");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003407
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003408 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003409 if (!backend_init)
3410 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003411
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003412 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003413 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003414 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003415 exit(EXIT_FAILURE);
3416 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003417
Peter Maatmane5b42e42013-03-27 22:38:53 +01003418 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003419 segv_compositor = ec;
3420
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003421 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003422
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003423 setenv("WAYLAND_DISPLAY", socket_name, 1);
3424
Ossama Othmana50e6e42013-05-14 09:48:26 -07003425 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003426 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003427 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003428 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003429
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003430 for (i = 1; i < argc; i++)
3431 weston_log("fatal: unhandled option: %s\n", argv[i]);
3432 if (argc > 1) {
3433 ret = EXIT_FAILURE;
3434 goto out;
3435 }
3436
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003437 weston_compositor_log_capabilities(ec);
3438
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003439 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003440 weston_log("fatal: failed to add socket: %m\n");
3441 ret = EXIT_FAILURE;
3442 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003443 }
3444
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003445 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003446
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003447 wl_display_run(display);
3448
3449 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003450 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003451 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003452
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003453 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003454
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003455 for (i = ARRAY_LENGTH(signals); i;)
3456 wl_event_source_remove(signals[--i]);
3457
Daniel Stone855028f2012-05-01 20:37:10 +01003458 weston_compositor_xkb_destroy(ec);
3459
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003460 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003461 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003462
Martin Minarik19e6f262012-06-07 13:08:46 +02003463 weston_log_file_close();
3464
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003465 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003466}