blob: db5a32d5d4c1a425fe9d9acc93136cdd1ded6f71 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanend581a8f2012-01-27 16:25:16 +02004 * Copyright © 2012 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberg06bc2642010-12-01 09:50:16 -050025#define _GNU_SOURCE
26
Kristian Høgsberga9410222011-01-14 17:22:35 -050027#include "config.h"
28
Daniel Stoneb7452fe2012-06-01 12:14:06 +010029#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040030#include <stdio.h>
31#include <string.h>
32#include <stdlib.h>
33#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010034#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050035#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020036#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040037#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010038#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040039#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020040#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020041#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020042#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040043#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050044#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040045#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040046#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040047#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050048#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040049#include <sys/time.h>
50#include <time.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050051
Marcin Slusarz554a0da2013-02-18 13:27:22 -050052#ifdef HAVE_LIBUNWIND
53#define UNW_LOCAL_ONLY
54#include <libunwind.h>
55#endif
56
Pekka Paalanen50719bc2011-11-22 14:18:50 +020057#include <wayland-server.h>
Kristian Høgsberg82863022010-06-04 21:52:02 -040058#include "compositor.h"
U. Artie Eoffec08f332013-05-13 15:55:47 -070059#include "subsurface-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030060#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040061#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050062#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050063
Kristian Høgsberg27da5382011-06-21 17:32:25 -040064static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040065static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040066
67static int
68sigchld_handler(int signal_number, void *data)
69{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050070 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040071 int status;
72 pid_t pid;
73
Bill Spitzak027b9622012-03-17 15:22:03 -070074 pid = waitpid(-1, &status, WNOHANG);
75 if (!pid)
76 return 1;
77
Kristian Høgsberg27da5382011-06-21 17:32:25 -040078 wl_list_for_each(p, &child_process_list, link) {
79 if (p->pid == pid)
80 break;
81 }
82
83 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020084 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040085 return 1;
86 }
87
88 wl_list_remove(&p->link);
89 p->cleanup(p, status);
90
91 return 1;
92}
93
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020094static void
Alexander Larsson0b135062013-05-28 16:23:36 +020095weston_output_transform_scale_init(struct weston_output *output,
96 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020097
Alex Wu2dda6042012-04-17 17:20:47 +080098WL_EXPORT int
99weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode)
100{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200101 struct weston_seat *seat;
102 pixman_region32_t old_output_region;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200103 int ret;
104
Alex Wu2dda6042012-04-17 17:20:47 +0800105 if (!output->switch_mode)
106 return -1;
107
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200108 ret = output->switch_mode(output, mode);
109 if (ret < 0)
110 return ret;
111
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);
222 exit(-1);
223 }
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
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400279 wl_signal_init(&surface->resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500280
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500281 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500282 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500283
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400284 surface->resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400285
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;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400526 struct wl_client *client = es->resource.client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400527
528 es->output_mask = mask;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400529 if (es->resource.client == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400530 return;
531 if (different == 0)
532 return;
533
534 wl_list_for_each(output, &es->compositor->output_list, link) {
535 if (1 << output->id & different)
536 resource =
537 find_resource_for_client(&output->resource_list,
538 client);
539 if (resource == NULL)
540 continue;
541 if (1 << output->id & entered)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400542 wl_surface_send_enter(&es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400543 if (1 << output->id & left)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400544 wl_surface_send_leave(&es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400545 }
546}
547
548static void
549weston_surface_assign_output(struct weston_surface *es)
550{
551 struct weston_compositor *ec = es->compositor;
552 struct weston_output *output, *new_output;
553 pixman_region32_t region;
554 uint32_t max, area, mask;
555 pixman_box32_t *e;
556
557 new_output = NULL;
558 max = 0;
559 mask = 0;
560 pixman_region32_init(&region);
561 wl_list_for_each(output, &ec->output_list, link) {
562 pixman_region32_intersect(&region, &es->transform.boundingbox,
563 &output->region);
564
565 e = pixman_region32_extents(&region);
566 area = (e->x2 - e->x1) * (e->y2 - e->y1);
567
568 if (area > 0)
569 mask |= 1 << output->id;
570
571 if (area >= max) {
572 new_output = output;
573 max = area;
574 }
575 }
576 pixman_region32_fini(&region);
577
578 es->output = new_output;
579 weston_surface_update_output_mask(es, mask);
580}
581
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200582static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200583surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
584 int32_t width, int32_t height,
585 pixman_region32_t *bbox)
586{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200587 float min_x = HUGE_VALF, min_y = HUGE_VALF;
588 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200589 int32_t s[4][2] = {
590 { sx, sy },
591 { sx, sy + height },
592 { sx + width, sy },
593 { sx + width, sy + height }
594 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200595 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200596 int i;
597
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300598 if (width == 0 || height == 0) {
599 /* avoid rounding empty bbox to 1x1 */
600 pixman_region32_init(bbox);
601 return;
602 }
603
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200604 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200605 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400606 weston_surface_to_global_float(surface,
607 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200608 if (x < min_x)
609 min_x = x;
610 if (x > max_x)
611 max_x = x;
612 if (y < min_y)
613 min_y = y;
614 if (y > max_y)
615 max_y = y;
616 }
617
Pekka Paalanen219b9822012-02-08 15:38:37 +0200618 int_x = floorf(min_x);
619 int_y = floorf(min_y);
620 pixman_region32_init_rect(bbox, int_x, int_y,
621 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200622}
623
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200624static void
625weston_surface_update_transform_disable(struct weston_surface *surface)
626{
627 surface->transform.enabled = 0;
628
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200629 /* round off fractions when not transformed */
630 surface->geometry.x = roundf(surface->geometry.x);
631 surface->geometry.y = roundf(surface->geometry.y);
632
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500633 /* Otherwise identity matrix, but with x and y translation. */
634 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
635 surface->transform.position.matrix.d[12] = surface->geometry.x;
636 surface->transform.position.matrix.d[13] = surface->geometry.y;
637
638 surface->transform.matrix = surface->transform.position.matrix;
639
Kristian Høgsberg9bcaaeb2013-02-28 14:56:43 -0500640 surface->transform.inverse = surface->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500641 surface->transform.inverse.d[12] = -surface->geometry.x;
642 surface->transform.inverse.d[13] = -surface->geometry.y;
643
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200644 pixman_region32_init_rect(&surface->transform.boundingbox,
645 surface->geometry.x,
646 surface->geometry.y,
647 surface->geometry.width,
648 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500649
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400650 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500651 pixman_region32_copy(&surface->transform.opaque,
652 &surface->opaque);
653 pixman_region32_translate(&surface->transform.opaque,
654 surface->geometry.x,
655 surface->geometry.y);
656 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200657}
658
659static int
660weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200661{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200662 struct weston_surface *parent = surface->geometry.parent;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200663 struct weston_matrix *matrix = &surface->transform.matrix;
664 struct weston_matrix *inverse = &surface->transform.inverse;
665 struct weston_transform *tform;
666
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200667 surface->transform.enabled = 1;
668
669 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300670 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200671 surface->transform.position.matrix.d[12] = surface->geometry.x;
672 surface->transform.position.matrix.d[13] = surface->geometry.y;
673
674 weston_matrix_init(matrix);
675 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
676 weston_matrix_multiply(matrix, &tform->matrix);
677
Pekka Paalanen483243f2013-03-08 14:56:50 +0200678 if (parent)
679 weston_matrix_multiply(matrix, &parent->transform.matrix);
680
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200681 if (weston_matrix_invert(inverse, matrix) < 0) {
682 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200683 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200684 " transformation not invertible.\n", surface);
685 return -1;
686 }
687
688 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
689 surface->geometry.height,
690 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500691
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200692 return 0;
693}
694
695WL_EXPORT void
696weston_surface_update_transform(struct weston_surface *surface)
697{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200698 struct weston_surface *parent = surface->geometry.parent;
699
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200700 if (!surface->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200701 return;
702
Pekka Paalanen483243f2013-03-08 14:56:50 +0200703 if (parent)
704 weston_surface_update_transform(parent);
705
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200706 surface->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200707
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500708 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200709
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200710 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500711 pixman_region32_fini(&surface->transform.opaque);
712 pixman_region32_init(&surface->transform.opaque);
713
Pekka Paalanencd403622012-01-25 13:37:39 +0200714 /* transform.position is always in transformation_list */
715 if (surface->geometry.transformation_list.next ==
716 &surface->transform.position.link &&
717 surface->geometry.transformation_list.prev ==
Pekka Paalanen483243f2013-03-08 14:56:50 +0200718 &surface->transform.position.link &&
719 !parent) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200720 weston_surface_update_transform_disable(surface);
721 } else {
722 if (weston_surface_update_transform_enable(surface) < 0)
723 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200724 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200725
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400726 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200727
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300728 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200729}
730
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200731WL_EXPORT void
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200732weston_surface_geometry_dirty(struct weston_surface *surface)
733{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200734 struct weston_surface *child;
735
736 /*
737 * The invariant: if surface->geometry.dirty, then all surfaces
738 * in surface->geometry.child_list have geometry.dirty too.
739 * Corollary: if not parent->geometry.dirty, then all ancestors
740 * are not dirty.
741 */
742
743 if (surface->transform.dirty)
744 return;
745
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200746 surface->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200747
748 wl_list_for_each(child, &surface->geometry.child_list,
749 geometry.parent_link)
750 weston_surface_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200751}
752
753WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100754weston_surface_to_global_fixed(struct weston_surface *surface,
755 wl_fixed_t sx, wl_fixed_t sy,
756 wl_fixed_t *x, wl_fixed_t *y)
757{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200758 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100759
760 weston_surface_to_global_float(surface,
761 wl_fixed_to_double(sx),
762 wl_fixed_to_double(sy),
763 &xf, &yf);
764 *x = wl_fixed_from_double(xf);
765 *y = wl_fixed_from_double(yf);
766}
767
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400768WL_EXPORT void
769weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200770 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200771{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200772 if (surface->transform.enabled) {
773 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
774
775 weston_matrix_transform(&surface->transform.inverse, &v);
776
777 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200778 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200779 "weston_surface_from_global(), divisor = %g\n",
780 v.f[3]);
781 *sx = 0;
782 *sy = 0;
783 return;
784 }
785
Pekka Paalanencd403622012-01-25 13:37:39 +0200786 *sx = v.f[0] / v.f[3];
787 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200788 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200789 *sx = x - surface->geometry.x;
790 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200791 }
792}
793
794WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100795weston_surface_from_global_fixed(struct weston_surface *surface,
796 wl_fixed_t x, wl_fixed_t y,
797 wl_fixed_t *sx, wl_fixed_t *sy)
798{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200799 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100800
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400801 weston_surface_from_global_float(surface,
802 wl_fixed_to_double(x),
803 wl_fixed_to_double(y),
804 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100805 *sx = wl_fixed_from_double(sxf);
806 *sy = wl_fixed_from_double(syf);
807}
808
809WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200810weston_surface_from_global(struct weston_surface *surface,
811 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
812{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200813 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200814
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400815 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200816 *sx = floorf(sxf);
817 *sy = floorf(syf);
818}
819
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400820WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400821weston_surface_schedule_repaint(struct weston_surface *surface)
822{
823 struct weston_output *output;
824
825 wl_list_for_each(output, &surface->compositor->output_list, link)
826 if (surface->output_mask & (1 << output->id))
827 weston_output_schedule_repaint(output);
828}
829
830WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500831weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500832{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400833 pixman_region32_union_rect(&surface->damage, &surface->damage,
834 0, 0, surface->geometry.width,
835 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200836
Kristian Høgsberg98238702012-08-03 16:29:12 -0400837 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500838}
839
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400840WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500841weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200842 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400843{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200844 surface->geometry.x = x;
845 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200846 surface->geometry.width = width;
847 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200848 weston_surface_geometry_dirty(surface);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400849}
850
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200851WL_EXPORT void
852weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200853 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200854{
855 surface->geometry.x = x;
856 surface->geometry.y = y;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200857 weston_surface_geometry_dirty(surface);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200858}
859
Pekka Paalanen483243f2013-03-08 14:56:50 +0200860static void
861transform_parent_handle_parent_destroy(struct wl_listener *listener,
862 void *data)
863{
864 struct weston_surface *surface =
865 container_of(listener, struct weston_surface,
866 geometry.parent_destroy_listener);
867
868 weston_surface_set_transform_parent(surface, NULL);
869}
870
871WL_EXPORT void
872weston_surface_set_transform_parent(struct weston_surface *surface,
873 struct weston_surface *parent)
874{
875 if (surface->geometry.parent) {
876 wl_list_remove(&surface->geometry.parent_destroy_listener.link);
877 wl_list_remove(&surface->geometry.parent_link);
878 }
879
880 surface->geometry.parent = parent;
881
882 surface->geometry.parent_destroy_listener.notify =
883 transform_parent_handle_parent_destroy;
884 if (parent) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400885 wl_signal_add(&parent->resource.destroy_signal,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200886 &surface->geometry.parent_destroy_listener);
887 wl_list_insert(&parent->geometry.child_list,
888 &surface->geometry.parent_link);
889 }
890
891 weston_surface_geometry_dirty(surface);
892}
893
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300894WL_EXPORT int
895weston_surface_is_mapped(struct weston_surface *surface)
896{
897 if (surface->output)
898 return 1;
899 else
900 return 0;
901}
902
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200903WL_EXPORT int32_t
904weston_surface_buffer_width(struct weston_surface *surface)
905{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200906 int32_t width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200907 switch (surface->buffer_transform) {
908 case WL_OUTPUT_TRANSFORM_90:
909 case WL_OUTPUT_TRANSFORM_270:
910 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
911 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200912 width = surface->buffer_ref.buffer->height;
913 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200914 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200915 width = surface->buffer_ref.buffer->width;
916 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200917 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200918 return width / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200919}
920
921WL_EXPORT int32_t
922weston_surface_buffer_height(struct weston_surface *surface)
923{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200924 int32_t height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200925 switch (surface->buffer_transform) {
926 case WL_OUTPUT_TRANSFORM_90:
927 case WL_OUTPUT_TRANSFORM_270:
928 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
929 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200930 height = surface->buffer_ref.buffer->width;
931 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200932 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200933 height = surface->buffer_ref.buffer->height;
934 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200935 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200936 return height / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200937}
938
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400939WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500940weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500941{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400942 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500943
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400944 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500945
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400946 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500947}
948
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400949WL_EXPORT struct weston_surface *
Tiago Vignatti9d393522012-02-10 16:26:19 +0200950weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100951 wl_fixed_t x, wl_fixed_t y,
952 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200953{
954 struct weston_surface *surface;
955
956 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100957 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500958 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100959 wl_fixed_to_int(*sx),
960 wl_fixed_to_int(*sy),
961 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200962 return surface;
963 }
964
965 return NULL;
966}
967
968static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500969weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400970{
Daniel Stone37816df2012-05-16 18:45:18 +0100971 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400972
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500973 if (!compositor->focus)
974 return;
975
Daniel Stone37816df2012-05-16 18:45:18 +0100976 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400977 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400978}
979
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400980WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500981weston_surface_unmap(struct weston_surface *surface)
982{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100983 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500984
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500985 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500986 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500987 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500988
Daniel Stone4dab5db2012-05-30 16:31:53 +0100989 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400990 if (seat->keyboard && seat->keyboard->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400991 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400992 if (seat->pointer && seat->pointer->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400993 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400994 NULL,
995 wl_fixed_from_int(0),
996 wl_fixed_from_int(0));
Daniel Stone4dab5db2012-05-30 16:31:53 +0100997 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500998
Kristian Høgsberg98238702012-08-03 16:29:12 -0400999 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001000}
1001
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001002struct weston_frame_callback {
1003 struct wl_resource resource;
1004 struct wl_list link;
1005};
1006
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001007static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001008destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001009{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001010 struct weston_surface *surface =
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001011 container_of(resource, struct weston_surface, resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001012 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001013 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001014
Pekka Paalanen483243f2013-03-08 14:56:50 +02001015 assert(wl_list_empty(&surface->geometry.child_list));
Pekka Paalanene67858b2013-04-25 13:57:42 +03001016 assert(wl_list_empty(&surface->subsurface_list_pending));
1017 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001018
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001019 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001020 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001021
Pekka Paalanenbc106382012-10-10 12:49:31 +03001022 wl_list_for_each_safe(cb, next,
1023 &surface->pending.frame_callback_list, link)
1024 wl_resource_destroy(&cb->resource);
1025
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001026 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001027 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001028 pixman_region32_fini(&surface->pending.damage);
1029
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001030 if (surface->pending.buffer)
1031 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1032
Pekka Paalanende685b82012-12-04 15:58:12 +02001033 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001034
Kristian Høgsberg42263852012-09-06 21:59:29 -04001035 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001036
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001037 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001038 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001039 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001040 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001041 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001042
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001043 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
1044 wl_resource_destroy(&cb->resource);
1045
Pekka Paalanen483243f2013-03-08 14:56:50 +02001046 weston_surface_set_transform_parent(surface, NULL);
1047
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001048 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001049}
1050
Alex Wu8811bf92012-02-28 18:07:54 +08001051WL_EXPORT void
1052weston_surface_destroy(struct weston_surface *surface)
1053{
1054 /* Not a valid way to destroy a client surface */
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001055 assert(surface->resource.client == NULL);
Alex Wu8811bf92012-02-28 18:07:54 +08001056
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001057 wl_signal_emit(&surface->resource.destroy_signal, &surface->resource);
1058 destroy_surface(&surface->resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001059}
1060
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001061static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001062weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1063 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001064{
Pekka Paalanende685b82012-12-04 15:58:12 +02001065 struct weston_buffer_reference *ref =
1066 container_of(listener, struct weston_buffer_reference,
1067 destroy_listener);
1068
1069 assert((struct wl_buffer *)data == ref->buffer);
1070 ref->buffer = NULL;
1071}
1072
1073WL_EXPORT void
1074weston_buffer_reference(struct weston_buffer_reference *ref,
1075 struct wl_buffer *buffer)
1076{
1077 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001078 ref->buffer->busy_count--;
1079 if (ref->buffer->busy_count == 0) {
1080 assert(ref->buffer->resource.client != NULL);
1081 wl_resource_queue_event(&ref->buffer->resource,
1082 WL_BUFFER_RELEASE);
1083 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001084 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001085 }
1086
Pekka Paalanende685b82012-12-04 15:58:12 +02001087 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001088 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001089 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001090 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001091 }
1092
Pekka Paalanende685b82012-12-04 15:58:12 +02001093 ref->buffer = buffer;
1094 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1095}
1096
1097static void
1098weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
1099{
1100 weston_buffer_reference(&surface->buffer_ref, buffer);
1101
Pekka Paalanena6421c42012-12-04 15:58:10 +02001102 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001103 if (weston_surface_is_mapped(surface))
1104 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001105 }
1106
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001107 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001108}
1109
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001110WL_EXPORT void
1111weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001112{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001113 wl_list_remove(&surface->layer_link);
1114 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001115 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001116 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001117}
1118
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001119WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001120weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001121{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001122 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001123
1124 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001125 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001126}
1127
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001128WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001129weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001130{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001131 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001132
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001133 pixman_region32_union(&compositor->primary_plane.damage,
1134 &compositor->primary_plane.damage,
1135 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001136 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001137}
1138
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001139static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001140surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001141 pixman_region32_t *opaque)
1142{
Pekka Paalanende685b82012-12-04 15:58:12 +02001143 if (surface->buffer_ref.buffer &&
1144 wl_buffer_is_shm(surface->buffer_ref.buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001145 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001146
1147 if (surface->transform.enabled) {
1148 pixman_box32_t *extents;
1149
1150 extents = pixman_region32_extents(&surface->damage);
1151 surface_compute_bbox(surface, extents->x1, extents->y1,
1152 extents->x2 - extents->x1,
1153 extents->y2 - extents->y1,
1154 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001155 pixman_region32_translate(&surface->damage,
1156 -surface->plane->x,
1157 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001158 } else {
1159 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001160 surface->geometry.x - surface->plane->x,
1161 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001162 }
1163
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001164 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001165 pixman_region32_union(&surface->plane->damage,
1166 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001167 empty_region(&surface->damage);
1168 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001169 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001170}
1171
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001172static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001173compositor_accumulate_damage(struct weston_compositor *ec)
1174{
1175 struct weston_plane *plane;
1176 struct weston_surface *es;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001177 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001178
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001179 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001180
1181 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001182 pixman_region32_copy(&plane->clip, &clip);
1183
1184 pixman_region32_init(&opaque);
1185
1186 wl_list_for_each(es, &ec->surface_list, link) {
1187 if (es->plane != plane)
1188 continue;
1189
1190 surface_accumulate_damage(es, &opaque);
1191 }
1192
1193 pixman_region32_union(&clip, &clip, &opaque);
1194 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001195 }
1196
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001197 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001198
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001199 wl_list_for_each(es, &ec->surface_list, link) {
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001200 /* Both the renderer and the backend have seen the buffer
1201 * by now. If renderer needs the buffer, it has its own
1202 * reference set. If the backend wants to keep the buffer
1203 * around for migrating the surface into a non-primary plane
1204 * later, keep_buffer is true. Otherwise, drop the core
1205 * reference now, and allow early buffer release. This enables
1206 * clients to use single-buffering.
1207 */
1208 if (!es->keep_buffer)
1209 weston_buffer_reference(&es->buffer_ref, NULL);
1210 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001211}
1212
1213static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001214surface_list_add(struct weston_compositor *compositor,
1215 struct weston_surface *surface)
1216{
1217 struct weston_subsurface *sub;
1218
1219 if (wl_list_empty(&surface->subsurface_list)) {
1220 weston_surface_update_transform(surface);
1221 wl_list_insert(compositor->surface_list.prev, &surface->link);
1222 return;
1223 }
1224
1225 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1226 if (!weston_surface_is_mapped(sub->surface))
1227 continue;
1228
1229 if (sub->surface == surface) {
1230 weston_surface_update_transform(sub->surface);
1231 wl_list_insert(compositor->surface_list.prev,
1232 &sub->surface->link);
1233 } else {
1234 surface_list_add(compositor, sub->surface);
1235 }
1236 }
1237}
1238
1239static void
1240weston_compositor_build_surface_list(struct weston_compositor *compositor)
1241{
1242 struct weston_surface *surface;
1243 struct weston_layer *layer;
1244
1245 wl_list_init(&compositor->surface_list);
1246 wl_list_for_each(layer, &compositor->layer_list, link) {
1247 wl_list_for_each(surface, &layer->surface_list, layer_link) {
1248 surface_list_add(compositor, surface);
1249 }
1250 }
1251}
1252
1253static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001254weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001255{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001256 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001257 struct weston_surface *es;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001258 struct weston_animation *animation, *next;
1259 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001260 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001261 pixman_region32_t output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001262
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001263 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001264 weston_compositor_build_surface_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001265
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001266 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001267 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001268 else
1269 wl_list_for_each(es, &ec->surface_list, link)
1270 weston_surface_move_to_plane(es, &ec->primary_plane);
1271
Pekka Paalanene67858b2013-04-25 13:57:42 +03001272 wl_list_init(&frame_callback_list);
1273 wl_list_for_each(es, &ec->surface_list, link) {
1274 if (es->output == output) {
1275 wl_list_insert_list(&frame_callback_list,
1276 &es->frame_callback_list);
1277 wl_list_init(&es->frame_callback_list);
1278 }
1279 }
1280
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001281 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001282
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001283 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001284 pixman_region32_intersect(&output_damage,
1285 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001286 pixman_region32_subtract(&output_damage,
1287 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001288
Scott Moreauccbf29d2012-02-22 14:21:41 -07001289 if (output->dirty)
1290 weston_output_update_matrix(output);
1291
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001292 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001293
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001294 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001295
Kristian Høgsbergef044142011-06-21 15:02:12 -04001296 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001297
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001298 weston_compositor_repick(ec);
1299 wl_event_loop_dispatch(ec->input_loop, 0);
1300
Jonas Ådahldb773762012-06-13 00:01:21 +02001301 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001302 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001303 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001304 }
1305
Scott Moreaud64cf212012-06-08 19:40:54 -06001306 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001307 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001308 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001309 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001310}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001311
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001312static int
1313weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001314{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001315 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001316
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001317 wl_event_loop_dispatch(compositor->input_loop, 0);
1318
1319 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001320}
1321
1322WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001323weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001324{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001325 struct weston_compositor *compositor = output->compositor;
1326 struct wl_event_loop *loop =
1327 wl_display_get_event_loop(compositor->wl_display);
1328 int fd;
1329
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001330 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001331 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001332 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001333 return;
1334 }
1335
1336 output->repaint_scheduled = 0;
1337 if (compositor->input_loop_source)
1338 return;
1339
1340 fd = wl_event_loop_get_fd(compositor->input_loop);
1341 compositor->input_loop_source =
1342 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1343 weston_compositor_read_input, compositor);
1344}
1345
1346static void
1347idle_repaint(void *data)
1348{
1349 struct weston_output *output = data;
1350
Jonas Ådahle5a12252013-04-05 23:07:11 +02001351 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001352}
1353
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001354WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001355weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1356{
1357 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001358 if (below != NULL)
1359 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001360}
1361
1362WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001363weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001364{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001365 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001366 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001367
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001368 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1369 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001370 return;
1371
Kristian Høgsbergef044142011-06-21 15:02:12 -04001372 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001373 output->repaint_needed = 1;
1374 if (output->repaint_scheduled)
1375 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001376
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001377 wl_event_loop_add_idle(loop, idle_repaint, output);
1378 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001379
1380 if (compositor->input_loop_source) {
1381 wl_event_source_remove(compositor->input_loop_source);
1382 compositor->input_loop_source = NULL;
1383 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001384}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001385
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001386WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001387weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1388{
1389 struct weston_output *output;
1390
1391 wl_list_for_each(output, &compositor->output_list, link)
1392 weston_output_schedule_repaint(output);
1393}
1394
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001395static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001396surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001397{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001398 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001399}
1400
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001401static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001402surface_attach(struct wl_client *client,
1403 struct wl_resource *resource,
1404 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1405{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001406 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001407 struct wl_buffer *buffer = NULL;
1408
1409 if (buffer_resource)
1410 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001411
Pekka Paalanende685b82012-12-04 15:58:12 +02001412 /* Attach, attach, without commit in between does not send
1413 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001414 if (surface->pending.buffer)
1415 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001416
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001417 surface->pending.sx = sx;
1418 surface->pending.sy = sy;
1419 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001420 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001421 if (buffer) {
1422 wl_signal_add(&buffer->resource.destroy_signal,
1423 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001424 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001425}
1426
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001427static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001428surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001429 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001430 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001431{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001432 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001433
Pekka Paalanen8e159182012-10-10 12:49:25 +03001434 pixman_region32_union_rect(&surface->pending.damage,
1435 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001436 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001437}
1438
Kristian Høgsberg33418202011-08-16 23:01:28 -04001439static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001440destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001441{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001442 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001443
1444 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001445 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001446}
1447
1448static void
1449surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001450 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001451{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001452 struct weston_frame_callback *cb;
Pekka Paalanenbc106382012-10-10 12:49:31 +03001453 struct weston_surface *surface = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001454
1455 cb = malloc(sizeof *cb);
1456 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001457 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001458 return;
1459 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001460
Kristian Høgsberg33418202011-08-16 23:01:28 -04001461 cb->resource.object.interface = &wl_callback_interface;
1462 cb->resource.object.id = callback;
1463 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001464 cb->resource.client = client;
1465 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001466
1467 wl_client_add_resource(client, &cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001468 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001469}
1470
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001471static void
1472surface_set_opaque_region(struct wl_client *client,
1473 struct wl_resource *resource,
1474 struct wl_resource *region_resource)
1475{
1476 struct weston_surface *surface = resource->data;
1477 struct weston_region *region;
1478
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001479 if (region_resource) {
1480 region = region_resource->data;
Pekka Paalanen512dde82012-10-10 12:49:27 +03001481 pixman_region32_copy(&surface->pending.opaque,
1482 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001483 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001484 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001485 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001486}
1487
1488static void
1489surface_set_input_region(struct wl_client *client,
1490 struct wl_resource *resource,
1491 struct wl_resource *region_resource)
1492{
1493 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001494 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001495
1496 if (region_resource) {
1497 region = region_resource->data;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001498 pixman_region32_copy(&surface->pending.input,
1499 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001500 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001501 pixman_region32_fini(&surface->pending.input);
1502 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001503 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001504}
1505
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001506static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001507weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001508{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001509 struct weston_subsurface *sub;
1510
1511 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1512 parent_link_pending) {
1513 wl_list_remove(&sub->parent_link);
1514 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1515 }
1516}
1517
1518static void
1519weston_surface_commit(struct weston_surface *surface)
1520{
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001521 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001522 int surface_width = 0;
1523 int surface_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001524
Alexander Larsson4ea95522013-05-22 14:41:37 +02001525 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001526 surface->buffer_transform = surface->pending.buffer_transform;
1527
Alexander Larsson4ea95522013-05-22 14:41:37 +02001528 /* wl_surface.set_buffer_scale */
1529 surface->buffer_scale = surface->pending.buffer_scale;
1530
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001531 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001532 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001533 weston_surface_attach(surface, surface->pending.buffer);
1534
Giulio Camuffo184df502013-02-21 11:29:21 +01001535 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001536 surface_width = weston_surface_buffer_width(surface);
1537 surface_height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001538 }
1539
1540 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001541 surface->configure(surface,
1542 surface->pending.sx, surface->pending.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001543 surface_width, surface_height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001544
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001545 if (surface->pending.buffer)
1546 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1547 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001548 surface->pending.sx = 0;
1549 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001550 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001551
1552 /* wl_surface.damage */
1553 pixman_region32_union(&surface->damage, &surface->damage,
1554 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001555 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1556 0, 0,
1557 surface->geometry.width,
1558 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001559 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001560
1561 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001562 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001563 surface->geometry.width,
1564 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001565 pixman_region32_intersect(&opaque,
1566 &opaque, &surface->pending.opaque);
1567
1568 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1569 pixman_region32_copy(&surface->opaque, &opaque);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001570 weston_surface_geometry_dirty(surface);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001571 }
1572
1573 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001574
1575 /* wl_surface.set_input_region */
1576 pixman_region32_fini(&surface->input);
1577 pixman_region32_init_rect(&surface->input, 0, 0,
1578 surface->geometry.width,
1579 surface->geometry.height);
1580 pixman_region32_intersect(&surface->input,
1581 &surface->input, &surface->pending.input);
1582
Pekka Paalanenbc106382012-10-10 12:49:31 +03001583 /* wl_surface.frame */
1584 wl_list_insert_list(&surface->frame_callback_list,
1585 &surface->pending.frame_callback_list);
1586 wl_list_init(&surface->pending.frame_callback_list);
1587
Pekka Paalanene67858b2013-04-25 13:57:42 +03001588 weston_surface_commit_subsurface_order(surface);
1589
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001590 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001591}
1592
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001593static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001594weston_subsurface_commit(struct weston_subsurface *sub);
1595
1596static void
1597weston_subsurface_parent_commit(struct weston_subsurface *sub,
1598 int parent_is_synchronized);
1599
1600static void
1601surface_commit(struct wl_client *client, struct wl_resource *resource)
1602{
1603 struct weston_surface *surface = resource->data;
1604 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1605
1606 if (sub) {
1607 weston_subsurface_commit(sub);
1608 return;
1609 }
1610
1611 weston_surface_commit(surface);
1612
1613 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1614 if (sub->surface != surface)
1615 weston_subsurface_parent_commit(sub, 0);
1616 }
1617}
1618
1619static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001620surface_set_buffer_transform(struct wl_client *client,
1621 struct wl_resource *resource, int transform)
1622{
1623 struct weston_surface *surface = resource->data;
1624
1625 surface->pending.buffer_transform = transform;
1626}
1627
Alexander Larsson4ea95522013-05-22 14:41:37 +02001628static void
1629surface_set_buffer_scale(struct wl_client *client,
1630 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001631 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02001632{
1633 struct weston_surface *surface = resource->data;
1634
1635 surface->pending.buffer_scale = scale;
1636}
1637
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001638static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001639 surface_destroy,
1640 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001641 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001642 surface_frame,
1643 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001644 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001645 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001646 surface_set_buffer_transform,
1647 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001648};
1649
1650static void
1651compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001652 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001653{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001654 struct weston_compositor *ec = resource->data;
1655 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001656
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001657 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001658 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001659 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001660 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001661 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001662
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001663 surface->resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001664
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001665 surface->resource.object.id = id;
1666 surface->resource.object.interface = &wl_surface_interface;
1667 surface->resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001668 (void (**)(void)) &surface_interface;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001669 surface->resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001670
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001671 wl_client_add_resource(client, &surface->resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001672}
1673
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001674static void
1675destroy_region(struct wl_resource *resource)
1676{
1677 struct weston_region *region =
1678 container_of(resource, struct weston_region, resource);
1679
1680 pixman_region32_fini(&region->region);
1681 free(region);
1682}
1683
1684static void
1685region_destroy(struct wl_client *client, struct wl_resource *resource)
1686{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001687 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001688}
1689
1690static void
1691region_add(struct wl_client *client, struct wl_resource *resource,
1692 int32_t x, int32_t y, int32_t width, int32_t height)
1693{
1694 struct weston_region *region = resource->data;
1695
1696 pixman_region32_union_rect(&region->region, &region->region,
1697 x, y, width, height);
1698}
1699
1700static void
1701region_subtract(struct wl_client *client, struct wl_resource *resource,
1702 int32_t x, int32_t y, int32_t width, int32_t height)
1703{
1704 struct weston_region *region = resource->data;
1705 pixman_region32_t rect;
1706
1707 pixman_region32_init_rect(&rect, x, y, width, height);
1708 pixman_region32_subtract(&region->region, &region->region, &rect);
1709 pixman_region32_fini(&rect);
1710}
1711
1712static const struct wl_region_interface region_interface = {
1713 region_destroy,
1714 region_add,
1715 region_subtract
1716};
1717
1718static void
1719compositor_create_region(struct wl_client *client,
1720 struct wl_resource *resource, uint32_t id)
1721{
1722 struct weston_region *region;
1723
1724 region = malloc(sizeof *region);
1725 if (region == NULL) {
1726 wl_resource_post_no_memory(resource);
1727 return;
1728 }
1729
1730 region->resource.destroy = destroy_region;
1731
1732 region->resource.object.id = id;
1733 region->resource.object.interface = &wl_region_interface;
1734 region->resource.object.implementation =
1735 (void (**)(void)) &region_interface;
1736 region->resource.data = region;
1737
1738 pixman_region32_init(&region->region);
1739
1740 wl_client_add_resource(client, &region->resource);
1741}
1742
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001743static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001744 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001745 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001746};
1747
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001748static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001749weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
1750{
1751 struct weston_surface *surface = sub->surface;
1752 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001753 int surface_width = 0;
1754 int surface_height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001755
Alexander Larsson4ea95522013-05-22 14:41:37 +02001756 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001757 surface->buffer_transform = sub->cached.buffer_transform;
1758
Alexander Larsson4ea95522013-05-22 14:41:37 +02001759 /* wl_surface.set_buffer_scale */
1760 surface->buffer_scale = sub->cached.buffer_scale;
1761
Pekka Paalanene67858b2013-04-25 13:57:42 +03001762 /* wl_surface.attach */
1763 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
1764 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
1765 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
1766
1767 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001768 surface_width = weston_surface_buffer_width(surface);
1769 surface_height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001770 }
1771
1772 if (surface->configure && sub->cached.newly_attached)
1773 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001774 surface_width, surface_height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001775 sub->cached.sx = 0;
1776 sub->cached.sy = 0;
1777 sub->cached.newly_attached = 0;
1778
1779 /* wl_surface.damage */
1780 pixman_region32_union(&surface->damage, &surface->damage,
1781 &sub->cached.damage);
1782 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1783 0, 0,
1784 surface->geometry.width,
1785 surface->geometry.height);
1786 empty_region(&sub->cached.damage);
1787
1788 /* wl_surface.set_opaque_region */
1789 pixman_region32_init_rect(&opaque, 0, 0,
1790 surface->geometry.width,
1791 surface->geometry.height);
1792 pixman_region32_intersect(&opaque,
1793 &opaque, &sub->cached.opaque);
1794
1795 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1796 pixman_region32_copy(&surface->opaque, &opaque);
1797 weston_surface_geometry_dirty(surface);
1798 }
1799
1800 pixman_region32_fini(&opaque);
1801
1802 /* wl_surface.set_input_region */
1803 pixman_region32_fini(&surface->input);
1804 pixman_region32_init_rect(&surface->input, 0, 0,
1805 surface->geometry.width,
1806 surface->geometry.height);
1807 pixman_region32_intersect(&surface->input,
1808 &surface->input, &sub->cached.input);
1809
1810 /* wl_surface.frame */
1811 wl_list_insert_list(&surface->frame_callback_list,
1812 &sub->cached.frame_callback_list);
1813 wl_list_init(&sub->cached.frame_callback_list);
1814
1815 weston_surface_commit_subsurface_order(surface);
1816
1817 weston_surface_schedule_repaint(surface);
1818
1819 sub->cached.has_data = 0;
1820}
1821
1822static void
1823weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
1824{
1825 struct weston_surface *surface = sub->surface;
1826
1827 /*
1828 * If this commit would cause the surface to move by the
1829 * attach(dx, dy) parameters, the old damage region must be
1830 * translated to correspond to the new surface coordinate system
1831 * origin.
1832 */
1833 pixman_region32_translate(&sub->cached.damage,
1834 -surface->pending.sx, -surface->pending.sy);
1835 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
1836 &surface->pending.damage);
1837 empty_region(&surface->pending.damage);
1838
1839 if (surface->pending.newly_attached) {
1840 sub->cached.newly_attached = 1;
1841 weston_buffer_reference(&sub->cached.buffer_ref,
1842 surface->pending.buffer);
1843 }
1844 sub->cached.sx += surface->pending.sx;
1845 sub->cached.sy += surface->pending.sy;
1846 surface->pending.sx = 0;
1847 surface->pending.sy = 0;
1848 surface->pending.newly_attached = 0;
1849
1850 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001851 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001852
1853 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
1854
1855 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
1856
1857 wl_list_insert_list(&sub->cached.frame_callback_list,
1858 &surface->pending.frame_callback_list);
1859 wl_list_init(&surface->pending.frame_callback_list);
1860
1861 sub->cached.has_data = 1;
1862}
1863
1864static int
1865weston_subsurface_is_synchronized(struct weston_subsurface *sub)
1866{
1867 while (sub) {
1868 if (sub->synchronized)
1869 return 1;
1870
1871 if (!sub->parent)
1872 return 0;
1873
1874 sub = weston_surface_to_subsurface(sub->parent);
1875 }
1876
1877 return 0;
1878}
1879
1880static void
1881weston_subsurface_commit(struct weston_subsurface *sub)
1882{
1883 struct weston_surface *surface = sub->surface;
1884 struct weston_subsurface *tmp;
1885
1886 /* Recursive check for effectively synchronized. */
1887 if (weston_subsurface_is_synchronized(sub)) {
1888 weston_subsurface_commit_to_cache(sub);
1889 } else {
1890 if (sub->cached.has_data) {
1891 /* flush accumulated state from cache */
1892 weston_subsurface_commit_to_cache(sub);
1893 weston_subsurface_commit_from_cache(sub);
1894 } else {
1895 weston_surface_commit(surface);
1896 }
1897
1898 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1899 if (tmp->surface != surface)
1900 weston_subsurface_parent_commit(tmp, 0);
1901 }
1902 }
1903}
1904
1905static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001906weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001907{
1908 struct weston_surface *surface = sub->surface;
1909 struct weston_subsurface *tmp;
1910
Pekka Paalanene67858b2013-04-25 13:57:42 +03001911 /* From now on, commit_from_cache the whole sub-tree, regardless of
1912 * the synchronized mode of each child. This sub-surface or some
1913 * of its ancestors were synchronized, so we are synchronized
1914 * all the way down.
1915 */
1916
1917 if (sub->cached.has_data)
1918 weston_subsurface_commit_from_cache(sub);
1919
1920 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1921 if (tmp->surface != surface)
1922 weston_subsurface_parent_commit(tmp, 1);
1923 }
1924}
1925
1926static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001927weston_subsurface_parent_commit(struct weston_subsurface *sub,
1928 int parent_is_synchronized)
1929{
1930 if (sub->position.set) {
1931 weston_surface_set_position(sub->surface,
1932 sub->position.x, sub->position.y);
1933 sub->position.set = 0;
1934 }
1935
1936 if (parent_is_synchronized || sub->synchronized)
1937 weston_subsurface_synchronized_commit(sub);
1938}
1939
1940static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001941subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
1942 int32_t width, int32_t height)
1943{
1944 struct weston_compositor *compositor = surface->compositor;
1945
1946 weston_surface_configure(surface,
1947 surface->geometry.x + dx,
1948 surface->geometry.y + dy,
1949 width, height);
1950
1951 /* No need to check parent mappedness, because if parent is not
1952 * mapped, parent is not in a visible layer, so this sub-surface
1953 * will not be drawn either.
1954 */
1955 if (!weston_surface_is_mapped(surface)) {
1956 wl_list_init(&surface->layer_link);
1957
1958 /* Cannot call weston_surface_update_transform(),
1959 * because that would call it also for the parent surface,
1960 * which might not be mapped yet. That would lead to
1961 * inconsistent state, where the window could never be
1962 * mapped.
1963 *
1964 * Instead just assing any output, to make
1965 * weston_surface_is_mapped() return true, so that when the
1966 * parent surface does get mapped, this one will get
1967 * included, too. See surface_list_add().
1968 */
1969 assert(!wl_list_empty(&compositor->output_list));
1970 surface->output = container_of(compositor->output_list.next,
1971 struct weston_output, link);
1972 }
1973}
1974
1975static struct weston_subsurface *
1976weston_surface_to_subsurface(struct weston_surface *surface)
1977{
1978 if (surface->configure == subsurface_configure)
1979 return surface->configure_private;
1980
1981 return NULL;
1982}
1983
Pekka Paalanen01388e22013-04-25 13:57:44 +03001984WL_EXPORT struct weston_surface *
1985weston_surface_get_main_surface(struct weston_surface *surface)
1986{
1987 struct weston_subsurface *sub;
1988
1989 while (surface && (sub = weston_surface_to_subsurface(surface)))
1990 surface = sub->parent;
1991
1992 return surface;
1993}
1994
Pekka Paalanene67858b2013-04-25 13:57:42 +03001995static void
1996subsurface_set_position(struct wl_client *client,
1997 struct wl_resource *resource, int32_t x, int32_t y)
1998{
1999 struct weston_subsurface *sub = resource->data;
2000
2001 if (!sub)
2002 return;
2003
2004 sub->position.x = x;
2005 sub->position.y = y;
2006 sub->position.set = 1;
2007}
2008
2009static struct weston_subsurface *
2010subsurface_from_surface(struct weston_surface *surface)
2011{
2012 struct weston_subsurface *sub;
2013
2014 sub = weston_surface_to_subsurface(surface);
2015 if (sub)
2016 return sub;
2017
2018 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2019 if (sub->surface == surface)
2020 return sub;
2021
2022 return NULL;
2023}
2024
2025static struct weston_subsurface *
2026subsurface_sibling_check(struct weston_subsurface *sub,
2027 struct weston_surface *surface,
2028 const char *request)
2029{
2030 struct weston_subsurface *sibling;
2031
2032 sibling = subsurface_from_surface(surface);
2033
2034 if (!sibling) {
2035 wl_resource_post_error(sub->resource,
2036 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2037 "%s: wl_surface@%d is not a parent or sibling",
2038 request, surface->resource.object.id);
2039 return NULL;
2040 }
2041
2042 if (sibling->parent != sub->parent) {
2043 wl_resource_post_error(sub->resource,
2044 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2045 "%s: wl_surface@%d has a different parent",
2046 request, surface->resource.object.id);
2047 return NULL;
2048 }
2049
2050 return sibling;
2051}
2052
2053static void
2054subsurface_place_above(struct wl_client *client,
2055 struct wl_resource *resource,
2056 struct wl_resource *sibling_resource)
2057{
2058 struct weston_subsurface *sub = resource->data;
2059 struct weston_surface *surface = sibling_resource->data;
2060 struct weston_subsurface *sibling;
2061
2062 if (!sub)
2063 return;
2064
2065 sibling = subsurface_sibling_check(sub, surface, "place_above");
2066 if (!sibling)
2067 return;
2068
2069 wl_list_remove(&sub->parent_link_pending);
2070 wl_list_insert(sibling->parent_link_pending.prev,
2071 &sub->parent_link_pending);
2072}
2073
2074static void
2075subsurface_place_below(struct wl_client *client,
2076 struct wl_resource *resource,
2077 struct wl_resource *sibling_resource)
2078{
2079 struct weston_subsurface *sub = resource->data;
2080 struct weston_surface *surface = sibling_resource->data;
2081 struct weston_subsurface *sibling;
2082
2083 if (!sub)
2084 return;
2085
2086 sibling = subsurface_sibling_check(sub, surface, "place_below");
2087 if (!sibling)
2088 return;
2089
2090 wl_list_remove(&sub->parent_link_pending);
2091 wl_list_insert(&sibling->parent_link_pending,
2092 &sub->parent_link_pending);
2093}
2094
2095static void
2096subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2097{
2098 struct weston_subsurface *sub = resource->data;
2099
2100 if (sub)
2101 sub->synchronized = 1;
2102}
2103
2104static void
2105subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2106{
2107 struct weston_subsurface *sub = resource->data;
2108
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002109 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002110 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002111
2112 /* If sub became effectively desynchronized, flush. */
2113 if (!weston_subsurface_is_synchronized(sub))
2114 weston_subsurface_synchronized_commit(sub);
2115 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002116}
2117
2118static void
2119weston_subsurface_cache_init(struct weston_subsurface *sub)
2120{
2121 pixman_region32_init(&sub->cached.damage);
2122 pixman_region32_init(&sub->cached.opaque);
2123 pixman_region32_init(&sub->cached.input);
2124 wl_list_init(&sub->cached.frame_callback_list);
2125 sub->cached.buffer_ref.buffer = NULL;
2126}
2127
2128static void
2129weston_subsurface_cache_fini(struct weston_subsurface *sub)
2130{
2131 struct weston_frame_callback *cb, *tmp;
2132
2133 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
2134 wl_resource_destroy(&cb->resource);
2135
2136 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2137 pixman_region32_fini(&sub->cached.damage);
2138 pixman_region32_fini(&sub->cached.opaque);
2139 pixman_region32_fini(&sub->cached.input);
2140}
2141
2142static void
2143weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2144{
2145 wl_list_remove(&sub->parent_link);
2146 wl_list_remove(&sub->parent_link_pending);
2147 wl_list_remove(&sub->parent_destroy_listener.link);
2148 sub->parent = NULL;
2149}
2150
2151static void
2152weston_subsurface_destroy(struct weston_subsurface *sub);
2153
2154static void
2155subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2156{
2157 struct weston_subsurface *sub =
2158 container_of(listener, struct weston_subsurface,
2159 surface_destroy_listener);
2160 assert(data == &sub->surface->resource);
2161
2162 /* The protocol object (wl_resource) is left inert. */
2163 if (sub->resource)
2164 sub->resource->data = NULL;
2165
2166 weston_subsurface_destroy(sub);
2167}
2168
2169static void
2170subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2171{
2172 struct weston_subsurface *sub =
2173 container_of(listener, struct weston_subsurface,
2174 parent_destroy_listener);
2175 assert(data == &sub->parent->resource);
2176 assert(sub->surface != sub->parent);
2177
2178 if (weston_surface_is_mapped(sub->surface))
2179 weston_surface_unmap(sub->surface);
2180
2181 weston_subsurface_unlink_parent(sub);
2182}
2183
2184static void
2185subsurface_resource_destroy(struct wl_resource *resource)
2186{
2187 struct weston_subsurface *sub = resource->data;
2188
2189 if (sub)
2190 weston_subsurface_destroy(sub);
2191
2192 free(resource);
2193}
2194
2195static void
2196subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2197{
2198 wl_resource_destroy(resource);
2199}
2200
2201static void
2202weston_subsurface_link_parent(struct weston_subsurface *sub,
2203 struct weston_surface *parent)
2204{
2205 sub->parent = parent;
2206 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
2207 wl_signal_add(&parent->resource.destroy_signal,
2208 &sub->parent_destroy_listener);
2209
2210 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2211 wl_list_insert(&parent->subsurface_list_pending,
2212 &sub->parent_link_pending);
2213}
2214
2215static void
2216weston_subsurface_link_surface(struct weston_subsurface *sub,
2217 struct weston_surface *surface)
2218{
2219 sub->surface = surface;
2220 sub->surface_destroy_listener.notify =
2221 subsurface_handle_surface_destroy;
2222 wl_signal_add(&surface->resource.destroy_signal,
2223 &sub->surface_destroy_listener);
2224}
2225
2226static void
2227weston_subsurface_destroy(struct weston_subsurface *sub)
2228{
2229 assert(sub->surface);
2230
2231 if (sub->resource) {
2232 assert(weston_surface_to_subsurface(sub->surface) == sub);
2233 assert(sub->parent_destroy_listener.notify ==
2234 subsurface_handle_parent_destroy);
2235
2236 weston_surface_set_transform_parent(sub->surface, NULL);
2237 if (sub->parent)
2238 weston_subsurface_unlink_parent(sub);
2239
2240 weston_subsurface_cache_fini(sub);
2241
2242 sub->surface->configure = NULL;
2243 sub->surface->configure_private = NULL;
2244 } else {
2245 /* the dummy weston_subsurface for the parent itself */
2246 assert(sub->parent_destroy_listener.notify == NULL);
2247 wl_list_remove(&sub->parent_link);
2248 wl_list_remove(&sub->parent_link_pending);
2249 }
2250
2251 wl_list_remove(&sub->surface_destroy_listener.link);
2252 free(sub);
2253}
2254
2255static const struct wl_subsurface_interface subsurface_implementation = {
2256 subsurface_destroy,
2257 subsurface_set_position,
2258 subsurface_place_above,
2259 subsurface_place_below,
2260 subsurface_set_sync,
2261 subsurface_set_desync
2262};
2263
2264static struct weston_subsurface *
2265weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2266 struct weston_surface *parent)
2267{
2268 struct weston_subsurface *sub;
2269
2270 sub = calloc(1, sizeof *sub);
2271 if (!sub)
2272 return NULL;
2273
2274 sub->resource = wl_client_add_object(surface->resource.client,
2275 &wl_subsurface_interface,
2276 &subsurface_implementation,
2277 id, sub);
2278 if (!sub->resource) {
2279 free(sub);
2280 return NULL;
2281 }
2282
2283 sub->resource->destroy = subsurface_resource_destroy;
2284 weston_subsurface_link_surface(sub, surface);
2285 weston_subsurface_link_parent(sub, parent);
2286 weston_subsurface_cache_init(sub);
2287 sub->synchronized = 1;
2288 weston_surface_set_transform_parent(surface, parent);
2289
2290 return sub;
2291}
2292
2293/* Create a dummy subsurface for having the parent itself in its
2294 * sub-surface lists. Makes stacking order manipulation easy.
2295 */
2296static struct weston_subsurface *
2297weston_subsurface_create_for_parent(struct weston_surface *parent)
2298{
2299 struct weston_subsurface *sub;
2300
2301 sub = calloc(1, sizeof *sub);
2302 if (!sub)
2303 return NULL;
2304
2305 weston_subsurface_link_surface(sub, parent);
2306 sub->parent = parent;
2307 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2308 wl_list_insert(&parent->subsurface_list_pending,
2309 &sub->parent_link_pending);
2310
2311 return sub;
2312}
2313
2314static void
2315subcompositor_get_subsurface(struct wl_client *client,
2316 struct wl_resource *resource,
2317 uint32_t id,
2318 struct wl_resource *surface_resource,
2319 struct wl_resource *parent_resource)
2320{
2321 struct weston_surface *surface = surface_resource->data;
2322 struct weston_surface *parent = parent_resource->data;
2323 struct weston_subsurface *sub;
2324 static const char where[] = "get_subsurface: wl_subsurface@";
2325
2326 if (surface == parent) {
2327 wl_resource_post_error(resource,
2328 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2329 "%s%d: wl_surface@%d cannot be its own parent",
2330 where, id, surface_resource->object.id);
2331 return;
2332 }
2333
2334 if (weston_surface_to_subsurface(surface)) {
2335 wl_resource_post_error(resource,
2336 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2337 "%s%d: wl_surface@%d is already a sub-surface",
2338 where, id, surface_resource->object.id);
2339 return;
2340 }
2341
2342 if (surface->configure) {
2343 wl_resource_post_error(resource,
2344 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2345 "%s%d: wl_surface@%d already has a role",
2346 where, id, surface_resource->object.id);
2347 return;
2348 }
2349
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002350 if (weston_surface_get_main_surface(parent) == surface) {
2351 wl_resource_post_error(resource,
2352 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2353 "%s%d: wl_surface@%d is an ancestor of parent",
2354 where, id, surface_resource->object.id);
2355 return;
2356 }
2357
Pekka Paalanene67858b2013-04-25 13:57:42 +03002358 /* make sure the parent is in its own list */
2359 if (wl_list_empty(&parent->subsurface_list)) {
2360 if (!weston_subsurface_create_for_parent(parent)) {
2361 wl_resource_post_no_memory(resource);
2362 return;
2363 }
2364 }
2365
2366 sub = weston_subsurface_create(id, surface, parent);
2367 if (!sub) {
2368 wl_resource_post_no_memory(resource);
2369 return;
2370 }
2371
2372 surface->configure = subsurface_configure;
2373 surface->configure_private = sub;
2374}
2375
2376static void
2377subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2378{
2379 wl_resource_destroy(resource);
2380}
2381
2382static const struct wl_subcompositor_interface subcompositor_interface = {
2383 subcompositor_destroy,
2384 subcompositor_get_subsurface
2385};
2386
2387static void
2388bind_subcompositor(struct wl_client *client,
2389 void *data, uint32_t version, uint32_t id)
2390{
2391 struct weston_compositor *compositor = data;
2392
2393 wl_client_add_object(client, &wl_subcompositor_interface,
2394 &subcompositor_interface, id, compositor);
2395}
2396
2397static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002398weston_compositor_dpms(struct weston_compositor *compositor,
2399 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002400{
2401 struct weston_output *output;
2402
2403 wl_list_for_each(output, &compositor->output_list, link)
2404 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002405 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002406}
2407
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002408WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002409weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002410{
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002411 switch (compositor->state) {
2412 case WESTON_COMPOSITOR_SLEEPING:
2413 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2414 /* fall through */
2415 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002416 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002417 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002418 /* fall through */
2419 default:
2420 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2421 wl_event_source_timer_update(compositor->idle_source,
2422 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002423 }
2424}
2425
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002426WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002427weston_compositor_offscreen(struct weston_compositor *compositor)
2428{
2429 switch (compositor->state) {
2430 case WESTON_COMPOSITOR_OFFSCREEN:
2431 return;
2432 case WESTON_COMPOSITOR_SLEEPING:
2433 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2434 /* fall through */
2435 default:
2436 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2437 wl_event_source_timer_update(compositor->idle_source, 0);
2438 }
2439}
2440
2441WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002442weston_compositor_sleep(struct weston_compositor *compositor)
2443{
2444 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2445 return;
2446
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002447 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002448 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2449 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2450}
2451
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002452static int
2453idle_handler(void *data)
2454{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002455 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002456
2457 if (compositor->idle_inhibit)
2458 return 1;
2459
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002460 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002461 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002462
2463 return 1;
2464}
2465
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002466WL_EXPORT void
2467weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2468{
2469 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002470 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002471 plane->x = x;
2472 plane->y = y;
2473}
2474
2475WL_EXPORT void
2476weston_plane_release(struct weston_plane *plane)
2477{
2478 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002479 pixman_region32_fini(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002480}
2481
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002482WL_EXPORT void
2483weston_compositor_stack_plane(struct weston_compositor *ec,
2484 struct weston_plane *plane,
2485 struct weston_plane *above)
2486{
2487 if (above)
2488 wl_list_insert(above->link.prev, &plane->link);
2489 else
2490 wl_list_insert(&ec->plane_list, &plane->link);
2491}
2492
Casey Dahlin9074db52012-04-19 22:50:09 -04002493static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002494{
2495 wl_list_remove(&resource->link);
2496 free(resource);
2497}
2498
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002499static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002500bind_output(struct wl_client *client,
2501 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002502{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002503 struct weston_output *output = data;
2504 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002505 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002506
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002507 resource = wl_client_add_object(client,
2508 &wl_output_interface, NULL, id, data);
2509
Casey Dahlin9074db52012-04-19 22:50:09 -04002510 wl_list_insert(&output->resource_list, &resource->link);
2511 resource->destroy = unbind_resource;
2512
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002513 wl_output_send_geometry(resource,
2514 output->x,
2515 output->y,
2516 output->mm_width,
2517 output->mm_height,
2518 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002519 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002520 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002521 if (version >= 2)
2522 wl_output_send_scale(resource,
2523 output->scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002524
2525 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002526 wl_output_send_mode(resource,
2527 mode->flags,
2528 mode->width,
2529 mode->height,
2530 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002531 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002532
2533 if (version >= 2)
2534 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002535}
2536
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002537WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002538weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002539{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002540 struct weston_compositor *c = output->compositor;
2541
Richard Hughes64ddde12013-05-01 21:52:10 +01002542 wl_signal_emit(&output->destroy_signal, output);
2543
Richard Hughesafe690c2013-05-02 10:10:04 +01002544 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002545 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002546 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002547 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002548
2549 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002550}
2551
Scott Moreau1bad5db2012-08-18 01:04:05 -06002552static void
2553weston_output_compute_transform(struct weston_output *output)
2554{
2555 struct weston_matrix transform;
2556 int flip;
2557
2558 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002559 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002560
2561 switch(output->transform) {
2562 case WL_OUTPUT_TRANSFORM_FLIPPED:
2563 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2564 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2565 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002566 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002567 flip = -1;
2568 break;
2569 default:
2570 flip = 1;
2571 break;
2572 }
2573
2574 switch(output->transform) {
2575 case WL_OUTPUT_TRANSFORM_NORMAL:
2576 case WL_OUTPUT_TRANSFORM_FLIPPED:
2577 transform.d[0] = flip;
2578 transform.d[1] = 0;
2579 transform.d[4] = 0;
2580 transform.d[5] = 1;
2581 break;
2582 case WL_OUTPUT_TRANSFORM_90:
2583 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2584 transform.d[0] = 0;
2585 transform.d[1] = -flip;
2586 transform.d[4] = 1;
2587 transform.d[5] = 0;
2588 break;
2589 case WL_OUTPUT_TRANSFORM_180:
2590 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2591 transform.d[0] = -flip;
2592 transform.d[1] = 0;
2593 transform.d[4] = 0;
2594 transform.d[5] = -1;
2595 break;
2596 case WL_OUTPUT_TRANSFORM_270:
2597 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2598 transform.d[0] = 0;
2599 transform.d[1] = flip;
2600 transform.d[4] = -1;
2601 transform.d[5] = 0;
2602 break;
2603 default:
2604 break;
2605 }
2606
2607 weston_matrix_multiply(&output->matrix, &transform);
2608}
2609
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002610WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002611weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002612{
Scott Moreau850ca422012-05-21 15:21:25 -06002613 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002614 struct weston_matrix camera;
2615 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002616
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002617 weston_matrix_init(&output->matrix);
2618 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002619 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2620 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002621
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002622 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002623 2.0 / (output->width + output->border.left + output->border.right),
2624 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2625
2626 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002627
Scott Moreauccbf29d2012-02-22 14:21:41 -07002628 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002629 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002630 weston_matrix_init(&camera);
2631 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002632 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002633 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002634 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002635 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002636 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002637 weston_matrix_multiply(&output->matrix, &modelview);
2638 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002639
Scott Moreauccbf29d2012-02-22 14:21:41 -07002640 output->dirty = 0;
2641}
2642
Scott Moreau1bad5db2012-08-18 01:04:05 -06002643static void
Alexander Larsson0b135062013-05-28 16:23:36 +02002644weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002645{
2646 output->transform = transform;
2647
2648 switch (transform) {
2649 case WL_OUTPUT_TRANSFORM_90:
2650 case WL_OUTPUT_TRANSFORM_270:
2651 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2652 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2653 /* Swap width and height */
2654 output->width = output->current->height;
2655 output->height = output->current->width;
2656 break;
2657 case WL_OUTPUT_TRANSFORM_NORMAL:
2658 case WL_OUTPUT_TRANSFORM_180:
2659 case WL_OUTPUT_TRANSFORM_FLIPPED:
2660 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2661 output->width = output->current->width;
2662 output->height = output->current->height;
2663 break;
2664 default:
2665 break;
2666 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06002667
Alexander Larsson4ea95522013-05-22 14:41:37 +02002668 output->scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02002669 output->width /= scale;
2670 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002671}
2672
Scott Moreauccbf29d2012-02-22 14:21:41 -07002673WL_EXPORT void
2674weston_output_move(struct weston_output *output, int x, int y)
2675{
2676 output->x = x;
2677 output->y = y;
2678
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002679 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002680 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002681 output->width,
2682 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002683}
2684
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002685WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002686weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02002687 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002688 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002689{
2690 output->compositor = c;
2691 output->x = x;
2692 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002693 output->border.top = 0;
2694 output->border.bottom = 0;
2695 output->border.left = 0;
2696 output->border.right = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02002697 output->mm_width = mm_width;
2698 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002699 output->dirty = 1;
Alexander Larssone32c3762013-05-28 16:23:37 +02002700 output->origin_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002701
Alexander Larsson0b135062013-05-28 16:23:36 +02002702 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06002703 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002704
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002705 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002706 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002707
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002708 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01002709 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002710 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002711 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002712
Casey Dahlin58ba1372012-04-19 22:50:08 -04002713 output->id = ffs(~output->compositor->output_id_pool) - 1;
2714 output->compositor->output_id_pool |= 1 << output->id;
2715
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002716 output->global =
2717 wl_display_add_global(c->wl_display, &wl_output_interface,
2718 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01002719 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002720}
2721
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002722static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002723compositor_bind(struct wl_client *client,
2724 void *data, uint32_t version, uint32_t id)
2725{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002726 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002727
2728 wl_client_add_object(client, &wl_compositor_interface,
2729 &compositor_interface, id, compositor);
2730}
2731
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002732static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002733log_uname(void)
2734{
2735 struct utsname usys;
2736
2737 uname(&usys);
2738
2739 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2740 usys.version, usys.machine);
2741}
2742
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002743WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02002744weston_environment_get_fd(const char *env)
2745{
2746 char *e, *end;
2747 int fd, flags;
2748
2749 e = getenv(env);
2750 if (!e)
2751 return -1;
2752 fd = strtol(e, &end, 0);
2753 if (*end != '\0')
2754 return -1;
2755
2756 flags = fcntl(fd, F_GETFD);
2757 if (flags == -1)
2758 return -1;
2759
2760 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
2761 unsetenv(env);
2762
2763 return fd;
2764}
2765
2766WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002767weston_compositor_init(struct weston_compositor *ec,
2768 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002769 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002770 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002771{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002772 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002773 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002774 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002775
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002776 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002777 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002778 wl_signal_init(&ec->destroy_signal);
2779 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002780 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002781 wl_signal_init(&ec->idle_signal);
2782 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002783 wl_signal_init(&ec->show_input_panel_signal);
2784 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02002785 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002786 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01002787 wl_signal_init(&ec->output_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002788 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002789
Casey Dahlin58ba1372012-04-19 22:50:08 -04002790 ec->output_id_pool = 0;
2791
Kristian Høgsberga8873122011-11-23 10:39:34 -05002792 if (!wl_display_add_global(display, &wl_compositor_interface,
2793 ec, compositor_bind))
2794 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002795
Pekka Paalanene67858b2013-04-25 13:57:42 +03002796 if (!wl_display_add_global(display, &wl_subcompositor_interface,
2797 ec, bind_subcompositor))
2798 return -1;
2799
Daniel Stone725c2c32012-06-22 14:04:36 +01002800 wl_list_init(&ec->surface_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002801 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002802 wl_list_init(&ec->layer_list);
2803 wl_list_init(&ec->seat_list);
2804 wl_list_init(&ec->output_list);
2805 wl_list_init(&ec->key_binding_list);
2806 wl_list_init(&ec->button_binding_list);
2807 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002808 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002809
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002810 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002811 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002812
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002813 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
2814 weston_config_section_get_string(s, "keymap_rules",
2815 (char **) &xkb_names.rules, NULL);
2816 weston_config_section_get_string(s, "keymap_model",
2817 (char **) &xkb_names.model, NULL);
2818 weston_config_section_get_string(s, "keymap_layout",
2819 (char **) &xkb_names.layout, NULL);
2820 weston_config_section_get_string(s, "keymap_variant",
2821 (char **) &xkb_names.variant, NULL);
2822 weston_config_section_get_string(s, "keymap_options",
2823 (char **) &xkb_names.options, NULL);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002824 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
2825 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01002826
2827 ec->ping_handler = NULL;
2828
2829 screenshooter_create(ec);
2830 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002831 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002832
2833 wl_data_device_manager_init(ec->wl_display);
2834
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002835 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002836
Daniel Stone725c2c32012-06-22 14:04:36 +01002837 loop = wl_display_get_event_loop(ec->wl_display);
2838 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2839 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2840
2841 ec->input_loop = wl_event_loop_create();
2842
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002843 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2844 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2845
2846 weston_compositor_schedule_repaint(ec);
2847
Daniel Stone725c2c32012-06-22 14:04:36 +01002848 return 0;
2849}
2850
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002851WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002852weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002853{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002854 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002855
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002856 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002857 if (ec->input_loop_source)
2858 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002859
Matt Roper361d2ad2011-08-29 13:52:23 -07002860 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002861 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002862 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002863
Daniel Stone325fc2d2012-05-30 16:31:58 +01002864 weston_binding_list_destroy_all(&ec->key_binding_list);
2865 weston_binding_list_destroy_all(&ec->button_binding_list);
2866 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002867 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002868
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002869 weston_plane_release(&ec->primary_plane);
2870
Jonas Ådahlc97af922012-03-28 22:36:09 +02002871 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07002872
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002873 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07002874}
2875
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05002876WL_EXPORT void
2877weston_version(int *major, int *minor, int *micro)
2878{
2879 *major = WESTON_VERSION_MAJOR;
2880 *minor = WESTON_VERSION_MINOR;
2881 *micro = WESTON_VERSION_MICRO;
2882}
2883
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002884static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002885 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002886 const char *desc;
2887} capability_strings[] = {
2888 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002889 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002890};
2891
2892static void
2893weston_compositor_log_capabilities(struct weston_compositor *compositor)
2894{
2895 unsigned i;
2896 int yes;
2897
2898 weston_log("Compositor capabilities:\n");
2899 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
2900 yes = compositor->capabilities & capability_strings[i].bit;
2901 weston_log_continue(STAMP_SPACE "%s %s\n",
2902 capability_strings[i].desc,
2903 yes ? "yes" : "no");
2904 }
2905}
2906
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002907static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002908{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002909 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002910
Martin Minarik6d118362012-06-07 18:01:59 +02002911 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002912 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002913
2914 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002915}
2916
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002917#ifdef HAVE_LIBUNWIND
2918
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002919static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002920print_backtrace(void)
2921{
2922 unw_cursor_t cursor;
2923 unw_context_t context;
2924 unw_word_t off;
2925 unw_proc_info_t pip;
2926 int ret, i = 0;
2927 char procname[256];
2928 const char *filename;
2929 Dl_info dlinfo;
2930
2931 pip.unwind_info = NULL;
2932 ret = unw_getcontext(&context);
2933 if (ret) {
2934 weston_log("unw_getcontext: %d\n", ret);
2935 return;
2936 }
2937
2938 ret = unw_init_local(&cursor, &context);
2939 if (ret) {
2940 weston_log("unw_init_local: %d\n", ret);
2941 return;
2942 }
2943
2944 ret = unw_step(&cursor);
2945 while (ret > 0) {
2946 ret = unw_get_proc_info(&cursor, &pip);
2947 if (ret) {
2948 weston_log("unw_get_proc_info: %d\n", ret);
2949 break;
2950 }
2951
2952 ret = unw_get_proc_name(&cursor, procname, 256, &off);
2953 if (ret && ret != -UNW_ENOMEM) {
2954 if (ret != -UNW_EUNSPEC)
2955 weston_log("unw_get_proc_name: %d\n", ret);
2956 procname[0] = '?';
2957 procname[1] = 0;
2958 }
2959
2960 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
2961 *dlinfo.dli_fname)
2962 filename = dlinfo.dli_fname;
2963 else
2964 filename = "?";
2965
2966 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
2967 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
2968
2969 ret = unw_step(&cursor);
2970 if (ret < 0)
2971 weston_log("unw_step: %d\n", ret);
2972 }
2973}
2974
2975#else
2976
2977static void
2978print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002979{
2980 void *buffer[32];
2981 int i, count;
2982 Dl_info info;
2983
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002984 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2985 for (i = 0; i < count; i++) {
2986 dladdr(buffer[i], &info);
2987 weston_log(" [%016lx] %s (%s)\n",
2988 (long) buffer[i],
2989 info.dli_sname ? info.dli_sname : "--",
2990 info.dli_fname);
2991 }
2992}
2993
2994#endif
2995
2996static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01002997on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002998{
Peter Maatmane5b42e42013-03-27 22:38:53 +01002999 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003000 * then call the backend restore function, which will switch
3001 * back to the vt we launched from or ungrab X etc and then
3002 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003003 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003004 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003005 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003006
Peter Maatmane5b42e42013-03-27 22:38:53 +01003007 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003008
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003009 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003010
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003011 segv_compositor->restore(segv_compositor);
3012
3013 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003014}
3015
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003016static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003017load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003018{
3019 char path[PATH_MAX];
3020 void *module, *init;
3021
3022 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003023 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003024 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003025 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003026
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003027 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3028 if (module) {
3029 weston_log("Module '%s' already loaded\n", path);
3030 dlclose(module);
3031 return NULL;
3032 }
3033
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003034 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003035 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003036 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003037 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003038 return NULL;
3039 }
3040
3041 init = dlsym(module, entrypoint);
3042 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003043 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003044 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003045 return NULL;
3046 }
3047
3048 return init;
3049}
3050
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003051static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003052load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003053 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003054{
3055 const char *p, *end;
3056 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003057 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003058 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003059
3060 if (modules == NULL)
3061 return 0;
3062
3063 p = modules;
3064 while (*p) {
3065 end = strchrnul(p, ',');
3066 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3067 module_init = load_module(buffer, "module_init");
3068 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003069 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003070 p = end;
3071 while (*p == ',')
3072 p++;
3073
3074 }
3075
3076 return 0;
3077}
3078
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003079static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003080 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3081
3082static const char xdg_wrong_message[] =
3083 "fatal: environment variable XDG_RUNTIME_DIR\n"
3084 "is set to \"%s\", which is not a directory.\n";
3085
3086static const char xdg_wrong_mode_message[] =
3087 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3088 "correctly. Unix access mode must be 0700 but is %o,\n"
3089 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003090 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003091
3092static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003093 "Refer to your distribution on how to get it, or\n"
3094 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3095 "on how to implement it.\n";
3096
Martin Minarik37032f82012-06-18 20:15:18 +02003097static void
3098verify_xdg_runtime_dir(void)
3099{
3100 char *dir = getenv("XDG_RUNTIME_DIR");
3101 struct stat s;
3102
3103 if (!dir) {
3104 weston_log(xdg_error_message);
3105 weston_log_continue(xdg_detail_message);
3106 exit(EXIT_FAILURE);
3107 }
3108
3109 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3110 weston_log(xdg_wrong_message, dir);
3111 weston_log_continue(xdg_detail_message);
3112 exit(EXIT_FAILURE);
3113 }
3114
3115 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3116 weston_log(xdg_wrong_mode_message,
3117 dir, s.st_mode & 0777, s.st_uid);
3118 weston_log_continue(xdg_detail_message);
3119 }
3120}
3121
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003122static int
3123usage(int error_code)
3124{
3125 fprintf(stderr,
3126 "Usage: weston [OPTIONS]\n\n"
3127 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3128 "Weston supports multiple backends, and depending on which backend is in use\n"
3129 "different options will be accepted.\n\n"
3130
3131
3132 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003133 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003134 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003135 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3136 "\t\t\t\twayland-backend.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003137 " -S, --socket=NAME\tName of socket to listen on\n"
3138 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003139 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003140 " --log==FILE\t\tLog to the given file\n"
3141 " -h, --help\t\tThis help message\n\n");
3142
3143 fprintf(stderr,
3144 "Options for drm-backend.so:\n\n"
3145 " --connector=ID\tBring up only this connector\n"
3146 " --seat=SEAT\t\tThe seat that weston should run on\n"
3147 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003148 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003149 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3150
3151 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003152 "Options for fbdev-backend.so:\n\n"
3153 " --tty=TTY\t\tThe tty to use\n"
3154 " --device=DEVICE\tThe framebuffer device to use\n\n");
3155
3156 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003157 "Options for x11-backend.so:\n\n"
3158 " --width=WIDTH\t\tWidth of X window\n"
3159 " --height=HEIGHT\tHeight of X window\n"
3160 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003161 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003162 " --output-count=COUNT\tCreate multiple outputs\n"
3163 " --no-input\t\tDont create input devices\n\n");
3164
3165 fprintf(stderr,
3166 "Options for wayland-backend.so:\n\n"
3167 " --width=WIDTH\t\tWidth of Wayland surface\n"
3168 " --height=HEIGHT\tHeight of Wayland surface\n"
3169 " --display=DISPLAY\tWayland display to connect to\n\n");
3170
Pekka Paalanene31e0532013-05-22 18:03:07 +03003171#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3172 fprintf(stderr,
3173 "Options for rpi-backend.so:\n\n"
3174 " --tty=TTY\t\tThe tty to use\n"
3175 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3176 " --transform=TR\tThe output transformation, TR is one of:\n"
3177 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3178 "\n");
3179#endif
3180
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003181 exit(error_code);
3182}
3183
Peter Maatmane5b42e42013-03-27 22:38:53 +01003184static void
3185catch_signals(void)
3186{
3187 struct sigaction action;
3188
3189 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3190 action.sa_sigaction = on_caught_signal;
3191 sigemptyset(&action.sa_mask);
3192 sigaction(SIGSEGV, &action, NULL);
3193 sigaction(SIGABRT, &action, NULL);
3194}
3195
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003196int main(int argc, char *argv[])
3197{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003198 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003199 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003200 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003201 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003202 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003203 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003204 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003205 int *argc, char *argv[],
3206 struct weston_config *config);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003207 int i, config_fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003208 char *backend = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003209 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003210 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003211 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003212 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003213 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003214 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003215 struct weston_config *config;
3216 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003217
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003218 const struct weston_option core_options[] = {
3219 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3220 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3221 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003222 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003223 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003224 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003225 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003226 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003227
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003228 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003229
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003230 if (help)
3231 usage(EXIT_SUCCESS);
3232
Scott Moreau12245142012-08-29 15:15:58 -06003233 if (version) {
3234 printf(PACKAGE_STRING "\n");
3235 return EXIT_SUCCESS;
3236 }
3237
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003238 weston_log_file_open(log);
3239
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003240 weston_log("%s\n"
3241 STAMP_SPACE "%s\n"
3242 STAMP_SPACE "Bug reports to: %s\n"
3243 STAMP_SPACE "Build: %s\n",
3244 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003245 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003246 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003247
Martin Minarik37032f82012-06-18 20:15:18 +02003248 verify_xdg_runtime_dir();
3249
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003250 display = wl_display_create();
3251
Tiago Vignatti2116b892011-08-08 05:52:59 -07003252 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003253 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3254 display);
3255 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3256 display);
3257 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3258 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003259
3260 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003261 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3262 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003263
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003264 if (!backend) {
3265 if (getenv("WAYLAND_DISPLAY"))
3266 backend = "wayland-backend.so";
3267 else if (getenv("DISPLAY"))
3268 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003269 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003270 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003271 }
3272
Ossama Othmana50e6e42013-05-14 09:48:26 -07003273 config_fd = open_config_file("weston.ini");
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003274 config = weston_config_parse(config_fd);
3275 close(config_fd);
3276
3277 section = weston_config_get_section(config, "core", NULL, NULL);
3278 weston_config_section_get_string(section, "modules",
3279 &modules, "desktop-shell.so");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003280
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003281 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003282 if (!backend_init)
3283 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003284
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003285 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003286 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003287 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003288 exit(EXIT_FAILURE);
3289 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003290
Peter Maatmane5b42e42013-03-27 22:38:53 +01003291 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003292 segv_compositor = ec;
3293
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003294 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003295
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003296 setenv("WAYLAND_DISPLAY", socket_name, 1);
3297
Ossama Othmana50e6e42013-05-14 09:48:26 -07003298 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003299 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003300 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003301 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003302
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003303 for (i = 1; i < argc; i++)
3304 weston_log("fatal: unhandled option: %s\n", argv[i]);
3305 if (argc > 1) {
3306 ret = EXIT_FAILURE;
3307 goto out;
3308 }
3309
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003310 weston_compositor_log_capabilities(ec);
3311
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003312 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003313 weston_log("fatal: failed to add socket: %m\n");
3314 ret = EXIT_FAILURE;
3315 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003316 }
3317
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003318 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003319
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003320 wl_display_run(display);
3321
3322 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003323 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003324 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003325
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003326 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003327
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003328 for (i = ARRAY_LENGTH(signals); i;)
3329 wl_event_source_remove(signals[--i]);
3330
Daniel Stone855028f2012-05-01 20:37:10 +01003331 weston_compositor_xkb_destroy(ec);
3332
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003333 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003334 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003335
Martin Minarik19e6f262012-06-07 13:08:46 +02003336 weston_log_file_close();
3337
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003338 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003339}