blob: f3823401f774dc9d50918b67c588057c3cd55eba [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
Alexander Larsson355748e2013-05-28 16:23:38 +020099weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode, int32_t scale)
Alex Wu2dda6042012-04-17 17:20:47 +0800100{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200101 struct weston_seat *seat;
102 pixman_region32_t old_output_region;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200103 int ret;
104
Alex Wu2dda6042012-04-17 17:20:47 +0800105 if (!output->switch_mode)
106 return -1;
107
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200108 ret = output->switch_mode(output, mode);
109 if (ret < 0)
110 return ret;
111
Alexander Larsson355748e2013-05-28 16:23:38 +0200112 output->scale = scale;
113
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200114 pixman_region32_init(&old_output_region);
115 pixman_region32_copy(&old_output_region, &output->region);
116
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200117 /* Update output region and transformation matrix */
Alexander Larsson0b135062013-05-28 16:23:36 +0200118 weston_output_transform_scale_init(output, output->transform, output->scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200119
120 pixman_region32_init(&output->previous_damage);
121 pixman_region32_init_rect(&output->region, output->x, output->y,
122 output->width, output->height);
123
124 weston_output_update_matrix(output);
125
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200126 /* If a pointer falls outside the outputs new geometry, move it to its
127 * lower-right corner */
128 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400129 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200130 int32_t x, y;
131
132 if (!pointer)
133 continue;
134
135 x = wl_fixed_to_int(pointer->x);
136 y = wl_fixed_to_int(pointer->y);
137
138 if (!pixman_region32_contains_point(&old_output_region,
139 x, y, NULL) ||
140 pixman_region32_contains_point(&output->region,
141 x, y, NULL))
142 continue;
143
144 if (x >= output->x + output->width)
145 x = output->x + output->width - 1;
146 if (y >= output->y + output->height)
147 y = output->y + output->height - 1;
148
149 pointer->x = wl_fixed_from_int(x);
150 pointer->y = wl_fixed_from_int(y);
151 }
152
153 pixman_region32_fini(&old_output_region);
154
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200155 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800156}
157
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400158WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500159weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400160{
161 wl_list_insert(&child_process_list, &process->link);
162}
163
Benjamin Franzke06286262011-05-06 19:12:33 +0200164static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200165child_client_exec(int sockfd, const char *path)
166{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500167 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200168 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200169 sigset_t allsigs;
170
171 /* do not give our signal mask to the new process */
172 sigfillset(&allsigs);
173 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200174
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500175 /* Launch clients as the user. */
176 seteuid(getuid());
177
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500178 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
179 * non-CLOEXEC fd to pass through exec. */
180 clientfd = dup(sockfd);
181 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200182 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500183 return;
184 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200185
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500186 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200187 setenv("WAYLAND_SOCKET", s, 1);
188
189 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200190 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200191 path);
192}
193
194WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500195weston_client_launch(struct weston_compositor *compositor,
196 struct weston_process *proc,
197 const char *path,
198 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200199{
200 int sv[2];
201 pid_t pid;
202 struct wl_client *client;
203
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300204 weston_log("launching '%s'\n", path);
205
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300206 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200207 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200208 "socketpair failed while launching '%s': %m\n",
209 path);
210 return NULL;
211 }
212
213 pid = fork();
214 if (pid == -1) {
215 close(sv[0]);
216 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200217 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200218 "fork failed while launching '%s': %m\n", path);
219 return NULL;
220 }
221
222 if (pid == 0) {
223 child_client_exec(sv[1], path);
224 exit(-1);
225 }
226
227 close(sv[1]);
228
229 client = wl_client_create(compositor->wl_display, sv[0]);
230 if (!client) {
231 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200232 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200233 "wl_client_create failed while launching '%s'.\n",
234 path);
235 return NULL;
236 }
237
238 proc->pid = pid;
239 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500240 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200241
242 return client;
243}
244
245static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300246surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
247{
248 struct weston_surface *surface =
249 container_of(listener, struct weston_surface,
250 pending.buffer_destroy_listener);
251
252 surface->pending.buffer = NULL;
253}
254
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200255static void
256empty_region(pixman_region32_t *region)
257{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300258 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200259 pixman_region32_init(region);
260}
261
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300262static void
263region_init_infinite(pixman_region32_t *region)
264{
265 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
266 UINT32_MAX, UINT32_MAX);
267}
268
Pekka Paalanene67858b2013-04-25 13:57:42 +0300269static struct weston_subsurface *
270weston_surface_to_subsurface(struct weston_surface *surface);
271
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500272WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500273weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500274{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500275 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400276
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200277 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400278 if (surface == NULL)
279 return NULL;
280
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400281 wl_signal_init(&surface->resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500282
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500283 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500284 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500285
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400286 surface->resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400287
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500288 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400289 surface->alpha = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400290
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100291 if (compositor->renderer->create_surface(surface) < 0) {
292 free(surface);
293 return NULL;
294 }
295
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200296 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200297 surface->buffer_scale = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200298 surface->pending.buffer_transform = surface->buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200299 surface->pending.buffer_scale = surface->buffer_scale;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400300 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400301 surface->plane = &compositor->primary_plane;
Giulio Camuffo184df502013-02-21 11:29:21 +0100302 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200303
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400304 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500305 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500306 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300307 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200308 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500309 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400310
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200311 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200312 wl_list_insert(&surface->geometry.transformation_list,
313 &surface->transform.position.link);
314 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200315 wl_list_init(&surface->geometry.child_list);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200316 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200317 surface->transform.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500318
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300319 surface->pending.buffer_destroy_listener.notify =
320 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300321 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300322 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300323 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300324 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300325
Pekka Paalanene67858b2013-04-25 13:57:42 +0300326 wl_list_init(&surface->subsurface_list);
327 wl_list_init(&surface->subsurface_list_pending);
328
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400329 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500330}
331
Alex Wu8811bf92012-02-28 18:07:54 +0800332WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500333weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200334 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500335{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100336 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500337}
338
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400339WL_EXPORT void
340weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200341 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200342{
343 if (surface->transform.enabled) {
344 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
345
346 weston_matrix_transform(&surface->transform.matrix, &v);
347
348 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200349 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700350 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200351 v.f[3]);
352 *x = 0;
353 *y = 0;
354 return;
355 }
356
357 *x = v.f[0] / v.f[3];
358 *y = v.f[1] / v.f[3];
359 } else {
360 *x = sx + surface->geometry.x;
361 *y = sy + surface->geometry.y;
362 }
363}
364
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500365WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200366weston_transformed_coord(int width, int height,
367 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200368 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200369 float sx, float sy, float *bx, float *by)
370{
371 switch (transform) {
372 case WL_OUTPUT_TRANSFORM_NORMAL:
373 default:
374 *bx = sx;
375 *by = sy;
376 break;
377 case WL_OUTPUT_TRANSFORM_FLIPPED:
378 *bx = width - sx;
379 *by = sy;
380 break;
381 case WL_OUTPUT_TRANSFORM_90:
382 *bx = height - sy;
383 *by = sx;
384 break;
385 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
386 *bx = height - sy;
387 *by = width - sx;
388 break;
389 case WL_OUTPUT_TRANSFORM_180:
390 *bx = width - sx;
391 *by = height - sy;
392 break;
393 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
394 *bx = sx;
395 *by = height - sy;
396 break;
397 case WL_OUTPUT_TRANSFORM_270:
398 *bx = sy;
399 *by = width - sx;
400 break;
401 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
402 *bx = sy;
403 *by = sx;
404 break;
405 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200406
407 *bx *= scale;
408 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200409}
410
411WL_EXPORT pixman_box32_t
412weston_transformed_rect(int width, int height,
413 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200414 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200415 pixman_box32_t rect)
416{
417 float x1, x2, y1, y2;
418
419 pixman_box32_t ret;
420
Alexander Larsson4ea95522013-05-22 14:41:37 +0200421 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200422 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200423 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200424 rect.x2, rect.y2, &x2, &y2);
425
426 if (x1 <= x2) {
427 ret.x1 = x1;
428 ret.x2 = x2;
429 } else {
430 ret.x1 = x2;
431 ret.x2 = x1;
432 }
433
434 if (y1 <= y2) {
435 ret.y1 = y1;
436 ret.y2 = y2;
437 } else {
438 ret.y1 = y2;
439 ret.y2 = y1;
440 }
441
442 return ret;
443}
444
445WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200446weston_surface_to_buffer_float(struct weston_surface *surface,
447 float sx, float sy, float *bx, float *by)
448{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200449 weston_transformed_coord(surface->geometry.width,
450 surface->geometry.height,
451 surface->buffer_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200452 surface->buffer_scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200453 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200454}
455
Alexander Larsson4ea95522013-05-22 14:41:37 +0200456WL_EXPORT void
457weston_surface_to_buffer(struct weston_surface *surface,
458 int sx, int sy, int *bx, int *by)
459{
460 float bxf, byf;
461
462 weston_transformed_coord(surface->geometry.width,
463 surface->geometry.height,
464 surface->buffer_transform,
465 surface->buffer_scale,
466 sx, sy, &bxf, &byf);
467 *bx = floorf(bxf);
468 *by = floorf(byf);
469}
470
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200471WL_EXPORT pixman_box32_t
472weston_surface_to_buffer_rect(struct weston_surface *surface,
473 pixman_box32_t rect)
474{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200475 return weston_transformed_rect(surface->geometry.width,
476 surface->geometry.height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200477 surface->buffer_transform,
478 surface->buffer_scale,
479 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200480}
481
482WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400483weston_surface_move_to_plane(struct weston_surface *surface,
484 struct weston_plane *plane)
485{
486 if (surface->plane == plane)
487 return;
488
489 weston_surface_damage_below(surface);
490 surface->plane = plane;
491 weston_surface_damage(surface);
492}
493
494WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500495weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200496{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500497 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200498
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500499 pixman_region32_init(&damage);
500 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
501 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400502 pixman_region32_union(&surface->plane->damage,
503 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500504 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200505}
506
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400507static struct wl_resource *
508find_resource_for_client(struct wl_list *list, struct wl_client *client)
509{
510 struct wl_resource *r;
511
512 wl_list_for_each(r, list, link) {
513 if (r->client == client)
514 return r;
515 }
516
517 return NULL;
518}
519
520static void
521weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
522{
523 uint32_t different = es->output_mask ^ mask;
524 uint32_t entered = mask & different;
525 uint32_t left = es->output_mask & different;
526 struct weston_output *output;
527 struct wl_resource *resource = NULL;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400528 struct wl_client *client = es->resource.client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400529
530 es->output_mask = mask;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400531 if (es->resource.client == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400532 return;
533 if (different == 0)
534 return;
535
536 wl_list_for_each(output, &es->compositor->output_list, link) {
537 if (1 << output->id & different)
538 resource =
539 find_resource_for_client(&output->resource_list,
540 client);
541 if (resource == NULL)
542 continue;
543 if (1 << output->id & entered)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400544 wl_surface_send_enter(&es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400545 if (1 << output->id & left)
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400546 wl_surface_send_leave(&es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400547 }
548}
549
550static void
551weston_surface_assign_output(struct weston_surface *es)
552{
553 struct weston_compositor *ec = es->compositor;
554 struct weston_output *output, *new_output;
555 pixman_region32_t region;
556 uint32_t max, area, mask;
557 pixman_box32_t *e;
558
559 new_output = NULL;
560 max = 0;
561 mask = 0;
562 pixman_region32_init(&region);
563 wl_list_for_each(output, &ec->output_list, link) {
564 pixman_region32_intersect(&region, &es->transform.boundingbox,
565 &output->region);
566
567 e = pixman_region32_extents(&region);
568 area = (e->x2 - e->x1) * (e->y2 - e->y1);
569
570 if (area > 0)
571 mask |= 1 << output->id;
572
573 if (area >= max) {
574 new_output = output;
575 max = area;
576 }
577 }
578 pixman_region32_fini(&region);
579
580 es->output = new_output;
581 weston_surface_update_output_mask(es, mask);
582}
583
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200584static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200585surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
586 int32_t width, int32_t height,
587 pixman_region32_t *bbox)
588{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200589 float min_x = HUGE_VALF, min_y = HUGE_VALF;
590 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200591 int32_t s[4][2] = {
592 { sx, sy },
593 { sx, sy + height },
594 { sx + width, sy },
595 { sx + width, sy + height }
596 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200597 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200598 int i;
599
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300600 if (width == 0 || height == 0) {
601 /* avoid rounding empty bbox to 1x1 */
602 pixman_region32_init(bbox);
603 return;
604 }
605
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200606 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200607 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400608 weston_surface_to_global_float(surface,
609 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200610 if (x < min_x)
611 min_x = x;
612 if (x > max_x)
613 max_x = x;
614 if (y < min_y)
615 min_y = y;
616 if (y > max_y)
617 max_y = y;
618 }
619
Pekka Paalanen219b9822012-02-08 15:38:37 +0200620 int_x = floorf(min_x);
621 int_y = floorf(min_y);
622 pixman_region32_init_rect(bbox, int_x, int_y,
623 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200624}
625
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200626static void
627weston_surface_update_transform_disable(struct weston_surface *surface)
628{
629 surface->transform.enabled = 0;
630
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200631 /* round off fractions when not transformed */
632 surface->geometry.x = roundf(surface->geometry.x);
633 surface->geometry.y = roundf(surface->geometry.y);
634
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500635 /* Otherwise identity matrix, but with x and y translation. */
636 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
637 surface->transform.position.matrix.d[12] = surface->geometry.x;
638 surface->transform.position.matrix.d[13] = surface->geometry.y;
639
640 surface->transform.matrix = surface->transform.position.matrix;
641
Kristian Høgsberg9bcaaeb2013-02-28 14:56:43 -0500642 surface->transform.inverse = surface->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500643 surface->transform.inverse.d[12] = -surface->geometry.x;
644 surface->transform.inverse.d[13] = -surface->geometry.y;
645
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200646 pixman_region32_init_rect(&surface->transform.boundingbox,
647 surface->geometry.x,
648 surface->geometry.y,
649 surface->geometry.width,
650 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500651
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400652 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500653 pixman_region32_copy(&surface->transform.opaque,
654 &surface->opaque);
655 pixman_region32_translate(&surface->transform.opaque,
656 surface->geometry.x,
657 surface->geometry.y);
658 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200659}
660
661static int
662weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200663{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200664 struct weston_surface *parent = surface->geometry.parent;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200665 struct weston_matrix *matrix = &surface->transform.matrix;
666 struct weston_matrix *inverse = &surface->transform.inverse;
667 struct weston_transform *tform;
668
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200669 surface->transform.enabled = 1;
670
671 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300672 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200673 surface->transform.position.matrix.d[12] = surface->geometry.x;
674 surface->transform.position.matrix.d[13] = surface->geometry.y;
675
676 weston_matrix_init(matrix);
677 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
678 weston_matrix_multiply(matrix, &tform->matrix);
679
Pekka Paalanen483243f2013-03-08 14:56:50 +0200680 if (parent)
681 weston_matrix_multiply(matrix, &parent->transform.matrix);
682
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200683 if (weston_matrix_invert(inverse, matrix) < 0) {
684 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200685 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200686 " transformation not invertible.\n", surface);
687 return -1;
688 }
689
690 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
691 surface->geometry.height,
692 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500693
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200694 return 0;
695}
696
697WL_EXPORT void
698weston_surface_update_transform(struct weston_surface *surface)
699{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200700 struct weston_surface *parent = surface->geometry.parent;
701
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200702 if (!surface->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200703 return;
704
Pekka Paalanen483243f2013-03-08 14:56:50 +0200705 if (parent)
706 weston_surface_update_transform(parent);
707
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200708 surface->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200709
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500710 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200711
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200712 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500713 pixman_region32_fini(&surface->transform.opaque);
714 pixman_region32_init(&surface->transform.opaque);
715
Pekka Paalanencd403622012-01-25 13:37:39 +0200716 /* transform.position is always in transformation_list */
717 if (surface->geometry.transformation_list.next ==
718 &surface->transform.position.link &&
719 surface->geometry.transformation_list.prev ==
Pekka Paalanen483243f2013-03-08 14:56:50 +0200720 &surface->transform.position.link &&
721 !parent) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200722 weston_surface_update_transform_disable(surface);
723 } else {
724 if (weston_surface_update_transform_enable(surface) < 0)
725 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200726 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200727
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400728 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200729
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300730 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200731}
732
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200733WL_EXPORT void
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200734weston_surface_geometry_dirty(struct weston_surface *surface)
735{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200736 struct weston_surface *child;
737
738 /*
739 * The invariant: if surface->geometry.dirty, then all surfaces
740 * in surface->geometry.child_list have geometry.dirty too.
741 * Corollary: if not parent->geometry.dirty, then all ancestors
742 * are not dirty.
743 */
744
745 if (surface->transform.dirty)
746 return;
747
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200748 surface->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200749
750 wl_list_for_each(child, &surface->geometry.child_list,
751 geometry.parent_link)
752 weston_surface_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200753}
754
755WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100756weston_surface_to_global_fixed(struct weston_surface *surface,
757 wl_fixed_t sx, wl_fixed_t sy,
758 wl_fixed_t *x, wl_fixed_t *y)
759{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200760 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100761
762 weston_surface_to_global_float(surface,
763 wl_fixed_to_double(sx),
764 wl_fixed_to_double(sy),
765 &xf, &yf);
766 *x = wl_fixed_from_double(xf);
767 *y = wl_fixed_from_double(yf);
768}
769
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400770WL_EXPORT void
771weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200772 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200773{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200774 if (surface->transform.enabled) {
775 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
776
777 weston_matrix_transform(&surface->transform.inverse, &v);
778
779 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200780 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200781 "weston_surface_from_global(), divisor = %g\n",
782 v.f[3]);
783 *sx = 0;
784 *sy = 0;
785 return;
786 }
787
Pekka Paalanencd403622012-01-25 13:37:39 +0200788 *sx = v.f[0] / v.f[3];
789 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200790 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200791 *sx = x - surface->geometry.x;
792 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200793 }
794}
795
796WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100797weston_surface_from_global_fixed(struct weston_surface *surface,
798 wl_fixed_t x, wl_fixed_t y,
799 wl_fixed_t *sx, wl_fixed_t *sy)
800{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200801 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100802
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400803 weston_surface_from_global_float(surface,
804 wl_fixed_to_double(x),
805 wl_fixed_to_double(y),
806 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100807 *sx = wl_fixed_from_double(sxf);
808 *sy = wl_fixed_from_double(syf);
809}
810
811WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200812weston_surface_from_global(struct weston_surface *surface,
813 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
814{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200815 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200816
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400817 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200818 *sx = floorf(sxf);
819 *sy = floorf(syf);
820}
821
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400822WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400823weston_surface_schedule_repaint(struct weston_surface *surface)
824{
825 struct weston_output *output;
826
827 wl_list_for_each(output, &surface->compositor->output_list, link)
828 if (surface->output_mask & (1 << output->id))
829 weston_output_schedule_repaint(output);
830}
831
832WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500833weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500834{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400835 pixman_region32_union_rect(&surface->damage, &surface->damage,
836 0, 0, surface->geometry.width,
837 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200838
Kristian Høgsberg98238702012-08-03 16:29:12 -0400839 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500840}
841
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400842WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500843weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200844 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400845{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200846 surface->geometry.x = x;
847 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200848 surface->geometry.width = width;
849 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200850 weston_surface_geometry_dirty(surface);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400851}
852
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200853WL_EXPORT void
854weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200855 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200856{
857 surface->geometry.x = x;
858 surface->geometry.y = y;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200859 weston_surface_geometry_dirty(surface);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200860}
861
Pekka Paalanen483243f2013-03-08 14:56:50 +0200862static void
863transform_parent_handle_parent_destroy(struct wl_listener *listener,
864 void *data)
865{
866 struct weston_surface *surface =
867 container_of(listener, struct weston_surface,
868 geometry.parent_destroy_listener);
869
870 weston_surface_set_transform_parent(surface, NULL);
871}
872
873WL_EXPORT void
874weston_surface_set_transform_parent(struct weston_surface *surface,
875 struct weston_surface *parent)
876{
877 if (surface->geometry.parent) {
878 wl_list_remove(&surface->geometry.parent_destroy_listener.link);
879 wl_list_remove(&surface->geometry.parent_link);
880 }
881
882 surface->geometry.parent = parent;
883
884 surface->geometry.parent_destroy_listener.notify =
885 transform_parent_handle_parent_destroy;
886 if (parent) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400887 wl_signal_add(&parent->resource.destroy_signal,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200888 &surface->geometry.parent_destroy_listener);
889 wl_list_insert(&parent->geometry.child_list,
890 &surface->geometry.parent_link);
891 }
892
893 weston_surface_geometry_dirty(surface);
894}
895
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300896WL_EXPORT int
897weston_surface_is_mapped(struct weston_surface *surface)
898{
899 if (surface->output)
900 return 1;
901 else
902 return 0;
903}
904
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200905WL_EXPORT int32_t
906weston_surface_buffer_width(struct weston_surface *surface)
907{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200908 int32_t width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200909 switch (surface->buffer_transform) {
910 case WL_OUTPUT_TRANSFORM_90:
911 case WL_OUTPUT_TRANSFORM_270:
912 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
913 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200914 width = surface->buffer_ref.buffer->height;
915 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200916 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200917 width = surface->buffer_ref.buffer->width;
918 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200919 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200920 return width / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200921}
922
923WL_EXPORT int32_t
924weston_surface_buffer_height(struct weston_surface *surface)
925{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200926 int32_t height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200927 switch (surface->buffer_transform) {
928 case WL_OUTPUT_TRANSFORM_90:
929 case WL_OUTPUT_TRANSFORM_270:
930 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
931 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200932 height = surface->buffer_ref.buffer->width;
933 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200934 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200935 height = surface->buffer_ref.buffer->height;
936 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200937 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200938 return height / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200939}
940
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400941WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500942weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500943{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400944 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500945
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400946 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500947
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400948 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500949}
950
Kristian Høgsberg6848c252013-05-08 22:02:59 -0400951WL_EXPORT struct weston_surface *
Tiago Vignatti9d393522012-02-10 16:26:19 +0200952weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100953 wl_fixed_t x, wl_fixed_t y,
954 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200955{
956 struct weston_surface *surface;
957
958 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100959 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500960 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100961 wl_fixed_to_int(*sx),
962 wl_fixed_to_int(*sy),
963 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200964 return surface;
965 }
966
967 return NULL;
968}
969
970static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500971weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400972{
Daniel Stone37816df2012-05-16 18:45:18 +0100973 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400974
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500975 if (!compositor->focus)
976 return;
977
Daniel Stone37816df2012-05-16 18:45:18 +0100978 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -0400979 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400980}
981
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400982WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500983weston_surface_unmap(struct weston_surface *surface)
984{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100985 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500986
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500987 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500988 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500989 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500990
Daniel Stone4dab5db2012-05-30 16:31:53 +0100991 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400992 if (seat->keyboard && seat->keyboard->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400993 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400994 if (seat->pointer && seat->pointer->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400995 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400996 NULL,
997 wl_fixed_from_int(0),
998 wl_fixed_from_int(0));
Daniel Stone4dab5db2012-05-30 16:31:53 +0100999 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001000
Kristian Høgsberg98238702012-08-03 16:29:12 -04001001 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001002}
1003
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001004struct weston_frame_callback {
1005 struct wl_resource resource;
1006 struct wl_list link;
1007};
1008
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001009static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001010destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001011{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001012 struct weston_surface *surface =
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001013 container_of(resource, struct weston_surface, resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001014 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001015 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001016
Pekka Paalanen483243f2013-03-08 14:56:50 +02001017 assert(wl_list_empty(&surface->geometry.child_list));
Pekka Paalanene67858b2013-04-25 13:57:42 +03001018 assert(wl_list_empty(&surface->subsurface_list_pending));
1019 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001020
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001021 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001022 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001023
Pekka Paalanenbc106382012-10-10 12:49:31 +03001024 wl_list_for_each_safe(cb, next,
1025 &surface->pending.frame_callback_list, link)
1026 wl_resource_destroy(&cb->resource);
1027
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001028 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001029 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001030 pixman_region32_fini(&surface->pending.damage);
1031
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001032 if (surface->pending.buffer)
1033 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1034
Pekka Paalanende685b82012-12-04 15:58:12 +02001035 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001036
Kristian Høgsberg42263852012-09-06 21:59:29 -04001037 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001038
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001039 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001040 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001041 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001042 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001043 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001044
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001045 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
1046 wl_resource_destroy(&cb->resource);
1047
Pekka Paalanen483243f2013-03-08 14:56:50 +02001048 weston_surface_set_transform_parent(surface, NULL);
1049
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001050 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001051}
1052
Alex Wu8811bf92012-02-28 18:07:54 +08001053WL_EXPORT void
1054weston_surface_destroy(struct weston_surface *surface)
1055{
1056 /* Not a valid way to destroy a client surface */
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001057 assert(surface->resource.client == NULL);
Alex Wu8811bf92012-02-28 18:07:54 +08001058
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001059 wl_signal_emit(&surface->resource.destroy_signal, &surface->resource);
1060 destroy_surface(&surface->resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001061}
1062
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001063static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001064weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1065 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001066{
Pekka Paalanende685b82012-12-04 15:58:12 +02001067 struct weston_buffer_reference *ref =
1068 container_of(listener, struct weston_buffer_reference,
1069 destroy_listener);
1070
1071 assert((struct wl_buffer *)data == ref->buffer);
1072 ref->buffer = NULL;
1073}
1074
1075WL_EXPORT void
1076weston_buffer_reference(struct weston_buffer_reference *ref,
1077 struct wl_buffer *buffer)
1078{
1079 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001080 ref->buffer->busy_count--;
1081 if (ref->buffer->busy_count == 0) {
1082 assert(ref->buffer->resource.client != NULL);
1083 wl_resource_queue_event(&ref->buffer->resource,
1084 WL_BUFFER_RELEASE);
1085 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001086 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001087 }
1088
Pekka Paalanende685b82012-12-04 15:58:12 +02001089 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001090 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001091 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001092 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001093 }
1094
Pekka Paalanende685b82012-12-04 15:58:12 +02001095 ref->buffer = buffer;
1096 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1097}
1098
1099static void
1100weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
1101{
1102 weston_buffer_reference(&surface->buffer_ref, buffer);
1103
Pekka Paalanena6421c42012-12-04 15:58:10 +02001104 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001105 if (weston_surface_is_mapped(surface))
1106 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001107 }
1108
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001109 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001110}
1111
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001112WL_EXPORT void
1113weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001114{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001115 wl_list_remove(&surface->layer_link);
1116 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001117 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001118 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001119}
1120
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001121WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001122weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001123{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001124 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001125
1126 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001127 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001128}
1129
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001130WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001131weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001132{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001133 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001134
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001135 pixman_region32_union(&compositor->primary_plane.damage,
1136 &compositor->primary_plane.damage,
1137 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001138 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001139}
1140
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001141static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001142surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001143 pixman_region32_t *opaque)
1144{
Pekka Paalanende685b82012-12-04 15:58:12 +02001145 if (surface->buffer_ref.buffer &&
1146 wl_buffer_is_shm(surface->buffer_ref.buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001147 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001148
1149 if (surface->transform.enabled) {
1150 pixman_box32_t *extents;
1151
1152 extents = pixman_region32_extents(&surface->damage);
1153 surface_compute_bbox(surface, extents->x1, extents->y1,
1154 extents->x2 - extents->x1,
1155 extents->y2 - extents->y1,
1156 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001157 pixman_region32_translate(&surface->damage,
1158 -surface->plane->x,
1159 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001160 } else {
1161 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001162 surface->geometry.x - surface->plane->x,
1163 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001164 }
1165
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001166 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001167 pixman_region32_union(&surface->plane->damage,
1168 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001169 empty_region(&surface->damage);
1170 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001171 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001172}
1173
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001174static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001175compositor_accumulate_damage(struct weston_compositor *ec)
1176{
1177 struct weston_plane *plane;
1178 struct weston_surface *es;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001179 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001180
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001181 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001182
1183 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001184 pixman_region32_copy(&plane->clip, &clip);
1185
1186 pixman_region32_init(&opaque);
1187
1188 wl_list_for_each(es, &ec->surface_list, link) {
1189 if (es->plane != plane)
1190 continue;
1191
1192 surface_accumulate_damage(es, &opaque);
1193 }
1194
1195 pixman_region32_union(&clip, &clip, &opaque);
1196 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001197 }
1198
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001199 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001200
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001201 wl_list_for_each(es, &ec->surface_list, link) {
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001202 /* Both the renderer and the backend have seen the buffer
1203 * by now. If renderer needs the buffer, it has its own
1204 * reference set. If the backend wants to keep the buffer
1205 * around for migrating the surface into a non-primary plane
1206 * later, keep_buffer is true. Otherwise, drop the core
1207 * reference now, and allow early buffer release. This enables
1208 * clients to use single-buffering.
1209 */
1210 if (!es->keep_buffer)
1211 weston_buffer_reference(&es->buffer_ref, NULL);
1212 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001213}
1214
1215static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001216surface_list_add(struct weston_compositor *compositor,
1217 struct weston_surface *surface)
1218{
1219 struct weston_subsurface *sub;
1220
1221 if (wl_list_empty(&surface->subsurface_list)) {
1222 weston_surface_update_transform(surface);
1223 wl_list_insert(compositor->surface_list.prev, &surface->link);
1224 return;
1225 }
1226
1227 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1228 if (!weston_surface_is_mapped(sub->surface))
1229 continue;
1230
1231 if (sub->surface == surface) {
1232 weston_surface_update_transform(sub->surface);
1233 wl_list_insert(compositor->surface_list.prev,
1234 &sub->surface->link);
1235 } else {
1236 surface_list_add(compositor, sub->surface);
1237 }
1238 }
1239}
1240
1241static void
1242weston_compositor_build_surface_list(struct weston_compositor *compositor)
1243{
1244 struct weston_surface *surface;
1245 struct weston_layer *layer;
1246
1247 wl_list_init(&compositor->surface_list);
1248 wl_list_for_each(layer, &compositor->layer_list, link) {
1249 wl_list_for_each(surface, &layer->surface_list, layer_link) {
1250 surface_list_add(compositor, surface);
1251 }
1252 }
1253}
1254
1255static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001256weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001257{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001258 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001259 struct weston_surface *es;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001260 struct weston_animation *animation, *next;
1261 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001262 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001263 pixman_region32_t output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001264
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001265 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001266 weston_compositor_build_surface_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001267
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001268 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001269 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001270 else
1271 wl_list_for_each(es, &ec->surface_list, link)
1272 weston_surface_move_to_plane(es, &ec->primary_plane);
1273
Pekka Paalanene67858b2013-04-25 13:57:42 +03001274 wl_list_init(&frame_callback_list);
1275 wl_list_for_each(es, &ec->surface_list, link) {
1276 if (es->output == output) {
1277 wl_list_insert_list(&frame_callback_list,
1278 &es->frame_callback_list);
1279 wl_list_init(&es->frame_callback_list);
1280 }
1281 }
1282
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001283 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001284
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001285 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001286 pixman_region32_intersect(&output_damage,
1287 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001288 pixman_region32_subtract(&output_damage,
1289 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001290
Scott Moreauccbf29d2012-02-22 14:21:41 -07001291 if (output->dirty)
1292 weston_output_update_matrix(output);
1293
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001294 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001295
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001296 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001297
Kristian Høgsbergef044142011-06-21 15:02:12 -04001298 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001299
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001300 weston_compositor_repick(ec);
1301 wl_event_loop_dispatch(ec->input_loop, 0);
1302
Jonas Ådahldb773762012-06-13 00:01:21 +02001303 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001304 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001305 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001306 }
1307
Scott Moreaud64cf212012-06-08 19:40:54 -06001308 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001309 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001310 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001311 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001312}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001313
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001314static int
1315weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001316{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001317 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001318
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001319 wl_event_loop_dispatch(compositor->input_loop, 0);
1320
1321 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001322}
1323
1324WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001325weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001326{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001327 struct weston_compositor *compositor = output->compositor;
1328 struct wl_event_loop *loop =
1329 wl_display_get_event_loop(compositor->wl_display);
1330 int fd;
1331
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001332 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001333 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001334 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001335 return;
1336 }
1337
1338 output->repaint_scheduled = 0;
1339 if (compositor->input_loop_source)
1340 return;
1341
1342 fd = wl_event_loop_get_fd(compositor->input_loop);
1343 compositor->input_loop_source =
1344 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1345 weston_compositor_read_input, compositor);
1346}
1347
1348static void
1349idle_repaint(void *data)
1350{
1351 struct weston_output *output = data;
1352
Jonas Ådahle5a12252013-04-05 23:07:11 +02001353 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001354}
1355
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001356WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001357weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1358{
1359 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001360 if (below != NULL)
1361 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001362}
1363
1364WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001365weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001366{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001367 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001368 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001369
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001370 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1371 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001372 return;
1373
Kristian Høgsbergef044142011-06-21 15:02:12 -04001374 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001375 output->repaint_needed = 1;
1376 if (output->repaint_scheduled)
1377 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001378
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001379 wl_event_loop_add_idle(loop, idle_repaint, output);
1380 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001381
1382 if (compositor->input_loop_source) {
1383 wl_event_source_remove(compositor->input_loop_source);
1384 compositor->input_loop_source = NULL;
1385 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001386}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001387
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001388WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001389weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1390{
1391 struct weston_output *output;
1392
1393 wl_list_for_each(output, &compositor->output_list, link)
1394 weston_output_schedule_repaint(output);
1395}
1396
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001397static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001398surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001399{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001400 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001401}
1402
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001403static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001404surface_attach(struct wl_client *client,
1405 struct wl_resource *resource,
1406 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1407{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001408 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001409 struct wl_buffer *buffer = NULL;
1410
1411 if (buffer_resource)
1412 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001413
Pekka Paalanende685b82012-12-04 15:58:12 +02001414 /* Attach, attach, without commit in between does not send
1415 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001416 if (surface->pending.buffer)
1417 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001418
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001419 surface->pending.sx = sx;
1420 surface->pending.sy = sy;
1421 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001422 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001423 if (buffer) {
1424 wl_signal_add(&buffer->resource.destroy_signal,
1425 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001426 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001427}
1428
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001429static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001430surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001431 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001432 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001433{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001434 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001435
Pekka Paalanen8e159182012-10-10 12:49:25 +03001436 pixman_region32_union_rect(&surface->pending.damage,
1437 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001438 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001439}
1440
Kristian Høgsberg33418202011-08-16 23:01:28 -04001441static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001442destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001443{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001444 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001445
1446 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001447 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001448}
1449
1450static void
1451surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001452 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001453{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001454 struct weston_frame_callback *cb;
Pekka Paalanenbc106382012-10-10 12:49:31 +03001455 struct weston_surface *surface = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001456
1457 cb = malloc(sizeof *cb);
1458 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001459 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001460 return;
1461 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001462
Kristian Høgsberg33418202011-08-16 23:01:28 -04001463 cb->resource.object.interface = &wl_callback_interface;
1464 cb->resource.object.id = callback;
1465 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001466 cb->resource.client = client;
1467 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001468
1469 wl_client_add_resource(client, &cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001470 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001471}
1472
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001473static void
1474surface_set_opaque_region(struct wl_client *client,
1475 struct wl_resource *resource,
1476 struct wl_resource *region_resource)
1477{
1478 struct weston_surface *surface = resource->data;
1479 struct weston_region *region;
1480
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001481 if (region_resource) {
1482 region = region_resource->data;
Pekka Paalanen512dde82012-10-10 12:49:27 +03001483 pixman_region32_copy(&surface->pending.opaque,
1484 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001485 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001486 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001487 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001488}
1489
1490static void
1491surface_set_input_region(struct wl_client *client,
1492 struct wl_resource *resource,
1493 struct wl_resource *region_resource)
1494{
1495 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001496 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001497
1498 if (region_resource) {
1499 region = region_resource->data;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001500 pixman_region32_copy(&surface->pending.input,
1501 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001502 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001503 pixman_region32_fini(&surface->pending.input);
1504 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001505 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001506}
1507
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001508static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001509weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001510{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001511 struct weston_subsurface *sub;
1512
1513 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1514 parent_link_pending) {
1515 wl_list_remove(&sub->parent_link);
1516 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1517 }
1518}
1519
1520static void
1521weston_surface_commit(struct weston_surface *surface)
1522{
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001523 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001524 int surface_width = 0;
1525 int surface_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001526
Alexander Larsson4ea95522013-05-22 14:41:37 +02001527 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001528 surface->buffer_transform = surface->pending.buffer_transform;
1529
Alexander Larsson4ea95522013-05-22 14:41:37 +02001530 /* wl_surface.set_buffer_scale */
1531 surface->buffer_scale = surface->pending.buffer_scale;
1532
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001533 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001534 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001535 weston_surface_attach(surface, surface->pending.buffer);
1536
Giulio Camuffo184df502013-02-21 11:29:21 +01001537 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001538 surface_width = weston_surface_buffer_width(surface);
1539 surface_height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001540 }
1541
1542 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001543 surface->configure(surface,
1544 surface->pending.sx, surface->pending.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001545 surface_width, surface_height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001546
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001547 if (surface->pending.buffer)
1548 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1549 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001550 surface->pending.sx = 0;
1551 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001552 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001553
1554 /* wl_surface.damage */
1555 pixman_region32_union(&surface->damage, &surface->damage,
1556 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001557 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1558 0, 0,
1559 surface->geometry.width,
1560 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001561 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001562
1563 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001564 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001565 surface->geometry.width,
1566 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001567 pixman_region32_intersect(&opaque,
1568 &opaque, &surface->pending.opaque);
1569
1570 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1571 pixman_region32_copy(&surface->opaque, &opaque);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001572 weston_surface_geometry_dirty(surface);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001573 }
1574
1575 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001576
1577 /* wl_surface.set_input_region */
1578 pixman_region32_fini(&surface->input);
1579 pixman_region32_init_rect(&surface->input, 0, 0,
1580 surface->geometry.width,
1581 surface->geometry.height);
1582 pixman_region32_intersect(&surface->input,
1583 &surface->input, &surface->pending.input);
1584
Pekka Paalanenbc106382012-10-10 12:49:31 +03001585 /* wl_surface.frame */
1586 wl_list_insert_list(&surface->frame_callback_list,
1587 &surface->pending.frame_callback_list);
1588 wl_list_init(&surface->pending.frame_callback_list);
1589
Pekka Paalanene67858b2013-04-25 13:57:42 +03001590 weston_surface_commit_subsurface_order(surface);
1591
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001592 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001593}
1594
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001595static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001596weston_subsurface_commit(struct weston_subsurface *sub);
1597
1598static void
1599weston_subsurface_parent_commit(struct weston_subsurface *sub,
1600 int parent_is_synchronized);
1601
1602static void
1603surface_commit(struct wl_client *client, struct wl_resource *resource)
1604{
1605 struct weston_surface *surface = resource->data;
1606 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1607
1608 if (sub) {
1609 weston_subsurface_commit(sub);
1610 return;
1611 }
1612
1613 weston_surface_commit(surface);
1614
1615 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1616 if (sub->surface != surface)
1617 weston_subsurface_parent_commit(sub, 0);
1618 }
1619}
1620
1621static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001622surface_set_buffer_transform(struct wl_client *client,
1623 struct wl_resource *resource, int transform)
1624{
1625 struct weston_surface *surface = resource->data;
1626
1627 surface->pending.buffer_transform = transform;
1628}
1629
Alexander Larsson4ea95522013-05-22 14:41:37 +02001630static void
1631surface_set_buffer_scale(struct wl_client *client,
1632 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001633 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02001634{
1635 struct weston_surface *surface = resource->data;
1636
1637 surface->pending.buffer_scale = scale;
1638}
1639
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001640static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001641 surface_destroy,
1642 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001643 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001644 surface_frame,
1645 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001646 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001647 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001648 surface_set_buffer_transform,
1649 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001650};
1651
1652static void
1653compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001654 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001655{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001656 struct weston_compositor *ec = resource->data;
1657 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001658
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001659 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001660 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001661 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001662 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001663 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001664
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001665 surface->resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001666
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001667 surface->resource.object.id = id;
1668 surface->resource.object.interface = &wl_surface_interface;
1669 surface->resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001670 (void (**)(void)) &surface_interface;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001671 surface->resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001672
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001673 wl_client_add_resource(client, &surface->resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001674}
1675
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001676static void
1677destroy_region(struct wl_resource *resource)
1678{
1679 struct weston_region *region =
1680 container_of(resource, struct weston_region, resource);
1681
1682 pixman_region32_fini(&region->region);
1683 free(region);
1684}
1685
1686static void
1687region_destroy(struct wl_client *client, struct wl_resource *resource)
1688{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001689 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001690}
1691
1692static void
1693region_add(struct wl_client *client, struct wl_resource *resource,
1694 int32_t x, int32_t y, int32_t width, int32_t height)
1695{
1696 struct weston_region *region = resource->data;
1697
1698 pixman_region32_union_rect(&region->region, &region->region,
1699 x, y, width, height);
1700}
1701
1702static void
1703region_subtract(struct wl_client *client, struct wl_resource *resource,
1704 int32_t x, int32_t y, int32_t width, int32_t height)
1705{
1706 struct weston_region *region = resource->data;
1707 pixman_region32_t rect;
1708
1709 pixman_region32_init_rect(&rect, x, y, width, height);
1710 pixman_region32_subtract(&region->region, &region->region, &rect);
1711 pixman_region32_fini(&rect);
1712}
1713
1714static const struct wl_region_interface region_interface = {
1715 region_destroy,
1716 region_add,
1717 region_subtract
1718};
1719
1720static void
1721compositor_create_region(struct wl_client *client,
1722 struct wl_resource *resource, uint32_t id)
1723{
1724 struct weston_region *region;
1725
1726 region = malloc(sizeof *region);
1727 if (region == NULL) {
1728 wl_resource_post_no_memory(resource);
1729 return;
1730 }
1731
1732 region->resource.destroy = destroy_region;
1733
1734 region->resource.object.id = id;
1735 region->resource.object.interface = &wl_region_interface;
1736 region->resource.object.implementation =
1737 (void (**)(void)) &region_interface;
1738 region->resource.data = region;
1739
1740 pixman_region32_init(&region->region);
1741
1742 wl_client_add_resource(client, &region->resource);
1743}
1744
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001745static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001746 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001747 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001748};
1749
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001750static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001751weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
1752{
1753 struct weston_surface *surface = sub->surface;
1754 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001755 int surface_width = 0;
1756 int surface_height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001757
Alexander Larsson4ea95522013-05-22 14:41:37 +02001758 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001759 surface->buffer_transform = sub->cached.buffer_transform;
1760
Alexander Larsson4ea95522013-05-22 14:41:37 +02001761 /* wl_surface.set_buffer_scale */
1762 surface->buffer_scale = sub->cached.buffer_scale;
1763
Pekka Paalanene67858b2013-04-25 13:57:42 +03001764 /* wl_surface.attach */
1765 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
1766 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
1767 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
1768
1769 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001770 surface_width = weston_surface_buffer_width(surface);
1771 surface_height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001772 }
1773
1774 if (surface->configure && sub->cached.newly_attached)
1775 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001776 surface_width, surface_height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001777 sub->cached.sx = 0;
1778 sub->cached.sy = 0;
1779 sub->cached.newly_attached = 0;
1780
1781 /* wl_surface.damage */
1782 pixman_region32_union(&surface->damage, &surface->damage,
1783 &sub->cached.damage);
1784 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1785 0, 0,
1786 surface->geometry.width,
1787 surface->geometry.height);
1788 empty_region(&sub->cached.damage);
1789
1790 /* wl_surface.set_opaque_region */
1791 pixman_region32_init_rect(&opaque, 0, 0,
1792 surface->geometry.width,
1793 surface->geometry.height);
1794 pixman_region32_intersect(&opaque,
1795 &opaque, &sub->cached.opaque);
1796
1797 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1798 pixman_region32_copy(&surface->opaque, &opaque);
1799 weston_surface_geometry_dirty(surface);
1800 }
1801
1802 pixman_region32_fini(&opaque);
1803
1804 /* wl_surface.set_input_region */
1805 pixman_region32_fini(&surface->input);
1806 pixman_region32_init_rect(&surface->input, 0, 0,
1807 surface->geometry.width,
1808 surface->geometry.height);
1809 pixman_region32_intersect(&surface->input,
1810 &surface->input, &sub->cached.input);
1811
1812 /* wl_surface.frame */
1813 wl_list_insert_list(&surface->frame_callback_list,
1814 &sub->cached.frame_callback_list);
1815 wl_list_init(&sub->cached.frame_callback_list);
1816
1817 weston_surface_commit_subsurface_order(surface);
1818
1819 weston_surface_schedule_repaint(surface);
1820
1821 sub->cached.has_data = 0;
1822}
1823
1824static void
1825weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
1826{
1827 struct weston_surface *surface = sub->surface;
1828
1829 /*
1830 * If this commit would cause the surface to move by the
1831 * attach(dx, dy) parameters, the old damage region must be
1832 * translated to correspond to the new surface coordinate system
1833 * origin.
1834 */
1835 pixman_region32_translate(&sub->cached.damage,
1836 -surface->pending.sx, -surface->pending.sy);
1837 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
1838 &surface->pending.damage);
1839 empty_region(&surface->pending.damage);
1840
1841 if (surface->pending.newly_attached) {
1842 sub->cached.newly_attached = 1;
1843 weston_buffer_reference(&sub->cached.buffer_ref,
1844 surface->pending.buffer);
1845 }
1846 sub->cached.sx += surface->pending.sx;
1847 sub->cached.sy += surface->pending.sy;
1848 surface->pending.sx = 0;
1849 surface->pending.sy = 0;
1850 surface->pending.newly_attached = 0;
1851
1852 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001853 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001854
1855 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
1856
1857 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
1858
1859 wl_list_insert_list(&sub->cached.frame_callback_list,
1860 &surface->pending.frame_callback_list);
1861 wl_list_init(&surface->pending.frame_callback_list);
1862
1863 sub->cached.has_data = 1;
1864}
1865
1866static int
1867weston_subsurface_is_synchronized(struct weston_subsurface *sub)
1868{
1869 while (sub) {
1870 if (sub->synchronized)
1871 return 1;
1872
1873 if (!sub->parent)
1874 return 0;
1875
1876 sub = weston_surface_to_subsurface(sub->parent);
1877 }
1878
1879 return 0;
1880}
1881
1882static void
1883weston_subsurface_commit(struct weston_subsurface *sub)
1884{
1885 struct weston_surface *surface = sub->surface;
1886 struct weston_subsurface *tmp;
1887
1888 /* Recursive check for effectively synchronized. */
1889 if (weston_subsurface_is_synchronized(sub)) {
1890 weston_subsurface_commit_to_cache(sub);
1891 } else {
1892 if (sub->cached.has_data) {
1893 /* flush accumulated state from cache */
1894 weston_subsurface_commit_to_cache(sub);
1895 weston_subsurface_commit_from_cache(sub);
1896 } else {
1897 weston_surface_commit(surface);
1898 }
1899
1900 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1901 if (tmp->surface != surface)
1902 weston_subsurface_parent_commit(tmp, 0);
1903 }
1904 }
1905}
1906
1907static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001908weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001909{
1910 struct weston_surface *surface = sub->surface;
1911 struct weston_subsurface *tmp;
1912
Pekka Paalanene67858b2013-04-25 13:57:42 +03001913 /* From now on, commit_from_cache the whole sub-tree, regardless of
1914 * the synchronized mode of each child. This sub-surface or some
1915 * of its ancestors were synchronized, so we are synchronized
1916 * all the way down.
1917 */
1918
1919 if (sub->cached.has_data)
1920 weston_subsurface_commit_from_cache(sub);
1921
1922 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
1923 if (tmp->surface != surface)
1924 weston_subsurface_parent_commit(tmp, 1);
1925 }
1926}
1927
1928static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03001929weston_subsurface_parent_commit(struct weston_subsurface *sub,
1930 int parent_is_synchronized)
1931{
1932 if (sub->position.set) {
1933 weston_surface_set_position(sub->surface,
1934 sub->position.x, sub->position.y);
1935 sub->position.set = 0;
1936 }
1937
1938 if (parent_is_synchronized || sub->synchronized)
1939 weston_subsurface_synchronized_commit(sub);
1940}
1941
1942static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001943subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
1944 int32_t width, int32_t height)
1945{
1946 struct weston_compositor *compositor = surface->compositor;
1947
1948 weston_surface_configure(surface,
1949 surface->geometry.x + dx,
1950 surface->geometry.y + dy,
1951 width, height);
1952
1953 /* No need to check parent mappedness, because if parent is not
1954 * mapped, parent is not in a visible layer, so this sub-surface
1955 * will not be drawn either.
1956 */
1957 if (!weston_surface_is_mapped(surface)) {
1958 wl_list_init(&surface->layer_link);
1959
1960 /* Cannot call weston_surface_update_transform(),
1961 * because that would call it also for the parent surface,
1962 * which might not be mapped yet. That would lead to
1963 * inconsistent state, where the window could never be
1964 * mapped.
1965 *
1966 * Instead just assing any output, to make
1967 * weston_surface_is_mapped() return true, so that when the
1968 * parent surface does get mapped, this one will get
1969 * included, too. See surface_list_add().
1970 */
1971 assert(!wl_list_empty(&compositor->output_list));
1972 surface->output = container_of(compositor->output_list.next,
1973 struct weston_output, link);
1974 }
1975}
1976
1977static struct weston_subsurface *
1978weston_surface_to_subsurface(struct weston_surface *surface)
1979{
1980 if (surface->configure == subsurface_configure)
1981 return surface->configure_private;
1982
1983 return NULL;
1984}
1985
Pekka Paalanen01388e22013-04-25 13:57:44 +03001986WL_EXPORT struct weston_surface *
1987weston_surface_get_main_surface(struct weston_surface *surface)
1988{
1989 struct weston_subsurface *sub;
1990
1991 while (surface && (sub = weston_surface_to_subsurface(surface)))
1992 surface = sub->parent;
1993
1994 return surface;
1995}
1996
Pekka Paalanene67858b2013-04-25 13:57:42 +03001997static void
1998subsurface_set_position(struct wl_client *client,
1999 struct wl_resource *resource, int32_t x, int32_t y)
2000{
2001 struct weston_subsurface *sub = resource->data;
2002
2003 if (!sub)
2004 return;
2005
2006 sub->position.x = x;
2007 sub->position.y = y;
2008 sub->position.set = 1;
2009}
2010
2011static struct weston_subsurface *
2012subsurface_from_surface(struct weston_surface *surface)
2013{
2014 struct weston_subsurface *sub;
2015
2016 sub = weston_surface_to_subsurface(surface);
2017 if (sub)
2018 return sub;
2019
2020 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2021 if (sub->surface == surface)
2022 return sub;
2023
2024 return NULL;
2025}
2026
2027static struct weston_subsurface *
2028subsurface_sibling_check(struct weston_subsurface *sub,
2029 struct weston_surface *surface,
2030 const char *request)
2031{
2032 struct weston_subsurface *sibling;
2033
2034 sibling = subsurface_from_surface(surface);
2035
2036 if (!sibling) {
2037 wl_resource_post_error(sub->resource,
2038 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2039 "%s: wl_surface@%d is not a parent or sibling",
2040 request, surface->resource.object.id);
2041 return NULL;
2042 }
2043
2044 if (sibling->parent != sub->parent) {
2045 wl_resource_post_error(sub->resource,
2046 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2047 "%s: wl_surface@%d has a different parent",
2048 request, surface->resource.object.id);
2049 return NULL;
2050 }
2051
2052 return sibling;
2053}
2054
2055static void
2056subsurface_place_above(struct wl_client *client,
2057 struct wl_resource *resource,
2058 struct wl_resource *sibling_resource)
2059{
2060 struct weston_subsurface *sub = resource->data;
2061 struct weston_surface *surface = sibling_resource->data;
2062 struct weston_subsurface *sibling;
2063
2064 if (!sub)
2065 return;
2066
2067 sibling = subsurface_sibling_check(sub, surface, "place_above");
2068 if (!sibling)
2069 return;
2070
2071 wl_list_remove(&sub->parent_link_pending);
2072 wl_list_insert(sibling->parent_link_pending.prev,
2073 &sub->parent_link_pending);
2074}
2075
2076static void
2077subsurface_place_below(struct wl_client *client,
2078 struct wl_resource *resource,
2079 struct wl_resource *sibling_resource)
2080{
2081 struct weston_subsurface *sub = resource->data;
2082 struct weston_surface *surface = sibling_resource->data;
2083 struct weston_subsurface *sibling;
2084
2085 if (!sub)
2086 return;
2087
2088 sibling = subsurface_sibling_check(sub, surface, "place_below");
2089 if (!sibling)
2090 return;
2091
2092 wl_list_remove(&sub->parent_link_pending);
2093 wl_list_insert(&sibling->parent_link_pending,
2094 &sub->parent_link_pending);
2095}
2096
2097static void
2098subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2099{
2100 struct weston_subsurface *sub = resource->data;
2101
2102 if (sub)
2103 sub->synchronized = 1;
2104}
2105
2106static void
2107subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2108{
2109 struct weston_subsurface *sub = resource->data;
2110
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002111 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002112 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002113
2114 /* If sub became effectively desynchronized, flush. */
2115 if (!weston_subsurface_is_synchronized(sub))
2116 weston_subsurface_synchronized_commit(sub);
2117 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002118}
2119
2120static void
2121weston_subsurface_cache_init(struct weston_subsurface *sub)
2122{
2123 pixman_region32_init(&sub->cached.damage);
2124 pixman_region32_init(&sub->cached.opaque);
2125 pixman_region32_init(&sub->cached.input);
2126 wl_list_init(&sub->cached.frame_callback_list);
2127 sub->cached.buffer_ref.buffer = NULL;
2128}
2129
2130static void
2131weston_subsurface_cache_fini(struct weston_subsurface *sub)
2132{
2133 struct weston_frame_callback *cb, *tmp;
2134
2135 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
2136 wl_resource_destroy(&cb->resource);
2137
2138 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2139 pixman_region32_fini(&sub->cached.damage);
2140 pixman_region32_fini(&sub->cached.opaque);
2141 pixman_region32_fini(&sub->cached.input);
2142}
2143
2144static void
2145weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2146{
2147 wl_list_remove(&sub->parent_link);
2148 wl_list_remove(&sub->parent_link_pending);
2149 wl_list_remove(&sub->parent_destroy_listener.link);
2150 sub->parent = NULL;
2151}
2152
2153static void
2154weston_subsurface_destroy(struct weston_subsurface *sub);
2155
2156static void
2157subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2158{
2159 struct weston_subsurface *sub =
2160 container_of(listener, struct weston_subsurface,
2161 surface_destroy_listener);
2162 assert(data == &sub->surface->resource);
2163
2164 /* The protocol object (wl_resource) is left inert. */
2165 if (sub->resource)
2166 sub->resource->data = NULL;
2167
2168 weston_subsurface_destroy(sub);
2169}
2170
2171static void
2172subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2173{
2174 struct weston_subsurface *sub =
2175 container_of(listener, struct weston_subsurface,
2176 parent_destroy_listener);
2177 assert(data == &sub->parent->resource);
2178 assert(sub->surface != sub->parent);
2179
2180 if (weston_surface_is_mapped(sub->surface))
2181 weston_surface_unmap(sub->surface);
2182
2183 weston_subsurface_unlink_parent(sub);
2184}
2185
2186static void
2187subsurface_resource_destroy(struct wl_resource *resource)
2188{
2189 struct weston_subsurface *sub = resource->data;
2190
2191 if (sub)
2192 weston_subsurface_destroy(sub);
2193
2194 free(resource);
2195}
2196
2197static void
2198subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2199{
2200 wl_resource_destroy(resource);
2201}
2202
2203static void
2204weston_subsurface_link_parent(struct weston_subsurface *sub,
2205 struct weston_surface *parent)
2206{
2207 sub->parent = parent;
2208 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
2209 wl_signal_add(&parent->resource.destroy_signal,
2210 &sub->parent_destroy_listener);
2211
2212 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2213 wl_list_insert(&parent->subsurface_list_pending,
2214 &sub->parent_link_pending);
2215}
2216
2217static void
2218weston_subsurface_link_surface(struct weston_subsurface *sub,
2219 struct weston_surface *surface)
2220{
2221 sub->surface = surface;
2222 sub->surface_destroy_listener.notify =
2223 subsurface_handle_surface_destroy;
2224 wl_signal_add(&surface->resource.destroy_signal,
2225 &sub->surface_destroy_listener);
2226}
2227
2228static void
2229weston_subsurface_destroy(struct weston_subsurface *sub)
2230{
2231 assert(sub->surface);
2232
2233 if (sub->resource) {
2234 assert(weston_surface_to_subsurface(sub->surface) == sub);
2235 assert(sub->parent_destroy_listener.notify ==
2236 subsurface_handle_parent_destroy);
2237
2238 weston_surface_set_transform_parent(sub->surface, NULL);
2239 if (sub->parent)
2240 weston_subsurface_unlink_parent(sub);
2241
2242 weston_subsurface_cache_fini(sub);
2243
2244 sub->surface->configure = NULL;
2245 sub->surface->configure_private = NULL;
2246 } else {
2247 /* the dummy weston_subsurface for the parent itself */
2248 assert(sub->parent_destroy_listener.notify == NULL);
2249 wl_list_remove(&sub->parent_link);
2250 wl_list_remove(&sub->parent_link_pending);
2251 }
2252
2253 wl_list_remove(&sub->surface_destroy_listener.link);
2254 free(sub);
2255}
2256
2257static const struct wl_subsurface_interface subsurface_implementation = {
2258 subsurface_destroy,
2259 subsurface_set_position,
2260 subsurface_place_above,
2261 subsurface_place_below,
2262 subsurface_set_sync,
2263 subsurface_set_desync
2264};
2265
2266static struct weston_subsurface *
2267weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2268 struct weston_surface *parent)
2269{
2270 struct weston_subsurface *sub;
2271
2272 sub = calloc(1, sizeof *sub);
2273 if (!sub)
2274 return NULL;
2275
2276 sub->resource = wl_client_add_object(surface->resource.client,
2277 &wl_subsurface_interface,
2278 &subsurface_implementation,
2279 id, sub);
2280 if (!sub->resource) {
2281 free(sub);
2282 return NULL;
2283 }
2284
2285 sub->resource->destroy = subsurface_resource_destroy;
2286 weston_subsurface_link_surface(sub, surface);
2287 weston_subsurface_link_parent(sub, parent);
2288 weston_subsurface_cache_init(sub);
2289 sub->synchronized = 1;
2290 weston_surface_set_transform_parent(surface, parent);
2291
2292 return sub;
2293}
2294
2295/* Create a dummy subsurface for having the parent itself in its
2296 * sub-surface lists. Makes stacking order manipulation easy.
2297 */
2298static struct weston_subsurface *
2299weston_subsurface_create_for_parent(struct weston_surface *parent)
2300{
2301 struct weston_subsurface *sub;
2302
2303 sub = calloc(1, sizeof *sub);
2304 if (!sub)
2305 return NULL;
2306
2307 weston_subsurface_link_surface(sub, parent);
2308 sub->parent = parent;
2309 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2310 wl_list_insert(&parent->subsurface_list_pending,
2311 &sub->parent_link_pending);
2312
2313 return sub;
2314}
2315
2316static void
2317subcompositor_get_subsurface(struct wl_client *client,
2318 struct wl_resource *resource,
2319 uint32_t id,
2320 struct wl_resource *surface_resource,
2321 struct wl_resource *parent_resource)
2322{
2323 struct weston_surface *surface = surface_resource->data;
2324 struct weston_surface *parent = parent_resource->data;
2325 struct weston_subsurface *sub;
2326 static const char where[] = "get_subsurface: wl_subsurface@";
2327
2328 if (surface == parent) {
2329 wl_resource_post_error(resource,
2330 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2331 "%s%d: wl_surface@%d cannot be its own parent",
2332 where, id, surface_resource->object.id);
2333 return;
2334 }
2335
2336 if (weston_surface_to_subsurface(surface)) {
2337 wl_resource_post_error(resource,
2338 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2339 "%s%d: wl_surface@%d is already a sub-surface",
2340 where, id, surface_resource->object.id);
2341 return;
2342 }
2343
2344 if (surface->configure) {
2345 wl_resource_post_error(resource,
2346 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2347 "%s%d: wl_surface@%d already has a role",
2348 where, id, surface_resource->object.id);
2349 return;
2350 }
2351
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002352 if (weston_surface_get_main_surface(parent) == surface) {
2353 wl_resource_post_error(resource,
2354 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2355 "%s%d: wl_surface@%d is an ancestor of parent",
2356 where, id, surface_resource->object.id);
2357 return;
2358 }
2359
Pekka Paalanene67858b2013-04-25 13:57:42 +03002360 /* make sure the parent is in its own list */
2361 if (wl_list_empty(&parent->subsurface_list)) {
2362 if (!weston_subsurface_create_for_parent(parent)) {
2363 wl_resource_post_no_memory(resource);
2364 return;
2365 }
2366 }
2367
2368 sub = weston_subsurface_create(id, surface, parent);
2369 if (!sub) {
2370 wl_resource_post_no_memory(resource);
2371 return;
2372 }
2373
2374 surface->configure = subsurface_configure;
2375 surface->configure_private = sub;
2376}
2377
2378static void
2379subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2380{
2381 wl_resource_destroy(resource);
2382}
2383
2384static const struct wl_subcompositor_interface subcompositor_interface = {
2385 subcompositor_destroy,
2386 subcompositor_get_subsurface
2387};
2388
2389static void
2390bind_subcompositor(struct wl_client *client,
2391 void *data, uint32_t version, uint32_t id)
2392{
2393 struct weston_compositor *compositor = data;
2394
2395 wl_client_add_object(client, &wl_subcompositor_interface,
2396 &subcompositor_interface, id, compositor);
2397}
2398
2399static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002400weston_compositor_dpms(struct weston_compositor *compositor,
2401 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002402{
2403 struct weston_output *output;
2404
2405 wl_list_for_each(output, &compositor->output_list, link)
2406 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002407 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002408}
2409
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002410WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002411weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002412{
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002413 switch (compositor->state) {
2414 case WESTON_COMPOSITOR_SLEEPING:
2415 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2416 /* fall through */
2417 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002418 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002419 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002420 /* fall through */
2421 default:
2422 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2423 wl_event_source_timer_update(compositor->idle_source,
2424 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002425 }
2426}
2427
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002428WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002429weston_compositor_offscreen(struct weston_compositor *compositor)
2430{
2431 switch (compositor->state) {
2432 case WESTON_COMPOSITOR_OFFSCREEN:
2433 return;
2434 case WESTON_COMPOSITOR_SLEEPING:
2435 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2436 /* fall through */
2437 default:
2438 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2439 wl_event_source_timer_update(compositor->idle_source, 0);
2440 }
2441}
2442
2443WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002444weston_compositor_sleep(struct weston_compositor *compositor)
2445{
2446 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2447 return;
2448
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002449 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002450 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2451 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2452}
2453
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002454static int
2455idle_handler(void *data)
2456{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002457 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002458
2459 if (compositor->idle_inhibit)
2460 return 1;
2461
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002462 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002463 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002464
2465 return 1;
2466}
2467
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002468WL_EXPORT void
2469weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2470{
2471 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002472 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002473 plane->x = x;
2474 plane->y = y;
2475}
2476
2477WL_EXPORT void
2478weston_plane_release(struct weston_plane *plane)
2479{
2480 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002481 pixman_region32_fini(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002482}
2483
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002484WL_EXPORT void
2485weston_compositor_stack_plane(struct weston_compositor *ec,
2486 struct weston_plane *plane,
2487 struct weston_plane *above)
2488{
2489 if (above)
2490 wl_list_insert(above->link.prev, &plane->link);
2491 else
2492 wl_list_insert(&ec->plane_list, &plane->link);
2493}
2494
Casey Dahlin9074db52012-04-19 22:50:09 -04002495static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002496{
2497 wl_list_remove(&resource->link);
2498 free(resource);
2499}
2500
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002501static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002502bind_output(struct wl_client *client,
2503 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002504{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002505 struct weston_output *output = data;
2506 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002507 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002508
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002509 resource = wl_client_add_object(client,
2510 &wl_output_interface, NULL, id, data);
2511
Casey Dahlin9074db52012-04-19 22:50:09 -04002512 wl_list_insert(&output->resource_list, &resource->link);
2513 resource->destroy = unbind_resource;
2514
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002515 wl_output_send_geometry(resource,
2516 output->x,
2517 output->y,
2518 output->mm_width,
2519 output->mm_height,
2520 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002521 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002522 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002523 if (version >= 2)
2524 wl_output_send_scale(resource,
2525 output->scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002526
2527 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002528 wl_output_send_mode(resource,
2529 mode->flags,
2530 mode->width,
2531 mode->height,
2532 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002533 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002534
2535 if (version >= 2)
2536 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002537}
2538
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002539WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002540weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002541{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002542 struct weston_compositor *c = output->compositor;
2543
Richard Hughes64ddde12013-05-01 21:52:10 +01002544 wl_signal_emit(&output->destroy_signal, output);
2545
Richard Hughesafe690c2013-05-02 10:10:04 +01002546 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002547 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002548 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002549 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002550
2551 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002552}
2553
Scott Moreau1bad5db2012-08-18 01:04:05 -06002554static void
2555weston_output_compute_transform(struct weston_output *output)
2556{
2557 struct weston_matrix transform;
2558 int flip;
2559
2560 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002561 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002562
2563 switch(output->transform) {
2564 case WL_OUTPUT_TRANSFORM_FLIPPED:
2565 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2566 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2567 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002568 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002569 flip = -1;
2570 break;
2571 default:
2572 flip = 1;
2573 break;
2574 }
2575
2576 switch(output->transform) {
2577 case WL_OUTPUT_TRANSFORM_NORMAL:
2578 case WL_OUTPUT_TRANSFORM_FLIPPED:
2579 transform.d[0] = flip;
2580 transform.d[1] = 0;
2581 transform.d[4] = 0;
2582 transform.d[5] = 1;
2583 break;
2584 case WL_OUTPUT_TRANSFORM_90:
2585 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2586 transform.d[0] = 0;
2587 transform.d[1] = -flip;
2588 transform.d[4] = 1;
2589 transform.d[5] = 0;
2590 break;
2591 case WL_OUTPUT_TRANSFORM_180:
2592 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2593 transform.d[0] = -flip;
2594 transform.d[1] = 0;
2595 transform.d[4] = 0;
2596 transform.d[5] = -1;
2597 break;
2598 case WL_OUTPUT_TRANSFORM_270:
2599 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2600 transform.d[0] = 0;
2601 transform.d[1] = flip;
2602 transform.d[4] = -1;
2603 transform.d[5] = 0;
2604 break;
2605 default:
2606 break;
2607 }
2608
2609 weston_matrix_multiply(&output->matrix, &transform);
2610}
2611
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002612WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002613weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002614{
Scott Moreau850ca422012-05-21 15:21:25 -06002615 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002616 struct weston_matrix camera;
2617 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002618
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002619 weston_matrix_init(&output->matrix);
2620 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002621 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2622 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002623
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002624 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002625 2.0 / (output->width + output->border.left + output->border.right),
2626 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2627
2628 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002629
Scott Moreauccbf29d2012-02-22 14:21:41 -07002630 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002631 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002632 weston_matrix_init(&camera);
2633 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002634 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002635 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002636 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002637 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002638 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002639 weston_matrix_multiply(&output->matrix, &modelview);
2640 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002641
Scott Moreauccbf29d2012-02-22 14:21:41 -07002642 output->dirty = 0;
2643}
2644
Scott Moreau1bad5db2012-08-18 01:04:05 -06002645static void
Alexander Larsson0b135062013-05-28 16:23:36 +02002646weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002647{
2648 output->transform = transform;
2649
2650 switch (transform) {
2651 case WL_OUTPUT_TRANSFORM_90:
2652 case WL_OUTPUT_TRANSFORM_270:
2653 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2654 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2655 /* Swap width and height */
2656 output->width = output->current->height;
2657 output->height = output->current->width;
2658 break;
2659 case WL_OUTPUT_TRANSFORM_NORMAL:
2660 case WL_OUTPUT_TRANSFORM_180:
2661 case WL_OUTPUT_TRANSFORM_FLIPPED:
2662 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2663 output->width = output->current->width;
2664 output->height = output->current->height;
2665 break;
2666 default:
2667 break;
2668 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06002669
Alexander Larsson4ea95522013-05-22 14:41:37 +02002670 output->scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02002671 output->width /= scale;
2672 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002673}
2674
Scott Moreauccbf29d2012-02-22 14:21:41 -07002675WL_EXPORT void
2676weston_output_move(struct weston_output *output, int x, int y)
2677{
2678 output->x = x;
2679 output->y = y;
2680
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002681 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002682 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002683 output->width,
2684 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002685}
2686
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002687WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002688weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02002689 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002690 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002691{
2692 output->compositor = c;
2693 output->x = x;
2694 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002695 output->border.top = 0;
2696 output->border.bottom = 0;
2697 output->border.left = 0;
2698 output->border.right = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02002699 output->mm_width = mm_width;
2700 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002701 output->dirty = 1;
Alexander Larssone32c3762013-05-28 16:23:37 +02002702 output->origin_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002703
Alexander Larsson0b135062013-05-28 16:23:36 +02002704 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06002705 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002706
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002707 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002708 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002709
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002710 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01002711 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002712 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002713 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002714
Casey Dahlin58ba1372012-04-19 22:50:08 -04002715 output->id = ffs(~output->compositor->output_id_pool) - 1;
2716 output->compositor->output_id_pool |= 1 << output->id;
2717
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002718 output->global =
2719 wl_display_add_global(c->wl_display, &wl_output_interface,
2720 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01002721 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002722}
2723
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002724static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002725compositor_bind(struct wl_client *client,
2726 void *data, uint32_t version, uint32_t id)
2727{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002728 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002729
2730 wl_client_add_object(client, &wl_compositor_interface,
2731 &compositor_interface, id, compositor);
2732}
2733
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002734static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002735log_uname(void)
2736{
2737 struct utsname usys;
2738
2739 uname(&usys);
2740
2741 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2742 usys.version, usys.machine);
2743}
2744
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002745WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02002746weston_environment_get_fd(const char *env)
2747{
2748 char *e, *end;
2749 int fd, flags;
2750
2751 e = getenv(env);
2752 if (!e)
2753 return -1;
2754 fd = strtol(e, &end, 0);
2755 if (*end != '\0')
2756 return -1;
2757
2758 flags = fcntl(fd, F_GETFD);
2759 if (flags == -1)
2760 return -1;
2761
2762 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
2763 unsetenv(env);
2764
2765 return fd;
2766}
2767
2768WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002769weston_compositor_init(struct weston_compositor *ec,
2770 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002771 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002772 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002773{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002774 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002775 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002776 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002777
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002778 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002779 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002780 wl_signal_init(&ec->destroy_signal);
2781 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002782 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002783 wl_signal_init(&ec->idle_signal);
2784 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002785 wl_signal_init(&ec->show_input_panel_signal);
2786 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02002787 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002788 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01002789 wl_signal_init(&ec->output_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002790 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002791
Casey Dahlin58ba1372012-04-19 22:50:08 -04002792 ec->output_id_pool = 0;
2793
Kristian Høgsberga8873122011-11-23 10:39:34 -05002794 if (!wl_display_add_global(display, &wl_compositor_interface,
2795 ec, compositor_bind))
2796 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002797
Pekka Paalanene67858b2013-04-25 13:57:42 +03002798 if (!wl_display_add_global(display, &wl_subcompositor_interface,
2799 ec, bind_subcompositor))
2800 return -1;
2801
Daniel Stone725c2c32012-06-22 14:04:36 +01002802 wl_list_init(&ec->surface_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002803 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002804 wl_list_init(&ec->layer_list);
2805 wl_list_init(&ec->seat_list);
2806 wl_list_init(&ec->output_list);
2807 wl_list_init(&ec->key_binding_list);
2808 wl_list_init(&ec->button_binding_list);
2809 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002810 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002811
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002812 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002813 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002814
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002815 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
2816 weston_config_section_get_string(s, "keymap_rules",
2817 (char **) &xkb_names.rules, NULL);
2818 weston_config_section_get_string(s, "keymap_model",
2819 (char **) &xkb_names.model, NULL);
2820 weston_config_section_get_string(s, "keymap_layout",
2821 (char **) &xkb_names.layout, NULL);
2822 weston_config_section_get_string(s, "keymap_variant",
2823 (char **) &xkb_names.variant, NULL);
2824 weston_config_section_get_string(s, "keymap_options",
2825 (char **) &xkb_names.options, NULL);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05002826 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
2827 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01002828
2829 ec->ping_handler = NULL;
2830
2831 screenshooter_create(ec);
2832 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002833 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002834
2835 wl_data_device_manager_init(ec->wl_display);
2836
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002837 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002838
Daniel Stone725c2c32012-06-22 14:04:36 +01002839 loop = wl_display_get_event_loop(ec->wl_display);
2840 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2841 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2842
2843 ec->input_loop = wl_event_loop_create();
2844
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002845 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2846 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2847
2848 weston_compositor_schedule_repaint(ec);
2849
Daniel Stone725c2c32012-06-22 14:04:36 +01002850 return 0;
2851}
2852
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002853WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002854weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002855{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002856 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002857
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002858 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002859 if (ec->input_loop_source)
2860 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002861
Matt Roper361d2ad2011-08-29 13:52:23 -07002862 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002863 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002864 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002865
Daniel Stone325fc2d2012-05-30 16:31:58 +01002866 weston_binding_list_destroy_all(&ec->key_binding_list);
2867 weston_binding_list_destroy_all(&ec->button_binding_list);
2868 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002869 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002870
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002871 weston_plane_release(&ec->primary_plane);
2872
Jonas Ådahlc97af922012-03-28 22:36:09 +02002873 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07002874
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002875 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07002876}
2877
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05002878WL_EXPORT void
2879weston_version(int *major, int *minor, int *micro)
2880{
2881 *major = WESTON_VERSION_MAJOR;
2882 *minor = WESTON_VERSION_MINOR;
2883 *micro = WESTON_VERSION_MICRO;
2884}
2885
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002886static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002887 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002888 const char *desc;
2889} capability_strings[] = {
2890 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03002891 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03002892};
2893
2894static void
2895weston_compositor_log_capabilities(struct weston_compositor *compositor)
2896{
2897 unsigned i;
2898 int yes;
2899
2900 weston_log("Compositor capabilities:\n");
2901 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
2902 yes = compositor->capabilities & capability_strings[i].bit;
2903 weston_log_continue(STAMP_SPACE "%s %s\n",
2904 capability_strings[i].desc,
2905 yes ? "yes" : "no");
2906 }
2907}
2908
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002909static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002910{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002911 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002912
Martin Minarik6d118362012-06-07 18:01:59 +02002913 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002914 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002915
2916 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002917}
2918
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002919#ifdef HAVE_LIBUNWIND
2920
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002921static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002922print_backtrace(void)
2923{
2924 unw_cursor_t cursor;
2925 unw_context_t context;
2926 unw_word_t off;
2927 unw_proc_info_t pip;
2928 int ret, i = 0;
2929 char procname[256];
2930 const char *filename;
2931 Dl_info dlinfo;
2932
2933 pip.unwind_info = NULL;
2934 ret = unw_getcontext(&context);
2935 if (ret) {
2936 weston_log("unw_getcontext: %d\n", ret);
2937 return;
2938 }
2939
2940 ret = unw_init_local(&cursor, &context);
2941 if (ret) {
2942 weston_log("unw_init_local: %d\n", ret);
2943 return;
2944 }
2945
2946 ret = unw_step(&cursor);
2947 while (ret > 0) {
2948 ret = unw_get_proc_info(&cursor, &pip);
2949 if (ret) {
2950 weston_log("unw_get_proc_info: %d\n", ret);
2951 break;
2952 }
2953
2954 ret = unw_get_proc_name(&cursor, procname, 256, &off);
2955 if (ret && ret != -UNW_ENOMEM) {
2956 if (ret != -UNW_EUNSPEC)
2957 weston_log("unw_get_proc_name: %d\n", ret);
2958 procname[0] = '?';
2959 procname[1] = 0;
2960 }
2961
2962 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
2963 *dlinfo.dli_fname)
2964 filename = dlinfo.dli_fname;
2965 else
2966 filename = "?";
2967
2968 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
2969 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
2970
2971 ret = unw_step(&cursor);
2972 if (ret < 0)
2973 weston_log("unw_step: %d\n", ret);
2974 }
2975}
2976
2977#else
2978
2979static void
2980print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002981{
2982 void *buffer[32];
2983 int i, count;
2984 Dl_info info;
2985
Marcin Slusarz554a0da2013-02-18 13:27:22 -05002986 count = backtrace(buffer, ARRAY_LENGTH(buffer));
2987 for (i = 0; i < count; i++) {
2988 dladdr(buffer[i], &info);
2989 weston_log(" [%016lx] %s (%s)\n",
2990 (long) buffer[i],
2991 info.dli_sname ? info.dli_sname : "--",
2992 info.dli_fname);
2993 }
2994}
2995
2996#endif
2997
2998static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01002999on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003000{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003001 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003002 * then call the backend restore function, which will switch
3003 * back to the vt we launched from or ungrab X etc and then
3004 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003005 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003006 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003007 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003008
Peter Maatmane5b42e42013-03-27 22:38:53 +01003009 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003010
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003011 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003012
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003013 segv_compositor->restore(segv_compositor);
3014
3015 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003016}
3017
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003018static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003019load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003020{
3021 char path[PATH_MAX];
3022 void *module, *init;
3023
3024 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003025 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003026 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003027 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003028
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003029 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3030 if (module) {
3031 weston_log("Module '%s' already loaded\n", path);
3032 dlclose(module);
3033 return NULL;
3034 }
3035
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003036 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003037 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003038 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003039 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003040 return NULL;
3041 }
3042
3043 init = dlsym(module, entrypoint);
3044 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003045 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003046 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003047 return NULL;
3048 }
3049
3050 return init;
3051}
3052
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003053static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003054load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003055 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003056{
3057 const char *p, *end;
3058 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003059 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003060 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003061
3062 if (modules == NULL)
3063 return 0;
3064
3065 p = modules;
3066 while (*p) {
3067 end = strchrnul(p, ',');
3068 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3069 module_init = load_module(buffer, "module_init");
3070 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003071 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003072 p = end;
3073 while (*p == ',')
3074 p++;
3075
3076 }
3077
3078 return 0;
3079}
3080
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003081static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003082 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3083
3084static const char xdg_wrong_message[] =
3085 "fatal: environment variable XDG_RUNTIME_DIR\n"
3086 "is set to \"%s\", which is not a directory.\n";
3087
3088static const char xdg_wrong_mode_message[] =
3089 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3090 "correctly. Unix access mode must be 0700 but is %o,\n"
3091 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003092 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003093
3094static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003095 "Refer to your distribution on how to get it, or\n"
3096 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3097 "on how to implement it.\n";
3098
Martin Minarik37032f82012-06-18 20:15:18 +02003099static void
3100verify_xdg_runtime_dir(void)
3101{
3102 char *dir = getenv("XDG_RUNTIME_DIR");
3103 struct stat s;
3104
3105 if (!dir) {
3106 weston_log(xdg_error_message);
3107 weston_log_continue(xdg_detail_message);
3108 exit(EXIT_FAILURE);
3109 }
3110
3111 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3112 weston_log(xdg_wrong_message, dir);
3113 weston_log_continue(xdg_detail_message);
3114 exit(EXIT_FAILURE);
3115 }
3116
3117 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3118 weston_log(xdg_wrong_mode_message,
3119 dir, s.st_mode & 0777, s.st_uid);
3120 weston_log_continue(xdg_detail_message);
3121 }
3122}
3123
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003124static int
3125usage(int error_code)
3126{
3127 fprintf(stderr,
3128 "Usage: weston [OPTIONS]\n\n"
3129 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3130 "Weston supports multiple backends, and depending on which backend is in use\n"
3131 "different options will be accepted.\n\n"
3132
3133
3134 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003135 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003136 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003137 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3138 "\t\t\t\twayland-backend.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003139 " -S, --socket=NAME\tName of socket to listen on\n"
3140 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003141 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003142 " --log==FILE\t\tLog to the given file\n"
3143 " -h, --help\t\tThis help message\n\n");
3144
3145 fprintf(stderr,
3146 "Options for drm-backend.so:\n\n"
3147 " --connector=ID\tBring up only this connector\n"
3148 " --seat=SEAT\t\tThe seat that weston should run on\n"
3149 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003150 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003151 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3152
3153 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003154 "Options for fbdev-backend.so:\n\n"
3155 " --tty=TTY\t\tThe tty to use\n"
3156 " --device=DEVICE\tThe framebuffer device to use\n\n");
3157
3158 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003159 "Options for x11-backend.so:\n\n"
3160 " --width=WIDTH\t\tWidth of X window\n"
3161 " --height=HEIGHT\tHeight of X window\n"
3162 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003163 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003164 " --output-count=COUNT\tCreate multiple outputs\n"
3165 " --no-input\t\tDont create input devices\n\n");
3166
3167 fprintf(stderr,
3168 "Options for wayland-backend.so:\n\n"
3169 " --width=WIDTH\t\tWidth of Wayland surface\n"
3170 " --height=HEIGHT\tHeight of Wayland surface\n"
3171 " --display=DISPLAY\tWayland display to connect to\n\n");
3172
Pekka Paalanene31e0532013-05-22 18:03:07 +03003173#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3174 fprintf(stderr,
3175 "Options for rpi-backend.so:\n\n"
3176 " --tty=TTY\t\tThe tty to use\n"
3177 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3178 " --transform=TR\tThe output transformation, TR is one of:\n"
3179 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3180 "\n");
3181#endif
3182
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003183 exit(error_code);
3184}
3185
Peter Maatmane5b42e42013-03-27 22:38:53 +01003186static void
3187catch_signals(void)
3188{
3189 struct sigaction action;
3190
3191 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3192 action.sa_sigaction = on_caught_signal;
3193 sigemptyset(&action.sa_mask);
3194 sigaction(SIGSEGV, &action, NULL);
3195 sigaction(SIGABRT, &action, NULL);
3196}
3197
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003198int main(int argc, char *argv[])
3199{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003200 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003201 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003202 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003203 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003204 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003205 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003206 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003207 int *argc, char *argv[],
3208 struct weston_config *config);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003209 int i, config_fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003210 char *backend = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003211 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003212 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003213 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003214 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003215 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003216 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003217 struct weston_config *config;
3218 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003219
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003220 const struct weston_option core_options[] = {
3221 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3222 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3223 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003224 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003225 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003226 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003227 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003228 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003229
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003230 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003231
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003232 if (help)
3233 usage(EXIT_SUCCESS);
3234
Scott Moreau12245142012-08-29 15:15:58 -06003235 if (version) {
3236 printf(PACKAGE_STRING "\n");
3237 return EXIT_SUCCESS;
3238 }
3239
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003240 weston_log_file_open(log);
3241
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003242 weston_log("%s\n"
3243 STAMP_SPACE "%s\n"
3244 STAMP_SPACE "Bug reports to: %s\n"
3245 STAMP_SPACE "Build: %s\n",
3246 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003247 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003248 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003249
Martin Minarik37032f82012-06-18 20:15:18 +02003250 verify_xdg_runtime_dir();
3251
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003252 display = wl_display_create();
3253
Tiago Vignatti2116b892011-08-08 05:52:59 -07003254 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003255 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3256 display);
3257 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3258 display);
3259 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3260 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003261
3262 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003263 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3264 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003265
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003266 if (!backend) {
3267 if (getenv("WAYLAND_DISPLAY"))
3268 backend = "wayland-backend.so";
3269 else if (getenv("DISPLAY"))
3270 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003271 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003272 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003273 }
3274
Ossama Othmana50e6e42013-05-14 09:48:26 -07003275 config_fd = open_config_file("weston.ini");
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003276 config = weston_config_parse(config_fd);
3277 close(config_fd);
3278
3279 section = weston_config_get_section(config, "core", NULL, NULL);
3280 weston_config_section_get_string(section, "modules",
3281 &modules, "desktop-shell.so");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003282
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003283 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003284 if (!backend_init)
3285 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003286
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003287 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003288 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003289 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003290 exit(EXIT_FAILURE);
3291 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003292
Peter Maatmane5b42e42013-03-27 22:38:53 +01003293 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003294 segv_compositor = ec;
3295
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003296 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003297
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003298 setenv("WAYLAND_DISPLAY", socket_name, 1);
3299
Ossama Othmana50e6e42013-05-14 09:48:26 -07003300 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003301 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003302 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003303 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003304
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003305 for (i = 1; i < argc; i++)
3306 weston_log("fatal: unhandled option: %s\n", argv[i]);
3307 if (argc > 1) {
3308 ret = EXIT_FAILURE;
3309 goto out;
3310 }
3311
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003312 weston_compositor_log_capabilities(ec);
3313
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003314 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003315 weston_log("fatal: failed to add socket: %m\n");
3316 ret = EXIT_FAILURE;
3317 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003318 }
3319
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003320 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003321
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003322 wl_display_run(display);
3323
3324 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003325 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003326 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003327
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003328 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003329
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003330 for (i = ARRAY_LENGTH(signals); i;)
3331 wl_event_source_remove(signals[--i]);
3332
Daniel Stone855028f2012-05-01 20:37:10 +01003333 weston_compositor_xkb_destroy(ec);
3334
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003335 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003336 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003337
Martin Minarik19e6f262012-06-07 13:08:46 +02003338 weston_log_file_close();
3339
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003340 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003341}