blob: e57f61fe2420c5bfcb93a24496d91899394dea78 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanend581a8f2012-01-27 16:25:16 +02004 * Copyright © 2012 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberga9410222011-01-14 17:22:35 -050025#include "config.h"
26
Daniel Stoneb7452fe2012-06-01 12:14:06 +010027#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010032#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020034#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040035#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010036#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040037#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020038#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020039#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020040#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040041#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050042#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040044#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040045#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050046#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040047#include <sys/time.h>
48#include <time.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050049
Marcin Slusarz554a0da2013-02-18 13:27:22 -050050#ifdef HAVE_LIBUNWIND
51#define UNW_LOCAL_ONLY
52#include <libunwind.h>
53#endif
54
Pekka Paalanen50719bc2011-11-22 14:18:50 +020055#include <wayland-server.h>
Kristian Høgsberg82863022010-06-04 21:52:02 -040056#include "compositor.h"
U. Artie Eoffec08f332013-05-13 15:55:47 -070057#include "subsurface-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030058#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040059#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050060#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061
Kristian Høgsberg27da5382011-06-21 17:32:25 -040062static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040063static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040064
65static int
66sigchld_handler(int signal_number, void *data)
67{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050068 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040069 int status;
70 pid_t pid;
71
Bill Spitzak027b9622012-03-17 15:22:03 -070072 pid = waitpid(-1, &status, WNOHANG);
73 if (!pid)
74 return 1;
75
Kristian Høgsberg27da5382011-06-21 17:32:25 -040076 wl_list_for_each(p, &child_process_list, link) {
77 if (p->pid == pid)
78 break;
79 }
80
81 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020082 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040083 return 1;
84 }
85
86 wl_list_remove(&p->link);
87 p->cleanup(p, status);
88
89 return 1;
90}
91
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020092static void
Alexander Larsson0b135062013-05-28 16:23:36 +020093weston_output_transform_scale_init(struct weston_output *output,
94 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020095
Alex Wu2dda6042012-04-17 17:20:47 +080096WL_EXPORT int
Alexander Larsson355748e2013-05-28 16:23:38 +020097weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode, int32_t scale)
Alex Wu2dda6042012-04-17 17:20:47 +080098{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020099 struct weston_seat *seat;
100 pixman_region32_t old_output_region;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200101 int ret;
102
Alex Wu2dda6042012-04-17 17:20:47 +0800103 if (!output->switch_mode)
104 return -1;
105
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200106 ret = output->switch_mode(output, mode);
107 if (ret < 0)
108 return ret;
109
Alexander Larsson355748e2013-05-28 16:23:38 +0200110 output->scale = scale;
111
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200112 pixman_region32_init(&old_output_region);
113 pixman_region32_copy(&old_output_region, &output->region);
114
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200115 /* Update output region and transformation matrix */
Alexander Larsson0b135062013-05-28 16:23:36 +0200116 weston_output_transform_scale_init(output, output->transform, output->scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200117
118 pixman_region32_init(&output->previous_damage);
119 pixman_region32_init_rect(&output->region, output->x, output->y,
120 output->width, output->height);
121
122 weston_output_update_matrix(output);
123
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200124 /* If a pointer falls outside the outputs new geometry, move it to its
125 * lower-right corner */
126 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400127 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200128 int32_t x, y;
129
130 if (!pointer)
131 continue;
132
133 x = wl_fixed_to_int(pointer->x);
134 y = wl_fixed_to_int(pointer->y);
135
136 if (!pixman_region32_contains_point(&old_output_region,
137 x, y, NULL) ||
138 pixman_region32_contains_point(&output->region,
139 x, y, NULL))
140 continue;
141
142 if (x >= output->x + output->width)
143 x = output->x + output->width - 1;
144 if (y >= output->y + output->height)
145 y = output->y + output->height - 1;
146
147 pointer->x = wl_fixed_from_int(x);
148 pointer->y = wl_fixed_from_int(y);
149 }
150
151 pixman_region32_fini(&old_output_region);
152
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200153 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800154}
155
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400156WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500157weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400158{
159 wl_list_insert(&child_process_list, &process->link);
160}
161
Benjamin Franzke06286262011-05-06 19:12:33 +0200162static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200163child_client_exec(int sockfd, const char *path)
164{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500165 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200166 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200167 sigset_t allsigs;
168
169 /* do not give our signal mask to the new process */
170 sigfillset(&allsigs);
171 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200172
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500173 /* Launch clients as the user. */
174 seteuid(getuid());
175
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500176 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
177 * non-CLOEXEC fd to pass through exec. */
178 clientfd = dup(sockfd);
179 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200180 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500181 return;
182 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200183
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500184 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200185 setenv("WAYLAND_SOCKET", s, 1);
186
187 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200188 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200189 path);
190}
191
192WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500193weston_client_launch(struct weston_compositor *compositor,
194 struct weston_process *proc,
195 const char *path,
196 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200197{
198 int sv[2];
199 pid_t pid;
200 struct wl_client *client;
201
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300202 weston_log("launching '%s'\n", path);
203
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300204 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200205 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200206 "socketpair failed while launching '%s': %m\n",
207 path);
208 return NULL;
209 }
210
211 pid = fork();
212 if (pid == -1) {
213 close(sv[0]);
214 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200215 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200216 "fork failed while launching '%s': %m\n", path);
217 return NULL;
218 }
219
220 if (pid == 0) {
221 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700222 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200223 }
224
225 close(sv[1]);
226
227 client = wl_client_create(compositor->wl_display, sv[0]);
228 if (!client) {
229 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200230 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200231 "wl_client_create failed while launching '%s'.\n",
232 path);
233 return NULL;
234 }
235
236 proc->pid = pid;
237 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500238 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200239
240 return client;
241}
242
243static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300244surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
245{
246 struct weston_surface *surface =
247 container_of(listener, struct weston_surface,
248 pending.buffer_destroy_listener);
249
250 surface->pending.buffer = NULL;
251}
252
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200253static void
254empty_region(pixman_region32_t *region)
255{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300256 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200257 pixman_region32_init(region);
258}
259
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300260static void
261region_init_infinite(pixman_region32_t *region)
262{
263 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
264 UINT32_MAX, UINT32_MAX);
265}
266
Pekka Paalanene67858b2013-04-25 13:57:42 +0300267static struct weston_subsurface *
268weston_surface_to_subsurface(struct weston_surface *surface);
269
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500270WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500271weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500272{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500273 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400274
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200275 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400276 if (surface == NULL)
277 return NULL;
278
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500279 wl_signal_init(&surface->destroy_signal);
280
281 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500282
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500283 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500284 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500285
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500286 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400287 surface->alpha = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400288
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100289 if (compositor->renderer->create_surface(surface) < 0) {
290 free(surface);
291 return NULL;
292 }
293
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200294 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200295 surface->buffer_scale = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200296 surface->pending.buffer_transform = surface->buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200297 surface->pending.buffer_scale = surface->buffer_scale;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400298 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400299 surface->plane = &compositor->primary_plane;
Giulio Camuffo184df502013-02-21 11:29:21 +0100300 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200301
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400302 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500303 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500304 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300305 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200306 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500307 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400308
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200309 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200310 wl_list_insert(&surface->geometry.transformation_list,
311 &surface->transform.position.link);
312 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200313 wl_list_init(&surface->geometry.child_list);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200314 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200315 surface->transform.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500316
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300317 surface->pending.buffer_destroy_listener.notify =
318 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300319 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300320 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300321 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300322 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300323
Pekka Paalanene67858b2013-04-25 13:57:42 +0300324 wl_list_init(&surface->subsurface_list);
325 wl_list_init(&surface->subsurface_list_pending);
326
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400327 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500328}
329
Alex Wu8811bf92012-02-28 18:07:54 +0800330WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500331weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200332 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500333{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100334 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500335}
336
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400337WL_EXPORT void
338weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200339 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200340{
341 if (surface->transform.enabled) {
342 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
343
344 weston_matrix_transform(&surface->transform.matrix, &v);
345
346 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200347 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700348 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200349 v.f[3]);
350 *x = 0;
351 *y = 0;
352 return;
353 }
354
355 *x = v.f[0] / v.f[3];
356 *y = v.f[1] / v.f[3];
357 } else {
358 *x = sx + surface->geometry.x;
359 *y = sy + surface->geometry.y;
360 }
361}
362
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500363WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200364weston_transformed_coord(int width, int height,
365 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200366 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200367 float sx, float sy, float *bx, float *by)
368{
369 switch (transform) {
370 case WL_OUTPUT_TRANSFORM_NORMAL:
371 default:
372 *bx = sx;
373 *by = sy;
374 break;
375 case WL_OUTPUT_TRANSFORM_FLIPPED:
376 *bx = width - sx;
377 *by = sy;
378 break;
379 case WL_OUTPUT_TRANSFORM_90:
380 *bx = height - sy;
381 *by = sx;
382 break;
383 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
384 *bx = height - sy;
385 *by = width - sx;
386 break;
387 case WL_OUTPUT_TRANSFORM_180:
388 *bx = width - sx;
389 *by = height - sy;
390 break;
391 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
392 *bx = sx;
393 *by = height - sy;
394 break;
395 case WL_OUTPUT_TRANSFORM_270:
396 *bx = sy;
397 *by = width - sx;
398 break;
399 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
400 *bx = sy;
401 *by = sx;
402 break;
403 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200404
405 *bx *= scale;
406 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200407}
408
409WL_EXPORT pixman_box32_t
410weston_transformed_rect(int width, int height,
411 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200412 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200413 pixman_box32_t rect)
414{
415 float x1, x2, y1, y2;
416
417 pixman_box32_t ret;
418
Alexander Larsson4ea95522013-05-22 14:41:37 +0200419 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200420 rect.x1, rect.y1, &x1, &y1);
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.x2, rect.y2, &x2, &y2);
423
424 if (x1 <= x2) {
425 ret.x1 = x1;
426 ret.x2 = x2;
427 } else {
428 ret.x1 = x2;
429 ret.x2 = x1;
430 }
431
432 if (y1 <= y2) {
433 ret.y1 = y1;
434 ret.y2 = y2;
435 } else {
436 ret.y1 = y2;
437 ret.y2 = y1;
438 }
439
440 return ret;
441}
442
443WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200444weston_surface_to_buffer_float(struct weston_surface *surface,
445 float sx, float sy, float *bx, float *by)
446{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200447 weston_transformed_coord(surface->geometry.width,
448 surface->geometry.height,
449 surface->buffer_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200450 surface->buffer_scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200451 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200452}
453
Alexander Larsson4ea95522013-05-22 14:41:37 +0200454WL_EXPORT void
455weston_surface_to_buffer(struct weston_surface *surface,
456 int sx, int sy, int *bx, int *by)
457{
458 float bxf, byf;
459
460 weston_transformed_coord(surface->geometry.width,
461 surface->geometry.height,
462 surface->buffer_transform,
463 surface->buffer_scale,
464 sx, sy, &bxf, &byf);
465 *bx = floorf(bxf);
466 *by = floorf(byf);
467}
468
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200469WL_EXPORT pixman_box32_t
470weston_surface_to_buffer_rect(struct weston_surface *surface,
471 pixman_box32_t rect)
472{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200473 return weston_transformed_rect(surface->geometry.width,
474 surface->geometry.height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200475 surface->buffer_transform,
476 surface->buffer_scale,
477 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200478}
479
480WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400481weston_surface_move_to_plane(struct weston_surface *surface,
482 struct weston_plane *plane)
483{
484 if (surface->plane == plane)
485 return;
486
487 weston_surface_damage_below(surface);
488 surface->plane = plane;
489 weston_surface_damage(surface);
490}
491
492WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500493weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200494{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500495 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200496
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500497 pixman_region32_init(&damage);
498 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
499 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400500 pixman_region32_union(&surface->plane->damage,
501 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500502 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200503}
504
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400505static void
506weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
507{
508 uint32_t different = es->output_mask ^ mask;
509 uint32_t entered = mask & different;
510 uint32_t left = es->output_mask & different;
511 struct weston_output *output;
512 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500513 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400514
515 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500516 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400517 return;
518 if (different == 0)
519 return;
520
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500521 client = wl_resource_get_client(es->resource);
522
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400523 wl_list_for_each(output, &es->compositor->output_list, link) {
524 if (1 << output->id & different)
525 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500526 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400527 client);
528 if (resource == NULL)
529 continue;
530 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500531 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400532 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500533 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400534 }
535}
536
537static void
538weston_surface_assign_output(struct weston_surface *es)
539{
540 struct weston_compositor *ec = es->compositor;
541 struct weston_output *output, *new_output;
542 pixman_region32_t region;
543 uint32_t max, area, mask;
544 pixman_box32_t *e;
545
546 new_output = NULL;
547 max = 0;
548 mask = 0;
549 pixman_region32_init(&region);
550 wl_list_for_each(output, &ec->output_list, link) {
551 pixman_region32_intersect(&region, &es->transform.boundingbox,
552 &output->region);
553
554 e = pixman_region32_extents(&region);
555 area = (e->x2 - e->x1) * (e->y2 - e->y1);
556
557 if (area > 0)
558 mask |= 1 << output->id;
559
560 if (area >= max) {
561 new_output = output;
562 max = area;
563 }
564 }
565 pixman_region32_fini(&region);
566
567 es->output = new_output;
568 weston_surface_update_output_mask(es, mask);
569}
570
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200571static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200572surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
573 int32_t width, int32_t height,
574 pixman_region32_t *bbox)
575{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200576 float min_x = HUGE_VALF, min_y = HUGE_VALF;
577 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200578 int32_t s[4][2] = {
579 { sx, sy },
580 { sx, sy + height },
581 { sx + width, sy },
582 { sx + width, sy + height }
583 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200584 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200585 int i;
586
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300587 if (width == 0 || height == 0) {
588 /* avoid rounding empty bbox to 1x1 */
589 pixman_region32_init(bbox);
590 return;
591 }
592
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200593 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200594 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400595 weston_surface_to_global_float(surface,
596 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200597 if (x < min_x)
598 min_x = x;
599 if (x > max_x)
600 max_x = x;
601 if (y < min_y)
602 min_y = y;
603 if (y > max_y)
604 max_y = y;
605 }
606
Pekka Paalanen219b9822012-02-08 15:38:37 +0200607 int_x = floorf(min_x);
608 int_y = floorf(min_y);
609 pixman_region32_init_rect(bbox, int_x, int_y,
610 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200611}
612
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200613static void
614weston_surface_update_transform_disable(struct weston_surface *surface)
615{
616 surface->transform.enabled = 0;
617
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200618 /* round off fractions when not transformed */
619 surface->geometry.x = roundf(surface->geometry.x);
620 surface->geometry.y = roundf(surface->geometry.y);
621
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500622 /* Otherwise identity matrix, but with x and y translation. */
623 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
624 surface->transform.position.matrix.d[12] = surface->geometry.x;
625 surface->transform.position.matrix.d[13] = surface->geometry.y;
626
627 surface->transform.matrix = surface->transform.position.matrix;
628
Kristian Høgsberg9bcaaeb2013-02-28 14:56:43 -0500629 surface->transform.inverse = surface->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500630 surface->transform.inverse.d[12] = -surface->geometry.x;
631 surface->transform.inverse.d[13] = -surface->geometry.y;
632
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200633 pixman_region32_init_rect(&surface->transform.boundingbox,
634 surface->geometry.x,
635 surface->geometry.y,
636 surface->geometry.width,
637 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500638
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400639 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500640 pixman_region32_copy(&surface->transform.opaque,
641 &surface->opaque);
642 pixman_region32_translate(&surface->transform.opaque,
643 surface->geometry.x,
644 surface->geometry.y);
645 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200646}
647
648static int
649weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200650{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200651 struct weston_surface *parent = surface->geometry.parent;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200652 struct weston_matrix *matrix = &surface->transform.matrix;
653 struct weston_matrix *inverse = &surface->transform.inverse;
654 struct weston_transform *tform;
655
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200656 surface->transform.enabled = 1;
657
658 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300659 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200660 surface->transform.position.matrix.d[12] = surface->geometry.x;
661 surface->transform.position.matrix.d[13] = surface->geometry.y;
662
663 weston_matrix_init(matrix);
664 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
665 weston_matrix_multiply(matrix, &tform->matrix);
666
Pekka Paalanen483243f2013-03-08 14:56:50 +0200667 if (parent)
668 weston_matrix_multiply(matrix, &parent->transform.matrix);
669
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200670 if (weston_matrix_invert(inverse, matrix) < 0) {
671 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200672 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200673 " transformation not invertible.\n", surface);
674 return -1;
675 }
676
677 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
678 surface->geometry.height,
679 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500680
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200681 return 0;
682}
683
684WL_EXPORT void
685weston_surface_update_transform(struct weston_surface *surface)
686{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200687 struct weston_surface *parent = surface->geometry.parent;
688
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200689 if (!surface->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200690 return;
691
Pekka Paalanen483243f2013-03-08 14:56:50 +0200692 if (parent)
693 weston_surface_update_transform(parent);
694
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200695 surface->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200696
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500697 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200698
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200699 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500700 pixman_region32_fini(&surface->transform.opaque);
701 pixman_region32_init(&surface->transform.opaque);
702
Pekka Paalanencd403622012-01-25 13:37:39 +0200703 /* transform.position is always in transformation_list */
704 if (surface->geometry.transformation_list.next ==
705 &surface->transform.position.link &&
706 surface->geometry.transformation_list.prev ==
Pekka Paalanen483243f2013-03-08 14:56:50 +0200707 &surface->transform.position.link &&
708 !parent) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200709 weston_surface_update_transform_disable(surface);
710 } else {
711 if (weston_surface_update_transform_enable(surface) < 0)
712 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200713 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200714
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400715 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200716
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300717 weston_surface_assign_output(surface);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300718
719 wl_signal_emit(&surface->compositor->transform_signal, surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200720}
721
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200722WL_EXPORT void
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200723weston_surface_geometry_dirty(struct weston_surface *surface)
724{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200725 struct weston_surface *child;
726
727 /*
728 * The invariant: if surface->geometry.dirty, then all surfaces
729 * in surface->geometry.child_list have geometry.dirty too.
730 * Corollary: if not parent->geometry.dirty, then all ancestors
731 * are not dirty.
732 */
733
734 if (surface->transform.dirty)
735 return;
736
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200737 surface->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200738
739 wl_list_for_each(child, &surface->geometry.child_list,
740 geometry.parent_link)
741 weston_surface_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200742}
743
744WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100745weston_surface_to_global_fixed(struct weston_surface *surface,
746 wl_fixed_t sx, wl_fixed_t sy,
747 wl_fixed_t *x, wl_fixed_t *y)
748{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200749 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100750
751 weston_surface_to_global_float(surface,
752 wl_fixed_to_double(sx),
753 wl_fixed_to_double(sy),
754 &xf, &yf);
755 *x = wl_fixed_from_double(xf);
756 *y = wl_fixed_from_double(yf);
757}
758
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400759WL_EXPORT void
760weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200761 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200762{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200763 if (surface->transform.enabled) {
764 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
765
766 weston_matrix_transform(&surface->transform.inverse, &v);
767
768 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200769 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200770 "weston_surface_from_global(), divisor = %g\n",
771 v.f[3]);
772 *sx = 0;
773 *sy = 0;
774 return;
775 }
776
Pekka Paalanencd403622012-01-25 13:37:39 +0200777 *sx = v.f[0] / v.f[3];
778 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200779 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200780 *sx = x - surface->geometry.x;
781 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200782 }
783}
784
785WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100786weston_surface_from_global_fixed(struct weston_surface *surface,
787 wl_fixed_t x, wl_fixed_t y,
788 wl_fixed_t *sx, wl_fixed_t *sy)
789{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200790 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100791
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400792 weston_surface_from_global_float(surface,
793 wl_fixed_to_double(x),
794 wl_fixed_to_double(y),
795 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100796 *sx = wl_fixed_from_double(sxf);
797 *sy = wl_fixed_from_double(syf);
798}
799
800WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200801weston_surface_from_global(struct weston_surface *surface,
802 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
803{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200804 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200805
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400806 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200807 *sx = floorf(sxf);
808 *sy = floorf(syf);
809}
810
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400811WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400812weston_surface_schedule_repaint(struct weston_surface *surface)
813{
814 struct weston_output *output;
815
816 wl_list_for_each(output, &surface->compositor->output_list, link)
817 if (surface->output_mask & (1 << output->id))
818 weston_output_schedule_repaint(output);
819}
820
821WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500822weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500823{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400824 pixman_region32_union_rect(&surface->damage, &surface->damage,
825 0, 0, surface->geometry.width,
826 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200827
Kristian Høgsberg98238702012-08-03 16:29:12 -0400828 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500829}
830
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400831WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500832weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200833 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400834{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200835 surface->geometry.x = x;
836 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200837 surface->geometry.width = width;
838 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200839 weston_surface_geometry_dirty(surface);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400840}
841
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200842WL_EXPORT void
843weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200844 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200845{
846 surface->geometry.x = x;
847 surface->geometry.y = y;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200848 weston_surface_geometry_dirty(surface);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200849}
850
Pekka Paalanen483243f2013-03-08 14:56:50 +0200851static void
852transform_parent_handle_parent_destroy(struct wl_listener *listener,
853 void *data)
854{
855 struct weston_surface *surface =
856 container_of(listener, struct weston_surface,
857 geometry.parent_destroy_listener);
858
859 weston_surface_set_transform_parent(surface, NULL);
860}
861
862WL_EXPORT void
863weston_surface_set_transform_parent(struct weston_surface *surface,
864 struct weston_surface *parent)
865{
866 if (surface->geometry.parent) {
867 wl_list_remove(&surface->geometry.parent_destroy_listener.link);
868 wl_list_remove(&surface->geometry.parent_link);
869 }
870
871 surface->geometry.parent = parent;
872
873 surface->geometry.parent_destroy_listener.notify =
874 transform_parent_handle_parent_destroy;
875 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500876 wl_signal_add(&parent->destroy_signal,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200877 &surface->geometry.parent_destroy_listener);
878 wl_list_insert(&parent->geometry.child_list,
879 &surface->geometry.parent_link);
880 }
881
882 weston_surface_geometry_dirty(surface);
883}
884
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300885WL_EXPORT int
886weston_surface_is_mapped(struct weston_surface *surface)
887{
888 if (surface->output)
889 return 1;
890 else
891 return 0;
892}
893
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200894WL_EXPORT int32_t
895weston_surface_buffer_width(struct weston_surface *surface)
896{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200897 int32_t width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200898 switch (surface->buffer_transform) {
899 case WL_OUTPUT_TRANSFORM_90:
900 case WL_OUTPUT_TRANSFORM_270:
901 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
902 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200903 width = surface->buffer_ref.buffer->height;
904 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200905 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200906 width = surface->buffer_ref.buffer->width;
907 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200908 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200909 return width / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200910}
911
912WL_EXPORT int32_t
913weston_surface_buffer_height(struct weston_surface *surface)
914{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200915 int32_t height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200916 switch (surface->buffer_transform) {
917 case WL_OUTPUT_TRANSFORM_90:
918 case WL_OUTPUT_TRANSFORM_270:
919 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
920 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200921 height = surface->buffer_ref.buffer->width;
922 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200923 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200924 height = surface->buffer_ref.buffer->height;
925 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200926 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200927 return height / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200928}
929
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400930WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500931weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500932{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400933 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500934
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400935 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500936
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400937 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500938}
939
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400940WL_EXPORT struct weston_surface *
Tiago Vignatti9d393522012-02-10 16:26:19 +0200941weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100942 wl_fixed_t x, wl_fixed_t y,
943 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200944{
945 struct weston_surface *surface;
946
947 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100948 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500949 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100950 wl_fixed_to_int(*sx),
951 wl_fixed_to_int(*sy),
952 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200953 return surface;
954 }
955
956 return NULL;
957}
958
959static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500960weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400961{
Daniel Stone37816df2012-05-16 18:45:18 +0100962 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400963
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500964 if (!compositor->focus)
965 return;
966
Daniel Stone37816df2012-05-16 18:45:18 +0100967 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400968 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400969}
970
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400971WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500972weston_surface_unmap(struct weston_surface *surface)
973{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100974 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500975
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500976 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500977 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500978 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500979
Daniel Stone4dab5db2012-05-30 16:31:53 +0100980 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400981 if (seat->keyboard && seat->keyboard->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400982 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400983 if (seat->pointer && seat->pointer->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400984 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400985 NULL,
986 wl_fixed_from_int(0),
987 wl_fixed_from_int(0));
Daniel Stone4dab5db2012-05-30 16:31:53 +0100988 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500989
Kristian Høgsberg98238702012-08-03 16:29:12 -0400990 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500991}
992
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400993struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -0500994 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400995 struct wl_list link;
996};
997
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500998
999WL_EXPORT void
1000weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001001{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001002 /* Not a valid way to destroy a client surface */
1003 assert(surface->resource == NULL);
1004
1005 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1006
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001007 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001008 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001009
Pekka Paalanen483243f2013-03-08 14:56:50 +02001010 assert(wl_list_empty(&surface->geometry.child_list));
Pekka Paalanene67858b2013-04-25 13:57:42 +03001011 assert(wl_list_empty(&surface->subsurface_list_pending));
1012 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001013
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001014 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001015 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001016
Pekka Paalanenbc106382012-10-10 12:49:31 +03001017 wl_list_for_each_safe(cb, next,
1018 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001019 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001020
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001021 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001022 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001023 pixman_region32_fini(&surface->pending.damage);
1024
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001025 if (surface->pending.buffer)
1026 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1027
Pekka Paalanende685b82012-12-04 15:58:12 +02001028 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001029
Kristian Høgsberg42263852012-09-06 21:59:29 -04001030 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001031
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001032 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001033 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001034 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001035 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001036 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001037
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001038 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001039 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001040
Pekka Paalanen483243f2013-03-08 14:56:50 +02001041 weston_surface_set_transform_parent(surface, NULL);
1042
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001043 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001044}
1045
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001046static void
1047destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001048{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001049 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001050
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001051 surface->resource = NULL;
1052 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001053}
1054
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001055static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001056weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1057 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001058{
Pekka Paalanende685b82012-12-04 15:58:12 +02001059 struct weston_buffer_reference *ref =
1060 container_of(listener, struct weston_buffer_reference,
1061 destroy_listener);
1062
1063 assert((struct wl_buffer *)data == ref->buffer);
1064 ref->buffer = NULL;
1065}
1066
1067WL_EXPORT void
1068weston_buffer_reference(struct weston_buffer_reference *ref,
1069 struct wl_buffer *buffer)
1070{
1071 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001072 ref->buffer->busy_count--;
1073 if (ref->buffer->busy_count == 0) {
1074 assert(ref->buffer->resource.client != NULL);
1075 wl_resource_queue_event(&ref->buffer->resource,
1076 WL_BUFFER_RELEASE);
1077 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001078 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001079 }
1080
Pekka Paalanende685b82012-12-04 15:58:12 +02001081 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001082 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001083 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001084 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001085 }
1086
Pekka Paalanende685b82012-12-04 15:58:12 +02001087 ref->buffer = buffer;
1088 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1089}
1090
1091static void
1092weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
1093{
1094 weston_buffer_reference(&surface->buffer_ref, buffer);
1095
Pekka Paalanena6421c42012-12-04 15:58:10 +02001096 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001097 if (weston_surface_is_mapped(surface))
1098 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001099 }
1100
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001101 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001102}
1103
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001104WL_EXPORT void
1105weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001106{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001107 wl_list_remove(&surface->layer_link);
1108 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001109 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001110 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001111}
1112
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001113WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001114weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001115{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001116 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001117
1118 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001119 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001120}
1121
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001122WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001123weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001124{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001125 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001126
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001127 pixman_region32_union(&compositor->primary_plane.damage,
1128 &compositor->primary_plane.damage,
1129 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001130 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001131}
1132
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001133static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001134surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001135 pixman_region32_t *opaque)
1136{
Pekka Paalanende685b82012-12-04 15:58:12 +02001137 if (surface->buffer_ref.buffer &&
1138 wl_buffer_is_shm(surface->buffer_ref.buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001139 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001140
1141 if (surface->transform.enabled) {
1142 pixman_box32_t *extents;
1143
1144 extents = pixman_region32_extents(&surface->damage);
1145 surface_compute_bbox(surface, extents->x1, extents->y1,
1146 extents->x2 - extents->x1,
1147 extents->y2 - extents->y1,
1148 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001149 pixman_region32_translate(&surface->damage,
1150 -surface->plane->x,
1151 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001152 } else {
1153 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001154 surface->geometry.x - surface->plane->x,
1155 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001156 }
1157
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001158 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001159 pixman_region32_union(&surface->plane->damage,
1160 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001161 empty_region(&surface->damage);
1162 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001163 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001164}
1165
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001166static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001167compositor_accumulate_damage(struct weston_compositor *ec)
1168{
1169 struct weston_plane *plane;
1170 struct weston_surface *es;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001171 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001172
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001173 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001174
1175 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001176 pixman_region32_copy(&plane->clip, &clip);
1177
1178 pixman_region32_init(&opaque);
1179
1180 wl_list_for_each(es, &ec->surface_list, link) {
1181 if (es->plane != plane)
1182 continue;
1183
1184 surface_accumulate_damage(es, &opaque);
1185 }
1186
1187 pixman_region32_union(&clip, &clip, &opaque);
1188 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001189 }
1190
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001191 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001192
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001193 wl_list_for_each(es, &ec->surface_list, link) {
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001194 /* Both the renderer and the backend have seen the buffer
1195 * by now. If renderer needs the buffer, it has its own
1196 * reference set. If the backend wants to keep the buffer
1197 * around for migrating the surface into a non-primary plane
1198 * later, keep_buffer is true. Otherwise, drop the core
1199 * reference now, and allow early buffer release. This enables
1200 * clients to use single-buffering.
1201 */
1202 if (!es->keep_buffer)
1203 weston_buffer_reference(&es->buffer_ref, NULL);
1204 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001205}
1206
1207static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001208surface_list_add(struct weston_compositor *compositor,
1209 struct weston_surface *surface)
1210{
1211 struct weston_subsurface *sub;
1212
1213 if (wl_list_empty(&surface->subsurface_list)) {
1214 weston_surface_update_transform(surface);
1215 wl_list_insert(compositor->surface_list.prev, &surface->link);
1216 return;
1217 }
1218
1219 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1220 if (!weston_surface_is_mapped(sub->surface))
1221 continue;
1222
1223 if (sub->surface == surface) {
1224 weston_surface_update_transform(sub->surface);
1225 wl_list_insert(compositor->surface_list.prev,
1226 &sub->surface->link);
1227 } else {
1228 surface_list_add(compositor, sub->surface);
1229 }
1230 }
1231}
1232
1233static void
1234weston_compositor_build_surface_list(struct weston_compositor *compositor)
1235{
1236 struct weston_surface *surface;
1237 struct weston_layer *layer;
1238
1239 wl_list_init(&compositor->surface_list);
1240 wl_list_for_each(layer, &compositor->layer_list, link) {
1241 wl_list_for_each(surface, &layer->surface_list, layer_link) {
1242 surface_list_add(compositor, surface);
1243 }
1244 }
1245}
1246
1247static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001248weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001249{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001250 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001251 struct weston_surface *es;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001252 struct weston_animation *animation, *next;
1253 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001254 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001255 pixman_region32_t output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001256
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001257 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001258 weston_compositor_build_surface_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001259
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001260 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001261 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001262 else
1263 wl_list_for_each(es, &ec->surface_list, link)
1264 weston_surface_move_to_plane(es, &ec->primary_plane);
1265
Pekka Paalanene67858b2013-04-25 13:57:42 +03001266 wl_list_init(&frame_callback_list);
1267 wl_list_for_each(es, &ec->surface_list, link) {
1268 if (es->output == output) {
1269 wl_list_insert_list(&frame_callback_list,
1270 &es->frame_callback_list);
1271 wl_list_init(&es->frame_callback_list);
1272 }
1273 }
1274
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001275 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001276
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001277 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001278 pixman_region32_intersect(&output_damage,
1279 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001280 pixman_region32_subtract(&output_damage,
1281 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001282
Scott Moreauccbf29d2012-02-22 14:21:41 -07001283 if (output->dirty)
1284 weston_output_update_matrix(output);
1285
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001286 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001287
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001288 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001289
Kristian Høgsbergef044142011-06-21 15:02:12 -04001290 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001291
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001292 weston_compositor_repick(ec);
1293 wl_event_loop_dispatch(ec->input_loop, 0);
1294
Jonas Ådahldb773762012-06-13 00:01:21 +02001295 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001296 wl_callback_send_done(cb->resource, msecs);
1297 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001298 }
1299
Scott Moreaud64cf212012-06-08 19:40:54 -06001300 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001301 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001302 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001303 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001304}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001305
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001306static int
1307weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001308{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001309 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001310
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001311 wl_event_loop_dispatch(compositor->input_loop, 0);
1312
1313 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001314}
1315
1316WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001317weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001318{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001319 struct weston_compositor *compositor = output->compositor;
1320 struct wl_event_loop *loop =
1321 wl_display_get_event_loop(compositor->wl_display);
1322 int fd;
1323
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001324 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001325 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001326 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001327 return;
1328 }
1329
1330 output->repaint_scheduled = 0;
1331 if (compositor->input_loop_source)
1332 return;
1333
1334 fd = wl_event_loop_get_fd(compositor->input_loop);
1335 compositor->input_loop_source =
1336 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1337 weston_compositor_read_input, compositor);
1338}
1339
1340static void
1341idle_repaint(void *data)
1342{
1343 struct weston_output *output = data;
1344
Jonas Ådahle5a12252013-04-05 23:07:11 +02001345 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001346}
1347
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001348WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001349weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1350{
1351 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001352 if (below != NULL)
1353 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001354}
1355
1356WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001357weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001358{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001359 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001360 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001361
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001362 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1363 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001364 return;
1365
Kristian Høgsbergef044142011-06-21 15:02:12 -04001366 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001367 output->repaint_needed = 1;
1368 if (output->repaint_scheduled)
1369 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001370
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001371 wl_event_loop_add_idle(loop, idle_repaint, output);
1372 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001373
1374 if (compositor->input_loop_source) {
1375 wl_event_source_remove(compositor->input_loop_source);
1376 compositor->input_loop_source = NULL;
1377 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001378}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001379
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001380WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001381weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1382{
1383 struct weston_output *output;
1384
1385 wl_list_for_each(output, &compositor->output_list, link)
1386 weston_output_schedule_repaint(output);
1387}
1388
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001389static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001390surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001391{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001392 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001393}
1394
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001395static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001396surface_attach(struct wl_client *client,
1397 struct wl_resource *resource,
1398 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1399{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001400 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001401 struct wl_buffer *buffer = NULL;
1402
1403 if (buffer_resource)
1404 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001405
Pekka Paalanende685b82012-12-04 15:58:12 +02001406 /* Attach, attach, without commit in between does not send
1407 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001408 if (surface->pending.buffer)
1409 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001410
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001411 surface->pending.sx = sx;
1412 surface->pending.sy = sy;
1413 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001414 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001415 if (buffer) {
1416 wl_signal_add(&buffer->resource.destroy_signal,
1417 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001418 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001419}
1420
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001421static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001422surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001423 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001424 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001425{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001426 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001427
Pekka Paalanen8e159182012-10-10 12:49:25 +03001428 pixman_region32_union_rect(&surface->pending.damage,
1429 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001430 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001431}
1432
Kristian Høgsberg33418202011-08-16 23:01:28 -04001433static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001434destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001435{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001436 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001437
1438 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001439 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001440}
1441
1442static void
1443surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001444 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001445{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001446 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001447 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001448
1449 cb = malloc(sizeof *cb);
1450 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001451 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001452 return;
1453 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001454
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001455 cb->resource = wl_client_add_object(client, &wl_callback_interface,
1456 NULL, callback, cb);
1457 wl_resource_set_destructor(cb->resource, destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001458
Pekka Paalanenbc106382012-10-10 12:49:31 +03001459 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001460}
1461
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001462static void
1463surface_set_opaque_region(struct wl_client *client,
1464 struct wl_resource *resource,
1465 struct wl_resource *region_resource)
1466{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001467 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001468 struct weston_region *region;
1469
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001470 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001471 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001472 pixman_region32_copy(&surface->pending.opaque,
1473 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001474 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001475 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001476 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001477}
1478
1479static void
1480surface_set_input_region(struct wl_client *client,
1481 struct wl_resource *resource,
1482 struct wl_resource *region_resource)
1483{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001484 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001485 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001486
1487 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001488 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001489 pixman_region32_copy(&surface->pending.input,
1490 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001491 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001492 pixman_region32_fini(&surface->pending.input);
1493 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001494 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001495}
1496
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001497static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001498weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001499{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001500 struct weston_subsurface *sub;
1501
1502 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1503 parent_link_pending) {
1504 wl_list_remove(&sub->parent_link);
1505 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1506 }
1507}
1508
1509static void
1510weston_surface_commit(struct weston_surface *surface)
1511{
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001512 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001513 int surface_width = 0;
1514 int surface_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001515
Alexander Larsson4ea95522013-05-22 14:41:37 +02001516 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001517 surface->buffer_transform = surface->pending.buffer_transform;
1518
Alexander Larsson4ea95522013-05-22 14:41:37 +02001519 /* wl_surface.set_buffer_scale */
1520 surface->buffer_scale = surface->pending.buffer_scale;
1521
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001522 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001523 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001524 weston_surface_attach(surface, surface->pending.buffer);
1525
Giulio Camuffo184df502013-02-21 11:29:21 +01001526 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001527 surface_width = weston_surface_buffer_width(surface);
1528 surface_height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001529 }
1530
1531 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001532 surface->configure(surface,
1533 surface->pending.sx, surface->pending.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001534 surface_width, surface_height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001535
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001536 if (surface->pending.buffer)
1537 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1538 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001539 surface->pending.sx = 0;
1540 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001541 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001542
1543 /* wl_surface.damage */
1544 pixman_region32_union(&surface->damage, &surface->damage,
1545 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001546 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1547 0, 0,
1548 surface->geometry.width,
1549 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001550 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001551
1552 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001553 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001554 surface->geometry.width,
1555 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001556 pixman_region32_intersect(&opaque,
1557 &opaque, &surface->pending.opaque);
1558
1559 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1560 pixman_region32_copy(&surface->opaque, &opaque);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001561 weston_surface_geometry_dirty(surface);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001562 }
1563
1564 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001565
1566 /* wl_surface.set_input_region */
1567 pixman_region32_fini(&surface->input);
1568 pixman_region32_init_rect(&surface->input, 0, 0,
1569 surface->geometry.width,
1570 surface->geometry.height);
1571 pixman_region32_intersect(&surface->input,
1572 &surface->input, &surface->pending.input);
1573
Pekka Paalanenbc106382012-10-10 12:49:31 +03001574 /* wl_surface.frame */
1575 wl_list_insert_list(&surface->frame_callback_list,
1576 &surface->pending.frame_callback_list);
1577 wl_list_init(&surface->pending.frame_callback_list);
1578
Pekka Paalanene67858b2013-04-25 13:57:42 +03001579 weston_surface_commit_subsurface_order(surface);
1580
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001581 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001582}
1583
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001584static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001585weston_subsurface_commit(struct weston_subsurface *sub);
1586
1587static void
1588weston_subsurface_parent_commit(struct weston_subsurface *sub,
1589 int parent_is_synchronized);
1590
1591static void
1592surface_commit(struct wl_client *client, struct wl_resource *resource)
1593{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001594 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001595 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1596
1597 if (sub) {
1598 weston_subsurface_commit(sub);
1599 return;
1600 }
1601
1602 weston_surface_commit(surface);
1603
1604 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1605 if (sub->surface != surface)
1606 weston_subsurface_parent_commit(sub, 0);
1607 }
1608}
1609
1610static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001611surface_set_buffer_transform(struct wl_client *client,
1612 struct wl_resource *resource, int transform)
1613{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001614 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001615
1616 surface->pending.buffer_transform = transform;
1617}
1618
Alexander Larsson4ea95522013-05-22 14:41:37 +02001619static void
1620surface_set_buffer_scale(struct wl_client *client,
1621 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001622 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02001623{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001624 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02001625
1626 surface->pending.buffer_scale = scale;
1627}
1628
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001629static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001630 surface_destroy,
1631 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001632 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001633 surface_frame,
1634 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001635 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001636 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001637 surface_set_buffer_transform,
1638 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001639};
1640
1641static void
1642compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001643 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001644{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001645 struct weston_compositor *ec = resource->data;
1646 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001647
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001648 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001649 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001650 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001651 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001652 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001653
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001654 surface->resource = wl_client_add_object(client, &wl_surface_interface,
1655 &surface_interface,
1656 id, surface);
1657 wl_resource_set_destructor(surface->resource, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001658}
1659
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001660static void
1661destroy_region(struct wl_resource *resource)
1662{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001663 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001664
1665 pixman_region32_fini(&region->region);
1666 free(region);
1667}
1668
1669static void
1670region_destroy(struct wl_client *client, struct wl_resource *resource)
1671{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001672 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001673}
1674
1675static void
1676region_add(struct wl_client *client, struct wl_resource *resource,
1677 int32_t x, int32_t y, int32_t width, int32_t height)
1678{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001679 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001680
1681 pixman_region32_union_rect(&region->region, &region->region,
1682 x, y, width, height);
1683}
1684
1685static void
1686region_subtract(struct wl_client *client, struct wl_resource *resource,
1687 int32_t x, int32_t y, int32_t width, int32_t height)
1688{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001689 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001690 pixman_region32_t rect;
1691
1692 pixman_region32_init_rect(&rect, x, y, width, height);
1693 pixman_region32_subtract(&region->region, &region->region, &rect);
1694 pixman_region32_fini(&rect);
1695}
1696
1697static const struct wl_region_interface region_interface = {
1698 region_destroy,
1699 region_add,
1700 region_subtract
1701};
1702
1703static void
1704compositor_create_region(struct wl_client *client,
1705 struct wl_resource *resource, uint32_t id)
1706{
1707 struct weston_region *region;
1708
1709 region = malloc(sizeof *region);
1710 if (region == NULL) {
1711 wl_resource_post_no_memory(resource);
1712 return;
1713 }
1714
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001715 pixman_region32_init(&region->region);
1716
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001717 region->resource = wl_client_add_object(client, &wl_region_interface,
1718 &region_interface, id, region);
1719 wl_resource_set_destructor(region->resource, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001720}
1721
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001722static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001723 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001724 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001725};
1726
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001727static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001728weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
1729{
1730 struct weston_surface *surface = sub->surface;
1731 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001732 int surface_width = 0;
1733 int surface_height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001734
Alexander Larsson4ea95522013-05-22 14:41:37 +02001735 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001736 surface->buffer_transform = sub->cached.buffer_transform;
1737
Alexander Larsson4ea95522013-05-22 14:41:37 +02001738 /* wl_surface.set_buffer_scale */
1739 surface->buffer_scale = sub->cached.buffer_scale;
1740
Pekka Paalanene67858b2013-04-25 13:57:42 +03001741 /* wl_surface.attach */
1742 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
1743 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
1744 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
1745
1746 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001747 surface_width = weston_surface_buffer_width(surface);
1748 surface_height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001749 }
1750
1751 if (surface->configure && sub->cached.newly_attached)
1752 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001753 surface_width, surface_height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001754 sub->cached.sx = 0;
1755 sub->cached.sy = 0;
1756 sub->cached.newly_attached = 0;
1757
1758 /* wl_surface.damage */
1759 pixman_region32_union(&surface->damage, &surface->damage,
1760 &sub->cached.damage);
1761 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1762 0, 0,
1763 surface->geometry.width,
1764 surface->geometry.height);
1765 empty_region(&sub->cached.damage);
1766
1767 /* wl_surface.set_opaque_region */
1768 pixman_region32_init_rect(&opaque, 0, 0,
1769 surface->geometry.width,
1770 surface->geometry.height);
1771 pixman_region32_intersect(&opaque,
1772 &opaque, &sub->cached.opaque);
1773
1774 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1775 pixman_region32_copy(&surface->opaque, &opaque);
1776 weston_surface_geometry_dirty(surface);
1777 }
1778
1779 pixman_region32_fini(&opaque);
1780
1781 /* wl_surface.set_input_region */
1782 pixman_region32_fini(&surface->input);
1783 pixman_region32_init_rect(&surface->input, 0, 0,
1784 surface->geometry.width,
1785 surface->geometry.height);
1786 pixman_region32_intersect(&surface->input,
1787 &surface->input, &sub->cached.input);
1788
1789 /* wl_surface.frame */
1790 wl_list_insert_list(&surface->frame_callback_list,
1791 &sub->cached.frame_callback_list);
1792 wl_list_init(&sub->cached.frame_callback_list);
1793
1794 weston_surface_commit_subsurface_order(surface);
1795
1796 weston_surface_schedule_repaint(surface);
1797
1798 sub->cached.has_data = 0;
1799}
1800
1801static void
1802weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
1803{
1804 struct weston_surface *surface = sub->surface;
1805
1806 /*
1807 * If this commit would cause the surface to move by the
1808 * attach(dx, dy) parameters, the old damage region must be
1809 * translated to correspond to the new surface coordinate system
1810 * origin.
1811 */
1812 pixman_region32_translate(&sub->cached.damage,
1813 -surface->pending.sx, -surface->pending.sy);
1814 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
1815 &surface->pending.damage);
1816 empty_region(&surface->pending.damage);
1817
1818 if (surface->pending.newly_attached) {
1819 sub->cached.newly_attached = 1;
1820 weston_buffer_reference(&sub->cached.buffer_ref,
1821 surface->pending.buffer);
1822 }
1823 sub->cached.sx += surface->pending.sx;
1824 sub->cached.sy += surface->pending.sy;
1825 surface->pending.sx = 0;
1826 surface->pending.sy = 0;
1827 surface->pending.newly_attached = 0;
1828
1829 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001830 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001831
1832 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
1833
1834 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
1835
1836 wl_list_insert_list(&sub->cached.frame_callback_list,
1837 &surface->pending.frame_callback_list);
1838 wl_list_init(&surface->pending.frame_callback_list);
1839
1840 sub->cached.has_data = 1;
1841}
1842
1843static int
1844weston_subsurface_is_synchronized(struct weston_subsurface *sub)
1845{
1846 while (sub) {
1847 if (sub->synchronized)
1848 return 1;
1849
1850 if (!sub->parent)
1851 return 0;
1852
1853 sub = weston_surface_to_subsurface(sub->parent);
1854 }
1855
1856 return 0;
1857}
1858
1859static void
1860weston_subsurface_commit(struct weston_subsurface *sub)
1861{
1862 struct weston_surface *surface = sub->surface;
1863 struct weston_subsurface *tmp;
1864
1865 /* Recursive check for effectively synchronized. */
1866 if (weston_subsurface_is_synchronized(sub)) {
1867 weston_subsurface_commit_to_cache(sub);
1868 } else {
1869 if (sub->cached.has_data) {
1870 /* flush accumulated state from cache */
1871 weston_subsurface_commit_to_cache(sub);
1872 weston_subsurface_commit_from_cache(sub);
1873 } else {
1874 weston_surface_commit(surface);
1875 }
1876
1877 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1878 if (tmp->surface != surface)
1879 weston_subsurface_parent_commit(tmp, 0);
1880 }
1881 }
1882}
1883
1884static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001885weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001886{
1887 struct weston_surface *surface = sub->surface;
1888 struct weston_subsurface *tmp;
1889
Pekka Paalanene67858b2013-04-25 13:57:42 +03001890 /* From now on, commit_from_cache the whole sub-tree, regardless of
1891 * the synchronized mode of each child. This sub-surface or some
1892 * of its ancestors were synchronized, so we are synchronized
1893 * all the way down.
1894 */
1895
1896 if (sub->cached.has_data)
1897 weston_subsurface_commit_from_cache(sub);
1898
1899 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1900 if (tmp->surface != surface)
1901 weston_subsurface_parent_commit(tmp, 1);
1902 }
1903}
1904
1905static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001906weston_subsurface_parent_commit(struct weston_subsurface *sub,
1907 int parent_is_synchronized)
1908{
1909 if (sub->position.set) {
1910 weston_surface_set_position(sub->surface,
1911 sub->position.x, sub->position.y);
1912 sub->position.set = 0;
1913 }
1914
1915 if (parent_is_synchronized || sub->synchronized)
1916 weston_subsurface_synchronized_commit(sub);
1917}
1918
1919static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001920subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
1921 int32_t width, int32_t height)
1922{
1923 struct weston_compositor *compositor = surface->compositor;
1924
1925 weston_surface_configure(surface,
1926 surface->geometry.x + dx,
1927 surface->geometry.y + dy,
1928 width, height);
1929
1930 /* No need to check parent mappedness, because if parent is not
1931 * mapped, parent is not in a visible layer, so this sub-surface
1932 * will not be drawn either.
1933 */
1934 if (!weston_surface_is_mapped(surface)) {
1935 wl_list_init(&surface->layer_link);
1936
1937 /* Cannot call weston_surface_update_transform(),
1938 * because that would call it also for the parent surface,
1939 * which might not be mapped yet. That would lead to
1940 * inconsistent state, where the window could never be
1941 * mapped.
1942 *
1943 * Instead just assing any output, to make
1944 * weston_surface_is_mapped() return true, so that when the
1945 * parent surface does get mapped, this one will get
1946 * included, too. See surface_list_add().
1947 */
1948 assert(!wl_list_empty(&compositor->output_list));
1949 surface->output = container_of(compositor->output_list.next,
1950 struct weston_output, link);
1951 }
1952}
1953
1954static struct weston_subsurface *
1955weston_surface_to_subsurface(struct weston_surface *surface)
1956{
1957 if (surface->configure == subsurface_configure)
1958 return surface->configure_private;
1959
1960 return NULL;
1961}
1962
Pekka Paalanen01388e22013-04-25 13:57:44 +03001963WL_EXPORT struct weston_surface *
1964weston_surface_get_main_surface(struct weston_surface *surface)
1965{
1966 struct weston_subsurface *sub;
1967
1968 while (surface && (sub = weston_surface_to_subsurface(surface)))
1969 surface = sub->parent;
1970
1971 return surface;
1972}
1973
Pekka Paalanene67858b2013-04-25 13:57:42 +03001974static void
1975subsurface_set_position(struct wl_client *client,
1976 struct wl_resource *resource, int32_t x, int32_t y)
1977{
1978 struct weston_subsurface *sub = resource->data;
1979
1980 if (!sub)
1981 return;
1982
1983 sub->position.x = x;
1984 sub->position.y = y;
1985 sub->position.set = 1;
1986}
1987
1988static struct weston_subsurface *
1989subsurface_from_surface(struct weston_surface *surface)
1990{
1991 struct weston_subsurface *sub;
1992
1993 sub = weston_surface_to_subsurface(surface);
1994 if (sub)
1995 return sub;
1996
1997 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
1998 if (sub->surface == surface)
1999 return sub;
2000
2001 return NULL;
2002}
2003
2004static struct weston_subsurface *
2005subsurface_sibling_check(struct weston_subsurface *sub,
2006 struct weston_surface *surface,
2007 const char *request)
2008{
2009 struct weston_subsurface *sibling;
2010
2011 sibling = subsurface_from_surface(surface);
2012
2013 if (!sibling) {
2014 wl_resource_post_error(sub->resource,
2015 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2016 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002017 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002018 return NULL;
2019 }
2020
2021 if (sibling->parent != sub->parent) {
2022 wl_resource_post_error(sub->resource,
2023 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2024 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002025 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002026 return NULL;
2027 }
2028
2029 return sibling;
2030}
2031
2032static void
2033subsurface_place_above(struct wl_client *client,
2034 struct wl_resource *resource,
2035 struct wl_resource *sibling_resource)
2036{
2037 struct weston_subsurface *sub = resource->data;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002038 struct weston_surface *surface =
2039 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002040 struct weston_subsurface *sibling;
2041
2042 if (!sub)
2043 return;
2044
2045 sibling = subsurface_sibling_check(sub, surface, "place_above");
2046 if (!sibling)
2047 return;
2048
2049 wl_list_remove(&sub->parent_link_pending);
2050 wl_list_insert(sibling->parent_link_pending.prev,
2051 &sub->parent_link_pending);
2052}
2053
2054static void
2055subsurface_place_below(struct wl_client *client,
2056 struct wl_resource *resource,
2057 struct wl_resource *sibling_resource)
2058{
2059 struct weston_subsurface *sub = resource->data;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002060 struct weston_surface *surface =
2061 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002062 struct weston_subsurface *sibling;
2063
2064 if (!sub)
2065 return;
2066
2067 sibling = subsurface_sibling_check(sub, surface, "place_below");
2068 if (!sibling)
2069 return;
2070
2071 wl_list_remove(&sub->parent_link_pending);
2072 wl_list_insert(&sibling->parent_link_pending,
2073 &sub->parent_link_pending);
2074}
2075
2076static void
2077subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2078{
2079 struct weston_subsurface *sub = resource->data;
2080
2081 if (sub)
2082 sub->synchronized = 1;
2083}
2084
2085static void
2086subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2087{
2088 struct weston_subsurface *sub = resource->data;
2089
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002090 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002091 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002092
2093 /* If sub became effectively desynchronized, flush. */
2094 if (!weston_subsurface_is_synchronized(sub))
2095 weston_subsurface_synchronized_commit(sub);
2096 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002097}
2098
2099static void
2100weston_subsurface_cache_init(struct weston_subsurface *sub)
2101{
2102 pixman_region32_init(&sub->cached.damage);
2103 pixman_region32_init(&sub->cached.opaque);
2104 pixman_region32_init(&sub->cached.input);
2105 wl_list_init(&sub->cached.frame_callback_list);
2106 sub->cached.buffer_ref.buffer = NULL;
2107}
2108
2109static void
2110weston_subsurface_cache_fini(struct weston_subsurface *sub)
2111{
2112 struct weston_frame_callback *cb, *tmp;
2113
2114 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002115 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002116
2117 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2118 pixman_region32_fini(&sub->cached.damage);
2119 pixman_region32_fini(&sub->cached.opaque);
2120 pixman_region32_fini(&sub->cached.input);
2121}
2122
2123static void
2124weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2125{
2126 wl_list_remove(&sub->parent_link);
2127 wl_list_remove(&sub->parent_link_pending);
2128 wl_list_remove(&sub->parent_destroy_listener.link);
2129 sub->parent = NULL;
2130}
2131
2132static void
2133weston_subsurface_destroy(struct weston_subsurface *sub);
2134
2135static void
2136subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2137{
2138 struct weston_subsurface *sub =
2139 container_of(listener, struct weston_subsurface,
2140 surface_destroy_listener);
2141 assert(data == &sub->surface->resource);
2142
2143 /* The protocol object (wl_resource) is left inert. */
2144 if (sub->resource)
2145 sub->resource->data = NULL;
2146
2147 weston_subsurface_destroy(sub);
2148}
2149
2150static void
2151subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2152{
2153 struct weston_subsurface *sub =
2154 container_of(listener, struct weston_subsurface,
2155 parent_destroy_listener);
2156 assert(data == &sub->parent->resource);
2157 assert(sub->surface != sub->parent);
2158
2159 if (weston_surface_is_mapped(sub->surface))
2160 weston_surface_unmap(sub->surface);
2161
2162 weston_subsurface_unlink_parent(sub);
2163}
2164
2165static void
2166subsurface_resource_destroy(struct wl_resource *resource)
2167{
2168 struct weston_subsurface *sub = resource->data;
2169
2170 if (sub)
2171 weston_subsurface_destroy(sub);
2172
2173 free(resource);
2174}
2175
2176static void
2177subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2178{
2179 wl_resource_destroy(resource);
2180}
2181
2182static void
2183weston_subsurface_link_parent(struct weston_subsurface *sub,
2184 struct weston_surface *parent)
2185{
2186 sub->parent = parent;
2187 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002188 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002189 &sub->parent_destroy_listener);
2190
2191 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2192 wl_list_insert(&parent->subsurface_list_pending,
2193 &sub->parent_link_pending);
2194}
2195
2196static void
2197weston_subsurface_link_surface(struct weston_subsurface *sub,
2198 struct weston_surface *surface)
2199{
2200 sub->surface = surface;
2201 sub->surface_destroy_listener.notify =
2202 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002203 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002204 &sub->surface_destroy_listener);
2205}
2206
2207static void
2208weston_subsurface_destroy(struct weston_subsurface *sub)
2209{
2210 assert(sub->surface);
2211
2212 if (sub->resource) {
2213 assert(weston_surface_to_subsurface(sub->surface) == sub);
2214 assert(sub->parent_destroy_listener.notify ==
2215 subsurface_handle_parent_destroy);
2216
2217 weston_surface_set_transform_parent(sub->surface, NULL);
2218 if (sub->parent)
2219 weston_subsurface_unlink_parent(sub);
2220
2221 weston_subsurface_cache_fini(sub);
2222
2223 sub->surface->configure = NULL;
2224 sub->surface->configure_private = NULL;
2225 } else {
2226 /* the dummy weston_subsurface for the parent itself */
2227 assert(sub->parent_destroy_listener.notify == NULL);
2228 wl_list_remove(&sub->parent_link);
2229 wl_list_remove(&sub->parent_link_pending);
2230 }
2231
2232 wl_list_remove(&sub->surface_destroy_listener.link);
2233 free(sub);
2234}
2235
2236static const struct wl_subsurface_interface subsurface_implementation = {
2237 subsurface_destroy,
2238 subsurface_set_position,
2239 subsurface_place_above,
2240 subsurface_place_below,
2241 subsurface_set_sync,
2242 subsurface_set_desync
2243};
2244
2245static struct weston_subsurface *
2246weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2247 struct weston_surface *parent)
2248{
2249 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002250 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002251
2252 sub = calloc(1, sizeof *sub);
2253 if (!sub)
2254 return NULL;
2255
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002256 sub->resource = wl_client_add_object(client,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002257 &wl_subsurface_interface,
2258 &subsurface_implementation,
2259 id, sub);
2260 if (!sub->resource) {
2261 free(sub);
2262 return NULL;
2263 }
2264
2265 sub->resource->destroy = subsurface_resource_destroy;
2266 weston_subsurface_link_surface(sub, surface);
2267 weston_subsurface_link_parent(sub, parent);
2268 weston_subsurface_cache_init(sub);
2269 sub->synchronized = 1;
2270 weston_surface_set_transform_parent(surface, parent);
2271
2272 return sub;
2273}
2274
2275/* Create a dummy subsurface for having the parent itself in its
2276 * sub-surface lists. Makes stacking order manipulation easy.
2277 */
2278static struct weston_subsurface *
2279weston_subsurface_create_for_parent(struct weston_surface *parent)
2280{
2281 struct weston_subsurface *sub;
2282
2283 sub = calloc(1, sizeof *sub);
2284 if (!sub)
2285 return NULL;
2286
2287 weston_subsurface_link_surface(sub, parent);
2288 sub->parent = parent;
2289 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2290 wl_list_insert(&parent->subsurface_list_pending,
2291 &sub->parent_link_pending);
2292
2293 return sub;
2294}
2295
2296static void
2297subcompositor_get_subsurface(struct wl_client *client,
2298 struct wl_resource *resource,
2299 uint32_t id,
2300 struct wl_resource *surface_resource,
2301 struct wl_resource *parent_resource)
2302{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002303 struct weston_surface *surface =
2304 wl_resource_get_user_data(surface_resource);
2305 struct weston_surface *parent =
2306 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002307 struct weston_subsurface *sub;
2308 static const char where[] = "get_subsurface: wl_subsurface@";
2309
2310 if (surface == parent) {
2311 wl_resource_post_error(resource,
2312 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2313 "%s%d: wl_surface@%d cannot be its own parent",
2314 where, id, surface_resource->object.id);
2315 return;
2316 }
2317
2318 if (weston_surface_to_subsurface(surface)) {
2319 wl_resource_post_error(resource,
2320 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2321 "%s%d: wl_surface@%d is already a sub-surface",
2322 where, id, surface_resource->object.id);
2323 return;
2324 }
2325
2326 if (surface->configure) {
2327 wl_resource_post_error(resource,
2328 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2329 "%s%d: wl_surface@%d already has a role",
2330 where, id, surface_resource->object.id);
2331 return;
2332 }
2333
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002334 if (weston_surface_get_main_surface(parent) == surface) {
2335 wl_resource_post_error(resource,
2336 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2337 "%s%d: wl_surface@%d is an ancestor of parent",
2338 where, id, surface_resource->object.id);
2339 return;
2340 }
2341
Pekka Paalanene67858b2013-04-25 13:57:42 +03002342 /* make sure the parent is in its own list */
2343 if (wl_list_empty(&parent->subsurface_list)) {
2344 if (!weston_subsurface_create_for_parent(parent)) {
2345 wl_resource_post_no_memory(resource);
2346 return;
2347 }
2348 }
2349
2350 sub = weston_subsurface_create(id, surface, parent);
2351 if (!sub) {
2352 wl_resource_post_no_memory(resource);
2353 return;
2354 }
2355
2356 surface->configure = subsurface_configure;
2357 surface->configure_private = sub;
2358}
2359
2360static void
2361subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2362{
2363 wl_resource_destroy(resource);
2364}
2365
2366static const struct wl_subcompositor_interface subcompositor_interface = {
2367 subcompositor_destroy,
2368 subcompositor_get_subsurface
2369};
2370
2371static void
2372bind_subcompositor(struct wl_client *client,
2373 void *data, uint32_t version, uint32_t id)
2374{
2375 struct weston_compositor *compositor = data;
2376
2377 wl_client_add_object(client, &wl_subcompositor_interface,
2378 &subcompositor_interface, id, compositor);
2379}
2380
2381static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002382weston_compositor_dpms(struct weston_compositor *compositor,
2383 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002384{
2385 struct weston_output *output;
2386
2387 wl_list_for_each(output, &compositor->output_list, link)
2388 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002389 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002390}
2391
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002392WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002393weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002394{
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002395 switch (compositor->state) {
2396 case WESTON_COMPOSITOR_SLEEPING:
2397 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2398 /* fall through */
2399 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002400 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002401 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002402 /* fall through */
2403 default:
2404 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2405 wl_event_source_timer_update(compositor->idle_source,
2406 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002407 }
2408}
2409
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002410WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002411weston_compositor_offscreen(struct weston_compositor *compositor)
2412{
2413 switch (compositor->state) {
2414 case WESTON_COMPOSITOR_OFFSCREEN:
2415 return;
2416 case WESTON_COMPOSITOR_SLEEPING:
2417 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2418 /* fall through */
2419 default:
2420 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2421 wl_event_source_timer_update(compositor->idle_source, 0);
2422 }
2423}
2424
2425WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002426weston_compositor_sleep(struct weston_compositor *compositor)
2427{
2428 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2429 return;
2430
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002431 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002432 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2433 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2434}
2435
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002436static int
2437idle_handler(void *data)
2438{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002439 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002440
2441 if (compositor->idle_inhibit)
2442 return 1;
2443
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002444 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002445 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002446
2447 return 1;
2448}
2449
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002450WL_EXPORT void
2451weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2452{
2453 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002454 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002455 plane->x = x;
2456 plane->y = y;
2457}
2458
2459WL_EXPORT void
2460weston_plane_release(struct weston_plane *plane)
2461{
2462 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002463 pixman_region32_fini(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002464}
2465
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002466WL_EXPORT void
2467weston_compositor_stack_plane(struct weston_compositor *ec,
2468 struct weston_plane *plane,
2469 struct weston_plane *above)
2470{
2471 if (above)
2472 wl_list_insert(above->link.prev, &plane->link);
2473 else
2474 wl_list_insert(&ec->plane_list, &plane->link);
2475}
2476
Casey Dahlin9074db52012-04-19 22:50:09 -04002477static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002478{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002479 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002480 free(resource);
2481}
2482
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002483static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002484bind_output(struct wl_client *client,
2485 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002486{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002487 struct weston_output *output = data;
2488 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002489 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002490
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002491 resource = wl_client_add_object(client,
2492 &wl_output_interface, NULL, id, data);
2493
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002494 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
2495 wl_resource_set_destructor(resource, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002496
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002497 wl_output_send_geometry(resource,
2498 output->x,
2499 output->y,
2500 output->mm_width,
2501 output->mm_height,
2502 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002503 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002504 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002505 if (version >= 2)
2506 wl_output_send_scale(resource,
2507 output->scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002508
2509 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002510 wl_output_send_mode(resource,
2511 mode->flags,
2512 mode->width,
2513 mode->height,
2514 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002515 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002516
2517 if (version >= 2)
2518 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002519}
2520
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002521WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002522weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002523{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002524 struct weston_compositor *c = output->compositor;
2525
Richard Hughes64ddde12013-05-01 21:52:10 +01002526 wl_signal_emit(&output->destroy_signal, output);
2527
Richard Hughesafe690c2013-05-02 10:10:04 +01002528 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002529 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002530 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002531 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002532
2533 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002534}
2535
Scott Moreau1bad5db2012-08-18 01:04:05 -06002536static void
2537weston_output_compute_transform(struct weston_output *output)
2538{
2539 struct weston_matrix transform;
2540 int flip;
2541
2542 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002543 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002544
2545 switch(output->transform) {
2546 case WL_OUTPUT_TRANSFORM_FLIPPED:
2547 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2548 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2549 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002550 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002551 flip = -1;
2552 break;
2553 default:
2554 flip = 1;
2555 break;
2556 }
2557
2558 switch(output->transform) {
2559 case WL_OUTPUT_TRANSFORM_NORMAL:
2560 case WL_OUTPUT_TRANSFORM_FLIPPED:
2561 transform.d[0] = flip;
2562 transform.d[1] = 0;
2563 transform.d[4] = 0;
2564 transform.d[5] = 1;
2565 break;
2566 case WL_OUTPUT_TRANSFORM_90:
2567 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2568 transform.d[0] = 0;
2569 transform.d[1] = -flip;
2570 transform.d[4] = 1;
2571 transform.d[5] = 0;
2572 break;
2573 case WL_OUTPUT_TRANSFORM_180:
2574 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2575 transform.d[0] = -flip;
2576 transform.d[1] = 0;
2577 transform.d[4] = 0;
2578 transform.d[5] = -1;
2579 break;
2580 case WL_OUTPUT_TRANSFORM_270:
2581 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2582 transform.d[0] = 0;
2583 transform.d[1] = flip;
2584 transform.d[4] = -1;
2585 transform.d[5] = 0;
2586 break;
2587 default:
2588 break;
2589 }
2590
2591 weston_matrix_multiply(&output->matrix, &transform);
2592}
2593
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002594WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002595weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002596{
Scott Moreau850ca422012-05-21 15:21:25 -06002597 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002598 struct weston_matrix camera;
2599 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002600
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002601 weston_matrix_init(&output->matrix);
2602 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002603 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2604 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002605
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002606 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002607 2.0 / (output->width + output->border.left + output->border.right),
2608 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2609
2610 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002611
Scott Moreauccbf29d2012-02-22 14:21:41 -07002612 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002613 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002614 weston_matrix_init(&camera);
2615 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002616 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002617 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002618 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002619 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002620 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002621 weston_matrix_multiply(&output->matrix, &modelview);
2622 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002623
Scott Moreauccbf29d2012-02-22 14:21:41 -07002624 output->dirty = 0;
2625}
2626
Scott Moreau1bad5db2012-08-18 01:04:05 -06002627static void
Alexander Larsson0b135062013-05-28 16:23:36 +02002628weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002629{
2630 output->transform = transform;
2631
2632 switch (transform) {
2633 case WL_OUTPUT_TRANSFORM_90:
2634 case WL_OUTPUT_TRANSFORM_270:
2635 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2636 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2637 /* Swap width and height */
2638 output->width = output->current->height;
2639 output->height = output->current->width;
2640 break;
2641 case WL_OUTPUT_TRANSFORM_NORMAL:
2642 case WL_OUTPUT_TRANSFORM_180:
2643 case WL_OUTPUT_TRANSFORM_FLIPPED:
2644 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2645 output->width = output->current->width;
2646 output->height = output->current->height;
2647 break;
2648 default:
2649 break;
2650 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06002651
Alexander Larsson4ea95522013-05-22 14:41:37 +02002652 output->scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02002653 output->width /= scale;
2654 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002655}
2656
Scott Moreauccbf29d2012-02-22 14:21:41 -07002657WL_EXPORT void
2658weston_output_move(struct weston_output *output, int x, int y)
2659{
2660 output->x = x;
2661 output->y = y;
2662
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002663 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002664 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002665 output->width,
2666 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002667}
2668
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002669WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002670weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02002671 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002672 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002673{
2674 output->compositor = c;
2675 output->x = x;
2676 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002677 output->border.top = 0;
2678 output->border.bottom = 0;
2679 output->border.left = 0;
2680 output->border.right = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02002681 output->mm_width = mm_width;
2682 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002683 output->dirty = 1;
Alexander Larssone32c3762013-05-28 16:23:37 +02002684 output->origin_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002685
Alexander Larsson0b135062013-05-28 16:23:36 +02002686 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06002687 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002688
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002689 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002690 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002691
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002692 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01002693 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002694 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002695 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002696
Casey Dahlin58ba1372012-04-19 22:50:08 -04002697 output->id = ffs(~output->compositor->output_id_pool) - 1;
2698 output->compositor->output_id_pool |= 1 << output->id;
2699
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002700 output->global =
2701 wl_display_add_global(c->wl_display, &wl_output_interface,
2702 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01002703 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002704}
2705
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002706static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002707compositor_bind(struct wl_client *client,
2708 void *data, uint32_t version, uint32_t id)
2709{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002710 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002711
2712 wl_client_add_object(client, &wl_compositor_interface,
2713 &compositor_interface, id, compositor);
2714}
2715
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002716static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002717log_uname(void)
2718{
2719 struct utsname usys;
2720
2721 uname(&usys);
2722
2723 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2724 usys.version, usys.machine);
2725}
2726
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002727WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02002728weston_environment_get_fd(const char *env)
2729{
2730 char *e, *end;
2731 int fd, flags;
2732
2733 e = getenv(env);
2734 if (!e)
2735 return -1;
2736 fd = strtol(e, &end, 0);
2737 if (*end != '\0')
2738 return -1;
2739
2740 flags = fcntl(fd, F_GETFD);
2741 if (flags == -1)
2742 return -1;
2743
2744 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
2745 unsetenv(env);
2746
2747 return fd;
2748}
2749
2750WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002751weston_compositor_init(struct weston_compositor *ec,
2752 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002753 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002754 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002755{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002756 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002757 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002758 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002759
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002760 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002761 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002762 wl_signal_init(&ec->destroy_signal);
2763 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002764 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002765 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002766 wl_signal_init(&ec->idle_signal);
2767 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002768 wl_signal_init(&ec->show_input_panel_signal);
2769 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02002770 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002771 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01002772 wl_signal_init(&ec->output_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002773 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002774
Casey Dahlin58ba1372012-04-19 22:50:08 -04002775 ec->output_id_pool = 0;
2776
Kristian Høgsberga8873122011-11-23 10:39:34 -05002777 if (!wl_display_add_global(display, &wl_compositor_interface,
2778 ec, compositor_bind))
2779 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002780
Pekka Paalanene67858b2013-04-25 13:57:42 +03002781 if (!wl_display_add_global(display, &wl_subcompositor_interface,
2782 ec, bind_subcompositor))
2783 return -1;
2784
Daniel Stone725c2c32012-06-22 14:04:36 +01002785 wl_list_init(&ec->surface_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002786 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002787 wl_list_init(&ec->layer_list);
2788 wl_list_init(&ec->seat_list);
2789 wl_list_init(&ec->output_list);
2790 wl_list_init(&ec->key_binding_list);
2791 wl_list_init(&ec->button_binding_list);
2792 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002793 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002794
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002795 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002796 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002797
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002798 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
2799 weston_config_section_get_string(s, "keymap_rules",
2800 (char **) &xkb_names.rules, NULL);
2801 weston_config_section_get_string(s, "keymap_model",
2802 (char **) &xkb_names.model, NULL);
2803 weston_config_section_get_string(s, "keymap_layout",
2804 (char **) &xkb_names.layout, NULL);
2805 weston_config_section_get_string(s, "keymap_variant",
2806 (char **) &xkb_names.variant, NULL);
2807 weston_config_section_get_string(s, "keymap_options",
2808 (char **) &xkb_names.options, NULL);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002809 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
2810 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01002811
2812 ec->ping_handler = NULL;
2813
2814 screenshooter_create(ec);
2815 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002816 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002817
2818 wl_data_device_manager_init(ec->wl_display);
2819
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002820 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002821
Daniel Stone725c2c32012-06-22 14:04:36 +01002822 loop = wl_display_get_event_loop(ec->wl_display);
2823 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2824 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2825
2826 ec->input_loop = wl_event_loop_create();
2827
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002828 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2829 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2830
2831 weston_compositor_schedule_repaint(ec);
2832
Daniel Stone725c2c32012-06-22 14:04:36 +01002833 return 0;
2834}
2835
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002836WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002837weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002838{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002839 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002840
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002841 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002842 if (ec->input_loop_source)
2843 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002844
Matt Roper361d2ad2011-08-29 13:52:23 -07002845 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002846 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002847 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002848
Daniel Stone325fc2d2012-05-30 16:31:58 +01002849 weston_binding_list_destroy_all(&ec->key_binding_list);
2850 weston_binding_list_destroy_all(&ec->button_binding_list);
2851 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002852 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002853
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002854 weston_plane_release(&ec->primary_plane);
2855
Jonas Ådahlc97af922012-03-28 22:36:09 +02002856 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07002857
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002858 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07002859}
2860
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05002861WL_EXPORT void
2862weston_version(int *major, int *minor, int *micro)
2863{
2864 *major = WESTON_VERSION_MAJOR;
2865 *minor = WESTON_VERSION_MINOR;
2866 *micro = WESTON_VERSION_MICRO;
2867}
2868
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002869static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002870 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002871 const char *desc;
2872} capability_strings[] = {
2873 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002874 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002875};
2876
2877static void
2878weston_compositor_log_capabilities(struct weston_compositor *compositor)
2879{
2880 unsigned i;
2881 int yes;
2882
2883 weston_log("Compositor capabilities:\n");
2884 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
2885 yes = compositor->capabilities & capability_strings[i].bit;
2886 weston_log_continue(STAMP_SPACE "%s %s\n",
2887 capability_strings[i].desc,
2888 yes ? "yes" : "no");
2889 }
2890}
2891
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002892static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002893{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002894 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002895
Martin Minarik6d118362012-06-07 18:01:59 +02002896 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002897 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002898
2899 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002900}
2901
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002902#ifdef HAVE_LIBUNWIND
2903
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002904static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002905print_backtrace(void)
2906{
2907 unw_cursor_t cursor;
2908 unw_context_t context;
2909 unw_word_t off;
2910 unw_proc_info_t pip;
2911 int ret, i = 0;
2912 char procname[256];
2913 const char *filename;
2914 Dl_info dlinfo;
2915
2916 pip.unwind_info = NULL;
2917 ret = unw_getcontext(&context);
2918 if (ret) {
2919 weston_log("unw_getcontext: %d\n", ret);
2920 return;
2921 }
2922
2923 ret = unw_init_local(&cursor, &context);
2924 if (ret) {
2925 weston_log("unw_init_local: %d\n", ret);
2926 return;
2927 }
2928
2929 ret = unw_step(&cursor);
2930 while (ret > 0) {
2931 ret = unw_get_proc_info(&cursor, &pip);
2932 if (ret) {
2933 weston_log("unw_get_proc_info: %d\n", ret);
2934 break;
2935 }
2936
2937 ret = unw_get_proc_name(&cursor, procname, 256, &off);
2938 if (ret && ret != -UNW_ENOMEM) {
2939 if (ret != -UNW_EUNSPEC)
2940 weston_log("unw_get_proc_name: %d\n", ret);
2941 procname[0] = '?';
2942 procname[1] = 0;
2943 }
2944
2945 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
2946 *dlinfo.dli_fname)
2947 filename = dlinfo.dli_fname;
2948 else
2949 filename = "?";
2950
2951 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
2952 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
2953
2954 ret = unw_step(&cursor);
2955 if (ret < 0)
2956 weston_log("unw_step: %d\n", ret);
2957 }
2958}
2959
2960#else
2961
2962static void
2963print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002964{
2965 void *buffer[32];
2966 int i, count;
2967 Dl_info info;
2968
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002969 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2970 for (i = 0; i < count; i++) {
2971 dladdr(buffer[i], &info);
2972 weston_log(" [%016lx] %s (%s)\n",
2973 (long) buffer[i],
2974 info.dli_sname ? info.dli_sname : "--",
2975 info.dli_fname);
2976 }
2977}
2978
2979#endif
2980
2981static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01002982on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002983{
Peter Maatmane5b42e42013-03-27 22:38:53 +01002984 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002985 * then call the backend restore function, which will switch
2986 * back to the vt we launched from or ungrab X etc and then
2987 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01002988 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002989 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01002990 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002991
Peter Maatmane5b42e42013-03-27 22:38:53 +01002992 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002993
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002994 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002995
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002996 segv_compositor->restore(segv_compositor);
2997
2998 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002999}
3000
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003001static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003002load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003003{
3004 char path[PATH_MAX];
3005 void *module, *init;
3006
3007 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003008 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003009 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003010 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003011
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003012 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3013 if (module) {
3014 weston_log("Module '%s' already loaded\n", path);
3015 dlclose(module);
3016 return NULL;
3017 }
3018
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003019 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003020 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003021 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003022 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003023 return NULL;
3024 }
3025
3026 init = dlsym(module, entrypoint);
3027 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003028 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003029 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003030 return NULL;
3031 }
3032
3033 return init;
3034}
3035
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003036static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003037load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003038 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003039{
3040 const char *p, *end;
3041 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003042 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003043 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003044
3045 if (modules == NULL)
3046 return 0;
3047
3048 p = modules;
3049 while (*p) {
3050 end = strchrnul(p, ',');
3051 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3052 module_init = load_module(buffer, "module_init");
3053 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003054 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003055 p = end;
3056 while (*p == ',')
3057 p++;
3058
3059 }
3060
3061 return 0;
3062}
3063
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003064static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003065 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3066
3067static const char xdg_wrong_message[] =
3068 "fatal: environment variable XDG_RUNTIME_DIR\n"
3069 "is set to \"%s\", which is not a directory.\n";
3070
3071static const char xdg_wrong_mode_message[] =
3072 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3073 "correctly. Unix access mode must be 0700 but is %o,\n"
3074 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003075 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003076
3077static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003078 "Refer to your distribution on how to get it, or\n"
3079 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3080 "on how to implement it.\n";
3081
Martin Minarik37032f82012-06-18 20:15:18 +02003082static void
3083verify_xdg_runtime_dir(void)
3084{
3085 char *dir = getenv("XDG_RUNTIME_DIR");
3086 struct stat s;
3087
3088 if (!dir) {
3089 weston_log(xdg_error_message);
3090 weston_log_continue(xdg_detail_message);
3091 exit(EXIT_FAILURE);
3092 }
3093
3094 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3095 weston_log(xdg_wrong_message, dir);
3096 weston_log_continue(xdg_detail_message);
3097 exit(EXIT_FAILURE);
3098 }
3099
3100 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3101 weston_log(xdg_wrong_mode_message,
3102 dir, s.st_mode & 0777, s.st_uid);
3103 weston_log_continue(xdg_detail_message);
3104 }
3105}
3106
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003107static int
3108usage(int error_code)
3109{
3110 fprintf(stderr,
3111 "Usage: weston [OPTIONS]\n\n"
3112 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3113 "Weston supports multiple backends, and depending on which backend is in use\n"
3114 "different options will be accepted.\n\n"
3115
3116
3117 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003118 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003119 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003120 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3121 "\t\t\t\twayland-backend.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003122 " -S, --socket=NAME\tName of socket to listen on\n"
3123 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003124 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003125 " --log==FILE\t\tLog to the given file\n"
3126 " -h, --help\t\tThis help message\n\n");
3127
3128 fprintf(stderr,
3129 "Options for drm-backend.so:\n\n"
3130 " --connector=ID\tBring up only this connector\n"
3131 " --seat=SEAT\t\tThe seat that weston should run on\n"
3132 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003133 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003134 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3135
3136 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003137 "Options for fbdev-backend.so:\n\n"
3138 " --tty=TTY\t\tThe tty to use\n"
3139 " --device=DEVICE\tThe framebuffer device to use\n\n");
3140
3141 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003142 "Options for x11-backend.so:\n\n"
3143 " --width=WIDTH\t\tWidth of X window\n"
3144 " --height=HEIGHT\tHeight of X window\n"
3145 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003146 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003147 " --output-count=COUNT\tCreate multiple outputs\n"
3148 " --no-input\t\tDont create input devices\n\n");
3149
3150 fprintf(stderr,
3151 "Options for wayland-backend.so:\n\n"
3152 " --width=WIDTH\t\tWidth of Wayland surface\n"
3153 " --height=HEIGHT\tHeight of Wayland surface\n"
3154 " --display=DISPLAY\tWayland display to connect to\n\n");
3155
Pekka Paalanene31e0532013-05-22 18:03:07 +03003156#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3157 fprintf(stderr,
3158 "Options for rpi-backend.so:\n\n"
3159 " --tty=TTY\t\tThe tty to use\n"
3160 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3161 " --transform=TR\tThe output transformation, TR is one of:\n"
3162 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3163 "\n");
3164#endif
3165
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003166 exit(error_code);
3167}
3168
Peter Maatmane5b42e42013-03-27 22:38:53 +01003169static void
3170catch_signals(void)
3171{
3172 struct sigaction action;
3173
3174 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3175 action.sa_sigaction = on_caught_signal;
3176 sigemptyset(&action.sa_mask);
3177 sigaction(SIGSEGV, &action, NULL);
3178 sigaction(SIGABRT, &action, NULL);
3179}
3180
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003181int main(int argc, char *argv[])
3182{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003183 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003184 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003185 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003186 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003187 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003188 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003189 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003190 int *argc, char *argv[],
3191 struct weston_config *config);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003192 int i, config_fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003193 char *backend = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003194 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003195 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003196 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003197 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003198 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003199 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003200 struct weston_config *config;
3201 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003202
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003203 const struct weston_option core_options[] = {
3204 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3205 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3206 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003207 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003208 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003209 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003210 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003211 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003212
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003213 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003214
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003215 if (help)
3216 usage(EXIT_SUCCESS);
3217
Scott Moreau12245142012-08-29 15:15:58 -06003218 if (version) {
3219 printf(PACKAGE_STRING "\n");
3220 return EXIT_SUCCESS;
3221 }
3222
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003223 weston_log_file_open(log);
3224
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003225 weston_log("%s\n"
3226 STAMP_SPACE "%s\n"
3227 STAMP_SPACE "Bug reports to: %s\n"
3228 STAMP_SPACE "Build: %s\n",
3229 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003230 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003231 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003232
Martin Minarik37032f82012-06-18 20:15:18 +02003233 verify_xdg_runtime_dir();
3234
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003235 display = wl_display_create();
3236
Tiago Vignatti2116b892011-08-08 05:52:59 -07003237 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003238 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3239 display);
3240 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3241 display);
3242 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3243 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003244
3245 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003246 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3247 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003248
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003249 if (!backend) {
3250 if (getenv("WAYLAND_DISPLAY"))
3251 backend = "wayland-backend.so";
3252 else if (getenv("DISPLAY"))
3253 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003254 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003255 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003256 }
3257
Ossama Othmana50e6e42013-05-14 09:48:26 -07003258 config_fd = open_config_file("weston.ini");
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003259 config = weston_config_parse(config_fd);
3260 close(config_fd);
3261
3262 section = weston_config_get_section(config, "core", NULL, NULL);
3263 weston_config_section_get_string(section, "modules",
3264 &modules, "desktop-shell.so");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003265
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003266 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003267 if (!backend_init)
3268 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003269
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003270 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003271 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003272 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003273 exit(EXIT_FAILURE);
3274 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003275
Peter Maatmane5b42e42013-03-27 22:38:53 +01003276 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003277 segv_compositor = ec;
3278
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003279 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003280
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003281 setenv("WAYLAND_DISPLAY", socket_name, 1);
3282
Ossama Othmana50e6e42013-05-14 09:48:26 -07003283 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003284 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003285 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003286 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003287
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003288 for (i = 1; i < argc; i++)
3289 weston_log("fatal: unhandled option: %s\n", argv[i]);
3290 if (argc > 1) {
3291 ret = EXIT_FAILURE;
3292 goto out;
3293 }
3294
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003295 weston_compositor_log_capabilities(ec);
3296
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003297 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003298 weston_log("fatal: failed to add socket: %m\n");
3299 ret = EXIT_FAILURE;
3300 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003301 }
3302
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003303 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003304
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003305 wl_display_run(display);
3306
3307 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003308 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003309 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003310
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003311 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003312
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003313 for (i = ARRAY_LENGTH(signals); i;)
3314 wl_event_source_remove(signals[--i]);
3315
Daniel Stone855028f2012-05-01 20:37:10 +01003316 weston_compositor_xkb_destroy(ec);
3317
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003318 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003319 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003320
Martin Minarik19e6f262012-06-07 13:08:46 +02003321 weston_log_file_close();
3322
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003323 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003324}