blob: 38ef0958104b0fba9389dfe1a54e520332f22357 [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 struct wl_resource *
506find_resource_for_client(struct wl_list *list, struct wl_client *client)
507{
508 struct wl_resource *r;
509
510 wl_list_for_each(r, list, link) {
511 if (r->client == client)
512 return r;
513 }
514
515 return NULL;
516}
517
518static void
519weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
520{
521 uint32_t different = es->output_mask ^ mask;
522 uint32_t entered = mask & different;
523 uint32_t left = es->output_mask & different;
524 struct weston_output *output;
525 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500526 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400527
528 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500529 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400530 return;
531 if (different == 0)
532 return;
533
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500534 client = wl_resource_get_client(es->resource);
535
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400536 wl_list_for_each(output, &es->compositor->output_list, link) {
537 if (1 << output->id & different)
538 resource =
539 find_resource_for_client(&output->resource_list,
540 client);
541 if (resource == NULL)
542 continue;
543 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500544 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400545 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500546 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400547 }
548}
549
550static void
551weston_surface_assign_output(struct weston_surface *es)
552{
553 struct weston_compositor *ec = es->compositor;
554 struct weston_output *output, *new_output;
555 pixman_region32_t region;
556 uint32_t max, area, mask;
557 pixman_box32_t *e;
558
559 new_output = NULL;
560 max = 0;
561 mask = 0;
562 pixman_region32_init(&region);
563 wl_list_for_each(output, &ec->output_list, link) {
564 pixman_region32_intersect(&region, &es->transform.boundingbox,
565 &output->region);
566
567 e = pixman_region32_extents(&region);
568 area = (e->x2 - e->x1) * (e->y2 - e->y1);
569
570 if (area > 0)
571 mask |= 1 << output->id;
572
573 if (area >= max) {
574 new_output = output;
575 max = area;
576 }
577 }
578 pixman_region32_fini(&region);
579
580 es->output = new_output;
581 weston_surface_update_output_mask(es, mask);
582}
583
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200584static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200585surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
586 int32_t width, int32_t height,
587 pixman_region32_t *bbox)
588{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200589 float min_x = HUGE_VALF, min_y = HUGE_VALF;
590 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200591 int32_t s[4][2] = {
592 { sx, sy },
593 { sx, sy + height },
594 { sx + width, sy },
595 { sx + width, sy + height }
596 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200597 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200598 int i;
599
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300600 if (width == 0 || height == 0) {
601 /* avoid rounding empty bbox to 1x1 */
602 pixman_region32_init(bbox);
603 return;
604 }
605
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200606 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200607 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400608 weston_surface_to_global_float(surface,
609 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200610 if (x < min_x)
611 min_x = x;
612 if (x > max_x)
613 max_x = x;
614 if (y < min_y)
615 min_y = y;
616 if (y > max_y)
617 max_y = y;
618 }
619
Pekka Paalanen219b9822012-02-08 15:38:37 +0200620 int_x = floorf(min_x);
621 int_y = floorf(min_y);
622 pixman_region32_init_rect(bbox, int_x, int_y,
623 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200624}
625
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200626static void
627weston_surface_update_transform_disable(struct weston_surface *surface)
628{
629 surface->transform.enabled = 0;
630
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200631 /* round off fractions when not transformed */
632 surface->geometry.x = roundf(surface->geometry.x);
633 surface->geometry.y = roundf(surface->geometry.y);
634
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500635 /* Otherwise identity matrix, but with x and y translation. */
636 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
637 surface->transform.position.matrix.d[12] = surface->geometry.x;
638 surface->transform.position.matrix.d[13] = surface->geometry.y;
639
640 surface->transform.matrix = surface->transform.position.matrix;
641
Kristian Høgsberg9bcaaeb2013-02-28 14:56:43 -0500642 surface->transform.inverse = surface->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500643 surface->transform.inverse.d[12] = -surface->geometry.x;
644 surface->transform.inverse.d[13] = -surface->geometry.y;
645
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200646 pixman_region32_init_rect(&surface->transform.boundingbox,
647 surface->geometry.x,
648 surface->geometry.y,
649 surface->geometry.width,
650 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500651
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400652 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500653 pixman_region32_copy(&surface->transform.opaque,
654 &surface->opaque);
655 pixman_region32_translate(&surface->transform.opaque,
656 surface->geometry.x,
657 surface->geometry.y);
658 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200659}
660
661static int
662weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200663{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200664 struct weston_surface *parent = surface->geometry.parent;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200665 struct weston_matrix *matrix = &surface->transform.matrix;
666 struct weston_matrix *inverse = &surface->transform.inverse;
667 struct weston_transform *tform;
668
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200669 surface->transform.enabled = 1;
670
671 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300672 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200673 surface->transform.position.matrix.d[12] = surface->geometry.x;
674 surface->transform.position.matrix.d[13] = surface->geometry.y;
675
676 weston_matrix_init(matrix);
677 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
678 weston_matrix_multiply(matrix, &tform->matrix);
679
Pekka Paalanen483243f2013-03-08 14:56:50 +0200680 if (parent)
681 weston_matrix_multiply(matrix, &parent->transform.matrix);
682
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200683 if (weston_matrix_invert(inverse, matrix) < 0) {
684 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200685 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200686 " transformation not invertible.\n", surface);
687 return -1;
688 }
689
690 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
691 surface->geometry.height,
692 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500693
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200694 return 0;
695}
696
697WL_EXPORT void
698weston_surface_update_transform(struct weston_surface *surface)
699{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200700 struct weston_surface *parent = surface->geometry.parent;
701
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200702 if (!surface->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200703 return;
704
Pekka Paalanen483243f2013-03-08 14:56:50 +0200705 if (parent)
706 weston_surface_update_transform(parent);
707
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200708 surface->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200709
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500710 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200711
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200712 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500713 pixman_region32_fini(&surface->transform.opaque);
714 pixman_region32_init(&surface->transform.opaque);
715
Pekka Paalanencd403622012-01-25 13:37:39 +0200716 /* transform.position is always in transformation_list */
717 if (surface->geometry.transformation_list.next ==
718 &surface->transform.position.link &&
719 surface->geometry.transformation_list.prev ==
Pekka Paalanen483243f2013-03-08 14:56:50 +0200720 &surface->transform.position.link &&
721 !parent) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200722 weston_surface_update_transform_disable(surface);
723 } else {
724 if (weston_surface_update_transform_enable(surface) < 0)
725 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200726 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200727
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400728 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200729
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300730 weston_surface_assign_output(surface);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300731
732 wl_signal_emit(&surface->compositor->transform_signal, surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200733}
734
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200735WL_EXPORT void
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200736weston_surface_geometry_dirty(struct weston_surface *surface)
737{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200738 struct weston_surface *child;
739
740 /*
741 * The invariant: if surface->geometry.dirty, then all surfaces
742 * in surface->geometry.child_list have geometry.dirty too.
743 * Corollary: if not parent->geometry.dirty, then all ancestors
744 * are not dirty.
745 */
746
747 if (surface->transform.dirty)
748 return;
749
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200750 surface->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200751
752 wl_list_for_each(child, &surface->geometry.child_list,
753 geometry.parent_link)
754 weston_surface_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200755}
756
757WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100758weston_surface_to_global_fixed(struct weston_surface *surface,
759 wl_fixed_t sx, wl_fixed_t sy,
760 wl_fixed_t *x, wl_fixed_t *y)
761{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200762 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100763
764 weston_surface_to_global_float(surface,
765 wl_fixed_to_double(sx),
766 wl_fixed_to_double(sy),
767 &xf, &yf);
768 *x = wl_fixed_from_double(xf);
769 *y = wl_fixed_from_double(yf);
770}
771
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400772WL_EXPORT void
773weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200774 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200775{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200776 if (surface->transform.enabled) {
777 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
778
779 weston_matrix_transform(&surface->transform.inverse, &v);
780
781 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200782 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200783 "weston_surface_from_global(), divisor = %g\n",
784 v.f[3]);
785 *sx = 0;
786 *sy = 0;
787 return;
788 }
789
Pekka Paalanencd403622012-01-25 13:37:39 +0200790 *sx = v.f[0] / v.f[3];
791 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200792 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200793 *sx = x - surface->geometry.x;
794 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200795 }
796}
797
798WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100799weston_surface_from_global_fixed(struct weston_surface *surface,
800 wl_fixed_t x, wl_fixed_t y,
801 wl_fixed_t *sx, wl_fixed_t *sy)
802{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200803 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100804
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400805 weston_surface_from_global_float(surface,
806 wl_fixed_to_double(x),
807 wl_fixed_to_double(y),
808 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100809 *sx = wl_fixed_from_double(sxf);
810 *sy = wl_fixed_from_double(syf);
811}
812
813WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200814weston_surface_from_global(struct weston_surface *surface,
815 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
816{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200817 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200818
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400819 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200820 *sx = floorf(sxf);
821 *sy = floorf(syf);
822}
823
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400824WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400825weston_surface_schedule_repaint(struct weston_surface *surface)
826{
827 struct weston_output *output;
828
829 wl_list_for_each(output, &surface->compositor->output_list, link)
830 if (surface->output_mask & (1 << output->id))
831 weston_output_schedule_repaint(output);
832}
833
834WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500835weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500836{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400837 pixman_region32_union_rect(&surface->damage, &surface->damage,
838 0, 0, surface->geometry.width,
839 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200840
Kristian Høgsberg98238702012-08-03 16:29:12 -0400841 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500842}
843
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400844WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500845weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200846 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400847{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200848 surface->geometry.x = x;
849 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200850 surface->geometry.width = width;
851 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200852 weston_surface_geometry_dirty(surface);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400853}
854
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200855WL_EXPORT void
856weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200857 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200858{
859 surface->geometry.x = x;
860 surface->geometry.y = y;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200861 weston_surface_geometry_dirty(surface);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200862}
863
Pekka Paalanen483243f2013-03-08 14:56:50 +0200864static void
865transform_parent_handle_parent_destroy(struct wl_listener *listener,
866 void *data)
867{
868 struct weston_surface *surface =
869 container_of(listener, struct weston_surface,
870 geometry.parent_destroy_listener);
871
872 weston_surface_set_transform_parent(surface, NULL);
873}
874
875WL_EXPORT void
876weston_surface_set_transform_parent(struct weston_surface *surface,
877 struct weston_surface *parent)
878{
879 if (surface->geometry.parent) {
880 wl_list_remove(&surface->geometry.parent_destroy_listener.link);
881 wl_list_remove(&surface->geometry.parent_link);
882 }
883
884 surface->geometry.parent = parent;
885
886 surface->geometry.parent_destroy_listener.notify =
887 transform_parent_handle_parent_destroy;
888 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500889 wl_signal_add(&parent->destroy_signal,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200890 &surface->geometry.parent_destroy_listener);
891 wl_list_insert(&parent->geometry.child_list,
892 &surface->geometry.parent_link);
893 }
894
895 weston_surface_geometry_dirty(surface);
896}
897
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300898WL_EXPORT int
899weston_surface_is_mapped(struct weston_surface *surface)
900{
901 if (surface->output)
902 return 1;
903 else
904 return 0;
905}
906
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200907WL_EXPORT int32_t
908weston_surface_buffer_width(struct weston_surface *surface)
909{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200910 int32_t width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200911 switch (surface->buffer_transform) {
912 case WL_OUTPUT_TRANSFORM_90:
913 case WL_OUTPUT_TRANSFORM_270:
914 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
915 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200916 width = surface->buffer_ref.buffer->height;
917 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200918 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200919 width = surface->buffer_ref.buffer->width;
920 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200921 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200922 return width / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200923}
924
925WL_EXPORT int32_t
926weston_surface_buffer_height(struct weston_surface *surface)
927{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200928 int32_t height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200929 switch (surface->buffer_transform) {
930 case WL_OUTPUT_TRANSFORM_90:
931 case WL_OUTPUT_TRANSFORM_270:
932 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
933 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200934 height = surface->buffer_ref.buffer->width;
935 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200936 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200937 height = surface->buffer_ref.buffer->height;
938 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200939 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200940 return height / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200941}
942
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400943WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500944weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500945{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400946 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500947
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400948 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500949
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400950 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500951}
952
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400953WL_EXPORT struct weston_surface *
Tiago Vignatti9d393522012-02-10 16:26:19 +0200954weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100955 wl_fixed_t x, wl_fixed_t y,
956 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200957{
958 struct weston_surface *surface;
959
960 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100961 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500962 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100963 wl_fixed_to_int(*sx),
964 wl_fixed_to_int(*sy),
965 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200966 return surface;
967 }
968
969 return NULL;
970}
971
972static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500973weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400974{
Daniel Stone37816df2012-05-16 18:45:18 +0100975 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400976
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500977 if (!compositor->focus)
978 return;
979
Daniel Stone37816df2012-05-16 18:45:18 +0100980 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400981 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400982}
983
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400984WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500985weston_surface_unmap(struct weston_surface *surface)
986{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100987 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500988
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500989 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500990 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500991 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500992
Daniel Stone4dab5db2012-05-30 16:31:53 +0100993 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400994 if (seat->keyboard && seat->keyboard->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400995 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400996 if (seat->pointer && seat->pointer->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400997 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400998 NULL,
999 wl_fixed_from_int(0),
1000 wl_fixed_from_int(0));
Daniel Stone4dab5db2012-05-30 16:31:53 +01001001 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001002
Kristian Høgsberg98238702012-08-03 16:29:12 -04001003 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001004}
1005
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001006struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001007 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001008 struct wl_list link;
1009};
1010
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001011
1012WL_EXPORT void
1013weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001014{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001015 /* Not a valid way to destroy a client surface */
1016 assert(surface->resource == NULL);
1017
1018 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1019
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001020 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001021 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001022
Pekka Paalanen483243f2013-03-08 14:56:50 +02001023 assert(wl_list_empty(&surface->geometry.child_list));
Pekka Paalanene67858b2013-04-25 13:57:42 +03001024 assert(wl_list_empty(&surface->subsurface_list_pending));
1025 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001026
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001027 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001028 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001029
Pekka Paalanenbc106382012-10-10 12:49:31 +03001030 wl_list_for_each_safe(cb, next,
1031 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001032 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001033
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001034 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001035 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001036 pixman_region32_fini(&surface->pending.damage);
1037
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001038 if (surface->pending.buffer)
1039 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1040
Pekka Paalanende685b82012-12-04 15:58:12 +02001041 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001042
Kristian Høgsberg42263852012-09-06 21:59:29 -04001043 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001044
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001045 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001046 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001047 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001048 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001049 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001050
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001051 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001052 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001053
Pekka Paalanen483243f2013-03-08 14:56:50 +02001054 weston_surface_set_transform_parent(surface, NULL);
1055
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001056 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001057}
1058
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001059static void
1060destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001061{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001062 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001063
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001064 surface->resource = NULL;
1065 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001066}
1067
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001068static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001069weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1070 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001071{
Pekka Paalanende685b82012-12-04 15:58:12 +02001072 struct weston_buffer_reference *ref =
1073 container_of(listener, struct weston_buffer_reference,
1074 destroy_listener);
1075
1076 assert((struct wl_buffer *)data == ref->buffer);
1077 ref->buffer = NULL;
1078}
1079
1080WL_EXPORT void
1081weston_buffer_reference(struct weston_buffer_reference *ref,
1082 struct wl_buffer *buffer)
1083{
1084 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001085 ref->buffer->busy_count--;
1086 if (ref->buffer->busy_count == 0) {
1087 assert(ref->buffer->resource.client != NULL);
1088 wl_resource_queue_event(&ref->buffer->resource,
1089 WL_BUFFER_RELEASE);
1090 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001091 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001092 }
1093
Pekka Paalanende685b82012-12-04 15:58:12 +02001094 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001095 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001096 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001097 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001098 }
1099
Pekka Paalanende685b82012-12-04 15:58:12 +02001100 ref->buffer = buffer;
1101 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1102}
1103
1104static void
1105weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
1106{
1107 weston_buffer_reference(&surface->buffer_ref, buffer);
1108
Pekka Paalanena6421c42012-12-04 15:58:10 +02001109 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001110 if (weston_surface_is_mapped(surface))
1111 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001112 }
1113
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001114 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001115}
1116
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001117WL_EXPORT void
1118weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001119{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001120 wl_list_remove(&surface->layer_link);
1121 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001122 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001123 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001124}
1125
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001126WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001127weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001128{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001129 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001130
1131 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001132 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001133}
1134
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001135WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001136weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001137{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001138 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001139
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001140 pixman_region32_union(&compositor->primary_plane.damage,
1141 &compositor->primary_plane.damage,
1142 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001143 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001144}
1145
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001146static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001147surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001148 pixman_region32_t *opaque)
1149{
Pekka Paalanende685b82012-12-04 15:58:12 +02001150 if (surface->buffer_ref.buffer &&
1151 wl_buffer_is_shm(surface->buffer_ref.buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001152 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001153
1154 if (surface->transform.enabled) {
1155 pixman_box32_t *extents;
1156
1157 extents = pixman_region32_extents(&surface->damage);
1158 surface_compute_bbox(surface, extents->x1, extents->y1,
1159 extents->x2 - extents->x1,
1160 extents->y2 - extents->y1,
1161 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001162 pixman_region32_translate(&surface->damage,
1163 -surface->plane->x,
1164 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001165 } else {
1166 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001167 surface->geometry.x - surface->plane->x,
1168 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001169 }
1170
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001171 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001172 pixman_region32_union(&surface->plane->damage,
1173 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001174 empty_region(&surface->damage);
1175 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001176 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001177}
1178
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001179static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001180compositor_accumulate_damage(struct weston_compositor *ec)
1181{
1182 struct weston_plane *plane;
1183 struct weston_surface *es;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001184 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001185
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001186 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001187
1188 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001189 pixman_region32_copy(&plane->clip, &clip);
1190
1191 pixman_region32_init(&opaque);
1192
1193 wl_list_for_each(es, &ec->surface_list, link) {
1194 if (es->plane != plane)
1195 continue;
1196
1197 surface_accumulate_damage(es, &opaque);
1198 }
1199
1200 pixman_region32_union(&clip, &clip, &opaque);
1201 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001202 }
1203
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001204 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001205
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001206 wl_list_for_each(es, &ec->surface_list, link) {
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001207 /* Both the renderer and the backend have seen the buffer
1208 * by now. If renderer needs the buffer, it has its own
1209 * reference set. If the backend wants to keep the buffer
1210 * around for migrating the surface into a non-primary plane
1211 * later, keep_buffer is true. Otherwise, drop the core
1212 * reference now, and allow early buffer release. This enables
1213 * clients to use single-buffering.
1214 */
1215 if (!es->keep_buffer)
1216 weston_buffer_reference(&es->buffer_ref, NULL);
1217 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001218}
1219
1220static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001221surface_list_add(struct weston_compositor *compositor,
1222 struct weston_surface *surface)
1223{
1224 struct weston_subsurface *sub;
1225
1226 if (wl_list_empty(&surface->subsurface_list)) {
1227 weston_surface_update_transform(surface);
1228 wl_list_insert(compositor->surface_list.prev, &surface->link);
1229 return;
1230 }
1231
1232 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1233 if (!weston_surface_is_mapped(sub->surface))
1234 continue;
1235
1236 if (sub->surface == surface) {
1237 weston_surface_update_transform(sub->surface);
1238 wl_list_insert(compositor->surface_list.prev,
1239 &sub->surface->link);
1240 } else {
1241 surface_list_add(compositor, sub->surface);
1242 }
1243 }
1244}
1245
1246static void
1247weston_compositor_build_surface_list(struct weston_compositor *compositor)
1248{
1249 struct weston_surface *surface;
1250 struct weston_layer *layer;
1251
1252 wl_list_init(&compositor->surface_list);
1253 wl_list_for_each(layer, &compositor->layer_list, link) {
1254 wl_list_for_each(surface, &layer->surface_list, layer_link) {
1255 surface_list_add(compositor, surface);
1256 }
1257 }
1258}
1259
1260static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001261weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001262{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001263 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001264 struct weston_surface *es;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001265 struct weston_animation *animation, *next;
1266 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001267 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001268 pixman_region32_t output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001269
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001270 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001271 weston_compositor_build_surface_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001272
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001273 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001274 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001275 else
1276 wl_list_for_each(es, &ec->surface_list, link)
1277 weston_surface_move_to_plane(es, &ec->primary_plane);
1278
Pekka Paalanene67858b2013-04-25 13:57:42 +03001279 wl_list_init(&frame_callback_list);
1280 wl_list_for_each(es, &ec->surface_list, link) {
1281 if (es->output == output) {
1282 wl_list_insert_list(&frame_callback_list,
1283 &es->frame_callback_list);
1284 wl_list_init(&es->frame_callback_list);
1285 }
1286 }
1287
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001288 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001289
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001290 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001291 pixman_region32_intersect(&output_damage,
1292 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001293 pixman_region32_subtract(&output_damage,
1294 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001295
Scott Moreauccbf29d2012-02-22 14:21:41 -07001296 if (output->dirty)
1297 weston_output_update_matrix(output);
1298
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001299 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001300
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001301 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001302
Kristian Høgsbergef044142011-06-21 15:02:12 -04001303 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001304
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001305 weston_compositor_repick(ec);
1306 wl_event_loop_dispatch(ec->input_loop, 0);
1307
Jonas Ådahldb773762012-06-13 00:01:21 +02001308 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001309 wl_callback_send_done(cb->resource, msecs);
1310 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001311 }
1312
Scott Moreaud64cf212012-06-08 19:40:54 -06001313 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001314 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001315 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001316 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001317}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001318
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001319static int
1320weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001321{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001322 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001323
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001324 wl_event_loop_dispatch(compositor->input_loop, 0);
1325
1326 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001327}
1328
1329WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001330weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001331{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001332 struct weston_compositor *compositor = output->compositor;
1333 struct wl_event_loop *loop =
1334 wl_display_get_event_loop(compositor->wl_display);
1335 int fd;
1336
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001337 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001338 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001339 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001340 return;
1341 }
1342
1343 output->repaint_scheduled = 0;
1344 if (compositor->input_loop_source)
1345 return;
1346
1347 fd = wl_event_loop_get_fd(compositor->input_loop);
1348 compositor->input_loop_source =
1349 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1350 weston_compositor_read_input, compositor);
1351}
1352
1353static void
1354idle_repaint(void *data)
1355{
1356 struct weston_output *output = data;
1357
Jonas Ådahle5a12252013-04-05 23:07:11 +02001358 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001359}
1360
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001361WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001362weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1363{
1364 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001365 if (below != NULL)
1366 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001367}
1368
1369WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001370weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001371{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001372 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001373 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001374
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001375 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1376 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001377 return;
1378
Kristian Høgsbergef044142011-06-21 15:02:12 -04001379 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001380 output->repaint_needed = 1;
1381 if (output->repaint_scheduled)
1382 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001383
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001384 wl_event_loop_add_idle(loop, idle_repaint, output);
1385 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001386
1387 if (compositor->input_loop_source) {
1388 wl_event_source_remove(compositor->input_loop_source);
1389 compositor->input_loop_source = NULL;
1390 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001391}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001392
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001393WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001394weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1395{
1396 struct weston_output *output;
1397
1398 wl_list_for_each(output, &compositor->output_list, link)
1399 weston_output_schedule_repaint(output);
1400}
1401
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001402static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001403surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001404{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001405 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001406}
1407
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001408static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001409surface_attach(struct wl_client *client,
1410 struct wl_resource *resource,
1411 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1412{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001413 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001414 struct wl_buffer *buffer = NULL;
1415
1416 if (buffer_resource)
1417 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001418
Pekka Paalanende685b82012-12-04 15:58:12 +02001419 /* Attach, attach, without commit in between does not send
1420 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001421 if (surface->pending.buffer)
1422 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001423
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001424 surface->pending.sx = sx;
1425 surface->pending.sy = sy;
1426 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001427 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001428 if (buffer) {
1429 wl_signal_add(&buffer->resource.destroy_signal,
1430 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001431 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001432}
1433
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001434static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001435surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001436 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001437 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001438{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001439 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001440
Pekka Paalanen8e159182012-10-10 12:49:25 +03001441 pixman_region32_union_rect(&surface->pending.damage,
1442 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001443 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001444}
1445
Kristian Høgsberg33418202011-08-16 23:01:28 -04001446static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001447destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001448{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001449 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001450
1451 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001452 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001453}
1454
1455static void
1456surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001457 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001458{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001459 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001460 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001461
1462 cb = malloc(sizeof *cb);
1463 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001464 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001465 return;
1466 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001467
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001468 cb->resource = wl_client_add_object(client, &wl_callback_interface,
1469 NULL, callback, cb);
1470 wl_resource_set_destructor(cb->resource, destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001471
Pekka Paalanenbc106382012-10-10 12:49:31 +03001472 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001473}
1474
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001475static void
1476surface_set_opaque_region(struct wl_client *client,
1477 struct wl_resource *resource,
1478 struct wl_resource *region_resource)
1479{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001480 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001481 struct weston_region *region;
1482
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001483 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001484 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001485 pixman_region32_copy(&surface->pending.opaque,
1486 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001487 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001488 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001489 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001490}
1491
1492static void
1493surface_set_input_region(struct wl_client *client,
1494 struct wl_resource *resource,
1495 struct wl_resource *region_resource)
1496{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001497 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001498 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001499
1500 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001501 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001502 pixman_region32_copy(&surface->pending.input,
1503 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001504 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001505 pixman_region32_fini(&surface->pending.input);
1506 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001507 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001508}
1509
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001510static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001511weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001512{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001513 struct weston_subsurface *sub;
1514
1515 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1516 parent_link_pending) {
1517 wl_list_remove(&sub->parent_link);
1518 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1519 }
1520}
1521
1522static void
1523weston_surface_commit(struct weston_surface *surface)
1524{
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001525 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001526 int surface_width = 0;
1527 int surface_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001528
Alexander Larsson4ea95522013-05-22 14:41:37 +02001529 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001530 surface->buffer_transform = surface->pending.buffer_transform;
1531
Alexander Larsson4ea95522013-05-22 14:41:37 +02001532 /* wl_surface.set_buffer_scale */
1533 surface->buffer_scale = surface->pending.buffer_scale;
1534
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001535 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001536 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001537 weston_surface_attach(surface, surface->pending.buffer);
1538
Giulio Camuffo184df502013-02-21 11:29:21 +01001539 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001540 surface_width = weston_surface_buffer_width(surface);
1541 surface_height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001542 }
1543
1544 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001545 surface->configure(surface,
1546 surface->pending.sx, surface->pending.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001547 surface_width, surface_height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001548
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001549 if (surface->pending.buffer)
1550 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1551 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001552 surface->pending.sx = 0;
1553 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001554 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001555
1556 /* wl_surface.damage */
1557 pixman_region32_union(&surface->damage, &surface->damage,
1558 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001559 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1560 0, 0,
1561 surface->geometry.width,
1562 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001563 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001564
1565 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001566 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001567 surface->geometry.width,
1568 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001569 pixman_region32_intersect(&opaque,
1570 &opaque, &surface->pending.opaque);
1571
1572 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1573 pixman_region32_copy(&surface->opaque, &opaque);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001574 weston_surface_geometry_dirty(surface);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001575 }
1576
1577 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001578
1579 /* wl_surface.set_input_region */
1580 pixman_region32_fini(&surface->input);
1581 pixman_region32_init_rect(&surface->input, 0, 0,
1582 surface->geometry.width,
1583 surface->geometry.height);
1584 pixman_region32_intersect(&surface->input,
1585 &surface->input, &surface->pending.input);
1586
Pekka Paalanenbc106382012-10-10 12:49:31 +03001587 /* wl_surface.frame */
1588 wl_list_insert_list(&surface->frame_callback_list,
1589 &surface->pending.frame_callback_list);
1590 wl_list_init(&surface->pending.frame_callback_list);
1591
Pekka Paalanene67858b2013-04-25 13:57:42 +03001592 weston_surface_commit_subsurface_order(surface);
1593
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001594 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001595}
1596
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001597static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001598weston_subsurface_commit(struct weston_subsurface *sub);
1599
1600static void
1601weston_subsurface_parent_commit(struct weston_subsurface *sub,
1602 int parent_is_synchronized);
1603
1604static void
1605surface_commit(struct wl_client *client, struct wl_resource *resource)
1606{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001607 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001608 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1609
1610 if (sub) {
1611 weston_subsurface_commit(sub);
1612 return;
1613 }
1614
1615 weston_surface_commit(surface);
1616
1617 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1618 if (sub->surface != surface)
1619 weston_subsurface_parent_commit(sub, 0);
1620 }
1621}
1622
1623static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001624surface_set_buffer_transform(struct wl_client *client,
1625 struct wl_resource *resource, int transform)
1626{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001627 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001628
1629 surface->pending.buffer_transform = transform;
1630}
1631
Alexander Larsson4ea95522013-05-22 14:41:37 +02001632static void
1633surface_set_buffer_scale(struct wl_client *client,
1634 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001635 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02001636{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001637 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02001638
1639 surface->pending.buffer_scale = scale;
1640}
1641
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001642static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001643 surface_destroy,
1644 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001645 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001646 surface_frame,
1647 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001648 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001649 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001650 surface_set_buffer_transform,
1651 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001652};
1653
1654static void
1655compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001656 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001657{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001658 struct weston_compositor *ec = resource->data;
1659 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001660
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001661 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001662 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001663 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001664 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001665 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001666
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001667 surface->resource = wl_client_add_object(client, &wl_surface_interface,
1668 &surface_interface,
1669 id, surface);
1670 wl_resource_set_destructor(surface->resource, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001671}
1672
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001673static void
1674destroy_region(struct wl_resource *resource)
1675{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001676 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001677
1678 pixman_region32_fini(&region->region);
1679 free(region);
1680}
1681
1682static void
1683region_destroy(struct wl_client *client, struct wl_resource *resource)
1684{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001685 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001686}
1687
1688static void
1689region_add(struct wl_client *client, struct wl_resource *resource,
1690 int32_t x, int32_t y, int32_t width, int32_t height)
1691{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001692 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001693
1694 pixman_region32_union_rect(&region->region, &region->region,
1695 x, y, width, height);
1696}
1697
1698static void
1699region_subtract(struct wl_client *client, struct wl_resource *resource,
1700 int32_t x, int32_t y, int32_t width, int32_t height)
1701{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001702 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001703 pixman_region32_t rect;
1704
1705 pixman_region32_init_rect(&rect, x, y, width, height);
1706 pixman_region32_subtract(&region->region, &region->region, &rect);
1707 pixman_region32_fini(&rect);
1708}
1709
1710static const struct wl_region_interface region_interface = {
1711 region_destroy,
1712 region_add,
1713 region_subtract
1714};
1715
1716static void
1717compositor_create_region(struct wl_client *client,
1718 struct wl_resource *resource, uint32_t id)
1719{
1720 struct weston_region *region;
1721
1722 region = malloc(sizeof *region);
1723 if (region == NULL) {
1724 wl_resource_post_no_memory(resource);
1725 return;
1726 }
1727
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001728 pixman_region32_init(&region->region);
1729
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001730 region->resource = wl_client_add_object(client, &wl_region_interface,
1731 &region_interface, id, region);
1732 wl_resource_set_destructor(region->resource, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001733}
1734
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001735static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001736 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001737 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001738};
1739
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001740static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001741weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
1742{
1743 struct weston_surface *surface = sub->surface;
1744 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001745 int surface_width = 0;
1746 int surface_height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001747
Alexander Larsson4ea95522013-05-22 14:41:37 +02001748 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001749 surface->buffer_transform = sub->cached.buffer_transform;
1750
Alexander Larsson4ea95522013-05-22 14:41:37 +02001751 /* wl_surface.set_buffer_scale */
1752 surface->buffer_scale = sub->cached.buffer_scale;
1753
Pekka Paalanene67858b2013-04-25 13:57:42 +03001754 /* wl_surface.attach */
1755 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
1756 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
1757 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
1758
1759 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001760 surface_width = weston_surface_buffer_width(surface);
1761 surface_height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001762 }
1763
1764 if (surface->configure && sub->cached.newly_attached)
1765 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001766 surface_width, surface_height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001767 sub->cached.sx = 0;
1768 sub->cached.sy = 0;
1769 sub->cached.newly_attached = 0;
1770
1771 /* wl_surface.damage */
1772 pixman_region32_union(&surface->damage, &surface->damage,
1773 &sub->cached.damage);
1774 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1775 0, 0,
1776 surface->geometry.width,
1777 surface->geometry.height);
1778 empty_region(&sub->cached.damage);
1779
1780 /* wl_surface.set_opaque_region */
1781 pixman_region32_init_rect(&opaque, 0, 0,
1782 surface->geometry.width,
1783 surface->geometry.height);
1784 pixman_region32_intersect(&opaque,
1785 &opaque, &sub->cached.opaque);
1786
1787 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1788 pixman_region32_copy(&surface->opaque, &opaque);
1789 weston_surface_geometry_dirty(surface);
1790 }
1791
1792 pixman_region32_fini(&opaque);
1793
1794 /* wl_surface.set_input_region */
1795 pixman_region32_fini(&surface->input);
1796 pixman_region32_init_rect(&surface->input, 0, 0,
1797 surface->geometry.width,
1798 surface->geometry.height);
1799 pixman_region32_intersect(&surface->input,
1800 &surface->input, &sub->cached.input);
1801
1802 /* wl_surface.frame */
1803 wl_list_insert_list(&surface->frame_callback_list,
1804 &sub->cached.frame_callback_list);
1805 wl_list_init(&sub->cached.frame_callback_list);
1806
1807 weston_surface_commit_subsurface_order(surface);
1808
1809 weston_surface_schedule_repaint(surface);
1810
1811 sub->cached.has_data = 0;
1812}
1813
1814static void
1815weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
1816{
1817 struct weston_surface *surface = sub->surface;
1818
1819 /*
1820 * If this commit would cause the surface to move by the
1821 * attach(dx, dy) parameters, the old damage region must be
1822 * translated to correspond to the new surface coordinate system
1823 * origin.
1824 */
1825 pixman_region32_translate(&sub->cached.damage,
1826 -surface->pending.sx, -surface->pending.sy);
1827 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
1828 &surface->pending.damage);
1829 empty_region(&surface->pending.damage);
1830
1831 if (surface->pending.newly_attached) {
1832 sub->cached.newly_attached = 1;
1833 weston_buffer_reference(&sub->cached.buffer_ref,
1834 surface->pending.buffer);
1835 }
1836 sub->cached.sx += surface->pending.sx;
1837 sub->cached.sy += surface->pending.sy;
1838 surface->pending.sx = 0;
1839 surface->pending.sy = 0;
1840 surface->pending.newly_attached = 0;
1841
1842 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001843 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001844
1845 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
1846
1847 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
1848
1849 wl_list_insert_list(&sub->cached.frame_callback_list,
1850 &surface->pending.frame_callback_list);
1851 wl_list_init(&surface->pending.frame_callback_list);
1852
1853 sub->cached.has_data = 1;
1854}
1855
1856static int
1857weston_subsurface_is_synchronized(struct weston_subsurface *sub)
1858{
1859 while (sub) {
1860 if (sub->synchronized)
1861 return 1;
1862
1863 if (!sub->parent)
1864 return 0;
1865
1866 sub = weston_surface_to_subsurface(sub->parent);
1867 }
1868
1869 return 0;
1870}
1871
1872static void
1873weston_subsurface_commit(struct weston_subsurface *sub)
1874{
1875 struct weston_surface *surface = sub->surface;
1876 struct weston_subsurface *tmp;
1877
1878 /* Recursive check for effectively synchronized. */
1879 if (weston_subsurface_is_synchronized(sub)) {
1880 weston_subsurface_commit_to_cache(sub);
1881 } else {
1882 if (sub->cached.has_data) {
1883 /* flush accumulated state from cache */
1884 weston_subsurface_commit_to_cache(sub);
1885 weston_subsurface_commit_from_cache(sub);
1886 } else {
1887 weston_surface_commit(surface);
1888 }
1889
1890 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1891 if (tmp->surface != surface)
1892 weston_subsurface_parent_commit(tmp, 0);
1893 }
1894 }
1895}
1896
1897static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001898weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001899{
1900 struct weston_surface *surface = sub->surface;
1901 struct weston_subsurface *tmp;
1902
Pekka Paalanene67858b2013-04-25 13:57:42 +03001903 /* From now on, commit_from_cache the whole sub-tree, regardless of
1904 * the synchronized mode of each child. This sub-surface or some
1905 * of its ancestors were synchronized, so we are synchronized
1906 * all the way down.
1907 */
1908
1909 if (sub->cached.has_data)
1910 weston_subsurface_commit_from_cache(sub);
1911
1912 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1913 if (tmp->surface != surface)
1914 weston_subsurface_parent_commit(tmp, 1);
1915 }
1916}
1917
1918static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001919weston_subsurface_parent_commit(struct weston_subsurface *sub,
1920 int parent_is_synchronized)
1921{
1922 if (sub->position.set) {
1923 weston_surface_set_position(sub->surface,
1924 sub->position.x, sub->position.y);
1925 sub->position.set = 0;
1926 }
1927
1928 if (parent_is_synchronized || sub->synchronized)
1929 weston_subsurface_synchronized_commit(sub);
1930}
1931
1932static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001933subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
1934 int32_t width, int32_t height)
1935{
1936 struct weston_compositor *compositor = surface->compositor;
1937
1938 weston_surface_configure(surface,
1939 surface->geometry.x + dx,
1940 surface->geometry.y + dy,
1941 width, height);
1942
1943 /* No need to check parent mappedness, because if parent is not
1944 * mapped, parent is not in a visible layer, so this sub-surface
1945 * will not be drawn either.
1946 */
1947 if (!weston_surface_is_mapped(surface)) {
1948 wl_list_init(&surface->layer_link);
1949
1950 /* Cannot call weston_surface_update_transform(),
1951 * because that would call it also for the parent surface,
1952 * which might not be mapped yet. That would lead to
1953 * inconsistent state, where the window could never be
1954 * mapped.
1955 *
1956 * Instead just assing any output, to make
1957 * weston_surface_is_mapped() return true, so that when the
1958 * parent surface does get mapped, this one will get
1959 * included, too. See surface_list_add().
1960 */
1961 assert(!wl_list_empty(&compositor->output_list));
1962 surface->output = container_of(compositor->output_list.next,
1963 struct weston_output, link);
1964 }
1965}
1966
1967static struct weston_subsurface *
1968weston_surface_to_subsurface(struct weston_surface *surface)
1969{
1970 if (surface->configure == subsurface_configure)
1971 return surface->configure_private;
1972
1973 return NULL;
1974}
1975
Pekka Paalanen01388e22013-04-25 13:57:44 +03001976WL_EXPORT struct weston_surface *
1977weston_surface_get_main_surface(struct weston_surface *surface)
1978{
1979 struct weston_subsurface *sub;
1980
1981 while (surface && (sub = weston_surface_to_subsurface(surface)))
1982 surface = sub->parent;
1983
1984 return surface;
1985}
1986
Pekka Paalanene67858b2013-04-25 13:57:42 +03001987static void
1988subsurface_set_position(struct wl_client *client,
1989 struct wl_resource *resource, int32_t x, int32_t y)
1990{
1991 struct weston_subsurface *sub = resource->data;
1992
1993 if (!sub)
1994 return;
1995
1996 sub->position.x = x;
1997 sub->position.y = y;
1998 sub->position.set = 1;
1999}
2000
2001static struct weston_subsurface *
2002subsurface_from_surface(struct weston_surface *surface)
2003{
2004 struct weston_subsurface *sub;
2005
2006 sub = weston_surface_to_subsurface(surface);
2007 if (sub)
2008 return sub;
2009
2010 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2011 if (sub->surface == surface)
2012 return sub;
2013
2014 return NULL;
2015}
2016
2017static struct weston_subsurface *
2018subsurface_sibling_check(struct weston_subsurface *sub,
2019 struct weston_surface *surface,
2020 const char *request)
2021{
2022 struct weston_subsurface *sibling;
2023
2024 sibling = subsurface_from_surface(surface);
2025
2026 if (!sibling) {
2027 wl_resource_post_error(sub->resource,
2028 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2029 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002030 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002031 return NULL;
2032 }
2033
2034 if (sibling->parent != sub->parent) {
2035 wl_resource_post_error(sub->resource,
2036 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2037 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002038 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002039 return NULL;
2040 }
2041
2042 return sibling;
2043}
2044
2045static void
2046subsurface_place_above(struct wl_client *client,
2047 struct wl_resource *resource,
2048 struct wl_resource *sibling_resource)
2049{
2050 struct weston_subsurface *sub = resource->data;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002051 struct weston_surface *surface =
2052 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002053 struct weston_subsurface *sibling;
2054
2055 if (!sub)
2056 return;
2057
2058 sibling = subsurface_sibling_check(sub, surface, "place_above");
2059 if (!sibling)
2060 return;
2061
2062 wl_list_remove(&sub->parent_link_pending);
2063 wl_list_insert(sibling->parent_link_pending.prev,
2064 &sub->parent_link_pending);
2065}
2066
2067static void
2068subsurface_place_below(struct wl_client *client,
2069 struct wl_resource *resource,
2070 struct wl_resource *sibling_resource)
2071{
2072 struct weston_subsurface *sub = resource->data;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002073 struct weston_surface *surface =
2074 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002075 struct weston_subsurface *sibling;
2076
2077 if (!sub)
2078 return;
2079
2080 sibling = subsurface_sibling_check(sub, surface, "place_below");
2081 if (!sibling)
2082 return;
2083
2084 wl_list_remove(&sub->parent_link_pending);
2085 wl_list_insert(&sibling->parent_link_pending,
2086 &sub->parent_link_pending);
2087}
2088
2089static void
2090subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2091{
2092 struct weston_subsurface *sub = resource->data;
2093
2094 if (sub)
2095 sub->synchronized = 1;
2096}
2097
2098static void
2099subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2100{
2101 struct weston_subsurface *sub = resource->data;
2102
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002103 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002104 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002105
2106 /* If sub became effectively desynchronized, flush. */
2107 if (!weston_subsurface_is_synchronized(sub))
2108 weston_subsurface_synchronized_commit(sub);
2109 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002110}
2111
2112static void
2113weston_subsurface_cache_init(struct weston_subsurface *sub)
2114{
2115 pixman_region32_init(&sub->cached.damage);
2116 pixman_region32_init(&sub->cached.opaque);
2117 pixman_region32_init(&sub->cached.input);
2118 wl_list_init(&sub->cached.frame_callback_list);
2119 sub->cached.buffer_ref.buffer = NULL;
2120}
2121
2122static void
2123weston_subsurface_cache_fini(struct weston_subsurface *sub)
2124{
2125 struct weston_frame_callback *cb, *tmp;
2126
2127 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002128 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002129
2130 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2131 pixman_region32_fini(&sub->cached.damage);
2132 pixman_region32_fini(&sub->cached.opaque);
2133 pixman_region32_fini(&sub->cached.input);
2134}
2135
2136static void
2137weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2138{
2139 wl_list_remove(&sub->parent_link);
2140 wl_list_remove(&sub->parent_link_pending);
2141 wl_list_remove(&sub->parent_destroy_listener.link);
2142 sub->parent = NULL;
2143}
2144
2145static void
2146weston_subsurface_destroy(struct weston_subsurface *sub);
2147
2148static void
2149subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2150{
2151 struct weston_subsurface *sub =
2152 container_of(listener, struct weston_subsurface,
2153 surface_destroy_listener);
2154 assert(data == &sub->surface->resource);
2155
2156 /* The protocol object (wl_resource) is left inert. */
2157 if (sub->resource)
2158 sub->resource->data = NULL;
2159
2160 weston_subsurface_destroy(sub);
2161}
2162
2163static void
2164subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2165{
2166 struct weston_subsurface *sub =
2167 container_of(listener, struct weston_subsurface,
2168 parent_destroy_listener);
2169 assert(data == &sub->parent->resource);
2170 assert(sub->surface != sub->parent);
2171
2172 if (weston_surface_is_mapped(sub->surface))
2173 weston_surface_unmap(sub->surface);
2174
2175 weston_subsurface_unlink_parent(sub);
2176}
2177
2178static void
2179subsurface_resource_destroy(struct wl_resource *resource)
2180{
2181 struct weston_subsurface *sub = resource->data;
2182
2183 if (sub)
2184 weston_subsurface_destroy(sub);
2185
2186 free(resource);
2187}
2188
2189static void
2190subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2191{
2192 wl_resource_destroy(resource);
2193}
2194
2195static void
2196weston_subsurface_link_parent(struct weston_subsurface *sub,
2197 struct weston_surface *parent)
2198{
2199 sub->parent = parent;
2200 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002201 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002202 &sub->parent_destroy_listener);
2203
2204 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2205 wl_list_insert(&parent->subsurface_list_pending,
2206 &sub->parent_link_pending);
2207}
2208
2209static void
2210weston_subsurface_link_surface(struct weston_subsurface *sub,
2211 struct weston_surface *surface)
2212{
2213 sub->surface = surface;
2214 sub->surface_destroy_listener.notify =
2215 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002216 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002217 &sub->surface_destroy_listener);
2218}
2219
2220static void
2221weston_subsurface_destroy(struct weston_subsurface *sub)
2222{
2223 assert(sub->surface);
2224
2225 if (sub->resource) {
2226 assert(weston_surface_to_subsurface(sub->surface) == sub);
2227 assert(sub->parent_destroy_listener.notify ==
2228 subsurface_handle_parent_destroy);
2229
2230 weston_surface_set_transform_parent(sub->surface, NULL);
2231 if (sub->parent)
2232 weston_subsurface_unlink_parent(sub);
2233
2234 weston_subsurface_cache_fini(sub);
2235
2236 sub->surface->configure = NULL;
2237 sub->surface->configure_private = NULL;
2238 } else {
2239 /* the dummy weston_subsurface for the parent itself */
2240 assert(sub->parent_destroy_listener.notify == NULL);
2241 wl_list_remove(&sub->parent_link);
2242 wl_list_remove(&sub->parent_link_pending);
2243 }
2244
2245 wl_list_remove(&sub->surface_destroy_listener.link);
2246 free(sub);
2247}
2248
2249static const struct wl_subsurface_interface subsurface_implementation = {
2250 subsurface_destroy,
2251 subsurface_set_position,
2252 subsurface_place_above,
2253 subsurface_place_below,
2254 subsurface_set_sync,
2255 subsurface_set_desync
2256};
2257
2258static struct weston_subsurface *
2259weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2260 struct weston_surface *parent)
2261{
2262 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002263 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002264
2265 sub = calloc(1, sizeof *sub);
2266 if (!sub)
2267 return NULL;
2268
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002269 sub->resource = wl_client_add_object(client,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002270 &wl_subsurface_interface,
2271 &subsurface_implementation,
2272 id, sub);
2273 if (!sub->resource) {
2274 free(sub);
2275 return NULL;
2276 }
2277
2278 sub->resource->destroy = subsurface_resource_destroy;
2279 weston_subsurface_link_surface(sub, surface);
2280 weston_subsurface_link_parent(sub, parent);
2281 weston_subsurface_cache_init(sub);
2282 sub->synchronized = 1;
2283 weston_surface_set_transform_parent(surface, parent);
2284
2285 return sub;
2286}
2287
2288/* Create a dummy subsurface for having the parent itself in its
2289 * sub-surface lists. Makes stacking order manipulation easy.
2290 */
2291static struct weston_subsurface *
2292weston_subsurface_create_for_parent(struct weston_surface *parent)
2293{
2294 struct weston_subsurface *sub;
2295
2296 sub = calloc(1, sizeof *sub);
2297 if (!sub)
2298 return NULL;
2299
2300 weston_subsurface_link_surface(sub, parent);
2301 sub->parent = parent;
2302 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2303 wl_list_insert(&parent->subsurface_list_pending,
2304 &sub->parent_link_pending);
2305
2306 return sub;
2307}
2308
2309static void
2310subcompositor_get_subsurface(struct wl_client *client,
2311 struct wl_resource *resource,
2312 uint32_t id,
2313 struct wl_resource *surface_resource,
2314 struct wl_resource *parent_resource)
2315{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002316 struct weston_surface *surface =
2317 wl_resource_get_user_data(surface_resource);
2318 struct weston_surface *parent =
2319 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002320 struct weston_subsurface *sub;
2321 static const char where[] = "get_subsurface: wl_subsurface@";
2322
2323 if (surface == parent) {
2324 wl_resource_post_error(resource,
2325 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2326 "%s%d: wl_surface@%d cannot be its own parent",
2327 where, id, surface_resource->object.id);
2328 return;
2329 }
2330
2331 if (weston_surface_to_subsurface(surface)) {
2332 wl_resource_post_error(resource,
2333 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2334 "%s%d: wl_surface@%d is already a sub-surface",
2335 where, id, surface_resource->object.id);
2336 return;
2337 }
2338
2339 if (surface->configure) {
2340 wl_resource_post_error(resource,
2341 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2342 "%s%d: wl_surface@%d already has a role",
2343 where, id, surface_resource->object.id);
2344 return;
2345 }
2346
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002347 if (weston_surface_get_main_surface(parent) == surface) {
2348 wl_resource_post_error(resource,
2349 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2350 "%s%d: wl_surface@%d is an ancestor of parent",
2351 where, id, surface_resource->object.id);
2352 return;
2353 }
2354
Pekka Paalanene67858b2013-04-25 13:57:42 +03002355 /* make sure the parent is in its own list */
2356 if (wl_list_empty(&parent->subsurface_list)) {
2357 if (!weston_subsurface_create_for_parent(parent)) {
2358 wl_resource_post_no_memory(resource);
2359 return;
2360 }
2361 }
2362
2363 sub = weston_subsurface_create(id, surface, parent);
2364 if (!sub) {
2365 wl_resource_post_no_memory(resource);
2366 return;
2367 }
2368
2369 surface->configure = subsurface_configure;
2370 surface->configure_private = sub;
2371}
2372
2373static void
2374subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2375{
2376 wl_resource_destroy(resource);
2377}
2378
2379static const struct wl_subcompositor_interface subcompositor_interface = {
2380 subcompositor_destroy,
2381 subcompositor_get_subsurface
2382};
2383
2384static void
2385bind_subcompositor(struct wl_client *client,
2386 void *data, uint32_t version, uint32_t id)
2387{
2388 struct weston_compositor *compositor = data;
2389
2390 wl_client_add_object(client, &wl_subcompositor_interface,
2391 &subcompositor_interface, id, compositor);
2392}
2393
2394static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002395weston_compositor_dpms(struct weston_compositor *compositor,
2396 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002397{
2398 struct weston_output *output;
2399
2400 wl_list_for_each(output, &compositor->output_list, link)
2401 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002402 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002403}
2404
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002405WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002406weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002407{
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002408 switch (compositor->state) {
2409 case WESTON_COMPOSITOR_SLEEPING:
2410 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2411 /* fall through */
2412 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002413 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002414 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002415 /* fall through */
2416 default:
2417 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2418 wl_event_source_timer_update(compositor->idle_source,
2419 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002420 }
2421}
2422
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002423WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002424weston_compositor_offscreen(struct weston_compositor *compositor)
2425{
2426 switch (compositor->state) {
2427 case WESTON_COMPOSITOR_OFFSCREEN:
2428 return;
2429 case WESTON_COMPOSITOR_SLEEPING:
2430 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2431 /* fall through */
2432 default:
2433 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2434 wl_event_source_timer_update(compositor->idle_source, 0);
2435 }
2436}
2437
2438WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002439weston_compositor_sleep(struct weston_compositor *compositor)
2440{
2441 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2442 return;
2443
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002444 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002445 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2446 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2447}
2448
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002449static int
2450idle_handler(void *data)
2451{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002452 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002453
2454 if (compositor->idle_inhibit)
2455 return 1;
2456
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002457 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002458 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002459
2460 return 1;
2461}
2462
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002463WL_EXPORT void
2464weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2465{
2466 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002467 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002468 plane->x = x;
2469 plane->y = y;
2470}
2471
2472WL_EXPORT void
2473weston_plane_release(struct weston_plane *plane)
2474{
2475 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002476 pixman_region32_fini(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002477}
2478
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002479WL_EXPORT void
2480weston_compositor_stack_plane(struct weston_compositor *ec,
2481 struct weston_plane *plane,
2482 struct weston_plane *above)
2483{
2484 if (above)
2485 wl_list_insert(above->link.prev, &plane->link);
2486 else
2487 wl_list_insert(&ec->plane_list, &plane->link);
2488}
2489
Casey Dahlin9074db52012-04-19 22:50:09 -04002490static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002491{
2492 wl_list_remove(&resource->link);
2493 free(resource);
2494}
2495
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002496static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002497bind_output(struct wl_client *client,
2498 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002499{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002500 struct weston_output *output = data;
2501 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002502 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002503
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002504 resource = wl_client_add_object(client,
2505 &wl_output_interface, NULL, id, data);
2506
Casey Dahlin9074db52012-04-19 22:50:09 -04002507 wl_list_insert(&output->resource_list, &resource->link);
2508 resource->destroy = unbind_resource;
2509
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002510 wl_output_send_geometry(resource,
2511 output->x,
2512 output->y,
2513 output->mm_width,
2514 output->mm_height,
2515 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002516 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002517 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002518 if (version >= 2)
2519 wl_output_send_scale(resource,
2520 output->scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002521
2522 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002523 wl_output_send_mode(resource,
2524 mode->flags,
2525 mode->width,
2526 mode->height,
2527 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002528 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002529
2530 if (version >= 2)
2531 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002532}
2533
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002534WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002535weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002536{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002537 struct weston_compositor *c = output->compositor;
2538
Richard Hughes64ddde12013-05-01 21:52:10 +01002539 wl_signal_emit(&output->destroy_signal, output);
2540
Richard Hughesafe690c2013-05-02 10:10:04 +01002541 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002542 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002543 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002544 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002545
2546 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002547}
2548
Scott Moreau1bad5db2012-08-18 01:04:05 -06002549static void
2550weston_output_compute_transform(struct weston_output *output)
2551{
2552 struct weston_matrix transform;
2553 int flip;
2554
2555 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002556 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002557
2558 switch(output->transform) {
2559 case WL_OUTPUT_TRANSFORM_FLIPPED:
2560 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2561 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2562 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002563 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002564 flip = -1;
2565 break;
2566 default:
2567 flip = 1;
2568 break;
2569 }
2570
2571 switch(output->transform) {
2572 case WL_OUTPUT_TRANSFORM_NORMAL:
2573 case WL_OUTPUT_TRANSFORM_FLIPPED:
2574 transform.d[0] = flip;
2575 transform.d[1] = 0;
2576 transform.d[4] = 0;
2577 transform.d[5] = 1;
2578 break;
2579 case WL_OUTPUT_TRANSFORM_90:
2580 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2581 transform.d[0] = 0;
2582 transform.d[1] = -flip;
2583 transform.d[4] = 1;
2584 transform.d[5] = 0;
2585 break;
2586 case WL_OUTPUT_TRANSFORM_180:
2587 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2588 transform.d[0] = -flip;
2589 transform.d[1] = 0;
2590 transform.d[4] = 0;
2591 transform.d[5] = -1;
2592 break;
2593 case WL_OUTPUT_TRANSFORM_270:
2594 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2595 transform.d[0] = 0;
2596 transform.d[1] = flip;
2597 transform.d[4] = -1;
2598 transform.d[5] = 0;
2599 break;
2600 default:
2601 break;
2602 }
2603
2604 weston_matrix_multiply(&output->matrix, &transform);
2605}
2606
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002607WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002608weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002609{
Scott Moreau850ca422012-05-21 15:21:25 -06002610 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002611 struct weston_matrix camera;
2612 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002613
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002614 weston_matrix_init(&output->matrix);
2615 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002616 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2617 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002618
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002619 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002620 2.0 / (output->width + output->border.left + output->border.right),
2621 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2622
2623 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002624
Scott Moreauccbf29d2012-02-22 14:21:41 -07002625 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002626 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002627 weston_matrix_init(&camera);
2628 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002629 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002630 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002631 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002632 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002633 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002634 weston_matrix_multiply(&output->matrix, &modelview);
2635 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002636
Scott Moreauccbf29d2012-02-22 14:21:41 -07002637 output->dirty = 0;
2638}
2639
Scott Moreau1bad5db2012-08-18 01:04:05 -06002640static void
Alexander Larsson0b135062013-05-28 16:23:36 +02002641weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002642{
2643 output->transform = transform;
2644
2645 switch (transform) {
2646 case WL_OUTPUT_TRANSFORM_90:
2647 case WL_OUTPUT_TRANSFORM_270:
2648 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2649 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2650 /* Swap width and height */
2651 output->width = output->current->height;
2652 output->height = output->current->width;
2653 break;
2654 case WL_OUTPUT_TRANSFORM_NORMAL:
2655 case WL_OUTPUT_TRANSFORM_180:
2656 case WL_OUTPUT_TRANSFORM_FLIPPED:
2657 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2658 output->width = output->current->width;
2659 output->height = output->current->height;
2660 break;
2661 default:
2662 break;
2663 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06002664
Alexander Larsson4ea95522013-05-22 14:41:37 +02002665 output->scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02002666 output->width /= scale;
2667 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002668}
2669
Scott Moreauccbf29d2012-02-22 14:21:41 -07002670WL_EXPORT void
2671weston_output_move(struct weston_output *output, int x, int y)
2672{
2673 output->x = x;
2674 output->y = y;
2675
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002676 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002677 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002678 output->width,
2679 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002680}
2681
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002682WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002683weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02002684 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002685 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002686{
2687 output->compositor = c;
2688 output->x = x;
2689 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002690 output->border.top = 0;
2691 output->border.bottom = 0;
2692 output->border.left = 0;
2693 output->border.right = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02002694 output->mm_width = mm_width;
2695 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002696 output->dirty = 1;
Alexander Larssone32c3762013-05-28 16:23:37 +02002697 output->origin_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002698
Alexander Larsson0b135062013-05-28 16:23:36 +02002699 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06002700 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002701
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002702 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002703 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002704
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002705 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01002706 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002707 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002708 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002709
Casey Dahlin58ba1372012-04-19 22:50:08 -04002710 output->id = ffs(~output->compositor->output_id_pool) - 1;
2711 output->compositor->output_id_pool |= 1 << output->id;
2712
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002713 output->global =
2714 wl_display_add_global(c->wl_display, &wl_output_interface,
2715 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01002716 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002717}
2718
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002719static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002720compositor_bind(struct wl_client *client,
2721 void *data, uint32_t version, uint32_t id)
2722{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002723 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002724
2725 wl_client_add_object(client, &wl_compositor_interface,
2726 &compositor_interface, id, compositor);
2727}
2728
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002729static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002730log_uname(void)
2731{
2732 struct utsname usys;
2733
2734 uname(&usys);
2735
2736 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2737 usys.version, usys.machine);
2738}
2739
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002740WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02002741weston_environment_get_fd(const char *env)
2742{
2743 char *e, *end;
2744 int fd, flags;
2745
2746 e = getenv(env);
2747 if (!e)
2748 return -1;
2749 fd = strtol(e, &end, 0);
2750 if (*end != '\0')
2751 return -1;
2752
2753 flags = fcntl(fd, F_GETFD);
2754 if (flags == -1)
2755 return -1;
2756
2757 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
2758 unsetenv(env);
2759
2760 return fd;
2761}
2762
2763WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002764weston_compositor_init(struct weston_compositor *ec,
2765 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002766 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002767 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002768{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002769 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002770 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002771 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002772
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002773 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002774 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002775 wl_signal_init(&ec->destroy_signal);
2776 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002777 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002778 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002779 wl_signal_init(&ec->idle_signal);
2780 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002781 wl_signal_init(&ec->show_input_panel_signal);
2782 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02002783 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002784 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01002785 wl_signal_init(&ec->output_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002786 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002787
Casey Dahlin58ba1372012-04-19 22:50:08 -04002788 ec->output_id_pool = 0;
2789
Kristian Høgsberga8873122011-11-23 10:39:34 -05002790 if (!wl_display_add_global(display, &wl_compositor_interface,
2791 ec, compositor_bind))
2792 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002793
Pekka Paalanene67858b2013-04-25 13:57:42 +03002794 if (!wl_display_add_global(display, &wl_subcompositor_interface,
2795 ec, bind_subcompositor))
2796 return -1;
2797
Daniel Stone725c2c32012-06-22 14:04:36 +01002798 wl_list_init(&ec->surface_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002799 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002800 wl_list_init(&ec->layer_list);
2801 wl_list_init(&ec->seat_list);
2802 wl_list_init(&ec->output_list);
2803 wl_list_init(&ec->key_binding_list);
2804 wl_list_init(&ec->button_binding_list);
2805 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002806 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002807
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002808 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002809 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002810
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002811 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
2812 weston_config_section_get_string(s, "keymap_rules",
2813 (char **) &xkb_names.rules, NULL);
2814 weston_config_section_get_string(s, "keymap_model",
2815 (char **) &xkb_names.model, NULL);
2816 weston_config_section_get_string(s, "keymap_layout",
2817 (char **) &xkb_names.layout, NULL);
2818 weston_config_section_get_string(s, "keymap_variant",
2819 (char **) &xkb_names.variant, NULL);
2820 weston_config_section_get_string(s, "keymap_options",
2821 (char **) &xkb_names.options, NULL);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002822 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
2823 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01002824
2825 ec->ping_handler = NULL;
2826
2827 screenshooter_create(ec);
2828 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002829 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002830
2831 wl_data_device_manager_init(ec->wl_display);
2832
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002833 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002834
Daniel Stone725c2c32012-06-22 14:04:36 +01002835 loop = wl_display_get_event_loop(ec->wl_display);
2836 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2837 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2838
2839 ec->input_loop = wl_event_loop_create();
2840
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002841 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2842 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2843
2844 weston_compositor_schedule_repaint(ec);
2845
Daniel Stone725c2c32012-06-22 14:04:36 +01002846 return 0;
2847}
2848
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002849WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002850weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002851{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002852 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002853
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002854 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002855 if (ec->input_loop_source)
2856 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002857
Matt Roper361d2ad2011-08-29 13:52:23 -07002858 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002859 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002860 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002861
Daniel Stone325fc2d2012-05-30 16:31:58 +01002862 weston_binding_list_destroy_all(&ec->key_binding_list);
2863 weston_binding_list_destroy_all(&ec->button_binding_list);
2864 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002865 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002866
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002867 weston_plane_release(&ec->primary_plane);
2868
Jonas Ådahlc97af922012-03-28 22:36:09 +02002869 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07002870
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002871 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07002872}
2873
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05002874WL_EXPORT void
2875weston_version(int *major, int *minor, int *micro)
2876{
2877 *major = WESTON_VERSION_MAJOR;
2878 *minor = WESTON_VERSION_MINOR;
2879 *micro = WESTON_VERSION_MICRO;
2880}
2881
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002882static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002883 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002884 const char *desc;
2885} capability_strings[] = {
2886 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002887 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002888};
2889
2890static void
2891weston_compositor_log_capabilities(struct weston_compositor *compositor)
2892{
2893 unsigned i;
2894 int yes;
2895
2896 weston_log("Compositor capabilities:\n");
2897 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
2898 yes = compositor->capabilities & capability_strings[i].bit;
2899 weston_log_continue(STAMP_SPACE "%s %s\n",
2900 capability_strings[i].desc,
2901 yes ? "yes" : "no");
2902 }
2903}
2904
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002905static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002906{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002907 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002908
Martin Minarik6d118362012-06-07 18:01:59 +02002909 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002910 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002911
2912 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002913}
2914
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002915#ifdef HAVE_LIBUNWIND
2916
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002917static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002918print_backtrace(void)
2919{
2920 unw_cursor_t cursor;
2921 unw_context_t context;
2922 unw_word_t off;
2923 unw_proc_info_t pip;
2924 int ret, i = 0;
2925 char procname[256];
2926 const char *filename;
2927 Dl_info dlinfo;
2928
2929 pip.unwind_info = NULL;
2930 ret = unw_getcontext(&context);
2931 if (ret) {
2932 weston_log("unw_getcontext: %d\n", ret);
2933 return;
2934 }
2935
2936 ret = unw_init_local(&cursor, &context);
2937 if (ret) {
2938 weston_log("unw_init_local: %d\n", ret);
2939 return;
2940 }
2941
2942 ret = unw_step(&cursor);
2943 while (ret > 0) {
2944 ret = unw_get_proc_info(&cursor, &pip);
2945 if (ret) {
2946 weston_log("unw_get_proc_info: %d\n", ret);
2947 break;
2948 }
2949
2950 ret = unw_get_proc_name(&cursor, procname, 256, &off);
2951 if (ret && ret != -UNW_ENOMEM) {
2952 if (ret != -UNW_EUNSPEC)
2953 weston_log("unw_get_proc_name: %d\n", ret);
2954 procname[0] = '?';
2955 procname[1] = 0;
2956 }
2957
2958 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
2959 *dlinfo.dli_fname)
2960 filename = dlinfo.dli_fname;
2961 else
2962 filename = "?";
2963
2964 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
2965 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
2966
2967 ret = unw_step(&cursor);
2968 if (ret < 0)
2969 weston_log("unw_step: %d\n", ret);
2970 }
2971}
2972
2973#else
2974
2975static void
2976print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002977{
2978 void *buffer[32];
2979 int i, count;
2980 Dl_info info;
2981
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002982 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2983 for (i = 0; i < count; i++) {
2984 dladdr(buffer[i], &info);
2985 weston_log(" [%016lx] %s (%s)\n",
2986 (long) buffer[i],
2987 info.dli_sname ? info.dli_sname : "--",
2988 info.dli_fname);
2989 }
2990}
2991
2992#endif
2993
2994static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01002995on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002996{
Peter Maatmane5b42e42013-03-27 22:38:53 +01002997 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002998 * then call the backend restore function, which will switch
2999 * back to the vt we launched from or ungrab X etc and then
3000 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003001 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003002 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003003 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003004
Peter Maatmane5b42e42013-03-27 22:38:53 +01003005 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003006
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003007 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003008
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003009 segv_compositor->restore(segv_compositor);
3010
3011 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003012}
3013
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003014static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003015load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003016{
3017 char path[PATH_MAX];
3018 void *module, *init;
3019
3020 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003021 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003022 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003023 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003024
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003025 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3026 if (module) {
3027 weston_log("Module '%s' already loaded\n", path);
3028 dlclose(module);
3029 return NULL;
3030 }
3031
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003032 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003033 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003034 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003035 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003036 return NULL;
3037 }
3038
3039 init = dlsym(module, entrypoint);
3040 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003041 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003042 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003043 return NULL;
3044 }
3045
3046 return init;
3047}
3048
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003049static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003050load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003051 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003052{
3053 const char *p, *end;
3054 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003055 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003056 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003057
3058 if (modules == NULL)
3059 return 0;
3060
3061 p = modules;
3062 while (*p) {
3063 end = strchrnul(p, ',');
3064 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3065 module_init = load_module(buffer, "module_init");
3066 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003067 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003068 p = end;
3069 while (*p == ',')
3070 p++;
3071
3072 }
3073
3074 return 0;
3075}
3076
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003077static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003078 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3079
3080static const char xdg_wrong_message[] =
3081 "fatal: environment variable XDG_RUNTIME_DIR\n"
3082 "is set to \"%s\", which is not a directory.\n";
3083
3084static const char xdg_wrong_mode_message[] =
3085 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3086 "correctly. Unix access mode must be 0700 but is %o,\n"
3087 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003088 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003089
3090static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003091 "Refer to your distribution on how to get it, or\n"
3092 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3093 "on how to implement it.\n";
3094
Martin Minarik37032f82012-06-18 20:15:18 +02003095static void
3096verify_xdg_runtime_dir(void)
3097{
3098 char *dir = getenv("XDG_RUNTIME_DIR");
3099 struct stat s;
3100
3101 if (!dir) {
3102 weston_log(xdg_error_message);
3103 weston_log_continue(xdg_detail_message);
3104 exit(EXIT_FAILURE);
3105 }
3106
3107 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3108 weston_log(xdg_wrong_message, dir);
3109 weston_log_continue(xdg_detail_message);
3110 exit(EXIT_FAILURE);
3111 }
3112
3113 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3114 weston_log(xdg_wrong_mode_message,
3115 dir, s.st_mode & 0777, s.st_uid);
3116 weston_log_continue(xdg_detail_message);
3117 }
3118}
3119
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003120static int
3121usage(int error_code)
3122{
3123 fprintf(stderr,
3124 "Usage: weston [OPTIONS]\n\n"
3125 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3126 "Weston supports multiple backends, and depending on which backend is in use\n"
3127 "different options will be accepted.\n\n"
3128
3129
3130 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003131 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003132 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003133 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3134 "\t\t\t\twayland-backend.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003135 " -S, --socket=NAME\tName of socket to listen on\n"
3136 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003137 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003138 " --log==FILE\t\tLog to the given file\n"
3139 " -h, --help\t\tThis help message\n\n");
3140
3141 fprintf(stderr,
3142 "Options for drm-backend.so:\n\n"
3143 " --connector=ID\tBring up only this connector\n"
3144 " --seat=SEAT\t\tThe seat that weston should run on\n"
3145 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003146 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003147 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3148
3149 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003150 "Options for fbdev-backend.so:\n\n"
3151 " --tty=TTY\t\tThe tty to use\n"
3152 " --device=DEVICE\tThe framebuffer device to use\n\n");
3153
3154 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003155 "Options for x11-backend.so:\n\n"
3156 " --width=WIDTH\t\tWidth of X window\n"
3157 " --height=HEIGHT\tHeight of X window\n"
3158 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003159 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003160 " --output-count=COUNT\tCreate multiple outputs\n"
3161 " --no-input\t\tDont create input devices\n\n");
3162
3163 fprintf(stderr,
3164 "Options for wayland-backend.so:\n\n"
3165 " --width=WIDTH\t\tWidth of Wayland surface\n"
3166 " --height=HEIGHT\tHeight of Wayland surface\n"
3167 " --display=DISPLAY\tWayland display to connect to\n\n");
3168
Pekka Paalanene31e0532013-05-22 18:03:07 +03003169#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3170 fprintf(stderr,
3171 "Options for rpi-backend.so:\n\n"
3172 " --tty=TTY\t\tThe tty to use\n"
3173 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3174 " --transform=TR\tThe output transformation, TR is one of:\n"
3175 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3176 "\n");
3177#endif
3178
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003179 exit(error_code);
3180}
3181
Peter Maatmane5b42e42013-03-27 22:38:53 +01003182static void
3183catch_signals(void)
3184{
3185 struct sigaction action;
3186
3187 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3188 action.sa_sigaction = on_caught_signal;
3189 sigemptyset(&action.sa_mask);
3190 sigaction(SIGSEGV, &action, NULL);
3191 sigaction(SIGABRT, &action, NULL);
3192}
3193
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003194int main(int argc, char *argv[])
3195{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003196 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003197 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003198 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003199 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003200 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003201 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003202 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003203 int *argc, char *argv[],
3204 struct weston_config *config);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003205 int i, config_fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003206 char *backend = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003207 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003208 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003209 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003210 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003211 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003212 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003213 struct weston_config *config;
3214 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003215
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003216 const struct weston_option core_options[] = {
3217 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3218 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3219 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003220 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003221 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003222 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003223 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003224 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003225
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003226 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003227
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003228 if (help)
3229 usage(EXIT_SUCCESS);
3230
Scott Moreau12245142012-08-29 15:15:58 -06003231 if (version) {
3232 printf(PACKAGE_STRING "\n");
3233 return EXIT_SUCCESS;
3234 }
3235
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003236 weston_log_file_open(log);
3237
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003238 weston_log("%s\n"
3239 STAMP_SPACE "%s\n"
3240 STAMP_SPACE "Bug reports to: %s\n"
3241 STAMP_SPACE "Build: %s\n",
3242 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003243 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003244 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003245
Martin Minarik37032f82012-06-18 20:15:18 +02003246 verify_xdg_runtime_dir();
3247
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003248 display = wl_display_create();
3249
Tiago Vignatti2116b892011-08-08 05:52:59 -07003250 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003251 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3252 display);
3253 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3254 display);
3255 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3256 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003257
3258 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003259 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3260 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003261
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003262 if (!backend) {
3263 if (getenv("WAYLAND_DISPLAY"))
3264 backend = "wayland-backend.so";
3265 else if (getenv("DISPLAY"))
3266 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003267 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003268 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003269 }
3270
Ossama Othmana50e6e42013-05-14 09:48:26 -07003271 config_fd = open_config_file("weston.ini");
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003272 config = weston_config_parse(config_fd);
3273 close(config_fd);
3274
3275 section = weston_config_get_section(config, "core", NULL, NULL);
3276 weston_config_section_get_string(section, "modules",
3277 &modules, "desktop-shell.so");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003278
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003279 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003280 if (!backend_init)
3281 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003282
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003283 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003284 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003285 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003286 exit(EXIT_FAILURE);
3287 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003288
Peter Maatmane5b42e42013-03-27 22:38:53 +01003289 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003290 segv_compositor = ec;
3291
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003292 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003293
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003294 setenv("WAYLAND_DISPLAY", socket_name, 1);
3295
Ossama Othmana50e6e42013-05-14 09:48:26 -07003296 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003297 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003298 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003299 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003300
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003301 for (i = 1; i < argc; i++)
3302 weston_log("fatal: unhandled option: %s\n", argv[i]);
3303 if (argc > 1) {
3304 ret = EXIT_FAILURE;
3305 goto out;
3306 }
3307
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003308 weston_compositor_log_capabilities(ec);
3309
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003310 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003311 weston_log("fatal: failed to add socket: %m\n");
3312 ret = EXIT_FAILURE;
3313 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003314 }
3315
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003316 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003317
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003318 wl_display_run(display);
3319
3320 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003321 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003322 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003323
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003324 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003325
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003326 for (i = ARRAY_LENGTH(signals); i;)
3327 wl_event_source_remove(signals[--i]);
3328
Daniel Stone855028f2012-05-01 20:37:10 +01003329 weston_compositor_xkb_destroy(ec);
3330
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003331 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003332 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003333
Martin Minarik19e6f262012-06-07 13:08:46 +02003334 weston_log_file_close();
3335
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003336 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003337}