blob: cca81708a5eeb9b580a220e7687fa8a6adc0b91b [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanend581a8f2012-01-27 16:25:16 +02004 * Copyright © 2012 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberga9410222011-01-14 17:22:35 -050025#include "config.h"
26
Daniel Stoneb7452fe2012-06-01 12:14:06 +010027#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010032#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020034#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040035#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010036#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040037#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020038#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020039#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020040#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040041#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050042#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040044#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040045#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050046#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040047#include <sys/time.h>
48#include <time.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050049
Marcin Slusarz554a0da2013-02-18 13:27:22 -050050#ifdef HAVE_LIBUNWIND
51#define UNW_LOCAL_ONLY
52#include <libunwind.h>
53#endif
54
Kristian Høgsberg82863022010-06-04 21:52:02 -040055#include "compositor.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030056#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040057#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050058#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050059
Kristian Høgsberg27da5382011-06-21 17:32:25 -040060static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040061static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040062
63static int
64sigchld_handler(int signal_number, void *data)
65{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050066 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040067 int status;
68 pid_t pid;
69
Bill Spitzak027b9622012-03-17 15:22:03 -070070 pid = waitpid(-1, &status, WNOHANG);
71 if (!pid)
72 return 1;
73
Kristian Høgsberg27da5382011-06-21 17:32:25 -040074 wl_list_for_each(p, &child_process_list, link) {
75 if (p->pid == pid)
76 break;
77 }
78
79 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020080 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040081 return 1;
82 }
83
84 wl_list_remove(&p->link);
85 p->cleanup(p, status);
86
87 return 1;
88}
89
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020090static void
Alexander Larsson0b135062013-05-28 16:23:36 +020091weston_output_transform_scale_init(struct weston_output *output,
92 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020093
Rob Bradford27b17932013-06-26 18:08:46 +010094static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050095weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010096
Alex Wu2dda6042012-04-17 17:20:47 +080097WL_EXPORT int
Hardening57388e42013-09-18 23:56:36 +020098weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode,
99 int32_t scale, enum weston_mode_switch_op op)
Alex Wu2dda6042012-04-17 17:20:47 +0800100{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200101 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200102 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200103 pixman_region32_t old_output_region;
Hardening57388e42013-09-18 23:56:36 +0200104 int ret, notify_mode_changed, notify_scale_changed;
105 int temporary_mode, temporary_scale;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200106
Alex Wu2dda6042012-04-17 17:20:47 +0800107 if (!output->switch_mode)
108 return -1;
109
Hardening57388e42013-09-18 23:56:36 +0200110 temporary_mode = (output->original_mode != 0);
111 temporary_scale = (output->current_scale != output->original_scale);
112 ret = 0;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200113
Hardening57388e42013-09-18 23:56:36 +0200114 notify_mode_changed = 0;
115 notify_scale_changed = 0;
116 switch(op) {
117 case WESTON_MODE_SWITCH_SET_NATIVE:
118 output->native_mode = mode;
119 if (!temporary_mode) {
120 notify_mode_changed = 1;
121 ret = output->switch_mode(output, mode);
122 if (ret < 0)
123 return ret;
124 }
125
126 output->native_scale = scale;
127 if(!temporary_scale)
128 notify_scale_changed = 1;
129 break;
130 case WESTON_MODE_SWITCH_SET_TEMPORARY:
131 if (!temporary_mode)
132 output->original_mode = output->native_mode;
133 if (!temporary_scale)
134 output->original_scale = output->native_scale;
135
136 ret = output->switch_mode(output, mode);
137 if (ret < 0)
138 return ret;
139
140 output->current_mode = mode;
141 output->current_scale = scale;
142 break;
143 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
144 if (!temporary_mode) {
145 weston_log("already in the native mode\n");
146 return -1;
147 }
148
149 notify_mode_changed = (output->original_mode != output->native_mode);
150
151 ret = output->switch_mode(output, mode);
152 if (ret < 0)
153 return ret;
154
155 if (output->original_scale != output->native_scale) {
156 notify_scale_changed = 1;
157 scale = output->native_scale;
158 output->original_scale = scale;
159 }
160 output->original_mode = 0;
161
162 output->current_scale = output->native_scale;
163 break;
164 default:
165 weston_log("unknown weston_switch_mode_op %d\n", op);
166 break;
167 }
Alexander Larsson355748e2013-05-28 16:23:38 +0200168
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200169 pixman_region32_init(&old_output_region);
170 pixman_region32_copy(&old_output_region, &output->region);
171
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200172 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200173 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200174
175 pixman_region32_init(&output->previous_damage);
176 pixman_region32_init_rect(&output->region, output->x, output->y,
177 output->width, output->height);
178
179 weston_output_update_matrix(output);
180
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200181 /* If a pointer falls outside the outputs new geometry, move it to its
182 * lower-right corner */
183 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400184 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200185 int32_t x, y;
186
187 if (!pointer)
188 continue;
189
190 x = wl_fixed_to_int(pointer->x);
191 y = wl_fixed_to_int(pointer->y);
192
193 if (!pixman_region32_contains_point(&old_output_region,
194 x, y, NULL) ||
195 pixman_region32_contains_point(&output->region,
196 x, y, NULL))
197 continue;
198
199 if (x >= output->x + output->width)
200 x = output->x + output->width - 1;
201 if (y >= output->y + output->height)
202 y = output->y + output->height - 1;
203
204 pointer->x = wl_fixed_from_int(x);
205 pointer->y = wl_fixed_from_int(y);
206 }
207
208 pixman_region32_fini(&old_output_region);
209
Hardening57388e42013-09-18 23:56:36 +0200210 /* notify clients of the changes */
211 if (notify_mode_changed || notify_scale_changed) {
212 wl_resource_for_each(resource, &output->resource_list) {
213 if(notify_mode_changed) {
214 wl_output_send_mode(resource,
215 mode->flags | WL_OUTPUT_MODE_CURRENT,
216 mode->width,
217 mode->height,
218 mode->refresh);
219 }
220
221 if (notify_scale_changed)
222 wl_output_send_scale(resource, scale);
223
224 if (wl_resource_get_version(resource) >= 2)
225 wl_output_send_done(resource);
226 }
227 }
228
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200229 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800230}
231
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400232WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500233weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400234{
235 wl_list_insert(&child_process_list, &process->link);
236}
237
Benjamin Franzke06286262011-05-06 19:12:33 +0200238static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200239child_client_exec(int sockfd, const char *path)
240{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500241 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200242 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200243 sigset_t allsigs;
244
245 /* do not give our signal mask to the new process */
246 sigfillset(&allsigs);
247 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200248
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100249 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
250 if (seteuid(getuid()) == -1) {
251 weston_log("compositor: failed seteuid\n");
252 return;
253 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500254
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500255 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
256 * non-CLOEXEC fd to pass through exec. */
257 clientfd = dup(sockfd);
258 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200259 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500260 return;
261 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200262
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500263 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200264 setenv("WAYLAND_SOCKET", s, 1);
265
266 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200267 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200268 path);
269}
270
271WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500272weston_client_launch(struct weston_compositor *compositor,
273 struct weston_process *proc,
274 const char *path,
275 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200276{
277 int sv[2];
278 pid_t pid;
279 struct wl_client *client;
280
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300281 weston_log("launching '%s'\n", path);
282
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300283 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200284 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200285 "socketpair failed while launching '%s': %m\n",
286 path);
287 return NULL;
288 }
289
290 pid = fork();
291 if (pid == -1) {
292 close(sv[0]);
293 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200295 "fork failed while launching '%s': %m\n", path);
296 return NULL;
297 }
298
299 if (pid == 0) {
300 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700301 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200302 }
303
304 close(sv[1]);
305
306 client = wl_client_create(compositor->wl_display, sv[0]);
307 if (!client) {
308 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200309 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200310 "wl_client_create failed while launching '%s'.\n",
311 path);
312 return NULL;
313 }
314
315 proc->pid = pid;
316 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500317 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200318
319 return client;
320}
321
322static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300323surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
324{
325 struct weston_surface *surface =
326 container_of(listener, struct weston_surface,
327 pending.buffer_destroy_listener);
328
329 surface->pending.buffer = NULL;
330}
331
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200332static void
333empty_region(pixman_region32_t *region)
334{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300335 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200336 pixman_region32_init(region);
337}
338
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300339static void
340region_init_infinite(pixman_region32_t *region)
341{
342 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
343 UINT32_MAX, UINT32_MAX);
344}
345
Pekka Paalanene67858b2013-04-25 13:57:42 +0300346static struct weston_subsurface *
347weston_surface_to_subsurface(struct weston_surface *surface);
348
Jason Ekstranda7af7042013-10-12 22:38:11 -0500349WL_EXPORT struct weston_view *
350weston_view_create(struct weston_surface *surface)
351{
352 struct weston_view *view;
353
354 view = calloc(1, sizeof *view);
355 if (view == NULL)
356 return NULL;
357
358 view->surface = surface;
359
Jason Ekstranda7af7042013-10-12 22:38:11 -0500360 /* Assign to surface */
361 wl_list_insert(&surface->views, &view->surface_link);
362
363 wl_signal_init(&view->destroy_signal);
364 wl_list_init(&view->link);
365 wl_list_init(&view->layer_link);
366
Xiong Zhang97116532013-10-23 13:58:31 +0800367 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500368
369 pixman_region32_init(&view->clip);
370
371 view->alpha = 1.0;
372 pixman_region32_init(&view->transform.opaque);
373
374 wl_list_init(&view->geometry.transformation_list);
375 wl_list_insert(&view->geometry.transformation_list,
376 &view->transform.position.link);
377 weston_matrix_init(&view->transform.position.matrix);
378 wl_list_init(&view->geometry.child_list);
379 pixman_region32_init(&view->transform.boundingbox);
380 view->transform.dirty = 1;
381
382 view->output = NULL;
383
384 return view;
385}
386
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500387WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500388weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500389{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500390 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400391
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200392 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400393 if (surface == NULL)
394 return NULL;
395
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500396 wl_signal_init(&surface->destroy_signal);
397
398 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500399
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500400 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200401 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400402
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200403 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200404 surface->buffer_scale = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200405 surface->pending.buffer_transform = surface->buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200406 surface->pending.buffer_scale = surface->buffer_scale;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400407 surface->output = NULL;
Giulio Camuffo184df502013-02-21 11:29:21 +0100408 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200409
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400410 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500411 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300412 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400413
Jason Ekstranda7af7042013-10-12 22:38:11 -0500414 wl_list_init(&surface->views);
415
416 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500417
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300418 surface->pending.buffer_destroy_listener.notify =
419 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300420 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300421 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300422 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300423 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300424
Pekka Paalanene67858b2013-04-25 13:57:42 +0300425 wl_list_init(&surface->subsurface_list);
426 wl_list_init(&surface->subsurface_list_pending);
427
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400428 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500429}
430
Alex Wu8811bf92012-02-28 18:07:54 +0800431WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500432weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200433 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500434{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100435 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500436}
437
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400438WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500439weston_view_to_global_float(struct weston_view *view,
440 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200441{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500442 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200443 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
444
Jason Ekstranda7af7042013-10-12 22:38:11 -0500445 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200446
447 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200448 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700449 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200450 v.f[3]);
451 *x = 0;
452 *y = 0;
453 return;
454 }
455
456 *x = v.f[0] / v.f[3];
457 *y = v.f[1] / v.f[3];
458 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500459 *x = sx + view->geometry.x;
460 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200461 }
462}
463
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500464WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200465weston_transformed_coord(int width, int height,
466 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200467 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200468 float sx, float sy, float *bx, float *by)
469{
470 switch (transform) {
471 case WL_OUTPUT_TRANSFORM_NORMAL:
472 default:
473 *bx = sx;
474 *by = sy;
475 break;
476 case WL_OUTPUT_TRANSFORM_FLIPPED:
477 *bx = width - sx;
478 *by = sy;
479 break;
480 case WL_OUTPUT_TRANSFORM_90:
481 *bx = height - sy;
482 *by = sx;
483 break;
484 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
485 *bx = height - sy;
486 *by = width - sx;
487 break;
488 case WL_OUTPUT_TRANSFORM_180:
489 *bx = width - sx;
490 *by = height - sy;
491 break;
492 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
493 *bx = sx;
494 *by = height - sy;
495 break;
496 case WL_OUTPUT_TRANSFORM_270:
497 *bx = sy;
498 *by = width - sx;
499 break;
500 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
501 *bx = sy;
502 *by = sx;
503 break;
504 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200505
506 *bx *= scale;
507 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200508}
509
510WL_EXPORT pixman_box32_t
511weston_transformed_rect(int width, int height,
512 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200513 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200514 pixman_box32_t rect)
515{
516 float x1, x2, y1, y2;
517
518 pixman_box32_t ret;
519
Alexander Larsson4ea95522013-05-22 14:41:37 +0200520 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200521 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200522 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200523 rect.x2, rect.y2, &x2, &y2);
524
525 if (x1 <= x2) {
526 ret.x1 = x1;
527 ret.x2 = x2;
528 } else {
529 ret.x1 = x2;
530 ret.x2 = x1;
531 }
532
533 if (y1 <= y2) {
534 ret.y1 = y1;
535 ret.y2 = y2;
536 } else {
537 ret.y1 = y2;
538 ret.y2 = y1;
539 }
540
541 return ret;
542}
543
544WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500545weston_transformed_region(int width, int height,
546 enum wl_output_transform transform,
547 int32_t scale,
548 pixman_region32_t *src, pixman_region32_t *dest)
549{
550 pixman_box32_t *src_rects, *dest_rects;
551 int nrects, i;
552
553 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
554 if (src != dest)
555 pixman_region32_copy(dest, src);
556 return;
557 }
558
559 src_rects = pixman_region32_rectangles(src, &nrects);
560 dest_rects = malloc(nrects * sizeof(*dest_rects));
561 if (!dest_rects)
562 return;
563
564 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
565 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
566 } else {
567 for (i = 0; i < nrects; i++) {
568 switch (transform) {
569 default:
570 case WL_OUTPUT_TRANSFORM_NORMAL:
571 dest_rects[i].x1 = src_rects[i].x1;
572 dest_rects[i].y1 = src_rects[i].y1;
573 dest_rects[i].x2 = src_rects[i].x2;
574 dest_rects[i].y2 = src_rects[i].y2;
575 break;
576 case WL_OUTPUT_TRANSFORM_90:
577 dest_rects[i].x1 = height - src_rects[i].y2;
578 dest_rects[i].y1 = src_rects[i].x1;
579 dest_rects[i].x2 = height - src_rects[i].y1;
580 dest_rects[i].y2 = src_rects[i].x2;
581 break;
582 case WL_OUTPUT_TRANSFORM_180:
583 dest_rects[i].x1 = width - src_rects[i].x2;
584 dest_rects[i].y1 = height - src_rects[i].y2;
585 dest_rects[i].x2 = width - src_rects[i].x1;
586 dest_rects[i].y2 = height - src_rects[i].y1;
587 break;
588 case WL_OUTPUT_TRANSFORM_270:
589 dest_rects[i].x1 = src_rects[i].y1;
590 dest_rects[i].y1 = width - src_rects[i].x2;
591 dest_rects[i].x2 = src_rects[i].y2;
592 dest_rects[i].y2 = width - src_rects[i].x1;
593 break;
594 case WL_OUTPUT_TRANSFORM_FLIPPED:
595 dest_rects[i].x1 = width - src_rects[i].x2;
596 dest_rects[i].y1 = src_rects[i].y1;
597 dest_rects[i].x2 = width - src_rects[i].x1;
598 dest_rects[i].y2 = src_rects[i].y2;
599 break;
600 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
601 dest_rects[i].x1 = height - src_rects[i].y2;
602 dest_rects[i].y1 = width - src_rects[i].x2;
603 dest_rects[i].x2 = height - src_rects[i].y1;
604 dest_rects[i].y2 = width - src_rects[i].x1;
605 break;
606 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
607 dest_rects[i].x1 = src_rects[i].x1;
608 dest_rects[i].y1 = height - src_rects[i].y2;
609 dest_rects[i].x2 = src_rects[i].x2;
610 dest_rects[i].y2 = height - src_rects[i].y1;
611 break;
612 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
613 dest_rects[i].x1 = src_rects[i].y1;
614 dest_rects[i].y1 = src_rects[i].x1;
615 dest_rects[i].x2 = src_rects[i].y2;
616 dest_rects[i].y2 = src_rects[i].x2;
617 break;
618 }
619 }
620 }
621
622 if (scale != 1) {
623 for (i = 0; i < nrects; i++) {
624 dest_rects[i].x1 *= scale;
625 dest_rects[i].x2 *= scale;
626 dest_rects[i].y1 *= scale;
627 dest_rects[i].y2 *= scale;
628 }
629 }
630
631 pixman_region32_clear(dest);
632 pixman_region32_init_rects(dest, dest_rects, nrects);
633 free(dest_rects);
634}
635
636WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200637weston_surface_to_buffer_float(struct weston_surface *surface,
638 float sx, float sy, float *bx, float *by)
639{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500640 weston_transformed_coord(surface->width,
641 surface->height,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200642 surface->buffer_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200643 surface->buffer_scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200644 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200645}
646
Alexander Larsson4ea95522013-05-22 14:41:37 +0200647WL_EXPORT void
648weston_surface_to_buffer(struct weston_surface *surface,
649 int sx, int sy, int *bx, int *by)
650{
651 float bxf, byf;
652
Jason Ekstranda7af7042013-10-12 22:38:11 -0500653 weston_transformed_coord(surface->width,
654 surface->height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200655 surface->buffer_transform,
656 surface->buffer_scale,
657 sx, sy, &bxf, &byf);
658 *bx = floorf(bxf);
659 *by = floorf(byf);
660}
661
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200662WL_EXPORT pixman_box32_t
663weston_surface_to_buffer_rect(struct weston_surface *surface,
664 pixman_box32_t rect)
665{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500666 return weston_transformed_rect(surface->width,
667 surface->height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200668 surface->buffer_transform,
669 surface->buffer_scale,
670 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200671}
672
673WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500674weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400675 struct weston_plane *plane)
676{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500677 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400678 return;
679
Jason Ekstranda7af7042013-10-12 22:38:11 -0500680 weston_view_damage_below(view);
681 view->plane = plane;
682 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400683}
684
685WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500686weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200687{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500688 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200689
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500690 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500691 pixman_region32_subtract(&damage, &view->transform.boundingbox,
692 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800693 if (view->plane)
694 pixman_region32_union(&view->plane->damage,
695 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500696 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800697 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200698}
699
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400700static void
701weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
702{
703 uint32_t different = es->output_mask ^ mask;
704 uint32_t entered = mask & different;
705 uint32_t left = es->output_mask & different;
706 struct weston_output *output;
707 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500708 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400709
710 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500711 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400712 return;
713 if (different == 0)
714 return;
715
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500716 client = wl_resource_get_client(es->resource);
717
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400718 wl_list_for_each(output, &es->compositor->output_list, link) {
719 if (1 << output->id & different)
720 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500721 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400722 client);
723 if (resource == NULL)
724 continue;
725 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500726 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400727 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500728 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400729 }
730}
731
732static void
733weston_surface_assign_output(struct weston_surface *es)
734{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500735 struct weston_output *new_output;
736 struct weston_view *view;
737 pixman_region32_t region;
738 uint32_t max, area, mask;
739 pixman_box32_t *e;
740
741 new_output = NULL;
742 max = 0;
743 mask = 0;
744 pixman_region32_init(&region);
745 wl_list_for_each(view, &es->views, surface_link) {
746 if (!view->output)
747 continue;
748
749 pixman_region32_intersect(&region, &view->transform.boundingbox,
750 &view->output->region);
751
752 e = pixman_region32_extents(&region);
753 area = (e->x2 - e->x1) * (e->y2 - e->y1);
754
755 mask |= view->output_mask;
756
757 if (area >= max) {
758 new_output = view->output;
759 max = area;
760 }
761 }
762 pixman_region32_fini(&region);
763
764 es->output = new_output;
765 weston_surface_update_output_mask(es, mask);
766}
767
768static void
769weston_view_assign_output(struct weston_view *ev)
770{
771 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400772 struct weston_output *output, *new_output;
773 pixman_region32_t region;
774 uint32_t max, area, mask;
775 pixman_box32_t *e;
776
777 new_output = NULL;
778 max = 0;
779 mask = 0;
780 pixman_region32_init(&region);
781 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500782 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400783 &output->region);
784
785 e = pixman_region32_extents(&region);
786 area = (e->x2 - e->x1) * (e->y2 - e->y1);
787
788 if (area > 0)
789 mask |= 1 << output->id;
790
791 if (area >= max) {
792 new_output = output;
793 max = area;
794 }
795 }
796 pixman_region32_fini(&region);
797
Jason Ekstranda7af7042013-10-12 22:38:11 -0500798 ev->output = new_output;
799 ev->output_mask = mask;
800
801 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400802}
803
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200804static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500805view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
806 int32_t width, int32_t height,
807 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200808{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200809 float min_x = HUGE_VALF, min_y = HUGE_VALF;
810 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200811 int32_t s[4][2] = {
812 { sx, sy },
813 { sx, sy + height },
814 { sx + width, sy },
815 { sx + width, sy + height }
816 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200817 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200818 int i;
819
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300820 if (width == 0 || height == 0) {
821 /* avoid rounding empty bbox to 1x1 */
822 pixman_region32_init(bbox);
823 return;
824 }
825
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200826 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200827 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500828 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200829 if (x < min_x)
830 min_x = x;
831 if (x > max_x)
832 max_x = x;
833 if (y < min_y)
834 min_y = y;
835 if (y > max_y)
836 max_y = y;
837 }
838
Pekka Paalanen219b9822012-02-08 15:38:37 +0200839 int_x = floorf(min_x);
840 int_y = floorf(min_y);
841 pixman_region32_init_rect(bbox, int_x, int_y,
842 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200843}
844
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200845static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500846weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200847{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500848 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200849
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200850 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500851 view->geometry.x = roundf(view->geometry.x);
852 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200853
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500854 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500855 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
856 view->transform.position.matrix.d[12] = view->geometry.x;
857 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500858
Jason Ekstranda7af7042013-10-12 22:38:11 -0500859 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500860
Jason Ekstranda7af7042013-10-12 22:38:11 -0500861 view->transform.inverse = view->transform.position.matrix;
862 view->transform.inverse.d[12] = -view->geometry.x;
863 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500864
Jason Ekstranda7af7042013-10-12 22:38:11 -0500865 pixman_region32_init_rect(&view->transform.boundingbox,
866 view->geometry.x,
867 view->geometry.y,
868 view->geometry.width,
869 view->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500870
Jason Ekstranda7af7042013-10-12 22:38:11 -0500871 if (view->alpha == 1.0) {
872 pixman_region32_copy(&view->transform.opaque,
873 &view->surface->opaque);
874 pixman_region32_translate(&view->transform.opaque,
875 view->geometry.x,
876 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500877 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200878}
879
880static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500881weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200882{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500883 struct weston_view *parent = view->geometry.parent;
884 struct weston_matrix *matrix = &view->transform.matrix;
885 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200886 struct weston_transform *tform;
887
Jason Ekstranda7af7042013-10-12 22:38:11 -0500888 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200889
890 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500891 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
892 view->transform.position.matrix.d[12] = view->geometry.x;
893 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200894
895 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500896 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200897 weston_matrix_multiply(matrix, &tform->matrix);
898
Pekka Paalanen483243f2013-03-08 14:56:50 +0200899 if (parent)
900 weston_matrix_multiply(matrix, &parent->transform.matrix);
901
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200902 if (weston_matrix_invert(inverse, matrix) < 0) {
903 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500904 weston_log("error: weston_view %p"
905 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200906 return -1;
907 }
908
Jason Ekstranda7af7042013-10-12 22:38:11 -0500909 view_compute_bbox(view, 0, 0, view->geometry.width,
910 view->geometry.height,
911 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500912
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200913 return 0;
914}
915
916WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500917weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200918{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500919 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200920
Jason Ekstranda7af7042013-10-12 22:38:11 -0500921 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200922 return;
923
Pekka Paalanen483243f2013-03-08 14:56:50 +0200924 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500925 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200926
Jason Ekstranda7af7042013-10-12 22:38:11 -0500927 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200928
Jason Ekstranda7af7042013-10-12 22:38:11 -0500929 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200930
Jason Ekstranda7af7042013-10-12 22:38:11 -0500931 pixman_region32_fini(&view->transform.boundingbox);
932 pixman_region32_fini(&view->transform.opaque);
933 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500934
Pekka Paalanencd403622012-01-25 13:37:39 +0200935 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500936 if (view->geometry.transformation_list.next ==
937 &view->transform.position.link &&
938 view->geometry.transformation_list.prev ==
939 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +0200940 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500941 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200942 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500943 if (weston_view_update_transform_enable(view) < 0)
944 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200945 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200946
Jason Ekstranda7af7042013-10-12 22:38:11 -0500947 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200948
Jason Ekstranda7af7042013-10-12 22:38:11 -0500949 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300950
Jason Ekstranda7af7042013-10-12 22:38:11 -0500951 wl_signal_emit(&view->surface->compositor->transform_signal,
952 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200953}
954
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200955WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500956weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200957{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500958 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200959
960 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -0500961 * The invariant: if view->geometry.dirty, then all views
962 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +0200963 * Corollary: if not parent->geometry.dirty, then all ancestors
964 * are not dirty.
965 */
966
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +0200968 return;
969
Jason Ekstranda7af7042013-10-12 22:38:11 -0500970 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200971
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200973 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500974 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200975}
976
977WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500978weston_view_to_global_fixed(struct weston_view *view,
979 wl_fixed_t vx, wl_fixed_t vy,
980 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +0100981{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200982 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100983
Jason Ekstranda7af7042013-10-12 22:38:11 -0500984 weston_view_to_global_float(view,
985 wl_fixed_to_double(vx),
986 wl_fixed_to_double(vy),
987 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100988 *x = wl_fixed_from_double(xf);
989 *y = wl_fixed_from_double(yf);
990}
991
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400992WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500993weston_view_from_global_float(struct weston_view *view,
994 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200995{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500996 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200997 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
998
Jason Ekstranda7af7042013-10-12 22:38:11 -0500999 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001000
1001 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001002 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001004 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 *vx = 0;
1006 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001007 return;
1008 }
1009
Jason Ekstranda7af7042013-10-12 22:38:11 -05001010 *vx = v.f[0] / v.f[3];
1011 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001012 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001013 *vx = x - view->geometry.x;
1014 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001015 }
1016}
1017
1018WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001019weston_view_from_global_fixed(struct weston_view *view,
1020 wl_fixed_t x, wl_fixed_t y,
1021 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001022{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001023 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001024
Jason Ekstranda7af7042013-10-12 22:38:11 -05001025 weston_view_from_global_float(view,
1026 wl_fixed_to_double(x),
1027 wl_fixed_to_double(y),
1028 &vxf, &vyf);
1029 *vx = wl_fixed_from_double(vxf);
1030 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001031}
1032
1033WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001034weston_view_from_global(struct weston_view *view,
1035 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001036{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001038
Jason Ekstranda7af7042013-10-12 22:38:11 -05001039 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1040 *vx = floorf(vxf);
1041 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001042}
1043
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001044WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001045weston_surface_schedule_repaint(struct weston_surface *surface)
1046{
1047 struct weston_output *output;
1048
1049 wl_list_for_each(output, &surface->compositor->output_list, link)
1050 if (surface->output_mask & (1 << output->id))
1051 weston_output_schedule_repaint(output);
1052}
1053
1054WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001055weston_view_schedule_repaint(struct weston_view *view)
1056{
1057 struct weston_output *output;
1058
1059 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1060 if (view->output_mask & (1 << output->id))
1061 weston_output_schedule_repaint(output);
1062}
1063
1064WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001065weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001066{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001067 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001068 0, 0, surface->width,
1069 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001070
Kristian Høgsberg98238702012-08-03 16:29:12 -04001071 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001072}
1073
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001074WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001075weston_view_configure(struct weston_view *view,
1076 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001077{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001078 view->geometry.x = x;
1079 view->geometry.y = y;
1080 view->geometry.width = width;
1081 view->geometry.height = height;
1082 weston_view_geometry_dirty(view);
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001083}
1084
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001085WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001086weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001087{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001088 view->geometry.x = x;
1089 view->geometry.y = y;
1090 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001091}
1092
Pekka Paalanen483243f2013-03-08 14:56:50 +02001093static void
1094transform_parent_handle_parent_destroy(struct wl_listener *listener,
1095 void *data)
1096{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001097 struct weston_view *view =
1098 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001099 geometry.parent_destroy_listener);
1100
Jason Ekstranda7af7042013-10-12 22:38:11 -05001101 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001102}
1103
1104WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001105weston_view_set_transform_parent(struct weston_view *view,
1106 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001107{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001108 if (view->geometry.parent) {
1109 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1110 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001111 }
1112
Jason Ekstranda7af7042013-10-12 22:38:11 -05001113 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001114
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001116 transform_parent_handle_parent_destroy;
1117 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001118 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001119 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001120 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001121 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001122 }
1123
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124 weston_view_geometry_dirty(view);
1125}
1126
1127WL_EXPORT int
1128weston_view_is_mapped(struct weston_view *view)
1129{
1130 if (view->output)
1131 return 1;
1132 else
1133 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001134}
1135
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001136WL_EXPORT int
1137weston_surface_is_mapped(struct weston_surface *surface)
1138{
1139 if (surface->output)
1140 return 1;
1141 else
1142 return 0;
1143}
1144
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001145WL_EXPORT int32_t
1146weston_surface_buffer_width(struct weston_surface *surface)
1147{
Alexander Larsson4ea95522013-05-22 14:41:37 +02001148 int32_t width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001149 switch (surface->buffer_transform) {
1150 case WL_OUTPUT_TRANSFORM_90:
1151 case WL_OUTPUT_TRANSFORM_270:
1152 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1153 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001154 width = surface->buffer_ref.buffer->height;
1155 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001156 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001157 width = surface->buffer_ref.buffer->width;
1158 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001159 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02001160 return width / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001161}
1162
1163WL_EXPORT int32_t
1164weston_surface_buffer_height(struct weston_surface *surface)
1165{
Alexander Larsson4ea95522013-05-22 14:41:37 +02001166 int32_t height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001167 switch (surface->buffer_transform) {
1168 case WL_OUTPUT_TRANSFORM_90:
1169 case WL_OUTPUT_TRANSFORM_270:
1170 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1171 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001172 height = surface->buffer_ref.buffer->width;
1173 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001174 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001175 height = surface->buffer_ref.buffer->height;
1176 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001177 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02001178 return height / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001179}
1180
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001181WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001182weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001183{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001184 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001185
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001186 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001187
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001188 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001189}
1190
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191WL_EXPORT struct weston_view *
1192weston_compositor_pick_view(struct weston_compositor *compositor,
1193 wl_fixed_t x, wl_fixed_t y,
1194 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001195{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001197
Jason Ekstranda7af7042013-10-12 22:38:11 -05001198 wl_list_for_each(view, &compositor->view_list, link) {
1199 weston_view_from_global_fixed(view, x, y, vx, vy);
1200 if (pixman_region32_contains_point(&view->surface->input,
1201 wl_fixed_to_int(*vx),
1202 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001203 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001204 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001205 }
1206
1207 return NULL;
1208}
1209
1210static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001211weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001212{
Daniel Stone37816df2012-05-16 18:45:18 +01001213 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001214
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001215 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001216 return;
1217
Daniel Stone37816df2012-05-16 18:45:18 +01001218 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001219 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001220}
1221
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001222WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001224{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001225 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001226
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 if (!weston_view_is_mapped(view))
1228 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001229
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 weston_view_damage_below(view);
1231 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001232 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 wl_list_remove(&view->layer_link);
1234 wl_list_init(&view->layer_link);
1235 wl_list_remove(&view->link);
1236 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237 view->output_mask = 0;
1238 weston_surface_assign_output(view->surface);
1239
1240 if (weston_surface_is_mapped(view->surface))
1241 return;
1242
1243 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1244 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001245 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001247 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001248 NULL,
1249 wl_fixed_from_int(0),
1250 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001251 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001252 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001253 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001254}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001255
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256WL_EXPORT void
1257weston_surface_unmap(struct weston_surface *surface)
1258{
1259 struct weston_view *view;
1260
1261 wl_list_for_each(view, &surface->views, surface_link)
1262 weston_view_unmap(view);
1263 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001264}
1265
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001266struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001267 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001268 struct wl_list link;
1269};
1270
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271WL_EXPORT void
1272weston_view_destroy(struct weston_view *view)
1273{
1274 wl_signal_emit(&view->destroy_signal, view);
1275
1276 assert(wl_list_empty(&view->geometry.child_list));
1277
1278 if (weston_view_is_mapped(view)) {
1279 weston_view_unmap(view);
1280 weston_compositor_build_view_list(view->surface->compositor);
1281 }
1282
1283 wl_list_remove(&view->link);
1284 wl_list_remove(&view->layer_link);
1285
1286 pixman_region32_fini(&view->clip);
1287 pixman_region32_fini(&view->transform.boundingbox);
1288
1289 weston_view_set_transform_parent(view, NULL);
1290
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291 wl_list_remove(&view->surface_link);
1292
1293 free(view);
1294}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001295
1296WL_EXPORT void
1297weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001298{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001299 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001300 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001301
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001302 if (--surface->ref_count > 0)
1303 return;
1304
1305 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1306
Pekka Paalanene67858b2013-04-25 13:57:42 +03001307 assert(wl_list_empty(&surface->subsurface_list_pending));
1308 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001309
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1311 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001312
Pekka Paalanenbc106382012-10-10 12:49:31 +03001313 wl_list_for_each_safe(cb, next,
1314 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001315 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001316
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001317 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001318 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001319 pixman_region32_fini(&surface->pending.damage);
1320
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001321 if (surface->pending.buffer)
1322 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1323
Pekka Paalanende685b82012-12-04 15:58:12 +02001324 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001325
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001326 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001327 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001328 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001329
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001330 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001331 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001332
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001333 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001334}
1335
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001336static void
1337destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001338{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001339 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001340
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001341 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001342}
1343
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001344static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001345weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1346{
1347 struct weston_buffer *buffer =
1348 container_of(listener, struct weston_buffer, destroy_listener);
1349
1350 wl_signal_emit(&buffer->destroy_signal, buffer);
1351 free(buffer);
1352}
1353
1354struct weston_buffer *
1355weston_buffer_from_resource(struct wl_resource *resource)
1356{
1357 struct weston_buffer *buffer;
1358 struct wl_listener *listener;
1359
1360 listener = wl_resource_get_destroy_listener(resource,
1361 weston_buffer_destroy_handler);
1362
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001363 if (listener)
1364 return container_of(listener, struct weston_buffer,
1365 destroy_listener);
1366
1367 buffer = zalloc(sizeof *buffer);
1368 if (buffer == NULL)
1369 return NULL;
1370
1371 buffer->resource = resource;
1372 wl_signal_init(&buffer->destroy_signal);
1373 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001374 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001375 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001376
1377 return buffer;
1378}
1379
1380static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001381weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1382 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001383{
Pekka Paalanende685b82012-12-04 15:58:12 +02001384 struct weston_buffer_reference *ref =
1385 container_of(listener, struct weston_buffer_reference,
1386 destroy_listener);
1387
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001388 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001389 ref->buffer = NULL;
1390}
1391
1392WL_EXPORT void
1393weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001394 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001395{
1396 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001397 ref->buffer->busy_count--;
1398 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001399 assert(wl_resource_get_client(ref->buffer->resource));
1400 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001401 WL_BUFFER_RELEASE);
1402 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001403 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001404 }
1405
Pekka Paalanende685b82012-12-04 15:58:12 +02001406 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001407 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001408 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001409 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001410 }
1411
Pekka Paalanende685b82012-12-04 15:58:12 +02001412 ref->buffer = buffer;
1413 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1414}
1415
1416static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001417weston_surface_attach(struct weston_surface *surface,
1418 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001419{
1420 weston_buffer_reference(&surface->buffer_ref, buffer);
1421
Pekka Paalanena6421c42012-12-04 15:58:10 +02001422 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001423 if (weston_surface_is_mapped(surface))
1424 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001425 }
1426
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001427 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001428}
1429
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001430WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431weston_view_restack(struct weston_view *view, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001432{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433 wl_list_remove(&view->layer_link);
1434 wl_list_insert(below, &view->layer_link);
1435 weston_view_damage_below(view);
1436 weston_surface_damage(view->surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001437}
1438
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001439WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001440weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001441{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001442 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001443
1444 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001445 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001446}
1447
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001448WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001449weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001450{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001451 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001452
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001453 pixman_region32_union(&compositor->primary_plane.damage,
1454 &compositor->primary_plane.damage,
1455 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001456 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001457}
1458
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001459static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001461{
Pekka Paalanende685b82012-12-04 15:58:12 +02001462 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001463 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001464 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001465
Jason Ekstranda7af7042013-10-12 22:38:11 -05001466 empty_region(&surface->damage);
1467}
1468
1469static void
1470view_accumulate_damage(struct weston_view *view,
1471 pixman_region32_t *opaque)
1472{
1473 pixman_region32_t damage;
1474
1475 pixman_region32_init(&damage);
1476 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001477 pixman_box32_t *extents;
1478
Jason Ekstranda7af7042013-10-12 22:38:11 -05001479 extents = pixman_region32_extents(&view->surface->damage);
1480 view_compute_bbox(view, extents->x1, extents->y1,
1481 extents->x2 - extents->x1,
1482 extents->y2 - extents->y1,
1483 &damage);
1484 pixman_region32_translate(&damage,
1485 -view->plane->x,
1486 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001487 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001488 pixman_region32_copy(&damage, &view->surface->damage);
1489 pixman_region32_translate(&damage,
1490 view->geometry.x - view->plane->x,
1491 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001492 }
1493
Jason Ekstranda7af7042013-10-12 22:38:11 -05001494 pixman_region32_subtract(&damage, &damage, opaque);
1495 pixman_region32_union(&view->plane->damage,
1496 &view->plane->damage, &damage);
1497 pixman_region32_fini(&damage);
1498 pixman_region32_copy(&view->clip, opaque);
1499 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001500}
1501
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001502static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001503compositor_accumulate_damage(struct weston_compositor *ec)
1504{
1505 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001506 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001507 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001508
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001509 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001510
1511 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001512 pixman_region32_copy(&plane->clip, &clip);
1513
1514 pixman_region32_init(&opaque);
1515
Jason Ekstranda7af7042013-10-12 22:38:11 -05001516 wl_list_for_each(ev, &ec->view_list, link) {
1517 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001518 continue;
1519
Jason Ekstranda7af7042013-10-12 22:38:11 -05001520 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001521 }
1522
1523 pixman_region32_union(&clip, &clip, &opaque);
1524 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001525 }
1526
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001527 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001528
Jason Ekstranda7af7042013-10-12 22:38:11 -05001529 wl_list_for_each(ev, &ec->view_list, link)
1530 ev->surface->touched = 0;
1531
1532 wl_list_for_each(ev, &ec->view_list, link) {
1533 if (ev->surface->touched)
1534 continue;
1535 ev->surface->touched = 1;
1536
1537 surface_flush_damage(ev->surface);
1538
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001539 /* Both the renderer and the backend have seen the buffer
1540 * by now. If renderer needs the buffer, it has its own
1541 * reference set. If the backend wants to keep the buffer
1542 * around for migrating the surface into a non-primary plane
1543 * later, keep_buffer is true. Otherwise, drop the core
1544 * reference now, and allow early buffer release. This enables
1545 * clients to use single-buffering.
1546 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 if (!ev->surface->keep_buffer)
1548 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001549 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001550}
1551
1552static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001553surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001554{
1555 struct weston_subsurface *sub;
1556
Pekka Paalanene67858b2013-04-25 13:57:42 +03001557 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001558 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001559 continue;
1560
Jason Ekstranda7af7042013-10-12 22:38:11 -05001561 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1562 wl_list_init(&sub->surface->views);
1563
1564 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001565 }
1566}
1567
1568static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001569surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001570{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001571 struct weston_subsurface *sub;
1572 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001573
Jason Ekstranda7af7042013-10-12 22:38:11 -05001574 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1575 if (sub->surface == surface)
1576 continue;
1577
1578 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1579 weston_view_destroy(view);
1580
1581 surface_free_unused_subsurface_views(sub->surface);
1582 }
1583}
1584
1585static void
1586view_list_add_subsurface_view(struct weston_compositor *compositor,
1587 struct weston_subsurface *sub,
1588 struct weston_view *parent)
1589{
1590 struct weston_subsurface *child;
1591 struct weston_view *view = NULL, *iv;
1592
1593 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1594 if (iv->geometry.parent == parent) {
1595 view = iv;
1596 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001597 }
1598 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001599
1600 if (view) {
1601 /* Put it back in the surface's list of views */
1602 wl_list_remove(&view->surface_link);
1603 wl_list_insert(&sub->surface->views, &view->surface_link);
1604 } else {
1605 view = weston_view_create(sub->surface);
1606 weston_view_configure(view,
1607 sub->position.x,
1608 sub->position.y,
1609 sub->surface->width,
1610 sub->surface->height);
1611 weston_view_set_transform_parent(view, parent);
1612 }
1613
1614 weston_view_update_transform(view);
1615 wl_list_insert(compositor->view_list.next, &view->link);
1616
1617 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
1618 if (child->surface != sub->surface)
1619 view_list_add_subsurface_view(compositor, child, view);
1620}
1621
1622static void
1623view_list_add(struct weston_compositor *compositor,
1624 struct weston_view *view)
1625{
1626 struct weston_subsurface *sub;
1627
1628 weston_view_update_transform(view);
1629 wl_list_insert(compositor->view_list.prev, &view->link);
1630
1631 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link)
1632 if (sub->surface != view->surface)
1633 view_list_add_subsurface_view(compositor, sub, view);
1634}
1635
1636static void
1637weston_compositor_build_view_list(struct weston_compositor *compositor)
1638{
1639 struct weston_view *view;
1640 struct weston_layer *layer;
1641
1642 wl_list_for_each(layer, &compositor->layer_list, link)
1643 wl_list_for_each(view, &layer->view_list, layer_link)
1644 surface_stash_subsurface_views(view->surface);
1645
1646 wl_list_init(&compositor->view_list);
1647 wl_list_for_each(layer, &compositor->layer_list, link) {
1648 wl_list_for_each(view, &layer->view_list, layer_link) {
1649 view_list_add(compositor, view);
1650 }
1651 }
1652
1653 wl_list_for_each(layer, &compositor->layer_list, link)
1654 wl_list_for_each(view, &layer->view_list, layer_link)
1655 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001656}
1657
David Herrmann1edf44c2013-10-22 17:11:26 +02001658static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001659weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001660{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001661 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001662 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001663 struct weston_animation *animation, *next;
1664 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001665 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001666 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001667 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001668
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001669 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001670 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001671
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001672 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001673 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001674 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001675 wl_list_for_each(ev, &ec->view_list, link)
1676 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001677
Pekka Paalanene67858b2013-04-25 13:57:42 +03001678 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001679 wl_list_for_each(ev, &ec->view_list, link) {
1680 /* Note: This operation is safe to do multiple times on the
1681 * same surface.
1682 */
1683 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001684 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001685 &ev->surface->frame_callback_list);
1686 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001687 }
1688 }
1689
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001690 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001691
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001692 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001693 pixman_region32_intersect(&output_damage,
1694 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001695 pixman_region32_subtract(&output_damage,
1696 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001697
Scott Moreauccbf29d2012-02-22 14:21:41 -07001698 if (output->dirty)
1699 weston_output_update_matrix(output);
1700
David Herrmann1edf44c2013-10-22 17:11:26 +02001701 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001702
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001703 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001704
Kristian Høgsbergef044142011-06-21 15:02:12 -04001705 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001706
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001707 weston_compositor_repick(ec);
1708 wl_event_loop_dispatch(ec->input_loop, 0);
1709
Jonas Ådahldb773762012-06-13 00:01:21 +02001710 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001711 wl_callback_send_done(cb->resource, msecs);
1712 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001713 }
1714
Scott Moreaud64cf212012-06-08 19:40:54 -06001715 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001716 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001717 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001718 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001719
1720 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001721}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001722
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001723static int
1724weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001725{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001726 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001727
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001728 wl_event_loop_dispatch(compositor->input_loop, 0);
1729
1730 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001731}
1732
1733WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001734weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001735{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001736 struct weston_compositor *compositor = output->compositor;
1737 struct wl_event_loop *loop =
1738 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001739 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001740
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001741 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001742
1743 if (output->repaint_needed &&
1744 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1745 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001746 r = weston_output_repaint(output, msecs);
1747 if (!r)
1748 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001749 }
1750
1751 output->repaint_scheduled = 0;
1752 if (compositor->input_loop_source)
1753 return;
1754
1755 fd = wl_event_loop_get_fd(compositor->input_loop);
1756 compositor->input_loop_source =
1757 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1758 weston_compositor_read_input, compositor);
1759}
1760
1761static void
1762idle_repaint(void *data)
1763{
1764 struct weston_output *output = data;
1765
Jonas Ådahle5a12252013-04-05 23:07:11 +02001766 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001767}
1768
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001769WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001770weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1771{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001772 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001773 if (below != NULL)
1774 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001775}
1776
1777WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001778weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001779{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001780 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001781 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001782
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001783 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1784 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001785 return;
1786
Kristian Høgsbergef044142011-06-21 15:02:12 -04001787 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001788 output->repaint_needed = 1;
1789 if (output->repaint_scheduled)
1790 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001791
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001792 wl_event_loop_add_idle(loop, idle_repaint, output);
1793 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001794
1795 if (compositor->input_loop_source) {
1796 wl_event_source_remove(compositor->input_loop_source);
1797 compositor->input_loop_source = NULL;
1798 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001799}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001800
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001801WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001802weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1803{
1804 struct weston_output *output;
1805
1806 wl_list_for_each(output, &compositor->output_list, link)
1807 weston_output_schedule_repaint(output);
1808}
1809
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001810static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001811surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001812{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001813 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001814}
1815
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001816static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001817surface_attach(struct wl_client *client,
1818 struct wl_resource *resource,
1819 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1820{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001821 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001822 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001823
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001824 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001825 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001826 if (buffer == NULL) {
1827 wl_client_post_no_memory(client);
1828 return;
1829 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001830 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001831
Pekka Paalanende685b82012-12-04 15:58:12 +02001832 /* Attach, attach, without commit in between does not send
1833 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001834 if (surface->pending.buffer)
1835 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001836
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001837 surface->pending.sx = sx;
1838 surface->pending.sy = sy;
1839 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001840 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001841 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001842 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001843 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001844 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001845}
1846
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001847static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001848surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001849 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001850 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001851{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001852 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001853
Pekka Paalanen8e159182012-10-10 12:49:25 +03001854 pixman_region32_union_rect(&surface->pending.damage,
1855 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001856 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001857}
1858
Kristian Høgsberg33418202011-08-16 23:01:28 -04001859static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001860destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001861{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001862 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001863
1864 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001865 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001866}
1867
1868static void
1869surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001870 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001871{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001872 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001873 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001874
1875 cb = malloc(sizeof *cb);
1876 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001877 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001878 return;
1879 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001880
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001881 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1882 callback);
1883 if (cb->resource == NULL) {
1884 free(cb);
1885 wl_resource_post_no_memory(resource);
1886 return;
1887 }
1888
Jason Ekstranda85118c2013-06-27 20:17:02 -05001889 wl_resource_set_implementation(cb->resource, NULL, cb,
1890 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001891
Pekka Paalanenbc106382012-10-10 12:49:31 +03001892 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001893}
1894
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001895static void
1896surface_set_opaque_region(struct wl_client *client,
1897 struct wl_resource *resource,
1898 struct wl_resource *region_resource)
1899{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001900 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001901 struct weston_region *region;
1902
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001903 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001904 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001905 pixman_region32_copy(&surface->pending.opaque,
1906 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001907 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001908 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001909 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001910}
1911
1912static void
1913surface_set_input_region(struct wl_client *client,
1914 struct wl_resource *resource,
1915 struct wl_resource *region_resource)
1916{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001917 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001918 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001919
1920 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001921 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001922 pixman_region32_copy(&surface->pending.input,
1923 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001924 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001925 pixman_region32_fini(&surface->pending.input);
1926 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001927 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001928}
1929
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001930static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001931weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001932{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001933 struct weston_subsurface *sub;
1934
1935 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1936 parent_link_pending) {
1937 wl_list_remove(&sub->parent_link);
1938 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1939 }
1940}
1941
1942static void
1943weston_surface_commit(struct weston_surface *surface)
1944{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001945 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001946 pixman_region32_t opaque;
1947
Alexander Larsson4ea95522013-05-22 14:41:37 +02001948 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001949 surface->buffer_transform = surface->pending.buffer_transform;
1950
Alexander Larsson4ea95522013-05-22 14:41:37 +02001951 /* wl_surface.set_buffer_scale */
1952 surface->buffer_scale = surface->pending.buffer_scale;
1953
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001954 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001955 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001956 weston_surface_attach(surface, surface->pending.buffer);
1957
Jason Ekstranda7af7042013-10-12 22:38:11 -05001958 surface->width = 0;
1959 surface->height = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001960 if (surface->buffer_ref.buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001961 /* This already includes the buffer scale */
1962 surface->width = weston_surface_buffer_width(surface);
1963 surface->height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001964 }
1965
1966 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001967 surface->configure(surface,
1968 surface->pending.sx, surface->pending.sy,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001969 surface->width, surface->height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001970
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001971 if (surface->pending.buffer)
1972 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1973 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001974 surface->pending.sx = 0;
1975 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001976 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001977
1978 /* wl_surface.damage */
1979 pixman_region32_union(&surface->damage, &surface->damage,
1980 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001981 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1982 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001983 surface->width,
1984 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001985 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001986
1987 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001988 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001989 surface->width,
1990 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001991 pixman_region32_intersect(&opaque,
1992 &opaque, &surface->pending.opaque);
1993
1994 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1995 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001996 wl_list_for_each(view, &surface->views, surface_link)
1997 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001998 }
1999
2000 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002001
2002 /* wl_surface.set_input_region */
2003 pixman_region32_fini(&surface->input);
2004 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002005 surface->width,
2006 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002007 pixman_region32_intersect(&surface->input,
2008 &surface->input, &surface->pending.input);
2009
Pekka Paalanenbc106382012-10-10 12:49:31 +03002010 /* wl_surface.frame */
2011 wl_list_insert_list(&surface->frame_callback_list,
2012 &surface->pending.frame_callback_list);
2013 wl_list_init(&surface->pending.frame_callback_list);
2014
Pekka Paalanene67858b2013-04-25 13:57:42 +03002015 weston_surface_commit_subsurface_order(surface);
2016
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002017 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002018}
2019
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002020static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002021weston_subsurface_commit(struct weston_subsurface *sub);
2022
2023static void
2024weston_subsurface_parent_commit(struct weston_subsurface *sub,
2025 int parent_is_synchronized);
2026
2027static void
2028surface_commit(struct wl_client *client, struct wl_resource *resource)
2029{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002030 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002031 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2032
2033 if (sub) {
2034 weston_subsurface_commit(sub);
2035 return;
2036 }
2037
2038 weston_surface_commit(surface);
2039
2040 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2041 if (sub->surface != surface)
2042 weston_subsurface_parent_commit(sub, 0);
2043 }
2044}
2045
2046static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002047surface_set_buffer_transform(struct wl_client *client,
2048 struct wl_resource *resource, int transform)
2049{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002050 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002051
2052 surface->pending.buffer_transform = transform;
2053}
2054
Alexander Larsson4ea95522013-05-22 14:41:37 +02002055static void
2056surface_set_buffer_scale(struct wl_client *client,
2057 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002058 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002059{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002060 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002061
2062 surface->pending.buffer_scale = scale;
2063}
2064
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002065static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002066 surface_destroy,
2067 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002068 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002069 surface_frame,
2070 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002071 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002072 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002073 surface_set_buffer_transform,
2074 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002075};
2076
2077static void
2078compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002079 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002080{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002081 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002082 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002083
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002084 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002085 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002086 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002087 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002088 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002089
Jason Ekstranda85118c2013-06-27 20:17:02 -05002090 surface->resource =
2091 wl_resource_create(client, &wl_surface_interface,
2092 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002093 if (surface->resource == NULL) {
2094 weston_surface_destroy(surface);
2095 wl_resource_post_no_memory(resource);
2096 return;
2097 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002098 wl_resource_set_implementation(surface->resource, &surface_interface,
2099 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002100}
2101
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002102static void
2103destroy_region(struct wl_resource *resource)
2104{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002105 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002106
2107 pixman_region32_fini(&region->region);
2108 free(region);
2109}
2110
2111static void
2112region_destroy(struct wl_client *client, struct wl_resource *resource)
2113{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002114 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002115}
2116
2117static void
2118region_add(struct wl_client *client, struct wl_resource *resource,
2119 int32_t x, int32_t y, int32_t width, int32_t height)
2120{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002121 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002122
2123 pixman_region32_union_rect(&region->region, &region->region,
2124 x, y, width, height);
2125}
2126
2127static void
2128region_subtract(struct wl_client *client, struct wl_resource *resource,
2129 int32_t x, int32_t y, int32_t width, int32_t height)
2130{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002131 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002132 pixman_region32_t rect;
2133
2134 pixman_region32_init_rect(&rect, x, y, width, height);
2135 pixman_region32_subtract(&region->region, &region->region, &rect);
2136 pixman_region32_fini(&rect);
2137}
2138
2139static const struct wl_region_interface region_interface = {
2140 region_destroy,
2141 region_add,
2142 region_subtract
2143};
2144
2145static void
2146compositor_create_region(struct wl_client *client,
2147 struct wl_resource *resource, uint32_t id)
2148{
2149 struct weston_region *region;
2150
2151 region = malloc(sizeof *region);
2152 if (region == NULL) {
2153 wl_resource_post_no_memory(resource);
2154 return;
2155 }
2156
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002157 pixman_region32_init(&region->region);
2158
Jason Ekstranda85118c2013-06-27 20:17:02 -05002159 region->resource =
2160 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002161 if (region->resource == NULL) {
2162 free(region);
2163 wl_resource_post_no_memory(resource);
2164 return;
2165 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002166 wl_resource_set_implementation(region->resource, &region_interface,
2167 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002168}
2169
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002170static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002171 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002172 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002173};
2174
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002175static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002176weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2177{
2178 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002179 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002180 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002181
Alexander Larsson4ea95522013-05-22 14:41:37 +02002182 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03002183 surface->buffer_transform = sub->cached.buffer_transform;
2184
Alexander Larsson4ea95522013-05-22 14:41:37 +02002185 /* wl_surface.set_buffer_scale */
2186 surface->buffer_scale = sub->cached.buffer_scale;
2187
Pekka Paalanene67858b2013-04-25 13:57:42 +03002188 /* wl_surface.attach */
2189 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
2190 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2191 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2192
Jason Ekstranda7af7042013-10-12 22:38:11 -05002193 surface->width = 0;
2194 surface->height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002195 if (surface->buffer_ref.buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002196 surface->width = weston_surface_buffer_width(surface);
2197 surface->height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002198 }
2199
2200 if (surface->configure && sub->cached.newly_attached)
2201 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002202 surface->width, surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002203 sub->cached.sx = 0;
2204 sub->cached.sy = 0;
2205 sub->cached.newly_attached = 0;
2206
2207 /* wl_surface.damage */
2208 pixman_region32_union(&surface->damage, &surface->damage,
2209 &sub->cached.damage);
2210 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2211 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002212 surface->width,
2213 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002214 empty_region(&sub->cached.damage);
2215
2216 /* wl_surface.set_opaque_region */
2217 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002218 surface->width,
2219 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002220 pixman_region32_intersect(&opaque,
2221 &opaque, &sub->cached.opaque);
2222
2223 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2224 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002225 wl_list_for_each(view, &surface->views, surface_link)
2226 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002227 }
2228
2229 pixman_region32_fini(&opaque);
2230
2231 /* wl_surface.set_input_region */
2232 pixman_region32_fini(&surface->input);
2233 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002234 surface->width,
2235 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002236 pixman_region32_intersect(&surface->input,
2237 &surface->input, &sub->cached.input);
2238
2239 /* wl_surface.frame */
2240 wl_list_insert_list(&surface->frame_callback_list,
2241 &sub->cached.frame_callback_list);
2242 wl_list_init(&sub->cached.frame_callback_list);
2243
2244 weston_surface_commit_subsurface_order(surface);
2245
2246 weston_surface_schedule_repaint(surface);
2247
2248 sub->cached.has_data = 0;
2249}
2250
2251static void
2252weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2253{
2254 struct weston_surface *surface = sub->surface;
2255
2256 /*
2257 * If this commit would cause the surface to move by the
2258 * attach(dx, dy) parameters, the old damage region must be
2259 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002260 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002261 */
2262 pixman_region32_translate(&sub->cached.damage,
2263 -surface->pending.sx, -surface->pending.sy);
2264 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2265 &surface->pending.damage);
2266 empty_region(&surface->pending.damage);
2267
2268 if (surface->pending.newly_attached) {
2269 sub->cached.newly_attached = 1;
2270 weston_buffer_reference(&sub->cached.buffer_ref,
2271 surface->pending.buffer);
2272 }
2273 sub->cached.sx += surface->pending.sx;
2274 sub->cached.sy += surface->pending.sy;
2275 surface->pending.sx = 0;
2276 surface->pending.sy = 0;
2277 surface->pending.newly_attached = 0;
2278
2279 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002280 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002281
2282 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2283
2284 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2285
2286 wl_list_insert_list(&sub->cached.frame_callback_list,
2287 &surface->pending.frame_callback_list);
2288 wl_list_init(&surface->pending.frame_callback_list);
2289
2290 sub->cached.has_data = 1;
2291}
2292
2293static int
2294weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2295{
2296 while (sub) {
2297 if (sub->synchronized)
2298 return 1;
2299
2300 if (!sub->parent)
2301 return 0;
2302
2303 sub = weston_surface_to_subsurface(sub->parent);
2304 }
2305
2306 return 0;
2307}
2308
2309static void
2310weston_subsurface_commit(struct weston_subsurface *sub)
2311{
2312 struct weston_surface *surface = sub->surface;
2313 struct weston_subsurface *tmp;
2314
2315 /* Recursive check for effectively synchronized. */
2316 if (weston_subsurface_is_synchronized(sub)) {
2317 weston_subsurface_commit_to_cache(sub);
2318 } else {
2319 if (sub->cached.has_data) {
2320 /* flush accumulated state from cache */
2321 weston_subsurface_commit_to_cache(sub);
2322 weston_subsurface_commit_from_cache(sub);
2323 } else {
2324 weston_surface_commit(surface);
2325 }
2326
2327 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2328 if (tmp->surface != surface)
2329 weston_subsurface_parent_commit(tmp, 0);
2330 }
2331 }
2332}
2333
2334static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002335weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002336{
2337 struct weston_surface *surface = sub->surface;
2338 struct weston_subsurface *tmp;
2339
Pekka Paalanene67858b2013-04-25 13:57:42 +03002340 /* From now on, commit_from_cache the whole sub-tree, regardless of
2341 * the synchronized mode of each child. This sub-surface or some
2342 * of its ancestors were synchronized, so we are synchronized
2343 * all the way down.
2344 */
2345
2346 if (sub->cached.has_data)
2347 weston_subsurface_commit_from_cache(sub);
2348
2349 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2350 if (tmp->surface != surface)
2351 weston_subsurface_parent_commit(tmp, 1);
2352 }
2353}
2354
2355static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002356weston_subsurface_parent_commit(struct weston_subsurface *sub,
2357 int parent_is_synchronized)
2358{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002359 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002360 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002361 wl_list_for_each(view, &sub->surface->views, surface_link)
2362 weston_view_set_position(view,
2363 sub->position.x,
2364 sub->position.y);
2365
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002366 sub->position.set = 0;
2367 }
2368
2369 if (parent_is_synchronized || sub->synchronized)
2370 weston_subsurface_synchronized_commit(sub);
2371}
2372
2373static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002374subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
2375 int32_t width, int32_t height)
2376{
2377 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002378 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002379
Jason Ekstranda7af7042013-10-12 22:38:11 -05002380 wl_list_for_each(view, &surface->views, surface_link)
2381 weston_view_configure(view,
2382 view->geometry.x + dx,
2383 view->geometry.y + dy,
2384 width, height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002385
2386 /* No need to check parent mappedness, because if parent is not
2387 * mapped, parent is not in a visible layer, so this sub-surface
2388 * will not be drawn either.
2389 */
2390 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002391 /* Cannot call weston_surface_update_transform(),
2392 * because that would call it also for the parent surface,
2393 * which might not be mapped yet. That would lead to
2394 * inconsistent state, where the window could never be
2395 * mapped.
2396 *
2397 * Instead just assing any output, to make
2398 * weston_surface_is_mapped() return true, so that when the
2399 * parent surface does get mapped, this one will get
2400 * included, too. See surface_list_add().
2401 */
2402 assert(!wl_list_empty(&compositor->output_list));
2403 surface->output = container_of(compositor->output_list.next,
2404 struct weston_output, link);
2405 }
2406}
2407
2408static struct weston_subsurface *
2409weston_surface_to_subsurface(struct weston_surface *surface)
2410{
2411 if (surface->configure == subsurface_configure)
2412 return surface->configure_private;
2413
2414 return NULL;
2415}
2416
Pekka Paalanen01388e22013-04-25 13:57:44 +03002417WL_EXPORT struct weston_surface *
2418weston_surface_get_main_surface(struct weston_surface *surface)
2419{
2420 struct weston_subsurface *sub;
2421
2422 while (surface && (sub = weston_surface_to_subsurface(surface)))
2423 surface = sub->parent;
2424
2425 return surface;
2426}
2427
Pekka Paalanene67858b2013-04-25 13:57:42 +03002428static void
2429subsurface_set_position(struct wl_client *client,
2430 struct wl_resource *resource, int32_t x, int32_t y)
2431{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002432 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002433
2434 if (!sub)
2435 return;
2436
2437 sub->position.x = x;
2438 sub->position.y = y;
2439 sub->position.set = 1;
2440}
2441
2442static struct weston_subsurface *
2443subsurface_from_surface(struct weston_surface *surface)
2444{
2445 struct weston_subsurface *sub;
2446
2447 sub = weston_surface_to_subsurface(surface);
2448 if (sub)
2449 return sub;
2450
2451 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2452 if (sub->surface == surface)
2453 return sub;
2454
2455 return NULL;
2456}
2457
2458static struct weston_subsurface *
2459subsurface_sibling_check(struct weston_subsurface *sub,
2460 struct weston_surface *surface,
2461 const char *request)
2462{
2463 struct weston_subsurface *sibling;
2464
2465 sibling = subsurface_from_surface(surface);
2466
2467 if (!sibling) {
2468 wl_resource_post_error(sub->resource,
2469 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2470 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002471 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002472 return NULL;
2473 }
2474
2475 if (sibling->parent != sub->parent) {
2476 wl_resource_post_error(sub->resource,
2477 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2478 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002479 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002480 return NULL;
2481 }
2482
2483 return sibling;
2484}
2485
2486static void
2487subsurface_place_above(struct wl_client *client,
2488 struct wl_resource *resource,
2489 struct wl_resource *sibling_resource)
2490{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002491 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002492 struct weston_surface *surface =
2493 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002494 struct weston_subsurface *sibling;
2495
2496 if (!sub)
2497 return;
2498
2499 sibling = subsurface_sibling_check(sub, surface, "place_above");
2500 if (!sibling)
2501 return;
2502
2503 wl_list_remove(&sub->parent_link_pending);
2504 wl_list_insert(sibling->parent_link_pending.prev,
2505 &sub->parent_link_pending);
2506}
2507
2508static void
2509subsurface_place_below(struct wl_client *client,
2510 struct wl_resource *resource,
2511 struct wl_resource *sibling_resource)
2512{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002513 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002514 struct weston_surface *surface =
2515 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002516 struct weston_subsurface *sibling;
2517
2518 if (!sub)
2519 return;
2520
2521 sibling = subsurface_sibling_check(sub, surface, "place_below");
2522 if (!sibling)
2523 return;
2524
2525 wl_list_remove(&sub->parent_link_pending);
2526 wl_list_insert(&sibling->parent_link_pending,
2527 &sub->parent_link_pending);
2528}
2529
2530static void
2531subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2532{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002533 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002534
2535 if (sub)
2536 sub->synchronized = 1;
2537}
2538
2539static void
2540subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2541{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002542 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002543
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002544 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002545 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002546
2547 /* If sub became effectively desynchronized, flush. */
2548 if (!weston_subsurface_is_synchronized(sub))
2549 weston_subsurface_synchronized_commit(sub);
2550 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002551}
2552
2553static void
2554weston_subsurface_cache_init(struct weston_subsurface *sub)
2555{
2556 pixman_region32_init(&sub->cached.damage);
2557 pixman_region32_init(&sub->cached.opaque);
2558 pixman_region32_init(&sub->cached.input);
2559 wl_list_init(&sub->cached.frame_callback_list);
2560 sub->cached.buffer_ref.buffer = NULL;
2561}
2562
2563static void
2564weston_subsurface_cache_fini(struct weston_subsurface *sub)
2565{
2566 struct weston_frame_callback *cb, *tmp;
2567
2568 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002569 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002570
2571 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2572 pixman_region32_fini(&sub->cached.damage);
2573 pixman_region32_fini(&sub->cached.opaque);
2574 pixman_region32_fini(&sub->cached.input);
2575}
2576
2577static void
2578weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2579{
2580 wl_list_remove(&sub->parent_link);
2581 wl_list_remove(&sub->parent_link_pending);
2582 wl_list_remove(&sub->parent_destroy_listener.link);
2583 sub->parent = NULL;
2584}
2585
2586static void
2587weston_subsurface_destroy(struct weston_subsurface *sub);
2588
2589static void
2590subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2591{
2592 struct weston_subsurface *sub =
2593 container_of(listener, struct weston_subsurface,
2594 surface_destroy_listener);
2595 assert(data == &sub->surface->resource);
2596
2597 /* The protocol object (wl_resource) is left inert. */
2598 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002599 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002600
2601 weston_subsurface_destroy(sub);
2602}
2603
2604static void
2605subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2606{
2607 struct weston_subsurface *sub =
2608 container_of(listener, struct weston_subsurface,
2609 parent_destroy_listener);
2610 assert(data == &sub->parent->resource);
2611 assert(sub->surface != sub->parent);
2612
2613 if (weston_surface_is_mapped(sub->surface))
2614 weston_surface_unmap(sub->surface);
2615
2616 weston_subsurface_unlink_parent(sub);
2617}
2618
2619static void
2620subsurface_resource_destroy(struct wl_resource *resource)
2621{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002622 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002623
2624 if (sub)
2625 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002626}
2627
2628static void
2629subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2630{
2631 wl_resource_destroy(resource);
2632}
2633
2634static void
2635weston_subsurface_link_parent(struct weston_subsurface *sub,
2636 struct weston_surface *parent)
2637{
2638 sub->parent = parent;
2639 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002640 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002641 &sub->parent_destroy_listener);
2642
2643 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2644 wl_list_insert(&parent->subsurface_list_pending,
2645 &sub->parent_link_pending);
2646}
2647
2648static void
2649weston_subsurface_link_surface(struct weston_subsurface *sub,
2650 struct weston_surface *surface)
2651{
2652 sub->surface = surface;
2653 sub->surface_destroy_listener.notify =
2654 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002655 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002656 &sub->surface_destroy_listener);
2657}
2658
2659static void
2660weston_subsurface_destroy(struct weston_subsurface *sub)
2661{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002662 struct weston_view *view, *next;
2663
Pekka Paalanene67858b2013-04-25 13:57:42 +03002664 assert(sub->surface);
2665
2666 if (sub->resource) {
2667 assert(weston_surface_to_subsurface(sub->surface) == sub);
2668 assert(sub->parent_destroy_listener.notify ==
2669 subsurface_handle_parent_destroy);
2670
Jason Ekstranda7af7042013-10-12 22:38:11 -05002671 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2672 weston_view_destroy(view);
2673
Pekka Paalanene67858b2013-04-25 13:57:42 +03002674 if (sub->parent)
2675 weston_subsurface_unlink_parent(sub);
2676
2677 weston_subsurface_cache_fini(sub);
2678
2679 sub->surface->configure = NULL;
2680 sub->surface->configure_private = NULL;
2681 } else {
2682 /* the dummy weston_subsurface for the parent itself */
2683 assert(sub->parent_destroy_listener.notify == NULL);
2684 wl_list_remove(&sub->parent_link);
2685 wl_list_remove(&sub->parent_link_pending);
2686 }
2687
2688 wl_list_remove(&sub->surface_destroy_listener.link);
2689 free(sub);
2690}
2691
2692static const struct wl_subsurface_interface subsurface_implementation = {
2693 subsurface_destroy,
2694 subsurface_set_position,
2695 subsurface_place_above,
2696 subsurface_place_below,
2697 subsurface_set_sync,
2698 subsurface_set_desync
2699};
2700
2701static struct weston_subsurface *
2702weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2703 struct weston_surface *parent)
2704{
2705 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002706 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002707
2708 sub = calloc(1, sizeof *sub);
2709 if (!sub)
2710 return NULL;
2711
Jason Ekstranda7af7042013-10-12 22:38:11 -05002712 wl_list_init(&sub->unused_views);
2713
Jason Ekstranda85118c2013-06-27 20:17:02 -05002714 sub->resource =
2715 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002716 if (!sub->resource) {
2717 free(sub);
2718 return NULL;
2719 }
2720
Jason Ekstranda85118c2013-06-27 20:17:02 -05002721 wl_resource_set_implementation(sub->resource,
2722 &subsurface_implementation,
2723 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002724 weston_subsurface_link_surface(sub, surface);
2725 weston_subsurface_link_parent(sub, parent);
2726 weston_subsurface_cache_init(sub);
2727 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002728
2729 return sub;
2730}
2731
2732/* Create a dummy subsurface for having the parent itself in its
2733 * sub-surface lists. Makes stacking order manipulation easy.
2734 */
2735static struct weston_subsurface *
2736weston_subsurface_create_for_parent(struct weston_surface *parent)
2737{
2738 struct weston_subsurface *sub;
2739
2740 sub = calloc(1, sizeof *sub);
2741 if (!sub)
2742 return NULL;
2743
2744 weston_subsurface_link_surface(sub, parent);
2745 sub->parent = parent;
2746 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2747 wl_list_insert(&parent->subsurface_list_pending,
2748 &sub->parent_link_pending);
2749
2750 return sub;
2751}
2752
2753static void
2754subcompositor_get_subsurface(struct wl_client *client,
2755 struct wl_resource *resource,
2756 uint32_t id,
2757 struct wl_resource *surface_resource,
2758 struct wl_resource *parent_resource)
2759{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002760 struct weston_surface *surface =
2761 wl_resource_get_user_data(surface_resource);
2762 struct weston_surface *parent =
2763 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002764 struct weston_subsurface *sub;
2765 static const char where[] = "get_subsurface: wl_subsurface@";
2766
2767 if (surface == parent) {
2768 wl_resource_post_error(resource,
2769 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2770 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002771 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002772 return;
2773 }
2774
2775 if (weston_surface_to_subsurface(surface)) {
2776 wl_resource_post_error(resource,
2777 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2778 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002779 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002780 return;
2781 }
2782
2783 if (surface->configure) {
2784 wl_resource_post_error(resource,
2785 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2786 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002787 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002788 return;
2789 }
2790
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002791 if (weston_surface_get_main_surface(parent) == surface) {
2792 wl_resource_post_error(resource,
2793 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2794 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002795 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002796 return;
2797 }
2798
Pekka Paalanene67858b2013-04-25 13:57:42 +03002799 /* make sure the parent is in its own list */
2800 if (wl_list_empty(&parent->subsurface_list)) {
2801 if (!weston_subsurface_create_for_parent(parent)) {
2802 wl_resource_post_no_memory(resource);
2803 return;
2804 }
2805 }
2806
2807 sub = weston_subsurface_create(id, surface, parent);
2808 if (!sub) {
2809 wl_resource_post_no_memory(resource);
2810 return;
2811 }
2812
2813 surface->configure = subsurface_configure;
2814 surface->configure_private = sub;
2815}
2816
2817static void
2818subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2819{
2820 wl_resource_destroy(resource);
2821}
2822
2823static const struct wl_subcompositor_interface subcompositor_interface = {
2824 subcompositor_destroy,
2825 subcompositor_get_subsurface
2826};
2827
2828static void
2829bind_subcompositor(struct wl_client *client,
2830 void *data, uint32_t version, uint32_t id)
2831{
2832 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002833 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002834
Jason Ekstranda85118c2013-06-27 20:17:02 -05002835 resource =
2836 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002837 if (resource == NULL) {
2838 wl_client_post_no_memory(client);
2839 return;
2840 }
2841 wl_resource_set_implementation(resource, &subcompositor_interface,
2842 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002843}
2844
2845static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002846weston_compositor_dpms(struct weston_compositor *compositor,
2847 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002848{
2849 struct weston_output *output;
2850
2851 wl_list_for_each(output, &compositor->output_list, link)
2852 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002853 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002854}
2855
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002856WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002857weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002858{
Neil Roberts8b62e202013-09-30 13:14:47 +01002859 uint32_t old_state = compositor->state;
2860
2861 /* The state needs to be changed before emitting the wake
2862 * signal because that may try to schedule a repaint which
2863 * will not work if the compositor is still sleeping */
2864 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2865
2866 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002867 case WESTON_COMPOSITOR_SLEEPING:
2868 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2869 /* fall through */
2870 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002871 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002872 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002873 /* fall through */
2874 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002875 wl_event_source_timer_update(compositor->idle_source,
2876 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002877 }
2878}
2879
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002880WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002881weston_compositor_offscreen(struct weston_compositor *compositor)
2882{
2883 switch (compositor->state) {
2884 case WESTON_COMPOSITOR_OFFSCREEN:
2885 return;
2886 case WESTON_COMPOSITOR_SLEEPING:
2887 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2888 /* fall through */
2889 default:
2890 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2891 wl_event_source_timer_update(compositor->idle_source, 0);
2892 }
2893}
2894
2895WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002896weston_compositor_sleep(struct weston_compositor *compositor)
2897{
2898 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2899 return;
2900
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002901 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002902 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2903 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2904}
2905
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002906static int
2907idle_handler(void *data)
2908{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002909 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002910
2911 if (compositor->idle_inhibit)
2912 return 1;
2913
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002914 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002915 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002916
2917 return 1;
2918}
2919
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002920WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08002921weston_plane_init(struct weston_plane *plane,
2922 struct weston_compositor *ec,
2923 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002924{
2925 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002926 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002927 plane->x = x;
2928 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08002929 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002930
2931 /* Init the link so that the call to wl_list_remove() when releasing
2932 * the plane without ever stacking doesn't lead to a crash */
2933 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002934}
2935
2936WL_EXPORT void
2937weston_plane_release(struct weston_plane *plane)
2938{
Xiong Zhang97116532013-10-23 13:58:31 +08002939 struct weston_view *view;
2940
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002941 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002942 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002943
Xiong Zhang97116532013-10-23 13:58:31 +08002944 wl_list_for_each(view, &plane->compositor->view_list, link) {
2945 if (view->plane == plane)
2946 view->plane = NULL;
2947 }
2948
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002949 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002950}
2951
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002952WL_EXPORT void
2953weston_compositor_stack_plane(struct weston_compositor *ec,
2954 struct weston_plane *plane,
2955 struct weston_plane *above)
2956{
2957 if (above)
2958 wl_list_insert(above->link.prev, &plane->link);
2959 else
2960 wl_list_insert(&ec->plane_list, &plane->link);
2961}
2962
Casey Dahlin9074db52012-04-19 22:50:09 -04002963static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002964{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002965 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002966}
2967
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002968static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002969bind_output(struct wl_client *client,
2970 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002971{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002972 struct weston_output *output = data;
2973 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002974 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002975
Jason Ekstranda85118c2013-06-27 20:17:02 -05002976 resource = wl_resource_create(client, &wl_output_interface,
2977 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002978 if (resource == NULL) {
2979 wl_client_post_no_memory(client);
2980 return;
2981 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002982
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002983 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002984 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002985
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002986 wl_output_send_geometry(resource,
2987 output->x,
2988 output->y,
2989 output->mm_width,
2990 output->mm_height,
2991 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002992 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002993 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002994 if (version >= 2)
2995 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02002996 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002997
2998 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002999 wl_output_send_mode(resource,
3000 mode->flags,
3001 mode->width,
3002 mode->height,
3003 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003004 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003005
3006 if (version >= 2)
3007 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003008}
3009
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003010WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003011weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003012{
Richard Hughes64ddde12013-05-01 21:52:10 +01003013 wl_signal_emit(&output->destroy_signal, output);
3014
Richard Hughesafe690c2013-05-02 10:10:04 +01003015 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003016 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003017 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003018 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003019
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003020 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003021}
3022
Scott Moreau1bad5db2012-08-18 01:04:05 -06003023static void
3024weston_output_compute_transform(struct weston_output *output)
3025{
3026 struct weston_matrix transform;
3027 int flip;
3028
3029 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003030 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003031
3032 switch(output->transform) {
3033 case WL_OUTPUT_TRANSFORM_FLIPPED:
3034 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3035 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3036 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003037 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003038 flip = -1;
3039 break;
3040 default:
3041 flip = 1;
3042 break;
3043 }
3044
3045 switch(output->transform) {
3046 case WL_OUTPUT_TRANSFORM_NORMAL:
3047 case WL_OUTPUT_TRANSFORM_FLIPPED:
3048 transform.d[0] = flip;
3049 transform.d[1] = 0;
3050 transform.d[4] = 0;
3051 transform.d[5] = 1;
3052 break;
3053 case WL_OUTPUT_TRANSFORM_90:
3054 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3055 transform.d[0] = 0;
3056 transform.d[1] = -flip;
3057 transform.d[4] = 1;
3058 transform.d[5] = 0;
3059 break;
3060 case WL_OUTPUT_TRANSFORM_180:
3061 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3062 transform.d[0] = -flip;
3063 transform.d[1] = 0;
3064 transform.d[4] = 0;
3065 transform.d[5] = -1;
3066 break;
3067 case WL_OUTPUT_TRANSFORM_270:
3068 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3069 transform.d[0] = 0;
3070 transform.d[1] = flip;
3071 transform.d[4] = -1;
3072 transform.d[5] = 0;
3073 break;
3074 default:
3075 break;
3076 }
3077
3078 weston_matrix_multiply(&output->matrix, &transform);
3079}
3080
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003081WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003082weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003083{
Scott Moreau850ca422012-05-21 15:21:25 -06003084 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003085 struct weston_matrix camera;
3086 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003087
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003088 weston_matrix_init(&output->matrix);
3089 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003090 -(output->x + output->width / 2.0),
3091 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003092
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003093 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003094 2.0 / output->width,
3095 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003096
3097 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003098
Scott Moreauccbf29d2012-02-22 14:21:41 -07003099 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003100 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003101 weston_matrix_init(&camera);
3102 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003103 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003104 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003105 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003106 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003107 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003108 weston_matrix_multiply(&output->matrix, &modelview);
3109 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003110
Scott Moreauccbf29d2012-02-22 14:21:41 -07003111 output->dirty = 0;
3112}
3113
Scott Moreau1bad5db2012-08-18 01:04:05 -06003114static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003115weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003116{
3117 output->transform = transform;
3118
3119 switch (transform) {
3120 case WL_OUTPUT_TRANSFORM_90:
3121 case WL_OUTPUT_TRANSFORM_270:
3122 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3123 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3124 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003125 output->width = output->current_mode->height;
3126 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003127 break;
3128 case WL_OUTPUT_TRANSFORM_NORMAL:
3129 case WL_OUTPUT_TRANSFORM_180:
3130 case WL_OUTPUT_TRANSFORM_FLIPPED:
3131 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003132 output->width = output->current_mode->width;
3133 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003134 break;
3135 default:
3136 break;
3137 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003138
Hardening57388e42013-09-18 23:56:36 +02003139 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003140 output->width /= scale;
3141 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003142}
3143
Scott Moreauccbf29d2012-02-22 14:21:41 -07003144WL_EXPORT void
3145weston_output_move(struct weston_output *output, int x, int y)
3146{
3147 output->x = x;
3148 output->y = y;
3149
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003150 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003151 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003152 output->width,
3153 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003154}
3155
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003156WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003157weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003158 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003159 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003160{
3161 output->compositor = c;
3162 output->x = x;
3163 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003164 output->mm_width = mm_width;
3165 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003166 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003167 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003168
Alexander Larsson0b135062013-05-28 16:23:36 +02003169 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003170 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003171
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003172 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003173 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003174
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003175 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003176 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003177 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003178 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003179
Casey Dahlin58ba1372012-04-19 22:50:08 -04003180 output->id = ffs(~output->compositor->output_id_pool) - 1;
3181 output->compositor->output_id_pool |= 1 << output->id;
3182
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003183 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003184 wl_global_create(c->wl_display, &wl_output_interface, 2,
3185 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003186 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003187}
3188
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003189WL_EXPORT void
3190weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003191 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003192 wl_fixed_t *x, wl_fixed_t *y)
3193{
3194 wl_fixed_t tx, ty;
3195 wl_fixed_t width, height;
3196
Hardeningff39efa2013-09-18 23:56:35 +02003197 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3198 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003199
3200 switch(output->transform) {
3201 case WL_OUTPUT_TRANSFORM_NORMAL:
3202 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003203 tx = device_x;
3204 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003205 break;
3206 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003207 tx = device_y;
3208 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003209 break;
3210 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003211 tx = width - device_x;
3212 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003213 break;
3214 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003215 tx = width - device_y;
3216 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003217 break;
3218 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003219 tx = width - device_x;
3220 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003221 break;
3222 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003223 tx = width - device_y;
3224 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003225 break;
3226 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003227 tx = device_x;
3228 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003229 break;
3230 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003231 tx = device_y;
3232 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003233 break;
3234 }
3235
Hardeningff39efa2013-09-18 23:56:35 +02003236 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3237 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003238}
3239
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003240static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003241compositor_bind(struct wl_client *client,
3242 void *data, uint32_t version, uint32_t id)
3243{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003244 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003245 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003246
Jason Ekstranda85118c2013-06-27 20:17:02 -05003247 resource = wl_resource_create(client, &wl_compositor_interface,
3248 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003249 if (resource == NULL) {
3250 wl_client_post_no_memory(client);
3251 return;
3252 }
3253
3254 wl_resource_set_implementation(resource, &compositor_interface,
3255 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003256}
3257
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003258static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003259log_uname(void)
3260{
3261 struct utsname usys;
3262
3263 uname(&usys);
3264
3265 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3266 usys.version, usys.machine);
3267}
3268
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003269WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003270weston_environment_get_fd(const char *env)
3271{
3272 char *e, *end;
3273 int fd, flags;
3274
3275 e = getenv(env);
3276 if (!e)
3277 return -1;
3278 fd = strtol(e, &end, 0);
3279 if (*end != '\0')
3280 return -1;
3281
3282 flags = fcntl(fd, F_GETFD);
3283 if (flags == -1)
3284 return -1;
3285
3286 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3287 unsetenv(env);
3288
3289 return fd;
3290}
3291
3292WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003293weston_compositor_init(struct weston_compositor *ec,
3294 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003295 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003296 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003297{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003298 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003299 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003300 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003301
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003302 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003303 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003304 wl_signal_init(&ec->destroy_signal);
3305 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003306 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003307 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003308 wl_signal_init(&ec->idle_signal);
3309 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003310 wl_signal_init(&ec->show_input_panel_signal);
3311 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003312 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003313 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003314 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003315 wl_signal_init(&ec->session_signal);
3316 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003317
Casey Dahlin58ba1372012-04-19 22:50:08 -04003318 ec->output_id_pool = 0;
3319
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003320 if (!wl_global_create(display, &wl_compositor_interface, 3,
3321 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003322 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003323
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003324 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3325 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003326 return -1;
3327
Jason Ekstranda7af7042013-10-12 22:38:11 -05003328 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003329 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003330 wl_list_init(&ec->layer_list);
3331 wl_list_init(&ec->seat_list);
3332 wl_list_init(&ec->output_list);
3333 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003334 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003335 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003336 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003337 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003338 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003339
Xiong Zhang97116532013-10-23 13:58:31 +08003340 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003341 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003342
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003343 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3344 weston_config_section_get_string(s, "keymap_rules",
3345 (char **) &xkb_names.rules, NULL);
3346 weston_config_section_get_string(s, "keymap_model",
3347 (char **) &xkb_names.model, NULL);
3348 weston_config_section_get_string(s, "keymap_layout",
3349 (char **) &xkb_names.layout, NULL);
3350 weston_config_section_get_string(s, "keymap_variant",
3351 (char **) &xkb_names.variant, NULL);
3352 weston_config_section_get_string(s, "keymap_options",
3353 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003354
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003355 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3356 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003357
3358 ec->ping_handler = NULL;
3359
3360 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003361 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003362
3363 wl_data_device_manager_init(ec->wl_display);
3364
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003365 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003366
Daniel Stone725c2c32012-06-22 14:04:36 +01003367 loop = wl_display_get_event_loop(ec->wl_display);
3368 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3369 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3370
3371 ec->input_loop = wl_event_loop_create();
3372
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003373 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3374 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3375
3376 weston_compositor_schedule_repaint(ec);
3377
Daniel Stone725c2c32012-06-22 14:04:36 +01003378 return 0;
3379}
3380
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003381WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003382weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003383{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003384 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003385
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003386 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003387 if (ec->input_loop_source)
3388 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003389
Matt Roper361d2ad2011-08-29 13:52:23 -07003390 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003391 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003392 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003393
Daniel Stone325fc2d2012-05-30 16:31:58 +01003394 weston_binding_list_destroy_all(&ec->key_binding_list);
3395 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003396 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003397 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003398 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003399
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003400 weston_plane_release(&ec->primary_plane);
3401
Jonas Ådahlc97af922012-03-28 22:36:09 +02003402 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003403
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003404 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003405}
3406
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003407WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003408weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3409 const struct weston_pointer_grab_interface *interface)
3410{
3411 struct weston_seat *seat;
3412
3413 ec->default_pointer_grab = interface;
3414 wl_list_for_each(seat, &ec->seat_list, link) {
3415 if (seat->pointer) {
3416 weston_pointer_set_default_grab(seat->pointer,
3417 interface);
3418 }
3419 }
3420}
3421
3422WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003423weston_version(int *major, int *minor, int *micro)
3424{
3425 *major = WESTON_VERSION_MAJOR;
3426 *minor = WESTON_VERSION_MINOR;
3427 *micro = WESTON_VERSION_MICRO;
3428}
3429
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003430static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003431 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003432 const char *desc;
3433} capability_strings[] = {
3434 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003435 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003436};
3437
3438static void
3439weston_compositor_log_capabilities(struct weston_compositor *compositor)
3440{
3441 unsigned i;
3442 int yes;
3443
3444 weston_log("Compositor capabilities:\n");
3445 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3446 yes = compositor->capabilities & capability_strings[i].bit;
3447 weston_log_continue(STAMP_SPACE "%s %s\n",
3448 capability_strings[i].desc,
3449 yes ? "yes" : "no");
3450 }
3451}
3452
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003453static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003454{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003455 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003456
Martin Minarik6d118362012-06-07 18:01:59 +02003457 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003458 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003459
3460 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003461}
3462
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003463#ifdef HAVE_LIBUNWIND
3464
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003465static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003466print_backtrace(void)
3467{
3468 unw_cursor_t cursor;
3469 unw_context_t context;
3470 unw_word_t off;
3471 unw_proc_info_t pip;
3472 int ret, i = 0;
3473 char procname[256];
3474 const char *filename;
3475 Dl_info dlinfo;
3476
3477 pip.unwind_info = NULL;
3478 ret = unw_getcontext(&context);
3479 if (ret) {
3480 weston_log("unw_getcontext: %d\n", ret);
3481 return;
3482 }
3483
3484 ret = unw_init_local(&cursor, &context);
3485 if (ret) {
3486 weston_log("unw_init_local: %d\n", ret);
3487 return;
3488 }
3489
3490 ret = unw_step(&cursor);
3491 while (ret > 0) {
3492 ret = unw_get_proc_info(&cursor, &pip);
3493 if (ret) {
3494 weston_log("unw_get_proc_info: %d\n", ret);
3495 break;
3496 }
3497
3498 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3499 if (ret && ret != -UNW_ENOMEM) {
3500 if (ret != -UNW_EUNSPEC)
3501 weston_log("unw_get_proc_name: %d\n", ret);
3502 procname[0] = '?';
3503 procname[1] = 0;
3504 }
3505
3506 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3507 *dlinfo.dli_fname)
3508 filename = dlinfo.dli_fname;
3509 else
3510 filename = "?";
3511
3512 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3513 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3514
3515 ret = unw_step(&cursor);
3516 if (ret < 0)
3517 weston_log("unw_step: %d\n", ret);
3518 }
3519}
3520
3521#else
3522
3523static void
3524print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003525{
3526 void *buffer[32];
3527 int i, count;
3528 Dl_info info;
3529
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003530 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3531 for (i = 0; i < count; i++) {
3532 dladdr(buffer[i], &info);
3533 weston_log(" [%016lx] %s (%s)\n",
3534 (long) buffer[i],
3535 info.dli_sname ? info.dli_sname : "--",
3536 info.dli_fname);
3537 }
3538}
3539
3540#endif
3541
3542static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003543on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003544{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003545 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003546 * then call the backend restore function, which will switch
3547 * back to the vt we launched from or ungrab X etc and then
3548 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003549 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003550 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003551 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003552
Peter Maatmane5b42e42013-03-27 22:38:53 +01003553 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003554
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003555 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003556
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003557 segv_compositor->restore(segv_compositor);
3558
3559 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003560}
3561
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003562WL_EXPORT void *
3563weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003564{
3565 char path[PATH_MAX];
3566 void *module, *init;
3567
3568 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003569 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003570 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003571 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003572
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003573 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3574 if (module) {
3575 weston_log("Module '%s' already loaded\n", path);
3576 dlclose(module);
3577 return NULL;
3578 }
3579
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003580 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003581 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003582 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003583 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003584 return NULL;
3585 }
3586
3587 init = dlsym(module, entrypoint);
3588 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003589 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003590 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003591 return NULL;
3592 }
3593
3594 return init;
3595}
3596
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003597static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003598load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003599 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003600{
3601 const char *p, *end;
3602 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003603 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003604 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003605
3606 if (modules == NULL)
3607 return 0;
3608
3609 p = modules;
3610 while (*p) {
3611 end = strchrnul(p, ',');
3612 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003613 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003614 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003615 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003616 p = end;
3617 while (*p == ',')
3618 p++;
3619
3620 }
3621
3622 return 0;
3623}
3624
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003625static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003626 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3627
3628static const char xdg_wrong_message[] =
3629 "fatal: environment variable XDG_RUNTIME_DIR\n"
3630 "is set to \"%s\", which is not a directory.\n";
3631
3632static const char xdg_wrong_mode_message[] =
3633 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3634 "correctly. Unix access mode must be 0700 but is %o,\n"
3635 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003636 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003637
3638static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003639 "Refer to your distribution on how to get it, or\n"
3640 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3641 "on how to implement it.\n";
3642
Martin Minarik37032f82012-06-18 20:15:18 +02003643static void
3644verify_xdg_runtime_dir(void)
3645{
3646 char *dir = getenv("XDG_RUNTIME_DIR");
3647 struct stat s;
3648
3649 if (!dir) {
3650 weston_log(xdg_error_message);
3651 weston_log_continue(xdg_detail_message);
3652 exit(EXIT_FAILURE);
3653 }
3654
3655 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3656 weston_log(xdg_wrong_message, dir);
3657 weston_log_continue(xdg_detail_message);
3658 exit(EXIT_FAILURE);
3659 }
3660
3661 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3662 weston_log(xdg_wrong_mode_message,
3663 dir, s.st_mode & 0777, s.st_uid);
3664 weston_log_continue(xdg_detail_message);
3665 }
3666}
3667
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003668static int
3669usage(int error_code)
3670{
3671 fprintf(stderr,
3672 "Usage: weston [OPTIONS]\n\n"
3673 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3674 "Weston supports multiple backends, and depending on which backend is in use\n"
3675 "different options will be accepted.\n\n"
3676
3677
3678 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003679 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003680 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003681 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3682 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003683 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003684 " -S, --socket=NAME\tName of socket to listen on\n"
3685 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003686 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003687 " --log==FILE\t\tLog to the given file\n"
3688 " -h, --help\t\tThis help message\n\n");
3689
3690 fprintf(stderr,
3691 "Options for drm-backend.so:\n\n"
3692 " --connector=ID\tBring up only this connector\n"
3693 " --seat=SEAT\t\tThe seat that weston should run on\n"
3694 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003695 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003696 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3697
3698 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003699 "Options for fbdev-backend.so:\n\n"
3700 " --tty=TTY\t\tThe tty to use\n"
3701 " --device=DEVICE\tThe framebuffer device to use\n\n");
3702
3703 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003704 "Options for x11-backend.so:\n\n"
3705 " --width=WIDTH\t\tWidth of X window\n"
3706 " --height=HEIGHT\tHeight of X window\n"
3707 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003708 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003709 " --output-count=COUNT\tCreate multiple outputs\n"
3710 " --no-input\t\tDont create input devices\n\n");
3711
3712 fprintf(stderr,
3713 "Options for wayland-backend.so:\n\n"
3714 " --width=WIDTH\t\tWidth of Wayland surface\n"
3715 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06003716 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06003717 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003718 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05003719 " --output-count=COUNT\tCreate multiple outputs\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003720 " --display=DISPLAY\tWayland display to connect to\n\n");
3721
Pekka Paalanene31e0532013-05-22 18:03:07 +03003722#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3723 fprintf(stderr,
3724 "Options for rpi-backend.so:\n\n"
3725 " --tty=TTY\t\tThe tty to use\n"
3726 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3727 " --transform=TR\tThe output transformation, TR is one of:\n"
3728 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3729 "\n");
3730#endif
3731
Hardeningc077a842013-07-08 00:51:35 +02003732#if defined(BUILD_RDP_COMPOSITOR)
3733 fprintf(stderr,
3734 "Options for rdp-backend.so:\n\n"
3735 " --width=WIDTH\t\tWidth of desktop\n"
3736 " --height=HEIGHT\tHeight of desktop\n"
3737 " --extra-modes=MODES\t\n"
3738 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3739 " --address=ADDR\tThe address to bind\n"
3740 " --port=PORT\tThe port to listen on\n"
3741 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3742 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3743 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3744 "\n");
3745#endif
3746
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003747 exit(error_code);
3748}
3749
Peter Maatmane5b42e42013-03-27 22:38:53 +01003750static void
3751catch_signals(void)
3752{
3753 struct sigaction action;
3754
3755 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3756 action.sa_sigaction = on_caught_signal;
3757 sigemptyset(&action.sa_mask);
3758 sigaction(SIGSEGV, &action, NULL);
3759 sigaction(SIGABRT, &action, NULL);
3760}
3761
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003762int main(int argc, char *argv[])
3763{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003764 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003765 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003766 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003767 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003768 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003769 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003770 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003771 int *argc, char *argv[],
3772 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003773 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003774 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003775 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05003776 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003777 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003778 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003779 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003780 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003781 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003782 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003783 struct weston_config *config;
3784 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003785
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003786 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003787 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05003788 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003789 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3790 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003791 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003792 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003793 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003794 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003795 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003796
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003797 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003798
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003799 if (help)
3800 usage(EXIT_SUCCESS);
3801
Scott Moreau12245142012-08-29 15:15:58 -06003802 if (version) {
3803 printf(PACKAGE_STRING "\n");
3804 return EXIT_SUCCESS;
3805 }
3806
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003807 weston_log_file_open(log);
3808
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003809 weston_log("%s\n"
3810 STAMP_SPACE "%s\n"
3811 STAMP_SPACE "Bug reports to: %s\n"
3812 STAMP_SPACE "Build: %s\n",
3813 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003814 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003815 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003816
Martin Minarik37032f82012-06-18 20:15:18 +02003817 verify_xdg_runtime_dir();
3818
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003819 display = wl_display_create();
3820
Tiago Vignatti2116b892011-08-08 05:52:59 -07003821 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003822 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3823 display);
3824 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3825 display);
3826 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3827 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003828
3829 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003830 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3831 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003832
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003833 config = weston_config_parse("weston.ini");
3834 if (config != NULL) {
3835 weston_log("Using config file '%s'\n",
3836 weston_config_get_full_path(config));
3837 } else {
3838 weston_log("Starting with no config file.\n");
3839 }
3840 section = weston_config_get_section(config, "core", NULL, NULL);
3841
3842 weston_config_section_get_string(section, "backend", &backend, NULL);
3843 if (option_backend) {
3844 backend = option_backend;
3845 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003846 if (!backend) {
3847 if (getenv("WAYLAND_DISPLAY"))
3848 backend = "wayland-backend.so";
3849 else if (getenv("DISPLAY"))
3850 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003851 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003852 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003853 }
3854
Jason Ekstranda985da42013-08-22 17:28:03 -05003855 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003856
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003857 backend_init = weston_load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003858 if (!backend_init)
3859 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003860
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003861 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003862 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003863 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003864 exit(EXIT_FAILURE);
3865 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003866
Peter Maatmane5b42e42013-03-27 22:38:53 +01003867 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003868 segv_compositor = ec;
3869
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003870 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003871 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003872
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003873 setenv("WAYLAND_DISPLAY", socket_name, 1);
3874
Jason Ekstranda985da42013-08-22 17:28:03 -05003875 if (!shell)
3876 weston_config_section_get_string(section, "shell", &shell,
3877 "desktop-shell.so");
3878 if (load_modules(ec, shell, &argc, argv) < 0)
3879 goto out;
3880
Ossama Othmana50e6e42013-05-14 09:48:26 -07003881 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003882 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003883 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003884 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003885
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003886 for (i = 1; i < argc; i++)
3887 weston_log("fatal: unhandled option: %s\n", argv[i]);
3888 if (argc > 1) {
3889 ret = EXIT_FAILURE;
3890 goto out;
3891 }
3892
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003893 weston_compositor_log_capabilities(ec);
3894
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003895 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003896 weston_log("fatal: failed to add socket: %m\n");
3897 ret = EXIT_FAILURE;
3898 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003899 }
3900
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003901 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003902
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003903 wl_display_run(display);
3904
3905 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003906 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003907 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003908
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003909 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003910
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003911 for (i = ARRAY_LENGTH(signals); i;)
3912 wl_event_source_remove(signals[--i]);
3913
Daniel Stone855028f2012-05-01 20:37:10 +01003914 weston_compositor_xkb_destroy(ec);
3915
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003916 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003917 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003918
Martin Minarik19e6f262012-06-07 13:08:46 +02003919 weston_log_file_close();
3920
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003921 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003922}